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