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