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