XEmacs 21.2-b2
[chise/xemacs-chise.git.1] / lisp / help.el
1 ;;; help.el --- help commands for XEmacs.
2
3 ;; Copyright (C) 1985, 1986, 1992-4, 1997 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal, dumped
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Synched up with: FSF 19.30.
26
27 ;;; Commentary:
28
29 ;; This file is dumped with XEmacs.
30
31 ;; This code implements XEmacs's on-line help system, the one invoked by
32 ;;`M-x help-for-help'.
33
34 ;; 06/11/1997 -- Converted to use char-after instead of broken
35 ;;  following-char. -slb
36
37 ;;; Code:
38
39 ;; Get the macro make-help-screen when this is compiled,
40 ;; or run interpreted, but not when the compiled code is loaded.
41 (eval-when-compile (require 'help-macro))
42
43 (defgroup help nil
44   "Support for on-line help systems."
45   :group 'emacs)
46
47 (defgroup help-appearance nil
48   "Appearance of help buffers."
49   :group 'help)
50
51 (defvar help-map (let ((map (make-sparse-keymap)))
52                    (set-keymap-name map 'help-map)
53                    (set-keymap-prompt
54                      map (purecopy (gettext "(Type ? for further options)")))
55                    map)
56   "Keymap for characters following the Help key.")
57
58 ;; global-map definitions moved to keydefs.el
59 (fset 'help-command help-map)
60
61 (define-key help-map (vector help-char) 'help-for-help)
62 (define-key help-map "?" 'help-for-help)
63 (define-key help-map 'help 'help-for-help)
64 (define-key help-map '(f1) 'help-for-help)
65
66 (define-key help-map "\C-l" 'describe-copying) ; on \C-c in FSFmacs
67 (define-key help-map "\C-d" 'describe-distribution)
68 (define-key help-map "\C-w" 'describe-no-warranty)
69 (define-key help-map "a" 'hyper-apropos) ; 'command-apropos in FSFmacs
70 (define-key help-map "A" 'command-apropos)
71
72 (define-key help-map "b" 'describe-bindings)
73 (define-key help-map "B" 'describe-beta)
74 (define-key help-map "\C-p" 'describe-pointer)
75
76 (define-key help-map "C" 'customize)
77 (define-key help-map "c" 'describe-key-briefly)
78 (define-key help-map "k" 'describe-key)
79
80 (define-key help-map "d" 'describe-function)
81 (define-key help-map "e" 'describe-last-error)
82 (define-key help-map "f" 'describe-function)
83
84 (define-key help-map "F" 'xemacs-local-faq)
85
86 (define-key help-map "i" 'info)
87 (define-key help-map '(control i) 'Info-query)
88 ;; FSFmacs has Info-goto-emacs-command-node on C-f, no binding
89 ;; for Info-elisp-ref
90 (define-key help-map '(control c) 'Info-goto-emacs-command-node)
91 (define-key help-map '(control k) 'Info-goto-emacs-key-command-node)
92 (define-key help-map '(control f) 'Info-elisp-ref)
93
94 (define-key help-map "l" 'view-lossage)
95
96 (define-key help-map "m" 'describe-mode)
97
98 (define-key help-map "\C-n" 'view-emacs-news)
99 (define-key help-map "n" 'view-emacs-news)
100
101 (define-key help-map "p" 'finder-by-keyword)
102
103 ;; Do this right with an autoload cookie in finder.el.
104 ;;(autoload 'finder-by-keyword "finder"
105 ;;  "Find packages matching a given keyword." t)
106
107 (define-key help-map "s" 'describe-syntax)
108
109 (define-key help-map "t" 'help-with-tutorial)
110
111 (define-key help-map "w" 'where-is)
112
113 (define-key help-map "v" 'describe-variable)
114
115 (if (fboundp 'view-last-error)
116     (define-key help-map "e" 'view-last-error))
117
118
119 (define-key help-map "q" 'help-quit)
120
121 ;#### This stuff was an attempt to have font locking and hyperlinks in the
122 ;help buffer, but it doesn't really work.  Some of this stuff comes from
123 ;FSF Emacs; but the FSF Emacs implementation is rather broken, as usual.
124 ;What needs to happen is this:
125 ;
126 ; -- we probably need a "hyperlink mode" from which help-mode is derived.
127 ; -- this means we probably need multiple inheritance of modes!
128 ;    Thankfully this is not hard to implement; we already have the
129 ;    ability for a keymap to have multiple parents.  However, we'd
130 ;    have to define any multiply-inherited-from modes using a standard
131 ;    `define-mode' construction instead of manually doing it, because
132 ;    we don't want each guy calling `kill-all-local-variables' and
133 ;    messing up the previous one.
134 ; -- we need to scan the buffer ourselves (not from font-lock, because
135 ;    the user might not have font-lock enabled) and highlight only
136 ;    those words that are *documented* functions and variables (and
137 ;    probably excluding words without dashes in them unless enclosed
138 ;    in quotes, so that common words like "list" and "point" don't
139 ;    become hyperlinks.
140 ; -- we should *not* use font-lock keywords like below.  Instead we
141 ;    should add the font-lock stuff ourselves during the scanning phase,
142 ;    if font-lock is enabled in this buffer.
143
144 ;(defun help-follow-reference (event extent user-data)
145 ;  (let ((symbol (intern-soft (extent-string extent))))
146 ;    (cond ((and symbol (fboundp symbol))
147 ;          (describe-function symbol))
148 ;         ((and symbol (boundp symbol))
149 ;          (describe-variable symbol))
150 ;         (t nil))))
151
152 ;(defvar help-font-lock-keywords
153 ;  (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
154 ;    (list
155 ;     ;;
156 ;     ;; The symbol itself.
157 ;     (list (concat "\\`\\(" name-char "+\\)\\(:\\)?")
158 ;          '(1 (if (match-beginning 2)
159 ;                  'font-lock-function-name-face
160 ;                'font-lock-variable-name-face)
161 ;              nil t))
162 ;     ;;
163 ;     ;; Words inside `' which tend to be symbol names.
164 ;     (list (concat "`\\(" sym-char sym-char "+\\)'")
165 ;          1 '(prog1
166 ;                 'font-lock-reference-face
167 ;               (add-list-mode-item (match-beginning 1)
168 ;                              (match-end 1)
169 ;                              nil
170 ;                              'help-follow-reference))
171 ;          t)
172 ;     ;;
173 ;     ;; CLisp `:' keywords as references.
174 ;     (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t)))
175 ;  "Default expressions to highlight in Help mode.")
176
177 ;(put 'help-mode 'font-lock-defaults '(help-font-lock-keywords))
178
179 (define-derived-mode help-mode view-major-mode "Help"
180   "Major mode for viewing help text.
181 Entry to this mode runs the normal hook `help-mode-hook'.
182 Commands:
183 \\{help-mode-map}"
184   )
185
186 (define-key help-mode-map "q" 'help-mode-quit)
187 (define-key help-mode-map "Q" 'help-mode-bury)
188 (define-key help-mode-map "f" 'find-function-at-point)
189 (define-key help-mode-map "d" 'describe-function-at-point)
190 (define-key help-mode-map "v" 'describe-variable-at-point)
191 (define-key help-mode-map "i" 'Info-elisp-ref)
192 (define-key help-mode-map "c" 'customize-variable)
193 (define-key help-mode-map [tab] 'help-next-symbol)
194 (define-key help-mode-map [(shift tab)] 'help-prev-symbol)
195 (define-key help-mode-map "n" 'help-next-section)
196 (define-key help-mode-map "p" 'help-prev-section)
197
198 (defun describe-function-at-point ()
199   "Describe directly the function at point in the other window."
200   (interactive)
201   (let ((symb (function-at-point)))
202     (when symb
203       (describe-function symb))))
204
205 (defun describe-variable-at-point ()
206   "Describe directly the variable at point in the other window."
207   (interactive)
208   (let ((symb (variable-at-point)))
209     (when symb
210       (describe-variable symb))))
211
212 (defun help-next-symbol ()
213   "Move point to the next quoted symbol."
214   (interactive)
215   (search-forward "`" nil t))
216
217 (defun help-prev-symbol ()
218   "Move point to the previous quoted symbol."
219   (interactive)
220   (search-backward "'" nil t))
221
222 (defun help-next-section ()
223   "Move point to the next quoted symbol."
224   (interactive)
225   (search-forward-regexp "^\\w+:" nil t))
226
227 (defun help-prev-section ()
228   "Move point to the previous quoted symbol."
229   (interactive)
230   (search-backward-regexp "^\\w+:" nil t))
231
232 (defun help-mode-bury ()
233   "Bury the help buffer, possibly restoring the previous window configuration."
234   (interactive)
235   (help-mode-quit t))
236
237 (defun help-mode-quit (&optional bury)
238   "Exit from help mode, possibly restoring the previous window configuration.
239 If the optional argument BURY is non-nil, the help buffer is buried,
240 otherwise it is killed."
241   (interactive)
242   (let ((buf (current-buffer)))
243     (cond ((frame-property (selected-frame) 'help-window-config)
244            (set-window-configuration
245             (frame-property (selected-frame) 'help-window-config))
246            (set-frame-property  (selected-frame) 'help-window-config nil))
247           ((not (one-window-p))
248            (delete-window)))
249     (if bury
250         (bury-buffer buf)
251       (kill-buffer buf))))
252
253 (defun help-quit ()
254   (interactive)
255   nil)
256
257 ;; This is a grody hack of the same genotype as `advertised-undo'; if the
258 ;; bindings of Backspace and C-h are the same, we want the menubar to claim
259 ;; that `info' in invoked with `C-h i', not `BS i'.
260
261 (defun deprecated-help-command ()
262   (interactive)
263   (if (eq 'help-command (key-binding "\C-h"))
264       (setq unread-command-event (character-to-event ?\C-h))
265     (help-for-help)))
266
267 ;;(define-key global-map 'backspace 'deprecated-help-command)
268
269 ;; This function has been moved to help-nomule.el and mule-help.el.
270 ;; TUTORIAL arg is XEmacs addition
271 ;(defun help-with-tutorial (&optional tutorial)
272 ;  "Select the XEmacs learn-by-doing tutorial.
273 ;Optional arg TUTORIAL specifies the tutorial file; default is \"TUTORIAL\"."
274 ;  (interactive)
275 ;  (if (null tutorial)
276 ;      (setq tutorial "TUTORIAL"))
277 ;  (let ((file (expand-file-name (concat "~/" tutorial))))
278 ;    (delete-other-windows)
279 ;    (if (get-file-buffer file)
280 ;       (switch-to-buffer (get-file-buffer file))
281 ;      (switch-to-buffer (create-file-buffer file))
282 ;      (setq buffer-file-name file)
283 ;      (setq default-directory (expand-file-name "~/"))
284 ;      (setq buffer-auto-save-file-name nil)
285 ;      (insert-file-contents (expand-file-name tutorial data-directory))
286 ;      (goto-char (point-min))
287 ;      (search-forward "\n<<")
288 ;      (delete-region (point-at-bol) (point-at-eol))
289 ;      (let ((n (- (window-height (selected-window))
290 ;                 (count-lines (point-min) (point))
291 ;                 6)))
292 ;       (if (< n 12)
293 ;           (newline n)
294 ;         ;; Some people get confused by the large gap.
295 ;         (newline (/ n 2))
296 ;         (insert "[Middle of page left blank for didactic purposes.  "
297 ;                 "Text continues below]")
298 ;         (newline (- n (/ n 2)))))
299 ;      (goto-char (point-min))
300 ;      (set-buffer-modified-p nil))))
301
302 ;; used by describe-key, describe-key-briefly, insert-key-binding, etc.
303
304 (defun key-or-menu-binding (key &optional menu-flag)
305   "Return the command invoked by KEY.
306 Like `key-binding', but handles menu events and toolbar presses correctly.
307 KEY is any value returned by `next-command-event'.
308 MENU-FLAG is a symbol that should be set to T if KEY is a menu event,
309  or NIL otherwise"
310   (let (defn)
311     (and menu-flag (set menu-flag nil))
312     ;; If the key typed was really a menu selection, grab the form out
313     ;; of the event object and intuit the function that would be called,
314     ;; and describe that instead.
315     (if (and (vectorp key) (= 1 (length key))
316              (or (misc-user-event-p (aref key 0))
317                  (eq (car-safe (aref key 0)) 'menu-selection)))
318         (let ((event (aref key 0)))
319           (setq defn (if (eventp event)
320                          (list (event-function event) (event-object event))
321                        (cdr event)))
322           (and menu-flag (set menu-flag t))
323           (when (eq (car defn) 'eval)
324             (setq defn (car (cdr defn))))
325           (when (eq (car-safe defn) 'call-interactively)
326             (setq defn (car (cdr defn))))
327           (when (and (consp defn) (null (cdr defn)))
328             (setq defn (car defn))))
329       ;; else
330       (setq defn (key-binding key)))
331     ;; kludge: if a toolbar button was pressed on, try to find the
332     ;; binding of the toolbar button.
333     (if (and (eq defn 'press-toolbar-button)
334              (vectorp key)
335              (button-press-event-p (aref key (1- (length key)))))
336         ;; wait for the button release.  We're on shaky ground here ...
337         (let ((event (next-command-event))
338               button)
339           (if (and (button-release-event-p event)
340                    (event-over-toolbar-p event)
341                    (eq 'release-and-activate-toolbar-button
342                        (key-binding (vector event)))
343                    (setq button (event-toolbar-button event)))
344               (toolbar-button-callback button)
345             ;; if anything went wrong, try returning the binding of
346             ;; the button-up event, of the original binding
347             (or (key-or-menu-binding (vector event))
348                 defn)))
349       ;; no toolbar kludge
350       defn)
351     ))
352
353 (defun describe-key-briefly (key &optional insert)
354   "Print the name of the function KEY invokes.  KEY is a string.
355 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
356   (interactive "kDescribe key briefly: \nP")
357   (let ((standard-output (if insert (current-buffer) t))
358         defn menup)
359     (setq defn (key-or-menu-binding key 'menup))
360     (if (or (null defn) (integerp defn))
361         (princ (format "%s is undefined" (key-description key)))
362       ;; If it's a keyboard macro which trivially invokes another command,
363       ;; document that instead.
364       (if (or (stringp defn) (vectorp defn))
365           (setq defn (or (key-binding defn)
366                          defn)))
367       (let ((last-event (and (vectorp key)
368                              (aref key (1- (length key))))))
369         (princ (format (cond (insert
370                               "%s (%s)")
371                              ((or (button-press-event-p last-event)
372                                   (button-release-event-p last-event))
373                               (gettext "%s at that spot runs the command %s"))
374                              (t
375                               (gettext "%s runs the command %s")))
376                        ;; This used to say 'This menu item' but it
377                        ;; could also be a scrollbar event.  We can't
378                        ;; distinguish at the moment.
379                        (if menup
380                            (if insert "item" "This item")
381                          (key-description key))
382                        (if (symbolp defn) defn (prin1-to-string defn))))))))
383
384 ;; #### this is a horrible piece of shit function that should
385 ;; not exist.  In FSF 19.30 this function has gotten three times
386 ;; as long and has tons and tons of dumb shit checking
387 ;; special-display-buffer-names and such crap.  I absolutely
388 ;; refuse to insert that Ebolification here.  I wanted to delete
389 ;; this function entirely but Mly bitched.
390 ;;
391 ;; If your user-land code calls this function, rewrite it to
392 ;; call with-displaying-help-buffer.
393
394 (defun print-help-return-message (&optional function)
395   "Display or return message saying how to restore windows after help command.
396 Computes a message and applies the optional argument FUNCTION to it.
397 If FUNCTION is nil, applies `message' to it, thus printing it."
398   (and (not (get-buffer-window standard-output))
399        (funcall
400         (or function 'message)
401         (concat
402          (substitute-command-keys
403           (if (one-window-p t)
404               (if pop-up-windows
405                   (gettext "Type \\[delete-other-windows] to remove help window.")
406                 (gettext "Type \\[switch-to-buffer] RET to remove help window."))
407    (gettext "Type \\[switch-to-buffer-other-window] RET to restore the other window.")))
408          (substitute-command-keys
409           (gettext "  \\[scroll-other-window] to scroll the help."))))))
410
411 (defcustom help-selects-help-window t
412   "*If nil, use the \"old Emacs\" behavior for Help buffers.
413 This just displays the buffer in another window, rather than selecting
414 the window."
415   :type 'boolean
416   :group 'help-appearance)
417
418 (defcustom help-max-help-buffers 10
419   "*Maximum help buffers to allow before they start getting killed.
420 If this is a positive integer, before a help buffer is displayed
421 by `with-displaying-help-buffer', any excess help buffers which
422 are not being displayed are first killed.  Otherwise, if it is
423 zero or nil, only one help buffer, \"*Help*\" is ever used."
424   :type '(choice integer (const :tag "None" nil))
425   :group 'help-appearance)
426
427 (defvar help-buffer-list nil
428   "List of help buffers used by `help-register-and-maybe-prune-excess'")
429
430 (defun help-register-and-maybe-prune-excess (newbuf)
431   "Register use of a help buffer and possibly kill any excess ones."
432   ;; remove new buffer from list
433   (setq help-buffer-list (remove newbuf help-buffer-list))
434   ;; maybe kill excess help buffers
435   (if (and (integerp help-max-help-buffers)
436            (> (length help-buffer-list) help-max-help-buffers))
437       (let ((keep-list nil)
438             (num-kill (- (length help-buffer-list)
439                          help-max-help-buffers)))
440         (while help-buffer-list
441           (let ((buf (car help-buffer-list)))
442             (if (and (or (equal buf newbuf) (get-buffer buf))
443                      (string-match "^*Help" buf)
444                      (save-excursion (set-buffer buf)
445                                      (eq major-mode 'help-mode)))
446                 (if (and (>= num-kill (length help-buffer-list))
447                          (not (get-buffer-window buf t t)))
448                     (kill-buffer buf)
449                   (setq keep-list (cons buf keep-list)))))
450           (setq help-buffer-list (cdr help-buffer-list)))
451         (setq help-buffer-list (nreverse keep-list))))
452   ;; push new buffer
453   (setq help-buffer-list (cons newbuf help-buffer-list)))
454
455 (defvar help-buffer-prefix-string "Help"
456   "Initial string to use in constructing help buffer names.
457 You should never set this directory, only let-bind it.")
458
459 (defun help-buffer-name (name)
460   "Return a name for a Help buffer using string NAME for context."
461   (if (and (integerp help-max-help-buffers)
462            (> help-max-help-buffers 0)
463            (stringp name))
464       (format "*%s: %s*" help-buffer-prefix-string name)
465     (format "*%s*" help-buffer-prefix-string)))
466
467 ;; Use this function for displaying help when C-h something is pressed
468 ;; or in similar situations.  Do *not* use it when you are displaying
469 ;; a help message and then prompting for input in the minibuffer --
470 ;; this macro usually selects the help buffer, which is not what you
471 ;; want in those situations.
472
473 ;; #### Should really be a macro to eliminate the requirement of
474 ;; caller to code a lambda form in THUNK -- mrb
475
476 ;; #### BEFORE you rush to make this a macro, think about backward
477 ;; compatibility.  The right way would be to create a macro with
478 ;; another name (which is a shame, because w-d-h-b is a perfect name
479 ;; for a macro) that uses with-displaying-help-buffer internally.
480
481 (defun with-displaying-help-buffer (thunk &optional name)
482   "Form which makes a help buffer with given NAME and evaluates BODY there.
483 The actual name of the buffer is generated by the function `help-buffer-name'."
484   (let* ((winconfig (current-window-configuration))
485          (was-one-window (one-window-p))
486          (buffer-name (help-buffer-name name))
487          (help-not-visible
488           (not (and (windows-of-buffer buffer-name) ;shortcut
489                     (memq (selected-frame)
490                           (mapcar 'window-frame
491                                   (windows-of-buffer buffer-name)))))))
492     (help-register-and-maybe-prune-excess buffer-name)
493     (prog1 (with-output-to-temp-buffer buffer-name
494              (prog1 (funcall thunk)
495                (save-excursion
496                  (set-buffer standard-output)
497                  (help-mode))))
498       (let ((helpwin (get-buffer-window buffer-name)))
499         (when helpwin
500           (with-current-buffer (window-buffer helpwin)
501             ;; If the *Help* buffer is already displayed on this
502             ;; frame, don't override the previous configuration
503             (when help-not-visible
504               (set-frame-property (selected-frame)
505                                   'help-window-config winconfig)))
506           (when help-selects-help-window
507             (select-window helpwin))
508           (cond ((eq helpwin (selected-window))
509                  (display-message 'command
510                    (substitute-command-keys "Type \\[help-mode-quit] to remove help window, \\[scroll-up] to scroll the help.")))
511                 (was-one-window
512                  (display-message 'command
513                    (substitute-command-keys "Type \\[delete-other-windows] to remove help window, \\[scroll-other-window] to scroll the help.")))
514                 (t
515                  (display-message 'command
516                    (substitute-command-keys "Type \\[switch-to-buffer-other-window] to restore the other window, \\[scroll-other-window] to scroll the help.")))))))))
517
518 (defun describe-key (key)
519   "Display documentation of the function invoked by KEY.
520 KEY is a string, or vector of events.
521 When called interactively, KEY may also be a menu selection."
522   (interactive "kDescribe key: ")
523   (let ((defn (key-or-menu-binding key))
524         (key-string (key-description key)))
525     (if (or (null defn) (integerp defn))
526         (message "%s is undefined" key-string)
527       (with-displaying-help-buffer
528        (lambda ()
529          (princ key-string)
530          (princ " runs ")
531          (if (symbolp defn)
532              (princ (format "`%s'" defn))
533            (princ defn))
534          (princ "\n\n")
535          (cond ((or (stringp defn) (vectorp defn))
536                 (let ((cmd (key-binding defn)))
537                   (if (not cmd)
538                       (princ "a keyboard macro")
539                     (progn
540                       (princ "a keyboard macro which runs the command ")
541                       (princ cmd)
542                       (princ ":\n\n")
543                       (if (documentation cmd) (princ (documentation cmd)))))))
544                ((and (consp defn) (not (eq 'lambda (car-safe defn))))
545                 (let ((describe-function-show-arglist nil))
546                   (describe-function-1 (car defn))))
547                ((symbolp defn)
548                 (describe-function-1 defn))
549                ((documentation defn)
550                 (princ (documentation defn)))
551                (t
552                 (princ "not documented"))))
553        (format "key `%s'" key-string)))))
554
555 (defun describe-mode ()
556   "Display documentation of current major mode and minor modes.
557 For this to work correctly for a minor mode, the mode's indicator variable
558 \(listed in `minor-mode-alist') must also be a function whose documentation
559 describes the minor mode."
560   (interactive)
561   (with-displaying-help-buffer
562    (lambda ()
563      ;; XEmacs change: print the major-mode documentation before
564      ;; the minor modes.
565      (princ mode-name)
566      (princ " mode:\n")
567      (princ (documentation major-mode))
568      (princ "\n\n----\n\n")
569      (let ((minor-modes minor-mode-alist))
570        (while minor-modes
571          (let* ((minor-mode (car (car minor-modes)))
572                 (indicator (car (cdr (car minor-modes)))))
573            ;; Document a minor mode if it is listed in minor-mode-alist,
574            ;; bound locally in this buffer, non-nil, and has a function
575            ;; definition.
576            (if (and (boundp minor-mode)
577                     (symbol-value minor-mode)
578                     (fboundp minor-mode))
579                (let ((pretty-minor-mode minor-mode))
580                  (if (string-match "-mode\\'" (symbol-name minor-mode))
581                      (setq pretty-minor-mode
582                            (capitalize
583                             (substring (symbol-name minor-mode)
584                                        0 (match-beginning 0)))))
585                  (while (and (consp indicator) (extentp (car indicator)))
586                    (setq indicator (cdr indicator)))
587                  (while (and indicator (symbolp indicator))
588                    (setq indicator (symbol-value indicator)))
589                  (princ (format "%s minor mode (indicator%s):\n"
590                                 pretty-minor-mode indicator))
591                  (princ (documentation minor-mode))
592                  (princ "\n\n----\n\n"))))
593          (setq minor-modes (cdr minor-modes)))))
594    (format "%s mode" mode-name)))
595
596 ;; So keyboard macro definitions are documented correctly
597 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
598
599 (defun describe-distribution ()
600   "Display info on how to obtain the latest version of XEmacs."
601   (interactive)
602   (find-file-read-only
603    (locate-data-file "DISTRIB")))
604
605 (defun describe-beta ()
606   "Display info on how to deal with Beta versions of XEmacs."
607   (interactive)
608   (find-file-read-only
609    (locate-data-file "BETA"))
610   (goto-char (point-min)))
611
612 (defun describe-copying ()
613   "Display info on how you may redistribute copies of XEmacs."
614   (interactive)
615   (find-file-read-only
616    (locate-data-file "COPYING"))
617   (goto-char (point-min)))
618
619 (defun describe-pointer ()
620   "Show a list of all defined mouse buttons, and their definitions."
621   (interactive)
622   (describe-bindings nil t))
623
624 (defun describe-project ()
625   "Display info on the GNU project."
626   (interactive)
627   (find-file-read-only
628    (locate-data-file "GNU"))
629   (goto-char (point-min)))
630
631 (defun describe-no-warranty ()
632   "Display info on all the kinds of warranty XEmacs does NOT have."
633   (interactive)
634   (describe-copying)
635   (let (case-fold-search)
636     (search-forward "NO WARRANTY")
637     (recenter 0)))
638
639 (defun describe-bindings (&optional prefix mouse-only-p)
640   "Show a list of all defined keys, and their definitions.
641 The list is put in a buffer, which is displayed.
642 If the optional argument PREFIX is supplied, only commands which
643 start with that sequence of keys are described.
644 If the second argument (prefix arg, interactively) is non-null
645 then only the mouse bindings are displayed."
646   (interactive (list nil current-prefix-arg))
647   (with-displaying-help-buffer
648    (lambda ()
649      (describe-bindings-1 prefix mouse-only-p))
650    (format "bindings for %s" major-mode)))
651
652 (defun describe-bindings-1 (&optional prefix mouse-only-p)
653   (let ((heading (if mouse-only-p
654             (gettext "button          binding\n------          -------\n")
655             (gettext "key             binding\n---             -------\n")))
656         (buffer (current-buffer))
657         (minor minor-mode-map-alist)
658         (local (current-local-map))
659         (shadow '()))
660     (set-buffer standard-output)
661     (while minor
662       (let ((sym (car (car minor)))
663             (map (cdr (car minor))))
664         (if (symbol-value-in-buffer sym buffer nil)
665             (progn
666               (insert (format "Minor Mode Bindings for `%s':\n"
667                               sym)
668                       heading)
669               (describe-bindings-internal map nil shadow prefix mouse-only-p)
670               (insert "\n")
671               (setq shadow (cons map shadow))))
672         (setq minor (cdr minor))))
673     (if local
674         (progn
675           (insert "Local Bindings:\n" heading)
676           (describe-bindings-internal local nil shadow prefix mouse-only-p)
677           (insert "\n")
678           (setq shadow (cons local shadow))))
679     (insert "Global Bindings:\n" heading)
680     (describe-bindings-internal (current-global-map)
681                                 nil shadow prefix mouse-only-p)
682     (when (and prefix function-key-map (not mouse-only-p))
683       (insert "\nFunction key map translations:\n" heading)
684       (describe-bindings-internal function-key-map nil nil
685                                   prefix mouse-only-p))
686     (set-buffer buffer)
687     standard-output))
688
689 (defun describe-prefix-bindings ()
690   "Describe the bindings of the prefix used to reach this command.
691 The prefix described consists of all but the last event
692 of the key sequence that ran this command."
693   (interactive)
694   (let* ((key (this-command-keys))
695          (prefix (make-vector (1- (length key)) nil))
696          i)
697     (setq i 0)
698     (while (< i (length prefix))
699       (aset prefix i (aref key i))
700       (setq i (1+ i)))
701     (with-displaying-help-buffer
702      (lambda ()
703        (princ "Key bindings starting with ")
704        (princ (key-description prefix))
705        (princ ":\n\n")
706        (describe-bindings-1 prefix nil))
707      (format "%s prefix" (key-description prefix)))))
708
709 ;; Make C-h after a prefix, when not specifically bound,
710 ;; run describe-prefix-bindings.
711 (setq prefix-help-command 'describe-prefix-bindings)
712
713 (defun describe-installation ()
714   "Display a buffer showing information about this XEmacs was compiled."
715   (interactive)
716   (if (and (boundp 'Installation-string)
717            (stringp Installation-string))
718       (with-displaying-help-buffer
719        (lambda ()
720          (princ Installation-string))
721        "Installation")
722     (error "No Installation information available.")))
723
724 (defun view-emacs-news ()
725   "Display info on recent changes to XEmacs."
726   (interactive)
727   (find-file (locate-data-file "NEWS")))
728
729 (defun xemacs-www-page ()
730   "Go to the XEmacs World Wide Web page."
731   (interactive)
732   (if (boundp 'browse-url-browser-function)
733       (funcall browse-url-browser-function "http://www.xemacs.org/")
734     (error "xemacs-www-page requires browse-url")))
735
736 (defun xemacs-www-faq ()
737   "View the latest and greatest XEmacs FAQ using the World Wide Web."
738   (interactive)
739   (if (boundp 'browse-url-browser-function)
740       (funcall browse-url-browser-function
741                "http://www.xemacs.org/faq/index.html")
742     (error "xemacs-www-faq requires browse-url")))
743
744 (defun xemacs-local-faq ()
745   "View the local copy of the XEmacs FAQ.
746 If you have access to the World Wide Web, you should use `xemacs-www-faq'
747 instead, to ensure that you get the most up-to-date information."
748   (interactive)
749   (save-window-excursion
750     (info)
751     (Info-find-node "xemacs-faq" "Top"))
752   (switch-to-buffer "*info*"))
753
754 (defcustom view-lossage-key-count 100
755   "*Number of keys `view-lossage' shows.
756 The maximum number of available keys is governed by `recent-keys-ring-size'."
757   :type 'integer
758   :group 'help)
759
760 (defcustom view-lossage-message-count 100
761   "*Number of minibuffer messages `view-lossage' shows."
762   :type 'integer
763   :group 'help)
764
765 (defun print-recent-messages (n)
766   "Print N most recent messages to standard-output, most recent first.
767 If N is nil, all messages will be printed."
768   (save-excursion
769     (let ((buffer (get-buffer-create " *Message-Log*"))
770           oldpoint extent)
771       (goto-char (point-max buffer) buffer)
772       (set-buffer standard-output)
773       (while (and (not (bobp buffer))
774                   (or (null n) (>= (decf n) 0)))
775         (setq oldpoint (point buffer))
776         (setq extent (extent-at oldpoint buffer
777                                 'message-multiline nil 'before))
778         ;; If the message was multiline, move all the way to the
779         ;; beginning.
780         (if extent
781             (goto-char (extent-start-position extent) buffer)
782           (forward-line -1 buffer))
783         (insert-buffer-substring buffer (point buffer) oldpoint)))))
784
785 (defun view-lossage ()
786   "Display recent input keystrokes and recent minibuffer messages.
787 The number of keys shown is controlled by `view-lossage-key-count'.
788 The number of messages shown is controlled by `view-lossage-message-count'."
789   (interactive)
790   (with-displaying-help-buffer
791    (lambda ()
792      (princ (key-description (recent-keys view-lossage-key-count)))
793      (save-excursion
794        (set-buffer standard-output)
795        (goto-char (point-min))
796        (insert "Recent keystrokes:\n\n")
797        (while (progn (move-to-column 50) (not (eobp)))
798          (search-forward " " nil t)
799          (insert "\n")))
800      ;; XEmacs addition: copy the messages from " *Message-Log*",
801      ;; reversing their order and handling multiline messages
802      ;; correctly.
803      (princ "\n\n\nRecent minibuffer messages (most recent first):\n\n")
804      (print-recent-messages view-lossage-message-count))
805    "lossage"))
806
807 (define-function 'help 'help-for-help)
808
809 (make-help-screen help-for-help
810   "A B C F I K L M N P S T V W C-c C-d C-f C-i C-k C-n C-w;  ? for more help:"
811   "Type a Help option:
812 \(Use SPC or DEL to scroll through this text.  Type \\<help-map>\\[help-quit] to exit the Help command.)
813
814 \\[hyper-apropos]       Type a substring; it shows a hypertext list of
815         functions and variables that contain that substring.
816         See also the `apropos' command.
817 \\[command-apropos]     Type a substring; it shows a list of commands
818         (interactively callable functions) that contain that substring.
819 \\[describe-bindings]   Table of all key bindings.
820 \\[describe-key-briefly]        Type a command key sequence;
821         it displays the function name that sequence runs.
822 \\[customize]   Customize Emacs options.
823 \\[Info-goto-emacs-command-node]        Type a function name; it displays the Info node for that command.
824 \\[describe-function]   Type a function name; it shows its documentation.
825 \\[Info-elisp-ref]      Type a function name; it jumps to the full documentation
826         in the XEmacs Lisp Programmer's Manual.
827 \\[xemacs-local-faq]    Local copy of the XEmacs FAQ.
828 \\[info]        Info documentation reader.
829 \\[Info-query]  Type an Info file name; it displays it in Info reader.
830 \\[describe-key]        Type a command key sequence;
831         it displays the documentation for the command bound to that key.
832 \\[Info-goto-emacs-key-command-node]    Type a command key sequence;
833         it displays the Info node for the command bound to that key.
834 \\[view-lossage]        Recent input keystrokes and minibuffer messages.
835 \\[describe-mode]       Documentation of current major and minor modes.
836 \\[view-emacs-news]     News of recent XEmacs changes.
837 \\[finder-by-keyword]   Type a topic keyword; it finds matching packages.
838 \\[describe-pointer]    Table of all mouse-button bindings.
839 \\[describe-syntax]     Contents of syntax table with explanations.
840 \\[help-with-tutorial]  XEmacs learn-by-doing tutorial.
841 \\[describe-variable]   Type a variable name; it displays its documentation and value.
842 \\[where-is]    Type a command name; it displays which keystrokes invoke that command.
843 \\[describe-distribution]       XEmacs ordering information.
844 \\[describe-no-warranty]        Information on absence of warranty for XEmacs.
845 \\[describe-copying]    XEmacs copying permission (General Public License)."
846   help-map)
847
848 (defmacro with-syntax-table (syntab &rest body)
849   "Evaluate BODY with the syntax-table SYNTAB"
850   `(let ((stab (syntax-table)))
851      (unwind-protect
852          (progn
853            (set-syntax-table (copy-syntax-table ,syntab))
854            ,@body)
855        (set-syntax-table stab))))
856 (put 'with-syntax-table 'lisp-indent-function 1)
857 (put 'with-syntax-table 'edebug-form-spec '(form body))
858
859 (defun function-called-at-point ()
860   "Return the function which is called by the list containing point.
861 If that gives no function, return the function whose name is around point.
862 If that doesn't give a function, return nil."
863   (or (ignore-errors
864         (save-excursion
865           (save-restriction
866             (narrow-to-region (max (point-min) (- (point) 1000))
867                               (point-max))
868             (backward-up-list 1)
869             (forward-char 1)
870             (let (obj)
871               (setq obj (read (current-buffer)))
872               (and (symbolp obj) (fboundp obj) obj)))))
873       (ignore-errors
874         (with-syntax-table emacs-lisp-mode-syntax-table
875           (save-excursion
876             (or (not (zerop (skip-syntax-backward "_w")))
877                 (eq (char-syntax (char-after (point))) ?w)
878                 (eq (char-syntax (char-after (point))) ?_)
879                 (forward-sexp -1))
880             (skip-chars-forward "`'")
881             (let ((obj (read (current-buffer))))
882               (and (symbolp obj) (fboundp obj) obj)))))))
883
884 (defun function-at-point ()
885   "Return the function whose name is around point.
886 If that gives no function, return the function which is called by the
887 list containing point.  If that doesn't give a function, return nil."
888   (or (ignore-errors
889         (with-syntax-table emacs-lisp-mode-syntax-table
890           (save-excursion
891             (or (not (zerop (skip-syntax-backward "_w")))
892                 (eq (char-syntax (char-after (point))) ?w)
893                 (eq (char-syntax (char-after (point))) ?_)
894                 (forward-sexp -1))
895             (skip-chars-forward "`'")
896             (let ((obj (read (current-buffer))))
897               (and (symbolp obj) (fboundp obj) obj)))))
898       (ignore-errors
899         (save-excursion
900           (save-restriction
901             (narrow-to-region (max (point-min) (- (point) 1000))
902                               (point-max))
903             (backward-up-list 1)
904             (forward-char 1)
905             (let (obj)
906               (setq obj (read (current-buffer)))
907               (and (symbolp obj) (fboundp obj) obj)))))))
908
909 ;; Default to nil for the non-hackers?  Not until we find a way to
910 ;; distinguish hackers from non-hackers automatically!
911 (defcustom describe-function-show-arglist t
912   "*If non-nil, describe-function will show its arglist,
913 unless the function is autoloaded."
914   :type 'boolean
915   :group 'help-appearance)
916
917 (defun describe-symbol-find-file (function)
918   (let ((files load-history)
919         file)
920     (while files
921       (if (memq function (cdr (car files)))
922           (setq file (car (car files))
923                 files nil))
924       (setq files (cdr files)))
925     file))
926 (define-obsolete-function-alias
927   'describe-function-find-file
928   'describe-symbol-find-file)
929
930 (defun describe-function (function)
931   "Display the full documentation of FUNCTION (a symbol).
932 When run interactively, it defaults to any function found by
933 `function-at-point'."
934   (interactive
935     (let* ((fn (function-at-point))
936            (val (let ((enable-recursive-minibuffers t))
937                   (completing-read
938                     (if fn
939                         (format (gettext "Describe function (default %s): ")
940                                 fn)
941                         (gettext "Describe function: "))
942                     obarray 'fboundp t nil 'function-history))))
943       (list (if (equal val "") fn (intern val)))))
944   (with-displaying-help-buffer
945    (lambda ()
946      (describe-function-1 function)
947      ;; Return the text we displayed.
948      (buffer-string nil nil standard-output))
949     (format "function `%s'" function)))
950
951 (defun function-obsolete-p (function)
952   "Return non-nil if FUNCTION is obsolete."
953   (not (null (get function 'byte-obsolete-info))))
954
955 (defun function-obsoleteness-doc (function)
956   "If FUNCTION is obsolete, return a string describing this."
957   (let ((obsolete (get function 'byte-obsolete-info)))
958     (if obsolete
959         (format "Obsolete; %s"
960                 (if (stringp (car obsolete))
961                     (car obsolete)
962                   (format "use `%s' instead." (car obsolete)))))))
963
964 (defun function-compatible-p (function)
965   "Return non-nil if FUNCTION is present for Emacs compatibility."
966   (not (null (get function 'byte-compatible-info))))
967
968 (defun function-compatibility-doc (function)
969   "If FUNCTION is Emacs compatible, return a string describing this."
970   (let ((compatible (get function 'byte-compatible-info)))
971     (if compatible
972         (format "Emacs Compatible; %s"
973                 (if (stringp (car compatible))
974                     (car compatible)
975                   (format "use `%s' instead." (car compatible)))))))
976
977 ;Here are all the possibilities below spelled out, for the benefit
978 ;of the I18N3 snarfer.
979 ;
980 ;(gettext "a built-in function")
981 ;(gettext "an interactive built-in function")
982 ;(gettext "a built-in macro")
983 ;(gettext "an interactive built-in macro")
984 ;(gettext "a compiled Lisp function")
985 ;(gettext "an interactive compiled Lisp function")
986 ;(gettext "a compiled Lisp macro")
987 ;(gettext "an interactive compiled Lisp macro")
988 ;(gettext "a Lisp function")
989 ;(gettext "an interactive Lisp function")
990 ;(gettext "a Lisp macro")
991 ;(gettext "an interactive Lisp macro")
992 ;(gettext "a mocklisp function")
993 ;(gettext "an interactive mocklisp function")
994 ;(gettext "a mocklisp macro")
995 ;(gettext "an interactive mocklisp macro")
996 ;(gettext "an autoloaded Lisp function")
997 ;(gettext "an interactive autoloaded Lisp function")
998 ;(gettext "an autoloaded Lisp macro")
999 ;(gettext "an interactive autoloaded Lisp macro")
1000
1001 ;; taken out of `describe-function-1'
1002 (defun function-arglist (function)
1003   "Return a string giving the argument list of FUNCTION.
1004 For example:
1005
1006         (function-arglist 'function-arglist)
1007         => (function-arglist FUNCTION)
1008
1009 This function is used by `describe-function-1' to list function
1010 arguments in the standard Lisp style."
1011   (let* ((fndef (indirect-function function))
1012          (arglist
1013          (cond ((compiled-function-p fndef)
1014                 (compiled-function-arglist fndef))
1015                ((eq (car-safe fndef) 'lambda)
1016                 (nth 1 fndef))
1017                ((subrp fndef)
1018                 (let* ((doc (documentation function))
1019                        (args (and (string-match
1020                                    "[\n\t ]*\narguments: ?(\\(.*\\))\n?\\'"
1021                                    doc)
1022                                   (match-string 1 doc))))
1023                   ;; If there are no arguments documented for the
1024                   ;; subr, rather don't print anything.
1025                   (cond ((null args) t)
1026                         ((equal args "") nil)
1027                         (args))))
1028                (t t))))
1029     (cond ((listp arglist)
1030            (prin1-to-string
1031             (cons function (mapcar (lambda (arg)
1032                                      (if (memq arg '(&optional &rest))
1033                                          arg
1034                                        (intern (upcase (symbol-name arg)))))
1035                                    arglist))
1036             t))
1037           ((stringp arglist)
1038            (format "(%s %s)" function arglist)))))
1039
1040 (defun function-documentation (function &optional strip-arglist)
1041   "Return a string giving the documentation for FUNCTION, if any.
1042 If the optional argument STRIP-ARGLIST is non-nil, remove the arglist
1043 part of the documentation of internal subroutines."
1044   (let ((doc (condition-case nil
1045                  (or (documentation function)
1046                      (gettext "not documented"))
1047                (void-function ""))))
1048     (if (and strip-arglist
1049              (string-match "[\n\t ]*\narguments: ?(\\(.*\\))\n?\\'" doc))
1050         (setq doc (substring doc 0 (match-beginning 0))))
1051     doc))
1052
1053 (defun describe-function-1 (function &optional nodoc)
1054   "This function does the work for `describe-function'."
1055   (princ (format "`%s' is " function))
1056   (let* ((def function)
1057          aliases file-name autoload-file kbd-macro-p fndef macrop)
1058     (while (and (symbolp def) (fboundp def))
1059       (when (not (eq def function))
1060         (setq aliases
1061               (if aliases
1062                   ;; I18N3 Need gettext due to concat
1063                   (concat aliases
1064                           (format
1065                            "\n     which is an alias for `%s', "
1066                            (symbol-name def)))
1067                 (format "an alias for `%s', " (symbol-name def)))))
1068       (setq def (symbol-function def)))
1069     (if (and (fboundp 'compiled-function-annotation)
1070              (compiled-function-p def))
1071         (setq file-name (compiled-function-annotation def)))
1072     (if (eq 'macro (car-safe def))
1073         (setq fndef (cdr def)
1074               file-name (and (compiled-function-p (cdr def))
1075                              (fboundp 'compiled-function-annotation)
1076                              (compiled-function-annotation (cdr def)))
1077               macrop t)
1078       (setq fndef def))
1079     (if aliases (princ aliases))
1080     (let ((int #'(lambda (string an-p macro-p)
1081                    (princ (format
1082                            (gettext (concat
1083                                      (cond ((commandp def)
1084                                             "an interactive ")
1085                                            (an-p "an ")
1086                                            (t "a "))
1087                                      "%s"
1088                                      (if macro-p " macro" " function")))
1089                            string)))))
1090       (cond ((or (stringp def) (vectorp def))
1091              (princ "a keyboard macro.")
1092              (setq kbd-macro-p t))
1093             ((subrp fndef)
1094              (funcall int "built-in" nil macrop))
1095             ((compiled-function-p fndef)
1096              (funcall int "compiled Lisp" nil macrop))
1097             ((eq (car-safe fndef) 'lambda)
1098              (funcall int "Lisp" nil macrop))
1099             ((eq (car-safe fndef) 'mocklisp)
1100              (funcall int "mocklisp" nil macrop))
1101             ((eq (car-safe def) 'autoload)
1102              (setq autoload-file (elt def 1))
1103              (funcall int "autoloaded Lisp" t (elt def 4)))
1104             ((and (symbolp def) (not (fboundp def)))
1105              (princ "a symbol with a void (unbound) function definition."))
1106             (t
1107              nil)))
1108     (princ "\n")
1109     (if autoload-file
1110         (princ (format "  -- autoloads from \"%s\"\n" autoload-file)))
1111     (or file-name
1112         (setq file-name (describe-symbol-find-file function)))
1113     (if file-name
1114         (princ (format "  -- loaded from \"%s\"\n" file-name)))
1115 ;;     (terpri)
1116     (if describe-function-show-arglist
1117         (let ((arglist (function-arglist function)))
1118           (when arglist
1119             (princ arglist)
1120             (terpri))))
1121     (terpri)
1122     (cond (kbd-macro-p
1123            (princ "These characters are executed:\n\n\t")
1124            (princ (key-description def))
1125            (cond ((setq def (key-binding def))
1126                   (princ (format "\n\nwhich executes the command `%s'.\n\n"
1127                                  def))
1128                   (describe-function-1 def))))
1129           (nodoc nil)
1130           (t
1131            ;; tell the user about obsoleteness.
1132            ;; If the function is obsolete and is aliased, don't
1133            ;; even bother to report the documentation, as a further
1134            ;; encouragement to use the new function.
1135            (let ((obsolete (function-obsoleteness-doc function))
1136                  (compatible (function-compatibility-doc function)))
1137              (when obsolete
1138                (princ obsolete)
1139                (terpri)
1140                (terpri))
1141              (when compatible
1142                (princ compatible)
1143                (terpri)
1144                (terpri))
1145              (unless (and obsolete aliases)
1146                (let ((doc (function-documentation function t)))
1147                  (princ "Documentation:\n")
1148                  (princ doc)
1149                  (unless (or (equal doc "")
1150                              (eq ?\n (aref doc (1- (length doc)))))
1151                    (terpri)))))))))
1152
1153
1154 ;;; [Obnoxious, whining people who complain very LOUDLY on Usenet
1155 ;;; are binding this to keys.]
1156 (defun describe-function-arglist (function)
1157   (interactive (list (or (function-at-point)
1158                          (error "no function call at point"))))
1159   (message nil)
1160   (message (function-arglist function)))
1161
1162
1163 (defun variable-at-point ()
1164   (ignore-errors
1165     (with-syntax-table emacs-lisp-mode-syntax-table
1166       (save-excursion
1167         (or (not (zerop (skip-syntax-backward "_w")))
1168             (eq (char-syntax (char-after (point))) ?w)
1169             (eq (char-syntax (char-after (point))) ?_)
1170             (forward-sexp -1))
1171         (skip-chars-forward "'")
1172         (let ((obj (read (current-buffer))))
1173           (and (symbolp obj) (boundp obj) obj))))))
1174
1175 (defun variable-obsolete-p (variable)
1176   "Return non-nil if VARIABLE is obsolete."
1177   (not (null (get variable 'byte-obsolete-variable))))
1178
1179 (defun variable-obsoleteness-doc (variable)
1180   "If VARIABLE is obsolete, return a string describing this."
1181   (let ((obsolete (get variable 'byte-obsolete-variable)))
1182     (if obsolete
1183         (format "Obsolete; %s"
1184                 (if (stringp obsolete)
1185                     obsolete
1186                   (format "use `%s' instead." obsolete))))))
1187
1188 (defun variable-compatible-p (variable)
1189   "Return non-nil if VARIABLE is Emacs compatible."
1190   (not (null (get variable 'byte-compatible-variable))))
1191
1192 (defun variable-compatibility-doc (variable)
1193   "If VARIABLE is Emacs compatible, return a string describing this."
1194   (let ((compatible (get variable 'byte-compatible-variable)))
1195     (if compatible
1196         (format "Emacs Compatible; %s"
1197                 (if (stringp compatible)
1198                     compatible
1199                   (format "use `%s' instead." compatible))))))
1200
1201 (defun built-in-variable-doc (variable)
1202   "Return a string describing whether VARIABLE is built-in."
1203   (let ((type (built-in-variable-type variable)))
1204     (case type
1205       (integer "a built-in integer variable")
1206       (const-integer "a built-in constant integer variable")
1207       (boolean "a built-in boolean variable")
1208       (const-boolean "a built-in constant boolean variable")
1209       (object "a simple built-in variable")
1210       (const-object "a simple built-in constant variable")
1211       (const-specifier "a built-in constant specifier variable")
1212       (current-buffer "a built-in buffer-local variable")
1213       (const-current-buffer "a built-in constant buffer-local variable")
1214       (default-buffer "a built-in default buffer-local variable")
1215       (selected-console "a built-in console-local variable")
1216       (const-selected-console "a built-in constant console-local variable")
1217       (default-console "a built-in default console-local variable")
1218       (t
1219        (if type "an unknown type of built-in variable?"
1220          "a variable declared in Lisp")))))
1221
1222 (defun describe-variable (variable)
1223   "Display the full documentation of VARIABLE (a symbol)."
1224   (interactive
1225    (let* ((v (variable-at-point))
1226           (val (let ((enable-recursive-minibuffers t))
1227                  (completing-read
1228                    (if v
1229                        (format "Describe variable (default %s): " v)
1230                        (gettext "Describe variable: "))
1231                    obarray 'boundp t nil 'variable-history))))
1232      (list (if (equal val "") v (intern val)))))
1233   (with-displaying-help-buffer
1234    (lambda ()
1235      (let ((origvar variable)
1236            aliases)
1237        (let ((print-escape-newlines t))
1238          (princ (format "`%s' is " (symbol-name variable)))
1239          (while (variable-alias variable)
1240            (let ((newvar (variable-alias variable)))
1241              (if aliases
1242                  ;; I18N3 Need gettext due to concat
1243                  (setq aliases
1244                        (concat aliases
1245                                (format "\n     which is an alias for `%s',"
1246                                        (symbol-name newvar))))
1247                (setq aliases
1248                      (format "an alias for `%s',"
1249                              (symbol-name newvar))))
1250              (setq variable newvar)))
1251          (if aliases
1252              (princ (format "%s" aliases)))
1253          (princ (built-in-variable-doc variable))
1254          (princ ".\n")
1255          (let ((file-name (describe-symbol-find-file variable)))
1256            (if file-name
1257                (princ (format "  -- loaded from \"%s\"\n" file-name))))
1258          (princ "\nValue: ")
1259          (if (not (boundp variable))
1260              (princ "void\n")
1261            (prin1 (symbol-value variable))
1262            (terpri))
1263          (terpri)
1264          (cond ((local-variable-p variable (current-buffer))
1265                 (let* ((void (cons nil nil))
1266                        (def (condition-case nil
1267                                 (default-value variable)
1268                               (error void))))
1269                   (princ "This value is specific to the current buffer.\n")
1270                   (if (local-variable-p variable nil)
1271                       (princ "(Its value is local to each buffer.)\n"))
1272                   (terpri)
1273                   (if (if (eq def void)
1274                           (boundp variable)
1275                         (not (eq (symbol-value variable) def)))
1276                       ;; #### I18N3 doesn't localize properly!
1277                       (progn (princ "Default-value: ")
1278                              (if (eq def void)
1279                                  (princ "void\n")
1280                                (prin1 def)
1281                                (terpri))
1282                              (terpri)))))
1283                ((local-variable-p variable (current-buffer) t)
1284                 (princ "Setting it would make its value buffer-local.\n\n"))))
1285        (princ "Documentation:")
1286        (terpri)
1287        (let ((doc (documentation-property variable 'variable-documentation))
1288              (obsolete (variable-obsoleteness-doc origvar))
1289              (compatible (variable-compatibility-doc origvar)))
1290          (when obsolete
1291            (princ obsolete)
1292            (terpri)
1293            (terpri))
1294          (when compatible
1295            (princ compatible)
1296            (terpri)
1297            (terpri))
1298          ;; don't bother to print anything if variable is obsolete and aliased.
1299          (when (or (not obsolete) (not aliases))
1300            (if doc
1301                ;; note: documentation-property calls substitute-command-keys.
1302                (princ doc)
1303              (princ "not documented as a variable."))))
1304        (terpri)))
1305    (format "variable `%s'" variable)))
1306
1307 (defun sorted-key-descriptions (keys &optional separator)
1308   "Sort and separate the key descriptions for KEYS.
1309 The sorting is done by length (shortest bindings first), and the bindings
1310 are separated with SEPARATOR (\", \" by default)."
1311   (mapconcat 'key-description
1312              (sort keys #'(lambda (x y)
1313                             (< (length x) (length y))))
1314              (or separator ", ")))
1315
1316 (defun where-is (definition &optional insert)
1317   "Print message listing key sequences that invoke specified command.
1318 Argument is a command definition, usually a symbol with a function definition.
1319 When run interactively, it defaults to any function found by
1320 `function-at-point'.
1321 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
1322   (interactive
1323    (let ((fn (function-at-point))
1324          (enable-recursive-minibuffers t)
1325          val)
1326      (setq val (read-command
1327                 (if fn (format "Where is command (default %s): " fn)
1328                   "Where is command: ")))
1329      (list (if (equal (symbol-name val) "")
1330                fn val)
1331            current-prefix-arg)))
1332   (let ((keys (where-is-internal definition)))
1333     (if keys
1334         (if insert
1335             (princ (format "%s (%s)" (sorted-key-descriptions keys)
1336                            definition) (current-buffer))
1337           (message "%s is on %s" definition (sorted-key-descriptions keys)))
1338       (if insert
1339           (princ (format (if (commandp definition) "M-x %s RET"
1340                            "M-: (%s ...)") definition) (current-buffer))
1341         (message "%s is not on any keys" definition))))
1342   nil)
1343
1344 ;; `locate-library' moved to "packages.el"
1345
1346 \f
1347 ;; Functions ported from C into Lisp in XEmacs
1348
1349 (defun describe-syntax ()
1350   "Describe the syntax specifications in the syntax table.
1351 The descriptions are inserted in a buffer, which is then displayed."
1352   (interactive)
1353   (with-displaying-help-buffer
1354    (lambda ()
1355      ;; defined in syntax.el
1356      (describe-syntax-table (syntax-table) standard-output))
1357    (format "syntax-table for %s" major-mode)))
1358
1359 (defun list-processes ()
1360   "Display a list of all processes.
1361 \(Any processes listed as Exited or Signaled are actually eliminated
1362 after the listing is made.)"
1363   (interactive)
1364   (with-output-to-temp-buffer "*Process List*"
1365     (set-buffer standard-output)
1366     (buffer-disable-undo standard-output)
1367     (make-local-variable 'truncate-lines)
1368     (setq truncate-lines t)
1369     ;;      00000000001111111111222222222233333333334444444444
1370     ;;      01234567890123456789012345678901234567890123456789
1371     ;; rewritten for I18N3.  This one should stay rewritten
1372     ;; so that the dashes will line up properly.
1373     (princ "Proc         Status   Buffer         Tty         Command\n----         ------   ------         ---         -------\n")
1374     (let ((tail (process-list)))
1375       (while tail
1376         (let* ((p (car tail))
1377                (pid (process-id p))
1378                (s (process-status p)))
1379           (setq tail (cdr tail))
1380           (princ (format "%-13s" (process-name p)))
1381           ;;(if (and (eq system-type 'vax-vms)
1382           ;;         (eq s 'signal)
1383           ;;        (< (process-exit-status p) NSIG))
1384           ;;    (princ (aref sys_errlist (process-exit-status p))))
1385           (princ s)
1386           (if (and (eq s 'exit) (/= (process-exit-status p) 0))
1387               (princ (format " %d" (process-exit-status p))))
1388           (if (memq s '(signal exit closed))
1389               ;; Do delete-exited-processes' work
1390               (delete-process p))
1391           (indent-to 22 1)              ;####
1392           (let ((b (process-buffer p)))
1393             (cond ((not b)
1394                    (princ "(none)"))
1395                   ((not (buffer-name b))
1396                    (princ "(killed)"))
1397                   (t
1398                    (princ (buffer-name b)))))
1399           (indent-to 37 1)              ;####
1400           (let ((tn (process-tty-name p)))
1401             (cond ((not tn)
1402                    (princ "(none)"))
1403                   (t
1404                    (princ (format "%s" tn)))))
1405           (indent-to 49 1)              ;####
1406           (if (not (integerp pid))
1407               (progn
1408                 (princ "network stream connection ")
1409                 (princ (car pid))
1410                 (princ "@")
1411                 (princ (cdr pid)))
1412             (let ((cmd (process-command p)))
1413               (while cmd
1414                 (princ (car cmd))
1415                 (setq cmd (cdr cmd))
1416                 (if cmd (princ " ")))))
1417           (terpri))))))
1418
1419 ;; Stop gap for 21.0 untill we do help-char etc properly.
1420 (defun help-keymap-with-help-key (keymap form)
1421   "Return a copy of KEYMAP with an help-key binding according to help-char
1422  invoking FORM like help-form.  An existing binding is not overridden.
1423  If FORM is nil then no binding is made."
1424   (let ((map (copy-keymap keymap))
1425         (key (if (characterp help-char)
1426                  (vector (character-to-event help-char))
1427                help-char)))
1428     (when (and form key (not (lookup-key map key)))
1429       (define-key map key
1430         `(lambda () (interactive) (help-print-help-form ,form))))
1431     map))
1432
1433 (defun help-print-help-form (form)
1434   (let ((string (eval form)))
1435     (if (stringp string)
1436         (with-displaying-help-buffer
1437          (insert string)))))
1438
1439
1440 ;;; help.el ends here