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