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