home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / informat.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  13.4 KB  |  412 lines

  1. ;; Info support functions package for Emacs
  2. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (require 'info)
  21.  
  22. (defun Info-tagify ()
  23.   "Create or update Info-file tag table in current buffer."
  24.   (interactive)
  25.   ;; Save and restore point and restrictions.
  26.   ;; save-restrictions would not work
  27.   ;; because it records the old max relative to the end.
  28.   ;; We record it relative to the beginning.
  29.   (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))
  30.   (let ((omin (point-min))
  31.     (omax (point-max))
  32.     (nomax (= (point-max) (1+ (buffer-size))))
  33.     (opoint (point)))
  34.     (unwind-protect
  35.     (progn
  36.       (widen)
  37.       (goto-char (point-min))
  38.       (if (search-forward "\^_\nIndirect:\n" nil t)
  39.           (message "Cannot tagify split info file")
  40.         (let ((regexp "Node:[ \t]*\\([^,\n\t]\\)*[,\t\n]")
  41.           (case-fold-search t)
  42.           list)
  43.           (while (search-forward "\n\^_" nil t)
  44.         (forward-line 1)
  45.         (let ((beg (point)))
  46.           (forward-line 1)
  47.           (if (re-search-backward regexp beg t)
  48.               (setq list
  49.                 (cons (list (buffer-substring
  50.                       (match-beginning 1)
  51.                       (match-end 1))
  52.                     beg)
  53.                   list)))))
  54.           (goto-char (point-max))
  55.           (forward-line -8)
  56.           (let ((buffer-read-only nil))
  57.         (if (search-forward "\^_\nEnd tag table\n" nil t)
  58.             (let ((end (point)))
  59.               (search-backward "\nTag table:\n")
  60.               (beginning-of-line)
  61.               (delete-region (point) end)))
  62.         (goto-char (point-max))
  63.         (insert "\^_\f\nTag table:\n")
  64.         (move-marker Info-tag-table-marker (point))
  65.         (setq list (nreverse list))
  66.         (while list
  67.           (insert "Node: " (car (car list)) ?\177)
  68.           (princ (car (cdr (car list))) (current-buffer))
  69.           (insert ?\n)
  70.           (setq list (cdr list)))
  71.         (insert "\^_\nEnd tag table\n")))))
  72.       (goto-char opoint)
  73.       (narrow-to-region omin (if nomax (1+ (buffer-size))
  74.                    (min omax (point-max))))))
  75.   (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
  76.  
  77. (defun Info-split ()
  78.   "Split an info file into an indirect file plus bounded-size subfiles.
  79. Each subfile will be up to 50000 characters plus one node.
  80.  
  81. To use this command, first visit a large Info file that has a tag table.
  82. The buffer is modified into a (small) indirect info file
  83. which should be saved in place of the original visited file.
  84.  
  85. The subfiles are written in the same directory the original file is in,
  86. with names generated by appending `-' and a number to the original file name.
  87.  
  88. The indirect file still functions as an Info file, but it contains
  89. just the tag table and a directory of subfiles."
  90.   (interactive)
  91.   (if (< (buffer-size) 70000)
  92.       (error "This is too small to be worth splitting"))
  93.   (goto-char (point-min))
  94.   (search-forward "\^_")
  95.   (forward-char -1)
  96.   (let ((start (point))
  97.     (chars-deleted 0)
  98.     subfiles
  99.     (subfile-number 1)
  100.     (case-fold-search t)
  101.     (filename (file-name-sans-versions buffer-file-name)))
  102.     (goto-char (point-max))
  103.     (forward-line -8)
  104.     (setq buffer-read-only nil)
  105.     (or (search-forward "\^_\nEnd tag table\n" nil t)
  106.     (error "Tag table required; use M-x Info-tagify"))
  107.     (search-backward "\nTag table:\n")
  108.     (if (looking-at "\nTag table:\n\^_")
  109.     (error "Tag table is just a skeleton; use M-x Info-tagify"))
  110.     (beginning-of-line)
  111.     (forward-char 1)
  112.     (save-restriction
  113.       (narrow-to-region (point-min) (point))
  114.       (goto-char (point-min))
  115.       (while (< (1+ (point)) (point-max))
  116.     (goto-char (min (+ (point) 50000) (point-max)))
  117.     (search-forward "\^_" nil 'move)
  118.     (setq subfiles
  119.           (cons (list (+ start chars-deleted)
  120.               (concat (file-name-nondirectory filename)
  121.                   (format "-%d" subfile-number)))
  122.             subfiles))
  123.     ;; Put a newline at end of split file, to make Unix happier.
  124.     (insert "\n")
  125.     (write-region (point-min) (point)
  126.               (concat filename (format "-%d" subfile-number)))
  127.     (delete-region (1- (point)) (point))
  128.     ;; Back up over the final ^_.
  129.     (forward-char -1)
  130.     (setq chars-deleted (+ chars-deleted (- (point) start)))
  131.     (delete-region start (point))
  132.     (setq subfile-number (1+ subfile-number))))
  133.     (while subfiles
  134.       (goto-char start)
  135.       (insert (nth 1 (car subfiles))
  136.           (format ": %d" (car (car subfiles)))
  137.           "\n")
  138.       (setq subfiles (cdr subfiles)))
  139.     (goto-char start)
  140.     (insert "\^_\nIndirect:\n")
  141.     (search-forward "\nTag Table:\n")
  142.     (insert "(Indirect)\n")))
  143.  
  144. (defun Info-validate ()
  145.   "Check current buffer for validity as an Info file.
  146. Check that every node pointer points to an existing node."
  147.   (interactive)
  148.   (save-excursion
  149.     (save-restriction
  150.       (widen)
  151.       (goto-char (point-min))
  152.       (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
  153.       (error "Don't yet know how to validate indirect info files: \"%s\""
  154.          (buffer-name (current-buffer))))
  155.       (goto-char (point-min))
  156.       (let ((allnodes '(("*")))
  157.         (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  158.         (case-fold-search t)
  159.         (tags-losing nil)
  160.         (lossages ()))
  161.     (while (search-forward "\n\^_" nil t)
  162.       (forward-line 1)
  163.       (let ((beg (point)))
  164.         (forward-line 1)
  165.         (if (re-search-backward regexp beg t)
  166.         (let ((name (downcase
  167.                   (buffer-substring
  168.                     (match-beginning 1)
  169.                 (progn
  170.                   (goto-char (match-end 1))
  171.                   (skip-chars-backward " \t")
  172.                   (point))))))
  173.           (if (assoc name allnodes)
  174.               (setq lossages
  175.                 (cons (list name "Duplicate node-name" nil)
  176.                   lossages))
  177.               (setq allnodes
  178.                 (cons (list name
  179.                     (progn
  180.                       (end-of-line)
  181.                       (and (re-search-backward
  182.                         "prev[ious]*:" beg t)
  183.                            (progn
  184.                          (goto-char (match-end 0))
  185.                          (downcase
  186.                            (Info-following-node-name)))))
  187.                     beg)
  188.                   allnodes)))))))
  189.     (goto-char (point-min))
  190.     (while (search-forward "\n\^_" nil t)
  191.       (forward-line 1)
  192.       (let ((beg (point))
  193.         thisnode next)
  194.         (forward-line 1)
  195.         (if (re-search-backward regexp beg t)
  196.         (save-restriction
  197.           (search-forward "\n\^_" nil 'move)
  198.           (narrow-to-region beg (point))
  199.           (setq thisnode (downcase
  200.                    (buffer-substring
  201.                      (match-beginning 1)
  202.                      (progn
  203.                        (goto-char (match-end 1))
  204.                        (skip-chars-backward " \t")
  205.                        (point)))))
  206.           (end-of-line)
  207.           (and (search-backward "next:" nil t)
  208.                (setq next (Info-validate-node-name "invalid Next"))
  209.                (assoc next allnodes)
  210.                (if (equal (car (cdr (assoc next allnodes)))
  211.                   thisnode)
  212.                ;; allow multiple `next' pointers to one node
  213.                (let ((tem lossages))
  214.                  (while tem
  215.                    (if (and (equal (car (cdr (car tem)))
  216.                            "should have Previous")
  217.                     (equal (car (car tem))
  218.                            next))
  219.                    (setq lossages (delq (car tem) lossages)))
  220.                    (setq tem (cdr tem))))
  221.              (setq lossages
  222.                    (cons (list next
  223.                        "should have Previous"
  224.                        thisnode)
  225.                      lossages))))
  226.           (end-of-line)
  227.           (if (re-search-backward "prev[ious]*:" nil t)
  228.               (Info-validate-node-name "invalid Previous"))
  229.           (end-of-line)
  230.           (if (search-backward "up:" nil t)
  231.               (Info-validate-node-name "invalid Up"))
  232.           (if (re-search-forward "\n* Menu:" nil t)
  233.               (while (re-search-forward "\n\\* " nil t)
  234.             (Info-validate-node-name
  235.               (concat "invalid menu item "
  236.                   (buffer-substring (point)
  237.                             (save-excursion
  238.                               (skip-chars-forward "^:")
  239.                               (point))))
  240.               (Info-extract-menu-node-name))))
  241.           (goto-char (point-min))
  242.           (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
  243.             (goto-char (+ (match-beginning 0) 5))
  244.             (skip-chars-forward " \n")
  245.             (Info-validate-node-name
  246.              (concat "invalid reference "
  247.                  (buffer-substring (point)
  248.                            (save-excursion
  249.                          (skip-chars-forward "^:")
  250.                          (point))))
  251.              (Info-extract-menu-node-name "Bad format cross-reference")))))))
  252.     (setq tags-losing (not (Info-validate-tags-table)))
  253.     (if (or lossages tags-losing)
  254.         (with-output-to-temp-buffer " *problems in info file*"
  255.           (while lossages
  256.         (princ "In node \"")
  257.         (princ (car (car lossages)))
  258.         (princ "\", ")
  259.         (let ((tem (nth 1 (car lossages))))
  260.           (cond ((string-match "\n" tem)
  261.              (princ (substring tem 0 (match-beginning 0)))
  262.              (princ "..."))
  263.             (t
  264.              (princ tem))))
  265.         (if (nth 2 (car lossages))
  266.             (progn
  267.               (princ ": ")
  268.               (let ((tem (nth 2 (car lossages))))
  269.             (cond ((string-match "\n" tem)
  270.                    (princ (substring tem 0 (match-beginning 0)))
  271.                    (princ "..."))
  272.                   (t
  273.                    (princ tem))))))
  274.         (terpri)
  275.         (setq lossages (cdr lossages)))
  276.           (if tags-losing (princ "\nTags table must be recomputed\n")))
  277.       ;; Here if info file is valid.
  278.       ;; If we already made a list of problems, clear it out.
  279.       (save-excursion
  280.         (if (get-buffer " *problems in info file*")
  281.         (progn
  282.           (set-buffer " *problems in info file*")
  283.           (kill-buffer (current-buffer)))))
  284.       (message "File appears valid"))))))
  285.  
  286. (defun Info-validate-node-name (kind &optional name)
  287.   (if name
  288.       nil
  289.     (goto-char (match-end 0))
  290.     (skip-chars-forward " \t")
  291.     (if (= (following-char) ?\()
  292.     nil
  293.       (setq name
  294.         (buffer-substring
  295.          (point)
  296.          (progn
  297.           (skip-chars-forward "^,\t\n")
  298.           (skip-chars-backward " ")
  299.           (point))))))
  300.   (if (null name)
  301.       nil
  302.     (setq name (downcase name))
  303.     (or (and (> (length name) 0) (= (aref name 0) ?\())
  304.     (assoc name allnodes)
  305.     (setq lossages
  306.           (cons (list thisnode kind name) lossages))))
  307.   name)
  308.  
  309. (defun Info-validate-tags-table ()
  310.   (goto-char (point-min))
  311.   (if (not (search-forward "\^_\nEnd tag table\n" nil t))
  312.       t
  313.     (not (catch 'losing
  314.        (let* ((end (match-beginning 0))
  315.           (start (progn (search-backward "\nTag table:\n")
  316.                 (1- (match-end 0))))
  317.           tem)
  318.          (setq tem allnodes)
  319.          (while tem
  320.            (goto-char start)
  321.            (or (equal (car (car tem)) "*")
  322.            (search-forward (concat "Node: "
  323.                        (car (car tem))
  324.                        "\177")
  325.                    end t)
  326.            (throw 'losing 'x))
  327.            (setq tem (cdr tem)))
  328.          (goto-char (1+ start))
  329.          (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
  330.            (setq tem (downcase (buffer-substring
  331.                      (match-beginning 1)
  332.                      (match-end 1))))
  333.            (setq tem (assoc tem allnodes))
  334.            (if (or (not tem)
  335.                (< 1000 (progn
  336.                  (goto-char (match-beginning 2))
  337.                  (setq tem (- (car (cdr (cdr tem)))
  338.                           (read (current-buffer))))
  339.                  (if (> tem 0) tem (- tem)))))
  340.            (throw 'losing 'y)))
  341.          (forward-line 1))
  342.        (or (looking-at "End tag table\n")
  343.            (throw 'losing 'z))
  344.        nil))))
  345.  
  346. (defun batch-info-validate ()
  347.   "Runs  Info-validate  on the files remaining on the command line.
  348. Must be used only with -batch, and kills emacs on completion.
  349. Each file will be processed even if an error occurred previously.
  350. For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
  351.   (if (not noninteractive)
  352.       (error "batch-info-validate may only be used -batch."))
  353.   (let ((version-control t)
  354.     (auto-save-default nil)
  355.     (find-file-run-dired nil)
  356.     (kept-old-versions 259259)
  357.     (kept-new-versions 259259))
  358.     (let ((error 0)
  359.       file
  360.       (files ()))
  361.       (while command-line-args-left
  362.     (setq file (expand-file-name (car command-line-args-left)))
  363.     (cond ((not (file-exists-p file))
  364.            (message ">> %s does not exist!" file)
  365.            (setq error 1
  366.              command-line-args-left (cdr command-line-args-left))) 
  367.           ((file-directory-p file)
  368.            (setq command-line-args-left (nconc (directory-files file)
  369.                           (cdr command-line-args-left))))
  370.           (t
  371.            (setq files (cons file files)
  372.              command-line-args-left (cdr command-line-args-left)))))
  373.       (while files
  374.     (setq file (car files)
  375.           files (cdr files))
  376.     (let ((lose nil))
  377.       (condition-case err
  378.           (progn
  379.         (if buffer-file-name (kill-buffer (current-buffer)))
  380.         (find-file file)
  381.         (buffer-disable-undo (current-buffer))
  382.         (set-buffer-modified-p nil)
  383.         (fundamental-mode)
  384.         (let ((case-fold-search nil))
  385.           (goto-char (point-max))
  386.           (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
  387.              (message "%s already tagified" file))
  388.             ((< (point-max) 30000)
  389.              (message "%s too small to bother tagifying" file))
  390.             (t
  391.              (Info-tagify file))))
  392.         (let ((loss-name " *problems in info file*"))
  393.           (message "Checking validity of info file %s..." file)
  394.           (if (get-buffer loss-name)
  395.               (kill-buffer loss-name))
  396.           (Info-validate)
  397.           (if (not (get-buffer loss-name))
  398.               nil ;(message "Checking validity of info file %s... OK" file)
  399.             (message "----------------------------------------------------------------------")
  400.             (message ">> PROBLEMS IN INFO FILE %s" file)
  401.             (save-excursion
  402.               (set-buffer loss-name)
  403.               (princ (buffer-substring (point-min) (point-max))))
  404.             (message "----------------------------------------------------------------------")
  405.             (setq error 1 lose t)))
  406.         (if (and (buffer-modified-p)
  407.              (not lose))
  408.             (progn (message "Saving modified %s" file)
  409.                (save-buffer))))
  410.         (error (message ">> Error: %s" (prin1-to-string err))))))
  411.       (kill-emacs error))))
  412.