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