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