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