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