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