(compilation-finish-functions): New variable.
[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.
53c63926 304 (pop-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))))))
debcea0c 467 (or (cdr dirs) (setq Info-dir-contents-directory (car dirs)))
825d6f08
RS
468 (setq dirs (cdr dirs))))
469
4db579ab 470 (or buffers
81e14cb2 471 (error "Can't find the Info directory node"))
44c327f9 472 ;; Distinguish the dir file that comes with Emacs from all the
f4008b6e
RS
473 ;; others. Yes, that is really what this is supposed to do.
474 ;; If it doesn't work, fix it.
7ea13762
RS
475 (setq buffer (car buffers)
476 others (cdr buffers))
44c327f9 477
debcea0c
KH
478 ;; Insert the entire original dir file as a start; note that we've
479 ;; already saved its default directory to use as the default
480 ;; directory for the whole concatenation.
7ea13762 481 (insert-buffer buffer)
44c327f9 482
7ea13762
RS
483 ;; Look at each of the other buffers one by one.
484 (while others
485 (let ((other (car others)))
486 ;; In each, find all the menus.
487 (save-excursion
488 (set-buffer other)
489 (goto-char (point-min))
490 ;; Find each menu, and add an elt to NODES for it.
491 (while (re-search-forward "^\\* Menu:" nil t)
492 (let (beg nodename end)
493 (forward-line 1)
494 (setq beg (point))
3a6ade8a 495 (search-backward "\n\^_")
7ea13762
RS
496 (search-forward "Node: ")
497 (setq nodename (Info-following-node-name))
3a6ade8a 498 (search-forward "\n\^_" nil 'move)
7ea13762
RS
499 (beginning-of-line)
500 (setq end (point))
501 (setq nodes (cons (list nodename other beg end) nodes))))))
502 (setq others (cdr others)))
503 ;; Add to the main menu a menu item for each other node.
504 (re-search-forward "^\\* Menu:")
505 (forward-line 1)
506 (let ((menu-items '("top"))
507 (nodes nodes)
508 (case-fold-search t)
3a6ade8a 509 (end (save-excursion (search-forward "\^_" nil t) (point))))
7ea13762
RS
510 (while nodes
511 (let ((nodename (car (car nodes))))
c3a29d70
RS
512 (save-excursion
513 (or (member (downcase nodename) menu-items)
514 (re-search-forward (concat "^\\* "
515 (regexp-quote nodename)
516 "::")
517 end t)
518 (progn
519 (insert "* " nodename "::" "\n")
520 (setq menu-items (cons nodename menu-items))))))
7ea13762
RS
521 (setq nodes (cdr nodes))))
522 ;; Now take each node of each of the other buffers
523 ;; and merge it into the main buffer.
524 (while nodes
525 (let ((nodename (car (car nodes))))
526 (goto-char (point-min))
527 ;; Find the like-named node in the main buffer.
3a6ade8a 528 (if (re-search-forward (concat "\n\^_.*\n.*Node: "
7ea13762
RS
529 (regexp-quote nodename)
530 "[,\n\t]")
531 nil t)
532 (progn
3a6ade8a 533 (search-forward "\n\^_" nil 'move)
25869acf
RS
534 (beginning-of-line)
535 (insert "\n"))
7ea13762
RS
536 ;; If none exists, add one.
537 (goto-char (point-max))
dfbf6104 538 (insert "\^_\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
7ea13762
RS
539 ;; Merge the text from the other buffer's menu
540 ;; into the menu in the like-named node in the main buffer.
25869acf 541 (apply 'insert-buffer-substring (cdr (car nodes))))
7ea13762
RS
542 (setq nodes (cdr nodes)))
543 ;; Kill all the buffers we just made.
544 (while buffers
545 (kill-buffer (car buffers))
825d6f08
RS
546 (setq buffers (cdr buffers)))
547 (message "Composing main Info directory...done"))
44c327f9
JB
548 (setq Info-dir-contents (buffer-string)))
549 (setq default-directory Info-dir-contents-directory))
7ea13762 550
a384cab3
JB
551(defun Info-read-subfile (nodepos)
552 (set-buffer (marker-buffer Info-tag-table-marker))
553 (goto-char (point-min))
554 (search-forward "\n\^_")
555 (let (lastfilepos
556 lastfilename)
557 (forward-line 2)
558 (catch 'foo
559 (while (not (looking-at "\^_"))
560 (if (not (eolp))
561 (let ((beg (point))
562 thisfilepos thisfilename)
563 (search-forward ": ")
564 (setq thisfilename (buffer-substring beg (- (point) 2)))
565 (setq thisfilepos (read (current-buffer)))
566 ;; read in version 19 stops at the end of number.
567 ;; Advance to the next line.
568 (forward-line 1)
569 (if (> thisfilepos nodepos)
570 (throw 'foo t))
571 (setq lastfilename thisfilename)
572 (setq lastfilepos thisfilepos))
573 (forward-line 1))))
574 (set-buffer (get-buffer "*info*"))
575 (or (equal Info-current-subfile lastfilename)
576 (let ((buffer-read-only nil))
577 (setq buffer-file-name nil)
578 (widen)
579 (erase-buffer)
1143a6b0 580 (info-insert-file-contents lastfilename)
a384cab3
JB
581 (set-buffer-modified-p nil)
582 (setq Info-current-subfile lastfilename)))
583 (goto-char (point-min))
584 (search-forward "\n\^_")
585 (+ (- nodepos lastfilepos) (point))))
586
587;; Select the info node that point is in.
588(defun Info-select-node ()
589 (save-excursion
590 ;; Find beginning of node.
591 (search-backward "\n\^_")
592 (forward-line 2)
593 ;; Get nodename spelled as it is in the node.
594 (re-search-forward "Node:[ \t]*")
595 (setq Info-current-node
70d78eb6
RS
596 (buffer-substring-no-properties (point)
597 (progn
598 (skip-chars-forward "^,\t\n")
599 (point))))
a384cab3
JB
600 (Info-set-mode-line)
601 ;; Find the end of it, and narrow.
602 (beginning-of-line)
603 (let (active-expression)
604 (narrow-to-region (point)
605 (if (re-search-forward "\n[\^_\f]" nil t)
606 (prog1
607 (1- (point))
608 (if (looking-at "[\n\^_\f]*execute: ")
609 (progn
610 (goto-char (match-end 0))
611 (setq active-expression
612 (read (current-buffer))))))
613 (point-max)))
f05f43e7 614 (if Info-enable-active-nodes (eval active-expression))
552775bd 615 (if Info-fontify (Info-fontify-node))
f05f43e7 616 (run-hooks 'Info-selection-hook))))
a384cab3
JB
617
618(defun Info-set-mode-line ()
619 (setq mode-line-buffer-identification
620 (concat
621 "Info: ("
622 (if Info-current-file
623 (file-name-nondirectory Info-current-file)
624 "")
625 ")"
626 (or Info-current-node ""))))
627\f
628;; Go to an info node specified with a filename-and-nodename string
629;; of the sort that is found in pointers in nodes.
630
631(defun Info-goto-node (nodename)
632 "Go to info node named NAME. Give just NODENAME or (FILENAME)NODENAME."
552775bd 633 (interactive (list (Info-read-node-name "Goto node: ")))
a384cab3
JB
634 (let (filename)
635 (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)"
636 nodename)
637 (setq filename (if (= (match-beginning 1) (match-end 1))
638 ""
639 (substring nodename (match-beginning 2) (match-end 2)))
640 nodename (substring nodename (match-beginning 3) (match-end 3)))
641 (let ((trim (string-match "\\s *\\'" filename)))
642 (if trim (setq filename (substring filename 0 trim))))
643 (let ((trim (string-match "\\s *\\'" nodename)))
644 (if trim (setq nodename (substring nodename 0 trim))))
e579232c 645 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
646 (Info-find-node (if (equal filename "") nil filename)
647 (if (equal nodename "") "Top" nodename))))
552775bd 648
03524be6
RS
649;; This function is used as the "completion table" while reading a node name.
650;; It does completion using the alist in completion-table
651;; unless STRING starts with an open-paren.
652(defun Info-read-node-name-1 (string predicate code)
653 (let ((no-completion (and (> (length string) 0) (eq (aref string 0) ?\())))
654 (cond ((eq code nil)
655 (if no-completion
656 string
657 (try-completion string completion-table predicate)))
658 ((eq code t)
659 (if no-completion
660 nil
661 (all-completions string completion-table predicate)))
662 ((eq code 'lambda)
663 (if no-completion
664 t
665 (assoc string completion-table))))))
666
552775bd
RS
667(defun Info-read-node-name (prompt &optional default)
668 (let* ((completion-ignore-case t)
03524be6
RS
669 (completion-table (Info-build-node-completions))
670 (nodename (completing-read prompt 'Info-read-node-name-1)))
552775bd
RS
671 (if (equal nodename "")
672 (or default
673 (Info-read-node-name prompt))
674 nodename)))
675
676(defun Info-build-node-completions ()
677 (or Info-current-file-completions
678 (let ((compl nil))
679 (save-excursion
680 (save-restriction
681 (if (marker-buffer Info-tag-table-marker)
682 (progn
683 (set-buffer (marker-buffer Info-tag-table-marker))
cedb118c 684 (widen)
552775bd
RS
685 (goto-char Info-tag-table-marker)
686 (while (re-search-forward "\nNode: \\(.*\\)\177" nil t)
687 (setq compl
688 (cons (list (buffer-substring (match-beginning 1)
689 (match-end 1)))
690 compl))))
691 (widen)
692 (goto-char (point-min))
693 (while (search-forward "\n\^_" nil t)
694 (forward-line 1)
695 (let ((beg (point)))
696 (forward-line 1)
697 (if (re-search-backward "Node: *\\([^,\n]*\\) *[,\n\t]"
698 beg t)
699 (setq compl
700 (cons (list (buffer-substring (match-beginning 1)
701 (match-end 1)))
702 compl))))))))
703 (setq Info-current-file-completions compl))))
a384cab3 704\f
aea2a8da
JB
705(defun Info-restore-point (hl)
706 "If this node has been visited, restore the point value when we left."
cedb118c
RS
707 (while hl
708 (if (and (equal (nth 0 (car hl)) Info-current-file)
e90d1271
RS
709 ;; Use string-equal, not equal, to ignore text props.
710 (string-equal (nth 1 (car hl)) Info-current-node))
cedb118c 711 (progn
d96504df
KH
712 (goto-char (nth 2 (car hl)))
713 (setq hl nil)) ;terminate the while at next iter
cedb118c 714 (setq hl (cdr hl)))))
aea2a8da 715\f
a384cab3 716(defvar Info-last-search nil
d5913bf6 717 "Default regexp for \\<Info-mode-map>\\[Info-search] command to search for.")
a384cab3
JB
718
719(defun Info-search (regexp)
720 "Search for REGEXP, starting from point, and select node it's found in."
721 (interactive "sSearch (regexp): ")
e579232c 722 (if transient-mark-mode (deactivate-mark))
a384cab3
JB
723 (if (equal regexp "")
724 (setq regexp Info-last-search)
725 (setq Info-last-search regexp))
726 (let ((found ()) current
727 (onode Info-current-node)
728 (ofile Info-current-file)
729 (opoint (point))
730 (osubfile Info-current-subfile))
731 (save-excursion
732 (save-restriction
733 (widen)
734 (if (null Info-current-subfile)
735 (progn (re-search-forward regexp) (setq found (point)))
736 (condition-case err
737 (progn (re-search-forward regexp) (setq found (point)))
738 (search-failed nil)))))
739 (if (not found) ;can only happen in subfile case -- else would have erred
740 (unwind-protect
741 (let ((list ()))
742 (set-buffer (marker-buffer Info-tag-table-marker))
743 (goto-char (point-min))
744 (search-forward "\n\^_\nIndirect:")
745 (save-restriction
746 (narrow-to-region (point)
747 (progn (search-forward "\n\^_")
748 (1- (point))))
749 (goto-char (point-min))
750 (search-forward (concat "\n" osubfile ": "))
751 (beginning-of-line)
752 (while (not (eobp))
753 (re-search-forward "\\(^.*\\): [0-9]+$")
754 (goto-char (+ (match-end 1) 2))
755 (setq list (cons (cons (read (current-buffer))
756 (buffer-substring (match-beginning 1)
757 (match-end 1)))
758 list))
759 (goto-char (1+ (match-end 0))))
760 (setq list (nreverse list)
761 current (car (car list))
762 list (cdr list)))
763 (while list
764 (message "Searching subfile %s..." (cdr (car list)))
765 (Info-read-subfile (car (car list)))
766 (setq list (cdr list))
dfbf6104 767;; (goto-char (point-min))
a384cab3
JB
768 (if (re-search-forward regexp nil t)
769 (setq found (point) list ())))
770 (if found
771 (message "")
772 (signal 'search-failed (list regexp))))
773 (if (not found)
774 (progn (Info-read-subfile opoint)
775 (goto-char opoint)
776 (Info-select-node)))))
777 (widen)
778 (goto-char found)
779 (Info-select-node)
e90d1271
RS
780 ;; Use string-equal, not equal, to ignore text props.
781 (or (and (string-equal onode Info-current-node)
a384cab3
JB
782 (equal ofile Info-current-file))
783 (setq Info-history (cons (list ofile onode opoint)
784 Info-history)))))
785\f
786;; Extract the value of the node-pointer named NAME.
787;; If there is none, use ERRORNAME in the error message;
788;; if ERRORNAME is nil, just return nil.
789(defun Info-extract-pointer (name &optional errorname)
790 (save-excursion
791 (goto-char (point-min))
792 (forward-line 1)
793 (if (re-search-backward (concat name ":") nil t)
794 (progn
795 (goto-char (match-end 0))
796 (Info-following-node-name))
797 (if (eq errorname t)
798 nil
ce35dad9 799 (error "Node has no %s" (capitalize (or errorname name)))))))
a384cab3 800
7ea13762
RS
801;; Return the node name in the buffer following point.
802;; ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
803;; saying which chas may appear in the node name.
a384cab3
JB
804(defun Info-following-node-name (&optional allowedchars)
805 (skip-chars-forward " \t")
70d78eb6 806 (buffer-substring-no-properties
a384cab3
JB
807 (point)
808 (progn
809 (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
810 (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
811 (if (looking-at "(")
812 (skip-chars-forward "^)")))
813 (skip-chars-backward " ")
814 (point))))
815
816(defun Info-next ()
817 "Go to the next node of this node."
818 (interactive)
819 (Info-goto-node (Info-extract-pointer "next")))
820
821(defun Info-prev ()
822 "Go to the previous node of this node."
823 (interactive)
824 (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))
825
826(defun Info-up ()
827 "Go to the superior node of this node."
828 (interactive)
aea2a8da
JB
829 (Info-goto-node (Info-extract-pointer "up"))
830 (Info-restore-point Info-history))
a384cab3
JB
831
832(defun Info-last ()
833 "Go back to the last node visited."
834 (interactive)
835 (or Info-history
836 (error "This is the first Info node you looked at"))
837 (let (filename nodename opoint)
838 (setq filename (car (car Info-history)))
839 (setq nodename (car (cdr (car Info-history))))
840 (setq opoint (car (cdr (cdr (car Info-history)))))
841 (setq Info-history (cdr Info-history))
842 (Info-find-node filename nodename)
843 (setq Info-history (cdr Info-history))
844 (goto-char opoint)))
845
846(defun Info-directory ()
847 "Go to the Info directory node."
848 (interactive)
849 (Info-find-node "dir" "top"))
850\f
851(defun Info-follow-reference (footnotename)
852 "Follow cross reference named NAME to the node it refers to.
853NAME may be an abbreviation of the reference name."
854 (interactive
855 (let ((completion-ignore-case t)
67bc89ab 856 completions default alt-default (start-point (point)) str i bol eol)
a384cab3 857 (save-excursion
67bc89ab
RS
858 ;; Store end and beginning of line.
859 (end-of-line)
860 (setq eol (point))
861 (beginning-of-line)
862 (setq bol (point))
863
a384cab3
JB
864 (goto-char (point-min))
865 (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t)
866 (setq str (buffer-substring
867 (match-beginning 1)
868 (1- (point))))
869 ;; See if this one should be the default.
870 (and (null default)
1f179e27 871 (<= (match-beginning 0) start-point)
a384cab3
JB
872 (<= start-point (point))
873 (setq default t))
67bc89ab
RS
874 ;; See if this one should be the alternate default.
875 (and (null alt-default)
876 (and (<= bol (match-beginning 0))
877 (<= (point) eol))
878 (setq alt-default t))
a384cab3
JB
879 (setq i 0)
880 (while (setq i (string-match "[ \n\t]+" str i))
881 (setq str (concat (substring str 0 i) " "
882 (substring str (match-end 0))))
883 (setq i (1+ i)))
884 ;; Record as a completion and perhaps as default.
885 (if (eq default t) (setq default str))
67bc89ab 886 (if (eq alt-default t) (setq alt-default str))
a384cab3
JB
887 (setq completions
888 (cons (cons str nil)
889 completions))))
67bc89ab
RS
890 ;; If no good default was found, try an alternate.
891 (or default
892 (setq default alt-default))
893 ;; If only one cross-reference found, then make it default.
894 (if (eq (length completions) 1)
895 (setq default (car (car completions))))
a384cab3 896 (if completions
b0ebdfe5
RS
897 (let ((input (completing-read (if default
898 (concat "Follow reference named: ("
899 default ") ")
900 "Follow reference named: ")
901 completions nil t)))
902 (list (if (equal input "")
903 default input)))
a384cab3 904 (error "No cross-references in this node"))))
3ffefa79 905 (let (target beg i (str (concat "\\*note " (regexp-quote footnotename))))
a384cab3
JB
906 (while (setq i (string-match " " str i))
907 (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i))))
908 (setq i (+ i 6)))
909 (save-excursion
910 (goto-char (point-min))
911 (or (re-search-forward str nil t)
912 (error "No cross-reference named %s" footnotename))
913 (goto-char (+ (match-beginning 0) 5))
914 (setq target
915 (Info-extract-menu-node-name "Bad format cross reference" t)))
916 (while (setq i (string-match "[ \t\n]+" target i))
917 (setq target (concat (substring target 0 i) " "
918 (substring target (match-end 0))))
919 (setq i (+ i 1)))
920 (Info-goto-node target)))
921
922(defun Info-extract-menu-node-name (&optional errmessage multi-line)
923 (skip-chars-forward " \t\n")
924 (let ((beg (point))
925 str i)
926 (skip-chars-forward "^:")
927 (forward-char 1)
928 (setq str
929 (if (looking-at ":")
70d78eb6 930 (buffer-substring-no-properties beg (1- (point)))
a384cab3
JB
931 (skip-chars-forward " \t\n")
932 (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n"))))
933 (while (setq i (string-match "\n" str i))
934 (aset str i ?\ ))
2e52ff59
RS
935 ;; Collapse multiple spaces.
936 (while (string-match " +" str)
937 (setq str (replace-match " " t t str)))
a384cab3
JB
938 str))
939
e8adde3f 940;; No one calls this.
9e5c2f50
RS
941;;(defun Info-menu-item-sequence (list)
942;; (while list
e8adde3f 943;; (Info-menu (car list))
9e5c2f50 944;; (setq list (cdr list))))
a384cab3 945
e8adde3f
RS
946(defun Info-complete-menu-item (string predicate action)
947 (let ((case-fold-search t))
948 (cond ((eq action nil)
949 (let (completions
950 (pattern (concat "\n\\* \\("
951 (regexp-quote string)
952 "[^:\t\n]*\\):")))
953 (save-excursion
954 (set-buffer Info-complete-menu-buffer)
955 (goto-char (point-min))
763da090 956 (search-forward "\n* Menu:")
e8adde3f
RS
957 (while (re-search-forward pattern nil t)
958 (setq completions (cons (cons (format "%s"
959 (buffer-substring
960 (match-beginning 1)
961 (match-end 1)))
962 (match-beginning 1))
963 completions))))
964 (try-completion string completions predicate)))
965 ((eq action t)
966 (let (completions
967 (pattern (concat "\n\\* \\("
968 (regexp-quote string)
969 "[^:\t\n]*\\):")))
970 (save-excursion
971 (set-buffer Info-complete-menu-buffer)
972 (goto-char (point-min))
763da090 973 (search-forward "\n* Menu:")
e8adde3f
RS
974 (while (re-search-forward pattern nil t)
975 (setq completions (cons (cons (format "%s"
976 (buffer-substring
977 (match-beginning 1)
978 (match-end 1)))
979 (match-beginning 1))
980 completions))))
981 (all-completions string completions predicate)))
982 (t
983 (save-excursion
984 (set-buffer Info-complete-menu-buffer)
985 (goto-char (point-min))
763da090 986 (search-forward "\n* Menu:")
e8adde3f
RS
987 (re-search-forward (concat "\n\\* "
988 (regexp-quote string)
989 ":")
990 nil t))))))
991
992
a384cab3
JB
993(defun Info-menu (menu-item)
994 "Go to node for menu item named (or abbreviated) NAME.
995Completion is allowed, and the menu item point is on is the default."
996 (interactive
997 (let ((completions '())
998 ;; If point is within a menu item, use that item as the default
999 (default nil)
1000 (p (point))
211d6309 1001 beg
a384cab3
JB
1002 (last nil))
1003 (save-excursion
1004 (goto-char (point-min))
1005 (if (not (search-forward "\n* menu:" nil t))
1006 (error "No menu in this node"))
e8adde3f
RS
1007 (setq beg (point))
1008 (and (< (point) p)
1009 (save-excursion
1010 (goto-char p)
1011 (end-of-line)
1012 (re-search-backward "\n\\* \\([^:\t\n]*\\):" beg t)
1013 (setq default (format "%s" (buffer-substring
1014 (match-beginning 1)
1015 (match-end 1)))))))
a384cab3
JB
1016 (let ((item nil))
1017 (while (null item)
e8adde3f
RS
1018 (setq item (let ((completion-ignore-case t)
1019 (Info-complete-menu-buffer (current-buffer)))
a384cab3
JB
1020 (completing-read (if default
1021 (format "Menu item (default %s): "
1022 default)
1023 "Menu item: ")
e8adde3f 1024 'Info-complete-menu-item nil t)))
aea2a8da
JB
1025 ;; we rely on the fact that completing-read accepts an input
1026 ;; of "" even when the require-match argument is true and ""
1027 ;; is not a valid possibility
a384cab3
JB
1028 (if (string= item "")
1029 (if default
1030 (setq item default)
1031 ;; ask again
1032 (setq item nil))))
1033 (list item))))
1034 ;; there is a problem here in that if several menu items have the same
1035 ;; name you can only go to the node of the first with this command.
1036 (Info-goto-node (Info-extract-menu-item menu-item)))
1037
1038(defun Info-extract-menu-item (menu-item)
1039 (setq menu-item (regexp-quote menu-item))
1040 (save-excursion
1041 (goto-char (point-min))
1042 (or (search-forward "\n* menu:" nil t)
1043 (error "No menu in this node"))
de3fa0b2
RS
1044 (or (re-search-forward (concat "\n\\* " menu-item ":") nil t)
1045 (re-search-forward (concat "\n\\* " menu-item) nil t)
a384cab3
JB
1046 (error "No such item in menu"))
1047 (beginning-of-line)
1048 (forward-char 2)
1049 (Info-extract-menu-node-name)))
1050
1051;; If COUNT is nil, use the last item in the menu.
1052(defun Info-extract-menu-counting (count)
1053 (save-excursion
1054 (goto-char (point-min))
1055 (or (search-forward "\n* menu:" nil t)
1056 (error "No menu in this node"))
1057 (if count
1058 (or (search-forward "\n* " nil t count)
1059 (error "Too few items in menu"))
1060 (while (search-forward "\n* " nil t)
1061 nil))
1062 (Info-extract-menu-node-name)))
1063
e38e7367
RM
1064(defun Info-nth-menu-item ()
1065 "Go to the node of the Nth menu item.
1066N is the digit argument used to invoke this command."
a384cab3 1067 (interactive)
e38e7367
RM
1068 (Info-goto-node
1069 (Info-extract-menu-counting
1070 (- (aref (this-command-keys) (1- (length (this-command-keys)))) ?0))))
a384cab3
JB
1071
1072(defun Info-top-node ()
1073 "Go to the Top node of this file."
1074 (interactive)
1075 (Info-goto-node "Top"))
1076
1077(defun Info-final-node ()
1078 "Go to the final node in this file."
1079 (interactive)
1080 (Info-goto-node "Top")
1081 (let (Info-history)
1082 ;; Go to the last node in the menu of Top.
1083 (Info-goto-node (Info-extract-menu-counting nil))
1084 ;; If the last node in the menu is not last in pointer structure,
1085 ;; move forward until we can't go any farther.
1086 (while (Info-forward-node t t) nil)
1087 ;; Then keep moving down to last subnode, unless we reach an index.
1088 (while (and (not (string-match "\\<index\\>" Info-current-node))
1089 (save-excursion (search-forward "\n* Menu:" nil t)))
1090 (Info-goto-node (Info-extract-menu-counting nil)))))
1091
1092(defun Info-forward-node (&optional not-down no-error)
1093 "Go forward one node, considering all nodes as forming one sequence."
1094 (interactive)
1095 (goto-char (point-min))
1096 (forward-line 1)
1097 ;; three possibilities, in order of priority:
1098 ;; 1. next node is in a menu in this node (but not in an index)
1099 ;; 2. next node is next at same level
1100 ;; 3. next node is up and next
1101 (cond ((and (not not-down)
1102 (save-excursion (search-forward "\n* menu:" nil t))
1103 (not (string-match "\\<index\\>" Info-current-node)))
463f48f4 1104 (Info-goto-node (Info-extract-menu-counting 1))
a384cab3
JB
1105 t)
1106 ((save-excursion (search-backward "next:" nil t))
1107 (Info-next)
1108 t)
1109 ((and (save-excursion (search-backward "up:" nil t))
e90d1271
RS
1110 ;; Use string-equal, not equal, to ignore text props.
1111 (not (string-equal (downcase (Info-extract-pointer "up"))
1112 "top")))
a384cab3
JB
1113 (let ((old-node Info-current-node))
1114 (Info-up)
1115 (let (Info-history success)
1116 (unwind-protect
1117 (setq success (Info-forward-node t no-error))
1118 (or success (Info-goto-node old-node))))))
1119 (no-error nil)
1120 (t (error "No pointer forward from this node"))))
1121
1122(defun Info-backward-node ()
1123 "Go backward one node, considering all nodes as forming one sequence."
1124 (interactive)
1125 (let ((prevnode (Info-extract-pointer "prev[ious]*" t))
1126 (upnode (Info-extract-pointer "up" t)))
1127 (cond ((and upnode (string-match "(" upnode))
1128 (error "First node in file"))
1129 ((and upnode (or (null prevnode)
e90d1271
RS
1130 ;; Use string-equal, not equal,
1131 ;; to ignore text properties.
1132 (string-equal (downcase prevnode)
1133 (downcase upnode))))
a384cab3
JB
1134 (Info-up))
1135 (prevnode
1136 ;; If we move back at the same level,
1137 ;; go down to find the last subnode*.
1138 (Info-prev)
1139 (let (Info-history)
1140 (while (and (not (string-match "\\<index\\>" Info-current-node))
1141 (save-excursion (search-forward "\n* Menu:" nil t)))
1142 (Info-goto-node (Info-extract-menu-counting nil)))))
1143 (t
1144 (error "No pointer backward from this node")))))
1145
1146(defun Info-exit ()
1147 "Exit Info by selecting some other buffer."
1148 (interactive)
552775bd
RS
1149 (if Info-standalone
1150 (save-buffers-kill-emacs)
1151 (switch-to-buffer (prog1 (other-buffer (current-buffer))
1152 (bury-buffer (current-buffer))))))
a384cab3 1153
253db917
ER
1154(defun Info-next-menu-item ()
1155 (interactive)
1156 (save-excursion
1157 (forward-line -1)
1158 (search-forward "\n* menu:" nil t)
1159 (or (search-forward "\n* " nil t)
1160 (error "No more items in menu"))
1161 (Info-goto-node (Info-extract-menu-node-name))))
1162
1163(defun Info-last-menu-item ()
1164 (interactive)
1165 (save-excursion
1166 (forward-line 1)
0a56332b
RS
1167 (let ((beg (save-excursion
1168 (and (search-backward "\n* menu:" nil t)
1169 (point)))))
1170 (or (and beg (search-backward "\n* " beg t))
1171 (error "No previous items in menu")))
1172 (Info-goto-node (save-excursion
1173 (goto-char (match-end 0))
1174 (Info-extract-menu-node-name)))))
253db917 1175
552775bd 1176(defmacro Info-no-error (&rest body)
253db917
ER
1177 (list 'condition-case nil (cons 'progn (append body '(t))) '(error nil)))
1178
1179(defun Info-next-preorder ()
3c9179ea
RS
1180 "Go to the next subnode or the next node, or go up a level."
1181 (interactive)
1182 (cond ((Info-no-error (Info-next-menu-item)))
1183 ((Info-no-error (Info-next)))
1184 ((Info-no-error (Info-up))
ed690657
KH
1185 ;; Since we have already gone thru all the items in this menu,
1186 ;; go up to the end of this node.
b54d0f0e
RS
1187 (goto-char (point-max))
1188 ;; Since logically we are done with the node with that menu,
1189 ;; move on from it.
1190 (Info-next-preorder))
3c9179ea
RS
1191 (t
1192 (error "No more nodes"))))
253db917
ER
1193
1194(defun Info-last-preorder ()
1195 "Go to the last node, popping up a level if there is none."
1196 (interactive)
0a56332b
RS
1197 (cond ((Info-no-error
1198 (Info-last-menu-item)
1199 ;; If we go down a menu item, go to the end of the node
1200 ;; so we can scroll back through it.
ed690657 1201 (goto-char (point-max)))
b54d0f0e
RS
1202 ;; Keep going down, as long as there are nested menu nodes.
1203 (while (Info-no-error
1204 (Info-last-menu-item)
1205 ;; If we go down a menu item, go to the end of the node
1206 ;; so we can scroll back through it.
1207 (goto-char (point-max))))
ed690657
KH
1208 (recenter -1))
1209 ((Info-no-error (Info-prev))
1210 (goto-char (point-max))
b54d0f0e
RS
1211 (while (Info-no-error
1212 (Info-last-menu-item)
1213 ;; If we go down a menu item, go to the end of the node
1214 ;; so we can scroll back through it.
1215 (goto-char (point-max))))
ed690657
KH
1216 (recenter -1))
1217 ((Info-no-error (Info-up))
1218 (goto-char (point-min))
1219 (or (search-forward "\n* Menu:" nil t)
1220 (goto-char (point-max))))
1221 (t (error "No previous nodes"))))
253db917
ER
1222
1223(defun Info-scroll-up ()
3f32dc86
RS
1224 "Scroll one screenful forward in Info, considering all nodes as one sequence.
1225Once you scroll far enough in a node that its menu appears on the screen,
1226the next scroll moves into its first subnode. When you scroll past
ed690657 1227the end of a node, that goes to the next node or back up to the parent node."
253db917 1228 (interactive)
0a56332b
RS
1229 (if (or (< (window-start) (point-min))
1230 (> (window-start) (point-max)))
1231 (set-window-start (selected-window) (point)))
1232 (let ((virtual-end (save-excursion
1233 (goto-char (point-min))
1234 (if (search-forward "\n* Menu:" nil t)
1235 (point)
1236 (point-max)))))
1237 (if (or (< virtual-end (window-start))
1238 (pos-visible-in-window-p virtual-end))
1239 (Info-next-preorder)
1240 (scroll-up))))
253db917
ER
1241
1242(defun Info-scroll-down ()
3f32dc86 1243 "Scroll one screenful back in Info, considering all nodes as one sequence.
ed690657
KH
1244Within the menu of a node, this goes to its last subnode.
1245When you scroll past the beginning of a node, that goes to the
1246previous node or back up to the parent node."
253db917 1247 (interactive)
0a56332b
RS
1248 (if (or (< (window-start) (point-min))
1249 (> (window-start) (point-max)))
1250 (set-window-start (selected-window) (point)))
b54d0f0e
RS
1251 (let* ((current-point (point))
1252 (virtual-end (save-excursion
1253 (beginning-of-line)
1254 (setq current-point (point))
1255 (goto-char (point-min))
1256 (search-forward "\n* Menu:"
1257 current-point
1258 t))))
0a56332b
RS
1259 (if (or virtual-end (pos-visible-in-window-p (point-min)))
1260 (Info-last-preorder)
1261 (scroll-down))))
253db917 1262
56cda6f5 1263(defun Info-next-reference (&optional recur)
552775bd
RS
1264 "Move cursor to the next cross-reference or menu item in the node."
1265 (interactive)
1266 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1267 (old-pt (point)))
1268 (or (eobp) (forward-char 1))
1269 (or (re-search-forward pat nil t)
1270 (progn
1271 (goto-char (point-min))
1272 (or (re-search-forward pat nil t)
1273 (progn
1274 (goto-char old-pt)
1275 (error "No cross references in this node")))))
1276 (goto-char (match-beginning 0))
1277 (if (looking-at "\\* Menu:")
56cda6f5
RS
1278 (if recur
1279 (error "No cross references in this node")
1280 (Info-next-reference t)))))
552775bd 1281
56cda6f5 1282(defun Info-prev-reference (&optional recur)
552775bd
RS
1283 "Move cursor to the previous cross-reference or menu item in the node."
1284 (interactive)
1285 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1286 (old-pt (point)))
1287 (or (re-search-backward pat nil t)
1288 (progn
1289 (goto-char (point-max))
1290 (or (re-search-backward pat nil t)
1291 (progn
1292 (goto-char old-pt)
1293 (error "No cross references in this node")))))
1294 (goto-char (match-beginning 0))
1295 (if (looking-at "\\* Menu:")
56cda6f5
RS
1296 (if recur
1297 (error "No cross references in this node")
1298 (Info-prev-reference t)))))
552775bd 1299
1143a6b0
ER
1300(defun Info-index (topic)
1301 "Look up a string in the index for this file.
1302The index is defined as the first node in the top-level menu whose
1303name contains the word \"Index\", plus any immediately following
1304nodes whose names also contain the word \"Index\".
1305If there are no exact matches to the specified topic, this chooses
1306the first match which is a case-insensitive substring of a topic.
1307Use the `,' command to see the other matches.
1308Give a blank topic name to go to the Index node itself."
1309 (interactive "sIndex topic: ")
1310 (let ((orignode Info-current-node)
1311 (rnode nil)
b4d8de7d 1312 (pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1143a6b0
ER
1313 (regexp-quote topic)))
1314 node)
1315 (Info-goto-node "Top")
1316 (or (search-forward "\n* menu:" nil t)
1317 (error "No index"))
1318 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1319 (error "No index"))
1320 (goto-char (match-beginning 1))
c696ac76
RS
1321 ;; Here, and subsequently in this function,
1322 ;; we bind Info-history to nil for internal node-switches
1323 ;; so that we don't put junk in the history.
1324 ;; In the first Info-goto-node call, above, we do update the history
1325 ;; because that is what the user's previous node choice into it.
1326 (let ((Info-history nil))
1143a6b0
ER
1327 (Info-goto-node (Info-extract-menu-node-name)))
1328 (or (equal topic "")
1329 (let ((matches nil)
1330 (exact nil)
c696ac76 1331 (Info-history nil)
1143a6b0
ER
1332 found)
1333 (while
1334 (progn
1335 (goto-char (point-min))
1336 (while (re-search-forward pattern nil t)
1337 (setq matches
1338 (cons (list (buffer-substring (match-beginning 1)
1339 (match-end 1))
1340 (buffer-substring (match-beginning 2)
1341 (match-end 2))
1342 Info-current-node
1343 (string-to-int (concat "0"
1344 (buffer-substring
1345 (match-beginning 3)
1346 (match-end 3)))))
1347 matches)))
1348 (and (setq node (Info-extract-pointer "next" t))
1349 (string-match "\\<Index\\>" node)))
1350 (Info-goto-node node))
1351 (or matches
1352 (progn
81e14cb2 1353 (Info-goto-node orignode)
920bdaab 1354 (error "No `%s' in index" topic)))
1143a6b0
ER
1355 ;; Here it is a feature that assoc is case-sensitive.
1356 (while (setq found (assoc topic matches))
1357 (setq exact (cons found exact)
1358 matches (delq found matches)))
1359 (setq Info-index-alternatives (nconc exact (nreverse matches)))
1360 (Info-index-next 0)))))
1361
1362(defun Info-index-next (num)
1363 "Go to the next matching index item from the last `i' command."
1364 (interactive "p")
1365 (or Info-index-alternatives
1366 (error "No previous `i' command in this file"))
1367 (while (< num 0)
1368 (setq num (+ num (length Info-index-alternatives))))
1369 (while (> num 0)
1370 (setq Info-index-alternatives
1371 (nconc (cdr Info-index-alternatives)
1372 (list (car Info-index-alternatives)))
1373 num (1- num)))
1374 (Info-goto-node (nth 1 (car Info-index-alternatives)))
1375 (if (> (nth 3 (car Info-index-alternatives)) 0)
1376 (forward-line (nth 3 (car Info-index-alternatives)))
1377 (forward-line 3) ; don't search in headers
1378 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
1379 (Info-find-index-name name)))
1380 (message "Found `%s' in %s. %s"
1143a6b0
ER
1381 (car (car Info-index-alternatives))
1382 (nth 2 (car Info-index-alternatives))
1383 (if (cdr Info-index-alternatives)
1384 "(Press `,' for more)"
1385 "(Only match)")))
1386
920bdaab
RS
1387(defun Info-find-index-name (name)
1388 "Move point to the place within the current node where NAME is defined."
1389 (if (or (re-search-forward (format
1390 "[a-zA-Z]+: %s\\( \\|$\\)"
1391 (regexp-quote name)) nil t)
1392 (search-forward (format "`%s'" name) nil t)
1393 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1394 (search-forward
1395 (format "`%s'" (substring name 0 (match-beginning 1)))
1396 nil t))
1397 (search-forward name nil t))
1398 (beginning-of-line)
1399 (goto-char (point-min))))
1400
a384cab3
JB
1401(defun Info-undefined ()
1402 "Make command be undefined in Info."
1403 (interactive)
1404 (ding))
1405
1406(defun Info-help ()
1407 "Enter the Info tutorial."
1408 (interactive)
1409 (delete-other-windows)
1410 (Info-find-node "info"
1411 (if (< (window-height) 23)
1412 "Help-Small-Screen"
1413 "Help")))
1414
1415(defun Info-summary ()
1416 "Display a brief summary of all Info commands."
1417 (interactive)
1418 (save-window-excursion
1419 (switch-to-buffer "*Help*")
1420 (erase-buffer)
1421 (insert (documentation 'Info-mode))
9d29f94c 1422 (help-mode)
a384cab3
JB
1423 (goto-char (point-min))
1424 (let (ch flag)
1425 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1426 (message (if flag "Type Space to see more"
1427 "Type Space to return to Info"))
1614c867 1428 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 1429 (progn (setq unread-command-events (list ch)) nil)
a384cab3 1430 flag))
552775bd
RS
1431 (scroll-up)))
1432 (bury-buffer "*Help*")))
a384cab3
JB
1433\f
1434(defun Info-get-token (pos start all &optional errorstring)
1435 "Return the token around POS,
1436POS must be somewhere inside the token
1437START is a regular expression which will match the
1438 beginning of the tokens delimited string
1439ALL is a regular expression with a single
90a715f0 1440 parenthesized subpattern which is the token to be
a384cab3
JB
1441 returned. E.g. '{\(.*\)}' would return any string
1442 enclosed in braces around POS.
1443SIG optional fourth argument, controls action on no match
1444 nil: return nil
1445 t: beep
1446 a string: signal an error, using that string."
1447 (save-excursion
1448 (goto-char pos)
1449 (re-search-backward start (max (point-min) (- pos 200)) 'yes)
373377ed
RS
1450 (let (found)
1451 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1452 (not (setq found (and (<= (match-beginning 0) pos)
1453 (> (match-end 0) pos))))))
1454 (if (and found (<= (match-beginning 0) pos)
1455 (> (match-end 0) pos))
1456 (buffer-substring (match-beginning 1) (match-end 1))
1457 (cond ((null errorstring)
1458 nil)
1459 ((eq errorstring t)
1460 (beep)
1461 nil)
1462 (t
1463 (error "No %s around position %d" errorstring pos)))))))
a384cab3 1464
981947af 1465(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
1466 "\\<Info-mode-map>Follow a node reference near point.
1467Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 1468At end of the node's text, moves to the next node, or up if none."
f9969361 1469 (interactive "e")
9e5c2f50
RS
1470 (let* ((start (event-start click))
1471 (window (car start))
1472 (pos (car (cdr start))))
1473 (select-window window)
1474 (goto-char pos))
981947af
KH
1475 (and (not (Info-try-follow-nearest-node))
1476 (save-excursion (forward-line 1) (eobp))
ed690657 1477 (Info-next-preorder)))
981947af
KH
1478
1479(defun Info-follow-nearest-node ()
1480 "\\<Info-mode-map>Follow a node reference near point.
1481Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
1482If no reference to follow, moves to the next node, or up if none."
1483 (interactive)
1484 (or (Info-try-follow-nearest-node)
ed690657 1485 (Info-next-preorder)))
981947af
KH
1486
1487;; Common subroutine.
1488(defun Info-try-follow-nearest-node ()
1489 "Follow a node reference near point. Return non-nil if successful."
a384cab3
JB
1490 (let (node)
1491 (cond
981947af
KH
1492 ((setq node (Info-get-token (point) "\\*note[ \n]"
1493 "\\*note[ \n]\\([^:]*\\):"))
a384cab3 1494 (Info-follow-reference node))
bc2ada62 1495 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::"))
a384cab3 1496 (Info-goto-node node))
bc2ada62 1497 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):"))
a384cab3 1498 (Info-menu node))
bc2ada62 1499 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
a384cab3 1500 (Info-goto-node node))
bc2ada62 1501 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
a384cab3 1502 (Info-goto-node node))
bc2ada62 1503 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
a384cab3 1504 (Info-goto-node "Top"))
bc2ada62 1505 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
981947af
KH
1506 (Info-goto-node node)))
1507 node))
a384cab3
JB
1508\f
1509(defvar Info-mode-map nil
1510 "Keymap containing Info commands.")
1511(if Info-mode-map
1512 nil
1513 (setq Info-mode-map (make-keymap))
1514 (suppress-keymap Info-mode-map)
1515 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 1516 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 1517 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd
RS
1518 (define-key Info-mode-map "\t" 'Info-next-reference)
1519 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
e38e7367
RM
1520 (define-key Info-mode-map "1" 'Info-nth-menu-item)
1521 (define-key Info-mode-map "2" 'Info-nth-menu-item)
1522 (define-key Info-mode-map "3" 'Info-nth-menu-item)
1523 (define-key Info-mode-map "4" 'Info-nth-menu-item)
1524 (define-key Info-mode-map "5" 'Info-nth-menu-item)
1525 (define-key Info-mode-map "6" 'Info-nth-menu-item)
1526 (define-key Info-mode-map "7" 'Info-nth-menu-item)
1527 (define-key Info-mode-map "8" 'Info-nth-menu-item)
1528 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 1529 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
1530 (define-key Info-mode-map "?" 'Info-summary)
1531 (define-key Info-mode-map "]" 'Info-forward-node)
1532 (define-key Info-mode-map "[" 'Info-backward-node)
1533 (define-key Info-mode-map "<" 'Info-top-node)
1534 (define-key Info-mode-map ">" 'Info-final-node)
1535 (define-key Info-mode-map "b" 'beginning-of-buffer)
1536 (define-key Info-mode-map "d" 'Info-directory)
1537 (define-key Info-mode-map "e" 'Info-edit)
1538 (define-key Info-mode-map "f" 'Info-follow-reference)
1539 (define-key Info-mode-map "g" 'Info-goto-node)
1540 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 1541 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
1542 (define-key Info-mode-map "l" 'Info-last)
1543 (define-key Info-mode-map "m" 'Info-menu)
1544 (define-key Info-mode-map "n" 'Info-next)
1545 (define-key Info-mode-map "p" 'Info-prev)
1546 (define-key Info-mode-map "q" 'Info-exit)
1547 (define-key Info-mode-map "s" 'Info-search)
47fc33ca
RS
1548 ;; For consistency with Rmail.
1549 (define-key Info-mode-map "\M-s" 'Info-search)
db0c9809 1550 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 1551 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 1552 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 1553 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 1554 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
aea2a8da 1555 )
a384cab3
JB
1556\f
1557;; Info mode is suitable only for specially formatted data.
1558(put 'info-mode 'mode-class 'special)
1559
1560(defun Info-mode ()
1561 "\\<Info-mode-map>
1562Info mode provides commands for browsing through the Info documentation tree.
1563Documentation in Info is divided into \"nodes\", each of which discusses
1564one topic and contains references to other nodes which discuss related
1565topics. Info has commands to follow the references and show you other nodes.
1566
1567\\[Info-help] Invoke the Info tutorial.
1568
1569Selecting other nodes:
21d5959f
RS
1570\\[Info-mouse-follow-nearest-node]
1571 Follow a node reference you click on.
1572 This works with menu items, cross references, and
1573 the \"next\", \"previous\" and \"up\", depending on where you click.
a384cab3 1574\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 1575\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
1576\\[Info-up] Move \"up\" from this node.
1577\\[Info-menu] Pick menu item specified by name (or abbreviation).
1578 Picking a menu item causes another node to be selected.
aea2a8da 1579\\[Info-directory] Go to the Info directory node.
a384cab3
JB
1580\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
1581\\[Info-last] Move to the last node you were at.
1143a6b0
ER
1582\\[Info-index] Look up a topic in this file's Index and move to that node.
1583\\[Info-index-next] (comma) Move to the next match from a previous `i' command.
a384cab3
JB
1584
1585Moving within a node:
8884bb82 1586\\[Info-scroll-up] Normally, scroll forward a full screen. If the end of the buffer is
803eaf50 1587already visible, try to go to the next menu entry, or up if there is none.
8884bb82 1588\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
803eaf50
ER
1589already visible, try to go to the previous menu entry, or up if there is none.
1590\\[beginning-of-buffer] Go to beginning of node.
a384cab3 1591
a384cab3
JB
1592Advanced commands:
1593\\[Info-exit] Quit Info: reselect previously selected buffer.
1594\\[Info-edit] Edit contents of selected node.
15951 Pick first item in node's menu.
15962, 3, 4, 5 Pick second ... fifth item in node's menu.
1597\\[Info-goto-node] Move to node specified by name.
1598 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 1599\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 1600\\[Info-search] Search through this Info file for specified regexp,
803eaf50 1601 and select the node in which the next occurrence is found.
552775bd
RS
1602\\[Info-next-reference] Move cursor to next cross-reference or menu item.
1603\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
1604 (kill-all-local-variables)
1605 (setq major-mode 'Info-mode)
1606 (setq mode-name "Info")
1607 (use-local-map Info-mode-map)
1608 (set-syntax-table text-mode-syntax-table)
1609 (setq local-abbrev-table text-mode-abbrev-table)
1610 (setq case-fold-search t)
1611 (setq buffer-read-only t)
a384cab3
JB
1612 (make-local-variable 'Info-current-file)
1613 (make-local-variable 'Info-current-subfile)
1614 (make-local-variable 'Info-current-node)
1615 (make-local-variable 'Info-tag-table-marker)
1616 (make-local-variable 'Info-history)
1143a6b0 1617 (make-local-variable 'Info-index-alternatives)
87485d6f 1618 (if (memq (framep (selected-frame)) '(x pc))
552775bd
RS
1619 (progn
1620 (make-face 'info-node)
1621 (make-face 'info-menu-5)
1622 (make-face 'info-xref)
1623 (or (face-differs-from-default-p 'info-node)
1624 (if (face-differs-from-default-p 'bold-italic)
1625 (copy-face 'bold-italic 'info-node)
1626 (copy-face 'bold 'info-node)))
1627 (or (face-differs-from-default-p 'info-menu-5)
1628 (set-face-underline-p 'info-menu-5 t))
1629 (or (face-differs-from-default-p 'info-xref)
1630 (copy-face 'bold 'info-xref)))
1631 (setq Info-fontify nil))
a384cab3
JB
1632 (Info-set-mode-line)
1633 (run-hooks 'Info-mode-hook))
1634
1635(defvar Info-edit-map nil
1636 "Local keymap used within `e' command of Info.")
1637(if Info-edit-map
1638 nil
1639 (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
1640 (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
1641
1642;; Info-edit mode is suitable only for specially formatted data.
1643(put 'info-edit-mode 'mode-class 'special)
1644
1645(defun Info-edit-mode ()
1646 "Major mode for editing the contents of an Info node.
a426b157 1647Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
1648which returns to Info mode for browsing.
1649\\{Info-edit-map}"
a384cab3
JB
1650 (use-local-map Info-edit-map)
1651 (setq major-mode 'Info-edit-mode)
1652 (setq mode-name "Info Edit")
1653 (kill-local-variable 'mode-line-buffer-identification)
1654 (setq buffer-read-only nil)
2c609f53 1655 (force-mode-line-update)
e82c28f9
KH
1656 (buffer-enable-undo (current-buffer))
1657 (run-hooks 'Info-edit-mode-hook))
1658
1659(defun Info-edit ()
1660 "Edit the contents of this Info node.
1661Allowed only if variable `Info-enable-edit' is non-nil."
1662 (interactive)
1663 (or Info-enable-edit
1664 (error "Editing info nodes is not enabled"))
1665 (Info-edit-mode)
8ab3e50b 1666 (message "%s" (substitute-command-keys
e82c28f9 1667 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
1668
1669(defun Info-cease-edit ()
1670 "Finish editing Info node; switch back to Info proper."
1671 (interactive)
1672 ;; Do this first, so nothing has changed if user C-g's at query.
1673 (and (buffer-modified-p)
1674 (y-or-n-p "Save the file? ")
1675 (save-buffer))
1676 (use-local-map Info-mode-map)
1677 (setq major-mode 'Info-mode)
1678 (setq mode-name "Info")
1679 (Info-set-mode-line)
1680 (setq buffer-read-only t)
2c609f53 1681 (force-mode-line-update)
a384cab3
JB
1682 (and (marker-position Info-tag-table-marker)
1683 (buffer-modified-p)
1684 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 1685\f
f88ab1c9
RS
1686(defvar Info-file-list-for-emacs
1687 '("ediff" "forms" "gnus" "info" ("mh" . "mh-e") "sc")
1688 "List of Info files that describe Emacs commands.
1689An element can be a file name, or a list of the form (PREFIX . FILE)
1690where PREFIX is a name prefix and FILE is the file to look in.
1691If the element is just a file name, the file name also serves as the prefix.")
1692
f0a8a3f1 1693(defun Info-find-emacs-command-nodes (command)
f88ab1c9 1694 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
1695The `info-file' property of COMMAND says which Info manual to search.
1696If COMMAND has no property, the variable `Info-file-list-for-emacs'
1697defines heuristics for which Info manual to try.
f0a8a3f1
RM
1698The locations are of the format used in Info-history, i.e.
1699\(FILENAME NODENAME BUFFERPOS\)."
f0a8a3f1
RM
1700 (let ((where '())
1701 (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
f88ab1c9
RS
1702 ":\\s *\\(.*\\)\\.$"))
1703 (info-file "emacs")) ;default
1704 ;; Determine which info file this command is documented in.
1705 (if (get command 'info-file)
1706 (setq info-file (get command 'info-file))
1707 ;; If it doesn't say explicitly, test its name against
1708 ;; various prefixes that we know.
1709 (let ((file-list Info-file-list-for-emacs))
1710 (while file-list
1711 (let* ((elt (car file-list))
1712 (name (if (consp elt)
1713 (car elt)
1714 elt))
1715 (file (if (consp elt) (cdr elt) elt))
f57b2cd8 1716 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
1717 "\\(\\'\\|-\\)")))
1718 (if (string-match regexp (symbol-name command))
1719 (setq info-file file file-list nil))
1720 (setq file-list (cdr file-list))))))
f0a8a3f1 1721 (save-excursion
f88ab1c9
RS
1722 (condition-case nil
1723 (Info-find-node info-file "Command Index")
1724 ;; Some manuals may not have a separate Command Index node,
1725 ;; so try just Index instead.
1726 (error
1727 (Info-find-node info-file "Index")))
f0a8a3f1
RM
1728 ;; Take the index node off the Info history.
1729 (setq Info-history (cdr Info-history))
1730 (goto-char (point-max))
1731 (while (re-search-backward cmd-desc nil t)
1732 (setq where (cons (list Info-current-file
1733 (buffer-substring
1734 (match-beginning 1)
1735 (match-end 1))
1736 0)
1737 where)))
1738 where)))
1739
1740;;;###autoload
1741(defun Info-goto-emacs-command-node (command)
e0568e86 1742 "Go to the Info node in the Emacs manual for command COMMAND.
f88ab1c9 1743The command is found by looking up in Emacs manual's Command Index
f57b2cd8
RS
1744or in another manual found via COMMAND's `info-file' property or
1745the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
1746 (interactive "CFind documentation for command: ")
1747 (or (commandp command)
1748 (signal 'wrong-type-argument (list 'commandp command)))
1749 (let ((where (Info-find-emacs-command-nodes command)))
1750 (if where
1751 (let ((num-matches (length where)))
1752 ;; Get Info running, and pop to it in another window.
1753 (save-window-excursion
1754 (info))
1755 (pop-to-buffer "*info*")
1756 (Info-find-node (car (car where))
1757 (car (cdr (car where))))
1758 (if (> num-matches 1)
1759 (progn
1760 ;; Info-find-node already pushed (car where) onto
1761 ;; Info-history. Put the other nodes that were found on
1762 ;; the history.
1763 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 1764 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
1765 (1- num-matches)
1766 (if (> num-matches 2) "ies" "y")
8ab3e50b 1767 (substitute-command-keys "\\[Info-last]")
cedb118c 1768 (if (> num-matches 2) "them" "it")))))
e9b81433 1769 (error "Couldn't find documentation for %s" command))))
f0a8a3f1
RM
1770
1771;;;###autoload
1772(defun Info-goto-emacs-key-command-node (key)
1773 "Go to the Info node in the Emacs manual the command bound to KEY, a string.
e0568e86 1774Interactively, if the binding is execute-extended-command, a command is read.
f88ab1c9 1775The command is found by looking up in Emacs manual's Command Index
f57b2cd8
RS
1776or in another manual found via COMMAND's `info-file' property or
1777the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
1778 (interactive "kFind documentation for key:")
1779 (let ((command (key-binding key)))
1780 (cond ((null command)
9e5c2f50 1781 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
1782 ((and (interactive-p)
1783 (eq command 'execute-extended-command))
1784 (Info-goto-emacs-command-node
1785 (read-command "Find documentation for command: ")))
1786 (t
1787 (Info-goto-emacs-command-node command)))))
552775bd 1788\f
98d8273c
KH
1789(defvar Info-title-face-alist
1790 '((?* bold underline)
1791 (?= bold-italic underline)
1792 (?- italic underline))
1793 "*Alist of face or list of faces to use for pseudo-underlined titles.
1794The alist key is the character the title is underlined with (?*, ?= or ?-).")
1795
552775bd
RS
1796(defun Info-fontify-node ()
1797 (save-excursion
1798 (let ((buffer-read-only nil))
1799 (goto-char (point-min))
1800 (if (looking-at "^File: [^,: \t]+,?[ \t]+")
1801 (progn
1802 (goto-char (match-end 0))
1803 (while
1804 (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
1805 (goto-char (match-end 0))
1806 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1807 'face 'info-xref)
1808 (put-text-property (match-beginning 1) (match-end 1)
1809 'mouse-face 'highlight))))
552775bd 1810 (goto-char (point-min))
98d8273c
KH
1811 (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\)$"
1812 nil t)
1813 (put-text-property (match-beginning 1) (match-end 1)
1814 'face
2e518b7d 1815 (cdr (assq (preceding-char) Info-title-face-alist)))
98d8273c
KH
1816 (put-text-property (match-end 1) (match-end 2)
1817 'invisible t))
1818 (goto-char (point-min))
7757c476 1819 (while (re-search-forward "\\*Note[ \n\t]+\\([^:]*\\):" nil t)
552775bd
RS
1820 (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
1821 nil
1822 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1823 'face 'info-xref)
1824 (put-text-property (match-beginning 1) (match-end 1)
1825 'mouse-face 'highlight)))
552775bd
RS
1826 (goto-char (point-min))
1827 (if (and (search-forward "\n* Menu:" nil t)
1828 (not (string-match "\\<Index\\>" Info-current-node))
1829 ;; Don't take time to annotate huge menus
34a0a4ee 1830 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
552775bd
RS
1831 (let ((n 0))
1832 (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
1833 (setq n (1+ n))
1834 (if (memq n '(5 9)) ; visual aids to help with 1-9 keys
1835 (put-text-property (match-beginning 0)
1836 (1+ (match-beginning 0))
1837 'face 'info-menu-5))
1838 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1839 'face 'info-node)
1840 (put-text-property (match-beginning 1) (match-end 1)
1841 'mouse-face 'highlight))))
552775bd 1842 (set-buffer-modified-p nil))))
49116ac0
JB
1843
1844(provide 'info)
1845
1a06eabd 1846;;; info.el ends here