Update maintainer email address.
[bpt/emacs.git] / lisp / info.el
CommitLineData
e3431643 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)
97f99202 273 (if (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
511else defaults to `Top'."
512 (interactive
513 (list (if current-prefix-arg
514 (completing-read "Node name: " (Info-build-node-completions)
515 nil t "Top")
516 "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:\\) *\\("
dd31e4e8
GM
667 (if (stringp nodename)
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)))
973a3104 904 (if Info-fontify (Info-fontify-menu-headers))
4fba3b2c
RS
905 (goto-char (point-min))
906 (if problems
907 (message "Composing main Info directory...problems encountered, see `*Messages*'")
908 (message "Composing main Info directory...done")))
44c327f9
JB
909 (setq Info-dir-contents (buffer-string)))
910 (setq default-directory Info-dir-contents-directory))
7ea13762 911
c5fe2ff1
RS
912;; Note that on entry to this function the current-buffer must be the
913;; *info* buffer; not the info tags buffer.
a384cab3 914(defun Info-read-subfile (nodepos)
a361deba
RS
915 ;; NODEPOS is either a position (in the Info file as a whole,
916 ;; not relative to a subfile) or the name of a subfile.
a384cab3
JB
917 (let (lastfilepos
918 lastfilename)
a361deba
RS
919 (if (numberp nodepos)
920 (save-excursion
921 (set-buffer (marker-buffer Info-tag-table-marker))
922 (goto-char (point-min))
ba42ce14
EZ
923 (or (looking-at "\^_")
924 (search-forward "\n\^_"))
a361deba
RS
925 (forward-line 2)
926 (catch 'foo
927 (while (not (looking-at "\^_"))
928 (if (not (eolp))
929 (let ((beg (point))
930 thisfilepos thisfilename)
931 (search-forward ": ")
932 (setq thisfilename (buffer-substring beg (- (point) 2)))
933 (setq thisfilepos (read (current-buffer)))
934 ;; read in version 19 stops at the end of number.
935 ;; Advance to the next line.
936 (forward-line 1)
937 (if (> thisfilepos nodepos)
938 (throw 'foo t))
939 (setq lastfilename thisfilename)
940 (setq lastfilepos thisfilepos))
941 (forward-line 1)))))
942 (setq lastfilename nodepos)
943 (setq lastfilepos 0))
c5fe2ff1
RS
944 ;; Assume previous buffer is in Info-mode.
945 ;; (set-buffer (get-buffer "*info*"))
a384cab3
JB
946 (or (equal Info-current-subfile lastfilename)
947 (let ((buffer-read-only nil))
948 (setq buffer-file-name nil)
949 (widen)
950 (erase-buffer)
1143a6b0 951 (info-insert-file-contents lastfilename)
a384cab3
JB
952 (set-buffer-modified-p nil)
953 (setq Info-current-subfile lastfilename)))
b0e52598
RS
954 ;; Widen in case we are in the same subfile as before.
955 (widen)
a384cab3 956 (goto-char (point-min))
ba42ce14
EZ
957 (if (looking-at "\^_")
958 (forward-char 1)
959 (search-forward "\n\^_"))
a361deba
RS
960 (if (numberp nodepos)
961 (+ (- nodepos lastfilepos) (point)))))
a384cab3 962
50ac70af
MB
963(defvar Info-header-line nil
964 "If the info node header is hidden, the text of the header.")
965
a384cab3 966(defun Info-select-node ()
fdf4b680
DL
967"Select the info node that point is in.
968Bind this in case the user sets it to nil."
1bcedb3b
RS
969 (let ((case-fold-search t))
970 (save-excursion
760d5cb3
GM
971 ;; Find beginning of node.
972 (if (search-backward "\n\^_" nil 'move)
973 (forward-line 2)
974 (if (looking-at "\^_")
975 (forward-line 1)
976 (signal 'search-failed (list "\n\^_"))))
977 ;; Get nodename spelled as it is in the node.
978 (re-search-forward "Node:[ \t]*")
979 (setq Info-current-node
980 (buffer-substring-no-properties (point)
981 (progn
982 (skip-chars-forward "^,\t\n")
983 (point))))
984 (Info-set-mode-line)
985 ;; Find the end of it, and narrow.
986 (beginning-of-line)
987 (let (active-expression)
50ac70af 988 ;; Narrow to the node contents
760d5cb3
GM
989 (narrow-to-region (point)
990 (if (re-search-forward "\n[\^_\f]" nil t)
991 (prog1
992 (1- (point))
993 (if (looking-at "[\n\^_\f]*execute: ")
994 (progn
995 (goto-char (match-end 0))
996 (setq active-expression
997 (read (current-buffer))))))
998 (point-max)))
999 (if Info-enable-active-nodes (eval active-expression))
1000 (if Info-fontify (Info-fontify-node))
50ac70af
MB
1001 (if Info-use-header-line
1002 (Info-setup-header-line)
1003 (setq Info-header-line nil))
760d5cb3 1004 (run-hooks 'Info-selection-hook)))))
a384cab3
JB
1005
1006(defun Info-set-mode-line ()
1007 (setq mode-line-buffer-identification
313fe58a 1008 (nconc (propertized-buffer-identification "%b")
918e364a
MB
1009 (list
1010 (concat " ("
1011 (file-name-nondirectory
1012 (if (stringp Info-current-file)
1013 Info-current-file
1014 (or buffer-file-name "")))
1015 ") "
1016 (or Info-current-node ""))))))
a384cab3 1017\f
50ac70af
MB
1018;; Skip the node header and make it into a header-line. This function
1019;; should be called when the node is already narrowed.
1020(defun Info-setup-header-line ()
1021 (goto-char (point-min))
6e961dcd
EZ
1022 (let* ((case-fold-search t)
1023 (header-end (save-excursion (forward-line 1) (1- (point))))
1024 ;; If we find neither Next: nor Prev: link, show the entire
1025 ;; node header. Otherwise, don't show the File: and Node:
1026 ;; parts, to avoid wasting precious space on information that
1027 ;; is available in the mode line.
1028 (header-beg (if (re-search-forward
1029 "\\(next\\|prev[ious]*\\): "
1030 header-end t)
1031 (match-beginning 1)
1032 (point))))
1033 (set (make-local-variable 'Info-header-line)
1034 (buffer-substring header-beg header-end))
1035 (setq header-line-format 'Info-header-line)
1036 (narrow-to-region (1+ header-end) (point-max))))
50ac70af 1037\f
a384cab3
JB
1038;; Go to an info node specified with a filename-and-nodename string
1039;; of the sort that is found in pointers in nodes.
1040
4fceda3c
SM
1041(defun Info-goto-node (nodename &optional fork)
1042 "Go to info node named NAME. Give just NODENAME or (FILENAME)NODENAME.
7210b6f5
DL
1043If FORK is non-nil (interactively with a prefix arg), show the node in
1044a new info buffer.
4fceda3c 1045If FORK is a string, it is the name to use for the new buffer."
d1268e52 1046 (interactive (list (Info-read-node-name "Go to node: ") current-prefix-arg))
d0e41cba 1047 (info-initialize)
4fceda3c 1048 (if fork
760d5cb3
GM
1049 (set-buffer
1050 (clone-buffer (concat "*info-" (if (stringp fork) fork nodename) "*") t)))
a384cab3
JB
1051 (let (filename)
1052 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
1053 nodename)
1054 (setq filename (if (= (match-beginning 1) (match-end 1))
1055 ""
1056 (substring nodename (match-beginning 2) (match-end 2)))
1057 nodename (substring nodename (match-beginning 3) (match-end 3)))
1058 (let ((trim (string-match "\\s *\\'" filename)))
1059 (if trim (setq filename (substring filename 0 trim))))
1060 (let ((trim (string-match "\\s *\\'" nodename)))
1061 (if trim (setq nodename (substring nodename 0 trim))))
e579232c 1062 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
1063 (Info-find-node (if (equal filename "") nil filename)
1064 (if (equal nodename "") "Top" nodename))))
552775bd 1065
ddf89211
RS
1066(defvar Info-read-node-completion-table)
1067
03524be6 1068;; This function is used as the "completion table" while reading a node name.
ddf89211 1069;; It does completion using the alist in Info-read-node-completion-table
03524be6
RS
1070;; unless STRING starts with an open-paren.
1071(defun Info-read-node-name-1 (string predicate code)
1072 (let ((no-completion (and (> (length string) 0) (eq (aref string 0) ?\())))
1073 (cond ((eq code nil)
1074 (if no-completion
1075 string
ddf89211 1076 (try-completion string Info-read-node-completion-table predicate)))
03524be6
RS
1077 ((eq code t)
1078 (if no-completion
1079 nil
ddf89211 1080 (all-completions string Info-read-node-completion-table predicate)))
03524be6
RS
1081 ((eq code 'lambda)
1082 (if no-completion
1083 t
ddf89211 1084 (assoc string Info-read-node-completion-table))))))
03524be6 1085
552775bd
RS
1086(defun Info-read-node-name (prompt &optional default)
1087 (let* ((completion-ignore-case t)
ddf89211 1088 (Info-read-node-completion-table (Info-build-node-completions))
b6d61ffa 1089 (nodename (completing-read prompt 'Info-read-node-name-1 nil t)))
552775bd
RS
1090 (if (equal nodename "")
1091 (or default
1092 (Info-read-node-name prompt))
1093 nodename)))
1094
1095(defun Info-build-node-completions ()
1096 (or Info-current-file-completions
1bcedb3b
RS
1097 (let ((compl nil)
1098 ;; Bind this in case the user sets it to nil.
11638562
EZ
1099 (case-fold-search t)
1100 (node-regexp "Node: *\\([^,\n]*\\) *[,\n\t]"))
552775bd
RS
1101 (save-excursion
1102 (save-restriction
1103 (if (marker-buffer Info-tag-table-marker)
c5fe2ff1
RS
1104 (let ((marker Info-tag-table-marker))
1105 (set-buffer (marker-buffer marker))
cedb118c 1106 (widen)
c5fe2ff1 1107 (goto-char marker)
fdf4b680 1108 (while (re-search-forward "\n\\(Node\\|Ref\\): \\(.*\\)\177" nil t)
552775bd 1109 (setq compl
fdf4b680 1110 (cons (list (match-string-no-properties 2))
552775bd
RS
1111 compl))))
1112 (widen)
1113 (goto-char (point-min))
11638562
EZ
1114 ;; If the buffer begins with a node header, process that first.
1115 (if (Info-node-at-bob-matching node-regexp)
110d4e80 1116 (setq compl (list (match-string-no-properties 1))))
11638562 1117 ;; Now for the rest of the nodes.
552775bd
RS
1118 (while (search-forward "\n\^_" nil t)
1119 (forward-line 1)
1120 (let ((beg (point)))
1121 (forward-line 1)
11638562 1122 (if (re-search-backward node-regexp beg t)
399c88ad 1123 (setq compl
110d4e80 1124 (cons (list (match-string-no-properties 1))
552775bd 1125 compl))))))))
87d2b5a2 1126 (setq compl (cons '("*") compl))
552775bd 1127 (setq Info-current-file-completions compl))))
a384cab3 1128\f
aea2a8da
JB
1129(defun Info-restore-point (hl)
1130 "If this node has been visited, restore the point value when we left."
cedb118c
RS
1131 (while hl
1132 (if (and (equal (nth 0 (car hl)) Info-current-file)
e90d1271
RS
1133 ;; Use string-equal, not equal, to ignore text props.
1134 (string-equal (nth 1 (car hl)) Info-current-node))
cedb118c 1135 (progn
d96504df
KH
1136 (goto-char (nth 2 (car hl)))
1137 (setq hl nil)) ;terminate the while at next iter
cedb118c 1138 (setq hl (cdr hl)))))
aea2a8da 1139\f
2c80a618
GM
1140(defvar Info-search-history nil
1141 "The history list for `Info-search'.")
a384cab3
JB
1142
1143(defun Info-search (regexp)
1144 "Search for REGEXP, starting from point, and select node it's found in."
bbc96600
EZ
1145 (interactive (list (read-string
1146 (if Info-search-history
1147 (format "Regexp search (default `%s'): "
1148 (car Info-search-history))
1149 "Regexp search: ")
1150 nil 'Info-search-history)))
2c80a618
GM
1151 (when transient-mark-mode
1152 (deactivate-mark))
1153 (when (equal regexp "")
1154 (setq regexp (car Info-search-history)))
c47b5bbe
DL
1155 (when regexp
1156 (let ((found ()) current
1157 (onode Info-current-node)
1158 (ofile Info-current-file)
1159 (opoint (point))
1160 (ostart (window-start))
1161 (osubfile Info-current-subfile))
1162 (save-excursion
1163 (save-restriction
1164 (widen)
1165 (if (null Info-current-subfile)
a384cab3 1166 (progn (re-search-forward regexp) (setq found (point)))
c47b5bbe
DL
1167 (condition-case err
1168 (progn (re-search-forward regexp) (setq found (point)))
1169 (search-failed nil)))))
760d5cb3 1170 (if (not found) ;can only happen in subfile case -- else would have erred
c47b5bbe
DL
1171 (unwind-protect
1172 (let ((list ()))
1173 (save-excursion
1174 (set-buffer (marker-buffer Info-tag-table-marker))
c5fe2ff1 1175 (goto-char (point-min))
c47b5bbe
DL
1176 (search-forward "\n\^_\nIndirect:")
1177 (save-restriction
1178 (narrow-to-region (point)
1179 (progn (search-forward "\n\^_")
1180 (1- (point))))
1181 (goto-char (point-min))
1182 (search-forward (concat "\n" osubfile ": "))
1183 (beginning-of-line)
1184 (while (not (eobp))
1185 (re-search-forward "\\(^.*\\): [0-9]+$")
1186 (goto-char (+ (match-end 1) 2))
1187 (setq list (cons (cons (read (current-buffer))
110d4e80 1188 (match-string-no-properties 1))
c47b5bbe
DL
1189 list))
1190 (goto-char (1+ (match-end 0))))
1191 (setq list (nreverse list)
1192 current (car (car list))
1193 list (cdr list))))
1194 (while list
1195 (message "Searching subfile %s..." (cdr (car list)))
1196 (Info-read-subfile (car (car list)))
1197 (setq list (cdr list))
c47b5bbe
DL
1198 (if (re-search-forward regexp nil t)
1199 (setq found (point) list ())))
1200 (if found
1201 (message "")
1202 (signal 'search-failed (list regexp))))
1203 (if (not found)
1204 (progn (Info-read-subfile osubfile)
1205 (goto-char opoint)
1206 (Info-select-node)
1207 (set-window-start (selected-window) ostart)))))
760d5cb3
GM
1208 (widen)
1209 (goto-char found)
1210 (Info-select-node)
1211 ;; Use string-equal, not equal, to ignore text props.
1212 (or (and (string-equal onode Info-current-node)
1213 (equal ofile Info-current-file))
1214 (setq Info-history (cons (list ofile onode opoint)
1215 Info-history))))))
a384cab3 1216\f
a384cab3 1217(defun Info-extract-pointer (name &optional errorname)
fdf4b680
DL
1218 "Extract the value of the node-pointer named NAME.
1219If there is none, use ERRORNAME in the error message;
1220if ERRORNAME is nil, just return nil.
1221Bind this in case the user sets it to nil."
1bcedb3b
RS
1222 (let ((case-fold-search t))
1223 (save-excursion
50ac70af
MB
1224 (save-restriction
1225 (goto-char (point-min))
1226 (when Info-header-line
1227 ;; expose the header line in the buffer
c1699282
SM
1228 (widen)
1229 (forward-line -1))
50ac70af
MB
1230 (let ((bound (point)))
1231 (forward-line 1)
c1699282 1232 (cond ((re-search-backward (concat name ":") bound t)
0cb39153
MB
1233 (goto-char (match-end 0))
1234 (Info-following-node-name))
50ac70af
MB
1235 ((not (eq errorname t))
1236 (error "Node has no %s"
1237 (capitalize (or errorname name))))))))))
a384cab3
JB
1238
1239(defun Info-following-node-name (&optional allowedchars)
fdf4b680
DL
1240 "Return the node name in the buffer following point.
1241ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
1242saying which chars may appear in the node name."
a384cab3 1243 (skip-chars-forward " \t")
70d78eb6 1244 (buffer-substring-no-properties
a384cab3
JB
1245 (point)
1246 (progn
1247 (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
1248 (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
1249 (if (looking-at "(")
1250 (skip-chars-forward "^)")))
1251 (skip-chars-backward " ")
1252 (point))))
1253
1254(defun Info-next ()
1255 "Go to the next node of this node."
1256 (interactive)
1257 (Info-goto-node (Info-extract-pointer "next")))
1258
1259(defun Info-prev ()
1260 "Go to the previous node of this node."
1261 (interactive)
1262 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
1263
5dab2fb4
RS
1264(defun Info-up (&optional same-file)
1265 "Go to the superior node of this node.
1266If SAME-FILE is non-nil, do not move to a different Info file."
a384cab3 1267 (interactive)
5dab2fb4 1268 (let ((node (Info-extract-pointer "up")))
4fceda3c 1269 (and (or same-file (not (stringp Info-current-file)))
5dab2fb4
RS
1270 (string-match "^(" node)
1271 (error "Up node is in another Info file"))
1272 (Info-goto-node node))
aea2a8da 1273 (Info-restore-point Info-history))
a384cab3
JB
1274
1275(defun Info-last ()
1276 "Go back to the last node visited."
1277 (interactive)
1278 (or Info-history
1279 (error "This is the first Info node you looked at"))
1280 (let (filename nodename opoint)
1281 (setq filename (car (car Info-history)))
1282 (setq nodename (car (cdr (car Info-history))))
1283 (setq opoint (car (cdr (cdr (car Info-history)))))
1284 (setq Info-history (cdr Info-history))
1285 (Info-find-node filename nodename)
1286 (setq Info-history (cdr Info-history))
1287 (goto-char opoint)))
1288
48e5b471 1289;;;###autoload
a384cab3
JB
1290(defun Info-directory ()
1291 "Go to the Info directory node."
1292 (interactive)
1293 (Info-find-node "dir" "top"))
1294\f
1295(defun Info-follow-reference (footnotename)
fdf4b680
DL
1296 "Follow cross reference named FOOTNOTENAME to the node it refers to.
1297FOOTNOTENAME may be an abbreviation of the reference name."
a384cab3
JB
1298 (interactive
1299 (let ((completion-ignore-case t)
1bcedb3b 1300 (case-fold-search t)
67bc89ab 1301 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 1302 (save-excursion
67bc89ab
RS
1303 ;; Store end and beginning of line.
1304 (end-of-line)
1305 (setq eol (point))
1306 (beginning-of-line)
1307 (setq bol (point))
1308
a384cab3
JB
1309 (goto-char (point-min))
1310 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
110d4e80 1311 (setq str (buffer-substring-no-properties
a384cab3
JB
1312 (match-beginning 1)
1313 (1- (point))))
1314 ;; See if this one should be the default.
1315 (and (null default)
1f179e27 1316 (<= (match-beginning 0) start-point)
a384cab3
JB
1317 (<= start-point (point))
1318 (setq default t))
67bc89ab
RS
1319 ;; See if this one should be the alternate default.
1320 (and (null alt-default)
1321 (and (<= bol (match-beginning 0))
1322 (<= (point) eol))
1323 (setq alt-default t))
a384cab3
JB
1324 (setq i 0)
1325 (while (setq i (string-match "[ \n\t]+" str i))
1326 (setq str (concat (substring str 0 i) " "
1327 (substring str (match-end 0))))
1328 (setq i (1+ i)))
1329 ;; Record as a completion and perhaps as default.
1330 (if (eq default t) (setq default str))
67bc89ab 1331 (if (eq alt-default t) (setq alt-default str))
ec6d29af
KH
1332 ;; Don't add this string if it's a duplicate.
1333 ;; We use a loop instead of "(assoc str completions)" because
1334 ;; we want to do a case-insensitive compare.
1335 (let ((tail completions)
1336 (tem (downcase str)))
1337 (while (and tail
1338 (not (string-equal tem (downcase (car (car tail))))))
1339 (setq tail (cdr tail)))
1340 (or tail
1341 (setq completions
1342 (cons (cons str nil)
1343 completions))))))
67bc89ab
RS
1344 ;; If no good default was found, try an alternate.
1345 (or default
1346 (setq default alt-default))
1347 ;; If only one cross-reference found, then make it default.
1348 (if (eq (length completions) 1)
1349 (setq default (car (car completions))))
a384cab3 1350 (if completions
b0ebdfe5
RS
1351 (let ((input (completing-read (if default
1352 (concat "Follow reference named: ("
1353 default ") ")
1354 "Follow reference named: ")
1355 completions nil t)))
1356 (list (if (equal input "")
1357 default input)))
a384cab3 1358 (error "No cross-references in this node"))))
ebf8f7e1
RS
1359
1360 (unless footnotename
1361 (error "No reference was specified"))
1362
1bcedb3b
RS
1363 (let (target beg i (str (concat "\\*note " (regexp-quote footnotename)))
1364 (case-fold-search t))
a384cab3
JB
1365 (while (setq i (string-match " " str i))
1366 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
1367 (setq i (+ i 6)))
1368 (save-excursion
1369 (goto-char (point-min))
1370 (or (re-search-forward str nil t)
1371 (error "No cross-reference named %s" footnotename))
1372 (goto-char (+ (match-beginning 0) 5))
1373 (setq target
1374 (Info-extract-menu-node-name "Bad format cross reference" t)))
1375 (while (setq i (string-match "[ \t\n]+" target i))
1376 (setq target (concat (substring target 0 i) " "
1377 (substring target (match-end 0))))
1378 (setq i (+ i 1)))
1379 (Info-goto-node target)))
1380
1381(defun Info-extract-menu-node-name (&optional errmessage multi-line)
1382 (skip-chars-forward " \t\n")
1383 (let ((beg (point))
1384 str i)
1385 (skip-chars-forward "^:")
1386 (forward-char 1)
1387 (setq str
1388 (if (looking-at ":")
70d78eb6 1389 (buffer-substring-no-properties beg (1- (point)))
a384cab3
JB
1390 (skip-chars-forward " \t\n")
1391 (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
1392 (while (setq i (string-match "\n" str i))
1393 (aset str i ?\ ))
2e52ff59
RS
1394 ;; Collapse multiple spaces.
1395 (while (string-match " +" str)
1396 (setq str (replace-match " " t t str)))
a384cab3
JB
1397 str))
1398
e8adde3f 1399;; No one calls this.
9e5c2f50
RS
1400;;(defun Info-menu-item-sequence (list)
1401;; (while list
e8adde3f 1402;; (Info-menu (car list))
9e5c2f50 1403;; (setq list (cdr list))))
a384cab3 1404
6356e646
RS
1405(defvar Info-complete-menu-buffer)
1406
e8adde3f 1407(defun Info-complete-menu-item (string predicate action)
760d5cb3
GM
1408 (let ((completion-ignore-case t)
1409 (case-fold-search t))
e8adde3f
RS
1410 (cond ((eq action nil)
1411 (let (completions
b37daea4 1412 (pattern (concat "\n\\* +\\("
e8adde3f
RS
1413 (regexp-quote string)
1414 "[^:\t\n]*\\):")))
1415 (save-excursion
1416 (set-buffer Info-complete-menu-buffer)
1417 (goto-char (point-min))
763da090 1418 (search-forward "\n* Menu:")
e8adde3f 1419 (while (re-search-forward pattern nil t)
110d4e80
DL
1420 (setq completions
1421 (cons (cons (match-string-no-properties 1)
1422 (match-beginning 1))
1423 completions))))
e8adde3f
RS
1424 (try-completion string completions predicate)))
1425 ((eq action t)
1426 (let (completions
b37daea4 1427 (pattern (concat "\n\\* +\\("
e8adde3f
RS
1428 (regexp-quote string)
1429 "[^:\t\n]*\\):")))
1430 (save-excursion
1431 (set-buffer Info-complete-menu-buffer)
1432 (goto-char (point-min))
763da090 1433 (search-forward "\n* Menu:")
e8adde3f 1434 (while (re-search-forward pattern nil t)
110d4e80
DL
1435 (setq completions (cons (cons
1436 (match-string-no-properties 1)
1437 (match-beginning 1))
e8adde3f
RS
1438 completions))))
1439 (all-completions string completions predicate)))
1440 (t
1441 (save-excursion
1442 (set-buffer Info-complete-menu-buffer)
1443 (goto-char (point-min))
763da090 1444 (search-forward "\n* Menu:")
b37daea4 1445 (re-search-forward (concat "\n\\* +"
e8adde3f
RS
1446 (regexp-quote string)
1447 ":")
1448 nil t))))))
1449
1450
4fceda3c 1451(defun Info-menu (menu-item &optional fork)
a384cab3 1452 "Go to node for menu item named (or abbreviated) NAME.
7210b6f5
DL
1453Completion is allowed, and the menu item point is on is the default.
1454If FORK is non-nil (interactively with a prefix arg), show the node in
1455a new info buffer. If FORK is a string, it is the name to use for the
1456new buffer."
a384cab3
JB
1457 (interactive
1458 (let ((completions '())
1459 ;; If point is within a menu item, use that item as the default
1460 (default nil)
1461 (p (point))
211d6309 1462 beg
760d5cb3
GM
1463 (last nil)
1464 (case-fold-search t))
a384cab3
JB
1465 (save-excursion
1466 (goto-char (point-min))
1467 (if (not (search-forward "\n* menu:" nil t))
1468 (error "No menu in this node"))
e8adde3f
RS
1469 (setq beg (point))
1470 (and (< (point) p)
1471 (save-excursion
1472 (goto-char p)
1473 (end-of-line)
4fc14e5a 1474 (if (re-search-backward "\n\\* +\\([^:\t\n]*\\):" beg t)
110d4e80 1475 (setq default (match-string-no-properties 1))))))
a384cab3
JB
1476 (let ((item nil))
1477 (while (null item)
e8adde3f
RS
1478 (setq item (let ((completion-ignore-case t)
1479 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
1480 (completing-read (if default
1481 (format "Menu item (default %s): "
1482 default)
760d5cb3 1483 "Menu item: ")
e8adde3f 1484 'Info-complete-menu-item nil t)))
aea2a8da
JB
1485 ;; we rely on the fact that completing-read accepts an input
1486 ;; of "" even when the require-match argument is true and ""
1487 ;; is not a valid possibility
a384cab3
JB
1488 (if (string= item "")
1489 (if default
1490 (setq item default)
760d5cb3
GM
1491 ;; ask again
1492 (setq item nil))))
4fceda3c 1493 (list item current-prefix-arg))))
a384cab3
JB
1494 ;; there is a problem here in that if several menu items have the same
1495 ;; name you can only go to the node of the first with this command.
4fceda3c 1496 (Info-goto-node (Info-extract-menu-item menu-item) (if fork menu-item)))
399c88ad 1497
a384cab3
JB
1498(defun Info-extract-menu-item (menu-item)
1499 (setq menu-item (regexp-quote menu-item))
1bcedb3b
RS
1500 (let ((case-fold-search t))
1501 (save-excursion
760d5cb3
GM
1502 (let ((case-fold-search t))
1503 (goto-char (point-min))
1504 (or (search-forward "\n* menu:" nil t)
1505 (error "No menu in this node"))
1506 (or (re-search-forward (concat "\n\\* +" menu-item ":") nil t)
1507 (re-search-forward (concat "\n\\* +" menu-item) nil t)
1508 (error "No such item in menu"))
1509 (beginning-of-line)
1510 (forward-char 2)
1511 (Info-extract-menu-node-name)))))
a384cab3
JB
1512
1513;; If COUNT is nil, use the last item in the menu.
1514(defun Info-extract-menu-counting (count)
1bcedb3b
RS
1515 (let ((case-fold-search t))
1516 (save-excursion
760d5cb3
GM
1517 (let ((case-fold-search t))
1518 (goto-char (point-min))
1519 (or (search-forward "\n* menu:" nil t)
1520 (error "No menu in this node"))
1521 (if count
1522 (or (search-forward "\n* " nil t count)
1523 (error "Too few items in menu"))
1524 (while (search-forward "\n* " nil t)
1525 nil))
1526 (Info-extract-menu-node-name)))))
a384cab3 1527
e38e7367
RM
1528(defun Info-nth-menu-item ()
1529 "Go to the node of the Nth menu item.
1530N is the digit argument used to invoke this command."
a384cab3 1531 (interactive)
e38e7367
RM
1532 (Info-goto-node
1533 (Info-extract-menu-counting
1534 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
1535
1536(defun Info-top-node ()
1537 "Go to the Top node of this file."
1538 (interactive)
1539 (Info-goto-node "Top"))
1540
1541(defun Info-final-node ()
1542 "Go to the final node in this file."
1543 (interactive)
1544 (Info-goto-node "Top")
760d5cb3
GM
1545 (let ((Info-history nil)
1546 (case-fold-search t))
a384cab3
JB
1547 ;; Go to the last node in the menu of Top.
1548 (Info-goto-node (Info-extract-menu-counting nil))
1549 ;; If the last node in the menu is not last in pointer structure,
399c88ad 1550 ;; move forward until we can't go any farther.
a384cab3
JB
1551 (while (Info-forward-node t t) nil)
1552 ;; Then keep moving down to last subnode, unless we reach an index.
1553 (while (and (not (string-match "\\<index\\>" Info-current-node))
1554 (save-excursion (search-forward "\n* Menu:" nil t)))
1555 (Info-goto-node (Info-extract-menu-counting nil)))))
1556
1557(defun Info-forward-node (&optional not-down no-error)
1558 "Go forward one node, considering all nodes as forming one sequence."
1559 (interactive)
1560 (goto-char (point-min))
1561 (forward-line 1)
760d5cb3
GM
1562 (let ((case-fold-search t))
1563 ;; three possibilities, in order of priority:
1564 ;; 1. next node is in a menu in this node (but not in an index)
1565 ;; 2. next node is next at same level
1566 ;; 3. next node is up and next
1567 (cond ((and (not not-down)
1568 (save-excursion (search-forward "\n* menu:" nil t))
1569 (not (string-match "\\<index\\>" Info-current-node)))
1570 (Info-goto-node (Info-extract-menu-counting 1))
1571 t)
b24e84ab
EZ
1572 ((save-excursion
1573 (save-restriction
1574 (let (limit)
1575 (when Info-header-line
1576 (goto-char (point-min))
1577 (widen)
1578 (forward-line -1)
1579 (setq limit (point))
1580 (forward-line 1))
1581 (search-backward "next:" limit t))))
760d5cb3
GM
1582 (Info-next)
1583 t)
b24e84ab
EZ
1584 ((and (save-excursion
1585 (save-restriction
1586 (let (limit)
1587 (when Info-header-line
1588 (goto-char (point-min))
1589 (widen)
1590 (forward-line -1)
1591 (setq limit (point))
1592 (forward-line 1))
1593 (search-backward "up:" limit t))))
760d5cb3
GM
1594 ;; Use string-equal, not equal, to ignore text props.
1595 (not (string-equal (downcase (Info-extract-pointer "up"))
1596 "top")))
1597 (let ((old-node Info-current-node))
1598 (Info-up)
1599 (let (Info-history success)
1600 (unwind-protect
1601 (setq success (Info-forward-node t no-error))
1602 (or success (Info-goto-node old-node))))))
1603 (no-error nil)
1604 (t (error "No pointer forward from this node")))))
a384cab3
JB
1605
1606(defun Info-backward-node ()
1607 "Go backward one node, considering all nodes as forming one sequence."
1608 (interactive)
1609 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
760d5cb3
GM
1610 (upnode (Info-extract-pointer "up" t))
1611 (case-fold-search t))
a384cab3
JB
1612 (cond ((and upnode (string-match "(" upnode))
1613 (error "First node in file"))
1614 ((and upnode (or (null prevnode)
e90d1271
RS
1615 ;; Use string-equal, not equal,
1616 ;; to ignore text properties.
1617 (string-equal (downcase prevnode)
1618 (downcase upnode))))
a384cab3
JB
1619 (Info-up))
1620 (prevnode
1621 ;; If we move back at the same level,
1622 ;; go down to find the last subnode*.
1623 (Info-prev)
1624 (let (Info-history)
1625 (while (and (not (string-match "\\<index\\>" Info-current-node))
1626 (save-excursion (search-forward "\n* Menu:" nil t)))
1627 (Info-goto-node (Info-extract-menu-counting nil)))))
1628 (t
1629 (error "No pointer backward from this node")))))
1630
1631(defun Info-exit ()
1632 "Exit Info by selecting some other buffer."
1633 (interactive)
552775bd
RS
1634 (if Info-standalone
1635 (save-buffers-kill-emacs)
4643e92f 1636 (quit-window)))
a384cab3 1637
253db917
ER
1638(defun Info-next-menu-item ()
1639 (interactive)
fdf4b680 1640 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
1641 (let* ((case-fold-search t)
1642 (node
1643 (save-excursion
1644 (forward-line -1)
1645 (search-forward "\n* menu:" nil t)
1646 (and (search-forward "\n* " nil t)
1647 (Info-extract-menu-node-name)))))
fadfb77f
RS
1648 (if node (Info-goto-node node)
1649 (error "No more items in menu"))))
253db917
ER
1650
1651(defun Info-last-menu-item ()
1652 (interactive)
1653 (save-excursion
1654 (forward-line 1)
fdf4b680 1655 ;; Bind this in case the user sets it to nil.
760d5cb3
GM
1656 (let* ((case-fold-search t)
1657 (beg (save-excursion
1658 (and (search-backward "\n* menu:" nil t)
1659 (point)))))
0a56332b
RS
1660 (or (and beg (search-backward "\n* " beg t))
1661 (error "No previous items in menu")))
1662 (Info-goto-node (save-excursion
1663 (goto-char (match-end 0))
1664 (Info-extract-menu-node-name)))))
253db917 1665
552775bd 1666(defmacro Info-no-error (&rest body)
253db917
ER
1667 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
1668
1669(defun Info-next-preorder ()
3c9179ea
RS
1670 "Go to the next subnode or the next node, or go up a level."
1671 (interactive)
1672 (cond ((Info-no-error (Info-next-menu-item)))
1673 ((Info-no-error (Info-next)))
5dab2fb4 1674 ((Info-no-error (Info-up t))
ed690657
KH
1675 ;; Since we have already gone thru all the items in this menu,
1676 ;; go up to the end of this node.
b54d0f0e
RS
1677 (goto-char (point-max))
1678 ;; Since logically we are done with the node with that menu,
1679 ;; move on from it.
1680 (Info-next-preorder))
3c9179ea
RS
1681 (t
1682 (error "No more nodes"))))
253db917
ER
1683
1684(defun Info-last-preorder ()
1685 "Go to the last node, popping up a level if there is none."
1686 (interactive)
0a56332b
RS
1687 (cond ((Info-no-error
1688 (Info-last-menu-item)
1689 ;; If we go down a menu item, go to the end of the node
1690 ;; so we can scroll back through it.
ed690657 1691 (goto-char (point-max)))
b54d0f0e
RS
1692 ;; Keep going down, as long as there are nested menu nodes.
1693 (while (Info-no-error
1694 (Info-last-menu-item)
1695 ;; If we go down a menu item, go to the end of the node
1696 ;; so we can scroll back through it.
1697 (goto-char (point-max))))
ed690657 1698 (recenter -1))
c2def7a0
MB
1699 ((and (Info-no-error (Info-extract-pointer "prev"))
1700 (not (equal (Info-extract-pointer "up")
5dab2fb4
RS
1701 (Info-extract-pointer "prev"))))
1702 (Info-no-error (Info-prev))
ed690657 1703 (goto-char (point-max))
b54d0f0e
RS
1704 (while (Info-no-error
1705 (Info-last-menu-item)
1706 ;; If we go down a menu item, go to the end of the node
1707 ;; so we can scroll back through it.
1708 (goto-char (point-max))))
ed690657 1709 (recenter -1))
5dab2fb4 1710 ((Info-no-error (Info-up t))
ed690657 1711 (goto-char (point-min))
760d5cb3
GM
1712 (let ((case-fold-search t))
1713 (or (search-forward "\n* Menu:" nil t)
1714 (goto-char (point-max)))))
ed690657 1715 (t (error "No previous nodes"))))
253db917
ER
1716
1717(defun Info-scroll-up ()
3f32dc86 1718 "Scroll one screenful forward in Info, considering all nodes as one sequence.
280d11ed 1719Once you scroll far enough in a node that its menu appears on the screen
7a53d8c8
EZ
1720but after point, the next scroll moves into its first subnode, unless
1721`Info-scroll-prefer-subnodes' is nil.
280d11ed 1722
7a53d8c8
EZ
1723When you scroll past the end of a node, that goes to the next node if
1724`Info-scroll-prefer-subnodes' is non-nil and to the first subnode otherwise;
1725if this node has no successor, it moves to the parent node's successor,
1726and so on. If `Info-scroll-prefer-subnodes' is non-nil and point is inside
1727the menu of a node, it moves to subnode indicated by the following menu
1728item. (That case won't normally result from this command, but can happen
1729in other ways.)"
280d11ed 1730
253db917 1731 (interactive)
0a56332b
RS
1732 (if (or (< (window-start) (point-min))
1733 (> (window-start) (point-max)))
1734 (set-window-start (selected-window) (point)))
760d5cb3
GM
1735 (let* ((case-fold-search t)
1736 (virtual-end (save-excursion
1737 (goto-char (point-min))
7a53d8c8
EZ
1738 (if (and Info-scroll-prefer-subnodes
1739 (search-forward "\n* Menu:" nil t))
760d5cb3
GM
1740 (point)
1741 (point-max)))))
0a56332b
RS
1742 (if (or (< virtual-end (window-start))
1743 (pos-visible-in-window-p virtual-end))
7a53d8c8
EZ
1744 (cond
1745 (Info-scroll-prefer-subnodes (Info-next-preorder))
1746 ((Info-no-error (Info-goto-node (Info-extract-menu-counting 1))))
1747 (t (Info-next-preorder)))
0a56332b 1748 (scroll-up))))
253db917
ER
1749
1750(defun Info-scroll-down ()
3f32dc86 1751 "Scroll one screenful back in Info, considering all nodes as one sequence.
c2def7a0
MB
1752If point is within the menu of a node, and `Info-scroll-prefer-subnodes'
1753is non-nil, this goes to its last subnode. When you scroll past the
1754beginning of a node, that goes to the previous node or back up to the
1755parent node."
253db917 1756 (interactive)
0a56332b
RS
1757 (if (or (< (window-start) (point-min))
1758 (> (window-start) (point-max)))
1759 (set-window-start (selected-window) (point)))
760d5cb3
GM
1760 (let* ((case-fold-search t)
1761 (current-point (point))
c2def7a0
MB
1762 (virtual-end
1763 (and Info-scroll-prefer-subnodes
1764 (save-excursion
1765 (beginning-of-line)
1766 (setq current-point (point))
1767 (goto-char (point-min))
1768 (search-forward "\n* Menu:"
1769 current-point
1770 t)))))
5b3ebb44 1771 (if (or virtual-end
7afe24e3 1772 (pos-visible-in-window-p (point-min) nil t))
0a56332b
RS
1773 (Info-last-preorder)
1774 (scroll-down))))
253db917 1775
56cda6f5 1776(defun Info-next-reference (&optional recur)
552775bd
RS
1777 "Move cursor to the next cross-reference or menu item in the node."
1778 (interactive)
1779 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1bcedb3b
RS
1780 (old-pt (point))
1781 (case-fold-search t))
552775bd
RS
1782 (or (eobp) (forward-char 1))
1783 (or (re-search-forward pat nil t)
1784 (progn
1785 (goto-char (point-min))
1786 (or (re-search-forward pat nil t)
1787 (progn
1788 (goto-char old-pt)
1789 (error "No cross references in this node")))))
1790 (goto-char (match-beginning 0))
1791 (if (looking-at "\\* Menu:")
56cda6f5
RS
1792 (if recur
1793 (error "No cross references in this node")
1794 (Info-next-reference t)))))
552775bd 1795
56cda6f5 1796(defun Info-prev-reference (&optional recur)
552775bd
RS
1797 "Move cursor to the previous cross-reference or menu item in the node."
1798 (interactive)
1799 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1bcedb3b
RS
1800 (old-pt (point))
1801 (case-fold-search t))
552775bd
RS
1802 (or (re-search-backward pat nil t)
1803 (progn
1804 (goto-char (point-max))
1805 (or (re-search-backward pat nil t)
1806 (progn
1807 (goto-char old-pt)
1808 (error "No cross references in this node")))))
1809 (goto-char (match-beginning 0))
1810 (if (looking-at "\\* Menu:")
56cda6f5
RS
1811 (if recur
1812 (error "No cross references in this node")
1813 (Info-prev-reference t)))))
552775bd 1814
1143a6b0 1815(defun Info-index (topic)
fdf4b680 1816 "Look up a string TOPIC in the index for this file.
1143a6b0
ER
1817The index is defined as the first node in the top-level menu whose
1818name contains the word \"Index\", plus any immediately following
1819nodes whose names also contain the word \"Index\".
1820If there are no exact matches to the specified topic, this chooses
1821the first match which is a case-insensitive substring of a topic.
1822Use the `,' command to see the other matches.
1823Give a blank topic name to go to the Index node itself."
1824 (interactive "sIndex topic: ")
1825 (let ((orignode Info-current-node)
1826 (rnode nil)
b37daea4 1827 (pattern (format "\n\\* +\\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1143a6b0 1828 (regexp-quote topic)))
1bcedb3b
RS
1829 node
1830 (case-fold-search t))
1143a6b0
ER
1831 (Info-goto-node "Top")
1832 (or (search-forward "\n* menu:" nil t)
1833 (error "No index"))
1834 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1835 (error "No index"))
1836 (goto-char (match-beginning 1))
c696ac76
RS
1837 ;; Here, and subsequently in this function,
1838 ;; we bind Info-history to nil for internal node-switches
1839 ;; so that we don't put junk in the history.
1840 ;; In the first Info-goto-node call, above, we do update the history
1841 ;; because that is what the user's previous node choice into it.
1842 (let ((Info-history nil))
1143a6b0
ER
1843 (Info-goto-node (Info-extract-menu-node-name)))
1844 (or (equal topic "")
1845 (let ((matches nil)
1846 (exact nil)
c696ac76 1847 (Info-history nil)
1143a6b0
ER
1848 found)
1849 (while
1850 (progn
1851 (goto-char (point-min))
1852 (while (re-search-forward pattern nil t)
983dfbf8
SM
1853 (push (list (match-string-no-properties 1)
1854 (match-string-no-properties 2)
1855 Info-current-node
1856 (string-to-int (concat "0"
1857 (match-string 3))))
1858 matches))
1143a6b0
ER
1859 (and (setq node (Info-extract-pointer "next" t))
1860 (string-match "\\<Index\\>" node)))
1861 (Info-goto-node node))
1862 (or matches
1863 (progn
81e14cb2 1864 (Info-goto-node orignode)
920bdaab 1865 (error "No `%s' in index" topic)))
1143a6b0
ER
1866 ;; Here it is a feature that assoc is case-sensitive.
1867 (while (setq found (assoc topic matches))
1868 (setq exact (cons found exact)
1869 matches (delq found matches)))
1870 (setq Info-index-alternatives (nconc exact (nreverse matches)))
1871 (Info-index-next 0)))))
1872
1873(defun Info-index-next (num)
1874 "Go to the next matching index item from the last `i' command."
1875 (interactive "p")
1876 (or Info-index-alternatives
882e61bf 1877 (error "No previous `i' command"))
1143a6b0
ER
1878 (while (< num 0)
1879 (setq num (+ num (length Info-index-alternatives))))
1880 (while (> num 0)
1881 (setq Info-index-alternatives
1882 (nconc (cdr Info-index-alternatives)
1883 (list (car Info-index-alternatives)))
1884 num (1- num)))
1885 (Info-goto-node (nth 1 (car Info-index-alternatives)))
1886 (if (> (nth 3 (car Info-index-alternatives)) 0)
1887 (forward-line (nth 3 (car Info-index-alternatives)))
760d5cb3 1888 (forward-line 3) ; don't search in headers
1143a6b0 1889 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
1890 (Info-find-index-name name)))
1891 (message "Found `%s' in %s. %s"
1143a6b0
ER
1892 (car (car Info-index-alternatives))
1893 (nth 2 (car Info-index-alternatives))
1894 (if (cdr Info-index-alternatives)
1895 "(Press `,' for more)"
1896 "(Only match)")))
1897
920bdaab
RS
1898(defun Info-find-index-name (name)
1899 "Move point to the place within the current node where NAME is defined."
1bcedb3b
RS
1900 (let ((case-fold-search t))
1901 (if (or (re-search-forward (format
1902 "[a-zA-Z]+: %s\\( \\|$\\)"
1903 (regexp-quote name)) nil t)
1904 (search-forward (format "`%s'" name) nil t)
1905 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1906 (search-forward
1907 (format "`%s'" (substring name 0 (match-beginning 1)))
1908 nil t))
1909 (search-forward name nil t))
1910 (beginning-of-line)
1911 (goto-char (point-min)))))
920bdaab 1912
a384cab3
JB
1913(defun Info-undefined ()
1914 "Make command be undefined in Info."
1915 (interactive)
1916 (ding))
1917
1918(defun Info-help ()
1919 "Enter the Info tutorial."
1920 (interactive)
1921 (delete-other-windows)
1922 (Info-find-node "info"
1923 (if (< (window-height) 23)
1924 "Help-Small-Screen"
1925 "Help")))
1926
1927(defun Info-summary ()
1928 "Display a brief summary of all Info commands."
1929 (interactive)
1930 (save-window-excursion
1931 (switch-to-buffer "*Help*")
881c84c7 1932 (setq buffer-read-only nil)
a384cab3
JB
1933 (erase-buffer)
1934 (insert (documentation 'Info-mode))
9d29f94c 1935 (help-mode)
a384cab3
JB
1936 (goto-char (point-min))
1937 (let (ch flag)
1938 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1939 (message (if flag "Type Space to see more"
1940 "Type Space to return to Info"))
1614c867 1941 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 1942 (progn (setq unread-command-events (list ch)) nil)
a384cab3 1943 flag))
552775bd
RS
1944 (scroll-up)))
1945 (bury-buffer "*Help*")))
a384cab3
JB
1946\f
1947(defun Info-get-token (pos start all &optional errorstring)
fdf4b680 1948 "Return the token around POS.
a384cab3
JB
1949POS must be somewhere inside the token
1950START is a regular expression which will match the
1951 beginning of the tokens delimited string
1952ALL is a regular expression with a single
90a715f0 1953 parenthesized subpattern which is the token to be
fdf4b680 1954 returned. E.g. '{\(.*\)}' would return any string
a384cab3 1955 enclosed in braces around POS.
fdf4b680 1956ERRORSTRING optional fourth argument, controls action on no match
a384cab3
JB
1957 nil: return nil
1958 t: beep
1959 a string: signal an error, using that string."
1bcedb3b
RS
1960 (let ((case-fold-search t))
1961 (save-excursion
1962 (goto-char pos)
1963 ;; First look for a match for START that goes across POS.
1964 (while (and (not (bobp)) (> (point) (- pos (length start)))
1965 (not (looking-at start)))
1966 (forward-char -1))
1967 ;; If we did not find one, search back for START
1968 ;; (this finds only matches that end at or before POS).
1969 (or (looking-at start)
1970 (progn
1971 (goto-char pos)
1972 (re-search-backward start (max (point-min) (- pos 200)) 'yes)))
1973 (let (found)
1974 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1975 (not (setq found (and (<= (match-beginning 0) pos)
1976 (> (match-end 0) pos))))))
1977 (if (and found (<= (match-beginning 0) pos)
1978 (> (match-end 0) pos))
110d4e80 1979 (match-string-no-properties 1)
1bcedb3b
RS
1980 (cond ((null errorstring)
1981 nil)
1982 ((eq errorstring t)
1983 (beep)
1984 nil)
1985 (t
1986 (error "No %s around position %d" errorstring pos))))))))
a384cab3 1987
981947af 1988(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
1989 "\\<Info-mode-map>Follow a node reference near point.
1990Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 1991At end of the node's text, moves to the next node, or up if none."
f9969361 1992 (interactive "e")
d1268e52 1993 (mouse-set-point click)
981947af
KH
1994 (and (not (Info-try-follow-nearest-node))
1995 (save-excursion (forward-line 1) (eobp))
ed690657 1996 (Info-next-preorder)))
981947af
KH
1997
1998(defun Info-follow-nearest-node ()
1999 "\\<Info-mode-map>Follow a node reference near point.
2000Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
2001If no reference to follow, moves to the next node, or up if none."
2002 (interactive)
2003 (or (Info-try-follow-nearest-node)
ed690657 2004 (Info-next-preorder)))
981947af
KH
2005
2006;; Common subroutine.
2007(defun Info-try-follow-nearest-node ()
2008 "Follow a node reference near point. Return non-nil if successful."
a384cab3
JB
2009 (let (node)
2010 (cond
981947af
KH
2011 ((setq node (Info-get-token (point) "\\*note[ \n]"
2012 "\\*note[ \n]\\([^:]*\\):"))
a384cab3 2013 (Info-follow-reference node))
b37daea4 2014 ((setq node (Info-get-token (point) "\\* +" "\\* +\\([^:]*\\)::"))
a384cab3 2015 (Info-goto-node node))
631ba13e
RS
2016 ((Info-get-token (point) "\\* +" "\\* +\\([^:]*\\):")
2017 (beginning-of-line)
2018 (forward-char 2)
2019 (setq node (Info-extract-menu-node-name))
2020 (Info-goto-node node))
bc2ada62 2021 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
a384cab3 2022 (Info-goto-node node))
bc2ada62 2023 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
a384cab3 2024 (Info-goto-node node))
bc2ada62 2025 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
a384cab3 2026 (Info-goto-node "Top"))
bc2ada62 2027 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
981947af
KH
2028 (Info-goto-node node)))
2029 node))
a384cab3
JB
2030\f
2031(defvar Info-mode-map nil
2032 "Keymap containing Info commands.")
2033(if Info-mode-map
2034 nil
2035 (setq Info-mode-map (make-keymap))
2036 (suppress-keymap Info-mode-map)
2037 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 2038 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 2039 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd
RS
2040 (define-key Info-mode-map "\t" 'Info-next-reference)
2041 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
e38e7367
RM
2042 (define-key Info-mode-map "1" 'Info-nth-menu-item)
2043 (define-key Info-mode-map "2" 'Info-nth-menu-item)
2044 (define-key Info-mode-map "3" 'Info-nth-menu-item)
2045 (define-key Info-mode-map "4" 'Info-nth-menu-item)
2046 (define-key Info-mode-map "5" 'Info-nth-menu-item)
2047 (define-key Info-mode-map "6" 'Info-nth-menu-item)
2048 (define-key Info-mode-map "7" 'Info-nth-menu-item)
2049 (define-key Info-mode-map "8" 'Info-nth-menu-item)
2050 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 2051 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
2052 (define-key Info-mode-map "?" 'Info-summary)
2053 (define-key Info-mode-map "]" 'Info-forward-node)
2054 (define-key Info-mode-map "[" 'Info-backward-node)
2055 (define-key Info-mode-map "<" 'Info-top-node)
2056 (define-key Info-mode-map ">" 'Info-final-node)
2057 (define-key Info-mode-map "b" 'beginning-of-buffer)
2058 (define-key Info-mode-map "d" 'Info-directory)
2059 (define-key Info-mode-map "e" 'Info-edit)
2060 (define-key Info-mode-map "f" 'Info-follow-reference)
2061 (define-key Info-mode-map "g" 'Info-goto-node)
2062 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 2063 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
2064 (define-key Info-mode-map "l" 'Info-last)
2065 (define-key Info-mode-map "m" 'Info-menu)
2066 (define-key Info-mode-map "n" 'Info-next)
2067 (define-key Info-mode-map "p" 'Info-prev)
2068 (define-key Info-mode-map "q" 'Info-exit)
2069 (define-key Info-mode-map "s" 'Info-search)
47fc33ca
RS
2070 ;; For consistency with Rmail.
2071 (define-key Info-mode-map "\M-s" 'Info-search)
4fceda3c 2072 (define-key Info-mode-map "\M-n" 'clone-buffer)
db0c9809 2073 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 2074 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 2075 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 2076 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 2077 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
aea2a8da 2078 )
75a209d4
RS
2079
2080(defun Info-check-pointer (item)
fdf4b680 2081 "Non-nil if ITEM is present in this node."
75a209d4
RS
2082 (condition-case nil
2083 (Info-extract-pointer item)
2084 (error nil)))
2085
d4af987a
DL
2086(easy-menu-define
2087 Info-mode-menu Info-mode-map
2088 "Menu for info files."
2089 '("Info"
2090 ["Up" Info-up :active (Info-check-pointer "up")
2091 :help "Go up in the Info tree"]
2092 ["Next" Info-next :active (Info-check-pointer "next")
2093 :help "Go to the next node"]
2094 ["Previous" Info-prev :active (Info-check-pointer "prev[ious]*")
2095 :help "Go to the previous node"]
2096 ["Backward" Info-backward-node
2097 :help "Go backward one node, considering all as a sequence"]
2098 ["Forward" Info-forward-node
2099 :help "Go forward one node, considering all as a sequence"]
7210b6f5
DL
2100 ["Beginning" beginning-of-buffer
2101 :help "Go to beginning of this node"]
d4af987a
DL
2102 ["Top" Info-top-node
2103 :help "Go to top node of file"]
2104 ["Final Node" Info-final-node
2105 :help "Go to final node in this file"]
2106 ("Menu Item" ["You should never see this" report-emacs-bug t])
2107 ("Reference" ["You should never see this" report-emacs-bug t])
2108 ["Search..." Info-search
2109 :help "Search for regular expression in this Info file"]
d1268e52 2110 ["Go to Node..." Info-goto-node
d4af987a 2111 :help "Go to a named node"]
ec03f31e 2112 ["Last" Info-last :active Info-history
d4af987a
DL
2113 :help "Go to the last node you were at"]
2114 ("Index..."
2115 ["Lookup a String" Info-index
2116 :help "Look for a string in the index items"]
2117 ["Next Matching Item" Info-index-next
2118 :help "Look for another occurrence of previous item"])
7210b6f5
DL
2119 ["Edit" Info-edit :help "Edit contents of this node"
2120 :active Info-enable-edit]
126f5d4d
MB
2121 ["Copy Node Name" Info-copy-current-node-name
2122 :help "Copy the name of the current node into the kill ring"]
ec03f31e 2123 ["Exit" Info-exit :help "Stop reading Info"]))
75a209d4 2124
7210b6f5
DL
2125
2126(defvar info-tool-bar-map
2127 (if (display-graphic-p)
2128 (let ((tool-bar-map (make-sparse-keymap)))
2129 (tool-bar-add-item-from-menu 'Info-exit "close" Info-mode-map)
2130 (tool-bar-add-item-from-menu 'Info-prev "left_arrow" Info-mode-map)
2131 (tool-bar-add-item-from-menu 'Info-next "right_arrow" Info-mode-map)
2132 (tool-bar-add-item-from-menu 'Info-up "up_arrow" Info-mode-map)
ec03f31e 2133 (tool-bar-add-item-from-menu 'Info-last "undo" Info-mode-map)
7210b6f5
DL
2134 (tool-bar-add-item-from-menu 'Info-top-node "home" Info-mode-map)
2135 (tool-bar-add-item-from-menu 'Info-index "index" Info-mode-map)
2136 (tool-bar-add-item-from-menu 'Info-goto-node "jump_to" Info-mode-map)
2137 (tool-bar-add-item-from-menu 'Info-search "search" Info-mode-map)
2138 tool-bar-map)))
2139
75a209d4
RS
2140(defvar Info-menu-last-node nil)
2141;; Last node the menu was created for.
6d2c8e3e 2142;; Value is a list, (FILE-NAME NODE-NAME).
75a209d4
RS
2143
2144(defun Info-menu-update ()
fdf4b680 2145 "Update the Info menu for the current node."
75a209d4
RS
2146 (condition-case nil
2147 (if (or (not (eq major-mode 'Info-mode))
6d2c8e3e
RS
2148 (equal (list Info-current-file Info-current-node)
2149 Info-menu-last-node))
75a209d4
RS
2150 ()
2151 ;; Update menu menu.
2152 (let* ((Info-complete-menu-buffer (current-buffer))
2153 (items (nreverse (condition-case nil
2154 (Info-complete-menu-item
2155 "" (lambda (e) t) t)
2156 (error nil))))
399c88ad 2157 entries current
75a209d4
RS
2158 (number 0))
2159 (while (and items (< number 9))
2160 (setq current (car items)
2161 items (cdr items)
2162 number (1+ number))
399c88ad 2163 (setq entries (cons `[,current
75a209d4
RS
2164 (Info-menu ,current)
2165 :keys ,(format "%d" number)]
2166 entries)))
2167 (if items
2168 (setq entries (cons ["Other..." Info-menu t] entries)))
2169 (or entries
5d5845dc 2170 (setq entries (list ["No menu" nil nil] nil :active)))
d4af987a 2171 (easy-menu-change '("Info") "Menu Item" (nreverse entries)))
75a209d4
RS
2172 ;; Update reference menu. Code stolen from `Info-follow-reference'.
2173 (let ((items nil)
399c88ad 2174 str i entries current
1bcedb3b
RS
2175 (number 0)
2176 (case-fold-search t))
75a209d4
RS
2177 (save-excursion
2178 (goto-char (point-min))
2179 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
2180 (setq str (buffer-substring
2181 (match-beginning 1)
2182 (1- (point))))
2183 (setq i 0)
2184 (while (setq i (string-match "[ \n\t]+" str i))
2185 (setq str (concat (substring str 0 i) " "
2186 (substring str (match-end 0))))
2187 (setq i (1+ i)))
2188 (setq items
2189 (cons str items))))
2190 (while (and items (< number 9))
2191 (setq current (car items)
2192 items (cdr items)
2193 number (1+ number))
399c88ad 2194 (setq entries (cons `[,current
75a209d4
RS
2195 (Info-follow-reference ,current)
2196 t]
2197 entries)))
2198 (if items
2199 (setq entries (cons ["Other..." Info-follow-reference t]
2200 entries)))
2201 (or entries
5d5845dc 2202 (setq entries (list ["No references" nil nil] nil :active)))
75a209d4
RS
2203 (easy-menu-change '("Info") "Reference" (nreverse entries)))
2204 ;; Update last seen node.
6d2c8e3e 2205 (setq Info-menu-last-node (list Info-current-file Info-current-node)))
75a209d4
RS
2206 ;; Try to avoid entering infinite beep mode in case of errors.
2207 (error (ding))))
2208
a384cab3 2209\f
126f5d4d
MB
2210(defun Info-copy-current-node-name ()
2211 "Put the name of the current info node into the kill ring.
2212The name of the info file is prepended to the node name in parentheses."
2213 (interactive)
2214 (unless Info-current-node
2215 (error "No current info node"))
2216 (kill-new
2217 (concat "("
2218 (file-name-nondirectory
2219 (if (stringp Info-current-file)
2220 Info-current-file
2221 (or buffer-file-name "")))
2222 ")"
2223 Info-current-node)))
2224
2225\f
a384cab3 2226;; Info mode is suitable only for specially formatted data.
68bc119c 2227(put 'Info-mode 'mode-class 'special)
a384cab3
JB
2228
2229(defun Info-mode ()
fdf4b680 2230 "Info mode provides commands for browsing through the Info documentation tree.
a384cab3
JB
2231Documentation in Info is divided into \"nodes\", each of which discusses
2232one topic and contains references to other nodes which discuss related
2233topics. Info has commands to follow the references and show you other nodes.
2234
fdf4b680 2235\\<Info-mode-map>\
a384cab3 2236\\[Info-help] Invoke the Info tutorial.
6b136ab9 2237\\[Info-exit] Quit Info: reselect previously selected buffer.
a384cab3
JB
2238
2239Selecting other nodes:
21d5959f
RS
2240\\[Info-mouse-follow-nearest-node]
2241 Follow a node reference you click on.
2242 This works with menu items, cross references, and
2243 the \"next\", \"previous\" and \"up\", depending on where you click.
6b136ab9 2244\\[Info-follow-nearest-node] Follow a node reference near point, like \\[Info-mouse-follow-nearest-node].
a384cab3 2245\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 2246\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
2247\\[Info-up] Move \"up\" from this node.
2248\\[Info-menu] Pick menu item specified by name (or abbreviation).
2249 Picking a menu item causes another node to be selected.
aea2a8da 2250\\[Info-directory] Go to the Info directory node.
a384cab3
JB
2251\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
2252\\[Info-last] Move to the last node you were at.
1143a6b0
ER
2253\\[Info-index] Look up a topic in this file's Index and move to that node.
2254\\[Info-index-next] (comma) Move to the next match from a previous `i' command.
6b136ab9
DL
2255\\[Info-top-node] Go to the Top node of this file.
2256\\[Info-final-node] Go to the final node in this file.
2257\\[Info-backward-node] Go backward one node, considering all nodes as forming one sequence.
2258\\[Info-forward-node] Go forward one node, considering all nodes as forming one sequence.
a384cab3
JB
2259
2260Moving within a node:
177c3549
RS
2261\\[Info-scroll-up] Normally, scroll forward a full screen.
2262Once you scroll far enough in a node that its menu appears on the screen
2263but after point, the next scroll moves into its first subnode.
2264When after all menu items (or if their is no menu), move up to
2265the parent node.
2266\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
803eaf50 2267already visible, try to go to the previous menu entry, or up if there is none.
399c88ad 2268\\[beginning-of-buffer] Go to beginning of node.
a384cab3 2269
a384cab3
JB
2270Advanced commands:
2271\\[Info-exit] Quit Info: reselect previously selected buffer.
2272\\[Info-edit] Edit contents of selected node.
22731 Pick first item in node's menu.
22742, 3, 4, 5 Pick second ... fifth item in node's menu.
2275\\[Info-goto-node] Move to node specified by name.
2276 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 2277\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 2278\\[Info-search] Search through this Info file for specified regexp,
803eaf50 2279 and select the node in which the next occurrence is found.
552775bd
RS
2280\\[Info-next-reference] Move cursor to next cross-reference or menu item.
2281\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
2282 (kill-all-local-variables)
2283 (setq major-mode 'Info-mode)
2284 (setq mode-name "Info")
f73299f3 2285 (setq tab-width 8)
a384cab3 2286 (use-local-map Info-mode-map)
75a209d4 2287 (add-hook 'activate-menubar-hook 'Info-menu-update nil t)
a384cab3
JB
2288 (set-syntax-table text-mode-syntax-table)
2289 (setq local-abbrev-table text-mode-abbrev-table)
2290 (setq case-fold-search t)
2291 (setq buffer-read-only t)
a384cab3
JB
2292 (make-local-variable 'Info-current-file)
2293 (make-local-variable 'Info-current-subfile)
2294 (make-local-variable 'Info-current-node)
2295 (make-local-variable 'Info-tag-table-marker)
c5fe2ff1
RS
2296 (setq Info-tag-table-marker (make-marker))
2297 (make-local-variable 'Info-tag-table-buffer)
2298 (setq Info-tag-table-buffer nil)
a384cab3 2299 (make-local-variable 'Info-history)
1143a6b0 2300 (make-local-variable 'Info-index-alternatives)
7210b6f5 2301 (set (make-local-variable 'tool-bar-map) info-tool-bar-map)
93480d70
RS
2302 ;; This is for the sake of the invisible text we use handling titles.
2303 (make-local-variable 'line-move-ignore-invisible)
2304 (setq line-move-ignore-invisible t)
b7aeabeb 2305 (add-hook 'clone-buffer-hook 'Info-clone-buffer-hook nil t)
a384cab3
JB
2306 (Info-set-mode-line)
2307 (run-hooks 'Info-mode-hook))
2308
4fceda3c
SM
2309(defun Info-clone-buffer-hook ()
2310 (when (bufferp Info-tag-table-buffer)
2311 (setq Info-tag-table-buffer
2312 (with-current-buffer Info-tag-table-buffer (clone-buffer)))
2313 (let ((m Info-tag-table-marker))
2314 (when (and (markerp m) (marker-position m))
2315 (setq Info-tag-table-marker
2316 (with-current-buffer Info-tag-table-buffer
2317 (copy-marker (marker-position m))))))))
2318
7210b6f5
DL
2319(defvar Info-edit-map (let ((map (make-sparse-keymap)))
2320 (set-keymap-parent map text-mode-map)
2321 (define-key map "\C-c\C-c" 'Info-cease-edit)
2322 map)
a384cab3 2323 "Local keymap used within `e' command of Info.")
a384cab3
JB
2324
2325;; Info-edit mode is suitable only for specially formatted data.
68bc119c 2326(put 'Info-edit-mode 'mode-class 'special)
a384cab3
JB
2327
2328(defun Info-edit-mode ()
2329 "Major mode for editing the contents of an Info node.
a426b157 2330Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
2331which returns to Info mode for browsing.
2332\\{Info-edit-map}"
a384cab3
JB
2333 (use-local-map Info-edit-map)
2334 (setq major-mode 'Info-edit-mode)
2335 (setq mode-name "Info Edit")
2336 (kill-local-variable 'mode-line-buffer-identification)
2337 (setq buffer-read-only nil)
2c609f53 2338 (force-mode-line-update)
e82c28f9
KH
2339 (buffer-enable-undo (current-buffer))
2340 (run-hooks 'Info-edit-mode-hook))
2341
2342(defun Info-edit ()
2343 "Edit the contents of this Info node.
2344Allowed only if variable `Info-enable-edit' is non-nil."
2345 (interactive)
2346 (or Info-enable-edit
2347 (error "Editing info nodes is not enabled"))
2348 (Info-edit-mode)
8ab3e50b 2349 (message "%s" (substitute-command-keys
760d5cb3 2350 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
2351
2352(defun Info-cease-edit ()
2353 "Finish editing Info node; switch back to Info proper."
2354 (interactive)
2355 ;; Do this first, so nothing has changed if user C-g's at query.
2356 (and (buffer-modified-p)
2357 (y-or-n-p "Save the file? ")
2358 (save-buffer))
2359 (use-local-map Info-mode-map)
2360 (setq major-mode 'Info-mode)
2361 (setq mode-name "Info")
2362 (Info-set-mode-line)
2363 (setq buffer-read-only t)
2c609f53 2364 (force-mode-line-update)
a384cab3
JB
2365 (and (marker-position Info-tag-table-marker)
2366 (buffer-modified-p)
2367 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 2368\f
f88ab1c9 2369(defvar Info-file-list-for-emacs
560ea2d5 2370 '("ediff" "forms" "gnus" "info" ("mh" . "mh-e") "sc" "message"
af718538
EZ
2371 ("dired" . "dired-x") ("c" . "ccmode") "viper" "vip"
2372 ("skeleton" . "autotype") ("auto-insert" . "autotype")
2373 ("copyright" . "autotype") ("executable" . "autotype")
2374 ("time-stamp" . "autotype") ("quickurl" . "autotype")
2375 ("tempo" . "autotype") ("hippie-expand" . "autotype")
2376 ("cvs" . "pcl-cvs")
379b70e7 2377 "ebrowse" "eshell" "cl" "idlwave" "reftex" "speedbar" "widget" "woman")
f88ab1c9
RS
2378 "List of Info files that describe Emacs commands.
2379An element can be a file name, or a list of the form (PREFIX . FILE)
2380where PREFIX is a name prefix and FILE is the file to look in.
2381If the element is just a file name, the file name also serves as the prefix.")
2382
f0a8a3f1 2383(defun Info-find-emacs-command-nodes (command)
f88ab1c9 2384 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
2385The `info-file' property of COMMAND says which Info manual to search.
2386If COMMAND has no property, the variable `Info-file-list-for-emacs'
2387defines heuristics for which Info manual to try.
fdf4b680 2388The locations are of the format used in `Info-history', i.e.
f0a8a3f1 2389\(FILENAME NODENAME BUFFERPOS\)."
f0a8a3f1 2390 (let ((where '())
b37daea4 2391 (cmd-desc (concat "^\\* +" (regexp-quote (symbol-name command))
218c2cc7 2392 "\\( <[0-9]+>\\)?:\\s *\\(.*\\)\\.$"))
f88ab1c9
RS
2393 (info-file "emacs")) ;default
2394 ;; Determine which info file this command is documented in.
2395 (if (get command 'info-file)
2396 (setq info-file (get command 'info-file))
2397 ;; If it doesn't say explicitly, test its name against
2398 ;; various prefixes that we know.
2399 (let ((file-list Info-file-list-for-emacs))
2400 (while file-list
2401 (let* ((elt (car file-list))
2402 (name (if (consp elt)
2403 (car elt)
2404 elt))
2405 (file (if (consp elt) (cdr elt) elt))
f57b2cd8 2406 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
2407 "\\(\\'\\|-\\)")))
2408 (if (string-match regexp (symbol-name command))
2409 (setq info-file file file-list nil))
2410 (setq file-list (cdr file-list))))))
218c2cc7
EZ
2411 (Info-find-node info-file "Top")
2412 (or (and (search-forward "\n* menu:" nil t)
2413 (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t))
2414 (error "Info file `%s' appears to lack an index" info-file))
2415 (goto-char (match-beginning 1))
2416 ;; Bind Info-history to nil, to prevent the index nodes from
2417 ;; getting into the node history.
2418 (let ((Info-history nil)
2419 (exact nil)
2420 node found)
2421 (Info-goto-node (Info-extract-menu-node-name))
2422 (while
2423 (progn
2424 (goto-char (point-min))
2425 (while (re-search-forward cmd-desc nil t)
2426 (setq where
2427 (cons (list Info-current-file
2428 (match-string-no-properties 2)
760d5cb3
GM
2429 0)
2430 where)))
218c2cc7
EZ
2431 (and (setq node (Info-extract-pointer "next" t))
2432 (string-match "\\<Index\\>" node)))
2433 (Info-goto-node node)))
2434 where))
f0a8a3f1
RM
2435
2436;;;###autoload
2437(defun Info-goto-emacs-command-node (command)
e0568e86 2438 "Go to the Info node in the Emacs manual for command COMMAND.
30db89f9 2439The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
2440or in another manual found via COMMAND's `info-file' property or
2441the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
2442 (interactive "CFind documentation for command: ")
2443 (or (commandp command)
2444 (signal 'wrong-type-argument (list 'commandp command)))
2445 (let ((where (Info-find-emacs-command-nodes command)))
2446 (if where
2447 (let ((num-matches (length where)))
2448 ;; Get Info running, and pop to it in another window.
2449 (save-window-excursion
2450 (info))
c5fe2ff1
RS
2451 ;; FIXME It would be cool if this could use a buffer other
2452 ;; than *info*.
f0a8a3f1 2453 (pop-to-buffer "*info*")
218c2cc7
EZ
2454 ;; Bind Info-history to nil, to prevent the last Index node
2455 ;; visited by Info-find-emacs-command-nodes from being
2456 ;; pushed onto the history.
2457 (let ((Info-history nil))
2458 (Info-find-node (car (car where))
2459 (car (cdr (car where)))))
f0a8a3f1
RM
2460 (if (> num-matches 1)
2461 (progn
218c2cc7
EZ
2462 ;; (car where) will be pushed onto Info-history
2463 ;; when/if they go to another node. Put the other
2464 ;; nodes that were found on the history.
f0a8a3f1 2465 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 2466 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
2467 (1- num-matches)
2468 (if (> num-matches 2) "ies" "y")
8ab3e50b 2469 (substitute-command-keys "\\[Info-last]")
cedb118c 2470 (if (> num-matches 2) "them" "it")))))
e9b81433 2471 (error "Couldn't find documentation for %s" command))))
f0a8a3f1
RM
2472
2473;;;###autoload
2474(defun Info-goto-emacs-key-command-node (key)
30db89f9
EZ
2475 "Go to the node in the Emacs manual which describes the command bound to KEY.
2476KEY is a string.
fdf4b680 2477Interactively, if the binding is `execute-extended-command', a command is read.
30db89f9 2478The command is found by looking up in Emacs manual's indices
f57b2cd8
RS
2479or in another manual found via COMMAND's `info-file' property or
2480the variable `Info-file-list-for-emacs'."
ea33749b 2481 (interactive "kFind documentation for key: ")
f0a8a3f1
RM
2482 (let ((command (key-binding key)))
2483 (cond ((null command)
9e5c2f50 2484 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
2485 ((and (interactive-p)
2486 (eq command 'execute-extended-command))
2487 (Info-goto-emacs-command-node
2488 (read-command "Find documentation for command: ")))
2489 (t
2490 (Info-goto-emacs-command-node command)))))
552775bd 2491\f
bbed5c3f 2492(defface Info-title-1-face
b250866f 2493 '((((type tty pc) (class color)) (:foreground "yellow" :weight bold))
333cd59e 2494 (t (:height 1.2 :inherit Info-title-2-face)))
bbed5c3f
GM
2495 "Face for Info titles at level 1."
2496 :group 'info)
2497
2498(defface Info-title-2-face
b250866f 2499 '((((type tty pc) (class color)) (:foreground "lightblue" :weight bold))
333cd59e 2500 (t (:height 1.2 :inherit Info-title-3-face)))
bbed5c3f
GM
2501 "Face for Info titles at level 2."
2502 :group 'info)
2503
2504(defface Info-title-3-face
b250866f 2505 '((((type tty pc) (class color)) (:weight bold))
a8b883c2 2506 (t (:height 1.2 :inherit Info-title-4-face)))
bbed5c3f
GM
2507 "Face for Info titles at level 3."
2508 :group 'info)
2509
a8b883c2
MB
2510(defface Info-title-4-face
2511 '((((type tty pc) (class color)) (:weight bold))
2512 (t (:weight bold :inherit variable-pitch)))
2513 "Face for Info titles at level 4."
2514 :group 'info)
2515
973a3104
MB
2516(defface info-menu-header
2517 '((((type tty pc))
42121c23
SZ
2518 :underline t
2519 :weight bold)
973a3104 2520 (t
42121c23
SZ
2521 :inherit variable-pitch
2522 :weight bold))
973a3104
MB
2523 "Face for headers in Info menus."
2524 :group 'info)
2525
2526(defun Info-fontify-menu-headers ()
2527 "Add the face `info-menu-header' to any header before a menu entry."
2528 (save-excursion
2529 (goto-char (point-min))
2530 (when (re-search-forward "\\* Menu:" nil t)
2531 (put-text-property (match-beginning 0) (match-end 0)
2532 'face 'info-menu-header)
2533 (while (re-search-forward "\n\n\\([^*\n ].*\\)\n\n?[*]" nil t)
2534 (put-text-property (match-beginning 1) (match-end 1)
2535 'face 'info-menu-header)))))
2536
552775bd
RS
2537(defun Info-fontify-node ()
2538 (save-excursion
1bcedb3b
RS
2539 (let ((buffer-read-only nil)
2540 (case-fold-search t))
552775bd 2541 (goto-char (point-min))
35d2d241
RS
2542 (when (looking-at "^File: [^,: \t]+,?[ \t]+")
2543 (goto-char (match-end 0))
50ac70af 2544 (while (looking-at "[ \t]*\\([^:, \t\n]+\\):[ \t]+\\([^:,\t\n]+\\),?")
35d2d241 2545 (goto-char (match-end 0))
50ac70af
MB
2546 (let* ((nbeg (match-beginning 2))
2547 (nend (match-end 2))
2548 (tbeg (match-beginning 1))
2549 (tag (buffer-substring tbeg (match-end 1))))
2550 (if (string-equal tag "Node")
2551 (put-text-property nbeg nend 'face 'info-header-node)
2552 (put-text-property nbeg nend 'face 'info-header-xref)
2553 (put-text-property nbeg nend 'mouse-face 'highlight)
2554 (put-text-property tbeg nend
2555 'help-echo
d1268e52 2556 (concat "Go to node "
50ac70af
MB
2557 (buffer-substring nbeg nend)))
2558 (let ((fun (cdr (assoc tag '(("Prev" . Info-prev)
2559 ("Next" . Info-next)
2560 ("Up" . Info-up))))))
2561 (when fun
2562 (let ((keymap (make-sparse-keymap)))
2563 (define-key keymap [header-line mouse-1] fun)
2564 (define-key keymap [header-line mouse-2] fun)
2565 (put-text-property tbeg nend 'local-map keymap))))
2566 ))))
552775bd 2567 (goto-char (point-min))
a8b883c2 2568 (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\|\\.+\\)$"
760d5cb3 2569 nil t)
bbed5c3f
GM
2570 (let ((c (preceding-char))
2571 face)
2572 (cond ((= c ?*) (setq face 'Info-title-1-face))
2573 ((= c ?=) (setq face 'Info-title-2-face))
a8b883c2
MB
2574 ((= c ?-) (setq face 'Info-title-3-face))
2575 (t (setq face 'Info-title-4-face)))
bbed5c3f
GM
2576 (put-text-property (match-beginning 1) (match-end 1)
2577 'face face))
93480d70
RS
2578 ;; This is a serious problem for trying to handle multiple
2579 ;; frame types at once. We want this text to be invisible
2580 ;; on frames that can display the font above.
3828218c
GM
2581 (when (memq (framep (selected-frame)) '(x pc w32 mac))
2582 (add-text-properties (match-end 1) (match-end 2)
2583 '(invisible t intangible t))
2584 (add-text-properties (1- (match-end 1)) (match-end 2)
2585 '(intangible t))))
98d8273c 2586 (goto-char (point-min))
7757c476 2587 (while (re-search-forward "\\*Note[ \n\t]+\\([^:]*\\):" nil t)
552775bd
RS
2588 (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
2589 nil
d1268e52
DL
2590 (add-text-properties (match-beginning 1) (match-end 1)
2591 '(face info-xref
2592 mouse-face highlight
2593 help-echo "mouse-2: go to this node"))))
552775bd
RS
2594 (goto-char (point-min))
2595 (if (and (search-forward "\n* Menu:" nil t)
2596 (not (string-match "\\<Index\\>" Info-current-node))
2597 ;; Don't take time to annotate huge menus
34a0a4ee 2598 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
552775bd 2599 (let ((n 0))
b37daea4 2600 (while (re-search-forward "^\\* +\\([^:\t\n]*\\):" nil t)
552775bd 2601 (setq n (1+ n))
760d5cb3 2602 (if (memq n '(5 9)) ; visual aids to help with 1-9 keys
552775bd
RS
2603 (put-text-property (match-beginning 0)
2604 (1+ (match-beginning 0))
2605 'face 'info-menu-5))
f7902bdc
DL
2606 (add-text-properties (match-beginning 1) (match-end 1)
2607 '(face info-xref
2608 mouse-face highlight
2609 help-echo "mouse-2: go to this node")))))
973a3104 2610 (Info-fontify-menu-headers)
552775bd 2611 (set-buffer-modified-p nil))))
c5fe2ff1
RS
2612\f
2613
2614;; When an Info buffer is killed, make sure the associated tags buffer
2615;; is killed too.
2616(defun Info-kill-buffer ()
2617 (and (eq major-mode 'Info-mode)
2618 Info-tag-table-buffer
2619 (kill-buffer Info-tag-table-buffer)))
2620
2621(add-hook 'kill-buffer-hook 'Info-kill-buffer)
3cb6768f
EL
2622
2623;;; Speedbar support:
2624;; These functions permit speedbar to display the "tags" in the
2625;; current info node.
96ee3f29 2626(eval-when-compile (require 'speedbar))
3cb6768f 2627
96ee3f29
KH
2628(defvar Info-speedbar-key-map nil
2629 "Keymap used when in the info display mode.")
3cb6768f 2630
96ee3f29
KH
2631(defun Info-install-speedbar-variables ()
2632 "Install those variables used by speedbar to enhance Info."
2633 (if Info-speedbar-key-map
2634 nil
2635 (setq Info-speedbar-key-map (speedbar-make-specialized-keymap))
2636
2637 ;; Basic tree features
2638 (define-key Info-speedbar-key-map "e" 'speedbar-edit-line)
2639 (define-key Info-speedbar-key-map "\C-m" 'speedbar-edit-line)
2640 (define-key Info-speedbar-key-map "+" 'speedbar-expand-line)
2641 (define-key Info-speedbar-key-map "-" 'speedbar-contract-line)
2642 )
2643
2644 (speedbar-add-expansion-list '("Info" Info-speedbar-menu-items
2645 Info-speedbar-key-map
2646 Info-speedbar-hierarchy-buttons)))
3cb6768f
EL
2647
2648(defvar Info-speedbar-menu-items
96ee3f29
KH
2649 '(["Browse Node" speedbar-edit-line t]
2650 ["Expand Node" speedbar-expand-line
2651 (save-excursion (beginning-of-line)
2652 (looking-at "[0-9]+: *.\\+. "))]
2653 ["Contract Node" speedbar-contract-line
2654 (save-excursion (beginning-of-line)
2655 (looking-at "[0-9]+: *.-. "))]
2656 )
3cb6768f
EL
2657 "Additional menu-items to add to speedbar frame.")
2658
96ee3f29
KH
2659;; Make sure our special speedbar major mode is loaded
2660(if (featurep 'speedbar)
2661 (Info-install-speedbar-variables)
2662 (add-hook 'speedbar-load-hook 'Info-install-speedbar-variables))
2663
2664;;; Info hierarchy display method
2665;;;###autoload
2666(defun Info-speedbar-browser ()
2667 "Initialize speedbar to display an info node browser.
2668This will add a speedbar major display mode."
2669 (interactive)
2670 (require 'speedbar)
2671 ;; Make sure that speedbar is active
2672 (speedbar-frame-mode 1)
2673 ;; Now, throw us into Info mode on speedbar.
2674 (speedbar-change-initial-expansion-list "Info")
2675 )
2676
7210b6f5
DL
2677(eval-when-compile (defvar speedbar-attached-frame))
2678
96ee3f29
KH
2679(defun Info-speedbar-hierarchy-buttons (directory depth &optional node)
2680 "Display an Info directory hierarchy in speedbar.
2681DIRECTORY is the current directory in the attached frame.
2682DEPTH is the current indentation depth.
2683NODE is an optional argument that is used to represent the
2684specific node to expand."
2685 (if (and (not node)
2686 (save-excursion (goto-char (point-min))
1bcedb3b
RS
2687 (let ((case-fold-search t))
2688 (looking-at "Info Nodes:"))))
96ee3f29
KH
2689 ;; Update our "current node" maybe?
2690 nil
2691 ;; We cannot use the generic list code, that depends on all leaves
2692 ;; being known at creation time.
2693 (if (not node)
2694 (speedbar-with-writable (insert "Info Nodes:\n")))
5fdc7997
EL
2695 (let ((completions nil)
2696 (cf (selected-frame)))
2697 (select-frame speedbar-attached-frame)
2698 (save-window-excursion
2699 (setq completions
2700 (Info-speedbar-fetch-file-nodes (or node '"(dir)top"))))
2701 (select-frame cf)
96ee3f29
KH
2702 (if completions
2703 (speedbar-with-writable
2704 (while completions
2705 (speedbar-make-tag-line 'bracket ?+ 'Info-speedbar-expand-node
2706 (cdr (car completions))
2707 (car (car completions))
2708 'Info-speedbar-goto-node
2709 (cdr (car completions))
2710 'info-xref depth)
2711 (setq completions (cdr completions)))
2712 t)
2713 nil))))
399c88ad 2714
96ee3f29 2715(defun Info-speedbar-goto-node (text node indent)
d1268e52 2716 "When user clicks on TEXT, go to an info NODE.
96ee3f29 2717The INDENT level is ignored."
760d5cb3
GM
2718 (select-frame speedbar-attached-frame)
2719 (let* ((buff (or (get-buffer "*info*")
2720 (progn (info) (get-buffer "*info*"))))
2721 (bwin (get-buffer-window buff 0)))
2722 (if bwin
2723 (progn
2724 (select-window bwin)
2725 (raise-frame (window-frame bwin)))
2726 (if speedbar-power-click
2727 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
2728 (select-frame speedbar-attached-frame)
2729 (switch-to-buffer buff)))
2730 (let ((junk (string-match "^(\\([^)]+\\))\\([^.]+\\)$" node))
2731 (file (match-string 1 node))
2732 (node (match-string 2 node)))
2733 (Info-find-node file node)
2734 ;; If we do a find-node, and we were in info mode, restore
2735 ;; the old default method. Once we are in info mode, it makes
2736 ;; sense to return to whatever method the user was using before.
2737 (if (string= speedbar-initial-expansion-list-name "Info")
2738 (speedbar-change-initial-expansion-list
2739 speedbar-previously-used-expansion-list-name)))))
96ee3f29
KH
2740
2741(defun Info-speedbar-expand-node (text token indent)
2742 "Expand the node the user clicked on.
2743TEXT is the text of the button we clicked on, a + or - item.
2744TOKEN is data related to this node (NAME . FILE).
2745INDENT is the current indentation depth."
2746 (cond ((string-match "+" text) ;we have to expand this file
2747 (speedbar-change-expand-button-char ?-)
2748 (if (speedbar-with-writable
760d5cb3
GM
2749 (save-excursion
2750 (end-of-line) (forward-char 1)
2751 (Info-speedbar-hierarchy-buttons nil (1+ indent) token)))
96ee3f29
KH
2752 (speedbar-change-expand-button-char ?-)
2753 (speedbar-change-expand-button-char ??)))
2754 ((string-match "-" text) ;we have to contract this node
2755 (speedbar-change-expand-button-char ?+)
2756 (speedbar-delete-subblock indent))
2757 (t (error "Ooops... not sure what to do")))
2758 (speedbar-center-buffer-smartly))
2759
2760(defun Info-speedbar-fetch-file-nodes (nodespec)
2761 "Fetch the subnodes from the info NODESPEC.
2762NODESPEC is a string of the form: (file)node.
2763Optional THISFILE represends the filename of"
2764 (save-excursion
2765 ;; Set up a buffer we can use to fake-out Info.
2766 (set-buffer (get-buffer-create "*info-browse-tmp*"))
2767 (if (not (equal major-mode 'Info-mode))
2768 (Info-mode))
2769 ;; Get the node into this buffer
2770 (let ((junk (string-match "^(\\([^)]+\\))\\([^.]+\\)$" nodespec))
2771 (file (match-string 1 nodespec))
2772 (node (match-string 2 nodespec)))
2773 (Info-find-node file node))
2774 ;; Scan the created buffer
2775 (goto-char (point-min))
2776 (let ((completions nil)
1bcedb3b 2777 (case-fold-search t)
96ee3f29
KH
2778 (thisfile (progn (string-match "^(\\([^)]+\\))" nodespec)
2779 (match-string 1 nodespec))))
2780 ;; Always skip the first one...
2781 (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
2782 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
2783 (let ((name (match-string 1)))
2784 (if (looking-at " *\\(([^)]+)[^.\n]+\\)\\.")
2785 (setq name (cons name (match-string 1)))
2786 (if (looking-at " *\\(([^)]+)\\)\\.")
2787 (setq name (cons name (concat (match-string 1) "Top")))
2788 (if (looking-at " \\([^.]+\\).")
2789 (setq name
2790 (cons name (concat "(" thisfile ")" (match-string 1))))
2791 (setq name (cons name (concat "(" thisfile ")" name))))))
2792 (setq completions (cons name completions))))
2793 (nreverse completions))))
2794
2795;;; Info mode node listing
3cb6768f
EL
2796(defun Info-speedbar-buttons (buffer)
2797 "Create a speedbar display to help navigation in an Info file.
2798BUFFER is the buffer speedbar is requesting buttons for."
96ee3f29 2799 (if (save-excursion (goto-char (point-min))
1bcedb3b
RS
2800 (let ((case-fold-search t))
2801 (not (looking-at "Info Nodes:"))))
96ee3f29
KH
2802 (erase-buffer))
2803 (Info-speedbar-hierarchy-buttons nil 0)
2804 )
49116ac0 2805
48e5b471
DL
2806(dolist (mess '("^Node has no Previous$"
2807 "^No menu in this node$"
2808 "^Node has no Next$"
399c88ad
SS
2809 "^No cross-references in this node^"
2810 search-failed
48e5b471
DL
2811 "^No \".*\" in index$"))
2812 (add-to-list 'debug-ignored-errors mess))
2813
49116ac0
JB
2814(provide 'info)
2815
1a06eabd 2816;;; info.el ends here