(makefile-gnumake-functions-alist): Add `addprefix'.
[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))))))
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.
1187 (goto-char (point-max)))
3c9179ea
RS
1188 (t
1189 (error "No more nodes"))))
253db917
ER
1190
1191(defun Info-last-preorder ()
1192 "Go to the last node, popping up a level if there is none."
1193 (interactive)
0a56332b
RS
1194 (cond ((Info-no-error
1195 (Info-last-menu-item)
1196 ;; If we go down a menu item, go to the end of the node
1197 ;; so we can scroll back through it.
ed690657
KH
1198 (goto-char (point-max)))
1199 (recenter -1))
1200 ((Info-no-error (Info-prev))
1201 (goto-char (point-max))
1202 (recenter -1))
1203 ((Info-no-error (Info-up))
1204 (goto-char (point-min))
1205 (or (search-forward "\n* Menu:" nil t)
1206 (goto-char (point-max))))
1207 (t (error "No previous nodes"))))
253db917
ER
1208
1209(defun Info-scroll-up ()
3f32dc86
RS
1210 "Scroll one screenful forward in Info, considering all nodes as one sequence.
1211Once you scroll far enough in a node that its menu appears on the screen,
1212the next scroll moves into its first subnode. When you scroll past
ed690657 1213the end of a node, that goes to the next node or back up to the parent node."
253db917 1214 (interactive)
0a56332b
RS
1215 (if (or (< (window-start) (point-min))
1216 (> (window-start) (point-max)))
1217 (set-window-start (selected-window) (point)))
1218 (let ((virtual-end (save-excursion
1219 (goto-char (point-min))
1220 (if (search-forward "\n* Menu:" nil t)
1221 (point)
1222 (point-max)))))
1223 (if (or (< virtual-end (window-start))
1224 (pos-visible-in-window-p virtual-end))
1225 (Info-next-preorder)
1226 (scroll-up))))
253db917
ER
1227
1228(defun Info-scroll-down ()
3f32dc86 1229 "Scroll one screenful back in Info, considering all nodes as one sequence.
ed690657
KH
1230Within the menu of a node, this goes to its last subnode.
1231When you scroll past the beginning of a node, that goes to the
1232previous node or back up to the parent node."
253db917 1233 (interactive)
0a56332b
RS
1234 (if (or (< (window-start) (point-min))
1235 (> (window-start) (point-max)))
1236 (set-window-start (selected-window) (point)))
1237 (let ((virtual-end (save-excursion
1238 (goto-char (point-min))
1239 (search-forward "\n* Menu:" nil t))))
1240 (if (or virtual-end (pos-visible-in-window-p (point-min)))
1241 (Info-last-preorder)
1242 (scroll-down))))
253db917 1243
56cda6f5 1244(defun Info-next-reference (&optional recur)
552775bd
RS
1245 "Move cursor to the next cross-reference or menu item in the node."
1246 (interactive)
1247 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1248 (old-pt (point)))
1249 (or (eobp) (forward-char 1))
1250 (or (re-search-forward pat nil t)
1251 (progn
1252 (goto-char (point-min))
1253 (or (re-search-forward pat nil t)
1254 (progn
1255 (goto-char old-pt)
1256 (error "No cross references in this node")))))
1257 (goto-char (match-beginning 0))
1258 (if (looking-at "\\* Menu:")
56cda6f5
RS
1259 (if recur
1260 (error "No cross references in this node")
1261 (Info-next-reference t)))))
552775bd 1262
56cda6f5 1263(defun Info-prev-reference (&optional recur)
552775bd
RS
1264 "Move cursor to the previous cross-reference or menu item in the node."
1265 (interactive)
1266 (let ((pat "\\*note[ \n\t]*\\([^:]*\\):\\|^\\* .*:")
1267 (old-pt (point)))
1268 (or (re-search-backward pat nil t)
1269 (progn
1270 (goto-char (point-max))
1271 (or (re-search-backward pat nil t)
1272 (progn
1273 (goto-char old-pt)
1274 (error "No cross references in this node")))))
1275 (goto-char (match-beginning 0))
1276 (if (looking-at "\\* Menu:")
56cda6f5
RS
1277 (if recur
1278 (error "No cross references in this node")
1279 (Info-prev-reference t)))))
552775bd 1280
1143a6b0
ER
1281(defun Info-index (topic)
1282 "Look up a string in the index for this file.
1283The index is defined as the first node in the top-level menu whose
1284name contains the word \"Index\", plus any immediately following
1285nodes whose names also contain the word \"Index\".
1286If there are no exact matches to the specified topic, this chooses
1287the first match which is a case-insensitive substring of a topic.
1288Use the `,' command to see the other matches.
1289Give a blank topic name to go to the Index node itself."
1290 (interactive "sIndex topic: ")
1291 (let ((orignode Info-current-node)
1292 (rnode nil)
b4d8de7d 1293 (pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
1143a6b0
ER
1294 (regexp-quote topic)))
1295 node)
1296 (Info-goto-node "Top")
1297 (or (search-forward "\n* menu:" nil t)
1298 (error "No index"))
1299 (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
1300 (error "No index"))
1301 (goto-char (match-beginning 1))
c696ac76
RS
1302 ;; Here, and subsequently in this function,
1303 ;; we bind Info-history to nil for internal node-switches
1304 ;; so that we don't put junk in the history.
1305 ;; In the first Info-goto-node call, above, we do update the history
1306 ;; because that is what the user's previous node choice into it.
1307 (let ((Info-history nil))
1143a6b0
ER
1308 (Info-goto-node (Info-extract-menu-node-name)))
1309 (or (equal topic "")
1310 (let ((matches nil)
1311 (exact nil)
c696ac76 1312 (Info-history nil)
1143a6b0
ER
1313 found)
1314 (while
1315 (progn
1316 (goto-char (point-min))
1317 (while (re-search-forward pattern nil t)
1318 (setq matches
1319 (cons (list (buffer-substring (match-beginning 1)
1320 (match-end 1))
1321 (buffer-substring (match-beginning 2)
1322 (match-end 2))
1323 Info-current-node
1324 (string-to-int (concat "0"
1325 (buffer-substring
1326 (match-beginning 3)
1327 (match-end 3)))))
1328 matches)))
1329 (and (setq node (Info-extract-pointer "next" t))
1330 (string-match "\\<Index\\>" node)))
1331 (Info-goto-node node))
1332 (or matches
1333 (progn
81e14cb2 1334 (Info-goto-node orignode)
920bdaab 1335 (error "No `%s' in index" topic)))
1143a6b0
ER
1336 ;; Here it is a feature that assoc is case-sensitive.
1337 (while (setq found (assoc topic matches))
1338 (setq exact (cons found exact)
1339 matches (delq found matches)))
1340 (setq Info-index-alternatives (nconc exact (nreverse matches)))
1341 (Info-index-next 0)))))
1342
1343(defun Info-index-next (num)
1344 "Go to the next matching index item from the last `i' command."
1345 (interactive "p")
1346 (or Info-index-alternatives
1347 (error "No previous `i' command in this file"))
1348 (while (< num 0)
1349 (setq num (+ num (length Info-index-alternatives))))
1350 (while (> num 0)
1351 (setq Info-index-alternatives
1352 (nconc (cdr Info-index-alternatives)
1353 (list (car Info-index-alternatives)))
1354 num (1- num)))
1355 (Info-goto-node (nth 1 (car Info-index-alternatives)))
1356 (if (> (nth 3 (car Info-index-alternatives)) 0)
1357 (forward-line (nth 3 (car Info-index-alternatives)))
1358 (forward-line 3) ; don't search in headers
1359 (let ((name (car (car Info-index-alternatives))))
920bdaab
RS
1360 (Info-find-index-name name)))
1361 (message "Found `%s' in %s. %s"
1143a6b0
ER
1362 (car (car Info-index-alternatives))
1363 (nth 2 (car Info-index-alternatives))
1364 (if (cdr Info-index-alternatives)
1365 "(Press `,' for more)"
1366 "(Only match)")))
1367
920bdaab
RS
1368(defun Info-find-index-name (name)
1369 "Move point to the place within the current node where NAME is defined."
1370 (if (or (re-search-forward (format
1371 "[a-zA-Z]+: %s\\( \\|$\\)"
1372 (regexp-quote name)) nil t)
1373 (search-forward (format "`%s'" name) nil t)
1374 (and (string-match "\\`.*\\( (.*)\\)\\'" name)
1375 (search-forward
1376 (format "`%s'" (substring name 0 (match-beginning 1)))
1377 nil t))
1378 (search-forward name nil t))
1379 (beginning-of-line)
1380 (goto-char (point-min))))
1381
a384cab3
JB
1382(defun Info-undefined ()
1383 "Make command be undefined in Info."
1384 (interactive)
1385 (ding))
1386
1387(defun Info-help ()
1388 "Enter the Info tutorial."
1389 (interactive)
1390 (delete-other-windows)
1391 (Info-find-node "info"
1392 (if (< (window-height) 23)
1393 "Help-Small-Screen"
1394 "Help")))
1395
1396(defun Info-summary ()
1397 "Display a brief summary of all Info commands."
1398 (interactive)
1399 (save-window-excursion
1400 (switch-to-buffer "*Help*")
1401 (erase-buffer)
1402 (insert (documentation 'Info-mode))
9d29f94c 1403 (help-mode)
a384cab3
JB
1404 (goto-char (point-min))
1405 (let (ch flag)
1406 (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
1407 (message (if flag "Type Space to see more"
1408 "Type Space to return to Info"))
1614c867 1409 (if (not (eq ?\ (setq ch (read-event))))
dbc4e1c1 1410 (progn (setq unread-command-events (list ch)) nil)
a384cab3 1411 flag))
552775bd
RS
1412 (scroll-up)))
1413 (bury-buffer "*Help*")))
a384cab3
JB
1414\f
1415(defun Info-get-token (pos start all &optional errorstring)
1416 "Return the token around POS,
1417POS must be somewhere inside the token
1418START is a regular expression which will match the
1419 beginning of the tokens delimited string
1420ALL is a regular expression with a single
90a715f0 1421 parenthesized subpattern which is the token to be
a384cab3
JB
1422 returned. E.g. '{\(.*\)}' would return any string
1423 enclosed in braces around POS.
1424SIG optional fourth argument, controls action on no match
1425 nil: return nil
1426 t: beep
1427 a string: signal an error, using that string."
1428 (save-excursion
1429 (goto-char pos)
1430 (re-search-backward start (max (point-min) (- pos 200)) 'yes)
373377ed
RS
1431 (let (found)
1432 (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
1433 (not (setq found (and (<= (match-beginning 0) pos)
1434 (> (match-end 0) pos))))))
1435 (if (and found (<= (match-beginning 0) pos)
1436 (> (match-end 0) pos))
1437 (buffer-substring (match-beginning 1) (match-end 1))
1438 (cond ((null errorstring)
1439 nil)
1440 ((eq errorstring t)
1441 (beep)
1442 nil)
1443 (t
1444 (error "No %s around position %d" errorstring pos)))))))
a384cab3 1445
981947af 1446(defun Info-mouse-follow-nearest-node (click)
f9969361
RS
1447 "\\<Info-mode-map>Follow a node reference near point.
1448Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where you click.
981947af 1449At end of the node's text, moves to the next node, or up if none."
f9969361 1450 (interactive "e")
9e5c2f50
RS
1451 (let* ((start (event-start click))
1452 (window (car start))
1453 (pos (car (cdr start))))
1454 (select-window window)
1455 (goto-char pos))
981947af
KH
1456 (and (not (Info-try-follow-nearest-node))
1457 (save-excursion (forward-line 1) (eobp))
ed690657 1458 (Info-next-preorder)))
981947af
KH
1459
1460(defun Info-follow-nearest-node ()
1461 "\\<Info-mode-map>Follow a node reference near point.
1462Like \\[Info-menu], \\[Info-follow-reference], \\[Info-next], \\[Info-prev] or \\[Info-up] command, depending on where point is.
1463If no reference to follow, moves to the next node, or up if none."
1464 (interactive)
1465 (or (Info-try-follow-nearest-node)
ed690657 1466 (Info-next-preorder)))
981947af
KH
1467
1468;; Common subroutine.
1469(defun Info-try-follow-nearest-node ()
1470 "Follow a node reference near point. Return non-nil if successful."
a384cab3
JB
1471 (let (node)
1472 (cond
981947af
KH
1473 ((setq node (Info-get-token (point) "\\*note[ \n]"
1474 "\\*note[ \n]\\([^:]*\\):"))
a384cab3 1475 (Info-follow-reference node))
bc2ada62 1476 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::"))
a384cab3 1477 (Info-goto-node node))
bc2ada62 1478 ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):"))
a384cab3 1479 (Info-menu node))
bc2ada62 1480 ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)"))
a384cab3 1481 (Info-goto-node node))
bc2ada62 1482 ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)"))
a384cab3 1483 (Info-goto-node node))
bc2ada62 1484 ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)"))
a384cab3 1485 (Info-goto-node "Top"))
bc2ada62 1486 ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)"))
981947af
KH
1487 (Info-goto-node node)))
1488 node))
a384cab3
JB
1489\f
1490(defvar Info-mode-map nil
1491 "Keymap containing Info commands.")
1492(if Info-mode-map
1493 nil
1494 (setq Info-mode-map (make-keymap))
1495 (suppress-keymap Info-mode-map)
1496 (define-key Info-mode-map "." 'beginning-of-buffer)
253db917 1497 (define-key Info-mode-map " " 'Info-scroll-up)
981947af 1498 (define-key Info-mode-map "\C-m" 'Info-follow-nearest-node)
552775bd
RS
1499 (define-key Info-mode-map "\t" 'Info-next-reference)
1500 (define-key Info-mode-map "\e\t" 'Info-prev-reference)
e38e7367
RM
1501 (define-key Info-mode-map "1" 'Info-nth-menu-item)
1502 (define-key Info-mode-map "2" 'Info-nth-menu-item)
1503 (define-key Info-mode-map "3" 'Info-nth-menu-item)
1504 (define-key Info-mode-map "4" 'Info-nth-menu-item)
1505 (define-key Info-mode-map "5" 'Info-nth-menu-item)
1506 (define-key Info-mode-map "6" 'Info-nth-menu-item)
1507 (define-key Info-mode-map "7" 'Info-nth-menu-item)
1508 (define-key Info-mode-map "8" 'Info-nth-menu-item)
1509 (define-key Info-mode-map "9" 'Info-nth-menu-item)
82a4c008 1510 (define-key Info-mode-map "0" 'undefined)
a384cab3
JB
1511 (define-key Info-mode-map "?" 'Info-summary)
1512 (define-key Info-mode-map "]" 'Info-forward-node)
1513 (define-key Info-mode-map "[" 'Info-backward-node)
1514 (define-key Info-mode-map "<" 'Info-top-node)
1515 (define-key Info-mode-map ">" 'Info-final-node)
1516 (define-key Info-mode-map "b" 'beginning-of-buffer)
1517 (define-key Info-mode-map "d" 'Info-directory)
1518 (define-key Info-mode-map "e" 'Info-edit)
1519 (define-key Info-mode-map "f" 'Info-follow-reference)
1520 (define-key Info-mode-map "g" 'Info-goto-node)
1521 (define-key Info-mode-map "h" 'Info-help)
1143a6b0 1522 (define-key Info-mode-map "i" 'Info-index)
a384cab3
JB
1523 (define-key Info-mode-map "l" 'Info-last)
1524 (define-key Info-mode-map "m" 'Info-menu)
1525 (define-key Info-mode-map "n" 'Info-next)
1526 (define-key Info-mode-map "p" 'Info-prev)
1527 (define-key Info-mode-map "q" 'Info-exit)
1528 (define-key Info-mode-map "s" 'Info-search)
47fc33ca
RS
1529 ;; For consistency with Rmail.
1530 (define-key Info-mode-map "\M-s" 'Info-search)
db0c9809 1531 (define-key Info-mode-map "t" 'Info-top-node)
a384cab3 1532 (define-key Info-mode-map "u" 'Info-up)
1143a6b0 1533 (define-key Info-mode-map "," 'Info-index-next)
803eaf50 1534 (define-key Info-mode-map "\177" 'Info-scroll-down)
981947af 1535 (define-key Info-mode-map [mouse-2] 'Info-mouse-follow-nearest-node)
aea2a8da 1536 )
a384cab3
JB
1537\f
1538;; Info mode is suitable only for specially formatted data.
1539(put 'info-mode 'mode-class 'special)
1540
1541(defun Info-mode ()
1542 "\\<Info-mode-map>
1543Info mode provides commands for browsing through the Info documentation tree.
1544Documentation in Info is divided into \"nodes\", each of which discusses
1545one topic and contains references to other nodes which discuss related
1546topics. Info has commands to follow the references and show you other nodes.
1547
1548\\[Info-help] Invoke the Info tutorial.
1549
1550Selecting other nodes:
21d5959f
RS
1551\\[Info-mouse-follow-nearest-node]
1552 Follow a node reference you click on.
1553 This works with menu items, cross references, and
1554 the \"next\", \"previous\" and \"up\", depending on where you click.
a384cab3 1555\\[Info-next] Move to the \"next\" node of this node.
bc2ada62 1556\\[Info-prev] Move to the \"previous\" node of this node.
a384cab3
JB
1557\\[Info-up] Move \"up\" from this node.
1558\\[Info-menu] Pick menu item specified by name (or abbreviation).
1559 Picking a menu item causes another node to be selected.
aea2a8da 1560\\[Info-directory] Go to the Info directory node.
a384cab3
JB
1561\\[Info-follow-reference] Follow a cross reference. Reads name of reference.
1562\\[Info-last] Move to the last node you were at.
1143a6b0
ER
1563\\[Info-index] Look up a topic in this file's Index and move to that node.
1564\\[Info-index-next] (comma) Move to the next match from a previous `i' command.
a384cab3
JB
1565
1566Moving within a node:
8884bb82 1567\\[Info-scroll-up] Normally, scroll forward a full screen. If the end of the buffer is
803eaf50 1568already visible, try to go to the next menu entry, or up if there is none.
8884bb82 1569\\[Info-scroll-down] Normally, scroll backward. If the beginning of the buffer is
803eaf50
ER
1570already visible, try to go to the previous menu entry, or up if there is none.
1571\\[beginning-of-buffer] Go to beginning of node.
a384cab3 1572
a384cab3
JB
1573Advanced commands:
1574\\[Info-exit] Quit Info: reselect previously selected buffer.
1575\\[Info-edit] Edit contents of selected node.
15761 Pick first item in node's menu.
15772, 3, 4, 5 Pick second ... fifth item in node's menu.
1578\\[Info-goto-node] Move to node specified by name.
1579 You may include a filename as well, as (FILENAME)NODENAME.
552775bd 1580\\[universal-argument] \\[info] Move to new Info file with completion.
a384cab3 1581\\[Info-search] Search through this Info file for specified regexp,
803eaf50 1582 and select the node in which the next occurrence is found.
552775bd
RS
1583\\[Info-next-reference] Move cursor to next cross-reference or menu item.
1584\\[Info-prev-reference] Move cursor to previous cross-reference or menu item."
a384cab3
JB
1585 (kill-all-local-variables)
1586 (setq major-mode 'Info-mode)
1587 (setq mode-name "Info")
1588 (use-local-map Info-mode-map)
1589 (set-syntax-table text-mode-syntax-table)
1590 (setq local-abbrev-table text-mode-abbrev-table)
1591 (setq case-fold-search t)
1592 (setq buffer-read-only t)
a384cab3
JB
1593 (make-local-variable 'Info-current-file)
1594 (make-local-variable 'Info-current-subfile)
1595 (make-local-variable 'Info-current-node)
1596 (make-local-variable 'Info-tag-table-marker)
1597 (make-local-variable 'Info-history)
1143a6b0 1598 (make-local-variable 'Info-index-alternatives)
87485d6f 1599 (if (memq (framep (selected-frame)) '(x pc))
552775bd
RS
1600 (progn
1601 (make-face 'info-node)
1602 (make-face 'info-menu-5)
1603 (make-face 'info-xref)
1604 (or (face-differs-from-default-p 'info-node)
1605 (if (face-differs-from-default-p 'bold-italic)
1606 (copy-face 'bold-italic 'info-node)
1607 (copy-face 'bold 'info-node)))
1608 (or (face-differs-from-default-p 'info-menu-5)
1609 (set-face-underline-p 'info-menu-5 t))
1610 (or (face-differs-from-default-p 'info-xref)
1611 (copy-face 'bold 'info-xref)))
1612 (setq Info-fontify nil))
a384cab3
JB
1613 (Info-set-mode-line)
1614 (run-hooks 'Info-mode-hook))
1615
1616(defvar Info-edit-map nil
1617 "Local keymap used within `e' command of Info.")
1618(if Info-edit-map
1619 nil
1620 (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map))
1621 (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
1622
1623;; Info-edit mode is suitable only for specially formatted data.
1624(put 'info-edit-mode 'mode-class 'special)
1625
1626(defun Info-edit-mode ()
1627 "Major mode for editing the contents of an Info node.
a426b157 1628Like text mode with the addition of `Info-cease-edit'
a384cab3
JB
1629which returns to Info mode for browsing.
1630\\{Info-edit-map}"
a384cab3
JB
1631 (use-local-map Info-edit-map)
1632 (setq major-mode 'Info-edit-mode)
1633 (setq mode-name "Info Edit")
1634 (kill-local-variable 'mode-line-buffer-identification)
1635 (setq buffer-read-only nil)
2c609f53 1636 (force-mode-line-update)
e82c28f9
KH
1637 (buffer-enable-undo (current-buffer))
1638 (run-hooks 'Info-edit-mode-hook))
1639
1640(defun Info-edit ()
1641 "Edit the contents of this Info node.
1642Allowed only if variable `Info-enable-edit' is non-nil."
1643 (interactive)
1644 (or Info-enable-edit
1645 (error "Editing info nodes is not enabled"))
1646 (Info-edit-mode)
8ab3e50b 1647 (message "%s" (substitute-command-keys
e82c28f9 1648 "Editing: Type \\<Info-edit-map>\\[Info-cease-edit] to return to info")))
a384cab3
JB
1649
1650(defun Info-cease-edit ()
1651 "Finish editing Info node; switch back to Info proper."
1652 (interactive)
1653 ;; Do this first, so nothing has changed if user C-g's at query.
1654 (and (buffer-modified-p)
1655 (y-or-n-p "Save the file? ")
1656 (save-buffer))
1657 (use-local-map Info-mode-map)
1658 (setq major-mode 'Info-mode)
1659 (setq mode-name "Info")
1660 (Info-set-mode-line)
1661 (setq buffer-read-only t)
2c609f53 1662 (force-mode-line-update)
a384cab3
JB
1663 (and (marker-position Info-tag-table-marker)
1664 (buffer-modified-p)
1665 (message "Tags may have changed. Use Info-tagify if necessary")))
f0a8a3f1 1666\f
f88ab1c9
RS
1667(defvar Info-file-list-for-emacs
1668 '("ediff" "forms" "gnus" "info" ("mh" . "mh-e") "sc")
1669 "List of Info files that describe Emacs commands.
1670An element can be a file name, or a list of the form (PREFIX . FILE)
1671where PREFIX is a name prefix and FILE is the file to look in.
1672If the element is just a file name, the file name also serves as the prefix.")
1673
f0a8a3f1 1674(defun Info-find-emacs-command-nodes (command)
f88ab1c9 1675 "Return a list of locations documenting COMMAND.
f57b2cd8
RS
1676The `info-file' property of COMMAND says which Info manual to search.
1677If COMMAND has no property, the variable `Info-file-list-for-emacs'
1678defines heuristics for which Info manual to try.
f0a8a3f1
RM
1679The locations are of the format used in Info-history, i.e.
1680\(FILENAME NODENAME BUFFERPOS\)."
f0a8a3f1
RM
1681 (let ((where '())
1682 (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
f88ab1c9
RS
1683 ":\\s *\\(.*\\)\\.$"))
1684 (info-file "emacs")) ;default
1685 ;; Determine which info file this command is documented in.
1686 (if (get command 'info-file)
1687 (setq info-file (get command 'info-file))
1688 ;; If it doesn't say explicitly, test its name against
1689 ;; various prefixes that we know.
1690 (let ((file-list Info-file-list-for-emacs))
1691 (while file-list
1692 (let* ((elt (car file-list))
1693 (name (if (consp elt)
1694 (car elt)
1695 elt))
1696 (file (if (consp elt) (cdr elt) elt))
f57b2cd8 1697 (regexp (concat "\\`" (regexp-quote name)
f88ab1c9
RS
1698 "\\(\\'\\|-\\)")))
1699 (if (string-match regexp (symbol-name command))
1700 (setq info-file file file-list nil))
1701 (setq file-list (cdr file-list))))))
f0a8a3f1 1702 (save-excursion
f88ab1c9
RS
1703 (condition-case nil
1704 (Info-find-node info-file "Command Index")
1705 ;; Some manuals may not have a separate Command Index node,
1706 ;; so try just Index instead.
1707 (error
1708 (Info-find-node info-file "Index")))
f0a8a3f1
RM
1709 ;; Take the index node off the Info history.
1710 (setq Info-history (cdr Info-history))
1711 (goto-char (point-max))
1712 (while (re-search-backward cmd-desc nil t)
1713 (setq where (cons (list Info-current-file
1714 (buffer-substring
1715 (match-beginning 1)
1716 (match-end 1))
1717 0)
1718 where)))
1719 where)))
1720
1721;;;###autoload
1722(defun Info-goto-emacs-command-node (command)
e0568e86 1723 "Go to the Info node in the Emacs manual for command COMMAND.
f88ab1c9 1724The command is found by looking up in Emacs manual's Command Index
f57b2cd8
RS
1725or in another manual found via COMMAND's `info-file' property or
1726the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
1727 (interactive "CFind documentation for command: ")
1728 (or (commandp command)
1729 (signal 'wrong-type-argument (list 'commandp command)))
1730 (let ((where (Info-find-emacs-command-nodes command)))
1731 (if where
1732 (let ((num-matches (length where)))
1733 ;; Get Info running, and pop to it in another window.
1734 (save-window-excursion
1735 (info))
1736 (pop-to-buffer "*info*")
1737 (Info-find-node (car (car where))
1738 (car (cdr (car where))))
1739 (if (> num-matches 1)
1740 (progn
1741 ;; Info-find-node already pushed (car where) onto
1742 ;; Info-history. Put the other nodes that were found on
1743 ;; the history.
1744 (setq Info-history (nconc (cdr where) Info-history))
8ab3e50b 1745 (message "Found %d other entr%s. Use %s to see %s."
cedb118c
RS
1746 (1- num-matches)
1747 (if (> num-matches 2) "ies" "y")
8ab3e50b 1748 (substitute-command-keys "\\[Info-last]")
cedb118c 1749 (if (> num-matches 2) "them" "it")))))
e9b81433 1750 (error "Couldn't find documentation for %s" command))))
f0a8a3f1
RM
1751
1752;;;###autoload
1753(defun Info-goto-emacs-key-command-node (key)
1754 "Go to the Info node in the Emacs manual the command bound to KEY, a string.
e0568e86 1755Interactively, if the binding is execute-extended-command, a command is read.
f88ab1c9 1756The command is found by looking up in Emacs manual's Command Index
f57b2cd8
RS
1757or in another manual found via COMMAND's `info-file' property or
1758the variable `Info-file-list-for-emacs'."
f0a8a3f1
RM
1759 (interactive "kFind documentation for key:")
1760 (let ((command (key-binding key)))
1761 (cond ((null command)
9e5c2f50 1762 (message "%s is undefined" (key-description key)))
f0a8a3f1
RM
1763 ((and (interactive-p)
1764 (eq command 'execute-extended-command))
1765 (Info-goto-emacs-command-node
1766 (read-command "Find documentation for command: ")))
1767 (t
1768 (Info-goto-emacs-command-node command)))))
552775bd 1769\f
98d8273c
KH
1770(defvar Info-title-face-alist
1771 '((?* bold underline)
1772 (?= bold-italic underline)
1773 (?- italic underline))
1774 "*Alist of face or list of faces to use for pseudo-underlined titles.
1775The alist key is the character the title is underlined with (?*, ?= or ?-).")
1776
552775bd
RS
1777(defun Info-fontify-node ()
1778 (save-excursion
1779 (let ((buffer-read-only nil))
1780 (goto-char (point-min))
1781 (if (looking-at "^File: [^,: \t]+,?[ \t]+")
1782 (progn
1783 (goto-char (match-end 0))
1784 (while
1785 (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?")
1786 (goto-char (match-end 0))
1787 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1788 'face 'info-xref)
1789 (put-text-property (match-beginning 1) (match-end 1)
1790 'mouse-face 'highlight))))
552775bd 1791 (goto-char (point-min))
98d8273c
KH
1792 (while (re-search-forward "\n\\([^ \t\n].+\\)\n\\(\\*+\\|=+\\|-+\\)$"
1793 nil t)
1794 (put-text-property (match-beginning 1) (match-end 1)
1795 'face
2e518b7d 1796 (cdr (assq (preceding-char) Info-title-face-alist)))
98d8273c
KH
1797 (put-text-property (match-end 1) (match-end 2)
1798 'invisible t))
1799 (goto-char (point-min))
7757c476 1800 (while (re-search-forward "\\*Note[ \n\t]+\\([^:]*\\):" nil t)
552775bd
RS
1801 (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
1802 nil
1803 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1804 'face 'info-xref)
1805 (put-text-property (match-beginning 1) (match-end 1)
1806 'mouse-face 'highlight)))
552775bd
RS
1807 (goto-char (point-min))
1808 (if (and (search-forward "\n* Menu:" nil t)
1809 (not (string-match "\\<Index\\>" Info-current-node))
1810 ;; Don't take time to annotate huge menus
34a0a4ee 1811 (< (- (point-max) (point)) Info-fontify-maximum-menu-size))
552775bd
RS
1812 (let ((n 0))
1813 (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
1814 (setq n (1+ n))
1815 (if (memq n '(5 9)) ; visual aids to help with 1-9 keys
1816 (put-text-property (match-beginning 0)
1817 (1+ (match-beginning 0))
1818 'face 'info-menu-5))
1819 (put-text-property (match-beginning 1) (match-end 1)
9a87b430
RS
1820 'face 'info-node)
1821 (put-text-property (match-beginning 1) (match-end 1)
1822 'mouse-face 'highlight))))
552775bd 1823 (set-buffer-modified-p nil))))
49116ac0
JB
1824
1825(provide 'info)
1826
1a06eabd 1827;;; info.el ends here