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