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