(rmail-restore-desktop-buffer): Move from desktop.el. Add Parameters.
[bpt/emacs.git] / lisp / info.el
CommitLineData
55535639 1;;; info.el --- info package for Emacs
e5167999 2
f6b2b74d 3;; Copyright (C) 1985,86,92,93,94,95,96,97,98,99,2000,01,02,03,2004
5b3ebb44 4;; Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: help
e5167999 8
a384cab3
JB
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a384cab3
JB
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
a384cab3 25
e5167999
ER
26;;; Commentary:
27
b578f267 28;; Note that nowadays we expect info files to be made using makeinfo.
921e5fe6
TTN
29;; In particular we make these assumptions:
30;; - a menu item MAY contain colons but not colon-space ": "
31;; - a menu item ending with ": " (but not ":: ") is an index entry
32;; - a node name MAY NOT contain a colon
505b68d5
TTN
33;; This distinction is to support indexing of computer programming
34;; language terms that may contain ":" but not ": ".
e5167999
ER
35
36;;; Code:
37
bc6262c8
DL
38(eval-when-compile (require 'jka-compr))
39
ded3e3d8
RS
40(defgroup info nil
41 "Info subsystem"
42 :group 'help
43 :group 'docs)
44
45
a384cab3 46(defvar Info-history nil
8a7757f6 47 "Stack of info nodes user has visited.
a384cab3
JB
48Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
49
8a7757f6
JL
50(defvar Info-history-list nil
51 "List of all info nodes user has visited.
52Each element of list is a list (FILENAME NODENAME).")
53
ded3e3d8 54(defcustom Info-enable-edit nil
d5913bf6 55 "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info can edit the current node.
ada0a60d
RS
56This is convenient if you want to write info files by hand.
57However, we recommend that you not do this.
58It is better to write a Texinfo file and generate the Info file from that,
ded3e3d8
RS
59because that gives you a printed manual as well."
60 :type 'boolean
61 :group 'info)
a384cab3 62
67f445d7 63(defvar Info-enable-active-nodes nil
a384cab3
JB
64 "Non-nil allows Info to execute Lisp code associated with nodes.
65The Lisp code is executed when the node is selected.")
67f445d7 66(put 'Info-enable-active-nodes 'risky-local-variable t)
a384cab3 67
93480d70 68(defface info-node
58b64ac7
RS
69 '((((class color) (background light)) (:foreground "brown" :weight bold :slant italic))
70 (((class color) (background dark)) (:foreground "white" :weight bold :slant italic))
71 (t (:weight bold :slant italic)))
93480d70
RS
72 "Face for Info node names."
73 :group 'info)
74
75(defface info-menu-5
7a3abcd8
EZ
76 '((((class color)) (:foreground "red1"))
77 (t (:underline t)))
ccfb7415 78 "Face for every third `*' in an Info menu."
93480d70
RS
79 :group 'info)
80
81(defface info-xref
8a7757f6
JL
82 '((((class color) (background light)) (:foreground "blue"))
83 (((class color) (background dark)) (:foreground "cyan"))
84 (t (:underline t)))
93480d70
RS
85 "Face for Info cross-references."
86 :group 'info)
87
8a7757f6
JL
88(defface info-xref-visited
89 '((((class color) (background light)) (:foreground "magenta4"))
90 (((class color) (background dark)) (:foreground "magenta4"))
91 (t (:underline t)))
92 "Face for visited Info cross-references."
93 :group 'info)
94
95(defcustom Info-fontify-visited-nodes t
96 "*Non-nil means to fontify visited nodes in a different face."
97 :version "21.4"
98 :type 'boolean
99 :group 'info)
100
88ef7f62 101(defcustom Info-fontify-maximum-menu-size 100000
ccfb7415 102 "*Maximum size of menu to fontify if `font-lock-mode' is non-nil."
ded3e3d8
RS
103 :type 'integer
104 :group 'info)
bdf62a4d 105
50ac70af 106(defcustom Info-use-header-line t
c3717746 107 "*Non-nil means to put the beginning-of-node links in an Emacs header-line.
50ac70af
MB
108A header-line does not scroll with the rest of the buffer."
109 :type 'boolean
110 :group 'info)
111
112(defface info-header-xref
52cbdfbd 113 '((t (:inherit info-xref)))
50ac70af
MB
114 "Face for Info cross-references in a node header."
115 :group 'info)
116
117(defface info-header-node
4ad6e379 118 '((t (:inherit info-node)))
50ac70af
MB
119 "Face for Info nodes in a node header."
120 :group 'info)
121
7d3766a8 122(defvar Info-directory-list nil
a384cab3 123 "List of directories to search for Info documentation files.
fc4d14c5 124If nil, meaning not yet initialized, Info uses the environment
44c327f9 125variable INFOPATH to initialize it, or `Info-default-directory-list'
dde7f979
SM
126if there is no INFOPATH variable in the environment, or the
127concatenation of the two if INFOPATH ends with a colon.
778911b9
EZ
128
129When `Info-directory-list' is initialized from the value of
4dddb0b7
EZ
130`Info-default-directory-list', and Emacs is installed in one of the
131standard directories, the directory of Info files that come with Emacs
132is put last (so that local Info files override standard ones).
133
134When `Info-directory-list' is initialized from the value of
135`Info-default-directory-list', and Emacs is not installed in one
136of the standard directories, the first element of the resulting
778911b9
EZ
137list is the directory where Emacs installs the Info files that
138come with it. This is so that Emacs's own manual, which suits the
4dddb0b7
EZ
139version of Emacs you are using, will always be found first. This
140is useful when you install an experimental version of Emacs without
141removing the standard installation.
142
143If you want to override the order of directories in
144`Info-default-directory-list', set INFOPATH in the environment.
5da0f945
RS
145
146If you run the Emacs executable from the `src' directory in the Emacs
778911b9
EZ
147source tree, and INFOPATH is not defined, the `info' directory in the
148source tree is used as the first element of `Info-directory-list', in
149place of the installation Info directory. This is useful when you run
150a version of Emacs without installing it.")
a384cab3 151
ded3e3d8 152(defcustom Info-additional-directory-list nil
bdf62a4d 153 "List of additional directories to search for Info documentation files.
e9b1d996 154These directories are searched after those in `Info-directory-list'."
ded3e3d8
RS
155 :type '(repeat directory)
156 :group 'info)
bdf62a4d 157
610bc96b 158(defcustom Info-scroll-prefer-subnodes nil
7a53d8c8
EZ
159 "*If non-nil, \\<Info-mode-map>\\[Info-scroll-up] in a menu visits subnodes.
160If this is non-nil, and you scroll far enough in a node that its menu
161appears on the screen, the next \\<Info-mode-map>\\[Info-scroll-up]
162moves to a subnode indicated by the following menu item. This means
163that you visit a subnode before getting to the end of the menu.
164
165Setting this option to nil results in behavior similar to the stand-alone
166Info reader program, which visits the first subnode from the menu only
167when you hit the end of the current node."
b52a319d 168 :version "21.4"
7a53d8c8
EZ
169 :type 'boolean
170 :group 'info)
171
a9efebd0 172(defcustom Info-hide-note-references t
5f812a3c 173 "*If non-nil, hide the tag and section reference in *note and * menu items.
8a7757f6
JL
174If value is non-nil but not `hide', also replaces the \"*note\" with \"see\".
175If value is non-nil but not t or `hide', the reference section is still shown."
5f812a3c 176 :version "21.4"
8a7757f6 177 :type '(choice (const :tag "No hiding" nil)
0452a274 178 (const :tag "Replace tag and hide reference" t)
8a7757f6
JL
179 (const :tag "Hide tag and reference" hide)
180 (other :tag "Only replace tag" tag))
a9efebd0
KS
181 :group 'info)
182
bb1e1730
KS
183(defcustom Info-refill-paragraphs nil
184 "*If non-nil, attempt to refill paragraphs with hidden references.
185This refilling may accidentally remove explicit line breaks in the info
186file, so be prepared for a few surprises if you enable this feature."
187 :version "21.4"
188 :type 'boolean
189 :group 'info)
190
8a7757f6
JL
191(defcustom Info-search-whitespace-regexp "\\\\(?:\\\\s-+\\\\)"
192 "*If non-nil, regular expression to match a sequence of whitespace chars.
193This applies to Info search for regular expressions.
194You might want to use something like \"[ \\t\\r\\n]+\" instead.
195In the Customization buffer, that is `[' followed by a space,
196a tab, a carriage return (control-M), a newline, and `]+'."
197 :type 'regexp
198 :group 'info)
199
dde7f979
SM
200(defcustom Info-mode-hook
201 ;; Try to obey obsolete Info-fontify settings.
202 (unless (and (boundp 'Info-fontify) (null Info-fontify))
203 '(turn-on-font-lock))
8a7757f6
JL
204 "Hooks run when `Info-mode' is called."
205 :type 'hook
206 :group 'info)
207
208(defcustom Info-selection-hook nil
209 "Hooks run when `Info-select-node' is called."
544e5562
CW
210 :type 'hook
211 :group 'info)
212
8a7757f6
JL
213(defvar Info-edit-mode-hook nil
214 "Hooks run when `Info-edit-mode' is called.")
215
a384cab3 216(defvar Info-current-file nil
d6be34f3
RS
217 "Info file that Info is now looking at, or nil.
218This is the name that was specified in Info, not the actual file name.
4fceda3c
SM
219It doesn't contain directory names or file name extensions added by Info.
220Can also be t when using `Info-on-current-buffer'.")
a384cab3
JB
221
222(defvar Info-current-subfile nil
fdf4b680
DL
223 "Info subfile that is actually in the *info* buffer now.
224nil if current info file is not split into subfiles.")
a384cab3
JB
225
226(defvar Info-current-node nil
227 "Name of node that Info is now looking at, or nil.")
228
c5fe2ff1 229(defvar Info-tag-table-marker nil
a384cab3
JB
230 "Marker pointing at beginning of current Info file's tag table.
231Marker points nowhere if file has no tag table.")
232
c5fe2ff1
RS
233(defvar Info-tag-table-buffer nil
234 "Buffer used for indirect tag tables.")
235
552775bd
RS
236(defvar Info-current-file-completions nil
237 "Cached completion list for current Info file.")
238
1143a6b0 239(defvar Info-index-alternatives nil
fdf4b680 240 "List of possible matches for last `Info-index' command.")
1143a6b0 241
8a7757f6
JL
242(defvar Info-reference-name nil
243 "Name of the selected cross-reference.
244Point is moved to the proper occurrence of this name within a node
245after selecting it.")
246
552775bd
RS
247(defvar Info-standalone nil
248 "Non-nil if Emacs was started solely as an Info browser.")
7d3766a8 249\f
bd0c9168 250(defvar Info-suffix-list
3c19e6c1
RS
251 ;; The MS-DOS list should work both when long file names are
252 ;; supported (Windows 9X), and when only 8+3 file names are available.
bd0c9168
RS
253 (if (eq system-type 'ms-dos)
254 '( (".gz" . "gunzip")
255 (".z" . "gunzip")
544e210f 256 (".bz2" . ("bzip2" "-dc"))
3c19e6c1
RS
257 (".inz" . "gunzip")
258 (".igz" . "gunzip")
259 (".info.Z" . "gunzip")
260 (".info.gz" . "gunzip")
261 ("-info.Z" . "gunzip")
262 ("-info.gz" . "gunzip")
263 ("/index.gz". "gunzip")
264 ("/index.z" . "gunzip")
9b842cab 265 (".inf" . nil)
3c19e6c1
RS
266 (".info" . nil)
267 ("-info" . nil)
268 ("/index" . nil)
bd0c9168 269 ("" . nil))
a70dc410
RS
270 '( (".info.Z". "uncompress")
271 (".info.Y". "unyabba")
272 (".info.gz". "gunzip")
273 (".info.z". "gunzip")
544e210f 274 (".info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
275 (".info". nil)
276 ("-info.Z". "uncompress")
277 ("-info.Y". "unyabba")
278 ("-info.gz". "gunzip")
544e210f 279 ("-info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
280 ("-info.z". "gunzip")
281 ("-info". nil)
282 ("/index.Z". "uncompress")
283 ("/index.Y". "unyabba")
284 ("/index.gz". "gunzip")
285 ("/index.z". "gunzip")
544e210f 286 ("/index.bz2". ("bzip2" "-dc"))
a70dc410
RS
287 ("/index". nil)
288 (".Z". "uncompress")
289 (".Y". "unyabba")
290 (".gz". "gunzip")
291 (".z". "gunzip")
544e210f 292 (".bz2" . ("bzip2" "-dc"))
a70dc410 293 ("". nil)))
1143a6b0
ER
294 "List of file name suffixes and associated decoding commands.
295Each entry should be (SUFFIX . STRING); the file is given to
544e210f
KH
296the command as standard input.
297
298STRING may be a list of strings. In that case, the first element is
299the command name, and the rest are arguments to that command.
300
301If STRING is nil, no decoding is done.
97f99202
KH
302Because the SUFFIXes are tried in order, the empty string should
303be last in the list.")
1143a6b0 304
c8e9dd54 305;; Concatenate SUFFIX onto FILENAME. SUFFIX should start with a dot.
e0c1e774
EZ
306;; First, on MS-DOS with no long file names support, delete some of
307;; the extension in FILENAME to make room.
308(defun info-insert-file-contents-1 (filename suffix lfn)
309 (if lfn ; long file names are supported
bd0c9168
RS
310 (concat filename suffix)
311 (let* ((sans-exts (file-name-sans-extension filename))
c8e9dd54
RS
312 ;; How long is the extension in FILENAME (not counting the dot).
313 (ext-len (max 0 (- (length filename) (length sans-exts) 1)))
314 ext-left)
9b842cab 315 ;; SUFFIX starts with a dot. If FILENAME already has one,
1583fe94 316 ;; get rid of the one in SUFFIX (unless suffix is empty).
c8e9dd54 317 (or (and (<= ext-len 0)
9b842cab 318 (not (eq (aref filename (1- (length filename))) ?.)))
1583fe94 319 (= (length suffix) 0)
9b842cab 320 (setq suffix (substring suffix 1)))
c8e9dd54
RS
321 ;; How many chars of that extension should we keep?
322 (setq ext-left (min ext-len (max 0 (- 3 (length suffix)))))
bd0c9168
RS
323 ;; Get rid of the rest of the extension, and add SUFFIX.
324 (concat (substring filename 0 (- (length filename)
325 (- ext-len ext-left)))
326 suffix))))
327
9c7924d5
RS
328(defun info-file-exists-p (filename)
329 (and (file-exists-p filename)
330 (not (file-directory-p filename))))
331
1143a6b0
ER
332(defun info-insert-file-contents (filename &optional visit)
333 "Insert the contents of an info file in the current buffer.
334Do the right thing if the file has been compressed or zipped."
e0c1e774 335 (let* ((tail Info-suffix-list)
ab5a535c
KS
336 (lfn (if (fboundp 'msdos-long-file-names)
337 (msdos-long-file-names)
338 t))
e0c1e774
EZ
339 (check-short (and (fboundp 'msdos-long-file-names)
340 lfn))
341 fullname decoder done)
b8fa4e87 342 (if (info-file-exists-p filename)
bd0c9168
RS
343 ;; FILENAME exists--see if that name contains a suffix.
344 ;; If so, set DECODE accordingly.
97f99202
KH
345 (progn
346 (while (and tail
347 (not (string-match
348 (concat (regexp-quote (car (car tail))) "$")
349 filename)))
350 (setq tail (cdr tail)))
351 (setq fullname filename
352 decoder (cdr (car tail))))
bd0c9168 353 ;; Try adding suffixes to FILENAME and see if we can find something.
e0c1e774
EZ
354 (while (and tail (not done))
355 (setq fullname (info-insert-file-contents-1 filename
356 (car (car tail)) lfn))
357 (if (info-file-exists-p fullname)
358 (setq done t
359 ;; If we found a file with a suffix, set DECODER
360 ;; according to the suffix.
361 decoder (cdr (car tail)))
362 ;; When the MS-DOS port runs on Windows, we need to check
363 ;; the short variant of a long file name as well.
364 (when check-short
365 (setq fullname (info-insert-file-contents-1 filename
366 (car (car tail)) nil))
367 (if (info-file-exists-p fullname)
368 (setq done t
369 decoder (cdr (car tail))))))
97f99202 370 (setq tail (cdr tail)))
97f99202 371 (or tail
bd0c9168 372 (error "Can't find %s or any compressed version of it" filename)))
e175bda2
RS
373 ;; check for conflict with jka-compr
374 (if (and (featurep 'jka-compr)
375 (jka-compr-installed-p)
376 (jka-compr-get-compression-info fullname))
377 (setq decoder nil))
97f99202 378 (if decoder
3c19e6c1
RS
379 (progn
380 (insert-file-contents-literally fullname visit)
381 (let ((buffer-read-only nil)
382 (coding-system-for-write 'no-conversion)
383 (default-directory (or (file-name-directory fullname)
384 default-directory)))
544e210f
KH
385 (or (consp decoder)
386 (setq decoder (list decoder)))
387 (apply 'call-process-region (point-min) (point-max)
388 (car decoder) t t nil (cdr decoder))))
3c19e6c1 389 (insert-file-contents fullname visit))))
7d3766a8 390\f
3a59b662
SM
391(defun Info-default-dirs ()
392 (let ((source (expand-file-name "info/" source-directory))
393 (sibling (if installation-directory
394 (expand-file-name "info/" installation-directory)
395 (if invocation-directory
396 (let ((infodir (expand-file-name
397 "../info/"
398 invocation-directory)))
399 (if (file-exists-p infodir)
400 infodir
401 (setq infodir (expand-file-name
402 "../../../info/"
403 invocation-directory))
404 (and (file-exists-p infodir)
405 infodir))))))
406 alternative)
407 (setq alternative
408 (if (and sibling (file-exists-p sibling))
409 ;; Uninstalled, Emacs builddir != srcdir.
410 sibling
411 ;; Uninstalled, builddir == srcdir
412 source))
413 (if (or (member alternative Info-default-directory-list)
414 ;; On DOS/NT, we use movable executables always,
415 ;; and we must always find the Info dir at run time.
416 (if (memq system-type '(ms-dos windows-nt))
417 nil
418 ;; Use invocation-directory for Info
419 ;; only if we used it for exec-directory also.
420 (not (string= exec-directory
421 (expand-file-name "lib-src/"
422 installation-directory))))
423 (not (file-exists-p alternative)))
424 Info-default-directory-list
425 ;; `alternative' contains the Info files that came with this
426 ;; version, so we should look there first. `Info-insert-dir'
427 ;; currently expects to find `alternative' first on the list.
428 (cons alternative
a025fccf
SM
429 ;; Don't drop the last part, it might contain non-Emacs stuff.
430 ;; (reverse (cdr (reverse
431 Info-default-directory-list)))) ;; )))
3a59b662 432
7d3766a8 433(defun info-initialize ()
fdf4b680 434 "Initialize `Info-directory-list', if that hasn't been done yet."
7d3766a8 435 (unless Info-directory-list
3a59b662 436 (let ((path (getenv "INFOPATH")))
7d3766a8 437 (setq Info-directory-list
78e7e8a0
MB
438 (prune-directory-list
439 (if path
3a59b662
SM
440 (if (string-match ":\\'" path)
441 (append (split-string (substring path 0 -1)
442 (regexp-quote path-separator))
443 (Info-default-dirs))
444 (split-string path (regexp-quote path-separator)))
445 (Info-default-dirs)))))))
1143a6b0 446
a6e4564e
RS
447;;;###autoload
448(defun info-other-window (&optional file)
449 "Like `info' but show the Info buffer in another window."
450 (interactive (if current-prefix-arg
451 (list (read-file-name "Info file name: " nil nil t))))
452 (let (same-window-buffer-names)
453 (info file)))
399c88ad 454
d282974b 455;;;###autoload (add-hook 'same-window-buffer-names "*info*")
211d6309 456
a384cab3
JB
457;;;###autoload
458(defun info (&optional file)
459 "Enter Info, the documentation browser.
460Optional argument FILE specifies the file to examine;
461the default is the top-level directory of Info.
83d5c772
DL
462Called from a program, FILE may specify an Info node of the form
463`(FILENAME)NODENAME'.
a384cab3
JB
464
465In interactive use, a prefix argument directs this command
e341dab2
RS
466to read a file name from the minibuffer.
467
468The search path for Info files is in the variable `Info-directory-list'.
399c88ad 469The top-level Info directory is made by combining all the files named `dir'
e341dab2 470in all the directories in that path."
a384cab3
JB
471 (interactive (if current-prefix-arg
472 (list (read-file-name "Info file name: " nil nil t))))
60705054 473 (pop-to-buffer "*info*")
a384cab3 474 (if file
60705054
SM
475 ;; If argument already contains parentheses, don't add another set
476 ;; since the argument will then be parsed improperly. This also
477 ;; has the added benefit of allowing node names to be included
478 ;; following the parenthesized filename.
479 (if (and (stringp file) (string-match "(.*)" file))
480 (Info-goto-node file)
481 (Info-goto-node (concat "(" file ")")))
482 (if (zerop (buffer-size))
bf7d6fa7 483 (Info-directory))))
a384cab3 484
c3717746
RS
485;;;###autoload
486(defun info-emacs-manual ()
3a8803c0 487 "Display the Emacs manual in Info mode."
c3717746
RS
488 (interactive)
489 (info "emacs"))
490
552775bd
RS
491;;;###autoload
492(defun info-standalone ()
493 "Run Emacs as a standalone Info reader.
494Usage: emacs -f info-standalone [filename]
495In standalone mode, \\<Info-mode-map>\\[Info-exit] exits Emacs itself."
496 (setq Info-standalone t)
497 (if (and command-line-args-left
498 (not (string-match "^-" (car command-line-args-left))))
499 (condition-case err
500 (progn
501 (info (car command-line-args-left))
502 (setq command-line-args-left (cdr command-line-args-left)))
503 (error (send-string-to-terminal
504 (format "%s\n" (if (eq (car-safe err) 'error)
505 (nth 1 err) err)))
506 (save-buffers-kill-emacs)))
507 (info)))
7d3766a8 508\f
983dfbf8 509;; See if the accessible portion of the buffer begins with a node
11638562
EZ
510;; delimiter, and the node header line which follows matches REGEXP.
511;; Typically, this test will be followed by a loop that examines the
512;; rest of the buffer with (search-forward "\n\^_"), and it's a pity
513;; to have the overhead of this special test inside the loop.
514
515;; This function changes match-data, but supposedly the caller might
516;; want to use the results of re-search-backward.
517
518;; The return value is the value of point at the beginning of matching
983dfbf8 519;; REGEXP, if the function succeeds, nil otherwise.
11638562 520(defun Info-node-at-bob-matching (regexp)
760d5cb3
GM
521 (and (bobp) ; are we at beginning of buffer?
522 (looking-at "\^_") ; does it begin with node delimiter?
11638562
EZ
523 (let (beg)
524 (forward-line 1)
525 (setq beg (point))
760d5cb3 526 (forward-line 1) ; does the line after delimiter match REGEXP?
11638562
EZ
527 (re-search-backward regexp beg t))))
528
8a7757f6
JL
529(defun Info-find-file (filename &optional noerror)
530 "Return expanded FILENAME, or t, if FILENAME is \"dir\".
531Optional second argument NOERROR, if t, means if file is not found
532just return nil (no error)."
a384cab3
JB
533 ;; Convert filename to lower case if not found as specified.
534 ;; Expand it.
4fceda3c 535 (if (stringp filename)
a384cab3 536 (let (temp temp-downcase found)
37a3ff5b 537 (setq filename (substitute-in-file-name filename))
bf7d6fa7 538 (cond
ab2f22ad
JH
539 ((string= (downcase filename) "dir")
540 (setq found t))
541 ((string= filename "apropos")
542 (setq found 'apropos))
bf7d6fa7
JL
543 ((string= filename "history")
544 (setq found 'history))
545 ((string= filename "toc")
546 (setq found 'toc))
ab2f22ad
JH
547 (t
548 (let ((dirs (if (string-match "^\\./" filename)
37a3ff5b
RS
549 ;; If specified name starts with `./'
550 ;; then just try current directory.
551 '("./")
552 (if (file-name-absolute-p filename)
553 ;; No point in searching for an
554 ;; absolute file name
555 '(nil)
556 (if Info-additional-directory-list
557 (append Info-directory-list
558 Info-additional-directory-list)
559 Info-directory-list)))))
560 ;; Search the directory list for file FILENAME.
561 (while (and dirs (not found))
562 (setq temp (expand-file-name filename (car dirs)))
563 (setq temp-downcase
564 (expand-file-name (downcase filename) (car dirs)))
565 ;; Try several variants of specified name.
e0c1e774 566 (let ((suffix-list Info-suffix-list)
ab5a535c
KS
567 (lfn (if (fboundp 'msdos-long-file-names)
568 (msdos-long-file-names)
569 t)))
37a3ff5b
RS
570 (while (and suffix-list (not found))
571 (cond ((info-file-exists-p
572 (info-insert-file-contents-1
e0c1e774 573 temp (car (car suffix-list)) lfn))
37a3ff5b
RS
574 (setq found temp))
575 ((info-file-exists-p
576 (info-insert-file-contents-1
e0c1e774
EZ
577 temp-downcase (car (car suffix-list)) lfn))
578 (setq found temp-downcase))
579 ((and (fboundp 'msdos-long-file-names)
580 lfn
581 (info-file-exists-p
582 (info-insert-file-contents-1
583 temp (car (car suffix-list)) nil)))
584 (setq found temp)))
37a3ff5b 585 (setq suffix-list (cdr suffix-list))))
ab2f22ad 586 (setq dirs (cdr dirs))))))
37a3ff5b
RS
587 (if found
588 (setq filename found)
8a7757f6
JL
589 (if noerror
590 (setq filename nil)
591 (error "Info file %s does not exist" filename)))
592 filename)))
593
594(defun Info-find-node (filename nodename &optional no-going-back)
595 "Go to an info node specified as separate FILENAME and NODENAME.
596NO-GOING-BACK is non-nil if recovering from an error in this function;
597it says do not attempt further (recursive) error recovery."
598 (info-initialize)
599 (setq filename (Info-find-file filename))
37a3ff5b
RS
600 ;; Record the node we are leaving.
601 (if (and Info-current-file (not no-going-back))
602 (setq Info-history
603 (cons (list Info-current-file Info-current-node (point))
604 Info-history)))
a384cab3 605 ;; Go into info buffer.
c5fe2ff1 606 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
4fceda3c
SM
607 (Info-find-node-2 filename nodename no-going-back))
608
609(defun Info-on-current-buffer (&optional nodename)
610 "Use the `Info-mode' to browse the current info buffer.
611If a prefix arg is provided, it queries for the NODENAME which
07e91aa3 612else defaults to \"Top\"."
4fceda3c
SM
613 (interactive
614 (list (if current-prefix-arg
615 (completing-read "Node name: " (Info-build-node-completions)
07e91aa3
SM
616 nil t "Top"))))
617 (unless nodename (setq nodename "Top"))
75296efc 618 (info-initialize)
4fceda3c
SM
619 (Info-mode)
620 (set (make-local-variable 'Info-current-file) t)
621 (Info-find-node-2 nil nodename))
622
f584b4c7
RS
623;; It's perhaps a bit nasty to kill the *info* buffer to force a re-read,
624;; but at least it keeps this routine (which is only for the benefit of
625;; makeinfo-buffer) out of the way of normal operations.
626;;
627(defun Info-revert-find-node (filename nodename)
628 "Go to an info node FILENAME and NODENAME, re-reading disk contents.
629When *info* is already displaying FILENAME and NODENAME, the window position
630is preserved, if possible."
631 (pop-to-buffer "*info*")
632 (let ((old-filename Info-current-file)
633 (old-nodename Info-current-node)
634 (pcolumn (current-column))
635 (pline (count-lines (point-min) (line-beginning-position)))
636 (wline (count-lines (point-min) (window-start)))
637 (old-history Info-history)
638 (new-history (and Info-current-file
639 (list Info-current-file Info-current-node (point)))))
640 (kill-buffer (current-buffer))
641 (Info-find-node filename nodename)
642 (setq Info-history old-history)
643 (if (and (equal old-filename Info-current-file)
644 (equal old-nodename Info-current-node))
645 (progn
646 ;; note goto-line is no good, we want to measure from point-min
647 (beginning-of-buffer)
648 (forward-line wline)
649 (set-window-start (selected-window) (point))
650 (beginning-of-buffer)
651 (forward-line pline)
652 (move-to-column pcolumn))
653 ;; only add to the history when coming from a different file+node
654 (if new-history
655 (setq Info-history (cons new-history Info-history))))))
656
357d11de
GM
657(defun Info-find-in-tag-table-1 (marker regexp case-fold)
658 "Find a node in a tag table.
659MARKER specifies the buffer and position to start searching at.
660REGEXP is a regular expression matching nodes or references. Its first
661group should match `Node:' or `Ref:'.
662CASE-FOLD t means search for a case-insensitive match.
663If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
664FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
665where the match was found, and MODE is `major-mode' of the buffer in
666which the match was found."
dde7f979 667 (let ((case-fold-search case-fold))
357d11de
GM
668 (save-excursion
669 (set-buffer (marker-buffer marker))
670 (goto-char marker)
921e5fe6 671
357d11de
GM
672 ;; Search tag table
673 (beginning-of-line)
674 (when (re-search-forward regexp nil t)
675 (list (string-equal "Ref:" (match-string 1))
365d2503 676 (+ (point-min) (read (current-buffer)))
357d11de
GM
677 major-mode)))))
678
679(defun Info-find-in-tag-table (marker regexp)
680 "Find a node in a tag table.
681MARKER specifies the buffer and position to start searching at.
682REGEXP is a regular expression matching nodes or references. Its first
683group should match `Node:' or `Ref:'.
684If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
685FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
686where the match was found, and MODE is `major-mode' of the buffer in
687which the match was found.
688This function tries to find a case-sensitive match first, then a
689case-insensitive match is tried."
690 (let ((result (Info-find-in-tag-table-1 marker regexp nil)))
691 (when (null (car result))
692 (setq result (Info-find-in-tag-table-1 marker regexp t)))
693 result))
694
695(defun Info-find-node-in-buffer-1 (regexp case-fold)
696 "Find a node or anchor in the current buffer.
697REGEXP is a regular expression matching nodes or references. Its first
698group should match `Node:' or `Ref:'.
699CASE-FOLD t means search for a case-insensitive match.
700Value is the position at which a match was found, or nil if not found."
701 (let ((case-fold-search case-fold)
702 found)
703 (save-excursion
704 (when (Info-node-at-bob-matching regexp)
705 (setq found (point)))
706 (while (and (not found)
707 (search-forward "\n\^_" nil t))
708 (forward-line 1)
709 (let ((beg (point)))
710 (forward-line 1)
711 (when (re-search-backward regexp beg t)
712 (beginning-of-line)
713 (setq found (point)))))
714 found)))
921e5fe6 715
357d11de
GM
716(defun Info-find-node-in-buffer (regexp)
717 "Find a node or anchor in the current buffer.
718REGEXP is a regular expression matching nodes or references. Its first
719group should match `Node:' or `Ref:'.
720Value is the position at which a match was found, or nil if not found.
721This function looks for a case-sensitive match first. If none is found,
722a case-insensitive match is tried."
723 (or (Info-find-node-in-buffer-1 regexp nil)
724 (Info-find-node-in-buffer-1 regexp t)))
921e5fe6 725
4fceda3c 726(defun Info-find-node-2 (filename nodename &optional no-going-back)
9e5c2f50 727 (buffer-disable-undo (current-buffer))
a384cab3
JB
728 (or (eq major-mode 'Info-mode)
729 (Info-mode))
730 (widen)
731 (setq Info-current-node nil)
732 (unwind-protect
8bdf4b20
KH
733 (let ((case-fold-search t)
734 anchorpos)
37a3ff5b
RS
735 ;; Switch files if necessary
736 (or (null filename)
737 (equal Info-current-file filename)
738 (let ((buffer-read-only nil))
739 (setq Info-current-file nil
740 Info-current-subfile nil
741 Info-current-file-completions nil
742 buffer-file-name nil)
743 (erase-buffer)
ab2f22ad
JH
744 (cond
745 ((eq filename t)
746 (Info-insert-dir))
747 ((eq filename 'apropos)
748 (insert-buffer-substring " *info-apropos*"))
bf7d6fa7
JL
749 ((eq filename 'history)
750 (insert-buffer-substring " *info-history*"))
751 ((eq filename 'toc)
752 (insert-buffer-substring " *info-toc*"))
ab2f22ad 753 (t
f7d5479b 754 (info-insert-file-contents filename nil)
ab2f22ad 755 (setq default-directory (file-name-directory filename))))
37a3ff5b
RS
756 (set-buffer-modified-p nil)
757 ;; See whether file has a tag table. Record the location if yes.
758 (goto-char (point-max))
759 (forward-line -8)
760 ;; Use string-equal, not equal, to ignore text props.
761 (if (not (or (string-equal nodename "*")
762 (not
763 (search-forward "\^_\nEnd tag table\n" nil t))))
764 (let (pos)
765 ;; We have a tag table. Find its beginning.
766 ;; Is this an indirect file?
767 (search-backward "\nTag table:\n")
768 (setq pos (point))
769 (if (save-excursion
770 (forward-line 2)
771 (looking-at "(Indirect)\n"))
772 ;; It is indirect. Copy it to another buffer
773 ;; and record that the tag table is in that buffer.
774 (let ((buf (current-buffer))
775 (tagbuf
776 (or Info-tag-table-buffer
777 (generate-new-buffer " *info tag table*"))))
778 (setq Info-tag-table-buffer tagbuf)
779 (save-excursion
780 (set-buffer tagbuf)
9e5c2f50 781 (buffer-disable-undo (current-buffer))
37a3ff5b
RS
782 (setq case-fold-search t)
783 (erase-buffer)
784 (insert-buffer-substring buf))
785 (set-marker Info-tag-table-marker
786 (match-end 0) tagbuf))
787 (set-marker Info-tag-table-marker pos)))
788 (set-marker Info-tag-table-marker nil))
789 (setq Info-current-file
ab2f22ad
JH
790 (cond
791 ((eq filename t) "dir")
792 ((eq filename 'apropos) "apropos")
bf7d6fa7
JL
793 ((eq filename 'history) "history")
794 ((eq filename 'toc) "toc")
ab2f22ad
JH
795 (t filename)))
796 ))
37a3ff5b
RS
797 ;; Use string-equal, not equal, to ignore text props.
798 (if (string-equal nodename "*")
799 (progn (setq Info-current-node nodename)
800 (Info-set-mode-line))
801 ;; Possibilities:
802 ;;
803 ;; 1. Anchor found in tag table
804 ;; 2. Anchor *not* in tag table
805 ;;
806 ;; 3. Node found in tag table
807 ;; 4. Node *not* found in tag table, but found in file
808 ;; 5. Node *not* in tag table, and *not* in file
809 ;;
810 ;; *Or* the same, but in an indirect subfile.
811
812 ;; Search file for a suitable node.
8bdf4b20 813 (let ((guesspos (point-min))
357d11de 814 (regexp (concat "\\(Node:\\|Ref:\\) *\\("
4ce5c223 815 (if (stringp nodename)
dd31e4e8
GM
816 (regexp-quote nodename)
817 "")
dde7f979 818 "\\) *[,\t\n\177]")))
37a3ff5b 819
82172606 820 (catch 'foo
921e5fe6 821
82172606 822 ;; First, search a tag table, if any
357d11de
GM
823 (when (marker-position Info-tag-table-marker)
824 (let* ((m Info-tag-table-marker)
825 (found (Info-find-in-tag-table m regexp)))
921e5fe6 826
357d11de
GM
827 (when found
828 ;; FOUND is (ANCHOR POS MODE).
829 (setq guesspos (nth 1 found))
921e5fe6 830
357d11de
GM
831 ;; If this is an indirect file, determine which
832 ;; file really holds this node and read it in.
833 (unless (eq (nth 2 found) 'Info-mode)
834 ;; Note that the current buffer must be the
835 ;; *info* buffer on entry to
836 ;; Info-read-subfile. Thus the hackery above.
837 (setq guesspos (Info-read-subfile guesspos)))
82172606
DL
838
839 ;; Handle anchor
357d11de
GM
840 (when (nth 0 found)
841 (goto-char (setq anchorpos guesspos))
842 (throw 'foo t)))))
82172606
DL
843
844 ;; Else we may have a node, which we search for:
845 (goto-char (max (point-min)
846 (- (byte-to-position guesspos) 1000)))
921e5fe6 847
357d11de
GM
848 ;; Now search from our advised position (or from beg of
849 ;; buffer) to find the actual node. First, check
850 ;; whether the node is right where we are, in case the
851 ;; buffer begins with a node.
852 (let ((pos (Info-find-node-in-buffer regexp)))
853 (when pos
854 (goto-char pos)
855 (throw 'foo t))
bf7d6fa7
JL
856 ;; No such anchor in tag table or node in tag table or file
857 (error "No such node or anchor: %s" nodename)))
82172606
DL
858
859 (Info-select-node)
8a7757f6
JL
860 (goto-char (point-min))
861 (cond (anchorpos
862 (let ((new-history (list Info-current-file
863 (substring-no-properties nodename))))
864 ;; Add anchors to the history too
865 (setq Info-history-list
866 (cons new-history
867 (delete new-history Info-history-list))))
868 (goto-char anchorpos))
869 (Info-reference-name
870 (Info-find-index-name Info-reference-name)
871 (setq Info-reference-name nil))))))
a384cab3
JB
872 ;; If we did not finish finding the specified node,
873 ;; go back to the previous one.
4db579ab 874 (or Info-current-node no-going-back (null Info-history)
37a3ff5b
RS
875 (let ((hist (car Info-history)))
876 (setq Info-history (cdr Info-history))
877 (Info-find-node (nth 0 hist) (nth 1 hist) t)
878 (goto-char (nth 2 hist))))))
a384cab3 879
44c327f9
JB
880;; Cache the contents of the (virtual) dir file, once we have merged
881;; it for the first time, so we can save time subsequently.
7ea13762
RS
882(defvar Info-dir-contents nil)
883
44c327f9
JB
884;; Cache for the directory we decided to use for the default-directory
885;; of the merged dir text.
886(defvar Info-dir-contents-directory nil)
887
c142ab2d
RS
888;; Record the file attributes of all the files from which we
889;; constructed Info-dir-contents.
890(defvar Info-dir-file-attributes nil)
891
4fba3b2c
RS
892(defvar Info-dir-file-name nil)
893
7ea13762 894;; Construct the Info directory node by merging the files named `dir'
44c327f9
JB
895;; from various directories. Set the *info* buffer's
896;; default-directory to the first directory we actually get any text
897;; from.
7ea13762 898(defun Info-insert-dir ()
c142ab2d
RS
899 (if (and Info-dir-contents Info-dir-file-attributes
900 ;; Verify that none of the files we used has changed
901 ;; since we used it.
902 (eval (cons 'and
b461e32d
SM
903 (mapcar (lambda (elt)
904 (let ((curr (file-attributes
905 ;; Handle symlinks
906 (file-truename (car elt)))))
907
908 ;; Don't compare the access time.
909 (if curr (setcar (nthcdr 4 curr) 0))
910 (setcar (nthcdr 4 (cdr elt)) 0)
911 (equal (cdr elt) curr)))
c142ab2d 912 Info-dir-file-attributes))))
4fba3b2c
RS
913 (progn
914 (insert Info-dir-contents)
915 (goto-char (point-min)))
dd31e4e8 916 (let ((dirs (if Info-additional-directory-list
b34dcc82
RS
917 (append Info-directory-list
918 Info-additional-directory-list)
919 Info-directory-list))
1bd19a31 920 (dir-file-attrs nil)
1bcedb3b
RS
921 ;; Bind this in case the user sets it to nil.
922 (case-fold-search t)
4fba3b2c
RS
923 ;; This is set non-nil if we find a problem in some input files.
924 problems
f4008b6e 925 buffers buffer others nodes dirs-done)
44c327f9
JB
926
927 ;; Search the directory list for the directory file.
7ea13762 928 (while dirs
8d1abb42
RS
929 (let ((truename (file-truename (expand-file-name (car dirs)))))
930 (or (member truename dirs-done)
931 (member (directory-file-name truename) dirs-done)
932 ;; Try several variants of specified name.
933 ;; Try upcasing, appending `.info', or both.
825d6f08
RS
934 (let* (file
935 (attrs
936 (or
937 (progn (setq file (expand-file-name "dir" truename))
938 (file-attributes file))
939 (progn (setq file (expand-file-name "DIR" truename))
940 (file-attributes file))
941 (progn (setq file (expand-file-name "dir.info" truename))
942 (file-attributes file))
943 (progn (setq file (expand-file-name "DIR.INFO" truename))
944 (file-attributes file)))))
8d1abb42
RS
945 (setq dirs-done
946 (cons truename
947 (cons (directory-file-name truename)
948 dirs-done)))
825d6f08
RS
949 (if attrs
950 (save-excursion
951 (or buffers
952 (message "Composing main Info directory..."))
2d41cf59 953 (set-buffer (generate-new-buffer " info dir"))
9d499629
RS
954 (condition-case nil
955 (progn
956 (insert-file-contents file)
1bd19a31
MB
957 (set (make-local-variable 'Info-dir-file-name)
958 file)
6af7040d 959 (push (current-buffer) buffers)
1bd19a31 960 (push (cons file attrs) dir-file-attrs))
9d499629 961 (error (kill-buffer (current-buffer))))))))
1bd19a31
MB
962 (unless (cdr dirs)
963 (set (make-local-variable 'Info-dir-contents-directory)
964 (file-name-as-directory (car dirs))))
825d6f08 965 (setq dirs (cdr dirs))))
399c88ad 966
4db579ab 967 (or buffers
81e14cb2 968 (error "Can't find the Info directory node"))
1bd19a31 969
44c327f9 970 ;; Distinguish the dir file that comes with Emacs from all the
f4008b6e 971 ;; others. Yes, that is really what this is supposed to do.
f601cd3e
DL
972 ;; The definition of `Info-directory-list' puts it first on that
973 ;; list and so last in `buffers' at this point.
974 (setq buffer (car (last buffers))
975 others (delq buffer buffers))
44c327f9 976
debcea0c
KH
977 ;; Insert the entire original dir file as a start; note that we've
978 ;; already saved its default directory to use as the default
979 ;; directory for the whole concatenation.
7ea13762 980 (insert-buffer buffer)
44c327f9 981
7ea13762 982 ;; Look at each of the other buffers one by one.
6af7040d
SM
983 (dolist (other others)
984 (let (this-buffer-nodes)
7ea13762 985 ;; In each, find all the menus.
6af7040d 986 (with-current-buffer other
7ea13762
RS
987 (goto-char (point-min))
988 ;; Find each menu, and add an elt to NODES for it.
989 (while (re-search-forward "^\\* Menu:" nil t)
6af7040d
SM
990 (while (and (zerop (forward-line 1)) (eolp)))
991 (let ((beg (point))
992 nodename end)
993 (re-search-backward "^\^_")
7ea13762 994 (search-forward "Node: ")
bf7d6fa7 995 (setq nodename (Info-following-node-name))
3a6ade8a 996 (search-forward "\n\^_" nil 'move)
7ea13762
RS
997 (beginning-of-line)
998 (setq end (point))
6af7040d 999 (push (list nodename other beg end) this-buffer-nodes)))
9f4aee27 1000 (if (assoc-string "top" this-buffer-nodes t)
4fba3b2c
RS
1001 (setq nodes (nconc this-buffer-nodes nodes))
1002 (setq problems t)
6af7040d 1003 (message "No `top' node in %s" Info-dir-file-name)))))
fdf4b680 1004 ;; Add to the main menu a menu item for each other node.
6af7040d 1005 (re-search-forward "^\\* Menu:")
7ea13762
RS
1006 (forward-line 1)
1007 (let ((menu-items '("top"))
3a6ade8a 1008 (end (save-excursion (search-forward "\^_" nil t) (point))))
6af7040d
SM
1009 (dolist (node nodes)
1010 (let ((nodename (car node)))
c3a29d70
RS
1011 (save-excursion
1012 (or (member (downcase nodename) menu-items)
b37daea4 1013 (re-search-forward (concat "^\\* +"
c3a29d70
RS
1014 (regexp-quote nodename)
1015 "::")
1016 end t)
1017 (progn
1018 (insert "* " nodename "::" "\n")
6af7040d 1019 (push nodename menu-items)))))))
7ea13762
RS
1020 ;; Now take each node of each of the other buffers
1021 ;; and merge it into the main buffer.
6af7040d 1022 (dolist (node nodes)
760d5cb3 1023 (let ((case-fold-search t)
6af7040d 1024 (nodename (car node)))
7ea13762
RS
1025 (goto-char (point-min))
1026 ;; Find the like-named node in the main buffer.
ba42ce14 1027 (if (re-search-forward (concat "^\^_.*\n.*Node: "
7ea13762
RS
1028 (regexp-quote nodename)
1029 "[,\n\t]")
1030 nil t)
1031 (progn
3a6ade8a 1032 (search-forward "\n\^_" nil 'move)
25869acf
RS
1033 (beginning-of-line)
1034 (insert "\n"))
7ea13762
RS
1035 ;; If none exists, add one.
1036 (goto-char (point-max))
dfbf6104 1037 (insert "\^_\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
7ea13762
RS
1038 ;; Merge the text from the other buffer's menu
1039 ;; into the menu in the like-named node in the main buffer.
6af7040d
SM
1040 (apply 'insert-buffer-substring (cdr node))))
1041 (Info-dir-remove-duplicates)
7ea13762 1042 ;; Kill all the buffers we just made.
6af7040d 1043 (mapc 'kill-buffer buffers)
4fba3b2c
RS
1044 (goto-char (point-min))
1045 (if problems
1046 (message "Composing main Info directory...problems encountered, see `*Messages*'")
1bd19a31
MB
1047 (message "Composing main Info directory...done"))
1048 (set (make-local-variable 'Info-dir-contents) (buffer-string))
1049 (set (make-local-variable 'Info-dir-file-attributes) dir-file-attrs)))
44c327f9 1050 (setq default-directory Info-dir-contents-directory))
7ea13762 1051
6af7040d
SM
1052(defvar Info-streamline-headings
1053 '(("Emacs" . "Emacs")
1054 ("Programming" . "Programming")
1055 ("Libraries" . "Libraries")
1056 ("World Wide Web\\|Net Utilities" . "Net Utilities"))
1057 "List of elements (RE . NAME) to merge headings matching RE to NAME.")
1058
1059(defun Info-dir-remove-duplicates ()
1060 (let (limit)
1061 (goto-char (point-min))
1062 ;; Remove duplicate headings in the same menu.
1063 (while (search-forward "\n* Menu:" nil t)
1064 (setq limit (save-excursion (search-forward "\n\1f" nil t)))
1065 ;; Look for the next heading to unify.
1066 (while (re-search-forward "^\\(\\w.*\\)\n\\*" limit t)
1067 (let ((name (match-string 1))
1068 (start (match-beginning 0))
1069 (entries nil) re)
1070 ;; Check whether this heading should be streamlined.
1071 (save-match-data
1072 (dolist (x Info-streamline-headings)
1073 (when (string-match (car x) name)
1074 (setq name (cdr x))
1075 (setq re (car x)))))
1076 (if re (replace-match name t t nil 1))
1077 (goto-char (if (re-search-forward "^[^* \n\t]" limit t)
1078 (match-beginning 0)
1079 (or limit (point-max))))
1080 ;; Look for other headings of the same category and merge them.
1081 (save-excursion
1082 (while (re-search-forward "^\\(\\w.*\\)\n\\*" limit t)
1083 (when (if re (save-match-data (string-match re (match-string 1)))
1084 (equal name (match-string 1)))
1085 (forward-line 0)
1086 ;; Delete redundant heading.
1087 (delete-region (match-beginning 0) (point))
1088 ;; Push the entries onto `text'.
1089 (push
1090 (delete-and-extract-region
1091 (point)
1092 (if (re-search-forward "^[^* \n\t]" nil t)
1093 (match-beginning 0)
1094 (or limit (point-max)))) entries))))
1095 ;; Insert the entries just found.
1096 (while (= (line-beginning-position 0) (1- (point)))
1097 (backward-char))
1098 (dolist (entry (nreverse entries))
1099 (insert entry)
1100 (while (= (line-beginning-position 0) (1- (point)))
1101 (delete-region (1- (point)) (point))))
921e5fe6 1102
6af7040d
SM
1103 ;; Now remove duplicate entries under the same heading.
1104 (let ((seen nil)
1105 (limit (point)))
1106 (goto-char start)
1107 (while (re-search-forward "^* \\([^:\n]+:\\(:\\|[^.\n]+\\).\\)"
1108 limit 'move)
1109 (let ((x (match-string 1)))
1110 (if (member-ignore-case x seen)
1111 (delete-region (match-beginning 0)
1112 (progn (re-search-forward "^[^ \t]" nil t)
25046503 1113 (match-beginning 0)))
6af7040d
SM
1114 (push x seen))))))))))
1115
c5fe2ff1
RS
1116;; Note that on entry to this function the current-buffer must be the
1117;; *info* buffer; not the info tags buffer.
a384cab3 1118(defun Info-read-subfile (nodepos)
a361deba
RS
1119 ;; NODEPOS is either a position (in the Info file as a whole,
1120 ;; not relative to a subfile) or the name of a subfile.
a384cab3
JB
1121 (let (lastfilepos
1122 lastfilename)
a361deba
RS
1123 (if (numberp nodepos)
1124 (save-excursion
1125 (set-buffer (marker-buffer Info-tag-table-marker))
1126 (goto-char (point-min))
ba42ce14
EZ
1127 (or (looking-at "\^_")
1128 (search-forward "\n\^_"))
a361deba
RS
1129 (forward-line 2)
1130 (catch 'foo
1131 (while (not (looking-at "\^_"))
1132 (if (not (eolp))
1133 (let ((beg (point))
1134 thisfilepos thisfilename)
1135 (search-forward ": ")
1136 (setq thisfilename (buffer-substring beg (- (point) 2)))
365d2503 1137 (setq thisfilepos (+ (point-min) (read (current-buffer))))
a361deba
RS
1138 ;; read in version 19 stops at the end of number.
1139 ;; Advance to the next line.
1140 (forward-line 1)
1141 (if (> thisfilepos nodepos)
1142 (throw 'foo t))
1143 (setq lastfilename thisfilename)
1144 (setq lastfilepos thisfilepos))
1145 (forward-line 1)))))
1146 (setq lastfilename nodepos)
1147 (setq lastfilepos 0))
c5fe2ff1
RS
1148 ;; Assume previous buffer is in Info-mode.
1149 ;; (set-buffer (get-buffer "*info*"))
a384cab3
JB
1150 (or (equal Info-current-subfile lastfilename)
1151 (let ((buffer-read-only nil))
1152 (setq buffer-file-name nil)
1153 (widen)
1154 (erase-buffer)
1143a6b0 1155 (info-insert-file-contents lastfilename)
a384cab3
JB
1156 (set-buffer-modified-p nil)
1157 (setq Info-current-subfile lastfilename)))
b0e52598
RS
1158 ;; Widen in case we are in the same subfile as before.
1159 (widen)
a384cab3 1160 (goto-char (point-min))
ba42ce14
EZ
1161 (if (looking-at "\^_")
1162 (forward-char 1)
1163 (search-forward "\n\^_"))
a361deba
RS
1164 (if (numberp nodepos)
1165 (+ (- nodepos lastfilepos) (point)))))
a384cab3 1166
ec9b1372 1167(defun Info-unescape-quotes (value)
1ed6e0c3 1168 "Unescape double quotes and backslashes in VALUE."
ec9b1372
KB
1169 (let ((start 0)
1170 (unquote value))
1171 (while (string-match "[^\\\"]*\\(\\\\\\)[\\\\\"]" unquote start)
1172 (setq unquote (replace-match "" t t unquote 1))
1173 (setq start (- (match-end 0) 1)))
1174 unquote))
1175
1176;; As of Texinfo 4.6, makeinfo writes constructs like
1177;; \0\h[image param=value ...\h\0]
1178;; into the Info file for handling images.
1179(defun Info-split-parameter-string (parameter-string)
1180 "Return alist of (\"KEY\" . \"VALUE\") from PARAMETER-STRING; a
1ed6e0c3
JPW
1181whitespace separated list of KEY=VALUE pairs. If VALUE contains
1182whitespace or double quotes, it must be quoted in double quotes and
1183any double quotes or backslashes must be escaped (\\\",\\\\)."
ec9b1372
KB
1184 (let ((start 0)
1185 (parameter-alist))
1186 (while (string-match
1187 "\\s *\\([^=]+\\)=\\(?:\\([^\\s \"]+\\)\\|\\(?:\"\\(\\(?:[^\\\"]\\|\\\\[\\\\\"]\\)*\\)\"\\)\\)"
1188 parameter-string start)
1189 (setq start (match-end 0))
1190 (push (cons (match-string 1 parameter-string)
1191 (or (match-string 2 parameter-string)
1192 (Info-unescape-quotes
1193 (match-string 3 parameter-string))))
1194 parameter-alist))
1195 parameter-alist))
1196
1197(defun Info-display-images-node ()
1198 "Display images in current node."
1199 (save-excursion
1200 (let ((inhibit-read-only t)
dde7f979 1201 (case-fold-search t))
ec9b1372
KB
1202 (goto-char (point-min))
1203 (while (re-search-forward
1204 "\\(\0\b[[]image\\(\\(?:[^\b]\\|[^\0]+\b\\)*\\)\0\b[]]\\)"
1205 nil t)
1206 (let* ((start (match-beginning 1))
1207 (parameter-alist (Info-split-parameter-string (match-string 2)))
1208 (src (cdr (assoc-string "src" parameter-alist)))
1209 (image-file (if src (if (file-name-absolute-p src) src
1210 (concat default-directory src))
1211 ""))
1212 (image (if (file-exists-p image-file)
1213 (create-image image-file)
1214 "[broken image]")))
ec9b1372
KB
1215 (if (not (get-text-property start 'display))
1216 (add-text-properties
1217 start (point) `(display ,image rear-nonsticky (display)))))))
1218 (set-buffer-modified-p nil)))
1219
0c1b7af5
JL
1220;; Texinfo 4.7 adds cookies of the form ^@^H[NAME CONTENTS ^@^H].
1221;; Hide any construct of the general form ^@[^@-^_][ ... ^@[^@-^_]],
1222;; including one optional trailing newline.
1223(defun Info-hide-cookies-node ()
1224 "Hide unrecognised cookies in current node."
1225 (save-excursion
1226 (let ((inhibit-read-only t)
1227 (case-fold-search t))
1228 (goto-char (point-min))
1229 (while (re-search-forward
1230 "\\(\0[\0-\37][[][^\0]*\0[\0-\37][]]\n?\\)"
1231 nil t)
1232 (let* ((start (match-beginning 1)))
1233 (if (not (get-text-property start 'invisible))
1234 (put-text-property start (point) 'invisible t)))))
1235 (set-buffer-modified-p nil)))
1236
a384cab3 1237(defun Info-select-node ()
b038485e
AS
1238 "Select the info node that point is in."
1239 ;; Bind this in case the user sets it to nil.
1bcedb3b
RS
1240 (let ((case-fold-search t))
1241 (save-excursion
760d5cb3
GM
1242 ;; Find beginning of node.
1243 (if (search-backward "\n\^_" nil 'move)
1244 (forward-line 2)
1245 (if (looking-at "\^_")
1246 (forward-line 1)
1247 (signal 'search-failed (list "\n\^_"))))
1248 ;; Get nodename spelled as it is in the node.
1249 (re-search-forward "Node:[ \t]*")
1250 (setq Info-current-node
1251 (buffer-substring-no-properties (point)
1252 (progn
1253 (skip-chars-forward "^,\t\n")
1254 (point))))
1255 (Info-set-mode-line)
1256 ;; Find the end of it, and narrow.
1257 (beginning-of-line)
1258 (let (active-expression)
50ac70af 1259 ;; Narrow to the node contents
760d5cb3
GM
1260 (narrow-to-region (point)
1261 (if (re-search-forward "\n[\^_\f]" nil t)
1262 (prog1
1263 (1- (point))
1264 (if (looking-at "[\n\^_\f]*execute: ")
1265 (progn
1266 (goto-char (match-end 0))
1267 (setq active-expression
1268 (read (current-buffer))))))
1269 (point-max)))
1270 (if Info-enable-active-nodes (eval active-expression))
8a7757f6
JL
1271 ;; Add a new unique history item to full history list
1272 (let ((new-history (list Info-current-file Info-current-node)))
1273 (setq Info-history-list
1274 (cons new-history (delete new-history Info-history-list))))
544e5562 1275 (Info-fontify-node)
ec9b1372 1276 (Info-display-images-node)
0c1b7af5 1277 (Info-hide-cookies-node)
760d5cb3 1278 (run-hooks 'Info-selection-hook)))))
a384cab3
JB
1279
1280(defun Info-set-mode-line ()
1281 (setq mode-line-buffer-identification
313fe58a 1282 (nconc (propertized-buffer-identification "%b")
918e364a
MB
1283 (list
1284 (concat " ("
1285 (file-name-nondirectory
1286 (if (stringp Info-current-file)
1287 Info-current-file
1288 (or buffer-file-name "")))
1289 ") "
1290 (or Info-current-node ""))))))
a384cab3
JB
1291\f
1292;; Go to an info node specified with a filename-and-nodename string
1293;; of the sort that is found in pointers in nodes.
1294
4fceda3c 1295(defun Info-goto-node (nodename &optional fork)
cfc697a2 1296 "Go to info node named NODENAME. Give just NODENAME or (FILENAME)NODENAME.
dc414be1
EZ
1297If NODENAME is of the form (FILENAME)NODENAME, the node is in the Info file
1298FILENAME; otherwise, NODENAME should be in the current Info file (or one of
1299its sub-files).
1300Completion is available, but only for node names in the current Info file.
7210b6f5
DL
1301If FORK is non-nil (interactively with a prefix arg), show the node in
1302a new info buffer.
4fceda3c 1303If FORK is a string, it is the name to use for the new buffer."
d1268e52 1304 (interactive (list (Info-read-node-name "Go to node: ") current-prefix-arg))
d0e41cba 1305 (info-initialize)
4fceda3c 1306 (if fork
760d5cb3
GM
1307 (set-buffer
1308 (clone-buffer (concat "*info-" (if (stringp fork) fork nodename) "*") t)))
a384cab3
JB
1309 (let (filename)
1310 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
1311 nodename)
1312 (setq filename (if (= (match-beginning 1) (match-end 1))
1313 ""
c60c12be
SM
1314 (match-string 2 nodename))
1315 nodename (match-string 3 nodename))
a384cab3
JB
1316 (let ((trim (string-match "\\s *\\'" filename)))
1317 (if trim (setq filename (substring filename 0 trim))))
1318 (let ((trim (string-match "\\s *\\'" nodename)))
1319 (if trim (setq nodename (substring nodename 0 trim))))
e579232c 1320 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
1321 (Info-find-node (if (equal filename "") nil filename)
1322 (if (equal nodename "") "Top" nodename))))
552775bd 1323
ddf89211
RS
1324(defvar Info-read-node-completion-table)
1325
03524be6 1326;; This function is used as the "completion table" while reading a node name.
ddf89211 1327;; It does completion using the alist in Info-read-node-completion-table
03524be6
RS
1328;; unless STRING starts with an open-paren.
1329(defun Info-read-node-name-1 (string predicate code)
365d2503
SM
1330 (cond
1331 ;; First complete embedded file names.
1332 ((string-match "\\`([^)]*\\'" string)
1333 (let ((file (substring string 1)))
1334 (cond
1335 ((eq code nil)
1336 (let ((comp (try-completion file 'locate-file-completion
1337 (cons Info-directory-list
1338 (mapcar 'car Info-suffix-list)))))
1339 (cond
1340 ((eq comp t) (concat string ")"))
1341 (comp (concat "(" comp)))))
1342 ((eq code t) (all-completions file 'locate-file-completion
1343 (cons Info-directory-list
1344 (mapcar 'car Info-suffix-list))))
1345 (t nil))))
1346 ;; If a file name was given, then any node is fair game.
1347 ((string-match "\\`(" string)
1348 (cond
1349 ((eq code nil) string)
1350 ((eq code t) nil)
1351 (t t)))
1352 ;; Otherwise use Info-read-node-completion-table.
1353 ((eq code nil)
1354 (try-completion string Info-read-node-completion-table predicate))
1355 ((eq code t)
1356 (all-completions string Info-read-node-completion-table predicate))
1357 (t
1358 (test-completion string Info-read-node-completion-table predicate))))
03524be6 1359
552775bd
RS
1360(defun Info-read-node-name (prompt &optional default)
1361 (let* ((completion-ignore-case t)
ddf89211 1362 (Info-read-node-completion-table (Info-build-node-completions))
b6d61ffa 1363 (nodename (completing-read prompt 'Info-read-node-name-1 nil t)))
552775bd
RS
1364 (if (equal nodename "")
1365 (or default
1366 (Info-read-node-name prompt))
1367 nodename)))
1368
1369(defun Info-build-node-completions ()
1370 (or Info-current-file-completions
1bcedb3b
RS
1371 (let ((compl nil)
1372 ;; Bind this in case the user sets it to nil.
11638562
EZ
1373 (case-fold-search t)
1374 (node-regexp "Node: *\\([^,\n]*\\) *[,\n\t]"))
552775bd
RS
1375 (save-excursion
1376 (save-restriction
1377 (if (marker-buffer Info-tag-table-marker)
c5fe2ff1
RS
1378 (let ((marker Info-tag-table-marker))
1379 (set-buffer (marker-buffer marker))
cedb118c 1380 (widen)
c5fe2ff1 1381 (goto-char marker)
fdf4b680 1382 (while (re-search-forward "\n\\(Node\\|Ref\\): \\(.*\\)\177" nil t)
552775bd 1383 (setq compl
fdf4b680 1384 (cons (list (match-string-no-properties 2))
552775bd
RS
1385 compl))))
1386 (widen)
1387 (goto-char (point-min))
11638562
EZ
1388 ;; If the buffer begins with a node header, process that first.
1389 (if (Info-node-at-bob-matching node-regexp)
110d4e80 1390 (setq compl (list (match-string-no-properties 1))))
11638562 1391 ;; Now for the rest of the nodes.
552775bd
RS
1392 (while (search-forward "\n\^_" nil t)
1393 (forward-line 1)
1394 (let ((beg (point)))
1395 (forward-line 1)
11638562 1396 (if (re-search-backward node-regexp beg t)
399c88ad 1397 (setq compl
110d4e80 1398 (cons (list (match-string-no-properties 1))
552775bd 1399 compl))))))))
87d2b5a2 1400 (setq compl (cons '("*") compl))
07b1b912 1401 (set (make-local-variable 'Info-current-file-completions) compl))))
a384cab3 1402\f
aea2a8da
JB
1403(defun Info-restore-point (hl)
1404 "If this node has been visited, restore the point value when we left."
cedb118c
RS
1405 (while hl
1406 (if (and (equal (nth 0 (car hl)) Info-current-file)
e90d1271
RS
1407 ;; Use string-equal, not equal, to ignore text props.
1408 (string-equal (nth 1 (car hl)) Info-current-node))
cedb118c 1409 (progn
d96504df
KH
1410 (goto-char (nth 2 (car hl)))
1411 (setq hl nil)) ;terminate the while at next iter
cedb118c 1412 (setq hl (cdr hl)))))
aea2a8da 1413\f
2c80a618
GM
1414(defvar Info-search-history nil
1415 "The history list for `Info-search'.")
a384cab3 1416
8a7757f6
JL
1417(defvar Info-search-case-fold nil
1418 "The value of `case-fold-search' from previous `Info-search' command.")
1419
a384cab3
JB
1420(defun Info-search (regexp)
1421 "Search for REGEXP, starting from point, and select node it's found in."
bbc96600
EZ
1422 (interactive (list (read-string
1423 (if Info-search-history
8a7757f6
JL
1424 (format "Regexp search%s (default `%s'): "
1425 (if case-fold-search "" " case-sensitively")
bbc96600 1426 (car Info-search-history))
8a7757f6
JL
1427 (format "Regexp search%s: "
1428 (if case-fold-search "" " case-sensitively")))
bbc96600 1429 nil 'Info-search-history)))
2c80a618
GM
1430 (when transient-mark-mode
1431 (deactivate-mark))
1432 (when (equal regexp "")
1433 (setq regexp (car Info-search-history)))
c47b5bbe 1434 (when regexp
774f8aee 1435 (let (found beg-found give-up
c47b5bbe
DL
1436 (onode Info-current-node)
1437 (ofile Info-current-file)
1438 (opoint (point))
1439 (ostart (window-start))
1440 (osubfile Info-current-subfile))
8a7757f6
JL
1441 (when Info-search-whitespace-regexp
1442 (setq regexp (replace-regexp-in-string
1443 "[ \t\n]+" Info-search-whitespace-regexp regexp)))
1444 (setq Info-search-case-fold case-fold-search)
c47b5bbe
DL
1445 (save-excursion
1446 (save-restriction
1447 (widen)
774f8aee
RS
1448 (while (and (not give-up)
1449 (or (null found)
1450 (isearch-range-invisible beg-found found)))
1451 (if (re-search-forward regexp nil t)
1452 (setq found (point) beg-found (match-beginning 0))
1453 (setq give-up t)))))
1454 ;; If no subfiles, give error now.
1455 (if give-up
c47b5bbe 1456 (if (null Info-current-subfile)
774f8aee
RS
1457 (re-search-forward regexp)
1458 (setq found nil)))
1459
1460 (unless found
1461 (unwind-protect
1462 ;; Try other subfiles.
1463 (let ((list ()))
1464 (save-excursion
1465 (set-buffer (marker-buffer Info-tag-table-marker))
1466 (goto-char (point-min))
1467 (search-forward "\n\^_\nIndirect:")
1468 (save-restriction
1469 (narrow-to-region (point)
1470 (progn (search-forward "\n\^_")
1471 (1- (point))))
c5fe2ff1 1472 (goto-char (point-min))
774f8aee
RS
1473 ;; Find the subfile we just searched.
1474 (search-forward (concat "\n" osubfile ": "))
1475 ;; Skip that one.
1476 (forward-line 1)
1477 ;; Make a list of all following subfiles.
1478 ;; Each elt has the form (VIRT-POSITION . SUBFILENAME).
1479 (while (not (eobp))
1480 (re-search-forward "\\(^.*\\): [0-9]+$")
1481 (goto-char (+ (match-end 1) 2))
1482 (setq list (cons (cons (+ (point-min)
1483 (read (current-buffer)))
1484 (match-string-no-properties 1))
1485 list))
1486 (goto-char (1+ (match-end 0))))
1487 ;; Put in forward order
1488 (setq list (nreverse list))))
1489 (while list
1490 (message "Searching subfile %s..." (cdr (car list)))
1491 (Info-read-subfile (car (car list)))
1492 (setq list (cdr list))
1493 (setq give-up nil found nil)
1494 (while (and (not give-up)
1495 (or (null found)
1496 (isearch-range-invisible beg-found found)))
c47b5bbe 1497 (if (re-search-forward regexp nil t)
774f8aee
RS
1498 (setq found (point) beg-found (match-beginning 0))
1499 (setq give-up t)))
1500 (if give-up
1501 (setq found nil))
c47b5bbe 1502 (if found
774f8aee
RS
1503 (setq list nil)))
1504 (if found
1505 (message "")
1506 (signal 'search-failed (list regexp))))
1507 (if (not found)
1508 (progn (Info-read-subfile osubfile)
1509 (goto-char opoint)
1510 (Info-select-node)
1511 (set-window-start (selected-window) ostart)))))
760d5cb3
GM
1512 (widen)
1513 (goto-char found)
1514 (Info-select-node)
1515 ;; Use string-equal, not equal, to ignore text props.
1516 (or (and (string-equal onode Info-current-node)
1517 (equal ofile Info-current-file))
1518 (setq Info-history (cons (list ofile onode opoint)
1519 Info-history))))))
8a7757f6
JL
1520
1521(defun Info-search-case-sensitively ()
1522 "Search for a regexp case-sensitively."
1523 (interactive)
1524 (let ((case-fold-search nil))
1525 (call-interactively 'Info-search)))
1526
1527(defun Info-search-next ()
1528 "Search for next regexp from a previous `Info-search' command."
1529 (interactive)
1530 (let ((case-fold-search Info-search-case-fold))
1531 (if Info-search-history
1532 (Info-search (car Info-search-history))
1533 (call-interactively 'Info-search))))
a384cab3 1534\f
a384cab3 1535(defun Info-extract-pointer (name &optional errorname)
fdf4b680
DL
1536 "Extract the value of the node-pointer named NAME.
1537If there is none, use ERRORNAME in the error message;
c60c12be
SM
1538if ERRORNAME is nil, just return nil."
1539 ;; Bind this in case the user sets it to nil.
1bcedb3b
RS
1540 (let ((case-fold-search t))
1541 (save-excursion
c60c12be
SM
1542 (goto-char (point-min))
1543 (let ((bound (point)))
1544 (forward-line 1)
1545 (cond ((re-search-backward
1546 (concat name ":" (Info-following-node-name-re)) bound t)
1547 (match-string 1))
1548 ((not (eq errorname t))
1549 (error "Node has no %s"
1550 (capitalize (or errorname name)))))))))
1551
1552(defun Info-following-node-name-re (&optional allowedchars)
1553 "Return a regexp matching a node name.
fdf4b680 1554ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
c60c12be
SM
1555saying which chars may appear in the node name.
1556Submatch 1 is the complete node name.
1557Submatch 2 if non-nil is the parenthesized file name part of the node name.
1558Submatch 3 is the local part of the node name.
1559End of submatch 0, 1, and 3 are the same, so you can safely concat."
1560 (concat "[ \t]*" ;Skip leading space.
1561 "\\(\\(([^)]+)\\)?" ;Node name can start with a file name.
1562 "\\([" (or allowedchars "^,\t\n") "]*" ;Any number of allowed chars.
1563 "[" (or allowedchars "^,\t\n") " ]" ;The last char can't be a space.
1564 "\\|\\)\\)")) ;Allow empty node names.
a384cab3 1565
718e6bc7
RS
1566;;; For compatibility; other files have used this name.
1567(defun Info-following-node-name ()
1568 (and (looking-at (Info-following-node-name-re))
1569 (match-string 1)))
1570
a384cab3
JB
1571(defun Info-next ()
1572 "Go to the next node of this node."
1573 (interactive)
1574 (Info-goto-node (Info-extract-pointer "next")))
1575
1576(defun Info-prev ()
1577 "Go to the previous node of this node."
1578 (interactive)
1579 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
1580
5dab2fb4
RS
1581(defun Info-up (&optional same-file)
1582 "Go to the superior node of this node.
1583If SAME-FILE is non-nil, do not move to a different Info file."
a384cab3 1584 (interactive)
8a7757f6
JL
1585 (let ((old-node Info-current-node)
1586 (old-file Info-current-file)
1587 (node (Info-extract-pointer "up")) p)
4fceda3c 1588 (and (or same-file (not (stringp Info-current-file)))
5dab2fb4
RS
1589 (string-match "^(" node)
1590 (error "Up node is in another Info file"))
8a7757f6
JL
1591 (Info-goto-node node)
1592 (setq p (point))
1593 (goto-char (point-min))
1594 (if (and (search-forward "\n* Menu:" nil t)
1595 (re-search-forward
1596 (if (string-equal old-node "Top")
1597 (concat "\n\\*[^:]+: +(" (file-name-nondirectory old-file) ")")
1598 (concat "\n\\* +\\(" (regexp-quote old-node)
1599 ":\\|[^:]+: +" (regexp-quote old-node) "\\)"))
1600 nil t))
1601 (beginning-of-line)
1602 (goto-char p)
1603 (Info-restore-point Info-history))))
a384cab3
JB
1604
1605(defun Info-last ()
1606 "Go back to the last node visited."
1607 (interactive)
1608 (or Info-history
1609 (error "This is the first Info node you looked at"))
1610 (let (filename nodename opoint)
1611 (setq filename (car (car Info-history)))
1612 (setq nodename (car (cdr (car Info-history))))
1613 (setq opoint (car (cdr (cdr (car Info-history)))))
1614 (setq Info-history (cdr Info-history))
1615 (Info-find-node filename nodename)
1616 (setq Info-history (cdr Info-history))
1617 (goto-char opoint)))
1618
48e5b471 1619;;;###autoload
a384cab3
JB
1620(defun Info-directory ()
1621 "Go to the Info directory node."
1622 (interactive)
1623 (Info-find-node "dir" "top"))
1624\f
8a7757f6 1625(defun Info-history ()
bf7d6fa7 1626 "Go to a node with a menu of visited nodes."
8a7757f6
JL
1627 (interactive)
1628 (let ((curr-file Info-current-file)
1629 (curr-node Info-current-node)
1630 p)
bf7d6fa7
JL
1631 (with-current-buffer (get-buffer-create " *info-history*")
1632 (let ((inhibit-read-only t))
1633 (erase-buffer)
1634 (goto-char (point-min))
1635 (insert "\n\1f\nFile: history Node: Top, Up: (dir)\n\n")
1636 (insert "Recently Visited Nodes\n**********************\n\n")
1637 (insert "* Menu:\n\n")
1638 (let ((hl (delete '("history" "Top") Info-history-list)))
1639 (while hl
1640 (let ((file (nth 0 (car hl)))
1641 (node (nth 1 (car hl))))
1642 (if (and (string-equal file curr-file)
1643 (string-equal node curr-node))
1644 (setq p (point)))
1645 (insert "* " node ": (" (file-name-nondirectory file)
1646 ")" node ".\n"))
1647 (setq hl (cdr hl))))))
1648 (Info-find-node "history" "Top")
8a7757f6
JL
1649 (goto-char (or p (point-min)))))
1650
8a7757f6 1651(defun Info-toc ()
bf7d6fa7 1652 "Go to a node with table of contents of the current Info file."
8a7757f6
JL
1653 (interactive)
1654 (let ((curr-file Info-current-file)
1655 (curr-node Info-current-node)
1656 p)
bf7d6fa7
JL
1657 (with-current-buffer (get-buffer-create " *info-toc*")
1658 (let ((inhibit-read-only t)
1659 (node-list (Info-build-toc curr-file)))
1660 (erase-buffer)
1661 (goto-char (point-min))
1662 (insert "\n\1f\nFile: toc Node: Top, Up: (dir)\n\n")
1663 (insert "Table of Contents\n*****************\n\n")
1664 (insert "*Note Top::\n")
1665 (Info-insert-toc
1666 (nth 2 (assoc "Top" node-list)) ; get Top nodes
1667 node-list 0 (file-name-nondirectory curr-file)))
1668 (if (not (bobp))
1669 (let ((Info-hide-note-references 'hide)
1670 (Info-fontify-visited-nodes nil))
1671 (Info-fontify-node)))
1672 (goto-char (point-min))
1673 (if (setq p (search-forward (concat "*Note " curr-node ":") nil t))
1674 (setq p (- p (length curr-node) 2))))
1675 (Info-find-node "toc" "Top")
8a7757f6
JL
1676 (goto-char (or p (point-min)))))
1677
bf7d6fa7 1678(defun Info-insert-toc (nodes node-list level curr-file)
8a7757f6
JL
1679 "Insert table of contents with references to nodes."
1680 (let ((section "Top"))
1681 (while nodes
1682 (let ((node (assoc (car nodes) node-list)))
1683 (unless (member (nth 1 node) (list nil section))
1684 (insert (setq section (nth 1 node)) "\n"))
1685 (insert (make-string level ?\t))
bf7d6fa7
JL
1686 (insert "*Note " (car nodes) ": (" curr-file ")" (car nodes) ".\n")
1687 (Info-insert-toc (nth 2 node) node-list (1+ level) curr-file)
8a7757f6
JL
1688 (setq nodes (cdr nodes))))))
1689
1690(defun Info-build-toc (file)
1691 "Build table of contents from menus of Info FILE and its subfiles."
1692 (if (equal file "dir")
1693 (error "Table of contents for Info directory is not supported yet"))
1694 (with-temp-buffer
1695 (let ((default-directory (or (and (stringp file)
1696 (file-name-directory
1697 (setq file (Info-find-file file))))
1698 default-directory))
1699 (sections '(("Top" "Top")))
1700 nodes subfiles)
1701 (while (or file subfiles)
1702 (or file (message "Searching subfile %s..." (car subfiles)))
1703 (erase-buffer)
1704 (info-insert-file-contents (or file (car subfiles)))
1705 (while (and (search-forward "\n\^_\nFile:" nil 'move)
1706 (search-forward "Node: " nil 'move))
1707 (let ((nodename (substring-no-properties (Info-following-node-name)))
1708 (bound (- (or (save-excursion (search-forward "\n\^_" nil t))
1709 (point-max)) 2))
1710 (section "Top")
1711 menu-items)
1712 (when (and (not (string-match "\\<index\\>" nodename))
1713 (re-search-forward "^\\* Menu:" bound t))
1714 (forward-line 1)
1715 (beginning-of-line)
1716 (setq bound (or (and (equal nodename "Top")
1717 (save-excursion
1718 (re-search-forward
1719 "^[ \t-]*The Detailed Node Listing" nil t)))
1720 bound))
1721 (while (< (point) bound)
1722 (cond
1723 ;; Menu item line
1724 ((looking-at "^\\* +[^:]+:")
1725 (beginning-of-line)
1726 (forward-char 2)
1727 (let ((menu-node-name (substring-no-properties
1728 (Info-extract-menu-node-name))))
1729 (setq menu-items (cons menu-node-name menu-items))
1730 (if (equal nodename "Top")
1731 (setq sections
1732 (cons (list menu-node-name section) sections)))))
1733 ;; Other non-empty strings in the Top node are section names
1734 ((and (equal nodename "Top")
1735 (looking-at "^\\([^ \t\n*=.-][^:\n]*\\)"))
1736 (setq section (match-string-no-properties 1))))
1737 (forward-line 1)
1738 (beginning-of-line)))
1739 (setq nodes (cons (list nodename
1740 (cadr (assoc nodename sections))
1741 (nreverse menu-items))
1742 nodes))
1743 (goto-char bound)))
1744 (if file
1745 (save-excursion
1746 (goto-char (point-min))
1747 (if (search-forward "\n\^_\nIndirect:" nil t)
1748 (let ((bound (save-excursion (search-forward "\n\^_" nil t))))
1749 (while (re-search-forward "^\\(.*\\): [0-9]+$" bound t)
1750 (setq subfiles (cons (match-string-no-properties 1)
1751 subfiles)))))
1752 (setq subfiles (nreverse subfiles)
1753 file nil))
1754 (setq subfiles (cdr subfiles))))
1755 (message "")
1756 (nreverse nodes))))
1757\f
1758(defun Info-follow-reference (footnotename &optional fork)
fdf4b680 1759 "Follow cross reference named FOOTNOTENAME to the node it refers to.
8a7757f6
JL
1760FOOTNOTENAME may be an abbreviation of the reference name.
1761If FORK is non-nil (interactively with a prefix arg), show the node in
1762a new info buffer. If FORK is a string, it is the name to use for the
1763new buffer."
a384cab3
JB
1764 (interactive
1765 (let ((completion-ignore-case t)
1bcedb3b 1766 (case-fold-search t)
67bc89ab 1767 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 1768 (save-excursion
67bc89ab
RS
1769 ;; Store end and beginning of line.
1770 (end-of-line)
1771 (setq eol (point))
1772 (beginning-of-line)
1773 (setq bol (point))
1774
a384cab3 1775 (goto-char (point-min))
8a7757f6 1776 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 1777 (setq str (match-string-no-properties 1))
a384cab3
JB
1778 ;; See if this one should be the default.
1779 (and (null default)
1f179e27 1780 (<= (match-beginning 0) start-point)
a384cab3
JB
1781 (<= start-point (point))
1782 (setq default t))
67bc89ab
RS
1783 ;; See if this one should be the alternate default.
1784 (and (null alt-default)
1785 (and (<= bol (match-beginning 0))
1786 (<= (point) eol))
1787 (setq alt-default t))
a384cab3
JB
1788 (setq i 0)
1789 (while (setq i (string-match "[ \n\t]+" str i))
1790 (setq str (concat (substring str 0 i) " "
1791 (substring str (match-end 0))))
1792 (setq i (1+ i)))
1793 ;; Record as a completion and perhaps as default.
1794 (if (eq default t) (setq default str))
67bc89ab 1795 (if (eq alt-default t) (setq alt-default str))
ec6d29af 1796 ;; Don't add this string if it's a duplicate.
c60c12be
SM
1797 (or (assoc-string str completions t)
1798 (push str completions))))
67bc89ab
RS
1799 ;; If no good default was found, try an alternate.
1800 (or default
1801 (setq default alt-default))
1802 ;; If only one cross-reference found, then make it default.
1803 (if (eq (length completions) 1)
c60c12be 1804 (setq default (car completions)))
a384cab3 1805 (if completions
b0ebdfe5 1806 (let ((input (completing-read (if default
968b7671
MB
1807 (concat
1808 "Follow reference named: (default "
1809 default ") ")
b0ebdfe5
RS
1810 "Follow reference named: ")
1811 completions nil t)))
1812 (list (if (equal input "")
8a7757f6 1813 default input) current-prefix-arg))
a384cab3 1814 (error "No cross-references in this node"))))
ebf8f7e1
RS
1815
1816 (unless footnotename
1817 (error "No reference was specified"))
1818
dde7f979 1819 (let (target i (str (concat "\\*note " (regexp-quote footnotename)))
1bcedb3b 1820 (case-fold-search t))
a384cab3
JB
1821 (while (setq i (string-match " " str i))
1822 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
1823 (setq i (+ i 6)))
1824 (save-excursion
8a7757f6
JL
1825 ;; Move point to the beginning of reference if point is on reference
1826 (or (looking-at "\\*note[ \n\t]+")
1827 (and (looking-back "\\*note[ \n\t]+")
1828 (goto-char (match-beginning 0)))
1829 (if (and (save-excursion
1830 (goto-char (+ (point) 5)) ; skip a possible *note
1831 (re-search-backward "\\*note[ \n\t]+" nil t)
cd478f3d
JL
1832 (looking-at (concat "\\*note[ \n\t]+"
1833 (Info-following-node-name-re "^.,\t"))))
8a7757f6
JL
1834 (<= (point) (match-end 0)))
1835 (goto-char (match-beginning 0))))
1836 ;; Go to the reference closest to point
1837 (let ((next-ref (save-excursion (and (re-search-forward str nil t)
1838 (+ (match-beginning 0) 5))))
1839 (prev-ref (save-excursion (and (re-search-backward str nil t)
1840 (+ (match-beginning 0) 5)))))
1841 (goto-char (cond ((and next-ref prev-ref)
1842 (if (< (abs (- next-ref (point)))
1843 (abs (- prev-ref (point))))
1844 next-ref prev-ref))
1845 ((or next-ref prev-ref))
1846 ((error "No cross-reference named %s" footnotename))))
1847 (setq target (Info-extract-menu-node-name t))))
a384cab3
JB
1848 (while (setq i (string-match "[ \t\n]+" target i))
1849 (setq target (concat (substring target 0 i) " "
1850 (substring target (match-end 0))))
1851 (setq i (+ i 1)))
8a7757f6 1852 (Info-goto-node target fork)))
a384cab3 1853
e057da52
SM
1854(defconst Info-menu-entry-name-re "\\(?:[^:]\\|:[^:,.;() \t\n]\\)*"
1855 ;; We allow newline because this is also used in Info-follow-reference,
1856 ;; where the xref name might be wrapped over two lines.
c60c12be
SM
1857 "Regexp that matches a menu entry name upto but not including the colon.
1858Because of ambiguities, this should be concatenated with something like
1859`:' and `Info-following-node-name-re'.")
1860
a1637357 1861(defun Info-extract-menu-node-name (&optional multi-line)
a384cab3 1862 (skip-chars-forward " \t\n")
c60c12be
SM
1863 (when (looking-at (concat Info-menu-entry-name-re ":\\(:\\|"
1864 (Info-following-node-name-re
1865 (if multi-line "^.,\t" "^.,\t\n")) "\\)"))
1866 (replace-regexp-in-string
1867 "[ \n]+" " "
1868 (or (match-string 2)
1869 ;; If the node name is the menu entry name (using `entry::').
1870 (buffer-substring (match-beginning 0) (1- (match-beginning 1)))))))
a384cab3 1871
e8adde3f 1872;; No one calls this.
9e5c2f50
RS
1873;;(defun Info-menu-item-sequence (list)
1874;; (while list
e8adde3f 1875;; (Info-menu (car list))
9e5c2f50 1876;; (setq list (cdr list))))
a384cab3 1877
6356e646 1878(defvar Info-complete-menu-buffer)
e4e8ee63
SM
1879(defvar Info-complete-next-re nil)
1880(defvar Info-complete-cache nil)
6356e646 1881
c60c12be
SM
1882(defconst Info-node-spec-re
1883 (concat (Info-following-node-name-re "^.,:") "[,:.]")
a1637357
SM
1884 "Regexp to match the text after a : until the terminating `.'.")
1885
e8adde3f 1886(defun Info-complete-menu-item (string predicate action)
2d12f5c2
SM
1887 ;; This uses two dynamically bound variables:
1888 ;; - `Info-complete-menu-buffer' which contains the buffer in which
1889 ;; is the menu of items we're trying to complete.
1890 ;; - `Info-complete-next-re' which, if non-nil, indicates that we should
1891 ;; also look for menu items in subsequent nodes as long as those
1892 ;; nodes' names match `Info-complete-next-re'. This feature is currently
1893 ;; only used for completion in Info-index.
0defe758
LK
1894
1895 ;; Note that `Info-complete-menu-buffer' could be current already,
1896 ;; so we want to save point.
1897 (save-excursion
1898 (set-buffer Info-complete-menu-buffer)
e4e8ee63
SM
1899 (let ((completion-ignore-case t)
1900 (case-fold-search t)
1901 (orignode Info-current-node)
1902 nextnode)
1903 (goto-char (point-min))
1904 (search-forward "\n* Menu:")
1905 (if (not (memq action '(nil t)))
1906 (re-search-forward
1907 (concat "\n\\* +" (regexp-quote string) ":") nil t)
1908 (let ((pattern (concat "\n\\* +\\("
1909 (regexp-quote string)
c60c12be 1910 Info-menu-entry-name-re "\\):" Info-node-spec-re))
e4e8ee63
SM
1911 completions)
1912 ;; Check the cache.
1913 (if (and (equal (nth 0 Info-complete-cache) Info-current-file)
1914 (equal (nth 1 Info-complete-cache) Info-current-node)
1915 (equal (nth 2 Info-complete-cache) Info-complete-next-re)
1916 (let ((prev (nth 3 Info-complete-cache)))
1917 (eq t (compare-strings string 0 (length prev)
1918 prev 0 nil t))))
1919 ;; We can reuse the previous list.
1920 (setq completions (nth 4 Info-complete-cache))
1921 ;; The cache can't be used.
1922 (while
1923 (progn
1924 (while (re-search-forward pattern nil t)
365d2503 1925 (push (match-string-no-properties 1)
e4e8ee63
SM
1926 completions))
1927 ;; Check subsequent nodes if applicable.
1928 (and Info-complete-next-re
1929 (setq nextnode (Info-extract-pointer "next" t))
1930 (string-match Info-complete-next-re nextnode)))
1931 (Info-goto-node nextnode))
1932 ;; Go back to the start node (for the next completion).
1933 (unless (equal Info-current-node orignode)
1934 (Info-goto-node orignode))
1935 ;; Update the cache.
271f4a9e
MB
1936 (set (make-local-variable 'Info-complete-cache)
1937 (list Info-current-file Info-current-node
1938 Info-complete-next-re string completions)))
e4e8ee63
SM
1939 (if action
1940 (all-completions string completions predicate)
1941 (try-completion string completions predicate)))))))
e8adde3f
RS
1942
1943
4fceda3c 1944(defun Info-menu (menu-item &optional fork)
dc414be1
EZ
1945 "Go to the node pointed to by the menu item named (or abbreviated) MENU-ITEM.
1946The menu item should one of those listed in the current node's menu.
1947Completion is allowed, and the default menu item is the one point is on.
7210b6f5
DL
1948If FORK is non-nil (interactively with a prefix arg), show the node in
1949a new info buffer. If FORK is a string, it is the name to use for the
1950new buffer."
a384cab3
JB
1951 (interactive
1952 (let ((completions '())
1953 ;; If point is within a menu item, use that item as the default
1954 (default nil)
1955 (p (point))
211d6309 1956 beg
760d5cb3
GM
1957 (last nil)
1958 (case-fold-search t))
a384cab3
JB
1959 (save-excursion
1960 (goto-char (point-min))
1961 (if (not (search-forward "\n* menu:" nil t))
1962 (error "No menu in this node"))
e8adde3f
RS
1963 (setq beg (point))
1964 (and (< (point) p)
1965 (save-excursion
1966 (goto-char p)
1967 (end-of-line)
00d32b3f
SM
1968 (if (re-search-backward (concat "\n\\* +\\("
1969 Info-menu-entry-name-re
1970 "\\):") beg t)
110d4e80 1971 (setq default (match-string-no-properties 1))))))
a384cab3
JB
1972 (let ((item nil))
1973 (while (null item)
e8adde3f
RS
1974 (setq item (let ((completion-ignore-case t)
1975 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
1976 (completing-read (if default
1977 (format "Menu item (default %s): "
1978 default)
760d5cb3 1979 "Menu item: ")
e8adde3f 1980 'Info-complete-menu-item nil t)))
aea2a8da
JB
1981 ;; we rely on the fact that completing-read accepts an input
1982 ;; of "" even when the require-match argument is true and ""
1983 ;; is not a valid possibility
a384cab3
JB
1984 (if (string= item "")
1985 (if default
1986 (setq item default)
760d5cb3
GM
1987 ;; ask again
1988 (setq item nil))))
4fceda3c 1989 (list item current-prefix-arg))))
a384cab3
JB
1990 ;; there is a problem here in that if several menu items have the same
1991 ;; name you can only go to the node of the first with this command.
4fceda3c 1992 (Info-goto-node (Info-extract-menu-item menu-item) (if fork menu-item)))
399c88ad 1993
a384cab3
JB
1994(defun Info-extract-menu-item (menu-item)
1995 (setq menu-item (regexp-quote menu-item))
1bcedb3b
RS
1996 (let ((case-fold-search t))
1997 (save-excursion
760d5cb3
GM
1998 (let ((case-fold-search t))
1999 (goto-char (point-min))
2000 (or (search-forward "\n* menu:" nil t)
2001 (error "No menu in this node"))
2002 (or (re-search-forward (concat "\n\\* +" menu-item ":") nil t)
2003 (re-search-forward (concat "\n\\* +" menu-item) nil t)
2004 (error "No such item in menu"))
2005 (beginning-of-line)
2006 (forward-char 2)
2007 (Info-extract-menu-node-name)))))
a384cab3
JB
2008
2009;; If COUNT is nil, use the last item in the menu.
2010(defun Info-extract-menu-counting (count)
1bcedb3b
RS
2011 (let ((case-fold-search t))
2012 (save-excursion
760d5cb3
GM
2013 (let ((case-fold-search t))
2014 (goto-char (point-min))
2015 (or (search-forward "\n* menu:" nil t)
2016 (error "No menu in this node"))
2017 (if count
2018 (or (search-forward "\n* " nil t count)
2019 (error "Too few items in menu"))
2020 (while (search-forward "\n* " nil t)
2021 nil))
2022 (Info-extract-menu-node-name)))))
a384cab3 2023
e38e7367
RM
2024(defun Info-nth-menu-item ()
2025 "Go to the node of the Nth menu item.
2026N is the digit argument used to invoke this command."
a384cab3 2027 (interactive)
e38e7367
RM
2028 (Info-goto-node
2029 (Info-extract-menu-counting
2030 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
2031
2032(defun Info-top-node ()
2033 "Go to the Top node of this file."
2034 (interactive)
2035 (Info-goto-node "Top"))
2036
2037(defun Info-final-node ()
2038 "Go to the final node in this file."
2039 (interactive)
2040 (Info-goto-node "Top")
760d5cb3
GM
2041 (let ((Info-history nil)
2042 (case-fold-search t))
a384cab3
JB
2043 ;; Go to the last node in the menu of Top.
2044 (Info-goto-node (Info-extract-menu-counting nil))
2045 ;; If the last node in the menu is not last in pointer structure,
399c88ad 2046 ;; move forward until we can't go any farther.
a384cab3
JB
2047 (while (Info-forward-node t t) nil)
2048 ;; Then keep moving down to last subnode, unless we reach an index.
2049 (while (and (not (string-match "\\<index\\>" Info-current-node))
2050 (save-excursion (search-forward "\n* Menu:" nil t)))
2051 (Info-goto-node (Info-extract-menu-counting nil)))))
2052
2053(defun Info-forward-node (&optional not-down no-error)
2054 "Go forward one node, considering all nodes as forming one sequence."
2055 (interactive)
2056 (goto-char (point-min))
2057 (forward-line 1)
760d5cb3
GM
2058 (let ((case-fold-search t))
2059 ;; three possibilities, in order of priority:
2060 ;; 1. next node is in a menu in this node (but not in an index)
2061 ;; 2. next node is next at same level
2062 ;; 3. next node is up and next
2063 (cond ((and (not not-down)
2064 (save-excursion (search-forward "\n* menu:" nil t))
2065 (not (string-match "\\<index\\>" Info-current-node)))
2066 (Info-goto-node (Info-extract-menu-counting 1))
2067 t)
6af7040d 2068 ((save-excursion (search-backward "next:" nil t))
760d5cb3
GM
2069 (Info-next)
2070 t)
6af7040d 2071 ((and (save-excursion (search-backward "up:" nil t))
760d5cb3
GM
2072 ;; Use string-equal, not equal, to ignore text props.
2073 (not (string-equal (downcase (Info-extract-pointer "up"))
2074 "top")))
2075 (let ((old-node Info-current-node))
2076 (Info-up)
2077 (let (Info-history success)
2078 (unwind-protect
2079 (setq success (Info-forward-node t no-error))
2080 (or success (Info-goto-node old-node))))))
2081 (no-error nil)
2082 (t (error "No pointer forward from this node")))))
a384cab3
JB
2083
2084(defun Info-backward-node ()
2085 "Go backward one node, considering all nodes as forming one sequence."
2086 (interactive)
2087 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
760d5cb3
GM
2088 (upnode (Info-extract-pointer "up" t))
2089 (case-fold-search t))
a384cab3
JB
2090 (cond ((and upnode (string-match "(" upnode))
2091 (error "First node in file"))
2092 ((and upnode (or (null prevnode)
e90d1271
RS
2093 ;; Use string-equal, not equal,
2094 ;; to ignore text properties.
2095 (string-equal (downcase prevnode)
2096 (downcase upnode))))
a384cab3
JB
2097 (Info-up))
2098 (prevnode
2099 ;; If we move back at the same level,
2100 ;; go down to find the last subnode*.
2101 (Info-prev)
2102 (let (Info-history)
2103 (while (and (not (string-match "\\<index\\>" Info-current-node))
2104 (save-excursion (search-forward "\n* Menu:" nil t)))
2105 (Info-goto-node (Info-extract-menu-counting nil)))))
2106 (t
2107 (error "No pointer backward from this node")))))
2108
2109(defun Info-exit ()
2110 "Exit Info by selecting some other buffer."
2111 (interactive)
552775bd
RS
2112 (if Info-standalone
2113 (save-buffers-kill-emacs)
4643e92f 2114 (quit-window)))
a384cab3 2115
253db917 2116(defun Info-next-menu-item ()
3a8803c0 2117 "Go to the node of the next menu item."
253db917 2118 (interactive)
fdf4b680 2119 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2120 (let* ((case-fold-search t)
2121 (node
2122 (save-excursion
2123 (forward-line -1)
2124 (search-forward "\n* menu:" nil t)
2125 (and (search-forward "\n* " nil t)
2126 (Info-extract-menu-node-name)))))
fadfb77f
RS
2127 (if node (Info-goto-node node)
2128 (error "No more items in menu"))))
253db917
ER
2129
2130(defun Info-last-menu-item ()
3a8803c0 2131 "Go to the node of the previous menu item."
253db917
ER
2132 (interactive)
2133 (save-excursion
2134 (forward-line 1)
fdf4b680 2135 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
2136 (let* ((case-fold-search t)
2137 (beg (save-excursion
2138 (and (search-backward "\n* menu:" nil t)
2139 (point)))))
0a56332b
RS
2140 (or (and beg (search-backward "\n* " beg t))
2141 (error "No previous items in menu")))
2142 (Info-goto-node (save-excursion
2143 (goto-char (match-end 0))
2144 (Info-extract-menu-node-name)))))
253db917 2145
552775bd 2146(defmacro Info-no-error (&rest body)
253db917
ER
2147 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
2148
2149(defun Info-next-preorder ()
3c9179ea
RS
2150 "Go to the next subnode or the next node, or go up a level."
2151 (interactive)
2152 (cond ((Info-no-error (Info-next-menu-item)))
2153 ((Info-no-error (Info-next)))
5dab2fb4 2154 ((Info-no-error (Info-up t))
ed690657
KH
2155 ;; Since we have already gone thru all the items in this menu,
2156 ;; go up to the end of this node.
b54d0f0e
RS
2157 (goto-char (point-max))
2158 ;; Since logically we are done with the node with that menu,
2159 ;; move on from it.
2160 (Info-next-preorder))
3c9179ea
RS
2161 (t
2162 (error "No more nodes"))))
253db917
ER
2163
2164(defun Info-last-preorder ()
2165 "Go to the last node, popping up a level if there is none."
2166 (interactive)
0a56332b
RS
2167 (cond ((Info-no-error
2168 (Info-last-menu-item)
2169 ;; If we go down a menu item, go to the end of the node
2170 ;; so we can scroll back through it.
ed690657 2171 (goto-char (point-max)))
b54d0f0e
RS
2172 ;; Keep going down, as long as there are nested menu nodes.
2173 (while (Info-no-error
2174 (Info-last-menu-item)
2175 ;; If we go down a menu item, go to the end of the node
2176 ;; so we can scroll back through it.
2177 (goto-char (point-max))))
ed690657 2178 (recenter -1))
c2def7a0
MB
2179 ((and (Info-no-error (Info-extract-pointer "prev"))
2180 (not (equal (Info-extract-pointer "up")
5dab2fb4
RS
2181 (Info-extract-pointer "prev"))))
2182 (Info-no-error (Info-prev))
ed690657 2183 (goto-char (point-max))
b54d0f0e
RS
2184 (while (Info-no-error
2185 (Info-last-menu-item)
2186 ;; If we go down a menu item, go to the end of the node
2187 ;; so we can scroll back through it.
2188 (goto-char (point-max))))
ed690657 2189 (recenter -1))
5dab2fb4 2190 ((Info-no-error (Info-up t))
ed690657 2191 (goto-char (point-min))
760d5cb3
GM
2192 (let ((case-fold-search t))
2193 (or (search-forward "\n* Menu:" nil t)
2194 (goto-char (point-max)))))
ed690657 2195 (t (error "No previous nodes"))))
253db917
ER
2196
2197(defun Info-scroll-up ()
3f32dc86 2198 "Scroll one screenful forward in Info, considering all nodes as one sequence.
280d11ed 2199Once you scroll far enough in a node that its menu appears on the screen
7a53d8c8
EZ
2200but after point, the next scroll moves into its first subnode, unless
2201`Info-scroll-prefer-subnodes' is nil.
280d11ed 2202
7a53d8c8
EZ
2203When you scroll past the end of a node, that goes to the next node if
2204`Info-scroll-prefer-subnodes' is non-nil and to the first subnode otherwise;
2205if this node has no successor, it moves to the parent node's successor,
2206and so on. If `Info-scroll-prefer-subnodes' is non-nil and point is inside
2207the menu of a node, it moves to subnode indicated by the following menu
2208item. (That case won't normally result from this command, but can happen
2209in other ways.)"
280d11ed 2210
253db917 2211 (interactive)
0a56332b
RS
2212 (if (or (< (window-start) (point-min))
2213 (> (window-start) (point-max)))
2214 (set-window-start (selected-window) (point)))
760d5cb3
GM
2215 (let* ((case-fold-search t)
2216 (virtual-end (save-excursion
2217 (goto-char (point-min))
7a53d8c8
EZ
2218 (if (and Info-scroll-prefer-subnodes
2219 (search-forward "\n* Menu:" nil t))
760d5cb3
GM
2220 (point)
2221 (point-max)))))
0a56332b
RS
2222 (if (or (< virtual-end (window-start))
2223 (pos-visible-in-window-p virtual-end))
7a53d8c8
EZ
2224 (cond
2225 (Info-scroll-prefer-subnodes (Info-next-preorder))
2226 ((Info-no-error (Info-goto-node (Info-extract-menu-counting 1))))
2227 (t (Info-next-preorder)))
0a56332b 2228 (scroll-up))))
253db917
ER
2229
2230(defun Info-scroll-down ()
3f32dc86 2231 "Scroll one screenful back in Info, considering all nodes as one sequence.
c2def7a0
MB
2232If point is within the menu of a node, and `Info-scroll-prefer-subnodes'
2233is non-nil, this goes to its last subnode. When you scroll past the
2234beginning of a node, that goes to the previous node or back up to the
2235parent node."
253db917 2236 (interactive)
0a56332b
RS
2237 (if (or (< (window-start) (point-min))
2238 (> (window-start) (point-max)))
2239 (set-window-start (selected-window) (point)))
760d5cb3
GM
2240 (let* ((case-fold-search t)
2241 (current-point (point))
c2def7a0
MB
2242 (virtual-end
2243 (and Info-scroll-prefer-subnodes
2244 (save-excursion
2245 (beginning-of-line)
2246 (setq current-point (point))
2247 (goto-char (point-min))
2248 (search-forward "\n* Menu:"
2249 current-point
2250 t)))))
3a8803c0 2251 (if (or virtual-end
7afe24e3 2252 (pos-visible-in-window-p (point-min) nil t))
0a56332b
RS
2253 (Info-last-preorder)
2254 (scroll-down))))
253db917 2255
56cda6f5 2256(defun Info-next-reference (&optional recur)
552775bd
RS
2257 "Move cursor to the next cross-reference or menu item in the node."
2258 (interactive)
8a7757f6 2259 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tp://")
1bcedb3b
RS
2260 (old-pt (point))
2261 (case-fold-search t))
552775bd
RS
2262 (or (eobp) (forward-char 1))
2263 (or (re-search-forward pat nil t)
2264 (progn
2265 (goto-char (point-min))
2266 (or (re-search-forward pat nil t)
2267 (progn
2268 (goto-char old-pt)
2269 (error "No cross references in this node")))))
8a7757f6 2270 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2271 (if (looking-at "\\* Menu:")
56cda6f5
RS
2272 (if recur
2273 (error "No cross references in this node")
2274 (Info-next-reference t)))))
552775bd 2275
56cda6f5 2276(defun Info-prev-reference (&optional recur)
552775bd
RS
2277 "Move cursor to the previous cross-reference or menu item in the node."
2278 (interactive)
8a7757f6 2279 (let ((pat "\\*note[ \n\t]+\\([^:]+\\):\\|^\\* .*:\\|[hf]t?tp://")
1bcedb3b
RS
2280 (old-pt (point))
2281 (case-fold-search t))
552775bd
RS
2282 (or (re-search-backward pat nil t)
2283 (progn
2284 (goto-char (point-max))
2285 (or (re-search-backward pat nil t)
2286 (progn
2287 (goto-char old-pt)
2288 (error "No cross references in this node")))))
8a7757f6 2289 (goto-char (or (match-beginning 1) (match-beginning 0)))
552775bd 2290 (if (looking-at "\\* Menu:")
56cda6f5
RS
2291 (if recur
2292 (error "No cross references in this node")
2293 (Info-prev-reference t)))))
552775bd 2294
e4e8ee63
SM
2295(defun Info-goto-index ()
2296 (Info-goto-node "Top")
2297 (or (search-forward "\n* menu:" nil t)
2298 (error "No index"))
2299 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
2300 (error "No index"))
2301 (goto-char (match-beginning 1))
e700ec12
SM
2302 ;; Protect Info-history so that the current node (Top) is not added to it.
2303 (let ((Info-history nil))
2304 (Info-goto-node (Info-extract-menu-node-name))))
e4e8ee63 2305
201bc1bd 2306;;;###autoload
1143a6b0 2307(defun Info-index (topic)
fdf4b680 2308 "Look up a string TOPIC in the index for this file.
e4e8ee63 2309The index is defined as the first node in the top level menu whose
1143a6b0
ER
2310name contains the word \"Index\", plus any immediately following
2311nodes whose names also contain the word \"Index\".
2312If there are no exact matches to the specified topic, this chooses
2313the first match which is a case-insensitive substring of a topic.
bf7d6fa7 2314Use the \\<Info-mode-map>\\[Info-index-next] command to see the other matches.
1143a6b0 2315Give a blank topic name to go to the Index node itself."
e4e8ee63
SM
2316 (interactive
2317 (list
2318 (let ((Info-complete-menu-buffer (clone-buffer))
2319 (Info-complete-next-re "\\<Index\\>"))
aebd1760
RS
2320 (if (equal Info-current-file "dir")
2321 (error "The Info directory node has no index; use m to select a manual"))
e4e8ee63
SM
2322 (unwind-protect
2323 (with-current-buffer Info-complete-menu-buffer
2324 (Info-goto-index)
2325 (completing-read "Index topic: " 'Info-complete-menu-item))
2326 (kill-buffer Info-complete-menu-buffer)))))
aebd1760
RS
2327 (if (equal Info-current-file "dir")
2328 (error "The Info directory node has no index; use m to select a manual"))
1143a6b0 2329 (let ((orignode Info-current-node)
35ab7deb 2330 (pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1143a6b0 2331 (regexp-quote topic)))
1bcedb3b
RS
2332 node
2333 (case-fold-search t))
e700ec12 2334 (Info-goto-index)
1143a6b0
ER
2335 (or (equal topic "")
2336 (let ((matches nil)
2337 (exact nil)
e700ec12
SM
2338 ;; We bind Info-history to nil for internal node-switches so
2339 ;; that we don't put junk in the history. In the first
2340 ;; Info-goto-index call, above, we do update the history
2341 ;; because that is what the user's previous node choice into it.
c696ac76 2342 (Info-history nil)
1143a6b0
ER
2343 found)
2344 (while
2345 (progn
2346 (goto-char (point-min))
2347 (while (re-search-forward pattern nil t)
983dfbf8
SM
2348 (push (list (match-string-no-properties 1)
2349 (match-string-no-properties 2)
2350 Info-current-node
3a8803c0
JPW
2351 (string-to-number (concat "0"
2352 (match-string 3))))
983dfbf8 2353 matches))
1143a6b0
ER
2354 (and (setq node (Info-extract-pointer "next" t))
2355 (string-match "\\<Index\\>" node)))
2356 (Info-goto-node node))
2357 (or matches
2358 (progn
81e14cb2 2359 (Info-goto-node orignode)
920bdaab 2360 (error "No `%s' in index" topic)))
1143a6b0
ER
2361 ;; Here it is a feature that assoc is case-sensitive.
2362 (while (setq found (assoc topic matches))
2363 (setq exact (cons found exact)
2364 matches (delq found matches)))
2365 (setq Info-index-alternatives (nconc exact (nreverse matches)))
2366 (Info-index-next 0)))))
2367
2368(defun Info-index-next (num)
bf7d6fa7 2369 "Go to the next matching index item from the last \\<Info-mode-map>\\[Info-index] command."
1143a6b0
ER
2370 (interactive "p")
2371 (or Info-index-alternatives
882e61bf 2372 (error "No previous `i' command"))
1143a6b0
ER
2373 (while (< num 0)
2374 (setq num (+ num (length Info-index-alternatives))))
2375 (while (> num 0)
2376 (setq Info-index-alternatives
2377 (nconc (cdr Info-index-alternatives)
2378 (list (car Info-index-alternatives)))
2379 num (1- num)))
2380 (Info-goto-node (nth 1 (car Info-index-alternatives)))
2381 (if (> (nth 3 (car Info-index-alternatives)) 0)
2382 (forward-line (nth 3 (car Info-index-alternatives)))
760d5cb3 2383 (forward-line 3) ; don't search in headers
1143a6b0 2384 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
2385 (Info-find-index-name name)))
2386 (message "Found `%s' in %s. %s"
1143a6b0
ER
2387 (car (car Info-index-alternatives))
2388 (nth 2 (car Info-index-alternatives))
2389 (if (cdr Info-index-alternatives)
c702ed73 2390 "(`,' tries to find next)"
1143a6b0
ER
2391 "(Only match)")))
2392
920bdaab
RS
2393(defun Info-find-index-name (name)
2394 "Move point to the place within the current node where NAME is defined."
1bcedb3b
RS
2395 (let ((case-fold-search t))
2396 (if (or (re-search-forward (format
2397 "[a-zA-Z]+: %s\\( \\|$\\)"
2398 (regexp-quote name)) nil t)
d31c6ecf
RS
2399 ;; Find a function definition with a return type.
2400 (re-search-forward (format
f2fc5b0a 2401 "[a-zA-Z]+: [a-zA-Z0-9_ *&]+ %s\\( \\|$\\)"
d31c6ecf 2402 (regexp-quote name)) nil t)
1bcedb3b
RS
2403 (search-forward (format "`%s'" name) nil t)
2404 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
2405 (search-forward
2406 (format "`%s'" (substring name 0 (match-beginning 1)))
2407 nil t))
bbe054f7
EZ
2408 (search-forward name nil t)
2409 ;; Try again without the " <1>" makeinfo can append
2410 (and (string-match "\\`\\(.*\\) <[0-9]+>\\'" name)
2411 (Info-find-index-name (match-string 1 name))))
2412 (progn (beginning-of-line) t) ;; non-nil for recursive call
1bcedb3b 2413 (goto-char (point-min)))))
920bdaab 2414
ab2f22ad
JH
2415;;;###autoload
2416(defun info-apropos (string)
2417 "Grovel indices of all known Info files on your system for STRING.
2418Build a menu of the possible matches."
2419 (interactive "sIndex apropos: ")
2420 (unless (string= string "")
2421 (let ((pattern (format "\n\\* +\\([^\n]*%s[^\n]*\\):[ \t]+\\([^.]+\\)."
2422 (regexp-quote string)))
2423 (ohist Info-history)
37600b6d 2424 (ohist-list Info-history-list)
ab2f22ad
JH
2425 (current-node Info-current-node)
2426 (current-file Info-current-file)
bf7d6fa7 2427 manuals matches node)
ab2f22ad
JH
2428 (let ((Info-fontify-maximum-menu-size 0)
2429 Info-use-header-lines
2430 Info-hide-note-references)
2431 (Info-directory)
2432 (message "Searching indices...")
2433 (goto-char (point-min))
2434 (re-search-forward "\\* Menu: *\n" nil t)
7b30b20c 2435 (while (re-search-forward "\\*.*: *(\\([^)]+\\))" nil t)
ab2f22ad
JH
2436 (add-to-list 'manuals (match-string 1)))
2437 (dolist (manual manuals)
2438 (message "Searching %s" manual)
2439 (condition-case nil
2440 (save-excursion
2441 (Info-find-node manual "Top")
2442 (when (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
2443 (goto-char (match-beginning 1))
2444 (Info-goto-node (Info-extract-menu-node-name))
2445 (while
2446 (progn
2447 (goto-char (point-min))
2448 (while (re-search-forward pattern nil t)
2449 (add-to-list 'matches
2450 (list (match-string 1)
2451 (match-string 2)
2452 manual)))
2453 (and (setq node (Info-extract-pointer "next" t))
2454 (string-match "\\<Index\\>" node)))
2455 (Info-goto-node node))))
2456 (error nil))))
2457 (Info-goto-node (concat "(" current-file ")" current-node))
37600b6d
JH
2458 (setq Info-history ohist
2459 Info-history-list ohist-list)
ab2f22ad
JH
2460 (message "Searching indices...done")
2461 (if (null matches)
2462 (message "No matches found")
2463 (with-current-buffer (get-buffer-create " *info-apropos*")
2464 (erase-buffer)
2465 (insert "\n\1f\nFile: apropos, Node: Top, Up: (dir)\n")
2466 (insert "* Menu: \nNodes whose indices contain \"" string "\"\n\n")
2467 (dolist (entry matches)
2468 (insert "* " (car entry) " [" (nth 2 entry)
2469 "]: (" (nth 2 entry) ")" (nth 1 entry) ".\n")))
2470 (Info-find-node "apropos" "top")))))
2471
a384cab3
JB
2472(defun Info-undefined ()
2473 "Make command be undefined in Info."
2474 (interactive)
2475 (ding))
2476
2477(defun Info-help ()
2478 "Enter the Info tutorial."
2479 (interactive)
2480 (delete-other-windows)
2481 (Info-find-node "info"
2482 (if (< (window-height) 23)
2483 "Help-Small-Screen"
2484 "Help")))
2485
2486(defun Info-summary ()
2487 "Display a brief summary of all Info commands."
2488 (interactive)
2489 (save-window-excursion
2490 (switch-to-buffer "*Help*")
881c84c7 2491 (setq buffer-read-only nil)
a384cab3
JB
2492 (erase-buffer)
2493 (insert (documentation 'Info-mode))
9d29f94c 2494 (help-mode)
a384cab3
JB
2495 (goto-char (point-min))
2496 (let (ch flag)
2497 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
2498 (message (if flag "Type Space to see more"
2499 "Type Space to return to Info"))
1614c867 2500 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 2501 (progn (setq unread-command-events (list ch)) nil)
a384cab3 2502 flag))
552775bd
RS
2503 (scroll-up)))
2504 (bury-buffer "*Help*")))
a384cab3
JB
2505\f
2506(defun Info-get-token (pos start all &optional errorstring)
fdf4b680 2507 "Return the token around POS.
a384cab3
JB
2508POS must be somewhere inside the token
2509START is a regular expression which will match the
2510 beginning of the tokens delimited string
2511ALL is a regular expression with a single
90a715f0 2512 parenthesized subpattern which is the token to be
fdf4b680 2513 returned. E.g. '{\(.*\)}' would return any string
a384cab3 2514 enclosed in braces around POS.
fdf4b680 2515ERRORSTRING optional fourth argument, controls action on no match
a384cab3
JB
2516 nil: return nil
2517 t: beep
2518 a string: signal an error, using that string."
1bcedb3b
RS
2519 (let ((case-fold-search t))
2520 (save-excursion
2521 (goto-char pos)
2522 ;; First look for a match for START that goes across POS.
2523 (while (and (not (bobp)) (> (point) (- pos (length start)))
2524 (not (looking-at start)))
2525 (forward-char -1))
2526 ;; If we did not find one, search back for START
2527 ;; (this finds only matches that end at or before POS).
2528 (or (looking-at start)
2529 (progn
2530 (goto-char pos)
2531 (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
2532 (let (found)
2533 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
2534 (not (setq found (and (<= (match-beginning 0) pos)
2535 (> (match-end 0) pos))))))
2536 (if (and found (<= (match-beginning 0) pos)
2537 (> (match-end 0) pos))
110d4e80 2538 (match-string-no-properties 1)
1bcedb3b
RS
2539 (cond ((null errorstring)
2540 nil)
2541 ((eq errorstring t)
2542 (beep)
2543 nil)
2544 (t
2545 (error "No %s around position %d" errorstring pos))))))))
a384cab3 2546
981947af 2547(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
2548 "\\<Info-mode-map>Follow a node reference near point.
2549Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 2550At end of the node's text, moves to the next node, or up if none."
f9969361 2551 (interactive "e")
d1268e52 2552 (mouse-set-point click)
981947af
KH
2553 (and (not (Info-try-follow-nearest-node))
2554 (save-excursion (forward-line 1) (eobp))
ed690657 2555 (Info-next-preorder)))
981947af 2556
8a7757f6 2557(defun Info-follow-nearest-node (&optional fork)
d0b560c6
JB
2558 "Follow a node reference near point.
2559If point is on a reference, follow that reference. Otherwise,
2560if point is in a menu item description, follow that menu item."
8a7757f6
JL
2561 (interactive "P")
2562 (or (Info-try-follow-nearest-node fork)
d0b560c6
JB
2563 (when (save-excursion
2564 (search-backward "\n* menu:" nil t))
2565 (save-excursion
2566 (beginning-of-line)
2567 (while (not (or (bobp) (looking-at "[^ \t]\\|[ \t]*$")))
2568 (beginning-of-line 0))
2569 (when (looking-at "\\* +\\([^\t\n]*\\):")
2570 (Info-goto-node
8a7757f6 2571 (Info-extract-menu-item (match-string-no-properties 1)) fork)
d0b560c6
JB
2572 t)))
2573 (error "Point neither on reference nor in menu item description")))
981947af
KH
2574
2575;; Common subroutine.
8a7757f6 2576(defun Info-try-follow-nearest-node (&optional fork)
981947af 2577 "Follow a node reference near point. Return non-nil if successful."
6af7040d 2578 (let (node)
a384cab3 2579 (cond
8a7757f6
JL
2580 ((and (Info-get-token (point) "[hf]t?tp://" "[hf]t?tp://\\([^ \t\n\"`({<>})']+\\)")
2581 (or (featurep 'browse-url) (require 'browse-url nil t)))
2582 (setq node t)
2583 (browse-url (browse-url-url-at-point)))
2584 ((setq node (Info-get-token (point) "\\*note[ \n\t]+"
2585 "\\*note[ \n\t]+\\([^:]*\\):\\(:\\|[ \n\t]*(\\)?"))
2586;;; (or (match-string 2)
2587;;; (setq Info-reference-name
2588;;; (replace-regexp-in-string
2589;;; "[ \n\t]+" " " (match-string-no-properties 1))))
2590 (Info-follow-reference node fork))
505b68d5 2591 ;; menu item: node name
b37daea4 2592 ((setq node (Info-get-token (point) "\\* +" "\\* +\\([^:]*\\)::"))
8a7757f6 2593 (Info-goto-node node fork))
505b68d5
TTN
2594 ;; menu item: index entry
2595 ((Info-get-token (point) "\\* +" "\\* +\\(.*\\): ")
8a7757f6
JL
2596 (if (save-match-data (string-match "\\<index\\>" Info-current-node))
2597 (setq Info-reference-name (match-string-no-properties 1)))
631ba13e
RS
2598 (beginning-of-line)
2599 (forward-char 2)
2600 (setq node (Info-extract-menu-node-name))
8a7757f6 2601 (Info-goto-node node fork))
bc2ada62 2602 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
8a7757f6 2603 (Info-goto-node node fork))
bc2ada62 2604 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
8a7757f6 2605 (Info-goto-node node fork))
bc2ada62 2606 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
8a7757f6 2607 (Info-goto-node "Top" fork))
bc2ada62 2608 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
8a7757f6 2609 (Info-goto-node node fork)))
981947af 2610 node))
a384cab3
JB
2611\f
2612(defvar Info-mode-map nil
2613 "Keymap containing Info commands.")
2614(if Info-mode-map
2615 nil
2616 (setq Info-mode-map (make-keymap))
2617 (suppress-keymap Info-mode-map)
2618 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 2619 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 2620 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd 2621 (define-key Info-mode-map "\t" 'Info-next-reference)
508f27a0
RS
2622 (define-key Info-mode-map [(shift tab)] 'Info-prev-reference)
2623 (define-key Info-mode-map [backtab] 'Info-prev-reference)
e38e7367
RM
2624 (define-key Info-mode-map "1" 'Info-nth-menu-item)
2625 (define-key Info-mode-map "2" 'Info-nth-menu-item)
2626 (define-key Info-mode-map "3" 'Info-nth-menu-item)
2627 (define-key Info-mode-map "4" 'Info-nth-menu-item)
2628 (define-key Info-mode-map "5" 'Info-nth-menu-item)
2629 (define-key Info-mode-map "6" 'Info-nth-menu-item)
2630 (define-key Info-mode-map "7" 'Info-nth-menu-item)
2631 (define-key Info-mode-map "8" 'Info-nth-menu-item)
2632 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 2633 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
2634 (define-key Info-mode-map "?" 'Info-summary)
2635 (define-key Info-mode-map "]" 'Info-forward-node)
2636 (define-key Info-mode-map "[" 'Info-backward-node)
2637 (define-key Info-mode-map "<" 'Info-top-node)
2638 (define-key Info-mode-map ">" 'Info-final-node)
2639 (define-key Info-mode-map "b" 'beginning-of-buffer)
2ad63128 2640 (define-key Info-mode-map "c" 'Info-copy-current-node-name)
a384cab3
JB
2641 (define-key Info-mode-map "d" 'Info-directory)
2642 (define-key Info-mode-map "e" 'Info-edit)
2643 (define-key Info-mode-map "f" 'Info-follow-reference)
2644 (define-key Info-mode-map "g" 'Info-goto-node)
2645 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 2646 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
2647 (define-key Info-mode-map "l" 'Info-last)
2648 (define-key Info-mode-map "m" 'Info-menu)
2649 (define-key Info-mode-map "n" 'Info-next)
2650 (define-key Info-mode-map "p" 'Info-prev)
2651 (define-key Info-mode-map "q" 'Info-exit)
2652 (define-key Info-mode-map "s" 'Info-search)
47fc33ca
RS
2653 ;; For consistency with Rmail.
2654 (define-key Info-mode-map "\M-s" 'Info-search)
4fceda3c 2655 (define-key Info-mode-map "\M-n" 'clone-buffer)
db0c9809 2656 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 2657 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 2658 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 2659 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 2660 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
aea2a8da 2661 )
75a209d4
RS
2662
2663(defun Info-check-pointer (item)
fdf4b680 2664 "Non-nil if ITEM is present in this node."
75a209d4
RS
2665 (condition-case nil
2666 (Info-extract-pointer item)
2667 (error nil)))
2668
d4af987a
DL
2669(easy-menu-define
2670 Info-mode-menu Info-mode-map
2671 "Menu for info files."
2672 '("Info"
2673 ["Up" Info-up :active (Info-check-pointer "up")
2674 :help "Go up in the Info tree"]
2675 ["Next" Info-next :active (Info-check-pointer "next")
2676 :help "Go to the next node"]
2677 ["Previous" Info-prev :active (Info-check-pointer "prev[ious]*")
2678 :help "Go to the previous node"]
2679 ["Backward" Info-backward-node
2680 :help "Go backward one node, considering all as a sequence"]
2681 ["Forward" Info-forward-node
2682 :help "Go forward one node, considering all as a sequence"]
7210b6f5
DL
2683 ["Beginning" beginning-of-buffer
2684 :help "Go to beginning of this node"]
d4af987a
DL
2685 ["Top" Info-top-node
2686 :help "Go to top node of file"]
2687 ["Final Node" Info-final-node
2688 :help "Go to final node in this file"]
2689 ("Menu Item" ["You should never see this" report-emacs-bug t])
2690 ("Reference" ["You should never see this" report-emacs-bug t])
2691 ["Search..." Info-search
2692 :help "Search for regular expression in this Info file"]
8a7757f6
JL
2693 ["Search Case-Sensitively..." Info-search-case-sensitively
2694 :help "Search for regular expression case sensitively"]
2695 ["Search Next" Info-search-next
2696 :help "Search for another occurrence of regular expression"]
d1268e52 2697 ["Go to Node..." Info-goto-node
d4af987a 2698 :help "Go to a named node"]
ec03f31e 2699 ["Last" Info-last :active Info-history
d4af987a 2700 :help "Go to the last node you were at"]
8a7757f6
JL
2701 ["History" Info-history :active Info-history-list
2702 :help "Go to the history buffer"]
2703 ["Table of Contents" Info-toc
2704 :help "Go to the buffer with a table of contents"]
d4af987a
DL
2705 ("Index..."
2706 ["Lookup a String" Info-index
2707 :help "Look for a string in the index items"]
2708 ["Next Matching Item" Info-index-next
ab2f22ad
JH
2709 :help "Look for another occurrence of previous item"]
2710 ["Lookup a string in all indices" info-apropos
2711 :help "Look for a string in the indices of all manuals"])
7210b6f5
DL
2712 ["Edit" Info-edit :help "Edit contents of this node"
2713 :active Info-enable-edit]
126f5d4d
MB
2714 ["Copy Node Name" Info-copy-current-node-name
2715 :help "Copy the name of the current node into the kill ring"]
8a7757f6
JL
2716 ["Clone Info buffer" clone-buffer
2717 :help "Create a twin copy of the current Info buffer."]
ec03f31e 2718 ["Exit" Info-exit :help "Stop reading Info"]))
75a209d4 2719
7210b6f5
DL
2720
2721(defvar info-tool-bar-map
2722 (if (display-graphic-p)
df6aed99
RS
2723 (let ((map (make-sparse-keymap)))
2724 (tool-bar-local-item-from-menu 'Info-exit "close" map Info-mode-map)
2725 (tool-bar-local-item-from-menu 'Info-prev "left_arrow" map Info-mode-map)
2726 (tool-bar-local-item-from-menu 'Info-next "right_arrow" map Info-mode-map)
2727 (tool-bar-local-item-from-menu 'Info-up "up_arrow" map Info-mode-map)
2728 (tool-bar-local-item-from-menu 'Info-last "undo" map Info-mode-map)
2729 (tool-bar-local-item-from-menu 'Info-top-node "home" map Info-mode-map)
2730 (tool-bar-local-item-from-menu 'Info-index "index" map Info-mode-map)
2731 (tool-bar-local-item-from-menu 'Info-goto-node "jump_to" map Info-mode-map)
2732 (tool-bar-local-item-from-menu 'Info-search "search" map Info-mode-map)
2733 map)))
7210b6f5 2734
75a209d4
RS
2735(defvar Info-menu-last-node nil)
2736;; Last node the menu was created for.
6d2c8e3e 2737;; Value is a list, (FILE-NAME NODE-NAME).
75a209d4
RS
2738
2739(defun Info-menu-update ()
fdf4b680 2740 "Update the Info menu for the current node."
75a209d4
RS
2741 (condition-case nil
2742 (if (or (not (eq major-mode 'Info-mode))
6d2c8e3e
RS
2743 (equal (list Info-current-file Info-current-node)
2744 Info-menu-last-node))
75a209d4
RS
2745 ()
2746 ;; Update menu menu.
2747 (let* ((Info-complete-menu-buffer (current-buffer))
2748 (items (nreverse (condition-case nil
e4e8ee63 2749 (Info-complete-menu-item "" nil t)
75a209d4 2750 (error nil))))
399c88ad 2751 entries current
75a209d4
RS
2752 (number 0))
2753 (while (and items (< number 9))
2754 (setq current (car items)
2755 items (cdr items)
2756 number (1+ number))
399c88ad 2757 (setq entries (cons `[,current
75a209d4
RS
2758 (Info-menu ,current)
2759 :keys ,(format "%d" number)]
2760 entries)))
2761 (if items
2762 (setq entries (cons ["Other..." Info-menu t] entries)))
2763 (or entries
5d5845dc 2764 (setq entries (list ["No menu" nil nil] nil :active)))
d4af987a 2765 (easy-menu-change '("Info") "Menu Item" (nreverse entries)))
75a209d4
RS
2766 ;; Update reference menu. Code stolen from `Info-follow-reference'.
2767 (let ((items nil)
399c88ad 2768 str i entries current
1bcedb3b
RS
2769 (number 0)
2770 (case-fold-search t))
75a209d4
RS
2771 (save-excursion
2772 (goto-char (point-min))
8a7757f6 2773 (while (re-search-forward "\\*note[ \n\t]+\\([^:]*\\):" nil t)
c60c12be 2774 (setq str (match-string 1))
75a209d4
RS
2775 (setq i 0)
2776 (while (setq i (string-match "[ \n\t]+" str i))
2777 (setq str (concat (substring str 0 i) " "
2778 (substring str (match-end 0))))
2779 (setq i (1+ i)))
2780 (setq items
2781 (cons str items))))
2782 (while (and items (< number 9))
2783 (setq current (car items)
2784 items (cdr items)
2785 number (1+ number))
399c88ad 2786 (setq entries (cons `[,current
75a209d4
RS
2787 (Info-follow-reference ,current)
2788 t]
2789 entries)))
2790 (if items
2791 (setq entries (cons ["Other..." Info-follow-reference t]
2792 entries)))
2793 (or entries
5d5845dc 2794 (setq entries (list ["No references" nil nil] nil :active)))
75a209d4
RS
2795 (easy-menu-change '("Info") "Reference" (nreverse entries)))
2796 ;; Update last seen node.
6d2c8e3e 2797 (setq Info-menu-last-node (list Info-current-file Info-current-node)))
75a209d4
RS
2798 ;; Try to avoid entering infinite beep mode in case of errors.
2799 (error (ding))))
2800
a384cab3 2801\f
126f5d4d
MB
2802(defun Info-copy-current-node-name ()
2803 "Put the name of the current info node into the kill ring.
2804The name of the info file is prepended to the node name in parentheses."
2805 (interactive)
2806 (unless Info-current-node
2807 (error "No current info node"))
2808 (kill-new
2809 (concat "("
2810 (file-name-nondirectory
2811 (if (stringp Info-current-file)
2812 Info-current-file
2813 (or buffer-file-name "")))
2814 ")"
2815 Info-current-node)))
2816
2817\f
a384cab3 2818;; Info mode is suitable only for specially formatted data.
68bc119c 2819(put 'Info-mode 'mode-class 'special)
2fa8f8a4 2820(put 'Info-mode 'no-clone-indirect t)
a384cab3
JB
2821
2822(defun Info-mode ()
fdf4b680 2823 "Info mode provides commands for browsing through the Info documentation tree.
a384cab3
JB
2824Documentation in Info is divided into \"nodes\", each of which discusses
2825one topic and contains references to other nodes which discuss related
2826topics. Info has commands to follow the references and show you other nodes.
2827
fdf4b680 2828\\<Info-mode-map>\
a384cab3 2829\\[Info-help] Invoke the Info tutorial.
6b136ab9 2830\\[Info-exit] Quit Info: reselect previously selected buffer.
a384cab3
JB
2831
2832Selecting other nodes:
21d5959f
RS
2833\\[Info-mouse-follow-nearest-node]
2834 Follow a node reference you click on.
2835 This works with menu items, cross references, and
2836 the \"next\", \"previous\" and \"up\", depending on where you click.
6b136ab9 2837\\[Info-follow-nearest-node] Follow a node reference near point, like \\[Info-mouse-follow-nearest-node].
a384cab3 2838\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 2839\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
2840\\[Info-up] Move \"up\" from this node.
2841\\[Info-menu] Pick menu item specified by name (or abbreviation).
e8042313 2842 Picking a menu item causes another node to be selected.
aea2a8da 2843\\[Info-directory] Go to the Info directory node.
a384cab3
JB
2844\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
2845\\[Info-last] Move to the last node you were at.
8a7757f6
JL
2846\\[Info-history] Go to the history buffer.
2847\\[Info-toc] Go to the buffer with a table of contents.
1143a6b0 2848\\[Info-index] Look up a topic in this file's Index and move to that node.
bf7d6fa7 2849\\[Info-index-next] (comma) Move to the next match from a previous \\<Info-mode-map>\\[Info-index] command.
6b136ab9
DL
2850\\[Info-top-node] Go to the Top node of this file.
2851\\[Info-final-node] Go to the final node in this file.
2852\\[Info-backward-node] Go backward one node, considering all nodes as forming one sequence.
2853\\[Info-forward-node] Go forward one node, considering all nodes as forming one sequence.
a384cab3
JB
2854
2855Moving within a node:
177c3549 2856\\[Info-scroll-up] Normally, scroll forward a full screen.
e8042313
PJ
2857 Once you scroll far enough in a node that its menu appears on the
2858 screen but after point, the next scroll moves into its first
2859 subnode. When after all menu items (or if there is no menu),
2860 move up to the parent node.
177c3549 2861\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
e8042313
PJ
2862 already visible, try to go to the previous menu entry, or up
2863 if there is none.
399c88ad 2864\\[beginning-of-buffer] Go to beginning of node.
a384cab3 2865
a384cab3 2866Advanced commands:
c2a87d74 2867\\[Info-copy-current-node-name] Put name of current info node in the kill ring.
8a7757f6 2868\\[clone-buffer] Select a new cloned Info buffer in another window.
a384cab3
JB
2869\\[Info-edit] Edit contents of selected node.
28701 Pick first item in node's menu.
28712, 3, 4, 5 Pick second ... fifth item in node's menu.
2872\\[Info-goto-node] Move to node specified by name.
e8042313 2873 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 2874\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 2875\\[Info-search] Search through this Info file for specified regexp,
e8042313 2876 and select the node in which the next occurrence is found.
8a7757f6
JL
2877\\[Info-search-case-sensitively] Search through this Info file
2878 for specified regexp case-sensitively.
2879\\[Info-search-next] Search for another occurrence of regexp
bf7d6fa7 2880 from a previous \\<Info-mode-map>\\[Info-search] command.
552775bd
RS
2881\\[Info-next-reference] Move cursor to next cross-reference or menu item.
2882\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
2883 (kill-all-local-variables)
2884 (setq major-mode 'Info-mode)
2885 (setq mode-name "Info")
f73299f3 2886 (setq tab-width 8)
a384cab3 2887 (use-local-map Info-mode-map)
75a209d4 2888 (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
a384cab3
JB
2889 (set-syntax-table text-mode-syntax-table)
2890 (setq local-abbrev-table text-mode-abbrev-table)
2891 (setq case-fold-search t)
2892 (setq buffer-read-only t)
a384cab3
JB
2893 (make-local-variable 'Info-current-file)
2894 (make-local-variable 'Info-current-subfile)
2895 (make-local-variable 'Info-current-node)
2896 (make-local-variable 'Info-tag-table-marker)
c5fe2ff1
RS
2897 (setq Info-tag-table-marker (make-marker))
2898 (make-local-variable 'Info-tag-table-buffer)
2899 (setq Info-tag-table-buffer nil)
a384cab3 2900 (make-local-variable 'Info-history)
1143a6b0 2901 (make-local-variable 'Info-index-alternatives)
dde7f979
SM
2902 (setq header-line-format
2903 (if Info-use-header-line
3536c121 2904 '(:eval (get-text-property (point-min) 'header-line))
dde7f979 2905 nil)) ; so the header line isn't displayed
7210b6f5 2906 (set (make-local-variable 'tool-bar-map) info-tool-bar-map)
93480d70
RS
2907 ;; This is for the sake of the invisible text we use handling titles.
2908 (make-local-variable 'line-move-ignore-invisible)
2909 (setq line-move-ignore-invisible t)
b7aeabeb 2910 (add-hook 'clone-buffer-hook 'Info-clone-buffer-hook nil t)
3d12b331 2911 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
a384cab3
JB
2912 (Info-set-mode-line)
2913 (run-hooks 'Info-mode-hook))
2914
4fceda3c
SM
2915(defun Info-clone-buffer-hook ()
2916 (when (bufferp Info-tag-table-buffer)
2917 (setq Info-tag-table-buffer
4ce5c223
SM
2918 (with-current-buffer Info-tag-table-buffer (clone-buffer))))
2919 (let ((m Info-tag-table-marker))
2920 (when (markerp m)
15533ae2 2921 (setq Info-tag-table-marker
4ce5c223 2922 (if (and (marker-position m) (bufferp Info-tag-table-buffer))
15533ae2
SM
2923 (with-current-buffer Info-tag-table-buffer
2924 (copy-marker (marker-position m)))
2925 (make-marker))))))
4fceda3c 2926
7210b6f5
DL
2927(defvar Info-edit-map (let ((map (make-sparse-keymap)))
2928 (set-keymap-parent map text-mode-map)
2929 (define-key map "\C-c\C-c" 'Info-cease-edit)
2930 map)
a384cab3 2931 "Local keymap used within `e' command of Info.")
a384cab3
JB
2932
2933;; Info-edit mode is suitable only for specially formatted data.
68bc119c 2934(put 'Info-edit-mode 'mode-class 'special)
a384cab3
JB
2935
2936(defun Info-edit-mode ()
2937 "Major mode for editing the contents of an Info node.
a426b157 2938Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
2939which returns to Info mode for browsing.
2940\\{Info-edit-map}"
a384cab3
JB
2941 (use-local-map Info-edit-map)
2942 (setq major-mode 'Info-edit-mode)
2943 (setq mode-name "Info Edit")
2944 (kill-local-variable 'mode-line-buffer-identification)
2945 (setq buffer-read-only nil)
2c609f53 2946 (force-mode-line-update)
e82c28f9
KH
2947 (buffer-enable-undo (current-buffer))
2948 (run-hooks 'Info-edit-mode-hook))
2949
2950(defun Info-edit ()
2951 "Edit the contents of this Info node.
2952Allowed only if variable `Info-enable-edit' is non-nil."
2953 (interactive)
2954 (or Info-enable-edit
2955 (error "Editing info nodes is not enabled"))
2956 (Info-edit-mode)
8ab3e50b 2957 (message "%s" (substitute-command-keys
760d5cb3 2958 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
2959
2960(defun Info-cease-edit ()
2961 "Finish editing Info node; switch back to Info proper."
2962 (interactive)
2963 ;; Do this first, so nothing has changed if user C-g's at query.
2964 (and (buffer-modified-p)
2965 (y-or-n-p "Save the file? ")
2966 (save-buffer))
2967 (use-local-map Info-mode-map)
2968 (setq major-mode 'Info-mode)
2969 (setq mode-name "Info")
2970 (Info-set-mode-line)
2971 (setq buffer-read-only t)
2c609f53 2972 (force-mode-line-update)
a384cab3
JB
2973 (and (marker-position Info-tag-table-marker)
2974 (buffer-modified-p)
2975 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 2976\f
f88ab1c9 2977(defvar Info-file-list-for-emacs
906b3163
EZ
2978 '("ediff" "eudc" "forms" "gnus" "info" ("mh" . "mh-e")
2979 "sc" "message" ("dired" . "dired-x") "viper" "vip" "idlwave"
2980 ("c" . "ccmode") ("c++" . "ccmode") ("objc" . "ccmode")
2981 ("java" . "ccmode") ("idl" . "ccmode") ("pike" . "ccmode")
af718538
EZ
2982 ("skeleton" . "autotype") ("auto-insert" . "autotype")
2983 ("copyright" . "autotype") ("executable" . "autotype")
2984 ("time-stamp" . "autotype") ("quickurl" . "autotype")
2985 ("tempo" . "autotype") ("hippie-expand" . "autotype")
807378ad
EZ
2986 ("cvs" . "pcl-cvs") ("ada" . "ada-mode") "calc"
2987 ("calcAlg" . "calc") ("calcDigit" . "calc") ("calcVar" . "calc")
906b3163
EZ
2988 "ebrowse" "eshell" "cl" "reftex" "speedbar" "widget" "woman"
2989 ("mail-header" . "emacs-mime") ("mail-content" . "emacs-mime")
2990 ("mail-encode" . "emacs-mime") ("mail-decode" . "emacs-mime")
2991 ("rfc2045" . "emacs-mime")
2992 ("rfc2231" . "emacs-mime") ("rfc2047" . "emacs-mime")
2993 ("rfc2045" . "emacs-mime") ("rfc1843" . "emacs-mime")
2994 ("ietf-drums" . "emacs-mime") ("quoted-printable" . "emacs-mime")
2995 ("binhex" . "emacs-mime") ("uudecode" . "emacs-mime")
2996 ("mailcap" . "emacs-mime") ("mm" . "emacs-mime")
2997 ("mml" . "emacs-mime"))
f88ab1c9
RS
2998 "List of Info files that describe Emacs commands.
2999An element can be a file name, or a list of the form (PREFIX . FILE)
3000where PREFIX is a name prefix and FILE is the file to look in.
3001If the element is just a file name, the file name also serves as the prefix.")
3002
f0a8a3f1 3003(defun Info-find-emacs-command-nodes (command)
f88ab1c9 3004 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
3005The `info-file' property of COMMAND says which Info manual to search.
3006If COMMAND has no property, the variable `Info-file-list-for-emacs'
3007defines heuristics for which Info manual to try.
fdf4b680 3008The locations are of the format used in `Info-history', i.e.
f0a8a3f1 3009\(FILENAME NODENAME BUFFERPOS\)."
f0a8a3f1 3010 (let ((where '())
b37daea4 3011 (cmd-desc (concat "^\\* +" (regexp-quote (symbol-name command))
218c2cc7 3012 "\\( <[0-9]+>\\)?:\\s *\\(.*\\)\\.$"))
f88ab1c9
RS
3013 (info-file "emacs")) ;default
3014 ;; Determine which info file this command is documented in.
3015 (if (get command 'info-file)
3016 (setq info-file (get command 'info-file))
3017 ;; If it doesn't say explicitly, test its name against
3018 ;; various prefixes that we know.
3019 (let ((file-list Info-file-list-for-emacs))
3020 (while file-list
3021 (let* ((elt (car file-list))
3022 (name (if (consp elt)
3023 (car elt)
3024 elt))
3025 (file (if (consp elt) (cdr elt) elt))
f57b2cd8 3026 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
3027 "\\(\\'\\|-\\)")))
3028 (if (string-match regexp (symbol-name command))
3029 (setq info-file file file-list nil))
3030 (setq file-list (cdr file-list))))))
218c2cc7
EZ
3031 (Info-find-node info-file "Top")
3032 (or (and (search-forward "\n* menu:" nil t)
3033 (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t))
3034 (error "Info file `%s' appears to lack an index" info-file))
3035 (goto-char (match-beginning 1))
3036 ;; Bind Info-history to nil, to prevent the index nodes from
3037 ;; getting into the node history.
3038 (let ((Info-history nil)
dde7f979 3039 node)
218c2cc7
EZ
3040 (Info-goto-node (Info-extract-menu-node-name))
3041 (while
3042 (progn
3043 (goto-char (point-min))
3044 (while (re-search-forward cmd-desc nil t)
3045 (setq where
3046 (cons (list Info-current-file
3047 (match-string-no-properties 2)
760d5cb3
GM
3048 0)
3049 where)))
218c2cc7
EZ
3050 (and (setq node (Info-extract-pointer "next" t))
3051 (string-match "\\<Index\\>" node)))
3052 (Info-goto-node node)))
3053 where))
f0a8a3f1
RM
3054
3055;;;###autoload
3056(defun Info-goto-emacs-command-node (command)
e0568e86 3057 "Go to the Info node in the Emacs manual for command COMMAND.
30db89f9 3058The command is found by looking up in Emacs manual's indices
f57b2cd8 3059or in another manual found via COMMAND's `info-file' property or
1ed6e0c3
JPW
3060the variable `Info-file-list-for-emacs'.
3061COMMAND must be a symbol or string."
f0a8a3f1 3062 (interactive "CFind documentation for command: ")
fafb00dc
MY
3063 ;; If command is given as a string, convert it to a symbol.
3064 (if (stringp command)
1ed6e0c3 3065 (setq command (intern command)))
f0a8a3f1
RM
3066 (or (commandp command)
3067 (signal 'wrong-type-argument (list 'commandp command)))
3068 (let ((where (Info-find-emacs-command-nodes command)))
3069 (if where
3070 (let ((num-matches (length where)))
3071 ;; Get Info running, and pop to it in another window.
3072 (save-window-excursion
3073 (info))
bf7d6fa7 3074 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
218c2cc7
EZ
3075 ;; Bind Info-history to nil, to prevent the last Index node
3076 ;; visited by Info-find-emacs-command-nodes from being
3077 ;; pushed onto the history.
3078 (let ((Info-history nil))
3079 (Info-find-node (car (car where))
3080 (car (cdr (car where)))))
f0a8a3f1
RM
3081 (if (> num-matches 1)
3082 (progn
218c2cc7
EZ
3083 ;; (car where) will be pushed onto Info-history
3084 ;; when/if they go to another node. Put the other
3085 ;; nodes that were found on the history.
f0a8a3f1 3086 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 3087 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
3088 (1- num-matches)
3089 (if (> num-matches 2) "ies" "y")
8ab3e50b 3090 (substitute-command-keys "\\[Info-last]")
cedb118c 3091 (if (> num-matches 2) "them" "it")))))
e9b81433 3092 (error "Couldn't find documentation for %s" command))))
f0a8a3f1
RM
3093
3094;;;###autoload
3095(defun Info-goto-emacs-key-command-node (key)
30db89f9
EZ
3096 "Go to the node in the Emacs manual which describes the command bound to KEY.
3097KEY is a string.
fdf4b680 3098Interactively, if the binding is `execute-extended-command', a command is read.
30db89f9 3099The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
3100or in another manual found via COMMAND's `info-file' property or
3101the variable `Info-file-list-for-emacs'."
ea33749b 3102 (interactive "kFind documentation for key: ")
f0a8a3f1
RM
3103 (let ((command (key-binding key)))
3104 (cond ((null command)
9e5c2f50 3105 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
3106 ((and (interactive-p)
3107 (eq command 'execute-extended-command))
3108 (Info-goto-emacs-command-node
3109 (read-command "Find documentation for command: ")))
3110 (t
3111 (Info-goto-emacs-command-node command)))))
552775bd 3112\f
bbed5c3f 3113(defface Info-title-1-face
b250866f 3114 '((((type tty pc) (class color)) (:foreground "yellow" :weight bold))
333cd59e 3115 (t (:height 1.2 :inherit Info-title-2-face)))
bbed5c3f
GM
3116 "Face for Info titles at level 1."
3117 :group 'info)
3118
3119(defface Info-title-2-face
b250866f 3120 '((((type tty pc) (class color)) (:foreground "lightblue" :weight bold))
333cd59e 3121 (t (:height 1.2 :inherit Info-title-3-face)))
bbed5c3f
GM
3122 "Face for Info titles at level 2."
3123 :group 'info)
3124
3125(defface Info-title-3-face
b250866f 3126 '((((type tty pc) (class color)) (:weight bold))
a8b883c2 3127 (t (:height 1.2 :inherit Info-title-4-face)))
bbed5c3f
GM
3128 "Face for Info titles at level 3."
3129 :group 'info)
3130
a8b883c2
MB
3131(defface Info-title-4-face
3132 '((((type tty pc) (class color)) (:weight bold))
3133 (t (:weight bold :inherit variable-pitch)))
3134 "Face for Info titles at level 4."
3135 :group 'info)
3136
973a3104
MB
3137(defface info-menu-header
3138 '((((type tty pc))
42121c23
SZ
3139 :underline t
3140 :weight bold)
973a3104 3141 (t
42121c23
SZ
3142 :inherit variable-pitch
3143 :weight bold))
973a3104
MB
3144 "Face for headers in Info menus."
3145 :group 'info)
3146
3536c121
LK
3147(defun Info-escape-percent (string)
3148 "Double all occurrences of `%' in STRING.
3149
3150Return a new string with all `%' characters replaced by `%%'.
3151Preserve text properties."
3152 (let ((start 0)
3153 (end (length string))
3154 mb me m matches)
3155 (save-match-data
3156 (while (and (< start end) (string-match "%" string start))
3157 (setq mb (match-beginning 0)
3158 me (1+ mb)
3159 m (substring string mb me)
1ed6e0c3
JPW
3160 matches (cons m
3161 (cons m
3162 (cons (substring string start mb)
3536c121
LK
3163 matches)))
3164 start me))
3165 (push (substring string start end) matches)
3166 (apply #'concat (nreverse matches)))))
3167
efb21aab
RS
3168(defvar Info-next-link-keymap
3169 (let ((keymap (make-sparse-keymap)))
3170 (define-key keymap [header-line mouse-1] 'Info-next)
3171 (define-key keymap [header-line mouse-2] 'Info-next)
3172 (define-key keymap [header-line down-mouse-1] 'ignore)
3173 (define-key keymap [mouse-2] 'Info-next)
3174 keymap)
3175 "Keymap to put on the Next link in the text or the header line.")
3176
3177(defvar Info-prev-link-keymap
3178 (let ((keymap (make-sparse-keymap)))
3179 (define-key keymap [header-line mouse-1] 'Info-prev)
3180 (define-key keymap [header-line mouse-2] 'Info-prev)
3181 (define-key keymap [header-line down-mouse-1] 'ignore)
3182 (define-key keymap [mouse-2] 'Info-prev)
3183 keymap)
3184 "Keymap to put on the Prev link in the text or the header line.")
3185
3186
3187(defvar Info-up-link-keymap
3188 (let ((keymap (make-sparse-keymap)))
3189 (define-key keymap [header-line mouse-1] 'Info-up)
3190 (define-key keymap [header-line mouse-2] 'Info-up)
3191 (define-key keymap [header-line down-mouse-1] 'ignore)
3192 (define-key keymap [mouse-2] 'Info-up)
3193 keymap)
3194 "Keymap to put on the Up link in the text or the header line.")
3195
552775bd 3196(defun Info-fontify-node ()
8a7757f6
JL
3197 "Fontify the node."
3198 (save-excursion
3199 (let* ((inhibit-read-only t)
3200 (case-fold-search t)
3201 paragraph-markers
3202 (not-fontified-p ; the node hasn't already been fontified
3203 (not (let ((where (next-property-change (point-min))))
3204 (and where (not (= where (point-max)))))))
3205 (fontify-visited-p ; visited nodes need to be re-fontified
3206 (and Info-fontify-visited-nodes
3207 ;; Don't take time to refontify visited nodes in huge nodes
3208 (< (- (point-max) (point-min)) Info-fontify-maximum-menu-size))))
3209
3210 ;; Fontify header line
3211 (goto-char (point-min))
3212 (when (and not-fontified-p (looking-at "^\\(File: [^,: \t]+,?[ \t]+\\)?"))
3213 (goto-char (match-end 0))
3214 (while (looking-at "[ \t]*\\([^:, \t\n]+\\):[ \t]+\\([^:,\t\n]+\\),?")
3215 (goto-char (match-end 0))
3216 (let* ((nbeg (match-beginning 2))
3217 (nend (match-end 2))
3218 (tbeg (match-beginning 1))
3219 (tag (match-string 1)))
3220 (if (string-equal tag "Node")
3221 (put-text-property nbeg nend 'font-lock-face 'info-header-node)
3222 (put-text-property nbeg nend 'font-lock-face 'info-header-xref)
3223 (put-text-property tbeg nend 'mouse-face 'highlight)
3224 (put-text-property tbeg nend
3225 'help-echo
3226 (concat "Go to node "
3227 (buffer-substring nbeg nend)))
3228 ;; Always set up the text property keymap.
3229 ;; It will either be used in the buffer
3230 ;; or copied in the header line.
3231 (put-text-property tbeg nend 'keymap
3232 (cond
3233 ((equal tag "Prev") Info-prev-link-keymap)
3234 ((equal tag "Next") Info-next-link-keymap)
3235 ((equal tag "Up") Info-up-link-keymap))))))
3236 (when Info-use-header-line
3237 (goto-char (point-min))
3238 (let ((header-end (line-end-position))
3239 header)
3240 ;; If we find neither Next: nor Prev: link, show the entire
3241 ;; node header. Otherwise, don't show the File: and Node:
3242 ;; parts, to avoid wasting precious space on information that
3243 ;; is available in the mode line.
3244 (if (re-search-forward
3245 "\\(next\\|up\\|prev[ious]*\\): "
3246 header-end t)
3247 (progn
3248 (goto-char (match-beginning 1))
3249 (setq header (buffer-substring (point) header-end)))
3250 (if (re-search-forward "node:[ \t]*[^ \t]+[ \t]*" header-end t)
3251 (setq header
3252 (concat "No next, prev or up links -- "
3253 (buffer-substring (point) header-end)))
3254 (setq header (buffer-substring (point) header-end))))
3255 (put-text-property (point-min) (1+ (point-min))
3256 'header-line (Info-escape-percent header))
3257 ;; Hide the part of the first line
3258 ;; that is in the header, if it is just part.
3259 (unless (bobp)
3260 ;; Hide the punctuation at the end, too.
3261 (skip-chars-backward " \t,")
3262 (put-text-property (point) header-end 'invisible t)))))
3263
3264 ;; Fontify titles
3265 (goto-char (point-min))
3266 (when not-fontified-p
3267 (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\|\\.+\\)$"
3268 nil t)
3269 (let* ((c (preceding-char))
3270 (face
3271 (cond ((= c ?*) 'Info-title-1-face)
3272 ((= c ?=) 'Info-title-2-face)
3273 ((= c ?-) 'Info-title-3-face)
3274 (t 'Info-title-4-face))))
3275 (put-text-property (match-beginning 1) (match-end 1)
3276 'font-lock-face face))
3277 ;; This is a serious problem for trying to handle multiple
3278 ;; frame types at once. We want this text to be invisible
3279 ;; on frames that can display the font above.
3280 (when (memq (framep (selected-frame)) '(x pc w32 mac))
3281 (add-text-properties (1- (match-beginning 2)) (match-end 2)
3282 '(invisible t front-sticky nil rear-nonsticky t)))))
3283
3284 ;; Fontify cross references
3285 (goto-char (point-min))
3286 (when (or not-fontified-p fontify-visited-p)
3287 (while (re-search-forward "\\(\\*Note[ \n\t]+\\)\\([^:]*\\)\\(:[ \t]*\\([^.,:(]*\\)\\(\\(([^)]*)\\)[^.,:]*\\)?[,:]?\n?\\)" nil t)
3288 (let ((start (match-beginning 0))
3289 (next (point))
3290 other-tag)
3291 (when not-fontified-p
3292 (when Info-hide-note-references
3293 ;; *Note is often used where *note should have been
3294 (goto-char start)
3295 (skip-syntax-backward " ")
3296 (setq other-tag
3297 (cond ((memq (char-before) '(nil ?\. ?! ??))
3298 "See ")
3299 ((memq (char-before) '(?\, ?\; ?\: ?-))
3300 "see ")
3301 ((memq (char-before) '(?\( ?\[ ?\{))
3302 ;; Check whether the paren is preceded by
3303 ;; an end of sentence
3304 (skip-syntax-backward " (")
3305 (if (memq (char-before) '(nil ?\. ?! ??))
3306 "See "
3307 "see "))
3308 ((save-match-data (looking-at "\n\n"))
3309 "See ")))
3310 (goto-char next)
3311 (add-text-properties
3312 (match-beginning 1)
3313 (or (save-match-data
3314 ;; Don't hide \n after *Note
3315 (let ((start1 (match-beginning 1)))
3316 (if (string-match "\n" (match-string 1))
3317 (+ start1 (match-beginning 0)))))
3318 (match-end 1))
3319 (if (and other-tag (not (eq Info-hide-note-references 'hide)))
3320 `(display ,other-tag front-sticky nil rear-nonsticky t)
3321 '(invisible t front-sticky nil rear-nonsticky t))))
3322 (add-text-properties
3323 (match-beginning 2) (match-end 2)
3324 (list
3325 'help-echo (if (or (match-end 5)
3326 (not (equal (match-string 4) "")))
3327 (concat "mouse-2: go to " (or (match-string 5)
3328 (match-string 4)))
3329 "mouse-2: go to this node")
3330 'mouse-face 'highlight)))
3331 (when (or not-fontified-p fontify-visited-p)
3332 (add-text-properties
3333 (match-beginning 2) (match-end 2)
3334 (list
3335 'font-lock-face
3336 ;; Display visited nodes in a different face
3337 (if (and Info-fontify-visited-nodes
3338 (save-match-data
3339 (let* ((node (replace-regexp-in-string
3340 "^[ \t]+" ""
3341 (replace-regexp-in-string
3342 "[ \t\n]+" " "
3343 (or (match-string 5)
3344 (and (not (equal (match-string 4) ""))
3345 (match-string 4))
3346 (match-string 2)))))
3347 (file (file-name-nondirectory
3348 Info-current-file))
3349 (hl Info-history-list)
3350 res)
3351 (if (string-match "(\\([^)]+\\))\\([^)]*\\)" node)
3352 (setq file (match-string 1 node)
3353 node (if (equal (match-string 2 node) "")
3354 "Top"
3355 (match-string 2 node))))
3356 (while hl
3357 (if (and (string-equal node (nth 1 (car hl)))
3358 (string-equal file
3359 (file-name-nondirectory
3360 (nth 0 (car hl)))))
3361 (setq res (car hl) hl nil)
3362 (setq hl (cdr hl))))
3363 res))) 'info-xref-visited 'info-xref))))
3364 (when not-fontified-p
3365 (when (memq Info-hide-note-references '(t hide))
3366 (add-text-properties (match-beginning 3) (match-end 3)
3367 '(invisible t front-sticky nil rear-nonsticky t))
3368 ;; Unhide the file name of the external reference in parens
bf7d6fa7 3369 (if (and (match-string 6) (not (eq Info-hide-note-references 'hide)))
8a7757f6
JL
3370 (remove-text-properties (match-beginning 6) (match-end 6)
3371 '(invisible t front-sticky nil rear-nonsticky t)))
3372 ;; Unhide newline because hidden newlines cause too long lines
3373 (save-match-data
bf7d6fa7
JL
3374 (let ((beg3 (match-beginning 3))
3375 (end3 (match-end 3)))
3376 (if (and (string-match "\n[ \t]*" (match-string 3))
3377 (not (save-match-data
3378 (save-excursion
3379 (goto-char (1+ end3))
3380 (looking-at "[.)]*$")))))
3381 (remove-text-properties (+ beg3 (match-beginning 0))
3382 (+ beg3 (match-end 0))
8a7757f6
JL
3383 '(invisible t front-sticky nil rear-nonsticky t))))))
3384 (when (and Info-refill-paragraphs Info-hide-note-references)
3385 (push (set-marker (make-marker) start)
3386 paragraph-markers))))))
3387
3388 ;; Refill paragraphs (experimental feature)
3389 (when (and not-fontified-p
3390 Info-refill-paragraphs
3391 paragraph-markers)
3392 (let ((fill-nobreak-invisible t)
3393 (fill-individual-varying-indent nil)
3394 (paragraph-start "\f\\|[ \t]*[-*]\\|[ \t]*$")
3395 (paragraph-separate ".*\\.[ \t]*\n[ \t]\\|[ \t]*[-*]\\|[ \t\f]*$")
3396 (adaptive-fill-mode nil))
3397 (goto-char (point-max))
3398 (while paragraph-markers
3399 (let ((m (car paragraph-markers)))
3400 (setq paragraph-markers (cdr paragraph-markers))
3401 (when (< m (point))
3402 (goto-char m)
3403 (beginning-of-line)
3404 (let ((beg (point)))
3405 (when (zerop (forward-paragraph))
3406 (fill-individual-paragraphs beg (point) nil nil)
3407 (goto-char beg))))
3408 (set-marker m nil)))))
3409
3410 ;; Fontify menu items
3411 (goto-char (point-min))
3412 (when (and (or not-fontified-p fontify-visited-p)
3413 (search-forward "\n* Menu:" nil t)
3414 (not (string-match "\\<Index\\>" Info-current-node))
3415 ;; Don't take time to annotate huge menus
3416 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
3417 (let ((n 0)
3418 cont)
3419 (while (re-search-forward
3420 (concat "^\\* +\\(" Info-menu-entry-name-re "\\)\\(:"
3421 Info-node-spec-re "\\([ \t]*\\)\\)")
3422 nil t)
3423 (when not-fontified-p
3424 (setq n (1+ n))
3425 (if (and (<= n 9) (zerop (% n 3))) ; visual aids to help with 1-9 keys
3426 (put-text-property (match-beginning 0)
3427 (1+ (match-beginning 0))
3428 'font-lock-face 'info-menu-5)))
3429 (when not-fontified-p
3430 (add-text-properties
3431 (match-beginning 1) (match-end 1)
3432 (list
3433 'help-echo (if (match-end 3)
3434 (concat "mouse-2: go to " (match-string 3))
3435 "mouse-2: go to this node")
3436 'mouse-face 'highlight)))
3437 (when (or not-fontified-p fontify-visited-p)
3438 (add-text-properties
3439 (match-beginning 1) (match-end 1)
3440 (list
3441 'font-lock-face
3442 ;; Display visited menu items in a different face
3443 (if (and Info-fontify-visited-nodes
3444 (save-match-data
3445 (let ((node (if (equal (match-string 3) "")
3446 (match-string 1)
3447 (match-string 3)))
3448 (file (file-name-nondirectory Info-current-file))
3449 (hl Info-history-list)
3450 res)
3451 (if (string-match "(\\([^)]+\\))\\([^)]*\\)" node)
3452 (setq file (match-string 1 node)
3453 node (if (equal (match-string 2 node) "")
3454 "Top"
3455 (match-string 2 node))))
3456 (while hl
3457 (if (and (string-equal node (nth 1 (car hl)))
3458 (string-equal file
3459 (file-name-nondirectory
3460 (nth 0 (car hl)))))
3461 (setq res (car hl) hl nil)
3462 (setq hl (cdr hl))))
3463 res))) 'info-xref-visited 'info-xref))))
3464 (when (and not-fontified-p (memq Info-hide-note-references '(t hide)))
3465 (put-text-property (match-beginning 2) (1- (match-end 6))
3466 'invisible t)
3467 ;; Unhide the file name in parens
3468 (if (and (match-end 4) (not (eq (char-after (match-end 4)) ?.)))
3469 (remove-text-properties (match-beginning 4) (match-end 4)
3470 '(invisible t)))
3471 ;; We need a stretchable space like :align-to but with
3472 ;; a minimum value.
3473 (put-text-property (1- (match-end 6)) (match-end 6) 'display
3474 (if (>= 22 (- (match-end 1)
3475 (match-beginning 0)))
3476 '(space :align-to 24)
3477 '(space :width 2)))
3478 (setq cont (looking-at "."))
3479 (while (and (= (forward-line 1) 0)
3480 (looking-at "\\([ \t]+\\)[^*\n]"))
3481 (put-text-property (match-beginning 1) (1- (match-end 1))
3482 'invisible t)
3483 (put-text-property (1- (match-end 1)) (match-end 1)
3484 'display
3485 (if cont
3486 '(space :align-to 26)
3487 '(space :align-to 24)))
3488 (setq cont t))))))
3489
3490 ;; Fontify menu headers
3491 ;; Add the face `info-menu-header' to any header before a menu entry
3492 (goto-char (point-min))
3493 (when (and not-fontified-p (re-search-forward "^\\* Menu:" nil t))
3494 (put-text-property (match-beginning 0) (match-end 0)
3495 'font-lock-face 'info-menu-header)
3496 (while (re-search-forward "\n\n\\([^*\n ].*\\)\n\n?[*]" nil t)
3497 (put-text-property (match-beginning 1) (match-end 1)
3498 'font-lock-face 'info-menu-header)))
3499
3500 ;; Fontify http and ftp references
3501 (goto-char (point-min))
3502 (when not-fontified-p
3503 (while (re-search-forward "[hf]t?tp://[^ \t\n\"`({<>})']+" nil t)
3504 (add-text-properties (match-beginning 0) (match-end 0)
3505 '(font-lock-face info-xref
3506 mouse-face highlight
3507 help-echo "mouse-2: go to this URL"))))
3508
3509 (set-buffer-modified-p nil))))
c5fe2ff1
RS
3510\f
3511
3512;; When an Info buffer is killed, make sure the associated tags buffer
3513;; is killed too.
3514(defun Info-kill-buffer ()
3515 (and (eq major-mode 'Info-mode)
3516 Info-tag-table-buffer
3517 (kill-buffer Info-tag-table-buffer)))
3518
3519(add-hook 'kill-buffer-hook 'Info-kill-buffer)
3cb6768f
EL
3520
3521;;; Speedbar support:
3522;; These functions permit speedbar to display the "tags" in the
3523;; current info node.
96ee3f29 3524(eval-when-compile (require 'speedbar))
3cb6768f 3525
96ee3f29
KH
3526(defvar Info-speedbar-key-map nil
3527 "Keymap used when in the info display mode.")
3cb6768f 3528
96ee3f29
KH
3529(defun Info-install-speedbar-variables ()
3530 "Install those variables used by speedbar to enhance Info."
3531 (if Info-speedbar-key-map
3532 nil
3533 (setq Info-speedbar-key-map (speedbar-make-specialized-keymap))
3534
3535 ;; Basic tree features
3536 (define-key Info-speedbar-key-map "e" 'speedbar-edit-line)
3537 (define-key Info-speedbar-key-map "\C-m" 'speedbar-edit-line)
3538 (define-key Info-speedbar-key-map "+" 'speedbar-expand-line)
3539 (define-key Info-speedbar-key-map "-" 'speedbar-contract-line)
3540 )
3541
3542 (speedbar-add-expansion-list '("Info" Info-speedbar-menu-items
3543 Info-speedbar-key-map
3544 Info-speedbar-hierarchy-buttons)))
3cb6768f
EL
3545
3546(defvar Info-speedbar-menu-items
96ee3f29
KH
3547 '(["Browse Node" speedbar-edit-line t]
3548 ["Expand Node" speedbar-expand-line
3549 (save-excursion (beginning-of-line)
3550 (looking-at "[0-9]+: *.\\+. "))]
3551 ["Contract Node" speedbar-contract-line
3552 (save-excursion (beginning-of-line)
3553 (looking-at "[0-9]+: *.-. "))]
3554 )
3cb6768f
EL
3555 "Additional menu-items to add to speedbar frame.")
3556
96ee3f29
KH
3557;; Make sure our special speedbar major mode is loaded
3558(if (featurep 'speedbar)
3559 (Info-install-speedbar-variables)
3560 (add-hook 'speedbar-load-hook 'Info-install-speedbar-variables))
3561
3562;;; Info hierarchy display method
3563;;;###autoload
3564(defun Info-speedbar-browser ()
3565 "Initialize speedbar to display an info node browser.
3566This will add a speedbar major display mode."
3567 (interactive)
3568 (require 'speedbar)
3569 ;; Make sure that speedbar is active
3570 (speedbar-frame-mode 1)
3571 ;; Now, throw us into Info mode on speedbar.
3572 (speedbar-change-initial-expansion-list "Info")
3573 )
3574
7210b6f5
DL
3575(eval-when-compile (defvar speedbar-attached-frame))
3576
96ee3f29
KH
3577(defun Info-speedbar-hierarchy-buttons (directory depth &optional node)
3578 "Display an Info directory hierarchy in speedbar.
3579DIRECTORY is the current directory in the attached frame.
3580DEPTH is the current indentation depth.
3581NODE is an optional argument that is used to represent the
3582specific node to expand."
3583 (if (and (not node)
3584 (save-excursion (goto-char (point-min))
1bcedb3b
RS
3585 (let ((case-fold-search t))
3586 (looking-at "Info Nodes:"))))
96ee3f29
KH
3587 ;; Update our "current node" maybe?
3588 nil
3589 ;; We cannot use the generic list code, that depends on all leaves
3590 ;; being known at creation time.
3591 (if (not node)
3592 (speedbar-with-writable (insert "Info Nodes:\n")))
5fdc7997
EL
3593 (let ((completions nil)
3594 (cf (selected-frame)))
3595 (select-frame speedbar-attached-frame)
3596 (save-window-excursion
3597 (setq completions
3598 (Info-speedbar-fetch-file-nodes (or node '"(dir)top"))))
3599 (select-frame cf)
96ee3f29
KH
3600 (if completions
3601 (speedbar-with-writable
dde7f979 3602 (dolist (completion completions)
96ee3f29 3603 (speedbar-make-tag-line 'bracket ?+ 'Info-speedbar-expand-node
dde7f979
SM
3604 (cdr completion)
3605 (car completion)
96ee3f29 3606 'Info-speedbar-goto-node
dde7f979
SM
3607 (cdr completion)
3608 'info-xref depth))
96ee3f29
KH
3609 t)
3610 nil))))
399c88ad 3611
96ee3f29 3612(defun Info-speedbar-goto-node (text node indent)
d1268e52 3613 "When user clicks on TEXT, go to an info NODE.
96ee3f29 3614The INDENT level is ignored."
760d5cb3
GM
3615 (select-frame speedbar-attached-frame)
3616 (let* ((buff (or (get-buffer "*info*")
3617 (progn (info) (get-buffer "*info*"))))
3618 (bwin (get-buffer-window buff 0)))
3619 (if bwin
3620 (progn
3621 (select-window bwin)
3622 (raise-frame (window-frame bwin)))
3623 (if speedbar-power-click
3624 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3625 (select-frame speedbar-attached-frame)
3626 (switch-to-buffer buff)))
dde7f979
SM
3627 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" node))
3628 (error "Invalid node %s" node)
3629 (Info-find-node (match-string 1 node) (match-string 2 node))
760d5cb3
GM
3630 ;; If we do a find-node, and we were in info mode, restore
3631 ;; the old default method. Once we are in info mode, it makes
3632 ;; sense to return to whatever method the user was using before.
3633 (if (string= speedbar-initial-expansion-list-name "Info")
3634 (speedbar-change-initial-expansion-list
3635 speedbar-previously-used-expansion-list-name)))))
96ee3f29
KH
3636
3637(defun Info-speedbar-expand-node (text token indent)
3638 "Expand the node the user clicked on.
3639TEXT is the text of the button we clicked on, a + or - item.
3640TOKEN is data related to this node (NAME . FILE).
3641INDENT is the current indentation depth."
3642 (cond ((string-match "+" text) ;we have to expand this file
3643 (speedbar-change-expand-button-char ?-)
3644 (if (speedbar-with-writable
760d5cb3
GM
3645 (save-excursion
3646 (end-of-line) (forward-char 1)
3647 (Info-speedbar-hierarchy-buttons nil (1+ indent) token)))
96ee3f29
KH
3648 (speedbar-change-expand-button-char ?-)
3649 (speedbar-change-expand-button-char ??)))
3650 ((string-match "-" text) ;we have to contract this node
3651 (speedbar-change-expand-button-char ?+)
3652 (speedbar-delete-subblock indent))
3653 (t (error "Ooops... not sure what to do")))
3654 (speedbar-center-buffer-smartly))
3655
3656(defun Info-speedbar-fetch-file-nodes (nodespec)
3657 "Fetch the subnodes from the info NODESPEC.
3658NODESPEC is a string of the form: (file)node.
3659Optional THISFILE represends the filename of"
3660 (save-excursion
3661 ;; Set up a buffer we can use to fake-out Info.
3662 (set-buffer (get-buffer-create "*info-browse-tmp*"))
3663 (if (not (equal major-mode 'Info-mode))
3664 (Info-mode))
3665 ;; Get the node into this buffer
dde7f979
SM
3666 (if (not (string-match "^(\\([^)]+\\))\\([^.]+\\)$" nodespec))
3667 (error "Invalid node specification %s" nodespec)
3668 (Info-find-node (match-string 1 nodespec) (match-string 2 nodespec)))
96ee3f29
KH
3669 ;; Scan the created buffer
3670 (goto-char (point-min))
3671 (let ((completions nil)
1bcedb3b 3672 (case-fold-search t)
96ee3f29
KH
3673 (thisfile (progn (string-match "^(\\([^)]+\\))" nodespec)
3674 (match-string 1 nodespec))))
3675 ;; Always skip the first one...
3676 (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
3677 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
3678 (let ((name (match-string 1)))
dde7f979
SM
3679 (push (cons name
3680 (if (looking-at " *\\(([^)]+)[^.\n]+\\)\\.")
3681 (match-string 1)
3682 (if (looking-at " *\\(([^)]+)\\)\\.")
3683 (concat (match-string 1) "Top")
3684 (concat "(" thisfile ")"
3685 (if (looking-at " \\([^.]+\\).")
3686 (match-string 1)
3687 name)))))
3688 completions)))
96ee3f29
KH
3689 (nreverse completions))))
3690
3691;;; Info mode node listing
a1637357 3692;; FIXME: Seems not to be used. -stef
3cb6768f
EL
3693(defun Info-speedbar-buttons (buffer)
3694 "Create a speedbar display to help navigation in an Info file.
3695BUFFER is the buffer speedbar is requesting buttons for."
96ee3f29 3696 (if (save-excursion (goto-char (point-min))
1bcedb3b
RS
3697 (let ((case-fold-search t))
3698 (not (looking-at "Info Nodes:"))))
96ee3f29
KH
3699 (erase-buffer))
3700 (Info-speedbar-hierarchy-buttons nil 0)
3701 )
49116ac0 3702
48e5b471
DL
3703(dolist (mess '("^Node has no Previous$"
3704 "^No menu in this node$"
3705 "^Node has no Next$"
399c88ad
SS
3706 "^No cross-references in this node^"
3707 search-failed
48e5b471
DL
3708 "^No \".*\" in index$"))
3709 (add-to-list 'debug-ignored-errors mess))
3710
49116ac0
JB
3711(provide 'info)
3712
ab5796a9 3713;;; arch-tag: f2480fe2-2139-40c1-a49b-6314991164ac
1a06eabd 3714;;; info.el ends here