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