home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / AP5 / TEXINFO.TGZ / TEXINFO.tar / usr / share / emacs / site-lisp / informat.el < prev    next >
Lisp/Scheme  |  1996-01-14  |  14KB  |  430 lines

  1. ;;; informat.el --- info support functions package for Emacs
  2.  
  3. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: help
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Code:
  26.  
  27. (require 'info)
  28.  
  29. ;;;###autoload
  30. (defun Info-tagify ()
  31.   "Create or update Info-file tag table in current buffer."
  32.   (interactive)
  33.   ;; Save and restore point and restrictions.
  34.   ;; save-restrictions would not work
  35.   ;; because it records the old max relative to the end.
  36.   ;; We record it relative to the beginning.
  37.   (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))
  38.   (let ((omin (point-min))
  39.     (omax (point-max))
  40.     (nomax (= (point-max) (1+ (buffer-size))))
  41.     (opoint (point)))
  42.     (unwind-protect
  43.     (progn
  44.       (widen)
  45.       (goto-char (point-min))
  46.       (if (search-forward "\^_\nIndirect:\n" nil t)
  47.           (message "Cannot tagify split info file")
  48.         (let ((regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  49.           (case-fold-search t)
  50.           list)
  51.           (while (search-forward "\n\^_" nil t)
  52.         ;; We want the 0-origin character position of the ^_.
  53.         ;; That is the same as the Emacs (1-origin) position
  54.         ;; of the newline before it.
  55.         (let ((beg (match-beginning 0)))
  56.           (forward-line 2)
  57.           (if (re-search-backward regexp beg t)
  58.               (setq list
  59.                 (cons (list (buffer-substring-no-properties
  60.                       (match-beginning 1)
  61.                       (match-end 1))
  62.                     beg)
  63.                   list)))))
  64.           (goto-char (point-max))
  65.           (forward-line -8)
  66.           (let ((buffer-read-only nil))
  67.         (if (search-forward "\^_\nEnd tag table\n" nil t)
  68.             (let ((end (point)))
  69.               (search-backward "\nTag table:\n")
  70.               (beginning-of-line)
  71.               (delete-region (point) end)))
  72.         (goto-char (point-max))
  73.         (insert "\^_\f\nTag table:\n")
  74.         (move-marker Info-tag-table-marker (point))
  75.         (setq list (nreverse list))
  76.         (while list
  77.           (insert "Node: " (car (car list)) ?\177)
  78.           (princ (car (cdr (car list))) (current-buffer))
  79.           (insert ?\n)
  80.           (setq list (cdr list)))
  81.         (insert "\^_\nEnd tag table\n")))))
  82.       (goto-char opoint)
  83.       (narrow-to-region omin (if nomax (1+ (buffer-size))
  84.                    (min omax (point-max))))))
  85.   (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
  86.  
  87. ;;;###autoload
  88. (defun Info-split ()
  89.   "Split an info file into an indirect file plus bounded-size subfiles.
  90. Each subfile will be up to 50,000 characters plus one node.
  91.  
  92. To use this command, first visit a large Info file that has a tag
  93. table.  The buffer is modified into a (small) indirect info file which
  94. should be saved in place of the original visited file.
  95.  
  96. The subfiles are written in the same directory the original file is
  97. in, with names generated by appending `-' and a number to the original
  98. file name.  The indirect file still functions as an Info file, but it
  99. contains just the tag table and a directory of subfiles."
  100.  
  101.   (interactive)
  102.   (if (< (buffer-size) 70000)
  103.       (error "This is too small to be worth splitting"))
  104.   (goto-char (point-min))
  105.   (search-forward "\^_")
  106.   (forward-char -1)
  107.   (let ((start (point))
  108.     (chars-deleted 0)
  109.     subfiles
  110.     (subfile-number 1)
  111.     (case-fold-search t)
  112.     (filename (file-name-sans-versions buffer-file-name)))
  113.     (goto-char (point-max))
  114.     (forward-line -8)
  115.     (setq buffer-read-only nil)
  116.     (or (search-forward "\^_\nEnd tag table\n" nil t)
  117.     (error "Tag table required; use M-x Info-tagify"))
  118.     (search-backward "\nTag table:\n")
  119.     (if (looking-at "\nTag table:\n\^_")
  120.     (error "Tag table is just a skeleton; use M-x Info-tagify"))
  121.     (beginning-of-line)
  122.     (forward-char 1)
  123.     (save-restriction
  124.       (narrow-to-region (point-min) (point))
  125.       (goto-char (point-min))
  126.       (while (< (1+ (point)) (point-max))
  127.     (goto-char (min (+ (point) 50000) (point-max)))
  128.     (search-forward "\^_" nil 'move)
  129.     (setq subfiles
  130.           (cons (list (+ start chars-deleted)
  131.               (concat (file-name-nondirectory filename)
  132.                   (format "-%d" subfile-number)))
  133.             subfiles))
  134.     ;; Put a newline at end of split file, to make Unix happier.
  135.     (insert "\n")
  136.     (write-region (point-min) (point)
  137.               (concat filename (format "-%d" subfile-number)))
  138.     (delete-region (1- (point)) (point))
  139.     ;; Back up over the final ^_.
  140.     (forward-char -1)
  141.     (setq chars-deleted (+ chars-deleted (- (point) start)))
  142.     (delete-region start (point))
  143.     (setq subfile-number (1+ subfile-number))))
  144.     (while subfiles
  145.       (goto-char start)
  146.       (insert (nth 1 (car subfiles))
  147.           (format ": %d" (1- (car (car subfiles))))
  148.           "\n")
  149.       (setq subfiles (cdr subfiles)))
  150.     (goto-char start)
  151.     (insert "\^_\nIndirect:\n")
  152.     (search-forward "\nTag Table:\n")
  153.     (insert "(Indirect)\n")))
  154.  
  155. ;;;###autoload
  156. (defun Info-validate ()
  157.   "Check current buffer for validity as an Info file.
  158. Check that every node pointer points to an existing node."
  159.   (interactive)
  160.   (save-excursion
  161.     (save-restriction
  162.       (widen)
  163.       (goto-char (point-min))
  164.       (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
  165.       (error "Don't yet know how to validate indirect info files: \"%s\""
  166.          (buffer-name (current-buffer))))
  167.       (goto-char (point-min))
  168.       (let ((allnodes '(("*")))
  169.         (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  170.         (case-fold-search t)
  171.         (tags-losing nil)
  172.         (lossages ()))
  173.     (while (search-forward "\n\^_" nil t)
  174.       (forward-line 1)
  175.       (let ((beg (point)))
  176.         (forward-line 1)
  177.         (if (re-search-backward regexp beg t)
  178.         (let ((name (downcase
  179.                   (buffer-substring-no-properties
  180.                     (match-beginning 1)
  181.                 (progn
  182.                   (goto-char (match-end 1))
  183.                   (skip-chars-backward " \t")
  184.                   (point))))))
  185.           (if (assoc name allnodes)
  186.               (setq lossages
  187.                 (cons (list name "Duplicate node-name" nil)
  188.                   lossages))
  189.               (setq allnodes
  190.                 (cons (list name
  191.                     (progn
  192.                       (end-of-line)
  193.                       (and (re-search-backward
  194.                         "prev[ious]*:" beg t)
  195.                            (progn
  196.                          (goto-char (match-end 0))
  197.                          (downcase
  198.                            (Info-following-node-name)))))
  199.                     beg)
  200.                   allnodes)))))))
  201.     (goto-char (point-min))
  202.     (while (search-forward "\n\^_" nil t)
  203.       (forward-line 1)
  204.       (let ((beg (point))
  205.         thisnode next)
  206.         (forward-line 1)
  207.         (if (re-search-backward regexp beg t)
  208.         (save-restriction
  209.           (search-forward "\n\^_" nil 'move)
  210.           (narrow-to-region beg (point))
  211.           (setq thisnode (downcase
  212.                    (buffer-substring-no-properties
  213.                      (match-beginning 1)
  214.                      (progn
  215.                        (goto-char (match-end 1))
  216.                        (skip-chars-backward " \t")
  217.                        (point)))))
  218.           (end-of-line)
  219.           (and (search-backward "next:" nil t)
  220.                (setq next (Info-validate-node-name "invalid Next"))
  221.                (assoc next allnodes)
  222.                (if (equal (car (cdr (assoc next allnodes)))
  223.                   thisnode)
  224.                ;; allow multiple `next' pointers to one node
  225.                (let ((tem lossages))
  226.                  (while tem
  227.                    (if (and (equal (car (cdr (car tem)))
  228.                            "should have Previous")
  229.                     (equal (car (car tem))
  230.                            next))
  231.                    (setq lossages (delq (car tem) lossages)))
  232.                    (setq tem (cdr tem))))
  233.              (setq lossages
  234.                    (cons (list next
  235.                        "should have Previous"
  236.                        thisnode)
  237.                      lossages))))
  238.           (end-of-line)
  239.           (if (re-search-backward "prev[ious]*:" nil t)
  240.               (Info-validate-node-name "invalid Previous"))
  241.           (end-of-line)
  242.           (if (search-backward "up:" nil t)
  243.               (Info-validate-node-name "invalid Up"))
  244.           (if (re-search-forward "\n* Menu:" nil t)
  245.               (while (re-search-forward "\n\\* " nil t)
  246.             (Info-validate-node-name
  247.               (concat "invalid menu it