New file.
[bpt/emacs.git] / lisp / info.el
CommitLineData
55535639 1;;; info.el --- info package for Emacs
e5167999 2
5b3ebb44
GM
3;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001
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.
e5167999
ER
29
30;;; Code:
31
bc6262c8
DL
32(eval-when-compile (require 'jka-compr))
33
ded3e3d8
RS
34(defgroup info nil
35 "Info subsystem"
36 :group 'help
37 :group 'docs)
38
39
a384cab3
JB
40(defvar Info-history nil
41 "List of info nodes user has visited.
42Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
43
ded3e3d8 44(defcustom Info-enable-edit nil
d5913bf6 45 "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info can edit the current node.
ada0a60d
RS
46This is convenient if you want to write info files by hand.
47However, we recommend that you not do this.
48It is better to write a Texinfo file and generate the Info file from that,
ded3e3d8
RS
49because that gives you a printed manual as well."
50 :type 'boolean
51 :group 'info)
a384cab3 52
67f445d7 53(defvar Info-enable-active-nodes nil
a384cab3
JB
54 "Non-nil allows Info to execute Lisp code associated with nodes.
55The Lisp code is executed when the node is selected.")
67f445d7 56(put 'Info-enable-active-nodes 'risky-local-variable t)
a384cab3 57
ded3e3d8
RS
58(defcustom Info-fontify t
59 "*Non-nil enables highlighting and fonts in Info nodes."
60 :type 'boolean
61 :group 'info)
552775bd 62
93480d70 63(defface info-node
52cbdfbd
MB
64 '((((class color) (background light)) (:foreground "brown" :bold t :italic t))
65 (((class color) (background dark)) (:foreground "white" :bold t :italic t))
7a3abcd8 66 (t (:bold t :italic t)))
93480d70
RS
67 "Face for Info node names."
68 :group 'info)
69
70(defface info-menu-5
7a3abcd8
EZ
71 '((((class color)) (:foreground "red1"))
72 (t (:underline t)))
030de92f 73 "Face for the fifth and nineth `*' in an Info menu."
93480d70
RS
74 :group 'info)
75
76(defface info-xref
52cbdfbd
MB
77 '((((class color) (background light)) (:foreground "magenta4" :bold t))
78 (((class color) (background dark)) (:foreground "cyan" :bold t))
7a3abcd8 79 (t (:bold t)))
93480d70
RS
80 "Face for Info cross-references."
81 :group 'info)
82
88ef7f62 83(defcustom Info-fontify-maximum-menu-size 100000
ded3e3d8
RS
84 "*Maximum size of menu to fontify if `Info-fontify' is non-nil."
85 :type 'integer
86 :group 'info)
bdf62a4d 87
50ac70af
MB
88(defcustom Info-use-header-line t
89 "*Non-nil means to put the beginning-of-node links in an emacs header-line.
90A header-line does not scroll with the rest of the buffer."
91 :type 'boolean
92 :group 'info)
93
94(defface info-header-xref
52cbdfbd 95 '((t (:inherit info-xref)))
50ac70af
MB
96 "Face for Info cross-references in a node header."
97 :group 'info)
98
99(defface info-header-node
4ad6e379 100 '((t (:inherit info-node)))
50ac70af
MB
101 "Face for Info nodes in a node header."
102 :group 'info)
103
7d3766a8 104(defvar Info-directory-list nil
a384cab3 105 "List of directories to search for Info documentation files.
aea2a8da 106nil means not yet initialized. In this case, Info uses the environment
44c327f9 107variable INFOPATH to initialize it, or `Info-default-directory-list'
e5bd2125 108if there is no INFOPATH variable in the environment.
778911b9
EZ
109
110When `Info-directory-list' is initialized from the value of
4dddb0b7
EZ
111`Info-default-directory-list', and Emacs is installed in one of the
112standard directories, the directory of Info files that come with Emacs
113is put last (so that local Info files override standard ones).
114
115When `Info-directory-list' is initialized from the value of
116`Info-default-directory-list', and Emacs is not installed in one
117of the standard directories, the first element of the resulting
778911b9
EZ
118list is the directory where Emacs installs the Info files that
119come with it. This is so that Emacs's own manual, which suits the
4dddb0b7
EZ
120version of Emacs you are using, will always be found first. This
121is useful when you install an experimental version of Emacs without
122removing the standard installation.
123
124If you want to override the order of directories in
125`Info-default-directory-list', set INFOPATH in the environment.
5da0f945
RS
126
127If you run the Emacs executable from the `src' directory in the Emacs
778911b9
EZ
128source tree, and INFOPATH is not defined, the `info' directory in the
129source tree is used as the first element of `Info-directory-list', in
130place of the installation Info directory. This is useful when you run
131a version of Emacs without installing it.")
a384cab3 132
ded3e3d8 133(defcustom Info-additional-directory-list nil
bdf62a4d 134 "List of additional directories to search for Info documentation files.
4645bb68 135These directories are searched after those in `Info-directory-list', and
60322163 136they are not searched for merging the `dir' file."
ded3e3d8
RS
137 :type '(repeat directory)
138 :group 'info)
bdf62a4d 139
7a53d8c8
EZ
140(defcustom Info-scroll-prefer-subnodes t
141 "*If non-nil, \\<Info-mode-map>\\[Info-scroll-up] in a menu visits subnodes.
142If this is non-nil, and you scroll far enough in a node that its menu
143appears on the screen, the next \\<Info-mode-map>\\[Info-scroll-up]
144moves to a subnode indicated by the following menu item. This means
145that you visit a subnode before getting to the end of the menu.
146
147Setting this option to nil results in behavior similar to the stand-alone
148Info reader program, which visits the first subnode from the menu only
149when you hit the end of the current node."
150 :type 'boolean
151 :group 'info)
152
a384cab3 153(defvar Info-current-file nil
d6be34f3
RS
154 "Info file that Info is now looking at, or nil.
155This is the name that was specified in Info, not the actual file name.
4fceda3c
SM
156It doesn't contain directory names or file name extensions added by Info.
157Can also be t when using `Info-on-current-buffer'.")
a384cab3
JB
158
159(defvar Info-current-subfile nil
fdf4b680
DL
160 "Info subfile that is actually in the *info* buffer now.
161nil if current info file is not split into subfiles.")
a384cab3
JB
162
163(defvar Info-current-node nil
164 "Name of node that Info is now looking at, or nil.")
165
c5fe2ff1 166(defvar Info-tag-table-marker nil
a384cab3
JB
167 "Marker pointing at beginning of current Info file's tag table.
168Marker points nowhere if file has no tag table.")
169
c5fe2ff1
RS
170(defvar Info-tag-table-buffer nil
171 "Buffer used for indirect tag tables.")
172
552775bd
RS
173(defvar Info-current-file-completions nil
174 "Cached completion list for current Info file.")
175
1143a6b0 176(defvar Info-index-alternatives nil
fdf4b680 177 "List of possible matches for last `Info-index' command.")
1143a6b0 178
552775bd
RS
179(defvar Info-standalone nil
180 "Non-nil if Emacs was started solely as an Info browser.")
7d3766a8 181\f
bd0c9168 182(defvar Info-suffix-list
3c19e6c1
RS
183 ;; The MS-DOS list should work both when long file names are
184 ;; supported (Windows 9X), and when only 8+3 file names are available.
bd0c9168
RS
185 (if (eq system-type 'ms-dos)
186 '( (".gz" . "gunzip")
187 (".z" . "gunzip")
544e210f 188 (".bz2" . ("bzip2" "-dc"))
3c19e6c1
RS
189 (".inz" . "gunzip")
190 (".igz" . "gunzip")
191 (".info.Z" . "gunzip")
192 (".info.gz" . "gunzip")
193 ("-info.Z" . "gunzip")
194 ("-info.gz" . "gunzip")
195 ("/index.gz". "gunzip")
196 ("/index.z" . "gunzip")
9b842cab 197 (".inf" . nil)
3c19e6c1
RS
198 (".info" . nil)
199 ("-info" . nil)
200 ("/index" . nil)
bd0c9168 201 ("" . nil))
a70dc410
RS
202 '( (".info.Z". "uncompress")
203 (".info.Y". "unyabba")
204 (".info.gz". "gunzip")
205 (".info.z". "gunzip")
544e210f 206 (".info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
207 (".info". nil)
208 ("-info.Z". "uncompress")
209 ("-info.Y". "unyabba")
210 ("-info.gz". "gunzip")
544e210f 211 ("-info.bz2" . ("bzip2" "-dc"))
a70dc410
RS
212 ("-info.z". "gunzip")
213 ("-info". nil)
214 ("/index.Z". "uncompress")
215 ("/index.Y". "unyabba")
216 ("/index.gz". "gunzip")
217 ("/index.z". "gunzip")
544e210f 218 ("/index.bz2". ("bzip2" "-dc"))
a70dc410
RS
219 ("/index". nil)
220 (".Z". "uncompress")
221 (".Y". "unyabba")
222 (".gz". "gunzip")
223 (".z". "gunzip")
544e210f 224 (".bz2" . ("bzip2" "-dc"))
a70dc410 225 ("". nil)))
1143a6b0
ER
226 "List of file name suffixes and associated decoding commands.
227Each entry should be (SUFFIX . STRING); the file is given to
544e210f
KH
228the command as standard input.
229
230STRING may be a list of strings. In that case, the first element is
231the command name, and the rest are arguments to that command.
232
233If STRING is nil, no decoding is done.
97f99202
KH
234Because the SUFFIXes are tried in order, the empty string should
235be last in the list.")
1143a6b0 236
c8e9dd54 237;; Concatenate SUFFIX onto FILENAME. SUFFIX should start with a dot.
e0c1e774
EZ
238;; First, on MS-DOS with no long file names support, delete some of
239;; the extension in FILENAME to make room.
240(defun info-insert-file-contents-1 (filename suffix lfn)
241 (if lfn ; long file names are supported
bd0c9168
RS
242 (concat filename suffix)
243 (let* ((sans-exts (file-name-sans-extension filename))
c8e9dd54
RS
244 ;; How long is the extension in FILENAME (not counting the dot).
245 (ext-len (max 0 (- (length filename) (length sans-exts) 1)))
246 ext-left)
9b842cab 247 ;; SUFFIX starts with a dot. If FILENAME already has one,
1583fe94 248 ;; get rid of the one in SUFFIX (unless suffix is empty).
c8e9dd54 249 (or (and (<= ext-len 0)
9b842cab 250 (not (eq (aref filename (1- (length filename))) ?.)))
1583fe94 251 (= (length suffix) 0)
9b842cab 252 (setq suffix (substring suffix 1)))
c8e9dd54
RS
253 ;; How many chars of that extension should we keep?
254 (setq ext-left (min ext-len (max 0 (- 3 (length suffix)))))
bd0c9168
RS
255 ;; Get rid of the rest of the extension, and add SUFFIX.
256 (concat (substring filename 0 (- (length filename)
257 (- ext-len ext-left)))
258 suffix))))
259
9c7924d5
RS
260(defun info-file-exists-p (filename)
261 (and (file-exists-p filename)
262 (not (file-directory-p filename))))
263
1143a6b0
ER
264(defun info-insert-file-contents (filename &optional visit)
265 "Insert the contents of an info file in the current buffer.
266Do the right thing if the file has been compressed or zipped."
e0c1e774
EZ
267 (let* ((tail Info-suffix-list)
268 (lfn (or (not (fboundp 'msdos-long-file-names))
269 (msdos-long-file-names)))
270 (check-short (and (fboundp 'msdos-long-file-names)
271 lfn))
272 fullname decoder done)
b8fa4e87 273 (if (info-file-exists-p filename)
bd0c9168
RS
274 ;; FILENAME exists--see if that name contains a suffix.
275 ;; If so, set DECODE accordingly.
97f99202
KH
276 (progn
277 (while (and tail
278 (not (string-match
279 (concat (regexp-quote (car (car tail))) "$")
280 filename)))
281 (setq tail (cdr tail)))
282 (setq fullname filename
283 decoder (cdr (car tail))))
bd0c9168 284 ;; Try adding suffixes to FILENAME and see if we can find something.
e0c1e774
EZ
285 (while (and tail (not done))
286 (setq fullname (info-insert-file-contents-1 filename
287 (car (car tail)) lfn))
288 (if (info-file-exists-p fullname)
289 (setq done t
290 ;; If we found a file with a suffix, set DECODER
291 ;; according to the suffix.
292 decoder (cdr (car tail)))
293 ;; When the MS-DOS port runs on Windows, we need to check
294 ;; the short variant of a long file name as well.
295 (when check-short
296 (setq fullname (info-insert-file-contents-1 filename
297 (car (car tail)) nil))
298 (if (info-file-exists-p fullname)
299 (setq done t
300 decoder (cdr (car tail))))))
97f99202 301 (setq tail (cdr tail)))
97f99202 302 (or tail
bd0c9168 303 (error "Can't find %s or any compressed version of it" filename)))
e175bda2
RS
304 ;; check for conflict with jka-compr
305 (if (and (featurep 'jka-compr)
306 (jka-compr-installed-p)
307 (jka-compr-get-compression-info fullname))
308 (setq decoder nil))
97f99202 309 (if decoder
3c19e6c1
RS
310 (progn
311 (insert-file-contents-literally fullname visit)
312 (let ((buffer-read-only nil)
313 (coding-system-for-write 'no-conversion)
314 (default-directory (or (file-name-directory fullname)
315 default-directory)))
544e210f
KH
316 (or (consp decoder)
317 (setq decoder (list decoder)))
318 (apply 'call-process-region (point-min) (point-max)
319 (car decoder) t t nil (cdr decoder))))
3c19e6c1 320 (insert-file-contents fullname visit))))
7d3766a8 321\f
7d3766a8 322(defun info-initialize ()
fdf4b680 323 "Initialize `Info-directory-list', if that hasn't been done yet."
7d3766a8
RS
324 (unless Info-directory-list
325 (let ((path (getenv "INFOPATH"))
326 (source (expand-file-name "info/" source-directory))
327 (sibling (if installation-directory
2c366083 328 (expand-file-name "info/" installation-directory)
fecd556a 329 (if invocation-directory
2c366083
EZ
330 (let ((infodir (expand-file-name
331 "../info/"
332 invocation-directory)))
333 (if (file-exists-p infodir)
334 infodir
335 (setq infodir (expand-file-name
336 "../../../info/"
337 invocation-directory))
338 (and (file-exists-p infodir)
339 infodir))))))
7d3766a8
RS
340 alternative)
341 (setq Info-directory-list
78e7e8a0
MB
342 (prune-directory-list
343 (if path
344 (split-string path (regexp-quote path-separator))
345 (if (and sibling (file-exists-p sibling))
346 ;; Uninstalled, Emacs builddir != srcdir.
347 (setq alternative sibling)
348 ;; Uninstalled, builddir == srcdir
349 (setq alternative source))
350 (if (or (member alternative Info-default-directory-list)
351 ;; On DOS/NT, we use movable executables always,
352 ;; and we must always find the Info dir at run time.
353 (if (memq system-type '(ms-dos windows-nt))
354 nil
355 ;; Use invocation-directory for Info
356 ;; only if we used it for exec-directory also.
357 (not (string= exec-directory
358 (expand-file-name "lib-src/"
359 installation-directory))))
360 (not (file-exists-p alternative)))
361 Info-default-directory-list
362 ;; `alternative' contains the Info files that came with this
363 ;; version, so we should look there first. `Info-insert-dir'
364 ;; currently expects to find `alternative' first on the list.
365 (cons alternative
366 (reverse (cdr (reverse Info-default-directory-list)))))))))))
1143a6b0 367
a6e4564e
RS
368;;;###autoload
369(defun info-other-window (&optional file)
370 "Like `info' but show the Info buffer in another window."
371 (interactive (if current-prefix-arg
372 (list (read-file-name "Info file name: " nil nil t))))
373 (let (same-window-buffer-names)
374 (info file)))
399c88ad 375
d282974b 376;;;###autoload (add-hook 'same-window-buffer-names "*info*")
211d6309 377
a384cab3
JB
378;;;###autoload
379(defun info (&optional file)
380 "Enter Info, the documentation browser.
381Optional argument FILE specifies the file to examine;
382the default is the top-level directory of Info.
83d5c772
DL
383Called from a program, FILE may specify an Info node of the form
384`(FILENAME)NODENAME'.
a384cab3
JB
385
386In interactive use, a prefix argument directs this command
e341dab2
RS
387to read a file name from the minibuffer.
388
389The search path for Info files is in the variable `Info-directory-list'.
399c88ad 390The top-level Info directory is made by combining all the files named `dir'
e341dab2 391in all the directories in that path."
a384cab3
JB
392 (interactive (if current-prefix-arg
393 (list (read-file-name "Info file name: " nil nil t))))
a384cab3 394 (if file
c613e27e
KH
395 (progn
396 (pop-to-buffer "*info*")
397 ;; If argument already contains parentheses, don't add another set
398 ;; since the argument will then be parsed improperly. This also
399 ;; has the added benefit of allowing node names to be included
400 ;; following the parenthesized filename.
401 (if (and (stringp file) (string-match "(.*)" file))
402 (Info-goto-node file)
403 (Info-goto-node (concat "(" file ")"))))
35d2d241
RS
404 (if (get-buffer "*info*")
405 (pop-to-buffer "*info*")
406 (Info-directory))))
a384cab3 407
552775bd
RS
408;;;###autoload
409(defun info-standalone ()
410 "Run Emacs as a standalone Info reader.
411Usage: emacs -f info-standalone [filename]
412In standalone mode, \\<Info-mode-map>\\[Info-exit] exits Emacs itself."
413 (setq Info-standalone t)
414 (if (and command-line-args-left
415 (not (string-match "^-" (car command-line-args-left))))
416 (condition-case err
417 (progn
418 (info (car command-line-args-left))
419 (setq command-line-args-left (cdr command-line-args-left)))
420 (error (send-string-to-terminal
421 (format "%s\n" (if (eq (car-safe err) 'error)
422 (nth 1 err) err)))
423 (save-buffers-kill-emacs)))
424 (info)))
7d3766a8 425\f
983dfbf8 426;; See if the accessible portion of the buffer begins with a node
11638562
EZ
427;; delimiter, and the node header line which follows matches REGEXP.
428;; Typically, this test will be followed by a loop that examines the
429;; rest of the buffer with (search-forward "\n\^_"), and it's a pity
430;; to have the overhead of this special test inside the loop.
431
432;; This function changes match-data, but supposedly the caller might
433;; want to use the results of re-search-backward.
434
435;; The return value is the value of point at the beginning of matching
983dfbf8 436;; REGEXP, if the function succeeds, nil otherwise.
11638562 437(defun Info-node-at-bob-matching (regexp)
760d5cb3
GM
438 (and (bobp) ; are we at beginning of buffer?
439 (looking-at "\^_") ; does it begin with node delimiter?
11638562
EZ
440 (let (beg)
441 (forward-line 1)
442 (setq beg (point))
760d5cb3 443 (forward-line 1) ; does the line after delimiter match REGEXP?
11638562
EZ
444 (re-search-backward regexp beg t))))
445
a384cab3 446(defun Info-find-node (filename nodename &optional no-going-back)
fdf4b680
DL
447 "Go to an info node specified as separate FILENAME and NODENAME.
448NO-GOING-BACK is non-nil if recovering from an error in this function;
449it says do not attempt further (recursive) error recovery."
d0e41cba 450 (info-initialize)
a384cab3
JB
451 ;; Convert filename to lower case if not found as specified.
452 ;; Expand it.
4fceda3c 453 (if (stringp filename)
a384cab3 454 (let (temp temp-downcase found)
37a3ff5b
RS
455 (setq filename (substitute-in-file-name filename))
456 (if (string= (downcase filename) "dir")
457 (setq found t)
458 (let ((dirs (if (string-match "^\\./" filename)
459 ;; If specified name starts with `./'
460 ;; then just try current directory.
461 '("./")
462 (if (file-name-absolute-p filename)
463 ;; No point in searching for an
464 ;; absolute file name
465 '(nil)
466 (if Info-additional-directory-list
467 (append Info-directory-list
468 Info-additional-directory-list)
469 Info-directory-list)))))
470 ;; Search the directory list for file FILENAME.
471 (while (and dirs (not found))
472 (setq temp (expand-file-name filename (car dirs)))
473 (setq temp-downcase
474 (expand-file-name (downcase filename) (car dirs)))
475 ;; Try several variants of specified name.
e0c1e774
EZ
476 (let ((suffix-list Info-suffix-list)
477 (lfn (or (not (fboundp 'msdos-long-file-names))
478 (msdos-long-file-names))))
37a3ff5b
RS
479 (while (and suffix-list (not found))
480 (cond ((info-file-exists-p
481 (info-insert-file-contents-1
e0c1e774 482 temp (car (car suffix-list)) lfn))
37a3ff5b
RS
483 (setq found temp))
484 ((info-file-exists-p
485 (info-insert-file-contents-1
e0c1e774
EZ
486 temp-downcase (car (car suffix-list)) lfn))
487 (setq found temp-downcase))
488 ((and (fboundp 'msdos-long-file-names)
489 lfn
490 (info-file-exists-p
491 (info-insert-file-contents-1
492 temp (car (car suffix-list)) nil)))
493 (setq found temp)))
37a3ff5b
RS
494 (setq suffix-list (cdr suffix-list))))
495 (setq dirs (cdr dirs)))))
496 (if found
497 (setq filename found)
498 (error "Info file %s does not exist" filename))))
499 ;; Record the node we are leaving.
500 (if (and Info-current-file (not no-going-back))
501 (setq Info-history
502 (cons (list Info-current-file Info-current-node (point))
503 Info-history)))
a384cab3 504 ;; Go into info buffer.
c5fe2ff1 505 (or (eq major-mode 'Info-mode) (pop-to-buffer "*info*"))
4fceda3c
SM
506 (Info-find-node-2 filename nodename no-going-back))
507
508(defun Info-on-current-buffer (&optional nodename)
509 "Use the `Info-mode' to browse the current info buffer.
510If a prefix arg is provided, it queries for the NODENAME which
07e91aa3 511else defaults to \"Top\"."
4fceda3c
SM
512 (interactive
513 (list (if current-prefix-arg
514 (completing-read "Node name: " (Info-build-node-completions)
07e91aa3
SM
515 nil t "Top"))))
516 (unless nodename (setq nodename "Top"))
75296efc 517 (info-initialize)
4fceda3c
SM
518 (Info-mode)
519 (set (make-local-variable 'Info-current-file) t)
520 (Info-find-node-2 nil nodename))
521
357d11de
GM
522(defun Info-find-in-tag-table-1 (marker regexp case-fold)
523 "Find a node in a tag table.
524MARKER specifies the buffer and position to start searching at.
525REGEXP is a regular expression matching nodes or references. Its first
526group should match `Node:' or `Ref:'.
527CASE-FOLD t means search for a case-insensitive match.
528If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
529FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
530where the match was found, and MODE is `major-mode' of the buffer in
531which the match was found."
532 (let ((case-fold-search case-fold)
533 found-mode guesspos found-anchor)
534 (save-excursion
535 (set-buffer (marker-buffer marker))
536 (goto-char marker)
537
538 ;; Search tag table
539 (beginning-of-line)
540 (when (re-search-forward regexp nil t)
541 (list (string-equal "Ref:" (match-string 1))
542 (1+ (read (current-buffer)))
543 major-mode)))))
544
545(defun Info-find-in-tag-table (marker regexp)
546 "Find a node in a tag table.
547MARKER specifies the buffer and position to start searching at.
548REGEXP is a regular expression matching nodes or references. Its first
549group should match `Node:' or `Ref:'.
550If a match was found, value is a list (FOUND-ANCHOR POS MODE), where
551FOUND-ANCHOR is non-nil if a `Ref:' was matched, POS is the position
552where the match was found, and MODE is `major-mode' of the buffer in
553which the match was found.
554This function tries to find a case-sensitive match first, then a
555case-insensitive match is tried."
556 (let ((result (Info-find-in-tag-table-1 marker regexp nil)))
557 (when (null (car result))
558 (setq result (Info-find-in-tag-table-1 marker regexp t)))
559 result))
560
561(defun Info-find-node-in-buffer-1 (regexp case-fold)
562 "Find a node or anchor in the current buffer.
563REGEXP is a regular expression matching nodes or references. Its first
564group should match `Node:' or `Ref:'.
565CASE-FOLD t means search for a case-insensitive match.
566Value is the position at which a match was found, or nil if not found."
567 (let ((case-fold-search case-fold)
568 found)
569 (save-excursion
570 (when (Info-node-at-bob-matching regexp)
571 (setq found (point)))
572 (while (and (not found)
573 (search-forward "\n\^_" nil t))
574 (forward-line 1)
575 (let ((beg (point)))
576 (forward-line 1)
577 (when (re-search-backward regexp beg t)
578 (beginning-of-line)
579 (setq found (point)))))
580 found)))
581
582(defun Info-find-node-in-buffer (regexp)
583 "Find a node or anchor in the current buffer.
584REGEXP is a regular expression matching nodes or references. Its first
585group should match `Node:' or `Ref:'.
586Value is the position at which a match was found, or nil if not found.
587This function looks for a case-sensitive match first. If none is found,
588a case-insensitive match is tried."
589 (or (Info-find-node-in-buffer-1 regexp nil)
590 (Info-find-node-in-buffer-1 regexp t)))
591
4fceda3c 592(defun Info-find-node-2 (filename nodename &optional no-going-back)
9e5c2f50 593 (buffer-disable-undo (current-buffer))
a384cab3
JB
594 (or (eq major-mode 'Info-mode)
595 (Info-mode))
596 (widen)
597 (setq Info-current-node nil)
598 (unwind-protect
8bdf4b20
KH
599 (let ((case-fold-search t)
600 anchorpos)
37a3ff5b
RS
601 ;; Switch files if necessary
602 (or (null filename)
603 (equal Info-current-file filename)
604 (let ((buffer-read-only nil))
605 (setq Info-current-file nil
606 Info-current-subfile nil
607 Info-current-file-completions nil
608 buffer-file-name nil)
609 (erase-buffer)
610 (if (eq filename t)
611 (Info-insert-dir)
612 (info-insert-file-contents filename t)
613 (setq default-directory (file-name-directory filename)))
614 (set-buffer-modified-p nil)
615 ;; See whether file has a tag table. Record the location if yes.
616 (goto-char (point-max))
617 (forward-line -8)
618 ;; Use string-equal, not equal, to ignore text props.
619 (if (not (or (string-equal nodename "*")
620 (not
621 (search-forward "\^_\nEnd tag table\n" nil t))))
622 (let (pos)
623 ;; We have a tag table. Find its beginning.
624 ;; Is this an indirect file?
625 (search-backward "\nTag table:\n")
626 (setq pos (point))
627 (if (save-excursion
628 (forward-line 2)
629 (looking-at "(Indirect)\n"))
630 ;; It is indirect. Copy it to another buffer
631 ;; and record that the tag table is in that buffer.
632 (let ((buf (current-buffer))
633 (tagbuf
634 (or Info-tag-table-buffer
635 (generate-new-buffer " *info tag table*"))))
636 (setq Info-tag-table-buffer tagbuf)
637 (save-excursion
638 (set-buffer tagbuf)
9e5c2f50 639 (buffer-disable-undo (current-buffer))
37a3ff5b
RS
640 (setq case-fold-search t)
641 (erase-buffer)
642 (insert-buffer-substring buf))
643 (set-marker Info-tag-table-marker
644 (match-end 0) tagbuf))
645 (set-marker Info-tag-table-marker pos)))
646 (set-marker Info-tag-table-marker nil))
647 (setq Info-current-file
648 (if (eq filename t) "dir" filename))))
649 ;; Use string-equal, not equal, to ignore text props.
650 (if (string-equal nodename "*")
651 (progn (setq Info-current-node nodename)
652 (Info-set-mode-line))
653 ;; Possibilities:
654 ;;
655 ;; 1. Anchor found in tag table
656 ;; 2. Anchor *not* in tag table
657 ;;
658 ;; 3. Node found in tag table
659 ;; 4. Node *not* found in tag table, but found in file
660 ;; 5. Node *not* in tag table, and *not* in file
661 ;;
662 ;; *Or* the same, but in an indirect subfile.
663
664 ;; Search file for a suitable node.
8bdf4b20 665 (let ((guesspos (point-min))
357d11de 666 (regexp (concat "\\(Node:\\|Ref:\\) *\\("
4ce5c223 667 (if (stringp nodename)
dd31e4e8
GM
668 (regexp-quote nodename)
669 "")
357d11de 670 "\\) *[,\t\n\177]"))
8bdf4b20 671 (nodepos nil))
37a3ff5b 672
82172606 673 (catch 'foo
357d11de 674
82172606 675 ;; First, search a tag table, if any
357d11de
GM
676 (when (marker-position Info-tag-table-marker)
677 (let* ((m Info-tag-table-marker)
678 (found (Info-find-in-tag-table m regexp)))
679
680 (when found
681 ;; FOUND is (ANCHOR POS MODE).
682 (setq guesspos (nth 1 found))
683
684 ;; If this is an indirect file, determine which
685 ;; file really holds this node and read it in.
686 (unless (eq (nth 2 found) 'Info-mode)
687 ;; Note that the current buffer must be the
688 ;; *info* buffer on entry to
689 ;; Info-read-subfile. Thus the hackery above.
690 (setq guesspos (Info-read-subfile guesspos)))
82172606
DL
691
692 ;; Handle anchor
357d11de
GM
693 (when (nth 0 found)
694 (goto-char (setq anchorpos guesspos))
695 (throw 'foo t)))))
82172606
DL
696
697 ;; Else we may have a node, which we search for:
698 (goto-char (max (point-min)
699 (- (byte-to-position guesspos) 1000)))
357d11de
GM
700
701 ;; Now search from our advised position (or from beg of
702 ;; buffer) to find the actual node. First, check
703 ;; whether the node is right where we are, in case the
704 ;; buffer begins with a node.
705 (let ((pos (Info-find-node-in-buffer regexp)))
706 (when pos
707 (goto-char pos)
708 (throw 'foo t))
709 (error "No such anchor in tag table or node in tag table or file: %s"
710 nodename)))
82172606
DL
711
712 (Info-select-node)
713 (goto-char (or anchorpos (point-min))))))
a384cab3
JB
714 ;; If we did not finish finding the specified node,
715 ;; go back to the previous one.
4db579ab 716 (or Info-current-node no-going-back (null Info-history)
37a3ff5b
RS
717 (let ((hist (car Info-history)))
718 (setq Info-history (cdr Info-history))
719 (Info-find-node (nth 0 hist) (nth 1 hist) t)
720 (goto-char (nth 2 hist))))))
a384cab3 721
44c327f9
JB
722;; Cache the contents of the (virtual) dir file, once we have merged
723;; it for the first time, so we can save time subsequently.
7ea13762
RS
724(defvar Info-dir-contents nil)
725
44c327f9
JB
726;; Cache for the directory we decided to use for the default-directory
727;; of the merged dir text.
728(defvar Info-dir-contents-directory nil)
729
c142ab2d
RS
730;; Record the file attributes of all the files from which we
731;; constructed Info-dir-contents.
732(defvar Info-dir-file-attributes nil)
733
4fba3b2c
RS
734(defvar Info-dir-file-name nil)
735
7ea13762 736;; Construct the Info directory node by merging the files named `dir'
44c327f9
JB
737;; from various directories. Set the *info* buffer's
738;; default-directory to the first directory we actually get any text
739;; from.
7ea13762 740(defun Info-insert-dir ()
c142ab2d
RS
741 (if (and Info-dir-contents Info-dir-file-attributes
742 ;; Verify that none of the files we used has changed
743 ;; since we used it.
744 (eval (cons 'and
b461e32d
SM
745 (mapcar (lambda (elt)
746 (let ((curr (file-attributes
747 ;; Handle symlinks
748 (file-truename (car elt)))))
749
750 ;; Don't compare the access time.
751 (if curr (setcar (nthcdr 4 curr) 0))
752 (setcar (nthcdr 4 (cdr elt)) 0)
753 (equal (cdr elt) curr)))
c142ab2d 754 Info-dir-file-attributes))))
4fba3b2c
RS
755 (progn
756 (insert Info-dir-contents)
757 (goto-char (point-min)))
dd31e4e8
GM
758 (let ((dirs (if Info-additional-directory-list
759 (append Info-directory-list
760 Info-additional-directory-list)
761 Info-directory-list))
1bcedb3b
RS
762 ;; Bind this in case the user sets it to nil.
763 (case-fold-search t)
4fba3b2c
RS
764 ;; This is set non-nil if we find a problem in some input files.
765 problems
f4008b6e 766 buffers buffer others nodes dirs-done)
44c327f9 767
825d6f08
RS
768 (setq Info-dir-file-attributes nil)
769
44c327f9 770 ;; Search the directory list for the directory file.
7ea13762 771 (while dirs
8d1abb42
RS
772 (let ((truename (file-truename (expand-file-name (car dirs)))))
773 (or (member truename dirs-done)
774 (member (directory-file-name truename) dirs-done)
775 ;; Try several variants of specified name.
776 ;; Try upcasing, appending `.info', or both.
825d6f08
RS
777 (let* (file
778 (attrs
779 (or
780 (progn (setq file (expand-file-name "dir" truename))
781 (file-attributes file))
782 (progn (setq file (expand-file-name "DIR" truename))
783 (file-attributes file))
784 (progn (setq file (expand-file-name "dir.info" truename))
785 (file-attributes file))
786 (progn (setq file (expand-file-name "DIR.INFO" truename))
787 (file-attributes file)))))
8d1abb42
RS
788 (setq dirs-done
789 (cons truename
790 (cons (directory-file-name truename)
791 dirs-done)))
825d6f08
RS
792 (if attrs
793 (save-excursion
794 (or buffers
795 (message "Composing main Info directory..."))
2d41cf59 796 (set-buffer (generate-new-buffer " info dir"))
9d499629
RS
797 (condition-case nil
798 (progn
799 (insert-file-contents file)
4fba3b2c
RS
800 (make-local-variable 'Info-dir-file-name)
801 (setq Info-dir-file-name file)
9d499629
RS
802 (setq buffers (cons (current-buffer) buffers)
803 Info-dir-file-attributes
804 (cons (cons file attrs)
805 Info-dir-file-attributes)))
806 (error (kill-buffer (current-buffer))))))))
3296453b
KH
807 (or (cdr dirs) (setq Info-dir-contents-directory
808 (file-name-as-directory (car dirs))))
825d6f08 809 (setq dirs (cdr dirs))))
399c88ad 810
4db579ab 811 (or buffers
81e14cb2 812 (error "Can't find the Info directory node"))
44c327f9 813 ;; Distinguish the dir file that comes with Emacs from all the
f4008b6e 814 ;; others. Yes, that is really what this is supposed to do.
f601cd3e
DL
815 ;; The definition of `Info-directory-list' puts it first on that
816 ;; list and so last in `buffers' at this point.
817 (setq buffer (car (last buffers))
818 others (delq buffer buffers))
44c327f9 819
debcea0c
KH
820 ;; Insert the entire original dir file as a start; note that we've
821 ;; already saved its default directory to use as the default
822 ;; directory for the whole concatenation.
7ea13762 823 (insert-buffer buffer)
44c327f9 824
7ea13762
RS
825 ;; Look at each of the other buffers one by one.
826 (while others
4fba3b2c 827 (let ((other (car others))
760d5cb3
GM
828 ;; Bind this in case the user sets it to nil.
829 (case-fold-search t)
4fba3b2c 830 this-buffer-nodes)
7ea13762
RS
831 ;; In each, find all the menus.
832 (save-excursion
833 (set-buffer other)
834 (goto-char (point-min))
835 ;; Find each menu, and add an elt to NODES for it.
836 (while (re-search-forward "^\\* Menu:" nil t)
837 (let (beg nodename end)
838 (forward-line 1)
fb163355
MB
839 (while (and (eolp) (not (eobp)))
840 (forward-line 1))
7ea13762 841 (setq beg (point))
ba42ce14
EZ
842 (or (search-backward "\n\^_" nil 'move)
843 (looking-at "\^_")
844 (signal 'search-failed (list "\n\^_")))
7ea13762
RS
845 (search-forward "Node: ")
846 (setq nodename (Info-following-node-name))
3a6ade8a 847 (search-forward "\n\^_" nil 'move)
7ea13762
RS
848 (beginning-of-line)
849 (setq end (point))
4fba3b2c
RS
850 (setq this-buffer-nodes
851 (cons (list nodename other beg end)
852 this-buffer-nodes))))
853 (if (assoc-ignore-case "top" this-buffer-nodes)
854 (setq nodes (nconc this-buffer-nodes nodes))
855 (setq problems t)
856 (message "No `top' node in %s" Info-dir-file-name))))
7ea13762 857 (setq others (cdr others)))
fdf4b680
DL
858 ;; Add to the main menu a menu item for each other node.
859 (let ((case-fold-search t)
760d5cb3 860 (re-search-forward "^\\* Menu:")))
7ea13762
RS
861 (forward-line 1)
862 (let ((menu-items '("top"))
863 (nodes nodes)
864 (case-fold-search t)
3a6ade8a 865 (end (save-excursion (search-forward "\^_" nil t) (point))))
7ea13762
RS
866 (while nodes
867 (let ((nodename (car (car nodes))))
c3a29d70
RS
868 (save-excursion
869 (or (member (downcase nodename) menu-items)
b37daea4 870 (re-search-forward (concat "^\\* +"
c3a29d70
RS
871 (regexp-quote nodename)
872 "::")
873 end t)
874 (progn
875 (insert "* " nodename "::" "\n")
876 (setq menu-items (cons nodename menu-items))))))
7ea13762
RS
877 (setq nodes (cdr nodes))))
878 ;; Now take each node of each of the other buffers
879 ;; and merge it into the main buffer.
880 (while nodes
760d5cb3
GM
881 (let ((case-fold-search t)
882 (nodename (car (car nodes))))
7ea13762
RS
883 (goto-char (point-min))
884 ;; Find the like-named node in the main buffer.
ba42ce14 885 (if (re-search-forward (concat "^\^_.*\n.*Node: "
7ea13762
RS
886 (regexp-quote nodename)
887 "[,\n\t]")
888 nil t)
889 (progn
3a6ade8a 890 (search-forward "\n\^_" nil 'move)
25869acf
RS
891 (beginning-of-line)
892 (insert "\n"))
7ea13762
RS
893 ;; If none exists, add one.
894 (goto-char (point-max))
dfbf6104 895 (insert "\^_\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
7ea13762
RS
896 ;; Merge the text from the other buffer's menu
897 ;; into the menu in the like-named node in the main buffer.
25869acf 898 (apply 'insert-buffer-substring (cdr (car nodes))))
7ea13762
RS
899 (setq nodes (cdr nodes)))
900 ;; Kill all the buffers we just made.
901 (while buffers
902 (kill-buffer (car buffers))
825d6f08 903 (setq buffers (cdr buffers)))
4fba3b2c
RS
904 (goto-char (point-min))
905 (if problems
906 (message "Composing main Info directory...problems encountered, see `*Messages*'")
907 (message "Composing main Info directory...done")))
44c327f9
JB
908 (setq Info-dir-contents (buffer-string)))
909 (setq default-directory Info-dir-contents-directory))
7ea13762 910
c5fe2ff1
RS
911;; Note that on entry to this function the current-buffer must be the
912;; *info* buffer; not the info tags buffer.
a384cab3 913(defun Info-read-subfile (nodepos)
a361deba
RS
914 ;; NODEPOS is either a position (in the Info file as a whole,
915 ;; not relative to a subfile) or the name of a subfile.
a384cab3
JB
916 (let (lastfilepos
917 lastfilename)
a361deba
RS
918 (if (numberp nodepos)
919 (save-excursion
920 (set-buffer (marker-buffer Info-tag-table-marker))
921 (goto-char (point-min))
ba42ce14
EZ
922 (or (looking-at "\^_")
923 (search-forward "\n\^_"))
a361deba
RS
924 (forward-line 2)
925 (catch 'foo
926 (while (not (looking-at "\^_"))
927 (if (not (eolp))
928 (let ((beg (point))
929 thisfilepos thisfilename)
930 (search-forward ": ")
931 (setq thisfilename (buffer-substring beg (- (point) 2)))
932 (setq thisfilepos (read (current-buffer)))
933 ;; read in version 19 stops at the end of number.
934 ;; Advance to the next line.
935 (forward-line 1)
936 (if (> thisfilepos nodepos)
937 (throw 'foo t))
938 (setq lastfilename thisfilename)
939 (setq lastfilepos thisfilepos))
940 (forward-line 1)))))
941 (setq lastfilename nodepos)
942 (setq lastfilepos 0))
c5fe2ff1
RS
943 ;; Assume previous buffer is in Info-mode.
944 ;; (set-buffer (get-buffer "*info*"))
a384cab3
JB
945 (or (equal Info-current-subfile lastfilename)
946 (let ((buffer-read-only nil))
947 (setq buffer-file-name nil)
948 (widen)
949 (erase-buffer)
1143a6b0 950 (info-insert-file-contents lastfilename)
a384cab3
JB
951 (set-buffer-modified-p nil)
952 (setq Info-current-subfile lastfilename)))
b0e52598
RS
953 ;; Widen in case we are in the same subfile as before.
954 (widen)
a384cab3 955 (goto-char (point-min))
ba42ce14
EZ
956 (if (looking-at "\^_")
957 (forward-char 1)
958 (search-forward "\n\^_"))
a361deba
RS
959 (if (numberp nodepos)
960 (+ (- nodepos lastfilepos) (point)))))
a384cab3 961
50ac70af
MB
962(defvar Info-header-line nil
963 "If the info node header is hidden, the text of the header.")
964
a384cab3 965(defun Info-select-node ()
fdf4b680
DL
966"Select the info node that point is in.
967Bind this in case the user sets it to nil."
1bcedb3b
RS
968 (let ((case-fold-search t))
969 (save-excursion
760d5cb3
GM
970 ;; Find beginning of node.
971 (if (search-backward "\n\^_" nil 'move)
972 (forward-line 2)
973 (if (looking-at "\^_")
974 (forward-line 1)
975 (signal 'search-failed (list "\n\^_"))))
976 ;; Get nodename spelled as it is in the node.
977 (re-search-forward "Node:[ \t]*")
978 (setq Info-current-node
979 (buffer-substring-no-properties (point)
980 (progn
981 (skip-chars-forward "^,\t\n")
982 (point))))
983 (Info-set-mode-line)
984 ;; Find the end of it, and narrow.
985 (beginning-of-line)
986 (let (active-expression)
50ac70af 987 ;; Narrow to the node contents
760d5cb3
GM
988 (narrow-to-region (point)
989 (if (re-search-forward "\n[\^_\f]" nil t)
990 (prog1
991 (1- (point))
992 (if (looking-at "[\n\^_\f]*execute: ")
993 (progn
994 (goto-char (match-end 0))
995 (setq active-expression
996 (read (current-buffer))))))
997 (point-max)))
998 (if Info-enable-active-nodes (eval active-expression))
999 (if Info-fontify (Info-fontify-node))
50ac70af
MB
1000 (if Info-use-header-line
1001 (Info-setup-header-line)
1002 (setq Info-header-line nil))
760d5cb3 1003 (run-hooks 'Info-selection-hook)))))
a384cab3
JB
1004
1005(defun Info-set-mode-line ()
1006 (setq mode-line-buffer-identification
313fe58a 1007 (nconc (propertized-buffer-identification "%b")
918e364a
MB
1008 (list
1009 (concat " ("
1010 (file-name-nondirectory
1011 (if (stringp Info-current-file)
1012 Info-current-file
1013 (or buffer-file-name "")))
1014 ") "
1015 (or Info-current-node ""))))))
a384cab3 1016\f
50ac70af
MB
1017;; Skip the node header and make it into a header-line. This function
1018;; should be called when the node is already narrowed.
1019(defun Info-setup-header-line ()
1020 (goto-char (point-min))
6e961dcd
EZ
1021 (let* ((case-fold-search t)
1022 (header-end (save-excursion (forward-line 1) (1- (point))))
1023 ;; If we find neither Next: nor Prev: link, show the entire
1024 ;; node header. Otherwise, don't show the File: and Node:
1025 ;; parts, to avoid wasting precious space on information that
1026 ;; is available in the mode line.
1027 (header-beg (if (re-search-forward
1028 "\\(next\\|prev[ious]*\\): "
1029 header-end t)
1030 (match-beginning 1)
1031 (point))))
1032 (set (make-local-variable 'Info-header-line)
1033 (buffer-substring header-beg header-end))
1034 (setq header-line-format 'Info-header-line)
1035 (narrow-to-region (1+ header-end) (point-max))))
50ac70af 1036\f
a384cab3
JB
1037;; Go to an info node specified with a filename-and-nodename string
1038;; of the sort that is found in pointers in nodes.
1039
4fceda3c 1040(defun Info-goto-node (nodename &optional fork)
cfc697a2 1041 "Go to info node named NODENAME. Give just NODENAME or (FILENAME)NODENAME.
dc414be1
EZ
1042If NODENAME is of the form (FILENAME)NODENAME, the node is in the Info file
1043FILENAME; otherwise, NODENAME should be in the current Info file (or one of
1044its sub-files).
1045Completion is available, but only for node names in the current Info file.
7210b6f5
DL
1046If FORK is non-nil (interactively with a prefix arg), show the node in
1047a new info buffer.
4fceda3c 1048If FORK is a string, it is the name to use for the new buffer."
d1268e52 1049 (interactive (list (Info-read-node-name "Go to node: ") current-prefix-arg))
d0e41cba 1050 (info-initialize)
4fceda3c 1051 (if fork
760d5cb3
GM
1052 (set-buffer
1053 (clone-buffer (concat "*info-" (if (stringp fork) fork nodename) "*") t)))
a384cab3
JB
1054 (let (filename)
1055 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
1056 nodename)
1057 (setq filename (if (= (match-beginning 1) (match-end 1))
1058 ""
1059 (substring nodename (match-beginning 2) (match-end 2)))
1060 nodename (substring nodename (match-beginning 3) (match-end 3)))
1061 (let ((trim (string-match "\\s *\\'" filename)))
1062 (if trim (setq filename (substring filename 0 trim))))
1063 (let ((trim (string-match "\\s *\\'" nodename)))
1064 (if trim (setq nodename (substring nodename 0 trim))))
e579232c 1065 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
1066 (Info-find-node (if (equal filename "") nil filename)
1067 (if (equal nodename "") "Top" nodename))))
552775bd 1068
ddf89211
RS
1069(defvar Info-read-node-completion-table)
1070
03524be6 1071;; This function is used as the "completion table" while reading a node name.
ddf89211 1072;; It does completion using the alist in Info-read-node-completion-table
03524be6
RS
1073;; unless STRING starts with an open-paren.
1074(defun Info-read-node-name-1 (string predicate code)
1075 (let ((no-completion (and (> (length string) 0) (eq (aref string 0) ?\())))
1076 (cond ((eq code nil)
1077 (if no-completion
1078 string
ddf89211 1079 (try-completion string Info-read-node-completion-table predicate)))
03524be6
RS
1080 ((eq code t)
1081 (if no-completion
1082 nil
ddf89211 1083 (all-completions string Info-read-node-completion-table predicate)))
03524be6
RS
1084 ((eq code 'lambda)
1085 (if no-completion
1086 t
ddf89211 1087 (assoc string Info-read-node-completion-table))))))
03524be6 1088
552775bd
RS
1089(defun Info-read-node-name (prompt &optional default)
1090 (let* ((completion-ignore-case t)
ddf89211 1091 (Info-read-node-completion-table (Info-build-node-completions))
b6d61ffa 1092 (nodename (completing-read prompt 'Info-read-node-name-1 nil t)))
552775bd
RS
1093 (if (equal nodename "")
1094 (or default
1095 (Info-read-node-name prompt))
1096 nodename)))
1097
1098(defun Info-build-node-completions ()
1099 (or Info-current-file-completions
1bcedb3b
RS
1100 (let ((compl nil)
1101 ;; Bind this in case the user sets it to nil.
11638562
EZ
1102 (case-fold-search t)
1103 (node-regexp "Node: *\\([^,\n]*\\) *[,\n\t]"))
552775bd
RS
1104 (save-excursion
1105 (save-restriction
1106 (if (marker-buffer Info-tag-table-marker)
c5fe2ff1
RS
1107 (let ((marker Info-tag-table-marker))
1108 (set-buffer (marker-buffer marker))
cedb118c 1109 (widen)
c5fe2ff1 1110 (goto-char marker)
fdf4b680 1111 (while (re-search-forward "\n\\(Node\\|Ref\\): \\(.*\\)\177" nil t)
552775bd 1112 (setq compl
fdf4b680 1113 (cons (list (match-string-no-properties 2))
552775bd
RS
1114 compl))))
1115 (widen)
1116 (goto-char (point-min))
11638562
EZ
1117 ;; If the buffer begins with a node header, process that first.
1118 (if (Info-node-at-bob-matching node-regexp)
110d4e80 1119 (setq compl (list (match-string-no-properties 1))))
11638562 1120 ;; Now for the rest of the nodes.
552775bd
RS
1121 (while (search-forward "\n\^_" nil t)
1122 (forward-line 1)
1123 (let ((beg (point)))
1124 (forward-line 1)
11638562 1125 (if (re-search-backward node-regexp beg t)
399c88ad 1126 (setq compl
110d4e80 1127 (cons (list (match-string-no-properties 1))
552775bd 1128 compl))))))))
87d2b5a2 1129 (setq compl (cons '("*") compl))
07b1b912 1130 (set (make-local-variable 'Info-current-file-completions) compl))))
a384cab3 1131\f
aea2a8da
JB
1132(defun Info-restore-point (hl)
1133 "If this node has been visited, restore the point value when we left."
cedb118c
RS
1134 (while hl
1135 (if (and (equal (nth 0 (car hl)) Info-current-file)
e90d1271
RS
1136 ;; Use string-equal, not equal, to ignore text props.
1137 (string-equal (nth 1 (car hl)) Info-current-node))
cedb118c 1138 (progn
d96504df
KH
1139 (goto-char (nth 2 (car hl)))
1140 (setq hl nil)) ;terminate the while at next iter
cedb118c 1141 (setq hl (cdr hl)))))
aea2a8da 1142\f
2c80a618
GM
1143(defvar Info-search-history nil
1144 "The history list for `Info-search'.")
a384cab3
JB
1145
1146(defun Info-search (regexp)
1147 "Search for REGEXP, starting from point, and select node it's found in."
bbc96600
EZ
1148 (interactive (list (read-string
1149 (if Info-search-history
1150 (format "Regexp search (default `%s'): "
1151 (car Info-search-history))
1152 "Regexp search: ")
1153 nil 'Info-search-history)))
2c80a618
GM
1154 (when transient-mark-mode
1155 (deactivate-mark))
1156 (when (equal regexp "")
1157 (setq regexp (car Info-search-history)))
c47b5bbe
DL
1158 (when regexp
1159 (let ((found ()) current
1160 (onode Info-current-node)
1161 (ofile Info-current-file)
1162 (opoint (point))
1163 (ostart (window-start))
1164 (osubfile Info-current-subfile))
1165 (save-excursion
1166 (save-restriction
1167 (widen)
1168 (if (null Info-current-subfile)
a384cab3 1169 (progn (re-search-forward regexp) (setq found (point)))
c47b5bbe
DL
1170 (condition-case err
1171 (progn (re-search-forward regexp) (setq found (point)))
1172 (search-failed nil)))))
760d5cb3 1173 (if (not found) ;can only happen in subfile case -- else would have erred
c47b5bbe
DL
1174 (unwind-protect
1175 (let ((list ()))
1176 (save-excursion
1177 (set-buffer (marker-buffer Info-tag-table-marker))
c5fe2ff1 1178 (goto-char (point-min))
c47b5bbe
DL
1179 (search-forward "\n\^_\nIndirect:")
1180 (save-restriction
1181 (narrow-to-region (point)
1182 (progn (search-forward "\n\^_")
1183 (1- (point))))
1184 (goto-char (point-min))
1185 (search-forward (concat "\n" osubfile ": "))
1186 (beginning-of-line)
1187 (while (not (eobp))
1188 (re-search-forward "\\(^.*\\): [0-9]+$")
1189 (goto-char (+ (match-end 1) 2))
1190 (setq list (cons (cons (read (current-buffer))
110d4e80 1191 (match-string-no-properties 1))
c47b5bbe
DL
1192 list))
1193 (goto-char (1+ (match-end 0))))
1194 (setq list (nreverse list)
1195 current (car (car list))
1196 list (cdr list))))
1197 (while list
1198 (message "Searching subfile %s..." (cdr (car list)))
1199 (Info-read-subfile (car (car list)))
1200 (setq list (cdr list))
c47b5bbe
DL
1201 (if (re-search-forward regexp nil t)
1202 (setq found (point) list ())))
1203 (if found
1204 (message "")
1205 (signal 'search-failed (list regexp))))
1206 (if (not found)
1207 (progn (Info-read-subfile osubfile)
1208 (goto-char opoint)
1209 (Info-select-node)
1210 (set-window-start (selected-window) ostart)))))
760d5cb3
GM
1211 (widen)
1212 (goto-char found)
1213 (Info-select-node)
1214 ;; Use string-equal, not equal, to ignore text props.
1215 (or (and (string-equal onode Info-current-node)
1216 (equal ofile Info-current-file))
1217 (setq Info-history (cons (list ofile onode opoint)
1218 Info-history))))))
a384cab3 1219\f
a384cab3 1220(defun Info-extract-pointer (name &optional errorname)
fdf4b680
DL
1221 "Extract the value of the node-pointer named NAME.
1222If there is none, use ERRORNAME in the error message;
1223if ERRORNAME is nil, just return nil.
1224Bind this in case the user sets it to nil."
1bcedb3b
RS
1225 (let ((case-fold-search t))
1226 (save-excursion
50ac70af
MB
1227 (save-restriction
1228 (goto-char (point-min))
1229 (when Info-header-line
1230 ;; expose the header line in the buffer
c1699282
SM
1231 (widen)
1232 (forward-line -1))
50ac70af
MB
1233 (let ((bound (point)))
1234 (forward-line 1)
c1699282 1235 (cond ((re-search-backward (concat name ":") bound t)
0cb39153
MB
1236 (goto-char (match-end 0))
1237 (Info-following-node-name))
50ac70af
MB
1238 ((not (eq errorname t))
1239 (error "Node has no %s"
1240 (capitalize (or errorname name))))))))))
a384cab3
JB
1241
1242(defun Info-following-node-name (&optional allowedchars)
fdf4b680
DL
1243 "Return the node name in the buffer following point.
1244ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
1245saying which chars may appear in the node name."
a384cab3 1246 (skip-chars-forward " \t")
70d78eb6 1247 (buffer-substring-no-properties
a384cab3
JB
1248 (point)
1249 (progn
1250 (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
1251 (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
1252 (if (looking-at "(")
1253 (skip-chars-forward "^)")))
1254 (skip-chars-backward " ")
1255 (point))))
1256
1257(defun Info-next ()
1258 "Go to the next node of this node."
1259 (interactive)
1260 (Info-goto-node (Info-extract-pointer "next")))
1261
1262(defun Info-prev ()
1263 "Go to the previous node of this node."
1264 (interactive)
1265 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
1266
5dab2fb4
RS
1267(defun Info-up (&optional same-file)
1268 "Go to the superior node of this node.
1269If SAME-FILE is non-nil, do not move to a different Info file."
a384cab3 1270 (interactive)
5dab2fb4 1271 (let ((node (Info-extract-pointer "up")))
4fceda3c 1272 (and (or same-file (not (stringp Info-current-file)))
5dab2fb4
RS
1273 (string-match "^(" node)
1274 (error "Up node is in another Info file"))
1275 (Info-goto-node node))
aea2a8da 1276 (Info-restore-point Info-history))
a384cab3
JB
1277
1278(defun Info-last ()
1279 "Go back to the last node visited."
1280 (interactive)
1281 (or Info-history
1282 (error "This is the first Info node you looked at"))
1283 (let (filename nodename opoint)
1284 (setq filename (car (car Info-history)))
1285 (setq nodename (car (cdr (car Info-history))))
1286 (setq opoint (car (cdr (cdr (car Info-history)))))
1287 (setq Info-history (cdr Info-history))
1288 (Info-find-node filename nodename)
1289 (setq Info-history (cdr Info-history))
1290 (goto-char opoint)))
1291
48e5b471 1292;;;###autoload
a384cab3
JB
1293(defun Info-directory ()
1294 "Go to the Info directory node."
1295 (interactive)
1296 (Info-find-node "dir" "top"))
1297\f
1298(defun Info-follow-reference (footnotename)
fdf4b680
DL
1299 "Follow cross reference named FOOTNOTENAME to the node it refers to.
1300FOOTNOTENAME may be an abbreviation of the reference name."
a384cab3
JB
1301 (interactive
1302 (let ((completion-ignore-case t)
1bcedb3b 1303 (case-fold-search t)
67bc89ab 1304 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 1305 (save-excursion
67bc89ab
RS
1306 ;; Store end and beginning of line.
1307 (end-of-line)
1308 (setq eol (point))
1309 (beginning-of-line)
1310 (setq bol (point))
1311
a384cab3
JB
1312 (goto-char (point-min))
1313 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
110d4e80 1314 (setq str (buffer-substring-no-properties
a384cab3
JB
1315 (match-beginning 1)
1316 (1- (point))))
1317 ;; See if this one should be the default.
1318 (and (null default)
1f179e27 1319 (<= (match-beginning 0) start-point)
a384cab3
JB
1320 (<= start-point (point))
1321 (setq default t))
67bc89ab
RS
1322 ;; See if this one should be the alternate default.
1323 (and (null alt-default)
1324 (and (<= bol (match-beginning 0))
1325 (<= (point) eol))
1326 (setq alt-default t))
a384cab3
JB
1327 (setq i 0)
1328 (while (setq i (string-match "[ \n\t]+" str i))
1329 (setq str (concat (substring str 0 i) " "
1330 (substring str (match-end 0))))
1331 (setq i (1+ i)))
1332 ;; Record as a completion and perhaps as default.
1333 (if (eq default t) (setq default str))
67bc89ab 1334 (if (eq alt-default t) (setq alt-default str))
ec6d29af
KH
1335 ;; Don't add this string if it's a duplicate.
1336 ;; We use a loop instead of "(assoc str completions)" because
1337 ;; we want to do a case-insensitive compare.
1338 (let ((tail completions)
1339 (tem (downcase str)))
1340 (while (and tail
1341 (not (string-equal tem (downcase (car (car tail))))))
1342 (setq tail (cdr tail)))
1343 (or tail
1344 (setq completions
1345 (cons (cons str nil)
1346 completions))))))
67bc89ab
RS
1347 ;; If no good default was found, try an alternate.
1348 (or default
1349 (setq default alt-default))
1350 ;; If only one cross-reference found, then make it default.
1351 (if (eq (length completions) 1)
1352 (setq default (car (car completions))))
a384cab3 1353 (if completions
b0ebdfe5
RS
1354 (let ((input (completing-read (if default
1355 (concat "Follow reference named: ("
1356 default ") ")
1357 "Follow reference named: ")
1358 completions nil t)))
1359 (list (if (equal input "")
1360 default input)))
a384cab3 1361 (error "No cross-references in this node"))))
ebf8f7e1
RS
1362
1363 (unless footnotename
1364 (error "No reference was specified"))
1365
1bcedb3b
RS
1366 (let (target beg i (str (concat "\\*note " (regexp-quote footnotename)))
1367 (case-fold-search t))
a384cab3
JB
1368 (while (setq i (string-match " " str i))
1369 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
1370 (setq i (+ i 6)))
1371 (save-excursion
1372 (goto-char (point-min))
1373 (or (re-search-forward str nil t)
1374 (error "No cross-reference named %s" footnotename))
1375 (goto-char (+ (match-beginning 0) 5))
1376 (setq target
1377 (Info-extract-menu-node-name "Bad format cross reference" t)))
1378 (while (setq i (string-match "[ \t\n]+" target i))
1379 (setq target (concat (substring target 0 i) " "
1380 (substring target (match-end 0))))
1381 (setq i (+ i 1)))
1382 (Info-goto-node target)))
1383
1384(defun Info-extract-menu-node-name (&optional errmessage multi-line)
1385 (skip-chars-forward " \t\n")
1386 (let ((beg (point))
1387 str i)
1388 (skip-chars-forward "^:")
1389 (forward-char 1)
1390 (setq str
1391 (if (looking-at ":")
70d78eb6 1392 (buffer-substring-no-properties beg (1- (point)))
a384cab3
JB
1393 (skip-chars-forward " \t\n")
1394 (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
1395 (while (setq i (string-match "\n" str i))
1396 (aset str i ?\ ))
2e52ff59
RS
1397 ;; Collapse multiple spaces.
1398 (while (string-match " +" str)
1399 (setq str (replace-match " " t t str)))
a384cab3
JB
1400 str))
1401
e8adde3f 1402;; No one calls this.
9e5c2f50
RS
1403;;(defun Info-menu-item-sequence (list)
1404;; (while list
e8adde3f 1405;; (Info-menu (car list))
9e5c2f50 1406;; (setq list (cdr list))))
a384cab3 1407
6356e646
RS
1408(defvar Info-complete-menu-buffer)
1409
e8adde3f 1410(defun Info-complete-menu-item (string predicate action)
760d5cb3
GM
1411 (let ((completion-ignore-case t)
1412 (case-fold-search t))
e8adde3f
RS
1413 (cond ((eq action nil)
1414 (let (completions
b37daea4 1415 (pattern (concat "\n\\* +\\("
e8adde3f
RS
1416 (regexp-quote string)
1417 "[^:\t\n]*\\):")))
1418 (save-excursion
1419 (set-buffer Info-complete-menu-buffer)
1420 (goto-char (point-min))
763da090 1421 (search-forward "\n* Menu:")
e8adde3f 1422 (while (re-search-forward pattern nil t)
110d4e80
DL
1423 (setq completions
1424 (cons (cons (match-string-no-properties 1)
1425 (match-beginning 1))
1426 completions))))
e8adde3f
RS
1427 (try-completion string completions predicate)))
1428 ((eq action t)
1429 (let (completions
b37daea4 1430 (pattern (concat "\n\\* +\\("
e8adde3f
RS
1431 (regexp-quote string)
1432 "[^:\t\n]*\\):")))
1433 (save-excursion
1434 (set-buffer Info-complete-menu-buffer)
1435 (goto-char (point-min))
763da090 1436 (search-forward "\n* Menu:")
e8adde3f 1437 (while (re-search-forward pattern nil t)
110d4e80
DL
1438 (setq completions (cons (cons
1439 (match-string-no-properties 1)
1440 (match-beginning 1))
e8adde3f
RS
1441 completions))))
1442 (all-completions string completions predicate)))
1443 (t
1444 (save-excursion
1445 (set-buffer Info-complete-menu-buffer)
1446 (goto-char (point-min))
763da090 1447 (search-forward "\n* Menu:")
b37daea4 1448 (re-search-forward (concat "\n\\* +"
e8adde3f
RS
1449 (regexp-quote string)
1450 ":")
1451 nil t))))))
1452
1453
4fceda3c 1454(defun Info-menu (menu-item &optional fork)
dc414be1
EZ
1455 "Go to the node pointed to by the menu item named (or abbreviated) MENU-ITEM.
1456The menu item should one of those listed in the current node's menu.
1457Completion is allowed, and the default menu item is the one point is on.
7210b6f5
DL
1458If FORK is non-nil (interactively with a prefix arg), show the node in
1459a new info buffer. If FORK is a string, it is the name to use for the
1460new buffer."
a384cab3
JB
1461 (interactive
1462 (let ((completions '())
1463 ;; If point is within a menu item, use that item as the default
1464 (default nil)
1465 (p (point))
211d6309 1466 beg
760d5cb3
GM
1467 (last nil)
1468 (case-fold-search t))
a384cab3
JB
1469 (save-excursion
1470 (goto-char (point-min))
1471 (if (not (search-forward "\n* menu:" nil t))
1472 (error "No menu in this node"))
e8adde3f
RS
1473 (setq beg (point))
1474 (and (< (point) p)
1475 (save-excursion
1476 (goto-char p)
1477 (end-of-line)
4fc14e5a 1478 (if (re-search-backward "\n\\* +\\([^:\t\n]*\\):" beg t)
110d4e80 1479 (setq default (match-string-no-properties 1))))))
a384cab3
JB
1480 (let ((item nil))
1481 (while (null item)
e8adde3f
RS
1482 (setq item (let ((completion-ignore-case t)
1483 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
1484 (completing-read (if default
1485 (format "Menu item (default %s): "
1486 default)
760d5cb3 1487 "Menu item: ")
e8adde3f 1488 'Info-complete-menu-item nil t)))
aea2a8da
JB
1489 ;; we rely on the fact that completing-read accepts an input
1490 ;; of "" even when the require-match argument is true and ""
1491 ;; is not a valid possibility
a384cab3
JB
1492 (if (string= item "")
1493 (if default
1494 (setq item default)
760d5cb3
GM
1495 ;; ask again
1496 (setq item nil))))
4fceda3c 1497 (list item current-prefix-arg))))
a384cab3
JB
1498 ;; there is a problem here in that if several menu items have the same
1499 ;; name you can only go to the node of the first with this command.
4fceda3c 1500 (Info-goto-node (Info-extract-menu-item menu-item) (if fork menu-item)))
399c88ad 1501
a384cab3
JB
1502(defun Info-extract-menu-item (menu-item)
1503 (setq menu-item (regexp-quote menu-item))
1bcedb3b
RS
1504 (let ((case-fold-search t))
1505 (save-excursion
760d5cb3
GM
1506 (let ((case-fold-search t))
1507 (goto-char (point-min))
1508 (or (search-forward "\n* menu:" nil t)
1509 (error "No menu in this node"))
1510 (or (re-search-forward (concat "\n\\* +" menu-item ":") nil t)
1511 (re-search-forward (concat "\n\\* +" menu-item) nil t)
1512 (error "No such item in menu"))
1513 (beginning-of-line)
1514 (forward-char 2)
1515 (Info-extract-menu-node-name)))))
a384cab3
JB
1516
1517;; If COUNT is nil, use the last item in the menu.
1518(defun Info-extract-menu-counting (count)
1bcedb3b
RS
1519 (let ((case-fold-search t))
1520 (save-excursion
760d5cb3
GM
1521 (let ((case-fold-search t))
1522 (goto-char (point-min))
1523 (or (search-forward "\n* menu:" nil t)
1524 (error "No menu in this node"))
1525 (if count
1526 (or (search-forward "\n* " nil t count)
1527 (error "Too few items in menu"))
1528 (while (search-forward "\n* " nil t)
1529 nil))
1530 (Info-extract-menu-node-name)))))
a384cab3 1531
e38e7367
RM
1532(defun Info-nth-menu-item ()
1533 "Go to the node of the Nth menu item.
1534N is the digit argument used to invoke this command."
a384cab3 1535 (interactive)
e38e7367
RM
1536 (Info-goto-node
1537 (Info-extract-menu-counting
1538 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
1539
1540(defun Info-top-node ()
1541 "Go to the Top node of this file."
1542 (interactive)
1543 (Info-goto-node "Top"))
1544
1545(defun Info-final-node ()
1546 "Go to the final node in this file."
1547 (interactive)
1548 (Info-goto-node "Top")
760d5cb3
GM
1549 (let ((Info-history nil)
1550 (case-fold-search t))
a384cab3
JB
1551 ;; Go to the last node in the menu of Top.
1552 (Info-goto-node (Info-extract-menu-counting nil))
1553 ;; If the last node in the menu is not last in pointer structure,
399c88ad 1554 ;; move forward until we can't go any farther.
a384cab3
JB
1555 (while (Info-forward-node t t) nil)
1556 ;; Then keep moving down to last subnode, unless we reach an index.
1557 (while (and (not (string-match "\\<index\\>" Info-current-node))
1558 (save-excursion (search-forward "\n* Menu:" nil t)))
1559 (Info-goto-node (Info-extract-menu-counting nil)))))
1560
1561(defun Info-forward-node (&optional not-down no-error)
1562 "Go forward one node, considering all nodes as forming one sequence."
1563 (interactive)
1564 (goto-char (point-min))
1565 (forward-line 1)
760d5cb3
GM
1566 (let ((case-fold-search t))
1567 ;; three possibilities, in order of priority:
1568 ;; 1. next node is in a menu in this node (but not in an index)
1569 ;; 2. next node is next at same level
1570 ;; 3. next node is up and next
1571 (cond ((and (not not-down)
1572 (save-excursion (search-forward "\n* menu:" nil t))
1573 (not (string-match "\\<index\\>" Info-current-node)))
1574 (Info-goto-node (Info-extract-menu-counting 1))
1575 t)
b24e84ab
EZ
1576 ((save-excursion
1577 (save-restriction
1578 (let (limit)
1579 (when Info-header-line
1580 (goto-char (point-min))
1581 (widen)
1582 (forward-line -1)
1583 (setq limit (point))
1584 (forward-line 1))
1585 (search-backward "next:" limit t))))
760d5cb3
GM
1586 (Info-next)
1587 t)
b24e84ab
EZ
1588 ((and (save-excursion
1589 (save-restriction
1590 (let (limit)
1591 (when Info-header-line
1592 (goto-char (point-min))
1593 (widen)
1594 (forward-line -1)
1595 (setq limit (point))
1596 (forward-line 1))
1597 (search-backward "up:" limit t))))
760d5cb3
GM
1598 ;; Use string-equal, not equal, to ignore text props.
1599 (not (string-equal (downcase (Info-extract-pointer "up"))
1600 "top")))
1601 (let ((old-node Info-current-node))
1602 (Info-up)
1603 (let (Info-history success)
1604 (unwind-protect
1605 (setq success (Info-forward-node t no-error))
1606 (or success (Info-goto-node old-node))))))
1607 (no-error nil)
1608 (t (error "No pointer forward from this node")))))
a384cab3
JB
1609
1610(defun Info-backward-node ()
1611 "Go backward one node, considering all nodes as forming one sequence."
1612 (interactive)
1613 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
760d5cb3
GM
1614 (upnode (Info-extract-pointer "up" t))
1615 (case-fold-search t))
a384cab3
JB
1616 (cond ((and upnode (string-match "(" upnode))
1617 (error "First node in file"))
1618 ((and upnode (or (null prevnode)
e90d1271
RS
1619 ;; Use string-equal, not equal,
1620 ;; to ignore text properties.
1621 (string-equal (downcase prevnode)
1622 (downcase upnode))))
a384cab3
JB
1623 (Info-up))
1624 (prevnode
1625 ;; If we move back at the same level,
1626 ;; go down to find the last subnode*.
1627 (Info-prev)
1628 (let (Info-history)
1629 (while (and (not (string-match "\\<index\\>" Info-current-node))
1630 (save-excursion (search-forward "\n* Menu:" nil t)))
1631 (Info-goto-node (Info-extract-menu-counting nil)))))
1632 (t
1633 (error "No pointer backward from this node")))))
1634
1635(defun Info-exit ()
1636 "Exit Info by selecting some other buffer."
1637 (interactive)
552775bd
RS
1638 (if Info-standalone
1639 (save-buffers-kill-emacs)
4643e92f 1640 (quit-window)))
a384cab3 1641
253db917
ER
1642(defun Info-next-menu-item ()
1643 (interactive)
fdf4b680 1644 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
1645 (let* ((case-fold-search t)
1646 (node
1647 (save-excursion
1648 (forward-line -1)
1649 (search-forward "\n* menu:" nil t)
1650 (and (search-forward "\n* " nil t)
1651 (Info-extract-menu-node-name)))))
fadfb77f
RS
1652 (if node (Info-goto-node node)
1653 (error "No more items in menu"))))
253db917
ER
1654
1655(defun Info-last-menu-item ()
1656 (interactive)
1657 (save-excursion
1658 (forward-line 1)
fdf4b680 1659 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
1660 (let* ((case-fold-search t)
1661 (beg (save-excursion
1662 (and (search-backward "\n* menu:" nil t)
1663 (point)))))
0a56332b
RS
1664 (or (and beg (search-backward "\n* " beg t))
1665 (error "No previous items in menu")))
1666 (Info-goto-node (save-excursion
1667 (goto-char (match-end 0))
1668 (Info-extract-menu-node-name)))))
253db917 1669
552775bd 1670(defmacro Info-no-error (&rest body)
253db917
ER
1671 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
1672
1673(defun Info-next-preorder ()
3c9179ea
RS
1674 "Go to the next subnode or the next node, or go up a level."
1675 (interactive)
1676 (cond ((Info-no-error (Info-next-menu-item)))
1677 ((Info-no-error (Info-next)))
5dab2fb4 1678 ((Info-no-error (Info-up t))
ed690657
KH
1679 ;; Since we have already gone thru all the items in this menu,
1680 ;; go up to the end of this node.
b54d0f0e
RS
1681 (goto-char (point-max))
1682 ;; Since logically we are done with the node with that menu,
1683 ;; move on from it.
1684 (Info-next-preorder))
3c9179ea
RS
1685 (t
1686 (error "No more nodes"))))
253db917
ER
1687
1688(defun Info-last-preorder ()
1689 "Go to the last node, popping up a level if there is none."
1690 (interactive)
0a56332b
RS
1691 (cond ((Info-no-error
1692 (Info-last-menu-item)
1693 ;; If we go down a menu item, go to the end of the node
1694 ;; so we can scroll back through it.
ed690657 1695 (goto-char (point-max)))
b54d0f0e
RS
1696 ;; Keep going down, as long as there are nested menu nodes.
1697 (while (Info-no-error
1698 (Info-last-menu-item)
1699 ;; If we go down a menu item, go to the end of the node
1700 ;; so we can scroll back through it.
1701 (goto-char (point-max))))
ed690657 1702 (recenter -1))
c2def7a0
MB
1703 ((and (Info-no-error (Info-extract-pointer "prev"))
1704 (not (equal (Info-extract-pointer "up")
5dab2fb4
RS
1705 (Info-extract-pointer "prev"))))
1706 (Info-no-error (Info-prev))
ed690657 1707 (goto-char (point-max))
b54d0f0e
RS
1708 (while (Info-no-error
1709 (Info-last-menu-item)
1710 ;; If we go down a menu item, go to the end of the node
1711 ;; so we can scroll back through it.
1712 (goto-char (point-max))))
ed690657 1713 (recenter -1))
5dab2fb4 1714 ((Info-no-error (Info-up t))
ed690657 1715 (goto-char (point-min))
760d5cb3
GM
1716 (let ((case-fold-search t))
1717 (or (search-forward "\n* Menu:" nil t)
1718 (goto-char (point-max)))))
ed690657 1719 (t (error "No previous nodes"))))
253db917
ER
1720
1721(defun Info-scroll-up ()
3f32dc86 1722 "Scroll one screenful forward in Info, considering all nodes as one sequence.
280d11ed 1723Once you scroll far enough in a node that its menu appears on the screen
7a53d8c8
EZ
1724but after point, the next scroll moves into its first subnode, unless
1725`Info-scroll-prefer-subnodes' is nil.
280d11ed 1726
7a53d8c8
EZ
1727When you scroll past the end of a node, that goes to the next node if
1728`Info-scroll-prefer-subnodes' is non-nil and to the first subnode otherwise;
1729if this node has no successor, it moves to the parent node's successor,
1730and so on. If `Info-scroll-prefer-subnodes' is non-nil and point is inside
1731the menu of a node, it moves to subnode indicated by the following menu
1732item. (That case won't normally result from this command, but can happen
1733in other ways.)"
280d11ed 1734
253db917 1735 (interactive)
0a56332b
RS
1736 (if (or (< (window-start) (point-min))
1737 (> (window-start) (point-max)))
1738 (set-window-start (selected-window) (point)))
760d5cb3
GM
1739 (let* ((case-fold-search t)
1740 (virtual-end (save-excursion
1741 (goto-char (point-min))
7a53d8c8
EZ
1742 (if (and Info-scroll-prefer-subnodes
1743 (search-forward "\n* Menu:" nil t))
760d5cb3
GM
1744 (point)
1745 (point-max)))))
0a56332b
RS
1746 (if (or (< virtual-end (window-start))
1747 (pos-visible-in-window-p virtual-end))
7a53d8c8
EZ
1748 (cond
1749 (Info-scroll-prefer-subnodes (Info-next-preorder))
1750 ((Info-no-error (Info-goto-node (Info-extract-menu-counting 1))))
1751 (t (Info-next-preorder)))
0a56332b 1752 (scroll-up))))
253db917
ER
1753
1754(defun Info-scroll-down ()
3f32dc86 1755 "Scroll one screenful back in Info, considering all nodes as one sequence.
c2def7a0
MB
1756If point is within the menu of a node, and `Info-scroll-prefer-subnodes'
1757is non-nil, this goes to its last subnode. When you scroll past the
1758beginning of a node, that goes to the previous node or back up to the
1759parent node."
253db917 1760 (interactive)
0a56332b
RS
1761 (if (or (< (window-start) (point-min))
1762 (> (window-start) (point-max)))
1763 (set-window-start (selected-window) (point)))
760d5cb3
GM
1764 (let* ((case-fold-search t)
1765 (current-point (point))
c2def7a0
MB
1766 (virtual-end
1767 (and Info-scroll-prefer-subnodes
1768 (save-excursion
1769 (beginning-of-line)
1770 (setq current-point (point))
1771 (goto-char (point-min))
1772 (search-forward "\n* Menu:"
1773 current-point
1774 t)))))
5b3ebb44 1775 (if (or virtual-end
7afe24e3 1776 (pos-visible-in-window-p (point-min) nil t))
0a56332b
RS
1777 (Info-last-preorder)
1778 (scroll-down))))
253db917 1779
56cda6f5 1780(defun Info-next-reference (&optional recur)
552775bd
RS
1781 "Move cursor to the next cross-reference or menu item in the node."
1782 (interactive)
1783 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1bcedb3b
RS
1784 (old-pt (point))
1785 (case-fold-search t))
552775bd
RS
1786 (or (eobp) (forward-char 1))
1787 (or (re-search-forward pat nil t)
1788 (progn
1789 (goto-char (point-min))
1790 (or (re-search-forward pat nil t)
1791 (progn
1792 (goto-char old-pt)
1793 (error "No cross references in this node")))))
1794 (goto-char (match-beginning 0))
1795 (if (looking-at "\\* Menu:")
56cda6f5
RS
1796 (if recur
1797 (error "No cross references in this node")
1798 (Info-next-reference t)))))
552775bd 1799
56cda6f5 1800(defun Info-prev-reference (&optional recur)
552775bd
RS
1801 "Move cursor to the previous cross-reference or menu item in the node."
1802 (interactive)
1803 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1bcedb3b
RS
1804 (old-pt (point))
1805 (case-fold-search t))
552775bd
RS
1806 (or (re-search-backward pat nil t)
1807 (progn
1808 (goto-char (point-max))
1809 (or (re-search-backward pat nil t)
1810 (progn
1811 (goto-char old-pt)
1812 (error "No cross references in this node")))))
1813 (goto-char (match-beginning 0))
1814 (if (looking-at "\\* Menu:")
56cda6f5
RS
1815 (if recur
1816 (error "No cross references in this node")
1817 (Info-prev-reference t)))))
552775bd 1818
1143a6b0 1819(defun Info-index (topic)
fdf4b680 1820 "Look up a string TOPIC in the index for this file.
1143a6b0
ER
1821The index is defined as the first node in the top-level menu whose
1822name contains the word \"Index\", plus any immediately following
1823nodes whose names also contain the word \"Index\".
1824If there are no exact matches to the specified topic, this chooses
1825the first match which is a case-insensitive substring of a topic.
1826Use the `,' command to see the other matches.
1827Give a blank topic name to go to the Index node itself."
1828 (interactive "sIndex topic: ")
1829 (let ((orignode Info-current-node)
1830 (rnode nil)
b37daea4 1831 (pattern (format "\n\\* +\\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1143a6b0 1832 (regexp-quote topic)))
1bcedb3b
RS
1833 node
1834 (case-fold-search t))
1143a6b0
ER
1835 (Info-goto-node "Top")
1836 (or (search-forward "\n* menu:" nil t)
1837 (error "No index"))
1838 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1839 (error "No index"))
1840 (goto-char (match-beginning 1))
c696ac76
RS
1841 ;; Here, and subsequently in this function,
1842 ;; we bind Info-history to nil for internal node-switches
1843 ;; so that we don't put junk in the history.
1844 ;; In the first Info-goto-node call, above, we do update the history
1845 ;; because that is what the user's previous node choice into it.
1846 (let ((Info-history nil))
1143a6b0
ER
1847 (Info-goto-node (Info-extract-menu-node-name)))
1848 (or (equal topic "")
1849 (let ((matches nil)
1850 (exact nil)
c696ac76 1851 (Info-history nil)
1143a6b0
ER
1852 found)
1853 (while
1854 (progn
1855 (goto-char (point-min))
1856 (while (re-search-forward pattern nil t)
983dfbf8
SM
1857 (push (list (match-string-no-properties 1)
1858 (match-string-no-properties 2)
1859 Info-current-node
1860 (string-to-int (concat "0"
1861 (match-string 3))))
1862 matches))
1143a6b0
ER
1863 (and (setq node (Info-extract-pointer "next" t))
1864 (string-match "\\<Index\\>" node)))
1865 (Info-goto-node node))
1866 (or matches
1867 (progn
81e14cb2 1868 (Info-goto-node orignode)
920bdaab 1869 (error "No `%s' in index" topic)))
1143a6b0
ER
1870 ;; Here it is a feature that assoc is case-sensitive.
1871 (while (setq found (assoc topic matches))
1872 (setq exact (cons found exact)
1873 matches (delq found matches)))
1874 (setq Info-index-alternatives (nconc exact (nreverse matches)))
1875 (Info-index-next 0)))))
1876
1877(defun Info-index-next (num)
1878 "Go to the next matching index item from the last `i' command."
1879 (interactive "p")
1880 (or Info-index-alternatives
882e61bf 1881 (error "No previous `i' command"))
1143a6b0
ER
1882 (while (< num 0)
1883 (setq num (+ num (length Info-index-alternatives))))
1884 (while (> num 0)
1885 (setq Info-index-alternatives
1886 (nconc (cdr Info-index-alternatives)
1887 (list (car Info-index-alternatives)))
1888 num (1- num)))
1889 (Info-goto-node (nth 1 (car Info-index-alternatives)))
1890 (if (> (nth 3 (car Info-index-alternatives)) 0)
1891 (forward-line (nth 3 (car Info-index-alternatives)))
760d5cb3 1892 (forward-line 3) ; don't search in headers
1143a6b0 1893 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
1894 (Info-find-index-name name)))
1895 (message "Found `%s' in %s. %s"
1143a6b0
ER
1896 (car (car Info-index-alternatives))
1897 (nth 2 (car Info-index-alternatives))
1898 (if (cdr Info-index-alternatives)
1899 "(Press `,' for more)"
1900 "(Only match)")))
1901
920bdaab
RS
1902(defun Info-find-index-name (name)
1903 "Move point to the place within the current node where NAME is defined."
1bcedb3b
RS
1904 (let ((case-fold-search t))
1905 (if (or (re-search-forward (format
1906 "[a-zA-Z]+: %s\\( \\|$\\)"
1907 (regexp-quote name)) nil t)
1908 (search-forward (format "`%s'" name) nil t)
1909 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1910 (search-forward
1911 (format "`%s'" (substring name 0 (match-beginning 1)))
1912 nil t))
1913 (search-forward name nil t))
1914 (beginning-of-line)
1915 (goto-char (point-min)))))
920bdaab 1916
a384cab3
JB
1917(defun Info-undefined ()
1918 "Make command be undefined in Info."
1919 (interactive)
1920 (ding))
1921
1922(defun Info-help ()
1923 "Enter the Info tutorial."
1924 (interactive)
1925 (delete-other-windows)
1926 (Info-find-node "info"
1927 (if (< (window-height) 23)
1928 "Help-Small-Screen"
1929 "Help")))
1930
1931(defun Info-summary ()
1932 "Display a brief summary of all Info commands."
1933 (interactive)
1934 (save-window-excursion
1935 (switch-to-buffer "*Help*")
881c84c7 1936 (setq buffer-read-only nil)
a384cab3
JB
1937 (erase-buffer)
1938 (insert (documentation 'Info-mode))
9d29f94c 1939 (help-mode)
a384cab3
JB
1940 (goto-char (point-min))
1941 (let (ch flag)
1942 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1943 (message (if flag "Type Space to see more"
1944 "Type Space to return to Info"))
1614c867 1945 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 1946 (progn (setq unread-command-events (list ch)) nil)
a384cab3 1947 flag))
552775bd
RS
1948 (scroll-up)))
1949 (bury-buffer "*Help*")))
a384cab3
JB
1950\f
1951(defun Info-get-token (pos start all &optional errorstring)
fdf4b680 1952 "Return the token around POS.
a384cab3
JB
1953POS must be somewhere inside the token
1954START is a regular expression which will match the
1955 beginning of the tokens delimited string
1956ALL is a regular expression with a single
90a715f0 1957 parenthesized subpattern which is the token to be
fdf4b680 1958 returned. E.g. '{\(.*\)}' would return any string
a384cab3 1959 enclosed in braces around POS.
fdf4b680 1960ERRORSTRING optional fourth argument, controls action on no match
a384cab3
JB
1961 nil: return nil
1962 t: beep
1963 a string: signal an error, using that string."
1bcedb3b
RS
1964 (let ((case-fold-search t))
1965 (save-excursion
1966 (goto-char pos)
1967 ;; First look for a match for START that goes across POS.
1968 (while (and (not (bobp)) (> (point) (- pos (length start)))
1969 (not (looking-at start)))
1970 (forward-char -1))
1971 ;; If we did not find one, search back for START
1972 ;; (this finds only matches that end at or before POS).
1973 (or (looking-at start)
1974 (progn
1975 (goto-char pos)
1976 (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
1977 (let (found)
1978 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1979 (not (setq found (and (<= (match-beginning 0) pos)
1980 (> (match-end 0) pos))))))
1981 (if (and found (<= (match-beginning 0) pos)
1982 (> (match-end 0) pos))
110d4e80 1983 (match-string-no-properties 1)
1bcedb3b
RS
1984 (cond ((null errorstring)
1985 nil)
1986 ((eq errorstring t)
1987 (beep)
1988 nil)
1989 (t
1990 (error "No %s around position %d" errorstring pos))))))))
a384cab3 1991
981947af 1992(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
1993 "\\<Info-mode-map>Follow a node reference near point.
1994Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 1995At end of the node's text, moves to the next node, or up if none."
f9969361 1996 (interactive "e")
d1268e52 1997 (mouse-set-point click)
981947af
KH
1998 (and (not (Info-try-follow-nearest-node))
1999 (save-excursion (forward-line 1) (eobp))
ed690657 2000 (Info-next-preorder)))
981947af
KH
2001
2002(defun Info-follow-nearest-node ()
2003 "\\<Info-mode-map>Follow a node reference near point.
2004Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
2005If no reference to follow, moves to the next node, or up if none."
2006 (interactive)
2007 (or (Info-try-follow-nearest-node)
ed690657 2008 (Info-next-preorder)))
981947af
KH
2009
2010;; Common subroutine.
2011(defun Info-try-follow-nearest-node ()
2012 "Follow a node reference near point. Return non-nil if successful."
a384cab3
JB
2013 (let (node)
2014 (cond
981947af
KH
2015 ((setq node (Info-get-token (point) "\\*note[ \n]"
2016 "\\*note[ \n]\\([^:]*\\):"))
a384cab3 2017 (Info-follow-reference node))
b37daea4 2018 ((setq node (Info-get-token (point) "\\* +" "\\* +\\([^:]*\\)::"))
a384cab3 2019 (Info-goto-node node))
631ba13e
RS
2020 ((Info-get-token (point) "\\* +" "\\* +\\([^:]*\\):")
2021 (beginning-of-line)
2022 (forward-char 2)
2023 (setq node (Info-extract-menu-node-name))
2024 (Info-goto-node node))
bc2ada62 2025 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
a384cab3 2026 (Info-goto-node node))
bc2ada62 2027 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
a384cab3 2028 (Info-goto-node node))
bc2ada62 2029 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
a384cab3 2030 (Info-goto-node "Top"))
bc2ada62 2031 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
981947af
KH
2032 (Info-goto-node node)))
2033 node))
a384cab3
JB
2034\f
2035(defvar Info-mode-map nil
2036 "Keymap containing Info commands.")
2037(if Info-mode-map
2038 nil
2039 (setq Info-mode-map (make-keymap))
2040 (suppress-keymap Info-mode-map)
2041 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 2042 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 2043 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd
RS
2044 (define-key Info-mode-map "\t" 'Info-next-reference)
2045 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
e38e7367
RM
2046 (define-key Info-mode-map "1" 'Info-nth-menu-item)
2047 (define-key Info-mode-map "2" 'Info-nth-menu-item)
2048 (define-key Info-mode-map "3" 'Info-nth-menu-item)
2049 (define-key Info-mode-map "4" 'Info-nth-menu-item)
2050 (define-key Info-mode-map "5" 'Info-nth-menu-item)
2051 (define-key Info-mode-map "6" 'Info-nth-menu-item)
2052 (define-key Info-mode-map "7" 'Info-nth-menu-item)
2053 (define-key Info-mode-map "8" 'Info-nth-menu-item)
2054 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 2055 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
2056 (define-key Info-mode-map "?" 'Info-summary)
2057 (define-key Info-mode-map "]" 'Info-forward-node)
2058 (define-key Info-mode-map "[" 'Info-backward-node)
2059 (define-key Info-mode-map "<" 'Info-top-node)
2060 (define-key Info-mode-map ">" 'Info-final-node)
2061 (define-key Info-mode-map "b" 'beginning-of-buffer)
2062 (define-key Info-mode-map "d" 'Info-directory)
2063 (define-key Info-mode-map "e" 'Info-edit)
2064 (define-key Info-mode-map "f" 'Info-follow-reference)
2065 (define-key Info-mode-map "g" 'Info-goto-node)
2066 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 2067 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
2068 (define-key Info-mode-map "l" 'Info-last)
2069 (define-key Info-mode-map "m" 'Info-menu)
2070 (define-key Info-mode-map "n" 'Info-next)
2071 (define-key Info-mode-map "p" 'Info-prev)
2072 (define-key Info-mode-map "q" 'Info-exit)
2073 (define-key Info-mode-map "s" 'Info-search)
47fc33ca
RS
2074 ;; For consistency with Rmail.
2075 (define-key Info-mode-map "\M-s" 'Info-search)
4fceda3c 2076 (define-key Info-mode-map "\M-n" 'clone-buffer)
db0c9809 2077 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 2078 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 2079 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 2080 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 2081 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
aea2a8da 2082 )
75a209d4
RS
2083
2084(defun Info-check-pointer (item)
fdf4b680 2085 "Non-nil if ITEM is present in this node."
75a209d4
RS
2086 (condition-case nil
2087 (Info-extract-pointer item)
2088 (error nil)))
2089
d4af987a
DL
2090(easy-menu-define
2091 Info-mode-menu Info-mode-map
2092 "Menu for info files."
2093 '("Info"
2094 ["Up" Info-up :active (Info-check-pointer "up")
2095 :help "Go up in the Info tree"]
2096 ["Next" Info-next :active (Info-check-pointer "next")
2097 :help "Go to the next node"]
2098 ["Previous" Info-prev :active (Info-check-pointer "prev[ious]*")
2099 :help "Go to the previous node"]
2100 ["Backward" Info-backward-node
2101 :help "Go backward one node, considering all as a sequence"]
2102 ["Forward" Info-forward-node
2103 :help "Go forward one node, considering all as a sequence"]
7210b6f5
DL
2104 ["Beginning" beginning-of-buffer
2105 :help "Go to beginning of this node"]
d4af987a
DL
2106 ["Top" Info-top-node
2107 :help "Go to top node of file"]
2108 ["Final Node" Info-final-node
2109 :help "Go to final node in this file"]
2110 ("Menu Item" ["You should never see this" report-emacs-bug t])
2111 ("Reference" ["You should never see this" report-emacs-bug t])
2112 ["Search..." Info-search
2113 :help "Search for regular expression in this Info file"]
d1268e52 2114 ["Go to Node..." Info-goto-node
d4af987a 2115 :help "Go to a named node"]
ec03f31e 2116 ["Last" Info-last :active Info-history
d4af987a
DL
2117 :help "Go to the last node you were at"]
2118 ("Index..."
2119 ["Lookup a String" Info-index
2120 :help "Look for a string in the index items"]
2121 ["Next Matching Item" Info-index-next
2122 :help "Look for another occurrence of previous item"])
7210b6f5
DL
2123 ["Edit" Info-edit :help "Edit contents of this node"
2124 :active Info-enable-edit]
126f5d4d
MB
2125 ["Copy Node Name" Info-copy-current-node-name
2126 :help "Copy the name of the current node into the kill ring"]
ec03f31e 2127 ["Exit" Info-exit :help "Stop reading Info"]))
75a209d4 2128
7210b6f5
DL
2129
2130(defvar info-tool-bar-map
2131 (if (display-graphic-p)
2132 (let ((tool-bar-map (make-sparse-keymap)))
2133 (tool-bar-add-item-from-menu 'Info-exit "close" Info-mode-map)
2134 (tool-bar-add-item-from-menu 'Info-prev "left_arrow" Info-mode-map)
2135 (tool-bar-add-item-from-menu 'Info-next "right_arrow" Info-mode-map)
2136 (tool-bar-add-item-from-menu 'Info-up "up_arrow" Info-mode-map)
ec03f31e 2137 (tool-bar-add-item-from-menu 'Info-last "undo" Info-mode-map)
7210b6f5
DL
2138 (tool-bar-add-item-from-menu 'Info-top-node "home" Info-mode-map)
2139 (tool-bar-add-item-from-menu 'Info-index "index" Info-mode-map)
2140 (tool-bar-add-item-from-menu 'Info-goto-node "jump_to" Info-mode-map)
2141 (tool-bar-add-item-from-menu 'Info-search "search" Info-mode-map)
2142 tool-bar-map)))
2143
75a209d4
RS
2144(defvar Info-menu-last-node nil)
2145;; Last node the menu was created for.
6d2c8e3e 2146;; Value is a list, (FILE-NAME NODE-NAME).
75a209d4
RS
2147
2148(defun Info-menu-update ()
fdf4b680 2149 "Update the Info menu for the current node."
75a209d4
RS
2150 (condition-case nil
2151 (if (or (not (eq major-mode 'Info-mode))
6d2c8e3e
RS
2152 (equal (list Info-current-file Info-current-node)
2153 Info-menu-last-node))
75a209d4
RS
2154 ()
2155 ;; Update menu menu.
2156 (let* ((Info-complete-menu-buffer (current-buffer))
2157 (items (nreverse (condition-case nil
2158 (Info-complete-menu-item
2159 "" (lambda (e) t) t)
2160 (error nil))))
399c88ad 2161 entries current
75a209d4
RS
2162 (number 0))
2163 (while (and items (< number 9))
2164 (setq current (car items)
2165 items (cdr items)
2166 number (1+ number))
399c88ad 2167 (setq entries (cons `[,current
75a209d4
RS
2168 (Info-menu ,current)
2169 :keys ,(format "%d" number)]
2170 entries)))
2171 (if items
2172 (setq entries (cons ["Other..." Info-menu t] entries)))
2173 (or entries
5d5845dc 2174 (setq entries (list ["No menu" nil nil] nil :active)))
d4af987a 2175 (easy-menu-change '("Info") "Menu Item" (nreverse entries)))
75a209d4
RS
2176 ;; Update reference menu. Code stolen from `Info-follow-reference'.
2177 (let ((items nil)
399c88ad 2178 str i entries current
1bcedb3b
RS
2179 (number 0)
2180 (case-fold-search t))
75a209d4
RS
2181 (save-excursion
2182 (goto-char (point-min))
2183 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
2184 (setq str (buffer-substring
2185 (match-beginning 1)
2186 (1- (point))))
2187 (setq i 0)
2188 (while (setq i (string-match "[ \n\t]+" str i))
2189 (setq str (concat (substring str 0 i) " "
2190 (substring str (match-end 0))))
2191 (setq i (1+ i)))
2192 (setq items
2193 (cons str items))))
2194 (while (and items (< number 9))
2195 (setq current (car items)
2196 items (cdr items)
2197 number (1+ number))
399c88ad 2198 (setq entries (cons `[,current
75a209d4
RS
2199 (Info-follow-reference ,current)
2200 t]
2201 entries)))
2202 (if items
2203 (setq entries (cons ["Other..." Info-follow-reference t]
2204 entries)))
2205 (or entries
5d5845dc 2206 (setq entries (list ["No references" nil nil] nil :active)))
75a209d4
RS
2207 (easy-menu-change '("Info") "Reference" (nreverse entries)))
2208 ;; Update last seen node.
6d2c8e3e 2209 (setq Info-menu-last-node (list Info-current-file Info-current-node)))
75a209d4
RS
2210 ;; Try to avoid entering infinite beep mode in case of errors.
2211 (error (ding))))
2212
a384cab3 2213\f
126f5d4d
MB
2214(defun Info-copy-current-node-name ()
2215 "Put the name of the current info node into the kill ring.
2216The name of the info file is prepended to the node name in parentheses."
2217 (interactive)
2218 (unless Info-current-node
2219 (error "No current info node"))
2220 (kill-new
2221 (concat "("
2222 (file-name-nondirectory
2223 (if (stringp Info-current-file)
2224 Info-current-file
2225 (or buffer-file-name "")))
2226 ")"
2227 Info-current-node)))
2228
2229\f
a384cab3 2230;; Info mode is suitable only for specially formatted data.
68bc119c 2231(put 'Info-mode 'mode-class 'special)
a384cab3
JB
2232
2233(defun Info-mode ()
fdf4b680 2234 "Info mode provides commands for browsing through the Info documentation tree.
a384cab3
JB
2235Documentation in Info is divided into \"nodes\", each of which discusses
2236one topic and contains references to other nodes which discuss related
2237topics. Info has commands to follow the references and show you other nodes.
2238
fdf4b680 2239\\<Info-mode-map>\
a384cab3 2240\\[Info-help] Invoke the Info tutorial.
6b136ab9 2241\\[Info-exit] Quit Info: reselect previously selected buffer.
a384cab3
JB
2242
2243Selecting other nodes:
21d5959f
RS
2244\\[Info-mouse-follow-nearest-node]
2245 Follow a node reference you click on.
2246 This works with menu items, cross references, and
2247 the \"next\", \"previous\" and \"up\", depending on where you click.
6b136ab9 2248\\[Info-follow-nearest-node] Follow a node reference near point, like \\[Info-mouse-follow-nearest-node].
a384cab3 2249\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 2250\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
2251\\[Info-up] Move \"up\" from this node.
2252\\[Info-menu] Pick menu item specified by name (or abbreviation).
2253 Picking a menu item causes another node to be selected.
aea2a8da 2254\\[Info-directory] Go to the Info directory node.
a384cab3
JB
2255\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
2256\\[Info-last] Move to the last node you were at.
1143a6b0
ER
2257\\[Info-index] Look up a topic in this file's Index and move to that node.
2258\\[Info-index-next] (comma) Move to the next match from a previous `i' command.
6b136ab9
DL
2259\\[Info-top-node] Go to the Top node of this file.
2260\\[Info-final-node] Go to the final node in this file.
2261\\[Info-backward-node] Go backward one node, considering all nodes as forming one sequence.
2262\\[Info-forward-node] Go forward one node, considering all nodes as forming one sequence.
a384cab3
JB
2263
2264Moving within a node:
177c3549
RS
2265\\[Info-scroll-up] Normally, scroll forward a full screen.
2266Once you scroll far enough in a node that its menu appears on the screen
2267but after point, the next scroll moves into its first subnode.
2268When after all menu items (or if their is no menu), move up to
2269the parent node.
2270\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
803eaf50 2271already visible, try to go to the previous menu entry, or up if there is none.
399c88ad 2272\\[beginning-of-buffer] Go to beginning of node.
a384cab3 2273
a384cab3
JB
2274Advanced commands:
2275\\[Info-exit] Quit Info: reselect previously selected buffer.
2276\\[Info-edit] Edit contents of selected node.
22771 Pick first item in node's menu.
22782, 3, 4, 5 Pick second ... fifth item in node's menu.
2279\\[Info-goto-node] Move to node specified by name.
2280 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 2281\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 2282\\[Info-search] Search through this Info file for specified regexp,
803eaf50 2283 and select the node in which the next occurrence is found.
552775bd
RS
2284\\[Info-next-reference] Move cursor to next cross-reference or menu item.
2285\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
2286 (kill-all-local-variables)
2287 (setq major-mode 'Info-mode)
2288 (setq mode-name "Info")
f73299f3 2289 (setq tab-width 8)
a384cab3 2290 (use-local-map Info-mode-map)
75a209d4 2291 (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
a384cab3
JB
2292 (set-syntax-table text-mode-syntax-table)
2293 (setq local-abbrev-table text-mode-abbrev-table)
2294 (setq case-fold-search t)
2295 (setq buffer-read-only t)
a384cab3
JB
2296 (make-local-variable 'Info-current-file)
2297 (make-local-variable 'Info-current-subfile)
2298 (make-local-variable 'Info-current-node)
2299 (make-local-variable 'Info-tag-table-marker)
c5fe2ff1
RS
2300 (setq Info-tag-table-marker (make-marker))
2301 (make-local-variable 'Info-tag-table-buffer)
2302 (setq Info-tag-table-buffer nil)
a384cab3 2303 (make-local-variable 'Info-history)
1143a6b0 2304 (make-local-variable 'Info-index-alternatives)
7210b6f5 2305 (set (make-local-variable 'tool-bar-map) info-tool-bar-map)
93480d70
RS
2306 ;; This is for the sake of the invisible text we use handling titles.
2307 (make-local-variable 'line-move-ignore-invisible)
2308 (setq line-move-ignore-invisible t)
b7aeabeb 2309 (add-hook 'clone-buffer-hook 'Info-clone-buffer-hook nil t)
a384cab3
JB
2310 (Info-set-mode-line)
2311 (run-hooks 'Info-mode-hook))
2312
4fceda3c
SM
2313(defun Info-clone-buffer-hook ()
2314 (when (bufferp Info-tag-table-buffer)
2315 (setq Info-tag-table-buffer
4ce5c223
SM
2316 (with-current-buffer Info-tag-table-buffer (clone-buffer))))
2317 (let ((m Info-tag-table-marker))
2318 (when (markerp m)
15533ae2 2319 (setq Info-tag-table-marker
4ce5c223 2320 (if (and (marker-position m) (bufferp Info-tag-table-buffer))
15533ae2
SM
2321 (with-current-buffer Info-tag-table-buffer
2322 (copy-marker (marker-position m)))
2323 (make-marker))))))
4fceda3c 2324
7210b6f5
DL
2325(defvar Info-edit-map (let ((map (make-sparse-keymap)))
2326 (set-keymap-parent map text-mode-map)
2327 (define-key map "\C-c\C-c" 'Info-cease-edit)
2328 map)
a384cab3 2329 "Local keymap used within `e' command of Info.")
a384cab3
JB
2330
2331;; Info-edit mode is suitable only for specially formatted data.
68bc119c 2332(put 'Info-edit-mode 'mode-class 'special)
a384cab3
JB
2333
2334(defun Info-edit-mode ()
2335 "Major mode for editing the contents of an Info node.
a426b157 2336Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
2337which returns to Info mode for browsing.
2338\\{Info-edit-map}"
a384cab3
JB
2339 (use-local-map Info-edit-map)
2340 (setq major-mode 'Info-edit-mode)
2341 (setq mode-name "Info Edit")
2342 (kill-local-variable 'mode-line-buffer-identification)
2343 (setq buffer-read-only nil)
2c609f53 2344 (force-mode-line-update)
e82c28f9
KH
2345 (buffer-enable-undo (current-buffer))
2346 (run-hooks 'Info-edit-mode-hook))
2347
2348(defun Info-edit ()
2349 "Edit the contents of this Info node.
2350Allowed only if variable `Info-enable-edit' is non-nil."
2351 (interactive)
2352 (or Info-enable-edit
2353 (error "Editing info nodes is not enabled"))
2354 (Info-edit-mode)
8ab3e50b 2355 (message "%s" (substitute-command-keys
760d5cb3 2356 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
2357
2358(defun Info-cease-edit ()
2359 "Finish editing Info node; switch back to Info proper."
2360 (interactive)
2361 ;; Do this first, so nothing has changed if user C-g's at query.
2362 (and (buffer-modified-p)
2363 (y-or-n-p "Save the file? ")
2364 (save-buffer))
2365 (use-local-map Info-mode-map)
2366 (setq major-mode 'Info-mode)
2367 (setq mode-name "Info")
2368 (Info-set-mode-line)
2369 (setq buffer-read-only t)
2c609f53 2370 (force-mode-line-update)
a384cab3
JB
2371 (and (marker-position Info-tag-table-marker)
2372 (buffer-modified-p)
2373 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 2374\f
f88ab1c9 2375(defvar Info-file-list-for-emacs
906b3163
EZ
2376 '("ediff" "eudc" "forms" "gnus" "info" ("mh" . "mh-e")
2377 "sc" "message" ("dired" . "dired-x") "viper" "vip" "idlwave"
2378 ("c" . "ccmode") ("c++" . "ccmode") ("objc" . "ccmode")
2379 ("java" . "ccmode") ("idl" . "ccmode") ("pike" . "ccmode")
af718538
EZ
2380 ("skeleton" . "autotype") ("auto-insert" . "autotype")
2381 ("copyright" . "autotype") ("executable" . "autotype")
2382 ("time-stamp" . "autotype") ("quickurl" . "autotype")
2383 ("tempo" . "autotype") ("hippie-expand" . "autotype")
906b3163
EZ
2384 ("cvs" . "pcl-cvs") ("ada" . "ada-mode")
2385 "ebrowse" "eshell" "cl" "reftex" "speedbar" "widget" "woman"
2386 ("mail-header" . "emacs-mime") ("mail-content" . "emacs-mime")
2387 ("mail-encode" . "emacs-mime") ("mail-decode" . "emacs-mime")
2388 ("rfc2045" . "emacs-mime")
2389 ("rfc2231" . "emacs-mime") ("rfc2047" . "emacs-mime")
2390 ("rfc2045" . "emacs-mime") ("rfc1843" . "emacs-mime")
2391 ("ietf-drums" . "emacs-mime") ("quoted-printable" . "emacs-mime")
2392 ("binhex" . "emacs-mime") ("uudecode" . "emacs-mime")
2393 ("mailcap" . "emacs-mime") ("mm" . "emacs-mime")
2394 ("mml" . "emacs-mime"))
f88ab1c9
RS
2395 "List of Info files that describe Emacs commands.
2396An element can be a file name, or a list of the form (PREFIX . FILE)
2397where PREFIX is a name prefix and FILE is the file to look in.
2398If the element is just a file name, the file name also serves as the prefix.")
2399
f0a8a3f1 2400(defun Info-find-emacs-command-nodes (command)
f88ab1c9 2401 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
2402The `info-file' property of COMMAND says which Info manual to search.
2403If COMMAND has no property, the variable `Info-file-list-for-emacs'
2404defines heuristics for which Info manual to try.
fdf4b680 2405The locations are of the format used in `Info-history', i.e.
f0a8a3f1 2406\(FILENAME NODENAME BUFFERPOS\)."
f0a8a3f1 2407 (let ((where '())
b37daea4 2408 (cmd-desc (concat "^\\* +" (regexp-quote (symbol-name command))
218c2cc7 2409 "\\( <[0-9]+>\\)?:\\s *\\(.*\\)\\.$"))
f88ab1c9
RS
2410 (info-file "emacs")) ;default
2411 ;; Determine which info file this command is documented in.
2412 (if (get command 'info-file)
2413 (setq info-file (get command 'info-file))
2414 ;; If it doesn't say explicitly, test its name against
2415 ;; various prefixes that we know.
2416 (let ((file-list Info-file-list-for-emacs))
2417 (while file-list
2418 (let* ((elt (car file-list))
2419 (name (if (consp elt)
2420 (car elt)
2421 elt))
2422 (file (if (consp elt) (cdr elt) elt))
f57b2cd8 2423 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
2424 "\\(\\'\\|-\\)")))
2425 (if (string-match regexp (symbol-name command))
2426 (setq info-file file file-list nil))
2427 (setq file-list (cdr file-list))))))
218c2cc7
EZ
2428 (Info-find-node info-file "Top")
2429 (or (and (search-forward "\n* menu:" nil t)
2430 (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t))
2431 (error "Info file `%s' appears to lack an index" info-file))
2432 (goto-char (match-beginning 1))
2433 ;; Bind Info-history to nil, to prevent the index nodes from
2434 ;; getting into the node history.
2435 (let ((Info-history nil)
2436 (exact nil)
2437 node found)
2438 (Info-goto-node (Info-extract-menu-node-name))
2439 (while
2440 (progn
2441 (goto-char (point-min))
2442 (while (re-search-forward cmd-desc nil t)
2443 (setq where
2444 (cons (list Info-current-file
2445 (match-string-no-properties 2)
760d5cb3
GM
2446 0)
2447 where)))
218c2cc7
EZ
2448 (and (setq node (Info-extract-pointer "next" t))
2449 (string-match "\\<Index\\>" node)))
2450 (Info-goto-node node)))
2451 where))
f0a8a3f1
RM
2452
2453;;;###autoload
2454(defun Info-goto-emacs-command-node (command)
e0568e86 2455 "Go to the Info node in the Emacs manual for command COMMAND.
30db89f9 2456The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
2457or in another manual found via COMMAND's `info-file' property or
2458the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
2459 (interactive "CFind documentation for command: ")
2460 (or (commandp command)
2461 (signal 'wrong-type-argument (list 'commandp command)))
2462 (let ((where (Info-find-emacs-command-nodes command)))
2463 (if where
2464 (let ((num-matches (length where)))
2465 ;; Get Info running, and pop to it in another window.
2466 (save-window-excursion
2467 (info))
c5fe2ff1
RS
2468 ;; FIXME It would be cool if this could use a buffer other
2469 ;; than *info*.
f0a8a3f1 2470 (pop-to-buffer "*info*")
218c2cc7
EZ
2471 ;; Bind Info-history to nil, to prevent the last Index node
2472 ;; visited by Info-find-emacs-command-nodes from being
2473 ;; pushed onto the history.
2474 (let ((Info-history nil))
2475 (Info-find-node (car (car where))
2476 (car (cdr (car where)))))
f0a8a3f1
RM
2477 (if (> num-matches 1)
2478 (progn
218c2cc7
EZ
2479 ;; (car where) will be pushed onto Info-history
2480 ;; when/if they go to another node. Put the other
2481 ;; nodes that were found on the history.
f0a8a3f1 2482 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 2483 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
2484 (1- num-matches)
2485 (if (> num-matches 2) "ies" "y")
8ab3e50b 2486 (substitute-command-keys "\\[Info-last]")
cedb118c 2487 (if (> num-matches 2) "them" "it")))))
e9b81433 2488 (error "Couldn't find documentation for %s" command))))
f0a8a3f1
RM
2489
2490;;;###autoload
2491(defun Info-goto-emacs-key-command-node (key)
30db89f9
EZ
2492 "Go to the node in the Emacs manual which describes the command bound to KEY.
2493KEY is a string.
fdf4b680 2494Interactively, if the binding is `execute-extended-command', a command is read.
30db89f9 2495The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
2496or in another manual found via COMMAND's `info-file' property or
2497the variable `Info-file-list-for-emacs'."
ea33749b 2498 (interactive "kFind documentation for key: ")
f0a8a3f1
RM
2499 (let ((command (key-binding key)))
2500 (cond ((null command)
9e5c2f50 2501 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
2502 ((and (interactive-p)
2503 (eq command 'execute-extended-command))
2504 (Info-goto-emacs-command-node
2505 (read-command "Find documentation for command: ")))
2506 (t
2507 (Info-goto-emacs-command-node command)))))
552775bd 2508\f
bbed5c3f 2509(defface Info-title-1-face
b250866f 2510 '((((type tty pc) (class color)) (:foreground "yellow" :weight bold))
333cd59e 2511 (t (:height 1.2 :inherit Info-title-2-face)))
bbed5c3f
GM
2512 "Face for Info titles at level 1."
2513 :group 'info)
2514
2515(defface Info-title-2-face
b250866f 2516 '((((type tty pc) (class color)) (:foreground "lightblue" :weight bold))
333cd59e 2517 (t (:height 1.2 :inherit Info-title-3-face)))
bbed5c3f
GM
2518 "Face for Info titles at level 2."
2519 :group 'info)
2520
2521(defface Info-title-3-face
b250866f 2522 '((((type tty pc) (class color)) (:weight bold))
a8b883c2 2523 (t (:height 1.2 :inherit Info-title-4-face)))
bbed5c3f
GM
2524 "Face for Info titles at level 3."
2525 :group 'info)
2526
a8b883c2
MB
2527(defface Info-title-4-face
2528 '((((type tty pc) (class color)) (:weight bold))
2529 (t (:weight bold :inherit variable-pitch)))
2530 "Face for Info titles at level 4."
2531 :group 'info)
2532
973a3104
MB
2533(defface info-menu-header
2534 '((((type tty pc))
42121c23
SZ
2535 :underline t
2536 :weight bold)
973a3104 2537 (t
42121c23
SZ
2538 :inherit variable-pitch
2539 :weight bold))
973a3104
MB
2540 "Face for headers in Info menus."
2541 :group 'info)
2542
2543(defun Info-fontify-menu-headers ()
2544 "Add the face `info-menu-header' to any header before a menu entry."
2545 (save-excursion
2546 (goto-char (point-min))
2547 (when (re-search-forward "\\* Menu:" nil t)
2548 (put-text-property (match-beginning 0) (match-end 0)
2549 'face 'info-menu-header)
2550 (while (re-search-forward "\n\n\\([^*\n ].*\\)\n\n?[*]" nil t)
2551 (put-text-property (match-beginning 1) (match-end 1)
2552 'face 'info-menu-header)))))
2553
552775bd 2554(defun Info-fontify-node ()
07becdb1
MB
2555 ;; Only fontify the node if it hasn't already been done. [We pass in
2556 ;; LIMIT arg to `next-property-change' because it seems to search past
2557 ;; (point-max).]
2558 (unless (< (next-property-change (point-min) nil (point-max))
2559 (point-max))
2560 (save-excursion
2561 (let ((buffer-read-only nil)
2562 (case-fold-search t))
2563 (goto-char (point-min))
2564 (when (looking-at "^File: [^,: \t]+,?[ \t]+")
35d2d241 2565 (goto-char (match-end 0))
07becdb1
MB
2566 (while (looking-at "[ \t]*\\([^:, \t\n]+\\):[ \t]+\\([^:,\t\n]+\\),?")
2567 (goto-char (match-end 0))
2568 (let* ((nbeg (match-beginning 2))
2569 (nend (match-end 2))
2570 (tbeg (match-beginning 1))
2571 (tag (buffer-substring tbeg (match-end 1))))
2572 (if (string-equal tag "Node")
2573 (put-text-property nbeg nend 'face 'info-header-node)
2574 (put-text-property nbeg nend 'face 'info-header-xref)
2575 (put-text-property nbeg nend 'mouse-face 'highlight)
2576 (put-text-property tbeg nend
2577 'help-echo
2578 (concat "Go to node "
2579 (buffer-substring nbeg nend)))
2580 (let ((fun (cdr (assoc tag '(("Prev" . Info-prev)
2581 ("Next" . Info-next)
2582 ("Up" . Info-up))))))
2583 (when fun
2584 (let ((keymap (make-sparse-keymap)))
2585 (define-key keymap [header-line mouse-1] fun)
2586 (define-key keymap [header-line mouse-2] fun)
2587 (put-text-property tbeg nend 'local-map keymap))))
2588 ))))
2589 (goto-char (point-min))
2590 (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\|\\.+\\)$"
2591 nil t)
2592 (let ((c (preceding-char))
2593 face)
2594 (cond ((= c ?*) (setq face 'Info-title-1-face))
2595 ((= c ?=) (setq face 'Info-title-2-face))
2596 ((= c ?-) (setq face 'Info-title-3-face))
2597 (t (setq face 'Info-title-4-face)))
2598 (put-text-property (match-beginning 1) (match-end 1)
2599 'face face))
2600 ;; This is a serious problem for trying to handle multiple
2601 ;; frame types at once. We want this text to be invisible
2602 ;; on frames that can display the font above.
2603 (when (memq (framep (selected-frame)) '(x pc w32 mac))
2604 (add-text-properties (match-end 1) (match-end 2)
2605 '(invisible t intangible t))
2606 (add-text-properties (1- (match-end 1)) (match-end 2)
2607 '(intangible t))))
2608 (goto-char (point-min))
2609 (while (re-search-forward "\\*Note[ \n\t]+\\([^:]*\\):" nil t)
2610 (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
2611 nil
2612 (add-text-properties (match-beginning 1) (match-end 1)
2613 '(face info-xref
2614 mouse-face highlight
2615 help-echo "mouse-2: go to this node"))))
2616 (goto-char (point-min))
2617 (if (and (search-forward "\n* Menu:" nil t)
2618 (not (string-match "\\<Index\\>" Info-current-node))
2619 ;; Don't take time to annotate huge menus
2620 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
2621 (let ((n 0))
2622 (while (re-search-forward "^\\* +\\([^:\t\n]*\\):" nil t)
2623 (setq n (1+ n))
2624 (if (memq n '(5 9)) ; visual aids to help with 1-9 keys
2625 (put-text-property (match-beginning 0)
2626 (1+ (match-beginning 0))
2627 'face 'info-menu-5))
2628 (add-text-properties (match-beginning 1) (match-end 1)
2629 '(face info-xref
2630 mouse-face highlight
2631 help-echo "mouse-2: go to this node")))))
2632 (Info-fontify-menu-headers)
2633 (set-buffer-modified-p nil)))))
c5fe2ff1
RS
2634\f
2635
2636;; When an Info buffer is killed, make sure the associated tags buffer
2637;; is killed too.
2638(defun Info-kill-buffer ()
2639 (and (eq major-mode 'Info-mode)
2640 Info-tag-table-buffer
2641 (kill-buffer Info-tag-table-buffer)))
2642
2643(add-hook 'kill-buffer-hook 'Info-kill-buffer)
3cb6768f
EL
2644
2645;;; Speedbar support:
2646;; These functions permit speedbar to display the "tags" in the
2647;; current info node.
96ee3f29 2648(eval-when-compile (require 'speedbar))
3cb6768f 2649
96ee3f29
KH
2650(defvar Info-speedbar-key-map nil
2651 "Keymap used when in the info display mode.")
3cb6768f 2652
96ee3f29
KH
2653(defun Info-install-speedbar-variables ()
2654 "Install those variables used by speedbar to enhance Info."
2655 (if Info-speedbar-key-map
2656 nil
2657 (setq Info-speedbar-key-map (speedbar-make-specialized-keymap))
2658
2659 ;; Basic tree features
2660 (define-key Info-speedbar-key-map "e" 'speedbar-edit-line)
2661 (define-key Info-speedbar-key-map "\C-m" 'speedbar-edit-line)
2662 (define-key Info-speedbar-key-map "+" 'speedbar-expand-line)
2663 (define-key Info-speedbar-key-map "-" 'speedbar-contract-line)
2664 )
2665
2666 (speedbar-add-expansion-list '("Info" Info-speedbar-menu-items
2667 Info-speedbar-key-map
2668 Info-speedbar-hierarchy-buttons)))
3cb6768f
EL
2669
2670(defvar Info-speedbar-menu-items
96ee3f29
KH
2671 '(["Browse Node" speedbar-edit-line t]
2672 ["Expand Node" speedbar-expand-line
2673 (save-excursion (beginning-of-line)
2674 (looking-at "[0-9]+: *.\\+. "))]
2675 ["Contract Node" speedbar-contract-line
2676 (save-excursion (beginning-of-line)
2677 (looking-at "[0-9]+: *.-. "))]
2678 )
3cb6768f
EL
2679 "Additional menu-items to add to speedbar frame.")
2680
96ee3f29
KH
2681;; Make sure our special speedbar major mode is loaded
2682(if (featurep 'speedbar)
2683 (Info-install-speedbar-variables)
2684 (add-hook 'speedbar-load-hook 'Info-install-speedbar-variables))
2685
2686;;; Info hierarchy display method
2687;;;###autoload
2688(defun Info-speedbar-browser ()
2689 "Initialize speedbar to display an info node browser.
2690This will add a speedbar major display mode."
2691 (interactive)
2692 (require 'speedbar)
2693 ;; Make sure that speedbar is active
2694 (speedbar-frame-mode 1)
2695 ;; Now, throw us into Info mode on speedbar.
2696 (speedbar-change-initial-expansion-list "Info")
2697 )
2698
7210b6f5
DL
2699(eval-when-compile (defvar speedbar-attached-frame))
2700
96ee3f29
KH
2701(defun Info-speedbar-hierarchy-buttons (directory depth &optional node)
2702 "Display an Info directory hierarchy in speedbar.
2703DIRECTORY is the current directory in the attached frame.
2704DEPTH is the current indentation depth.
2705NODE is an optional argument that is used to represent the
2706specific node to expand."
2707 (if (and (not node)
2708 (save-excursion (goto-char (point-min))
1bcedb3b
RS
2709 (let ((case-fold-search t))
2710 (looking-at "Info Nodes:"))))
96ee3f29
KH
2711 ;; Update our "current node" maybe?
2712 nil
2713 ;; We cannot use the generic list code, that depends on all leaves
2714 ;; being known at creation time.
2715 (if (not node)
2716 (speedbar-with-writable (insert "Info Nodes:\n")))
5fdc7997
EL
2717 (let ((completions nil)
2718 (cf (selected-frame)))
2719 (select-frame speedbar-attached-frame)
2720 (save-window-excursion
2721 (setq completions
2722 (Info-speedbar-fetch-file-nodes (or node '"(dir)top"))))
2723 (select-frame cf)
96ee3f29
KH
2724 (if completions
2725 (speedbar-with-writable
2726 (while completions
2727 (speedbar-make-tag-line 'bracket ?+ 'Info-speedbar-expand-node
2728 (cdr (car completions))
2729 (car (car completions))
2730 'Info-speedbar-goto-node
2731 (cdr (car completions))
2732 'info-xref depth)
2733 (setq completions (cdr completions)))
2734 t)
2735 nil))))
399c88ad 2736
96ee3f29 2737(defun Info-speedbar-goto-node (text node indent)
d1268e52 2738 "When user clicks on TEXT, go to an info NODE.
96ee3f29 2739The INDENT level is ignored."
760d5cb3
GM
2740 (select-frame speedbar-attached-frame)
2741 (let* ((buff (or (get-buffer "*info*")
2742 (progn (info) (get-buffer "*info*"))))
2743 (bwin (get-buffer-window buff 0)))
2744 (if bwin
2745 (progn
2746 (select-window bwin)
2747 (raise-frame (window-frame bwin)))
2748 (if speedbar-power-click
2749 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
2750 (select-frame speedbar-attached-frame)
2751 (switch-to-buffer buff)))
2752 (let ((junk (string-match "^(\\([^)]+\\))\\([^.]+\\)$" node))
2753 (file (match-string 1 node))
2754 (node (match-string 2 node)))
2755 (Info-find-node file node)
2756 ;; If we do a find-node, and we were in info mode, restore
2757 ;; the old default method. Once we are in info mode, it makes
2758 ;; sense to return to whatever method the user was using before.
2759 (if (string= speedbar-initial-expansion-list-name "Info")
2760 (speedbar-change-initial-expansion-list
2761 speedbar-previously-used-expansion-list-name)))))
96ee3f29
KH
2762
2763(defun Info-speedbar-expand-node (text token indent)
2764 "Expand the node the user clicked on.
2765TEXT is the text of the button we clicked on, a + or - item.
2766TOKEN is data related to this node (NAME . FILE).
2767INDENT is the current indentation depth."
2768 (cond ((string-match "+" text) ;we have to expand this file
2769 (speedbar-change-expand-button-char ?-)
2770 (if (speedbar-with-writable
760d5cb3
GM
2771 (save-excursion
2772 (end-of-line) (forward-char 1)
2773 (Info-speedbar-hierarchy-buttons nil (1+ indent) token)))
96ee3f29
KH
2774 (speedbar-change-expand-button-char ?-)
2775 (speedbar-change-expand-button-char ??)))
2776 ((string-match "-" text) ;we have to contract this node
2777 (speedbar-change-expand-button-char ?+)
2778 (speedbar-delete-subblock indent))
2779 (t (error "Ooops... not sure what to do")))
2780 (speedbar-center-buffer-smartly))
2781
2782(defun Info-speedbar-fetch-file-nodes (nodespec)
2783 "Fetch the subnodes from the info NODESPEC.
2784NODESPEC is a string of the form: (file)node.
2785Optional THISFILE represends the filename of"
2786 (save-excursion
2787 ;; Set up a buffer we can use to fake-out Info.
2788 (set-buffer (get-buffer-create "*info-browse-tmp*"))
2789 (if (not (equal major-mode 'Info-mode))
2790 (Info-mode))
2791 ;; Get the node into this buffer
2792 (let ((junk (string-match "^(\\([^)]+\\))\\([^.]+\\)$" nodespec))
2793 (file (match-string 1 nodespec))
2794 (node (match-string 2 nodespec)))
2795 (Info-find-node file node))
2796 ;; Scan the created buffer
2797 (goto-char (point-min))
2798 (let ((completions nil)
1bcedb3b 2799 (case-fold-search t)
96ee3f29
KH
2800 (thisfile (progn (string-match "^(\\([^)]+\\))" nodespec)
2801 (match-string 1 nodespec))))
2802 ;; Always skip the first one...
2803 (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
2804 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
2805 (let ((name (match-string 1)))
2806 (if (looking-at " *\\(([^)]+)[^.\n]+\\)\\.")
2807 (setq name (cons name (match-string 1)))
2808 (if (looking-at " *\\(([^)]+)\\)\\.")
2809 (setq name (cons name (concat (match-string 1) "Top")))
2810 (if (looking-at " \\([^.]+\\).")
2811 (setq name
2812 (cons name (concat "(" thisfile ")" (match-string 1))))
2813 (setq name (cons name (concat "(" thisfile ")" name))))))
2814 (setq completions (cons name completions))))
2815 (nreverse completions))))
2816
2817;;; Info mode node listing
3cb6768f
EL
2818(defun Info-speedbar-buttons (buffer)
2819 "Create a speedbar display to help navigation in an Info file.
2820BUFFER is the buffer speedbar is requesting buttons for."
96ee3f29 2821 (if (save-excursion (goto-char (point-min))
1bcedb3b
RS
2822 (let ((case-fold-search t))
2823 (not (looking-at "Info Nodes:"))))
96ee3f29
KH
2824 (erase-buffer))
2825 (Info-speedbar-hierarchy-buttons nil 0)
2826 )
49116ac0 2827
48e5b471
DL
2828(dolist (mess '("^Node has no Previous$"
2829 "^No menu in this node$"
2830 "^Node has no Next$"
399c88ad
SS
2831 "^No cross-references in this node^"
2832 search-failed
48e5b471
DL
2833 "^No \".*\" in index$"))
2834 (add-to-list 'debug-ignored-errors mess))
2835
49116ac0
JB
2836(provide 'info)
2837
1a06eabd 2838;;; info.el ends here