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