2008-12-18 Carsten Dominik <dominik@science.uva.nl>
[bpt/emacs.git] / lisp / info.el
CommitLineData
55535639 1;;; info.el --- info package for Emacs
e5167999 2
83040cb0 3;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
e1af69b4
GM
4;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5;; Free Software Foundation, Inc.
3a801d0c 6
e5167999 7;; Maintainer: FSF
fd7fa35a 8;; Keywords: help
e5167999 9
a384cab3
JB
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
a384cab3 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
a384cab3
JB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a384cab3 24
e5167999
ER
25;;; Commentary:
26
185cff95 27;; Note that nowadays we expect Info files to be made using makeinfo.
921e5fe6
TTN
28;; In particular we make these assumptions:
29;; - a menu item MAY contain colons but not colon-space ": "
30;; - a menu item ending with ": " (but not ":: ") is an index entry
31;; - a node name MAY NOT contain a colon
505b68d5
TTN
32;; This distinction is to support indexing of computer programming
33;; language terms that may contain ":" but not ": ".
e5167999
ER
34
35;;; Code:
36
43f8b275 37(eval-when-compile (require 'jka-compr) (require 'cl))
bc6262c8 38
ded3e3d8 39(defgroup info nil
fc039bdc 40 "Info subsystem."
ded3e3d8
RS
41 :group 'help
42 :group 'docs)
43
44
a384cab3 45(defvar Info-history nil
185cff95 46 "Stack of Info nodes user has visited.
db90d225 47Each element of the stack is a list (FILENAME NODENAME BUFFERPOS).")
a384cab3 48
424b9d3e 49(defvar Info-history-forward nil
185cff95 50 "Stack of Info nodes user has visited with `Info-history-back' command.
db90d225 51Each element of the stack is a list (FILENAME NODENAME BUFFERPOS).")
424b9d3e 52
8a7757f6 53(defvar Info-history-list nil
185cff95 54 "List of all Info nodes user has visited.
db90d225 55Each element of the list is a list (FILENAME NODENAME).")
8a7757f6 56
ded3e3d8 57(defcustom Info-enable-edit nil
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
JL
1009 ((numberp Info-point-loc)
1010 (forward-line (1- Info-point-loc))
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)
1818 "\\b")
d457219d
JL
1819 bound noerror count
1820 (unless isearch-forward 'backward))
1821 (Info-search (if isearch-regexp string (regexp-quote string))
1822 bound noerror count
98db7a38
JB
1823 (unless isearch-forward 'backward)))
1824 (point))
98e77be5
JL
1825 (let ((isearch-search-fun-function nil))
1826 (isearch-search-fun))))
0b02cda9
JL
1827
1828(defun Info-isearch-wrap ()
d457219d
JL
1829 (if Info-isearch-search
1830 (if Info-isearch-initial-node
1831 (progn
1832 (if isearch-forward (Info-top-node) (Info-final-node))
1833 (goto-char (if isearch-forward (point-min) (point-max))))
1834 (setq Info-isearch-initial-node Info-current-node)
1835 (setq isearch-wrapped nil))
0b02cda9
JL
1836 (goto-char (if isearch-forward (point-min) (point-max)))))
1837
1838(defun Info-isearch-push-state ()
1839 `(lambda (cmd)
bec275ed 1840 (Info-isearch-pop-state cmd ',Info-current-file ',Info-current-node)))
0b02cda9
JL
1841
1842(defun Info-isearch-pop-state (cmd file node)
bec275ed
JL
1843 (or (and (equal Info-current-file file)
1844 (equal Info-current-node node))
0b02cda9
JL
1845 (progn (Info-find-node file node) (sit-for 0))))
1846
d457219d
JL
1847(defun Info-isearch-start ()
1848 (setq Info-isearch-initial-node nil))
0ec3382b 1849
a2c72e6c 1850(defun Info-isearch-filter-predicate (beg-found found)
0ec3382b
JL
1851 "Skip invisible text, node header line and Tag Table node."
1852 (save-match-data
1853 (let ((backward (< found beg-found)))
1854 (not
1855 (or
1856 (if backward
1857 (or (text-property-not-all found beg-found 'invisible nil)
1858 (text-property-not-all found beg-found 'display nil))
1859 (or (text-property-not-all beg-found found 'invisible nil)
1860 (text-property-not-all beg-found found 'display nil)))
1861 ;; Skip node header line
1862 (and (save-excursion (forward-line -1)
1863 (looking-at "\^_"))
1864 (forward-line (if backward -1 1)))
1865 ;; Skip Tag Table node
1866 (save-excursion
1867 (and (search-backward "\^_" nil t)
1868 (looking-at
1869 "\^_\n\\(Tag Table\\|Local Variables\\)"))))))))
1870
a384cab3 1871\f
a384cab3 1872(defun Info-extract-pointer (name &optional errorname)
fdf4b680
DL
1873 "Extract the value of the node-pointer named NAME.
1874If there is none, use ERRORNAME in the error message;
c60c12be
SM
1875if ERRORNAME is nil, just return nil."
1876 ;; Bind this in case the user sets it to nil.
1bcedb3b
RS
1877 (let ((case-fold-search t))
1878 (save-excursion
c60c12be
SM
1879 (goto-char (point-min))
1880 (let ((bound (point)))
1881 (forward-line 1)
1882 (cond ((re-search-backward
1883 (concat name ":" (Info-following-node-name-re)) bound t)
bec275ed 1884 (match-string-no-properties 1))
c60c12be
SM
1885 ((not (eq errorname t))
1886 (error "Node has no %s"
1887 (capitalize (or errorname name)))))))))
1888
1889(defun Info-following-node-name-re (&optional allowedchars)
1890 "Return a regexp matching a node name.
fdf4b680 1891ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
c60c12be
SM
1892saying which chars may appear in the node name.
1893Submatch 1 is the complete node name.
1894Submatch 2 if non-nil is the parenthesized file name part of the node name.
1895Submatch 3 is the local part of the node name.
1896End of submatch 0, 1, and 3 are the same, so you can safely concat."
1897 (concat "[ \t]*" ;Skip leading space.
1898 "\\(\\(([^)]+)\\)?" ;Node name can start with a file name.
1899 "\\([" (or allowedchars "^,\t\n") "]*" ;Any number of allowed chars.
1900 "[" (or allowedchars "^,\t\n") " ]" ;The last char can't be a space.
1901 "\\|\\)\\)")) ;Allow empty node names.
a384cab3 1902
718e6bc7
RS
1903;;; For compatibility; other files have used this name.
1904(defun Info-following-node-name ()
1905 (and (looking-at (Info-following-node-name-re))
bec275ed 1906 (match-string-no-properties 1)))
718e6bc7 1907
a384cab3
JB
1908(defun Info-next ()
1909 "Go to the next node of this node."
1910 (interactive)
222a6c9b
CY
1911 ;; In case another window is currently selected
1912 (save-window-excursion
1913 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1914 (Info-goto-node (Info-extract-pointer "next"))))
a384cab3
JB
1915
1916(defun Info-prev ()
1917 "Go to the previous node of this node."
1918 (interactive)
222a6c9b
CY
1919 ;; In case another window is currently selected
1920 (save-window-excursion
1921 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1922 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous"))))
a384cab3 1923
5dab2fb4
RS
1924(defun Info-up (&optional same-file)
1925 "Go to the superior node of this node.
1926If SAME-FILE is non-nil, do not move to a different Info file."
a384cab3 1927 (interactive)
222a6c9b
CY
1928 ;; In case another window is currently selected
1929 (save-window-excursion
1930 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
1931 (let ((old-node Info-current-node)
1932 (old-file Info-current-file)
1933 (node (Info-extract-pointer "up")) p)
d590dbd2 1934 (and same-file
222a6c9b
CY
1935 (string-match "^(" node)
1936 (error "Up node is in another Info file"))
1937 (Info-goto-node node)
1938 (setq p (point))
1939 (goto-char (point-min))
bec275ed
JL
1940 (if (and (stringp old-file)
1941 (search-forward "\n* Menu:" nil t)
222a6c9b
CY
1942 (re-search-forward
1943 (if (string-equal old-node "Top")
1944 (concat "\n\\*[^:]+: +(" (file-name-nondirectory old-file) ")")
1945 (concat "\n\\* +\\(" (regexp-quote old-node)
1946 ":\\|[^:]+: +" (regexp-quote old-node) "\\)"))
1947 nil t))
1948 (progn (beginning-of-line) (if (looking-at "^\\* ") (forward-char 2)))
1949 (goto-char p)
1950 (Info-restore-point Info-history)))))
a384cab3 1951
424b9d3e
JL
1952(defun Info-history-back ()
1953 "Go back in the history to the last node visited."
a384cab3
JB
1954 (interactive)
1955 (or Info-history
1956 (error "This is the first Info node you looked at"))
424b9d3e
JL
1957 (let ((history-forward
1958 (cons (list Info-current-file Info-current-node (point))
1959 Info-history-forward))
1960 filename nodename opoint)
a384cab3
JB
1961 (setq filename (car (car Info-history)))
1962 (setq nodename (car (cdr (car Info-history))))
1963 (setq opoint (car (cdr (cdr (car Info-history)))))
1964 (setq Info-history (cdr Info-history))
1965 (Info-find-node filename nodename)
1966 (setq Info-history (cdr Info-history))
424b9d3e
JL
1967 (setq Info-history-forward history-forward)
1968 (goto-char opoint)))
1969
1970(defalias 'Info-last 'Info-history-back)
1971
1972(defun Info-history-forward ()
1973 "Go forward in the history of visited nodes."
1974 (interactive)
1975 (or Info-history-forward
1976 (error "This is the last Info node you looked at"))
1977 (let ((history-forward (cdr Info-history-forward))
1978 filename nodename opoint)
1979 (setq filename (car (car Info-history-forward)))
1980 (setq nodename (car (cdr (car Info-history-forward))))
1981 (setq opoint (car (cdr (cdr (car Info-history-forward)))))
1982 (Info-find-node filename nodename)
1983 (setq Info-history-forward history-forward)
a384cab3
JB
1984 (goto-char opoint)))
1985
48e5b471 1986;;;###autoload
a384cab3
JB
1987(defun Info-directory ()
1988 "Go to the Info directory node."
1989 (interactive)
1990 (Info-find-node "dir" "top"))
1991\f
8a7757f6 1992(defun Info-history ()
89b064a9 1993 "Go to a node with a menu of visited nodes."
8a7757f6
JL
1994 (interactive)
1995 (let ((curr-file Info-current-file)
1996 (curr-node Info-current-node)
1997 p)
89b064a9
JL
1998 (with-current-buffer (get-buffer-create " *info-history*")
1999 (let ((inhibit-read-only t))
2000 (erase-buffer)
2001 (goto-char (point-min))
ea99d5c8 2002 (insert "\n\^_\nFile: history, Node: Top, Up: (dir)\n\n")
89b064a9
JL
2003 (insert "Recently Visited Nodes\n**********************\n\n")
2004 (insert "* Menu:\n\n")
2005 (let ((hl (delete '("history" "Top") Info-history-list)))
2006 (while hl
2007 (let ((file (nth 0 (car hl)))
2008 (node (nth 1 (car hl))))
bec275ed
JL
2009 (if (and (equal file curr-file)
2010 (equal node curr-node))
89b064a9 2011 (setq p (point)))
bec275ed
JL
2012 (if (stringp file)
2013 (insert "* " node ": ("
2014 (propertize (or (file-name-directory file) "") 'invisible t)
2015 (file-name-nondirectory file)
2016 ")" node ".\n")))
89b064a9 2017 (setq hl (cdr hl))))))
bec275ed 2018 (Info-find-node 'history "Top")
8a7757f6
JL
2019 (goto-char (or p (point-min)))))
2020
8a7757f6 2021(defun Info-toc ()
bc224645
JL
2022 "Go to a node with table of contents of the current Info file.
2023Table of contents is created from the tree structure of menus."
8a7757f6 2024 (interactive)
bec275ed
JL
2025 (if (stringp Info-current-file)
2026 (let ((curr-file (substring-no-properties Info-current-file))
2027 (curr-node (substring-no-properties Info-current-node))
2028 p)
2029 (with-current-buffer (get-buffer-create " *info-toc*")
2030 (let ((inhibit-read-only t)
586f9284 2031 (node-list (Info-toc-nodes curr-file)))
bec275ed
JL
2032 (erase-buffer)
2033 (goto-char (point-min))
2034 (insert "\n\^_\nFile: toc, Node: Top, Up: (dir)\n\n")
2035 (insert "Table of Contents\n*****************\n\n")
2036 (insert "*Note Top: (" curr-file ")Top.\n")
2037 (Info-insert-toc
586f9284 2038 (nth 3 (assoc "Top" node-list)) ; get Top nodes
bec275ed
JL
2039 node-list 0 curr-file))
2040 (if (not (bobp))
2041 (let ((Info-hide-note-references 'hide)
2042 (Info-fontify-visited-nodes nil))
2043 (Info-mode)
2044 (setq Info-current-file 'toc Info-current-node "Top")
2045 (goto-char (point-min))
2046 (narrow-to-region (or (re-search-forward "\n[\^_\f]\n" nil t)
2047 (point-min))
2048 (point-max))
2049 (Info-fontify-node)
2050 (widen)))
2051 (goto-char (point-min))
2052 (if (setq p (search-forward (concat "*Note " curr-node ":") nil t))
2053 (setq p (- p (length curr-node) 2))))
2054 (Info-find-node 'toc "Top")
2055 (goto-char (or p (point-min))))))
8a7757f6 2056
89b064a9 2057(defun Info-insert-toc (nodes node-list level curr-file)
8a7757f6
JL
2058 "Insert table of contents with references to nodes."
2059 (let ((section "Top"))
2060 (while nodes
2061 (let ((node (assoc (car nodes) node-list)))
586f9284
JL
2062 (unless (member (nth 2 node) (list nil section))
2063 (insert (setq section (nth 2 node)) "\n"))
8a7757f6 2064 (insert (make-string level ?\t))
89b064a9 2065 (insert "*Note " (car nodes) ": (" curr-file ")" (car nodes) ".\n")
586f9284 2066 (Info-insert-toc (nth 3 node) node-list (1+ level) curr-file)
8a7757f6
JL
2067 (setq nodes (cdr nodes))))))
2068
2069(defun Info-build-toc (file)
2070 "Build table of contents from menus of Info FILE and its subfiles."
8a7757f6 2071 (with-temp-buffer
ea99d5c8
JL
2072 (let* ((file (and (stringp file) (Info-find-file file)))
2073 (default-directory (or (and (stringp file)
2074 (file-name-directory file))
141b49f5 2075 default-directory))
ea99d5c8 2076 (main-file (and (stringp file) file))
141b49f5
JL
2077 (sections '(("Top" "Top")))
2078 nodes subfiles)
2079 (while (or main-file subfiles)
586f9284 2080 ;; (or main-file (message "Searching subfile %s..." (car subfiles)))
8a7757f6 2081 (erase-buffer)
141b49f5
JL
2082 (info-insert-file-contents (or main-file (car subfiles)))
2083 (goto-char (point-min))
8a7757f6
JL
2084 (while (and (search-forward "\n\^_\nFile:" nil 'move)
2085 (search-forward "Node: " nil 'move))
586f9284
JL
2086 (let* ((nodename (substring-no-properties (Info-following-node-name)))
2087 (bound (- (or (save-excursion (search-forward "\n\^_" nil t))
2088 (point-max)) 2))
2089 (upnode (and (re-search-forward
2090 (concat "Up:" (Info-following-node-name-re))
2091 bound t)
2092 (match-string-no-properties 1)))
2093 (section "Top")
2094 menu-items)
2095 (when (string-match "(" upnode) (setq upnode nil))
141b49f5 2096 (when (and (not (Info-index-node nodename file))
8a7757f6
JL
2097 (re-search-forward "^\\* Menu:" bound t))
2098 (forward-line 1)
2099 (beginning-of-line)
2100 (setq bound (or (and (equal nodename "Top")
2101 (save-excursion
2102 (re-search-forward
2103 "^[ \t-]*The Detailed Node Listing" nil t)))
2104 bound))
2105 (while (< (point) bound)
2106 (cond
2107 ;; Menu item line
2108 ((looking-at "^\\* +[^:]+:")
2109 (beginning-of-line)
2110 (forward-char 2)
2111 (let ((menu-node-name (substring-no-properties
2112 (Info-extract-menu-node-name))))
2113 (setq menu-items (cons menu-node-name menu-items))
2114 (if (equal nodename "Top")
2115 (setq sections
2116 (cons (list menu-node-name section) sections)))))
2117 ;; Other non-empty strings in the Top node are section names
2118 ((and (equal nodename "Top")
2119 (looking-at "^\\([^ \t\n*=.-][^:\n]*\\)"))
2120 (setq section (match-string-no-properties 1))))
2121 (forward-line 1)
2122 (beginning-of-line)))
586f9284 2123 (setq nodes (cons (list nodename upnode
8a7757f6
JL
2124 (cadr (assoc nodename sections))
2125 (nreverse menu-items))
2126 nodes))
2127 (goto-char bound)))
141b49f5 2128 (if main-file
8a7757f6
JL
2129 (save-excursion
2130 (goto-char (point-min))
2131 (if (search-forward "\n\^_\nIndirect:" nil t)
2132 (let ((bound (save-excursion (search-forward "\n\^_" nil t))))
2133 (while (re-search-forward "^\\(.*\\): [0-9]+$" bound t)
2134 (setq subfiles (cons (match-string-no-properties 1)
2135 subfiles)))))
2136 (setq subfiles (nreverse subfiles)
141b49f5 2137 main-file nil))
8a7757f6
JL
2138 (setq subfiles (cdr subfiles))))
2139 (message "")
2140 (nreverse nodes))))
586f9284
JL
2141
2142(defvar Info-toc-nodes nil
2143 "Alist of cached parent-children node information in visited Info files.
2144Each element is (FILE (NODE-NAME PARENT SECTION CHILDREN) ...)
2145where PARENT is the parent node extracted from the Up pointer,
2146SECTION is the section name in the Top node where this node is placed,
2147CHILDREN is a list of child nodes extracted from the node menu.")
2148
2149(defun Info-toc-nodes (file)
2150 "Return a node list of Info FILE with parent-children information.
2151This information is cached in the variable `Info-toc-nodes' with the help
2152of the function `Info-build-toc'."
2153 (or file (setq file Info-current-file))
2154 (or (assoc file Info-toc-nodes)
2155 ;; Skip virtual Info files
2156 (and (or (not (stringp file))
2157 (member file '("dir" apropos history toc)))
2158 (push (cons file nil) Info-toc-nodes))
2159 ;; Scan the entire manual and cache the result in Info-toc-nodes
2160 (let ((nodes (Info-build-toc file)))
2161 (push (cons file nodes) Info-toc-nodes)
2162 nodes)
2163 ;; If there is an error, still add nil to the cache
2164 (push (cons file nil) Info-toc-nodes))
2165 (cdr (assoc file Info-toc-nodes)))
2166
8a7757f6
JL
2167\f
2168(defun Info-follow-reference (footnotename &optional fork)
fdf4b680 2169 "Follow cross reference named FOOTNOTENAME to the node it refers to.
8a7757f6
JL
2170FOOTNOTENAME may be an abbreviation of the reference name.
2171If FORK is non-nil (interactively with a prefix arg), show the node in
185cff95 2172a new Info buffer. If FORK is a string, it is the name to use for the
8a7757f6 2173new buffer."
a384cab3
JB
2174 (interactive
2175 (let ((completion-ignore-case t)
1bcedb3b 2176 (case-fold-search t)
67bc89ab 2177 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 2178 (save-excursion
67bc89ab
RS
2179 ;; Store end and beginning of line.
2180 (end-of-line)
2181 (setq eol (point))
2182 (beginning-of-line)
2183 (setq bol (point))
2184
a384cab3 2185 (goto-char (point-min))
8a7757f6 2186 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 2187 (setq str (match-string-no-properties 1))
a384cab3
JB
2188 ;; See if this one should be the default.
2189 (and (null default)
1f179e27 2190 (<= (match-beginning 0) start-point)
a384cab3
JB
2191 (<= start-point (point))
2192 (setq default t))
67bc89ab
RS
2193 ;; See if this one should be the alternate default.
2194 (and (null alt-default)
2195 (and (<= bol (match-beginning 0))
2196 (<= (point) eol))
2197 (setq alt-default t))
a384cab3
JB
2198 (setq i 0)
2199 (while (setq i (string-match "[ \n\t]+" str i))
2200 (setq str (concat (substring str 0 i) " "
2201 (substring str (match-end 0))))
2202 (setq i (1+ i)))
2203 ;; Record as a completion and perhaps as default.
2204 (if (eq default t) (setq default str))
67bc89ab 2205 (if (eq alt-default t) (setq alt-default str))
ec6d29af 2206 ;; Don't add this string if it's a duplicate.
c60c12be
SM
2207 (or (assoc-string str completions t)
2208 (push str completions))))
67bc89ab
RS
2209 ;; If no good default was found, try an alternate.
2210 (or default
2211 (setq default alt-default))
2212 ;; If only one cross-reference found, then make it default.
2213 (if (eq (length completions) 1)
c60c12be 2214 (setq default (car completions)))
a384cab3 2215 (if completions
b0ebdfe5 2216 (let ((input (completing-read (if default
968b7671 2217 (concat
5b76833f
RF
2218 "Follow reference named (default "
2219 default "): ")
b0ebdfe5
RS
2220 "Follow reference named: ")
2221 completions nil t)))
2222 (list (if (equal input "")
8a7757f6 2223 default input) current-prefix-arg))
a384cab3 2224 (error "No cross-references in this node"))))
ebf8f7e1
RS
2225
2226 (unless footnotename
2227 (error "No reference was specified"))
2228
dde7f979 2229 (let (target i (str (concat "\\*note " (regexp-quote footnotename)))
1bcedb3b 2230 (case-fold-search t))
a384cab3
JB
2231 (while (setq i (string-match " " str i))
2232 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
2233 (setq i (+ i 6)))
2234 (save-excursion
8a7757f6
JL
2235 ;; Move point to the beginning of reference if point is on reference
2236 (or (looking-at "\\*note[ \n\t]+")
2237 (and (looking-back "\\*note[ \n\t]+")
2238 (goto-char (match-beginning 0)))
2239 (if (and (save-excursion
2240 (goto-char (+ (point) 5)) ; skip a possible *note
2241 (re-search-backward "\\*note[ \n\t]+" nil t)
836bb478 2242 (looking-at str))
8a7757f6
JL
2243 (<= (point) (match-end 0)))
2244 (goto-char (match-beginning 0))))
2245 ;; Go to the reference closest to point
2246 (let ((next-ref (save-excursion (and (re-search-forward str nil t)
2247 (+ (match-beginning 0) 5))))
2248 (prev-ref (save-excursion (and (re-search-backward str nil t)
2249 (+ (match-beginning 0) 5)))))
2250 (goto-char (cond ((and next-ref prev-ref)
2251 (if (< (abs (- next-ref (point)))
2252 (abs (- prev-ref (point))))
2253 next-ref prev-ref))
2254 ((or next-ref prev-ref))
2255 ((error "No cross-reference named %s" footnotename))))
2256 (setq target (Info-extract-menu-node-name t))))
a384cab3
JB
2257 (while (setq i (string-match "[ \t\n]+" target i))
2258 (setq target (concat (substring target 0 i) " "
2259 (substring target (match-end 0))))
2260 (setq i (+ i 1)))
8a7757f6 2261 (Info-goto-node target fork)))
a384cab3 2262
e057da52
SM
2263(defconst Info-menu-entry-name-re "\\(?:[^:]\\|:[^:,.;() \t\n]\\)*"
2264 ;; We allow newline because this is also used in Info-follow-reference,
2265 ;; where the xref name might be wrapped over two lines.
c60c12be
SM
2266 "Regexp that matches a menu entry name upto but not including the colon.
2267Because of ambiguities, this should be concatenated with something like
2268`:' and `Info-following-node-name-re'.")
2269
836bb478 2270(defun Info-extract-menu-node-name (&optional multi-line index-node)
a384cab3 2271 (skip-chars-forward " \t\n")
c60c12be
SM
2272 (when (looking-at (concat Info-menu-entry-name-re ":\\(:\\|"
2273 (Info-following-node-name-re
836bb478
JL
2274 (cond
2275 (index-node "^,\t\n")
2276 (multi-line "^.,\t")
2277 (t "^.,\t\n")))
2278 "\\)"
2279 (if index-node
2280 "\\.\\(?:[ \t\n]+(line +\\([0-9]+\\))\\)?"
2281 "")))
2282 (if index-node
2283 (setq Info-point-loc
2284 (if (match-beginning 5)
2285 (string-to-number (match-string 5))
bec275ed
JL
2286 (buffer-substring-no-properties
2287 (match-beginning 0) (1- (match-beginning 1)))))
02c9f5cd 2288;;; Uncomment next line to use names of cross-references in non-index nodes:
836bb478
JL
2289;;; (setq Info-point-loc
2290;;; (buffer-substring (match-beginning 0) (1- (match-beginning 1))))
2291 )
c60c12be
SM
2292 (replace-regexp-in-string
2293 "[ \n]+" " "
bec275ed 2294 (or (match-string-no-properties 2)
c60c12be 2295 ;; If the node name is the menu entry name (using `entry::').
bec275ed
JL
2296 (buffer-substring-no-properties
2297 (match-beginning 0) (1- (match-beginning 1)))))))
a384cab3 2298
e8adde3f 2299;; No one calls this.
9e5c2f50
RS
2300;;(defun Info-menu-item-sequence (list)
2301;; (while list
e8adde3f 2302;; (Info-menu (car list))
9e5c2f50 2303;; (setq list (cdr list))))
a384cab3 2304
6356e646 2305(defvar Info-complete-menu-buffer)
e4e8ee63 2306(defvar Info-complete-next-re nil)
141b49f5 2307(defvar Info-complete-nodes nil)
e4e8ee63 2308(defvar Info-complete-cache nil)
6356e646 2309
c60c12be
SM
2310(defconst Info-node-spec-re
2311 (concat (Info-following-node-name-re "^.,:") "[,:.]")
a1637357
SM
2312 "Regexp to match the text after a : until the terminating `.'.")
2313
e8adde3f 2314(defun Info-complete-menu-item (string predicate action)
2d12f5c2
SM
2315 ;; This uses two dynamically bound variables:
2316 ;; - `Info-complete-menu-buffer' which contains the buffer in which
2317 ;; is the menu of items we're trying to complete.
2318 ;; - `Info-complete-next-re' which, if non-nil, indicates that we should
2319 ;; also look for menu items in subsequent nodes as long as those
2320 ;; nodes' names match `Info-complete-next-re'. This feature is currently
141b49f5
JL
2321 ;; not used.
2322 ;; - `Info-complete-nodes' which, if non-nil, indicates that we should
2323 ;; also look for menu items in these nodes. This feature is currently
2d12f5c2 2324 ;; only used for completion in Info-index.
0defe758
LK
2325
2326 ;; Note that `Info-complete-menu-buffer' could be current already,
2327 ;; so we want to save point.
e8dab975 2328 (with-current-buffer Info-complete-menu-buffer
494858be
SM
2329 (save-excursion
2330 (let ((completion-ignore-case t)
2331 (case-fold-search t)
2332 (orignode Info-current-node)
2333 nextnode)
2334 (goto-char (point-min))
2335 (search-forward "\n* Menu:")
2336 (if (not (memq action '(nil t)))
2337 (re-search-forward
2338 (concat "\n\\* +" (regexp-quote string) ":") nil t)
2339 (let ((pattern (concat "\n\\* +\\("
2340 (regexp-quote string)
2341 Info-menu-entry-name-re "\\):" Info-node-spec-re))
2342 completions
2343 (complete-nodes Info-complete-nodes))
2344 ;; Check the cache.
2345 (if (and (equal (nth 0 Info-complete-cache) Info-current-file)
2346 (equal (nth 1 Info-complete-cache) Info-current-node)
2347 (equal (nth 2 Info-complete-cache) Info-complete-next-re)
2348 (equal (nth 5 Info-complete-cache) Info-complete-nodes)
2349 (let ((prev (nth 3 Info-complete-cache)))
2350 (eq t (compare-strings string 0 (length prev)
2351 prev 0 nil t))))
2352 ;; We can reuse the previous list.
2353 (setq completions (nth 4 Info-complete-cache))
2354 ;; The cache can't be used.
2355 (while
2356 (progn
2357 (while (re-search-forward pattern nil t)
2358 (push (match-string-no-properties 1)
2359 completions))
2360 ;; Check subsequent nodes if applicable.
2361 (or (and Info-complete-next-re
2362 (setq nextnode (Info-extract-pointer "next" t))
2363 (string-match Info-complete-next-re nextnode))
2364 (and complete-nodes
2365 (setq complete-nodes (cdr complete-nodes)
2366 nextnode (car complete-nodes)))))
2367 (Info-goto-node nextnode))
2368 ;; Go back to the start node (for the next completion).
2369 (unless (equal Info-current-node orignode)
2370 (Info-goto-node orignode))
2371 ;; Update the cache.
2372 (set (make-local-variable 'Info-complete-cache)
d5850019
JB
2373 (list Info-current-file Info-current-node
2374 Info-complete-next-re string completions
2375 Info-complete-nodes)))
2376 (if action
2377 (all-completions string completions predicate)
2378 (try-completion string completions predicate))))))))
e8adde3f
RS
2379
2380
4fceda3c 2381(defun Info-menu (menu-item &optional fork)
dc414be1
EZ
2382 "Go to the node pointed to by the menu item named (or abbreviated) MENU-ITEM.
2383The menu item should one of those listed in the current node's menu.
2384Completion is allowed, and the default menu item is the one point is on.
7210b6f5 2385If FORK is non-nil (interactively with a prefix arg), show the node in
185cff95 2386a new Info buffer. If FORK is a string, it is the name to use for the
7210b6f5 2387new buffer."
a384cab3
JB
2388 (interactive
2389 (let ((completions '())
2390 ;; If point is within a menu item, use that item as the default
2391 (default nil)
2392 (p (point))
211d6309 2393 beg
760d5cb3
GM
2394 (last nil)
2395 (case-fold-search t))
a384cab3
JB
2396 (save-excursion
2397 (goto-char (point-min))
2398 (if (not (search-forward "\n* menu:" nil t))
2399 (error "No menu in this node"))
e8adde3f
RS
2400 (setq beg (point))
2401 (and (< (point) p)
2402 (save-excursion
2403 (goto-char p)
2404 (end-of-line)
00d32b3f
SM
2405 (if (re-search-backward (concat "\n\\* +\\("
2406 Info-menu-entry-name-re
2407 "\\):") beg t)
110d4e80 2408 (setq default (match-string-no-properties 1))))))
a384cab3
JB
2409 (let ((item nil))
2410 (while (null item)
e8adde3f
RS
2411 (setq item (let ((completion-ignore-case t)
2412 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
2413 (completing-read (if default
2414 (format "Menu item (default %s): "
2415 default)
760d5cb3 2416 "Menu item: ")
e8adde3f 2417 'Info-complete-menu-item nil t)))
aea2a8da
JB
2418 ;; we rely on the fact that completing-read accepts an input
2419 ;; of "" even when the require-match argument is true and ""
2420 ;; is not a valid possibility
a384cab3
JB
2421 (if (string= item "")
2422 (if default
2423 (setq item default)
760d5cb3
GM
2424 ;; ask again
2425 (setq item nil))))
4fceda3c 2426 (list item current-prefix-arg))))
a384cab3
JB
2427 ;; there is a problem here in that if several menu items have the same
2428 ;; name you can only go to the node of the first with this command.
4fceda3c 2429 (Info-goto-node (Info-extract-menu-item menu-item) (if fork menu-item)))
399c88ad 2430
a384cab3
JB
2431(defun Info-extract-menu-item (menu-item)
2432 (setq menu-item (regexp-quote menu-item))
1bcedb3b
RS
2433 (let ((case-fold-search t))
2434 (save-excursion
760d5cb3
GM
2435 (let ((case-fold-search t))
2436 (goto-char (point-min))
2437 (or (search-forward "\n* menu:" nil t)
2438 (error "No menu in this node"))
2439 (or (re-search-forward (concat "\n\\* +" menu-item ":") nil t)
2440 (re-search-forward (concat "\n\\* +" menu-item) nil t)
2441 (error "No such item in menu"))
2442 (beginning-of-line)
2443 (forward-char 2)
141b49f5 2444 (Info-extract-menu-node-name nil (Info-index-node))))))
a384cab3
JB
2445
2446;; If COUNT is nil, use the last item in the menu.
c24d4826 2447(defun Info-extract-menu-counting (count &optional no-detail)
1bcedb3b
RS
2448 (let ((case-fold-search t))
2449 (save-excursion
c24d4826
MR
2450 (let ((case-fold-search t)
2451 (bound (when (and no-detail
2452 (re-search-forward
2453 "^[ \t-]*The Detailed Node Listing" nil t))
2454 (match-beginning 0))))
760d5cb3 2455 (goto-char (point-min))
c24d4826 2456 (or (search-forward "\n* menu:" bound t)
760d5cb3
GM
2457 (error "No menu in this node"))
2458 (if count
c24d4826 2459 (or (search-forward "\n* " bound t count)
760d5cb3 2460 (error "Too few items in menu"))
c24d4826 2461 (while (search-forward "\n* " bound t)
760d5cb3 2462 nil))
141b49f5 2463 (Info-extract-menu-node-name nil (Info-index-node))))))
a384cab3 2464
e38e7367
RM
2465(defun Info-nth-menu-item ()
2466 "Go to the node of the Nth menu item.
2467N is the digit argument used to invoke this command."
a384cab3 2468 (interactive)
e38e7367
RM
2469 (Info-goto-node
2470 (Info-extract-menu-counting
2471 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
2472
2473(defun Info-top-node ()
2474 "Go to the Top node of this file."
2475 (interactive)
2476 (Info-goto-node "Top"))
2477
2478(defun Info-final-node ()
2479 "Go to the final node in this file."
2480 (interactive)
2481 (Info-goto-node "Top")
760d5cb3
GM
2482 (let ((Info-history nil)
2483 (case-fold-search t))
c24d4826
MR
2484 ;; Go to the last node in the menu of Top. But don't delve into
2485 ;; detailed node listings.
2486 (Info-goto-node (Info-extract-menu-counting nil t))
a384cab3 2487 ;; If the last node in the menu is not last in pointer structure,
c24d4826
MR
2488 ;; move forward (but not down- or upward - see bug#1116) until we
2489 ;; can't go any farther.
2490 (while (Info-forward-node t t t) nil)
a384cab3 2491 ;; Then keep moving down to last subnode, unless we reach an index.
141b49f5 2492 (while (and (not (Info-index-node))
a384cab3
JB
2493 (save-excursion (search-forward "\n* Menu:" nil t)))
2494 (Info-goto-node (Info-extract-menu-counting nil)))))
2495
c24d4826 2496(defun Info-forward-node (&optional not-down not-up no-error)
a384cab3
JB
2497 "Go forward one node, considering all nodes as forming one sequence."
2498 (interactive)
2499 (goto-char (point-min))
2500 (forward-line 1)
760d5cb3
GM
2501 (let ((case-fold-search t))
2502 ;; three possibilities, in order of priority:
2503 ;; 1. next node is in a menu in this node (but not in an index)
2504 ;; 2. next node is next at same level
2505 ;; 3. next node is up and next
2506 (cond ((and (not not-down)
2507 (save-excursion (search-forward "\n* menu:" nil t))
141b49f5 2508 (not (Info-index-node)))
760d5cb3
GM
2509 (Info-goto-node (Info-extract-menu-counting 1))
2510 t)
6af7040d 2511 ((save-excursion (search-backward "next:" nil t))
760d5cb3
GM
2512 (Info-next)
2513 t)
c24d4826
MR
2514 ((and (not not-up)
2515 (save-excursion (search-backward "up:" nil t))
760d5cb3
GM
2516 ;; Use string-equal, not equal, to ignore text props.
2517 (not (string-equal (downcase (Info-extract-pointer "up"))
2518 "top")))
2519 (let ((old-node Info-current-node))
2520 (Info-up)
2521 (let (Info-history success)
2522 (unwind-protect
c24d4826 2523 (setq success (Info-forward-node t nil no-error))
760d5cb3
GM
2524 (or success (Info-goto-node old-node))))))
2525 (no-error nil)
2526 (t (error "No pointer forward from this node")))))
a384cab3
JB
2527
2528(defun Info-backward-node ()
2529 "Go backward one node, considering all nodes as forming one sequence."
2530 (interactive)
2531 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
760d5cb3
GM
2532 (upnode (Info-extract-pointer "up" t))
2533 (case-fold-search t))
a384cab3
JB
2534 (cond ((and upnode (string-match "(" upnode))
2535 (error "First node in file"))
2536 ((and upnode (or (null prevnode)
e90d1271
RS
2537 ;; Use string-equal, not equal,
2538 ;; to ignore text properties.
2539 (string-equal (downcase prevnode)
2540 (downcase upnode))))
a384cab3
JB
2541 (Info-up))
2542 (prevnode
2543 ;; If we move back at the same level,
2544 ;; go down to find the last subnode*.
2545 (Info-prev)
2546 (let (Info-history)
141b49f5 2547 (while (and (not (Info-index-node))
a384cab3
JB
2548 (save-excursion (search-forward "\n* Menu:" nil t)))
2549 (Info-goto-node (Info-extract-menu-counting nil)))))
2550 (t
2551 (error "No pointer backward from this node")))))
2552
2553(defun Info-exit ()
2554 "Exit Info by selecting some other buffer."
2555 (interactive)
552775bd
RS
2556 (if Info-standalone
2557 (save-buffers-kill-emacs)
4643e92f 2558 (quit-window)))
a384cab3 2559
253db917 2560(defun Info-next-menu-item ()
3a8803c0 2561 "Go to the node of the next menu item."
253db917 2562 (interactive)
fdf4b680 2563 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2564 (let* ((case-fold-search t)
2565 (node
2566 (save-excursion
2567 (forward-line -1)
2568 (search-forward "\n* menu:" nil t)
2569 (and (search-forward "\n* " nil t)
2570 (Info-extract-menu-node-name)))))
fadfb77f
RS
2571 (if node (Info-goto-node node)
2572 (error "No more items in menu"))))
253db917
ER
2573
2574(defun Info-last-menu-item ()
3a8803c0 2575 "Go to the node of the previous menu item."
253db917
ER
2576 (interactive)
2577 (save-excursion
2578 (forward-line 1)
fdf4b680 2579 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2580 (let* ((case-fold-search t)
2581 (beg (save-excursion
2582 (and (search-backward "\n* menu:" nil t)
2583 (point)))))
0a56332b
RS
2584 (or (and beg (search-backward "\n* " beg t))
2585 (error "No previous items in menu")))
2586 (Info-goto-node (save-excursion
2587 (goto-char (match-end 0))
2588 (Info-extract-menu-node-name)))))
253db917 2589
552775bd 2590(defmacro Info-no-error (&rest body)
253db917
ER
2591 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
2592
2593(defun Info-next-preorder ()
3c9179ea
RS
2594 "Go to the next subnode or the next node, or go up a level."
2595 (interactive)
2596 (cond ((Info-no-error (Info-next-menu-item)))
2597 ((Info-no-error (Info-next)))
5dab2fb4 2598 ((Info-no-error (Info-up t))
ed690657
KH
2599 ;; Since we have already gone thru all the items in this menu,
2600 ;; go up to the end of this node.
b54d0f0e
RS
2601 (goto-char (point-max))
2602 ;; Since logically we are done with the node with that menu,
fd3d78f2
JL
2603 ;; move on from it. But don't add intermediate nodes
2604 ;; to the history on recursive calls.
2605 (let (Info-history)
2606 (Info-next-preorder)))
3c9179ea
RS
2607 (t
2608 (error "No more nodes"))))
253db917
ER
2609
2610(defun Info-last-preorder ()
2611 "Go to the last node, popping up a level if there is none."
2612 (interactive)
0a56332b
RS
2613 (cond ((Info-no-error
2614 (Info-last-menu-item)
2615 ;; If we go down a menu item, go to the end of the node
2616 ;; so we can scroll back through it.
ed690657 2617 (goto-char (point-max)))
b54d0f0e
RS
2618 ;; Keep going down, as long as there are nested menu nodes.
2619 (while (Info-no-error
2620 (Info-last-menu-item)
2621 ;; If we go down a menu item, go to the end of the node
2622 ;; so we can scroll back through it.
2623 (goto-char (point-max))))
ed690657 2624 (recenter -1))
c2def7a0
MB
2625 ((and (Info-no-error (Info-extract-pointer "prev"))
2626 (not (equal (Info-extract-pointer "up")
5dab2fb4
RS
2627 (Info-extract-pointer "prev"))))
2628 (Info-no-error (Info-prev))
ed690657 2629 (goto-char (point-max))
b54d0f0e
RS
2630 (while (Info-no-error
2631 (Info-last-menu-item)
2632 ;; If we go down a menu item, go to the end of the node
2633 ;; so we can scroll back through it.
2634 (goto-char (point-max))))
ed690657 2635 (recenter -1))
5dab2fb4 2636 ((Info-no-error (Info-up t))
ed690657 2637 (goto-char (point-min))
760d5cb3
GM
2638 (let ((case-fold-search t))
2639 (or (search-forward "\n* Menu:" nil t)
2640 (goto-char (point-max)))))
ed690657 2641 (t (error "No previous nodes"))))
253db917
ER
2642
2643(defun Info-scroll-up ()
3f32dc86 2644 "Scroll one screenful forward in Info, considering all nodes as one sequence.
280d11ed 2645Once you scroll far enough in a node that its menu appears on the screen
7a53d8c8
EZ
2646but after point, the next scroll moves into its first subnode, unless
2647`Info-scroll-prefer-subnodes' is nil.
280d11ed 2648
7a53d8c8
EZ
2649When you scroll past the end of a node, that goes to the next node if
2650`Info-scroll-prefer-subnodes' is non-nil and to the first subnode otherwise;
2651if this node has no successor, it moves to the parent node's successor,
2652and so on. If `Info-scroll-prefer-subnodes' is non-nil and point is inside
2653the menu of a node, it moves to subnode indicated by the following menu
2654item. (That case won't normally result from this command, but can happen
2655in other ways.)"
280d11ed 2656
253db917 2657 (interactive)
0a56332b
RS
2658 (if (or (< (window-start) (point-min))
2659 (> (window-start) (point-max)))
2660 (set-window-start (selected-window) (point)))
760d5cb3
GM
2661 (let* ((case-fold-search t)
2662 (virtual-end (save-excursion
2663 (goto-char (point-min))
7a53d8c8
EZ
2664 (if (and Info-scroll-prefer-subnodes
2665 (search-forward "\n* Menu:" nil t))
760d5cb3
GM
2666 (point)
2667 (point-max)))))
0a56332b
RS
2668 (if (or (< virtual-end (window-start))
2669 (pos-visible-in-window-p virtual-end))
7a53d8c8
EZ
2670 (cond
2671 (Info-scroll-prefer-subnodes (Info-next-preorder))
2672 ((Info-no-error (Info-goto-node (Info-extract-menu-counting 1))))
2673 (t (Info-next-preorder)))
0a56332b 2674 (scroll-up))))
253db917 2675
8aab8422
CY
2676(defun Info-mouse-scroll-up (e)
2677 "Scroll one screenful forward in Info, using the mouse.
2678See `Info-scroll-up'."
2679 (interactive "e")
2680 (save-selected-window
2681 (if (eventp e)
2682 (select-window (posn-window (event-start e))))
2683 (Info-scroll-up)))
2684
253db917 2685(defun Info-scroll-down ()
3f32dc86 2686 "Scroll one screenful back in Info, considering all nodes as one sequence.
c2def7a0
MB
2687If point is within the menu of a node, and `Info-scroll-prefer-subnodes'
2688is non-nil, this goes to its last subnode. When you scroll past the
2689beginning of a node, that goes to the previous node or back up to the
2690parent node."
253db917 2691 (interactive)
0a56332b
RS
2692 (if (or (< (window-start) (point-min))
2693 (> (window-start) (point-max)))
2694 (set-window-start (selected-window) (point)))
760d5cb3
GM
2695 (let* ((case-fold-search t)
2696 (current-point (point))
c2def7a0
MB
2697 (virtual-end
2698 (and Info-scroll-prefer-subnodes
2699 (save-excursion
2700 (beginning-of-line)
2701 (setq current-point (point))
2702 (goto-char (point-min))
2703 (search-forward "\n* Menu:"
2704 current-point
2705 t)))))
3a8803c0 2706 (if (or virtual-end
7afe24e3 2707 (pos-visible-in-window-p (point-min) nil t))
0a56332b
RS
2708 (Info-last-preorder)
2709 (scroll-down))))
253db917 2710
8aab8422
CY
2711(defun Info-mouse-scroll-down (e)
2712 "Scroll one screenful backward in Info, using the mouse.
2713See `Info-scroll-down'."
2714 (interactive "e")
2715 (save-selected-window
2716 (if (eventp e)
2717 (select-window (posn-window (event-start e))))
2718 (Info-scroll-down)))
2719
56cda6f5 2720(defun Info-next-reference (&optional recur)
552775bd
RS
2721 "Move cursor to the next cross-reference or menu item in the node."
2722 (interactive)
4ba89826 2723 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tps?://")
1bcedb3b
RS
2724 (old-pt (point))
2725 (case-fold-search t))
552775bd
RS
2726 (or (eobp) (forward-char 1))
2727 (or (re-search-forward pat nil t)
2728 (progn
2729 (goto-char (point-min))
2730 (or (re-search-forward pat nil t)
2731 (progn
2732 (goto-char old-pt)
2733 (error "No cross references in this node")))))
8a7757f6 2734 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2735 (if (looking-at "\\* Menu:")
56cda6f5
RS
2736 (if recur
2737 (error "No cross references in this node")
185cff95
JL
2738 (Info-next-reference t))
2739 (if (looking-at "^\\* ")
2740 (forward-char 2)))))
552775bd 2741
56cda6f5 2742(defun Info-prev-reference (&optional recur)
552775bd
RS
2743 "Move cursor to the previous cross-reference or menu item in the node."
2744 (interactive)
4ba89826 2745 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tps?://")
1bcedb3b
RS
2746 (old-pt (point))
2747 (case-fold-search t))
552775bd
RS
2748 (or (re-search-backward pat nil t)
2749 (progn
2750 (goto-char (point-max))
2751 (or (re-search-backward pat nil t)
2752 (progn
2753 (goto-char old-pt)
2754 (error "No cross references in this node")))))
8a7757f6 2755 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2756 (if (looking-at "\\* Menu:")
56cda6f5
RS
2757 (if recur
2758 (error "No cross references in this node")
185cff95
JL
2759 (Info-prev-reference t))
2760 (if (looking-at "^\\* ")
2761 (forward-char 2)))))
141b49f5
JL
2762\f
2763(defvar Info-index-nodes nil
2764 "Alist of cached index node names of visited Info files.
2765Each element has the form (INFO-FILE INDEX-NODE-NAMES-LIST).")
2766
2767(defun Info-index-nodes (&optional file)
2768 "Return a list of names of all index nodes in Info FILE.
2769If FILE is omitted, it defaults to the current Info file.
50163b00
JL
2770First look in a list of cached index node names. Then scan Info
2771file and its subfiles for nodes with the index cookie. Then try
2772to find index nodes starting from the first node in the top level
2773menu whose name contains the word \"Index\", plus any immediately
2774following nodes whose names also contain the word \"Index\"."
141b49f5
JL
2775 (or file (setq file Info-current-file))
2776 (or (assoc file Info-index-nodes)
2777 ;; Skip virtual Info files
586f9284
JL
2778 (and (or (not (stringp file))
2779 (member file '("dir" apropos history toc)))
50163b00 2780 (setq Info-index-nodes (cons (cons file nil) Info-index-nodes)))
66fc722f 2781 (if (Info-file-supports-index-cookies file)
94b10022
JL
2782 ;; Find nodes with index cookie
2783 (let* ((default-directory (or (and (stringp file)
2784 (file-name-directory
2785 (setq file (Info-find-file file))))
2786 default-directory))
2787 Info-history Info-history-list Info-fontify-maximum-menu-size
2788 (main-file file) subfiles nodes node)
2789 (condition-case nil
2790 (with-temp-buffer
2791 (while (or main-file subfiles)
2792 (erase-buffer)
2793 (info-insert-file-contents (or main-file (car subfiles)))
2794 (goto-char (point-min))
2795 (while (search-forward "\0\b[index\0\b]" nil 'move)
2796 (save-excursion
2797 (re-search-backward "^\^_")
2798 (search-forward "Node: ")
2799 (setq nodes (cons (Info-following-node-name) nodes))))
2800 (if main-file
2801 (save-excursion
2802 (goto-char (point-min))
2803 (if (search-forward "\n\^_\nIndirect:" nil t)
2804 (let ((bound (save-excursion (search-forward "\n\^_" nil t))))
2805 (while (re-search-forward "^\\(.*\\): [0-9]+$" bound t)
2806 (setq subfiles (cons (match-string-no-properties 1)
2807 subfiles)))))
2808 (setq subfiles (nreverse subfiles)
2809 main-file nil))
2810 (setq subfiles (cdr subfiles)))))
2811 (error nil))
2812 (if nodes
2813 (setq nodes (nreverse nodes)
2814 Info-index-nodes (cons (cons file nodes) Info-index-nodes)))
2815 nodes)
2816 ;; Else find nodes with the word "Index" in the node name
2817 (let ((case-fold-search t)
66fc722f 2818 Info-history Info-history-list Info-fontify-maximum-menu-size Info-point-loc
94b10022
JL
2819 nodes node)
2820 (condition-case nil
2821 (with-temp-buffer
2822 (Info-mode)
2823 (Info-find-node file "Top")
2824 (when (and (search-forward "\n* menu:" nil t)
2825 (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t))
2826 (goto-char (match-beginning 1))
2827 (setq nodes (list (Info-extract-menu-node-name)))
2828 (Info-goto-node (car nodes))
2829 (while (and (setq node (Info-extract-pointer "next" t))
2830 (string-match "\\<Index\\>" node))
2831 (setq nodes (cons node nodes))
2832 (Info-goto-node node))))
2833 (error nil))
2834 (if nodes
2835 (setq nodes (nreverse nodes)
2836 Info-index-nodes (cons (cons file nodes) Info-index-nodes)))
2837 nodes))
50163b00
JL
2838 ;; If file has no index nodes, still add it to the cache
2839 (setq Info-index-nodes (cons (cons file nil) Info-index-nodes)))
141b49f5
JL
2840 (cdr (assoc file Info-index-nodes)))
2841
2842(defun Info-index-node (&optional node file)
2843 "Return non-nil value if NODE is an index node.
2844If NODE is nil, check the current Info node.
2845If FILE is nil, check the current Info file."
66fc722f 2846 (or file (setq file Info-current-file))
50163b00 2847 (if (or (and node (not (equal node Info-current-node)))
66fc722f 2848 (assoc file Info-index-nodes))
50163b00
JL
2849 (member (or node Info-current-node) (Info-index-nodes file))
2850 ;; Don't search all index nodes if request is only for the current node
2851 ;; and file is not in the cache of index nodes
66fc722f 2852 (if (Info-file-supports-index-cookies file)
94b10022
JL
2853 (save-excursion
2854 (goto-char (+ (or (save-excursion
2855 (search-backward "\n\^_" nil t))
2856 (point-min)) 2))
2857 (search-forward "\0\b[index\0\b]"
2858 (or (save-excursion
2859 (search-forward "\n\^_" nil t))
2860 (point-max)) t))
2861 (save-match-data
2862 (string-match "\\<Index\\>" (or node Info-current-node ""))))))
552775bd 2863
e4e8ee63 2864(defun Info-goto-index ()
141b49f5
JL
2865 "Go to the first index node."
2866 (let ((node (car (Info-index-nodes))))
2867 (or node (error "No index"))
2868 (Info-goto-node node)))
e4e8ee63 2869
201bc1bd 2870;;;###autoload
1143a6b0 2871(defun Info-index (topic)
4a99f153 2872 "Look up a string TOPIC in the index for this manual and go to that entry.
1143a6b0
ER
2873If there are no exact matches to the specified topic, this chooses
2874the first match which is a case-insensitive substring of a topic.
89b064a9 2875Use the \\<Info-mode-map>\\[Info-index-next] command to see the other matches.
4a99f153 2876Give an empty topic name to go to the Index node itself."
e4e8ee63
SM
2877 (interactive
2878 (list
ea71ffe8
CY
2879 (let ((completion-ignore-case t)
2880 (Info-complete-menu-buffer (clone-buffer))
141b49f5
JL
2881 (Info-complete-nodes (Info-index-nodes))
2882 (Info-history-list nil))
aebd1760
RS
2883 (if (equal Info-current-file "dir")
2884 (error "The Info directory node has no index; use m to select a manual"))
e4e8ee63
SM
2885 (unwind-protect
2886 (with-current-buffer Info-complete-menu-buffer
2887 (Info-goto-index)
2888 (completing-read "Index topic: " 'Info-complete-menu-item))
2889 (kill-buffer Info-complete-menu-buffer)))))
aebd1760
RS
2890 (if (equal Info-current-file "dir")
2891 (error "The Info directory node has no index; use m to select a manual"))
03e8d958
KS
2892 ;; Strip leading colon in topic; index format does not allow them.
2893 (if (and (stringp topic)
2894 (> (length topic) 0)
2895 (= (aref topic 0) ?:))
2896 (setq topic (substring topic 1)))
1143a6b0 2897 (let ((orignode Info-current-node)
836bb478 2898 (pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^\n]*\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?"
1143a6b0 2899 (regexp-quote topic)))
141b49f5
JL
2900 node (nodes (Info-index-nodes))
2901 (ohist-list Info-history-list)
1bcedb3b 2902 (case-fold-search t))
e700ec12 2903 (Info-goto-index)
1143a6b0
ER
2904 (or (equal topic "")
2905 (let ((matches nil)
2906 (exact nil)
e700ec12
SM
2907 ;; We bind Info-history to nil for internal node-switches so
2908 ;; that we don't put junk in the history. In the first
2909 ;; Info-goto-index call, above, we do update the history
2910 ;; because that is what the user's previous node choice into it.
c696ac76 2911 (Info-history nil)
1143a6b0
ER
2912 found)
2913 (while
2914 (progn
2915 (goto-char (point-min))
2916 (while (re-search-forward pattern nil t)
983dfbf8
SM
2917 (push (list (match-string-no-properties 1)
2918 (match-string-no-properties 2)
2919 Info-current-node
3a8803c0
JPW
2920 (string-to-number (concat "0"
2921 (match-string 3))))
983dfbf8 2922 matches))
141b49f5 2923 (setq nodes (cdr nodes) node (car nodes)))
1143a6b0
ER
2924 (Info-goto-node node))
2925 (or matches
2926 (progn
81e14cb2 2927 (Info-goto-node orignode)
920bdaab 2928 (error "No `%s' in index" topic)))
1143a6b0
ER
2929 ;; Here it is a feature that assoc is case-sensitive.
2930 (while (setq found (assoc topic matches))
2931 (setq exact (cons found exact)
2932 matches (delq found matches)))
141b49f5 2933 (setq Info-history-list ohist-list)
1143a6b0
ER
2934 (setq Info-index-alternatives (nconc exact (nreverse matches)))
2935 (Info-index-next 0)))))
2936
2937(defun Info-index-next (num)
89b064a9 2938 "Go to the next matching index item from the last \\<Info-mode-map>\\[Info-index] command."
1143a6b0
ER
2939 (interactive "p")
2940 (or Info-index-alternatives
882e61bf 2941 (error "No previous `i' command"))
1143a6b0
ER
2942 (while (< num 0)
2943 (setq num (+ num (length Info-index-alternatives))))
2944 (while (> num 0)
2945 (setq Info-index-alternatives
2946 (nconc (cdr Info-index-alternatives)
2947 (list (car Info-index-alternatives)))
2948 num (1- num)))
2949 (Info-goto-node (nth 1 (car Info-index-alternatives)))
2950 (if (> (nth 3 (car Info-index-alternatives)) 0)
836bb478 2951 (forward-line (1- (nth 3 (car Info-index-alternatives))))
760d5cb3 2952 (forward-line 3) ; don't search in headers
1143a6b0 2953 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
2954 (Info-find-index-name name)))
2955 (message "Found `%s' in %s. %s"
1143a6b0
ER
2956 (car (car Info-index-alternatives))
2957 (nth 2 (car Info-index-alternatives))
2958 (if (cdr Info-index-alternatives)
fa62821e
EZ
2959 (format "(%s total; use `%s' for next)"
2960 (length Info-index-alternatives)
2961 (key-description (where-is-internal
2962 'Info-index-next overriding-local-map
2963 t)))
1143a6b0
ER
2964 "(Only match)")))
2965
920bdaab
RS
2966(defun Info-find-index-name (name)
2967 "Move point to the place within the current node where NAME is defined."
1bcedb3b
RS
2968 (let ((case-fold-search t))
2969 (if (or (re-search-forward (format
2970 "[a-zA-Z]+: %s\\( \\|$\\)"
2971 (regexp-quote name)) nil t)
d31c6ecf
RS
2972 ;; Find a function definition with a return type.
2973 (re-search-forward (format
f2fc5b0a 2974 "[a-zA-Z]+: [a-zA-Z0-9_ *&]+ %s\\( \\|$\\)"
d31c6ecf 2975 (regexp-quote name)) nil t)
1bcedb3b
RS
2976 (search-forward (format "`%s'" name) nil t)
2977 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
2978 (search-forward
2979 (format "`%s'" (substring name 0 (match-beginning 1)))
2980 nil t))
bbe054f7
EZ
2981 (search-forward name nil t)
2982 ;; Try again without the " <1>" makeinfo can append
2983 (and (string-match "\\`\\(.*\\) <[0-9]+>\\'" name)
2984 (Info-find-index-name (match-string 1 name))))
2985 (progn (beginning-of-line) t) ;; non-nil for recursive call
1bcedb3b 2986 (goto-char (point-min)))))
920bdaab 2987
ab2f22ad
JH
2988;;;###autoload
2989(defun info-apropos (string)
2990 "Grovel indices of all known Info files on your system for STRING.
2991Build a menu of the possible matches."
2992 (interactive "sIndex apropos: ")
2993 (unless (string= string "")
836bb478 2994 (let ((pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^\n]+\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?"
ab2f22ad
JH
2995 (regexp-quote string)))
2996 (ohist Info-history)
37600b6d 2997 (ohist-list Info-history-list)
ab2f22ad
JH
2998 (current-node Info-current-node)
2999 (current-file Info-current-file)
141b49f5
JL
3000 manuals matches node nodes)
3001 (let ((Info-fontify-maximum-menu-size nil))
ab2f22ad 3002 (Info-directory)
59ad6ae5
EZ
3003 ;; current-node and current-file are nil when they invoke info-apropos
3004 ;; as the first Info command, i.e. info-apropos loads info.el. In that
3005 ;; case, we use (DIR)Top instead, to avoid signalling an error after
3006 ;; the search is complete.
3007 (when (null current-node)
3008 (setq current-file Info-current-file)
3009 (setq current-node Info-current-node))
ab2f22ad
JH
3010 (message "Searching indices...")
3011 (goto-char (point-min))
3012 (re-search-forward "\\* Menu: *\n" nil t)
7b30b20c 3013 (while (re-search-forward "\\*.*: *(\\([^)]+\\))" nil t)
9ef5b432
EZ
3014 ;; add-to-list makes sure we don't have duplicates in `manuals',
3015 ;; so that the following dolist loop runs faster.
3016 (add-to-list 'manuals (match-string 1)))
185cff95 3017 (dolist (manual (nreverse manuals))
ab2f22ad 3018 (message "Searching %s" manual)
185cff95
JL
3019 (condition-case err
3020 (if (setq nodes (Info-index-nodes (Info-find-file manual)))
141b49f5
JL
3021 (save-excursion
3022 (Info-find-node manual (car nodes))
3023 (while
3024 (progn
3025 (goto-char (point-min))
3026 (while (re-search-forward pattern nil t)
185cff95
JL
3027 (setq matches
3028 (cons (list manual
3029 (match-string-no-properties 1)
3030 (match-string-no-properties 2)
3031 (match-string-no-properties 3))
3032 matches)))
141b49f5 3033 (setq nodes (cdr nodes) node (car nodes)))
185cff95
JL
3034 (Info-goto-node node))))
3035 (error
3036 (message "%s" (if (eq (car-safe err) 'error)
3037 (nth 1 err) err))
3038 (sit-for 1 t)))))
bec275ed 3039 (Info-find-node current-file current-node)
37600b6d
JH
3040 (setq Info-history ohist
3041 Info-history-list ohist-list)
ab2f22ad
JH
3042 (message "Searching indices...done")
3043 (if (null matches)
3044 (message "No matches found")
3045 (with-current-buffer (get-buffer-create " *info-apropos*")
3046 (erase-buffer)
141b49f5 3047 (insert "\n\^_\nFile: apropos, Node: Index, Up: (dir)\n")
185cff95
JL
3048 (insert "* Menu: \nNodes whose indices contain `" string "':\n\n")
3049 (dolist (entry (nreverse matches))
5242942f
JH
3050 (insert
3051 (format "* %-38s (%s)%s.%s\n"
3052 (concat (nth 1 entry) " [" (nth 0 entry) "]:")
3053 (nth 0 entry)
3054 (nth 2 entry)
3055 (if (nth 3 entry)
3056 (concat " (line " (nth 3 entry) ")")
3057 "")))))
bec275ed 3058 (Info-find-node 'apropos "Index")
82f1aca9 3059 (setq Info-complete-cache nil)))))
ab2f22ad 3060
a384cab3
JB
3061(defun Info-undefined ()
3062 "Make command be undefined in Info."
3063 (interactive)
3064 (ding))
3065
3066(defun Info-help ()
3067 "Enter the Info tutorial."
3068 (interactive)
3069 (delete-other-windows)
3070 (Info-find-node "info"
3071 (if (< (window-height) 23)
3072 "Help-Small-Screen"
3073 "Help")))
3074
3075(defun Info-summary ()
3076 "Display a brief summary of all Info commands."
3077 (interactive)
3078 (save-window-excursion
3079 (switch-to-buffer "*Help*")
881c84c7 3080 (setq buffer-read-only nil)
a384cab3
JB
3081 (erase-buffer)
3082 (insert (documentation 'Info-mode))
9d29f94c 3083 (help-mode)
a384cab3
JB
3084 (goto-char (point-min))
3085 (let (ch flag)
3086 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
3087 (message (if flag "Type Space to see more"
3088 "Type Space to return to Info"))
fc039bdc 3089 (if (not (eq ?\s (setq ch (read-event))))
dbc4e1c1 3090 (progn (setq unread-command-events (list ch)) nil)
a384cab3 3091 flag))
552775bd
RS
3092 (scroll-up)))
3093 (bury-buffer "*Help*")))
a384cab3
JB
3094\f
3095(defun Info-get-token (pos start all &optional errorstring)
fdf4b680 3096 "Return the token around POS.
a384cab3
JB
3097POS must be somewhere inside the token
3098START is a regular expression which will match the
3099 beginning of the tokens delimited string
3100ALL is a regular expression with a single
90a715f0 3101 parenthesized subpattern which is the token to be
fdf4b680 3102 returned. E.g. '{\(.*\)}' would return any string
a384cab3 3103 enclosed in braces around POS.
fdf4b680 3104ERRORSTRING optional fourth argument, controls action on no match
a384cab3
JB
3105 nil: return nil
3106 t: beep
3107 a string: signal an error, using that string."
1bcedb3b
RS
3108 (let ((case-fold-search t))
3109 (save-excursion
3110 (goto-char pos)
3111 ;; First look for a match for START that goes across POS.
3112 (while (and (not (bobp)) (> (point) (- pos (length start)))
3113 (not (looking-at start)))
3114 (forward-char -1))
3115 ;; If we did not find one, search back for START
3116 ;; (this finds only matches that end at or before POS).
3117 (or (looking-at start)
3118 (progn
3119 (goto-char pos)
3120 (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
3121 (let (found)
3122 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
3123 (not (setq found (and (<= (match-beginning 0) pos)
3124 (> (match-end 0) pos))))))
3125 (if (and found (<= (match-beginning 0) pos)
3126 (> (match-end 0) pos))
110d4e80 3127 (match-string-no-properties 1)
1bcedb3b
RS
3128 (cond ((null errorstring)
3129 nil)
3130 ((eq errorstring t)
3131 (beep)
3132 nil)
3133 (t
3134 (error "No %s around position %d" errorstring pos))))))))
a384cab3 3135
981947af 3136(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
3137 "\\<Info-mode-map>Follow a node reference near point.
3138Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 3139At end of the node's text, moves to the next node, or up if none."
f9969361 3140 (interactive "e")
d1268e52 3141 (mouse-set-point click)
e2469835 3142 (and (not (Info-follow-nearest-node))
981947af 3143 (save-excursion (forward-line 1) (eobp))
ed690657 3144 (Info-next-preorder)))
981947af 3145
8a7757f6 3146(defun Info-follow-nearest-node (&optional fork)
d0b560c6
JB
3147 "Follow a node reference near point.
3148If point is on a reference, follow that reference. Otherwise,
04d6dfe2
R
3149if point is in a menu item description, follow that menu item.
3150
6716bfa4 3151If FORK is non-nil (interactively with a prefix arg), show the node in
04d6dfe2 3152a new Info buffer.
6716bfa4 3153If FORK is a string, it is the name to use for the new buffer."
8a7757f6
JL
3154 (interactive "P")
3155 (or (Info-try-follow-nearest-node fork)
d0b560c6
JB
3156 (when (save-excursion
3157 (search-backward "\n* menu:" nil t))
3158 (save-excursion
3159 (beginning-of-line)
3160 (while (not (or (bobp) (looking-at "[^ \t]\\|[ \t]*$")))
3161 (beginning-of-line 0))
3162 (when (looking-at "\\* +\\([^\t\n]*\\):")
3163 (Info-goto-node
8a7757f6 3164 (Info-extract-menu-item (match-string-no-properties 1)) fork)
d0b560c6 3165 t)))
2cf7c3d1
MR
3166 (and (eq this-command 'Info-mouse-follow-nearest-node)
3167 ;; Don't raise an error when mouse-1 is bound to this - it's
3168 ;; often used to simply select the window or frame.
3169 (eq 'mouse-1 (event-basic-type last-input-event)))
d0b560c6 3170 (error "Point neither on reference nor in menu item description")))
981947af
KH
3171
3172;; Common subroutine.
8a7757f6 3173(defun Info-try-follow-nearest-node (&optional fork)
8aed3f67 3174 "Follow a node reference near point. Return non-nil if successful.
7d1ac206 3175If FORK is non-nil, it is passed to `Info-goto-node'."
6af7040d 3176 (let (node)
a384cab3 3177 (cond
4ba89826 3178 ((Info-get-token (point) "[hf]t?tps?://" "[hf]t?tps?://\\([^ \t\n\"`({<>})']+\\)")
8a7757f6
JL
3179 (setq node t)
3180 (browse-url (browse-url-url-at-point)))
3181 ((setq node (Info-get-token (point) "\\*note[ \n\t]+"
3182 "\\*note[ \n\t]+\\([^:]*\\):\\(:\\|[ \n\t]*(\\)?"))
8a7757f6 3183 (Info-follow-reference node fork))
505b68d5 3184 ;; menu item: node name
b37daea4 3185 ((setq node (Info-get-token (point) "\\* +" "\\* +\\([^:]*\\)::"))
8a7757f6 3186 (Info-goto-node node fork))
836bb478 3187 ;; menu item: node name or index entry
505b68d5 3188 ((Info-get-token (point) "\\* +" "\\* +\\(.*\\): ")
631ba13e
RS
3189 (beginning-of-line)
3190 (forward-char 2)
141b49f5 3191 (setq node (Info-extract-menu-node-name nil (Info-index-node)))
8a7757f6 3192 (Info-goto-node node fork))
bc2ada62 3193 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
8a7757f6 3194 (Info-goto-node node fork))
bc2ada62 3195 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
8a7757f6 3196 (Info-goto-node node fork))
bc2ada62 3197 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
8a7757f6 3198 (Info-goto-node "Top" fork))
bc2ada62 3199 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
8a7757f6 3200 (Info-goto-node node fork)))
981947af 3201 node))
a384cab3 3202\f
fdeb777a
SM
3203(defvar Info-mode-map
3204 (let ((map (make-keymap)))
3205 (suppress-keymap map)
3206 (define-key map "." 'beginning-of-buffer)
3207 (define-key map " " 'Info-scroll-up)
3208 (define-key map "\C-m" 'Info-follow-nearest-node)
3209 (define-key map "\t" 'Info-next-reference)
3210 (define-key map "\e\t" 'Info-prev-reference)
3211 (define-key map [(shift tab)] 'Info-prev-reference)
3212 (define-key map [backtab] 'Info-prev-reference)
3213 (define-key map "1" 'Info-nth-menu-item)
3214 (define-key map "2" 'Info-nth-menu-item)
3215 (define-key map "3" 'Info-nth-menu-item)
3216 (define-key map "4" 'Info-nth-menu-item)
3217 (define-key map "5" 'Info-nth-menu-item)
3218 (define-key map "6" 'Info-nth-menu-item)
3219 (define-key map "7" 'Info-nth-menu-item)
3220 (define-key map "8" 'Info-nth-menu-item)
3221 (define-key map "9" 'Info-nth-menu-item)
3222 (define-key map "0" 'undefined)
3223 (define-key map "?" 'Info-summary)
3224 (define-key map "]" 'Info-forward-node)
3225 (define-key map "[" 'Info-backward-node)
3226 (define-key map "<" 'Info-top-node)
3227 (define-key map ">" 'Info-final-node)
3228 (define-key map "b" 'beginning-of-buffer)
3229 (define-key map "d" 'Info-directory)
3230 (define-key map "e" 'Info-edit)
3231 (define-key map "f" 'Info-follow-reference)
3232 (define-key map "g" 'Info-goto-node)
3233 (define-key map "h" 'Info-help)
3234 (define-key map "i" 'Info-index)
3235 (define-key map "l" 'Info-history-back)
3236 (define-key map "L" 'Info-history)
3237 (define-key map "m" 'Info-menu)
3238 (define-key map "n" 'Info-next)
3239 (define-key map "p" 'Info-prev)
3240 (define-key map "q" 'Info-exit)
3241 (define-key map "r" 'Info-history-forward)
3242 (define-key map "s" 'Info-search)
3243 (define-key map "S" 'Info-search-case-sensitively)
3244 ;; For consistency with Rmail.
3245 (define-key map "\M-s" 'Info-search)
3246 (define-key map "\M-n" 'clone-buffer)
3247 (define-key map "t" 'Info-top-node)
3248 (define-key map "T" 'Info-toc)
3249 (define-key map "u" 'Info-up)
3250 ;; `w' for consistency with `dired-copy-filename-as-kill'.
3251 (define-key map "w" 'Info-copy-current-node-name)
3252 (define-key map "c" 'Info-copy-current-node-name)
3253 ;; `^' for consistency with `dired-up-directory'.
3254 (define-key map "^" 'Info-up)
3255 (define-key map "," 'Info-index-next)
3256 (define-key map "\177" 'Info-scroll-down)
3257 (define-key map [mouse-2] 'Info-mouse-follow-nearest-node)
3258 (define-key map [follow-link] 'mouse-face)
3259 map)
a384cab3 3260 "Keymap containing Info commands.")
fdeb777a 3261
75a209d4
RS
3262
3263(defun Info-check-pointer (item)
fdf4b680 3264 "Non-nil if ITEM is present in this node."
75a209d4
RS
3265 (condition-case nil
3266 (Info-extract-pointer item)
3267 (error nil)))
3268
d4af987a
DL
3269(easy-menu-define
3270 Info-mode-menu Info-mode-map
185cff95 3271 "Menu for Info files."
d4af987a
DL
3272 '("Info"
3273 ["Up" Info-up :active (Info-check-pointer "up")
3274 :help "Go up in the Info tree"]
3275 ["Next" Info-next :active (Info-check-pointer "next")
3276 :help "Go to the next node"]
3277 ["Previous" Info-prev :active (Info-check-pointer "prev[ious]*")
3278 :help "Go to the previous node"]
3279 ["Backward" Info-backward-node
3280 :help "Go backward one node, considering all as a sequence"]
3281 ["Forward" Info-forward-node
3282 :help "Go forward one node, considering all as a sequence"]
7210b6f5
DL
3283 ["Beginning" beginning-of-buffer
3284 :help "Go to beginning of this node"]
d4af987a
DL
3285 ["Top" Info-top-node
3286 :help "Go to top node of file"]
3287 ["Final Node" Info-final-node
3288 :help "Go to final node in this file"]
3289 ("Menu Item" ["You should never see this" report-emacs-bug t])
3290 ("Reference" ["You should never see this" report-emacs-bug t])
3291 ["Search..." Info-search
3292 :help "Search for regular expression in this Info file"]
8a7757f6
JL
3293 ["Search Next" Info-search-next
3294 :help "Search for another occurrence of regular expression"]
d1268e52 3295 ["Go to Node..." Info-goto-node
d4af987a 3296 :help "Go to a named node"]
424b9d3e
JL
3297 ["Back in history" Info-history-back :active Info-history
3298 :help "Go back in history to the last node you were at"]
3299 ["Forward in history" Info-history-forward :active Info-history-forward
3300 :help "Go forward in history"]
8a7757f6 3301 ["History" Info-history :active Info-history-list
bc224645 3302 :help "Go to menu of visited nodes"]
8a7757f6 3303 ["Table of Contents" Info-toc
bc224645 3304 :help "Go to table of contents"]
5938202c
JL
3305 ("Index"
3306 ["Lookup a String..." Info-index
d4af987a 3307 :help "Look for a string in the index items"]
5938202c 3308 ["Next Matching Item" Info-index-next :active Info-index-alternatives
ab2f22ad 3309 :help "Look for another occurrence of previous item"]
5938202c 3310 ["Lookup a string in all indices..." info-apropos
ab2f22ad 3311 :help "Look for a string in the indices of all manuals"])
126f5d4d
MB
3312 ["Copy Node Name" Info-copy-current-node-name
3313 :help "Copy the name of the current node into the kill ring"]
8a7757f6
JL
3314 ["Clone Info buffer" clone-buffer
3315 :help "Create a twin copy of the current Info buffer."]
ec03f31e 3316 ["Exit" Info-exit :help "Stop reading Info"]))
75a209d4 3317
7210b6f5
DL
3318
3319(defvar info-tool-bar-map
b88e7560
CY
3320 (let ((map (make-sparse-keymap)))
3321 (tool-bar-local-item-from-menu 'Info-history-back "left-arrow" map Info-mode-map
3322 :rtl "right-arrow")
3323 (tool-bar-local-item-from-menu 'Info-history-forward "right-arrow" map Info-mode-map
3324 :rtl "left-arrow")
3325 (tool-bar-local-item-from-menu 'Info-prev "prev-node" map Info-mode-map
3326 :rtl "next-node")
3327 (tool-bar-local-item-from-menu 'Info-next "next-node" map Info-mode-map
3328 :rtl "prev-node")
3329 (tool-bar-local-item-from-menu 'Info-up "up-node" map Info-mode-map)
3330 (tool-bar-local-item-from-menu 'Info-top-node "home" map Info-mode-map)
3331 (tool-bar-local-item-from-menu 'Info-goto-node "jump-to" map Info-mode-map)
3332 (tool-bar-local-item-from-menu 'Info-index "index" map Info-mode-map)
3333 (tool-bar-local-item-from-menu 'Info-search "search" map Info-mode-map)
3334 (tool-bar-local-item-from-menu 'Info-exit "exit" map Info-mode-map)
3335 map))
7210b6f5 3336
75a209d4
RS
3337(defvar Info-menu-last-node nil)
3338;; Last node the menu was created for.
6d2c8e3e 3339;; Value is a list, (FILE-NAME NODE-NAME).
75a209d4
RS
3340
3341(defun Info-menu-update ()
fdf4b680 3342 "Update the Info menu for the current node."
75a209d4
RS
3343 (condition-case nil
3344 (if (or (not (eq major-mode 'Info-mode))
6d2c8e3e
RS
3345 (equal (list Info-current-file Info-current-node)
3346 Info-menu-last-node))
75a209d4
RS
3347 ()
3348 ;; Update menu menu.
3349 (let* ((Info-complete-menu-buffer (current-buffer))
3350 (items (nreverse (condition-case nil
e4e8ee63 3351 (Info-complete-menu-item "" nil t)
75a209d4 3352 (error nil))))
399c88ad 3353 entries current
75a209d4
RS
3354 (number 0))
3355 (while (and items (< number 9))
3356 (setq current (car items)
3357 items (cdr items)
3358 number (1+ number))
399c88ad 3359 (setq entries (cons `[,current
75a209d4
RS
3360 (Info-menu ,current)
3361 :keys ,(format "%d" number)]
3362 entries)))
3363 (if items
3364 (setq entries (cons ["Other..." Info-menu t] entries)))
3365 (or entries
5d5845dc 3366 (setq entries (list ["No menu" nil nil] nil :active)))
d4af987a 3367 (easy-menu-change '("Info") "Menu Item" (nreverse entries)))
75a209d4
RS
3368 ;; Update reference menu. Code stolen from `Info-follow-reference'.
3369 (let ((items nil)
399c88ad 3370 str i entries current
1bcedb3b
RS
3371 (number 0)
3372 (case-fold-search t))
75a209d4
RS
3373 (save-excursion
3374 (goto-char (point-min))
8a7757f6 3375 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 3376 (setq str (match-string 1))
75a209d4
RS
3377 (setq i 0)
3378 (while (setq i (string-match "[ \n\t]+" str i))
3379 (setq str (concat (substring str 0 i) " "
3380 (substring str (match-end 0))))
3381 (setq i (1+ i)))
3382 (setq items
3383 (cons str items))))
3384 (while (and items (< number 9))
3385 (setq current (car items)
3386 items (cdr items)
3387 number (1+ number))
399c88ad 3388 (setq entries (cons `[,current
75a209d4
RS
3389 (Info-follow-reference ,current)
3390 t]
3391 entries)))
3392 (if items
3393 (setq entries (cons ["Other..." Info-follow-reference t]
3394 entries)))
3395 (or entries
5d5845dc 3396 (setq entries (list ["No references" nil nil] nil :active)))
75a209d4
RS
3397 (easy-menu-change '("Info") "Reference" (nreverse entries)))
3398 ;; Update last seen node.
6d2c8e3e 3399 (setq Info-menu-last-node (list Info-current-file Info-current-node)))
75a209d4
RS
3400 ;; Try to avoid entering infinite beep mode in case of errors.
3401 (error (ding))))
3402
a384cab3 3403\f
50163b00 3404(defun Info-copy-current-node-name (&optional arg)
185cff95
JL
3405 "Put the name of the current Info node into the kill ring.
3406The name of the Info file is prepended to the node name in parentheses.
50163b00
JL
3407With a zero prefix arg, put the name inside a function call to `info'."
3408 (interactive "P")
126f5d4d 3409 (unless Info-current-node
185cff95 3410 (error "No current Info node"))
bec275ed 3411 (let ((node (if (stringp Info-current-file)
e56f84d7 3412 (concat "(" (file-name-nondirectory Info-current-file) ") "
bec275ed 3413 Info-current-node))))
50163b00
JL
3414 (if (zerop (prefix-numeric-value arg))
3415 (setq node (concat "(info \"" node "\")")))
bec275ed
JL
3416 (unless (stringp Info-current-file)
3417 (setq node (format "(Info-find-node '%S '%S)"
3418 Info-current-file Info-current-node)))
50163b00
JL
3419 (kill-new node)
3420 (message "%s" node)))
126f5d4d
MB
3421
3422\f
a384cab3 3423;; Info mode is suitable only for specially formatted data.
68bc119c 3424(put 'Info-mode 'mode-class 'special)
2fa8f8a4 3425(put 'Info-mode 'no-clone-indirect t)
a384cab3 3426
f4f2bca3 3427(defvar tool-bar-map)
e0385bf4 3428(defvar bookmark-make-record-function)
f4f2bca3 3429
80123c9e
LH
3430;; Autoload cookie needed by desktop.el
3431;;;###autoload
a384cab3 3432(defun Info-mode ()
fdf4b680 3433 "Info mode provides commands for browsing through the Info documentation tree.
a384cab3
JB
3434Documentation in Info is divided into \"nodes\", each of which discusses
3435one topic and contains references to other nodes which discuss related
3436topics. Info has commands to follow the references and show you other nodes.
3437
fdf4b680 3438\\<Info-mode-map>\
a384cab3 3439\\[Info-help] Invoke the Info tutorial.
6b136ab9 3440\\[Info-exit] Quit Info: reselect previously selected buffer.
a384cab3
JB
3441
3442Selecting other nodes:
21d5959f
RS
3443\\[Info-mouse-follow-nearest-node]
3444 Follow a node reference you click on.
3445 This works with menu items, cross references, and
3446 the \"next\", \"previous\" and \"up\", depending on where you click.
6b136ab9 3447\\[Info-follow-nearest-node] Follow a node reference near point, like \\[Info-mouse-follow-nearest-node].
a384cab3 3448\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 3449\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
3450\\[Info-up] Move \"up\" from this node.
3451\\[Info-menu] Pick menu item specified by name (or abbreviation).
e8042313 3452 Picking a menu item causes another node to be selected.
aea2a8da 3453\\[Info-directory] Go to the Info directory node.
8300a5b5
JL
3454\\[Info-top-node] Go to the Top node of this file.
3455\\[Info-final-node] Go to the final node in this file.
3456\\[Info-backward-node] Go backward one node, considering all nodes as forming one sequence.
3457\\[Info-forward-node] Go forward one node, considering all nodes as forming one sequence.
3458\\[Info-next-reference] Move cursor to next cross-reference or menu item.
3459\\[Info-prev-reference] Move cursor to previous cross-reference or menu item.
a384cab3 3460\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
424b9d3e
JL
3461\\[Info-history-back] Move back in history to the last node you were at.
3462\\[Info-history-forward] Move forward in history to the node you returned from after using \\[Info-history-back].
bc224645
JL
3463\\[Info-history] Go to menu of visited nodes.
3464\\[Info-toc] Go to table of contents of the current Info file.
a384cab3
JB
3465
3466Moving within a node:
177c3549 3467\\[Info-scroll-up] Normally, scroll forward a full screen.
e8042313
PJ
3468 Once you scroll far enough in a node that its menu appears on the
3469 screen but after point, the next scroll moves into its first
3470 subnode. When after all menu items (or if there is no menu),
3471 move up to the parent node.
177c3549 3472\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
e8042313
PJ
3473 already visible, try to go to the previous menu entry, or up
3474 if there is none.
399c88ad 3475\\[beginning-of-buffer] Go to beginning of node.
a384cab3 3476
a384cab3 3477Advanced commands:
a384cab3 3478\\[Info-search] Search through this Info file for specified regexp,
e8042313 3479 and select the node in which the next occurrence is found.
bc224645 3480\\[Info-search-case-sensitively] Search through this Info file for specified regexp case-sensitively.
8a7757f6 3481\\[Info-search-next] Search for another occurrence of regexp
89b064a9 3482 from a previous \\<Info-mode-map>\\[Info-search] command.
88e1d519 3483\\[isearch-forward], \\[isearch-forward-regexp] Use Isearch to search through multiple Info nodes.
9012c084 3484\\[Info-index] Search for a topic in this manual's Index and go to index entry.
8300a5b5
JL
3485\\[Info-index-next] (comma) Move to the next match from a previous \\<Info-mode-map>\\[Info-index] command.
3486\\[info-apropos] Look for a string in the indices of all manuals.
3487\\[Info-goto-node] Move to node specified by name.
3488 You may include a filename as well, as (FILENAME)NODENAME.
34891 .. 9 Pick first ... ninth item in node's menu.
3490 Every third `*' is highlighted to help pick the right number.
3491\\[Info-copy-current-node-name] Put name of current Info node in the kill ring.
3492\\[clone-buffer] Select a new cloned Info buffer in another window.
3493\\[universal-argument] \\[info] Move to new Info file with completion.
3494\\[universal-argument] N \\[info] Select Info buffer with prefix number in the name *info*<N>."
a384cab3
JB
3495 (kill-all-local-variables)
3496 (setq major-mode 'Info-mode)
3497 (setq mode-name "Info")
f73299f3 3498 (setq tab-width 8)
a384cab3 3499 (use-local-map Info-mode-map)
75a209d4 3500 (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
a384cab3
JB
3501 (set-syntax-table text-mode-syntax-table)
3502 (setq local-abbrev-table text-mode-abbrev-table)
3503 (setq case-fold-search t)
3504 (setq buffer-read-only t)
a384cab3
JB
3505 (make-local-variable 'Info-current-file)
3506 (make-local-variable 'Info-current-subfile)
3507 (make-local-variable 'Info-current-node)
3508 (make-local-variable 'Info-tag-table-marker)
c5fe2ff1
RS
3509 (setq Info-tag-table-marker (make-marker))
3510 (make-local-variable 'Info-tag-table-buffer)
3511 (setq Info-tag-table-buffer nil)
a384cab3 3512 (make-local-variable 'Info-history)
424b9d3e 3513 (make-local-variable 'Info-history-forward)
1143a6b0 3514 (make-local-variable 'Info-index-alternatives)
eb033d40
DR
3515 (if Info-use-header-line ; do not override global header lines
3516 (setq header-line-format
3517 '(:eval (get-text-property (point-min) 'header-line))))
7210b6f5 3518 (set (make-local-variable 'tool-bar-map) info-tool-bar-map)
93480d70
RS
3519 ;; This is for the sake of the invisible text we use handling titles.
3520 (make-local-variable 'line-move-ignore-invisible)
3521 (setq line-move-ignore-invisible t)
9d9d10ec 3522 (make-local-variable 'desktop-save-buffer)
ab357a65
RS
3523 (make-local-variable 'widen-automatically)
3524 (setq widen-automatically nil)
9d9d10ec 3525 (setq desktop-save-buffer 'Info-desktop-buffer-misc-data)
34d86208 3526 (add-hook 'kill-buffer-hook 'Info-kill-buffer nil t)
274bcfe0 3527 (add-hook 'clone-buffer-hook 'Info-clone-buffer nil t)
3d12b331 3528 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
d457219d 3529 (add-hook 'isearch-mode-hook 'Info-isearch-start nil t)
0b02cda9
JL
3530 (set (make-local-variable 'isearch-search-fun-function)
3531 'Info-isearch-search)
3532 (set (make-local-variable 'isearch-wrap-function)
3533 'Info-isearch-wrap)
3534 (set (make-local-variable 'isearch-push-state-function)
3535 'Info-isearch-push-state)
a2c72e6c
JL
3536 (set (make-local-variable 'isearch-filter-predicate)
3537 'Info-isearch-filter-predicate)
0b02cda9
JL
3538 (set (make-local-variable 'search-whitespace-regexp)
3539 Info-search-whitespace-regexp)
e56f84d7
TTN
3540 (set (make-local-variable 'revert-buffer-function)
3541 'Info-revert-buffer-function)
a384cab3 3542 (Info-set-mode-line)
e0385bf4
KF
3543 (set (make-local-variable 'bookmark-make-record-function)
3544 'Info-bookmark-make-record)
d39f149d 3545 (run-mode-hooks 'Info-mode-hook))
a384cab3 3546
34d86208
JL
3547;; When an Info buffer is killed, make sure the associated tags buffer
3548;; is killed too.
3549(defun Info-kill-buffer ()
3550 (and (eq major-mode 'Info-mode)
3551 Info-tag-table-buffer
3552 (kill-buffer Info-tag-table-buffer)))
3553
274bcfe0
RS
3554;; Placed on `clone-buffer-hook'.
3555(defun Info-clone-buffer ()
4fceda3c
SM
3556 (when (bufferp Info-tag-table-buffer)
3557 (setq Info-tag-table-buffer
4ce5c223
SM
3558 (with-current-buffer Info-tag-table-buffer (clone-buffer))))
3559 (let ((m Info-tag-table-marker))
3560 (when (markerp m)
15533ae2 3561 (setq Info-tag-table-marker
4ce5c223 3562 (if (and (marker-position m) (bufferp Info-tag-table-buffer))
15533ae2
SM
3563 (with-current-buffer Info-tag-table-buffer
3564 (copy-marker (marker-position m)))
3565 (make-marker))))))
4fceda3c 3566
7210b6f5
DL
3567(defvar Info-edit-map (let ((map (make-sparse-keymap)))
3568 (set-keymap-parent map text-mode-map)
3569 (define-key map "\C-c\C-c" 'Info-cease-edit)
3570 map)
a384cab3 3571 "Local keymap used within `e' command of Info.")
a384cab3
JB
3572
3573;; Info-edit mode is suitable only for specially formatted data.
68bc119c 3574(put 'Info-edit-mode 'mode-class 'special)
a384cab3
JB
3575
3576(defun Info-edit-mode ()
3577 "Major mode for editing the contents of an Info node.
a426b157 3578Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
3579which returns to Info mode for browsing.
3580\\{Info-edit-map}"
a384cab3
JB
3581 (use-local-map Info-edit-map)
3582 (setq major-mode 'Info-edit-mode)
3583 (setq mode-name "Info Edit")
3584 (kill-local-variable 'mode-line-buffer-identification)
3585 (setq buffer-read-only nil)
2c609f53 3586 (force-mode-line-update)
e82c28f9 3587 (buffer-enable-undo (current-buffer))
d39f149d 3588 (run-mode-hooks 'Info-edit-mode-hook))
e82c28f9
KH
3589
3590(defun Info-edit ()
3591 "Edit the contents of this Info node.
3592Allowed only if variable `Info-enable-edit' is non-nil."
3593 (interactive)
3594 (or Info-enable-edit
185cff95 3595 (error "Editing Info nodes is not enabled"))
e82c28f9 3596 (Info-edit-mode)
8ab3e50b 3597 (message "%s" (substitute-command-keys
760d5cb3 3598 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
3599
3600(defun Info-cease-edit ()
3601 "Finish editing Info node; switch back to Info proper."
3602 (interactive)
3603 ;; Do this first, so nothing has changed if user C-g's at query.
3604 (and (buffer-modified-p)
3605 (y-or-n-p "Save the file? ")
3606 (save-buffer))
3607 (use-local-map Info-mode-map)
3608 (setq major-mode 'Info-mode)
3609 (setq mode-name "Info")
3610 (Info-set-mode-line)
3611 (setq buffer-read-only t)
2c609f53 3612 (force-mode-line-update)
a384cab3
JB
3613 (and (marker-position Info-tag-table-marker)
3614 (buffer-modified-p)
3615 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 3616\f
f88ab1c9 3617(defvar Info-file-list-for-emacs
02c9f5cd 3618 '("ediff" "eudc" "forms" "gnus" "info" ("Info" . "info") ("mh" . "mh-e")
906b3163
EZ
3619 "sc" "message" ("dired" . "dired-x") "viper" "vip" "idlwave"
3620 ("c" . "ccmode") ("c++" . "ccmode") ("objc" . "ccmode")
3621 ("java" . "ccmode") ("idl" . "ccmode") ("pike" . "ccmode")
af718538
EZ
3622 ("skeleton" . "autotype") ("auto-insert" . "autotype")
3623 ("copyright" . "autotype") ("executable" . "autotype")
3624 ("time-stamp" . "autotype") ("quickurl" . "autotype")
3625 ("tempo" . "autotype") ("hippie-expand" . "autotype")
807378ad
EZ
3626 ("cvs" . "pcl-cvs") ("ada" . "ada-mode") "calc"
3627 ("calcAlg" . "calc") ("calcDigit" . "calc") ("calcVar" . "calc")
906b3163
EZ
3628 "ebrowse" "eshell" "cl" "reftex" "speedbar" "widget" "woman"
3629 ("mail-header" . "emacs-mime") ("mail-content" . "emacs-mime")
3630 ("mail-encode" . "emacs-mime") ("mail-decode" . "emacs-mime")
3631 ("rfc2045" . "emacs-mime")
3632 ("rfc2231" . "emacs-mime") ("rfc2047" . "emacs-mime")
3633 ("rfc2045" . "emacs-mime") ("rfc1843" . "emacs-mime")
3634 ("ietf-drums" . "emacs-mime") ("quoted-printable" . "emacs-mime")
3635 ("binhex" . "emacs-mime") ("uudecode" . "emacs-mime")
3636 ("mailcap" . "emacs-mime") ("mm" . "emacs-mime")
3637 ("mml" . "emacs-mime"))
f88ab1c9
RS
3638 "List of Info files that describe Emacs commands.
3639An element can be a file name, or a list of the form (PREFIX . FILE)
3640where PREFIX is a name prefix and FILE is the file to look in.
3641If the element is just a file name, the file name also serves as the prefix.")
3642
f0a8a3f1 3643(defun Info-find-emacs-command-nodes (command)
f88ab1c9 3644 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
3645The `info-file' property of COMMAND says which Info manual to search.
3646If COMMAND has no property, the variable `Info-file-list-for-emacs'
3647defines heuristics for which Info manual to try.
fdf4b680 3648The locations are of the format used in `Info-history', i.e.
02c9f5cd
JL
3649\(FILENAME NODENAME BUFFERPOS\), where BUFFERPOS is the line number
3650in the first element of the returned list (which is treated specially in
3651`Info-goto-emacs-command-node'), and 0 for the rest elements of a list."
3652 (let ((where '()) line-number
b37daea4 3653 (cmd-desc (concat "^\\* +" (regexp-quote (symbol-name command))
f254e21f 3654 "\\( <[0-9]+>\\)?:\\s *\\(.*\\)\\."
02c9f5cd 3655 "\\(?:[ \t\n]+(line +\\([0-9]+\\))\\)?"))
f88ab1c9 3656 (info-file "emacs")) ;default
185cff95 3657 ;; Determine which Info file this command is documented in.
f88ab1c9
RS
3658 (if (get command 'info-file)
3659 (setq info-file (get command 'info-file))
3660 ;; If it doesn't say explicitly, test its name against
3661 ;; various prefixes that we know.
3662 (let ((file-list Info-file-list-for-emacs))
3663 (while file-list
3664 (let* ((elt (car file-list))
3665 (name (if (consp elt)
3666 (car elt)
3667 elt))
3668 (file (if (consp elt) (cdr elt) elt))
cc3f6c32 3669 (case-fold-search nil)
f57b2cd8 3670 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
3671 "\\(\\'\\|-\\)")))
3672 (if (string-match regexp (symbol-name command))
3673 (setq info-file file file-list nil))
3674 (setq file-list (cdr file-list))))))
218c2cc7 3675 (Info-find-node info-file "Top")
218c2cc7
EZ
3676 ;; Bind Info-history to nil, to prevent the index nodes from
3677 ;; getting into the node history.
3678 (let ((Info-history nil)
141b49f5
JL
3679 (Info-history-list nil)
3680 node (nodes (Info-index-nodes)))
3681 (Info-goto-node (car nodes))
218c2cc7
EZ
3682 (while
3683 (progn
3684 (goto-char (point-min))
3685 (while (re-search-forward cmd-desc nil t)
3686 (setq where
3687 (cons (list Info-current-file
3688 (match-string-no-properties 2)
760d5cb3 3689 0)
02c9f5cd
JL
3690 where))
3691 (setq line-number (and (match-beginning 3)
3692 (string-to-number (match-string 3)))))
141b49f5 3693 (and (setq nodes (cdr nodes) node (car nodes))))
218c2cc7 3694 (Info-goto-node node)))
02c9f5cd
JL
3695 (if (and line-number where)
3696 (cons (list (nth 0 (car where)) (nth 1 (car where)) line-number)
3697 (cdr where))
3698 where)))
f0a8a3f1 3699
02c9f5cd 3700;;;###autoload (put 'Info-goto-emacs-command-node 'info-file "emacs")
f0a8a3f1
RM
3701;;;###autoload
3702(defun Info-goto-emacs-command-node (command)
e0568e86 3703 "Go to the Info node in the Emacs manual for command COMMAND.
30db89f9 3704The command is found by looking up in Emacs manual's indices
f57b2cd8 3705or in another manual found via COMMAND's `info-file' property or
1ed6e0c3
JPW
3706the variable `Info-file-list-for-emacs'.
3707COMMAND must be a symbol or string."
f0a8a3f1 3708 (interactive "CFind documentation for command: ")
fafb00dc
MY
3709 ;; If command is given as a string, convert it to a symbol.
3710 (if (stringp command)
1ed6e0c3 3711 (setq command (intern command)))
f0a8a3f1
RM
3712 (or (commandp command)
3713 (signal 'wrong-type-argument (list 'commandp command)))
3714 (let ((where (Info-find-emacs-command-nodes command)))
3715 (if where
3716 (let ((num-matches (length where)))
3717 ;; Get Info running, and pop to it in another window.
3718 (save-window-excursion
3719 (info))
89b064a9 3720 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
218c2cc7
EZ
3721 ;; Bind Info-history to nil, to prevent the last Index node
3722 ;; visited by Info-find-emacs-command-nodes from being
3723 ;; pushed onto the history.
02c9f5cd
JL
3724 (let ((Info-history nil) (Info-history-list nil)
3725 (line-number (nth 2 (car where))))
3726 (Info-find-node (nth 0 (car where)) (nth 1 (car where)))
3727 (if (and (integerp line-number) (> line-number 0))
3728 (forward-line (1- line-number))))
f0a8a3f1
RM
3729 (if (> num-matches 1)
3730 (progn
218c2cc7
EZ
3731 ;; (car where) will be pushed onto Info-history
3732 ;; when/if they go to another node. Put the other
3733 ;; nodes that were found on the history.
f0a8a3f1 3734 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 3735 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
3736 (1- num-matches)
3737 (if (> num-matches 2) "ies" "y")
424b9d3e 3738 (substitute-command-keys "\\[Info-history-back]")
cedb118c 3739 (if (> num-matches 2) "them" "it")))))
e9b81433 3740 (error "Couldn't find documentation for %s" command))))
f0a8a3f1 3741
02c9f5cd 3742;;;###autoload (put 'Info-goto-emacs-key-command-node 'info-file "emacs")
f0a8a3f1
RM
3743;;;###autoload
3744(defun Info-goto-emacs-key-command-node (key)
30db89f9
EZ
3745 "Go to the node in the Emacs manual which describes the command bound to KEY.
3746KEY is a string.
fdf4b680 3747Interactively, if the binding is `execute-extended-command', a command is read.
30db89f9 3748The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
3749or in another manual found via COMMAND's `info-file' property or
3750the variable `Info-file-list-for-emacs'."
ea33749b 3751 (interactive "kFind documentation for key: ")
f0a8a3f1
RM
3752 (let ((command (key-binding key)))
3753 (cond ((null command)
9e5c2f50 3754 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
3755 ((and (interactive-p)
3756 (eq command 'execute-extended-command))
3757 (Info-goto-emacs-command-node
3758 (read-command "Find documentation for command: ")))
3759 (t
3760 (Info-goto-emacs-command-node command)))))
552775bd 3761\f
efb21aab
RS
3762(defvar Info-next-link-keymap
3763 (let ((keymap (make-sparse-keymap)))
3764 (define-key keymap [header-line mouse-1] 'Info-next)
3765 (define-key keymap [header-line mouse-2] 'Info-next)
3766 (define-key keymap [header-line down-mouse-1] 'ignore)
3767 (define-key keymap [mouse-2] 'Info-next)
3c1498ac 3768 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3769 keymap)
3770 "Keymap to put on the Next link in the text or the header line.")
3771
3772(defvar Info-prev-link-keymap
3773 (let ((keymap (make-sparse-keymap)))
3774 (define-key keymap [header-line mouse-1] 'Info-prev)
3775 (define-key keymap [header-line mouse-2] 'Info-prev)
3776 (define-key keymap [header-line down-mouse-1] 'ignore)
3777 (define-key keymap [mouse-2] 'Info-prev)
3c1498ac 3778 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3779 keymap)
3780 "Keymap to put on the Prev link in the text or the header line.")
3781
efb21aab
RS
3782(defvar Info-up-link-keymap
3783 (let ((keymap (make-sparse-keymap)))
3784 (define-key keymap [header-line mouse-1] 'Info-up)
3785 (define-key keymap [header-line mouse-2] 'Info-up)
3786 (define-key keymap [header-line down-mouse-1] 'ignore)
3787 (define-key keymap [mouse-2] 'Info-up)
3c1498ac 3788 (define-key keymap [follow-link] 'mouse-face)
efb21aab
RS
3789 keymap)
3790 "Keymap to put on the Up link in the text or the header line.")
3791
fdeb777a 3792(defun Info-insert-breadcrumbs ()
586f9284
JL
3793 (let ((nodes (Info-toc-nodes Info-current-file))
3794 (node Info-current-node)
fdeb777a 3795 (crumbs ())
586f9284
JL
3796 (depth Info-breadcrumbs-depth))
3797
3798 ;; Get ancestors from the cached parent-children node info
3799 (while (and (not (equal "Top" node)) (> depth 0))
3800 (setq node (nth 1 (assoc node nodes)))
3801 (if node (push node crumbs))
3802 (setq depth (1- depth)))
3803
3804 ;; Add bottom node.
3805 (when Info-use-header-line
3806 ;; Let it disappear if crumbs is nil.
3807 (nconc crumbs (list Info-current-node)))
3808 (when (or Info-use-header-line crumbs)
3809 ;; Add top node (and continuation if needed).
3810 (setq crumbs
3811 (cons "Top" (if (member (pop crumbs) '(nil "Top"))
3812 crumbs (cons nil crumbs))))
3813 ;; Eliminate duplicate.
3814 (forward-line 1)
3815 (dolist (node crumbs)
3816 (let ((text
3817 (if (not (equal node "Top")) node
3818 (format "(%s)Top"
3819 (if (stringp Info-current-file)
3820 (file-name-nondirectory Info-current-file)
3821 ;; Can be `toc', `apropos', or even `history'.
3822 Info-current-file)))))
3823 (insert (if (bolp) "" " > ")
3824 (cond
3825 ((null node) "...")
3826 ((equal node Info-current-node)
3827 ;; No point linking to ourselves.
3828 (propertize text 'font-lock-face 'info-header-node))
3829 (t
3830 (concat "*Note " text "::"))))))
3831 (insert "\n"))))
fdeb777a 3832
552775bd 3833(defun Info-fontify-node ()
8a7757f6
JL
3834 "Fontify the node."
3835 (save-excursion
3836 (let* ((inhibit-read-only t)
3837 (case-fold-search t)
3838 paragraph-markers
3839 (not-fontified-p ; the node hasn't already been fontified
f9b27c85 3840 (not (let ((where (next-single-property-change (point-min)
499878f8 3841 'font-lock-face)))
8a7757f6
JL
3842 (and where (not (= where (point-max)))))))
3843 (fontify-visited-p ; visited nodes need to be re-fontified
3844 (and Info-fontify-visited-nodes
3845 ;; Don't take time to refontify visited nodes in huge nodes
3d0fc0b7 3846 Info-fontify-maximum-menu-size
e14105f6
TTN
3847 (< (- (point-max) (point-min)) Info-fontify-maximum-menu-size)))
3848 rbeg rend)
8a7757f6
JL
3849
3850 ;; Fontify header line
3851 (goto-char (point-min))
3852 (when (and not-fontified-p (looking-at "^\\(File: [^,: \t]+,?[ \t]+\\)?"))
3853 (goto-char (match-end 0))
3854 (while (looking-at "[ \t]*\\([^:, \t\n]+\\):[ \t]+\\([^:,\t\n]+\\),?")
3855 (goto-char (match-end 0))
3856 (let* ((nbeg (match-beginning 2))
3857 (nend (match-end 2))
3858 (tbeg (match-beginning 1))
3859 (tag (match-string 1)))
99315976 3860 (if (string-equal (downcase tag) "node")
8a7757f6
JL
3861 (put-text-property nbeg nend 'font-lock-face 'info-header-node)
3862 (put-text-property nbeg nend 'font-lock-face 'info-header-xref)
3863 (put-text-property tbeg nend 'mouse-face 'highlight)
3864 (put-text-property tbeg nend
3865 'help-echo
3c1498ac 3866 (concat "mouse-2: Go to node "
8a7757f6
JL
3867 (buffer-substring nbeg nend)))
3868 ;; Always set up the text property keymap.
3869 ;; It will either be used in the buffer
3870 ;; or copied in the header line.
99315976
JL
3871 (put-text-property
3872 tbeg nend 'keymap
3873 (cond
3874 ((string-equal (downcase tag) "prev") Info-prev-link-keymap)
3875 ((string-equal (downcase tag) "next") Info-next-link-keymap)
3876 ((string-equal (downcase tag) "up" ) Info-up-link-keymap))))))
3bfd723a 3877
fdeb777a
SM
3878 (when (> Info-breadcrumbs-depth 0)
3879 (Info-insert-breadcrumbs))
3bfd723a 3880
fdeb777a 3881 ;; Treat header line.
8a7757f6
JL
3882 (when Info-use-header-line
3883 (goto-char (point-min))
de22f49c
SM
3884 (let* ((header-end (line-end-position))
3885 (header
3886 ;; If we find neither Next: nor Prev: link, show the entire
3887 ;; node header. Otherwise, don't show the File: and Node:
3888 ;; parts, to avoid wasting precious space on information that
3889 ;; is available in the mode line.
3890 (if (re-search-forward
3891 "\\(next\\|up\\|prev[ious]*\\): "
3892 header-end t)
3893 (progn
3894 (goto-char (match-beginning 1))
3895 (buffer-substring (point) header-end))
3896 (if (re-search-forward "node:[ \t]*[^ \t]+[ \t]*"
3897 header-end t)
8a7757f6 3898 (concat "No next, prev or up links -- "
de22f49c
SM
3899 (buffer-substring (point) header-end))
3900 (buffer-substring (point) header-end)))))
8a7757f6 3901 (put-text-property (point-min) (1+ (point-min))
69672704
JL
3902 'header-line
3903 (replace-regexp-in-string
3904 "%"
3905 ;; Preserve text properties on duplicated `%'.
3906 (lambda (s) (concat s s)) header))
8a7757f6
JL
3907 ;; Hide the part of the first line
3908 ;; that is in the header, if it is just part.
fdeb777a
SM
3909 (cond
3910 ((> Info-breadcrumbs-depth 0)
3911 (put-text-property (point-min) (1+ header-end) 'invisible t))
3912 ((not (bobp))
8a7757f6
JL
3913 ;; Hide the punctuation at the end, too.
3914 (skip-chars-backward " \t,")
fdeb777a 3915 (put-text-property (point) header-end 'invisible t))))))
8a7757f6
JL
3916
3917 ;; Fontify titles
3918 (goto-char (point-min))
de22f49c
SM
3919 (when (and font-lock-mode not-fontified-p)
3920 (while (and (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*\\*+\\|==+\\|--+\\|\\.\\.+\\)$"
3921 nil t)
3922 ;; Only consider it as an underlined title if the ASCII
3923 ;; underline has the same size as the text. A typical
3924 ;; counter example is when a continuation "..." is alone
3925 ;; on a line.
4dacf0ff
JL
3926 (= (string-width (match-string 1))
3927 (string-width (match-string 2))))
8a7757f6
JL
3928 (let* ((c (preceding-char))
3929 (face
a335c06e
MB
3930 (cond ((= c ?*) 'info-title-1)
3931 ((= c ?=) 'info-title-2)
3932 ((= c ?-) 'info-title-3)
3933 (t 'info-title-4))))
8a7757f6
JL
3934 (put-text-property (match-beginning 1) (match-end 1)
3935 'font-lock-face face))
3936 ;; This is a serious problem for trying to handle multiple
3937 ;; frame types at once. We want this text to be invisible
3938 ;; on frames that can display the font above.
9e2a2647 3939 (when (memq (framep (selected-frame)) '(x pc w32 ns))
8a7757f6
JL
3940 (add-text-properties (1- (match-beginning 2)) (match-end 2)
3941 '(invisible t front-sticky nil rear-nonsticky t)))))
3942
3943 ;; Fontify cross references
3944 (goto-char (point-min))
3945 (when (or not-fontified-p fontify-visited-p)
3946 (while (re-search-forward "\\(\\*Note[ \n\t]+\\)\\([^:]*\\)\\(:[ \t]*\\([^.,:(]*\\)\\(\\(([^)]*)\\)[^.,:]*\\)?[,:]?\n?\\)" nil t)
3947 (let ((start (match-beginning 0))
3948 (next (point))
3949 other-tag)
3950 (when not-fontified-p
3951 (when Info-hide-note-references
fdeb777a
SM
3952 (when (and (not (eq Info-hide-note-references 'hide))
3953 (> (line-number-at-pos) 4)) ; Skip breadcrumbs
fc11ddc8
JL
3954 ;; *Note is often used where *note should have been
3955 (goto-char start)
3956 (skip-syntax-backward " ")
d75baf12
RS
3957 (when (memq (char-before) '(?\( ?\[ ?\{))
3958 ;; Check whether the paren is preceded by
3959 ;; an end of sentence
3960 (skip-syntax-backward " ("))
fc11ddc8 3961 (setq other-tag
e8e6694b
RS
3962 (cond ((save-match-data (looking-back "\\<see"))
3963 "")
dc7ef9f0
CY
3964 ((save-match-data (looking-back "\\<in"))
3965 "")
e8e6694b 3966 ((memq (char-before) '(nil ?\. ?! ??))
fc11ddc8 3967 "See ")
d75baf12
RS
3968 ((save-match-data
3969 (save-excursion
3970 (search-forward "\n\n" start t)))
3971 "See ")
3972 (t "see "))))
8a7757f6
JL
3973 (goto-char next)
3974 (add-text-properties
3975 (match-beginning 1)
3976 (or (save-match-data
3977 ;; Don't hide \n after *Note
3978 (let ((start1 (match-beginning 1)))
3979 (if (string-match "\n" (match-string 1))
3980 (+ start1 (match-beginning 0)))))
3981 (match-end 1))
fc11ddc8 3982 (if other-tag
8a7757f6
JL
3983 `(display ,other-tag front-sticky nil rear-nonsticky t)
3984 '(invisible t front-sticky nil rear-nonsticky t))))
3985 (add-text-properties
3986 (match-beginning 2) (match-end 2)
3987 (list
3988 'help-echo (if (or (match-end 5)
3989 (not (equal (match-string 4) "")))
3990 (concat "mouse-2: go to " (or (match-string 5)
3991 (match-string 4)))
3992 "mouse-2: go to this node")
3993 'mouse-face 'highlight)))
3994 (when (or not-fontified-p fontify-visited-p)
e14105f6
TTN
3995 (setq rbeg (match-beginning 2)
3996 rend (match-end 2))
3997 (put-text-property
3998 rbeg rend
3999 'font-lock-face
4000 ;; Display visited nodes in a different face
4001 (if (and Info-fontify-visited-nodes
4002 (save-match-data
4003 (let* ((node (replace-regexp-in-string
4004 "^[ \t]+" ""
4005 (replace-regexp-in-string
4006 "[ \t\n]+" " "
bec275ed 4007 (or (match-string-no-properties 5)
e14105f6 4008 (and (not (equal (match-string 4) ""))
bec275ed
JL
4009 (match-string-no-properties 4))
4010 (match-string-no-properties 2)))))
d8fbba22
CY
4011 (external-link-p
4012 (string-match "(\\([^)]+\\))\\([^)]*\\)" node))
4013 (file (if external-link-p
4014 (file-name-nondirectory
bec275ed 4015 (match-string-no-properties 1 node))
d8fbba22 4016 Info-current-file))
e14105f6
TTN
4017 (hl Info-history-list)
4018 res)
d8fbba22
CY
4019 (if external-link-p
4020 (setq node (if (equal (match-string 2 node) "")
e14105f6 4021 "Top"
bec275ed 4022 (match-string-no-properties 2 node))))
d590dbd2
CY
4023 (while hl
4024 (if (and (string-equal node (nth 1 (car hl)))
bec275ed
JL
4025 (equal file
4026 (if (and external-link-p
4027 (stringp (caar hl)))
4028 (file-name-nondirectory
4029 (caar hl))
4030 (caar hl))))
d590dbd2
CY
4031 (setq res (car hl) hl nil)
4032 (setq hl (cdr hl))))
e14105f6
TTN
4033 res))) 'info-xref-visited 'info-xref))
4034 ;; For multiline ref, unfontify newline and surrounding whitespace
4035 (save-excursion
4036 (goto-char rbeg)
4037 (save-match-data
4038 (while (re-search-forward "\\s-*\n\\s-*" rend t nil)
4039 (remove-text-properties (match-beginning 0)
4040 (match-end 0)
4041 '(font-lock-face t))))))
8a7757f6
JL
4042 (when not-fontified-p
4043 (when (memq Info-hide-note-references '(t hide))
4044 (add-text-properties (match-beginning 3) (match-end 3)
4045 '(invisible t front-sticky nil rear-nonsticky t))
4046 ;; Unhide the file name of the external reference in parens
89b064a9 4047 (if (and (match-string 6) (not (eq Info-hide-note-references 'hide)))
8a7757f6
JL
4048 (remove-text-properties (match-beginning 6) (match-end 6)
4049 '(invisible t front-sticky nil rear-nonsticky t)))
4050 ;; Unhide newline because hidden newlines cause too long lines
4051 (save-match-data
89b064a9
JL
4052 (let ((beg3 (match-beginning 3))
4053 (end3 (match-end 3)))
4054 (if (and (string-match "\n[ \t]*" (match-string 3))
4055 (not (save-match-data
4056 (save-excursion
4057 (goto-char (1+ end3))
4058 (looking-at "[.)]*$")))))
4059 (remove-text-properties (+ beg3 (match-beginning 0))
4060 (+ beg3 (match-end 0))
8a7757f6
JL
4061 '(invisible t front-sticky nil rear-nonsticky t))))))
4062 (when (and Info-refill-paragraphs Info-hide-note-references)
4063 (push (set-marker (make-marker) start)
4064 paragraph-markers))))))
4065
4066 ;; Refill paragraphs (experimental feature)
4067 (when (and not-fontified-p
4068 Info-refill-paragraphs
4069 paragraph-markers)
4070 (let ((fill-nobreak-invisible t)
4071 (fill-individual-varying-indent nil)
4072 (paragraph-start "\f\\|[ \t]*[-*]\\|[ \t]*$")
4073 (paragraph-separate ".*\\.[ \t]*\n[ \t]\\|[ \t]*[-*]\\|[ \t\f]*$")
4074 (adaptive-fill-mode nil))
4075 (goto-char (point-max))
a73db543
SM
4076 (dolist (m paragraph-markers)
4077 (when (< m (point))
4078 (goto-char m)
4079 (beginning-of-line)
4080 (let ((beg (point)))
4081 (when (zerop (forward-paragraph))
4082 (fill-individual-paragraphs beg (point) nil nil)
4083 (goto-char beg))))
4084 (set-marker m nil))))
8a7757f6
JL
4085
4086 ;; Fontify menu items
4087 (goto-char (point-min))
4088 (when (and (or not-fontified-p fontify-visited-p)
4089 (search-forward "\n* Menu:" nil t)
8a7757f6 4090 ;; Don't take time to annotate huge menus
3d0fc0b7 4091 Info-fontify-maximum-menu-size
8a7757f6
JL
4092 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
4093 (let ((n 0)
4094 cont)
4095 (while (re-search-forward
1ecc5922
RS
4096 (concat "^\\* Menu:\\|\\(?:^\\* +\\(" Info-menu-entry-name-re "\\)\\(:"
4097 Info-node-spec-re "\\([ \t]*\\)\\)\\)")
8a7757f6 4098 nil t)
1ecc5922
RS
4099 (when (match-beginning 1)
4100 (when not-fontified-p
4101 (setq n (1+ n))
4102 (if (and (<= n 9) (zerop (% n 3))) ; visual aids to help with 1-9 keys
4103 (put-text-property (match-beginning 0)
4104 (1+ (match-beginning 0))
185cff95 4105 'font-lock-face 'info-menu-star)))
1ecc5922
RS
4106 (when not-fontified-p
4107 (add-text-properties
4108 (match-beginning 1) (match-end 1)
4109 (list
4110 'help-echo (if (and (match-end 3)
4111 (not (equal (match-string 3) "")))
4112 (concat "mouse-2: go to " (match-string 3))
4113 "mouse-2: go to this node")
4114 'mouse-face 'highlight)))
4115 (when (or not-fontified-p fontify-visited-p)
a73db543 4116 (put-text-property
1ecc5922 4117 (match-beginning 1) (match-end 1)
a73db543
SM
4118 'font-lock-face
4119 ;; Display visited menu items in a different face
4120 (if (and Info-fontify-visited-nodes
4121 (save-match-data
d8fbba22 4122 (let* ((node (if (equal (match-string 3) "")
bec275ed
JL
4123 (match-string-no-properties 1)
4124 (match-string-no-properties 3)))
d8fbba22
CY
4125 (external-link-p
4126 (string-match "(\\([^)]+\\))\\([^)]*\\)" node))
4127 (file (if external-link-p
4128 (file-name-nondirectory
bec275ed 4129 (match-string-no-properties 1 node))
d8fbba22
CY
4130 Info-current-file))
4131 (hl Info-history-list)
4132 res)
4133 (if external-link-p
4134 (setq node (if (equal (match-string 2 node) "")
a73db543 4135 "Top"
bec275ed 4136 (match-string-no-properties 2 node))))
d590dbd2
CY
4137 (while hl
4138 (if (and (string-equal node (nth 1 (car hl)))
bec275ed
JL
4139 (equal file
4140 (if (and external-link-p
4141 (stringp (caar hl)))
4142 (file-name-nondirectory
4143 (caar hl))
4144 (caar hl))))
d590dbd2
CY
4145 (setq res (car hl) hl nil)
4146 (setq hl (cdr hl))))
a73db543 4147 res))) 'info-xref-visited 'info-xref)))
0f218170
JL
4148 (when (and not-fontified-p
4149 (memq Info-hide-note-references '(t hide))
4150 (not (Info-index-node)))
1ecc5922
RS
4151 (put-text-property (match-beginning 2) (1- (match-end 6))
4152 'invisible t)
4153 ;; Unhide the file name in parens
4154 (if (and (match-end 4) (not (eq (char-after (match-end 4)) ?.)))
4155 (remove-text-properties (match-beginning 4) (match-end 4)
4156 '(invisible t)))
4157 ;; We need a stretchable space like :align-to but with
4158 ;; a minimum value.
4159 (put-text-property (1- (match-end 6)) (match-end 6) 'display
4160 (if (>= 22 (- (match-end 1)
4161 (match-beginning 0)))
4162 '(space :align-to 24)
4163 '(space :width 2)))
4164 (setq cont (looking-at "."))
4165 (while (and (= (forward-line 1) 0)
4166 (looking-at "\\([ \t]+\\)[^*\n]"))
4167 (put-text-property (match-beginning 1) (1- (match-end 1))
4168 'invisible t)
4169 (put-text-property (1- (match-end 1)) (match-end 1)
4170 'display
4171 (if cont
4172 '(space :align-to 26)
4173 '(space :align-to 24)))
4174 (setq cont t)))))))
8a7757f6
JL
4175
4176 ;; Fontify menu headers
4177 ;; Add the face `info-menu-header' to any header before a menu entry
4178 (goto-char (point-min))
4179 (when (and not-fontified-p (re-search-forward "^\\* Menu:" nil t))
4180 (put-text-property (match-beginning 0) (match-end 0)
4181 'font-lock-face 'info-menu-header)
4182 (while (re-search-forward "\n\n\\([^*\n ].*\\)\n\n?[*]" nil t)
4183 (put-text-property (match-beginning 1) (match-end 1)
4184 'font-lock-face 'info-menu-header)))
4185
836bb478
JL
4186 ;; Hide index line numbers
4187 (goto-char (point-min))
141b49f5 4188 (when (and not-fontified-p (Info-index-node))
836bb478
JL
4189 (while (re-search-forward "[ \t\n]*(line +[0-9]+)" nil t)
4190 (put-text-property (match-beginning 0) (match-end 0)
4191 'invisible t)))
4192
8a7757f6
JL
4193 ;; Fontify http and ftp references
4194 (goto-char (point-min))
4195 (when not-fontified-p
68b77b64
GM
4196 (while (re-search-forward "\\(https?\\|ftp\\)://[^ \t\n\"`({<>})']+"
4197 nil t)
8a7757f6
JL
4198 (add-text-properties (match-beginning 0) (match-end 0)
4199 '(font-lock-face info-xref
fdeb777a
SM
4200 mouse-face highlight
4201 help-echo "mouse-2: go to this URL"))))
8a7757f6
JL
4202
4203 (set-buffer-modified-p nil))))
c5fe2ff1 4204\f
3cb6768f
EL
4205;;; Speedbar support:
4206;; These functions permit speedbar to display the "tags" in the
185cff95 4207;; current Info node.
96ee3f29 4208(eval-when-compile (require 'speedbar))
3cb6768f 4209
96ee3f29
KH
4210(defvar Info-speedbar-key-map nil
4211 "Keymap used when in the info display mode.")
3cb6768f 4212
96ee3f29
KH
4213(defun Info-install-speedbar-variables ()
4214 "Install those variables used by speedbar to enhance Info."
4215 (if Info-speedbar-key-map
4216 nil
4217 (setq Info-speedbar-key-map (speedbar-make-specialized-keymap))
4218
4219 ;; Basic tree features
4220 (define-key Info-speedbar-key-map "e" 'speedbar-edit-line)
4221 (define-key Info-speedbar-key-map "\C-m" 'speedbar-edit-line)
4222 (define-key Info-speedbar-key-map "+" 'speedbar-expand-line)
4223 (define-key Info-speedbar-key-map "-" 'speedbar-contract-line)
4224 )
4225
4226 (speedbar-add-expansion-list '("Info" Info-speedbar-menu-items
4227 Info-speedbar-key-map
4228 Info-speedbar-hierarchy-buttons)))
3cb6768f
EL
4229
4230(defvar Info-speedbar-menu-items
96ee3f29
KH
4231 '(["Browse Node" speedbar-edit-line t]
4232 ["Expand Node" speedbar-expand-line
4233 (save-excursion (beginning-of-line)
4234 (looking-at "[0-9]+: *.\\+. "))]
4235 ["Contract Node" speedbar-contract-line
4236 (save-excursion (beginning-of-line)
4237 (looking-at "[0-9]+: *.-. "))]
4238 )
3cb6768f
EL
4239 "Additional menu-items to add to speedbar frame.")
4240
96ee3f29
KH
4241;; Make sure our special speedbar major mode is loaded
4242(if (featurep 'speedbar)
4243 (Info-install-speedbar-variables)
4244 (add-hook 'speedbar-load-hook 'Info-install-speedbar-variables))
4245
4246;;; Info hierarchy display method
4247;;;###autoload
4248(defun Info-speedbar-browser ()
185cff95 4249 "Initialize speedbar to display an Info node browser.
96ee3f29
KH
4250This will add a speedbar major display mode."
4251 (interactive)
4252 (require 'speedbar)
4253 ;; Make sure that speedbar is active
4254 (speedbar-frame-mode 1)
4255 ;; Now, throw us into Info mode on speedbar.
4256 (speedbar-change-initial-expansion-list "Info")
4257 )
4258
4259(defun Info-speedbar-hierarchy-buttons (directory depth &optional node)
4260 "Display an Info directory hierarchy in speedbar.
4261DIRECTORY is the current directory in the attached frame.
4262DEPTH is the current indentation depth.
4263NODE is an optional argument that is used to represent the
4264specific node to expand."
4265 (if (and (not node)
4266 (save-excursion (goto-char (point-min))
1bcedb3b
RS
4267 (let ((case-fold-search t))
4268 (looking-at "Info Nodes:"))))
96ee3f29
KH
4269 ;; Update our "current node" maybe?
4270 nil
4271 ;; We cannot use the generic list code, that depends on all leaves
4272 ;; being known at creation time.
4273 (if (not node)
4274 (speedbar-with-writable (insert "Info Nodes:\n")))
24bbdbef
CY
4275 (let ((completions nil))
4276 (speedbar-select-attached-frame)
5fdc7997
EL
4277 (save-window-excursion
4278 (setq completions
4279 (Info-speedbar-fetch-file-nodes (or node '"(dir)top"))))
222a6c9b 4280 (select-frame (speedbar-current-frame))
96ee3f29
KH
4281 (if completions
4282 (speedbar-with-writable
dde7f979 4283 (dolist (completion completions)
96ee3f29 4284 (speedbar-make-tag-line 'bracket ?+ 'Info-speedbar-expand-node
dde7f979
SM
4285 (cdr completion)
4286 (car completion)
96ee3f29 4287 'Info-speedbar-goto-node
dde7f979
SM
4288 (cdr completion)
4289 'info-xref depth))
96ee3f29
KH
4290 t)
4291 nil))))
399c88ad 4292
96ee3f29 4293(defun Info-speedbar-goto-node (text node indent)
d1268e52 4294 "When user clicks on TEXT, go to an info NODE.
96ee3f29 4295The INDENT level is ignored."
24bbdbef 4296 (speedbar-select-attached-frame)
760d5cb3
GM
4297 (let* ((buff (or (get-buffer "*info*")
4298 (progn (info) (get-buffer "*info*"))))
4299 (bwin (get-buffer-window buff 0)))
4300 (if bwin
4301 (progn
4302 (select-window bwin)
4303 (raise-frame (window-frame bwin)))
4304 (if speedbar-power-click
4305 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
24bbdbef 4306 (speedbar-select-attached-frame)
760d5cb3 4307 (switch-to-buffer buff)))
dde7f979
SM
4308 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" node))
4309 (error "Invalid node %s" node)
4310 (Info-find-node (match-string 1 node) (match-string 2 node))
760d5cb3
GM
4311 ;; If we do a find-node, and we were in info mode, restore
4312 ;; the old default method. Once we are in info mode, it makes
4313 ;; sense to return to whatever method the user was using before.
4314 (if (string= speedbar-initial-expansion-list-name "Info")
4315 (speedbar-change-initial-expansion-list
4316 speedbar-previously-used-expansion-list-name)))))
96ee3f29
KH
4317
4318(defun Info-speedbar-expand-node (text token indent)
4319 "Expand the node the user clicked on.
4320TEXT is the text of the button we clicked on, a + or - item.
4321TOKEN is data related to this node (NAME . FILE).
4322INDENT is the current indentation depth."
4323 (cond ((string-match "+" text) ;we have to expand this file
4324 (speedbar-change-expand-button-char ?-)
4325 (if (speedbar-with-writable
760d5cb3
GM
4326 (save-excursion
4327 (end-of-line) (forward-char 1)
4328 (Info-speedbar-hierarchy-buttons nil (1+ indent) token)))
96ee3f29
KH
4329 (speedbar-change-expand-button-char ?-)
4330 (speedbar-change-expand-button-char ??)))
4331 ((string-match "-" text) ;we have to contract this node
4332 (speedbar-change-expand-button-char ?+)
4333 (speedbar-delete-subblock indent))
4334 (t (error "Ooops... not sure what to do")))
4335 (speedbar-center-buffer-smartly))
4336
4337(defun Info-speedbar-fetch-file-nodes (nodespec)
4338 "Fetch the subnodes from the info NODESPEC.
db90d225 4339NODESPEC is a string of the form: (file)node."
e8dab975
SM
4340 ;; Set up a buffer we can use to fake-out Info.
4341 (with-current-buffer (get-buffer-create " *info-browse-tmp*")
96ee3f29
KH
4342 (if (not (equal major-mode 'Info-mode))
4343 (Info-mode))
4344 ;; Get the node into this buffer
dde7f979
SM
4345 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" nodespec))
4346 (error "Invalid node specification %s" nodespec)
4347 (Info-find-node (match-string 1 nodespec) (match-string 2 nodespec)))
96ee3f29
KH
4348 ;; Scan the created buffer
4349 (goto-char (point-min))
4350 (let ((completions nil)
1bcedb3b 4351 (case-fold-search t)
96ee3f29
KH
4352 (thisfile (progn (string-match "^(\\([^)]+\\))" nodespec)
4353 (match-string 1 nodespec))))
4354 ;; Always skip the first one...
4355 (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
4356 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
4357 (let ((name (match-string 1)))
dde7f979
SM
4358 (push (cons name
4359 (if (looking-at " *\\(([^)]+)[^.\n]+\\)\\.")
4360 (match-string 1)
4361 (if (looking-at " *\\(([^)]+)\\)\\.")
4362 (concat (match-string 1) "Top")
4363 (concat "(" thisfile ")"
4364 (if (looking-at " \\([^.]+\\).")
4365 (match-string 1)
4366 name)))))
4367 completions)))
96ee3f29
KH
4368 (nreverse completions))))
4369
4370;;; Info mode node listing
24bbdbef 4371;; This is called by `speedbar-add-localized-speedbar-support'
3cb6768f
EL
4372(defun Info-speedbar-buttons (buffer)
4373 "Create a speedbar display to help navigation in an Info file.
4374BUFFER is the buffer speedbar is requesting buttons for."
96ee3f29 4375 (if (save-excursion (goto-char (point-min))
1bcedb3b
RS
4376 (let ((case-fold-search t))
4377 (not (looking-at "Info Nodes:"))))
96ee3f29 4378 (erase-buffer))
24bbdbef 4379 (Info-speedbar-hierarchy-buttons nil 0))
49116ac0 4380
6270bbe7
JL
4381(dolist (mess '("^First node in file$"
4382 "^No `.*' in index$"
4383 "^No cross-reference named"
4384 "^No cross.references in this node$"
185cff95 4385 "^No current Info node$"
48e5b471 4386 "^No menu in this node$"
6270bbe7
JL
4387 "^No more items in menu$"
4388 "^No more nodes$"
4389 "^No pointer \\(?:forward\\|backward\\) from this node$"
4390 "^No previous `i' command$"
4391 "^No previous items in menu$"
4392 "^No previous nodes$"
4393 "^No such item in menu$"
4394 "^No such node or anchor"
4395 "^Node has no"
4396 "^Point neither on reference nor in menu item description$"
4397 "^This is the \\(?:first\\|last\\) Info node you looked at$"
4398 search-failed))
48e5b471
DL
4399 (add-to-list 'debug-ignored-errors mess))
4400
f3a45db0
LH
4401;;;; Desktop support
4402
4403(defun Info-desktop-buffer-misc-data (desktop-dirname)
4404 "Auxiliary information to be saved in desktop file."
bec275ed
JL
4405 (unless (member Info-current-file '(apropos history toc nil))
4406 (list Info-current-file Info-current-node)))
f3a45db0 4407
f3a45db0
LH
4408(defun Info-restore-desktop-buffer (desktop-buffer-file-name
4409 desktop-buffer-name
4410 desktop-buffer-misc)
185cff95 4411 "Restore an Info buffer specified in a desktop file."
f3a45db0
LH
4412 (let ((first (nth 0 desktop-buffer-misc))
4413 (second (nth 1 desktop-buffer-misc)))
4414 (when (and first second)
c9ce1e40
JL
4415 (when desktop-buffer-name
4416 (set-buffer (get-buffer-create desktop-buffer-name))
4417 (Info-mode))
2a3f604d 4418 (Info-find-node first second)
f3a45db0
LH
4419 (current-buffer))))
4420
9e7357b0
AS
4421(add-to-list 'desktop-buffer-mode-handlers
4422 '(Info-mode . Info-restore-desktop-buffer))
80123c9e 4423
23ceed9a 4424;;;; Bookmark support
43f8b275
SM
4425(declare-function bookmark-make-record-default "bookmark" (&optional pos-only))
4426(declare-function bookmark-prop-get "bookmark" (bookmark prop))
4427(declare-function bookmark-default-handler "bookmark" (bmk))
4428(declare-function bookmark-get-bookmark-record "bookmark" (bmk))
e1af69b4 4429
32a091dd 4430(defun Info-bookmark-make-record ()
136894c8 4431 `(,Info-current-node
43f8b275
SM
4432 ,@(bookmark-make-record-default 'point-only)
4433 (filename . ,Info-current-file)
32a091dd
SM
4434 (info-node . ,Info-current-node)
4435 (handler . Info-bookmark-jump)))
23ceed9a
TH
4436
4437;;;###autoload
4438(defun Info-bookmark-jump (bmk)
4439 ;; This implements the `handler' function interface for record type returned
e0385bf4 4440 ;; by `Info-bookmark-make-record', which see.
43f8b275
SM
4441 (let* ((file (bookmark-prop-get bmk 'filename))
4442 (info-node (bookmark-prop-get bmk 'info-node))
4443 (buf (save-window-excursion ;FIXME: doesn't work with frames!
4444 (Info-find-node file info-node) (current-buffer))))
4445 ;; Use bookmark-default-handler to move to the appropriate location
4446 ;; within the node.
4447 (bookmark-default-handler
4448 (list* "" `(buffer . ,buf) (bookmark-get-bookmark-record bmk)))))
23ceed9a 4449
49116ac0
JB
4450(provide 'info)
4451
83040cb0 4452;; arch-tag: f2480fe2-2139-40c1-a49b-6314991164ac
1a06eabd 4453;;; info.el ends here