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