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