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