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