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