]> code.delx.au - gnu-emacs/blob - lisp/info.el
*** empty log message ***
[gnu-emacs] / lisp / info.el
1 ;;; info.el --- info package for Emacs -- could use a "create node" feature.
2
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 (defvar Info-history nil
22 "List of info nodes user has visited.
23 Each element of list is a list (FILENAME NODENAME BUFFERPOS).")
24
25 (defvar Info-enable-edit nil
26 "Non-nil means the \\<info-mode-map>\\[Info-edit] command in Info can edit the current node.")
27
28 (defvar Info-enable-active-nodes t
29 "Non-nil allows Info to execute Lisp code associated with nodes.
30 The Lisp code is executed when the node is selected.")
31
32 (defvar Info-default-directory-list nil
33 "List of default directories to search for Info documentation files.
34 This value is used as the default for `Info-directory-list'. It is set
35 in paths.el.")
36
37 (defvar Info-directory-list nil
38 "List of directories to search for Info documentation files.
39 nil means not yet initialized. In this case, Info uses the environment
40 variable INFODIR to initialize it, or `Info-default-directory-list'
41 if there is no INFODIR variable in the environment.")
42
43 (defvar Info-current-file nil
44 "Info file that Info is now looking at, or nil.")
45
46 (defvar Info-current-subfile nil
47 "Info subfile that is actually in the *info* buffer now,
48 or nil if current info file is not split into subfiles.")
49
50 (defvar Info-current-node nil
51 "Name of node that Info is now looking at, or nil.")
52
53 (defvar Info-tag-table-marker (make-marker)
54 "Marker pointing at beginning of current Info file's tag table.
55 Marker points nowhere if file has no tag table.")
56
57 ;;;###autoload
58 (defun info (&optional file)
59 "Enter Info, the documentation browser.
60 Optional argument FILE specifies the file to examine;
61 the default is the top-level directory of Info.
62
63 In interactive use, a prefix argument directs this command
64 to read a file name from the minibuffer."
65 (interactive (if current-prefix-arg
66 (list (read-file-name "Info file name: " nil nil t))))
67 (or Info-directory-list
68 (setq Info-directory-list
69 (let ((path (getenv "INFOPATH")))
70 (if path
71 (let ((list nil)
72 idx)
73 (while (> (length path) 0)
74 (setq idx (or (string-match ":" path) (length path))
75 list (cons (substring path 0 idx) list)
76 path (substring path (min (1+ idx)
77 (length path)))))
78 (nreverse list))
79 Info-default-directory-list))))
80 (if file
81 (Info-goto-node (concat "(" file ")"))
82 (if (get-buffer "*info*")
83 (switch-to-buffer "*info*")
84 (Info-directory))))
85
86 ;; Go to an info node specified as separate filename and nodename.
87 ;; no-going-back is non-nil if recovering from an error in this function;
88 ;; it says do not attempt further (recursive) error recovery.
89 (defun Info-find-node (filename nodename &optional no-going-back)
90 ;; Convert filename to lower case if not found as specified.
91 ;; Expand it.
92 (if filename
93 (let (temp temp-downcase found)
94 (setq filename (substitute-in-file-name filename))
95 (let ((dirs (if (string-match "^\\./" filename)
96 ;; If specified name starts with `./'
97 ;; then just try current directory.
98 '("./")
99 Info-directory-list)))
100 ;; Search the directory list for file FILENAME.
101 (while (and dirs (not found))
102 (setq temp (expand-file-name filename (car dirs)))
103 (setq temp-downcase
104 (expand-file-name (downcase filename) (car dirs)))
105 ;; Try several variants of specified name.
106 ;; Try downcasing, appending `.info', or both.
107 (cond ((file-exists-p temp)
108 (setq found temp))
109 ((file-exists-p temp-downcase)
110 (setq found temp-downcase))
111 ((file-exists-p (concat temp ".info"))
112 (setq found (concat temp ".info")))
113 ((file-exists-p (concat temp-downcase ".info"))
114 (setq found (concat temp-downcase ".info"))))
115 (setq dirs (cdr dirs))))
116 (if found
117 (setq filename found)
118 (error "Info file %s does not exist" filename))))
119 ;; Record the node we are leaving.
120 (if (and Info-current-file (not no-going-back))
121 (setq Info-history
122 (cons (list Info-current-file Info-current-node (point))
123 Info-history)))
124 ;; Go into info buffer.
125 (switch-to-buffer "*info*")
126 (buffer-flush-undo (current-buffer))
127 (or (eq major-mode 'Info-mode)
128 (Info-mode))
129 (widen)
130 (setq Info-current-node nil)
131 (unwind-protect
132 (progn
133 ;; Switch files if necessary
134 (or (null filename)
135 (equal Info-current-file filename)
136 (let ((buffer-read-only nil))
137 (setq Info-current-file nil
138 Info-current-subfile nil
139 buffer-file-name nil)
140 (erase-buffer)
141 (insert-file-contents filename t)
142 (set-buffer-modified-p nil)
143 (setq default-directory (file-name-directory filename))
144 ;; See whether file has a tag table. Record the location if yes.
145 (set-marker Info-tag-table-marker nil)
146 (goto-char (point-max))
147 (forward-line -8)
148 (or (equal nodename "*")
149 (not (search-forward "\^_\nEnd tag table\n" nil t))
150 (let (pos)
151 ;; We have a tag table. Find its beginning.
152 ;; Is this an indirect file?
153 (search-backward "\nTag table:\n")
154 (setq pos (point))
155 (if (save-excursion
156 (forward-line 2)
157 (looking-at "(Indirect)\n"))
158 ;; It is indirect. Copy it to another buffer
159 ;; and record that the tag table is in that buffer.
160 (save-excursion
161 (let ((buf (current-buffer)))
162 (set-buffer (get-buffer-create " *info tag table*"))
163 (buffer-flush-undo (current-buffer))
164 (setq case-fold-search t)
165 (erase-buffer)
166 (insert-buffer-substring buf)
167 (set-marker Info-tag-table-marker
168 (match-end 0))))
169 (set-marker Info-tag-table-marker pos))))
170 (setq Info-current-file
171 (file-name-sans-versions buffer-file-name))))
172 (if (equal nodename "*")
173 (progn (setq Info-current-node nodename)
174 (Info-set-mode-line))
175 ;; Search file for a suitable node.
176 ;; First get advice from tag table if file has one.
177 ;; Also, if this is an indirect info file,
178 ;; read the proper subfile into this buffer.
179 (let ((guesspos (point-min)))
180 (if (marker-position Info-tag-table-marker)
181 (save-excursion
182 (set-buffer (marker-buffer Info-tag-table-marker))
183 (goto-char Info-tag-table-marker)
184 (if (search-forward (concat "Node: " nodename "\177") nil t)
185 (progn
186 (setq guesspos (read (current-buffer)))
187 ;; If this is an indirect file,
188 ;; determine which file really holds this node
189 ;; and read it in.
190 (if (not (eq (current-buffer) (get-buffer "*info*")))
191 (setq guesspos
192 (Info-read-subfile guesspos))))
193 (error "No such node: \"%s\"" nodename))))
194 (goto-char (max (point-min) (- guesspos 1000))))
195 ;; Now search from our advised position (or from beg of buffer)
196 ;; to find the actual node.
197 (let ((regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n]")))
198 (catch 'foo
199 (while (search-forward "\n\^_" nil t)
200 (forward-line 1)
201 (let ((beg (point)))
202 (forward-line 1)
203 (if (re-search-backward regexp beg t)
204 (throw 'foo t))))
205 (error "No such node: %s" nodename)))
206 (Info-select-node)))
207 ;; If we did not finish finding the specified node,
208 ;; go back to the previous one.
209 (or Info-current-node no-going-back
210 (let ((hist (car Info-history)))
211 (setq Info-history (cdr Info-history))
212 (Info-find-node (nth 0 hist) (nth 1 hist) t)
213 (goto-char (nth 2 hist)))))
214 (goto-char (point-min)))
215
216 (defun Info-read-subfile (nodepos)
217 (set-buffer (marker-buffer Info-tag-table-marker))
218 (goto-char (point-min))
219 (search-forward "\n\^_")
220 (let (lastfilepos
221 lastfilename)
222 (forward-line 2)
223 (catch 'foo
224 (while (not (looking-at "\^_"))
225 (if (not (eolp))
226 (let ((beg (point))
227 thisfilepos thisfilename)
228 (search-forward ": ")
229 (setq thisfilename (buffer-substring beg (- (point) 2)))
230 (setq thisfilepos (read (current-buffer)))
231 ;; read in version 19 stops at the end of number.
232 ;; Advance to the next line.
233 (forward-line 1)
234 (if (> thisfilepos nodepos)
235 (throw 'foo t))
236 (setq lastfilename thisfilename)
237 (setq lastfilepos thisfilepos))
238 (forward-line 1))))
239 (set-buffer (get-buffer "*info*"))
240 (or (equal Info-current-subfile lastfilename)
241 (let ((buffer-read-only nil))
242 (setq buffer-file-name nil)
243 (widen)
244 (erase-buffer)
245 (insert-file-contents lastfilename)
246 (set-buffer-modified-p nil)
247 (setq Info-current-subfile lastfilename)))
248 (goto-char (point-min))
249 (search-forward "\n\^_")
250 (+ (- nodepos lastfilepos) (point))))
251
252 ;; Select the info node that point is in.
253 (defun Info-select-node ()
254 (save-excursion
255 ;; Find beginning of node.
256 (search-backward "\n\^_")
257 (forward-line 2)
258 ;; Get nodename spelled as it is in the node.
259 (re-search-forward "Node:[ \t]*")
260 (setq Info-current-node
261 (buffer-substring (point)
262 (progn
263 (skip-chars-forward "^,\t\n")
264 (point))))
265 (Info-set-mode-line)
266 ;; Find the end of it, and narrow.
267 (beginning-of-line)
268 (let (active-expression)
269 (narrow-to-region (point)
270 (if (re-search-forward "\n[\^_\f]" nil t)
271 (prog1
272 (1- (point))
273 (if (looking-at "[\n\^_\f]*execute: ")
274 (progn
275 (goto-char (match-end 0))
276 (setq active-expression
277 (read (current-buffer))))))
278 (point-max)))
279 (if Info-enable-active-nodes (eval active-expression)))))
280
281 (defun Info-set-mode-line ()
282 (setq mode-line-buffer-identification
283 (concat
284 "Info: ("
285 (if Info-current-file
286 (file-name-nondirectory Info-current-file)
287 "")
288 ")"
289 (or Info-current-node ""))))
290 \f
291 ;; Go to an info node specified with a filename-and-nodename string
292 ;; of the sort that is found in pointers in nodes.
293
294 (defun Info-goto-node (nodename)
295 "Go to info node named NAME. Give just NODENAME or (FILENAME)NODENAME."
296 (interactive "sGoto node: ")
297 (let (filename)
298 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
299 nodename)
300 (setq filename (if (= (match-beginning 1) (match-end 1))
301 ""
302 (substring nodename (match-beginning 2) (match-end 2)))
303 nodename (substring nodename (match-beginning 3) (match-end 3)))
304 (let ((trim (string-match "\\s *\\'" filename)))
305 (if trim (setq filename (substring filename 0 trim))))
306 (let ((trim (string-match "\\s *\\'" nodename)))
307 (if trim (setq nodename (substring nodename 0 trim))))
308 (Info-find-node (if (equal filename "") nil filename)
309 (if (equal nodename "") "Top" nodename))))
310 \f
311 (defun Info-restore-point (hl)
312 "If this node has been visited, restore the point value when we left."
313 (if hl
314 (if (and (equal (nth 0 (car hl)) Info-current-file)
315 (equal (nth 1 (car hl)) Info-current-node))
316 (goto-char (nth 2 (car hl)))
317 (Info-restore-point (cdr hl)))))
318 \f
319 (defvar Info-last-search nil
320 "Default regexp for \\<info-mode-map>\\[Info-search] command to search for.")
321
322 (defun Info-search (regexp)
323 "Search for REGEXP, starting from point, and select node it's found in."
324 (interactive "sSearch (regexp): ")
325 (if (equal regexp "")
326 (setq regexp Info-last-search)
327 (setq Info-last-search regexp))
328 (let ((found ()) current
329 (onode Info-current-node)
330 (ofile Info-current-file)
331 (opoint (point))
332 (osubfile Info-current-subfile))
333 (save-excursion
334 (save-restriction
335 (widen)
336 (if (null Info-current-subfile)
337 (progn (re-search-forward regexp) (setq found (point)))
338 (condition-case err
339 (progn (re-search-forward regexp) (setq found (point)))
340 (search-failed nil)))))
341 (if (not found) ;can only happen in subfile case -- else would have erred
342 (unwind-protect
343 (let ((list ()))
344 (set-buffer (marker-buffer Info-tag-table-marker))
345 (goto-char (point-min))
346 (search-forward "\n\^_\nIndirect:")
347 (save-restriction
348 (narrow-to-region (point)
349 (progn (search-forward "\n\^_")
350 (1- (point))))
351 (goto-char (point-min))
352 (search-forward (concat "\n" osubfile ": "))
353 (beginning-of-line)
354 (while (not (eobp))
355 (re-search-forward "\\(^.*\\): [0-9]+$")
356 (goto-char (+ (match-end 1) 2))
357 (setq list (cons (cons (read (current-buffer))
358 (buffer-substring (match-beginning 1)
359 (match-end 1)))
360 list))
361 (goto-char (1+ (match-end 0))))
362 (setq list (nreverse list)
363 current (car (car list))
364 list (cdr list)))
365 (while list
366 (message "Searching subfile %s..." (cdr (car list)))
367 (Info-read-subfile (car (car list)))
368 (setq list (cdr list))
369 (goto-char (point-min))
370 (if (re-search-forward regexp nil t)
371 (setq found (point) list ())))
372 (if found
373 (message "")
374 (signal 'search-failed (list regexp))))
375 (if (not found)
376 (progn (Info-read-subfile opoint)
377 (goto-char opoint)
378 (Info-select-node)))))
379 (widen)
380 (goto-char found)
381 (Info-select-node)
382 (or (and (equal onode Info-current-node)
383 (equal ofile Info-current-file))
384 (setq Info-history (cons (list ofile onode opoint)
385 Info-history)))))
386 \f
387 ;; Extract the value of the node-pointer named NAME.
388 ;; If there is none, use ERRORNAME in the error message;
389 ;; if ERRORNAME is nil, just return nil.
390 (defun Info-extract-pointer (name &optional errorname)
391 (save-excursion
392 (goto-char (point-min))
393 (forward-line 1)
394 (if (re-search-backward (concat name ":") nil t)
395 (progn
396 (goto-char (match-end 0))
397 (Info-following-node-name))
398 (if (eq errorname t)
399 nil
400 (error (concat "Node has no " (capitalize (or errorname name))))))))
401
402 (defun Info-following-node-name (&optional allowedchars)
403 (skip-chars-forward " \t")
404 (buffer-substring
405 (point)
406 (progn
407 (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
408 (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
409 (if (looking-at "(")
410 (skip-chars-forward "^)")))
411 (skip-chars-backward " ")
412 (point))))
413
414 (defun Info-next ()
415 "Go to the next node of this node."
416 (interactive)
417 (Info-goto-node (Info-extract-pointer "next")))
418
419 (defun Info-prev ()
420 "Go to the previous node of this node."
421 (interactive)
422 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
423
424 (defun Info-up ()
425 "Go to the superior node of this node."
426 (interactive)
427 (Info-goto-node (Info-extract-pointer "up"))
428 (Info-restore-point Info-history))
429
430 (defun Info-last ()
431 "Go back to the last node visited."
432 (interactive)
433 (or Info-history
434 (error "This is the first Info node you looked at"))
435 (let (filename nodename opoint)
436 (setq filename (car (car Info-history)))
437 (setq nodename (car (cdr (car Info-history))))
438 (setq opoint (car (cdr (cdr (car Info-history)))))
439 (setq Info-history (cdr Info-history))
440 (Info-find-node filename nodename)
441 (setq Info-history (cdr Info-history))
442 (goto-char opoint)))
443
444 (defun Info-directory ()
445 "Go to the Info directory node."
446 (interactive)
447 (Info-find-node "dir" "top"))
448 \f
449 (defun Info-follow-reference (footnotename)
450 "Follow cross reference named NAME to the node it refers to.
451 NAME may be an abbreviation of the reference name."
452 (interactive
453 (let ((completion-ignore-case t)
454 completions default (start-point (point)) str i)
455 (save-excursion
456 (goto-char (point-min))
457 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
458 (setq str (buffer-substring
459 (match-beginning 1)
460 (1- (point))))
461 ;; See if this one should be the default.
462 (and (null default)
463 (< (match-beginning 0) start-point)
464 (<= start-point (point))
465 (setq default t))
466 (setq i 0)
467 (while (setq i (string-match "[ \n\t]+" str i))
468 (setq str (concat (substring str 0 i) " "
469 (substring str (match-end 0))))
470 (setq i (1+ i)))
471 ;; Record as a completion and perhaps as default.
472 (if (eq default t) (setq default str))
473 (setq completions
474 (cons (cons str nil)
475 completions))))
476 (if completions
477 (list (completing-read (if default
478 (concat "Follow reference named: ("
479 default ") ")
480 "Follow reference named: ")
481 completions default t))
482 (error "No cross-references in this node"))))
483 (let (target beg i (str (concat "\\*note " footnotename)))
484 (while (setq i (string-match " " str i))
485 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
486 (setq i (+ i 6)))
487 (save-excursion
488 (goto-char (point-min))
489 (or (re-search-forward str nil t)
490 (error "No cross-reference named %s" footnotename))
491 (goto-char (+ (match-beginning 0) 5))
492 (setq target
493 (Info-extract-menu-node-name "Bad format cross reference" t)))
494 (while (setq i (string-match "[ \t\n]+" target i))
495 (setq target (concat (substring target 0 i) " "
496 (substring target (match-end 0))))
497 (setq i (+ i 1)))
498 (Info-goto-node target)))
499
500 (defun Info-extract-menu-node-name (&optional errmessage multi-line)
501 (skip-chars-forward " \t\n")
502 (let ((beg (point))
503 str i)
504 (skip-chars-forward "^:")
505 (forward-char 1)
506 (setq str
507 (if (looking-at ":")
508 (buffer-substring beg (1- (point)))
509 (skip-chars-forward " \t\n")
510 (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
511 (while (setq i (string-match "\n" str i))
512 (aset str i ?\ ))
513 str))
514
515 (defun Info-menu-item-sequence (list)
516 (while list
517 (Info-menu-item (car list))
518 (setq list (cdr list))))
519
520 (defun Info-menu (menu-item)
521 "Go to node for menu item named (or abbreviated) NAME.
522 Completion is allowed, and the menu item point is on is the default."
523 (interactive
524 (let ((completions '())
525 ;; If point is within a menu item, use that item as the default
526 (default nil)
527 (p (point))
528 (last nil))
529 (save-excursion
530 (goto-char (point-min))
531 (if (not (search-forward "\n* menu:" nil t))
532 (error "No menu in this node"))
533 (while (re-search-forward
534 "\n\\* \\([^:\t\n]*\\):" nil t)
535 (if (and (null default)
536 (prog1 (if last (< last p) nil)
537 (setq last (match-beginning 0)))
538 (<= p last))
539 (setq default (car (car completions))))
540 (setq completions (cons (cons (buffer-substring
541 (match-beginning 1)
542 (match-end 1))
543 (match-beginning 1))
544 completions)))
545 (if (and (null default) last
546 (< last p)
547 (<= p (progn (end-of-line) (point))))
548 (setq default (car (car completions)))))
549 (let ((item nil))
550 (while (null item)
551 (setq item (let ((completion-ignore-case t))
552 (completing-read (if default
553 (format "Menu item (default %s): "
554 default)
555 "Menu item: ")
556 completions nil t)))
557 ;; we rely on the fact that completing-read accepts an input
558 ;; of "" even when the require-match argument is true and ""
559 ;; is not a valid possibility
560 (if (string= item "")
561 (if default
562 (setq item default)
563 ;; ask again
564 (setq item nil))))
565 (list item))))
566 ;; there is a problem here in that if several menu items have the same
567 ;; name you can only go to the node of the first with this command.
568 (Info-goto-node (Info-extract-menu-item menu-item)))
569
570 (defun Info-extract-menu-item (menu-item)
571 (setq menu-item (regexp-quote menu-item))
572 (save-excursion
573 (goto-char (point-min))
574 (or (search-forward "\n* menu:" nil t)
575 (error "No menu in this node"))
576 (or (re-search-forward (concat "\n* " menu-item ":") nil t)
577 (re-search-forward (concat "\n* " menu-item) nil t)
578 (error "No such item in menu"))
579 (beginning-of-line)
580 (forward-char 2)
581 (Info-extract-menu-node-name)))
582
583 ;; If COUNT is nil, use the last item in the menu.
584 (defun Info-extract-menu-counting (count)
585 (save-excursion
586 (goto-char (point-min))
587 (or (search-forward "\n* menu:" nil t)
588 (error "No menu in this node"))
589 (if count
590 (or (search-forward "\n* " nil t count)
591 (error "Too few items in menu"))
592 (while (search-forward "\n* " nil t)
593 nil))
594 (Info-extract-menu-node-name)))
595
596 (defun Info-first-menu-item ()
597 "Go to the node of the first menu item."
598 (interactive)
599 (Info-goto-node (Info-extract-menu-counting 1)))
600
601 (defun Info-second-menu-item ()
602 "Go to the node of the second menu item."
603 (interactive)
604 (Info-goto-node (Info-extract-menu-counting 2)))
605
606 (defun Info-third-menu-item ()
607 "Go to the node of the third menu item."
608 (interactive)
609 (Info-goto-node (Info-extract-menu-counting 3)))
610
611 (defun Info-fourth-menu-item ()
612 "Go to the node of the fourth menu item."
613 (interactive)
614 (Info-goto-node (Info-extract-menu-counting 4)))
615
616 (defun Info-fifth-menu-item ()
617 "Go to the node of the fifth menu item."
618 (interactive)
619 (Info-goto-node (Info-extract-menu-counting 5)))
620
621 (defun Info-top-node ()
622 "Go to the Top node of this file."
623 (interactive)
624 (Info-goto-node "Top"))
625
626 (defun Info-final-node ()
627 "Go to the final node in this file."
628 (interactive)
629 (Info-goto-node "Top")
630 (let (Info-history)
631 ;; Go to the last node in the menu of Top.
632 (Info-goto-node (Info-extract-menu-counting nil))
633 ;; If the last node in the menu is not last in pointer structure,
634 ;; move forward until we can't go any farther.
635 (while (Info-forward-node t t) nil)
636 ;; Then keep moving down to last subnode, unless we reach an index.
637 (while (and (not (string-match "\\<index\\>" Info-current-node))
638 (save-excursion (search-forward "\n* Menu:" nil t)))
639 (Info-goto-node (Info-extract-menu-counting nil)))))
640
641 (defun Info-forward-node (&optional not-down no-error)
642 "Go forward one node, considering all nodes as forming one sequence."
643 (interactive)
644 (goto-char (point-min))
645 (forward-line 1)
646 ;; three possibilities, in order of priority:
647 ;; 1. next node is in a menu in this node (but not in an index)
648 ;; 2. next node is next at same level
649 ;; 3. next node is up and next
650 (cond ((and (not not-down)
651 (save-excursion (search-forward "\n* menu:" nil t))
652 (not (string-match "\\<index\\>" Info-current-node)))
653 (Info-first-menu-item)
654 t)
655 ((save-excursion (search-backward "next:" nil t))
656 (Info-next)
657 t)
658 ((and (save-excursion (search-backward "up:" nil t))
659 (not (equal (downcase (Info-extract-pointer "up")) "top")))
660 (let ((old-node Info-current-node))
661 (Info-up)
662 (let (Info-history success)
663 (unwind-protect
664 (setq success (Info-forward-node t no-error))
665 (or success (Info-goto-node old-node))))))
666 (no-error nil)
667 (t (error "No pointer forward from this node"))))
668
669 (defun Info-backward-node ()
670 "Go backward one node, considering all nodes as forming one sequence."
671 (interactive)
672 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
673 (upnode (Info-extract-pointer "up" t)))
674 (cond ((and upnode (string-match "(" upnode))
675 (error "First node in file"))
676 ((and upnode (or (null prevnode)
677 (equal (downcase prevnode) (downcase upnode))))
678 (Info-up))
679 (prevnode
680 ;; If we move back at the same level,
681 ;; go down to find the last subnode*.
682 (Info-prev)
683 (let (Info-history)
684 (while (and (not (string-match "\\<index\\>" Info-current-node))
685 (save-excursion (search-forward "\n* Menu:" nil t)))
686 (Info-goto-node (Info-extract-menu-counting nil)))))
687 (t
688 (error "No pointer backward from this node")))))
689
690 (defun Info-exit ()
691 "Exit Info by selecting some other buffer."
692 (interactive)
693 (switch-to-buffer (prog1 (other-buffer (current-buffer))
694 (bury-buffer (current-buffer)))))
695
696 (defun Info-undefined ()
697 "Make command be undefined in Info."
698 (interactive)
699 (ding))
700
701 (defun Info-help ()
702 "Enter the Info tutorial."
703 (interactive)
704 (delete-other-windows)
705 (Info-find-node "info"
706 (if (< (window-height) 23)
707 "Help-Small-Screen"
708 "Help")))
709
710 (defun Info-summary ()
711 "Display a brief summary of all Info commands."
712 (interactive)
713 (save-window-excursion
714 (switch-to-buffer "*Help*")
715 (erase-buffer)
716 (insert (documentation 'Info-mode))
717 (goto-char (point-min))
718 (let (ch flag)
719 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
720 (message (if flag "Type Space to see more"
721 "Type Space to return to Info"))
722 (if (/= ?\ (setq ch (read-char)))
723 (progn (setq unread-command-char ch) nil)
724 flag))
725 (scroll-up)))))
726 \f
727 (defun Info-get-token (pos start all &optional errorstring)
728 "Return the token around POS,
729 POS must be somewhere inside the token
730 START is a regular expression which will match the
731 beginning of the tokens delimited string
732 ALL is a regular expression with a single
733 parenthized subpattern which is the token to be
734 returned. E.g. '{\(.*\)}' would return any string
735 enclosed in braces around POS.
736 SIG optional fourth argument, controls action on no match
737 nil: return nil
738 t: beep
739 a string: signal an error, using that string."
740 (save-excursion
741 (goto-char pos)
742 (re-search-backward start (max (point-min) (- pos 200)) 'yes)
743 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
744 (not (and (<= (match-beginning 0) pos)
745 (> (match-end 0) pos)))))
746 (if (and (<= (match-beginning 0) pos)
747 (> (match-end 0) pos))
748 (buffer-substring (match-beginning 1) (match-end 1))
749 (cond ((null errorstring)
750 nil)
751 ((eq errorstring t)
752 (beep)
753 nil)
754 (t
755 (error "No %s around position %d" errorstring pos))))))
756
757 (defun Info-follow-nearest-node (click)
758 "\\<Info-mode-map>Follow a node reference near point. Like \\[Info-menu], \\Info-follow-reference], \\[Info-next], \\[Info-previous] or \\Info-up] command.
759 At end of the node's text, moves to the next node."
760 (interactive "K")
761 (let* ((relative-coordinates (coordinates-in-window-p (mouse-coords click)
762 (selected-window)))
763 (rel-x (car relative-coordinates))
764 (rel-y (cdr relative-coordinates)))
765 (move-to-window-line rel-y)
766 (move-to-column rel-x))
767 (let (node)
768 (cond
769 ((setq node (Info-get-token (point) "\\*note " "\\*note \\([^:]*\\):" t))
770 (Info-follow-reference node))
771 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::" t))
772 (Info-goto-node node))
773 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):" t))
774 (Info-menu node))
775 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)" t))
776 (Info-goto-node node))
777 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)" t))
778 (Info-goto-node node))
779 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)" t))
780 (Info-goto-node "Top"))
781 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)" t))
782 (Info-goto-node node))
783 ((save-excursion (forward-line 1) (eobp))
784 (Info-next)))
785 ))
786 \f
787 (defvar Info-mode-map nil
788 "Keymap containing Info commands.")
789 (if Info-mode-map
790 nil
791 (setq Info-mode-map (make-keymap))
792 (suppress-keymap Info-mode-map)
793 (define-key Info-mode-map "." 'beginning-of-buffer)
794 (define-key Info-mode-map " " 'scroll-up)
795 (define-key Info-mode-map "1" 'Info-first-menu-item)
796 (define-key Info-mode-map "2" 'Info-second-menu-item)
797 (define-key Info-mode-map "3" 'Info-third-menu-item)
798 (define-key Info-mode-map "4" 'Info-fourth-menu-item)
799 (define-key Info-mode-map "5" 'Info-fifth-menu-item)
800 (define-key Info-mode-map "6" 'undefined)
801 (define-key Info-mode-map "7" 'undefined)
802 (define-key Info-mode-map "8" 'undefined)
803 (define-key Info-mode-map "9" 'undefined)
804 (define-key Info-mode-map "0" 'undefined)
805 (define-key Info-mode-map "?" 'Info-summary)
806 (define-key Info-mode-map "]" 'Info-forward-node)
807 (define-key Info-mode-map "[" 'Info-backward-node)
808 (define-key Info-mode-map "<" 'Info-top-node)
809 (define-key Info-mode-map ">" 'Info-final-node)
810 (define-key Info-mode-map "b" 'beginning-of-buffer)
811 (define-key Info-mode-map "d" 'Info-directory)
812 (define-key Info-mode-map "e" 'Info-edit)
813 (define-key Info-mode-map "f" 'Info-follow-reference)
814 (define-key Info-mode-map "g" 'Info-goto-node)
815 (define-key Info-mode-map "h" 'Info-help)
816 (define-key Info-mode-map "l" 'Info-last)
817 (define-key Info-mode-map "m" 'Info-menu)
818 (define-key Info-mode-map "n" 'Info-next)
819 (define-key Info-mode-map "p" 'Info-prev)
820 (define-key Info-mode-map "q" 'Info-exit)
821 (define-key Info-mode-map "s" 'Info-search)
822 (define-key Info-mode-map "u" 'Info-up)
823 (define-key Info-mode-map "\177" 'scroll-down)
824 )
825 \f
826 ;; Info mode is suitable only for specially formatted data.
827 (put 'info-mode 'mode-class 'special)
828
829 (defun Info-mode ()
830 "\\<Info-mode-map>
831 Info mode provides commands for browsing through the Info documentation tree.
832 Documentation in Info is divided into \"nodes\", each of which discusses
833 one topic and contains references to other nodes which discuss related
834 topics. Info has commands to follow the references and show you other nodes.
835
836 \\[Info-help] Invoke the Info tutorial.
837
838 Selecting other nodes:
839 \\[Info-next] Move to the \"next\" node of this node.
840 \\[Info-previous] Move to the \"previous\" node of this node.
841 \\[Info-up] Move \"up\" from this node.
842 \\[Info-menu] Pick menu item specified by name (or abbreviation).
843 Picking a menu item causes another node to be selected.
844 \\[Info-directory] Go to the Info directory node.
845 \\[Info-follow-reference] Follow a cross reference. Reads name of reference.
846 \\[Info-last] Move to the last node you were at.
847
848 Moving within a node:
849 \\[scroll-up] scroll forward a full screen. \\[scroll-down] scroll backward.
850 \\[beginning-of-buffer] Go to beginning of node.
851
852 Advanced commands:
853 \\[Info-exit] Quit Info: reselect previously selected buffer.
854 \\[Info-edit] Edit contents of selected node.
855 1 Pick first item in node's menu.
856 2, 3, 4, 5 Pick second ... fifth item in node's menu.
857 \\[Info-goto-node] Move to node specified by name.
858 You may include a filename as well, as (FILENAME)NODENAME.
859 \\[Info-search] Search through this Info file for specified regexp,
860 and select the node in which the next occurrence is found."
861 (kill-all-local-variables)
862 (setq major-mode 'Info-mode)
863 (setq mode-name "Info")
864 (use-local-map Info-mode-map)
865 (set-syntax-table text-mode-syntax-table)
866 (setq local-abbrev-table text-mode-abbrev-table)
867 (setq case-fold-search t)
868 (setq buffer-read-only t)
869 (make-local-variable 'Info-current-file)
870 (make-local-variable 'Info-current-subfile)
871 (make-local-variable 'Info-current-node)
872 (make-local-variable 'Info-tag-table-marker)
873 (make-local-variable 'Info-history)
874 (Info-set-mode-line)
875 (run-hooks 'Info-mode-hook))
876
877 (defvar Info-edit-map nil
878 "Local keymap used within `e' command of Info.")
879 (if Info-edit-map
880 nil
881 (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
882 (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
883
884 ;; Info-edit mode is suitable only for specially formatted data.
885 (put 'info-edit-mode 'mode-class 'special)
886
887 (defun Info-edit-mode ()
888 "Major mode for editing the contents of an Info node.
889 Like text mode with the addition of Info-cease-edit
890 which returns to Info mode for browsing.
891 \\{Info-edit-map}"
892 )
893
894 (defun Info-edit ()
895 "Edit the contents of this Info node.
896 Allowed only if variable `Info-enable-edit' is non-nil."
897 (interactive)
898 (or Info-enable-edit
899 (error "Editing info nodes is not enabled"))
900 (use-local-map Info-edit-map)
901 (setq major-mode 'Info-edit-mode)
902 (setq mode-name "Info Edit")
903 (kill-local-variable 'mode-line-buffer-identification)
904 (setq buffer-read-only nil)
905 ;; Make mode line update.
906 (set-buffer-modified-p (buffer-modified-p))
907 (message (substitute-command-keys
908 "Editing: Type \\<info-mode-map>\\[Info-cease-edit] to return to info")))
909
910 (defun Info-cease-edit ()
911 "Finish editing Info node; switch back to Info proper."
912 (interactive)
913 ;; Do this first, so nothing has changed if user C-g's at query.
914 (and (buffer-modified-p)
915 (y-or-n-p "Save the file? ")
916 (save-buffer))
917 (use-local-map Info-mode-map)
918 (setq major-mode 'Info-mode)
919 (setq mode-name "Info")
920 (Info-set-mode-line)
921 (setq buffer-read-only t)
922 ;; Make mode line update.
923 (set-buffer-modified-p (buffer-modified-p))
924 (and (marker-position Info-tag-table-marker)
925 (buffer-modified-p)
926 (message "Tags may have changed. Use Info-tagify if necessary")))
927 \f
928 (defun Info-find-emacs-command-nodes (command)
929 "Return a list of locations documenting COMMAND in the Emacs Info manual.
930 The locations are of the format used in Info-history, i.e.
931 \(FILENAME NODENAME BUFFERPOS\)."
932 (require 'info)
933 (let ((where '())
934 (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
935 ":\\s *\\(.*\\)\\.$")))
936 (save-excursion
937 (Info-find-node "emacs" "Command Index")
938 ;; Take the index node off the Info history.
939 (setq Info-history (cdr Info-history))
940 (goto-char (point-max))
941 (while (re-search-backward cmd-desc nil t)
942 (setq where (cons (list Info-current-file
943 (buffer-substring
944 (match-beginning 1)
945 (match-end 1))
946 0)
947 where)))
948 where)))
949
950 ;;;###autoload
951 (defun Info-goto-emacs-command-node (command)
952 "Go to the Info node in the Emacs manual for command COMMAND."
953 (interactive "CFind documentation for command: ")
954 (or (commandp command)
955 (signal 'wrong-type-argument (list 'commandp command)))
956 (let ((where (Info-find-emacs-command-nodes command)))
957 (if where
958 (let ((num-matches (length where)))
959 ;; Get Info running, and pop to it in another window.
960 (save-window-excursion
961 (info))
962 (pop-to-buffer "*info*")
963 (Info-find-node (car (car where))
964 (car (cdr (car where))))
965 (if (> num-matches 1)
966 (progn
967 ;; Info-find-node already pushed (car where) onto
968 ;; Info-history. Put the other nodes that were found on
969 ;; the history.
970 (setq Info-history (nconc (cdr where) Info-history))
971 (message (substitute-command-keys
972 "Found %d other entr%. Use \\[Info-last] to see %s."
973 (1- num-matches)
974 (if (> num-matches 2) "ies" "y")
975 (if (> num-matches 2) "them" "it"))))))
976 (error "Couldn't find documentation for %s." command))))
977 ;;;###autoload
978 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
979
980 ;;;###autoload
981 (defun Info-goto-emacs-key-command-node (key)
982 "Go to the Info node in the Emacs manual the command bound to KEY, a string.
983 Interactively, if the binding is execute-extended-command, a command is read."
984 (interactive "kFind documentation for key:")
985 (let ((command (key-binding key)))
986 (cond ((null command)
987 (message "%s is undefined" (key-description keys)))
988 ((and (interactive-p)
989 (eq command 'execute-extended-command))
990 (Info-goto-emacs-command-node
991 (read-command "Find documentation for command: ")))
992 (t
993 (Info-goto-emacs-command-node command)))))
994 ;;;###autoload
995 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
996
997 (provide 'info)
998
999 ;;; info.el ends here