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