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 / info.el < prev    next >
Encoding:
Text File  |  1993-02-17  |  32.5 KB  |  972 lines

  1. ;; Info package for Emacs  -- could use a "create node" feature.
  2. ;; Copyright (C) 1985-1993 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 2, 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. (provide 'info)
  21.  
  22. (defvar Info-history nil
  23.   "List of info nodes user has visited.
  24. Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
  25.  
  26. (defvar Info-enable-edit nil
  27.   "Non-nil means the \\[Info-edit] command in Info can edit the current node.")
  28.  
  29. (defvar Info-enable-active-nodes t
  30.   "Non-nil allows Info to execute Lisp code associated with nodes.
  31. The Lisp code is executed when the node is selected.")
  32.  
  33. (defvar Info-directory-list nil
  34.   "List of directories to search for Info documentation files.")
  35.  
  36. (defvar Info-current-file nil
  37.   "Info file that Info is now looking at, or nil.")
  38.  
  39. (defvar Info-current-subfile nil
  40.   "Info subfile that is actually in the *info* buffer now,
  41. or nil if current info file is not split into subfiles.")
  42.  
  43. (defvar Info-current-node nil
  44.   "Name of node that Info is now looking at, or nil.")
  45.  
  46. (defvar Info-tag-table-marker (make-marker)
  47.   "Marker pointing at beginning of current Info file's tag table.
  48. Marker points nowhere if file has no tag table.")
  49.  
  50. (defun info ()
  51.   "Enter Info, the documentation browser."
  52.   (interactive)
  53.   (if (get-buffer "*info*")
  54.       (switch-to-buffer "*info*")
  55.     (Info-directory)))
  56.  
  57. ;; Go to an info node specified as separate filename and nodename.
  58. ;; no-going-back is non-nil if recovering from an error in this function;
  59. ;; it says do not attempt further (recursive) error recovery.
  60. (defun Info-find-node (filename nodename &optional no-going-back)
  61.   ;; Convert filename to lower case if not found as specified.
  62.   ;; Expand it.
  63.   (if filename
  64.       (let (temp found)
  65.     (setq filename (substitute-in-file-name filename))
  66.     (if (not (string-match "^\\./" filename))
  67.         (let ((dirs Info-directory-list))
  68.           ;; Search the directory list for file FILENAME.
  69.           (while (and dirs (not found))
  70.         (setq found
  71.               (expand-file-name filename (car dirs)))
  72.         (if (not (file-exists-p found))
  73.             (setq found nil))
  74.         ;; Also try (downcase FILENAME) in each directory.
  75.         (or found
  76.             (progn
  77.               (setq found
  78.                 (expand-file-name (downcase filename)
  79.                           (car dirs)))
  80.               (if (not (file-exists-p found))
  81.               (setq found nil))))
  82.         (setq dirs (cdr dirs))))
  83.       ;; FILENAME starts with `./'; just look in current directory.
  84.       (if (file-exists-p (expand-file-name filename))
  85.           (setq found (file-exists-p (expand-file-name filename)))))
  86.     (if found
  87.         (setq filename found)
  88.       (error "Info file %s does not exist" filename))))
  89.   ;; Record the node we are leaving.
  90.   (if (and Info-current-file (not no-going-back))
  91.       (setq Info-history
  92.         (cons (list Info-current-file Info-current-node (point))
  93.           Info-history)))
  94.   ;; Go into info buffer.
  95.   (switch-to-buffer "*info*")
  96.   (or (eq major-mode 'Info-mode)
  97.       (Info-mode))
  98.   (widen)
  99.   (setq Info-current-node nil)
  100.   (unwind-protect
  101.       (progn
  102.     ;; Switch files if necessary
  103.     (or (null filename)
  104.         (equal Info-current-file filename)
  105.         (let ((buffer-read-only nil))
  106.           (setq Info-current-file nil
  107.             Info-current-subfile nil
  108.             buffer-file-name nil)
  109.           (erase-buffer)
  110.           (insert-file-contents filename t)
  111.           (set-buffer-modified-p nil)
  112.           (setq default-directory (file-name-directory filename))
  113.           ;; See whether file has a tag table.  Record the location if yes.
  114.           (set-marker Info-tag-table-marker nil)
  115.           (goto-char (point-max))
  116.           (forward-line -8)
  117.           (or (equal nodename "*")
  118.           (not (search-forward "\^_\nEnd tag table\n" nil t))
  119.           (let (pos)
  120.             ;; We have a tag table.  Find its beginning.
  121.             ;; Is this an indirect file?
  122.             (search-backward "\nTag table:\n")
  123.             (setq pos (point))
  124.             (if (save-excursion
  125.               (forward-line 2)
  126.               (looking-at "(Indirect)\n"))
  127.             ;; It is indirect.  Copy it to another buffer
  128.             ;; and record that the tag table is in that buffer.
  129.             (save-excursion
  130.               (let ((buf (current-buffer)))
  131.                 (set-buffer (get-buffer-create " *info tag table*"))
  132.                 (setq case-fold-search t)
  133.                 (erase-buffer)
  134.                 (insert-buffer-substring buf)
  135.                 (set-marker Info-tag-table-marker
  136.                     (match-end 0))))
  137.              (set-marker Info-tag-table-marker pos))))
  138.           (setq Info-current-file
  139.             (file-name-sans-versions buffer-file-name))))
  140.     (if (equal nodename "*")
  141.         (progn (setq Info-current-node nodename)
  142.            (Info-set-mode-line))
  143.       ;; Search file for a suitable node.
  144.       ;; First get advice from tag table if file has one.
  145.       ;; Also, if this is an indirect info file,
  146.       ;; read the proper subfile into this buffer.
  147.       (let ((guesspos (point-min)))
  148.         (if (marker-position Info-tag-table-marker)
  149.         (save-excursion
  150.           (set-buffer (marker-buffer Info-tag-table-marker))
  151.           (goto-char Info-tag-table-marker)
  152.           (if (search-forward (concat "Node: " nodename "\177") nil t)
  153.               (progn
  154.             (setq guesspos (read (current-buffer)))
  155.             ;; If this is an indirect file,
  156.             ;; determine which file really holds this node
  157.             ;; and read it in.
  158.             (if (not (eq (current-buffer) (get-buffer "*info*")))
  159.                 (setq guesspos
  160.                   (Info-read-subfile guesspos))))
  161.             (error "No such node: \"%s\"" nodename))))
  162.         (goto-char (max (point-min) (- guesspos 1000))))
  163.       ;; Now search from our advised position (or from beg of buffer)
  164.       ;; to find the actual node.
  165.       (let ((regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n]")))
  166.         (catch 'foo
  167.           (while (search-forward "\n\^_" nil t)
  168.         (forward-line 1)
  169.         (let ((beg (point)))
  170.           (forward-line 1)
  171.           (if (re-search-backward regexp beg t)
  172.               (throw 'foo t))))
  173.           (error "No such node: %s" nodename)))
  174.       (Info-select-node)))
  175.     ;; If we did not finish finding the specified node,
  176.     ;; go back to the previous one.
  177.     (or Info-current-node no-going-back
  178.     (let ((hist (car Info-history)))
  179.       (setq Info-history (cdr Info-history))
  180.       (Info-find-node (nth 0 hist) (nth 1 hist) t)
  181.       (goto-char (nth 2 hist)))))
  182.   (goto-char (point-min)))
  183.  
  184. (defun Info-read-subfile (nodepos)
  185.   (set-buffer (marker-buffer Info-tag-table-marker))
  186.   (goto-char (point-min))
  187.   (search-forward "\n\^_")
  188.   (let (lastfilepos
  189.     lastfilename)
  190.     (forward-line 2)
  191.     (catch 'foo
  192.       (while (not (looking-at "\^_"))
  193.     (if (not (eolp))
  194.         (let ((beg (point))
  195.           thisfilepos thisfilename)
  196.           (search-forward ": ")
  197.           (setq thisfilename  (buffer-substring beg (- (point) 2)))
  198.           (setq thisfilepos (read (current-buffer)))
  199.           ;; read in version 19 stops at the end of number.
  200.           ;; Advance to the next line.
  201.           (forward-line 1)
  202.           (if (> thisfilepos nodepos)
  203.           (throw 'foo t))
  204.           (setq lastfilename thisfilename)
  205.           (setq lastfilepos thisfilepos)))))
  206.     (set-buffer (get-buffer "*info*"))
  207.     (or (equal Info-current-subfile lastfilename)
  208.     (let ((buffer-read-only nil))
  209.       (setq buffer-file-name nil)
  210.       (widen)
  211.       (erase-buffer)
  212.       (insert-file-contents lastfilename)
  213.       (set-buffer-modified-p nil)
  214.       (setq Info-current-subfile lastfilename)))
  215.     (goto-char (point-min))
  216.     (search-forward "\n\^_")
  217.     (+ (- nodepos lastfilepos) (point))))
  218.  
  219. ;; Select the info node that point is in.
  220. (defun Info-select-node ()
  221.   (save-excursion
  222.    ;; Find beginning of node.
  223.    (search-backward "\n\^_")
  224.    (forward-line 2)
  225.    ;; Get nodename spelled as it is in the node.
  226.    (re-search-forward "Node:[ \t]*")
  227.    (setq Info-current-node
  228.      (buffer-substring (point)
  229.                (progn
  230.                 (skip-chars-forward "^,\t\n")
  231.                 (point))))
  232.    (Info-set-mode-line)
  233.    ;; Find the end of it, and narrow.
  234.    (beginning-of-line)
  235.    (let (active-expression)
  236.      (narrow-to-region (point)
  237.                (if (re-search-forward "\n[\^_\f]" nil t)
  238.                (prog1
  239.                 (1- (point))
  240.                 (if (looking-at "[\n\^_\f]*execute: ")
  241.                 (progn
  242.                   (goto-char (match-end 0))
  243.                   (setq active-expression
  244.                     (read (current-buffer))))))
  245.              (point-max)))
  246.      (if Info-enable-active-nodes (eval active-expression))
  247.      (Info-fontify-node)
  248.      )))
  249.  
  250. (defun Info-set-mode-line ()
  251.   (setq mode-line-buffer-identification
  252.     (concat
  253.      "Info:  ("
  254.      (if Info-current-file
  255.          (file-name-nondirectory Info-current-file)
  256.        "")
  257.      ")"
  258.      (or Info-current-node ""))))
  259.  
  260. ;; Go to an info node specified with a filename-and-nodename string
  261. ;; of the sort that is found in pointers in nodes.
  262.  
  263. (defun Info-goto-node (nodename)
  264.   "Go to info node named NAME.  Give just NODENAME or (FILENAME)NODENAME."
  265.   (interactive "sGoto node: ")
  266.   (let (filename)
  267.     (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
  268.           nodename)
  269.     (setq filename (if (= (match-beginning 1) (match-end 1))
  270.                ""
  271.              (substring nodename (match-beginning 2) (match-end 2)))
  272.       nodename (substring nodename (match-beginning 3) (match-end 3)))
  273.     (let ((trim (string-match "\\s *\\'" filename)))
  274.       (if trim (setq filename (substring filename 0 trim))))
  275.     (let ((trim (string-match "\\s *\\'" nodename)))
  276.       (if trim (setq nodename (substring nodename 0 trim))))
  277.     (Info-find-node (if (equal filename "") nil filename)
  278.             (if (equal nodename "") "Top" nodename))))
  279.  
  280. (defvar Info-last-search nil
  281.   "Default regexp for Info S command to search for.")
  282.  
  283. (defun Info-search (regexp)
  284.   "Search for REGEXP, starting from point, and select node it's found in."
  285.   (interactive "sSearch (regexp): ")
  286.   (if (equal regexp "")
  287.       (setq regexp Info-last-search)
  288.     (setq Info-last-search regexp))
  289.   (let ((found ()) current
  290.     (onode Info-current-node)
  291.     (ofile Info-current-file)
  292.     (opoint (point))
  293.     (osubfile Info-current-subfile))
  294.     (save-excursion
  295.       (save-restriction
  296.     (widen)
  297.     (if (null Info-current-subfile)
  298.         (progn (re-search-forward regexp) (setq found (point)))
  299.       (condition-case err
  300.           (progn (re-search-forward regexp) (setq found (point)))
  301.         (search-failed nil)))))
  302.     (if (not found) ;can only happen in subfile case -- else would have erred
  303.     (unwind-protect
  304.         (let ((list ()))
  305.           (set-buffer (marker-buffer Info-tag-table-marker))
  306.           (goto-char (point-min))
  307.           (search-forward "\n\^_\nIndirect:")
  308.           (save-restriction
  309.         (narrow-to-region (point)
  310.                   (progn (search-forward "\n\^_")
  311.                      (1- (point))))
  312.         (goto-char (point-min))
  313.         (search-forward (concat "\n" osubfile ": "))
  314.         (beginning-of-line)
  315.         (while (not (eobp))
  316.           (re-search-forward "\\(^.*\\): [0-9]+$")
  317.           (goto-char (+ (match-end 1) 2))
  318.           (setq list (cons (cons (read (current-buffer))
  319.                      (buffer-substring (match-beginning 1)
  320.                                (match-end 1)))
  321.                    list))
  322.           (goto-char (1+ (match-end 0))))
  323.         (setq list (nreverse list)
  324.               current (car (car list))
  325.               list (cdr list)))
  326.           (while list
  327.         (message "Searching subfile %s..." (cdr (car list)))
  328.         (Info-read-subfile (car (car list)))
  329.         (setq list (cdr list))
  330.         (goto-char (point-min))
  331.         (if (re-search-forward regexp nil t)
  332.             (setq found (point) list ())))
  333.           (if found
  334.           (message "")
  335.         (signal 'search-failed (list regexp))))
  336.       (if (not found)
  337.           (progn (Info-read-subfile opoint)
  338.              (goto-char opoint)
  339.              (Info-select-node)))))
  340.     (widen)
  341.     (goto-char found)
  342.     (Info-select-node)
  343.     (or (and (equal onode Info-current-node)
  344.          (equal ofile Info-current-file))
  345.     (setq Info-history (cons (list ofile onode opoint)
  346.                  Info-history)))))
  347.  
  348. (defun Info-extract-pointer (name &optional errorname)
  349.   (save-excursion
  350.    (goto-char (point-min))
  351.    (forward-line 1)
  352.    (if (re-search-backward (concat name ":") nil t)
  353.        nil
  354.      (error (concat "Node has no " (capitalize (or errorname name)))))
  355.    (goto-char (match-end 0))
  356.    (Info-following-node-name)))
  357.  
  358. (defun Info-following-node-name (&optional allowedchars)
  359.   (or allowedchars (setq allowedchars "^,\t\n"))
  360.   (skip-chars-forward " \t")
  361.   (buffer-substring
  362.    (point)
  363.    (let ((allowed (concat "[" allowedchars "]"))
  364.      (skip (concat allowedchars "(")))
  365.      (while (looking-at allowed)
  366.        (skip-chars-forward skip)
  367.        (if (looking-at "(")
  368.        (skip-chars-forward "^)")))
  369.      (skip-chars-backward " \t")
  370.      (point))))
  371.  
  372. (defun Info-next ()
  373.   "Go to the next node of this node."
  374.   (interactive)
  375.   (Info-goto-node (Info-extract-pointer "next")))
  376.  
  377. (defun Info-prev ()
  378.   "Go to the previous node of this node."
  379.   (interactive)
  380.   (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
  381.  
  382. (defun Info-up ()
  383.   "Go to the superior node of this node."
  384.   (interactive)
  385.   (Info-goto-node (Info-extract-pointer "up")))
  386.  
  387. (defun Info-last ()
  388.   "Go back to the last node visited."
  389.   (interactive)
  390.   (or Info-history
  391.       (error "This is the first Info node you looked at"))
  392.   (let (filename nodename opoint)
  393.     (setq filename (car (car Info-history)))
  394.     (setq nodename (car (cdr (car Info-history))))
  395.     (setq opoint (car (cdr (cdr (car Info-history)))))
  396.     (setq Info-history (cdr Info-history))
  397.     (Info-find-node filename nodename)
  398.     (setq Info-history (cdr Info-history))
  399.     (goto-char opoint)))
  400.  
  401. (defun Info-directory ()
  402.   "Go to the Info directory node."
  403.   (interactive)
  404.   (Info-find-node "dir" "top"))
  405.  
  406. (defun Info-follow-reference (footnotename)
  407.   "Follow cross reference named NAME to the node it refers to.
  408. NAME may be an abbreviation of the reference name."
  409.   (interactive
  410.    (let ((completion-ignore-case t)
  411.      completions default (start-point (point)) str i)
  412.      (save-excursion
  413.        (goto-char (point-min))
  414.        (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
  415.      (setq str (buffer-substring
  416.             (match-beginning 1)
  417.             (1- (point))))
  418.      ;; See if this one should be the default.
  419.      (and (null default)
  420.           (< (match-beginning 0) start-point)
  421.           (<= start-point (point))
  422.           (setq default t))
  423.      (setq i 0)
  424.      (while (setq i (string-match "[ \n\t]+" str i))
  425.        (setq str (concat (substring str 0 i) " "
  426.                  (substring str (match-end 0))))
  427.        (setq i (1+ i)))
  428.      ;; Record as a completion and perhaps as default.
  429.      (if (eq default t) (setq default str))
  430.      (setq completions
  431.            (cons (cons str nil)
  432.              completions))))
  433.      (if completions
  434.      (list (completing-read (if default
  435.                     (concat "Follow reference named: ("
  436.                         default ") ")
  437.                   "Follow reference named: ")
  438.                 completions nil t))
  439.        (error "No cross-references in this node"))))
  440.   (let (i
  441.         (blanks "[ \t\n]+")
  442.         (str (concat "\\*note " footnotename)))
  443.     ;; replace " " with "[ \t\n]+"
  444.     (while (setq i (string-match " " str i))
  445.       (setq str (concat (substring str 0 i)
  446.             blanks
  447.             (substring str (1+ i))))
  448.       (setq i (+ i (length blanks))))
  449.  
  450.     (save-excursion
  451.       (goto-char (point-min))
  452.       (or (re-search-forward str nil t)
  453.       (error "No cross-reference named %s" footnotename))
  454.       (goto-char (+ (match-beginning 0) 5)) ;5 is (length "*note")
  455.       (setq str (Info-extract-menu-node-name "Bad format cross reference")))
  456.  
  457.     (Info-goto-node str)))
  458.  
  459. (defun Info-extract-menu-node-name (&optional errmessage)
  460.   (skip-chars-forward " \t\n")
  461.   (let ((beg (point))
  462.     str
  463.     i)
  464.     (skip-chars-forward "^:")
  465.     (forward-char 1)
  466.     (setq str (if (looking-at ":")
  467.           (buffer-substring beg (1- (point)))
  468.         (Info-following-node-name "^.,\t")))
  469.     ;; replace regexp "[ \t\n]+" with " "
  470.     (while (setq i (string-match "[ \t\n]+" str i))
  471.       (setq str (concat (substring str 0 i) 
  472.                         " "
  473.                         (substring str (match-end 0))))
  474.       (setq i (+ i 1)))
  475.     str))
  476.  
  477. (defun Info-menu (menu-item)
  478.   "Go to node for menu item named (or abbreviated) NAME.
  479. Completion is allowed, and the menu item point is on is the default."
  480.   (interactive
  481.    (let ((completions '())
  482.      ;; If point is within a menu item, use that item as the default
  483.      (default nil)
  484.      (p (point))
  485.      (last nil))
  486.      (save-excursion
  487.        (goto-char (point-min))
  488.        (if (not (search-forward "\n* menu:" nil t))
  489.        (error "No menu in this node"))
  490.        (while (re-search-forward
  491.         "\n\\* \\([^:\t\n]*\\):" nil t)
  492.      (if (and (null default)
  493.           (prog1 (if last (< last p) nil)
  494.             (setq last (match-beginning 0)))
  495.           (<= p last))
  496.          (setq default (car (car completions))))
  497.      (setq completions (cons (cons (buffer-substring
  498.                      (match-beginning 1)
  499.                      (match-end 1))
  500.                        (match-beginning 1))
  501.                  completions)))
  502.        (if (and (null default) last
  503.         (< last p)
  504.         (<= p (progn (end-of-line) (point))))
  505.        (setq default (car (car completions)))))
  506.      (let ((item nil))
  507.        (while (null item)
  508.      (setq item (let ((completion-ignore-case t))
  509.               (completing-read (if default
  510.                        (format "Menu item (default %s): "
  511.                            default)
  512.                        "Menu item: ")
  513.                        completions nil t)))
  514.      ;; we rely on the bug (which RMS won't change for his own reasons)
  515.      ;; that ;; completing-read accepts an input of "" even when the
  516.      ;; require-match argument is true and "" is not a valid possibility
  517.      (if (string= item "")
  518.          (if default
  519.          (setq item default)
  520.              ;; ask again
  521.              (setq item nil))))
  522.        (list item))))
  523.   (Info-goto-node (Info-extract-menu-item menu-item)))
  524.   
  525. (defun Info-extract-menu-item (menu-item)
  526.   (save-excursion
  527.     (goto-char (point-min))
  528.     (or (search-forward "\n* menu:" nil t)
  529.     (error "No menu in this node"))
  530.     (or (search-forward (concat "\n* " menu-item ":") nil t)
  531.     (search-forward (concat "\n* " menu-item) nil t)
  532.     (error "No such item in menu"))
  533.     (beginning-of-line)
  534.     (forward-char 2)
  535.     (Info-extract-menu-node-name)))
  536.  
  537. (defun Info-extract-menu-counting (count)
  538.   (save-excursion
  539.     (goto-char (point-min))
  540.     (or (search-forward "\n* menu:" nil t)
  541.     (error "No menu in this node"))
  542.     (or (search-forward "\n* " nil t count)
  543.     (error "Too few items in menu"))
  544.     (Info-extract-menu-node-name)))
  545.  
  546. (defun Info-first-menu-item ()
  547.   "Go to the node of the first menu item."
  548.   (interactive)
  549.   (Info-goto-node (Info-extract-menu-counting 1)))
  550.  
  551. (defun Info-second-menu-item ()
  552.   "Go to the node of the second menu item."
  553.   (interactive)
  554.   (Info-goto-node (Info-extract-menu-counting 2)))
  555.  
  556. (defun Info-third-menu-item ()
  557.   "Go to the node of the third menu item."
  558.   (interactive)
  559.   (Info-goto-node (Info-extract-menu-counting 3)))
  560.  
  561. (defun Info-fourth-menu-item ()
  562.   "Go to the node of the fourth menu item."
  563.   (interactive)
  564.   (Info-goto-node (Info-extract-menu-counting 4)))
  565.  
  566. (defun Info-fifth-menu-item ()
  567.   "Go to the node of the fifth menu item."
  568.   (interactive)
  569.   (Info-goto-node (Info-extract-menu-counting 5)))
  570.  
  571. (defun Info-exit ()
  572.   "Exit Info by selecting some other buffer."
  573.   (interactive)
  574.   (switch-to-buffer (prog1 (other-buffer (current-buffer))
  575.                (bury-buffer (current-buffer)))))
  576.  
  577. (defun Info-undefined ()
  578.   "Make command be undefined in Info."
  579.   (interactive)
  580.   (ding))
  581.  
  582. (defun Info-help ()
  583.   "Enter the Info tutorial."
  584.   (interactive)
  585.   (Info-find-node "info"
  586.           (if (< (window-height) 23)
  587.               "Help-Small-Screen"
  588.             "Help")))
  589.  
  590. (defun Info-summary ()
  591.   "Display a brief summary of all Info commands."
  592.   (interactive)
  593.   (save-window-excursion
  594.     (switch-to-buffer "*Help*")
  595.     (erase-buffer)
  596.     (insert (documentation 'Info-mode))
  597.     (goto-char (point-min))
  598.     (let (ch flag
  599.       (event (allocate-event)))
  600.       (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
  601.             (message (if flag "Type Space to see more"
  602.                    "Type Space to return to Info"))
  603.             (setq ch (event-to-character (next-command-event event)))
  604.             (if (/= ?\  ch)
  605.             (progn (setq unread-command-event event) nil)
  606.               flag))
  607.     (scroll-up)))))
  608.  
  609. (defun Info-get-token (pos start all &optional errorstring)
  610.   "Return the token around POS,
  611. POS must be somewhere inside the token
  612. START is a regular expression which will match the
  613.     beginning of the tokens delimited string
  614. ALL is a regular expression with a single
  615.     parenthized subpattern which is the token to be
  616.     returned. E.g. '{\(.*\)}' would return any string
  617.     enclosed in braces around POS.
  618. SIG optional fourth argument, controls action on no match
  619.     nil: return nil
  620.     t: beep
  621.     a string: signal an error, using that string."
  622.   (save-excursion
  623.     (goto-char pos)
  624.     (re-search-backward start (max (point-min) (- pos 200)) 'yes)
  625.     (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
  626.         (not (and (<= (match-beginning 0) pos)
  627.               (> (match-end 0) pos)))))
  628.     (if (and (<= (match-beginning 0) pos)
  629.          (> (match-end 0) pos))
  630.     (buffer-substring (match-beginning 1) (match-end 1))
  631.       (cond ((null errorstring)
  632.          nil)
  633.         ((eq errorstring t)
  634.          (beep)
  635.          nil)
  636.         (t
  637.          (error "No %s around position %d" errorstring pos))))))
  638.  
  639. (defun Info-follow-nearest-node (event)
  640.   "Follow a node reference near point.  Like M, F, N, P or U command.
  641. At end of the node's text, moves to the next node."
  642.   (interactive "@e")
  643.   (mouse-set-point event)
  644.   (let (node)
  645.     (cond
  646.      ((setq node (Info-get-token (point) "\\*note " "\\*note \\([^:]*\\):" t))
  647.       (Info-follow-reference node))
  648.      ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::" t))
  649.       (Info-goto-node node))
  650.      ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):" t))
  651.       (Info-menu node))
  652.      ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)" t))
  653.       (Info-goto-node node))
  654.      ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)" t))
  655.       (Info-goto-node node))
  656.      ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)" t))
  657.       (Info-goto-node "Top"))
  658.      ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)" t))
  659.       (Info-goto-node node))
  660.      ((save-excursion (forward-line 1) (eobp))
  661.       (Info-next)))
  662.     ))
  663.  
  664. (defvar Info-mode-map nil
  665.   "Keymap containing Info commands.")
  666. (if Info-mode-map
  667.     nil
  668.   (setq Info-mode-map (make-keymap))
  669.   (suppress-keymap Info-mode-map)
  670.   (define-key Info-mode-map "." 'beginning-of-buffer)
  671.   (define-key Info-mode-map " " 'scroll-up)
  672.   (define-key Info-mode-map "1" 'Info-first-menu-item)
  673.   (define-key Info-mode-map "2" 'Info-second-menu-item)
  674.   (define-key Info-mode-map "3" 'Info-third-menu-item)
  675.   (define-key Info-mode-map "4" 'Info-fourth-menu-item)
  676.   (define-key Info-mode-map "5" 'Info-fifth-menu-item)
  677.   (define-key Info-mode-map "6" 'undefined)
  678.   (define-key Info-mode-map "7" 'undefined)
  679.   (define-key Info-mode-map "8" 'undefined)
  680.   (define-key Info-mode-map "9" 'undefined)
  681.   (define-key Info-mode-map "0" 'undefined)
  682.   (define-key Info-mode-map "?" 'Info-summary)
  683.   (define-key Info-mode-map "b" 'beginning-of-buffer)
  684.   (define-key Info-mode-map "d" 'Info-directory)
  685.   (define-key Info-mode-map "e" 'Info-edit)
  686.   (define-key Info-mode-map "f" 'Info-follow-reference)
  687.   (define-key Info-mode-map "g" 'Info-goto-node)
  688.   (define-key Info-mode-map "h" 'Info-help)
  689.   (define-key Info-mode-map "l" 'Info-last)
  690.   (define-key Info-mode-map "m" 'Info-menu)
  691.   (define-key Info-mode-map "n" 'Info-next)
  692.   (define-key Info-mode-map "p" 'Info-prev)
  693.   (define-key Info-mode-map "q" 'Info-exit)
  694.   (define-key Info-mode-map "s" 'Info-search)
  695.   (define-key Info-mode-map "u" 'Info-up)
  696.   (define-key Info-mode-map "\177" 'scroll-down)
  697.  
  698.   ;;(define-key Info-mode-map 'button1 'mouse-scroll-up-full)
  699.   ;;(define-key Info-mode-map 'button2 'Info-follow-nearest-node)
  700.   ;;(define-key Info-mode-map 'button3 'mouse-scroll-down-full)
  701.  
  702.   (define-key Info-mode-map 'button2 'Info-follow-indicated-node)
  703.   (define-key Info-mode-map 'button3 'Info-select-node-menu)
  704.   )
  705.  
  706. ;; Info mode is suitable only for specially formatted data.
  707. (put 'info-mode 'mode-class 'special)
  708.  
  709. (defun Info-mode ()
  710.   "Info mode provides commands for browsing through the Info documentation tree.
  711. Documentation in Info is divided into \"nodes\", each of which
  712. discusses one topic and contains references to other nodes
  713. which discuss related topics.  Info has commands to follow
  714. the references and show you other nodes.
  715.  
  716. h    Invoke the Info tutorial.
  717.  
  718. Selecting other nodes:
  719. n    Move to the \"next\" node of this node.
  720. p    Move to the \"previous\" node of this node.
  721. u    Move \"up\" from this node.
  722. m    Pick menu item specified by name (or abbreviation).
  723.     Picking a menu item causes another node to be selected.
  724. f    Follow a cross reference.  Reads name of reference.
  725. l    Move to the last node you were at.
  726.  
  727. Moving within a node:
  728. Space    scroll forward a full screen.     DEL  scroll backward.
  729. b    Go to beginning of node.
  730.  
  731. Mouse commands:
  732. Left Button    Set point
  733. Middle Button    Click on a highlighted node reference to go to it
  734. Right Button    Pop up a menu of applicable Info commands
  735.  
  736. Advanced commands:
  737. q    Quit Info: reselect previously selected buffer.
  738. e    Edit contents of selected node.
  739. 1    Pick first item in node's menu.
  740. 2, 3, 4, 5   Pick second ... fifth item in node's menu.
  741. g    Move to node specified by name.
  742.     You may include a filename as well, as (FILENAME)NODENAME.
  743. s    Search through this Info file for specified regexp,
  744.     and select the node in which the next occurrence is found."
  745.   (kill-all-local-variables)
  746.   (setq major-mode 'Info-mode)
  747.   (setq mode-name "Info")
  748.   (use-local-map Info-mode-map)
  749.   (set-syntax-table text-mode-syntax-table)
  750.   (setq local-abbrev-table text-mode-abbrev-table)
  751.   (setq case-fold-search t)
  752.   (setq buffer-read-only t)
  753.   (make-local-variable 'Info-current-file)
  754.   (make-local-variable 'Info-current-subfile)
  755.   (make-local-variable 'Info-current-node)
  756.   (make-local-variable 'Info-tag-table-marker)
  757.   (make-local-variable 'Info-history)
  758.   (Info-set-mode-line))
  759.  
  760. (defvar Info-edit-map nil
  761.   "Local keymap used within `e' command of Info.")
  762. (if Info-edit-map
  763.     nil
  764.   (setq Info-edit-map (make-sparse-keymap))
  765.   (set-keymap-parent Info-edit-map text-mode-map)
  766.   (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
  767.  
  768. ;; Info-edit mode is suitable only for specially formatted data.
  769. (put 'info-edit-mode 'mode-class 'special)
  770.  
  771. (defun Info-edit-mode ()
  772.   "Major mode for editing the contents of an Info node.
  773. Like text mode with the addition of Info-cease-edit
  774. which returns to Info mode for browsing.
  775. \\{Info-edit-map}"
  776.   )
  777.  
  778. (defun Info-edit ()
  779.   "Edit the contents of this Info node.
  780. Allowed only if variable Info-enable-edit is non-nil."
  781.   (interactive)
  782.   (or Info-enable-edit
  783.       (error "Editing info nodes is not enabled"))
  784.   (use-local-map Info-edit-map)
  785.   (setq major-mode 'Info-edit-mode)
  786.   (setq mode-name "Info Edit")
  787.   (kill-local-variable 'mode-line-buffer-identification)
  788.   (setq buffer-read-only nil)
  789.   ;; Make mode line update.
  790.   (set-buffer-modified-p (buffer-modified-p))
  791.   (message (substitute-command-keys
  792.          "Editing: Type \\[Info-cease-edit] to return to info")))
  793.  
  794. (defun Info-cease-edit ()
  795.   "Finish editing Info node; switch back to Info proper."
  796.   (interactive)
  797.   ;; Do this first, so nothing has changed if user C-g's at query.
  798.   (and (buffer-modified-p)
  799.        (y-or-n-p "Save the file? ")
  800.        (save-buffer))
  801.   (use-local-map Info-mode-map)
  802.   (setq major-mode 'Info-mode)
  803.   (setq mode-name "Info")
  804.   (Info-set-mode-line)
  805.   (setq buffer-read-only t)
  806.   ;; Make mode line update.
  807.   (set-buffer-modified-p (buffer-modified-p))
  808.   (and (marker-position Info-tag-table-marker)
  809.        (buffer-modified-p)
  810.        (message "Tags may have changed.  Use Info-tagify if necessary")))
  811.  
  812.  
  813. ;;; fontification and mousability
  814.  
  815. (defvar Info-fontify t)
  816.  
  817. ;(defvar Info-faces-initted nil)
  818. ;(defun Info-init-faces ()
  819. ;  (if Info-faces-initted
  820. ;      nil
  821. ;    (make-face 'info-node)
  822. ;    (make-face 'info-xref)
  823. ;    (or (face-differs-from-default-p 'info-node)
  824. ;    (copy-face 'bold-italic 'info-node))
  825. ;    (or (face-differs-from-default-p 'info-xref)
  826. ;    (copy-face 'bold 'info-xref))
  827. ;    (setq Info-faces-initted t)))
  828.  
  829. (make-face 'info-node)
  830. (make-face 'info-xref)
  831. (or (face-differs-from-default-p 'info-node)
  832.     (copy-face 'bold-italic 'info-node))
  833. (or (face-differs-from-default-p 'info-xref)
  834.     (copy-face 'bold 'info-xref))
  835.  
  836. (defun Info-fontify-node ()
  837. ;;  (Info-init-faces)
  838.   (if Info-fontify
  839.       (save-excursion
  840.     (map-extents (function (lambda (x y)
  841.                  (if (eq (extent-data x) 'info)
  842.                      (delete-extent x))))
  843.              (current-buffer) (point-min) (point-max) nil)
  844.     (let ((case-fold-search t)
  845.           extent)
  846.       (goto-char (point-min))
  847.       (if (looking-at "^File: [^,: \t]+,?[ \t]+")
  848.           (progn
  849.         (goto-char (match-end 0))
  850.         (while
  851.             (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
  852.           (goto-char (match-end 0))
  853.           (setq extent (make-extent (match-beginning 1) (match-end 1)))
  854.           (set-extent-face extent 'info-xref)
  855.           (set-extent-data extent 'info)
  856.           (set-extent-attribute extent 'highlight))))
  857.       (goto-char (point-min))
  858.       (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
  859.         (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
  860.         nil
  861.           (setq extent (make-extent (match-beginning 0) (match-end 1)))
  862.           (set-extent-face extent 'info-xref)
  863.           (set-extent-data extent 'info)
  864.           (set-extent-attribute extent 'highlight)))
  865.       (goto-char (point-min))
  866.       (if (search-forward "\n* menu:" nil t)
  867.           (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
  868.         (setq extent (make-extent (match-beginning 0) (match-end 1)))
  869.         (set-extent-face extent 'info-node)
  870.         (set-extent-data extent 'info)
  871.         (set-extent-attribute extent 'highlight)))))))
  872.  
  873. (defun Info-indicated-node (event)
  874.   (save-window-excursion
  875.     (save-excursion
  876.       (mouse-set-point event)
  877.       (let* ((buffer (window-buffer (event-window event)))
  878.          (p (event-point event))
  879.          (extent (and p (extent-at p buffer 'highlight)))
  880.          (text (and extent
  881.             (save-excursion
  882.               (set-buffer buffer)
  883.               (buffer-substring
  884.                (extent-start-position extent)
  885.                (extent-end-position extent)))))
  886.          (case-fold-search t)
  887.          i)
  888.     (cond ((null extent)
  889.            nil)
  890.           ((string-match "\\`\\*note[ \n\t]*\\([^:]*\\):?\\'" text)
  891.            ;; it's a cross-reference
  892.            (setq text (substring text (match-beginning 1) (match-end 1)))
  893.            (while (setq i (string-match "[ \n\t]+" text i))
  894.          (setq text (concat (substring text 0 i) " "
  895.                     (substring text (match-end 0))))
  896.          (setq i (1+ i)))
  897.            (list 'Info-follow-reference text))
  898.           ((and (save-excursion (goto-char (extent-start-position extent))
  899.                     (= ?\n (preceding-char)))
  900.             (string-match "\\`\\* \\([^:\t\n]+\\):?\\'" text))
  901.            ;; it's a menu entry
  902.            (setq text (substring text (match-beginning 1) (match-end 1)))
  903.            (list 'Info-menu text))
  904.           (t
  905.            ;; otherwise, it must be a node-name in the first line
  906.            (list 'Info-goto-node text)))))))
  907.  
  908.  
  909. (defun Info-follow-indicated-node (event)
  910.   "Follow the crossreference or menu item at the click-location."
  911.   (interactive "e")
  912.   (mouse-set-point event)
  913.   (eval (or (Info-indicated-node event)
  914.         (error "click on a cross-reference to follow"))))
  915.  
  916.  
  917. (defun Info-select-node-menu (event)
  918.   "Pops up a menu of applicable Info commands."
  919.   (interactive "e")
  920.   (select-window (event-window event))
  921.   (let ((case-fold-search t)
  922.     up-p prev-p next-p menu
  923.     i text xrefs subnodes in)
  924.     (save-excursion
  925.       (goto-char (point-min))
  926.       (if (looking-at ".*\\bNext:") (setq next-p t))
  927.       (if (looking-at ".*\\bPrev:") (setq prev-p t))
  928.       (if (looking-at ".*Up:") (setq up-p t))
  929.       (setq menu (nconc (list "Info" "Info Commands:" "----")
  930.             (if (setq in (Info-indicated-node event))
  931.                 (list (vector (car (cdr in)) in t)))
  932.             (list
  933.              ["Goto Info Top-level" Info-directory t]
  934.              (vector "Next Node" 'Info-next next-p)
  935.              (vector "Previous Node" 'Info-prev prev-p)
  936.              (vector "Parent Node (Up)" 'Info-up up-p)
  937.              ["Goto Node..." Info-goto-node t]
  938.              ["Goto Last Visited Node" Info-last t])))
  939.       (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
  940.     (setq text (buffer-substring (match-beginning 1) (match-end 1)))
  941.     (while (setq i (string-match "[ \n\t]+" text i))
  942.       (setq text (concat (substring text 0 i) " "
  943.                  (substring text (match-end 0))))
  944.       (setq i (1+ i)))
  945.     (setq xrefs (cons text xrefs)))
  946.       (setq xrefs (nreverse xrefs))
  947.       (goto-char (point-min))
  948.       (if (search-forward "\n* menu:" nil t)
  949.       (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
  950.         (setq text (buffer-substring (match-beginning 1) (match-end 1)))
  951.         (setq subnodes (cons text subnodes))))
  952.       (setq subnodes (nreverse subnodes)))
  953.     (if (or xrefs subnodes)
  954.     (nconc menu (list "-----")))
  955.     (if xrefs
  956.     (nconc menu
  957.            (list
  958.         (apply 'list "Cross-References"
  959.                (mapcar '(lambda (xref)
  960.                   (vector xref
  961.                       (list 'Info-follow-reference xref)
  962.                       t))
  963.                    xrefs)))))
  964.     (if subnodes
  965.     (nconc menu
  966.            (list
  967.         (apply 'list "Sub-Nodes"
  968.                (mapcar '(lambda (node)
  969.                   (vector node (list 'Info-menu node) t))
  970.                    subnodes)))))
  971.     (popup-menu menu)))
  972.