home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / info.el < prev    next >
Encoding:
Text File  |  1994-05-30  |  54.8 KB  |  1,564 lines

  1. ;;; info.el --- info package for Emacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 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
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;;; Note that nowadays we expect info files to be made using makeinfo.
  27.  
  28. ;;; Code:
  29.  
  30. (defvar Info-history nil
  31.   "List of info nodes user has visited.
  32. Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
  33.  
  34. (defvar Info-enable-edit nil
  35.   "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info can edit the current node.
  36. This is convenient if you want to write info files by hand.
  37. However, we recommend that you not do this.
  38. It is better to write a Texinfo file and generate the Info file from that,
  39. because that gives you a printed manual as well.")
  40.  
  41. (defvar Info-enable-active-nodes t
  42.   "Non-nil allows Info to execute Lisp code associated with nodes.
  43. The Lisp code is executed when the node is selected.")
  44.  
  45. (defvar Info-default-directory-list nil
  46.   "List of default directories to search for Info documentation files.
  47. This value is used as the default for `Info-directory-list'.  It is set
  48. in paths.el.")
  49.  
  50. (defvar Info-fontify t
  51.   "*Non-nil enables highlighting and fonts in Info nodes.")
  52.  
  53. (defvar Info-directory-list
  54.   (let ((path (getenv "INFOPATH"))
  55.     (sibling (expand-file-name "../info/" (invocation-directory))))
  56.     (if path
  57.     (let ((list nil)
  58.           idx)
  59.       (while (> (length path) 0)
  60.         (setq idx (or (string-match ":" path) (length path))
  61.           list (cons (substring path 0 idx) list)
  62.           path (substring path (min (1+ idx)
  63.                         (length path)))))
  64.       (nreverse list))
  65.       (if (or (member sibling Info-default-directory-list)
  66.           (not (file-exists-p sibling)))
  67.       Info-default-directory-list
  68.     (reverse (cons sibling (cdr (reverse Info-default-directory-list)))))))
  69.   "List of directories to search for Info documentation files.
  70. nil means not yet initialized.  In this case, Info uses the environment
  71. variable INFOPATH to initialize it, or `Info-default-directory-list'
  72. if there is no INFOPATH variable in the environment.
  73. The last element of `Info-default-directory-list' is the directory
  74. where Emacs installs the Info files that come with it.
  75. If Emacs finds it has been installed elsewhere, or not yet installed,
  76. it replaces that element with the directory that its Info files are in.")
  77.  
  78. (defvar Info-current-file nil
  79.   "Info file that Info is now looking at, or nil.")
  80.  
  81. (defvar Info-current-subfile nil
  82.   "Info subfile that is actually in the *info* buffer now,
  83. or nil if current info file is not split into subfiles.")
  84.  
  85. (defvar Info-current-node nil
  86.   "Name of node that Info is now looking at, or nil.")
  87.  
  88. (defvar Info-tag-table-marker (make-marker)
  89.   "Marker pointing at beginning of current Info file's tag table.
  90. Marker points nowhere if file has no tag table.")
  91.  
  92. (defvar Info-current-file-completions nil
  93.   "Cached completion list for current Info file.")
  94.  
  95. (defvar Info-index-alternatives nil
  96.   "List of possible matches for last Info-index command.")
  97.  
  98. (defvar Info-standalone nil
  99.   "Non-nil if Emacs was started solely as an Info browser.")
  100.  
  101. (defvar Info-suffix-list '( (".info.Z"  . "uncompress")
  102.                 (".info.Y"  . "unyabba")
  103.                 (".info.gz" . "gunzip")
  104.                 (".info.z"  . "gunzip")
  105.                 (".info"    . nil)
  106.                 (".Z"       . "uncompress")
  107.                 (".Y"       . "unyabba")
  108.                 (".gz"      . "gunzip")
  109.                 (".z"       . "gunzip")
  110.                 (""         . nil))
  111.   "List of file name suffixes and associated decoding commands.
  112. Each entry should be (SUFFIX . STRING); the file is given to
  113. the command as standard input.  If STRING is nil, no decoding is done.
  114. Because the SUFFIXes are tried in order, the empty string should
  115. be last in the list.")
  116.  
  117. (defun info-insert-file-contents (filename &optional visit)
  118.   "Insert the contents of an info file in the current buffer.
  119. Do the right thing if the file has been compressed or zipped."
  120.   (let ((tail Info-suffix-list)
  121.     fullname decoder)
  122.     (if (file-exists-p filename)
  123.     (progn
  124.       (while (and tail
  125.               (not (string-match
  126.                 (concat (regexp-quote (car (car tail))) "$")
  127.                 filename)))
  128.         (setq tail (cdr tail)))
  129.       (setq fullname filename
  130.         decoder (cdr (car tail))))
  131.       (while (and tail
  132.           (not (file-exists-p (concat filename (car (car tail))))))
  133.     (setq tail (cdr tail)))
  134.       (setq fullname (concat filename (car (car tail)))
  135.         decoder (cdr (car tail)))
  136.       ;; check for conflict with jka-compr
  137.       (if (and (featurep 'jka-compr)
  138.            (jka-compr-installed-p)
  139.            (jka-compr-get-compression-info (concat filename
  140.                                (car (car tail)))))
  141.       (setq decoder nil))
  142.       (or tail
  143.       (error "Can't find %s or any compressed version of it!" filename)))
  144.     (insert-file-contents fullname visit)
  145.     (if decoder
  146.     (let ((buffer-read-only nil))
  147.       (shell-command-on-region (point-min) (point-max) decoder t)))))
  148.  
  149. ;;;###autoload
  150. (defun info (&optional file)
  151.   "Enter Info, the documentation browser.
  152. Optional argument FILE specifies the file to examine;
  153. the default is the top-level directory of Info.
  154.  
  155. In interactive use, a prefix argument directs this command
  156. to read a file name from the minibuffer."
  157.   (interactive (if current-prefix-arg
  158.            (list (read-file-name "Info file name: " nil nil t))))
  159.   (if file
  160.       (Info-goto-node (concat "(" file ")"))
  161.     (if (get-buffer "*info*")
  162.     (switch-to-buffer "*info*")
  163.       (Info-directory))))
  164.  
  165. ;;;###autoload
  166. (defun info-standalone ()
  167.   "Run Emacs as a standalone Info reader.
  168. Usage:  emacs -f info-standalone [filename]
  169. In standalone mode, \\<Info-mode-map>\\[Info-exit] exits Emacs itself."
  170.   (setq Info-standalone t)
  171.   (if (and command-line-args-left
  172.        (not (string-match "^-" (car command-line-args-left))))
  173.       (condition-case err
  174.       (progn
  175.         (info (car command-line-args-left))
  176.         (setq command-line-args-left (cdr command-line-args-left)))
  177.     (error (send-string-to-terminal
  178.         (format "%s\n" (if (eq (car-safe err) 'error)
  179.                    (nth 1 err) err)))
  180.            (save-buffers-kill-emacs)))
  181.     (info)))
  182.  
  183. ;; Go to an info node specified as separate filename and nodename.
  184. ;; no-going-back is non-nil if recovering from an error in this function;
  185. ;; it says do not attempt further (recursive) error recovery.
  186. (defun Info-find-node (filename nodename &optional no-going-back)
  187.   ;; Convert filename to lower case if not found as specified.
  188.   ;; Expand it.
  189.   (if filename
  190.       (let (temp temp-downcase found)
  191.     (setq filename (substitute-in-file-name filename))
  192.     (if (string= (downcase (file-name-nondirectory filename)) "dir")
  193.         (setq found t)
  194.       (let ((dirs (if (string-match "^\\./" filename)
  195.               ;; If specified name starts with `./'
  196.               ;; then just try current directory.
  197.               '("./")
  198.             Info-directory-list)))
  199.         ;; Search the directory list for file FILENAME.
  200.         (while (and dirs (not found))
  201.           (setq temp (expand-file-name filename (car dirs)))
  202.           (setq temp-downcase
  203.             (expand-file-name (downcase filename) (car dirs)))
  204.           ;; Try several variants of specified name.
  205.           (catch 'foundit
  206.         (mapcar
  207.          (function
  208.           (lambda (x)
  209.             (if (file-exists-p (concat temp (car x)))
  210.             (progn
  211.               (setq found temp)
  212.               (throw 'foundit nil)))
  213.             (if (file-exists-p (concat temp-downcase (car x)))
  214.             (progn
  215.               (setq found temp-downcase)
  216.               (throw 'foundit nil)))))
  217.          Info-suffix-list))
  218.           (setq dirs (cdr dirs)))))
  219.     (if found
  220.         (setq filename found)
  221.       (error "Info file %s does not exist" filename))))
  222.   ;; Record the node we are leaving.
  223.   (if (and Info-current-file (not no-going-back))
  224.       (setq Info-history
  225.         (cons (list Info-current-file Info-current-node (point))
  226.           Info-history)))
  227.   ;; Go into info buffer.
  228.   (switch-to-buffer "*info*")
  229.   (buffer-disable-undo (current-buffer))
  230.   (or (eq major-mode 'Info-mode)
  231.       (Info-mode))
  232.   (widen)
  233.   (setq Info-current-node nil)
  234.   (unwind-protect
  235.       (progn
  236.     ;; Switch files if necessary
  237.     (or (null filename)
  238.         (equal Info-current-file filename)
  239.         (let ((buffer-read-only nil))
  240.           (setq Info-current-file nil
  241.             Info-current-subfile nil
  242.             Info-current-file-completions nil
  243.             Info-index-alternatives nil
  244.             buffer-file-name nil)
  245.           (erase-buffer)
  246.           (if (eq filename t)
  247.           (Info-insert-dir)
  248.         (info-insert-file-contents filename t)
  249.         (setq default-directory (file-name-directory filename)))
  250.           (set-buffer-modified-p nil)
  251.           ;; See whether file has a tag table.  Record the location if yes.
  252.           (set-marker Info-tag-table-marker nil)
  253.           (goto-char (point-max))
  254.           (forward-line -8)
  255.           (or (equal nodename "*")
  256.           (not (search-forward "\^_\nEnd tag table\n" nil t))
  257.           (let (pos)
  258.             ;; We have a tag table.  Find its beginning.
  259.             ;; Is this an indirect file?
  260.             (search-backward "\nTag table:\n")
  261.             (setq pos (point))
  262.             (if (save-excursion
  263.               (forward-line 2)
  264.               (looking-at "(Indirect)\n"))
  265.             ;; It is indirect.  Copy it to another buffer
  266.             ;; and record that the tag table is in that buffer.
  267.             (save-excursion
  268.               (let ((buf (current-buffer)))
  269.                 (set-buffer (get-buffer-create " *info tag table*"))
  270.                             (buffer-disable-undo (current-buffer))
  271.                 (setq case-fold-search t)
  272.                 (erase-buffer)
  273.                 (insert-buffer-substring buf)
  274.                 (set-marker Info-tag-table-marker
  275.                     (match-end 0))))
  276.               (set-marker Info-tag-table-marker pos))))
  277.           (setq Info-current-file
  278.             (if (eq filename t) "dir"
  279.               (file-name-sans-versions buffer-file-name)))))
  280.     (if (equal nodename "*")
  281.         (progn (setq Info-current-node nodename)
  282.            (Info-set-mode-line))
  283.       ;; Search file for a suitable node.
  284.       (let ((guesspos (point-min))
  285.         (regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n\177]")))
  286.         ;; First get advice from tag table if file has one.
  287.         ;; Also, if this is an indirect info file,
  288.         ;; read the proper subfile into this buffer.
  289.         (if (marker-position Info-tag-table-marker)
  290.         (save-excursion
  291.           (set-buffer (marker-buffer Info-tag-table-marker))
  292.           (goto-char Info-tag-table-marker)
  293.           (if (re-search-forward regexp nil t)
  294.               (progn
  295.             (setq guesspos (read (current-buffer)))
  296.             ;; If this is an indirect file,
  297.             ;; determine which file really holds this node
  298.             ;; and read it in.
  299.             (if (not (eq (current-buffer) (get-buffer "*info*")))
  300.                 (setq guesspos
  301.                   (Info-read-subfile guesspos))))
  302.             (error "No such node: \"%s\"" nodename))))
  303.         (goto-char (max (point-min) (- guesspos 1000)))
  304.         ;; Now search from our advised position (or from beg of buffer)
  305.         ;; to find the actual node.
  306.         (catch 'foo
  307.           (while (search-forward "\n\^_" nil t)
  308.         (forward-line 1)
  309.         (let ((beg (point)))
  310.           (forward-line 1)
  311.           (if (re-search-backward regexp beg t)
  312.               (throw 'foo t))))
  313.           (error "No such node: %s" nodename)))
  314.       (Info-select-node)))
  315.     ;; If we did not finish finding the specified node,
  316.     ;; go back to the previous one.
  317.     (or Info-current-node no-going-back (null Info-history)
  318.     (let ((hist (car Info-history)))
  319.       (setq Info-history (cdr Info-history))
  320.       (Info-find-node (nth 0 hist) (nth 1 hist) t)
  321.       (goto-char (nth 2 hist)))))
  322.   (goto-char (point-min)))
  323.  
  324. ;; Cache the contents of the (virtual) dir file, once we have merged
  325. ;; it for the first time, so we can save time subsequently.
  326. (defvar Info-dir-contents nil)
  327.  
  328. ;; Cache for the directory we decided to use for the default-directory
  329. ;; of the merged dir text.
  330. (defvar Info-dir-contents-directory nil)
  331.  
  332. ;; Record the file attributes of all the files from which we
  333. ;; constructed Info-dir-contents.
  334. (defvar Info-dir-file-attributes nil)
  335.  
  336. ;; Construct the Info directory node by merging the files named `dir'
  337. ;; from various directories.  Set the *info* buffer's
  338. ;; default-directory to the first directory we actually get any text
  339. ;; from.
  340. (defun Info-insert-dir ()
  341.   (if (and Info-dir-contents Info-dir-file-attributes
  342.        ;; Verify that none of the files we used has changed
  343.        ;; since we used it.
  344.        (eval (cons 'and
  345.                (mapcar '(lambda (elt)
  346.                   (equal (cdr elt)
  347.                      (file-attributes (car elt))))
  348.                    Info-dir-file-attributes))))
  349.       (insert Info-dir-contents)
  350.     (let ((dirs Info-directory-list)
  351.       buffers buffer others nodes dirs-done)
  352.  
  353.       ;; Search the directory list for the directory file.
  354.       (while dirs
  355.     (let ((truename (file-truename (expand-file-name (car dirs)))))
  356.       (or (member truename dirs-done)
  357.           (member (directory-file-name truename) dirs-done)
  358.           ;; Try several variants of specified name.
  359.           ;; Try upcasing, appending `.info', or both.
  360.           (let* (temp
  361.              (buffer
  362.               (cond
  363.                ((progn (setq temp (expand-file-name "DIR" (car dirs)))
  364.                    (file-exists-p temp))
  365.             (find-file-noselect temp))
  366.                ((progn (setq temp (expand-file-name "dir" (car dirs)))
  367.                    (file-exists-p temp))
  368.             (find-file-noselect temp))
  369.                ((progn (setq temp (expand-file-name "DIR.INFO" (car dirs)))
  370.                    (file-exists-p temp))
  371.             (find-file-noselect temp))
  372.                ((progn (setq temp (expand-file-name "dir.info" (car dirs)))
  373.                    (file-exists-p temp))
  374.             (find-file-noselect temp)))))
  375.         (setq dirs-done
  376.               (cons truename
  377.                 (cons (directory-file-name truename)
  378.                   dirs-done)))
  379.         (if buffer (setq buffers (cons buffer buffers)
  380.                  Info-dir-file-attributes
  381.                  (cons (cons (buffer-file-name buffer)
  382.                          (file-attributes (buffer-file-name buffer)))
  383.                        Info-dir-file-attributes))))))
  384.     (setq dirs (cdr dirs)))
  385.  
  386.       (or buffers
  387.       (error "Can't find the info directory node"))
  388.       ;; Distinguish the dir file that comes with Emacs from all the
  389.       ;; others.  Yes, that is really what this is supposed to do.
  390.       ;; If it doesn't work, fix it.
  391.       (setq buffer (car buffers)
  392.         others (cdr buffers))
  393.  
  394.       ;; Insert the entire original dir file as a start; use its
  395.       ;; default directory as the default directory for the whole
  396.       ;; concatenation.
  397.       (insert-buffer buffer)
  398.       (setq Info-dir-contents-directory (save-excursion
  399.                       (set-buffer buffer)
  400.                       default-directory))
  401.  
  402.       ;; Look at each of the other buffers one by one.
  403.       (while others
  404.     (let ((other (car others)))
  405.       ;; In each, find all the menus.
  406.       (save-excursion
  407.         (set-buffer other)
  408.         (goto-char (point-min))
  409.         ;; Find each menu, and add an elt to NODES for it.
  410.         (while (re-search-forward "^\\* Menu:" nil t)
  411.           (let (beg nodename end)
  412.         (forward-line 1)
  413.         (setq beg (point))
  414.         (search-backward "\n\^_")
  415.         (search-forward "Node: ")
  416.         (setq nodename (Info-following-node-name))
  417.         (search-forward "\n\^_" nil 'move)
  418.         (beginning-of-line)
  419.         (setq end (point))
  420.         (setq nodes (cons (list nodename other beg end) nodes))))))
  421.     (setq others (cdr others)))
  422.       ;; Add to the main menu a menu item for each other node.
  423.       (re-search-forward "^\\* Menu:")
  424.       (forward-line 1)
  425.       (let ((menu-items '("top"))
  426.         (nodes nodes)
  427.         (case-fold-search t)
  428.         (end (save-excursion (search-forward "\^_" nil t) (point))))
  429.     (while nodes
  430.       (let ((nodename (car (car nodes))))
  431.         (save-excursion
  432.           (or (member (downcase nodename) menu-items)
  433.           (re-search-forward (concat "^\\* "
  434.                          (regexp-quote nodename)
  435.                          "::")
  436.                      end t)
  437.           (progn
  438.             (insert "* " nodename "::" "\n")
  439.             (setq menu-items (cons nodename menu-items))))))
  440.       (setq nodes (cdr nodes))))
  441.       ;; Now take each node of each of the other buffers
  442.       ;; and merge it into the main buffer.
  443.       (while nodes
  444.     (let ((nodename (car (car nodes))))
  445.       (goto-char (point-min))
  446.       ;; Find the like-named node in the main buffer.
  447.       (if (re-search-forward (concat "\n\^_.*\n.*Node: "
  448.                      (regexp-quote nodename)
  449.                      "[,\n\t]")
  450.                  nil t)
  451.           (progn
  452.         (search-forward "\n\^_" nil 'move)
  453.         (beginning-of-line))
  454.         ;; If none exists, add one.
  455.         (goto-char (point-max))
  456.         (insert "\^_\nFile: dir\tnode: " nodename "\n\n* Menu:\n\n"))
  457.       ;; Merge the text from the other buffer's menu
  458.       ;; into the menu in the like-named node in the main buffer.
  459.       (apply 'insert-buffer-substring (cdr (car nodes)))
  460.       (insert "\n"))
  461.     (setq nodes (cdr nodes)))
  462.       ;; Kill all the buffers we just made.
  463.       (while buffers
  464.     (kill-buffer (car buffers))
  465.     (setq buffers (cdr buffers))))
  466.     (setq Info-dir-contents (buffer-string)))
  467.   (setq default-directory Info-dir-contents-directory))
  468.  
  469. (defun Info-read-subfile (nodepos)
  470.   (set-buffer (marker-buffer Info-tag-table-marker))
  471.   (goto-char (point-min))
  472.   (search-forward "\n\^_")
  473.   (let (lastfilepos
  474.     lastfilename)
  475.     (forward-line 2)
  476.     (catch 'foo
  477.       (while (not (looking-at "\^_"))
  478.     (if (not (eolp))
  479.         (let ((beg (point))
  480.           thisfilepos thisfilename)
  481.           (search-forward ": ")
  482.           (setq thisfilename  (buffer-substring beg (- (point) 2)))
  483.           (setq thisfilepos (read (current-buffer)))
  484.           ;; read in version 19 stops at the end of number.
  485.           ;; Advance to the next line.
  486.           (forward-line 1)
  487.           (if (> thisfilepos nodepos)
  488.           (throw 'foo t))
  489.           (setq lastfilename thisfilename)
  490.           (setq lastfilepos thisfilepos))
  491.       (forward-line 1))))
  492.     (set-buffer (get-buffer "*info*"))
  493.     (or (equal Info-current-subfile lastfilename)
  494.     (let ((buffer-read-only nil))
  495.       (setq buffer-file-name nil)
  496.       (widen)
  497.       (erase-buffer)
  498.       (info-insert-file-contents lastfilename)
  499.       (set-buffer-modified-p nil)
  500.       (setq Info-current-subfile lastfilename)))
  501.     (goto-char (point-min))
  502.     (search-forward "\n\^_")
  503.     (+ (- nodepos lastfilepos) (point))))
  504.  
  505. ;; Select the info node that point is in.
  506. (defun Info-select-node ()
  507.   (save-excursion
  508.    ;; Find beginning of node.
  509.    (search-backward "\n\^_")
  510.    (forward-line 2)
  511.    ;; Get nodename spelled as it is in the node.
  512.    (re-search-forward "Node:[ \t]*")
  513.    (setq Info-current-node
  514.      (buffer-substring (point)
  515.                (progn
  516.                 (skip-chars-forward "^,\t\n")
  517.                 (point))))
  518.    (Info-set-mode-line)
  519.    ;; Find the end of it, and narrow.
  520.    (beginning-of-line)
  521.    (let (active-expression)
  522.      (narrow-to-region (point)
  523.                (if (re-search-forward "\n[\^_\f]" nil t)
  524.                (prog1
  525.                 (1- (point))
  526.                 (if (looking-at "[\n\^_\f]*execute: ")
  527.                 (progn
  528.                   (goto-char (match-end 0))
  529.                   (setq active-expression
  530.                     (read (current-buffer))))))
  531.              (point-max)))
  532.      (if Info-enable-active-nodes (eval active-expression))
  533.      (if Info-fontify (Info-fontify-node))
  534.      (run-hooks 'Info-selection-hook))))
  535.  
  536. (defun Info-set-mode-line ()
  537.   (setq mode-line-buffer-identification
  538.     (concat
  539.      "Info:  ("
  540.      (if Info-current-file
  541.          (file-name-nondirectory Info-current-file)
  542.        "")
  543.      ")"
  544.      (or Info-current-node ""))))
  545.  
  546. ;; Go to an info node specified with a filename-and-nodename string
  547. ;; of the sort that is found in pointers in nodes.
  548.  
  549. (defun Info-goto-node (nodename)
  550.   "Go to info node named NAME.  Give just NODENAME or (FILENAME)NODENAME."
  551.   (interactive (list (Info-read-node-name "Goto node: ")))
  552.   (let (filename)
  553.     (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
  554.           nodename)
  555.     (setq filename (if (= (match-beginning 1) (match-end 1))
  556.                ""
  557.              (substring nodename (match-beginning 2) (match-end 2)))
  558.       nodename (substring nodename (match-beginning 3) (match-end 3)))
  559.     (let ((trim (string-match "\\s *\\'" filename)))
  560.       (if trim (setq filename (substring filename 0 trim))))
  561.     (let ((trim (string-match "\\s *\\'" nodename)))
  562.       (if trim (setq nodename (substring nodename 0 trim))))
  563.     (Info-find-node (if (equal filename "") nil filename)
  564.             (if (equal nodename "") "Top" nodename))))
  565.  
  566. (defun Info-read-node-name (prompt &optional default)
  567.   (let* ((completion-ignore-case t)
  568.      (nodename (completing-read prompt (Info-build-node-completions))))
  569.     (if (equal nodename "")
  570.     (or default
  571.         (Info-read-node-name prompt))
  572.       nodename)))
  573.  
  574. (defun Info-build-node-completions ()
  575.   (or Info-current-file-completions
  576.       (let ((compl nil))
  577.     (save-excursion
  578.       (save-restriction
  579.         (if (marker-buffer Info-tag-table-marker)
  580.         (progn
  581.           (set-buffer (marker-buffer Info-tag-table-marker))
  582.           (widen)
  583.           (goto-char Info-tag-table-marker)
  584.           (while (re-search-forward "\nNode: \\(.*\\)\177" nil t)
  585.             (setq compl
  586.               (cons (list (buffer-substring (match-beginning 1)
  587.                             (match-end 1)))
  588.                 compl))))
  589.           (widen)
  590.           (goto-char (point-min))
  591.           (while (search-forward "\n\^_" nil t)
  592.         (forward-line 1)
  593.         (let ((beg (point)))
  594.           (forward-line 1)
  595.           (if (re-search-backward "Node: *\\([^,\n]*\\) *[,\n\t]"
  596.                       beg t)
  597.               (setq compl 
  598.                 (cons (list (buffer-substring (match-beginning 1)
  599.                               (match-end 1)))
  600.                   compl))))))))
  601.     (setq Info-current-file-completions compl))))
  602.  
  603. (defun Info-restore-point (hl)
  604.   "If this node has been visited, restore the point value when we left."
  605.   (while hl
  606.     (if (and (equal (nth 0 (car hl)) Info-current-file)
  607.          (equal (nth 1 (car hl)) Info-current-node))
  608.     (progn
  609.       (goto-char (nth 2 (car hl)))
  610.       (setq hl nil))        ;terminate the while at next iter
  611.       (setq hl (cdr hl)))))
  612.  
  613. (defvar Info-last-search nil
  614.   "Default regexp for \\<Info-mode-map>\\[Info-search] command to search for.")
  615.  
  616. (defun Info-search (regexp)
  617.   "Search for REGEXP, starting from point, and select node it's found in."
  618.   (interactive "sSearch (regexp): ")
  619.   (if (equal regexp "")
  620.       (setq regexp Info-last-search)
  621.     (setq Info-last-search regexp))
  622.   (let ((found ()) current
  623.     (onode Info-current-node)
  624.     (ofile Info-current-file)
  625.     (opoint (point))
  626.     (osubfile Info-current-subfile))
  627.     (save-excursion
  628.       (save-restriction
  629.     (widen)
  630.     (if (null Info-current-subfile)
  631.         (progn (re-search-forward regexp) (setq found (point)))
  632.       (condition-case err
  633.           (progn (re-search-forward regexp) (setq found (point)))
  634.         (search-failed nil)))))
  635.     (if (not found) ;can only happen in subfile case -- else would have erred
  636.     (unwind-protect
  637.         (let ((list ()))
  638.           (set-buffer (marker-buffer Info-tag-table-marker))
  639.           (goto-char (point-min))
  640.           (search-forward "\n\^_\nIndirect:")
  641.           (save-restriction
  642.         (narrow-to-region (point)
  643.                   (progn (search-forward "\n\^_")
  644.                      (1- (point))))
  645.         (goto-char (point-min))
  646.         (search-forward (concat "\n" osubfile ": "))
  647.         (beginning-of-line)
  648.         (while (not (eobp))
  649.           (re-search-forward "\\(^.*\\): [0-9]+$")
  650.           (goto-char (+ (match-end 1) 2))
  651.           (setq list (cons (cons (read (current-buffer))
  652.                      (buffer-substring (match-beginning 1)
  653.                                (match-end 1)))
  654.                    list))
  655.           (goto-char (1+ (match-end 0))))
  656.         (setq list (nreverse list)
  657.               current (car (car list))
  658.               list (cdr list)))
  659.           (while list
  660.         (message "Searching subfile %s..." (cdr (car list)))
  661.         (Info-read-subfile (car (car list)))
  662.         (setq list (cdr list))
  663.         (goto-char (point-min))
  664.         (if (re-search-forward regexp nil t)
  665.             (setq found (point) list ())))
  666.           (if found
  667.           (message "")
  668.         (signal 'search-failed (list regexp))))
  669.       (if (not found)
  670.           (progn (Info-read-subfile opoint)
  671.              (goto-char opoint)
  672.              (Info-select-node)))))
  673.     (widen)
  674.     (goto-char found)
  675.     (Info-select-node)
  676.     (or (and (equal onode Info-current-node)
  677.          (equal ofile Info-current-file))
  678.     (setq Info-history (cons (list ofile onode opoint)
  679.                  Info-history)))))
  680.  
  681. ;; Extract the value of the node-pointer named NAME.
  682. ;; If there is none, use ERRORNAME in the error message; 
  683. ;; if ERRORNAME is nil, just return nil.
  684. (defun Info-extract-pointer (name &optional errorname)
  685.   (save-excursion
  686.    (goto-char (point-min))
  687.    (forward-line 1)
  688.    (if (re-search-backward (concat name ":") nil t)
  689.        (progn
  690.      (goto-char (match-end 0))
  691.      (Info-following-node-name))
  692.      (if (eq errorname t)
  693.      nil
  694.        (error (concat "Node has no " (capitalize (or errorname name))))))))
  695.  
  696. ;; Return the node name in the buffer following point.
  697. ;; ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
  698. ;; saying which chas may appear in the node name.
  699. (defun Info-following-node-name (&optional allowedchars)
  700.   (skip-chars-forward " \t")
  701.   (buffer-substring
  702.    (point)
  703.    (progn
  704.      (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
  705.        (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
  706.        (if (looking-at "(")
  707.        (skip-chars-forward "^)")))
  708.      (skip-chars-backward " ")
  709.      (point))))
  710.  
  711. (defun Info-next ()
  712.   "Go to the next node of this node."
  713.   (interactive)
  714.   (Info-goto-node (Info-extract-pointer "next")))
  715.  
  716. (defun Info-prev ()
  717.   "Go to the previous node of this node."
  718.   (interactive)
  719.   (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
  720.  
  721. (defun Info-up ()
  722.   "Go to the superior node of this node."
  723.   (interactive)
  724.   (Info-goto-node (Info-extract-pointer "up"))
  725.   (Info-restore-point Info-history))
  726.  
  727. (defun Info-last ()
  728.   "Go back to the last node visited."
  729.   (interactive)
  730.   (or Info-history
  731.       (error "This is the first Info node you looked at"))
  732.   (let (filename nodename opoint)
  733.     (setq filename (car (car Info-history)))
  734.     (setq nodename (car (cdr (car Info-history))))
  735.     (setq opoint (car (cdr (cdr (car Info-history)))))
  736.     (setq Info-history (cdr Info-history))
  737.     (Info-find-node filename nodename)
  738.     (setq Info-history (cdr Info-history))
  739.     (goto-char opoint)))
  740.  
  741. (defun Info-directory ()
  742.   "Go to the Info directory node."
  743.   (interactive)
  744.   (Info-find-node "dir" "top"))
  745.  
  746. (defun Info-follow-reference (footnotename)
  747.   "Follow cross reference named NAME to the node it refers to.
  748. NAME may be an abbreviation of the reference name."
  749.   (interactive
  750.    (let ((completion-ignore-case t)
  751.      completions default alt-default (start-point (point)) str i bol eol)
  752.      (save-excursion
  753.        ;; Store end and beginning of line.
  754.        (end-of-line)
  755.        (setq eol (point))
  756.        (beginning-of-line)
  757.        (setq bol (point))
  758.  
  759.        (goto-char (point-min))
  760.        (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
  761.      (setq str (buffer-substring
  762.             (match-beginning 1)
  763.             (1- (point))))
  764.      ;; See if this one should be the default.
  765.      (and (null default)
  766.           (<= (match-beginning 0) start-point)
  767.           (<= start-point (point))
  768.           (setq default t))
  769.      ;; See if this one should be the alternate default.
  770.      (and (null alt-default)
  771.           (and (<= bol (match-beginning 0))
  772.            (<= (point) eol))
  773.           (setq alt-default t))
  774.      (setq i 0)
  775.      (while (setq i (string-match "[ \n\t]+" str i))
  776.        (setq str (concat (substring str 0 i) " "
  777.                  (substring str (match-end 0))))
  778.        (setq i (1+ i)))
  779.      ;; Record as a completion and perhaps as default.
  780.      (if (eq default t) (setq default str))
  781.      (if (eq alt-default t) (setq alt-default str))
  782.      (setq completions
  783.            (cons (cons str nil)
  784.              completions))))
  785.      ;; If no good default was found, try an alternate.
  786.      (or default
  787.      (setq default alt-default))
  788.      ;; If only one cross-reference found, then make it default.
  789.      (if (eq (length completions) 1)
  790.          (setq default (car (car completions))))
  791.      (if completions
  792.      (let ((input (completing-read (if default
  793.                        (concat "Follow reference named: ("
  794.                            default ") ")
  795.                      "Follow reference named: ")
  796.                        completions nil t)))
  797.        (list (if (equal input "")
  798.              default input)))
  799.        (error "No cross-references in this node"))))
  800.   (let (target beg i (str (concat "\\*note " footnotename)))
  801.     (while (setq i (string-match " " str i))
  802.       (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
  803.       (setq i (+ i 6)))
  804.     (save-excursion
  805.       (goto-char (point-min))
  806.       (or (re-search-forward str nil t)
  807.       (error "No cross-reference named %s" footnotename))
  808.       (goto-char (+ (match-beginning 0) 5))
  809.       (setq target
  810.         (Info-extract-menu-node-name "Bad format cross reference" t)))
  811.     (while (setq i (string-match "[ \t\n]+" target i))
  812.       (setq target (concat (substring target 0 i) " "
  813.                (substring target (match-end 0))))
  814.       (setq i (+ i 1)))
  815.     (Info-goto-node target)))
  816.  
  817. (defun Info-extract-menu-node-name (&optional errmessage multi-line)
  818.   (skip-chars-forward " \t\n")
  819.   (let ((beg (point))
  820.     str i)
  821.     (skip-chars-forward "^:")
  822.     (forward-char 1)
  823.     (setq str
  824.       (if (looking-at ":")
  825.           (buffer-substring beg (1- (point)))
  826.         (skip-chars-forward " \t\n")
  827.         (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
  828.     (while (setq i (string-match "\n" str i))
  829.       (aset str i ?\ ))
  830.     str))
  831.  
  832. ;; No one calls this and Info-menu-item doesn't exist.
  833. ;;(defun Info-menu-item-sequence (list)
  834. ;;  (while list
  835. ;;    (Info-menu-item (car list))
  836. ;;    (setq list (cdr list))))
  837.  
  838. (defun Info-menu (menu-item)
  839.   "Go to node for menu item named (or abbreviated) NAME.
  840. Completion is allowed, and the menu item point is on is the default."
  841.   (interactive
  842.    (let ((completions '())
  843.      ;; If point is within a menu item, use that item as the default
  844.      (default nil)
  845.      (p (point))
  846.      (last nil))
  847.      (save-excursion
  848.        (goto-char (point-min))
  849.        (if (not (search-forward "\n* menu:" nil t))
  850.        (error "No menu in this node"))
  851.        (while (re-search-forward
  852.         "\n\\* \\([^:\t\n]*\\):" nil t)
  853.      (if (and (null default)
  854.           (prog1 (if last (< last p) nil)
  855.             (setq last (match-beginning 0)))
  856.           (<= p last))
  857.          (setq default (car (car completions))))
  858.      (setq completions (cons (cons (buffer-substring
  859.                      (match-beginning 1)
  860.                      (match-end 1))
  861.                        (match-beginning 1))
  862.                  completions)))
  863.        (if (and (null default) last
  864.         (< last p)
  865.         (<= p (progn (end-of-line) (point))))
  866.        (setq default (car (car completions)))))
  867.      (let ((item nil))
  868.        (while (null item)
  869.      (setq item (let ((completion-ignore-case t))
  870.               (completing-read (if default
  871.                        (format "Menu item (default %s): "
  872.                            default)
  873.                        "Menu item: ")
  874.                        completions nil t)))
  875.      ;; we rely on the fact that completing-read accepts an input
  876.      ;; of "" even when the require-match argument is true and ""
  877.      ;; is not a valid possibility
  878.      (if (string= item "")
  879.          (if default
  880.          (setq item default)
  881.              ;; ask again
  882.              (setq item nil))))
  883.        (list item))))
  884.   ;; there is a problem here in that if several menu items have the same
  885.   ;; name you can only go to the node of the first with this command.
  886.   (Info-goto-node (Info-extract-menu-item menu-item)))
  887.   
  888. (defun Info-extract-menu-item (menu-item)
  889.   (setq menu-item (regexp-quote menu-item))
  890.   (save-excursion
  891.     (goto-char (point-min))
  892.     (or (search-forward "\n* menu:" nil t)
  893.     (error "No menu in this node"))
  894.     (or (re-search-forward (concat "\n\\* " menu-item ":") nil t)
  895.     (re-search-forward (concat "\n\\* " menu-item) nil t)
  896.     (error "No such item in menu"))
  897.     (beginning-of-line)
  898.     (forward-char 2)
  899.     (Info-extract-menu-node-name)))
  900.  
  901. ;; If COUNT is nil, use the last item in the menu.
  902. (defun Info-extract-menu-counting (count)
  903.   (save-excursion
  904.     (goto-char (point-min))
  905.     (or (search-forward "\n* menu:" nil t)
  906.     (error "No menu in this node"))
  907.     (if count
  908.     (or (search-forward "\n* " nil t count)
  909.         (error "Too few items in menu"))
  910.       (while (search-forward "\n* " nil t)
  911.     nil))
  912.     (Info-extract-menu-node-name)))
  913.  
  914. (defun Info-nth-menu-item ()
  915.   "Go to the node of the Nth menu item.
  916. N is the digit argument used to invoke this command."
  917.   (interactive)
  918.   (Info-goto-node
  919.    (Info-extract-menu-counting
  920.     (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
  921.  
  922. (defun Info-top-node ()
  923.   "Go to the Top node of this file."
  924.   (interactive)
  925.   (Info-goto-node "Top"))
  926.  
  927. (defun Info-final-node ()
  928.   "Go to the final node in this file."
  929.   (interactive)
  930.   (Info-goto-node "Top")
  931.   (let (Info-history)
  932.     ;; Go to the last node in the menu of Top.
  933.     (Info-goto-node (Info-extract-menu-counting nil))
  934.     ;; If the last node in the menu is not last in pointer structure,
  935.     ;; move forward until we can't go any farther. 
  936.     (while (Info-forward-node t t) nil)
  937.     ;; Then keep moving down to last subnode, unless we reach an index.
  938.     (while (and (not (string-match "\\<index\\>" Info-current-node))
  939.         (save-excursion (search-forward "\n* Menu:" nil t)))
  940.       (Info-goto-node (Info-extract-menu-counting nil)))))
  941.  
  942. (defun Info-forward-node (&optional not-down no-error)
  943.   "Go forward one node, considering all nodes as forming one sequence."
  944.   (interactive)
  945.   (goto-char (point-min))
  946.   (forward-line 1)
  947.   ;; three possibilities, in order of priority:
  948.   ;;     1. next node is in a menu in this node (but not in an index)
  949.   ;;     2. next node is next at same level
  950.   ;;     3. next node is up and next
  951.   (cond ((and (not not-down)
  952.               (save-excursion (search-forward "\n* menu:" nil t))
  953.           (not (string-match "\\<index\\>" Info-current-node)))
  954.      (Info-goto-node (Info-extract-menu-counting 1))
  955.          t)
  956.         ((save-excursion (search-backward "next:" nil t))
  957.          (Info-next)
  958.          t)
  959.         ((and (save-excursion (search-backward "up:" nil t))
  960.           (not (equal (downcase (Info-extract-pointer "up")) "top")))
  961.          (let ((old-node Info-current-node))
  962.            (Info-up)
  963.            (let (Info-history success)
  964.              (unwind-protect
  965.                  (setq success (Info-forward-node t no-error))
  966.                (or success (Info-goto-node old-node))))))
  967.         (no-error nil)
  968.         (t (error "No pointer forward from this node"))))
  969.  
  970. (defun Info-backward-node ()
  971.   "Go backward one node, considering all nodes as forming one sequence."
  972.   (interactive)
  973.   (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
  974.     (upnode (Info-extract-pointer "up" t)))
  975.     (cond ((and upnode (string-match "(" upnode))
  976.        (error "First node in file"))
  977.       ((and upnode (or (null prevnode)
  978.                (equal (downcase prevnode) (downcase upnode))))
  979.        (Info-up))
  980.       (prevnode
  981.        ;; If we move back at the same level,
  982.        ;; go down to find the last subnode*.
  983.        (Info-prev)
  984.        (let (Info-history)
  985.          (while (and (not (string-match "\\<index\\>" Info-current-node))
  986.              (save-excursion (search-forward "\n* Menu:" nil t)))
  987.            (Info-goto-node (Info-extract-menu-counting nil)))))
  988.       (t
  989.        (error "No pointer backward from this node")))))
  990.  
  991. (defun Info-exit ()
  992.   "Exit Info by selecting some other buffer."
  993.   (interactive)
  994.   (if Info-standalone
  995.       (save-buffers-kill-emacs)
  996.     (switch-to-buffer (prog1 (other-buffer (current-buffer))
  997.             (bury-buffer (current-buffer))))))
  998.  
  999. (defun Info-next-menu-item ()
  1000.   (interactive)
  1001.   (save-excursion
  1002.     (forward-line -1)
  1003.     (search-forward "\n* menu:" nil t)
  1004.     (or (search-forward "\n* " nil t)
  1005.     (error "No more items in menu"))
  1006.     (Info-goto-node (Info-extract-menu-node-name))))
  1007.  
  1008. (defun Info-last-menu-item ()
  1009.   (interactive)
  1010.   (save-excursion
  1011.     (forward-line 1)
  1012.     (search-backward "\n* menu:" nil t)
  1013.     (or (search-backward "\n* " nil t)
  1014.     (error "No previous items in menu"))
  1015.     (Info-goto-node (Info-extract-menu-node-name))))
  1016.  
  1017. (defmacro Info-no-error (&rest body)
  1018.   (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
  1019.  
  1020. (defun Info-next-preorder ()
  1021.   "Go to the next node, popping up a level if there is none."
  1022.   (interactive)
  1023.   (cond ((Info-no-error (Info-next-menu-item))    )
  1024.     ((Info-no-error (Info-up))        (forward-line 1))
  1025.     (t                     (error "No more nodes"))))
  1026.  
  1027. (defun Info-last-preorder ()
  1028.   "Go to the last node, popping up a level if there is none."
  1029.   (interactive)
  1030.   (cond ((Info-no-error (Info-last-menu-item))    )
  1031.     ((Info-no-error (Info-up))        (forward-line -1))
  1032.     (t                     (error "No previous nodes"))))
  1033.  
  1034. (defun Info-scroll-up ()
  1035.   "Read the next screen.  If end of buffer is visible, go to next entry."
  1036.   (interactive)
  1037.   (if (pos-visible-in-window-p (point-max))
  1038.       (Info-next-preorder)
  1039.     (scroll-up)))
  1040.  
  1041. (defun Info-scroll-down ()
  1042.   "Read the previous screen.  If start of buffer is visible, go to last entry."
  1043.   (interactive)
  1044.   (if (pos-visible-in-window-p (point-min))
  1045.       (Info-last-preorder)
  1046.     (scroll-down)))
  1047.  
  1048. (defun Info-next-reference ()
  1049.   "Move cursor to the next cross-reference or menu item in the node."
  1050.   (interactive)
  1051.   (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
  1052.     (old-pt (point)))
  1053.     (or (eobp) (forward-char 1))
  1054.     (or (re-search-forward pat nil t)
  1055.     (progn
  1056.       (goto-char (point-min))
  1057.       (or (re-search-forward pat nil t)
  1058.           (progn
  1059.         (goto-char old-pt)
  1060.         (error "No cross references in this node")))))
  1061.     (goto-char (match-beginning 0))
  1062.     (if (looking-at "\\* Menu:")
  1063.     (Info-next-reference))))
  1064.  
  1065. (defun Info-prev-reference ()
  1066.   "Move cursor to the previous cross-reference or menu item in the node."
  1067.   (interactive)
  1068.   (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
  1069.     (old-pt (point)))
  1070.     (or (re-search-backward pat nil t)
  1071.     (progn
  1072.       (goto-char (point-max))
  1073.       (or (re-search-backward pat nil t)
  1074.           (progn
  1075.         (goto-char old-pt)
  1076.         (error "No cross references in this node")))))
  1077.     (goto-char (match-beginning 0))
  1078.     (if (looking-at "\\* Menu:")
  1079.     (Info-prev-reference))))
  1080.  
  1081. (defun Info-index (topic)
  1082.   "Look up a string in the index for this file.
  1083. The index is defined as the first node in the top-level menu whose
  1084. name contains the word \"Index\", plus any immediately following
  1085. nodes whose names also contain the word \"Index\".
  1086. If there are no exact matches to the specified topic, this chooses
  1087. the first match which is a case-insensitive substring of a topic.
  1088. Use the `,' command to see the other matches.
  1089. Give a blank topic name to go to the Index node itself."
  1090.   (interactive "sIndex topic: ")
  1091.   (let ((orignode Info-current-node)
  1092.     (rnode nil)
  1093.     (pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ t]*\\([0-9]*\\)"
  1094.              (regexp-quote topic)))
  1095.     node)
  1096.     (Info-goto-node "Top")
  1097.     (or (search-forward "\n* menu:" nil t)
  1098.     (error "No index"))
  1099.     (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
  1100.     (error "No index"))
  1101.     (goto-char (match-beginning 1))
  1102.     (let ((Info-keeping-history nil))
  1103.       (Info-goto-node (Info-extract-menu-node-name)))
  1104.     (or (equal topic "")
  1105.     (let ((matches nil)
  1106.           (exact nil)
  1107.           (Info-keeping-history nil)
  1108.           found)
  1109.       (while
  1110.           (progn
  1111.         (goto-char (point-min))
  1112.         (while (re-search-forward pattern nil t)
  1113.           (setq matches
  1114.             (cons (list (buffer-substring (match-beginning 1)
  1115.                               (match-end 1))
  1116.                     (buffer-substring (match-beginning 2)
  1117.                               (match-end 2))
  1118.                     Info-current-node
  1119.                     (string-to-int (concat "0"
  1120.                                (buffer-substring
  1121.                                 (match-beginning 3)
  1122.                                 (match-end 3)))))
  1123.                   matches)))
  1124.         (and (setq node (Info-extract-pointer "next" t))
  1125.              (string-match "\\<Index\\>" node)))
  1126.         (Info-goto-node node))
  1127.       (or matches
  1128.           (progn
  1129.         (Info-last)
  1130.         (error "No \"%s\" in index" topic)))
  1131.       ;; Here it is a feature that assoc is case-sensitive.
  1132.       (while (setq found (assoc topic matches))
  1133.         (setq exact (cons found exact)
  1134.           matches (delq found matches)))
  1135.       (setq Info-index-alternatives (nconc exact (nreverse matches)))
  1136.       (Info-index-next 0)))))
  1137.  
  1138. (defun Info-index-next (num)
  1139.   "Go to the next matching index item from the last `i' command."
  1140.   (interactive "p")
  1141.   (or Info-index-alternatives
  1142.       (error "No previous `i' command in this file"))
  1143.   (while (< num 0)
  1144.     (setq num (+ num (length Info-index-alternatives))))
  1145.   (while (> num 0)
  1146.     (setq Info-index-alternatives
  1147.       (nconc (cdr Info-index-alternatives)
  1148.          (list (car Info-index-alternatives)))
  1149.       num (1- num)))
  1150.   (Info-goto-node (nth 1 (car Info-index-alternatives)))
  1151.   (if (> (nth 3 (car Info-index-alternatives)) 0)
  1152.       (forward-line (nth 3 (car Info-index-alternatives)))
  1153.     (forward-line 3)  ; don't search in headers
  1154.     (let ((name (car (car Info-index-alternatives))))
  1155.       (if (or (re-search-forward (format
  1156.                   "\\(Function\\|Command\\): %s\\( \\|$\\)"
  1157.                   (regexp-quote name)) nil t)
  1158.           (search-forward (format "`%s'" name) nil t)
  1159.           (and (string-match "\\`.*\\( (.*)\\)\\'" name)
  1160.            (search-forward
  1161.             (format "`%s'" (substring name 0 (match-beginning 1)))
  1162.             nil t))
  1163.           (search-forward name nil t))
  1164.       (beginning-of-line)
  1165.     (goto-char (point-min)))))
  1166.   (message "Found \"%s\" in %s.  %s"
  1167.        (car (car Info-index-alternatives))
  1168.        (nth 2 (car Info-index-alternatives))
  1169.        (if (cdr Info-index-alternatives)
  1170.            "(Press `,' for more)"
  1171.          "(Only match)")))
  1172.  
  1173. (defun Info-undefined ()
  1174.   "Make command be undefined in Info."
  1175.   (interactive)
  1176.   (ding))
  1177.  
  1178. (defun Info-help ()
  1179.   "Enter the Info tutorial."
  1180.   (interactive)
  1181.   (delete-other-windows)
  1182.   (Info-find-node "info"
  1183.           (if (< (window-height) 23)
  1184.               "Help-Small-Screen"
  1185.             "Help")))
  1186.  
  1187. (defun Info-summary ()
  1188.   "Display a brief summary of all Info commands."
  1189.   (interactive)
  1190.   (save-window-excursion
  1191.     (switch-to-buffer "*Help*")
  1192.     (erase-buffer)
  1193.     (insert (documentation 'Info-mode))
  1194.     (goto-char (point-min))
  1195.     (let (ch flag)
  1196.       (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
  1197.             (message (if flag "Type Space to see more"
  1198.                    "Type Space to return to Info"))
  1199.             (if (not (eq ?\  (setq ch (read-event))))
  1200.             (progn (setq unread-command-events (list ch)) nil)
  1201.               flag))
  1202.     (scroll-up)))
  1203.     (bury-buffer "*Help*")))
  1204.  
  1205. (defun Info-get-token (pos start all &optional errorstring)
  1206.   "Return the token around POS,
  1207. POS must be somewhere inside the token
  1208. START is a regular expression which will match the
  1209.     beginning of the tokens delimited string
  1210. ALL is a regular expression with a single
  1211.     parenthized subpattern which is the token to be
  1212.     returned. E.g. '{\(.*\)}' would return any string
  1213.     enclosed in braces around POS.
  1214. SIG optional fourth argument, controls action on no match
  1215.     nil: return nil
  1216.     t: beep
  1217.     a string: signal an error, using that string."
  1218.   (save-excursion
  1219.     (goto-char pos)
  1220.     (re-search-backward start (max (point-min) (- pos 200)) 'yes)
  1221.     (let (found)
  1222.       (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
  1223.           (not (setq found (and (<= (match-beginning 0) pos)
  1224.                     (> (match-end 0) pos))))))
  1225.       (if (and found (<= (match-beginning 0) pos)
  1226.            (> (match-end 0) pos))
  1227.       (buffer-substring (match-beginning 1) (match-end 1))
  1228.     (cond ((null errorstring)
  1229.            nil)
  1230.           ((eq errorstring t)
  1231.            (beep)
  1232.            nil)
  1233.           (t
  1234.            (error "No %s around position %d" errorstring pos)))))))
  1235.  
  1236. (defun Info-mouse-follow-nearest-node (click)
  1237.   "\\<Info-mode-map>Follow a node reference near point.
  1238. Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
  1239. At end of the node's text, moves to the next node, or up if none."
  1240.   (interactive "e")
  1241.   (let* ((start (event-start click))
  1242.      (window (car start))
  1243.      (pos (car (cdr start))))
  1244.     (select-window window)
  1245.     (goto-char pos))
  1246.   (and (not (Info-try-follow-nearest-node))
  1247.        (save-excursion (forward-line 1) (eobp))
  1248.        (Info-next-preorder)))
  1249.  
  1250. (defun Info-follow-nearest-node ()
  1251.   "\\<Info-mode-map>Follow a node reference near point.
  1252. Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
  1253. If no reference to follow, moves to the next node, or up if none."
  1254.   (interactive)
  1255.   (or (Info-try-follow-nearest-node)
  1256.       (Info-next-preorder)))
  1257.  
  1258. ;; Common subroutine.
  1259. (defun Info-try-follow-nearest-node ()
  1260.   "Follow a node reference near point.  Return non-nil if successful."
  1261.   (let (node)
  1262.     (cond
  1263.      ((setq node (Info-get-token (point) "\\*note[ \n]"
  1264.                  "\\*note[ \n]\\([^:]*\\):"))
  1265.       (Info-follow-reference node))
  1266.      ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::"))
  1267.       (Info-goto-node node))
  1268.      ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):"))
  1269.       (Info-menu node))
  1270.      ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
  1271.       (Info-goto-node node))
  1272.      ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
  1273.       (Info-goto-node node))
  1274.      ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
  1275.       (Info-goto-node "Top"))
  1276.      ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
  1277.       (Info-goto-node node)))
  1278.     node))
  1279.  
  1280. (defvar Info-mode-map nil
  1281.   "Keymap containing Info commands.")
  1282. (if Info-mode-map
  1283.     nil
  1284.   (setq Info-mode-map (make-keymap))
  1285.   (suppress-keymap Info-mode-map)
  1286.   (define-key Info-mode-map "." 'beginning-of-buffer)
  1287.   (define-key Info-mode-map " " 'Info-scroll-up)
  1288.   (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
  1289.   (define-key Info-mode-map "\t" 'Info-next-reference)
  1290.   (define-key Info-mode-map "\e\t" 'Info-prev-reference)
  1291.   (define-key Info-mode-map "1" 'Info-nth-menu-item)
  1292.   (define-key Info-mode-map "2" 'Info-nth-menu-item)
  1293.   (define-key Info-mode-map "3" 'Info-nth-menu-item)
  1294.   (define-key Info-mode-map "4" 'Info-nth-menu-item)
  1295.   (define-key Info-mode-map "5" 'Info-nth-menu-item)
  1296.   (define-key Info-mode-map "6" 'Info-nth-menu-item)
  1297.   (define-key Info-mode-map "7" 'Info-nth-menu-item)
  1298.   (define-key Info-mode-map "8" 'Info-nth-menu-item)
  1299.   (define-key Info-mode-map "9" 'Info-nth-menu-item)
  1300.   (define-key Info-mode-map "0" 'undefined)
  1301.   (define-key Info-mode-map "?" 'Info-summary)
  1302.   (define-key Info-mode-map "]" 'Info-forward-node)
  1303.   (define-key Info-mode-map "[" 'Info-backward-node)
  1304.   (define-key Info-mode-map "<" 'Info-top-node)
  1305.   (define-key Info-mode-map ">" 'Info-final-node)
  1306.   (define-key Info-mode-map "b" 'beginning-of-buffer)
  1307.   (define-key Info-mode-map "d" 'Info-directory)
  1308.   (define-key Info-mode-map "e" 'Info-edit)
  1309.   (define-key Info-mode-map "f" 'Info-follow-reference)
  1310.   (define-key Info-mode-map "g" 'Info-goto-node)
  1311.   (define-key Info-mode-map "h" 'Info-help)
  1312.   (define-key Info-mode-map "i" 'Info-index)
  1313.   (define-key Info-mode-map "l" 'Info-last)
  1314.   (define-key Info-mode-map "m" 'Info-menu)
  1315.   (define-key Info-mode-map "n" 'Info-next)
  1316.   (define-key Info-mode-map "p" 'Info-prev)
  1317.   (define-key Info-mode-map "q" 'Info-exit)
  1318.   (define-key Info-mode-map "s" 'Info-search)
  1319.   (define-key Info-mode-map "t" 'Info-top-node)
  1320.   (define-key Info-mode-map "u" 'Info-up)
  1321.   (define-key Info-mode-map "," 'Info-index-next)
  1322.   (define-key Info-mode-map "\177" 'Info-scroll-down)
  1323.   (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
  1324.   )
  1325.  
  1326. ;; Info mode is suitable only for specially formatted data.
  1327. (put 'info-mode 'mode-class 'special)
  1328.  
  1329. (defun Info-mode ()
  1330.   "\\<Info-mode-map>
  1331. Info mode provides commands for browsing through the Info documentation tree.
  1332. Documentation in Info is divided into \"nodes\", each of which discusses
  1333. one topic and contains references to other nodes which discuss related
  1334. topics.  Info has commands to follow the references and show you other nodes.
  1335.  
  1336. \\[Info-help]    Invoke the Info tutorial.
  1337.  
  1338. Selecting other nodes:
  1339. \\[Info-next]    Move to the \"next\" node of this node.
  1340. \\[Info-prev]    Move to the \"previous\" node of this node.
  1341. \\[Info-up]    Move \"up\" from this node.
  1342. \\[Info-menu]    Pick menu item specified by name (or abbreviation).
  1343.     Picking a menu item causes another node to be selected.
  1344. \\[Info-directory]    Go to the Info directory node.
  1345. \\[Info-follow-reference]    Follow a cross reference.  Reads name of reference.
  1346. \\[Info-last]    Move to the last node you were at.
  1347. \\[Info-index]    Look up a topic in this file's Index and move to that node.
  1348. \\[Info-index-next]    (comma) Move to the next match from a previous `i' command.
  1349.  
  1350. Moving within a node:
  1351. \\[Info-scroll-up]    Normally, scroll forward a full screen.  If the end of the buffer is
  1352. already visible, try to go to the next menu entry, or up if there is none.
  1353. \\[Info-scroll-down]  Normally, scroll backward.  If the beginning of the buffer is
  1354. already visible, try to go to the previous menu entry, or up if there is none.
  1355. \\[beginning-of-buffer]    Go to beginning of node.  
  1356.  
  1357. Advanced commands:
  1358. \\[Info-exit]    Quit Info: reselect previously selected buffer.
  1359. \\[Info-edit]    Edit contents of selected node.
  1360. 1    Pick first item in node's menu.
  1361. 2, 3, 4, 5   Pick second ... fifth item in node's menu.
  1362. \\[Info-goto-node]    Move to node specified by name.
  1363.     You may include a filename as well, as (FILENAME)NODENAME.
  1364. \\[universal-argument] \\[info]    Move to new Info file with completion.
  1365. \\[Info-search]    Search through this Info file for specified regexp,
  1366.     and select the node in which the next occurrence is found.
  1367. \\[Info-next-preorder]    Next-preorder; that is, try to go to the next menu item,
  1368.     and if that fails try to move up, and if that fails, tell user
  1369.      he/she is done reading.
  1370. \\[Info-next-reference]    Move cursor to next cross-reference or menu item.
  1371. \\[Info-prev-reference]    Move cursor to previous cross-reference or menu item."
  1372.   (kill-all-local-variables)
  1373.   (setq major-mode 'Info-mode)
  1374.   (setq mode-name "Info")
  1375.   (use-local-map Info-mode-map)
  1376.   (set-syntax-table text-mode-syntax-table)
  1377.   (setq local-abbrev-table text-mode-abbrev-table)
  1378.   (setq case-fold-search t)
  1379.   (setq buffer-read-only t)
  1380.   (make-local-variable 'Info-current-file)
  1381.   (make-local-variable 'Info-current-subfile)
  1382.   (make-local-variable 'Info-current-node)
  1383.   (make-local-variable 'Info-tag-table-marker)
  1384.   (make-local-variable 'Info-history)
  1385.   (make-local-variable 'Info-index-alternatives)
  1386.   (if (fboundp 'make-face)
  1387.       (progn
  1388.     (make-face 'info-node)
  1389.     (make-face 'info-menu-5)
  1390.     (make-face 'info-xref)
  1391.     (or (face-differs-from-default-p 'info-node)
  1392.         (if (face-differs-from-default-p 'bold-italic)
  1393.         (copy-face 'bold-italic 'info-node)
  1394.           (copy-face 'bold 'info-node)))
  1395.     (or (face-differs-from-default-p 'info-menu-5)
  1396.         (set-face-underline-p 'info-menu-5 t))
  1397.     (or (face-differs-from-default-p 'info-xref)
  1398.         (copy-face 'bold 'info-xref)))
  1399.     (setq Info-fontify nil))
  1400.   (Info-set-mode-line)
  1401.   (run-hooks 'Info-mode-hook))
  1402.  
  1403. (defvar Info-edit-map nil
  1404.   "Local keymap used within `e' command of Info.")
  1405. (if Info-edit-map
  1406.     nil
  1407.   (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
  1408.   (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
  1409.  
  1410. ;; Info-edit mode is suitable only for specially formatted data.
  1411. (put 'info-edit-mode 'mode-class 'special)
  1412.  
  1413. (defun Info-edit-mode ()
  1414.   "Major mode for editing the contents of an Info node.
  1415. Like text mode with the addition of `Info-cease-edit'
  1416. which returns to Info mode for browsing.
  1417. \\{Info-edit-map}"
  1418.   )
  1419.  
  1420. (defun Info-edit ()
  1421.   "Edit the contents of this Info node.
  1422. Allowed only if variable `Info-enable-edit' is non-nil."
  1423.   (interactive)
  1424.   (or Info-enable-edit
  1425.       (error "Editing info nodes is not enabled"))
  1426.   (use-local-map Info-edit-map)
  1427.   (setq major-mode 'Info-edit-mode)
  1428.   (setq mode-name "Info Edit")
  1429.   (kill-local-variable 'mode-line-buffer-identification)
  1430.   (setq buffer-read-only nil)
  1431.   ;; Make mode line update.
  1432.   (set-buffer-modified-p (buffer-modified-p))
  1433.   (message (substitute-command-keys
  1434.          "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
  1435.  
  1436. (defun Info-cease-edit ()
  1437.   "Finish editing Info node; switch back to Info proper."
  1438.   (interactive)
  1439.   ;; Do this first, so nothing has changed if user C-g's at query.
  1440.   (and (buffer-modified-p)
  1441.        (y-or-n-p "Save the file? ")
  1442.        (save-buffer))
  1443.   (use-local-map Info-mode-map)
  1444.   (setq major-mode 'Info-mode)
  1445.   (setq mode-name "Info")
  1446.   (Info-set-mode-line)
  1447.   (setq buffer-read-only t)
  1448.   ;; Make mode line update.
  1449.   (set-buffer-modified-p (buffer-modified-p))
  1450.   (and (marker-position Info-tag-table-marker)
  1451.        (buffer-modified-p)
  1452.        (message "Tags may have changed.  Use Info-tagify if necessary")))
  1453.  
  1454. (defun Info-find-emacs-command-nodes (command)
  1455.   "Return a list of locations documenting COMMAND in the Emacs Info manual.
  1456. The locations are of the format used in Info-history, i.e.
  1457. \(FILENAME NODENAME BUFFERPOS\)."
  1458.   (require 'info)
  1459.   (let ((where '())
  1460.     (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
  1461.               ":\\s *\\(.*\\)\\.$")))
  1462.     (save-excursion
  1463.       (Info-find-node "emacs" "Command Index")
  1464.       ;; Take the index node off the Info history.
  1465.       (setq Info-history (cdr Info-history))
  1466.       (goto-char (point-max))
  1467.       (while (re-search-backward cmd-desc nil t)
  1468.       (setq where (cons (list Info-current-file
  1469.                   (buffer-substring
  1470.                    (match-beginning 1)
  1471.                    (match-end 1))
  1472.                   0)
  1473.                 where)))
  1474.       where)))
  1475.  
  1476. ;;;###autoload
  1477. (defun Info-goto-emacs-command-node (command)
  1478.   "Go to the Info node in the Emacs manual for command COMMAND.
  1479. The command is found by looking up in Emacs manual's Command Index."
  1480.   (interactive "CFind documentation for command: ")
  1481.   (or (commandp command)
  1482.       (signal 'wrong-type-argument (list 'commandp command)))
  1483.   (let ((where (Info-find-emacs-command-nodes command)))
  1484.     (if where
  1485.     (let ((num-matches (length where)))
  1486.       ;; Get Info running, and pop to it in another window.
  1487.       (save-window-excursion
  1488.         (info))
  1489.       (pop-to-buffer "*info*")
  1490.       (Info-find-node (car (car where))
  1491.               (car (cdr (car where))))
  1492.       (if (> num-matches 1)
  1493.           (progn
  1494.         ;; Info-find-node already pushed (car where) onto
  1495.         ;; Info-history.  Put the other nodes that were found on
  1496.         ;; the history.
  1497.         (setq Info-history (nconc (cdr where) Info-history))
  1498.         (message (substitute-command-keys
  1499.               "Found %d other entr%s.  Use \\[Info-last] to see %s.")
  1500.              (1- num-matches)
  1501.              (if (> num-matches 2) "ies" "y")
  1502.              (if (> num-matches 2) "them" "it")))))
  1503.       (error "Couldn't find documentation for %s." command))))
  1504.  
  1505. ;;;###autoload
  1506. (defun Info-goto-emacs-key-command-node (key)
  1507.   "Go to the Info node in the Emacs manual the command bound to KEY, a string.
  1508. Interactively, if the binding is execute-extended-command, a command is read.
  1509. The command is found by looking up in Emacs manual's Command Index."
  1510.   (interactive "kFind documentation for key:")
  1511.   (let ((command (key-binding key)))
  1512.     (cond ((null command)
  1513.        (message "%s is undefined" (key-description key)))
  1514.       ((and (interactive-p)
  1515.         (eq command 'execute-extended-command))
  1516.        (Info-goto-emacs-command-node
  1517.         (read-command "Find documentation for command: ")))
  1518.       (t
  1519.        (Info-goto-emacs-command-node command)))))
  1520.  
  1521. (defun Info-fontify-node ()
  1522.   (save-excursion
  1523.     (let ((buffer-read-only nil))
  1524.       (goto-char (point-min))
  1525.       (if (looking-at "^File: [^,: \t]+,?[ \t]+")
  1526.       (progn
  1527.         (goto-char (match-end 0))
  1528.         (while
  1529.         (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
  1530.           (goto-char (match-end 0))
  1531.           (put-text-property (match-beginning 1) (match-end 1)
  1532.                  'face 'info-xref)
  1533.           (put-text-property (match-beginning 1) (match-end 1)
  1534.                  'mouse-face 'highlight))))
  1535.       (goto-char (point-min))
  1536.       (while (re-search-forward "\\*Note[ \n\t]*\\([^:]*\\):" nil t)
  1537.     (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
  1538.         nil
  1539.       (put-text-property (match-beginning 1) (match-end 1)
  1540.                  'face 'info-xref)
  1541.       (put-text-property (match-beginning 1) (match-end 1)
  1542.                  'mouse-face 'highlight)))
  1543.       (goto-char (point-min))
  1544.       (if (and (search-forward "\n* Menu:" nil t)
  1545.            (not (string-match "\\<Index\\>" Info-current-node))
  1546.            ;; Don't take time to annotate huge menus
  1547.            (< (- (point-max) (point)) 30000))
  1548.       (let ((n 0))
  1549.         (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
  1550.           (setq n (1+ n))
  1551.           (if (memq n '(5 9))   ; visual aids to help with 1-9 keys
  1552.           (put-text-property (match-beginning 0)
  1553.                      (1+ (match-beginning 0))
  1554.                      'face 'info-menu-5))
  1555.           (put-text-property (match-beginning 1) (match-end 1)
  1556.                  'face 'info-node)
  1557.           (put-text-property (match-beginning 1) (match-end 1)
  1558.                  'mouse-face 'highlight))))
  1559.       (set-buffer-modified-p nil))))
  1560.  
  1561. (provide 'info)
  1562.  
  1563. ;;; info.el ends here
  1564.