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