Updates to my about.el bio.
[sxemacs] / lisp / info.el
1 ;;; info.el --- info package for SXEmacs.
2 ;; Keywords: help
3
4 ;; Copyright (C) 1985, 1986, 1993, 1997 Free Software Foundation, Inc.
5 ;; Copyright (C) 2005, 2020 Steve Youngs.
6
7 ;; Author: Dave Gillespie <daveg@synaptics.com>
8 ;;         Richard Stallman <rms@gnu.ai.mit.edu>
9 ;; Maintainer: Dave Gillespie <daveg@synaptics.com>
10 ;; Version: 1.07 of 7/22/93
11 ;; Keywords: docs, help
12
13 ;; This file is part of SXEmacs.
14
15 ;; SXEmacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; SXEmacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Synched up with: Not synched with FSF.
29
30 ;; Commentary:
31
32 ;; This is based on an early Emacs 19 info.el file.
33 ;;
34 ;; Note that Info-directory has been replaced by Info-directory-list,
35 ;; a search path of directories in which to find Info files.
36 ;; Also, Info tries adding ".info" to a file name if the name itself
37 ;; is not found.
38 ;;
39 ;; See the change log below for further details.
40
41
42 ;; LCD Archive Entry:
43 ;; info-dg|Dave Gillespie|daveg@synaptics.com
44 ;; |Info reader with many enhancements; replaces standard info.el.
45 ;; |93-07-22|1.07|~/modes/info.el
46
47 ;; Also available from anonymous FTP on csvax.cs.caltech.edu.
48
49
50 ;; Change Log:
51
52 ;; Modified 3/7/1991 by Dave Gillespie:
53 ;; (Author's address: daveg@synaptics.com or daveg@csvax.cs.caltech.edu)
54 ;;
55 ;; Added keys:  i, t, <, >, [, ], {, }, 6, 7, 8, 9, 0.
56 ;; Look at help for info-mode (type ? in Info) for descriptions.
57 ;;
58 ;; If Info-directory-list is undefined and there is no INFOPATH
59 ;; in the environment, use value of Info-directory for compatibility
60 ;; with Emacs 18.57.
61 ;;
62 ;; All files named "localdir" found in the path are appended to "dir",
63 ;; the Info directory.  For this to work, "dir" should contain only
64 ;; one node (Top), and each "localdir" should contain no ^_ or ^L
65 ;; characters.  Generally they will contain only one or several
66 ;; additional lines for the top-level menu.  Note that "dir" is
67 ;; modified in memory each time it is loaded, but not on disk.
68 ;;
69 ;; If "dir" contains a line of the form:  "* Locals:"
70 ;; then the "localdir"s are inserted there instead of at the end.
71
72
73 ;; Modified 4/3/1991 by Dave Gillespie:
74 ;;
75 ;; Added Info-mode-hook (suggested by Sebastian Kremer).
76 ;; Also added epoch-info-startup/select-hooks from Simon Spero's info.el.
77 ;;
78 ;; Added automatic decoding of compressed Info files.
79 ;; See documentation for the variable Info-suffix-list.  Default is to
80 ;; run "uncompress" on ".Z" files and "unyabba" on ".Y" files.
81 ;; (See comp.sources.unix v24i073-076 for yabba/unyabba, a free software
82 ;; alternative to compress/uncompress.)
83 ;; Note: "dir" and "localdir" files should not be compressed.
84 ;;
85 ;; Changed variables like Info-enable-edit to be settable by M-x set-variable.
86 ;;
87 ;; Added Info-auto-advance variable.  If t, SPC and DEL will act like
88 ;; } and {, i.e., they advance to the next/previous node if at the end
89 ;; of the buffer.
90 ;;
91 ;; Changed `u' to restore point to most recent location in that node.
92 ;; Added `=' to do this manually at any time.  (Suggested by David Fox).
93 ;;
94 ;; Changed `m' and `0-9' to try interpreting menu name as a file name
95 ;; if not found as a node name.  This allows (dir) menus of the form,
96 ;;     Emacs::          Cool text editor
97 ;; as a shorthand for
98 ;;     Emacs:(emacs).   Cool text editor
99 ;;
100 ;; Enhanced `i' to use line-number information in the index.
101 ;; Added `,' to move among all matches to a previous `i' command.
102 ;;
103 ;; Added `a' (Info-annotate) for adding personal notes to any Info node.
104 ;; Notes are not stored in the actual Info files, but in the user's own
105 ;; ~/.infonotes file.
106 ;;
107 ;; Added Info-footnote-tag, made default be "Ref" instead of "Note".
108 ;;
109 ;; Got mouse-click stuff to work under Emacs version 18.  Check it out!
110 ;; Left and right clicks scroll the Info window.
111 ;; Middle click goes to clicked-on node, e.g., "Next:", a menu, or a note.
112
113
114 ;; Modified 6/29/1991 by Dave Gillespie:
115 ;;
116 ;; Renamed epoch-info-startup/select-hooks to Info-startup/select-hook.
117 ;;
118 ;; Made Info-select-node into a command on the `!' key.
119 ;;
120 ;; Added Info-mouse-support user option.
121 ;;
122 ;; Cleaned up the implementation of some routines.
123 ;;
124 ;; Added special treatment of quoted words in annotations:  The `g'
125 ;; command for a nonexistent node name scans for an annotation
126 ;; (in any node of any file) containing that name in quotes:  g foo RET
127 ;; looks for an annotation containing:  "foo"  or:  <<foo>>
128 ;; If found, it goes to that file and node.
129 ;;
130 ;; Added a call to set up Info-directory-list in Info-find-node to
131 ;; work around a bug in GNUS where it calls Info-goto-node before info.
132 ;;
133 ;; Added completion for `g' command (inspired by Richard Kim's infox.el).
134 ;; Completion knows all node names for the current file, and all annotation
135 ;; tags (see above).  It does not complete file names or node names in
136 ;; other files.
137 ;;
138 ;; Added `k' (Info-emacs-key) and `*' (Info-elisp-ref) commands.  You may
139 ;; wish to bind these to global keys outside of Info mode.
140 ;;
141 ;; Allowed localdir files to be full dir-like files; only the menu part
142 ;; of each localdir is copied.  Also, redundant menu items are omitted.
143 ;;
144 ;; Changed Info-history to hold only one entry at a time for each node,
145 ;; and to be circular so that multiple `l's come back again to the most
146 ;; recent node.  Note that the format of Info-history entries has changed,
147 ;; which may interfere with external programs that try to operate on it.
148 ;; (Also inspired by Kim's infox.el).
149 ;;
150 ;; Changed `n', `]', `l', etc. to accept prefix arguments to move several
151 ;; steps at once.  Most accept negative arguments to move oppositely.
152 ;;
153 ;; Changed `?' to bury *Help* buffer afterwards to keep it out of the way.
154 ;;
155 ;; Rearranged `?' key's display to be a little better for new users.
156 ;;
157 ;; Changed `a' to save whole window configuration and restore on C-c C-c.
158 ;;
159 ;; Fixed the bug reported by Bill Reynolds on gnu.emacs.bugs.
160 ;;
161 ;; Changed Info-last to restore window-start as well as cursor position.
162 ;;
163 ;; Changed middle mouse button in space after end of node to do Info-last
164 ;; if we got here by following a cross reference, else do Info-global-next.
165 ;;
166 ;; Added some new mouse bindings: shift-left = Info-global-next,
167 ;; shift-right = Info-global-prev, shift-middle = Info-last.
168 ;;
169 ;; Fixed Info-follow-reference not to make assumptions about length
170 ;; of Info-footnote-tag [Linus Tolke].
171 ;;
172 ;; Changed default for Info-auto-advance mode to be press-twice-for-next-node.
173 ;;
174 ;; Modified x-mouse-ignore to preserve last-command variable, so that
175 ;; press-twice Info-auto-advance mode works with the mouse.
176
177
178 ;; Modified 3/4/1992 by Dave Gillespie:
179 ;;
180 ;; Added an "autoload" command to help autoload.el.
181 ;;
182 ;; Changed `*' command to look for file `elisp' as well as for `lispref'.
183 ;;
184 ;; Fixed a bug involving footnote names containing regexp special characters.
185 ;;
186 ;; Fixed a bug in completion during `f' (or `r') command.
187 ;;
188 ;; Added TAB (Info-next-reference), M-TAB, and RET keys to Info mode.
189 ;;
190 ;; Added new bindings, `C-h C-k' for Info-emacs-key and `C-h C-f' for
191 ;; Info-elisp-ref.  These bindings are made when info.el is loaded, and
192 ;; only if those key sequences were previously unbound.  These bindings
193 ;; work at any time, not just when Info is already running.
194
195
196 ;; Modified 3/8/1992 by Dave Gillespie:
197 ;;
198 ;; Fixed some long lines that were causing trouble with mailers.
199
200
201 ;; Modified 3/9/1992 by Dave Gillespie:
202 ;;
203 ;; Added `C-h C-i' (Info-query).
204 ;;
205 ;; Added Info-novice mode, warns if the user attempts to switch to
206 ;; a different Info file.
207 ;;
208 ;; Fixed a bug that caused problems using compressed Info files
209 ;; and Info-directory-list at the same time.
210 ;;
211 ;; Disabled Info-mouse-support by default if Epoch or Hyperbole is in use.
212 ;;
213 ;; Added an expand-file-name call to Info-find-node to fix a small bug.
214
215
216 ;; Modified 5/22/1992 by Dave Gillespie:
217 ;;
218 ;; Added "standalone" operation:  "emacs -f info" runs Emacs specifically
219 ;; for use as an Info browser.  In this mode, the `q' key quits Emacs
220 ;; itself.  Also, "emacs -f info arg" starts in Info file "arg" instead
221 ;; of "dir".
222 ;;
223 ;; Changed to prefer "foo.info" over "foo".  If both exist, "foo" is
224 ;; probably a directory or executable program!
225 ;;
226 ;; Made control-mouse act like regular-mouse does in other buffers.
227 ;; (In most systems, this will be set-cursor for left-mouse, x-cut
228 ;; for right-mouse, and x-paste, which will be an error, for
229 ;; middle-mouse.)
230 ;;
231 ;; Improved prompting and searching for `,' key.
232 ;;
233 ;; Fixed a bug where some "* Menu:" lines disappeared when "dir"
234 ;; contained several nodes.
235
236
237 ;; Modified 9/10/1992 by Dave Gillespie:
238 ;;
239 ;; Mixed in support for XEmacs.  Mouse works the same as in
240 ;; the other Emacs versions by default; added Info-lucid-mouse-style
241 ;; variable, which enables mouse operation similar to XEmacs's default.
242 ;;
243 ;; Fixed a bug where RET couldn't understand "* Foo::" if "Foo" was a
244 ;; file name instead of a node name.
245 ;;
246 ;; Added `x' (Info-bookmark), a simple interface to the annotation
247 ;; tags feature.  Added `j' (Info-goto-bookmark), like `g' but only
248 ;; completes bookmarks.
249 ;;
250 ;; Added `<<tag>>' as alternate to `"tag"' in annotations.
251 ;;
252 ;; Added `v' (Info-visit-file), like Info-goto-node but specialized
253 ;; for going to a new Info file (with file name completion).
254 ;;
255 ;; Added recognition of gzip'd ".z" files.
256
257
258 ;; Modified 5/9/1993 by Dave Gillespie:
259 ;;
260 ;; Merged in various things from FSF's latest Emacs 19 info.el.
261
262 ;; Modified 6/2/1993 by Dave Gillespie:
263 ;;
264 ;; Changed to use new suffix ".gz" for gzip files.
265
266
267 ;; Modified 7/22/1993 by Dave Gillespie:
268 ;;
269 ;; Changed Info-footnote-tag to "See" instead of "Ref".
270 ;;
271 ;; Extended Info-fontify-node to work with FSF version of Emacs 19.
272
273 ;; Modified 7/30/1993 by Jamie Zawinski:
274 ;;
275 ;; Commented out the tty and fsf19 mouse support, because why bother.
276 ;; Commented out the politically incorrect version of XEmacs mouse support.
277 ;; Commented out mouse scrolling bindings because the party line on that
278 ;;  is "scrollbars are coming soon."
279 ;; Commented out munging of help-for-help's doc; put it in help.el.
280 ;; Did Info-edit-map the modern XEmacs way.
281 ;; Pruned extra cruft from fontification and mouse handling code.
282 ;; Fixed ASCII-centric bogosity in unreading of events.
283
284 ;; Modified 8/11/95 by Chuck Thompson:
285 ;;
286 ;; Removed any pretense of ever referencing Info-directory since it
287 ;; wasn't working anyhow.
288
289 ;; Modified 4/5/97 by Tomasz J. Cholewo:
290 ;;
291 ;; Modified Info-search to use with-caps-disable-folding
292
293 ;; Modified 6/21/97 by Hrvoje Niksic
294 ;;
295 ;; Fixed up Info-next-reference to work sanely when n < 0.
296 ;; Added S-tab binding.
297
298 ;; Modified 1997-07-10 by Karl M. Hegbloom
299 ;;
300 ;; Added `Info-minibuffer-history'
301 ;; (also added to defaults in "lisp/utils/savehist.el")
302 ;;  Other changes in main ChangeLog.
303
304 ;; Modified 1998-03-29 by Oscar Figueiredo
305 ;;
306 ;; Added automatic dir/localdir (re)building capability for directories that
307 ;; contain none or when it has become older than info files in the same
308 ;; directory.
309
310 ;; Modified 1998-09-23 by Didier Verna <didier@xemacs.org>
311 ;;
312 ;; Use the new macro `with-search-caps-disable-folding'
313
314 ;; Code:
315 (eval-when-compile
316   (condition-case nil (require 'browse-url) (error nil)))
317
318 (defgroup info nil
319   "The info package for Emacs."
320   :group 'help
321   :group 'docs)
322
323 (defgroup info-faces nil
324   "The faces used by info browser."
325   :group 'info
326   :group 'faces)
327
328
329 (defcustom Info-inhibit-toolbar nil
330   "*Non-nil means don't use the specialized Info toolbar."
331   :type 'boolean
332   :group 'info)
333
334 (defcustom Info-novice nil
335   "*Non-nil means to ask for confirmation before switching Info files."
336   :type 'boolean
337   :group 'info)
338
339 ;; Presently, the only reason this exists is to keep a few FSF-originated
340 ;; packages happy. We don't use it. --SY
341 (defvar Info-file-list-for-emacs nil)
342 (make-compatible-variable 'Info-file-list-for-emacs
343                           "SXEmacs does NOT use this")
344
345 (defvar Info-history nil
346   "List of info nodes user has visited.
347 Each element of list is a list (\"(FILENAME)NODENAME\" BUFPOS WINSTART).")
348
349 (defvar Info-keeping-history t
350   "Non-nil if Info-find-node should modify Info-history.
351 This is for use only by certain internal Info routines.")
352
353 (defvar Info-minibuffer-history nil
354   "Minibuffer history for Info.")
355
356 (defcustom Info-enable-edit nil
357   "*Non-nil means the \\<Info-mode-map>\\[Info-edit] command in Info
358 can edit the current node.
359 This is convenient if you want to write info files by hand.
360 However, we recommend that you not do this.
361 It is better to write a Texinfo file and generate the Info file from that,
362 because that gives you a printed manual as well."
363   :type 'boolean
364   :group 'info)
365
366 (defcustom Info-enable-active-nodes t
367   "*Non-nil allows Info to execute Lisp code associated with nodes.
368 The Lisp code is executed when the node is selected."
369   :type 'boolean
370   :group 'info)
371
372 (defcustom Info-restoring-point t
373   "*Non-nil means to restore the cursor position when re-entering a node."
374   :type 'boolean
375   :group 'info)
376
377 (defcustom Info-auto-advance 'twice
378   "*Control what SPC and DEL do when they can't scroll any further.
379 If nil, they beep and remain in the current node.
380 If t, they move to the next node (like Info-global-next/prev).
381 If anything else, they must be pressed twice to move to the next node."
382   :type '(choice (const :tag "off" nil)
383                  (const :tag "advance" t)
384                  (const :tag "confirm" twice))
385   :group 'info)
386
387 (defcustom Info-fontify t
388   "*Non-nil enables font features in XEmacs.
389 This variable is ignored unless running under XEmacs."
390   :type 'boolean
391   :group 'info)
392
393 (defcustom Info-additional-search-directory-list nil
394   "*List of additional directories to search for Info documentation
395 files.  These directories are not searched for merging the `dir'
396 file. An example might be something like:
397 \"/usr/local/lib/xemacs/packages/lisp/calc/\""
398   :type '(repeat directory)
399   :group 'info)
400
401 (defcustom Info-auto-generate-directory 'if-outdated
402   "*When to auto generate an info directory listing.
403 Possible values are:
404 nil or `never' never auto-generate a directory listing,
405   use any existing `dir' or `localdir' file and ignore info
406   directories containing none
407 `always' auto-generate a directory listing ignoring existing
408   `dir' and `localdir' files
409 `if-missing', the default, auto-generates a directory listing
410   if no `dir' or `localdir' file is present.  Otherwise the
411   contents of any of these files is used instead.
412 `if-outdated' auto-generates a directory listing if the `dir'
413   and `localdir' are either inexistent or outdated (touched
414   less recently than an info file in the same directory)."
415   :type '(choice (const :tag "never" never)
416                  (const :tag "always" always)
417                  (const :tag "if-missing" if-missing)
418                  (const :tag "if-outdated" if-outdated))
419   :group 'info)
420
421 (defcustom Info-save-auto-generated-dir 'never
422   "*Whether an auto-generated info directory listing should be saved.
423 Possible values are:
424 nil or `never', the default, auto-generated info directory
425   information will never be saved.
426 `always', auto-generated info directory information will be saved to
427   a `dir' file in the same directory overwriting it if it exists
428 `conservative', auto-generated info directory information will be saved
429   to a `dir' file in the same directory but the user is asked before
430   overwriting any existing file."
431   :type '(choice (const :tag "never" never)
432                  (const :tag "always" always)
433                  (const :tag "conservative" conservative))
434   :group 'info)
435
436 (defconst Info-emacs-info-file-name "sxemacs.info"
437   "The filename of the SXEmacs info for `Info-goto-emacs-command-node'
438 \(`\\<help-mode-map>\\[Info-goto-emacs-command-node]'\)")
439
440 ;;;###autoload
441 (defvar Info-directory-list nil
442   "List of directories to search for Info documentation files.
443
444 The first directory in this list, the \"dir\" file there will become
445 the (dir)Top node of the Info documentation tree.
446
447 Note: DO NOT use the `customize' interface to change the value of this
448 variable.  Its value is created dynamically on each startup, depending
449 on XEmacs packages installed on the system.  If you want to change the
450 search path, make the needed modifications on the variable's value
451 from .emacs.  For instance:
452
453     (setq Info-directory-list (cons \"~/info\" Info-directory-list))")
454
455 ;; This could as well be hard-coded since ${srcdir}/info/dir is in CVS --dv
456 (defconst Info-localdir-heading-regexp "^Local Packages:$"
457   "The menu part of localdir files will be inserted below this topic
458 heading.")
459
460 (defface info-node '((t (:bold t :italic t)))
461   "Face used for node links in info."
462   :group 'info-faces)
463
464 (defface info-xref '((t (:bold t)))
465   "Face used for cross-references in info."
466   :group 'info-faces)
467
468 ;; This list is based on Karl Berry-s advice about extensions `info' itself
469 ;; might encounter. --dv
470 (defcustom Info-suffix-list '(("" . nil)
471                               (".info" . nil)
472                               (".gz" . "gzip -dc %s")
473                               (".info.gz" . "gzip -dc %s")
474                               (".z" . "gzip -dc %s")
475                               (".info.z" . "gzip -dc %s")
476                               (".bz2" . "bzip2 -dc %s")
477                               (".info.bz2" . "bzip2 -dc %s")
478                               (".Z" . "uncompress -c %s")
479                               (".info.Z" . "uncompress -c %s")
480                               (".zip" . "unzip -c %s")
481                               (".info.zip" . "unzip -c %s")
482                               (".y" . "cat %s | unyabba")
483                               ("info.y" . "cat %s | unyabba")
484                               ;; These ones are for MS-DOS filenames.
485                               (".inf" . nil)
486                               (".igz" . "gzip -dc %s")
487                               (".inz" . "gzip -c %s"))
488   "*List of file name suffixes and associated decoding commands.
489 Each entry should be (SUFFIX . STRING); if STRING contains %s, that is
490 changed to name of the file to decode, otherwise the file is given to
491 the command as standard input.  If STRING is nil, no decoding is done."
492   :type '(repeat (cons (string :tag "suffix")
493                        (choice :tag "command"
494                                (const  :tag "none" :value nil)
495                                (string :tag ""))))
496   :group 'info)
497
498 (defcustom Info-footnote-tag "Note"
499   "*Symbol that identifies a footnote or cross-reference.
500 All \"*Note\" references will be changed to use this word instead."
501   :type 'string
502   :group 'info)
503
504 (defvar Info-current-file nil
505   "Info file that Info is now looking at, or nil.
506 This is the name that was specified in Info, not the actual file name.
507 It doesn't contain directory names or file name extensions added by Info.")
508
509 (defvar Info-current-subfile nil
510   "Info subfile that is actually in the *info* buffer now,
511 or nil if current info file is not split into subfiles.")
512
513 (defvar Info-current-node nil
514   "Name of node that Info is now looking at, or nil.")
515
516 (defvar Info-tag-table-marker nil
517   "Marker pointing at beginning of current Info file's tag table.
518 Marker points nowhere if file has no tag table.")
519
520 (defvar Info-tag-table-buffer nil)
521
522 (defvar Info-current-file-completions nil
523   "Cached completion list for current Info file.")
524
525 (defvar Info-current-annotation-completions nil
526   "Cached completion list for current annotation files.")
527
528 (defvar Info-index-alternatives nil
529   "List of possible matches for last Info-index command.")
530
531 (defvar Info-index-first-alternative nil)
532
533 (defcustom Info-annotations-path
534   (list
535    (paths-construct-path (list user-init-directory "info.notes"))
536    (paths-construct-path '("~" ".infonotes"))
537    (paths-construct-path '("usr" "lib" "info.notes")
538                          (char-to-string directory-sep-char)))
539   "*Names of files that contain annotations for different Info nodes.
540 By convention, the first one should reside in your personal directory.
541 The last should be a world-writable \"public\" annotations file."
542   :type '(repeat file)
543   :group 'info)
544
545 (defcustom Info-button1-follows-hyperlink nil
546   "*Non-nil means mouse button1 click will follow hyperlink."
547   :type 'boolean
548   :group 'info)
549
550 (defvar Info-standalone nil
551   "Non-nil if Emacs was started solely as an Info browser.")
552
553 (defvar Info-in-cross-reference nil)
554 (defvar Info-window-configuration nil)
555
556 (defvar Info-dir-prologue "-*- Text -*-
557 This is the file .../info/dir, which contains the topmost node of the
558 Info hierarchy.  The first time you invoke Info you start off
559 looking at that node, which is (dir)Top.
560 \037
561 File: dir       Node: Top       This is the top of the INFO tree
562   This (the Directory node) gives a menu of major topics.
563
564 * Menu: The list of major topics begins on the next line.
565
566 ")
567
568 (defcustom Info-no-description-string "[No description available]"
569   "*Description string for info files that have none"
570   :type 'string
571   :group 'info)
572
573 ;;;###autoload
574 (defun info (&optional file)
575   "Enter Info, the documentation browser.
576 Optional argument FILE specifies the file to examine;
577 the default is the top-level directory of Info.
578
579 In interactive use, a prefix argument directs this command
580 to read a file name from the minibuffer."
581   (interactive (if current-prefix-arg
582                    (list (read-file-name "Info file name: " nil nil t))))
583   (let ((p command-line-args))
584     (while p
585       (and (string-match "^-[fe]" (car p))
586            (equal (nth 1 p) "info")
587            (not Info-standalone)
588            (setq Info-standalone t)
589            (= (length p) 3)
590            (not (string-match "^-" (nth 2 p)))
591            (setq file (nth 2 p))
592            (setq command-line-args-left nil))
593       (setq p (cdr p))))
594 ;  (Info-setup-x) ??? What was this going to be?  Can anyone tell karlheg?
595   (if file
596       (unwind-protect
597           (Info-goto-node (concat "(" file ")"))
598         (and Info-standalone (info)))
599     (if (get-buffer "*info*")
600         (switch-to-buffer "*info*")
601       (Info-directory))))
602
603 ;;;###autoload
604 (defun Info-query (file)
605   "Enter Info, the documentation browser.  Prompt for name of Info file."
606   (interactive "sInfo topic (default = menu): ")
607   (info)
608   (if (equal file "")
609       (Info-goto-node "(dir)")
610     (Info-goto-node (concat "(" file ")"))))
611
612 (defun Info-setup-initial ()
613   (let ((f Info-annotations-path))
614     (while f
615       (if (and (file-exists-p (car f)) (not (get-file-buffer (car f))))
616           (bury-buffer (find-file-noselect (car f))))
617       (setq f (cdr f)))))
618
619 ;;;###autoload
620 (defun Info-find-node (filename &optional nodename no-going-back tryfile line)
621   "Go to an info node specified as separate FILENAME and NODENAME.
622 Look for a plausible filename, or if not found then look for URL's and
623 dispatch to the appropriate fn.  NO-GOING-BACK is non-nil if
624 recovering from an error in this function; it says do not attempt
625 further (recursive) error recovery.  TRYFILE is ??"
626
627   (Info-setup-initial)
628
629   (cond
630    ;; empty filename is simple case
631    ((null filename)
632     (Info-find-file-node nil nodename no-going-back tryfile line))
633    ;; Convert filename to lower case if not found as specified.
634    ;; Expand it, look harder...
635    ((let ((fname (substitute-in-file-name filename))
636           temp found)
637       (let ((dirs (cond
638                    ;; If specified name starts with `./', then just try
639                    ;; current directory. No point in searching for an absolute
640                    ;; file name
641                    ((string-match "^\\./" fname)
642                     (list default-directory))
643                    ((file-name-absolute-p fname)
644                     '(nil))
645                    (Info-additional-search-directory-list
646                     (append Info-directory-list
647                             Info-additional-search-directory-list))
648                    (t Info-directory-list))))
649         ;; Search the directory list for file FNAME.
650         (while (and dirs (not found))
651           (setq temp (expand-file-name fname (car dirs)))
652           (setq found (Info-suffixed-file temp))
653           (setq dirs (cdr dirs)))
654         (if found
655             (progn (setq filename (expand-file-name found))
656                    t))))
657     (Info-find-file-node filename nodename no-going-back tryfile line))
658    ;; Look for a URL.  This pattern is stolen from w3.el to prevent
659    ;; loading it if we won't need it.
660    ((string-match  (concat #r"^\(wais\|solo\|x-exec\|newspost\|www\|"
661                            #r"mailto\|news\|tn3270\|ftp\|http\|file\|"
662                            #r"telnet\|gopher\):")
663                    filename)
664     (if-fboundp 'browse-url
665         (browse-url filename)
666       (error "Cannot follow URLs in this SXEmacs")))
667    (t
668     (error "Info file %s does not exist" filename))))
669
670 (defun Info-find-file-node (filename nodename
671                                      &optional no-going-back tryfile line)
672   ;; This is the guts of what was Info-find-node. Whoever wrote this
673   ;; should be locked up where they can't do any more harm.
674
675   ;; Go into info buffer.
676   (or (eq major-mode 'Info-mode)
677       (switch-to-buffer "*info*"))
678   (buffer-disable-undo (current-buffer))
679   (run-hooks 'Info-startup-hook)
680   (or (eq major-mode 'Info-mode)
681       (Info-mode))
682   (or (null filename)
683       (equal Info-current-file filename)
684       (not Info-novice)
685       (string= "dir" (file-name-nondirectory Info-current-file))
686       (if (y-or-n-p
687            (format "Leave Info file `%s'? "
688                    (file-name-nondirectory Info-current-file)))
689           (message "")
690         (keyboard-quit)))
691   ;; Record the node we are leaving.
692   (if (and Info-current-file (not no-going-back))
693       (Info-history-add Info-current-file Info-current-node (point)))
694   (widen)
695   (setq Info-current-node nil
696         Info-in-cross-reference nil)
697   (unwind-protect
698       (progn
699         ;; Switch files if necessary
700         (or (null filename)
701             (equal Info-current-file filename)
702             (let ((buffer-read-only nil))
703               (setq Info-current-file nil
704                     Info-current-subfile nil
705                     Info-current-file-completions nil
706                     Info-index-alternatives nil
707                     buffer-file-name nil
708                     buffer-file-truename nil)
709               (erase-buffer)
710               (if (string= "dir" (file-name-nondirectory filename))
711                   (Info-insert-dir)
712                 (Info-insert-file-contents filename t)
713                 (setq default-directory (file-name-directory filename)))
714               (set-buffer-modified-p nil)
715               ;; See whether file has a tag table.  Record the location if yes.
716               (set-marker Info-tag-table-marker nil)
717               (goto-char (point-max))
718               (forward-line -8)
719               (or (equal nodename "*")
720                   (not (search-forward "\037\nEnd tag table\n" nil t))
721                   (let (pos)
722                     ;; We have a tag table.  Find its beginning.
723                     ;; Is this an indirect file?
724                     (search-backward "\nTag table:\n")
725                     (setq pos (point))
726                     (if (save-excursion
727                           (forward-line 2)
728                           (looking-at "(Indirect)\n"))
729                         ;; It is indirect.  Copy it to another buffer
730                         ;; and record that the tag table is in that buffer.
731                           (let ((buf (current-buffer))
732                                 (m Info-tag-table-marker))
733                             (or
734                              Info-tag-table-buffer
735                              (setq
736                               Info-tag-table-buffer
737                               (generate-new-buffer " *info tag table*")))
738                             (save-excursion
739                               (set-buffer Info-tag-table-buffer)
740                               (buffer-disable-undo (current-buffer))
741                               (setq case-fold-search t)
742                               (erase-buffer)
743                               (insert-buffer-substring buf)
744                               (set-marker m (match-end 0))))
745                      (set-marker Info-tag-table-marker pos))))
746               (setq Info-current-file
747                     (file-name-sans-versions buffer-file-name))))
748         (if (equal nodename "*")
749             (progn (setq Info-current-node nodename)
750                    (Info-set-mode-line)
751                    (goto-char (point-min)))
752           ;; Search file for a suitable node.
753           (let* ((qnode (regexp-quote nodename))
754                  (regexp (concat "Node: *" qnode " *[,\t\n\177]"))
755                  (guesspos (point-min))
756                  (found t))
757             ;; First get advice from tag table if file has one.
758             ;; Also, if this is an indirect info file,
759             ;; read the proper subfile into this buffer.
760             (if (marker-position Info-tag-table-marker)
761                 (let (foun found-mode (m Info-tag-table-marker))
762                   (save-excursion
763                     (set-buffer (marker-buffer Info-tag-table-marker))
764                     (goto-char m)
765                     (setq foun (re-search-forward regexp nil t))
766                     (if foun
767                         (setq guesspos (read (current-buffer))))
768                     (setq found-mode major-mode))
769                   (if foun
770                       ;; If this is an indirect file,
771                       ;; determine which file really holds this node
772                       ;; and read it in.
773                       (if (not (eq major-mode found-mode))
774                           (setq guesspos
775                                 (Info-read-subfile guesspos))))))
776             (if (eq buffer-file-coding-system (find-coding-system 'utf-8))
777                 (goto-char (point-min))
778               (goto-char (max (point-min) (- guesspos 1000))))
779             ;; Now search from our advised position (or from beg of buffer)
780             ;; to find the actual node.
781             (catch 'foo
782               (while (search-forward "\n\037" nil t)
783                 (forward-line 1)
784                 (let ((beg (point)))
785                   (forward-line 1)
786                   (if (re-search-backward regexp beg t)
787                       (throw 'foo t))))
788               (setq found nil)
789               (let ((bufs (delq nil (mapcar 'get-file-buffer
790                                             Info-annotations-path)))
791                     (pattern (if (string-match #r"\`<<.*>>\'" qnode) qnode
792                                (format "\"%s\"\\|<<%s>>" qnode qnode)))
793                     (pat2 (concat #r"------ *File: *\([^ ].*[^ ]\) *Node: "
794                                   #r"*\([^ ].*[^ ]\) *Line: *\([0-9]+\)"))
795                     (afile nil) anode aline)
796                 (while (and bufs (not anode))
797                   (save-excursion
798                     (set-buffer (car bufs))
799                     (goto-char (point-min))
800                     (if (re-search-forward pattern nil t)
801                         (if (re-search-backward pat2 nil t)
802                             (setq afile (buffer-substring (match-beginning 1)
803                                                           (match-end 1))
804                                   anode (buffer-substring (match-beginning 2)
805                                                           (match-end 2))
806                                   aline (string-to-int
807                                          (buffer-substring (match-beginning 3)
808                                                            (match-end 3)))))))
809                   (setq bufs (cdr bufs)))
810                 (if anode
811                     (Info-find-node afile anode t nil aline)
812                   (if tryfile
813                       (condition-case nil
814                           (Info-find-node nodename "Top" t)
815                         (error nil)))))
816               (or Info-current-node
817                   (error "No such node: %s" nodename)))
818             (if found
819                 (progn
820                   (Info-select-node)
821                   (goto-char (point-min))
822                   (if line (forward-line line)))))))
823     ;; If we did not finish finding the specified node,
824     ;; go back to the previous one.
825     (or Info-current-node no-going-back
826         (let ((hist (car Info-history)))
827           ;; The following is no longer safe with new Info-history system
828           ;; (setq Info-history (cdr Info-history))
829           (Info-goto-node (car hist) t)
830           (goto-char (+ (point-min) (nth 1 hist)))))))
831
832 ;; Cache the contents of the (virtual) dir file, once we have merged
833 ;; it for the first time, so we can save time subsequently.
834 (defvar Info-dir-contents nil)
835
836 ;; Cache for the directory we decided to use for the default-directory
837 ;; of the merged dir text.
838 (defvar Info-dir-contents-directory nil)
839
840 ;; Record the file attributes of all the files from which we
841 ;; constructed Info-dir-contents.
842 (defvar Info-dir-file-attributes nil)
843
844 (defun Info-insert-dir ()
845   "Construct the Info directory node by merging the files named
846 \"dir\" or \"localdir\" from the directories in `Info-directory-list'.
847 The \"dir\" files will take precedence in cases where both exist.  It
848 sets the *info* buffer's `default-directory' to the first directory we
849 actually get any text from."
850   (if (and Info-dir-contents Info-dir-file-attributes
851            ;; Verify that none of the files we used has changed
852            ;; since we used it.
853            (eval (cons 'and
854                        (mapcar #'(lambda (elt)
855                                    (let ((curr (file-attributes (car elt))))
856                                      ;; Don't compare the access time.
857                                      (if curr (setcar (nthcdr 4 curr) 0))
858                                      (setcar (nthcdr 4 (cdr elt)) 0)
859                                      (equal (cdr elt) curr)))
860                                Info-dir-file-attributes))))
861       (insert Info-dir-contents)
862     (let ((dirs (reverse Info-directory-list))
863           buffers lbuffers buffer others nodes dirs-done)
864
865       (setq Info-dir-file-attributes nil)
866
867       ;; Search the directory list for the directory file.
868       (while dirs
869         (let ((truename (file-truename (expand-file-name (car dirs)))))
870           (or (member truename dirs-done)
871               (member (directory-file-name truename) dirs-done)
872               ;; Karl Berry recently added the ability all possibilities for
873               ;; extension as for normal info files. This code however is
874               ;; still unsatisfactory: if one day, we find a compressed dir
875               ;; file (which looks possible), we should be able to handle it
876               ;; (which means decompress and read it, update it, save and
877               ;; recompress it). --dv
878               (let ((trials '("dir" "DIR"
879                               "dir.info" "DIR.INFO"
880                               "dir.inf" "DIR.INF"
881                               "localdir" "LOCALDIR"
882                               "localdir.info" "LOCALDIR.INFO"
883                               "localdir.inf" "LOCALDIR.INF"))
884                     buf file attrs)
885                 (catch 'found
886                   (while (setq file (pop trials))
887                     (setq file (expand-file-name file truename))
888                     (and (setq attrs (file-attributes file))
889                          (throw 'found t))))
890                 (unless file
891                   (setq file (expand-file-name "dir" truename)))
892                 (setq dirs-done
893                       (cons truename
894                             (cons (directory-file-name truename)
895                                   dirs-done)))
896                 (Info-maybe-update-dir file)
897                 (setq attrs (file-attributes file))
898                 (if (or (setq buf (find-buffer-visiting file))
899                         attrs)
900                     (save-excursion
901                       (or buffers
902                           (message "Composing main Info directory..."))
903                       (set-buffer (or buf
904                                       (generate-new-buffer
905                                        (if (string-match "localdir" file)
906                                            "localdir"
907                                          "info dir"))))
908                       (if (not buf)
909                           (insert-file-contents file))
910                       (if (string-match "localdir" (buffer-name))
911                           (setq lbuffers (cons (current-buffer) lbuffers))
912                         (setq buffers (cons (current-buffer) buffers)))
913                       (if attrs
914                           (setq Info-dir-file-attributes
915                                 (cons (cons file attrs)
916                                       Info-dir-file-attributes)))))))
917           (or (cdr dirs) (setq Info-dir-contents-directory (car dirs)))
918           (setq dirs (cdr dirs))))
919
920       ;; ensure that the localdir files are inserted last, and reverse
921       ;; the list of them so that when they get pushed in, they appear
922       ;; in the same order they got specified in the path, from top to
923       ;; bottom.
924       (nconc buffers (reverse lbuffers))
925
926       (or buffers
927           (error "Can't find the Info directory node"))
928       ;; Distinguish the dir file that comes with Emacs from all the
929       ;; others.  Yes, that is really what this is supposed to do.
930       ;; If it doesn't work, fix it.
931       (setq buffer (car buffers)
932             ;; reverse it since they are pushed down from the top. the
933             ;; `Info-directory-list can be specified in natural order
934             ;; this way.
935             others (reverse (cdr buffers)))
936
937       ;; Insert the entire original dir file as a start; note that we've
938       ;; already saved its default directory to use as the default
939       ;; directory for the whole concatenation.
940       (insert-buffer buffer)
941
942       ;; Look at each of the other buffers one by one.
943       (while others
944         (let ((other (car others))
945               (info-buffer (current-buffer)))
946           (if (string-match "localdir" (buffer-name other))
947               (save-excursion
948                 (set-buffer info-buffer)
949                 (goto-char (point-max))
950                 (cond
951                  ((re-search-backward "^ *\\* *Locals *: *$" nil t)
952                   (delete-region (match-beginning 0) (match-end 0)))
953                  ;; look for a line like |Local XEmacs packages:
954                  ;; or mismatch on some text ...
955                  ((re-search-backward Info-localdir-heading-regexp nil t)
956                   ;; This is for people who underline topic headings with
957                   ;; equal signs or dashes.
958                   (when (save-excursion
959                           (forward-line 1)
960                           (beginning-of-line)
961                           (looking-at "^[ \t]*[-=*]+"))
962                     (forward-line 1))
963                   (forward-line 1)
964                   (beginning-of-line))
965                  (t (search-backward "\^L" nil t)))
966                 ;; Insert menu part of the file
967                 (let* ((pt (point))
968                        (len (length (buffer-string nil nil other))))
969                   (insert (buffer-string nil nil other))
970                   (goto-char (+ pt len))
971                   (save-excursion
972                     (goto-char pt)
973                     (if (search-forward "* Menu:" (+ pt len) t)
974                         (progn
975                           (forward-line 1)
976                           (delete-region pt (point)))))))
977             ;; In each, find all the menus.
978             (save-excursion
979               (set-buffer other)
980               (goto-char (point-min))
981               ;; Find each menu, and add an elt to NODES for it.
982               (while (re-search-forward "^\\* Menu:" nil t)
983                 (let (beg nodename end)
984                   (forward-line 1)
985                   (setq beg (point))
986                   (search-backward "\n\037")
987                   (search-forward "Node: ")
988                   (setq nodename (Info-following-node-name))
989                   (search-forward "\n\037" nil 'move)
990                   (beginning-of-line)
991                   (setq end (point))
992                   (setq nodes (cons (list nodename other beg end) nodes))))))
993           (setq others (cdr others))))
994
995       ;; Add to the main menu a menu item for each other node.
996       (re-search-forward "^\\* Menu:" nil t)
997       (forward-line 1)
998       (let ((menu-items '("top"))
999             (nodes nodes)
1000             (case-fold-search t)
1001             (end (save-excursion (search-forward "\037" nil t) (point))))
1002         (while nodes
1003           (let ((nodename (car (car nodes))))
1004             (save-excursion
1005               (or (member (downcase nodename) menu-items)
1006                   (re-search-forward (concat "^\\* "
1007                                              (regexp-quote nodename)
1008                                              "::")
1009                                      end t)
1010                   (progn
1011                     (insert "* " nodename "::" "\n")
1012                     (setq menu-items (cons nodename menu-items))))))
1013           (setq nodes (cdr nodes))))
1014       ;; Now take each node of each of the other buffers
1015       ;; and merge it into the main buffer.
1016       (while nodes
1017         (let ((nodename (car (car nodes))))
1018           (goto-char (point-min))
1019           ;; Find the like-named node in the main buffer.
1020           (if (re-search-forward (concat "\n\037.*\n.*Node: "
1021                                          (regexp-quote nodename)
1022                                          "[,\n\t]")
1023                                  nil t)
1024               (progn
1025                 (search-forward "\n\037" nil 'move)
1026                 (beginning-of-line)
1027                 (insert "\n"))
1028             ;; If none exists, add one.
1029             (goto-char (point-max))
1030             (insert "\037\nFile: dir\tNode: " nodename "\n\n* Menu:\n\n"))
1031           ;; Merge the text from the other buffer's menu
1032           ;; into the menu in the like-named node in the main buffer.
1033           (apply 'insert-buffer-substring (cdr (car nodes))))
1034         (setq nodes (cdr nodes)))
1035       ;; Kill all the buffers we just made.
1036       (while buffers
1037         (kill-buffer (car buffers))
1038         (setq buffers (cdr buffers)))
1039       (while lbuffers
1040         (kill-buffer (car lbuffers))
1041         (setq lbuffers (cdr lbuffers)))
1042       (message "Composing main Info directory...done"))
1043     (setq Info-dir-contents (buffer-string)))
1044   (setq default-directory (file-name-as-directory Info-dir-contents-directory))
1045   (setq buffer-file-name (caar Info-dir-file-attributes)
1046         buffer-file-truename (file-truename buffer-file-name)))
1047
1048 (defmacro Info-directory-files (dir-file &optional all full nosort files-only)
1049   "Return a list of Info files living in the same directory as DIR-FILE.
1050 This list actually contains the files living in this directory, except for
1051 the dir file itself and the secondary info files (foo-1 foo-2 etc).
1052
1053 If the optional argument ALL is non nil, the secondary info files are also
1054 included in the list.
1055
1056 Please refer to the function `directory-files' for the meaning of the other
1057 optional arguments."
1058   `(let* ((dir (file-name-directory ,dir-file))
1059           (all-files (remove ,dir-file (directory-files dir ',full nil ',nosort
1060                                                         ',files-only))))
1061      (setq all-files
1062            (if ,full
1063                (remove (concat dir ".")
1064                        (remove (concat dir "..") all-files))
1065              (remove "."
1066                      (remove ".." all-files))))
1067      (if ,all
1068          all-files
1069        (let ((suff-match
1070               (concat "-[0-9]+\\("
1071                       ;; Extract all known compression suffixes from
1072                       ;; Info-suffix-list. These suffixes can typically  be
1073                       ;; found in entries of the form `.info.something'.
1074                       (let ((suff-list Info-suffix-list)
1075                             suff regexp)
1076                         (while (setq suff (pop suff-list))
1077                           (and (string-match "^\\.info" (car suff))
1078                                (setq regexp (concat regexp
1079                                                     (regexp-quote
1080                                                      (substring
1081                                                       (car suff) 5))
1082                                                     (and suff-list "\\|")))))
1083                         regexp)
1084                       "\\)?$"))
1085              info-files file)
1086          (while (setq file (pop all-files))
1087            (or (string-match suff-match file)
1088                (push file info-files)))
1089          (reverse info-files)
1090          ))
1091      ))
1092
1093 (defun Info-maybe-update-dir (file)
1094   "Rebuild dir or localdir according to `Info-auto-generate-directory'."
1095   (unless (or (not (file-exists-p (file-name-directory file)))
1096               (null (Info-directory-files file 'all)))
1097     (if (not (find-buffer-visiting file))
1098         (if (not (file-exists-p file))
1099             (if (or (memq Info-auto-generate-directory
1100                           '(always if-missing if-outdated)))
1101                 (Info-build-dir-anew (file-name-directory file)))
1102           (if (or (eq Info-auto-generate-directory 'always)
1103                   (and (eq Info-auto-generate-directory 'if-outdated)
1104                        (Info-dir-outdated-p file)))
1105               (Info-rebuild-dir file))))))
1106
1107 ;; Record which *.info files are newer than the dir file
1108 (defvar Info-dir-newer-info-files nil)
1109
1110 (defun Info-dir-outdated-p (file)
1111   "Return non-nil if dir or localdir is outdated.
1112 dir or localdir are outdated when an info file in the same
1113 directory has been modified more recently."
1114   (let ((dir-mod-time (nth 5 (file-attributes file)))
1115         f-mod-time newer)
1116     (setq Info-dir-newer-info-files nil)
1117     (mapcar
1118      #'(lambda (f)
1119          (prog2
1120              (setq f-mod-time (nth 5 (file-attributes f)))
1121              (setq newer (or (> (car f-mod-time) (car dir-mod-time))
1122                              (and (= (car f-mod-time) (car dir-mod-time))
1123                                   (> (car (cdr f-mod-time))
1124                                      (car (cdr dir-mod-time))))))
1125            (if (and (file-readable-p f) newer)
1126                (setq Info-dir-newer-info-files
1127                      (cons f Info-dir-newer-info-files)))))
1128      (Info-directory-files file nil 'fullname 'nosort t))
1129     Info-dir-newer-info-files))
1130
1131 (defun Info-extract-dir-entry-from (file)
1132   "Extract the dir entry from the info FILE.
1133 The dir entry is delimited by the markers `START-INFO-DIR-ENTRY'
1134 and `END-INFO-DIR-ENTRY'."
1135   (save-excursion
1136     (set-buffer (get-buffer-create " *Info-tmp*"))
1137     (when (file-readable-p file)
1138       (insert-file-contents file nil nil nil t)
1139       (goto-char (point-min))
1140       (let (beg)
1141         (unless (null (re-search-forward "^START-INFO-DIR-ENTRY" nil t))
1142           (forward-line 1)
1143           (setq beg (point))
1144           (unless (null (re-search-forward "^END-INFO-DIR-ENTRY" nil t))
1145             (goto-char (match-beginning 0))
1146             (car (Info-parse-dir-entries beg (point)))))))))
1147
1148 ;; Parse dir entries contained between START and END into a list of the form
1149 ;; (filename topic node (description-line-1 description-line-2 ...))
1150 (defun Info-parse-dir-entries (start end)
1151   (let (entry entries)
1152     (save-excursion
1153       (save-restriction
1154         (narrow-to-region start end)
1155         (goto-char start)
1156         (while (re-search-forward
1157                 (concat #r"^\* \([^:]+\):\("
1158                         "[ \t]*"
1159                         #r"(\([^)]*\))\w*\.\|:\)")
1160                 nil t)
1161           (setq entry (list (match-string 2)
1162                             (match-string 1)
1163                             (downcase (or (match-string 3)
1164                                           (match-string 1)))))
1165           (setq entry
1166                 (cons (nreverse
1167                        (cdr
1168                         (nreverse
1169                          (split-string
1170                           (buffer-substring
1171                            (re-search-forward "[ \t]*" nil t)
1172                            (or (and (re-search-forward "^[^ \t]" nil t)
1173                                     (goto-char (match-beginning 0)))
1174                                (point-max)))
1175                           "[ \t]*\n[ \t]*"))))
1176                       entry))
1177           (setq entries (cons (nreverse entry) entries)))))
1178     (nreverse entries)))
1179
1180 (defun Info-dump-dir-entries (entries)
1181   (let ((tab-width 8)
1182         (description-col 0)
1183         len)
1184     (mapcar #'(lambda (e)
1185                 (setq e (cdr e))        ; Drop filename
1186                 (setq len (length (concat (car e)
1187                                           (car (cdr e)))))
1188                 (if (> len description-col)
1189                     (setq description-col len)))
1190             entries)
1191     (setq description-col (+ 5 description-col))
1192     (mapcar #'(lambda (e)
1193                 (setq e (cdr e))        ; Drop filename
1194                 (insert "* " (car e) ":" (car (cdr e)))
1195                 (setq e (car (cdr (cdr e))))
1196                 (while e
1197                   (indent-to-column description-col)
1198                   (insert (car e) "\n")
1199                   (setq e (cdr e))))
1200             entries)
1201     (insert "\n")))
1202
1203
1204 (defun Info-build-dir-anew (directory)
1205   "Build info directory information for DIRECTORY.
1206 The generated directory listing may be saved to a `dir' according
1207 to the value of `Info-save-auto-generated-dir'."
1208   (save-excursion
1209     (let* ((dirfile (expand-file-name "dir" directory))
1210            (to-temp (or (null Info-save-auto-generated-dir)
1211                         (eq Info-save-auto-generated-dir 'never)
1212                         (and (not (file-writable-p dirfile))
1213                              (message "File not writable %s. Using temporary."
1214                                       dirfile))))
1215            (info-files (Info-directory-files dirfile nil 'fullname nil t)))
1216       (if to-temp
1217           (message "Creating temporary dir in %s..." directory)
1218         (message "Creating %s..." dirfile))
1219       (set-buffer (find-file-noselect dirfile t))
1220       (setq buffer-read-only nil)
1221       (erase-buffer)
1222       (insert Info-dir-prologue "Info files in " directory ":\n\n")
1223       (Info-dump-dir-entries
1224        (mapcar
1225         #'(lambda (f)
1226             (or (Info-extract-dir-entry-from f)
1227                 (list 'dummy
1228                       (progn (string-match #r"\([^.]*\)\(\..*\)?$"
1229                                            (file-name-nondirectory f))
1230                              (capitalize
1231                               (match-string 1 (file-name-nondirectory f))))
1232                       ":"
1233                       (list Info-no-description-string))))
1234         info-files))
1235       (if to-temp
1236           (set-buffer-modified-p nil)
1237         (save-buffer))
1238       (if to-temp
1239           (message "Creating temporary dir in %s...done" directory)
1240         (message "Creating %s...done" dirfile)))))
1241
1242
1243 (defun Info-rebuild-dir (file)
1244   "Build info directory information in the directory of dir FILE.
1245 Description of info files are merged from the info files in the
1246 directory and the contents of FILE with the description in info files
1247 taking precedence over descriptions in FILE.
1248 The generated directory listing may be saved to a `dir' according to
1249 the value of `Info-save-auto-generated-dir'."
1250   (save-excursion
1251     (save-restriction
1252       (let (dir-section-contents dir-full-contents
1253             dir-entry
1254             file-dir-entry
1255             mark next-section
1256             not-first-section
1257             (to-temp
1258              (or (null Info-save-auto-generated-dir)
1259                  (eq Info-save-auto-generated-dir 'never)
1260                  (and (eq Info-save-auto-generated-dir 'always)
1261                       (not (file-writable-p file))
1262                       (message "File not writable %s. Using temporary." file))
1263                  (and (eq Info-save-auto-generated-dir 'conservative)
1264                       (or (and (not (file-writable-p file))
1265                                (message
1266                                 "File not writable %s. Using temporary." file))
1267                           (not (y-or-n-p
1268                                 (message "%s is outdated. Overwrite ? "
1269                                          file))))))))
1270         (set-buffer (find-file-noselect file t))
1271         (setq buffer-read-only nil)
1272         (if to-temp
1273             (message "Rebuilding temporary %s..." file)
1274           (message "Rebuilding %s..." file))
1275         (catch 'done
1276           (setq buffer-read-only nil)
1277           (goto-char (point-min))
1278           (unless (and (search-forward "\037")
1279                        (re-search-forward "^\\* Menu:.*$" nil t)
1280                        (setq mark (and (re-search-forward "^\\* " nil t)
1281                                        (match-beginning 0))))
1282             (throw 'done nil))
1283           (setq dir-full-contents (Info-parse-dir-entries mark (point-max)))
1284           (setq next-section (or (and (re-search-forward "^[^* \t].*:[ \t]*$"
1285                                                          nil t)
1286                                       (match-beginning 0))
1287                                  (point-max)))
1288           (while next-section
1289             (narrow-to-region mark next-section)
1290             (setq dir-section-contents (nreverse (Info-parse-dir-entries
1291                                                   (point-min) (point-max))))
1292             (mapcar
1293              #'(lambda (file)
1294                  (setq dir-entry (assoc (downcase
1295                                          (file-name-sans-extension
1296                                           (file-name-nondirectory file)))
1297                                         dir-section-contents)
1298                        file-dir-entry (Info-extract-dir-entry-from file))
1299                  (if dir-entry
1300                      (if file-dir-entry
1301                          ;; A dir entry in the info file takes precedence over
1302                          ;; an existing entry in the dir file
1303                          (setcdr dir-entry (cdr file-dir-entry)))
1304                    (unless (or not-first-section
1305                                (assoc (downcase
1306                                        (file-name-sans-extension
1307                                         (file-name-nondirectory file)))
1308                                       dir-full-contents))
1309                      (if file-dir-entry
1310                          (setq dir-section-contents
1311                                (cons file-dir-entry dir-section-contents))
1312                        (setq dir-section-contents
1313                              (cons (list 'dummy
1314                                          (capitalize (file-name-sans-extension
1315                                                       (file-name-nondirectory
1316                                                        file)))
1317                                          ":"
1318                                          (list Info-no-description-string))
1319                                    dir-section-contents))))))
1320              Info-dir-newer-info-files)
1321             (delete-region (point-min) (point-max))
1322             (Info-dump-dir-entries (nreverse dir-section-contents))
1323             (widen)
1324             (if (= next-section (point-max))
1325                 (setq next-section nil)
1326               (or (setq mark (and (re-search-forward "^\\* " nil t)
1327                                   (match-beginning 0)))
1328                   (throw 'done nil))
1329               (setq next-section (or (and (re-search-forward
1330                                            "^[^* \t].*:[ \t]*$" nil t)
1331                                           (match-beginning 0))
1332                                      (point-max))))
1333             (setq not-first-section t)))
1334         (if to-temp
1335             (progn
1336               (set-buffer-modified-p nil)
1337               (message "Rebuilding temporary %s...done" file))
1338           (save-buffer)
1339           (message "Rebuilding %s...done" file))))))
1340
1341 ;;;###autoload
1342 (defun Info-batch-rebuild-dir ()
1343   "(Re)build `dir' files in the directories remaining on the command line.
1344 Use this from the command line, with `-batch', it won't work in an
1345 interactive XEmacs.
1346
1347 Each file is processed even if an error occurred previously. For example,
1348 invoke \"sxemacs -batch -f Info-batch-rebuild-dir /usr/local/info\"."
1349   ;; command-line-args-left is what is left of the command line (from
1350   ;; startup.el)
1351   (defvar command-line-args-left)       ; Avoid 'free variable' warning
1352   (if (not noninteractive)
1353       (error "`Info-batch-rebuild-dir' is to be used only with -batch"))
1354   (let ((Info-save-auto-generated-dir 'always)
1355         dir localdir)
1356     (while command-line-args-left
1357       (if  (not (file-directory-p (car command-line-args-left)))
1358           (message "Warning: Skipped %s. Not a directory."
1359                    (car command-line-args-left))
1360         (setq dir (expand-file-name "dir" (car command-line-args-left)))
1361         (setq localdir (expand-file-name "localdir"
1362                                          (car command-line-args-left)))
1363         (cond
1364          ((file-exists-p dir)
1365           (Info-rebuild-dir dir))
1366          ((file-exists-p localdir)
1367           (Info-rebuild-dir localdir))
1368          (t
1369           (Info-build-dir-anew (car command-line-args-left)))))
1370       (setq command-line-args-left (cdr command-line-args-left)))
1371     (message "Done")
1372     (kill-emacs 0)))
1373
1374 (defun Info-history-add (file node point)
1375   (if Info-keeping-history
1376       (let* ((name (format "(%s)%s" (Info-file-name-only file) node))
1377              (found (assoc name Info-history)))
1378         (if found
1379             (setq Info-history (delq found Info-history)))
1380         (setq Info-history (cons (list name (- point (point-min))
1381                                        (and (eq (window-buffer)
1382                                                 (current-buffer))
1383                                             (- (window-start) (point-min))))
1384                                  Info-history)))))
1385
1386 (defun Info-file-name-only (file)
1387   (let ((dir (file-name-directory file))
1388         (p Info-directory-list))
1389     (while (and p (not (equal (car p) dir)))
1390       (setq p (cdr p)))
1391     (if p (file-name-nondirectory file) file)))
1392
1393 (defun Info-read-subfile (nodepos)
1394   (let (lastfilepos
1395         lastfilename)
1396     (save-excursion
1397       (set-buffer (marker-buffer Info-tag-table-marker))
1398       (goto-char (point-min))
1399       (search-forward "\n\037")
1400       (forward-line 2)
1401       (catch 'foo
1402         (while (not (looking-at "\037"))
1403           (if (not (eolp))
1404               (let ((start (point))
1405                     thisfilepos thisfilename)
1406                 (search-forward ": ")
1407                 (setq thisfilename  (buffer-substring start (- (point) 2)))
1408                 (setq thisfilepos (read (current-buffer)))
1409                 ;; read in version 19 stops at the end of number.
1410                 ;; Advance to the next line.
1411                 (if (eolp)
1412                     (forward-line 1))
1413                 (if (> thisfilepos nodepos)
1414                     (throw 'foo t))
1415                 (setq lastfilename thisfilename)
1416                 (setq lastfilepos thisfilepos))
1417             (throw 'foo t)))))
1418     (or (equal Info-current-subfile lastfilename)
1419         (let ((buffer-read-only nil))
1420           (setq buffer-file-name nil
1421                 buffer-file-truename nil)
1422           (widen)
1423           (erase-buffer)
1424           (Info-insert-file-contents (Info-suffixed-file
1425                                       (expand-file-name lastfilename
1426                                                         (file-name-directory
1427                                                          Info-current-file))
1428                                       'exact)
1429                                      t)
1430           (set-buffer-modified-p nil)
1431           (setq Info-current-subfile lastfilename)))
1432     (goto-char (point-min))
1433     (search-forward "\n\037")
1434     (+ (- nodepos lastfilepos) (point))))
1435
1436 (defun Info-all-case-regexp (str)
1437   (let ((regexp "")
1438         (len (length str))
1439         (i 0)
1440         c)
1441     (while (< i len)
1442       (setq c (aref str i))
1443       (cond ((or (and (>= c ?A) (<= c ?Z))
1444                  (and (>= c ?a) (<= c ?z)))
1445              (setq regexp (concat regexp
1446                                   "["
1447                                   (char-to-string (downcase c))
1448                                   "\\|"
1449                                   (char-to-string (upcase c))
1450                                   "]")))
1451             (t
1452              (setq regexp (concat regexp (char-to-string c)))))
1453       (setq i (1+ i)))
1454     regexp))
1455
1456 (defun Info-suffixed-file (name &optional exact)
1457   "Look for an info file named NAME. This function tries to be smart in
1458 finding the file corresponding to NAME: if it doesn't exist, several
1459 variants are looked for, notably by appending suffixes from
1460 `Info-suffix-list' and by trying to change the characters case in NAME.
1461
1462 The optional argument EXACT prevents this function from trying different case
1463 versions of NAME. Only the suffixes are tried."
1464   (catch 'found
1465     ;; First, try NAME alone:
1466     (and (file-regular-p name) (throw 'found name))
1467     ;; Then, try different variants
1468     (let ((suff-match (concat "\\("
1469                               (let ((suff-list Info-suffix-list)
1470                                     suff regexp)
1471                                 (while (setq suff (pop suff-list))
1472                                   (setq regexp
1473                                         (concat regexp
1474                                                 (regexp-quote (car suff))
1475                                                 (and suff-list "\\|"))))
1476                                 regexp)
1477                               "\\)?$"))
1478           (dir (file-name-directory name))
1479           file files)
1480       (setq name (file-name-nondirectory name))
1481       (setq files
1482             (condition-case data ;; protect against invalid directory
1483                 ;; First, try NAME[.<suffix>]
1484                 (append
1485                  (directory-files dir 'fullname
1486                                   (concat "^" (regexp-quote name) suff-match)
1487                                   nil t)
1488                  (if exact
1489                      nil
1490                    ;; Then, try to match the name independantly of the
1491                    ;; characters case.
1492                    (directory-files dir 'fullname
1493                                     (Info-all-case-regexp
1494                                      (concat "^"
1495                                              (regexp-quote name)
1496                                              suff-match))
1497                                     nil t)))
1498               (t
1499                (display-warning 'info
1500                  (format "directory `%s' error: %s" dir data))
1501                nil)))
1502       (while (setq file (pop files))
1503         (and (file-regular-p file)
1504              (throw 'found file)))
1505       )))
1506
1507 (defun Info-insert-file-contents (file &optional visit)
1508   (setq file (expand-file-name file default-directory))
1509   (let ((suff Info-suffix-list)
1510         len)
1511     (while (and suff
1512                 (setq len (length (car (car suff))))
1513                 (or (<= (length file) len)
1514                     (not (or
1515                           (equal (substring file (- len))
1516                                  (car (car suff)))
1517                           (equal (substring file (- len))
1518                                  (upcase (car (car suff)))))
1519                          )))
1520       (setq suff (cdr suff)))
1521     (if (stringp (cdr (car suff)))
1522         (let ((command (if (string-match "%s" (cdr (car suff)))
1523                            (format (cdr (car suff)) file)
1524                          (concat (cdr (car suff)) " < " file))))
1525           (message "%s..." command)
1526           (call-process shell-file-name nil t nil shell-command-switch command)
1527           (message "")
1528           (when visit
1529             (setq buffer-file-name file
1530                   buffer-file-truename (file-truename buffer-file-name))
1531             (set-buffer-modified-p nil)
1532             (clear-visited-file-modtime)))
1533       (insert-file-contents file visit)
1534       ;; Often info files, especially those from FSF-land set the
1535       ;; coding system via a "Last page Local variables" at the end
1536       ;; of the file.  #'insert-file-contents doesn't (rightly so)
1537       ;; process local vars.  But this time we need it, so force the
1538       ;; issue and set the coding system if we find it. --SY.
1539       (hack-local-variables)
1540       (when (and-boundp 'coding
1541               (find-coding-system coding))
1542         (let ((coding-system-for-read coding))
1543           (set-buffer-file-coding-system coding)
1544           (insert-file-contents file visit nil nil t))))))
1545
1546 (defun Info-select-node ()
1547   "Select the node that point is in, after using `g *' to select whole file."
1548   (interactive)
1549   (widen)
1550   (save-excursion
1551    ;; Find beginning of node.
1552    (search-backward "\n\037")
1553    (forward-line 2)
1554    ;; Get nodename spelled as it is in the node.
1555    (re-search-forward "Node:[ \t]*")
1556    (setq Info-current-node
1557          (buffer-substring (point)
1558                            (progn
1559                             (skip-chars-forward "^,\t\n")
1560                             (point))))
1561    (Info-set-mode-line)
1562    ;; Find the end of it, and narrow.
1563    (beginning-of-line)
1564    (let (active-expression)
1565      (narrow-to-region (point)
1566                        (if (re-search-forward "\n[\037\f]" nil t)
1567                            (prog1
1568                             (1- (point))
1569                             (if (looking-at "[\n\037\f]*execute: ")
1570                                 (progn
1571                                   (goto-char (match-end 0))
1572                                   (setq active-expression
1573                                         (read (current-buffer))))))
1574                          (point-max)))
1575      (or (equal Info-footnote-tag "Note")
1576          (progn
1577            (goto-char (point-min))
1578            (let ((buffer-read-only nil)
1579                  (bufmod (buffer-modified-p))
1580                  (case-fold-search t))
1581              (while (re-search-forward "\\*[Nn]ote\\([ \n]\\)" nil t)
1582                (replace-match (concat "*" Info-footnote-tag "\ ")))
1583              (set-buffer-modified-p bufmod))))
1584      (Info-reannotate-node)
1585      ;; XEmacs: remove v19 test
1586      (and Info-fontify
1587           (Info-fontify-node))
1588      (run-hooks 'Info-select-hook)
1589      (if Info-enable-active-nodes (eval active-expression)))))
1590
1591 (defun Info-set-mode-line ()
1592   (setq modeline-buffer-identification
1593         (list (cons modeline-buffer-id-left-extent "Info: ")
1594               (cons modeline-buffer-id-right-extent
1595                     (concat
1596                      "("
1597                      (if Info-current-file
1598                          (let ((name (file-name-nondirectory
1599                                       Info-current-file)))
1600                            (if (string-match #r"^\([^.]*\)\..*$" name)
1601                                (match-string 1 name)
1602                              name))
1603                        "")
1604                      ")"
1605                      (or Info-current-node ""))))))
1606 \f
1607 ;; Go to an info node specified with a filename-and-nodename string
1608 ;; of the sort that is found in pointers in nodes.
1609
1610 ;;;###autoload
1611 (defun Info-goto-node (nodename &optional no-going-back tryfile)
1612   "Go to info node named NAME.  Give just NODENAME or (FILENAME)NODENAME.
1613 Actually, the following interpretations of NAME are tried in order:
1614     (FILENAME)NODENAME
1615     (FILENAME)     (using Top node)
1616     NODENAME       (in current file)
1617     TAGNAME        (see below)
1618     FILENAME       (using Top node)
1619 where TAGNAME is a string that appears in quotes: \"TAGNAME\", in an
1620 annotation for any node of any file.  (See `a' and `x' commands.)"
1621   (interactive (list (Info-read-node-name "Goto node, file or tag: ")
1622                      nil t))
1623   (let (filename)
1624     (string-match (concat #r"\s *\((\s *\("
1625                           "[^\t)]*"
1626                           #r"\)\s *)\s *\|\)\(.*\)")
1627                   nodename)
1628     (setq filename (if (= (match-beginning 1) (match-end 1))
1629                        ""
1630                      (substring nodename (match-beginning 2) (match-end 2)))
1631           nodename (substring nodename (match-beginning 3) (match-end 3)))
1632     (let ((trim (string-match #r"\s *\'" filename)))
1633       (if trim (setq filename (substring filename 0 trim))))
1634     (let ((trim (string-match #r"\s *\'" nodename)))
1635       (if trim (setq nodename (substring nodename 0 trim))))
1636     (Info-find-node (if (equal filename "") nil filename)
1637                     (if (equal nodename "") "Top" nodename)
1638                     no-going-back (and tryfile (equal filename "")))))
1639
1640 (defun Info-goto-bookmark ()
1641   (interactive)
1642   (let ((completion-ignore-case nil)
1643         (tag (completing-read "Goto tag: "
1644                               (Info-build-annotation-completions)
1645                               nil t nil
1646                               'Info-minibuffer-history)))
1647     (or (equal tag "") (Info-find-node nil (format "<<%s>>" tag)))))
1648
1649 ;;;###autoload
1650 (defun Info-visit-file (file)
1651   "Directly visit an info file."
1652   (interactive "fVisit Info file: ")
1653   (Info-find-node (expand-file-name file) "Top"))
1654
1655 (defun Info-restore-point (&optional always)
1656   "Restore point to same location it had last time we were in this node."
1657   (interactive "p")
1658   (if (or Info-restoring-point always)
1659       (let* ((name (format "(%s)%s"
1660                            (Info-file-name-only Info-current-file)
1661                            Info-current-node))
1662              (p (assoc name Info-history)))
1663         (if p (Info-restore-history-entry p)))))
1664
1665 (defun Info-restore-history-entry (entry)
1666   (goto-char (+ (nth 1 entry) (point-min)))
1667   (and (nth 2 entry)
1668        (get-buffer-window (current-buffer))
1669        (set-window-start (get-buffer-window (current-buffer))
1670                          (+ (nth 2 entry) (point-min)))))
1671
1672 (defvar Info-read-node-completion-table)
1673
1674 ;; This function is used as the "completion table" while reading a node name.
1675 ;; It does completion using the alist in Info-read-node-completion-table
1676 ;; unless STRING starts with an open-paren.
1677 (defun Info-read-node-name-1 (string predicate code)
1678   (let ((no-completion (and (> (length string) 0) (eq (aref string 0) ?\())))
1679     (cond ((eq code nil)
1680            (if no-completion
1681                string
1682              (try-completion string Info-read-node-completion-table
1683                              predicate)))
1684           ((eq code t)
1685            (if no-completion
1686                nil
1687              (all-completions string Info-read-node-completion-table
1688                               predicate)))
1689           ((eq code 'lambda)
1690            (if no-completion
1691                t
1692              (assoc string Info-read-node-completion-table))))))
1693
1694 (defun Info-read-node-name (prompt &optional default)
1695   (Info-setup-initial)
1696   (let* ((completion-ignore-case t)
1697          (Info-read-node-completion-table (Info-build-node-completions))
1698          (nodename (completing-read prompt 'Info-read-node-name-1
1699                                     nil t nil 'Info-minibuffer-history
1700                                     default)))
1701     (if (equal nodename "")
1702         (or default
1703             (Info-read-node-name prompt))
1704       nodename)))
1705
1706 (defun Info-build-annotation-completions ()
1707   (or Info-current-annotation-completions
1708       (save-excursion
1709         (let ((bufs (delq nil (mapcar 'get-file-buffer
1710                                       Info-annotations-path)))
1711               (compl nil))
1712           (while bufs
1713             (set-buffer (car bufs))
1714             (goto-char (point-min))
1715             (while (re-search-forward #r"<<\(.*\)>>" nil t)
1716               (setq compl (cons (list (buffer-substring (match-beginning 1)
1717                                                         (match-end 1)))
1718                                 compl)))
1719             (setq bufs (cdr bufs)))
1720           (setq Info-current-annotation-completions compl)))))
1721
1722 (defun Info-build-node-completions ()
1723   (or Info-current-file-completions
1724       (let ((m Info-tag-table-marker)
1725             (compl (Info-build-annotation-completions)))
1726         (save-excursion
1727           (save-restriction
1728             (widen)
1729             (if (marker-buffer Info-tag-table-marker)
1730                 (progn
1731                   (set-buffer (marker-buffer Info-tag-table-marker))
1732                   (goto-char m)
1733                   (while (re-search-forward "\nNode: \\(.*\\)\177" nil t)
1734                     (setq compl
1735                           (cons (list (buffer-substring (match-beginning 1)
1736                                                         (match-end 1)))
1737                                 compl))))
1738               (goto-char (point-min))
1739               (while (search-forward "\n\037" nil t)
1740                 (forward-line 1)
1741                 (let ((start (point)))
1742                   (forward-line 1)
1743                   (if (re-search-backward "Node: *\\([^,\n]*\\) *[,\n\t]"
1744                                           start t)
1745                       (setq compl
1746                             (cons (list (buffer-substring (match-beginning 1)
1747                                                           (match-end 1)))
1748                                   compl))))))))
1749         (setq Info-current-file-completions compl))))
1750 \f
1751 (defvar Info-last-search nil
1752   "Default regexp for \\<Info-mode-map>\\[Info-search] command to search for.")
1753
1754
1755 ;;;###autoload
1756 (defun Info-search (regexp)
1757   "Search for REGEXP, starting from point, and select node it's found in."
1758   (interactive (list
1759                 (read-from-minibuffer
1760                  (if Info-last-search
1761                      (format "Search (regexp, default %s): "
1762                              Info-last-search)
1763                    "Search (regexp): ")
1764                  nil nil nil nil nil Info-last-search)))
1765   (setq Info-last-search regexp)
1766   (with-search-caps-disable-folding regexp t
1767     (let ((found ())
1768           (onode Info-current-node)
1769           (ofile Info-current-file)
1770           (opoint (point))
1771           (osubfile Info-current-subfile))
1772       (save-excursion
1773         (save-restriction
1774           (widen)
1775           (if (null Info-current-subfile)
1776               (progn (re-search-forward regexp) (setq found (point)))
1777             (condition-case nil
1778                 (progn (re-search-forward regexp) (setq found (point)))
1779               (search-failed nil)))))
1780       (if (not found)
1781           ;; can only happen in subfile case -- else would have erred
1782           (unwind-protect
1783               (let ((list ()))
1784                 (save-excursion
1785                   (set-buffer (marker-buffer Info-tag-table-marker))
1786                   (goto-char (point-min))
1787                   (search-forward "\n\037\nIndirect:")
1788                   (save-restriction
1789                     (narrow-to-region (point)
1790                                       (progn (search-forward "\n\037")
1791                                              (1- (point))))
1792                     (goto-char (point-min))
1793                     (search-forward (concat "\n" osubfile ": "))
1794                     (beginning-of-line)
1795                     (while (not (eobp))
1796                       (re-search-forward #r"\(^.*\): [0-9]+$")
1797                       (goto-char (+ (match-end 1) 2))
1798                       (setq list (cons (cons (read (current-buffer))
1799                                              (buffer-substring
1800                                               (match-beginning 1)
1801                                               (match-end 1)))
1802                                        list))
1803                       (goto-char (1+ (match-end 0))))
1804                     (setq list (nreverse list)
1805                           list (cdr list))))
1806                 (while list
1807                   (message "Searching subfile %s..." (cdr (car list)))
1808                   (Info-read-subfile (car (car list)))
1809                   (setq list (cdr list))
1810                   (goto-char (point-min))
1811                   (if (re-search-forward regexp nil t)
1812                       (setq found (point) list ())))
1813                 (if found
1814                     (message "")
1815                   (signal 'search-failed (list regexp))))
1816             (if (not found)
1817                 (progn (Info-read-subfile opoint)
1818                        (goto-char opoint)
1819                        (Info-select-node)))))
1820       (widen)
1821       (goto-char found)
1822       (Info-select-node)
1823       (or (and (equal onode Info-current-node)
1824                (equal ofile Info-current-file))
1825           (Info-history-add ofile onode opoint)))))
1826 \f
1827 ;; Extract the value of the node-pointer named NAME.
1828 ;; If there is none, use ERRORNAME in the error message;
1829 ;; if ERRORNAME is nil, just return nil.
1830 (defun Info-extract-pointer (name &optional errorname)
1831   (save-excursion
1832    (goto-char (point-min))
1833    (forward-line 4)
1834    (let ((case-fold-search t))
1835      (if (re-search-backward (concat name ":") nil t)
1836          (progn
1837            (goto-char (match-end 0))
1838            (Info-following-node-name))
1839        (if (eq errorname t)
1840            nil
1841          (error (concat "Node has no " (capitalize (or errorname name)))))))))
1842
1843 ;; Return the node name in the buffer following point.
1844 ;; ALLOWEDCHARS, if non-nil, goes within [...] to make a regexp
1845 ;; saying which chars may appear in the node name.
1846 (defun Info-following-node-name (&optional allowedchars)
1847   (skip-chars-forward " \t")
1848   (buffer-substring
1849    (point)
1850    (progn
1851      (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]"))
1852        (skip-chars-forward (concat (or allowedchars "^,\t\n") "("))
1853        (if (looking-at "(")
1854            (skip-chars-forward "^)")))
1855      (skip-chars-backward " .")
1856      (point))))
1857
1858 (defun Info-next (&optional n)
1859   "Go to the next node of this node.
1860 A positive or negative prefix argument moves by multiple nodes."
1861   (interactive "p")
1862   (or n (setq n 1))
1863   (if (< n 0)
1864       (Info-prev (- n))
1865     (while (>= (setq n (1- n)) 0)
1866       (Info-goto-node (Info-extract-pointer "next")))))
1867
1868 (defun Info-prev (&optional n)
1869   "Go to the previous node of this node.
1870 A positive or negative prefix argument moves by multiple nodes."
1871   (interactive "p")
1872   (or n (setq n 1))
1873   (if (< n 0)
1874       (Info-next (- n))
1875     (while (>= (setq n (1- n)) 0)
1876       (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous")))))
1877
1878 (defun Info-up (&optional n)
1879   "Go to the superior node of this node.
1880 A positive prefix argument moves up several times."
1881   (interactive "p")
1882   (or n (setq n 1))
1883   (while (>= (setq n (1- n)) 0)
1884     (Info-goto-node (Info-extract-pointer "up")))
1885   (if (interactive-p) (Info-restore-point)))
1886
1887 (defun Info-last (&optional n)
1888   "Go back to the last node visited.
1889 With a prefix argument, go to Nth most recently visited node.  History is
1890 circular; after oldest node, history comes back around to most recent one.
1891 Argument can be negative to go through the circle in the other direction.
1892 \(In other words, `l' is like \"undo\" and `C-u - l' is like \"redo\".)"
1893   (interactive "p")
1894   (or n (setq n 1))
1895   (or Info-history
1896       (error "This is the first Info node you looked at"))
1897   (let ((len (1+ (length Info-history))))
1898     (setq n (% (+ n (* len 100)) len)))
1899   (if (> n 0)
1900       (let ((entry (nth (1- n) Info-history)))
1901         (Info-history-add Info-current-file Info-current-node (point))
1902         (while (>= (setq n (1- n)) 0)
1903           (setq Info-history (nconc (cdr Info-history)
1904                                     (list (car Info-history)))))
1905         (setq Info-history (cdr Info-history))
1906         (let ((Info-keeping-history nil))
1907           (Info-goto-node (car entry)))
1908         (Info-restore-history-entry entry))))
1909
1910 (defun Info-directory ()
1911   "Go to the Info directory node."
1912   (interactive)
1913   (Info-find-node "dir" "top"))
1914 \f
1915 (defun Info-follow-reference (footnotename)
1916   "Follow cross reference named NAME to the node it refers to.
1917 NAME may be an abbreviation of the reference name."
1918   (interactive
1919    (let ((completion-ignore-case t)
1920          completions default (start-point (point)) str i)
1921      (save-excursion
1922        (goto-char (point-min))
1923        (while (re-search-forward (format "\\*%s[ \n\t]*\\([^:]*\\):"
1924                                          Info-footnote-tag)
1925                                  nil t)
1926          (setq str (buffer-substring
1927                     (match-beginning 1)
1928                     (1- (point))))
1929          ;; See if this one should be the default.
1930          (and (null default)
1931               (< (match-beginning 0) start-point)
1932               (<= start-point (point))
1933               (setq default t))
1934          (setq i 0)
1935          (while (setq i (string-match "[ \n\t]+" str i))
1936            (setq str (concat (substring str 0 i) " "
1937                              (substring str (match-end 0))))
1938            (setq i (1+ i)))
1939          ;; Record as a completion and perhaps as default.
1940          (if (eq default t) (setq default str))
1941          (setq completions
1942                (cons (cons str nil)
1943                      completions))))
1944      (if completions
1945          (let ((item (completing-read (if default
1946                                           (concat "Follow reference named: ("
1947                                                   default ") ")
1948                                         "Follow reference named: ")
1949                                       completions nil t nil
1950                                       'Info-minibuffer-history
1951                                       default)))
1952            (if (and (string= item "") default)
1953                (list default)
1954              (list item)))
1955        (error "No cross-references in this node"))))
1956   (let (target i (str (concat "\\*" Info-footnote-tag " "
1957                               (regexp-quote footnotename))))
1958     (while (setq i (string-match " " str i))
1959       (setq str (concat (substring str 0 i) "\\([ \t\n]+\\)"
1960                         (substring str (1+ i))))
1961       (setq i (+ i 10)))
1962     (save-excursion
1963       (goto-char (point-min))
1964       (or (re-search-forward str nil t)
1965           (error "No cross-reference named %s" footnotename))
1966       (goto-char (match-end 1))
1967       (setq target
1968             (Info-extract-menu-node-name "Bad format cross reference" t)))
1969     (while (setq i (string-match "[ \t\n]+" target i))
1970       (setq target (concat (substring target 0 i) " "
1971                            (substring target (match-end 0))))
1972       (setq i (+ i 1)))
1973     (Info-goto-node target)
1974     (setq Info-in-cross-reference t)))
1975
1976 (defun Info-next-reference (n)
1977   (interactive "p")
1978   (let ((pat (format (concat "\\*%s[ \n\t]*"
1979                              #r"\([^:]*\):\|^\* .*:\|<<.*>>")
1980                      Info-footnote-tag))
1981         (old-pt (point))
1982         wrapped found-nomenu)
1983     (while (< n 0)
1984       (unless (re-search-backward pat nil t)
1985         ;; Don't wrap more than once in a buffer where only the
1986         ;; menu references are found.
1987         (when (and wrapped (not found-nomenu))
1988           (goto-char old-pt)
1989           (error "No cross references in this node"))
1990         (setq wrapped t)
1991         (goto-char (point-max))
1992         (unless (re-search-backward pat nil t)
1993           (goto-char old-pt)
1994           (error "No cross references in this node")))
1995       (unless (save-excursion
1996                 (goto-char (match-beginning 0))
1997                 (when (looking-at "\\* Menu:")
1998                   (decf n)))
1999         (setq found-nomenu t))
2000       (incf n))
2001     (while (> n 0)
2002       (or (eobp) (forward-char 1))
2003       (unless (re-search-forward pat nil t)
2004         (when (and wrapped (not found-nomenu))
2005           (goto-char old-pt)
2006           (error "No cross references in this node"))
2007         (setq wrapped t)
2008         (goto-char (point-min))
2009         (unless (re-search-forward pat nil t)
2010           (goto-char old-pt)
2011           (error "No cross references in this node")))
2012       (unless (save-excursion
2013                 (goto-char (match-beginning 0))
2014                 (when (looking-at "\\* Menu:")
2015                   (incf n)))
2016         (setq found-nomenu t))
2017       (decf n))
2018     (when (looking-at "\\* Menu:")
2019       (error "No cross references in this node"))
2020     (goto-char (match-beginning 0))))
2021
2022 (defun Info-prev-reference (n)
2023   (interactive "p")
2024   (Info-next-reference (- n)))
2025
2026 (defun Info-extract-menu-node-name (&optional errmessage multi-line)
2027   (skip-chars-forward " \t\n")
2028   (let ((start (point))
2029         str i)
2030     (skip-chars-forward "^:")
2031     (forward-char 1)
2032     (setq str
2033           (if (looking-at ":")
2034               (buffer-substring start (1- (point)))
2035             (skip-chars-forward " \t\n")
2036             ;; Kludge.
2037             ;; Allow dots in node name not followed by whitespace.
2038             (re-search-forward
2039              (concat "\\(([^)]+)[^.,"
2040                      (if multi-line "" "\n")
2041                      "]*\\|\\([^.,\t"
2042                      (if multi-line "" "\n")
2043                      ;; We consider dots followed by newline as
2044                      ;; end of nodename even if multil-line.
2045                      ;; Also stops at .).  It is generated by @pxref.
2046                      ;; Skips sequential dots.
2047                      "]\\|\\.+[^ \t\n)]\\)+\\)"))
2048             (match-string 1)))
2049     (while (setq i (string-match "\n" str i))
2050       (aset str i ?\ ))
2051     str))
2052
2053 (defun Info-menu (menu-item)
2054   "Go to node for menu item named (or abbreviated) NAME.
2055 Completion is allowed, and the menu item point is on is the default."
2056   (interactive
2057    (let ((completions '())
2058          ;; If point is within a menu item, use that item as the default
2059          (default nil)
2060          (p (point))
2061          (last nil))
2062      (save-excursion
2063        (goto-char (point-min))
2064        (let ((case-fold-search t))
2065          (if (not (search-forward "\n* menu:" nil t))
2066              (error "No menu in this node")))
2067        (while (re-search-forward
2068                 "\n\\* \\([^:\t\n]*\\):" nil t)
2069          (if (and (null default)
2070                   (prog1 (if last (< last p) nil)
2071                     (setq last (match-beginning 0)))
2072                   (<= p last))
2073              (setq default (car (car completions))))
2074          (setq completions (cons (cons (buffer-substring
2075                                          (match-beginning 1)
2076                                          (match-end 1))
2077                                        (match-beginning 1))
2078                                  completions)))
2079        (if (and (null default) last
2080                 (< last p)
2081                 (<= p (progn (end-of-line) (point))))
2082            (setq default (car (car completions)))))
2083      (let ((item nil))
2084        (while (null item)
2085          (setq item (let ((completion-ignore-case t))
2086                       (completing-read (if default
2087                                            (format "Menu item (default %s): "
2088                                                    default)
2089                                            "Menu item: ")
2090                                        completions nil t nil
2091                                        'Info-minibuffer-history
2092                                        default)))
2093          ;; we rely on the fact that completing-read accepts an input
2094          ;; of "" even when the require-match argument is true and ""
2095          ;; is not a valid possibility
2096          (if (string= item "")
2097              (if default
2098                  (setq item default)
2099                  ;; ask again
2100                  (setq item nil))))
2101        (list item))))
2102   ;; there is a problem here in that if several menu items have the same
2103   ;; name you can only go to the node of the first with this command.
2104   (Info-goto-node (Info-extract-menu-item menu-item) nil t))
2105
2106 (defun Info-extract-menu-item (menu-item &optional noerror)
2107   (save-excursion
2108     (goto-char (point-min))
2109     (if (let ((case-fold-search t))
2110           (search-forward "\n* menu:" nil t))
2111         (if (or (search-forward (concat "\n* " menu-item ":") nil t)
2112                 (search-forward (concat "\n* " menu-item) nil t))
2113             (progn
2114               (beginning-of-line)
2115               (forward-char 2)
2116               (Info-extract-menu-node-name))
2117           (and (not noerror) (error "No such item in menu")))
2118       (and (not noerror) (error "No menu in this node")))))
2119
2120 ;; If COUNT is nil, use the last item in the menu.
2121 (defun Info-extract-menu-counting (count &optional noerror noindex)
2122   (save-excursion
2123     (goto-char (point-min))
2124     (if (let ((case-fold-search t))
2125           (and (search-forward "\n* menu:" nil t)
2126                (or (not noindex)
2127                    (not (string-match "\\<Index\\>" Info-current-node)))))
2128         (if (search-forward "\n* " nil t count)
2129             (progn
2130               (or count
2131                   (while (search-forward "\n* " nil t)))
2132               (Info-extract-menu-node-name))
2133           (and (not noerror) (error "Too few items in menu")))
2134       (and (not noerror) (error "No menu in this node")))))
2135
2136 (defun Info-nth-menu-item (n)
2137   "Go to the node of the Nth menu item."
2138   (interactive "P")
2139   (or n (setq n (- last-command-char ?0)))
2140   (if (< n 1) (error "Index must be at least 1"))
2141   (Info-goto-node (Info-extract-menu-counting n) nil t))
2142
2143 (defun Info-last-menu-item ()
2144   "Go to the node of the tenth menu item."
2145   (interactive)
2146   (Info-goto-node (Info-extract-menu-counting nil) nil t))
2147 \f
2148 (defun Info-top ()
2149   "Go to the Top node of this file."
2150   (interactive)
2151   (Info-goto-node "Top"))
2152
2153 (defun Info-end ()
2154   "Go to the final node in this file."
2155   (interactive)
2156   (Info-top)
2157   (let ((Info-keeping-history nil)
2158         node)
2159     (Info-last-menu-item)
2160     (while (setq node (or (Info-extract-pointer "next" t)
2161                           (Info-extract-menu-counting nil t t)))
2162       (Info-goto-node node))
2163     (or (equal (Info-extract-pointer "up" t) "Top")
2164         (let ((executing-kbd-macro ""))   ; suppress messages
2165           (condition-case nil
2166               (Info-global-next 10000)
2167             (error nil))))))
2168
2169 (defun Info-global-next (&optional n)
2170   "Go to the next node in this file, traversing node structure as necessary.
2171 This works only if the Info file is structured as a hierarchy of nodes.
2172 A positive or negative prefix argument moves by multiple nodes."
2173   (interactive "p")
2174   (or n (setq n 1))
2175   (if (< n 0)
2176       (Info-global-prev (- n))
2177     (while (>= (setq n (1- n)) 0)
2178       (let (node)
2179         (cond ((and (string-match "^Top$" Info-current-node)
2180                     (setq node (Info-extract-pointer "next" t))
2181                     (Info-extract-menu-item node t))
2182                (Info-goto-node node))
2183               ((setq node (Info-extract-menu-counting 1 t t))
2184                (message "Going down...")
2185                (Info-goto-node node))
2186               (t
2187                (let ((Info-keeping-history Info-keeping-history)
2188                      (orignode Info-current-node)
2189                      (ups ""))
2190                  (while (not (Info-extract-pointer "next" t))
2191                    (if (and (setq node (Info-extract-pointer "up" t))
2192                             (not (equal node "Top")))
2193                        (progn
2194                          (message "Going%s..." (setq ups (concat ups " up")))
2195                          (Info-goto-node node)
2196                          (setq Info-keeping-history nil))
2197                      (if orignode
2198                          (let ((Info-keeping-history nil))
2199                            (Info-goto-node orignode)))
2200                      (error "Last node in file")))
2201                  (Info-next))))))))
2202
2203 (defun Info-page-next (&optional n)
2204   "Scroll forward one screenful, or go to next global node.
2205 A positive or negative prefix argument moves by multiple screenfuls."
2206   (interactive "p")
2207   (or n (setq n 1))
2208   (if (< n 0)
2209       (Info-page-prev (- n))
2210     (while (>= (setq n (1- n)) 0)
2211       (if (pos-visible-in-window-p (point-max))
2212           (progn
2213             (Info-global-next)
2214             (message "Node: %s" Info-current-node))
2215         (scroll-up)))))
2216
2217 (defun Info-scroll-next (arg)
2218   (interactive "P")
2219   (if Info-auto-advance
2220       (if (and (pos-visible-in-window-p (point-max))
2221                (not (eq Info-auto-advance t))
2222                (not (eq last-command this-command)))
2223           (message "Hit %s again to go to next node"
2224                    (if (= last-command-char 0)
2225                        "mouse button"
2226                      (key-description (char-to-string last-command-char))))
2227         (Info-page-next)
2228         (setq this-command 'Info))
2229     (scroll-up arg)))
2230
2231 (defun Info-global-prev (&optional n)
2232   "Go to the previous node in this file, traversing structure as necessary.
2233 This works only if the Info file is structured as a hierarchy of nodes.
2234 A positive or negative prefix argument moves by multiple nodes."
2235   (interactive "p")
2236   (or n (setq n 1))
2237   (if (< n 0)
2238       (Info-global-next (- n))
2239     (while (>= (setq n (1- n)) 0)
2240       (let ((upnode (Info-extract-pointer "up" t))
2241             (prevnode (Info-extract-pointer "prev[ious]*" t)))
2242         (if (or (not prevnode)
2243                 (equal prevnode upnode))
2244             (if (string-match "^Top$" Info-current-node)
2245                 (error "First node in file")
2246               (message "Going up...")
2247               (Info-up))
2248           (Info-goto-node prevnode)
2249           (let ((downs "")
2250                 (Info-keeping-history nil)
2251                 node)
2252             (while (setq node (Info-extract-menu-counting nil t t))
2253               (message "Going%s..." (setq downs (concat downs " down")))
2254               (Info-goto-node node))))))))
2255
2256 (defun Info-page-prev (&optional n)
2257   "Scroll backward one screenful, or go to previous global node.
2258 A positive or negative prefix argument moves by multiple screenfuls."
2259   (interactive "p")
2260   (or n (setq n 1))
2261   (if (< n 0)
2262       (Info-page-next (- n))
2263     (while (>= (setq n (1- n)) 0)
2264       (if (pos-visible-in-window-p (point-min))
2265           (progn
2266             (Info-global-prev)
2267             (message "Node: %s" Info-current-node)
2268             (goto-char (point-max))
2269             (recenter -1)
2270             (move-to-window-line 0))
2271         (scroll-down)))))
2272
2273 (defun Info-scroll-prev (arg)
2274   (interactive "P")
2275   (if Info-auto-advance
2276       (if (and (pos-visible-in-window-p (point-min))
2277                (not (eq Info-auto-advance t))
2278                (not (eq last-command this-command)))
2279           (message "Hit %s again to go to previous node"
2280                    (if (mouse-event-p last-command-event)
2281                        "mouse button"
2282                      (key-description (event-key last-command-event))))
2283         (Info-page-prev)
2284         (setq this-command 'Info))
2285     (scroll-down arg)))
2286 \f
2287 (defun Info-index (topic)
2288   "Look up a string in the index for this file.
2289 The index is defined as the first node in the top-level menu whose
2290 name contains the word \"Index\", plus any immediately following
2291 nodes whose names also contain the word \"Index\".
2292 If there are no exact matches to the specified topic, this chooses
2293 the first match which is a case-insensitive substring of a topic.
2294 Use the `,' command to see the other matches.
2295 Give a blank topic name to go to the Index node itself."
2296   (interactive "sIndex topic: ")
2297   (let ((pattern (format "\n\\* \\([^\n:]*%s[^\n:]*\\):[ \t]*%s"
2298                          (regexp-quote topic)
2299                          "\\(.*\\)\\.[ \t]*\\([0-9]*\\)$"))
2300         node)
2301     (message "Searching index for `%s'..." topic)
2302     (Info-goto-node "Top")
2303     (let ((case-fold-search t))
2304       (or (search-forward "\n* menu:" nil t)
2305           (error "No index"))
2306       (or (re-search-forward "\n\\* \\(.*\\<Index\\>\\)" nil t)
2307           (error "No index")))
2308     (goto-char (match-beginning 1))
2309     (let ((Info-keeping-history nil)
2310           (Info-fontify (and Info-fontify (equal topic ""))))
2311       (Info-goto-node (Info-extract-menu-node-name)))
2312     (or (equal topic "")
2313         (let ((matches nil)
2314               (exact nil)
2315               (Info-keeping-history nil)
2316               found)
2317           (while
2318               (progn
2319                 (goto-char (point-min))
2320                 (while (re-search-forward pattern nil t)
2321                   (setq matches
2322                         (cons (list (buffer-substring (match-beginning 1)
2323                                                       (match-end 1))
2324                                     (buffer-substring (match-beginning 2)
2325                                                       (match-end 2))
2326                                     Info-current-node
2327                                     (string-to-int (concat "0"
2328                                                            (buffer-substring
2329                                                             (match-beginning 3)
2330                                                             (match-end 3)))))
2331                               matches)))
2332                 (and (setq node (Info-extract-pointer "next" t))
2333                      (string-match "\\<Index\\>" node)))
2334             (let ((Info-fontify nil))
2335               (Info-goto-node node)))
2336           (or matches
2337               (progn
2338                 (Info-last)
2339                 (error "No \"%s\" in index" topic)))
2340           ;; Here it is a feature that assoc is case-sensitive.
2341           (while (setq found (assoc topic matches))
2342             (setq exact (cons found exact)
2343                   matches (delq found matches)))
2344           (setq Info-index-alternatives (nconc exact (nreverse matches))
2345                 Info-index-first-alternative (car Info-index-alternatives))
2346           (Info-index-next 0)))))
2347
2348 (defun Info-index-next (num)
2349   "Go to the next matching index item from the last `i' command."
2350   (interactive "p")
2351   (or Info-index-alternatives
2352       (error "No previous `i' command in this file"))
2353   (while (< num 0)
2354     (setq num (+ num (length Info-index-alternatives))))
2355   (while (> num 0)
2356     (setq Info-index-alternatives
2357           (nconc (cdr Info-index-alternatives)
2358                  (list (car Info-index-alternatives)))
2359           num (1- num)))
2360   (Info-goto-node (nth 1 (car Info-index-alternatives)))
2361   (if (> (nth 3 (car Info-index-alternatives)) 0)
2362       (forward-line (nth 3 (car Info-index-alternatives)))
2363     (forward-line 3)  ; don't search in headers
2364     (let ((name (car (car Info-index-alternatives))))
2365       (if (or (re-search-forward (format
2366                                   #r"\(Function\|Command\): %s\( \|$\)"
2367                                   (regexp-quote name)) nil t)
2368               (re-search-forward (format "^`%s[ ']" (regexp-quote name)) nil t)
2369               (search-forward (format "`%s'" name) nil t)
2370               (and (string-match #r"\`.*\( (.*)\)\'" name)
2371                    (search-forward
2372                     (format "`%s'" (substring name 0 (match-beginning 1)))
2373                     nil t))
2374               (search-forward name nil t))
2375           (beginning-of-line)
2376         (goto-char (point-min)))))
2377   (message "Found \"%s\" in %s.  %s"
2378            (car (car Info-index-alternatives))
2379            (nth 2 (car Info-index-alternatives))
2380            (if (cdr Info-index-alternatives)
2381                (if (eq (car (cdr Info-index-alternatives))
2382                        Info-index-first-alternative)
2383                    "(Press `,' to repeat)"
2384                  (format "(Press `,' for %d more)"
2385                          (- (1- (length Info-index-alternatives))
2386                             (length (memq Info-index-first-alternative
2387                                           (cdr Info-index-alternatives))))))
2388              "(Only match)")))
2389
2390
2391 ;;;###autoload
2392 (defun Info-emacs-command (command)
2393   "Look up an Emacs command in the Emacs manual in the Info system.
2394 This command is designed to be used whether you are already in Info or not."
2395   (interactive "CLook up command in Emacs manual: ")
2396   (save-window-excursion
2397     (info)
2398     (Info-find-node Info-emacs-info-file-name "Top")
2399     (Info-index (symbol-name command)))
2400   (pop-to-buffer "*info*"))
2401
2402
2403 ;;;###autoload
2404 (defun Info-goto-emacs-command-node (key)
2405   "Look up an Emacs command in the Emacs manual in the Info system.
2406 This command is designed to be used whether you are already in Info or not."
2407   (interactive "CLook up command in Emacs manual: ")
2408   (Info-emacs-command key))
2409
2410 ;;;###autoload
2411 (defun Info-goto-emacs-key-command-node (key)
2412   "Look up an Emacs key sequence in the Emacs manual in the Info system.
2413 This command is designed to be used whether you are already in Info or not."
2414   (interactive "kLook up key in Emacs manual: ")
2415   (let ((command (key-binding key)))
2416     (cond ((eq command 'keyboard-quit)
2417            (keyboard-quit))
2418           ((null command)
2419            (error "%s is undefined" (key-description key)))
2420           ((and (interactive-p) (eq command 'execute-extended-command))
2421            (call-interactively 'Info-goto-emacs-command-node))
2422           (t
2423            (Info-goto-emacs-command-node command)))))
2424
2425 ;;;###autoload
2426 (defun Info-emacs-key (key)
2427   "Look up an Emacs key sequence in the Emacs manual in the Info system.
2428 This command is designed to be used whether you are already in Info or not."
2429   (interactive "kLook up key in Emacs manual: ")
2430   (cond ((eq (key-binding key) 'keyboard-quit)
2431          (keyboard-quit))
2432         ((and (interactive-p) (eq (key-binding key) 'execute-extended-command))
2433          (call-interactively 'Info-goto-emacs-command-node))
2434         (t
2435          (save-window-excursion
2436            (info)
2437            (Info-find-node Info-emacs-info-file-name "Top")
2438            (setq key (key-description key))
2439            (let (p)
2440              (if (setq p (string-match "[@{}]" key))
2441                  (setq key (concat (substring key 0 p) "@" (substring key p))))
2442              (if (string-match "^ESC " key)
2443                  (setq key (concat "M-" (substring key 4))))
2444              (if (string-match "^M-C-" key)
2445                  (setq key (concat "C-M-" (substring key 4)))))
2446            (Info-index key))
2447          (pop-to-buffer "*info*"))))
2448
2449 ;;;###autoload
2450 (defun Info-elisp-ref (func)
2451   "Look up an Emacs Lisp function in the Elisp manual in the Info system.
2452 This command is designed to be used whether you are already in Info or not."
2453   (interactive (let ((fn (function-at-point))
2454                      (enable-recursive-minibuffers t)
2455                      val)
2456                  (setq val (completing-read
2457                             (format "Look up Emacs Lisp function%s: "
2458                                     (if fn
2459                                         (format " (default %s)" fn)
2460                                       ""))
2461                             obarray 'fboundp t
2462                             nil nil (and fn (symbol-name fn))))
2463                  (list (if (equal val "")
2464                            fn (intern val)))))
2465   (save-window-excursion
2466     (info)
2467     (condition-case nil
2468         (Info-find-node "lispref" "Top")
2469       (error (Info-find-node "elisp" "Top")))
2470     (Info-index (symbol-name func)))
2471   (pop-to-buffer "*info*"))
2472 \f
2473 (defun Info-reannotate-node ()
2474   (let ((bufs (delq nil (mapcar 'get-file-buffer Info-annotations-path))))
2475     (if bufs
2476         (let ((ibuf (current-buffer))
2477               (file (concat "\\(" (regexp-quote
2478                              (file-name-nondirectory Info-current-file))
2479                             "\\|" (regexp-quote Info-current-file) "\\)"))
2480               (node (regexp-quote Info-current-node))
2481               (savept (point)))
2482           (goto-char (point-min))
2483           (if (search-forward "\n------ NOTE:\n" nil t)
2484               (let ((buffer-read-only nil)
2485                     (bufmod (buffer-modified-p))
2486                     top)
2487                 (setq savept (copy-marker savept))
2488                 (goto-char (point-min))
2489                 (while (search-forward "\n------ NOTE:" nil t)
2490                   (setq top (1+ (match-beginning 0)))
2491                   (if (search-forward "\n------\n" nil t)
2492                       (delete-region top (point)))
2493                   (backward-char 1))
2494                 (set-buffer-modified-p bufmod)))
2495           (save-excursion
2496             (while bufs
2497               (set-buffer (car bufs))
2498               (goto-char (point-min))
2499               (while (re-search-forward
2500                       (format
2501                        "------ *File: *%s *Node: *%s *Line: *\\([0-9]+\\) *\n"
2502                        file node)
2503                       nil t)
2504                 (let ((line (string-to-int
2505                              (buffer-substring (match-beginning 2)
2506                                                (match-end 2))))
2507                       (top (point))
2508                       bot)
2509                   (search-forward "\n------\n" nil t)
2510                   (setq bot (point))
2511                   (save-excursion
2512                     (set-buffer ibuf)
2513                     (if (integerp savept) (setq savept (copy-marker savept)))
2514                     (if (= line 0)
2515                         (goto-char (point-max))
2516                       (goto-char (point-min))
2517                       (forward-line line))
2518                     (let ((buffer-read-only nil)
2519                           (bufmod (buffer-modified-p)))
2520                       (insert "------ NOTE:\n")
2521                       (insert-buffer-substring (car bufs) top bot)
2522                       (set-buffer-modified-p bufmod)))))
2523               (setq bufs (cdr bufs))))
2524           (goto-char savept)))))
2525
2526 (defvar Info-annotate-map nil
2527   "Local keymap used within `a' command of Info.")
2528
2529 (if Info-annotate-map
2530     nil
2531   ;; (setq Info-annotate-map (nconc (make-sparse-keymap) text-mode-map))
2532   (setq Info-annotate-map (copy-keymap text-mode-map))
2533   (define-key Info-annotate-map "\C-c\C-c" 'Info-cease-annotate))
2534
2535 (defun Info-annotate-mode ()
2536   "Major mode for adding an annotation to an Info node.
2537 Like text mode with the addition of Info-cease-annotate
2538 which returns to Info mode for browsing.
2539 \\{Info-annotate-map}")
2540
2541 (defun Info-annotate (arg)
2542   "Add a personal annotation to the current Info node.
2543  Only you will be able to see this annotation.  Annotations are stored
2544 in the file \"info.notes\" in your `user-init-directory' by default.  If
2545 point is inside an existing annotation, edit that annotation.  A prefix
2546 argument specifies which annotations file (from `Info-annotations-path')
2547 is to be edited; default is 1."
2548   (interactive "p")
2549   (setq arg (1- arg))
2550   (if (or (< arg 0) (not (nth arg Info-annotations-path)))
2551       (if (= arg 0)
2552           (setq Info-annotations-path
2553                 (list (read-file-name
2554                        "Annotations file: " "~/" "~/.infonotes")))
2555         (error "File number must be in the range from 1 to %d"
2556                (length Info-annotations-path))))
2557   (let ((which nil)
2558         (file (file-name-nondirectory Info-current-file))
2559         (d Info-directory-list)
2560         where pt)
2561     (while (and d (not (equal (expand-file-name file (car d))
2562                               Info-current-file)))
2563       (setq d (cdr d)))
2564     (or d (setq file Info-current-file))
2565     (if (and (save-excursion
2566                (goto-char (min (point-max) (+ (point) 13)))
2567                (and (search-backward "------ NOTE:\n" nil t)
2568                     (setq pt (match-end 0))
2569                     (search-forward "\n------\n" nil t)))
2570              (< (point) (match-end 0)))
2571         (setq which (format "File: *%s *Node: *%s *Line:.*\n%s"
2572                             (regexp-quote file)
2573                             (regexp-quote Info-current-node)
2574                             (regexp-quote
2575                              (buffer-substring pt (match-beginning 0))))
2576               where (max (- (point) pt) 0)))
2577     (let ((node Info-current-node)
2578           (line (if (looking-at "[ \n]*\\'") 0
2579                   (count-lines (point-min) (point)))))
2580       (or which
2581           (let ((buffer-read-only nil)
2582                 (bufmod (buffer-modified-p)))
2583             (beginning-of-line)
2584             (if (bobp) (goto-char (point-max)))
2585             (insert "------ NOTE:\n------\n")
2586             (backward-char 20)
2587             (set-buffer-modified-p bufmod)))
2588       ;; (setq Info-window-start (window-start))
2589       (setq Info-window-configuration (current-window-configuration))
2590       (pop-to-buffer (find-file-noselect (nth arg Info-annotations-path)))
2591       (use-local-map Info-annotate-map)
2592       (setq major-mode 'Info-annotate-mode)
2593       (setq mode-name "Info Annotate")
2594       (if which
2595           (if (save-excursion
2596                 (goto-char (point-min))
2597                 (re-search-forward which nil t))
2598               (progn
2599                 (goto-char (match-beginning 0))
2600                 (forward-line 1)
2601                 (forward-char where)))
2602         (let ((bufmod (buffer-modified-p)))
2603           (goto-char (point-max))
2604           (insert (format "\n------ File: %s  Node: %s  Line: %d\n"
2605                           file node line))
2606           (setq pt (point))
2607           (insert "\n------\n"
2608                   "\nPress C-c C-c to save and return to Info.\n")
2609           (goto-char pt)
2610           (set-buffer-modified-p bufmod))))))
2611
2612 (defun Info-cease-annotate ()
2613   (interactive)
2614   (let ((bufmod (buffer-modified-p)))
2615     (while (save-excursion
2616              (goto-char (point-min))
2617              (re-search-forward "\n\n?Press .* to save and return to Info.\n"
2618                                 nil t))
2619       (delete-region (1+ (match-beginning 0)) (match-end 0)))
2620     (while (save-excursion
2621              (goto-char (point-min))
2622              (re-search-forward "\n------ File:.*Node:.*Line:.*\n+------\n"
2623                                 nil t))
2624       (delete-region (match-beginning 0) (match-end 0)))
2625     (set-buffer-modified-p bufmod))
2626   (save-buffer)
2627   (fundamental-mode)
2628   (bury-buffer)
2629   (or (one-window-p) (delete-window))
2630   (info)
2631   (setq Info-current-annotation-completions nil)
2632   (set-window-configuration Info-window-configuration)
2633   (Info-reannotate-node))
2634
2635 (defun Info-bookmark (arg tag)
2636   (interactive "p\nsBookmark name: ")
2637   (Info-annotate arg)
2638   (if (or (string-match "^\"\\(.*\\)\"$" tag)
2639           (string-match #r"^<<\(.*\)>>$" tag))
2640       (setq tag (substring tag (match-beginning 1) (match-end 1))))
2641   (let ((pt (point)))
2642     (search-forward "\n------\n")
2643     (let ((end (- (point) 8)))
2644       (goto-char pt)
2645       (if (re-search-forward "<<[^>\n]*>>" nil t)
2646           (delete-region (match-beginning 0) (match-end 0))
2647         (goto-char end))
2648       (or (equal tag "")
2649           (insert "<<" tag ">>"))))
2650   (Info-cease-annotate))
2651 \f
2652 (defun Info-exit ()
2653   "Exit Info by selecting some other buffer."
2654   (interactive)
2655   (if Info-standalone
2656       (save-buffers-kill-emacs)
2657     (bury-buffer (current-buffer))
2658     (if (and (featurep 'toolbar)
2659              (boundp 'toolbar-info-frame)
2660              (eq toolbar-info-frame (selected-frame)))
2661         (condition-case ()
2662             (delete-frame toolbar-info-frame)
2663           (error (bury-buffer)))
2664       (switch-to-buffer (other-buffer (current-buffer))))))
2665
2666 (defun Info-undefined ()
2667   "Make command be undefined in Info."
2668   (interactive)
2669   (ding))
2670
2671 (defun Info-help ()
2672   "Enter the Info tutorial."
2673   (interactive)
2674   (delete-other-windows)
2675   (Info-find-node "info"
2676                   (if (< (window-height) 23)
2677                       "Help-Small-Screen"
2678                     "Help")))
2679
2680 (defun Info-summary ()
2681   "Display a brief summary of all Info commands."
2682   (interactive)
2683   (save-window-excursion
2684     (switch-to-buffer "*Help*")
2685     (erase-buffer)
2686     (insert (documentation 'Info-mode))
2687     (goto-char (point-min))
2688     (let (flag)
2689       (while (progn (setq flag (not (pos-visible-in-window-p (point-max))))
2690                     (message (if flag "Type Space to see more"
2691                                "Type Space to return to Info"))
2692                     (let ((e (next-command-event)))
2693                       (if (/= ?\  (event-to-character e))
2694                           (progn (setq unread-command-event e) nil)
2695                         flag)))
2696         (scroll-up)))
2697     (message "")
2698     (bury-buffer "*Help*")))
2699 \f
2700 (defun Info-get-token (pos start all &optional errorstring)
2701   "Return the token around POS,
2702 POS must be somewhere inside the token
2703 START is a regular expression which will match the
2704     beginning of the tokens delimited string
2705 ALL is a regular expression with a single
2706     parenthized subpattern which is the token to be
2707     returned. E.g. '{\(.*\)}' would return any string
2708     enclosed in braces around POS.
2709 SIG optional fourth argument, controls action on no match
2710     nil: return nil
2711     t: beep
2712     a string: signal an error, using that string."
2713   (save-excursion
2714     (goto-char (point-min))
2715     (re-search-backward "\\`")  ; Bug fix due to Nicholas J. Foskett.
2716     (goto-char pos)
2717     (re-search-backward start (max (point-min) (- pos 200)) 'yes)
2718     (let (found)
2719       (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes)
2720                   (not (setq found (and (<= (match-beginning 0) pos)
2721                                         (> (match-end 0) pos))))))
2722       (if (and found (<= (match-beginning 0) pos)
2723                (> (match-end 0) pos))
2724           (buffer-substring (match-beginning 1) (match-end 1))
2725         (cond ((null errorstring)
2726                nil)
2727               ((eq errorstring t)
2728                (beep)
2729                nil)
2730               (t
2731                (error "No %s around position %d" errorstring pos)))))))
2732
2733 (defun Info-follow-clicked-node (event)
2734   "Follow a node reference near clicked point.  Like M, F, N, P or U command.
2735 At end of the node's text, moves to the next node."
2736   (interactive "@e")
2737   (or (and (event-point event)
2738            (Info-follow-nearest-node
2739             (max (progn
2740                    (select-window (event-window event))
2741                    (event-point event))
2742                  (1+ (point-min)))))
2743       (error "click on a cross-reference to follow")))
2744
2745 (defun Info-maybe-follow-clicked-node (event &optional click-count)
2746   "Follow a node reference (if any) near clicked point.
2747 Like M, F, N, P or U command.  At end of the node's text, moves to the
2748 next node.  No error is given if there is no node to follow."
2749   (interactive "@e")
2750   (and Info-button1-follows-hyperlink
2751        (event-point event)
2752        (Info-follow-nearest-node
2753         (max (progn
2754                (select-window (event-window event))
2755                (event-point event))
2756              (1+ (point-min))))))
2757
2758 (defun Info-find-nearest-node (point)
2759   (let (node)
2760     (cond
2761      ((= point (point-min)) nil)   ; don't trigger on accidental RET.
2762      ((setq node (Info-get-token point
2763                                  (format "\\*%s[ \n]" Info-footnote-tag)
2764                                  (format "\\*%s[ \n]\\([^:]*\\):"
2765                                          Info-footnote-tag)))
2766       (list "Following cross-reference %s..."
2767             (list 'Info-follow-reference node)))
2768      ((setq node (Info-get-token point "\\* " #r"\* \([^:]*\)::"))
2769       (list "Selecting menu item %s..."
2770             (list 'Info-goto-node node nil t)))
2771      ((setq node (Info-get-token point "\\* " #r"\* \([^:]*\):"))
2772       (list "Selecting menu item %s..."
2773             (list 'Info-menu node)))
2774      ((setq node (Info-get-token point "Up: " "Up: \\([^,\n\t]*\\)"))
2775       (list "Going up..."
2776             (list 'Info-goto-node node)))
2777      ((setq node (Info-get-token point "Next: " "Next: \\([^,\n\t]*\\)"))
2778       (list "Next node..."
2779             (list 'Info-goto-node node)))
2780      ((setq node (Info-get-token point "File: " "File: \\([^,\n\t]*\\)"))
2781       (list "Top node..."
2782             (list 'Info-goto-node "Top")))
2783      ((setq node (Info-get-token point "Prev[ious]*: "
2784                                  "Prev[ious]*: \\([^,\n\t]*\\)"))
2785       (list "Previous node..."
2786             (list 'Info-goto-node node)))
2787      ((setq node (Info-get-token point "Node: " "Node: \\([^,\n\t]*\\)"))
2788       (list "Reselecting %s..."
2789             (list 'Info-goto-node node)))
2790      ((save-excursion (goto-char point) (looking-at "[ \n]*\\'"))
2791       (if Info-in-cross-reference
2792           (list "Back to last node..."
2793                 '(Info-last))
2794         (list "Next node..."
2795               '(Info-global-next)))))
2796     ))
2797
2798 (defun Info-follow-nearest-node (point)
2799   "Follow a node reference near point.  Like M, F, N, P or U command.
2800 At end of the node's text, moves to the next node."
2801   (interactive "d")
2802   (let ((data (Info-find-nearest-node point)))
2803     (if (null data)
2804         nil
2805       (let ((msg (format (car data) (nth 1 (nth 1 data)))))
2806         (message "%s" msg)
2807         (eval (nth 1 data))
2808         (message "%sdone" msg))
2809       t)))
2810
2811 (defun Info-indicated-node (event)
2812   (condition-case ()
2813       (save-excursion
2814         (cond ((eventp event)
2815                (set-buffer (event-buffer event))
2816                (setq event (event-point event))))
2817         (let* ((data (Info-find-nearest-node event))
2818                (name (nth 1 (nth 1 data))))
2819           (and name (nth 1 data))))
2820     (error nil)))
2821 \f
2822 (defun Info-mouse-track-double-click-hook (event click-count)
2823   "Handle double-clicks by turning pages, like the `gv' ghostscript viewer"
2824   (if (/= click-count 2)
2825       ;; Return nil so any other hooks are performed.
2826       nil
2827       (let* ((fw (face-width 'default))
2828              (fh (face-height 'default))
2829              (x (/ (event-x-pixel event) fw))
2830              (y (/ (event-y-pixel event) fw))
2831              (w (/ (window-pixel-width (event-window event)) fw))
2832              (h (/ (window-pixel-height (event-window event)) fh))
2833              (bx 3)
2834              (by 2))
2835         (cond
2836           ((<= y by) (Info-up) t)
2837           ((>= y (- h by)) (Info-nth-menu-item 1) t)
2838           ((<= x bx) (Info-prev) t)
2839           ((>= x (- w bx)) (Info-next) t)
2840           (t nil)))))
2841 \f
2842 (defvar Info-mode-map nil
2843   "Keymap containing Info commands.")
2844
2845 (if Info-mode-map
2846     nil
2847   (setq Info-mode-map (make-sparse-keymap))
2848   (suppress-keymap Info-mode-map)
2849   (define-key Info-mode-map "." 'beginning-of-buffer)
2850   (define-key Info-mode-map " " 'Info-scroll-next)
2851   (define-key Info-mode-map "1" 'Info-nth-menu-item)
2852   (define-key Info-mode-map "2" 'Info-nth-menu-item)
2853   (define-key Info-mode-map "3" 'Info-nth-menu-item)
2854   (define-key Info-mode-map "4" 'Info-nth-menu-item)
2855   (define-key Info-mode-map "5" 'Info-nth-menu-item)
2856   (define-key Info-mode-map "6" 'Info-nth-menu-item)
2857   (define-key Info-mode-map "7" 'Info-nth-menu-item)
2858   (define-key Info-mode-map "8" 'Info-nth-menu-item)
2859   (define-key Info-mode-map "9" 'Info-nth-menu-item)
2860   (define-key Info-mode-map "0" 'Info-last-menu-item)
2861   (define-key Info-mode-map "?" 'Info-summary)
2862   (define-key Info-mode-map "a" 'Info-annotate)
2863   (define-key Info-mode-map "b" 'beginning-of-buffer)
2864   (define-key Info-mode-map "d" 'Info-directory)
2865   (define-key Info-mode-map "e" 'Info-edit)
2866   (define-key Info-mode-map "f" 'Info-follow-reference)
2867   (define-key Info-mode-map "g" 'Info-goto-node)
2868   (define-key Info-mode-map "h" 'Info-help)
2869   (define-key Info-mode-map "i" 'Info-index)
2870   (define-key Info-mode-map "j" 'Info-goto-bookmark)
2871   (define-key Info-mode-map "k" 'Info-emacs-key)
2872   (define-key Info-mode-map "l" 'Info-last)
2873   (define-key Info-mode-map "m" 'Info-menu)
2874   (define-key Info-mode-map "n" 'Info-next)
2875   (define-key Info-mode-map "p" 'Info-prev)
2876   (define-key Info-mode-map "q" 'Info-exit)
2877   (define-key Info-mode-map "r" 'Info-follow-reference)
2878   (define-key Info-mode-map "s" 'Info-search)
2879   (define-key Info-mode-map "t" 'Info-top)
2880   (define-key Info-mode-map "u" 'Info-up)
2881   (define-key Info-mode-map "v" 'Info-visit-file)
2882   (define-key Info-mode-map "x" 'Info-bookmark)
2883   (define-key Info-mode-map "<" 'Info-top)
2884   (define-key Info-mode-map ">" 'Info-end)
2885   (define-key Info-mode-map "[" 'Info-global-prev)
2886   (define-key Info-mode-map "]" 'Info-global-next)
2887   (define-key Info-mode-map "{" 'Info-page-prev)
2888   (define-key Info-mode-map "}" 'Info-page-next)
2889   (define-key Info-mode-map "=" 'Info-restore-point)
2890   (define-key Info-mode-map "!" 'Info-select-node)
2891   (define-key Info-mode-map "@" 'Info-follow-nearest-node)
2892   (define-key Info-mode-map "," 'Info-index-next)
2893   (define-key Info-mode-map "*" 'Info-elisp-ref)
2894   (define-key Info-mode-map [tab] 'Info-next-reference)
2895   (define-key Info-mode-map [(meta tab)] 'Info-prev-reference)
2896   (define-key Info-mode-map [(shift tab)] 'Info-prev-reference)
2897   (define-key Info-mode-map "\r" 'Info-follow-nearest-node)
2898   ;; XEmacs addition
2899   (define-key Info-mode-map 'backspace 'Info-scroll-prev)
2900   (define-key Info-mode-map 'delete 'Info-scroll-prev)
2901   (define-key Info-mode-map 'button2 'Info-follow-clicked-node)
2902   (define-key Info-mode-map 'button3 'Info-select-node-menu))
2903
2904 \f
2905 ;; Info mode is suitable only for specially formatted data.
2906 (put 'info-mode 'mode-class 'special)
2907
2908 (defun Info-mode ()
2909   "Info mode is for browsing through the Info documentation tree.
2910 Documentation in Info is divided into \"nodes\", each of which
2911 discusses one topic and contains references to other nodes
2912 which discuss related topics.  Info has commands to follow
2913 the references and show you other nodes.
2914
2915 h       Invoke the Info tutorial.
2916 q       Quit Info: return to the previously selected file or buffer.
2917
2918 Selecting other nodes:
2919 n       Move to the \"next\" node of this node.
2920 p       Move to the \"previous\" node of this node.
2921 m       Pick menu item specified by name (or abbreviation).
2922 1-9, 0  Pick first..ninth, last item in node's menu.
2923         Menu items select nodes that are \"subsections\" of this node.
2924 u       Move \"up\" from this node (i.e., from a subsection to a section).
2925 f or r  Follow a cross reference by name (or abbrev).  Type `l' to get back.
2926 RET     Follow cross reference or menu item indicated by cursor.
2927 i       Look up a topic in this file's Index and move to that node.
2928 ,       (comma) Move to the next match from a previous `i' command.
2929 l       (letter L) Move back to the last node you were in.
2930
2931 Moving within a node:
2932 Space   Scroll forward a full screen.   DEL       Scroll backward.
2933 b       Go to beginning of node.        Meta->    Go to end of node.
2934 TAB     Go to next cross-reference.     Meta-TAB  Go to previous ref.
2935
2936 Mouse commands:
2937 Left Button     Set point (usual text-mode functionality)
2938 Middle Button   Click on a highlighted node reference to go to it.
2939 Right Button    Pop up a menu of applicable Info commands.
2940
2941 Left Button Double Click in window edges:
2942  Top edge:    Go up to the parent node, like `u'.
2943  Left edge:   Go to the previous node, like `p'.
2944  Right edge:  Go to the next node, like `n'.
2945  Bottom edge: Follow first menu item, like `1'.
2946
2947 Advanced commands:
2948 g       Move to node, file, or annotation tag specified by name.
2949         Examples:  `g Rectangles' `g (Emacs)Rectangles' `g Emacs'.
2950 v       Move to file, with filename completion.
2951 k       Look up a key sequence in Emacs manual (also C-h C-k at any time).
2952 *       Look up a function name in Emacs Lisp manual (also C-h C-f).
2953 d       Go to the main directory of Info files.
2954 < or t  Go to Top (first) node of this file.
2955 >       Go to last node in this file.
2956 \[      Go to previous node, treating file as one linear document.
2957 \]      Go to next node, treating file as one linear document.
2958 {       Scroll backward, or go to previous node if at top.
2959 }       Scroll forward, or go to next node if at bottom.
2960 =       Restore cursor position from last time in this node.
2961 a       Add a private note (annotation) to the current node.
2962 x, j    Add, jump to a bookmark (annotation tag).
2963 s       Search this Info file for a node containing the specified regexp.
2964 e       Edit the contents of the current node."
2965   (kill-all-local-variables)
2966   (setq major-mode 'Info-mode)
2967   (setq mode-name "Info")
2968   (use-local-map Info-mode-map)
2969   (set-syntax-table text-mode-syntax-table)
2970   (setq local-abbrev-table text-mode-abbrev-table)
2971   (setq case-fold-search t)
2972   (setq buffer-read-only t)
2973 ;  (setq buffer-mouse-map Info-mode-mouse-map)
2974   (make-local-variable 'Info-current-file)
2975   (make-local-variable 'Info-current-subfile)
2976   (make-local-variable 'Info-current-node)
2977   (make-local-variable 'Info-tag-table-marker)
2978   (setq Info-tag-table-marker (make-marker))
2979   (make-local-variable 'Info-tag-table-buffer)
2980   (setq Info-tag-table-buffer nil)
2981   (make-local-variable 'Info-current-file-completions)
2982   (make-local-variable 'Info-current-annotation-completions)
2983   (make-local-variable 'Info-index-alternatives)
2984   (make-local-variable 'Info-history)
2985   ;; Faces are now defined by `defface'...
2986   (make-local-variable 'mouse-track-click-hook)
2987   (add-hook 'mouse-track-click-hook 'Info-maybe-follow-clicked-node)
2988   (add-hook 'mouse-track-click-hook 'Info-mouse-track-double-click-hook)
2989   ;; #### The console-on-window-system-p check is to allow this to
2990   ;; work on tty's.  The real problem here is that featurep really
2991   ;; needs to have some device/console domain knowledge added to it.
2992   (defvar info::toolbar)
2993   (if (and (featurep 'toolbar)
2994            (console-on-window-system-p)
2995            (not Info-inhibit-toolbar))
2996       (set-specifier default-toolbar (cons (current-buffer) info::toolbar)))
2997   (if (featurep 'menubar)
2998       (progn
2999         ;; make a local copy of the menubar, so our modes don't
3000         ;; change the global menubar
3001         (easy-menu-add '("Info" :filter Info-menu-filter))))
3002   (run-hooks 'Info-mode-hook)
3003   (Info-set-mode-line))
3004
3005 (defvar Info-edit-map nil
3006   "Local keymap used within `e' command of Info.")
3007
3008 (if Info-edit-map
3009     nil
3010   ;; XEmacs: remove FSF stuff
3011   (setq Info-edit-map (make-sparse-keymap))
3012   (set-keymap-name Info-edit-map 'Info-edit-map)
3013   (set-keymap-parents Info-edit-map (list text-mode-map))
3014   (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit))
3015
3016 ;; Info-edit mode is suitable only for specially formatted data.
3017 (put 'info-edit-mode 'mode-class 'special)
3018
3019 (defun Info-edit-mode ()
3020   "Major mode for editing the contents of an Info node.
3021 Like text mode with the addition of `Info-cease-edit'
3022 which returns to Info mode for browsing.
3023 \\{Info-edit-map}"
3024   )
3025
3026 (defun Info-edit ()
3027   "Edit the contents of this Info node.
3028 Allowed only if variable `Info-enable-edit' is non-nil."
3029   (interactive)
3030   (or Info-enable-edit
3031       (error "Editing info nodes is not enabled"))
3032   (use-local-map Info-edit-map)
3033   (setq major-mode 'Info-edit-mode)
3034   (setq mode-name "Info Edit")
3035   (kill-local-variable 'modeline-buffer-identification)
3036   (setq buffer-read-only nil)
3037   ;; Make mode line update.
3038   (set-buffer-modified-p (buffer-modified-p))
3039   (message (substitute-command-keys
3040              "Editing: Type \\[Info-cease-edit] to return to info")))
3041
3042 (defun Info-cease-edit ()
3043   "Finish editing Info node; switch back to Info proper."
3044   (interactive)
3045   ;; Do this first, so nothing has changed if user C-g's at query.
3046   (and (buffer-modified-p)
3047        (y-or-n-p "Save the file? ")
3048        (save-buffer))
3049   (use-local-map Info-mode-map)
3050   (setq major-mode 'Info-mode)
3051   (setq mode-name "Info")
3052   (Info-set-mode-line)
3053   (setq buffer-read-only t)
3054   ;; Make mode line update.
3055   (set-buffer-modified-p (buffer-modified-p))
3056   (and (marker-position Info-tag-table-marker)
3057        (buffer-modified-p)
3058        (message "Tags may have changed.  Use Info-tagify if necessary")))
3059 \f
3060 (defun Info-find-emacs-command-nodes (command)
3061   "Return a list of locations documenting COMMAND in the SXEmacs Info manual.
3062 The locations are of the format used in Info-history, i.e.
3063 \(FILENAME NODENAME BUFFERPOS\)."
3064   (let ((where '())
3065         (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command))
3066                           #r":\s *\(.*\)\.")))
3067     (save-excursion
3068       (Info-find-node "SXEmacs" "Command Index")
3069       ;; Take the index node off the Info history.
3070       ;; ??? says this isn't safe someplace else... hmmm.
3071       (setq Info-history (cdr Info-history))
3072       (goto-char (point-max))
3073       (while (re-search-backward cmd-desc nil t)
3074           (setq where (cons (list Info-current-file
3075                                   (buffer-substring
3076                                    (match-beginning 1)
3077                                    (match-end 1))
3078                                   0)
3079                             where)))
3080       where)))
3081 \f
3082 ;;; fontification and mousability for info
3083
3084 (defun Info-highlight-region (start end face)
3085   (let ((extent nil)
3086         (splitp (string-match "\n[ \t]+" (buffer-substring start end))))
3087     (if splitp
3088         (save-excursion
3089           (setq extent (make-extent start (progn (goto-char start)
3090                                                  (end-of-line)
3091                                                  (point))))
3092           (set-extent-face extent face)
3093           (set-extent-property extent 'info t)
3094           (set-extent-property extent 'highlight t)
3095           (skip-chars-forward "\n\t ")
3096           (setq extent (make-extent (point) end)))
3097       (setq extent (make-extent start end)))
3098     (set-extent-face extent face)
3099     (set-extent-property extent 'info t)
3100     (set-extent-property extent 'highlight t)))
3101
3102 (defun Info-fontify-node ()
3103   (save-excursion
3104     (let ((case-fold-search t)
3105           (xref-regexp (concat "\\*"
3106                                (regexp-quote Info-footnote-tag)
3107                                "[ \n\t]*\\([^:]*\\):")))
3108       ;; Clear the old extents
3109       (map-extents #'(lambda (x y) (delete-extent x))
3110                    (current-buffer) (point-min) (point-max) nil)
3111       ;; Break the top line iff it is > 79 characters.  Some info nodes
3112       ;; have top lines that span 3 lines because of long node titles.
3113       ;; eg: (Info-find-node "lispref.info" "Window-Level Event Position Info")
3114       (toggle-read-only -1)
3115       (let ((extent nil)
3116             (len 0)
3117             (done nil)
3118             (p (point-min)))
3119         (goto-char (point-min))
3120         (re-search-forward "Node: *[^,]+,  " nil t)
3121         (setq len (- (point) (point-min))
3122               extent (make-extent (point-min) (point)))
3123         (set-extent-property extent 'invisible t)
3124         (while (not done)
3125           (goto-char p)
3126           (end-of-line)
3127           (if (< (current-column) (+ 78 len))
3128               (setq done t)
3129             (goto-char p)
3130             (forward-char (+ 79 len))
3131             (re-search-backward "," nil t)
3132             (forward-char 1)
3133             (insert "\n")
3134             (just-one-space)
3135             (delete-backward-char 1)
3136             (setq p (point)
3137                   len 0))))
3138       (toggle-read-only 1)
3139       ;; Highlight xrefs in the top few lines of the node
3140       (goto-char (point-min))
3141       (if (looking-at "^File: [^,: \t]+,?[ \t]+")
3142           (progn
3143             (goto-char (match-end 0))
3144             (while
3145                 (looking-at "[ \t]*[^:, \t\n]+:[ \t]+\\([^:,\t\n]+\\),?\n?")
3146               (goto-char (match-end 0))
3147               (Info-highlight-region (match-beginning 1) (match-end 1)
3148                                      'info-xref))))
3149       ;; Now get the xrefs in the body
3150       (goto-char (point-min))
3151       (while (re-search-forward xref-regexp nil t)
3152         (if (= (char-after (1- (match-beginning 0))) ?\") ; hack
3153             nil
3154           (Info-highlight-region (match-beginning 1) (match-end 1)
3155                                  'info-xref)))
3156       ;; then highlight the nodes in the menu.
3157       (goto-char (point-min))
3158       (if (and (search-forward "\n* menu:" nil t))
3159           (while (re-search-forward
3160                   "^\\* \\([^:\t\n]*\\):?:[ \t\n]" nil t)
3161             (Info-highlight-region (match-beginning 1) (match-end 1)
3162                                    'info-node)))
3163       (set-buffer-modified-p nil))))
3164
3165 (defun Info-construct-menu (&optional event)
3166   "Construct a menu of Info commands.
3167 Adds an entry for the node at EVENT, or under point if EVENT is omitted.
3168 Used to construct the menubar submenu and popup menu."
3169   (or event (setq event (point)))
3170   (let ((case-fold-search t)
3171         (xref-regexp (concat "\\*"
3172                              (regexp-quote Info-footnote-tag)
3173                              "[ \n\t]*\\([^:]*\\):"))
3174         up-p prev-p next-p menu xrefs subnodes in)
3175     (save-excursion
3176       ;; `one-space' fixes "Notes:" xrefs that are split across lines.
3177       (flet
3178           ((one-space (text)
3179                       (let (i)
3180                         (while (setq i (string-match "[ \n\t]+" text i))
3181                           (setq text (concat (substring text 0 i) " "
3182                                              (substring text (match-end 0))))
3183                           (setq i (1+ i)))
3184                         text)))
3185         (goto-char (point-min))
3186         (if (looking-at ".*\\bNext:") (setq next-p t))
3187         (if (looking-at ".*\\bPrev:") (setq prev-p t))
3188         (if (looking-at ".*Up:") (setq up-p t))
3189         (setq menu (nconc
3190                     (if (setq in (Info-indicated-node event))
3191                         (list (vector (one-space (cadr in)) in t)
3192                               "--:shadowEtchedIn"))
3193                     (list
3194                      ["Goto Info Top-level" Info-directory]
3195                      (vector "Next Node" 'Info-next :active next-p)
3196                      (vector "Previous Node" 'Info-prev :active prev-p)
3197                      (vector "Parent Node (Up)" 'Info-up :active up-p)
3198                      ["Goto Node..." Info-goto-node]
3199                      ["Goto Last Visited Node " Info-last])))
3200         ;; Find the xrefs and make a list
3201         (while (re-search-forward xref-regexp nil t)
3202           (setq xrefs (cons (one-space (buffer-substring (match-beginning 1)
3203                                                          (match-end 1)))
3204                             xrefs))))
3205       (setq xrefs (nreverse xrefs))
3206       (if (> (length xrefs) 21) (setcdr (nthcdr 20 xrefs) '(more)))
3207       ;; Find the subnodes and make a list
3208       (goto-char (point-min))
3209       (if (search-forward "\n* menu:" nil t)
3210       (while (re-search-forward "^\\* \\([^:\t\n]*\\):" nil t)
3211         (setq subnodes (cons (buffer-substring (match-beginning 1)
3212                                                (match-end 1))
3213                              subnodes))))
3214       (setq subnodes (nreverse subnodes))
3215       (if (> (length subnodes) 21) (setcdr (nthcdr 20 subnodes) '(more))))
3216     (if xrefs
3217         (nconc menu (list "--:shadowDoubleEtchedIn"
3218                           "    Cross-References"
3219                           "--:singleLine")
3220                (mapcar #'(lambda (xref)
3221                            (if (eq xref 'more)
3222                                "...more..."
3223                              (vector xref
3224                                      (list 'Info-follow-reference xref))))
3225                        xrefs)))
3226     (if subnodes
3227         (nconc menu (list "--:shadowDoubleEtchedIn"
3228                           "      Sub-Nodes"
3229                           "--:singleLine")
3230                (mapcar #'(lambda (node)
3231                            (if (eq node 'more)
3232                                "...more..."
3233                              (vector node (list 'Info-menu node))))
3234                        subnodes)))
3235     menu))
3236
3237 (defun Info-menu-filter (menu)
3238   "This is the menu filter for the \"Info\" submenu."
3239   (Info-construct-menu))
3240
3241 (defun Info-select-node-menu (event)
3242   "Pops up a menu of applicable Info commands."
3243   (interactive "e")
3244   (select-window (event-window event))
3245   (let ((menu (Info-construct-menu event)))
3246     (setq menu (nconc (list "Info" ; title: not displayed
3247                             "     Info Commands"
3248                             "--:shadowDoubleEtchedOut")
3249                       menu))
3250     (let ((popup-menu-titles nil))
3251       (popup-menu menu))))
3252 \f
3253 ;;; Info toolbar support
3254
3255 ;; exit icon taken from GNUS
3256 (defvar info::toolbar-exit-icon
3257   (if (featurep 'toolbar)
3258       (toolbar-make-button-list
3259        (expand-file-name (if (featurep 'xpm) "info-exit.xpm" "info-exit.xbm")
3260                          toolbar-icon-directory)))
3261   "Exit Info icon")
3262
3263 (defvar info::toolbar-up-icon
3264   (if (featurep 'toolbar)
3265       (toolbar-make-button-list
3266        (expand-file-name (if (featurep 'xpm) "info-up.xpm" "info-up.xbm")
3267                          toolbar-icon-directory)))
3268   "Up icon")
3269
3270 (defvar info::toolbar-next-icon
3271   (if (featurep 'toolbar)
3272       (toolbar-make-button-list
3273        (expand-file-name (if (featurep 'xpm) "info-next.xpm" "info-next.xbm")
3274                          toolbar-icon-directory)))
3275   "Next icon")
3276
3277 (defvar info::toolbar-prev-icon
3278   (if (featurep 'toolbar)
3279       (toolbar-make-button-list
3280        (expand-file-name (if (featurep 'xpm) "info-prev.xpm" "info-prev.xbm")
3281                          toolbar-icon-directory)))
3282   "Prev icon")
3283
3284 (defvar info::toolbar
3285   (if (featurep 'toolbar)
3286 ; disabled until we get the next/prev-win icons working again.
3287 ;      (cons (first initial-toolbar-spec)
3288 ;       (cons (second initial-toolbar-spec)
3289              '([info::toolbar-exit-icon
3290                  Info-exit
3291                  t
3292                  "Exit info"]
3293                 [info::toolbar-next-icon
3294                  Info-next
3295                  t
3296                  "Next entry in same section"]
3297                 [info::toolbar-prev-icon
3298                  Info-prev
3299                  t
3300                  "Prev entry in same section"]
3301                 [info::toolbar-up-icon
3302                  Info-up
3303                  t
3304                  "Up entry to enclosing section"]
3305                 )))
3306 ;))
3307 \f
3308 (provide 'info)
3309
3310 (run-hooks 'Info-load-hook)
3311
3312 ;;; info.el ends here