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