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