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