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