(get-next-valid-buffer, last-buffer)
[bpt/emacs.git] / lisp / info.el
CommitLineData
55535639 1;;; info.el --- info package for Emacs
e5167999 2
83040cb0 3;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
0d30b337 4;; 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: help
e5167999 8
a384cab3
JB
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a384cab3
JB
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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
a384cab3 25
e5167999
ER
26;;; Commentary:
27
185cff95 28;; Note that nowadays we expect Info files to be made using makeinfo.
921e5fe6
TTN
29;; In particular we make these assumptions:
30;; - a menu item MAY contain colons but not colon-space ": "
31;; - a menu item ending with ": " (but not ":: ") is an index entry
32;; - a node name MAY NOT contain a colon
505b68d5
TTN
33;; This distinction is to support indexing of computer programming
34;; language terms that may contain ":" but not ": ".
e5167999
ER
35
36;;; Code:
37
bc6262c8
DL
38(eval-when-compile (require 'jka-compr))
39
ded3e3d8 40(defgroup info nil
fc039bdc 41 "Info subsystem."
ded3e3d8
RS
42 :group 'help
43 :group 'docs)
44
45
a384cab3 46(defvar Info-history nil
185cff95 47 "Stack of Info nodes user has visited.
db90d225 48Each element of the stack is a list (FILENAME NODENAME BUFFERPOS).")
a384cab3 49
424b9d3e 50(defvar Info-history-forward nil
185cff95 51 "Stack of Info nodes user has visited with `Info-history-back' command.
db90d225 52Each element of the stack is a list (FILENAME NODENAME BUFFERPOS).")
424b9d3e 53
8a7757f6 54(defvar Info-history-list nil
185cff95 55 "List of all Info nodes user has visited.
db90d225 56Each element of the list is a list (FILENAME NODENAME).")
8a7757f6 57
ded3e3d8 58(defcustom Info-enable-edit nil
d5913bf6 59 "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info can edit the current node.
185cff95 60This is convenient if you want to write Info files by hand.
ada0a60d
RS
61However, we recommend that you not do this.
62It is better to write a Texinfo file and generate the Info file from that,
ded3e3d8
RS
63because that gives you a printed manual as well."
64 :type 'boolean
65 :group 'info)
a384cab3 66
67f445d7 67(defvar Info-enable-active-nodes nil
a384cab3
JB
68 "Non-nil allows Info to execute Lisp code associated with nodes.
69The Lisp code is executed when the node is selected.")
67f445d7 70(put 'Info-enable-active-nodes 'risky-local-variable t)
a384cab3 71
93480d70 72(defface info-node
508bce68
SM
73 '((((class color) (background light)) :foreground "brown" :weight bold :slant italic)
74 (((class color) (background dark)) :foreground "white" :weight bold :slant italic)
75 (t :weight bold :slant italic))
93480d70
RS
76 "Face for Info node names."
77 :group 'info)
78
185cff95
JL
79(defface info-title-1
80 '((((type tty pc) (class color) (background light))
81 :foreground "green" :weight bold)
82 (((type tty pc) (class color) (background dark))
83 :foreground "yellow" :weight bold)
84 (t :height 1.2 :inherit info-title-2))
85 "Face for info titles at level 1."
86 :group 'info)
87;; backward-compatibility alias
88(put 'Info-title-1-face 'face-alias 'info-title-1)
89
90(defface info-title-2
91 '((((type tty pc) (class color)) :foreground "lightblue" :weight bold)
92 (t :height 1.2 :inherit info-title-3))
93 "Face for info titles at level 2."
94 :group 'info)
95;; backward-compatibility alias
96(put 'Info-title-2-face 'face-alias 'info-title-2)
97
98(defface info-title-3
99 '((((type tty pc) (class color)) :weight bold)
100 (t :height 1.2 :inherit info-title-4))
101 "Face for info titles at level 3."
102 :group 'info)
103;; backward-compatibility alias
104(put 'Info-title-3-face 'face-alias 'info-title-3)
105
106(defface info-title-4
107 '((((type tty pc) (class color)) :weight bold)
108 (t :weight bold :inherit variable-pitch))
109 "Face for info titles at level 4."
110 :group 'info)
111;; backward-compatibility alias
112(put 'Info-title-4-face 'face-alias 'info-title-4)
113
114(defface info-menu-header
115 '((((type tty pc))
116 :underline t
117 :weight bold)
118 (t
119 :inherit variable-pitch
120 :weight bold))
121 "Face for headers in Info menus."
122 :group 'info)
123
124(defface info-menu-star
508bce68
SM
125 '((((class color)) :foreground "red1")
126 (t :underline t))
ccfb7415 127 "Face for every third `*' in an Info menu."
93480d70 128 :group 'info)
185cff95 129(put 'info-menu-5 'face-alias 'info-menu-star)
93480d70
RS
130
131(defface info-xref
e37c1854 132 '((((min-colors 88)
ea81d57e
DN
133 (class color) (background light)) :foreground "blue1" :underline t)
134 (((class color) (background light)) :foreground "blue" :underline t)
e37c1854 135 (((min-colors 88)
ea81d57e 136 (class color) (background dark)) :foreground "cyan1" :underline t)
cc3f6c32 137 (((class color) (background dark)) :foreground "cyan" :underline t)
508bce68 138 (t :underline t))
93480d70
RS
139 "Face for Info cross-references."
140 :group 'info)
141
8a7757f6 142(defface info-xref-visited
6b3600ff 143 '((default :inherit info-xref)
508bce68 144 (((class color) (background light)) :foreground "magenta4")
71672328 145 (((class color) (background dark)) :foreground "violet"))
8a7757f6
JL
146 "Face for visited Info cross-references."
147 :group 'info)
148
149(defcustom Info-fontify-visited-nodes t
185cff95 150 "*Non-nil to fontify references to visited nodes in `info-xref-visited' face."
bf247b6e 151 :version "22.1"
8a7757f6
JL
152 :type 'boolean
153 :group 'info)
154
88ef7f62 155(defcustom Info-fontify-maximum-menu-size 100000
ccfb7415 156 "*Maximum size of menu to fontify if `font-lock-mode' is non-nil."
ded3e3d8
RS
157 :type 'integer
158 :group 'info)
bdf62a4d 159
50ac70af 160(defcustom Info-use-header-line t
c3717746 161 "*Non-nil means to put the beginning-of-node links in an Emacs header-line.
50ac70af
MB
162A header-line does not scroll with the rest of the buffer."
163 :type 'boolean
164 :group 'info)
165
166(defface info-header-xref
508bce68 167 '((t :inherit info-xref))
50ac70af
MB
168 "Face for Info cross-references in a node header."
169 :group 'info)
170
171(defface info-header-node
508bce68 172 '((t :inherit info-node))
50ac70af
MB
173 "Face for Info nodes in a node header."
174 :group 'info)
175
7d3766a8 176(defvar Info-directory-list nil
a384cab3 177 "List of directories to search for Info documentation files.
fc4d14c5 178If nil, meaning not yet initialized, Info uses the environment
44c327f9 179variable INFOPATH to initialize it, or `Info-default-directory-list'
dde7f979
SM
180if there is no INFOPATH variable in the environment, or the
181concatenation of the two if INFOPATH ends with a colon.
778911b9
EZ
182
183When `Info-directory-list' is initialized from the value of
4dddb0b7
EZ
184`Info-default-directory-list', and Emacs is installed in one of the
185standard directories, the directory of Info files that come with Emacs
186is put last (so that local Info files override standard ones).
187
188When `Info-directory-list' is initialized from the value of
189`Info-default-directory-list', and Emacs is not installed in one
190of the standard directories, the first element of the resulting
778911b9
EZ
191list is the directory where Emacs installs the Info files that
192come with it. This is so that Emacs's own manual, which suits the
4dddb0b7
EZ
193version of Emacs you are using, will always be found first. This
194is useful when you install an experimental version of Emacs without
195removing the standard installation.
196
197If you want to override the order of directories in
198`Info-default-directory-list', set INFOPATH in the environment.
5da0f945
RS
199
200If you run the Emacs executable from the `src' directory in the Emacs
778911b9
EZ
201source tree, and INFOPATH is not defined, the `info' directory in the
202source tree is used as the first element of `Info-directory-list', in
203place of the installation Info directory. This is useful when you run
204a version of Emacs without installing it.")
a384cab3 205
ded3e3d8 206(defcustom Info-additional-directory-list nil
bdf62a4d 207 "List of additional directories to search for Info documentation files.
e9b1d996 208These directories are searched after those in `Info-directory-list'."
ded3e3d8
RS
209 :type '(repeat directory)
210 :group 'info)
bdf62a4d 211
610bc96b 212(defcustom Info-scroll-prefer-subnodes nil
7a53d8c8 213 "*If non-nil, \\<Info-mode-map>\\[Info-scroll-up] in a menu visits subnodes.
185cff95 214
7a53d8c8
EZ
215If this is non-nil, and you scroll far enough in a node that its menu
216appears on the screen, the next \\<Info-mode-map>\\[Info-scroll-up]
217moves to a subnode indicated by the following menu item. This means
218that you visit a subnode before getting to the end of the menu.
219
220Setting this option to nil results in behavior similar to the stand-alone
221Info reader program, which visits the first subnode from the menu only
222when you hit the end of the current node."
bf247b6e 223 :version "22.1"
7a53d8c8
EZ
224 :type 'boolean
225 :group 'info)
226
a9efebd0 227(defcustom Info-hide-note-references t
5f812a3c 228 "*If non-nil, hide the tag and section reference in *note and * menu items.
8a7757f6 229If value is non-nil but not `hide', also replaces the \"*note\" with \"see\".
185cff95
JL
230If value is non-nil but not t or `hide', the reference section is still shown.
231`nil' completely disables this feature."
bf247b6e 232 :version "22.1"
8a7757f6 233 :type '(choice (const :tag "No hiding" nil)
0452a274 234 (const :tag "Replace tag and hide reference" t)
8a7757f6
JL
235 (const :tag "Hide tag and reference" hide)
236 (other :tag "Only replace tag" tag))
a9efebd0
KS
237 :group 'info)
238
bb1e1730
KS
239(defcustom Info-refill-paragraphs nil
240 "*If non-nil, attempt to refill paragraphs with hidden references.
185cff95 241This refilling may accidentally remove explicit line breaks in the Info
bb1e1730 242file, so be prepared for a few surprises if you enable this feature."
bf247b6e 243 :version "22.1"
bb1e1730
KS
244 :type 'boolean
245 :group 'info)
246
180ee7c7 247(defcustom Info-search-whitespace-regexp "\\s-+"
8a7757f6
JL
248 "*If non-nil, regular expression to match a sequence of whitespace chars.
249This applies to Info search for regular expressions.
250You might want to use something like \"[ \\t\\r\\n]+\" instead.
251In the Customization buffer, that is `[' followed by a space,
252a tab, a carriage return (control-M), a newline, and `]+'."
253 :type 'regexp
254 :group 'info)
255
98e77be5 256(defcustom Info-isearch-search t
6270bbe7
JL
257 "*If non-nil, isearch in Info searches through multiple nodes.
258Before leaving the initial Info node, where isearch was started,
259it fails once with the error message [initial node], and with
260subsequent C-s/C-r continues through other nodes without failing
261with this error message in other nodes. When isearch fails for
262the rest of the manual, it wraps aroung the whole manual and
263restarts the search from the top/final node depending on
264search direction.
265
266Setting this option to nil restores the default isearch behavior
267with wrapping around the current Info node."
98e77be5
JL
268 :version "22.1"
269 :type 'boolean
270 :group 'info)
271
d457219d
JL
272(defvar Info-isearch-initial-node nil)
273
dde7f979
SM
274(defcustom Info-mode-hook
275 ;; Try to obey obsolete Info-fontify settings.
276 (unless (and (boundp 'Info-fontify) (null Info-fontify))
277 '(turn-on-font-lock))
8a7757f6
JL
278 "Hooks run when `Info-mode' is called."
279 :type 'hook
280 :group 'info)
281
282(defcustom Info-selection-hook nil
283 "Hooks run when `Info-select-node' is called."
544e5562
CW
284 :type 'hook
285 :group 'info)
286
8a7757f6
JL
287(defvar Info-edit-mode-hook nil
288 "Hooks run when `Info-edit-mode' is called.")
289
a384cab3 290(defvar Info-current-file nil
d6be34f3
RS
291 "Info file that Info is now looking at, or nil.
292This is the name that was specified in Info, not the actual file name.
d590dbd2 293It doesn't contain directory names or file name extensions added by Info.")
a384cab3
JB
294
295(defvar Info-current-subfile nil
fdf4b680 296 "Info subfile that is actually in the *info* buffer now.
185cff95 297nil if current Info file is not split into subfiles.")
a384cab3
JB
298
299(defvar Info-current-node nil
300 "Name of node that Info is now looking at, or nil.")
301
c5fe2ff1 302(defvar Info-tag-table-marker nil
a384cab3
JB
303 "Marker pointing at beginning of current Info file's tag table.
304Marker points nowhere if file has no tag table.")
305
c5fe2ff1
RS
306(defvar Info-tag-table-buffer nil
307 "Buffer used for indirect tag tables.")
308
552775bd
RS
309(defvar Info-current-file-completions nil
310 "Cached completion list for current Info file.")
311
94b10022
JL
312(defvar Info-file-supports-index-cookies nil
313 "Non-nil if current Info file supports index cookies.")
314
1143a6b0 315(defvar Info-index-alternatives nil
fdf4b680 316 "List of possible matches for last `Info-index' command.")
1143a6b0 317
836bb478
JL
318(defvar Info-point-loc nil
319 "Point location within a selected node.
320If string, the point is moved to the proper occurrence of the
321name of the followed cross reference within a selected node.
322If number, the point is moved to the corresponding line.")
8a7757f6 323
552775bd
RS
324(defvar Info-standalone nil
325 "Non-nil if Emacs was started solely as an Info browser.")
7d3766a8 326\f
bd0c9168 327(defvar Info-suffix-list
3c19e6c1
RS
328 ;; The MS-DOS list should work both when long file names are
329 ;; supported (Windows 9X), and when only 8+3 file names are available.
bd0c9168
RS
330 (if (eq system-type 'ms-dos)
331 '( (".gz" . "gunzip")
332 (".z" . "gunzip")
544e210f 333 (".bz2" . ("bzip2" "-dc"))
3c19e6c1
RS
334 (".inz" . "gunzip")
335 (".igz" . "gunzip")
336 (".info.Z" . "gunzip")
337 (".info.gz" . "gunzip")
338 ("-info.Z" . "gunzip")
339 ("-info.gz" . "gunzip")
340 ("/index.gz". "gunzip")
341 ("/index.z" . "gunzip")
9b842cab 342 (".inf" . nil)
3c19e6c1
RS
343 (".info" . nil)
344 ("-info" . nil)
345 ("/index" . nil)
bd0c9168 346 ("" . nil))
a70dc410
RS
347 '( (".info.Z". "uncompress")
348 (".info.Y". "unyabba")
349 (".info.gz". "gunzip")
350 (".info.z". "gunzip")
544e210f 351 (".info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
352 (".info". nil)
353 ("-info.Z". "uncompress")
354 ("-info.Y". "unyabba")
355 ("-info.gz". "gunzip")
544e210f 356 ("-info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
357 ("-info.z". "gunzip")
358 ("-info". nil)
359 ("/index.Z". "uncompress")
360 ("/index.Y". "unyabba")
361 ("/index.gz". "gunzip")
362 ("/index.z". "gunzip")
544e210f 363 ("/index.bz2". ("bzip2" "-dc"))
a70dc410
RS
364 ("/index". nil)
365 (".Z". "uncompress")
366 (".Y". "unyabba")
367 (".gz". "gunzip")
368 (".z". "gunzip")
544e210f 369 (".bz2" . ("bzip2" "-dc"))
a70dc410 370 ("". nil)))
1143a6b0
ER
371 "List of file name suffixes and associated decoding commands.
372Each entry should be (SUFFIX . STRING); the file is given to
544e210f
KH
373the command as standard input.
374
375STRING may be a list of strings. In that case, the first element is
376the command name, and the rest are arguments to that command.
377
378If STRING is nil, no decoding is done.
97f99202
KH
379Because the SUFFIXes are tried in order, the empty string should
380be last in the list.")
1143a6b0 381
c8e9dd54 382;; Concatenate SUFFIX onto FILENAME. SUFFIX should start with a dot.
e0c1e774
EZ
383;; First, on MS-DOS with no long file names support, delete some of
384;; the extension in FILENAME to make room.
385(defun info-insert-file-contents-1 (filename suffix lfn)
386 (if lfn ; long file names are supported
bd0c9168
RS
387 (concat filename suffix)
388 (let* ((sans-exts (file-name-sans-extension filename))
c8e9dd54
RS
389 ;; How long is the extension in FILENAME (not counting the dot).
390 (ext-len (max 0 (- (length filename) (length sans-exts) 1)))
391 ext-left)
9b842cab 392 ;; SUFFIX starts with a dot. If FILENAME already has one,
1583fe94 393 ;; get rid of the one in SUFFIX (unless suffix is empty).
c8e9dd54 394 (or (and (<= ext-len 0)
9b842cab 395 (not (eq (aref filename (1- (length filename))) ?.)))
1583fe94 396 (= (length suffix) 0)
9b842cab 397 (setq suffix (substring suffix 1)))
c8e9dd54
RS
398 ;; How many chars of that extension should we keep?
399 (setq ext-left (min ext-len (max 0 (- 3 (length suffix)))))
bd0c9168
RS
400 ;; Get rid of the rest of the extension, and add SUFFIX.
401 (concat (substring filename 0 (- (length filename)
402 (- ext-len ext-left)))
403 suffix))))
404
9c7924d5
RS
405(defun info-file-exists-p (filename)
406 (and (file-exists-p filename)
407 (not (file-directory-p filename))))
408
1143a6b0 409(defun info-insert-file-contents (filename &optional visit)
185cff95 410 "Insert the contents of an Info file in the current buffer.
1143a6b0 411Do the right thing if the file has been compressed or zipped."
e0c1e774 412 (let* ((tail Info-suffix-list)
ab5a535c
KS
413 (lfn (if (fboundp 'msdos-long-file-names)
414 (msdos-long-file-names)
415 t))
e0c1e774
EZ
416 (check-short (and (fboundp 'msdos-long-file-names)
417 lfn))
418 fullname decoder done)
b8fa4e87 419 (if (info-file-exists-p filename)
bd0c9168
RS
420 ;; FILENAME exists--see if that name contains a suffix.
421 ;; If so, set DECODE accordingly.
97f99202
KH
422 (progn
423 (while (and tail
424 (not (string-match
425 (concat (regexp-quote (car (car tail))) "$")
426 filename)))
427 (setq tail (cdr tail)))
428 (setq fullname filename
429 decoder (cdr (car tail))))
bd0c9168 430 ;; Try adding suffixes to FILENAME and see if we can find something.
e0c1e774
EZ
431 (while (and tail (not done))
432 (setq fullname (info-insert-file-contents-1 filename
433 (car (car tail)) lfn))
434 (if (info-file-exists-p fullname)
435 (setq done t
436 ;; If we found a file with a suffix, set DECODER
437 ;; according to the suffix.
438 decoder (cdr (car tail)))
439 ;; When the MS-DOS port runs on Windows, we need to check
440 ;; the short variant of a long file name as well.
441 (when check-short
442 (setq fullname (info-insert-file-contents-1 filename
443 (car (car tail)) nil))
444 (if (info-file-exists-p fullname)
445 (setq done t
446 decoder (cdr (car tail))))))
97f99202 447 (setq tail (cdr tail)))
97f99202 448 (or tail
bd0c9168 449 (error "Can't find %s or any compressed version of it" filename)))
e175bda2 450 ;; check for conflict with jka-compr
70746649 451 (if (and (jka-compr-installed-p)
e175bda2
RS
452 (jka-compr-get-compression-info fullname))
453 (setq decoder nil))
97f99202 454 (if decoder
3c19e6c1
RS
455 (progn
456 (insert-file-contents-literally fullname visit)
457 (let ((buffer-read-only nil)
458 (coding-system-for-write 'no-conversion)
459 (default-directory (or (file-name-directory fullname)
460 default-directory)))
544e210f
KH
461 (or (consp decoder)
462 (setq decoder (list decoder)))
463 (apply 'call-process-region (point-min) (point-max)
464 (car decoder) t t nil (cdr decoder))))
3c19e6c1 465 (insert-file-contents fullname visit))))
7d3766a8 466\f
3a59b662
SM
467(defun Info-default-dirs ()
468 (let ((source (expand-file-name "info/" source-directory))
469 (sibling (if installation-directory
470 (expand-file-name "info/" installation-directory)
471 (if invocation-directory
472 (let ((infodir (expand-file-name
473 "../info/"
474 invocation-directory)))
475 (if (file-exists-p infodir)
476 infodir
477 (setq infodir (expand-file-name
478 "../../../info/"
479 invocation-directory))
480 (and (file-exists-p infodir)
481 infodir))))))
482 alternative)
483 (setq alternative
484 (if (and sibling (file-exists-p sibling))
485 ;; Uninstalled, Emacs builddir != srcdir.
486 sibling
487 ;; Uninstalled, builddir == srcdir
488 source))
489 (if (or (member alternative Info-default-directory-list)
490 ;; On DOS/NT, we use movable executables always,
491 ;; and we must always find the Info dir at run time.
492 (if (memq system-type '(ms-dos windows-nt))
493 nil
494 ;; Use invocation-directory for Info
495 ;; only if we used it for exec-directory also.
496 (not (string= exec-directory
497 (expand-file-name "lib-src/"
498 installation-directory))))
499 (not (file-exists-p alternative)))
500 Info-default-directory-list
501 ;; `alternative' contains the Info files that came with this
502 ;; version, so we should look there first. `Info-insert-dir'
503 ;; currently expects to find `alternative' first on the list.
504 (cons alternative
a025fccf
SM
505 ;; Don't drop the last part, it might contain non-Emacs stuff.
506 ;; (reverse (cdr (reverse
507 Info-default-directory-list)))) ;; )))
3a59b662 508
7d3766a8 509(defun info-initialize ()
fdf4b680 510 "Initialize `Info-directory-list', if that hasn't been done yet."
7d3766a8 511 (unless Info-directory-list
3a59b662 512 (let ((path (getenv "INFOPATH")))
7d3766a8 513 (setq Info-directory-list
78e7e8a0
MB
514 (prune-directory-list
515 (if path
3a59b662
SM
516 (if (string-match ":\\'" path)
517 (append (split-string (substring path 0 -1)
518 (regexp-quote path-separator))
519 (Info-default-dirs))
520 (split-string path (regexp-quote path-separator)))
521 (Info-default-dirs)))))))
1143a6b0 522
a6e4564e
RS
523;;;###autoload
524(defun info-other-window (&optional file)
525 "Like `info' but show the Info buffer in another window."
526 (interactive (if current-prefix-arg
527 (list (read-file-name "Info file name: " nil nil t))))
836bb478 528 (let (same-window-buffer-names same-window-regexps)
a6e4564e 529 (info file)))
399c88ad 530
836bb478 531;;;###autoload (add-hook 'same-window-regexps "\\*info\\*\\(\\|<[0-9]+>\\)")
211d6309 532
cc3f6c32 533;;;###autoload (put 'info 'info-file "emacs")
a384cab3 534;;;###autoload
836bb478 535(defun info (&optional file buffer)
a384cab3
JB
536 "Enter Info, the documentation browser.
537Optional argument FILE specifies the file to examine;
538the default is the top-level directory of Info.
83d5c772
DL
539Called from a program, FILE may specify an Info node of the form
540`(FILENAME)NODENAME'.
836bb478
JL
541Optional argument BUFFER specifies the Info buffer name;
542the default buffer name is *info*. If BUFFER exists,
543just switch to BUFFER. Otherwise, create a new buffer
544with the top-level Info directory.
a384cab3 545
836bb478
JL
546In interactive use, a non-numeric prefix argument directs
547this command to read a file name from the minibuffer.
bc224645
JL
548A numeric prefix argument selects an Info buffer with the prefix number
549appended to the Info buffer name.
e341dab2
RS
550
551The search path for Info files is in the variable `Info-directory-list'.
399c88ad 552The top-level Info directory is made by combining all the files named `dir'
e341dab2 553in all the directories in that path."
836bb478
JL
554 (interactive (list
555 (if (and current-prefix-arg (not (numberp current-prefix-arg)))
556 (read-file-name "Info file name: " nil nil t))
557 (if (numberp current-prefix-arg)
558 (format "*info*<%s>" current-prefix-arg))))
559 (pop-to-buffer (or buffer "*info*"))
560 (if (and buffer (not (eq major-mode 'Info-mode)))
561 (Info-mode))
a384cab3 562 (if file
60705054
SM
563 ;; If argument already contains parentheses, don't add another set
564 ;; since the argument will then be parsed improperly. This also
565 ;; has the added benefit of allowing node names to be included
566 ;; following the parenthesized filename.
83040cb0
SM
567 (Info-goto-node
568 (if (and (stringp file) (string-match "(.*)" file))
569 file
570 (concat "(" file ")")))
60705054 571 (if (zerop (buffer-size))
89b064a9 572 (Info-directory))))
a384cab3 573
c3717746
RS
574;;;###autoload
575(defun info-emacs-manual ()
3a8803c0 576 "Display the Emacs manual in Info mode."
c3717746
RS
577 (interactive)
578 (info "emacs"))
579
552775bd
RS
580;;;###autoload
581(defun info-standalone ()
582 "Run Emacs as a standalone Info reader.
583Usage: emacs -f info-standalone [filename]
584In standalone mode, \\<Info-mode-map>\\[Info-exit] exits Emacs itself."
585 (setq Info-standalone t)
586 (if (and command-line-args-left
587 (not (string-match "^-" (car command-line-args-left))))
588 (condition-case err
589 (progn
590 (info (car command-line-args-left))
591 (setq command-line-args-left (cdr command-line-args-left)))
592 (error (send-string-to-terminal
593 (format "%s\n" (if (eq (car-safe err) 'error)
594 (nth 1 err) err)))
595 (save-buffers-kill-emacs)))
596 (info)))
7d3766a8 597\f
983dfbf8 598;; See if the accessible portion of the buffer begins with a node
11638562
EZ
599;; delimiter, and the node header line which follows matches REGEXP.
600;; Typically, this test will be followed by a loop that examines the
601;; rest of the buffer with (search-forward "\n\^_"), and it's a pity
602;; to have the overhead of this special test inside the loop.
603
604;; This function changes match-data, but supposedly the caller might
605;; want to use the results of re-search-backward.
606
607;; The return value is the value of point at the beginning of matching
983dfbf8 608;; REGEXP, if the function succeeds, nil otherwise.
11638562 609(defun Info-node-at-bob-matching (regexp)
760d5cb3
GM
610 (and (bobp) ; are we at beginning of buffer?
611 (looking-at "\^_") ; does it begin with node delimiter?
11638562
EZ
612 (let (beg)
613 (forward-line 1)
614 (setq beg (point))
760d5cb3 615 (forward-line 1) ; does the line after delimiter match REGEXP?
11638562
EZ
616 (re-search-backward regexp beg t))))
617
8a7757f6
JL
618(defun Info-find-file (filename &optional noerror)
619 "Return expanded FILENAME, or t, if FILENAME is \"dir\".
620Optional second argument NOERROR, if t, means if file is not found
621just return nil (no error)."
a384cab3
JB
622 ;; Convert filename to lower case if not found as specified.
623 ;; Expand it.
4fceda3c 624 (if (stringp filename)
a384cab3 625 (let (temp temp-downcase found)
37a3ff5b 626 (setq filename (substitute-in-file-name filename))
89b064a9 627 (cond
ab2f22ad
JH
628 ((string= (downcase filename) "dir")
629 (setq found t))
630 ((string= filename "apropos")
631 (setq found 'apropos))
89b064a9
JL
632 ((string= filename "history")
633 (setq found 'history))
634 ((string= filename "toc")
635 (setq found 'toc))
ab2f22ad
JH
636 (t
637 (let ((dirs (if (string-match "^\\./" filename)
37a3ff5b
RS
638 ;; If specified name starts with `./'
639 ;; then just try current directory.
640 '("./")
641 (if (file-name-absolute-p filename)
642 ;; No point in searching for an
643 ;; absolute file name
644 '(nil)
645 (if Info-additional-directory-list
646 (append Info-directory-list
647 Info-additional-directory-list)
648 Info-directory-list)))))
649 ;; Search the directory list for file FILENAME.
650 (while (and dirs (not found))
651 (setq temp (expand-file-name filename (car dirs)))
652 (setq temp-downcase
653 (expand-file-name (downcase filename) (car dirs)))
654 ;; Try several variants of specified name.
e0c1e774 655 (let ((suffix-list Info-suffix-list)
ab5a535c
KS
656 (lfn (if (fboundp 'msdos-long-file-names)
657 (msdos-long-file-names)
658 t)))
37a3ff5b
RS
659 (while (and suffix-list (not found))
660 (cond ((info-file-exists-p
661 (info-insert-file-contents-1
e0c1e774 662 temp (car (car suffix-list)) lfn))
37a3ff5b
RS
663 (setq found temp))
664 ((info-file-exists-p
665 (info-insert-file-contents-1
e0c1e774
EZ
666 temp-downcase (car (car suffix-list)) lfn))
667 (setq found temp-downcase))
668 ((and (fboundp 'msdos-long-file-names)
669 lfn
670 (info-file-exists-p
671 (info-insert-file-contents-1
672 temp (car (car suffix-list)) nil)))
673 (setq found temp)))
37a3ff5b 674 (setq suffix-list (cdr suffix-list))))
ab2f22ad 675 (setq dirs (cdr dirs))))))
37a3ff5b
RS
676 (if found
677 (setq filename found)
8a7757f6
JL
678 (if noerror
679 (setq filename nil)
680 (error "Info file %s does not exist" filename)))
681 filename)))
682
683(defun Info-find-node (filename nodename &optional no-going-back)
185cff95 684 "Go to an Info node specified as separate FILENAME and NODENAME.
8a7757f6
JL
685NO-GOING-BACK is non-nil if recovering from an error in this function;
686it says do not attempt further (recursive) error recovery."
687 (info-initialize)
688 (setq filename (Info-find-file filename))
185cff95 689 ;; Go into Info buffer.
6270bbe7 690 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
37a3ff5b 691 ;; Record the node we are leaving.
d590dbd2 692 (if (not no-going-back)
37a3ff5b
RS
693 (setq Info-history
694 (cons (list Info-current-file Info-current-node (point))
695 Info-history)))
4fceda3c
SM
696 (Info-find-node-2 filename nodename no-going-back))
697
d590dbd2 698;;;###autoload
4fceda3c 699(defun Info-on-current-buffer (&optional nodename)
70746649
RS
700 "Use Info mode to browse the current Info buffer.
701With a prefix arg, this queries for the node name to visit first;
702otherwise, that defaults to `Top'."
4fceda3c
SM
703 (interactive
704 (list (if current-prefix-arg
705 (completing-read "Node name: " (Info-build-node-completions)
07e91aa3
SM
706 nil t "Top"))))
707 (unless nodename (setq nodename "Top"))
75296efc 708 (info-initialize)
4fceda3c 709 (Info-mode)
d590dbd2
CY
710 (set (make-local-variable 'Info-current-file)
711 (or buffer-file-name
712 ;; If called on a non-file buffer, make a fake file name.
713 (concat default-directory (buffer-name))))
4fceda3c
SM
714 (Info-find-node-2 nil nodename))
715
f584b4c7
RS
716;; It's perhaps a bit nasty to kill the *info* buffer to force a re-read,
717;; but at least it keeps this routine (which is only for the benefit of
718;; makeinfo-buffer) out of the way of normal operations.
719;;
720(defun Info-revert-find-node (filename nodename)
185cff95 721 "Go to an Info node FILENAME and NODENAME, re-reading disk contents.
f584b4c7
RS
722When *info* is already displaying FILENAME and NODENAME, the window position
723is preserved, if possible."
724 (pop-to-buffer "*info*")
725 (let ((old-filename Info-current-file)
726 (old-nodename Info-current-node)
727 (pcolumn (current-column))
728 (pline (count-lines (point-min) (line-beginning-position)))
729 (wline (count-lines (point-min) (window-start)))
730 (old-history Info-history)
d590dbd2 731 (new-history (list Info-current-file Info-current-node (point))))
f584b4c7
RS
732 (kill-buffer (current-buffer))
733 (Info-find-node filename nodename)
734 (setq Info-history old-history)
735 (if (and (equal old-filename Info-current-file)
736 (equal old-nodename Info-current-node))
737 (progn
738 ;; note goto-line is no good, we want to measure from point-min
cb7de2e7 739 (goto-char (point-min))
f584b4c7
RS
740 (forward-line wline)
741 (set-window-start (selected-window) (point))
cb7de2e7 742 (goto-char (point-min))
f584b4c7
RS
743 (forward-line pline)
744 (move-to-column pcolumn))
745 ;; only add to the history when coming from a different file+node
746 (if new-history
747 (setq Info-history (cons new-history Info-history))))))
748
357d11de
GM
749(defun Info-find-in-tag-table-1 (marker regexp case-fold)
750 "Find a node in a tag table.
751MARKER specifies the buffer and position to start searching at.
752REGEXP is a regular expression matching nodes or references. Its first
753group should match `Node:' or `Ref:'.
754CASE-FOLD t means search for a case-insensitive match.
755If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
756FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
757where the match was found, and MODE is `major-mode' of the buffer in
758which the match was found."
dde7f979 759 (let ((case-fold-search case-fold))
357d11de
GM
760 (save-excursion
761 (set-buffer (marker-buffer marker))
762 (goto-char marker)
921e5fe6 763
357d11de
GM
764 ;; Search tag table
765 (beginning-of-line)
766 (when (re-search-forward regexp nil t)
767 (list (string-equal "Ref:" (match-string 1))
365d2503 768 (+ (point-min) (read (current-buffer)))
357d11de
GM
769 major-mode)))))
770
771(defun Info-find-in-tag-table (marker regexp)
772 "Find a node in a tag table.
773MARKER specifies the buffer and position to start searching at.
774REGEXP is a regular expression matching nodes or references. Its first
775group should match `Node:' or `Ref:'.
776If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
777FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
778where the match was found, and MODE is `major-mode' of the buffer in
779which the match was found.
780This function tries to find a case-sensitive match first, then a
781case-insensitive match is tried."
782 (let ((result (Info-find-in-tag-table-1 marker regexp nil)))
783 (when (null (car result))
784 (setq result (Info-find-in-tag-table-1 marker regexp t)))
785 result))
786
787(defun Info-find-node-in-buffer-1 (regexp case-fold)
788 "Find a node or anchor in the current buffer.
789REGEXP is a regular expression matching nodes or references. Its first
790group should match `Node:' or `Ref:'.
791CASE-FOLD t means search for a case-insensitive match.
792Value is the position at which a match was found, or nil if not found."
793 (let ((case-fold-search case-fold)
794 found)
795 (save-excursion
796 (when (Info-node-at-bob-matching regexp)
797 (setq found (point)))
798 (while (and (not found)
799 (search-forward "\n\^_" nil t))
800 (forward-line 1)
801 (let ((beg (point)))
802 (forward-line 1)
803 (when (re-search-backward regexp beg t)
804 (beginning-of-line)
805 (setq found (point)))))
806 found)))
921e5fe6 807
357d11de
GM
808(defun Info-find-node-in-buffer (regexp)
809 "Find a node or anchor in the current buffer.
810REGEXP is a regular expression matching nodes or references. Its first
811group should match `Node:' or `Ref:'.
812Value is the position at which a match was found, or nil if not found.
813This function looks for a case-sensitive match first. If none is found,
814a case-insensitive match is tried."
815 (or (Info-find-node-in-buffer-1 regexp nil)
816 (Info-find-node-in-buffer-1 regexp t)))
921e5fe6 817
4fceda3c 818(defun Info-find-node-2 (filename nodename &optional no-going-back)
9e5c2f50 819 (buffer-disable-undo (current-buffer))
a384cab3
JB
820 (or (eq major-mode 'Info-mode)
821 (Info-mode))
822 (widen)
823 (setq Info-current-node nil)
824 (unwind-protect
8bdf4b20
KH
825 (let ((case-fold-search t)
826 anchorpos)
37a3ff5b
RS
827 ;; Switch files if necessary
828 (or (null filename)
829 (equal Info-current-file filename)
830 (let ((buffer-read-only nil))
831 (setq Info-current-file nil
832 Info-current-subfile nil
833 Info-current-file-completions nil
834 buffer-file-name nil)
835 (erase-buffer)
ab2f22ad
JH
836 (cond
837 ((eq filename t)
838 (Info-insert-dir))
839 ((eq filename 'apropos)
840 (insert-buffer-substring " *info-apropos*"))
89b064a9
JL
841 ((eq filename 'history)
842 (insert-buffer-substring " *info-history*"))
843 ((eq filename 'toc)
844 (insert-buffer-substring " *info-toc*"))
ab2f22ad 845 (t
f7d5479b 846 (info-insert-file-contents filename nil)
ab2f22ad 847 (setq default-directory (file-name-directory filename))))
37a3ff5b 848 (set-buffer-modified-p nil)
94b10022
JL
849
850 ;; Check makeinfo version for index cookie support
851 (let ((found nil))
852 (goto-char (point-min))
853 (condition-case ()
854 (if (and (re-search-forward
855 "makeinfo version \\([0-9]+.[0-9]+\\)"
856 (line-beginning-position 3) t)
857 (not (version< (match-string 1) "4.7")))
858 (setq found t))
859 (error nil))
860 (set (make-local-variable 'Info-file-supports-index-cookies) found))
861
37a3ff5b
RS
862 ;; See whether file has a tag table. Record the location if yes.
863 (goto-char (point-max))
864 (forward-line -8)
865 ;; Use string-equal, not equal, to ignore text props.
866 (if (not (or (string-equal nodename "*")
867 (not
868 (search-forward "\^_\nEnd tag table\n" nil t))))
869 (let (pos)
870 ;; We have a tag table. Find its beginning.
871 ;; Is this an indirect file?
872 (search-backward "\nTag table:\n")
873 (setq pos (point))
874 (if (save-excursion
875 (forward-line 2)
876 (looking-at "(Indirect)\n"))
877 ;; It is indirect. Copy it to another buffer
878 ;; and record that the tag table is in that buffer.
879 (let ((buf (current-buffer))
880 (tagbuf
881 (or Info-tag-table-buffer
882 (generate-new-buffer " *info tag table*"))))
883 (setq Info-tag-table-buffer tagbuf)
884 (save-excursion
885 (set-buffer tagbuf)
9e5c2f50 886 (buffer-disable-undo (current-buffer))
37a3ff5b
RS
887 (setq case-fold-search t)
888 (erase-buffer)
889 (insert-buffer-substring buf))
890 (set-marker Info-tag-table-marker
891 (match-end 0) tagbuf))
892 (set-marker Info-tag-table-marker pos)))
893 (set-marker Info-tag-table-marker nil))
894 (setq Info-current-file
ab2f22ad
JH
895 (cond
896 ((eq filename t) "dir")
897 ((eq filename 'apropos) "apropos")
89b064a9
JL
898 ((eq filename 'history) "history")
899 ((eq filename 'toc) "toc")
ab2f22ad
JH
900 (t filename)))
901 ))
37a3ff5b
RS
902 ;; Use string-equal, not equal, to ignore text props.
903 (if (string-equal nodename "*")
904 (progn (setq Info-current-node nodename)
905 (Info-set-mode-line))
906 ;; Possibilities:
907 ;;
908 ;; 1. Anchor found in tag table
909 ;; 2. Anchor *not* in tag table
910 ;;
911 ;; 3. Node found in tag table
912 ;; 4. Node *not* found in tag table, but found in file
913 ;; 5. Node *not* in tag table, and *not* in file
914 ;;
915 ;; *Or* the same, but in an indirect subfile.
916
917 ;; Search file for a suitable node.
8bdf4b20 918 (let ((guesspos (point-min))
357d11de 919 (regexp (concat "\\(Node:\\|Ref:\\) *\\("
4ce5c223 920 (if (stringp nodename)
dd31e4e8
GM
921 (regexp-quote nodename)
922 "")
dde7f979 923 "\\) *[,\t\n\177]")))
37a3ff5b 924
82172606 925 (catch 'foo
921e5fe6 926
82172606 927 ;; First, search a tag table, if any
357d11de
GM
928 (when (marker-position Info-tag-table-marker)
929 (let* ((m Info-tag-table-marker)
930 (found (Info-find-in-tag-table m regexp)))
921e5fe6 931
357d11de
GM
932 (when found
933 ;; FOUND is (ANCHOR POS MODE).
934 (setq guesspos (nth 1 found))
921e5fe6 935
357d11de
GM
936 ;; If this is an indirect file, determine which
937 ;; file really holds this node and read it in.
938 (unless (eq (nth 2 found) 'Info-mode)
939 ;; Note that the current buffer must be the
940 ;; *info* buffer on entry to
941 ;; Info-read-subfile. Thus the hackery above.
942 (setq guesspos (Info-read-subfile guesspos)))
82172606
DL
943
944 ;; Handle anchor
357d11de
GM
945 (when (nth 0 found)
946 (goto-char (setq anchorpos guesspos))
947 (throw 'foo t)))))
82172606
DL
948
949 ;; Else we may have a node, which we search for:
950 (goto-char (max (point-min)
951 (- (byte-to-position guesspos) 1000)))
921e5fe6 952
357d11de
GM
953 ;; Now search from our advised position (or from beg of
954 ;; buffer) to find the actual node. First, check
955 ;; whether the node is right where we are, in case the
956 ;; buffer begins with a node.
957 (let ((pos (Info-find-node-in-buffer regexp)))
958 (when pos
959 (goto-char pos)
141b49f5
JL
960 (throw 'foo t)))
961
962 (when (string-match "\\([^.]+\\)\\." nodename)
963 (let (Info-point-loc)
964 (Info-find-node-2
965 filename (match-string 1 nodename) no-going-back))
966 (widen)
967 (throw 'foo t))
968
969 ;; No such anchor in tag table or node in tag table or file
970 (error "No such node or anchor: %s" nodename))
82172606
DL
971
972 (Info-select-node)
8a7757f6
JL
973 (goto-char (point-min))
974 (cond (anchorpos
975 (let ((new-history (list Info-current-file
976 (substring-no-properties nodename))))
977 ;; Add anchors to the history too
978 (setq Info-history-list
979 (cons new-history
980 (delete new-history Info-history-list))))
981 (goto-char anchorpos))
836bb478
JL
982 ((numberp Info-point-loc)
983 (forward-line (1- Info-point-loc))
984 (setq Info-point-loc nil))
985 ((stringp Info-point-loc)
986 (Info-find-index-name Info-point-loc)
987 (setq Info-point-loc nil))))))
a384cab3
JB
988 ;; If we did not finish finding the specified node,
989 ;; go back to the previous one.
4db579ab 990 (or Info-current-node no-going-back (null Info-history)
37a3ff5b
RS
991 (let ((hist (car Info-history)))
992 (setq Info-history (cdr Info-history))
993 (Info-find-node (nth 0 hist) (nth 1 hist) t)
994 (goto-char (nth 2 hist))))))
a384cab3 995
44c327f9
JB
996;; Cache the contents of the (virtual) dir file, once we have merged
997;; it for the first time, so we can save time subsequently.
7ea13762
RS
998(defvar Info-dir-contents nil)
999
44c327f9
JB
1000;; Cache for the directory we decided to use for the default-directory
1001;; of the merged dir text.
1002(defvar Info-dir-contents-directory nil)
1003
c142ab2d
RS
1004;; Record the file attributes of all the files from which we
1005;; constructed Info-dir-contents.
1006(defvar Info-dir-file-attributes nil)
1007
4fba3b2c
RS
1008(defvar Info-dir-file-name nil)
1009
7ea13762 1010;; Construct the Info directory node by merging the files named `dir'
44c327f9
JB
1011;; from various directories. Set the *info* buffer's
1012;; default-directory to the first directory we actually get any text
1013;; from.
7ea13762 1014(defun Info-insert-dir ()
c142ab2d
RS
1015 (if (and Info-dir-contents Info-dir-file-attributes
1016 ;; Verify that none of the files we used has changed
1017 ;; since we used it.
1018 (eval (cons 'and
b461e32d
SM
1019 (mapcar (lambda (elt)
1020 (let ((curr (file-attributes
1021 ;; Handle symlinks
1022 (file-truename (car elt)))))
1023
1024 ;; Don't compare the access time.
1025 (if curr (setcar (nthcdr 4 curr) 0))
1026 (setcar (nthcdr 4 (cdr elt)) 0)
1027 (equal (cdr elt) curr)))
c142ab2d 1028 Info-dir-file-attributes))))
4fba3b2c
RS
1029 (progn
1030 (insert Info-dir-contents)
1031 (goto-char (point-min)))
dd31e4e8 1032 (let ((dirs (if Info-additional-directory-list
b34dcc82
RS
1033 (append Info-directory-list
1034 Info-additional-directory-list)
1035 Info-directory-list))
1bd19a31 1036 (dir-file-attrs nil)
1bcedb3b
RS
1037 ;; Bind this in case the user sets it to nil.
1038 (case-fold-search t)
4fba3b2c
RS
1039 ;; This is set non-nil if we find a problem in some input files.
1040 problems
f4008b6e 1041 buffers buffer others nodes dirs-done)
44c327f9
JB
1042
1043 ;; Search the directory list for the directory file.
7ea13762 1044 (while dirs
8d1abb42
RS
1045 (let ((truename (file-truename (expand-file-name (car dirs)))))
1046 (or (member truename dirs-done)
1047 (member (directory-file-name truename) dirs-done)
1048 ;; Try several variants of specified name.
1049 ;; Try upcasing, appending `.info', or both.
825d6f08
RS
1050 (let* (file
1051 (attrs
1052 (or
1053 (progn (setq file (expand-file-name "dir" truename))
1054 (file-attributes file))
1055 (progn (setq file (expand-file-name "DIR" truename))
1056 (file-attributes file))
1057 (progn (setq file (expand-file-name "dir.info" truename))
1058 (file-attributes file))
1059 (progn (setq file (expand-file-name "DIR.INFO" truename))
1060 (file-attributes file)))))
8d1abb42
RS
1061 (setq dirs-done
1062 (cons truename
1063 (cons (directory-file-name truename)
1064 dirs-done)))
825d6f08
RS
1065 (if attrs
1066 (save-excursion
1067 (or buffers
1068 (message "Composing main Info directory..."))
2d41cf59 1069 (set-buffer (generate-new-buffer " info dir"))
9d499629
RS
1070 (condition-case nil
1071 (progn
1072 (insert-file-contents file)
1bd19a31
MB
1073 (set (make-local-variable 'Info-dir-file-name)
1074 file)
6af7040d 1075 (push (current-buffer) buffers)
1bd19a31 1076 (push (cons file attrs) dir-file-attrs))
9d499629 1077 (error (kill-buffer (current-buffer))))))))
1bd19a31
MB
1078 (unless (cdr dirs)
1079 (set (make-local-variable 'Info-dir-contents-directory)
1080 (file-name-as-directory (car dirs))))
825d6f08 1081 (setq dirs (cdr dirs))))
399c88ad 1082
4db579ab 1083 (or buffers
81e14cb2 1084 (error "Can't find the Info directory node"))
1bd19a31 1085
44c327f9 1086 ;; Distinguish the dir file that comes with Emacs from all the
f4008b6e 1087 ;; others. Yes, that is really what this is supposed to do.
f601cd3e
DL
1088 ;; The definition of `Info-directory-list' puts it first on that
1089 ;; list and so last in `buffers' at this point.
1090 (setq buffer (car (last buffers))
1091 others (delq buffer buffers))
44c327f9 1092
debcea0c
KH
1093 ;; Insert the entire original dir file as a start; note that we've
1094 ;; already saved its default directory to use as the default
1095 ;; directory for the whole concatenation.
69672704 1096 (save-excursion (insert-buffer-substring buffer))
44c327f9 1097
7ea13762 1098 ;; Look at each of the other buffers one by one.
6af7040d
SM
1099 (dolist (other others)
1100 (let (this-buffer-nodes)
7ea13762 1101 ;; In each, find all the menus.
6af7040d 1102 (with-current-buffer other
7ea13762
RS
1103 (goto-char (point-min))
1104 ;; Find each menu, and add an elt to NODES for it.
1105 (while (re-search-forward "^\\* Menu:" nil t)
6af7040d
SM
1106 (while (and (zerop (forward-line 1)) (eolp)))
1107 (let ((beg (point))
1108 nodename end)
1109 (re-search-backward "^\^_")
7ea13762 1110 (search-forward "Node: ")
89b064a9 1111 (setq nodename (Info-following-node-name))
3a6ade8a 1112 (search-forward "\n\^_" nil 'move)
7ea13762
RS
1113 (beginning-of-line)
1114 (setq end (point))
6af7040d 1115 (push (list nodename other beg end) this-buffer-nodes)))
9f4aee27 1116 (if (assoc-string "top" this-buffer-nodes t)
4fba3b2c
RS
1117 (setq nodes (nconc this-buffer-nodes nodes))
1118 (setq problems t)
6af7040d 1119 (message "No `top' node in %s" Info-dir-file-name)))))
fdf4b680 1120 ;; Add to the main menu a menu item for each other node.
6af7040d 1121 (re-search-forward "^\\* Menu:")
7ea13762
RS
1122 (forward-line 1)
1123 (let ((menu-items '("top"))
3a6ade8a 1124 (end (save-excursion (search-forward "\^_" nil t) (point))))
6af7040d
SM
1125 (dolist (node nodes)
1126 (let ((nodename (car node)))
c3a29d70
RS
1127 (save-excursion
1128 (or (member (downcase nodename) menu-items)
b37daea4 1129 (re-search-forward (concat "^\\* +"
c3a29d70
RS
1130 (regexp-quote nodename)
1131 "::")
1132 end t)
1133 (progn
1134 (insert "* " nodename "::" "\n")
6af7040d 1135 (push nodename menu-items)))))))
7ea13762
RS
1136 ;; Now take each node of each of the other buffers
1137 ;; and merge it into the main buffer.
6af7040d 1138 (dolist (node nodes)
760d5cb3 1139 (let ((case-fold-search t)
6af7040d 1140 (nodename (car node)))
7ea13762
RS
1141 (goto-char (point-min))
1142 ;; Find the like-named node in the main buffer.
ba42ce14 1143 (if (re-search-forward (concat "^\^_.*\n.*Node: "
7ea13762
RS
1144 (regexp-quote nodename)
1145 "[,\n\t]")
1146 nil t)
1147 (progn
3a6ade8a 1148 (search-forward "\n\^_" nil 'move)
25869acf
RS
1149 (beginning-of-line)
1150 (insert "\n"))
7ea13762
RS
1151 ;; If none exists, add one.
1152 (goto-char (point-max))
dfbf6104 1153 (insert "\^_\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
7ea13762
RS
1154 ;; Merge the text from the other buffer's menu
1155 ;; into the menu in the like-named node in the main buffer.
6af7040d
SM
1156 (apply 'insert-buffer-substring (cdr node))))
1157 (Info-dir-remove-duplicates)
dc9bec22
TTN
1158 ;; Kill all the buffers we just made, including the special one excised.
1159 (mapc 'kill-buffer (cons buffer buffers))
4fba3b2c
RS
1160 (goto-char (point-min))
1161 (if problems
1162 (message "Composing main Info directory...problems encountered, see `*Messages*'")
1bd19a31
MB
1163 (message "Composing main Info directory...done"))
1164 (set (make-local-variable 'Info-dir-contents) (buffer-string))
1165 (set (make-local-variable 'Info-dir-file-attributes) dir-file-attrs)))
44c327f9 1166 (setq default-directory Info-dir-contents-directory))
7ea13762 1167
6af7040d
SM
1168(defvar Info-streamline-headings
1169 '(("Emacs" . "Emacs")
1170 ("Programming" . "Programming")
1171 ("Libraries" . "Libraries")
1172 ("World Wide Web\\|Net Utilities" . "Net Utilities"))
1173 "List of elements (RE . NAME) to merge headings matching RE to NAME.")
1174
1175(defun Info-dir-remove-duplicates ()
1176 (let (limit)
1177 (goto-char (point-min))
1178 ;; Remove duplicate headings in the same menu.
1179 (while (search-forward "\n* Menu:" nil t)
141b49f5 1180 (setq limit (save-excursion (search-forward "\n\^_" nil t)))
6af7040d
SM
1181 ;; Look for the next heading to unify.
1182 (while (re-search-forward "^\\(\\w.*\\)\n\\*" limit t)
1183 (let ((name (match-string 1))
1184 (start (match-beginning 0))
1185 (entries nil) re)
1186 ;; Check whether this heading should be streamlined.
1187 (save-match-data
1188 (dolist (x Info-streamline-headings)
1189 (when (string-match (car x) name)
1190 (setq name (cdr x))
1191 (setq re (car x)))))
1192 (if re (replace-match name t t nil 1))
1193 (goto-char (if (re-search-forward "^[^* \n\t]" limit t)
1194 (match-beginning 0)
1195 (or limit (point-max))))
1196 ;; Look for other headings of the same category and merge them.
1197 (save-excursion
1198 (while (re-search-forward "^\\(\\w.*\\)\n\\*" limit t)
1199 (when (if re (save-match-data (string-match re (match-string 1)))
1200 (equal name (match-string 1)))
1201 (forward-line 0)
1202 ;; Delete redundant heading.
1203 (delete-region (match-beginning 0) (point))
1204 ;; Push the entries onto `text'.
1205 (push
1206 (delete-and-extract-region
1207 (point)
1208 (if (re-search-forward "^[^* \n\t]" nil t)
1209 (match-beginning 0)
1210 (or limit (point-max)))) entries))))
1211 ;; Insert the entries just found.
1212 (while (= (line-beginning-position 0) (1- (point)))
1213 (backward-char))
1214 (dolist (entry (nreverse entries))
1215 (insert entry)
1216 (while (= (line-beginning-position 0) (1- (point)))
1217 (delete-region (1- (point)) (point))))
921e5fe6 1218
6af7040d
SM
1219 ;; Now remove duplicate entries under the same heading.
1220 (let ((seen nil)
1221 (limit (point)))
1222 (goto-char start)
1223 (while (re-search-forward "^* \\([^:\n]+:\\(:\\|[^.\n]+\\).\\)"
1224 limit 'move)
b499789c
TTN
1225 ;; Fold case straight away; `member-ignore-case' here wasteful.
1226 (let ((x (downcase (match-string 1))))
1227 (if (member x seen)
6af7040d
SM
1228 (delete-region (match-beginning 0)
1229 (progn (re-search-forward "^[^ \t]" nil t)
25046503 1230 (match-beginning 0)))
6af7040d
SM
1231 (push x seen))))))))))
1232
c5fe2ff1
RS
1233;; Note that on entry to this function the current-buffer must be the
1234;; *info* buffer; not the info tags buffer.
a384cab3 1235(defun Info-read-subfile (nodepos)
a361deba
RS
1236 ;; NODEPOS is either a position (in the Info file as a whole,
1237 ;; not relative to a subfile) or the name of a subfile.
a384cab3
JB
1238 (let (lastfilepos
1239 lastfilename)
a361deba
RS
1240 (if (numberp nodepos)
1241 (save-excursion
1242 (set-buffer (marker-buffer Info-tag-table-marker))
1243 (goto-char (point-min))
ba42ce14
EZ
1244 (or (looking-at "\^_")
1245 (search-forward "\n\^_"))
a361deba
RS
1246 (forward-line 2)
1247 (catch 'foo
1248 (while (not (looking-at "\^_"))
1249 (if (not (eolp))
1250 (let ((beg (point))
1251 thisfilepos thisfilename)
1252 (search-forward ": ")
1253 (setq thisfilename (buffer-substring beg (- (point) 2)))
365d2503 1254 (setq thisfilepos (+ (point-min) (read (current-buffer))))
a361deba
RS
1255 ;; read in version 19 stops at the end of number.
1256 ;; Advance to the next line.
1257 (forward-line 1)
1258 (if (> thisfilepos nodepos)
1259 (throw 'foo t))
1260 (setq lastfilename thisfilename)
1261 (setq lastfilepos thisfilepos))
1262 (forward-line 1)))))
1263 (setq lastfilename nodepos)
1264 (setq lastfilepos 0))
c5fe2ff1
RS
1265 ;; Assume previous buffer is in Info-mode.
1266 ;; (set-buffer (get-buffer "*info*"))
a384cab3
JB
1267 (or (equal Info-current-subfile lastfilename)
1268 (let ((buffer-read-only nil))
1269 (setq buffer-file-name nil)
1270 (widen)
1271 (erase-buffer)
1143a6b0 1272 (info-insert-file-contents lastfilename)
a384cab3
JB
1273 (set-buffer-modified-p nil)
1274 (setq Info-current-subfile lastfilename)))
b0e52598
RS
1275 ;; Widen in case we are in the same subfile as before.
1276 (widen)
a384cab3 1277 (goto-char (point-min))
ba42ce14
EZ
1278 (if (looking-at "\^_")
1279 (forward-char 1)
1280 (search-forward "\n\^_"))
a361deba
RS
1281 (if (numberp nodepos)
1282 (+ (- nodepos lastfilepos) (point)))))
a384cab3 1283
ec9b1372 1284(defun Info-unescape-quotes (value)
1ed6e0c3 1285 "Unescape double quotes and backslashes in VALUE."
ec9b1372
KB
1286 (let ((start 0)
1287 (unquote value))
1288 (while (string-match "[^\\\"]*\\(\\\\\\)[\\\\\"]" unquote start)
1289 (setq unquote (replace-match "" t t unquote 1))
1290 (setq start (- (match-end 0) 1)))
1291 unquote))
1292
1293;; As of Texinfo 4.6, makeinfo writes constructs like
1294;; \0\h[image param=value ...\h\0]
1295;; into the Info file for handling images.
1296(defun Info-split-parameter-string (parameter-string)
1297 "Return alist of (\"KEY\" . \"VALUE\") from PARAMETER-STRING; a
1ed6e0c3
JPW
1298whitespace separated list of KEY=VALUE pairs. If VALUE contains
1299whitespace or double quotes, it must be quoted in double quotes and
1300any double quotes or backslashes must be escaped (\\\",\\\\)."
ec9b1372
KB
1301 (let ((start 0)
1302 (parameter-alist))
1303 (while (string-match
1304 "\\s *\\([^=]+\\)=\\(?:\\([^\\s \"]+\\)\\|\\(?:\"\\(\\(?:[^\\\"]\\|\\\\[\\\\\"]\\)*\\)\"\\)\\)"
1305 parameter-string start)
1306 (setq start (match-end 0))
1307 (push (cons (match-string 1 parameter-string)
1308 (or (match-string 2 parameter-string)
1309 (Info-unescape-quotes
1310 (match-string 3 parameter-string))))
1311 parameter-alist))
1312 parameter-alist))
1313
1314(defun Info-display-images-node ()
1315 "Display images in current node."
1316 (save-excursion
1317 (let ((inhibit-read-only t)
dde7f979 1318 (case-fold-search t))
ec9b1372
KB
1319 (goto-char (point-min))
1320 (while (re-search-forward
1321 "\\(\0\b[[]image\\(\\(?:[^\b]\\|[^\0]+\b\\)*\\)\0\b[]]\\)"
1322 nil t)
1323 (let* ((start (match-beginning 1))
1324 (parameter-alist (Info-split-parameter-string (match-string 2)))
1325 (src (cdr (assoc-string "src" parameter-alist)))
1326 (image-file (if src (if (file-name-absolute-p src) src
1327 (concat default-directory src))
1328 ""))
1329 (image (if (file-exists-p image-file)
1330 (create-image image-file)
1331 "[broken image]")))
ec9b1372
KB
1332 (if (not (get-text-property start 'display))
1333 (add-text-properties
1334 start (point) `(display ,image rear-nonsticky (display)))))))
1335 (set-buffer-modified-p nil)))
1336
0c1b7af5
JL
1337;; Texinfo 4.7 adds cookies of the form ^@^H[NAME CONTENTS ^@^H].
1338;; Hide any construct of the general form ^@[^@-^_][ ... ^@[^@-^_]],
1339;; including one optional trailing newline.
1340(defun Info-hide-cookies-node ()
e37c1854 1341 "Hide unrecognized cookies in current node."
0c1b7af5
JL
1342 (save-excursion
1343 (let ((inhibit-read-only t)
1344 (case-fold-search t))
1345 (goto-char (point-min))
1346 (while (re-search-forward
1347 "\\(\0[\0-\37][[][^\0]*\0[\0-\37][]]\n?\\)"
1348 nil t)
1349 (let* ((start (match-beginning 1)))
1350 (if (not (get-text-property start 'invisible))
1351 (put-text-property start (point) 'invisible t)))))
1352 (set-buffer-modified-p nil)))
1353
a384cab3 1354(defun Info-select-node ()
185cff95 1355 "Select the Info node that point is in."
b038485e 1356 ;; Bind this in case the user sets it to nil.
1bcedb3b
RS
1357 (let ((case-fold-search t))
1358 (save-excursion
760d5cb3
GM
1359 ;; Find beginning of node.
1360 (if (search-backward "\n\^_" nil 'move)
1361 (forward-line 2)
1362 (if (looking-at "\^_")
1363 (forward-line 1)
1364 (signal 'search-failed (list "\n\^_"))))
1365 ;; Get nodename spelled as it is in the node.
1366 (re-search-forward "Node:[ \t]*")
1367 (setq Info-current-node
1368 (buffer-substring-no-properties (point)
1369 (progn
1370 (skip-chars-forward "^,\t\n")
1371 (point))))
1372 (Info-set-mode-line)
1373 ;; Find the end of it, and narrow.
1374 (beginning-of-line)
1375 (let (active-expression)
50ac70af 1376 ;; Narrow to the node contents
760d5cb3
GM
1377 (narrow-to-region (point)
1378 (if (re-search-forward "\n[\^_\f]" nil t)
1379 (prog1
1380 (1- (point))
1381 (if (looking-at "[\n\^_\f]*execute: ")
1382 (progn
1383 (goto-char (match-end 0))
1384 (setq active-expression
1385 (read (current-buffer))))))
1386 (point-max)))
1387 (if Info-enable-active-nodes (eval active-expression))
8a7757f6
JL
1388 ;; Add a new unique history item to full history list
1389 (let ((new-history (list Info-current-file Info-current-node)))
1390 (setq Info-history-list
424b9d3e
JL
1391 (cons new-history (delete new-history Info-history-list)))
1392 (setq Info-history-forward nil))
141b49f5
JL
1393 (if (not (eq Info-fontify-maximum-menu-size nil))
1394 (Info-fontify-node))
ec9b1372 1395 (Info-display-images-node)
0c1b7af5 1396 (Info-hide-cookies-node)
760d5cb3 1397 (run-hooks 'Info-selection-hook)))))
a384cab3
JB
1398
1399(defun Info-set-mode-line ()
1400 (setq mode-line-buffer-identification
313fe58a 1401 (nconc (propertized-buffer-identification "%b")
918e364a
MB
1402 (list
1403 (concat " ("
d590dbd2
CY
1404 (if Info-current-file
1405 (file-name-nondirectory Info-current-file)
1406 " ")
1407 ") " (or Info-current-node ""))))))
a384cab3 1408\f
185cff95 1409;; Go to an Info node specified with a filename-and-nodename string
a384cab3
JB
1410;; of the sort that is found in pointers in nodes.
1411
83040cb0
SM
1412;; Don't autoload this function: the correct entry point for other packages
1413;; to use is `info'. --Stef
1414;; ;;;###autoload
4fceda3c 1415(defun Info-goto-node (nodename &optional fork)
185cff95 1416 "Go to Info node named NODENAME. Give just NODENAME or (FILENAME)NODENAME.
dc414be1
EZ
1417If NODENAME is of the form (FILENAME)NODENAME, the node is in the Info file
1418FILENAME; otherwise, NODENAME should be in the current Info file (or one of
1419its sub-files).
1420Completion is available, but only for node names in the current Info file.
7210b6f5 1421If FORK is non-nil (interactively with a prefix arg), show the node in
185cff95 1422a new Info buffer.
4fceda3c 1423If FORK is a string, it is the name to use for the new buffer."
d1268e52 1424 (interactive (list (Info-read-node-name "Go to node: ") current-prefix-arg))
d0e41cba 1425 (info-initialize)
4fceda3c 1426 (if fork
760d5cb3
GM
1427 (set-buffer
1428 (clone-buffer (concat "*info-" (if (stringp fork) fork nodename) "*") t)))
a384cab3
JB
1429 (let (filename)
1430 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
1431 nodename)
1432 (setq filename (if (= (match-beginning 1) (match-end 1))
1433 ""
c60c12be
SM
1434 (match-string 2 nodename))
1435 nodename (match-string 3 nodename))
836bb478 1436 (let ((trim (string-match "\\s +\\'" filename)))
a384cab3 1437 (if trim (setq filename (substring filename 0 trim))))
836bb478 1438 (let ((trim (string-match "\\s +\\'" nodename)))
a384cab3 1439 (if trim (setq nodename (substring nodename 0 trim))))
e579232c 1440 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
1441 (Info-find-node (if (equal filename "") nil filename)
1442 (if (equal nodename "") "Top" nodename))))
552775bd 1443
ddf89211
RS
1444(defvar Info-read-node-completion-table)
1445
a89da416
RS
1446(defun Info-read-node-name-2 (string path-and-suffixes action)
1447 "Virtual completion table for file names input in Info node names.
1448PATH-AND-SUFFIXES is a pair of lists, (DIRECTORIES . SUFFIXES)."
1449 (let* ((names nil)
1450 (suffixes (remove "" (cdr path-and-suffixes)))
1451 (suffix (concat (regexp-opt suffixes t) "\\'"))
1452 (string-dir (file-name-directory string))
1453 (dirs
1454 (if (file-name-absolute-p string)
1455 (list (file-name-directory string))
1456 (car path-and-suffixes))))
1457 (dolist (dir dirs)
1458 (unless dir
1459 (setq dir default-directory))
1460 (if string-dir (setq dir (expand-file-name string-dir dir)))
1461 (when (file-directory-p dir)
1462 (dolist (file (file-name-all-completions
1463 (file-name-nondirectory string) dir))
1464 ;; If the file name has no suffix or a standard suffix,
1465 ;; include it.
1466 (and (or (null (file-name-extension file))
1467 (string-match suffix file))
185cff95 1468 ;; But exclude subfiles of split Info files.
a89da416
RS
1469 (not (string-match "-[0-9]+\\'" file))
1470 ;; And exclude backup files.
1471 (not (string-match "~\\'" file))
1472 (push (if string-dir (concat string-dir file) file) names))
1473 ;; If the file name ends in a standard suffix,
1474 ;; add the unsuffixed name as a completion option.
1475 (when (string-match suffix file)
1476 (setq file (substring file 0 (match-beginning 0)))
1477 (push (if string-dir (concat string-dir file) file) names)))))
1478 (cond
1479 ((eq action t) (all-completions string names))
1480 ((null action) (try-completion string names))
1481 (t (test-completion string names)))))
1482
03524be6 1483;; This function is used as the "completion table" while reading a node name.
ddf89211 1484;; It does completion using the alist in Info-read-node-completion-table
03524be6
RS
1485;; unless STRING starts with an open-paren.
1486(defun Info-read-node-name-1 (string predicate code)
365d2503
SM
1487 (cond
1488 ;; First complete embedded file names.
1489 ((string-match "\\`([^)]*\\'" string)
1490 (let ((file (substring string 1)))
1491 (cond
1492 ((eq code nil)
a89da416 1493 (let ((comp (try-completion file 'Info-read-node-name-2
365d2503
SM
1494 (cons Info-directory-list
1495 (mapcar 'car Info-suffix-list)))))
1496 (cond
1497 ((eq comp t) (concat string ")"))
1498 (comp (concat "(" comp)))))
a89da416
RS
1499 ((eq code t)
1500 (all-completions file 'Info-read-node-name-2
1501 (cons Info-directory-list
1502 (mapcar 'car Info-suffix-list))))
365d2503
SM
1503 (t nil))))
1504 ;; If a file name was given, then any node is fair game.
1505 ((string-match "\\`(" string)
1506 (cond
1507 ((eq code nil) string)
1508 ((eq code t) nil)
1509 (t t)))
1510 ;; Otherwise use Info-read-node-completion-table.
1511 ((eq code nil)
1512 (try-completion string Info-read-node-completion-table predicate))
1513 ((eq code t)
1514 (all-completions string Info-read-node-completion-table predicate))
1515 (t
1516 (test-completion string Info-read-node-completion-table predicate))))
03524be6 1517
a89da416
RS
1518;; Arrange to highlight the proper letters in the completion list buffer.
1519(put 'Info-read-node-name-1 'completion-base-size-function
70f31424
JL
1520 (lambda ()
1521 (if (string-match "\\`([^)]*\\'"
1522 (or completion-common-substring
1523 (minibuffer-completion-contents)))
1524 1
1525 0)))
a89da416 1526
552775bd
RS
1527(defun Info-read-node-name (prompt &optional default)
1528 (let* ((completion-ignore-case t)
ddf89211 1529 (Info-read-node-completion-table (Info-build-node-completions))
b6d61ffa 1530 (nodename (completing-read prompt 'Info-read-node-name-1 nil t)))
552775bd
RS
1531 (if (equal nodename "")
1532 (or default
1533 (Info-read-node-name prompt))
1534 nodename)))
1535
1536(defun Info-build-node-completions ()
1537 (or Info-current-file-completions
1bcedb3b
RS
1538 (let ((compl nil)
1539 ;; Bind this in case the user sets it to nil.
11638562
EZ
1540 (case-fold-search t)
1541 (node-regexp "Node: *\\([^,\n]*\\) *[,\n\t]"))
552775bd
RS
1542 (save-excursion
1543 (save-restriction
1544 (if (marker-buffer Info-tag-table-marker)
c5fe2ff1
RS
1545 (let ((marker Info-tag-table-marker))
1546 (set-buffer (marker-buffer marker))
cedb118c 1547 (widen)
c5fe2ff1 1548 (goto-char marker)
fdf4b680 1549 (while (re-search-forward "\n\\(Node\\|Ref\\): \\(.*\\)\177" nil t)
552775bd 1550 (setq compl
fdf4b680 1551 (cons (list (match-string-no-properties 2))
552775bd
RS
1552 compl))))
1553 (widen)
1554 (goto-char (point-min))
11638562
EZ
1555 ;; If the buffer begins with a node header, process that first.
1556 (if (Info-node-at-bob-matching node-regexp)
110d4e80 1557 (setq compl (list (match-string-no-properties 1))))
11638562 1558 ;; Now for the rest of the nodes.
552775bd
RS
1559 (while (search-forward "\n\^_" nil t)
1560 (forward-line 1)
1561 (let ((beg (point)))
1562 (forward-line 1)
11638562 1563 (if (re-search-backward node-regexp beg t)
399c88ad 1564 (setq compl
110d4e80 1565 (cons (list (match-string-no-properties 1))
552775bd 1566 compl))))))))
87d2b5a2 1567 (setq compl (cons '("*") compl))
07b1b912 1568 (set (make-local-variable 'Info-current-file-completions) compl))))
a384cab3 1569\f
aea2a8da
JB
1570(defun Info-restore-point (hl)
1571 "If this node has been visited, restore the point value when we left."
cedb118c
RS
1572 (while hl
1573 (if (and (equal (nth 0 (car hl)) Info-current-file)
e90d1271
RS
1574 ;; Use string-equal, not equal, to ignore text props.
1575 (string-equal (nth 1 (car hl)) Info-current-node))
cedb118c 1576 (progn
d96504df
KH
1577 (goto-char (nth 2 (car hl)))
1578 (setq hl nil)) ;terminate the while at next iter
cedb118c 1579 (setq hl (cdr hl)))))
aea2a8da 1580\f
2c80a618
GM
1581(defvar Info-search-history nil
1582 "The history list for `Info-search'.")
a384cab3 1583
8a7757f6
JL
1584(defvar Info-search-case-fold nil
1585 "The value of `case-fold-search' from previous `Info-search' command.")
1586
0b02cda9
JL
1587(defun Info-search (regexp &optional bound noerror count direction)
1588 "Search for REGEXP, starting from point, and select node it's found in.
1589If DIRECTION is `backward', search in the reverse direction."
bbc96600
EZ
1590 (interactive (list (read-string
1591 (if Info-search-history
5b76833f
RF
1592 (format "Regexp search%s (default %s): "
1593 (if case-fold-search "" " case-sensitively")
bbc96600 1594 (car Info-search-history))
8a7757f6 1595 (format "Regexp search%s: "
5b76833f 1596 (if case-fold-search "" " case-sensitively")))
bbc96600 1597 nil 'Info-search-history)))
2c80a618
GM
1598 (when transient-mark-mode
1599 (deactivate-mark))
1600 (when (equal regexp "")
1601 (setq regexp (car Info-search-history)))
c47b5bbe 1602 (when regexp
774f8aee 1603 (let (found beg-found give-up
0b02cda9 1604 (backward (eq direction 'backward))
c47b5bbe
DL
1605 (onode Info-current-node)
1606 (ofile Info-current-file)
1607 (opoint (point))
0b02cda9
JL
1608 (opoint-min (point-min))
1609 (opoint-max (point-max))
c47b5bbe
DL
1610 (ostart (window-start))
1611 (osubfile Info-current-subfile))
8a7757f6 1612 (setq Info-search-case-fold case-fold-search)
c47b5bbe
DL
1613 (save-excursion
1614 (save-restriction
1615 (widen)
ca53db33
JL
1616 (when backward
1617 ;; Hide Info file header for backward search
1618 (narrow-to-region (save-excursion
1619 (goto-char (point-min))
1620 (search-forward "\n\^_")
1621 (1- (point)))
1622 (point-max)))
774f8aee 1623 (while (and (not give-up)
be4b3ae8
JL
1624 (save-match-data
1625 (or (null found)
1626 (if backward
1627 (isearch-range-invisible found beg-found)
1628 (isearch-range-invisible beg-found found))
1629 ;; Skip node header line
180ee7c7
JL
1630 (and (save-excursion (forward-line -1)
1631 (looking-at "\^_"))
866920e3 1632 (forward-line (if backward -1 1)))
be4b3ae8
JL
1633 ;; Skip Tag Table node
1634 (save-excursion
1635 (and (search-backward "\^_" nil t)
1636 (looking-at "\^_\nTag Table"))))))
180ee7c7 1637 (let ((search-spaces-regexp Info-search-whitespace-regexp))
5d4faeda
RS
1638 (if (if backward
1639 (re-search-backward regexp bound t)
1640 (re-search-forward regexp bound t))
1641 (setq found (point) beg-found (if backward (match-end 0)
1642 (match-beginning 0)))
1643 (setq give-up t))))))
d457219d
JL
1644
1645 (when (and isearch-mode Info-isearch-search
1646 (not Info-isearch-initial-node)
1647 (not bound)
1648 (or give-up (and found (not (and (> found opoint-min)
1649 (< found opoint-max))))))
1650 (signal 'search-failed (list regexp "initial node")))
1651
774f8aee
RS
1652 ;; If no subfiles, give error now.
1653 (if give-up
c47b5bbe 1654 (if (null Info-current-subfile)
180ee7c7
JL
1655 (let ((search-spaces-regexp Info-search-whitespace-regexp))
1656 (if backward
1657 (re-search-backward regexp)
1658 (re-search-forward regexp)))
774f8aee
RS
1659 (setq found nil)))
1660
d457219d
JL
1661 (if (and bound (not found))
1662 (signal 'search-failed (list regexp)))
1663
0b02cda9 1664 (unless (or found bound)
774f8aee
RS
1665 (unwind-protect
1666 ;; Try other subfiles.
1667 (let ((list ()))
1668 (save-excursion
1669 (set-buffer (marker-buffer Info-tag-table-marker))
1670 (goto-char (point-min))
1671 (search-forward "\n\^_\nIndirect:")
1672 (save-restriction
1673 (narrow-to-region (point)
1674 (progn (search-forward "\n\^_")
1675 (1- (point))))
c5fe2ff1 1676 (goto-char (point-min))
774f8aee
RS
1677 ;; Find the subfile we just searched.
1678 (search-forward (concat "\n" osubfile ": "))
1679 ;; Skip that one.
0b02cda9 1680 (forward-line (if backward 0 1))
866920e3 1681 (if backward (forward-char -1))
774f8aee
RS
1682 ;; Make a list of all following subfiles.
1683 ;; Each elt has the form (VIRT-POSITION . SUBFILENAME).
0b02cda9
JL
1684 (while (not (if backward (bobp) (eobp)))
1685 (if backward
1686 (re-search-backward "\\(^.*\\): [0-9]+$")
1687 (re-search-forward "\\(^.*\\): [0-9]+$"))
774f8aee
RS
1688 (goto-char (+ (match-end 1) 2))
1689 (setq list (cons (cons (+ (point-min)
1690 (read (current-buffer)))
1691 (match-string-no-properties 1))
1692 list))
0b02cda9
JL
1693 (goto-char (if backward
1694 (1- (match-beginning 0))
1695 (1+ (match-end 0)))))
774f8aee
RS
1696 ;; Put in forward order
1697 (setq list (nreverse list))))
1698 (while list
1699 (message "Searching subfile %s..." (cdr (car list)))
1700 (Info-read-subfile (car (car list)))
ca53db33
JL
1701 (when backward
1702 ;; Hide Info file header for backward search
1703 (narrow-to-region (save-excursion
1704 (goto-char (point-min))
1705 (search-forward "\n\^_")
1706 (1- (point)))
1707 (point-max))
1708 (goto-char (point-max)))
774f8aee
RS
1709 (setq list (cdr list))
1710 (setq give-up nil found nil)
1711 (while (and (not give-up)
be4b3ae8
JL
1712 (save-match-data
1713 (or (null found)
1714 (if backward
1715 (isearch-range-invisible found beg-found)
1716 (isearch-range-invisible beg-found found))
1717 ;; Skip node header line
180ee7c7
JL
1718 (and (save-excursion (forward-line -1)
1719 (looking-at "\^_"))
866920e3 1720 (forward-line (if backward -1 1)))
be4b3ae8
JL
1721 ;; Skip Tag Table node
1722 (save-excursion
1723 (and (search-backward "\^_" nil t)
1724 (looking-at "\^_\nTag Table"))))))
180ee7c7
JL
1725 (let ((search-spaces-regexp Info-search-whitespace-regexp))
1726 (if (if backward
1727 (re-search-backward regexp nil t)
1728 (re-search-forward regexp nil t))
1729 (setq found (point) beg-found (if backward (match-end 0)
1730 (match-beginning 0)))
1731 (setq give-up t))))
774f8aee
RS
1732 (if give-up
1733 (setq found nil))
c47b5bbe 1734 (if found
774f8aee
RS
1735 (setq list nil)))
1736 (if found
1737 (message "")
1738 (signal 'search-failed (list regexp))))
1739 (if (not found)
1740 (progn (Info-read-subfile osubfile)
1741 (goto-char opoint)
1742 (Info-select-node)
1743 (set-window-start (selected-window) ostart)))))
0b02cda9
JL
1744
1745 (if (and (string= osubfile Info-current-subfile)
1746 (> found opoint-min)
1747 (< found opoint-max))
1748 ;; Search landed in the same node
1749 (goto-char found)
1750 (widen)
1751 (goto-char found)
1752 (save-match-data (Info-select-node)))
1753
760d5cb3
GM
1754 ;; Use string-equal, not equal, to ignore text props.
1755 (or (and (string-equal onode Info-current-node)
1756 (equal ofile Info-current-file))
866920e3
JL
1757 (and isearch-mode isearch-wrapped
1758 (eq opoint (if isearch-forward opoint-min opoint-max)))
760d5cb3
GM
1759 (setq Info-history (cons (list ofile onode opoint)
1760 Info-history))))))
8a7757f6
JL
1761
1762(defun Info-search-case-sensitively ()
1763 "Search for a regexp case-sensitively."
1764 (interactive)
1765 (let ((case-fold-search nil))
1766 (call-interactively 'Info-search)))
1767
1768(defun Info-search-next ()
1769 "Search for next regexp from a previous `Info-search' command."
1770 (interactive)
1771 (let ((case-fold-search Info-search-case-fold))
1772 (if Info-search-history
1773 (Info-search (car Info-search-history))
1774 (call-interactively 'Info-search))))
0b02cda9
JL
1775
1776(defun Info-search-backward (regexp &optional bound noerror count)
1777 "Search for REGEXP in the reverse direction."
1778 (interactive (list (read-string
1779 (if Info-search-history
5b76833f
RF
1780 (format "Regexp search%s backward (default %s): "
1781 (if case-fold-search "" " case-sensitively")
0b02cda9
JL
1782 (car Info-search-history))
1783 (format "Regexp search%s backward: "
5b76833f 1784 (if case-fold-search "" " case-sensitively")))
0b02cda9
JL
1785 nil 'Info-search-history)))
1786 (Info-search regexp bound noerror count 'backward))
1787
1788(defun Info-isearch-search ()
9553cf63 1789 (if Info-isearch-search
98e77be5 1790 (lambda (string &optional bound noerror count)
d457219d
JL
1791 (if isearch-word
1792 (Info-search (concat "\\b" (replace-regexp-in-string
69672704 1793 "\\W+" "\\W+"
d457219d 1794 (replace-regexp-in-string
69672704
JL
1795 "^\\W+\\|\\W+$" "" string)
1796 nil t)
1797 "\\b")
d457219d
JL
1798 bound noerror count
1799 (unless isearch-forward 'backward))
1800 (Info-search (if isearch-regexp string (regexp-quote string))
1801 bound noerror count
1802 (unless isearch-forward 'backward))
1803 (point)))
98e77be5
JL
1804 (let ((isearch-search-fun-function nil))
1805 (isearch-search-fun))))
0b02cda9
JL
1806
1807(defun Info-isearch-wrap ()
d457219d
JL
1808 (if Info-isearch-search
1809 (if Info-isearch-initial-node
1810 (progn
1811 (if isearch-forward (Info-top-node) (Info-final-node))
1812 (goto-char (if isearch-forward (point-min) (point-max))))
1813 (setq Info-isearch-initial-node Info-current-node)
1814 (setq isearch-wrapped nil))
0b02cda9
JL
1815 (goto-char (if isearch-forward (point-min) (point-max)))))
1816
1817(defun Info-isearch-push-state ()
1818 `(lambda (cmd)
1819 (Info-isearch-pop-state cmd ,Info-current-file ,Info-current-node)))
1820
1821(defun Info-isearch-pop-state (cmd file node)
1822 (or (and (string= Info-current-file file)
1823 (string= Info-current-node node))
1824 (progn (Info-find-node file node) (sit-for 0))))
1825
d457219d
JL
1826(defun Info-isearch-start ()
1827 (setq Info-isearch-initial-node nil))
a384cab3 1828\f
a384cab3 1829(defun Info-extract-pointer (name &optional errorname)
fdf4b680
DL
1830 "Extract the value of the node-pointer named NAME.
1831If there is none, use ERRORNAME in the error message;
c60c12be
SM
1832if ERRORNAME is nil, just return nil."
1833 ;; Bind this in case the user sets it to nil.
1bcedb3b
RS
1834 (let ((case-fold-search t))
1835 (save-excursion
c60c12be
SM
1836 (goto-char (point-min))
1837 (let ((bound (point)))
1838 (forward-line 1)
1839 (cond ((re-search-backward
1840 (concat name ":" (Info-following-node-name-re)) bound t)
1841 (match-string 1))
1842 ((not (eq errorname t))
1843 (error "Node has no %s"
1844 (capitalize (or errorname name)))))))))
1845
1846(defun Info-following-node-name-re (&optional allowedchars)
1847 "Return a regexp matching a node name.
fdf4b680 1848ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
c60c12be
SM
1849saying which chars may appear in the node name.
1850Submatch 1 is the complete node name.
1851Submatch 2 if non-nil is the parenthesized file name part of the node name.
1852Submatch 3 is the local part of the node name.
1853End of submatch 0, 1, and 3 are the same, so you can safely concat."
1854 (concat "[ \t]*" ;Skip leading space.
1855 "\\(\\(([^)]+)\\)?" ;Node name can start with a file name.
1856 "\\([" (or allowedchars "^,\t\n") "]*" ;Any number of allowed chars.
1857 "[" (or allowedchars "^,\t\n") " ]" ;The last char can't be a space.
1858 "\\|\\)\\)")) ;Allow empty node names.
a384cab3 1859
718e6bc7
RS
1860;;; For compatibility; other files have used this name.
1861(defun Info-following-node-name ()
1862 (and (looking-at (Info-following-node-name-re))
1863 (match-string 1)))
1864
a384cab3
JB
1865(defun Info-next ()
1866 "Go to the next node of this node."
1867 (interactive)
222a6c9b
CY
1868 ;; In case another window is currently selected
1869 (save-window-excursion
1870 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1871 (Info-goto-node (Info-extract-pointer "next"))))
a384cab3
JB
1872
1873(defun Info-prev ()
1874 "Go to the previous node of this node."
1875 (interactive)
222a6c9b
CY
1876 ;; In case another window is currently selected
1877 (save-window-excursion
1878 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1879 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous"))))
a384cab3 1880
5dab2fb4
RS
1881(defun Info-up (&optional same-file)
1882 "Go to the superior node of this node.
1883If SAME-FILE is non-nil, do not move to a different Info file."
a384cab3 1884 (interactive)
222a6c9b
CY
1885 ;; In case another window is currently selected
1886 (save-window-excursion
1887 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1888 (let ((old-node Info-current-node)
1889 (old-file Info-current-file)
1890 (node (Info-extract-pointer "up")) p)
d590dbd2 1891 (and same-file
222a6c9b
CY
1892 (string-match "^(" node)
1893 (error "Up node is in another Info file"))
1894 (Info-goto-node node)
1895 (setq p (point))
1896 (goto-char (point-min))
1897 (if (and (search-forward "\n* Menu:" nil t)
1898 (re-search-forward
1899 (if (string-equal old-node "Top")
1900 (concat "\n\\*[^:]+: +(" (file-name-nondirectory old-file) ")")
1901 (concat "\n\\* +\\(" (regexp-quote old-node)
1902 ":\\|[^:]+: +" (regexp-quote old-node) "\\)"))
1903 nil t))
1904 (progn (beginning-of-line) (if (looking-at "^\\* ") (forward-char 2)))
1905 (goto-char p)
1906 (Info-restore-point Info-history)))))
a384cab3 1907
424b9d3e
JL
1908(defun Info-history-back ()
1909 "Go back in the history to the last node visited."
a384cab3
JB
1910 (interactive)
1911 (or Info-history
1912 (error "This is the first Info node you looked at"))
424b9d3e
JL
1913 (let ((history-forward
1914 (cons (list Info-current-file Info-current-node (point))
1915 Info-history-forward))
1916 filename nodename opoint)
a384cab3
JB
1917 (setq filename (car (car Info-history)))
1918 (setq nodename (car (cdr (car Info-history))))
1919 (setq opoint (car (cdr (cdr (car Info-history)))))
1920 (setq Info-history (cdr Info-history))
1921 (Info-find-node filename nodename)
1922 (setq Info-history (cdr Info-history))
424b9d3e
JL
1923 (setq Info-history-forward history-forward)
1924 (goto-char opoint)))
1925
1926(defalias 'Info-last 'Info-history-back)
1927
1928(defun Info-history-forward ()
1929 "Go forward in the history of visited nodes."
1930 (interactive)
1931 (or Info-history-forward
1932 (error "This is the last Info node you looked at"))
1933 (let ((history-forward (cdr Info-history-forward))
1934 filename nodename opoint)
1935 (setq filename (car (car Info-history-forward)))
1936 (setq nodename (car (cdr (car Info-history-forward))))
1937 (setq opoint (car (cdr (cdr (car Info-history-forward)))))
1938 (Info-find-node filename nodename)
1939 (setq Info-history-forward history-forward)
a384cab3
JB
1940 (goto-char opoint)))
1941
48e5b471 1942;;;###autoload
a384cab3
JB
1943(defun Info-directory ()
1944 "Go to the Info directory node."
1945 (interactive)
1946 (Info-find-node "dir" "top"))
1947\f
8a7757f6 1948(defun Info-history ()
89b064a9 1949 "Go to a node with a menu of visited nodes."
8a7757f6
JL
1950 (interactive)
1951 (let ((curr-file Info-current-file)
1952 (curr-node Info-current-node)
1953 p)
89b064a9
JL
1954 (with-current-buffer (get-buffer-create " *info-history*")
1955 (let ((inhibit-read-only t))
1956 (erase-buffer)
1957 (goto-char (point-min))
ea99d5c8 1958 (insert "\n\^_\nFile: history, Node: Top, Up: (dir)\n\n")
89b064a9
JL
1959 (insert "Recently Visited Nodes\n**********************\n\n")
1960 (insert "* Menu:\n\n")
1961 (let ((hl (delete '("history" "Top") Info-history-list)))
1962 (while hl
1963 (let ((file (nth 0 (car hl)))
1964 (node (nth 1 (car hl))))
1965 (if (and (string-equal file curr-file)
1966 (string-equal node curr-node))
1967 (setq p (point)))
99315976
JL
1968 (insert "* " node ": ("
1969 (propertize (or (file-name-directory file) "") 'invisible t)
1970 (file-name-nondirectory file)
89b064a9
JL
1971 ")" node ".\n"))
1972 (setq hl (cdr hl))))))
1973 (Info-find-node "history" "Top")
8a7757f6
JL
1974 (goto-char (or p (point-min)))))
1975
8a7757f6 1976(defun Info-toc ()
bc224645
JL
1977 "Go to a node with table of contents of the current Info file.
1978Table of contents is created from the tree structure of menus."
8a7757f6 1979 (interactive)
ea99d5c8
JL
1980 (let ((curr-file (substring-no-properties Info-current-file))
1981 (curr-node (substring-no-properties Info-current-node))
8a7757f6 1982 p)
89b064a9
JL
1983 (with-current-buffer (get-buffer-create " *info-toc*")
1984 (let ((inhibit-read-only t)
1985 (node-list (Info-build-toc curr-file)))
1986 (erase-buffer)
1987 (goto-char (point-min))
ea99d5c8 1988 (insert "\n\^_\nFile: toc, Node: Top, Up: (dir)\n\n")
89b064a9 1989 (insert "Table of Contents\n*****************\n\n")
ea99d5c8 1990 (insert "*Note Top: (" curr-file ")Top.\n")
89b064a9
JL
1991 (Info-insert-toc
1992 (nth 2 (assoc "Top" node-list)) ; get Top nodes
ea99d5c8 1993 node-list 0 curr-file))
89b064a9
JL
1994 (if (not (bobp))
1995 (let ((Info-hide-note-references 'hide)
1996 (Info-fontify-visited-nodes nil))
50163b00 1997 (Info-mode)
141b49f5 1998 (setq Info-current-file "toc" Info-current-node "Top")
ea99d5c8
JL
1999 (goto-char (point-min))
2000 (narrow-to-region (or (re-search-forward "\n[\^_\f]\n" nil t)
2001 (point-min))
2002 (point-max))
2003 (Info-fontify-node)
2004 (widen)))
89b064a9
JL
2005 (goto-char (point-min))
2006 (if (setq p (search-forward (concat "*Note " curr-node ":") nil t))
2007 (setq p (- p (length curr-node) 2))))
2008 (Info-find-node "toc" "Top")
8a7757f6
JL
2009 (goto-char (or p (point-min)))))
2010
89b064a9 2011(defun Info-insert-toc (nodes node-list level curr-file)
8a7757f6
JL
2012 "Insert table of contents with references to nodes."
2013 (let ((section "Top"))
2014 (while nodes
2015 (let ((node (assoc (car nodes) node-list)))
2016 (unless (member (nth 1 node) (list nil section))
2017 (insert (setq section (nth 1 node)) "\n"))
2018 (insert (make-string level ?\t))
89b064a9
JL
2019 (insert "*Note " (car nodes) ": (" curr-file ")" (car nodes) ".\n")
2020 (Info-insert-toc (nth 2 node) node-list (1+ level) curr-file)
8a7757f6
JL
2021 (setq nodes (cdr nodes))))))
2022
2023(defun Info-build-toc (file)
2024 "Build table of contents from menus of Info FILE and its subfiles."
8a7757f6 2025 (with-temp-buffer
ea99d5c8
JL
2026 (let* ((file (and (stringp file) (Info-find-file file)))
2027 (default-directory (or (and (stringp file)
2028 (file-name-directory file))
141b49f5 2029 default-directory))
ea99d5c8 2030 (main-file (and (stringp file) file))
141b49f5
JL
2031 (sections '(("Top" "Top")))
2032 nodes subfiles)
2033 (while (or main-file subfiles)
2034 (or main-file (message "Searching subfile %s..." (car subfiles)))
8a7757f6 2035 (erase-buffer)
141b49f5
JL
2036 (info-insert-file-contents (or main-file (car subfiles)))
2037 (goto-char (point-min))
8a7757f6
JL
2038 (while (and (search-forward "\n\^_\nFile:" nil 'move)
2039 (search-forward "Node: " nil 'move))
2040 (let ((nodename (substring-no-properties (Info-following-node-name)))
2041 (bound (- (or (save-excursion (search-forward "\n\^_" nil t))
2042 (point-max)) 2))
2043 (section "Top")
2044 menu-items)
141b49f5 2045 (when (and (not (Info-index-node nodename file))
8a7757f6
JL
2046 (re-search-forward "^\\* Menu:" bound t))
2047 (forward-line 1)
2048 (beginning-of-line)
2049 (setq bound (or (and (equal nodename "Top")
2050 (save-excursion
2051 (re-search-forward
2052 "^[ \t-]*The Detailed Node Listing" nil t)))
2053 bound))
2054 (while (< (point) bound)
2055 (cond
2056 ;; Menu item line
2057 ((looking-at "^\\* +[^:]+:")
2058 (beginning-of-line)
2059 (forward-char 2)
2060 (let ((menu-node-name (substring-no-properties
2061 (Info-extract-menu-node-name))))
2062 (setq menu-items (cons menu-node-name menu-items))
2063 (if (equal nodename "Top")
2064 (setq sections
2065 (cons (list menu-node-name section) sections)))))
2066 ;; Other non-empty strings in the Top node are section names
2067 ((and (equal nodename "Top")
2068 (looking-at "^\\([^ \t\n*=.-][^:\n]*\\)"))
2069 (setq section (match-string-no-properties 1))))
2070 (forward-line 1)
2071 (beginning-of-line)))
2072 (setq nodes (cons (list nodename
2073 (cadr (assoc nodename sections))
2074 (nreverse menu-items))
2075 nodes))
2076 (goto-char bound)))
141b49f5 2077 (if main-file
8a7757f6
JL
2078 (save-excursion
2079 (goto-char (point-min))
2080 (if (search-forward "\n\^_\nIndirect:" nil t)
2081 (let ((bound (save-excursion (search-forward "\n\^_" nil t))))
2082 (while (re-search-forward "^\\(.*\\): [0-9]+$" bound t)
2083 (setq subfiles (cons (match-string-no-properties 1)
2084 subfiles)))))
2085 (setq subfiles (nreverse subfiles)
141b49f5 2086 main-file nil))
8a7757f6
JL
2087 (setq subfiles (cdr subfiles))))
2088 (message "")
2089 (nreverse nodes))))
2090\f
2091(defun Info-follow-reference (footnotename &optional fork)
fdf4b680 2092 "Follow cross reference named FOOTNOTENAME to the node it refers to.
8a7757f6
JL
2093FOOTNOTENAME may be an abbreviation of the reference name.
2094If FORK is non-nil (interactively with a prefix arg), show the node in
185cff95 2095a new Info buffer. If FORK is a string, it is the name to use for the
8a7757f6 2096new buffer."
a384cab3
JB
2097 (interactive
2098 (let ((completion-ignore-case t)
1bcedb3b 2099 (case-fold-search t)
67bc89ab 2100 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 2101 (save-excursion
67bc89ab
RS
2102 ;; Store end and beginning of line.
2103 (end-of-line)
2104 (setq eol (point))
2105 (beginning-of-line)
2106 (setq bol (point))
2107
a384cab3 2108 (goto-char (point-min))
8a7757f6 2109 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 2110 (setq str (match-string-no-properties 1))
a384cab3
JB
2111 ;; See if this one should be the default.
2112 (and (null default)
1f179e27 2113 (<= (match-beginning 0) start-point)
a384cab3
JB
2114 (<= start-point (point))
2115 (setq default t))
67bc89ab
RS
2116 ;; See if this one should be the alternate default.
2117 (and (null alt-default)
2118 (and (<= bol (match-beginning 0))
2119 (<= (point) eol))
2120 (setq alt-default t))
a384cab3
JB
2121 (setq i 0)
2122 (while (setq i (string-match "[ \n\t]+" str i))
2123 (setq str (concat (substring str 0 i) " "
2124 (substring str (match-end 0))))
2125 (setq i (1+ i)))
2126 ;; Record as a completion and perhaps as default.
2127 (if (eq default t) (setq default str))
67bc89ab 2128 (if (eq alt-default t) (setq alt-default str))
ec6d29af 2129 ;; Don't add this string if it's a duplicate.
c60c12be
SM
2130 (or (assoc-string str completions t)
2131 (push str completions))))
67bc89ab
RS
2132 ;; If no good default was found, try an alternate.
2133 (or default
2134 (setq default alt-default))
2135 ;; If only one cross-reference found, then make it default.
2136 (if (eq (length completions) 1)
c60c12be 2137 (setq default (car completions)))
a384cab3 2138 (if completions
b0ebdfe5 2139 (let ((input (completing-read (if default
968b7671 2140 (concat
5b76833f
RF
2141 "Follow reference named (default "
2142 default "): ")
b0ebdfe5
RS
2143 "Follow reference named: ")
2144 completions nil t)))
2145 (list (if (equal input "")
8a7757f6 2146 default input) current-prefix-arg))
a384cab3 2147 (error "No cross-references in this node"))))
ebf8f7e1
RS
2148
2149 (unless footnotename
2150 (error "No reference was specified"))
2151
dde7f979 2152 (let (target i (str (concat "\\*note " (regexp-quote footnotename)))
1bcedb3b 2153 (case-fold-search t))
a384cab3
JB
2154 (while (setq i (string-match " " str i))
2155 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
2156 (setq i (+ i 6)))
2157 (save-excursion
8a7757f6
JL
2158 ;; Move point to the beginning of reference if point is on reference
2159 (or (looking-at "\\*note[ \n\t]+")
2160 (and (looking-back "\\*note[ \n\t]+")
2161 (goto-char (match-beginning 0)))
2162 (if (and (save-excursion
2163 (goto-char (+ (point) 5)) ; skip a possible *note
2164 (re-search-backward "\\*note[ \n\t]+" nil t)
836bb478 2165 (looking-at str))
8a7757f6
JL
2166 (<= (point) (match-end 0)))
2167 (goto-char (match-beginning 0))))
2168 ;; Go to the reference closest to point
2169 (let ((next-ref (save-excursion (and (re-search-forward str nil t)
2170 (+ (match-beginning 0) 5))))
2171 (prev-ref (save-excursion (and (re-search-backward str nil t)
2172 (+ (match-beginning 0) 5)))))
2173 (goto-char (cond ((and next-ref prev-ref)
2174 (if (< (abs (- next-ref (point)))
2175 (abs (- prev-ref (point))))
2176 next-ref prev-ref))
2177 ((or next-ref prev-ref))
2178 ((error "No cross-reference named %s" footnotename))))
2179 (setq target (Info-extract-menu-node-name t))))
a384cab3
JB
2180 (while (setq i (string-match "[ \t\n]+" target i))
2181 (setq target (concat (substring target 0 i) " "
2182 (substring target (match-end 0))))
2183 (setq i (+ i 1)))
8a7757f6 2184 (Info-goto-node target fork)))
a384cab3 2185
e057da52
SM
2186(defconst Info-menu-entry-name-re "\\(?:[^:]\\|:[^:,.;() \t\n]\\)*"
2187 ;; We allow newline because this is also used in Info-follow-reference,
2188 ;; where the xref name might be wrapped over two lines.
c60c12be
SM
2189 "Regexp that matches a menu entry name upto but not including the colon.
2190Because of ambiguities, this should be concatenated with something like
2191`:' and `Info-following-node-name-re'.")
2192
836bb478 2193(defun Info-extract-menu-node-name (&optional multi-line index-node)
a384cab3 2194 (skip-chars-forward " \t\n")
c60c12be
SM
2195 (when (looking-at (concat Info-menu-entry-name-re ":\\(:\\|"
2196 (Info-following-node-name-re
836bb478
JL
2197 (cond
2198 (index-node "^,\t\n")
2199 (multi-line "^.,\t")
2200 (t "^.,\t\n")))
2201 "\\)"
2202 (if index-node
2203 "\\.\\(?:[ \t\n]+(line +\\([0-9]+\\))\\)?"
2204 "")))
2205 (if index-node
2206 (setq Info-point-loc
2207 (if (match-beginning 5)
2208 (string-to-number (match-string 5))
2209 (buffer-substring (match-beginning 0) (1- (match-beginning 1)))))
02c9f5cd 2210;;; Uncomment next line to use names of cross-references in non-index nodes:
836bb478
JL
2211;;; (setq Info-point-loc
2212;;; (buffer-substring (match-beginning 0) (1- (match-beginning 1))))
2213 )
c60c12be
SM
2214 (replace-regexp-in-string
2215 "[ \n]+" " "
2216 (or (match-string 2)
2217 ;; If the node name is the menu entry name (using `entry::').
2218 (buffer-substring (match-beginning 0) (1- (match-beginning 1)))))))
a384cab3 2219
e8adde3f 2220;; No one calls this.
9e5c2f50
RS
2221;;(defun Info-menu-item-sequence (list)
2222;; (while list
e8adde3f 2223;; (Info-menu (car list))
9e5c2f50 2224;; (setq list (cdr list))))
a384cab3 2225
6356e646 2226(defvar Info-complete-menu-buffer)
e4e8ee63 2227(defvar Info-complete-next-re nil)
141b49f5 2228(defvar Info-complete-nodes nil)
e4e8ee63 2229(defvar Info-complete-cache nil)
6356e646 2230
c60c12be
SM
2231(defconst Info-node-spec-re
2232 (concat (Info-following-node-name-re "^.,:") "[,:.]")
a1637357
SM
2233 "Regexp to match the text after a : until the terminating `.'.")
2234
e8adde3f 2235(defun Info-complete-menu-item (string predicate action)
2d12f5c2
SM
2236 ;; This uses two dynamically bound variables:
2237 ;; - `Info-complete-menu-buffer' which contains the buffer in which
2238 ;; is the menu of items we're trying to complete.
2239 ;; - `Info-complete-next-re' which, if non-nil, indicates that we should
2240 ;; also look for menu items in subsequent nodes as long as those
2241 ;; nodes' names match `Info-complete-next-re'. This feature is currently
141b49f5
JL
2242 ;; not used.
2243 ;; - `Info-complete-nodes' which, if non-nil, indicates that we should
2244 ;; also look for menu items in these nodes. This feature is currently
2d12f5c2 2245 ;; only used for completion in Info-index.
0defe758
LK
2246
2247 ;; Note that `Info-complete-menu-buffer' could be current already,
2248 ;; so we want to save point.
2249 (save-excursion
2250 (set-buffer Info-complete-menu-buffer)
e4e8ee63
SM
2251 (let ((completion-ignore-case t)
2252 (case-fold-search t)
2253 (orignode Info-current-node)
2254 nextnode)
2255 (goto-char (point-min))
2256 (search-forward "\n* Menu:")
2257 (if (not (memq action '(nil t)))
2258 (re-search-forward
2259 (concat "\n\\* +" (regexp-quote string) ":") nil t)
2260 (let ((pattern (concat "\n\\* +\\("
2261 (regexp-quote string)
c60c12be 2262 Info-menu-entry-name-re "\\):" Info-node-spec-re))
e4e8ee63
SM
2263 completions)
2264 ;; Check the cache.
2265 (if (and (equal (nth 0 Info-complete-cache) Info-current-file)
2266 (equal (nth 1 Info-complete-cache) Info-current-node)
2267 (equal (nth 2 Info-complete-cache) Info-complete-next-re)
141b49f5 2268 (equal (nth 5 Info-complete-cache) Info-complete-nodes)
e4e8ee63
SM
2269 (let ((prev (nth 3 Info-complete-cache)))
2270 (eq t (compare-strings string 0 (length prev)
2271 prev 0 nil t))))
2272 ;; We can reuse the previous list.
2273 (setq completions (nth 4 Info-complete-cache))
2274 ;; The cache can't be used.
2275 (while
2276 (progn
2277 (while (re-search-forward pattern nil t)
365d2503 2278 (push (match-string-no-properties 1)
e4e8ee63
SM
2279 completions))
2280 ;; Check subsequent nodes if applicable.
141b49f5
JL
2281 (or (and Info-complete-next-re
2282 (setq nextnode (Info-extract-pointer "next" t))
2283 (string-match Info-complete-next-re nextnode))
2284 (and Info-complete-nodes
2285 (setq Info-complete-nodes (cdr Info-complete-nodes)
2286 nextnode (car Info-complete-nodes)))))
e4e8ee63
SM
2287 (Info-goto-node nextnode))
2288 ;; Go back to the start node (for the next completion).
2289 (unless (equal Info-current-node orignode)
2290 (Info-goto-node orignode))
2291 ;; Update the cache.
271f4a9e
MB
2292 (set (make-local-variable 'Info-complete-cache)
2293 (list Info-current-file Info-current-node
141b49f5
JL
2294 Info-complete-next-re string completions
2295 Info-complete-nodes)))
e4e8ee63
SM
2296 (if action
2297 (all-completions string completions predicate)
2298 (try-completion string completions predicate)))))))
e8adde3f
RS
2299
2300
4fceda3c 2301(defun Info-menu (menu-item &optional fork)
dc414be1
EZ
2302 "Go to the node pointed to by the menu item named (or abbreviated) MENU-ITEM.
2303The menu item should one of those listed in the current node's menu.
2304Completion is allowed, and the default menu item is the one point is on.
7210b6f5 2305If FORK is non-nil (interactively with a prefix arg), show the node in
185cff95 2306a new Info buffer. If FORK is a string, it is the name to use for the
7210b6f5 2307new buffer."
a384cab3
JB
2308 (interactive
2309 (let ((completions '())
2310 ;; If point is within a menu item, use that item as the default
2311 (default nil)
2312 (p (point))
211d6309 2313 beg
760d5cb3
GM
2314 (last nil)
2315 (case-fold-search t))
a384cab3
JB
2316 (save-excursion
2317 (goto-char (point-min))
2318 (if (not (search-forward "\n* menu:" nil t))
2319 (error "No menu in this node"))
e8adde3f
RS
2320 (setq beg (point))
2321 (and (< (point) p)
2322 (save-excursion
2323 (goto-char p)
2324 (end-of-line)
00d32b3f
SM
2325 (if (re-search-backward (concat "\n\\* +\\("
2326 Info-menu-entry-name-re
2327 "\\):") beg t)
110d4e80 2328 (setq default (match-string-no-properties 1))))))
a384cab3
JB
2329 (let ((item nil))
2330 (while (null item)
e8adde3f
RS
2331 (setq item (let ((completion-ignore-case t)
2332 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
2333 (completing-read (if default
2334 (format "Menu item (default %s): "
2335 default)
760d5cb3 2336 "Menu item: ")
e8adde3f 2337 'Info-complete-menu-item nil t)))
aea2a8da
JB
2338 ;; we rely on the fact that completing-read accepts an input
2339 ;; of "" even when the require-match argument is true and ""
2340 ;; is not a valid possibility
a384cab3
JB
2341 (if (string= item "")
2342 (if default
2343 (setq item default)
760d5cb3
GM
2344 ;; ask again
2345 (setq item nil))))
4fceda3c 2346 (list item current-prefix-arg))))
a384cab3
JB
2347 ;; there is a problem here in that if several menu items have the same
2348 ;; name you can only go to the node of the first with this command.
4fceda3c 2349 (Info-goto-node (Info-extract-menu-item menu-item) (if fork menu-item)))
399c88ad 2350
a384cab3
JB
2351(defun Info-extract-menu-item (menu-item)
2352 (setq menu-item (regexp-quote menu-item))
1bcedb3b
RS
2353 (let ((case-fold-search t))
2354 (save-excursion
760d5cb3
GM
2355 (let ((case-fold-search t))
2356 (goto-char (point-min))
2357 (or (search-forward "\n* menu:" nil t)
2358 (error "No menu in this node"))
2359 (or (re-search-forward (concat "\n\\* +" menu-item ":") nil t)
2360 (re-search-forward (concat "\n\\* +" menu-item) nil t)
2361 (error "No such item in menu"))
2362 (beginning-of-line)
2363 (forward-char 2)
141b49f5 2364 (Info-extract-menu-node-name nil (Info-index-node))))))
a384cab3
JB
2365
2366;; If COUNT is nil, use the last item in the menu.
2367(defun Info-extract-menu-counting (count)
1bcedb3b
RS
2368 (let ((case-fold-search t))
2369 (save-excursion
760d5cb3
GM
2370 (let ((case-fold-search t))
2371 (goto-char (point-min))
2372 (or (search-forward "\n* menu:" nil t)
2373 (error "No menu in this node"))
2374 (if count
2375 (or (search-forward "\n* " nil t count)
2376 (error "Too few items in menu"))
2377 (while (search-forward "\n* " nil t)
2378 nil))
141b49f5 2379 (Info-extract-menu-node-name nil (Info-index-node))))))
a384cab3 2380
e38e7367
RM
2381(defun Info-nth-menu-item ()
2382 "Go to the node of the Nth menu item.
2383N is the digit argument used to invoke this command."
a384cab3 2384 (interactive)
e38e7367
RM
2385 (Info-goto-node
2386 (Info-extract-menu-counting
2387 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
2388
2389(defun Info-top-node ()
2390 "Go to the Top node of this file."
2391 (interactive)
2392 (Info-goto-node "Top"))
2393
2394(defun Info-final-node ()
2395 "Go to the final node in this file."
2396 (interactive)
2397 (Info-goto-node "Top")
760d5cb3
GM
2398 (let ((Info-history nil)
2399 (case-fold-search t))
a384cab3
JB
2400 ;; Go to the last node in the menu of Top.
2401 (Info-goto-node (Info-extract-menu-counting nil))
2402 ;; If the last node in the menu is not last in pointer structure,
399c88ad 2403 ;; move forward until we can't go any farther.
a384cab3
JB
2404 (while (Info-forward-node t t) nil)
2405 ;; Then keep moving down to last subnode, unless we reach an index.
141b49f5 2406 (while (and (not (Info-index-node))
a384cab3
JB
2407 (save-excursion (search-forward "\n* Menu:" nil t)))
2408 (Info-goto-node (Info-extract-menu-counting nil)))))
2409
2410(defun Info-forward-node (&optional not-down no-error)
2411 "Go forward one node, considering all nodes as forming one sequence."
2412 (interactive)
2413 (goto-char (point-min))
2414 (forward-line 1)
760d5cb3
GM
2415 (let ((case-fold-search t))
2416 ;; three possibilities, in order of priority:
2417 ;; 1. next node is in a menu in this node (but not in an index)
2418 ;; 2. next node is next at same level
2419 ;; 3. next node is up and next
2420 (cond ((and (not not-down)
2421 (save-excursion (search-forward "\n* menu:" nil t))
141b49f5 2422 (not (Info-index-node)))
760d5cb3
GM
2423 (Info-goto-node (Info-extract-menu-counting 1))
2424 t)
6af7040d 2425 ((save-excursion (search-backward "next:" nil t))
760d5cb3
GM
2426 (Info-next)
2427 t)
6af7040d 2428 ((and (save-excursion (search-backward "up:" nil t))
760d5cb3
GM
2429 ;; Use string-equal, not equal, to ignore text props.
2430 (not (string-equal (downcase (Info-extract-pointer "up"))
2431 "top")))
2432 (let ((old-node Info-current-node))
2433 (Info-up)
2434 (let (Info-history success)
2435 (unwind-protect
2436 (setq success (Info-forward-node t no-error))
2437 (or success (Info-goto-node old-node))))))
2438 (no-error nil)
2439 (t (error "No pointer forward from this node")))))
a384cab3
JB
2440
2441(defun Info-backward-node ()
2442 "Go backward one node, considering all nodes as forming one sequence."
2443 (interactive)
2444 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
760d5cb3
GM
2445 (upnode (Info-extract-pointer "up" t))
2446 (case-fold-search t))
a384cab3
JB
2447 (cond ((and upnode (string-match "(" upnode))
2448 (error "First node in file"))
2449 ((and upnode (or (null prevnode)
e90d1271
RS
2450 ;; Use string-equal, not equal,
2451 ;; to ignore text properties.
2452 (string-equal (downcase prevnode)
2453 (downcase upnode))))
a384cab3
JB
2454 (Info-up))
2455 (prevnode
2456 ;; If we move back at the same level,
2457 ;; go down to find the last subnode*.
2458 (Info-prev)
2459 (let (Info-history)
141b49f5 2460 (while (and (not (Info-index-node))
a384cab3
JB
2461 (save-excursion (search-forward "\n* Menu:" nil t)))
2462 (Info-goto-node (Info-extract-menu-counting nil)))))
2463 (t
2464 (error "No pointer backward from this node")))))
2465
2466(defun Info-exit ()
2467 "Exit Info by selecting some other buffer."
2468 (interactive)
552775bd
RS
2469 (if Info-standalone
2470 (save-buffers-kill-emacs)
4643e92f 2471 (quit-window)))
a384cab3 2472
253db917 2473(defun Info-next-menu-item ()
3a8803c0 2474 "Go to the node of the next menu item."
253db917 2475 (interactive)
fdf4b680 2476 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2477 (let* ((case-fold-search t)
2478 (node
2479 (save-excursion
2480 (forward-line -1)
2481 (search-forward "\n* menu:" nil t)
2482 (and (search-forward "\n* " nil t)
2483 (Info-extract-menu-node-name)))))
fadfb77f
RS
2484 (if node (Info-goto-node node)
2485 (error "No more items in menu"))))
253db917
ER
2486
2487(defun Info-last-menu-item ()
3a8803c0 2488 "Go to the node of the previous menu item."
253db917
ER
2489 (interactive)
2490 (save-excursion
2491 (forward-line 1)
fdf4b680 2492 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2493 (let* ((case-fold-search t)
2494 (beg (save-excursion
2495 (and (search-backward "\n* menu:" nil t)
2496 (point)))))
0a56332b
RS
2497 (or (and beg (search-backward "\n* " beg t))
2498 (error "No previous items in menu")))
2499 (Info-goto-node (save-excursion
2500 (goto-char (match-end 0))
2501 (Info-extract-menu-node-name)))))
253db917 2502
552775bd 2503(defmacro Info-no-error (&rest body)
253db917
ER
2504 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
2505
2506(defun Info-next-preorder ()
3c9179ea
RS
2507 "Go to the next subnode or the next node, or go up a level."
2508 (interactive)
2509 (cond ((Info-no-error (Info-next-menu-item)))
2510 ((Info-no-error (Info-next)))
5dab2fb4 2511 ((Info-no-error (Info-up t))
ed690657
KH
2512 ;; Since we have already gone thru all the items in this menu,
2513 ;; go up to the end of this node.
b54d0f0e
RS
2514 (goto-char (point-max))
2515 ;; Since logically we are done with the node with that menu,
2516 ;; move on from it.
2517 (Info-next-preorder))
3c9179ea
RS
2518 (t
2519 (error "No more nodes"))))
253db917
ER
2520
2521(defun Info-last-preorder ()
2522 "Go to the last node, popping up a level if there is none."
2523 (interactive)
0a56332b
RS
2524 (cond ((Info-no-error
2525 (Info-last-menu-item)
2526 ;; If we go down a menu item, go to the end of the node
2527 ;; so we can scroll back through it.
ed690657 2528 (goto-char (point-max)))
b54d0f0e
RS
2529 ;; Keep going down, as long as there are nested menu nodes.
2530 (while (Info-no-error
2531 (Info-last-menu-item)
2532 ;; If we go down a menu item, go to the end of the node
2533 ;; so we can scroll back through it.
2534 (goto-char (point-max))))
ed690657 2535 (recenter -1))
c2def7a0
MB
2536 ((and (Info-no-error (Info-extract-pointer "prev"))
2537 (not (equal (Info-extract-pointer "up")
5dab2fb4
RS
2538 (Info-extract-pointer "prev"))))
2539 (Info-no-error (Info-prev))
ed690657 2540 (goto-char (point-max))
b54d0f0e
RS
2541 (while (Info-no-error
2542 (Info-last-menu-item)
2543 ;; If we go down a menu item, go to the end of the node
2544 ;; so we can scroll back through it.
2545 (goto-char (point-max))))
ed690657 2546 (recenter -1))
5dab2fb4 2547 ((Info-no-error (Info-up t))
ed690657 2548 (goto-char (point-min))
760d5cb3
GM
2549 (let ((case-fold-search t))
2550 (or (search-forward "\n* Menu:" nil t)
2551 (goto-char (point-max)))))
ed690657 2552 (t (error "No previous nodes"))))
253db917
ER
2553
2554(defun Info-scroll-up ()
3f32dc86 2555 "Scroll one screenful forward in Info, considering all nodes as one sequence.
280d11ed 2556Once you scroll far enough in a node that its menu appears on the screen
7a53d8c8
EZ
2557but after point, the next scroll moves into its first subnode, unless
2558`Info-scroll-prefer-subnodes' is nil.
280d11ed 2559
7a53d8c8
EZ
2560When you scroll past the end of a node, that goes to the next node if
2561`Info-scroll-prefer-subnodes' is non-nil and to the first subnode otherwise;
2562if this node has no successor, it moves to the parent node's successor,
2563and so on. If `Info-scroll-prefer-subnodes' is non-nil and point is inside
2564the menu of a node, it moves to subnode indicated by the following menu
2565item. (That case won't normally result from this command, but can happen
2566in other ways.)"
280d11ed 2567
253db917 2568 (interactive)
0a56332b
RS
2569 (if (or (< (window-start) (point-min))
2570 (> (window-start) (point-max)))
2571 (set-window-start (selected-window) (point)))
760d5cb3
GM
2572 (let* ((case-fold-search t)
2573 (virtual-end (save-excursion
2574 (goto-char (point-min))
7a53d8c8
EZ
2575 (if (and Info-scroll-prefer-subnodes
2576 (search-forward "\n* Menu:" nil t))
760d5cb3
GM
2577 (point)
2578 (point-max)))))
0a56332b
RS
2579 (if (or (< virtual-end (window-start))
2580 (pos-visible-in-window-p virtual-end))
7a53d8c8
EZ
2581 (cond
2582 (Info-scroll-prefer-subnodes (Info-next-preorder))
2583 ((Info-no-error (Info-goto-node (Info-extract-menu-counting 1))))
2584 (t (Info-next-preorder)))
0a56332b 2585 (scroll-up))))
253db917
ER
2586
2587(defun Info-scroll-down ()
3f32dc86 2588 "Scroll one screenful back in Info, considering all nodes as one sequence.
c2def7a0
MB
2589If point is within the menu of a node, and `Info-scroll-prefer-subnodes'
2590is non-nil, this goes to its last subnode. When you scroll past the
2591beginning of a node, that goes to the previous node or back up to the
2592parent node."
253db917 2593 (interactive)
0a56332b
RS
2594 (if (or (< (window-start) (point-min))
2595 (> (window-start) (point-max)))
2596 (set-window-start (selected-window) (point)))
760d5cb3
GM
2597 (let* ((case-fold-search t)
2598 (current-point (point))
c2def7a0
MB
2599 (virtual-end
2600 (and Info-scroll-prefer-subnodes
2601 (save-excursion
2602 (beginning-of-line)
2603 (setq current-point (point))
2604 (goto-char (point-min))
2605 (search-forward "\n* Menu:"
2606 current-point
2607 t)))))
3a8803c0 2608 (if (or virtual-end
7afe24e3 2609 (pos-visible-in-window-p (point-min) nil t))
0a56332b
RS
2610 (Info-last-preorder)
2611 (scroll-down))))
253db917 2612
56cda6f5 2613(defun Info-next-reference (&optional recur)
552775bd
RS
2614 "Move cursor to the next cross-reference or menu item in the node."
2615 (interactive)
8a7757f6 2616 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tp://")
1bcedb3b
RS
2617 (old-pt (point))
2618 (case-fold-search t))
552775bd
RS
2619 (or (eobp) (forward-char 1))
2620 (or (re-search-forward pat nil t)
2621 (progn
2622 (goto-char (point-min))
2623 (or (re-search-forward pat nil t)
2624 (progn
2625 (goto-char old-pt)
2626 (error "No cross references in this node")))))
8a7757f6 2627 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2628 (if (looking-at "\\* Menu:")
56cda6f5
RS
2629 (if recur
2630 (error "No cross references in this node")
185cff95
JL
2631 (Info-next-reference t))
2632 (if (looking-at "^\\* ")
2633 (forward-char 2)))))
552775bd 2634
56cda6f5 2635(defun Info-prev-reference (&optional recur)
552775bd
RS
2636 "Move cursor to the previous cross-reference or menu item in the node."
2637 (interactive)
8a7757f6 2638 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tp://")
1bcedb3b
RS
2639 (old-pt (point))
2640 (case-fold-search t))
552775bd
RS
2641 (or (re-search-backward pat nil t)
2642 (progn
2643 (goto-char (point-max))
2644 (or (re-search-backward pat nil t)
2645 (progn
2646 (goto-char old-pt)
2647 (error "No cross references in this node")))))
8a7757f6 2648 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2649 (if (looking-at "\\* Menu:")
56cda6f5
RS
2650 (if recur
2651 (error "No cross references in this node")
185cff95
JL
2652 (Info-prev-reference t))
2653 (if (looking-at "^\\* ")
2654 (forward-char 2)))))
141b49f5
JL
2655\f
2656(defvar Info-index-nodes nil
2657 "Alist of cached index node names of visited Info files.
2658Each element has the form (INFO-FILE INDEX-NODE-NAMES-LIST).")
2659
2660(defun Info-index-nodes (&optional file)
2661 "Return a list of names of all index nodes in Info FILE.
2662If FILE is omitted, it defaults to the current Info file.
50163b00
JL
2663First look in a list of cached index node names. Then scan Info
2664file and its subfiles for nodes with the index cookie. Then try
2665to find index nodes starting from the first node in the top level
2666menu whose name contains the word \"Index\", plus any immediately
2667following nodes whose names also contain the word \"Index\"."
141b49f5
JL
2668 (or file (setq file Info-current-file))
2669 (or (assoc file Info-index-nodes)
2670 ;; Skip virtual Info files
50163b00
JL
2671 (and (member file '("dir" "history" "toc" "apropos"))
2672 (setq Info-index-nodes (cons (cons file nil) Info-index-nodes)))
2673 (not (stringp file))
94b10022
JL
2674 (if Info-file-supports-index-cookies
2675 ;; Find nodes with index cookie
2676 (let* ((default-directory (or (and (stringp file)
2677 (file-name-directory
2678 (setq file (Info-find-file file))))
2679 default-directory))
2680 Info-history Info-history-list Info-fontify-maximum-menu-size
2681 (main-file file) subfiles nodes node)
2682 (condition-case nil
2683 (with-temp-buffer
2684 (while (or main-file subfiles)
2685 (erase-buffer)
2686 (info-insert-file-contents (or main-file (car subfiles)))
2687 (goto-char (point-min))
2688 (while (search-forward "\0\b[index\0\b]" nil 'move)
2689 (save-excursion
2690 (re-search-backward "^\^_")
2691 (search-forward "Node: ")
2692 (setq nodes (cons (Info-following-node-name) nodes))))
2693 (if main-file
2694 (save-excursion
2695 (goto-char (point-min))
2696 (if (search-forward "\n\^_\nIndirect:" nil t)
2697 (let ((bound (save-excursion (search-forward "\n\^_" nil t))))
2698 (while (re-search-forward "^\\(.*\\): [0-9]+$" bound t)
2699 (setq subfiles (cons (match-string-no-properties 1)
2700 subfiles)))))
2701 (setq subfiles (nreverse subfiles)
2702 main-file nil))
2703 (setq subfiles (cdr subfiles)))))
2704 (error nil))
2705 (if nodes
2706 (setq nodes (nreverse nodes)
2707 Info-index-nodes (cons (cons file nodes) Info-index-nodes)))
2708 nodes)
2709 ;; Else find nodes with the word "Index" in the node name
2710 (let ((case-fold-search t)
2711 Info-history Info-history-list Info-fontify-maximum-menu-size
2712 nodes node)
2713 (condition-case nil
2714 (with-temp-buffer
2715 (Info-mode)
2716 (Info-find-node file "Top")
2717 (when (and (search-forward "\n* menu:" nil t)
2718 (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t))
2719 (goto-char (match-beginning 1))
2720 (setq nodes (list (Info-extract-menu-node-name)))
2721 (Info-goto-node (car nodes))
2722 (while (and (setq node (Info-extract-pointer "next" t))
2723 (string-match "\\<Index\\>" node))
2724 (setq nodes (cons node nodes))
2725 (Info-goto-node node))))
2726 (error nil))
2727 (if nodes
2728 (setq nodes (nreverse nodes)
2729 Info-index-nodes (cons (cons file nodes) Info-index-nodes)))
2730 nodes))
50163b00
JL
2731 ;; If file has no index nodes, still add it to the cache
2732 (setq Info-index-nodes (cons (cons file nil) Info-index-nodes)))
141b49f5
JL
2733 (cdr (assoc file Info-index-nodes)))
2734
2735(defun Info-index-node (&optional node file)
2736 "Return non-nil value if NODE is an index node.
2737If NODE is nil, check the current Info node.
2738If FILE is nil, check the current Info file."
50163b00
JL
2739 (if (or (and node (not (equal node Info-current-node)))
2740 (assoc (or file Info-current-file) Info-index-nodes))
2741 (member (or node Info-current-node) (Info-index-nodes file))
2742 ;; Don't search all index nodes if request is only for the current node
2743 ;; and file is not in the cache of index nodes
94b10022
JL
2744 (if Info-file-supports-index-cookies
2745 (save-excursion
2746 (goto-char (+ (or (save-excursion
2747 (search-backward "\n\^_" nil t))
2748 (point-min)) 2))
2749 (search-forward "\0\b[index\0\b]"
2750 (or (save-excursion
2751 (search-forward "\n\^_" nil t))
2752 (point-max)) t))
2753 (save-match-data
2754 (string-match "\\<Index\\>" (or node Info-current-node ""))))))
552775bd 2755
e4e8ee63 2756(defun Info-goto-index ()
141b49f5
JL
2757 "Go to the first index node."
2758 (let ((node (car (Info-index-nodes))))
2759 (or node (error "No index"))
2760 (Info-goto-node node)))
e4e8ee63 2761
201bc1bd 2762;;;###autoload
1143a6b0 2763(defun Info-index (topic)
fdf4b680 2764 "Look up a string TOPIC in the index for this file.
1143a6b0
ER
2765If there are no exact matches to the specified topic, this chooses
2766the first match which is a case-insensitive substring of a topic.
89b064a9 2767Use the \\<Info-mode-map>\\[Info-index-next] command to see the other matches.
1143a6b0 2768Give a blank topic name to go to the Index node itself."
e4e8ee63
SM
2769 (interactive
2770 (list
2771 (let ((Info-complete-menu-buffer (clone-buffer))
141b49f5
JL
2772 (Info-complete-nodes (Info-index-nodes))
2773 (Info-history-list nil))
aebd1760
RS
2774 (if (equal Info-current-file "dir")
2775 (error "The Info directory node has no index; use m to select a manual"))
e4e8ee63
SM
2776 (unwind-protect
2777 (with-current-buffer Info-complete-menu-buffer
2778 (Info-goto-index)
2779 (completing-read "Index topic: " 'Info-complete-menu-item))
2780 (kill-buffer Info-complete-menu-buffer)))))
aebd1760
RS
2781 (if (equal Info-current-file "dir")
2782 (error "The Info directory node has no index; use m to select a manual"))
1143a6b0 2783 (let ((orignode Info-current-node)
836bb478 2784 (pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^\n]*\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?"
1143a6b0 2785 (regexp-quote topic)))
141b49f5
JL
2786 node (nodes (Info-index-nodes))
2787 (ohist-list Info-history-list)
1bcedb3b 2788 (case-fold-search t))
e700ec12 2789 (Info-goto-index)
1143a6b0
ER
2790 (or (equal topic "")
2791 (let ((matches nil)
2792 (exact nil)
e700ec12
SM
2793 ;; We bind Info-history to nil for internal node-switches so
2794 ;; that we don't put junk in the history. In the first
2795 ;; Info-goto-index call, above, we do update the history
2796 ;; because that is what the user's previous node choice into it.
c696ac76 2797 (Info-history nil)
1143a6b0
ER
2798 found)
2799 (while
2800 (progn
2801 (goto-char (point-min))
2802 (while (re-search-forward pattern nil t)
983dfbf8
SM
2803 (push (list (match-string-no-properties 1)
2804 (match-string-no-properties 2)
2805 Info-current-node
3a8803c0
JPW
2806 (string-to-number (concat "0"
2807 (match-string 3))))
983dfbf8 2808 matches))
141b49f5 2809 (setq nodes (cdr nodes) node (car nodes)))
1143a6b0
ER
2810 (Info-goto-node node))
2811 (or matches
2812 (progn
81e14cb2 2813 (Info-goto-node orignode)
920bdaab 2814 (error "No `%s' in index" topic)))
1143a6b0
ER
2815 ;; Here it is a feature that assoc is case-sensitive.
2816 (while (setq found (assoc topic matches))
2817 (setq exact (cons found exact)
2818 matches (delq found matches)))
141b49f5 2819 (setq Info-history-list ohist-list)
1143a6b0
ER
2820 (setq Info-index-alternatives (nconc exact (nreverse matches)))
2821 (Info-index-next 0)))))
2822
2823(defun Info-index-next (num)
89b064a9 2824 "Go to the next matching index item from the last \\<Info-mode-map>\\[Info-index] command."
1143a6b0
ER
2825 (interactive "p")
2826 (or Info-index-alternatives
882e61bf 2827 (error "No previous `i' command"))
1143a6b0
ER
2828 (while (< num 0)
2829 (setq num (+ num (length Info-index-alternatives))))
2830 (while (> num 0)
2831 (setq Info-index-alternatives
2832 (nconc (cdr Info-index-alternatives)
2833 (list (car Info-index-alternatives)))
2834 num (1- num)))
2835 (Info-goto-node (nth 1 (car Info-index-alternatives)))
2836 (if (> (nth 3 (car Info-index-alternatives)) 0)
836bb478 2837 (forward-line (1- (nth 3 (car Info-index-alternatives))))
760d5cb3 2838 (forward-line 3) ; don't search in headers
1143a6b0 2839 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
2840 (Info-find-index-name name)))
2841 (message "Found `%s' in %s. %s"
1143a6b0
ER
2842 (car (car Info-index-alternatives))
2843 (nth 2 (car Info-index-alternatives))
2844 (if (cdr Info-index-alternatives)
05d36154
JL
2845 (format "(%s total; use `,' for next)"
2846 (length Info-index-alternatives))
1143a6b0
ER
2847 "(Only match)")))
2848
920bdaab
RS
2849(defun Info-find-index-name (name)
2850 "Move point to the place within the current node where NAME is defined."
1bcedb3b
RS
2851 (let ((case-fold-search t))
2852 (if (or (re-search-forward (format
2853 "[a-zA-Z]+: %s\\( \\|$\\)"
2854 (regexp-quote name)) nil t)
d31c6ecf
RS
2855 ;; Find a function definition with a return type.
2856 (re-search-forward (format
f2fc5b0a 2857 "[a-zA-Z]+: [a-zA-Z0-9_ *&]+ %s\\( \\|$\\)"
d31c6ecf 2858 (regexp-quote name)) nil t)
1bcedb3b
RS
2859 (search-forward (format "`%s'" name) nil t)
2860 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
2861 (search-forward
2862 (format "`%s'" (substring name 0 (match-beginning 1)))
2863 nil t))
bbe054f7
EZ
2864 (search-forward name nil t)
2865 ;; Try again without the " <1>" makeinfo can append
2866 (and (string-match "\\`\\(.*\\) <[0-9]+>\\'" name)
2867 (Info-find-index-name (match-string 1 name))))
2868 (progn (beginning-of-line) t) ;; non-nil for recursive call
1bcedb3b 2869 (goto-char (point-min)))))
920bdaab 2870
ab2f22ad
JH
2871;;;###autoload
2872(defun info-apropos (string)
2873 "Grovel indices of all known Info files on your system for STRING.
2874Build a menu of the possible matches."
2875 (interactive "sIndex apropos: ")
2876 (unless (string= string "")
836bb478 2877 (let ((pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^\n]+\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?"
ab2f22ad
JH
2878 (regexp-quote string)))
2879 (ohist Info-history)
37600b6d 2880 (ohist-list Info-history-list)
ab2f22ad
JH
2881 (current-node Info-current-node)
2882 (current-file Info-current-file)
141b49f5
JL
2883 manuals matches node nodes)
2884 (let ((Info-fontify-maximum-menu-size nil))
ab2f22ad
JH
2885 (Info-directory)
2886 (message "Searching indices...")
2887 (goto-char (point-min))
2888 (re-search-forward "\\* Menu: *\n" nil t)
7b30b20c 2889 (while (re-search-forward "\\*.*: *(\\([^)]+\\))" nil t)
185cff95
JL
2890 (setq manuals (cons (match-string 1) manuals)))
2891 (dolist (manual (nreverse manuals))
ab2f22ad 2892 (message "Searching %s" manual)
185cff95
JL
2893 (condition-case err
2894 (if (setq nodes (Info-index-nodes (Info-find-file manual)))
141b49f5
JL
2895 (save-excursion
2896 (Info-find-node manual (car nodes))
2897 (while
2898 (progn
2899 (goto-char (point-min))
2900 (while (re-search-forward pattern nil t)
185cff95
JL
2901 (setq matches
2902 (cons (list manual
2903 (match-string-no-properties 1)
2904 (match-string-no-properties 2)
2905 (match-string-no-properties 3))
2906 matches)))
141b49f5 2907 (setq nodes (cdr nodes) node (car nodes)))
185cff95
JL
2908 (Info-goto-node node))))
2909 (error
2910 (message "%s" (if (eq (car-safe err) 'error)
2911 (nth 1 err) err))
2912 (sit-for 1 t)))))
ab2f22ad 2913 (Info-goto-node (concat "(" current-file ")" current-node))
37600b6d
JH
2914 (setq Info-history ohist
2915 Info-history-list ohist-list)
ab2f22ad
JH
2916 (message "Searching indices...done")
2917 (if (null matches)
2918 (message "No matches found")
2919 (with-current-buffer (get-buffer-create " *info-apropos*")
2920 (erase-buffer)
141b49f5 2921 (insert "\n\^_\nFile: apropos, Node: Index, Up: (dir)\n")
185cff95
JL
2922 (insert "* Menu: \nNodes whose indices contain `" string "':\n\n")
2923 (dolist (entry (nreverse matches))
5242942f
JH
2924 (insert
2925 (format "* %-38s (%s)%s.%s\n"
2926 (concat (nth 1 entry) " [" (nth 0 entry) "]:")
2927 (nth 0 entry)
2928 (nth 2 entry)
2929 (if (nth 3 entry)
2930 (concat " (line " (nth 3 entry) ")")
2931 "")))))
2932 (Info-find-node "apropos" "Index")
82f1aca9 2933 (setq Info-complete-cache nil)))))
ab2f22ad 2934
a384cab3
JB
2935(defun Info-undefined ()
2936 "Make command be undefined in Info."
2937 (interactive)
2938 (ding))
2939
2940(defun Info-help ()
2941 "Enter the Info tutorial."
2942 (interactive)
2943 (delete-other-windows)
2944 (Info-find-node "info"
2945 (if (< (window-height) 23)
2946 "Help-Small-Screen"
2947 "Help")))
2948
2949(defun Info-summary ()
2950 "Display a brief summary of all Info commands."
2951 (interactive)
2952 (save-window-excursion
2953 (switch-to-buffer "*Help*")
881c84c7 2954 (setq buffer-read-only nil)
a384cab3
JB
2955 (erase-buffer)
2956 (insert (documentation 'Info-mode))
9d29f94c 2957 (help-mode)
a384cab3
JB
2958 (goto-char (point-min))
2959 (let (ch flag)
2960 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
2961 (message (if flag "Type Space to see more"
2962 "Type Space to return to Info"))
fc039bdc 2963 (if (not (eq ?\s (setq ch (read-event))))
dbc4e1c1 2964 (progn (setq unread-command-events (list ch)) nil)
a384cab3 2965 flag))
552775bd
RS
2966 (scroll-up)))
2967 (bury-buffer "*Help*")))
a384cab3
JB
2968\f
2969(defun Info-get-token (pos start all &optional errorstring)
fdf4b680 2970 "Return the token around POS.
a384cab3
JB
2971POS must be somewhere inside the token
2972START is a regular expression which will match the
2973 beginning of the tokens delimited string
2974ALL is a regular expression with a single
90a715f0 2975 parenthesized subpattern which is the token to be
fdf4b680 2976 returned. E.g. '{\(.*\)}' would return any string
a384cab3 2977 enclosed in braces around POS.
fdf4b680 2978ERRORSTRING optional fourth argument, controls action on no match
a384cab3
JB
2979 nil: return nil
2980 t: beep
2981 a string: signal an error, using that string."
1bcedb3b
RS
2982 (let ((case-fold-search t))
2983 (save-excursion
2984 (goto-char pos)
2985 ;; First look for a match for START that goes across POS.
2986 (while (and (not (bobp)) (> (point) (- pos (length start)))
2987 (not (looking-at start)))
2988 (forward-char -1))
2989 ;; If we did not find one, search back for START
2990 ;; (this finds only matches that end at or before POS).
2991 (or (looking-at start)
2992 (progn
2993 (goto-char pos)
2994 (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
2995 (let (found)
2996 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
2997 (not (setq found (and (<= (match-beginning 0) pos)
2998 (> (match-end 0) pos))))))
2999 (if (and found (<= (match-beginning 0) pos)
3000 (> (match-end 0) pos))
110d4e80 3001 (match-string-no-properties 1)
1bcedb3b
RS
3002 (cond ((null errorstring)
3003 nil)
3004 ((eq errorstring t)
3005 (beep)
3006 nil)
3007 (t
3008 (error "No %s around position %d" errorstring pos))))))))
a384cab3 3009
981947af 3010(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
3011 "\\<Info-mode-map>Follow a node reference near point.
3012Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 3013At end of the node's text, moves to the next node, or up if none."
f9969361 3014 (interactive "e")
d1268e52 3015 (mouse-set-point click)
981947af
KH
3016 (and (not (Info-try-follow-nearest-node))
3017 (save-excursion (forward-line 1) (eobp))
ed690657 3018 (Info-next-preorder)))
981947af 3019
8a7757f6 3020(defun Info-follow-nearest-node (&optional fork)
d0b560c6
JB
3021 "Follow a node reference near point.
3022If point is on a reference, follow that reference. Otherwise,
3023if point is in a menu item description, follow that menu item."
8a7757f6
JL
3024 (interactive "P")
3025 (or (Info-try-follow-nearest-node fork)
d0b560c6
JB
3026 (when (save-excursion
3027 (search-backward "\n* menu:" nil t))
3028 (save-excursion
3029 (beginning-of-line)
3030 (while (not (or (bobp) (looking-at "[^ \t]\\|[ \t]*$")))
3031 (beginning-of-line 0))
3032 (when (looking-at "\\* +\\([^\t\n]*\\):")
3033 (Info-goto-node
8a7757f6 3034 (Info-extract-menu-item (match-string-no-properties 1)) fork)
d0b560c6
JB
3035 t)))
3036 (error "Point neither on reference nor in menu item description")))
981947af
KH
3037
3038;; Common subroutine.
8a7757f6 3039(defun Info-try-follow-nearest-node (&optional fork)
981947af 3040 "Follow a node reference near point. Return non-nil if successful."
6af7040d 3041 (let (node)
a384cab3 3042 (cond
6b3600ff 3043 ((Info-get-token (point) "[hf]t?tp://" "[hf]t?tp://\\([^ \t\n\"`({<>})']+\\)")
8a7757f6
JL
3044 (setq node t)
3045 (browse-url (browse-url-url-at-point)))
3046 ((setq node (Info-get-token (point) "\\*note[ \n\t]+"
3047 "\\*note[ \n\t]+\\([^:]*\\):\\(:\\|[ \n\t]*(\\)?"))
8a7757f6 3048 (Info-follow-reference node fork))
505b68d5 3049 ;; menu item: node name
b37daea4 3050 ((setq node (Info-get-token (point) "\\* +" "\\* +\\([^:]*\\)::"))
8a7757f6 3051 (Info-goto-node node fork))
836bb478 3052 ;; menu item: node name or index entry
505b68d5 3053 ((Info-get-token (point) "\\* +" "\\* +\\(.*\\): ")
631ba13e
RS
3054 (beginning-of-line)
3055 (forward-char 2)
141b49f5 3056 (setq node (Info-extract-menu-node-name nil (Info-index-node)))
8a7757f6 3057 (Info-goto-node node fork))
bc2ada62 3058 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
8a7757f6 3059 (Info-goto-node node fork))
bc2ada62 3060 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
8a7757f6 3061 (Info-goto-node node fork))
bc2ada62 3062 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
8a7757f6 3063 (Info-goto-node "Top" fork))
bc2ada62 3064 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
8a7757f6 3065 (Info-goto-node node fork)))
981947af 3066 node))
a384cab3
JB
3067\f
3068(defvar Info-mode-map nil
3069 "Keymap containing Info commands.")
3070(if Info-mode-map
3071 nil
3072 (setq Info-mode-map (make-keymap))
3073 (suppress-keymap Info-mode-map)
3074 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 3075 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 3076 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd 3077 (define-key Info-mode-map "\t" 'Info-next-reference)
a0c64f02 3078 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
508f27a0
RS
3079 (define-key Info-mode-map [(shift tab)] 'Info-prev-reference)
3080 (define-key Info-mode-map [backtab] 'Info-prev-reference)
e38e7367
RM
3081 (define-key Info-mode-map "1" 'Info-nth-menu-item)
3082 (define-key Info-mode-map "2" 'Info-nth-menu-item)
3083 (define-key Info-mode-map "3" 'Info-nth-menu-item)
3084 (define-key Info-mode-map "4" 'Info-nth-menu-item)
3085 (define-key Info-mode-map "5" 'Info-nth-menu-item)
3086 (define-key Info-mode-map "6" 'Info-nth-menu-item)
3087 (define-key Info-mode-map "7" 'Info-nth-menu-item)
3088 (define-key Info-mode-map "8" 'Info-nth-menu-item)
3089 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 3090 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
3091 (define-key Info-mode-map "?" 'Info-summary)
3092 (define-key Info-mode-map "]" 'Info-forward-node)
3093 (define-key Info-mode-map "[" 'Info-backward-node)
3094 (define-key Info-mode-map "<" 'Info-top-node)
3095 (define-key Info-mode-map ">" 'Info-final-node)
3096 (define-key Info-mode-map "b" 'beginning-of-buffer)
3097 (define-key Info-mode-map "d" 'Info-directory)
3098 (define-key Info-mode-map "e" 'Info-edit)
3099 (define-key Info-mode-map "f" 'Info-follow-reference)
3100 (define-key Info-mode-map "g" 'Info-goto-node)
3101 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 3102 (define-key Info-mode-map "i" 'Info-index)
424b9d3e 3103 (define-key Info-mode-map "l" 'Info-history-back)
bc224645 3104 (define-key Info-mode-map "L" 'Info-history)
a384cab3
JB
3105 (define-key Info-mode-map "m" 'Info-menu)
3106 (define-key Info-mode-map "n" 'Info-next)
3107 (define-key Info-mode-map "p" 'Info-prev)
3108 (define-key Info-mode-map "q" 'Info-exit)
424b9d3e 3109 (define-key Info-mode-map "r" 'Info-history-forward)
a384cab3 3110 (define-key Info-mode-map "s" 'Info-search)
50163b00 3111 (define-key Info-mode-map "S" 'Info-search-case-sensitively)
47fc33ca
RS
3112 ;; For consistency with Rmail.
3113 (define-key Info-mode-map "\M-s" 'Info-search)
4fceda3c 3114 (define-key Info-mode-map "\M-n" 'clone-buffer)
db0c9809 3115 (define-key Info-mode-map "t" 'Info-top-node)
bc224645 3116 (define-key Info-mode-map "T" 'Info-toc)
a384cab3 3117 (define-key Info-mode-map "u" 'Info-up)
185cff95 3118 ;; `w' for consistency with `dired-copy-filename-as-kill'.
50163b00 3119 (define-key Info-mode-map "w" 'Info-copy-current-node-name)
185cff95
JL
3120 (define-key Info-mode-map "c" 'Info-copy-current-node-name)
3121 ;; `^' for consistency with `dired-up-directory'.
3122 (define-key Info-mode-map "^" 'Info-up)
1143a6b0 3123 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 3124 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 3125 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
3c1498ac 3126 (define-key Info-mode-map [follow-link] 'mouse-face)
aea2a8da 3127 )
75a209d4
RS
3128
3129(defun Info-check-pointer (item)
fdf4b680 3130 "Non-nil if ITEM is present in this node."
75a209d4
RS
3131 (condition-case nil
3132 (Info-extract-pointer item)
3133 (error nil)))
3134
d4af987a
DL
3135(easy-menu-define
3136 Info-mode-menu Info-mode-map
185cff95 3137 "Menu for Info files."
d4af987a
DL
3138 '("Info"
3139 ["Up" Info-up :active (Info-check-pointer "up")
3140 :help "Go up in the Info tree"]
3141 ["Next" Info-next :active (Info-check-pointer "next")
3142 :help "Go to the next node"]
3143 ["Previous" Info-prev :active (Info-check-pointer "prev[ious]*")
3144 :help "Go to the previous node"]
3145 ["Backward" Info-backward-node
3146 :help "Go backward one node, considering all as a sequence"]
3147 ["Forward" Info-forward-node
3148 :help "Go forward one node, considering all as a sequence"]
7210b6f5
DL
3149 ["Beginning" beginning-of-buffer
3150 :help "Go to beginning of this node"]
d4af987a
DL
3151 ["Top" Info-top-node
3152 :help "Go to top node of file"]
3153 ["Final Node" Info-final-node
3154 :help "Go to final node in this file"]
3155 ("Menu Item" ["You should never see this" report-emacs-bug t])
3156 ("Reference" ["You should never see this" report-emacs-bug t])
3157 ["Search..." Info-search
3158 :help "Search for regular expression in this Info file"]
8a7757f6
JL
3159 ["Search Next" Info-search-next
3160 :help "Search for another occurrence of regular expression"]
d1268e52 3161 ["Go to Node..." Info-goto-node
d4af987a 3162 :help "Go to a named node"]
424b9d3e
JL
3163 ["Back in history" Info-history-back :active Info-history
3164 :help "Go back in history to the last node you were at"]
3165 ["Forward in history" Info-history-forward :active Info-history-forward
3166 :help "Go forward in history"]
8a7757f6 3167 ["History" Info-history :active Info-history-list
bc224645 3168 :help "Go to menu of visited nodes"]
8a7757f6 3169 ["Table of Contents" Info-toc
bc224645 3170 :help "Go to table of contents"]
5938202c
JL
3171 ("Index"
3172 ["Lookup a String..." Info-index
d4af987a 3173 :help "Look for a string in the index items"]
5938202c 3174 ["Next Matching Item" Info-index-next :active Info-index-alternatives
ab2f22ad 3175 :help "Look for another occurrence of previous item"]
5938202c 3176 ["Lookup a string in all indices..." info-apropos
ab2f22ad 3177 :help "Look for a string in the indices of all manuals"])
126f5d4d
MB
3178 ["Copy Node Name" Info-copy-current-node-name
3179 :help "Copy the name of the current node into the kill ring"]
8a7757f6
JL
3180 ["Clone Info buffer" clone-buffer
3181 :help "Create a twin copy of the current Info buffer."]
ec03f31e 3182 ["Exit" Info-exit :help "Stop reading Info"]))
75a209d4 3183
7210b6f5
DL
3184
3185(defvar info-tool-bar-map
3186 (if (display-graphic-p)
df6aed99
RS
3187 (let ((map (make-sparse-keymap)))
3188 (tool-bar-local-item-from-menu 'Info-exit "close" map Info-mode-map)
3c513c32
JD
3189 (tool-bar-local-item-from-menu 'Info-history-back "left-arrow" map Info-mode-map)
3190 (tool-bar-local-item-from-menu 'Info-history-forward "right-arrow" map Info-mode-map)
db2cf961
JD
3191 (tool-bar-local-item-from-menu 'Info-prev "prev-node" map Info-mode-map)
3192 (tool-bar-local-item-from-menu 'Info-next "next-node" map Info-mode-map)
3193 (tool-bar-local-item-from-menu 'Info-up "up-node" map Info-mode-map)
df6aed99
RS
3194 (tool-bar-local-item-from-menu 'Info-top-node "home" map Info-mode-map)
3195 (tool-bar-local-item-from-menu 'Info-index "index" map Info-mode-map)
ffda926c 3196 (tool-bar-local-item-from-menu 'Info-goto-node "jump-to" map Info-mode-map)
df6aed99
RS
3197 (tool-bar-local-item-from-menu 'Info-search "search" map Info-mode-map)
3198 map)))
7210b6f5 3199
75a209d4
RS
3200(defvar Info-menu-last-node nil)
3201;; Last node the menu was created for.
6d2c8e3e 3202;; Value is a list, (FILE-NAME NODE-NAME).
75a209d4
RS
3203
3204(defun Info-menu-update ()
fdf4b680 3205 "Update the Info menu for the current node."
75a209d4
RS
3206 (condition-case nil
3207 (if (or (not (eq major-mode 'Info-mode))
6d2c8e3e
RS
3208 (equal (list Info-current-file Info-current-node)
3209 Info-menu-last-node))
75a209d4
RS
3210 ()
3211 ;; Update menu menu.
3212 (let* ((Info-complete-menu-buffer (current-buffer))
3213 (items (nreverse (condition-case nil
e4e8ee63 3214 (Info-complete-menu-item "" nil t)
75a209d4 3215 (error nil))))
399c88ad 3216 entries current
75a209d4
RS
3217 (number 0))
3218 (while (and items (< number 9))
3219 (setq current (car items)
3220 items (cdr items)
3221 number (1+ number))
399c88ad 3222 (setq entries (cons `[,current
75a209d4
RS
3223 (Info-menu ,current)
3224 :keys ,(format "%d" number)]
3225 entries)))
3226 (if items
3227 (setq entries (cons ["Other..." Info-menu t] entries)))
3228 (or entries
5d5845dc 3229 (setq entries (list ["No menu" nil nil] nil :active)))
d4af987a 3230 (easy-menu-change '("Info") "Menu Item" (nreverse entries)))
75a209d4
RS
3231 ;; Update reference menu. Code stolen from `Info-follow-reference'.
3232 (let ((items nil)
399c88ad 3233 str i entries current
1bcedb3b
RS
3234 (number 0)
3235 (case-fold-search t))
75a209d4
RS
3236 (save-excursion
3237 (goto-char (point-min))
8a7757f6 3238 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 3239 (setq str (match-string 1))
75a209d4
RS
3240 (setq i 0)
3241 (while (setq i (string-match "[ \n\t]+" str i))
3242 (setq str (concat (substring str 0 i) " "
3243 (substring str (match-end 0))))
3244 (setq i (1+ i)))
3245 (setq items
3246 (cons str items))))
3247 (while (and items (< number 9))
3248 (setq current (car items)
3249 items (cdr items)
3250 number (1+ number))
399c88ad 3251 (setq entries (cons `[,current
75a209d4
RS
3252 (Info-follow-reference ,current)
3253 t]
3254 entries)))
3255 (if items
3256 (setq entries (cons ["Other..." Info-follow-reference t]
3257 entries)))
3258 (or entries
5d5845dc 3259 (setq entries (list ["No references" nil nil] nil :active)))
75a209d4
RS
3260 (easy-menu-change '("Info") "Reference" (nreverse entries)))
3261 ;; Update last seen node.
6d2c8e3e 3262 (setq Info-menu-last-node (list Info-current-file Info-current-node)))
75a209d4
RS
3263 ;; Try to avoid entering infinite beep mode in case of errors.
3264 (error (ding))))
3265
a384cab3 3266\f
50163b00 3267(defun Info-copy-current-node-name (&optional arg)
185cff95
JL
3268 "Put the name of the current Info node into the kill ring.
3269The name of the Info file is prepended to the node name in parentheses.
50163b00
JL
3270With a zero prefix arg, put the name inside a function call to `info'."
3271 (interactive "P")
126f5d4d 3272 (unless Info-current-node
185cff95 3273 (error "No current Info node"))
d590dbd2
CY
3274 (let ((node (concat "(" (file-name-nondirectory Info-current-file) ")"
3275 Info-current-node)))
50163b00
JL
3276 (if (zerop (prefix-numeric-value arg))
3277 (setq node (concat "(info \"" node "\")")))
3278 (kill-new node)
3279 (message "%s" node)))
126f5d4d
MB
3280
3281\f
a384cab3 3282;; Info mode is suitable only for specially formatted data.
68bc119c 3283(put 'Info-mode 'mode-class 'special)
2fa8f8a4 3284(put 'Info-mode 'no-clone-indirect t)
a384cab3 3285
f4f2bca3
RS
3286(defvar tool-bar-map)
3287
80123c9e
LH
3288;; Autoload cookie needed by desktop.el
3289;;;###autoload
a384cab3 3290(defun Info-mode ()
fdf4b680 3291 "Info mode provides commands for browsing through the Info documentation tree.
a384cab3
JB
3292Documentation in Info is divided into \"nodes\", each of which discusses
3293one topic and contains references to other nodes which discuss related
3294topics. Info has commands to follow the references and show you other nodes.
3295
fdf4b680 3296\\<Info-mode-map>\
a384cab3 3297\\[Info-help] Invoke the Info tutorial.
6b136ab9 3298\\[Info-exit] Quit Info: reselect previously selected buffer.
a384cab3
JB
3299
3300Selecting other nodes:
21d5959f
RS
3301\\[Info-mouse-follow-nearest-node]
3302 Follow a node reference you click on.
3303 This works with menu items, cross references, and
3304 the \"next\", \"previous\" and \"up\", depending on where you click.
6b136ab9 3305\\[Info-follow-nearest-node] Follow a node reference near point, like \\[Info-mouse-follow-nearest-node].
a384cab3 3306\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 3307\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
3308\\[Info-up] Move \"up\" from this node.
3309\\[Info-menu] Pick menu item specified by name (or abbreviation).
e8042313 3310 Picking a menu item causes another node to be selected.
aea2a8da 3311\\[Info-directory] Go to the Info directory node.
8300a5b5
JL
3312\\[Info-top-node] Go to the Top node of this file.
3313\\[Info-final-node] Go to the final node in this file.
3314\\[Info-backward-node] Go backward one node, considering all nodes as forming one sequence.
3315\\[Info-forward-node] Go forward one node, considering all nodes as forming one sequence.
3316\\[Info-next-reference] Move cursor to next cross-reference or menu item.
3317\\[Info-prev-reference] Move cursor to previous cross-reference or menu item.
a384cab3 3318\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
424b9d3e
JL
3319\\[Info-history-back] Move back in history to the last node you were at.
3320\\[Info-history-forward] Move forward in history to the node you returned from after using \\[Info-history-back].
bc224645
JL
3321\\[Info-history] Go to menu of visited nodes.
3322\\[Info-toc] Go to table of contents of the current Info file.
a384cab3
JB
3323
3324Moving within a node:
177c3549 3325\\[Info-scroll-up] Normally, scroll forward a full screen.
e8042313
PJ
3326 Once you scroll far enough in a node that its menu appears on the
3327 screen but after point, the next scroll moves into its first
3328 subnode. When after all menu items (or if there is no menu),
3329 move up to the parent node.
177c3549 3330\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
e8042313
PJ
3331 already visible, try to go to the previous menu entry, or up
3332 if there is none.
399c88ad 3333\\[beginning-of-buffer] Go to beginning of node.
a384cab3 3334
a384cab3 3335Advanced commands:
a384cab3 3336\\[Info-search] Search through this Info file for specified regexp,
e8042313 3337 and select the node in which the next occurrence is found.
bc224645 3338\\[Info-search-case-sensitively] Search through this Info file for specified regexp case-sensitively.
8a7757f6 3339\\[Info-search-next] Search for another occurrence of regexp
89b064a9 3340 from a previous \\<Info-mode-map>\\[Info-search] command.
8300a5b5
JL
3341\\[Info-index] Look up a topic in this file's Index and move to that node.
3342\\[Info-index-next] (comma) Move to the next match from a previous \\<Info-mode-map>\\[Info-index] command.
3343\\[info-apropos] Look for a string in the indices of all manuals.
3344\\[Info-goto-node] Move to node specified by name.
3345 You may include a filename as well, as (FILENAME)NODENAME.
33461 .. 9 Pick first ... ninth item in node's menu.
3347 Every third `*' is highlighted to help pick the right number.
3348\\[Info-copy-current-node-name] Put name of current Info node in the kill ring.
3349\\[clone-buffer] Select a new cloned Info buffer in another window.
3350\\[universal-argument] \\[info] Move to new Info file with completion.
3351\\[universal-argument] N \\[info] Select Info buffer with prefix number in the name *info*<N>."
a384cab3
JB
3352 (kill-all-local-variables)
3353 (setq major-mode 'Info-mode)
3354 (setq mode-name "Info")
f73299f3 3355 (setq tab-width 8)
a384cab3 3356 (use-local-map Info-mode-map)
75a209d4 3357 (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
a384cab3
JB
3358 (set-syntax-table text-mode-syntax-table)
3359 (setq local-abbrev-table text-mode-abbrev-table)
3360 (setq case-fold-search t)
3361 (setq buffer-read-only t)
a384cab3
JB
3362 (make-local-variable 'Info-current-file)
3363 (make-local-variable 'Info-current-subfile)
3364 (make-local-variable 'Info-current-node)
3365 (make-local-variable 'Info-tag-table-marker)
c5fe2ff1
RS
3366 (setq Info-tag-table-marker (make-marker))
3367 (make-local-variable 'Info-tag-table-buffer)
3368 (setq Info-tag-table-buffer nil)
a384cab3 3369 (make-local-variable 'Info-history)
424b9d3e 3370 (make-local-variable 'Info-history-forward)
1143a6b0 3371 (make-local-variable 'Info-index-alternatives)
dde7f979
SM
3372 (setq header-line-format
3373 (if Info-use-header-line
3536c121 3374 '(:eval (get-text-property (point-min) 'header-line))
dde7f979 3375 nil)) ; so the header line isn't displayed
7210b6f5 3376 (set (make-local-variable 'tool-bar-map) info-tool-bar-map)
93480d70
RS
3377 ;; This is for the sake of the invisible text we use handling titles.
3378 (make-local-variable 'line-move-ignore-invisible)
3379 (setq line-move-ignore-invisible t)
9d9d10ec 3380 (make-local-variable 'desktop-save-buffer)
ab357a65
RS
3381 (make-local-variable 'widen-automatically)
3382 (setq widen-automatically nil)
9d9d10ec 3383 (setq desktop-save-buffer 'Info-desktop-buffer-misc-data)
34d86208 3384 (add-hook 'kill-buffer-hook 'Info-kill-buffer nil t)
b7aeabeb 3385 (add-hook 'clone-buffer-hook 'Info-clone-buffer-hook nil t)
3d12b331 3386 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
d457219d 3387 (add-hook 'isearch-mode-hook 'Info-isearch-start nil t)
0b02cda9
JL
3388 (set (make-local-variable 'isearch-search-fun-function)
3389 'Info-isearch-search)
3390 (set (make-local-variable 'isearch-wrap-function)
3391 'Info-isearch-wrap)
3392 (set (make-local-variable 'isearch-push-state-function)
3393 'Info-isearch-push-state)
3394 (set (make-local-variable 'search-whitespace-regexp)
3395 Info-search-whitespace-regexp)
a384cab3 3396 (Info-set-mode-line)
d39f149d 3397 (run-mode-hooks 'Info-mode-hook))
a384cab3 3398
34d86208
JL
3399;; When an Info buffer is killed, make sure the associated tags buffer
3400;; is killed too.
3401(defun Info-kill-buffer ()
3402 (and (eq major-mode 'Info-mode)
3403 Info-tag-table-buffer
3404 (kill-buffer Info-tag-table-buffer)))
3405
4fceda3c
SM
3406(defun Info-clone-buffer-hook ()
3407 (when (bufferp Info-tag-table-buffer)
3408 (setq Info-tag-table-buffer
4ce5c223
SM
3409 (with-current-buffer Info-tag-table-buffer (clone-buffer))))
3410 (let ((m Info-tag-table-marker))
3411 (when (markerp m)
15533ae2 3412 (setq Info-tag-table-marker
4ce5c223 3413 (if (and (marker-position m) (bufferp Info-tag-table-buffer))
15533ae2
SM
3414 (with-current-buffer Info-tag-table-buffer
3415 (copy-marker (marker-position m)))
3416 (make-marker))))))
4fceda3c 3417
7210b6f5
DL
3418(defvar Info-edit-map (let ((map (make-sparse-keymap)))
3419 (set-keymap-parent map text-mode-map)
3420 (define-key map "\C-c\C-c" 'Info-cease-edit)
3421 map)
a384cab3 3422 "Local keymap used within `e' command of Info.")
a384cab3
JB
3423
3424;; Info-edit mode is suitable only for specially formatted data.
68bc119c 3425(put 'Info-edit-mode 'mode-class 'special)
a384cab3
JB
3426
3427(defun Info-edit-mode ()
3428 "Major mode for editing the contents of an Info node.
a426b157 3429Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
3430which returns to Info mode for browsing.
3431\\{Info-edit-map}"
a384cab3
JB
3432 (use-local-map Info-edit-map)
3433 (setq major-mode 'Info-edit-mode)
3434 (setq mode-name "Info Edit")
3435 (kill-local-variable 'mode-line-buffer-identification)
3436 (setq buffer-read-only nil)
2c609f53 3437 (force-mode-line-update)
e82c28f9 3438 (buffer-enable-undo (current-buffer))
d39f149d 3439 (run-mode-hooks 'Info-edit-mode-hook))
e82c28f9
KH
3440
3441(defun Info-edit ()
3442 "Edit the contents of this Info node.
3443Allowed only if variable `Info-enable-edit' is non-nil."
3444 (interactive)
3445 (or Info-enable-edit
185cff95 3446 (error "Editing Info nodes is not enabled"))
e82c28f9 3447 (Info-edit-mode)
8ab3e50b 3448 (message "%s" (substitute-command-keys
760d5cb3 3449 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
3450
3451(defun Info-cease-edit ()
3452 "Finish editing Info node; switch back to Info proper."
3453 (interactive)
3454 ;; Do this first, so nothing has changed if user C-g's at query.
3455 (and (buffer-modified-p)
3456 (y-or-n-p "Save the file? ")
3457 (save-buffer))
3458 (use-local-map Info-mode-map)
3459 (setq major-mode 'Info-mode)
3460 (setq mode-name "Info")
3461 (Info-set-mode-line)
3462 (setq buffer-read-only t)
2c609f53 3463 (force-mode-line-update)
a384cab3
JB
3464 (and (marker-position Info-tag-table-marker)
3465 (buffer-modified-p)
3466 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 3467\f
f88ab1c9 3468(defvar Info-file-list-for-emacs
02c9f5cd 3469 '("ediff" "eudc" "forms" "gnus" "info" ("Info" . "info") ("mh" . "mh-e")
906b3163
EZ
3470 "sc" "message" ("dired" . "dired-x") "viper" "vip" "idlwave"
3471 ("c" . "ccmode") ("c++" . "ccmode") ("objc" . "ccmode")
3472 ("java" . "ccmode") ("idl" . "ccmode") ("pike" . "ccmode")
af718538
EZ
3473 ("skeleton" . "autotype") ("auto-insert" . "autotype")
3474 ("copyright" . "autotype") ("executable" . "autotype")
3475 ("time-stamp" . "autotype") ("quickurl" . "autotype")
3476 ("tempo" . "autotype") ("hippie-expand" . "autotype")
807378ad
EZ
3477 ("cvs" . "pcl-cvs") ("ada" . "ada-mode") "calc"
3478 ("calcAlg" . "calc") ("calcDigit" . "calc") ("calcVar" . "calc")
906b3163
EZ
3479 "ebrowse" "eshell" "cl" "reftex" "speedbar" "widget" "woman"
3480 ("mail-header" . "emacs-mime") ("mail-content" . "emacs-mime")
3481 ("mail-encode" . "emacs-mime") ("mail-decode" . "emacs-mime")
3482 ("rfc2045" . "emacs-mime")
3483 ("rfc2231" . "emacs-mime") ("rfc2047" . "emacs-mime")
3484 ("rfc2045" . "emacs-mime") ("rfc1843" . "emacs-mime")
3485 ("ietf-drums" . "emacs-mime") ("quoted-printable" . "emacs-mime")
3486 ("binhex" . "emacs-mime") ("uudecode" . "emacs-mime")
3487 ("mailcap" . "emacs-mime") ("mm" . "emacs-mime")
3488 ("mml" . "emacs-mime"))
f88ab1c9
RS
3489 "List of Info files that describe Emacs commands.
3490An element can be a file name, or a list of the form (PREFIX . FILE)
3491where PREFIX is a name prefix and FILE is the file to look in.
3492If the element is just a file name, the file name also serves as the prefix.")
3493
f0a8a3f1 3494(defun Info-find-emacs-command-nodes (command)
f88ab1c9 3495 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
3496The `info-file' property of COMMAND says which Info manual to search.
3497If COMMAND has no property, the variable `Info-file-list-for-emacs'
3498defines heuristics for which Info manual to try.
fdf4b680 3499The locations are of the format used in `Info-history', i.e.
02c9f5cd
JL
3500\(FILENAME NODENAME BUFFERPOS\), where BUFFERPOS is the line number
3501in the first element of the returned list (which is treated specially in
3502`Info-goto-emacs-command-node'), and 0 for the rest elements of a list."
3503 (let ((where '()) line-number
b37daea4 3504 (cmd-desc (concat "^\\* +" (regexp-quote (symbol-name command))
f254e21f 3505 "\\( <[0-9]+>\\)?:\\s *\\(.*\\)\\."
02c9f5cd 3506 "\\(?:[ \t\n]+(line +\\([0-9]+\\))\\)?"))
f88ab1c9 3507 (info-file "emacs")) ;default
185cff95 3508 ;; Determine which Info file this command is documented in.
f88ab1c9
RS
3509 (if (get command 'info-file)
3510 (setq info-file (get command 'info-file))
3511 ;; If it doesn't say explicitly, test its name against
3512 ;; various prefixes that we know.
3513 (let ((file-list Info-file-list-for-emacs))
3514 (while file-list
3515 (let* ((elt (car file-list))
3516 (name (if (consp elt)
3517 (car elt)
3518 elt))
3519 (file (if (consp elt) (cdr elt) elt))
cc3f6c32 3520 (case-fold-search nil)
f57b2cd8 3521 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
3522 "\\(\\'\\|-\\)")))
3523 (if (string-match regexp (symbol-name command))
3524 (setq info-file file file-list nil))
3525 (setq file-list (cdr file-list))))))
218c2cc7 3526 (Info-find-node info-file "Top")
218c2cc7
EZ
3527 ;; Bind Info-history to nil, to prevent the index nodes from
3528 ;; getting into the node history.
3529 (let ((Info-history nil)
141b49f5
JL
3530 (Info-history-list nil)
3531 node (nodes (Info-index-nodes)))
3532 (Info-goto-node (car nodes))
218c2cc7
EZ
3533 (while
3534 (progn
3535 (goto-char (point-min))
3536 (while (re-search-forward cmd-desc nil t)
3537 (setq where
3538 (cons (list Info-current-file
3539 (match-string-no-properties 2)
760d5cb3 3540 0)
02c9f5cd
JL
3541 where))
3542 (setq line-number (and (match-beginning 3)
3543 (string-to-number (match-string 3)))))
141b49f5 3544 (and (setq nodes (cdr nodes) node (car nodes))))
218c2cc7 3545 (Info-goto-node node)))
02c9f5cd
JL
3546 (if (and line-number where)
3547 (cons (list (nth 0 (car where)) (nth 1 (car where)) line-number)
3548 (cdr where))
3549 where)))
f0a8a3f1 3550
02c9f5cd 3551;;;###autoload (put 'Info-goto-emacs-command-node 'info-file "emacs")
f0a8a3f1
RM
3552;;;###autoload
3553(defun Info-goto-emacs-command-node (command)
e0568e86 3554 "Go to the Info node in the Emacs manual for command COMMAND.
30db89f9 3555The command is found by looking up in Emacs manual's indices
f57b2cd8 3556or in another manual found via COMMAND's `info-file' property or
1ed6e0c3
JPW
3557the variable `Info-file-list-for-emacs'.
3558COMMAND must be a symbol or string."
f0a8a3f1 3559 (interactive "CFind documentation for command: ")
fafb00dc
MY
3560 ;; If command is given as a string, convert it to a symbol.
3561 (if (stringp command)
1ed6e0c3 3562 (setq command (intern command)))
f0a8a3f1
RM
3563 (or (commandp command)
3564 (signal 'wrong-type-argument (list 'commandp command)))
3565 (let ((where (Info-find-emacs-command-nodes command)))
3566 (if where
3567 (let ((num-matches (length where)))
3568 ;; Get Info running, and pop to it in another window.
3569 (save-window-excursion
3570 (info))
89b064a9 3571 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
218c2cc7
EZ
3572 ;; Bind Info-history to nil, to prevent the last Index node
3573 ;; visited by Info-find-emacs-command-nodes from being
3574 ;; pushed onto the history.
02c9f5cd
JL
3575 (let ((Info-history nil) (Info-history-list nil)
3576 (line-number (nth 2 (car where))))
3577 (Info-find-node (nth 0 (car where)) (nth 1 (car where)))
3578 (if (and (integerp line-number) (> line-number 0))
3579 (forward-line (1- line-number))))
f0a8a3f1
RM
3580 (if (> num-matches 1)
3581 (progn
218c2cc7
EZ
3582 ;; (car where) will be pushed onto Info-history
3583 ;; when/if they go to another node. Put the other
3584 ;; nodes that were found on the history.
f0a8a3f1 3585 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 3586 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
3587 (1- num-matches)
3588 (if (> num-matches 2) "ies" "y")
424b9d3e 3589 (substitute-command-keys "\\[Info-history-back]")
cedb118c 3590 (if (> num-matches 2) "them" "it")))))
e9b81433 3591 (error "Couldn't find documentation for %s" command))))
f0a8a3f1 3592
02c9f5cd 3593;;;###autoload (put 'Info-goto-emacs-key-command-node 'info-file "emacs")
f0a8a3f1
RM
3594;;;###autoload
3595(defun Info-goto-emacs-key-command-node (key)
30db89f9
EZ
3596 "Go to the node in the Emacs manual which describes the command bound to KEY.
3597KEY is a string.
fdf4b680 3598Interactively, if the binding is `execute-extended-command', a command is read.
30db89f9 3599The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
3600or in another manual found via COMMAND's `info-file' property or
3601the variable `Info-file-list-for-emacs'."
ea33749b 3602 (interactive "kFind documentation for key: ")
f0a8a3f1
RM
3603 (let ((command (key-binding key)))
3604 (cond ((null command)
9e5c2f50 3605 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
3606 ((and (interactive-p)
3607 (eq command 'execute-extended-command))
3608 (Info-goto-emacs-command-node
3609 (read-command "Find documentation for command: ")))
3610 (t
3611 (Info-goto-emacs-command-node command)))))
552775bd 3612\f
efb21aab
RS
3613(defvar Info-next-link-keymap
3614 (let ((keymap (make-sparse-keymap)))
3615 (define-key keymap [header-line mouse-1] 'Info-next)
3616 (define-key keymap [header-line mouse-2] 'Info-next)
3617 (define-key keymap [header-line down-mouse-1] 'ignore)
3618 (define-key keymap [mouse-2] 'Info-next)
3c1498ac 3619 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3620 keymap)
3621 "Keymap to put on the Next link in the text or the header line.")
3622
3623(defvar Info-prev-link-keymap
3624 (let ((keymap (make-sparse-keymap)))
3625 (define-key keymap [header-line mouse-1] 'Info-prev)
3626 (define-key keymap [header-line mouse-2] 'Info-prev)
3627 (define-key keymap [header-line down-mouse-1] 'ignore)
3628 (define-key keymap [mouse-2] 'Info-prev)
3c1498ac 3629 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3630 keymap)
3631 "Keymap to put on the Prev link in the text or the header line.")
3632
efb21aab
RS
3633(defvar Info-up-link-keymap
3634 (let ((keymap (make-sparse-keymap)))
3635 (define-key keymap [header-line mouse-1] 'Info-up)
3636 (define-key keymap [header-line mouse-2] 'Info-up)
3637 (define-key keymap [header-line down-mouse-1] 'ignore)
3638 (define-key keymap [mouse-2] 'Info-up)
3c1498ac 3639 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3640 keymap)
3641 "Keymap to put on the Up link in the text or the header line.")
3642
552775bd 3643(defun Info-fontify-node ()
8a7757f6
JL
3644 "Fontify the node."
3645 (save-excursion
3646 (let* ((inhibit-read-only t)
3647 (case-fold-search t)
3648 paragraph-markers
3649 (not-fontified-p ; the node hasn't already been fontified
3650 (not (let ((where (next-property-change (point-min))))
3651 (and where (not (= where (point-max)))))))
3652 (fontify-visited-p ; visited nodes need to be re-fontified
3653 (and Info-fontify-visited-nodes
3654 ;; Don't take time to refontify visited nodes in huge nodes
e14105f6
TTN
3655 (< (- (point-max) (point-min)) Info-fontify-maximum-menu-size)))
3656 rbeg rend)
8a7757f6
JL
3657
3658 ;; Fontify header line
3659 (goto-char (point-min))
3660 (when (and not-fontified-p (looking-at "^\\(File: [^,: \t]+,?[ \t]+\\)?"))
3661 (goto-char (match-end 0))
3662 (while (looking-at "[ \t]*\\([^:, \t\n]+\\):[ \t]+\\([^:,\t\n]+\\),?")
3663 (goto-char (match-end 0))
3664 (let* ((nbeg (match-beginning 2))
3665 (nend (match-end 2))
3666 (tbeg (match-beginning 1))
3667 (tag (match-string 1)))
99315976 3668 (if (string-equal (downcase tag) "node")
8a7757f6
JL
3669 (put-text-property nbeg nend 'font-lock-face 'info-header-node)
3670 (put-text-property nbeg nend 'font-lock-face 'info-header-xref)
3671 (put-text-property tbeg nend 'mouse-face 'highlight)
3672 (put-text-property tbeg nend
3673 'help-echo
3c1498ac 3674 (concat "mouse-2: Go to node "
8a7757f6
JL
3675 (buffer-substring nbeg nend)))
3676 ;; Always set up the text property keymap.
3677 ;; It will either be used in the buffer
3678 ;; or copied in the header line.
99315976
JL
3679 (put-text-property
3680 tbeg nend 'keymap
3681 (cond
3682 ((string-equal (downcase tag) "prev") Info-prev-link-keymap)
3683 ((string-equal (downcase tag) "next") Info-next-link-keymap)
3684 ((string-equal (downcase tag) "up" ) Info-up-link-keymap))))))
8a7757f6
JL
3685 (when Info-use-header-line
3686 (goto-char (point-min))
de22f49c
SM
3687 (let* ((header-end (line-end-position))
3688 (header
3689 ;; If we find neither Next: nor Prev: link, show the entire
3690 ;; node header. Otherwise, don't show the File: and Node:
3691 ;; parts, to avoid wasting precious space on information that
3692 ;; is available in the mode line.
3693 (if (re-search-forward
3694 "\\(next\\|up\\|prev[ious]*\\): "
3695 header-end t)
3696 (progn
3697 (goto-char (match-beginning 1))
3698 (buffer-substring (point) header-end))
3699 (if (re-search-forward "node:[ \t]*[^ \t]+[ \t]*"
3700 header-end t)
8a7757f6 3701 (concat "No next, prev or up links -- "
de22f49c
SM
3702 (buffer-substring (point) header-end))
3703 (buffer-substring (point) header-end)))))
8a7757f6 3704 (put-text-property (point-min) (1+ (point-min))
69672704
JL
3705 'header-line
3706 (replace-regexp-in-string
3707 "%"
3708 ;; Preserve text properties on duplicated `%'.
3709 (lambda (s) (concat s s)) header))
8a7757f6
JL
3710 ;; Hide the part of the first line
3711 ;; that is in the header, if it is just part.
3712 (unless (bobp)
3713 ;; Hide the punctuation at the end, too.
3714 (skip-chars-backward " \t,")
3715 (put-text-property (point) header-end 'invisible t)))))
3716
3717 ;; Fontify titles
3718 (goto-char (point-min))
de22f49c
SM
3719 (when (and font-lock-mode not-fontified-p)
3720 (while (and (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*\\*+\\|==+\\|--+\\|\\.\\.+\\)$"
3721 nil t)
3722 ;; Only consider it as an underlined title if the ASCII
3723 ;; underline has the same size as the text. A typical
3724 ;; counter example is when a continuation "..." is alone
3725 ;; on a line.
4dacf0ff
JL
3726 (= (string-width (match-string 1))
3727 (string-width (match-string 2))))
8a7757f6
JL
3728 (let* ((c (preceding-char))
3729 (face
a335c06e
MB
3730 (cond ((= c ?*) 'info-title-1)
3731 ((= c ?=) 'info-title-2)
3732 ((= c ?-) 'info-title-3)
3733 (t 'info-title-4))))
8a7757f6
JL
3734 (put-text-property (match-beginning 1) (match-end 1)
3735 'font-lock-face face))
3736 ;; This is a serious problem for trying to handle multiple
3737 ;; frame types at once. We want this text to be invisible
3738 ;; on frames that can display the font above.
3739 (when (memq (framep (selected-frame)) '(x pc w32 mac))
3740 (add-text-properties (1- (match-beginning 2)) (match-end 2)
3741 '(invisible t front-sticky nil rear-nonsticky t)))))
3742
3743 ;; Fontify cross references
3744 (goto-char (point-min))
3745 (when (or not-fontified-p fontify-visited-p)
3746 (while (re-search-forward "\\(\\*Note[ \n\t]+\\)\\([^:]*\\)\\(:[ \t]*\\([^.,:(]*\\)\\(\\(([^)]*)\\)[^.,:]*\\)?[,:]?\n?\\)" nil t)
3747 (let ((start (match-beginning 0))
3748 (next (point))
3749 other-tag)
3750 (when not-fontified-p
3751 (when Info-hide-note-references
fc11ddc8
JL
3752 (when (not (eq Info-hide-note-references 'hide))
3753 ;; *Note is often used where *note should have been
3754 (goto-char start)
3755 (skip-syntax-backward " ")
d75baf12
RS
3756 (when (memq (char-before) '(?\( ?\[ ?\{))
3757 ;; Check whether the paren is preceded by
3758 ;; an end of sentence
3759 (skip-syntax-backward " ("))
fc11ddc8 3760 (setq other-tag
e8e6694b
RS
3761 (cond ((save-match-data (looking-back "\\<see"))
3762 "")
3763 ((memq (char-before) '(nil ?\. ?! ??))
fc11ddc8 3764 "See ")
d75baf12
RS
3765 ((save-match-data
3766 (save-excursion
3767 (search-forward "\n\n" start t)))
3768 "See ")
3769 (t "see "))))
8a7757f6
JL
3770 (goto-char next)
3771 (add-text-properties
3772 (match-beginning 1)
3773 (or (save-match-data
3774 ;; Don't hide \n after *Note
3775 (let ((start1 (match-beginning 1)))
3776 (if (string-match "\n" (match-string 1))
3777 (+ start1 (match-beginning 0)))))
3778 (match-end 1))
fc11ddc8 3779 (if other-tag
8a7757f6
JL
3780 `(display ,other-tag front-sticky nil rear-nonsticky t)
3781 '(invisible t front-sticky nil rear-nonsticky t))))
3782 (add-text-properties
3783 (match-beginning 2) (match-end 2)
3784 (list
3785 'help-echo (if (or (match-end 5)
3786 (not (equal (match-string 4) "")))
3787 (concat "mouse-2: go to " (or (match-string 5)
3788 (match-string 4)))
3789 "mouse-2: go to this node")
3790 'mouse-face 'highlight)))
3791 (when (or not-fontified-p fontify-visited-p)
e14105f6
TTN
3792 (setq rbeg (match-beginning 2)
3793 rend (match-end 2))
3794 (put-text-property
3795 rbeg rend
3796 'font-lock-face
3797 ;; Display visited nodes in a different face
3798 (if (and Info-fontify-visited-nodes
3799 (save-match-data
3800 (let* ((node (replace-regexp-in-string
3801 "^[ \t]+" ""
3802 (replace-regexp-in-string
3803 "[ \t\n]+" " "
3804 (or (match-string 5)
3805 (and (not (equal (match-string 4) ""))
3806 (match-string 4))
3807 (match-string 2)))))
d8fbba22
CY
3808 (external-link-p
3809 (string-match "(\\([^)]+\\))\\([^)]*\\)" node))
3810 (file (if external-link-p
3811 (file-name-nondirectory
3812 (match-string 1 node))
3813 Info-current-file))
e14105f6
TTN
3814 (hl Info-history-list)
3815 res)
d8fbba22
CY
3816 (if external-link-p
3817 (setq node (if (equal (match-string 2 node) "")
e14105f6
TTN
3818 "Top"
3819 (match-string 2 node))))
d590dbd2
CY
3820 (while hl
3821 (if (and (string-equal node (nth 1 (car hl)))
d8fbba22
CY
3822 (string-equal
3823 file (if external-link-p
3824 (file-name-nondirectory
3825 (caar hl))
3826 (caar hl))))
d590dbd2
CY
3827 (setq res (car hl) hl nil)
3828 (setq hl (cdr hl))))
e14105f6
TTN
3829 res))) 'info-xref-visited 'info-xref))
3830 ;; For multiline ref, unfontify newline and surrounding whitespace
3831 (save-excursion
3832 (goto-char rbeg)
3833 (save-match-data
3834 (while (re-search-forward "\\s-*\n\\s-*" rend t nil)
3835 (remove-text-properties (match-beginning 0)
3836 (match-end 0)
3837 '(font-lock-face t))))))
8a7757f6
JL
3838 (when not-fontified-p
3839 (when (memq Info-hide-note-references '(t hide))
3840 (add-text-properties (match-beginning 3) (match-end 3)
3841 '(invisible t front-sticky nil rear-nonsticky t))
3842 ;; Unhide the file name of the external reference in parens
89b064a9 3843 (if (and (match-string 6) (not (eq Info-hide-note-references 'hide)))
8a7757f6
JL
3844 (remove-text-properties (match-beginning 6) (match-end 6)
3845 '(invisible t front-sticky nil rear-nonsticky t)))
3846 ;; Unhide newline because hidden newlines cause too long lines
3847 (save-match-data
89b064a9
JL
3848 (let ((beg3 (match-beginning 3))
3849 (end3 (match-end 3)))
3850 (if (and (string-match "\n[ \t]*" (match-string 3))
3851 (not (save-match-data
3852 (save-excursion
3853 (goto-char (1+ end3))
3854 (looking-at "[.)]*$")))))
3855 (remove-text-properties (+ beg3 (match-beginning 0))
3856 (+ beg3 (match-end 0))
8a7757f6
JL
3857 '(invisible t front-sticky nil rear-nonsticky t))))))
3858 (when (and Info-refill-paragraphs Info-hide-note-references)
3859 (push (set-marker (make-marker) start)
3860 paragraph-markers))))))
3861
3862 ;; Refill paragraphs (experimental feature)
3863 (when (and not-fontified-p
3864 Info-refill-paragraphs
3865 paragraph-markers)
3866 (let ((fill-nobreak-invisible t)
3867 (fill-individual-varying-indent nil)
3868 (paragraph-start "\f\\|[ \t]*[-*]\\|[ \t]*$")
3869 (paragraph-separate ".*\\.[ \t]*\n[ \t]\\|[ \t]*[-*]\\|[ \t\f]*$")
3870 (adaptive-fill-mode nil))
3871 (goto-char (point-max))
a73db543
SM
3872 (dolist (m paragraph-markers)
3873 (when (< m (point))
3874 (goto-char m)
3875 (beginning-of-line)
3876 (let ((beg (point)))
3877 (when (zerop (forward-paragraph))
3878 (fill-individual-paragraphs beg (point) nil nil)
3879 (goto-char beg))))
3880 (set-marker m nil))))
8a7757f6
JL
3881
3882 ;; Fontify menu items
3883 (goto-char (point-min))
3884 (when (and (or not-fontified-p fontify-visited-p)
3885 (search-forward "\n* Menu:" nil t)
141b49f5 3886 (not (Info-index-node))
8a7757f6
JL
3887 ;; Don't take time to annotate huge menus
3888 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
3889 (let ((n 0)
3890 cont)
3891 (while (re-search-forward
1ecc5922
RS
3892 (concat "^\\* Menu:\\|\\(?:^\\* +\\(" Info-menu-entry-name-re "\\)\\(:"
3893 Info-node-spec-re "\\([ \t]*\\)\\)\\)")
8a7757f6 3894 nil t)
1ecc5922
RS
3895 (when (match-beginning 1)
3896 (when not-fontified-p
3897 (setq n (1+ n))
3898 (if (and (<= n 9) (zerop (% n 3))) ; visual aids to help with 1-9 keys
3899 (put-text-property (match-beginning 0)
3900 (1+ (match-beginning 0))
185cff95 3901 'font-lock-face 'info-menu-star)))
1ecc5922
RS
3902 (when not-fontified-p
3903 (add-text-properties
3904 (match-beginning 1) (match-end 1)
3905 (list
3906 'help-echo (if (and (match-end 3)
3907 (not (equal (match-string 3) "")))
3908 (concat "mouse-2: go to " (match-string 3))
3909 "mouse-2: go to this node")
3910 'mouse-face 'highlight)))
3911 (when (or not-fontified-p fontify-visited-p)
a73db543 3912 (put-text-property
1ecc5922 3913 (match-beginning 1) (match-end 1)
a73db543
SM
3914 'font-lock-face
3915 ;; Display visited menu items in a different face
3916 (if (and Info-fontify-visited-nodes
3917 (save-match-data
d8fbba22
CY
3918 (let* ((node (if (equal (match-string 3) "")
3919 (match-string 1)
3920 (match-string 3)))
3921 (external-link-p
3922 (string-match "(\\([^)]+\\))\\([^)]*\\)" node))
3923 (file (if external-link-p
3924 (file-name-nondirectory
3925 (match-string 1 node))
3926 Info-current-file))
3927 (hl Info-history-list)
3928 res)
3929 (if external-link-p
3930 (setq node (if (equal (match-string 2 node) "")
a73db543
SM
3931 "Top"
3932 (match-string 2 node))))
d590dbd2
CY
3933 (while hl
3934 (if (and (string-equal node (nth 1 (car hl)))
d8fbba22
CY
3935 (string-equal
3936 file (if external-link-p
3937 (file-name-nondirectory (caar hl))
3938 (caar hl))))
d590dbd2
CY
3939 (setq res (car hl) hl nil)
3940 (setq hl (cdr hl))))
a73db543 3941 res))) 'info-xref-visited 'info-xref)))
1ecc5922
RS
3942 (when (and not-fontified-p (memq Info-hide-note-references '(t hide)))
3943 (put-text-property (match-beginning 2) (1- (match-end 6))
3944 'invisible t)
3945 ;; Unhide the file name in parens
3946 (if (and (match-end 4) (not (eq (char-after (match-end 4)) ?.)))
3947 (remove-text-properties (match-beginning 4) (match-end 4)
3948 '(invisible t)))
3949 ;; We need a stretchable space like :align-to but with
3950 ;; a minimum value.
3951 (put-text-property (1- (match-end 6)) (match-end 6) 'display
3952 (if (>= 22 (- (match-end 1)
3953 (match-beginning 0)))
3954 '(space :align-to 24)
3955 '(space :width 2)))
3956 (setq cont (looking-at "."))
3957 (while (and (= (forward-line 1) 0)
3958 (looking-at "\\([ \t]+\\)[^*\n]"))
3959 (put-text-property (match-beginning 1) (1- (match-end 1))
3960 'invisible t)
3961 (put-text-property (1- (match-end 1)) (match-end 1)
3962 'display
3963 (if cont
3964 '(space :align-to 26)
3965 '(space :align-to 24)))
3966 (setq cont t)))))))
8a7757f6
JL
3967
3968 ;; Fontify menu headers
3969 ;; Add the face `info-menu-header' to any header before a menu entry
3970 (goto-char (point-min))
3971 (when (and not-fontified-p (re-search-forward "^\\* Menu:" nil t))
3972 (put-text-property (match-beginning 0) (match-end 0)
3973 'font-lock-face 'info-menu-header)
3974 (while (re-search-forward "\n\n\\([^*\n ].*\\)\n\n?[*]" nil t)
3975 (put-text-property (match-beginning 1) (match-end 1)
3976 'font-lock-face 'info-menu-header)))
3977
836bb478
JL
3978 ;; Hide index line numbers
3979 (goto-char (point-min))
141b49f5 3980 (when (and not-fontified-p (Info-index-node))
836bb478
JL
3981 (while (re-search-forward "[ \t\n]*(line +[0-9]+)" nil t)
3982 (put-text-property (match-beginning 0) (match-end 0)
3983 'invisible t)))
3984
8a7757f6
JL
3985 ;; Fontify http and ftp references
3986 (goto-char (point-min))
3987 (when not-fontified-p
3988 (while (re-search-forward "[hf]t?tp://[^ \t\n\"`({<>})']+" nil t)
3989 (add-text-properties (match-beginning 0) (match-end 0)
3990 '(font-lock-face info-xref
3991 mouse-face highlight
3992 help-echo "mouse-2: go to this URL"))))
3993
3994 (set-buffer-modified-p nil))))
c5fe2ff1 3995\f
3cb6768f
EL
3996;;; Speedbar support:
3997;; These functions permit speedbar to display the "tags" in the
185cff95 3998;; current Info node.
96ee3f29 3999(eval-when-compile (require 'speedbar))
3cb6768f 4000
96ee3f29
KH
4001(defvar Info-speedbar-key-map nil
4002 "Keymap used when in the info display mode.")
3cb6768f 4003
96ee3f29
KH
4004(defun Info-install-speedbar-variables ()
4005 "Install those variables used by speedbar to enhance Info."
4006 (if Info-speedbar-key-map
4007 nil
4008 (setq Info-speedbar-key-map (speedbar-make-specialized-keymap))
4009
4010 ;; Basic tree features
4011 (define-key Info-speedbar-key-map "e" 'speedbar-edit-line)
4012 (define-key Info-speedbar-key-map "\C-m" 'speedbar-edit-line)
4013 (define-key Info-speedbar-key-map "+" 'speedbar-expand-line)
4014 (define-key Info-speedbar-key-map "-" 'speedbar-contract-line)
4015 )
4016
4017 (speedbar-add-expansion-list '("Info" Info-speedbar-menu-items
4018 Info-speedbar-key-map
4019 Info-speedbar-hierarchy-buttons)))
3cb6768f
EL
4020
4021(defvar Info-speedbar-menu-items
96ee3f29
KH
4022 '(["Browse Node" speedbar-edit-line t]
4023 ["Expand Node" speedbar-expand-line
4024 (save-excursion (beginning-of-line)
4025 (looking-at "[0-9]+: *.\\+. "))]
4026 ["Contract Node" speedbar-contract-line
4027 (save-excursion (beginning-of-line)
4028 (looking-at "[0-9]+: *.-. "))]
4029 )
3cb6768f
EL
4030 "Additional menu-items to add to speedbar frame.")
4031
96ee3f29
KH
4032;; Make sure our special speedbar major mode is loaded
4033(if (featurep 'speedbar)
4034 (Info-install-speedbar-variables)
4035 (add-hook 'speedbar-load-hook 'Info-install-speedbar-variables))
4036
4037;;; Info hierarchy display method
4038;;;###autoload
4039(defun Info-speedbar-browser ()
185cff95 4040 "Initialize speedbar to display an Info node browser.
96ee3f29
KH
4041This will add a speedbar major display mode."
4042 (interactive)
4043 (require 'speedbar)
4044 ;; Make sure that speedbar is active
4045 (speedbar-frame-mode 1)
4046 ;; Now, throw us into Info mode on speedbar.
4047 (speedbar-change-initial-expansion-list "Info")
4048 )
4049
4050(defun Info-speedbar-hierarchy-buttons (directory depth &optional node)
4051 "Display an Info directory hierarchy in speedbar.
4052DIRECTORY is the current directory in the attached frame.
4053DEPTH is the current indentation depth.
4054NODE is an optional argument that is used to represent the
4055specific node to expand."
4056 (if (and (not node)
4057 (save-excursion (goto-char (point-min))
1bcedb3b
RS
4058 (let ((case-fold-search t))
4059 (looking-at "Info Nodes:"))))
96ee3f29
KH
4060 ;; Update our "current node" maybe?
4061 nil
4062 ;; We cannot use the generic list code, that depends on all leaves
4063 ;; being known at creation time.
4064 (if (not node)
4065 (speedbar-with-writable (insert "Info Nodes:\n")))
24bbdbef
CY
4066 (let ((completions nil))
4067 (speedbar-select-attached-frame)
5fdc7997
EL
4068 (save-window-excursion
4069 (setq completions
4070 (Info-speedbar-fetch-file-nodes (or node '"(dir)top"))))
222a6c9b 4071 (select-frame (speedbar-current-frame))
96ee3f29
KH
4072 (if completions
4073 (speedbar-with-writable
dde7f979 4074 (dolist (completion completions)
96ee3f29 4075 (speedbar-make-tag-line 'bracket ?+ 'Info-speedbar-expand-node
dde7f979
SM
4076 (cdr completion)
4077 (car completion)
96ee3f29 4078 'Info-speedbar-goto-node
dde7f979
SM
4079 (cdr completion)
4080 'info-xref depth))
96ee3f29
KH
4081 t)
4082 nil))))
399c88ad 4083
96ee3f29 4084(defun Info-speedbar-goto-node (text node indent)
d1268e52 4085 "When user clicks on TEXT, go to an info NODE.
96ee3f29 4086The INDENT level is ignored."
24bbdbef 4087 (speedbar-select-attached-frame)
760d5cb3
GM
4088 (let* ((buff (or (get-buffer "*info*")
4089 (progn (info) (get-buffer "*info*"))))
4090 (bwin (get-buffer-window buff 0)))
4091 (if bwin
4092 (progn
4093 (select-window bwin)
4094 (raise-frame (window-frame bwin)))
4095 (if speedbar-power-click
4096 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
24bbdbef 4097 (speedbar-select-attached-frame)
760d5cb3 4098 (switch-to-buffer buff)))
dde7f979
SM
4099 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" node))
4100 (error "Invalid node %s" node)
4101 (Info-find-node (match-string 1 node) (match-string 2 node))
760d5cb3
GM
4102 ;; If we do a find-node, and we were in info mode, restore
4103 ;; the old default method. Once we are in info mode, it makes
4104 ;; sense to return to whatever method the user was using before.
4105 (if (string= speedbar-initial-expansion-list-name "Info")
4106 (speedbar-change-initial-expansion-list
4107 speedbar-previously-used-expansion-list-name)))))
96ee3f29
KH
4108
4109(defun Info-speedbar-expand-node (text token indent)
4110 "Expand the node the user clicked on.
4111TEXT is the text of the button we clicked on, a + or - item.
4112TOKEN is data related to this node (NAME . FILE).
4113INDENT is the current indentation depth."
4114 (cond ((string-match "+" text) ;we have to expand this file
4115 (speedbar-change-expand-button-char ?-)
4116 (if (speedbar-with-writable
760d5cb3
GM
4117 (save-excursion
4118 (end-of-line) (forward-char 1)
4119 (Info-speedbar-hierarchy-buttons nil (1+ indent) token)))
96ee3f29
KH
4120 (speedbar-change-expand-button-char ?-)
4121 (speedbar-change-expand-button-char ??)))
4122 ((string-match "-" text) ;we have to contract this node
4123 (speedbar-change-expand-button-char ?+)
4124 (speedbar-delete-subblock indent))
4125 (t (error "Ooops... not sure what to do")))
4126 (speedbar-center-buffer-smartly))
4127
4128(defun Info-speedbar-fetch-file-nodes (nodespec)
4129 "Fetch the subnodes from the info NODESPEC.
db90d225 4130NODESPEC is a string of the form: (file)node."
96ee3f29
KH
4131 (save-excursion
4132 ;; Set up a buffer we can use to fake-out Info.
357e6045 4133 (set-buffer (get-buffer-create " *info-browse-tmp*"))
96ee3f29
KH
4134 (if (not (equal major-mode 'Info-mode))
4135 (Info-mode))
4136 ;; Get the node into this buffer
dde7f979
SM
4137 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" nodespec))
4138 (error "Invalid node specification %s" nodespec)
4139 (Info-find-node (match-string 1 nodespec) (match-string 2 nodespec)))
96ee3f29
KH
4140 ;; Scan the created buffer
4141 (goto-char (point-min))
4142 (let ((completions nil)
1bcedb3b 4143 (case-fold-search t)
96ee3f29
KH
4144 (thisfile (progn (string-match "^(\\([^)]+\\))" nodespec)
4145 (match-string 1 nodespec))))
4146 ;; Always skip the first one...
4147 (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
4148 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
4149 (let ((name (match-string 1)))
dde7f979
SM
4150 (push (cons name
4151 (if (looking-at " *\\(([^)]+)[^.\n]+\\)\\.")
4152 (match-string 1)
4153 (if (looking-at " *\\(([^)]+)\\)\\.")
4154 (concat (match-string 1) "Top")
4155 (concat "(" thisfile ")"
4156 (if (looking-at " \\([^.]+\\).")
4157 (match-string 1)
4158 name)))))
4159 completions)))
96ee3f29
KH
4160 (nreverse completions))))
4161
4162;;; Info mode node listing
24bbdbef 4163;; This is called by `speedbar-add-localized-speedbar-support'
3cb6768f
EL
4164(defun Info-speedbar-buttons (buffer)
4165 "Create a speedbar display to help navigation in an Info file.
4166BUFFER is the buffer speedbar is requesting buttons for."
96ee3f29 4167 (if (save-excursion (goto-char (point-min))
1bcedb3b
RS
4168 (let ((case-fold-search t))
4169 (not (looking-at "Info Nodes:"))))
96ee3f29 4170 (erase-buffer))
24bbdbef 4171 (Info-speedbar-hierarchy-buttons nil 0))
49116ac0 4172
6270bbe7
JL
4173(dolist (mess '("^First node in file$"
4174 "^No `.*' in index$"
4175 "^No cross-reference named"
4176 "^No cross.references in this node$"
185cff95 4177 "^No current Info node$"
48e5b471 4178 "^No menu in this node$"
6270bbe7
JL
4179 "^No more items in menu$"
4180 "^No more nodes$"
4181 "^No pointer \\(?:forward\\|backward\\) from this node$"
4182 "^No previous `i' command$"
4183 "^No previous items in menu$"
4184 "^No previous nodes$"
4185 "^No such item in menu$"
4186 "^No such node or anchor"
4187 "^Node has no"
4188 "^Point neither on reference nor in menu item description$"
4189 "^This is the \\(?:first\\|last\\) Info node you looked at$"
4190 search-failed))
48e5b471
DL
4191 (add-to-list 'debug-ignored-errors mess))
4192
f3a45db0
LH
4193;;;; Desktop support
4194
4195(defun Info-desktop-buffer-misc-data (desktop-dirname)
4196 "Auxiliary information to be saved in desktop file."
c9ce1e40
JL
4197 (if (not (member Info-current-file '("apropos" "history" "toc")))
4198 (list Info-current-file Info-current-node)))
f3a45db0 4199
f3a45db0
LH
4200(defun Info-restore-desktop-buffer (desktop-buffer-file-name
4201 desktop-buffer-name
4202 desktop-buffer-misc)
185cff95 4203 "Restore an Info buffer specified in a desktop file."
f3a45db0
LH
4204 (let ((first (nth 0 desktop-buffer-misc))
4205 (second (nth 1 desktop-buffer-misc)))
4206 (when (and first second)
c9ce1e40
JL
4207 (when desktop-buffer-name
4208 (set-buffer (get-buffer-create desktop-buffer-name))
4209 (Info-mode))
2a3f604d 4210 (Info-find-node first second)
f3a45db0
LH
4211 (current-buffer))))
4212
9e7357b0
AS
4213(add-to-list 'desktop-buffer-mode-handlers
4214 '(Info-mode . Info-restore-desktop-buffer))
80123c9e 4215
49116ac0
JB
4216(provide 'info)
4217
83040cb0 4218;; arch-tag: f2480fe2-2139-40c1-a49b-6314991164ac
1a06eabd 4219;;; info.el ends here