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