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