(Electric-buffer-menu-mouse-select): New function.
[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.
4db579ab 307 (or Info-current-node no-going-back (null Info-history)
a384cab3
JB
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 375
4db579ab
KH
376 (or buffers
377 (error "Can't find the info directory node"))
44c327f9 378 ;; Distinguish the dir file that comes with Emacs from all the
f4008b6e
RS
379 ;; others. Yes, that is really what this is supposed to do.
380 ;; If it doesn't work, fix it.
7ea13762
RS
381 (setq buffer (car buffers)
382 others (cdr buffers))
44c327f9
JB
383
384 ;; Insert the entire original dir file as a start; use its
385 ;; default directory as the default directory for the whole
386 ;; concatenation.
7ea13762 387 (insert-buffer buffer)
44c327f9
JB
388 (setq Info-dir-contents-directory (save-excursion
389 (set-buffer buffer)
390 default-directory))
391
7ea13762
RS
392 ;; Look at each of the other buffers one by one.
393 (while others
394 (let ((other (car others)))
395 ;; In each, find all the menus.
396 (save-excursion
397 (set-buffer other)
398 (goto-char (point-min))
399 ;; Find each menu, and add an elt to NODES for it.
400 (while (re-search-forward "^\\* Menu:" nil t)
401 (let (beg nodename end)
402 (forward-line 1)
403 (setq beg (point))
3a6ade8a 404 (search-backward "\n\^_")
7ea13762
RS
405 (search-forward "Node: ")
406 (setq nodename (Info-following-node-name))
3a6ade8a 407 (search-forward "\n\^_" nil 'move)
7ea13762
RS
408 (beginning-of-line)
409 (setq end (point))
410 (setq nodes (cons (list nodename other beg end) nodes))))))
411 (setq others (cdr others)))
412 ;; Add to the main menu a menu item for each other node.
413 (re-search-forward "^\\* Menu:")
414 (forward-line 1)
415 (let ((menu-items '("top"))
416 (nodes nodes)
417 (case-fold-search t)
3a6ade8a 418 (end (save-excursion (search-forward "\^_" nil t) (point))))
7ea13762
RS
419 (while nodes
420 (let ((nodename (car (car nodes))))
c3a29d70
RS
421 (save-excursion
422 (or (member (downcase nodename) menu-items)
423 (re-search-forward (concat "^\\* "
424 (regexp-quote nodename)
425 "::")
426 end t)
427 (progn
428 (insert "* " nodename "::" "\n")
429 (setq menu-items (cons nodename menu-items))))))
7ea13762
RS
430 (setq nodes (cdr nodes))))
431 ;; Now take each node of each of the other buffers
432 ;; and merge it into the main buffer.
433 (while nodes
434 (let ((nodename (car (car nodes))))
435 (goto-char (point-min))
436 ;; Find the like-named node in the main buffer.
3a6ade8a 437 (if (re-search-forward (concat "\n\^_.*\n.*Node: "
7ea13762
RS
438 (regexp-quote nodename)
439 "[,\n\t]")
440 nil t)
441 (progn
3a6ade8a 442 (search-forward "\n\^_" nil 'move)
7ea13762
RS
443 (beginning-of-line))
444 ;; If none exists, add one.
445 (goto-char (point-max))
3a6ade8a 446 (insert "\^_\nFile: dir\tnode: " nodename "\n\n* Menu:\n\n"))
7ea13762
RS
447 ;; Merge the text from the other buffer's menu
448 ;; into the menu in the like-named node in the main buffer.
449 (apply 'insert-buffer-substring (cdr (car nodes)))
450 (insert "\n"))
451 (setq nodes (cdr nodes)))
452 ;; Kill all the buffers we just made.
453 (while buffers
454 (kill-buffer (car buffers))
455 (setq buffers (cdr buffers))))
44c327f9
JB
456 (setq Info-dir-contents (buffer-string)))
457 (setq default-directory Info-dir-contents-directory))
7ea13762 458
a384cab3
JB
459(defun Info-read-subfile (nodepos)
460 (set-buffer (marker-buffer Info-tag-table-marker))
461 (goto-char (point-min))
462 (search-forward "\n\^_")
463 (let (lastfilepos
464 lastfilename)
465 (forward-line 2)
466 (catch 'foo
467 (while (not (looking-at "\^_"))
468 (if (not (eolp))
469 (let ((beg (point))
470 thisfilepos thisfilename)
471 (search-forward ": ")
472 (setq thisfilename (buffer-substring beg (- (point) 2)))
473 (setq thisfilepos (read (current-buffer)))
474 ;; read in version 19 stops at the end of number.
475 ;; Advance to the next line.
476 (forward-line 1)
477 (if (> thisfilepos nodepos)
478 (throw 'foo t))
479 (setq lastfilename thisfilename)
480 (setq lastfilepos thisfilepos))
481 (forward-line 1))))
482 (set-buffer (get-buffer "*info*"))
483 (or (equal Info-current-subfile lastfilename)
484 (let ((buffer-read-only nil))
485 (setq buffer-file-name nil)
486 (widen)
487 (erase-buffer)
1143a6b0 488 (info-insert-file-contents lastfilename)
a384cab3
JB
489 (set-buffer-modified-p nil)
490 (setq Info-current-subfile lastfilename)))
491 (goto-char (point-min))
492 (search-forward "\n\^_")
493 (+ (- nodepos lastfilepos) (point))))
494
495;; Select the info node that point is in.
496(defun Info-select-node ()
497 (save-excursion
498 ;; Find beginning of node.
499 (search-backward "\n\^_")
500 (forward-line 2)
501 ;; Get nodename spelled as it is in the node.
502 (re-search-forward "Node:[ \t]*")
503 (setq Info-current-node
504 (buffer-substring (point)
505 (progn
506 (skip-chars-forward "^,\t\n")
507 (point))))
508 (Info-set-mode-line)
509 ;; Find the end of it, and narrow.
510 (beginning-of-line)
511 (let (active-expression)
512 (narrow-to-region (point)
513 (if (re-search-forward "\n[\^_\f]" nil t)
514 (prog1
515 (1- (point))
516 (if (looking-at "[\n\^_\f]*execute: ")
517 (progn
518 (goto-char (match-end 0))
519 (setq active-expression
520 (read (current-buffer))))))
521 (point-max)))
f05f43e7 522 (if Info-enable-active-nodes (eval active-expression))
552775bd 523 (if Info-fontify (Info-fontify-node))
f05f43e7 524 (run-hooks 'Info-selection-hook))))
a384cab3
JB
525
526(defun Info-set-mode-line ()
527 (setq mode-line-buffer-identification
528 (concat
529 "Info: ("
530 (if Info-current-file
531 (file-name-nondirectory Info-current-file)
532 "")
533 ")"
534 (or Info-current-node ""))))
535\f
536;; Go to an info node specified with a filename-and-nodename string
537;; of the sort that is found in pointers in nodes.
538
539(defun Info-goto-node (nodename)
540 "Go to info node named NAME. Give just NODENAME or (FILENAME)NODENAME."
552775bd 541 (interactive (list (Info-read-node-name "Goto node: ")))
a384cab3
JB
542 (let (filename)
543 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
544 nodename)
545 (setq filename (if (= (match-beginning 1) (match-end 1))
546 ""
547 (substring nodename (match-beginning 2) (match-end 2)))
548 nodename (substring nodename (match-beginning 3) (match-end 3)))
549 (let ((trim (string-match "\\s *\\'" filename)))
550 (if trim (setq filename (substring filename 0 trim))))
551 (let ((trim (string-match "\\s *\\'" nodename)))
552 (if trim (setq nodename (substring nodename 0 trim))))
553 (Info-find-node (if (equal filename "") nil filename)
554 (if (equal nodename "") "Top" nodename))))
552775bd
RS
555
556(defun Info-read-node-name (prompt &optional default)
557 (let* ((completion-ignore-case t)
558 (nodename (completing-read prompt (Info-build-node-completions))))
559 (if (equal nodename "")
560 (or default
561 (Info-read-node-name prompt))
562 nodename)))
563
564(defun Info-build-node-completions ()
565 (or Info-current-file-completions
566 (let ((compl nil))
567 (save-excursion
568 (save-restriction
569 (if (marker-buffer Info-tag-table-marker)
570 (progn
571 (set-buffer (marker-buffer Info-tag-table-marker))
cedb118c 572 (widen)
552775bd
RS
573 (goto-char Info-tag-table-marker)
574 (while (re-search-forward "\nNode: \\(.*\\)\177" nil t)
575 (setq compl
576 (cons (list (buffer-substring (match-beginning 1)
577 (match-end 1)))
578 compl))))
579 (widen)
580 (goto-char (point-min))
581 (while (search-forward "\n\^_" nil t)
582 (forward-line 1)
583 (let ((beg (point)))
584 (forward-line 1)
585 (if (re-search-backward "Node: *\\([^,\n]*\\) *[,\n\t]"
586 beg t)
587 (setq compl
588 (cons (list (buffer-substring (match-beginning 1)
589 (match-end 1)))
590 compl))))))))
591 (setq Info-current-file-completions compl))))
a384cab3 592\f
aea2a8da
JB
593(defun Info-restore-point (hl)
594 "If this node has been visited, restore the point value when we left."
cedb118c
RS
595 (while hl
596 (if (and (equal (nth 0 (car hl)) Info-current-file)
597 (equal (nth 1 (car hl)) Info-current-node))
598 (progn
d96504df
KH
599 (goto-char (nth 2 (car hl)))
600 (setq hl nil)) ;terminate the while at next iter
cedb118c 601 (setq hl (cdr hl)))))
aea2a8da 602\f
a384cab3 603(defvar Info-last-search nil
d5913bf6 604 "Default regexp for \\<Info-mode-map>\\[Info-search] command to search for.")
a384cab3
JB
605
606(defun Info-search (regexp)
607 "Search for REGEXP, starting from point, and select node it's found in."
608 (interactive "sSearch (regexp): ")
609 (if (equal regexp "")
610 (setq regexp Info-last-search)
611 (setq Info-last-search regexp))
612 (let ((found ()) current
613 (onode Info-current-node)
614 (ofile Info-current-file)
615 (opoint (point))
616 (osubfile Info-current-subfile))
617 (save-excursion
618 (save-restriction
619 (widen)
620 (if (null Info-current-subfile)
621 (progn (re-search-forward regexp) (setq found (point)))
622 (condition-case err
623 (progn (re-search-forward regexp) (setq found (point)))
624 (search-failed nil)))))
625 (if (not found) ;can only happen in subfile case -- else would have erred
626 (unwind-protect
627 (let ((list ()))
628 (set-buffer (marker-buffer Info-tag-table-marker))
629 (goto-char (point-min))
630 (search-forward "\n\^_\nIndirect:")
631 (save-restriction
632 (narrow-to-region (point)
633 (progn (search-forward "\n\^_")
634 (1- (point))))
635 (goto-char (point-min))
636 (search-forward (concat "\n" osubfile ": "))
637 (beginning-of-line)
638 (while (not (eobp))
639 (re-search-forward "\\(^.*\\): [0-9]+$")
640 (goto-char (+ (match-end 1) 2))
641 (setq list (cons (cons (read (current-buffer))
642 (buffer-substring (match-beginning 1)
643 (match-end 1)))
644 list))
645 (goto-char (1+ (match-end 0))))
646 (setq list (nreverse list)
647 current (car (car list))
648 list (cdr list)))
649 (while list
650 (message "Searching subfile %s..." (cdr (car list)))
651 (Info-read-subfile (car (car list)))
652 (setq list (cdr list))
653 (goto-char (point-min))
654 (if (re-search-forward regexp nil t)
655 (setq found (point) list ())))
656 (if found
657 (message "")
658 (signal 'search-failed (list regexp))))
659 (if (not found)
660 (progn (Info-read-subfile opoint)
661 (goto-char opoint)
662 (Info-select-node)))))
663 (widen)
664 (goto-char found)
665 (Info-select-node)
666 (or (and (equal onode Info-current-node)
667 (equal ofile Info-current-file))
668 (setq Info-history (cons (list ofile onode opoint)
669 Info-history)))))
670\f
671;; Extract the value of the node-pointer named NAME.
672;; If there is none, use ERRORNAME in the error message;
673;; if ERRORNAME is nil, just return nil.
674(defun Info-extract-pointer (name &optional errorname)
675 (save-excursion
676 (goto-char (point-min))
677 (forward-line 1)
678 (if (re-search-backward (concat name ":") nil t)
679 (progn
680 (goto-char (match-end 0))
681 (Info-following-node-name))
682 (if (eq errorname t)
683 nil
684 (error (concat "Node has no " (capitalize (or errorname name))))))))
685
7ea13762
RS
686;; Return the node name in the buffer following point.
687;; ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
688;; saying which chas may appear in the node name.
a384cab3
JB
689(defun Info-following-node-name (&optional allowedchars)
690 (skip-chars-forward " \t")
691 (buffer-substring
692 (point)
693 (progn
694 (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
695 (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
696 (if (looking-at "(")
697 (skip-chars-forward "^)")))
698 (skip-chars-backward " ")
699 (point))))
700
701(defun Info-next ()
702 "Go to the next node of this node."
703 (interactive)
704 (Info-goto-node (Info-extract-pointer "next")))
705
706(defun Info-prev ()
707 "Go to the previous node of this node."
708 (interactive)
709 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
710
711(defun Info-up ()
712 "Go to the superior node of this node."
713 (interactive)
aea2a8da
JB
714 (Info-goto-node (Info-extract-pointer "up"))
715 (Info-restore-point Info-history))
a384cab3
JB
716
717(defun Info-last ()
718 "Go back to the last node visited."
719 (interactive)
720 (or Info-history
721 (error "This is the first Info node you looked at"))
722 (let (filename nodename opoint)
723 (setq filename (car (car Info-history)))
724 (setq nodename (car (cdr (car Info-history))))
725 (setq opoint (car (cdr (cdr (car Info-history)))))
726 (setq Info-history (cdr Info-history))
727 (Info-find-node filename nodename)
728 (setq Info-history (cdr Info-history))
729 (goto-char opoint)))
730
731(defun Info-directory ()
732 "Go to the Info directory node."
733 (interactive)
734 (Info-find-node "dir" "top"))
735\f
736(defun Info-follow-reference (footnotename)
737 "Follow cross reference named NAME to the node it refers to.
738NAME may be an abbreviation of the reference name."
739 (interactive
740 (let ((completion-ignore-case t)
67bc89ab 741 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 742 (save-excursion
67bc89ab
RS
743 ;; Store end and beginning of line.
744 (end-of-line)
745 (setq eol (point))
746 (beginning-of-line)
747 (setq bol (point))
748
a384cab3
JB
749 (goto-char (point-min))
750 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
751 (setq str (buffer-substring
752 (match-beginning 1)
753 (1- (point))))
754 ;; See if this one should be the default.
755 (and (null default)
1f179e27 756 (<= (match-beginning 0) start-point)
a384cab3
JB
757 (<= start-point (point))
758 (setq default t))
67bc89ab
RS
759 ;; See if this one should be the alternate default.
760 (and (null alt-default)
761 (and (<= bol (match-beginning 0))
762 (<= (point) eol))
763 (setq alt-default t))
a384cab3
JB
764 (setq i 0)
765 (while (setq i (string-match "[ \n\t]+" str i))
766 (setq str (concat (substring str 0 i) " "
767 (substring str (match-end 0))))
768 (setq i (1+ i)))
769 ;; Record as a completion and perhaps as default.
770 (if (eq default t) (setq default str))
67bc89ab 771 (if (eq alt-default t) (setq alt-default str))
a384cab3
JB
772 (setq completions
773 (cons (cons str nil)
774 completions))))
67bc89ab
RS
775 ;; If no good default was found, try an alternate.
776 (or default
777 (setq default alt-default))
778 ;; If only one cross-reference found, then make it default.
779 (if (eq (length completions) 1)
780 (setq default (car (car completions))))
a384cab3 781 (if completions
b0ebdfe5
RS
782 (let ((input (completing-read (if default
783 (concat "Follow reference named: ("
784 default ") ")
785 "Follow reference named: ")
786 completions nil t)))
787 (list (if (equal input "")
788 default input)))
a384cab3
JB
789 (error "No cross-references in this node"))))
790 (let (target beg i (str (concat "\\*note " footnotename)))
791 (while (setq i (string-match " " str i))
792 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
793 (setq i (+ i 6)))
794 (save-excursion
795 (goto-char (point-min))
796 (or (re-search-forward str nil t)
797 (error "No cross-reference named %s" footnotename))
798 (goto-char (+ (match-beginning 0) 5))
799 (setq target
800 (Info-extract-menu-node-name "Bad format cross reference" t)))
801 (while (setq i (string-match "[ \t\n]+" target i))
802 (setq target (concat (substring target 0 i) " "
803 (substring target (match-end 0))))
804 (setq i (+ i 1)))
805 (Info-goto-node target)))
806
807(defun Info-extract-menu-node-name (&optional errmessage multi-line)
808 (skip-chars-forward " \t\n")
809 (let ((beg (point))
810 str i)
811 (skip-chars-forward "^:")
812 (forward-char 1)
813 (setq str
814 (if (looking-at ":")
815 (buffer-substring beg (1- (point)))
816 (skip-chars-forward " \t\n")
817 (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
818 (while (setq i (string-match "\n" str i))
819 (aset str i ?\ ))
820 str))
821
9e5c2f50
RS
822;; No one calls this and Info-menu-item doesn't exist.
823;;(defun Info-menu-item-sequence (list)
824;; (while list
825;; (Info-menu-item (car list))
826;; (setq list (cdr list))))
a384cab3
JB
827
828(defun Info-menu (menu-item)
829 "Go to node for menu item named (or abbreviated) NAME.
830Completion is allowed, and the menu item point is on is the default."
831 (interactive
832 (let ((completions '())
833 ;; If point is within a menu item, use that item as the default
834 (default nil)
835 (p (point))
836 (last nil))
837 (save-excursion
838 (goto-char (point-min))
839 (if (not (search-forward "\n* menu:" nil t))
840 (error "No menu in this node"))
841 (while (re-search-forward
842 "\n\\* \\([^:\t\n]*\\):" nil t)
843 (if (and (null default)
844 (prog1 (if last (< last p) nil)
845 (setq last (match-beginning 0)))
846 (<= p last))
847 (setq default (car (car completions))))
848 (setq completions (cons (cons (buffer-substring
849 (match-beginning 1)
850 (match-end 1))
851 (match-beginning 1))
852 completions)))
853 (if (and (null default) last
854 (< last p)
855 (<= p (progn (end-of-line) (point))))
856 (setq default (car (car completions)))))
857 (let ((item nil))
858 (while (null item)
859 (setq item (let ((completion-ignore-case t))
860 (completing-read (if default
861 (format "Menu item (default %s): "
862 default)
863 "Menu item: ")
864 completions nil t)))
aea2a8da
JB
865 ;; we rely on the fact that completing-read accepts an input
866 ;; of "" even when the require-match argument is true and ""
867 ;; is not a valid possibility
a384cab3
JB
868 (if (string= item "")
869 (if default
870 (setq item default)
871 ;; ask again
872 (setq item nil))))
873 (list item))))
874 ;; there is a problem here in that if several menu items have the same
875 ;; name you can only go to the node of the first with this command.
876 (Info-goto-node (Info-extract-menu-item menu-item)))
877
878(defun Info-extract-menu-item (menu-item)
879 (setq menu-item (regexp-quote menu-item))
880 (save-excursion
881 (goto-char (point-min))
882 (or (search-forward "\n* menu:" nil t)
883 (error "No menu in this node"))
de3fa0b2
RS
884 (or (re-search-forward (concat "\n\\* " menu-item ":") nil t)
885 (re-search-forward (concat "\n\\* " menu-item) nil t)
a384cab3
JB
886 (error "No such item in menu"))
887 (beginning-of-line)
888 (forward-char 2)
889 (Info-extract-menu-node-name)))
890
891;; If COUNT is nil, use the last item in the menu.
892(defun Info-extract-menu-counting (count)
893 (save-excursion
894 (goto-char (point-min))
895 (or (search-forward "\n* menu:" nil t)
896 (error "No menu in this node"))
897 (if count
898 (or (search-forward "\n* " nil t count)
899 (error "Too few items in menu"))
900 (while (search-forward "\n* " nil t)
901 nil))
902 (Info-extract-menu-node-name)))
903
e38e7367
RM
904(defun Info-nth-menu-item ()
905 "Go to the node of the Nth menu item.
906N is the digit argument used to invoke this command."
a384cab3 907 (interactive)
e38e7367
RM
908 (Info-goto-node
909 (Info-extract-menu-counting
910 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
911
912(defun Info-top-node ()
913 "Go to the Top node of this file."
914 (interactive)
915 (Info-goto-node "Top"))
916
917(defun Info-final-node ()
918 "Go to the final node in this file."
919 (interactive)
920 (Info-goto-node "Top")
921 (let (Info-history)
922 ;; Go to the last node in the menu of Top.
923 (Info-goto-node (Info-extract-menu-counting nil))
924 ;; If the last node in the menu is not last in pointer structure,
925 ;; move forward until we can't go any farther.
926 (while (Info-forward-node t t) nil)
927 ;; Then keep moving down to last subnode, unless we reach an index.
928 (while (and (not (string-match "\\<index\\>" Info-current-node))
929 (save-excursion (search-forward "\n* Menu:" nil t)))
930 (Info-goto-node (Info-extract-menu-counting nil)))))
931
932(defun Info-forward-node (&optional not-down no-error)
933 "Go forward one node, considering all nodes as forming one sequence."
934 (interactive)
935 (goto-char (point-min))
936 (forward-line 1)
937 ;; three possibilities, in order of priority:
938 ;; 1. next node is in a menu in this node (but not in an index)
939 ;; 2. next node is next at same level
940 ;; 3. next node is up and next
941 (cond ((and (not not-down)
942 (save-excursion (search-forward "\n* menu:" nil t))
943 (not (string-match "\\<index\\>" Info-current-node)))
463f48f4 944 (Info-goto-node (Info-extract-menu-counting 1))
a384cab3
JB
945 t)
946 ((save-excursion (search-backward "next:" nil t))
947 (Info-next)
948 t)
949 ((and (save-excursion (search-backward "up:" nil t))
950 (not (equal (downcase (Info-extract-pointer "up")) "top")))
951 (let ((old-node Info-current-node))
952 (Info-up)
953 (let (Info-history success)
954 (unwind-protect
955 (setq success (Info-forward-node t no-error))
956 (or success (Info-goto-node old-node))))))
957 (no-error nil)
958 (t (error "No pointer forward from this node"))))
959
960(defun Info-backward-node ()
961 "Go backward one node, considering all nodes as forming one sequence."
962 (interactive)
963 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
964 (upnode (Info-extract-pointer "up" t)))
965 (cond ((and upnode (string-match "(" upnode))
966 (error "First node in file"))
967 ((and upnode (or (null prevnode)
968 (equal (downcase prevnode) (downcase upnode))))
969 (Info-up))
970 (prevnode
971 ;; If we move back at the same level,
972 ;; go down to find the last subnode*.
973 (Info-prev)
974 (let (Info-history)
975 (while (and (not (string-match "\\<index\\>" Info-current-node))
976 (save-excursion (search-forward "\n* Menu:" nil t)))
977 (Info-goto-node (Info-extract-menu-counting nil)))))
978 (t
979 (error "No pointer backward from this node")))))
980
981(defun Info-exit ()
982 "Exit Info by selecting some other buffer."
983 (interactive)
552775bd
RS
984 (if Info-standalone
985 (save-buffers-kill-emacs)
986 (switch-to-buffer (prog1 (other-buffer (current-buffer))
987 (bury-buffer (current-buffer))))))
a384cab3 988
253db917
ER
989(defun Info-next-menu-item ()
990 (interactive)
991 (save-excursion
992 (forward-line -1)
993 (search-forward "\n* menu:" nil t)
994 (or (search-forward "\n* " nil t)
995 (error "No more items in menu"))
996 (Info-goto-node (Info-extract-menu-node-name))))
997
998(defun Info-last-menu-item ()
999 (interactive)
1000 (save-excursion
1001 (forward-line 1)
1002 (search-backward "\n* menu:" nil t)
1003 (or (search-backward "\n* " nil t)
1004 (error "No previous items in menu"))
1005 (Info-goto-node (Info-extract-menu-node-name))))
1006
552775bd 1007(defmacro Info-no-error (&rest body)
253db917
ER
1008 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
1009
1010(defun Info-next-preorder ()
1011 "Go to the next node, popping up a level if there is none."
1012 (interactive)
552775bd
RS
1013 (cond ((looking-at "\\*note[ \n]*\\([^:]*\\):")
1014 (Info-follow-reference
1015 (buffer-substring (match-beginning 1) (match-end 1))))
1016 ((Info-no-error (Info-next-menu-item)) )
1017 ((Info-no-error (Info-up)) (forward-line 1))
253db917
ER
1018 (t (error "No more nodes"))))
1019
1020(defun Info-last-preorder ()
1021 "Go to the last node, popping up a level if there is none."
1022 (interactive)
552775bd
RS
1023 (cond ((Info-no-error (Info-last-menu-item)) )
1024 ((Info-no-error (Info-up)) (forward-line -1))
253db917
ER
1025 (t (error "No previous nodes"))))
1026
1027(defun Info-scroll-up ()
1028 "Read the next screen. If end of buffer is visible, go to next entry."
1029 (interactive)
1030 (if (pos-visible-in-window-p (point-max))
1031 (Info-next-preorder)
1032 (scroll-up))
1033 )
1034
1035(defun Info-scroll-down ()
1036 "Read the previous screen. If start of buffer is visible, go to last entry."
1037 (interactive)
1038 (if (pos-visible-in-window-p (point-min))
1039 (Info-last-preorder)
1040 (scroll-down))
1041 )
1042
552775bd
RS
1043(defun Info-next-reference ()
1044 "Move cursor to the next cross-reference or menu item in the node."
1045 (interactive)
1046 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1047 (old-pt (point)))
1048 (or (eobp) (forward-char 1))
1049 (or (re-search-forward pat nil t)
1050 (progn
1051 (goto-char (point-min))
1052 (or (re-search-forward pat nil t)
1053 (progn
1054 (goto-char old-pt)
1055 (error "No cross references in this node")))))
1056 (goto-char (match-beginning 0))
1057 (if (looking-at "\\* Menu:")
1058 (Info-next-reference))))
1059
1060(defun Info-prev-reference ()
1061 "Move cursor to the previous cross-reference or menu item in the node."
1062 (interactive)
1063 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1064 (old-pt (point)))
1065 (or (re-search-backward pat nil t)
1066 (progn
1067 (goto-char (point-max))
1068 (or (re-search-backward pat nil t)
1069 (progn
1070 (goto-char old-pt)
1071 (error "No cross references in this node")))))
1072 (goto-char (match-beginning 0))
1073 (if (looking-at "\\* Menu:")
1074 (Info-prev-reference))))
1075
1143a6b0
ER
1076(defun Info-index (topic)
1077 "Look up a string in the index for this file.
1078The index is defined as the first node in the top-level menu whose
1079name contains the word \"Index\", plus any immediately following
1080nodes whose names also contain the word \"Index\".
1081If there are no exact matches to the specified topic, this chooses
1082the first match which is a case-insensitive substring of a topic.
1083Use the `,' command to see the other matches.
1084Give a blank topic name to go to the Index node itself."
1085 (interactive "sIndex topic: ")
1086 (let ((orignode Info-current-node)
1087 (rnode nil)
1088 (pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ t]*\\([0-9]*\\)"
1089 (regexp-quote topic)))
1090 node)
1091 (Info-goto-node "Top")
1092 (or (search-forward "\n* menu:" nil t)
1093 (error "No index"))
1094 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1095 (error "No index"))
1096 (goto-char (match-beginning 1))
1097 (let ((Info-keeping-history nil))
1098 (Info-goto-node (Info-extract-menu-node-name)))
1099 (or (equal topic "")
1100 (let ((matches nil)
1101 (exact nil)
1102 (Info-keeping-history nil)
1103 found)
1104 (while
1105 (progn
1106 (goto-char (point-min))
1107 (while (re-search-forward pattern nil t)
1108 (setq matches
1109 (cons (list (buffer-substring (match-beginning 1)
1110 (match-end 1))
1111 (buffer-substring (match-beginning 2)
1112 (match-end 2))
1113 Info-current-node
1114 (string-to-int (concat "0"
1115 (buffer-substring
1116 (match-beginning 3)
1117 (match-end 3)))))
1118 matches)))
1119 (and (setq node (Info-extract-pointer "next" t))
1120 (string-match "\\<Index\\>" node)))
1121 (Info-goto-node node))
1122 (or matches
1123 (progn
1124 (Info-last)
1125 (error "No \"%s\" in index" topic)))
1126 ;; Here it is a feature that assoc is case-sensitive.
1127 (while (setq found (assoc topic matches))
1128 (setq exact (cons found exact)
1129 matches (delq found matches)))
1130 (setq Info-index-alternatives (nconc exact (nreverse matches)))
1131 (Info-index-next 0)))))
1132
1133(defun Info-index-next (num)
1134 "Go to the next matching index item from the last `i' command."
1135 (interactive "p")
1136 (or Info-index-alternatives
1137 (error "No previous `i' command in this file"))
1138 (while (< num 0)
1139 (setq num (+ num (length Info-index-alternatives))))
1140 (while (> num 0)
1141 (setq Info-index-alternatives
1142 (nconc (cdr Info-index-alternatives)
1143 (list (car Info-index-alternatives)))
1144 num (1- num)))
1145 (Info-goto-node (nth 1 (car Info-index-alternatives)))
1146 (if (> (nth 3 (car Info-index-alternatives)) 0)
1147 (forward-line (nth 3 (car Info-index-alternatives)))
1148 (forward-line 3) ; don't search in headers
1149 (let ((name (car (car Info-index-alternatives))))
1150 (if (or (re-search-forward (format
1151 "\\(Function\\|Command\\): %s\\( \\|$\\)"
1152 (regexp-quote name)) nil t)
1153 (search-forward (format "`%s'" name) nil t)
1154 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1155 (search-forward
1156 (format "`%s'" (substring name 0 (match-beginning 1)))
1157 nil t))
1158 (search-forward name nil t))
1159 (beginning-of-line)
1160 (goto-char (point-min)))))
1161 (message "Found \"%s\" in %s. %s"
1162 (car (car Info-index-alternatives))
1163 (nth 2 (car Info-index-alternatives))
1164 (if (cdr Info-index-alternatives)
1165 "(Press `,' for more)"
1166 "(Only match)")))
1167
a384cab3
JB
1168(defun Info-undefined ()
1169 "Make command be undefined in Info."
1170 (interactive)
1171 (ding))
1172
1173(defun Info-help ()
1174 "Enter the Info tutorial."
1175 (interactive)
1176 (delete-other-windows)
1177 (Info-find-node "info"
1178 (if (< (window-height) 23)
1179 "Help-Small-Screen"
1180 "Help")))
1181
1182(defun Info-summary ()
1183 "Display a brief summary of all Info commands."
1184 (interactive)
1185 (save-window-excursion
1186 (switch-to-buffer "*Help*")
1187 (erase-buffer)
1188 (insert (documentation 'Info-mode))
1189 (goto-char (point-min))
1190 (let (ch flag)
1191 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1192 (message (if flag "Type Space to see more"
1193 "Type Space to return to Info"))
1614c867 1194 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 1195 (progn (setq unread-command-events (list ch)) nil)
a384cab3 1196 flag))
552775bd
RS
1197 (scroll-up)))
1198 (bury-buffer "*Help*")))
a384cab3
JB
1199\f
1200(defun Info-get-token (pos start all &optional errorstring)
1201 "Return the token around POS,
1202POS must be somewhere inside the token
1203START is a regular expression which will match the
1204 beginning of the tokens delimited string
1205ALL is a regular expression with a single
1206 parenthized subpattern which is the token to be
1207 returned. E.g. '{\(.*\)}' would return any string
1208 enclosed in braces around POS.
1209SIG optional fourth argument, controls action on no match
1210 nil: return nil
1211 t: beep
1212 a string: signal an error, using that string."
1213 (save-excursion
1214 (goto-char pos)
1215 (re-search-backward start (max (point-min) (- pos 200)) 'yes)
373377ed
RS
1216 (let (found)
1217 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1218 (not (setq found (and (<= (match-beginning 0) pos)
1219 (> (match-end 0) pos))))))
1220 (if (and found (<= (match-beginning 0) pos)
1221 (> (match-end 0) pos))
1222 (buffer-substring (match-beginning 1) (match-end 1))
1223 (cond ((null errorstring)
1224 nil)
1225 ((eq errorstring t)
1226 (beep)
1227 nil)
1228 (t
1229 (error "No %s around position %d" errorstring pos)))))))
a384cab3 1230
aea2a8da 1231(defun Info-follow-nearest-node (click)
f9969361
RS
1232 "\\<Info-mode-map>Follow a node reference near point.
1233Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
a384cab3 1234At end of the node's text, moves to the next node."
f9969361 1235 (interactive "e")
9e5c2f50
RS
1236 (let* ((start (event-start click))
1237 (window (car start))
1238 (pos (car (cdr start))))
1239 (select-window window)
1240 (goto-char pos))
a384cab3
JB
1241 (let (node)
1242 (cond
bc2ada62 1243 ((setq node (Info-get-token (point) "\\*note[ \n]" "\\*note[ \n]\\([^:]*\\):"))
a384cab3 1244 (Info-follow-reference node))
bc2ada62 1245 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::"))
a384cab3 1246 (Info-goto-node node))
bc2ada62 1247 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):"))
a384cab3 1248 (Info-menu node))
bc2ada62 1249 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
a384cab3 1250 (Info-goto-node node))
bc2ada62 1251 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
a384cab3 1252 (Info-goto-node node))
bc2ada62 1253 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
a384cab3 1254 (Info-goto-node "Top"))
bc2ada62 1255 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
a384cab3
JB
1256 (Info-goto-node node))
1257 ((save-excursion (forward-line 1) (eobp))
1258 (Info-next)))
1259 ))
1260\f
1261(defvar Info-mode-map nil
1262 "Keymap containing Info commands.")
1263(if Info-mode-map
1264 nil
1265 (setq Info-mode-map (make-keymap))
1266 (suppress-keymap Info-mode-map)
1267 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917
ER
1268 (define-key Info-mode-map " " 'Info-scroll-up)
1269 (define-key Info-mode-map "\C-m" 'Info-next-preorder)
552775bd
RS
1270 (define-key Info-mode-map "\t" 'Info-next-reference)
1271 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
e38e7367
RM
1272 (define-key Info-mode-map "1" 'Info-nth-menu-item)
1273 (define-key Info-mode-map "2" 'Info-nth-menu-item)
1274 (define-key Info-mode-map "3" 'Info-nth-menu-item)
1275 (define-key Info-mode-map "4" 'Info-nth-menu-item)
1276 (define-key Info-mode-map "5" 'Info-nth-menu-item)
1277 (define-key Info-mode-map "6" 'Info-nth-menu-item)
1278 (define-key Info-mode-map "7" 'Info-nth-menu-item)
1279 (define-key Info-mode-map "8" 'Info-nth-menu-item)
1280 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 1281 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
1282 (define-key Info-mode-map "?" 'Info-summary)
1283 (define-key Info-mode-map "]" 'Info-forward-node)
1284 (define-key Info-mode-map "[" 'Info-backward-node)
1285 (define-key Info-mode-map "<" 'Info-top-node)
1286 (define-key Info-mode-map ">" 'Info-final-node)
1287 (define-key Info-mode-map "b" 'beginning-of-buffer)
1288 (define-key Info-mode-map "d" 'Info-directory)
1289 (define-key Info-mode-map "e" 'Info-edit)
1290 (define-key Info-mode-map "f" 'Info-follow-reference)
1291 (define-key Info-mode-map "g" 'Info-goto-node)
1292 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 1293 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
1294 (define-key Info-mode-map "l" 'Info-last)
1295 (define-key Info-mode-map "m" 'Info-menu)
1296 (define-key Info-mode-map "n" 'Info-next)
1297 (define-key Info-mode-map "p" 'Info-prev)
1298 (define-key Info-mode-map "q" 'Info-exit)
1299 (define-key Info-mode-map "s" 'Info-search)
db0c9809 1300 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 1301 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 1302 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 1303 (define-key Info-mode-map "\177" 'Info-scroll-down)
ffb4fdde 1304 (define-key Info-mode-map [mouse-2] 'Info-follow-nearest-node)
aea2a8da 1305 )
a384cab3
JB
1306\f
1307;; Info mode is suitable only for specially formatted data.
1308(put 'info-mode 'mode-class 'special)
1309
1310(defun Info-mode ()
1311 "\\<Info-mode-map>
1312Info mode provides commands for browsing through the Info documentation tree.
1313Documentation in Info is divided into \"nodes\", each of which discusses
1314one topic and contains references to other nodes which discuss related
1315topics. Info has commands to follow the references and show you other nodes.
1316
1317\\[Info-help] Invoke the Info tutorial.
1318
1319Selecting other nodes:
1320\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 1321\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
1322\\[Info-up] Move \"up\" from this node.
1323\\[Info-menu] Pick menu item specified by name (or abbreviation).
1324 Picking a menu item causes another node to be selected.
aea2a8da 1325\\[Info-directory] Go to the Info directory node.
a384cab3
JB
1326\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
1327\\[Info-last] Move to the last node you were at.
1143a6b0
ER
1328\\[Info-index] Look up a topic in this file's Index and move to that node.
1329\\[Info-index-next] (comma) Move to the next match from a previous `i' command.
a384cab3
JB
1330
1331Moving within a node:
8884bb82 1332\\[Info-scroll-up] Normally, scroll forward a full screen. If the end of the buffer is
803eaf50 1333already visible, try to go to the next menu entry, or up if there is none.
8884bb82 1334\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
803eaf50
ER
1335already visible, try to go to the previous menu entry, or up if there is none.
1336\\[beginning-of-buffer] Go to beginning of node.
a384cab3 1337
a384cab3
JB
1338Advanced commands:
1339\\[Info-exit] Quit Info: reselect previously selected buffer.
1340\\[Info-edit] Edit contents of selected node.
13411 Pick first item in node's menu.
13422, 3, 4, 5 Pick second ... fifth item in node's menu.
1343\\[Info-goto-node] Move to node specified by name.
1344 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 1345\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 1346\\[Info-search] Search through this Info file for specified regexp,
803eaf50
ER
1347 and select the node in which the next occurrence is found.
1348\\[Info-next-preorder] Next-preorder; that is, try to go to the next menu item,
1349 and if that fails try to move up, and if that fails, tell user
552775bd
RS
1350 he/she is done reading.
1351\\[Info-next-reference] Move cursor to next cross-reference or menu item.
1352\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
1353 (kill-all-local-variables)
1354 (setq major-mode 'Info-mode)
1355 (setq mode-name "Info")
1356 (use-local-map Info-mode-map)
1357 (set-syntax-table text-mode-syntax-table)
1358 (setq local-abbrev-table text-mode-abbrev-table)
1359 (setq case-fold-search t)
1360 (setq buffer-read-only t)
a384cab3
JB
1361 (make-local-variable 'Info-current-file)
1362 (make-local-variable 'Info-current-subfile)
1363 (make-local-variable 'Info-current-node)
1364 (make-local-variable 'Info-tag-table-marker)
1365 (make-local-variable 'Info-history)
1143a6b0 1366 (make-local-variable 'Info-index-alternatives)
552775bd
RS
1367 (if (fboundp 'make-face)
1368 (progn
1369 (make-face 'info-node)
1370 (make-face 'info-menu-5)
1371 (make-face 'info-xref)
1372 (or (face-differs-from-default-p 'info-node)
1373 (if (face-differs-from-default-p 'bold-italic)
1374 (copy-face 'bold-italic 'info-node)
1375 (copy-face 'bold 'info-node)))
1376 (or (face-differs-from-default-p 'info-menu-5)
1377 (set-face-underline-p 'info-menu-5 t))
1378 (or (face-differs-from-default-p 'info-xref)
1379 (copy-face 'bold 'info-xref)))
1380 (setq Info-fontify nil))
a384cab3
JB
1381 (Info-set-mode-line)
1382 (run-hooks 'Info-mode-hook))
1383
1384(defvar Info-edit-map nil
1385 "Local keymap used within `e' command of Info.")
1386(if Info-edit-map
1387 nil
1388 (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
1389 (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
1390
1391;; Info-edit mode is suitable only for specially formatted data.
1392(put 'info-edit-mode 'mode-class 'special)
1393
1394(defun Info-edit-mode ()
1395 "Major mode for editing the contents of an Info node.
a426b157 1396Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
1397which returns to Info mode for browsing.
1398\\{Info-edit-map}"
1399 )
1400
1401(defun Info-edit ()
1402 "Edit the contents of this Info node.
1403Allowed only if variable `Info-enable-edit' is non-nil."
1404 (interactive)
1405 (or Info-enable-edit
1406 (error "Editing info nodes is not enabled"))
1407 (use-local-map Info-edit-map)
1408 (setq major-mode 'Info-edit-mode)
1409 (setq mode-name "Info Edit")
1410 (kill-local-variable 'mode-line-buffer-identification)
1411 (setq buffer-read-only nil)
1412 ;; Make mode line update.
1413 (set-buffer-modified-p (buffer-modified-p))
1414 (message (substitute-command-keys
9b7412a7 1415 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
1416
1417(defun Info-cease-edit ()
1418 "Finish editing Info node; switch back to Info proper."
1419 (interactive)
1420 ;; Do this first, so nothing has changed if user C-g's at query.
1421 (and (buffer-modified-p)
1422 (y-or-n-p "Save the file? ")
1423 (save-buffer))
1424 (use-local-map Info-mode-map)
1425 (setq major-mode 'Info-mode)
1426 (setq mode-name "Info")
1427 (Info-set-mode-line)
1428 (setq buffer-read-only t)
1429 ;; Make mode line update.
1430 (set-buffer-modified-p (buffer-modified-p))
1431 (and (marker-position Info-tag-table-marker)
1432 (buffer-modified-p)
1433 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1
RM
1434\f
1435(defun Info-find-emacs-command-nodes (command)
1436 "Return a list of locations documenting COMMAND in the Emacs Info manual.
1437The locations are of the format used in Info-history, i.e.
1438\(FILENAME NODENAME BUFFERPOS\)."
1439 (require 'info)
1440 (let ((where '())
1441 (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
1442 ":\\s *\\(.*\\)\\.$")))
1443 (save-excursion
1444 (Info-find-node "emacs" "Command Index")
1445 ;; Take the index node off the Info history.
1446 (setq Info-history (cdr Info-history))
1447 (goto-char (point-max))
1448 (while (re-search-backward cmd-desc nil t)
1449 (setq where (cons (list Info-current-file
1450 (buffer-substring
1451 (match-beginning 1)
1452 (match-end 1))
1453 0)
1454 where)))
1455 where)))
1456
1457;;;###autoload
1458(defun Info-goto-emacs-command-node (command)
e0568e86
RM
1459 "Go to the Info node in the Emacs manual for command COMMAND.
1460The command is found by looking up in Emacs manual's Command Index."
f0a8a3f1
RM
1461 (interactive "CFind documentation for command: ")
1462 (or (commandp command)
1463 (signal 'wrong-type-argument (list 'commandp command)))
1464 (let ((where (Info-find-emacs-command-nodes command)))
1465 (if where
1466 (let ((num-matches (length where)))
1467 ;; Get Info running, and pop to it in another window.
1468 (save-window-excursion
1469 (info))
1470 (pop-to-buffer "*info*")
1471 (Info-find-node (car (car where))
1472 (car (cdr (car where))))
1473 (if (> num-matches 1)
1474 (progn
1475 ;; Info-find-node already pushed (car where) onto
1476 ;; Info-history. Put the other nodes that were found on
1477 ;; the history.
1478 (setq Info-history (nconc (cdr where) Info-history))
1479 (message (substitute-command-keys
cedb118c
RS
1480 "Found %d other entr%s. Use \\[Info-last] to see %s.")
1481 (1- num-matches)
1482 (if (> num-matches 2) "ies" "y")
1483 (if (> num-matches 2) "them" "it")))))
f0a8a3f1 1484 (error "Couldn't find documentation for %s." command))))
f0a8a3f1
RM
1485
1486;;;###autoload
1487(defun Info-goto-emacs-key-command-node (key)
1488 "Go to the Info node in the Emacs manual the command bound to KEY, a string.
e0568e86
RM
1489Interactively, if the binding is execute-extended-command, a command is read.
1490The command is found by looking up in Emacs manual's Command Index."
f0a8a3f1
RM
1491 (interactive "kFind documentation for key:")
1492 (let ((command (key-binding key)))
1493 (cond ((null command)
9e5c2f50 1494 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
1495 ((and (interactive-p)
1496 (eq command 'execute-extended-command))
1497 (Info-goto-emacs-command-node
1498 (read-command "Find documentation for command: ")))
1499 (t
1500 (Info-goto-emacs-command-node command)))))
552775bd
RS
1501\f
1502(defun Info-fontify-node ()
1503 (save-excursion
1504 (let ((buffer-read-only nil))
1505 (goto-char (point-min))
1506 (if (looking-at "^File: [^,: \t]+,?[ \t]+")
1507 (progn
1508 (goto-char (match-end 0))
1509 (while
1510 (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
1511 (goto-char (match-end 0))
1512 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1513 'face 'info-xref)
1514 (put-text-property (match-beginning 1) (match-end 1)
1515 'mouse-face 'highlight))))
552775bd
RS
1516 (goto-char (point-min))
1517 (while (re-search-forward "\\*Note[ \n\t]*\\([^:]*\\):" nil t)
1518 (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
1519 nil
1520 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1521 'face 'info-xref)
1522 (put-text-property (match-beginning 1) (match-end 1)
1523 'mouse-face 'highlight)))
552775bd
RS
1524 (goto-char (point-min))
1525 (if (and (search-forward "\n* Menu:" nil t)
1526 (not (string-match "\\<Index\\>" Info-current-node))
1527 ;; Don't take time to annotate huge menus
9a87b430 1528 (< (- (point-max) (point)) 30000))
552775bd
RS
1529 (let ((n 0))
1530 (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
1531 (setq n (1+ n))
1532 (if (memq n '(5 9)) ; visual aids to help with 1-9 keys
1533 (put-text-property (match-beginning 0)
1534 (1+ (match-beginning 0))
1535 'face 'info-menu-5))
1536 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1537 'face 'info-node)
1538 (put-text-property (match-beginning 1) (match-end 1)
1539 'mouse-face 'highlight))))
552775bd 1540 (set-buffer-modified-p nil))))
49116ac0
JB
1541
1542(provide 'info)
1543
1a06eabd 1544;;; info.el ends here