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