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