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