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