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