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