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