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