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