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