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