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