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