1;;; info.el --- info package for Emacs.
2
3;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 97 Free Software
4;; Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: help
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27
28;; Note that nowadays we expect info files to be made using makeinfo.
29
30;;; Code:
31
32(defgroup info nil
33  "Info subsystem"
34  :group 'help
35  :group 'docs)
36
37
38(defvar Info-history nil
39  "List of info nodes user has visited.
40Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
41
42(defcustom Info-enable-edit nil
43  "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info can edit the current node.
44This is convenient if you want to write info files by hand.
45However, we recommend that you not do this.
46It is better to write a Texinfo file and generate the Info file from that,
47because that gives you a printed manual as well."
48  :type 'boolean
49  :group 'info)
50
51(defvar Info-enable-active-nodes nil
52  "Non-nil allows Info to execute Lisp code associated with nodes.
53The Lisp code is executed when the node is selected.")
54(put 'Info-enable-active-nodes 'risky-local-variable t)
55
56(defcustom Info-fontify t
57  "*Non-nil enables highlighting and fonts in Info nodes."
58  :type 'boolean
59  :group 'info)
60
61(defcustom Info-fontify-maximum-menu-size 30000
62  "*Maximum size of menu to fontify if `Info-fontify' is non-nil."
63  :type 'integer
64  :group 'info)
65
66(defvar Info-directory-list
67  (let ((path (getenv "INFOPATH"))
68	;; This is for older Emacs versions
69	;; which might get this info.el from the Texinfo distribution.
70	(path-separator (if (boundp 'path-separator) path-separator
71			  (if (eq system-type 'ms-dos) ";" ":")))
72	(source (expand-file-name "info/" source-directory))
73	(sibling (if installation-directory
74		     (expand-file-name "info/" installation-directory)))
75	alternative)
76    (if path
77	(let ((list nil)
78	      idx)
79	  (while (> (length path) 0)
80	    (setq idx (or (string-match path-separator path) (length path))
81		  list (cons (substring path 0 idx) list)
82		  path (substring path (min (1+ idx)
83					    (length path)))))
84	  (nreverse list))
85      (if (and sibling (file-exists-p sibling))
86	  (setq alternative sibling)
87	(setq alternative source))
88      (if (or (member alternative Info-default-directory-list)
89	      (not (file-exists-p alternative))
90	      ;; On DOS/NT, we use movable executables always,
91	      ;; and we must always find the Info dir at run time.
92	      (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
93		  nil
94		;; Use invocation-directory for Info only if we used it for
95		;; exec-directory also.
96		(not (string= exec-directory
97			      (expand-file-name "lib-src/"
98						installation-directory)))))
99	  Info-default-directory-list
100	(reverse (cons alternative
101		       (cdr (reverse Info-default-directory-list)))))))
102  "List of directories to search for Info documentation files.
103nil means not yet initialized.  In this case, Info uses the environment
104variable INFOPATH to initialize it, or `Info-default-directory-list'
105if there is no INFOPATH variable in the environment.
106The last element of `Info-default-directory-list' is the directory
107where Emacs installs the Info files that come with it.
108
109If you run the Emacs executable from the `src' directory in the Emacs
110source tree, the `info' directory in the source tree is used as the last
111element, in place of the installation Info directory.  This is useful
112when you run a version of Emacs without installing it.")
113
114(defcustom Info-additional-directory-list nil
115  "List of additional directories to search for Info documentation files.
116These directories are not searched for merging the `dir' file."
117  :type '(repeat directory)
118  :group 'info)
119
120(defvar Info-current-file nil
121  "Info file that Info is now looking at, or nil.
122This is the name that was specified in Info, not the actual file name.
123It doesn't contain directory names or file name extensions added by Info.")
124
125(defvar Info-current-subfile nil
126  "Info subfile that is actually in the *info* buffer now,
127or nil if current info file is not split into subfiles.")
128
129(defvar Info-current-node nil
130  "Name of node that Info is now looking at, or nil.")
131
132(defvar Info-tag-table-marker nil
133  "Marker pointing at beginning of current Info file's tag table.
134Marker points nowhere if file has no tag table.")
135
136(defvar Info-tag-table-buffer nil
137  "Buffer used for indirect tag tables.")
138
139(defvar Info-current-file-completions nil
140  "Cached completion list for current Info file.")
141
142(defvar Info-index-alternatives nil
143  "List of possible matches for last Info-index command.")
144
145(defvar Info-standalone nil
146  "Non-nil if Emacs was started solely as an Info browser.")
147
148(defvar Info-suffix-list
149  (if (eq system-type 'ms-dos)
150      '( (".gz"      . "gunzip")
151	 (".z"       . "gunzip")
152	 (".inf"     . nil)
153	 (""         . nil))
154    '( (".info.Z".    "uncompress")
155       (".info.Y".    "unyabba")
156       (".info.gz".   "gunzip")
157       (".info.z".    "gunzip")
158       (".info".      nil)
159       ("-info.Z".   "uncompress")
160       ("-info.Y".   "unyabba")
161       ("-info.gz".  "gunzip")
162       ("-info.z".   "gunzip")
163       ("-info".     nil)
164       ("/index.Z".   "uncompress")
165       ("/index.Y".   "unyabba")
166       ("/index.gz".  "gunzip")
167       ("/index.z".   "gunzip")
168       ("/index".     nil)
169       (".Z".         "uncompress")
170       (".Y".         "unyabba")
171       (".gz".        "gunzip")
172       (".z".         "gunzip")
173       ("".           nil)))
174  "List of file name suffixes and associated decoding commands.
175Each entry should be (SUFFIX . STRING); the file is given to
176the command as standard input.  If STRING is nil, no decoding is done.
177Because the SUFFIXes are tried in order, the empty string should
178be last in the list.")
179
180;; Concatenate SUFFIX onto FILENAME.  SUFFIX should start with a dot.
181;; First, on ms-dos, delete some of the extension in FILENAME
182;; to make room.
183(defun info-insert-file-contents-1 (filename suffix)
184  (if (not (eq system-type 'ms-dos))
185      (concat filename suffix)
186    (let* ((sans-exts (file-name-sans-extension filename))
187	   ;; How long is the extension in FILENAME (not counting the dot).
188	   (ext-len (max 0 (- (length filename) (length sans-exts) 1)))
189	   ext-left)
190      ;; SUFFIX starts with a dot.  If FILENAME already has one,
191      ;; get rid of the one in SUFFIX (unless suffix is empty).
192      (or (and (<= ext-len 0)
193	       (not (eq (aref filename (1- (length filename))) ?.)))
194	  (= (length suffix) 0)
195	  (setq suffix (substring suffix 1)))
196      ;; How many chars of that extension should we keep?
197      (setq ext-left (min ext-len (max 0 (- 3 (length suffix)))))
198      ;; Get rid of the rest of the extension, and add SUFFIX.
199      (concat (substring filename 0 (- (length filename)
200				       (- ext-len ext-left)))
201	      suffix))))
202
203(defun info-insert-file-contents (filename &optional visit)
204  "Insert the contents of an info file in the current buffer.
205Do the right thing if the file has been compressed or zipped."
206  (let ((tail Info-suffix-list)
207	fullname decoder)
208    (if (file-exists-p filename)
209	;; FILENAME exists--see if that name contains a suffix.
210	;; If so, set DECODE accordingly.
211	(progn
212	  (while (and tail
213		      (not (string-match
214			    (concat (regexp-quote (car (car tail))) "$")
215			    filename)))
216	    (setq tail (cdr tail)))
217	  (setq fullname filename
218		decoder (cdr (car tail))))
219      ;; Try adding suffixes to FILENAME and see if we can find something.
220      (while (and tail
221		  (not (file-exists-p (info-insert-file-contents-1
222				       filename (car (car tail))))))
223	(setq tail (cdr tail)))
224      ;; If we found a file with a suffix, set DECODER according to the suffix
225      ;; and set FULLNAME to the file's actual name.
226      (setq fullname (info-insert-file-contents-1 filename (car (car tail)))
227	    decoder (cdr (car tail)))
228      (or tail
229	  (error "Can't find %s or any compressed version of it" filename)))
230    ;; check for conflict with jka-compr
231    (if (and (featurep 'jka-compr)
232	     (jka-compr-installed-p)
233	     (jka-compr-get-compression-info fullname))
234	(setq decoder nil))
235    (insert-file-contents fullname visit)
236    (if decoder
237	(let ((buffer-read-only nil)
238	      (default-directory (or (file-name-directory fullname)
239				     default-directory)))
240	  (call-process-region (point-min) (point-max) decoder t t)))))
241
242;;;###autoload (add-hook 'same-window-buffer-names "*info*")
243
244;;;###autoload
245(defun info (&optional file)
246  "Enter Info, the documentation browser.
247Optional argument FILE specifies the file to examine;
248the default is the top-level directory of Info.
249
250In interactive use, a prefix argument directs this command
251to read a file name from the minibuffer.
252
253The search path for Info files is in the variable `Info-directory-list'.
254The top-level Info directory is made by combining all the files named `dir'
255in all the directories in that path."
256  (interactive (if current-prefix-arg
257		   (list (read-file-name "Info file name: " nil nil t))))
258  (if file
259      (Info-goto-node (concat "(" file ")"))
260    (if (get-buffer "*info*")
261	(pop-to-buffer "*info*")
262      (Info-directory))))
263
264;;;###autoload
265(defun info-standalone ()
266  "Run Emacs as a standalone Info reader.
267Usage:  emacs -f info-standalone [filename]
268In standalone mode, \\<Info-mode-map>\\[Info-exit] exits Emacs itself."
269  (setq Info-standalone t)
270  (if (and command-line-args-left
271	   (not (string-match "^-" (car command-line-args-left))))
272      (condition-case err
273	  (progn
274	    (info (car command-line-args-left))
275	    (setq command-line-args-left (cdr command-line-args-left)))
276	(error (send-string-to-terminal
277		(format "%s\n" (if (eq (car-safe err) 'error)
278				   (nth 1 err) err)))
279	       (save-buffers-kill-emacs)))
280    (info)))
281
282;; Go to an info node specified as separate filename and nodename.
283;; no-going-back is non-nil if recovering from an error in this function;
284;; it says do not attempt further (recursive) error recovery.
285(defun Info-find-node (filename nodename &optional no-going-back)
286  ;; Convert filename to lower case if not found as specified.
287  ;; Expand it.
288  (if filename
289      (let (temp temp-downcase found)
290	(setq filename (substitute-in-file-name filename))
291	(if (string= (downcase filename) "dir")
292	    (setq found t)
293	  (let ((dirs (if (string-match "^\\./" filename)
294			  ;; If specified name starts with `./'
295			  ;; then just try current directory.
296			  '("./")
297			(if (file-name-absolute-p filename)
298			    ;; No point in searching for an
299			    ;; absolute file name
300			    '(nil)
301			  (if Info-additional-directory-list
302			      (append Info-directory-list
303				      Info-additional-directory-list)
304			    Info-directory-list)))))
305	    ;; Search the directory list for file FILENAME.
306	    (while (and dirs (not found))
307	      (setq temp (expand-file-name filename (car dirs)))
308	      (setq temp-downcase
309		    (expand-file-name (downcase filename) (car dirs)))
310	      ;; Try several variants of specified name.
311	      (let ((suffix-list Info-suffix-list))
312		(while (and suffix-list (not found))
313		  (cond ((file-exists-p
314			  (info-insert-file-contents-1
315			   temp (car (car suffix-list))))
316			 (setq found temp))
317			((file-exists-p
318			  (info-insert-file-contents-1
319			   temp-downcase (car (car suffix-list))))
320			 (setq found temp-downcase)))
321		  (setq suffix-list (cdr suffix-list))))
322	      (setq dirs (cdr dirs)))))
323	(if found
324	    (setq filename found)
325	  (error "Info file %s does not exist" filename))))
326  ;; Record the node we are leaving.
327  (if (and Info-current-file (not no-going-back))
328      (setq Info-history
329	    (cons (list Info-current-file Info-current-node (point))
330		  Info-history)))
331  ;; Go into info buffer.
332  (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
333  (buffer-disable-undo (current-buffer))
334  (or (eq major-mode 'Info-mode)
335      (Info-mode))
336  (widen)
337  (setq Info-current-node nil)
338  (unwind-protect
339      (progn
340	;; Switch files if necessary
341	(or (null filename)
342	    (equal Info-current-file filename)
343	    (let ((buffer-read-only nil))
344	      (setq Info-current-file nil
345		    Info-current-subfile nil
346		    Info-current-file-completions nil
347		    buffer-file-name nil)
348	      (erase-buffer)
349	      (if (eq filename t)
350		  (Info-insert-dir)
351		(info-insert-file-contents filename t)
352		(setq default-directory (file-name-directory filename)))
353	      (set-buffer-modified-p nil)
354	      ;; See whether file has a tag table.  Record the location if yes.
355	      (goto-char (point-max))
356	      (forward-line -8)
357	      ;; Use string-equal, not equal, to ignore text props.
358	      (if (not (or (string-equal nodename "*")
359			   (not
360			    (search-forward "\^_\nEnd tag table\n" nil t))))
361		  (let (pos)
362		    ;; We have a tag table.  Find its beginning.
363		    ;; Is this an indirect file?
364		    (search-backward "\nTag table:\n")
365		    (setq pos (point))
366		    (if (save-excursion
367			  (forward-line 2)
368			  (looking-at "(Indirect)\n"))
369			;; It is indirect.  Copy it to another buffer
370			;; and record that the tag table is in that buffer.
371			(let ((buf (current-buffer))
372			      (tagbuf
373			       (or Info-tag-table-buffer
374				   (generate-new-buffer " *info tag table*"))))
375			  (setq Info-tag-table-buffer tagbuf)
376			  (save-excursion
377			    (set-buffer tagbuf)
378                            (buffer-disable-undo (current-buffer))
379			    (setq case-fold-search t)
380			    (erase-buffer)
381			    (insert-buffer-substring buf))
382			  (set-marker Info-tag-table-marker
383				      (match-end 0) tagbuf))
384		      (set-marker Info-tag-table-marker pos)))
385		(set-marker Info-tag-table-marker nil))
386	      (setq Info-current-file
387		    (if (eq filename t) "dir" filename))))
388	;; Use string-equal, not equal, to ignore text props.
389	(if (string-equal nodename "*")
390	    (progn (setq Info-current-node nodename)
391		   (Info-set-mode-line))
392	  ;; Search file for a suitable node.
393	  (let ((guesspos (point-min))
394		(regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n\177]")))
395	    ;; First get advice from tag table if file has one.
396	    ;; Also, if this is an indirect info file,
397	    ;; read the proper subfile into this buffer.
398	    (if (marker-position Info-tag-table-marker)
399		(save-excursion
400		  (let ((m Info-tag-table-marker)
401			found found-mode)
402		    (save-excursion
403		      (set-buffer (marker-buffer m))
404		      (goto-char m)
405		      (beginning-of-line) ;so re-search will work.
406		      (setq found (re-search-forward regexp nil t))
407		      (if found
408			  (setq guesspos (read (current-buffer))))
409		      (setq found-mode major-mode))
410		    (if found
411			(progn
412			  ;; If this is an indirect file, determine
413			  ;; which file really holds this node and
414			  ;; read it in.
415			  (if (not (eq found-mode 'Info-mode))
416			      ;; Note that the current buffer must be
417			      ;; the *info* buffer on entry to
418			      ;; Info-read-subfile.  Thus the hackery
419			      ;; above.
420			      (setq guesspos (Info-read-subfile guesspos))))
421		      (error "No such node: %s" nodename)))))
422	    (goto-char (max (point-min) (- guesspos 1000)))
423	    ;; Now search from our advised position (or from beg of buffer)
424	    ;; to find the actual node.
425	    (catch 'foo
426	      (while (search-forward "\n\^_" nil t)
427		(forward-line 1)
428		(let ((beg (point)))
429		  (forward-line 1)
430		  (if (re-search-backward regexp beg t)
431		      (throw 'foo t))))
432	      (error "No such node: %s" nodename)))
433	  (Info-select-node)))
434    ;; If we did not finish finding the specified node,
435    ;; go back to the previous one.
436    (or Info-current-node no-going-back (null Info-history)
437	(let ((hist (car Info-history)))
438	  (setq Info-history (cdr Info-history))
439	  (Info-find-node (nth 0 hist) (nth 1 hist) t)
440	  (goto-char (nth 2 hist)))))
441  (goto-char (point-min)))
442
443;; Cache the contents of the (virtual) dir file, once we have merged
444;; it for the first time, so we can save time subsequently.
445(defvar Info-dir-contents nil)
446
447;; Cache for the directory we decided to use for the default-directory
448;; of the merged dir text.
449(defvar Info-dir-contents-directory nil)
450
451;; Record the file attributes of all the files from which we
452;; constructed Info-dir-contents.
453(defvar Info-dir-file-attributes nil)
454
455;; Construct the Info directory node by merging the files named `dir'
456;; from various directories.  Set the *info* buffer's
457;; default-directory to the first directory we actually get any text
458;; from.
459(defun Info-insert-dir ()
460  (if (and Info-dir-contents Info-dir-file-attributes
461	   ;; Verify that none of the files we used has changed
462	   ;; since we used it.
463	   (eval (cons 'and
464		       (mapcar '(lambda (elt)
465				  (let ((curr (file-attributes (car elt))))
466				    ;; Don't compare the access time.
467				    (if curr (setcar (nthcdr 4 curr) 0))
468				    (setcar (nthcdr 4 (cdr elt)) 0)
469				    (equal (cdr elt) curr)))
470			       Info-dir-file-attributes))))
471      (insert Info-dir-contents)
472    (let ((dirs Info-directory-list)
473	  buffers buffer others nodes dirs-done)
474
475      (setq Info-dir-file-attributes nil)
476
477      ;; Search the directory list for the directory file.
478      (while dirs
479	(let ((truename (file-truename (expand-file-name (car dirs)))))
480	  (or (member truename dirs-done)
481	      (member (directory-file-name truename) dirs-done)
482	      ;; Try several variants of specified name.
483	      ;; Try upcasing, appending `.info', or both.
484	      (let* (file
485		     (attrs
486		      (or
487		       (progn (setq file (expand-file-name "dir" truename))
488			      (file-attributes file))
489		       (progn (setq file (expand-file-name "DIR" truename))
490			      (file-attributes file))
491		       (progn (setq file (expand-file-name "dir.info" truename))
492			      (file-attributes file))
493		       (progn (setq file (expand-file-name "DIR.INFO" truename))
494			      (file-attributes file)))))
495		(setq dirs-done
496		      (cons truename
497			    (cons (directory-file-name truename)
498				  dirs-done)))
499		(if attrs
500		    (save-excursion
501		      (or buffers
502			  (message "Composing main Info directory..."))
503		      (set-buffer (generate-new-buffer "info dir"))
504		      (insert-file-contents file)
505		      (setq buffers (cons (current-buffer) buffers)
506			    Info-dir-file-attributes
507			    (cons (cons file attrs)
508				  Info-dir-file-attributes))))))
509	  (or (cdr dirs) (setq Info-dir-contents-directory
510			       (file-name-as-directory (car dirs))))
511	  (setq dirs (cdr dirs))))
512
513      (or buffers
514	  (error "Can't find the Info directory node"))
515      ;; Distinguish the dir file that comes with Emacs from all the
516      ;; others.  Yes, that is really what this is supposed to do.
517      ;; If it doesn't work, fix it.
518      (setq buffer (car buffers)
519	    others (cdr buffers))
520
521      ;; Insert the entire original dir file as a start; note that we've
522      ;; already saved its default directory to use as the default
523      ;; directory for the whole concatenation.
524      (insert-buffer buffer)
525
526      ;; Look at each of the other buffers one by one.
527      (while others
528	(let ((other (car others)))
529	  ;; In each, find all the menus.
530	  (save-excursion
531	    (set-buffer other)
532	    (goto-char (point-min))
533	    ;; Find each menu, and add an elt to NODES for it.
534	    (while (re-search-forward "^\\* Menu:" nil t)
535	      (let (beg nodename end)
536		(forward-line 1)
537		(setq beg (point))
538		(search-backward "\n\^_")
539		(search-forward "Node: ")
540		(setq nodename (Info-following-node-name))
541		(search-forward "\n\^_" nil 'move)
542		(beginning-of-line)
543		(setq end (point))
544		(setq nodes (cons (list nodename other beg end) nodes))))))
545	(setq others (cdr others)))
546      ;; Add to the main menu a menu item for each other node.
547      (re-search-forward "^\\* Menu:")
548      (forward-line 1)
549      (let ((menu-items '("top"))
550	    (nodes nodes)
551	    (case-fold-search t)
552	    (end (save-excursion (search-forward "\^_" nil t) (point))))
553	(while nodes
554	  (let ((nodename (car (car nodes))))
555	    (save-excursion
556	      (or (member (downcase nodename) menu-items)
557		  (re-search-forward (concat "^\\* "
558					     (regexp-quote nodename)
559					     "::")
560				     end t)
561		  (progn
562		    (insert "* " nodename "::" "\n")
563		    (setq menu-items (cons nodename menu-items))))))
564	  (setq nodes (cdr nodes))))
565      ;; Now take each node of each of the other buffers
566      ;; and merge it into the main buffer.
567      (while nodes
568	(let ((nodename (car (car nodes))))
569	  (goto-char (point-min))
570	  ;; Find the like-named node in the main buffer.
571	  (if (re-search-forward (concat "\n\^_.*\n.*Node: "
572					 (regexp-quote nodename)
573					 "[,\n\t]")
574				 nil t)
575	      (progn
576		(search-forward "\n\^_" nil 'move)
577		(beginning-of-line)
578		(insert "\n"))
579	    ;; If none exists, add one.
580	    (goto-char (point-max))
581	    (insert "\^_\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
582	  ;; Merge the text from the other buffer's menu
583	  ;; into the menu in the like-named node in the main buffer.
584	  (apply 'insert-buffer-substring (cdr (car nodes))))
585	(setq nodes (cdr nodes)))
586      ;; Kill all the buffers we just made.
587      (while buffers
588	(kill-buffer (car buffers))
589	(setq buffers (cdr buffers)))
590      (message "Composing main Info directory...done"))
591    (setq Info-dir-contents (buffer-string)))
592  (setq default-directory Info-dir-contents-directory))
593
594;; Note that on entry to this function the current-buffer must be the
595;; *info* buffer; not the info tags buffer.
596(defun Info-read-subfile (nodepos)
597  ;; NODEPOS is either a position (in the Info file as a whole,
598  ;; not relative to a subfile) or the name of a subfile.
599  (let (lastfilepos
600	lastfilename)
601    (if (numberp nodepos)
602	(save-excursion
603	  (set-buffer (marker-buffer Info-tag-table-marker))
604	  (goto-char (point-min))
605	  (search-forward "\n\^_")
606	  (forward-line 2)
607	  (catch 'foo
608	    (while (not (looking-at "\^_"))
609	      (if (not (eolp))
610		  (let ((beg (point))
611			thisfilepos thisfilename)
612		    (search-forward ": ")
613		    (setq thisfilename  (buffer-substring beg (- (point) 2)))
614		    (setq thisfilepos (read (current-buffer)))
615		    ;; read in version 19 stops at the end of number.
616		    ;; Advance to the next line.
617		    (forward-line 1)
618		    (if (> thisfilepos nodepos)
619			(throw 'foo t))
620		    (setq lastfilename thisfilename)
621		    (setq lastfilepos thisfilepos))
622		(forward-line 1)))))
623      (setq lastfilename nodepos)
624      (setq lastfilepos 0))
625    ;; Assume previous buffer is in Info-mode.
626    ;; (set-buffer (get-buffer "*info*"))
627    (or (equal Info-current-subfile lastfilename)
628	(let ((buffer-read-only nil))
629	  (setq buffer-file-name nil)
630	  (widen)
631	  (erase-buffer)
632	  (info-insert-file-contents lastfilename)
633	  (set-buffer-modified-p nil)
634	  (setq Info-current-subfile lastfilename)))
635    (goto-char (point-min))
636    (search-forward "\n\^_")
637    (if (numberp nodepos)
638	(+ (- nodepos lastfilepos) (point)))))
639
640;; Select the info node that point is in.
641(defun Info-select-node ()
642  (save-excursion
643   ;; Find beginning of node.
644   (search-backward "\n\^_")
645   (forward-line 2)
646   ;; Get nodename spelled as it is in the node.
647   (re-search-forward "Node:[ \t]*")
648   (setq Info-current-node
649	 (buffer-substring-no-properties (point)
650					 (progn
651					  (skip-chars-forward "^,\t\n")
652					  (point))))
653   (Info-set-mode-line)
654   ;; Find the end of it, and narrow.
655   (beginning-of-line)
656   (let (active-expression)
657     (narrow-to-region (point)
658		       (if (re-search-forward "\n[\^_\f]" nil t)
659			   (prog1
660			    (1- (point))
661			    (if (looking-at "[\n\^_\f]*execute: ")
662				(progn
663				  (goto-char (match-end 0))
664				  (setq active-expression
665					(read (current-buffer))))))
666			 (point-max)))
667     (if Info-enable-active-nodes (eval active-expression))
668     (if Info-fontify (Info-fontify-node))
669     (run-hooks 'Info-selection-hook))))
670
671(defun Info-set-mode-line ()
672  (setq mode-line-buffer-identification
673	(concat
674	 "  Info:  ("
675	 (if Info-current-file
676	     (file-name-nondirectory Info-current-file)
677	   "")
678	 ")"
679	 (or Info-current-node ""))))
680
681;; Go to an info node specified with a filename-and-nodename string
682;; of the sort that is found in pointers in nodes.
683
684(defun Info-goto-node (nodename)
685  "Go to info node named NAME.  Give just NODENAME or (FILENAME)NODENAME."
686  (interactive (list (Info-read-node-name "Goto node: ")))
687  (let (filename)
688    (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
689		  nodename)
690    (setq filename (if (= (match-beginning 1) (match-end 1))
691		       ""
692		     (substring nodename (match-beginning 2) (match-end 2)))
693	  nodename (substring nodename (match-beginning 3) (match-end 3)))
694    (let ((trim (string-match "\\s *\\'" filename)))
695      (if trim (setq filename (substring filename 0 trim))))
696    (let ((trim (string-match "\\s *\\'" nodename)))
697      (if trim (setq nodename (substring nodename 0 trim))))
698    (if transient-mark-mode (deactivate-mark))
699    (Info-find-node (if (equal filename "") nil filename)
700		    (if (equal nodename "") "Top" nodename))))
701
702;; This function is used as the "completion table" while reading a node name.
703;; It does completion using the alist in completion-table
704;; unless STRING starts with an open-paren.
705(defun Info-read-node-name-1 (string predicate code)
706  (let ((no-completion (and (> (length string) 0) (eq (aref string 0) ?\())))
707    (cond ((eq code nil)
708	   (if no-completion
709	       string
710	     (try-completion string completion-table predicate)))
711	  ((eq code t)
712	   (if no-completion
713	       nil
714	     (all-completions string completion-table predicate)))
715	  ((eq code 'lambda)
716	   (if no-completion
717	       t
718	     (assoc string completion-table))))))
719
720(defun Info-read-node-name (prompt &optional default)
721  (let* ((completion-ignore-case t)
722	 (completion-table (Info-build-node-completions))
723	 (nodename (completing-read prompt 'Info-read-node-name-1 nil t)))
724    (if (equal nodename "")
725	(or default
726	    (Info-read-node-name prompt))
727      nodename)))
728
729(defun Info-build-node-completions ()
730  (or Info-current-file-completions
731      (let ((compl nil))
732	(save-excursion
733	  (save-restriction
734	    (if (marker-buffer Info-tag-table-marker)
735		(let ((marker Info-tag-table-marker))
736		  (set-buffer (marker-buffer marker))
737		  (widen)
738		  (goto-char marker)
739		  (while (re-search-forward "\nNode: \\(.*\\)\177" nil t)
740		    (setq compl
741			  (cons (list (buffer-substring (match-beginning 1)
742							(match-end 1)))
743				compl))))
744	      (widen)
745	      (goto-char (point-min))
746	      (while (search-forward "\n\^_" nil t)
747		(forward-line 1)
748		(let ((beg (point)))
749		  (forward-line 1)
750		  (if (re-search-backward "Node: *\\([^,\n]*\\) *[,\n\t]"
751					  beg t)
752		      (setq compl
753			    (cons (list (buffer-substring (match-beginning 1)
754							  (match-end 1)))
755				  compl))))))))
756	(setq Info-current-file-completions compl))))
757
758(defun Info-restore-point (hl)
759  "If this node has been visited, restore the point value when we left."
760  (while hl
761    (if (and (equal (nth 0 (car hl)) Info-current-file)
762	     ;; Use string-equal, not equal, to ignore text props.
763	     (string-equal (nth 1 (car hl)) Info-current-node))
764	(progn
765	  (goto-char (nth 2 (car hl)))
766	  (setq hl nil))		;terminate the while at next iter
767      (setq hl (cdr hl)))))
768
769(defvar Info-last-search nil
770  "Default regexp for \\<Info-mode-map>\\[Info-search] command to search for.")
771
772(defun Info-search (regexp)
773  "Search for REGEXP, starting from point, and select node it's found in."
774  (interactive "sSearch (regexp): ")
775  (if transient-mark-mode (deactivate-mark))
776  (if (equal regexp "")
777      (setq regexp Info-last-search)
778    (setq Info-last-search regexp))
779  (let ((found ()) current
780	(onode Info-current-node)
781	(ofile Info-current-file)
782	(opoint (point))
783	(ostart (window-start))
784	(osubfile Info-current-subfile))
785    (save-excursion
786      (save-restriction
787	(widen)
788	(if (null Info-current-subfile)
789	    (progn (re-search-forward regexp) (setq found (point)))
790	  (condition-case err
791	      (progn (re-search-forward regexp) (setq found (point)))
792	    (search-failed nil)))))
793    (if (not found) ;can only happen in subfile case -- else would have erred
794	(unwind-protect
795	    (let ((list ()))
796	      (save-excursion
797		(set-buffer (marker-buffer Info-tag-table-marker))
798		(goto-char (point-min))
799		(search-forward "\n\^_\nIndirect:")
800		(save-restriction
801		  (narrow-to-region (point)
802				    (progn (search-forward "\n\^_")
803					   (1- (point))))
804		  (goto-char (point-min))
805		  (search-forward (concat "\n" osubfile ": "))
806		  (beginning-of-line)
807		  (while (not (eobp))
808		    (re-search-forward "\\(^.*\\): [0-9]+$")
809		    (goto-char (+ (match-end 1) 2))
810		    (setq list (cons (cons (read (current-buffer))
811					   (buffer-substring
812					    (match-beginning 1) (match-end 1)))
813				     list))
814		    (goto-char (1+ (match-end 0))))
815		  (setq list (nreverse list)
816			current (car (car list))
817			list (cdr list))))
818	      (while list
819		(message "Searching subfile %s..." (cdr (car list)))
820		(Info-read-subfile (car (car list)))
821		(setq list (cdr list))
822;;		(goto-char (point-min))
823		(if (re-search-forward regexp nil t)
824		    (setq found (point) list ())))
825	      (if found
826		  (message "")
827		(signal 'search-failed (list regexp))))
828	  (if (not found)
829	      (progn (Info-read-subfile osubfile)
830		     (goto-char opoint)
831		     (Info-select-node)
832		     (set-window-start (selected-window) ostart)))))
833    (widen)
834    (goto-char found)
835    (Info-select-node)
836    ;; Use string-equal, not equal, to ignore text props.
837    (or (and (string-equal onode Info-current-node)
838	     (equal ofile Info-current-file))
839	(setq Info-history (cons (list ofile onode opoint)
840				 Info-history)))))
841
842;; Extract the value of the node-pointer named NAME.
843;; If there is none, use ERRORNAME in the error message;
844;; if ERRORNAME is nil, just return nil.
845(defun Info-extract-pointer (name &optional errorname)
846  (save-excursion
847   (goto-char (point-min))
848   (forward-line 1)
849   (if (re-search-backward (concat name ":") nil t)
850       (progn
851	 (goto-char (match-end 0))
852	 (Info-following-node-name))
853     (if (eq errorname t)
854	 nil
855       (error "Node has no %s" (capitalize (or errorname name)))))))
856
857;; Return the node name in the buffer following point.
858;; ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
859;; saying which chars may appear in the node name.
860(defun Info-following-node-name (&optional allowedchars)
861  (skip-chars-forward " \t")
862  (buffer-substring-no-properties
863   (point)
864   (progn
865     (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
866       (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
867       (if (looking-at "(")
868	   (skip-chars-forward "^)")))
869     (skip-chars-backward " ")
870     (point))))
871
872(defun Info-next ()
873  "Go to the next node of this node."
874  (interactive)
875  (Info-goto-node (Info-extract-pointer "next")))
876
877(defun Info-prev ()
878  "Go to the previous node of this node."
879  (interactive)
880  (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
881
882(defun Info-up ()
883  "Go to the superior node of this node."
884  (interactive)
885  (Info-goto-node (Info-extract-pointer "up"))
886  (Info-restore-point Info-history))
887
888(defun Info-last ()
889  "Go back to the last node visited."
890  (interactive)
891  (or Info-history
892      (error "This is the first Info node you looked at"))
893  (let (filename nodename opoint)
894    (setq filename (car (car Info-history)))
895    (setq nodename (car (cdr (car Info-history))))
896    (setq opoint (car (cdr (cdr (car Info-history)))))
897    (setq Info-history (cdr Info-history))
898    (Info-find-node filename nodename)
899    (setq Info-history (cdr Info-history))
900    (goto-char opoint)))
901
902(defun Info-directory ()
903  "Go to the Info directory node."
904  (interactive)
905  (Info-find-node "dir" "top"))
906
907(defun Info-follow-reference (footnotename)
908  "Follow cross reference named NAME to the node it refers to.
909NAME may be an abbreviation of the reference name."
910  (interactive
911   (let ((completion-ignore-case t)
912	 completions default alt-default (start-point (point)) str i bol eol)
913     (save-excursion
914       ;; Store end and beginning of line.
915       (end-of-line)
916       (setq eol (point))
917       (beginning-of-line)
918       (setq bol (point))
919
920       (goto-char (point-min))
921       (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
922	 (setq str (buffer-substring
923		    (match-beginning 1)
924		    (1- (point))))
925	 ;; See if this one should be the default.
926	 (and (null default)
927	      (<= (match-beginning 0) start-point)
928	      (<= start-point (point))
929	      (setq default t))
930	 ;; See if this one should be the alternate default.
931	 (and (null alt-default)
932	      (and (<= bol (match-beginning 0))
933		   (<= (point) eol))
934	      (setq alt-default t))
935	 (setq i 0)
936	 (while (setq i (string-match "[ \n\t]+" str i))
937	   (setq str (concat (substring str 0 i) " "
938			     (substring str (match-end 0))))
939	   (setq i (1+ i)))
940	 ;; Record as a completion and perhaps as default.
941	 (if (eq default t) (setq default str))
942	 (if (eq alt-default t) (setq alt-default str))
943	 ;; Don't add this string if it's a duplicate.
944	 ;; We use a loop instead of "(assoc str completions)" because
945	 ;; we want to do a case-insensitive compare.
946	 (let ((tail completions)
947	       (tem (downcase str)))
948	   (while (and tail
949		       (not (string-equal tem (downcase (car (car tail))))))
950	     (setq tail (cdr tail)))
951	   (or tail
952	       (setq completions
953		     (cons (cons str nil)
954			   completions))))))
955     ;; If no good default was found, try an alternate.
956     (or default
957	 (setq default alt-default))
958     ;; If only one cross-reference found, then make it default.
959     (if (eq (length completions) 1)
960         (setq default (car (car completions))))
961     (if completions
962	 (let ((input (completing-read (if default
963					   (concat "Follow reference named: ("
964						   default ") ")
965					 "Follow reference named: ")
966				       completions nil t)))
967	   (list (if (equal input "")
968		     default input)))
969       (error "No cross-references in this node"))))
970  (let (target beg i (str (concat "\\*note " (regexp-quote footnotename))))
971    (while (setq i (string-match " " str i))
972      (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
973      (setq i (+ i 6)))
974    (save-excursion
975      (goto-char (point-min))
976      (or (re-search-forward str nil t)
977	  (error "No cross-reference named %s" footnotename))
978      (goto-char (+ (match-beginning 0) 5))
979      (setq target
980	    (Info-extract-menu-node-name "Bad format cross reference" t)))
981    (while (setq i (string-match "[ \t\n]+" target i))
982      (setq target (concat (substring target 0 i) " "
983			   (substring target (match-end 0))))
984      (setq i (+ i 1)))
985    (Info-goto-node target)))
986
987(defun Info-extract-menu-node-name (&optional errmessage multi-line)
988  (skip-chars-forward " \t\n")
989  (let ((beg (point))
990	str i)
991    (skip-chars-forward "^:")
992    (forward-char 1)
993    (setq str
994	  (if (looking-at ":")
995	      (buffer-substring-no-properties beg (1- (point)))
996	    (skip-chars-forward " \t\n")
997	    (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
998    (while (setq i (string-match "\n" str i))
999      (aset str i ?\ ))
1000    ;; Collapse multiple spaces.
1001    (while (string-match "  +" str)
1002      (setq str (replace-match " " t t str)))
1003    str))
1004
1005;; No one calls this.
1006;;(defun Info-menu-item-sequence (list)
1007;;  (while list
1008;;    (Info-menu (car list))
1009;;    (setq list (cdr list))))
1010
1011(defun Info-complete-menu-item (string predicate action)
1012  (let ((case-fold-search t))
1013    (cond ((eq action nil)
1014	   (let (completions
1015		 (pattern (concat "\n\\* \\("
1016				  (regexp-quote string)
1017				  "[^:\t\n]*\\):")))
1018	     (save-excursion
1019	       (set-buffer Info-complete-menu-buffer)
1020	       (goto-char (point-min))
1021	       (search-forward "\n* Menu:")
1022	       (while (re-search-forward pattern nil t)
1023		 (setq completions (cons (cons (format "%s"
1024						       (buffer-substring
1025							(match-beginning 1)
1026							(match-end 1)))
1027					       (match-beginning 1))
1028					 completions))))
1029	     (try-completion string completions predicate)))
1030	  ((eq action t)
1031	   (let (completions
1032		 (pattern (concat "\n\\* \\("
1033				  (regexp-quote string)
1034				  "[^:\t\n]*\\):")))
1035	     (save-excursion
1036	       (set-buffer Info-complete-menu-buffer)
1037	       (goto-char (point-min))
1038	       (search-forward "\n* Menu:")
1039	       (while (re-search-forward pattern nil t)
1040		 (setq completions (cons (cons (format "%s"
1041						       (buffer-substring
1042							(match-beginning 1)
1043							(match-end 1)))
1044					       (match-beginning 1))
1045					 completions))))
1046	     (all-completions string completions predicate)))
1047	  (t
1048	   (save-excursion
1049	     (set-buffer Info-complete-menu-buffer)
1050	     (goto-char (point-min))
1051	     (search-forward "\n* Menu:")
1052	     (re-search-forward (concat "\n\\* "
1053					(regexp-quote string)
1054					":")
1055				nil t))))))
1056
1057
1058(defun Info-menu (menu-item)
1059  "Go to node for menu item named (or abbreviated) NAME.
1060Completion is allowed, and the menu item point is on is the default."
1061  (interactive
1062   (let ((completions '())
1063	 ;; If point is within a menu item, use that item as the default
1064	 (default nil)
1065	 (p (point))
1066	 beg
1067	 (last nil))
1068     (save-excursion
1069       (goto-char (point-min))
1070       (if (not (search-forward "\n* menu:" nil t))
1071	   (error "No menu in this node"))
1072       (setq beg (point))
1073       (and (< (point) p)
1074	    (save-excursion
1075	      (goto-char p)
1076	      (end-of-line)
1077	      (re-search-backward "\n\\* \\([^:\t\n]*\\):" beg t)
1078	      (setq default (format "%s" (buffer-substring
1079					  (match-beginning 1)
1080					  (match-end 1)))))))
1081     (let ((item nil))
1082       (while (null item)
1083	 (setq item (let ((completion-ignore-case t)
1084			  (Info-complete-menu-buffer (current-buffer)))
1085		      (completing-read (if default
1086					   (format "Menu item (default %s): "
1087						   default)
1088					   "Menu item: ")
1089				       'Info-complete-menu-item nil t)))
1090	 ;; we rely on the fact that completing-read accepts an input
1091	 ;; of "" even when the require-match argument is true and ""
1092	 ;; is not a valid possibility
1093	 (if (string= item "")
1094	     (if default
1095		 (setq item default)
1096	         ;; ask again
1097	         (setq item nil))))
1098       (list item))))
1099  ;; there is a problem here in that if several menu items have the same
1100  ;; name you can only go to the node of the first with this command.
1101  (Info-goto-node (Info-extract-menu-item menu-item)))
1102
1103(defun Info-extract-menu-item (menu-item)
1104  (setq menu-item (regexp-quote menu-item))
1105  (save-excursion
1106    (goto-char (point-min))
1107    (or (search-forward "\n* menu:" nil t)
1108	(error "No menu in this node"))
1109    (or (re-search-forward (concat "\n\\* " menu-item ":") nil t)
1110	(re-search-forward (concat "\n\\* " menu-item) nil t)
1111	(error "No such item in menu"))
1112    (beginning-of-line)
1113    (forward-char 2)
1114    (Info-extract-menu-node-name)))
1115
1116;; If COUNT is nil, use the last item in the menu.
1117(defun Info-extract-menu-counting (count)
1118  (save-excursion
1119    (goto-char (point-min))
1120    (or (search-forward "\n* menu:" nil t)
1121	(error "No menu in this node"))
1122    (if count
1123	(or (search-forward "\n* " nil t count)
1124	    (error "Too few items in menu"))
1125      (while (search-forward "\n* " nil t)
1126	nil))
1127    (Info-extract-menu-node-name)))
1128
1129(defun Info-nth-menu-item ()
1130  "Go to the node of the Nth menu item.
1131N is the digit argument used to invoke this command."
1132  (interactive)
1133  (Info-goto-node
1134   (Info-extract-menu-counting
1135    (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
1136
1137(defun Info-top-node ()
1138  "Go to the Top node of this file."
1139  (interactive)
1140  (Info-goto-node "Top"))
1141
1142(defun Info-final-node ()
1143  "Go to the final node in this file."
1144  (interactive)
1145  (Info-goto-node "Top")
1146  (let (Info-history)
1147    ;; Go to the last node in the menu of Top.
1148    (Info-goto-node (Info-extract-menu-counting nil))
1149    ;; If the last node in the menu is not last in pointer structure,
1150    ;; move forward until we can't go any farther.
1151    (while (Info-forward-node t t) nil)
1152    ;; Then keep moving down to last subnode, unless we reach an index.
1153    (while (and (not (string-match "\\<index\\>" Info-current-node))
1154		(save-excursion (search-forward "\n* Menu:" nil t)))
1155      (Info-goto-node (Info-extract-menu-counting nil)))))
1156
1157(defun Info-forward-node (&optional not-down no-error)
1158  "Go forward one node, considering all nodes as forming one sequence."
1159  (interactive)
1160  (goto-char (point-min))
1161  (forward-line 1)
1162  ;; three possibilities, in order of priority:
1163  ;;     1. next node is in a menu in this node (but not in an index)
1164  ;;     2. next node is next at same level
1165  ;;     3. next node is up and next
1166  (cond ((and (not not-down)
1167              (save-excursion (search-forward "\n* menu:" nil t))
1168	      (not (string-match "\\<index\\>" Info-current-node)))
1169	 (Info-goto-node (Info-extract-menu-counting 1))
1170         t)
1171        ((save-excursion (search-backward "next:" nil t))
1172         (Info-next)
1173         t)
1174        ((and (save-excursion (search-backward "up:" nil t))
1175	      ;; Use string-equal, not equal, to ignore text props.
1176	      (not (string-equal (downcase (Info-extract-pointer "up"))
1177				 "top")))
1178         (let ((old-node Info-current-node))
1179           (Info-up)
1180           (let (Info-history success)
1181             (unwind-protect
1182                 (setq success (Info-forward-node t no-error))
1183               (or success (Info-goto-node old-node))))))
1184        (no-error nil)
1185        (t (error "No pointer forward from this node"))))
1186
1187(defun Info-backward-node ()
1188  "Go backward one node, considering all nodes as forming one sequence."
1189  (interactive)
1190  (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
1191	(upnode (Info-extract-pointer "up" t)))
1192    (cond ((and upnode (string-match "(" upnode))
1193	   (error "First node in file"))
1194	  ((and upnode (or (null prevnode)
1195			   ;; Use string-equal, not equal,
1196			   ;; to ignore text properties.
1197			   (string-equal (downcase prevnode)
1198					 (downcase upnode))))
1199	   (Info-up))
1200	  (prevnode
1201	   ;; If we move back at the same level,
1202	   ;; go down to find the last subnode*.
1203	   (Info-prev)
1204	   (let (Info-history)
1205	     (while (and (not (string-match "\\<index\\>" Info-current-node))
1206			 (save-excursion (search-forward "\n* Menu:" nil t)))
1207	       (Info-goto-node (Info-extract-menu-counting nil)))))
1208	  (t
1209	   (error "No pointer backward from this node")))))
1210
1211(defun Info-exit ()
1212  "Exit Info by selecting some other buffer."
1213  (interactive)
1214  (if Info-standalone
1215      (save-buffers-kill-emacs)
1216    (bury-buffer)))
1217
1218(defun Info-next-menu-item ()
1219  (interactive)
1220  (save-excursion
1221    (forward-line -1)
1222    (search-forward "\n* menu:" nil t)
1223    (or (search-forward "\n* " nil t)
1224	(error "No more items in menu"))
1225    (Info-goto-node (Info-extract-menu-node-name))))
1226
1227(defun Info-last-menu-item ()
1228  (interactive)
1229  (save-excursion
1230    (forward-line 1)
1231    (let ((beg (save-excursion
1232		 (and (search-backward "\n* menu:" nil t)
1233		      (point)))))
1234      (or (and beg (search-backward "\n* " beg t))
1235	  (error "No previous items in menu")))
1236    (Info-goto-node (save-excursion
1237		      (goto-char (match-end 0))
1238		      (Info-extract-menu-node-name)))))
1239
1240(defmacro Info-no-error (&rest body)
1241  (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
1242
1243(defun Info-next-preorder ()
1244  "Go to the next subnode or the next node, or go up a level."
1245  (interactive)
1246  (cond ((Info-no-error (Info-next-menu-item)))
1247	((Info-no-error (Info-next)))
1248	((Info-no-error (Info-up))
1249	 ;; Since we have already gone thru all the items in this menu,
1250	 ;; go up to the end of this node.
1251	 (goto-char (point-max))
1252	 ;; Since logically we are done with the node with that menu,
1253	 ;; move on from it.
1254	 (Info-next-preorder))
1255	(t
1256	 (error "No more nodes"))))
1257
1258(defun Info-last-preorder ()
1259  "Go to the last node, popping up a level if there is none."
1260  (interactive)
1261  (cond ((Info-no-error
1262	  (Info-last-menu-item)
1263	  ;; If we go down a menu item, go to the end of the node
1264	  ;; so we can scroll back through it.
1265	  (goto-char (point-max)))
1266	 ;; Keep going down, as long as there are nested menu nodes.
1267	 (while (Info-no-error
1268		 (Info-last-menu-item)
1269		 ;; If we go down a menu item, go to the end of the node
1270		 ;; so we can scroll back through it.
1271		 (goto-char (point-max))))
1272	 (recenter -1))
1273	((Info-no-error (Info-prev))
1274	 (goto-char (point-max))
1275	 (while (Info-no-error
1276		 (Info-last-menu-item)
1277		 ;; If we go down a menu item, go to the end of the node
1278		 ;; so we can scroll back through it.
1279		 (goto-char (point-max))))
1280	 (recenter -1))
1281	((Info-no-error (Info-up))
1282	 (goto-char (point-min))
1283	 (or (search-forward "\n* Menu:" nil t)
1284	     (goto-char (point-max))))
1285	(t (error "No previous nodes"))))
1286
1287(defun Info-scroll-up ()
1288  "Scroll one screenful forward in Info, considering all nodes as one sequence.
1289Once you scroll far enough in a node that its menu appears on the screen
1290but after point, the next scroll moves into its first subnode.
1291
1292When you scroll past the end of a node, that goes to the next node; if
1293this node has no successor, it moves to the parent node's successor,
1294and so on.  If point is inside the menu of a node, it moves to
1295subnode indicated by the following menu item.  (That case won't
1296normally result from this command, but can happen in other ways.)"
1297
1298  (interactive)
1299  (if (or (< (window-start) (point-min))
1300	  (> (window-start) (point-max)))
1301      (set-window-start (selected-window) (point)))
1302  (let ((virtual-end (save-excursion
1303		       (goto-char (point-min))
1304		       (if (search-forward "\n* Menu:" nil t)
1305			   (point)
1306			 (point-max)))))
1307    (if (or (< virtual-end (window-start))
1308	    (pos-visible-in-window-p virtual-end))
1309	(Info-next-preorder)
1310      (scroll-up))))
1311
1312(defun Info-scroll-down ()
1313  "Scroll one screenful back in Info, considering all nodes as one sequence.
1314Within the menu of a node, this goes to its last subnode.
1315When you scroll past the beginning of a node, that goes to the
1316previous node or back up to the parent node."
1317  (interactive)
1318  (if (or (< (window-start) (point-min))
1319	  (> (window-start) (point-max)))
1320      (set-window-start (selected-window) (point)))
1321  (let* ((current-point (point))
1322	 (virtual-end (save-excursion
1323			(beginning-of-line)
1324			(setq current-point (point))
1325			(goto-char (point-min))
1326			(search-forward "\n* Menu:"
1327					current-point
1328					t))))
1329    (if (or virtual-end (pos-visible-in-window-p (point-min)))
1330	(Info-last-preorder)
1331      (scroll-down))))
1332
1333(defun Info-next-reference (&optional recur)
1334  "Move cursor to the next cross-reference or menu item in the node."
1335  (interactive)
1336  (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1337	(old-pt (point)))
1338    (or (eobp) (forward-char 1))
1339    (or (re-search-forward pat nil t)
1340	(progn
1341	  (goto-char (point-min))
1342	  (or (re-search-forward pat nil t)
1343	      (progn
1344		(goto-char old-pt)
1345		(error "No cross references in this node")))))
1346    (goto-char (match-beginning 0))
1347    (if (looking-at "\\* Menu:")
1348	(if recur
1349	    (error "No cross references in this node")
1350	  (Info-next-reference t)))))
1351
1352(defun Info-prev-reference (&optional recur)
1353  "Move cursor to the previous cross-reference or menu item in the node."
1354  (interactive)
1355  (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1356	(old-pt (point)))
1357    (or (re-search-backward pat nil t)
1358	(progn
1359	  (goto-char (point-max))
1360	  (or (re-search-backward pat nil t)
1361	      (progn
1362		(goto-char old-pt)
1363		(error "No cross references in this node")))))
1364    (goto-char (match-beginning 0))
1365    (if (looking-at "\\* Menu:")
1366	(if recur
1367	    (error "No cross references in this node")
1368	  (Info-prev-reference t)))))
1369
1370(defun Info-index (topic)
1371  "Look up a string in the index for this file.
1372The index is defined as the first node in the top-level menu whose
1373name contains the word \"Index\", plus any immediately following
1374nodes whose names also contain the word \"Index\".
1375If there are no exact matches to the specified topic, this chooses
1376the first match which is a case-insensitive substring of a topic.
1377Use the `,' command to see the other matches.
1378Give a blank topic name to go to the Index node itself."
1379  (interactive "sIndex topic: ")
1380  (let ((orignode Info-current-node)
1381	(rnode nil)
1382	(pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1383			 (regexp-quote topic)))
1384	node)
1385    (Info-goto-node "Top")
1386    (or (search-forward "\n* menu:" nil t)
1387	(error "No index"))
1388    (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1389	(error "No index"))
1390    (goto-char (match-beginning 1))
1391    ;; Here, and subsequently in this function,
1392    ;; we bind Info-history to nil for internal node-switches
1393    ;; so that we don't put junk in the history.
1394    ;; In the first Info-goto-node call, above, we do update the history
1395    ;; because that is what the user's previous node choice into it.
1396    (let ((Info-history nil))
1397      (Info-goto-node (Info-extract-menu-node-name)))
1398    (or (equal topic "")
1399	(let ((matches nil)
1400	      (exact nil)
1401	      (Info-history nil)
1402	      found)
1403	  (while
1404	      (progn
1405		(goto-char (point-min))
1406		(while (re-search-forward pattern nil t)
1407		  (setq matches
1408			(cons (list (buffer-substring (match-beginning 1)
1409						      (match-end 1))
1410				    (buffer-substring (match-beginning 2)
1411						      (match-end 2))
1412				    Info-current-node
1413				    (string-to-int (concat "0"
1414							   (buffer-substring
1415							    (match-beginning 3)
1416							    (match-end 3)))))
1417			      matches)))
1418		(and (setq node (Info-extract-pointer "next" t))
1419		     (string-match "\\<Index\\>" node)))
1420	    (Info-goto-node node))
1421	  (or matches
1422	      (progn
1423		(Info-goto-node orignode)
1424		(error "No `%s' in index" topic)))
1425	  ;; Here it is a feature that assoc is case-sensitive.
1426	  (while (setq found (assoc topic matches))
1427	    (setq exact (cons found exact)
1428		  matches (delq found matches)))
1429	  (setq Info-index-alternatives (nconc exact (nreverse matches)))
1430	  (Info-index-next 0)))))
1431
1432(defun Info-index-next (num)
1433  "Go to the next matching index item from the last `i' command."
1434  (interactive "p")
1435  (or Info-index-alternatives
1436      (error "No previous `i' command"))
1437  (while (< num 0)
1438    (setq num (+ num (length Info-index-alternatives))))
1439  (while (> num 0)
1440    (setq Info-index-alternatives
1441	  (nconc (cdr Info-index-alternatives)
1442		 (list (car Info-index-alternatives)))
1443	  num (1- num)))
1444  (Info-goto-node (nth 1 (car Info-index-alternatives)))
1445  (if (> (nth 3 (car Info-index-alternatives)) 0)
1446      (forward-line (nth 3 (car Info-index-alternatives)))
1447    (forward-line 3)  ; don't search in headers
1448    (let ((name (car (car Info-index-alternatives))))
1449      (Info-find-index-name name)))
1450  (message "Found `%s' in %s.  %s"
1451	   (car (car Info-index-alternatives))
1452	   (nth 2 (car Info-index-alternatives))
1453	   (if (cdr Info-index-alternatives)
1454	       "(Press `,' for more)"
1455	     "(Only match)")))
1456
1457(defun Info-find-index-name (name)
1458  "Move point to the place within the current node where NAME is defined."
1459  (if (or (re-search-forward (format
1460			      "[a-zA-Z]+: %s\\( \\|$\\)"
1461			      (regexp-quote name)) nil t)
1462	  (search-forward (format "`%s'" name) nil t)
1463	  (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1464	       (search-forward
1465		(format "`%s'" (substring name 0 (match-beginning 1)))
1466		nil t))
1467	  (search-forward name nil t))
1468      (beginning-of-line)
1469    (goto-char (point-min))))
1470
1471(defun Info-undefined ()
1472  "Make command be undefined in Info."
1473  (interactive)
1474  (ding))
1475
1476(defun Info-help ()
1477  "Enter the Info tutorial."
1478  (interactive)
1479  (delete-other-windows)
1480  (Info-find-node "info"
1481		  (if (< (window-height) 23)
1482		      "Help-Small-Screen"
1483		    "Help")))
1484
1485(defun Info-summary ()
1486  "Display a brief summary of all Info commands."
1487  (interactive)
1488  (save-window-excursion
1489    (switch-to-buffer "*Help*")
1490    (erase-buffer)
1491    (insert (documentation 'Info-mode))
1492    (help-mode)
1493    (goto-char (point-min))
1494    (let (ch flag)
1495      (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1496		    (message (if flag "Type Space to see more"
1497			       "Type Space to return to Info"))
1498		    (if (not (eq ?\  (setq ch (read-event))))
1499			(progn (setq unread-command-events (list ch)) nil)
1500		      flag))
1501	(scroll-up)))
1502    (bury-buffer "*Help*")))
1503
1504(defun Info-get-token (pos start all &optional errorstring)
1505  "Return the token around POS,
1506POS must be somewhere inside the token
1507START is a regular expression which will match the
1508    beginning of the tokens delimited string
1509ALL is a regular expression with a single
1510    parenthesized subpattern which is the token to be
1511    returned. E.g. '{\(.*\)}' would return any string
1512    enclosed in braces around POS.
1513SIG optional fourth argument, controls action on no match
1514    nil: return nil
1515    t: beep
1516    a string: signal an error, using that string."
1517  (save-excursion
1518    (goto-char pos)
1519    ;; First look for a match for START that goes across POS.
1520    (while (and (not (bobp)) (> (point) (- pos (length start)))
1521		(not (looking-at start)))
1522      (forward-char -1))
1523    ;; If we did not find one, search back for START
1524    ;; (this finds only matches that end at or before POS).
1525    (or (looking-at start)
1526	(progn
1527	  (goto-char pos)
1528	  (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
1529    (let (found)
1530      (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1531		  (not (setq found (and (<= (match-beginning 0) pos)
1532					(> (match-end 0) pos))))))
1533      (if (and found (<= (match-beginning 0) pos)
1534	       (> (match-end 0) pos))
1535	  (buffer-substring (match-beginning 1) (match-end 1))
1536	(cond ((null errorstring)
1537	       nil)
1538	      ((eq errorstring t)
1539	       (beep)
1540	       nil)
1541	      (t
1542	       (error "No %s around position %d" errorstring pos)))))))
1543
1544(defun Info-mouse-follow-nearest-node (click)
1545  "\\<Info-mode-map>Follow a node reference near point.
1546Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
1547At end of the node's text, moves to the next node, or up if none."
1548  (interactive "e")
1549  (let* ((start (event-start click))
1550	 (window (car start))
1551	 (pos (car (cdr start))))
1552    (select-window window)
1553    (goto-char pos))
1554  (and (not (Info-try-follow-nearest-node))
1555       (save-excursion (forward-line 1) (eobp))
1556       (Info-next-preorder)))
1557
1558(defun Info-follow-nearest-node ()
1559  "\\<Info-mode-map>Follow a node reference near point.
1560Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
1561If no reference to follow, moves to the next node, or up if none."
1562  (interactive)
1563  (or (Info-try-follow-nearest-node)
1564      (Info-next-preorder)))
1565
1566;; Common subroutine.
1567(defun Info-try-follow-nearest-node ()
1568  "Follow a node reference near point.  Return non-nil if successful."
1569  (let (node)
1570    (cond
1571     ((setq node (Info-get-token (point) "\\*note[ \n]"
1572				 "\\*note[ \n]\\([^:]*\\):"))
1573      (Info-follow-reference node))
1574     ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::"))
1575      (Info-goto-node node))
1576     ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):"))
1577      (Info-menu node))
1578     ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
1579      (Info-goto-node node))
1580     ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
1581      (Info-goto-node node))
1582     ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
1583      (Info-goto-node "Top"))
1584     ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
1585      (Info-goto-node node)))
1586    node))
1587
1588(defvar Info-mode-map nil
1589  "Keymap containing Info commands.")
1590(if Info-mode-map
1591    nil
1592  (setq Info-mode-map (make-keymap))
1593  (suppress-keymap Info-mode-map)
1594  (define-key Info-mode-map "." 'beginning-of-buffer)
1595  (define-key Info-mode-map " " 'Info-scroll-up)
1596  (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
1597  (define-key Info-mode-map "\t" 'Info-next-reference)
1598  (define-key Info-mode-map "\e\t" 'Info-prev-reference)
1599  (define-key Info-mode-map "1" 'Info-nth-menu-item)
1600  (define-key Info-mode-map "2" 'Info-nth-menu-item)
1601  (define-key Info-mode-map "3" 'Info-nth-menu-item)
1602  (define-key Info-mode-map "4" 'Info-nth-menu-item)
1603  (define-key Info-mode-map "5" 'Info-nth-menu-item)
1604  (define-key Info-mode-map "6" 'Info-nth-menu-item)
1605  (define-key Info-mode-map "7" 'Info-nth-menu-item)
1606  (define-key Info-mode-map "8" 'Info-nth-menu-item)
1607  (define-key Info-mode-map "9" 'Info-nth-menu-item)
1608  (define-key Info-mode-map "0" 'undefined)
1609  (define-key Info-mode-map "?" 'Info-summary)
1610  (define-key Info-mode-map "]" 'Info-forward-node)
1611  (define-key Info-mode-map "[" 'Info-backward-node)
1612  (define-key Info-mode-map "<" 'Info-top-node)
1613  (define-key Info-mode-map ">" 'Info-final-node)
1614  (define-key Info-mode-map "b" 'beginning-of-buffer)
1615  (define-key Info-mode-map "d" 'Info-directory)
1616  (define-key Info-mode-map "e" 'Info-edit)
1617  (define-key Info-mode-map "f" 'Info-follow-reference)
1618  (define-key Info-mode-map "g" 'Info-goto-node)
1619  (define-key Info-mode-map "h" 'Info-help)
1620  (define-key Info-mode-map "i" 'Info-index)
1621  (define-key Info-mode-map "l" 'Info-last)
1622  (define-key Info-mode-map "m" 'Info-menu)
1623  (define-key Info-mode-map "n" 'Info-next)
1624  (define-key Info-mode-map "p" 'Info-prev)
1625  (define-key Info-mode-map "q" 'Info-exit)
1626  (define-key Info-mode-map "s" 'Info-search)
1627  ;; For consistency with Rmail.
1628  (define-key Info-mode-map "\M-s" 'Info-search)
1629  (define-key Info-mode-map "t" 'Info-top-node)
1630  (define-key Info-mode-map "u" 'Info-up)
1631  (define-key Info-mode-map "," 'Info-index-next)
1632  (define-key Info-mode-map "\177" 'Info-scroll-down)
1633  (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
1634  )
1635
1636(defun Info-check-pointer (item)
1637  ;; Non-nil if ITEM is present in this node.
1638  (condition-case nil
1639      (Info-extract-pointer item)
1640    (error nil)))
1641
1642(easy-menu-define Info-mode-menu Info-mode-map
1643  "Menu for info files."
1644  '("Info"
1645    ["Up" Info-up (Info-check-pointer "up")]
1646    ["Next" Info-next (Info-check-pointer "next")]
1647    ["Previous" Info-prev (Info-check-pointer "prev[ious]*")]
1648    ("Menu item" ["You should never see this" report-emacs-bug t])
1649    ("Reference" ["You should never see this" report-emacs-bug t])
1650    ["Search..." Info-search t]
1651    ["Goto node..." Info-goto-node t]
1652    ["Last" Info-last Info-history]
1653    ["Exit" Info-exit t]))
1654
1655(defvar Info-menu-last-node nil)
1656;; Last node the menu was created for.
1657
1658(defun Info-menu-update ()
1659  ;; Update the Info menu for the current node.
1660  (condition-case nil
1661      (if (or (not (eq major-mode 'Info-mode))
1662	      (eq Info-current-node Info-menu-last-node))
1663	  ()
1664	;; Update menu menu.
1665	(let* ((Info-complete-menu-buffer (current-buffer))
1666	       (items (nreverse (condition-case nil
1667				    (Info-complete-menu-item
1668				     "" (lambda (e) t) t)
1669				  (error nil))))
1670	       entries current
1671	       (number 0))
1672	  (while (and items (< number 9))
1673	    (setq current (car items)
1674		  items (cdr items)
1675		  number (1+ number))
1676	    (setq entries (cons `[,current
1677				  (Info-menu ,current)
1678				  :keys ,(format "%d" number)]
1679				entries)))
1680	  (if items
1681	      (setq entries (cons ["Other..." Info-menu t] entries)))
1682	  (or entries
1683	      (setq entries (list ["No menu" nil nil])))
1684	  (easy-menu-change '("Info") "Menu item" (nreverse entries)))
1685	;; Update reference menu.  Code stolen from `Info-follow-reference'.
1686	(let ((items nil)
1687	      str i entries current
1688	      (number 0))
1689	  (save-excursion
1690	    (goto-char (point-min))
1691	    (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
1692	      (setq str (buffer-substring
1693			 (match-beginning 1)
1694			 (1- (point))))
1695	      (setq i 0)
1696	      (while (setq i (string-match "[ \n\t]+" str i))
1697		(setq str (concat (substring str 0 i) " "
1698				  (substring str (match-end 0))))
1699		(setq i (1+ i)))
1700	      (setq items
1701		    (cons str items))))
1702	  (while (and items (< number 9))
1703	    (setq current (car items)
1704		  items (cdr items)
1705		  number (1+ number))
1706	    (setq entries (cons `[,current
1707				  (Info-follow-reference ,current)
1708				  t]
1709				entries)))
1710	  (if items
1711	      (setq entries (cons ["Other..." Info-follow-reference t]
1712				  entries)))
1713	  (or entries
1714	      (setq entries (list ["No references" nil nil])))
1715	  (easy-menu-change '("Info") "Reference" (nreverse entries)))
1716	;; Update last seen node.
1717	(setq Info-menu-last-node (current-buffer)))
1718    ;; Try to avoid entering infinite beep mode in case of errors.
1719    (error (ding))))
1720
1721
1722;; Info mode is suitable only for specially formatted data.
1723(put 'info-mode 'mode-class 'special)
1724
1725(defun Info-mode ()
1726  "\\<Info-mode-map>
1727Info mode provides commands for browsing through the Info documentation tree.
1728Documentation in Info is divided into \"nodes\", each of which discusses
1729one topic and contains references to other nodes which discuss related
1730topics.  Info has commands to follow the references and show you other nodes.
1731
1732\\[Info-help]	Invoke the Info tutorial.
1733
1734Selecting other nodes:
1735\\[Info-mouse-follow-nearest-node]
1736	Follow a node reference you click on.
1737	  This works with menu items, cross references, and
1738	  the \"next\", \"previous\" and \"up\", depending on where you click.
1739\\[Info-next]	Move to the \"next\" node of this node.
1740\\[Info-prev]	Move to the \"previous\" node of this node.
1741\\[Info-up]	Move \"up\" from this node.
1742\\[Info-menu]	Pick menu item specified by name (or abbreviation).
1743	Picking a menu item causes another node to be selected.
1744\\[Info-directory]	Go to the Info directory node.
1745\\[Info-follow-reference]	Follow a cross reference.  Reads name of reference.
1746\\[Info-last]	Move to the last node you were at.
1747\\[Info-index]	Look up a topic in this file's Index and move to that node.
1748\\[Info-index-next]	(comma) Move to the next match from a previous `i' command.
1749
1750Moving within a node:
1751\\[Info-scroll-up]	Normally, scroll forward a full screen.  If the end of the buffer is
1752already visible, try to go to the next menu entry, or up if there is none.
1753\\[Info-scroll-down]  Normally, scroll backward.  If the beginning of the buffer is
1754already visible, try to go to the previous menu entry, or up if there is none.
1755\\[beginning-of-buffer]	Go to beginning of node.
1756
1757Advanced commands:
1758\\[Info-exit]	Quit Info: reselect previously selected buffer.
1759\\[Info-edit]	Edit contents of selected node.
17601	Pick first item in node's menu.
17612, 3, 4, 5   Pick second ... fifth item in node's menu.
1762\\[Info-goto-node]	Move to node specified by name.
1763	You may include a filename as well, as (FILENAME)NODENAME.
1764\\[universal-argument] \\[info]	Move to new Info file with completion.
1765\\[Info-search]	Search through this Info file for specified regexp,
1766	and select the node in which the next occurrence is found.
1767\\[Info-next-reference]	Move cursor to next cross-reference or menu item.
1768\\[Info-prev-reference]	Move cursor to previous cross-reference or menu item."
1769  (kill-all-local-variables)
1770  (setq major-mode 'Info-mode)
1771  (setq mode-name "Info")
1772  (setq tab-width 8)
1773  (use-local-map Info-mode-map)
1774  (make-local-hook 'activate-menubar-hook)
1775  (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
1776  (set-syntax-table text-mode-syntax-table)
1777  (setq local-abbrev-table text-mode-abbrev-table)
1778  (setq case-fold-search t)
1779  (setq buffer-read-only t)
1780  (make-local-variable 'Info-current-file)
1781  (make-local-variable 'Info-current-subfile)
1782  (make-local-variable 'Info-current-node)
1783  (make-local-variable 'Info-tag-table-marker)
1784  (setq Info-tag-table-marker (make-marker))
1785  (make-local-variable 'Info-tag-table-buffer)
1786  (setq Info-tag-table-buffer nil)
1787  (make-local-variable 'Info-history)
1788  (make-local-variable 'Info-index-alternatives)
1789  (if (memq (framep (selected-frame)) '(x pc w32))
1790      (progn
1791	(make-face 'info-node)
1792	(make-face 'info-menu-5)
1793	(make-face 'info-xref)
1794	(or (face-differs-from-default-p 'info-node)
1795	    (if (face-differs-from-default-p 'bold-italic)
1796		(copy-face 'bold-italic 'info-node)
1797	      (copy-face 'bold 'info-node)))
1798	(or (face-differs-from-default-p 'info-menu-5)
1799	    (set-face-underline-p 'info-menu-5 t))
1800	(or (face-differs-from-default-p 'info-xref)
1801	    (copy-face 'bold 'info-xref)))
1802    (setq Info-fontify nil))
1803  (Info-set-mode-line)
1804  (run-hooks 'Info-mode-hook))
1805
1806(defvar Info-edit-map nil
1807  "Local keymap used within `e' command of Info.")
1808(if Info-edit-map
1809    nil
1810  (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
1811  (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
1812
1813;; Info-edit mode is suitable only for specially formatted data.
1814(put 'info-edit-mode 'mode-class 'special)
1815
1816(defun Info-edit-mode ()
1817  "Major mode for editing the contents of an Info node.
1818Like text mode with the addition of `Info-cease-edit'
1819which returns to Info mode for browsing.
1820\\{Info-edit-map}"
1821  (use-local-map Info-edit-map)
1822  (setq major-mode 'Info-edit-mode)
1823  (setq mode-name "Info Edit")
1824  (kill-local-variable 'mode-line-buffer-identification)
1825  (setq buffer-read-only nil)
1826  (force-mode-line-update)
1827  (buffer-enable-undo (current-buffer))
1828  (run-hooks 'Info-edit-mode-hook))
1829
1830(defun Info-edit ()
1831  "Edit the contents of this Info node.
1832Allowed only if variable `Info-enable-edit' is non-nil."
1833  (interactive)
1834  (or Info-enable-edit
1835      (error "Editing info nodes is not enabled"))
1836  (Info-edit-mode)
1837  (message "%s" (substitute-command-keys
1838	    "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
1839
1840(defun Info-cease-edit ()
1841  "Finish editing Info node; switch back to Info proper."
1842  (interactive)
1843  ;; Do this first, so nothing has changed if user C-g's at query.
1844  (and (buffer-modified-p)
1845       (y-or-n-p "Save the file? ")
1846       (save-buffer))
1847  (use-local-map Info-mode-map)
1848  (setq major-mode 'Info-mode)
1849  (setq mode-name "Info")
1850  (Info-set-mode-line)
1851  (setq buffer-read-only t)
1852  (force-mode-line-update)
1853  (and (marker-position Info-tag-table-marker)
1854       (buffer-modified-p)
1855       (message "Tags may have changed.  Use Info-tagify if necessary")))
1856
1857(defvar Info-file-list-for-emacs
1858  '("ediff" "forms" "gnus" "info" ("mh" . "mh-e") "sc")
1859  "List of Info files that describe Emacs commands.
1860An element can be a file name, or a list of the form (PREFIX . FILE)
1861where PREFIX is a name prefix and FILE is the file to look in.
1862If the element is just a file name, the file name also serves as the prefix.")
1863
1864(defun Info-find-emacs-command-nodes (command)
1865  "Return a list of locations documenting COMMAND.
1866The `info-file' property of COMMAND says which Info manual to search.
1867If COMMAND has no property, the variable `Info-file-list-for-emacs'
1868defines heuristics for which Info manual to try.
1869The locations are of the format used in Info-history, i.e.
1870\(FILENAME NODENAME BUFFERPOS\)."
1871  (let ((where '())
1872	(cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
1873			  ":\\s *\\(.*\\)\\.$"))
1874	(info-file "emacs"))		;default
1875    ;; Determine which info file this command is documented in.
1876    (if (get command 'info-file)
1877	(setq info-file (get command 'info-file))
1878      ;; If it doesn't say explicitly, test its name against
1879      ;; various prefixes that we know.
1880      (let ((file-list Info-file-list-for-emacs))
1881	(while file-list
1882	  (let* ((elt (car file-list))
1883		 (name (if (consp elt)
1884			   (car elt)
1885			 elt))
1886		 (file (if (consp elt) (cdr elt) elt))
1887		 (regexp (concat "\\`" (regexp-quote name)
1888				 "\\(\\'\\|-\\)")))
1889	    (if (string-match regexp (symbol-name command))
1890		(setq info-file file file-list nil))
1891	    (setq file-list (cdr file-list))))))
1892    (save-excursion
1893      (condition-case nil
1894	  (Info-find-node info-file "Command Index")
1895	;; Some manuals may not have a separate Command Index node,
1896	;; so try just Index instead.
1897	(error
1898	 (Info-find-node info-file "Index")))
1899      ;; Take the index node off the Info history.
1900      (setq Info-history (cdr Info-history))
1901      (goto-char (point-max))
1902      (while (re-search-backward cmd-desc nil t)
1903	  (setq where (cons (list Info-current-file
1904				  (buffer-substring
1905				   (match-beginning 1)
1906				   (match-end 1))
1907				  0)
1908			    where)))
1909      where)))
1910
1911;;;###autoload
1912(defun Info-goto-emacs-command-node (command)
1913  "Go to the Info node in the Emacs manual for command COMMAND.
1914The command is found by looking up in Emacs manual's Command Index
1915or in another manual found via COMMAND's `info-file' property or
1916the variable `Info-file-list-for-emacs'."
1917  (interactive "CFind documentation for command: ")
1918  (or (commandp command)
1919      (signal 'wrong-type-argument (list 'commandp command)))
1920  (let ((where (Info-find-emacs-command-nodes command)))
1921    (if where
1922	(let ((num-matches (length where)))
1923	  ;; Get Info running, and pop to it in another window.
1924	  (save-window-excursion
1925	    (info))
1926	  ;; FIXME It would be cool if this could use a buffer other
1927	  ;; than *info*.
1928	  (pop-to-buffer "*info*")
1929	  (Info-find-node (car (car where))
1930			  (car (cdr (car where))))
1931	  (if (> num-matches 1)
1932	      (progn
1933		;; Info-find-node already pushed (car where) onto
1934		;; Info-history.  Put the other nodes that were found on
1935		;; the history.
1936		(setq Info-history (nconc (cdr where) Info-history))
1937		(message "Found %d other entr%s.  Use %s to see %s."
1938			 (1- num-matches)
1939			 (if (> num-matches 2) "ies" "y")
1940			 (substitute-command-keys "\\[Info-last]")
1941			 (if (> num-matches 2) "them" "it")))))
1942      (error "Couldn't find documentation for %s" command))))
1943
1944;;;###autoload
1945(defun Info-goto-emacs-key-command-node (key)
1946  "Go to the Info node in the Emacs manual the command bound to KEY, a string.
1947Interactively, if the binding is execute-extended-command, a command is read.
1948The command is found by looking up in Emacs manual's Command Index
1949or in another manual found via COMMAND's `info-file' property or
1950the variable `Info-file-list-for-emacs'."
1951  (interactive "kFind documentation for key:")
1952  (let ((command (key-binding key)))
1953    (cond ((null command)
1954	   (message "%s is undefined" (key-description key)))
1955	  ((and (interactive-p)
1956		(eq command 'execute-extended-command))
1957	   (Info-goto-emacs-command-node
1958	    (read-command "Find documentation for command: ")))
1959	  (t
1960	   (Info-goto-emacs-command-node command)))))
1961
1962(defcustom Info-title-face-alist
1963  '((?* bold underline)
1964    (?= bold-italic underline)
1965    (?- italic underline))
1966  "*Alist of face or list of faces to use for pseudo-underlined titles.
1967The alist key is the character the title is underlined with (?*, ?= or ?-)."
1968  :type '(repeat (list character face face))
1969  :group 'info)
1970
1971(defun Info-fontify-node ()
1972  (save-excursion
1973    (let ((buffer-read-only nil))
1974      (goto-char (point-min))
1975      (if (looking-at "^File: [^,: \t]+,?[ \t]+")
1976	  (progn
1977	    (goto-char (match-end 0))
1978	    (while
1979		(looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
1980	      (goto-char (match-end 0))
1981	      (put-text-property (match-beginning 1) (match-end 1)
1982				 'face 'info-xref)
1983	      (put-text-property (match-beginning 1) (match-end 1)
1984				 'mouse-face 'highlight))))
1985      (goto-char (point-min))
1986      (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\)$"
1987				 nil t)
1988	(put-text-property (match-beginning 1) (match-end 1)
1989			   'face
1990			   (cdr (assq (preceding-char) Info-title-face-alist)))
1991	(put-text-property (match-end 1) (match-end 2)
1992			   'invisible t))
1993      (goto-char (point-min))
1994      (while (re-search-forward "\\*Note[ \n\t]+\\([^:]*\\):" nil t)
1995	(if (= (char-after (1- (match-beginning 0))) ?\") ; hack
1996	    nil
1997	  (put-text-property (match-beginning 1) (match-end 1)
1998			     'face 'info-xref)
1999	  (put-text-property (match-beginning 1) (match-end 1)
2000			     'mouse-face 'highlight)))
2001      (goto-char (point-min))
2002      (if (and (search-forward "\n* Menu:" nil t)
2003	       (not (string-match "\\\\>" Info-current-node))
2004	       ;; Don't take time to annotate huge menus
2005	       (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
2006	  (let ((n 0))
2007	    (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
2008	      (setq n (1+ n))
2009	      (if (memq n '(5 9))   ; visual aids to help with 1-9 keys
2010		  (put-text-property (match-beginning 0)
2011				     (1+ (match-beginning 0))
2012				     'face 'info-menu-5))
2013	      (put-text-property (match-beginning 1) (match-end 1)
2014				 'face 'info-node)
2015	      (put-text-property (match-beginning 1) (match-end 1)
2016				 'mouse-face 'highlight))))
2017      (set-buffer-modified-p nil))))
2018
2019
2020;; When an Info buffer is killed, make sure the associated tags buffer
2021;; is killed too.
2022(defun Info-kill-buffer ()
2023  (and (eq major-mode 'Info-mode)
2024       Info-tag-table-buffer
2025       (kill-buffer Info-tag-table-buffer)))
2026
2027(add-hook 'kill-buffer-hook 'Info-kill-buffer)
2028
2029
2030(provide 'info)
2031
2032;;; info.el ends here
2033