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