XEmacs 21.2.22 "Mercedes".
[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       (if help-buffer-prefix-string
465           (format "*%s: %s*" help-buffer-prefix-string name)
466         (format "*%s*" name))
467     (format "*%s*" help-buffer-prefix-string)))
468
469 ;; Use this function for displaying help when C-h something is pressed
470 ;; or in similar situations.  Do *not* use it when you are displaying
471 ;; a help message and then prompting for input in the minibuffer --
472 ;; this macro usually selects the help buffer, which is not what you
473 ;; want in those situations.
474
475 ;; #### Should really be a macro to eliminate the requirement of
476 ;; caller to code a lambda form in THUNK -- mrb
477
478 ;; #### BEFORE you rush to make this a macro, think about backward
479 ;; compatibility.  The right way would be to create a macro with
480 ;; another name (which is a shame, because w-d-h-b is a perfect name
481 ;; for a macro) that uses with-displaying-help-buffer internally.
482
483 (defun with-displaying-help-buffer (thunk &optional name)
484   "Form which makes a help buffer with given NAME and evaluates BODY there.
485 The actual name of the buffer is generated by the function `help-buffer-name'."
486   (let* ((winconfig (current-window-configuration))
487          (was-one-window (one-window-p))
488          (buffer-name (help-buffer-name name))
489          (help-not-visible
490           (not (and (windows-of-buffer buffer-name) ;shortcut
491                     (memq (selected-frame)
492                           (mapcar 'window-frame
493                                   (windows-of-buffer buffer-name)))))))
494     (help-register-and-maybe-prune-excess buffer-name)
495     (prog1 (with-output-to-temp-buffer buffer-name
496              (prog1 (funcall thunk)
497                (save-excursion
498                  (set-buffer standard-output)
499                  (help-mode))))
500       (let ((helpwin (get-buffer-window buffer-name)))
501         (when helpwin
502           (with-current-buffer (window-buffer helpwin)
503             ;; If the *Help* buffer is already displayed on this
504             ;; frame, don't override the previous configuration
505             (when help-not-visible
506               (set-frame-property (selected-frame)
507                                   'help-window-config winconfig)))
508           (when help-selects-help-window
509             (select-window helpwin))
510           (cond ((eq helpwin (selected-window))
511                  (display-message 'command
512                    (substitute-command-keys "Type \\[help-mode-quit] to remove help window, \\[scroll-up] to scroll the help.")))
513                 (was-one-window
514                  (display-message 'command
515                    (substitute-command-keys "Type \\[delete-other-windows] to remove help window, \\[scroll-other-window] to scroll the help.")))
516                 (t
517                  (display-message 'command
518                    (substitute-command-keys "Type \\[switch-to-buffer-other-window] to restore the other window, \\[scroll-other-window] to scroll the help.")))))))))
519
520 (defun describe-key (key)
521   "Display documentation of the function invoked by KEY.
522 KEY is a string, or vector of events.
523 When called interactively, KEY may also be a menu selection."
524   (interactive "kDescribe key: ")
525   (let ((defn (key-or-menu-binding key))
526         (key-string (key-description key)))
527     (if (or (null defn) (integerp defn))
528         (message "%s is undefined" key-string)
529       (with-displaying-help-buffer
530        (lambda ()
531          (princ key-string)
532          (princ " runs ")
533          (if (symbolp defn)
534              (princ (format "`%s'" defn))
535            (princ defn))
536          (princ "\n\n")
537          (cond ((or (stringp defn) (vectorp defn))
538                 (let ((cmd (key-binding defn)))
539                   (if (not cmd)
540                       (princ "a keyboard macro")
541                     (progn
542                       (princ "a keyboard macro which runs the command ")
543                       (princ cmd)
544                       (princ ":\n\n")
545                       (if (documentation cmd) (princ (documentation cmd)))))))
546                ((and (consp defn) (not (eq 'lambda (car-safe defn))))
547                 (let ((describe-function-show-arglist nil))
548                   (describe-function-1 (car defn))))
549                ((symbolp defn)
550                 (describe-function-1 defn))
551                ((documentation defn)
552                 (princ (documentation defn)))
553                (t
554                 (princ "not documented"))))
555        (format "key `%s'" key-string)))))
556
557 (defun describe-mode ()
558   "Display documentation of current major mode and minor modes.
559 For this to work correctly for a minor mode, the mode's indicator variable
560 \(listed in `minor-mode-alist') must also be a function whose documentation
561 describes the minor mode."
562   (interactive)
563   (with-displaying-help-buffer
564    (lambda ()
565      ;; XEmacs change: print the major-mode documentation before
566      ;; the minor modes.
567      (princ mode-name)
568      (princ " mode:\n")
569      (princ (documentation major-mode))
570      (princ "\n\n----\n\n")
571      (let ((minor-modes minor-mode-alist))
572        (while minor-modes
573          (let* ((minor-mode (car (car minor-modes)))
574                 (indicator (car (cdr (car minor-modes)))))
575            ;; Document a minor mode if it is listed in minor-mode-alist,
576            ;; bound locally in this buffer, non-nil, and has a function
577            ;; definition.
578            (if (and (boundp minor-mode)
579                     (symbol-value minor-mode)
580                     (fboundp minor-mode))
581                (let ((pretty-minor-mode minor-mode))
582                  (if (string-match "-mode\\'" (symbol-name minor-mode))
583                      (setq pretty-minor-mode
584                            (capitalize
585                             (substring (symbol-name minor-mode)
586                                        0 (match-beginning 0)))))
587                  (while (and (consp indicator) (extentp (car indicator)))
588                    (setq indicator (cdr indicator)))
589                  (while (and indicator (symbolp indicator))
590                    (setq indicator (symbol-value indicator)))
591                  (princ (format "%s minor mode (indicator%s):\n"
592                                 pretty-minor-mode indicator))
593                  (princ (documentation minor-mode))
594                  (princ "\n\n----\n\n"))))
595          (setq minor-modes (cdr minor-modes)))))
596    (format "%s mode" mode-name)))
597
598 ;; So keyboard macro definitions are documented correctly
599 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
600
601 (defun describe-distribution ()
602   "Display info on how to obtain the latest version of XEmacs."
603   (interactive)
604   (find-file-read-only
605    (locate-data-file "DISTRIB")))
606
607 (defun describe-beta ()
608   "Display info on how to deal with Beta versions of XEmacs."
609   (interactive)
610   (find-file-read-only
611    (locate-data-file "BETA"))
612   (goto-char (point-min)))
613
614 (defun describe-copying ()
615   "Display info on how you may redistribute copies of XEmacs."
616   (interactive)
617   (find-file-read-only
618    (locate-data-file "COPYING"))
619   (goto-char (point-min)))
620
621 (defun describe-pointer ()
622   "Show a list of all defined mouse buttons, and their definitions."
623   (interactive)
624   (describe-bindings nil t))
625
626 (defun describe-project ()
627   "Display info on the GNU project."
628   (interactive)
629   (find-file-read-only
630    (locate-data-file "GNU"))
631   (goto-char (point-min)))
632
633 (defun describe-no-warranty ()
634   "Display info on all the kinds of warranty XEmacs does NOT have."
635   (interactive)
636   (describe-copying)
637   (let (case-fold-search)
638     (search-forward "NO WARRANTY")
639     (recenter 0)))
640
641 (defun describe-bindings (&optional prefix mouse-only-p)
642   "Show a list of all defined keys, and their definitions.
643 The list is put in a buffer, which is displayed.
644 If the optional argument PREFIX is supplied, only commands which
645 start with that sequence of keys are described.
646 If the second argument (prefix arg, interactively) is non-null
647 then only the mouse bindings are displayed."
648   (interactive (list nil current-prefix-arg))
649   (with-displaying-help-buffer
650    (lambda ()
651      (describe-bindings-1 prefix mouse-only-p))
652    (format "bindings for %s" major-mode)))
653
654 (defun describe-bindings-1 (&optional prefix mouse-only-p)
655   (let ((heading (if mouse-only-p
656             (gettext "button          binding\n------          -------\n")
657             (gettext "key             binding\n---             -------\n")))
658         (buffer (current-buffer))
659         (minor minor-mode-map-alist)
660         (local (current-local-map))
661         (shadow '()))
662     (set-buffer standard-output)
663     (while minor
664       (let ((sym (car (car minor)))
665             (map (cdr (car minor))))
666         (if (symbol-value-in-buffer sym buffer nil)
667             (progn
668               (insert (format "Minor Mode Bindings for `%s':\n"
669                               sym)
670                       heading)
671               (describe-bindings-internal map nil shadow prefix mouse-only-p)
672               (insert "\n")
673               (setq shadow (cons map shadow))))
674         (setq minor (cdr minor))))
675     (if local
676         (progn
677           (insert "Local Bindings:\n" heading)
678           (describe-bindings-internal local nil shadow prefix mouse-only-p)
679           (insert "\n")
680           (setq shadow (cons local shadow))))
681     (insert "Global Bindings:\n" heading)
682     (describe-bindings-internal (current-global-map)
683                                 nil shadow prefix mouse-only-p)
684     (when (and prefix function-key-map (not mouse-only-p))
685       (insert "\nFunction key map translations:\n" heading)
686       (describe-bindings-internal function-key-map nil nil
687                                   prefix mouse-only-p))
688     (set-buffer buffer)
689     standard-output))
690
691 (defun describe-prefix-bindings ()
692   "Describe the bindings of the prefix used to reach this command.
693 The prefix described consists of all but the last event
694 of the key sequence that ran this command."
695   (interactive)
696   (let* ((key (this-command-keys))
697          (prefix (make-vector (1- (length key)) nil))
698          i)
699     (setq i 0)
700     (while (< i (length prefix))
701       (aset prefix i (aref key i))
702       (setq i (1+ i)))
703     (with-displaying-help-buffer
704      (lambda ()
705        (princ "Key bindings starting with ")
706        (princ (key-description prefix))
707        (princ ":\n\n")
708        (describe-bindings-1 prefix nil))
709      (format "%s prefix" (key-description prefix)))))
710
711 ;; Make C-h after a prefix, when not specifically bound,
712 ;; run describe-prefix-bindings.
713 (setq prefix-help-command 'describe-prefix-bindings)
714
715 (defun describe-installation ()
716   "Display a buffer showing information about this XEmacs was compiled."
717   (interactive)
718   (if (and (boundp 'Installation-string)
719            (stringp Installation-string))
720       (with-displaying-help-buffer
721        (lambda ()
722          (princ Installation-string))
723        "Installation")
724     (error "No Installation information available.")))
725
726 (defun view-emacs-news ()
727   "Display info on recent changes to XEmacs."
728   (interactive)
729   (find-file (locate-data-file "NEWS")))
730
731 (defun xemacs-www-page ()
732   "Go to the XEmacs World Wide Web page."
733   (interactive)
734   (if (boundp 'browse-url-browser-function)
735       (funcall browse-url-browser-function "http://www.xemacs.org/")
736     (error "xemacs-www-page requires browse-url")))
737
738 (defun xemacs-www-faq ()
739   "View the latest and greatest XEmacs FAQ using the World Wide Web."
740   (interactive)
741   (if (boundp 'browse-url-browser-function)
742       (funcall browse-url-browser-function
743                "http://www.xemacs.org/faq/index.html")
744     (error "xemacs-www-faq requires browse-url")))
745
746 (defun xemacs-local-faq ()
747   "View the local copy of the XEmacs FAQ.
748 If you have access to the World Wide Web, you should use `xemacs-www-faq'
749 instead, to ensure that you get the most up-to-date information."
750   (interactive)
751   (save-window-excursion
752     (info)
753     (Info-find-node "xemacs-faq" "Top"))
754   (switch-to-buffer "*info*"))
755
756 (defcustom view-lossage-key-count 100
757   "*Number of keys `view-lossage' shows.
758 The maximum number of available keys is governed by `recent-keys-ring-size'."
759   :type 'integer
760   :group 'help)
761
762 (defcustom view-lossage-message-count 100
763   "*Number of minibuffer messages `view-lossage' shows."
764   :type 'integer
765   :group 'help)
766
767 (defun print-recent-messages (n)
768   "Print N most recent messages to standard-output, most recent first.
769 If N is nil, all messages will be printed."
770   (save-excursion
771     (let ((buffer (get-buffer-create " *Message-Log*"))
772           oldpoint extent)
773       (goto-char (point-max buffer) buffer)
774       (set-buffer standard-output)
775       (while (and (not (bobp buffer))
776                   (or (null n) (>= (decf n) 0)))
777         (setq oldpoint (point buffer))
778         (setq extent (extent-at oldpoint buffer
779                                 'message-multiline nil 'before))
780         ;; If the message was multiline, move all the way to the
781         ;; beginning.
782         (if extent
783             (goto-char (extent-start-position extent) buffer)
784           (forward-line -1 buffer))
785         (insert-buffer-substring buffer (point buffer) oldpoint)))))
786
787 (defun view-lossage ()
788   "Display recent input keystrokes and recent minibuffer messages.
789 The number of keys shown is controlled by `view-lossage-key-count'.
790 The number of messages shown is controlled by `view-lossage-message-count'."
791   (interactive)
792   (with-displaying-help-buffer
793    (lambda ()
794      (princ (key-description (recent-keys view-lossage-key-count)))
795      (save-excursion
796        (set-buffer standard-output)
797        (goto-char (point-min))
798        (insert "Recent keystrokes:\n\n")
799        (while (progn (move-to-column 50) (not (eobp)))
800          (search-forward " " nil t)
801          (insert "\n")))
802      ;; XEmacs addition: copy the messages from " *Message-Log*",
803      ;; reversing their order and handling multiline messages
804      ;; correctly.
805      (princ "\n\n\nRecent minibuffer messages (most recent first):\n\n")
806      (print-recent-messages view-lossage-message-count))
807    "lossage"))
808
809 (define-function 'help 'help-for-help)
810
811 (make-help-screen help-for-help
812   "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:"
813   "Type a Help option:
814 \(Use SPC or DEL to scroll through this text.  Type \\<help-map>\\[help-quit] to exit the Help command.)
815
816 \\[hyper-apropos]       Type a substring; it shows a hypertext list of
817         functions and variables that contain that substring.
818         See also the `apropos' command.
819 \\[command-apropos]     Type a substring; it shows a list of commands
820         (interactively callable functions) that contain that substring.
821 \\[describe-bindings]   Table of all key bindings.
822 \\[describe-key-briefly]        Type a command key sequence;
823         it displays the function name that sequence runs.
824 \\[customize]   Customize Emacs options.
825 \\[Info-goto-emacs-command-node]        Type a function name; it displays the Info node for that command.
826 \\[describe-function]   Type a function name; it shows its documentation.
827 \\[Info-elisp-ref]      Type a function name; it jumps to the full documentation
828         in the XEmacs Lisp Programmer's Manual.
829 \\[xemacs-local-faq]    Local copy of the XEmacs FAQ.
830 \\[info]        Info documentation reader.
831 \\[Info-query]  Type an Info file name; it displays it in Info reader.
832 \\[describe-key]        Type a command key sequence;
833         it displays the documentation for the command bound to that key.
834 \\[Info-goto-emacs-key-command-node]    Type a command key sequence;
835         it displays the Info node for the command bound to that key.
836 \\[view-lossage]        Recent input keystrokes and minibuffer messages.
837 \\[describe-mode]       Documentation of current major and minor modes.
838 \\[view-emacs-news]     News of recent XEmacs changes.
839 \\[finder-by-keyword]   Type a topic keyword; it finds matching packages.
840 \\[describe-pointer]    Table of all mouse-button bindings.
841 \\[describe-syntax]     Contents of syntax table with explanations.
842 \\[help-with-tutorial]  XEmacs learn-by-doing tutorial.
843 \\[describe-variable]   Type a variable name; it displays its documentation and value.
844 \\[where-is]    Type a command name; it displays which keystrokes invoke that command.
845 \\[describe-distribution]       XEmacs ordering information.
846 \\[describe-no-warranty]        Information on absence of warranty for XEmacs.
847 \\[describe-copying]    XEmacs copying permission (General Public License)."
848   help-map)
849
850 (defmacro with-syntax-table (syntab &rest body)
851   "Evaluate BODY with the syntax-table SYNTAB"
852   `(let ((stab (syntax-table)))
853      (unwind-protect
854          (progn
855            (set-syntax-table (copy-syntax-table ,syntab))
856            ,@body)
857        (set-syntax-table stab))))
858 (put 'with-syntax-table 'lisp-indent-function 1)
859 (put 'with-syntax-table 'edebug-form-spec '(form body))
860
861 (defun function-called-at-point ()
862   "Return the function which is called by the list containing point.
863 If that gives no function, return the function whose name is around point.
864 If that doesn't give a function, return nil."
865   (or (ignore-errors
866         (save-excursion
867           (save-restriction
868             (narrow-to-region (max (point-min) (- (point) 1000))
869                               (point-max))
870             (backward-up-list 1)
871             (forward-char 1)
872             (let (obj)
873               (setq obj (read (current-buffer)))
874               (and (symbolp obj) (fboundp obj) obj)))))
875       (ignore-errors
876         (with-syntax-table emacs-lisp-mode-syntax-table
877           (save-excursion
878             (or (not (zerop (skip-syntax-backward "_w")))
879                 (eq (char-syntax (char-after (point))) ?w)
880                 (eq (char-syntax (char-after (point))) ?_)
881                 (forward-sexp -1))
882             (skip-chars-forward "`'")
883             (let ((obj (read (current-buffer))))
884               (and (symbolp obj) (fboundp obj) obj)))))))
885
886 (defun function-at-point ()
887   "Return the function whose name is around point.
888 If that gives no function, return the function which is called by the
889 list containing point.  If that doesn't give a function, return nil."
890   (or (ignore-errors
891         (with-syntax-table emacs-lisp-mode-syntax-table
892           (save-excursion
893             (or (not (zerop (skip-syntax-backward "_w")))
894                 (eq (char-syntax (char-after (point))) ?w)
895                 (eq (char-syntax (char-after (point))) ?_)
896                 (forward-sexp -1))
897             (skip-chars-forward "`'")
898             (let ((obj (read (current-buffer))))
899               (and (symbolp obj) (fboundp obj) obj)))))
900       (ignore-errors
901         (save-excursion
902           (save-restriction
903             (narrow-to-region (max (point-min) (- (point) 1000))
904                               (point-max))
905             (backward-up-list 1)
906             (forward-char 1)
907             (let (obj)
908               (setq obj (read (current-buffer)))
909               (and (symbolp obj) (fboundp obj) obj)))))))
910
911 ;; Default to nil for the non-hackers?  Not until we find a way to
912 ;; distinguish hackers from non-hackers automatically!
913 (defcustom describe-function-show-arglist t
914   "*If non-nil, describe-function will show its arglist,
915 unless the function is autoloaded."
916   :type 'boolean
917   :group 'help-appearance)
918
919 (defun describe-symbol-find-file (symbol)
920   (loop for (file . load-data) in load-history
921     do (when (memq symbol load-data)
922          (return file))))
923
924 (define-obsolete-function-alias
925   'describe-function-find-file
926   'describe-symbol-find-file)
927
928 (defun describe-function (function)
929   "Display the full documentation of FUNCTION (a symbol).
930 When run interactively, it defaults to any function found by
931 `function-at-point'."
932   (interactive
933     (let* ((fn (function-at-point))
934            (val (let ((enable-recursive-minibuffers t))
935                   (completing-read
936                     (if fn
937                         (format (gettext "Describe function (default %s): ")
938                                 fn)
939                         (gettext "Describe function: "))
940                     obarray 'fboundp t nil 'function-history))))
941       (list (if (equal val "") fn (intern val)))))
942   (with-displaying-help-buffer
943    (lambda ()
944      (describe-function-1 function)
945      ;; Return the text we displayed.
946      (buffer-string nil nil standard-output))
947     (format "function `%s'" function)))
948
949 (defun function-obsolete-p (function)
950   "Return non-nil if FUNCTION is obsolete."
951   (not (null (get function 'byte-obsolete-info))))
952
953 (defun function-obsoleteness-doc (function)
954   "If FUNCTION is obsolete, return a string describing this."
955   (let ((obsolete (get function 'byte-obsolete-info)))
956     (if obsolete
957         (format "Obsolete; %s"
958                 (if (stringp (car obsolete))
959                     (car obsolete)
960                   (format "use `%s' instead." (car obsolete)))))))
961
962 (defun function-compatible-p (function)
963   "Return non-nil if FUNCTION is present for Emacs compatibility."
964   (not (null (get function 'byte-compatible-info))))
965
966 (defun function-compatibility-doc (function)
967   "If FUNCTION is Emacs compatible, return a string describing this."
968   (let ((compatible (get function 'byte-compatible-info)))
969     (if compatible
970         (format "Emacs Compatible; %s"
971                 (if (stringp (car compatible))
972                     (car compatible)
973                   (format "use `%s' instead." (car compatible)))))))
974
975 ;Here are all the possibilities below spelled out, for the benefit
976 ;of the I18N3 snarfer.
977 ;
978 ;(gettext "a built-in function")
979 ;(gettext "an interactive built-in function")
980 ;(gettext "a built-in macro")
981 ;(gettext "an interactive built-in macro")
982 ;(gettext "a compiled Lisp function")
983 ;(gettext "an interactive compiled Lisp function")
984 ;(gettext "a compiled Lisp macro")
985 ;(gettext "an interactive compiled Lisp macro")
986 ;(gettext "a Lisp function")
987 ;(gettext "an interactive Lisp function")
988 ;(gettext "a Lisp macro")
989 ;(gettext "an interactive Lisp macro")
990 ;(gettext "a mocklisp function")
991 ;(gettext "an interactive mocklisp function")
992 ;(gettext "a mocklisp macro")
993 ;(gettext "an interactive mocklisp macro")
994 ;(gettext "an autoloaded Lisp function")
995 ;(gettext "an interactive autoloaded Lisp function")
996 ;(gettext "an autoloaded Lisp macro")
997 ;(gettext "an interactive autoloaded Lisp macro")
998
999 ;; taken out of `describe-function-1'
1000 (defun function-arglist (function)
1001   "Return a string giving the argument list of FUNCTION.
1002 For example:
1003
1004         (function-arglist 'function-arglist)
1005         => (function-arglist FUNCTION)
1006
1007 This function is used by `describe-function-1' to list function
1008 arguments in the standard Lisp style."
1009   (let* ((fndef (indirect-function function))
1010          (arglist
1011          (cond ((compiled-function-p fndef)
1012                 (compiled-function-arglist fndef))
1013                ((eq (car-safe fndef) 'lambda)
1014                 (nth 1 fndef))
1015                ((subrp fndef)
1016                 (let* ((doc (documentation function))
1017                        (args (and (string-match
1018                                    "[\n\t ]*\narguments: ?(\\(.*\\))\n?\\'"
1019                                    doc)
1020                                   (match-string 1 doc))))
1021                   ;; If there are no arguments documented for the
1022                   ;; subr, rather don't print anything.
1023                   (cond ((null args) t)
1024                         ((equal args "") nil)
1025                         (args))))
1026                (t t))))
1027     (cond ((listp arglist)
1028            (prin1-to-string
1029             (cons function (mapcar (lambda (arg)
1030                                      (if (memq arg '(&optional &rest))
1031                                          arg
1032                                        (intern (upcase (symbol-name arg)))))
1033                                    arglist))
1034             t))
1035           ((stringp arglist)
1036            (format "(%s %s)" function arglist)))))
1037
1038 (defun function-documentation (function &optional strip-arglist)
1039   "Return a string giving the documentation for FUNCTION, if any.
1040 If the optional argument STRIP-ARGLIST is non-nil, remove the arglist
1041 part of the documentation of internal subroutines."
1042   (let ((doc (condition-case nil
1043                  (or (documentation function)
1044                      (gettext "not documented"))
1045                (void-function ""))))
1046     (if (and strip-arglist
1047              (string-match "[\n\t ]*\narguments: ?(\\(.*\\))\n?\\'" doc))
1048         (setq doc (substring doc 0 (match-beginning 0))))
1049     doc))
1050
1051 (defun describe-function-1 (function &optional nodoc)
1052   "This function does the work for `describe-function'."
1053   (princ (format "`%s' is " function))
1054   (let* ((def function)
1055          aliases file-name autoload-file kbd-macro-p fndef macrop)
1056     (while (and (symbolp def) (fboundp def))
1057       (when (not (eq def function))
1058         (setq aliases
1059               (if aliases
1060                   ;; I18N3 Need gettext due to concat
1061                   (concat aliases
1062                           (format
1063                            "\n     which is an alias for `%s', "
1064                            (symbol-name def)))
1065                 (format "an alias for `%s', " (symbol-name def)))))
1066       (setq def (symbol-function def)))
1067     (if (and (fboundp 'compiled-function-annotation)
1068              (compiled-function-p def))
1069         (setq file-name (compiled-function-annotation def)))
1070     (if (eq 'macro (car-safe def))
1071         (setq fndef (cdr def)
1072               file-name (and (compiled-function-p (cdr def))
1073                              (fboundp 'compiled-function-annotation)
1074                              (compiled-function-annotation (cdr def)))
1075               macrop t)
1076       (setq fndef def))
1077     (if aliases (princ aliases))
1078     (let ((int #'(lambda (string an-p macro-p)
1079                    (princ (format
1080                            (gettext (concat
1081                                      (cond ((commandp def)
1082                                             "an interactive ")
1083                                            (an-p "an ")
1084                                            (t "a "))
1085                                      "%s"
1086                                      (if macro-p " macro" " function")))
1087                            string)))))
1088       (cond ((or (stringp def) (vectorp def))
1089              (princ "a keyboard macro.")
1090              (setq kbd-macro-p t))
1091             ((subrp fndef)
1092              (funcall int "built-in" nil macrop))
1093             ((compiled-function-p fndef)
1094              (funcall int "compiled Lisp" nil macrop))
1095             ((eq (car-safe fndef) 'lambda)
1096              (funcall int "Lisp" nil macrop))
1097             ((eq (car-safe fndef) 'mocklisp)
1098              (funcall int "mocklisp" nil macrop))
1099             ((eq (car-safe def) 'autoload)
1100              (setq autoload-file (elt def 1))
1101              (funcall int "autoloaded Lisp" t (elt def 4)))
1102             ((and (symbolp def) (not (fboundp def)))
1103              (princ "a symbol with a void (unbound) function definition."))
1104             (t
1105              nil)))
1106     (princ "\n")
1107     (if autoload-file
1108         (princ (format "  -- autoloads from \"%s\"\n" autoload-file)))
1109     (or file-name
1110         (setq file-name (describe-symbol-find-file function)))
1111     (if file-name
1112         (princ (format "  -- loaded from \"%s\"\n" file-name)))
1113 ;;     (terpri)
1114     (if describe-function-show-arglist
1115         (let ((arglist (function-arglist function)))
1116           (when arglist
1117             (princ arglist)
1118             (terpri))))
1119     (terpri)
1120     (cond (kbd-macro-p
1121            (princ "These characters are executed:\n\n\t")
1122            (princ (key-description def))
1123            (cond ((setq def (key-binding def))
1124                   (princ (format "\n\nwhich executes the command `%s'.\n\n"
1125                                  def))
1126                   (describe-function-1 def))))
1127           (nodoc nil)
1128           (t
1129            ;; tell the user about obsoleteness.
1130            ;; If the function is obsolete and is aliased, don't
1131            ;; even bother to report the documentation, as a further
1132            ;; encouragement to use the new function.
1133            (let ((obsolete (function-obsoleteness-doc function))
1134                  (compatible (function-compatibility-doc function)))
1135              (when obsolete
1136                (princ obsolete)
1137                (terpri)
1138                (terpri))
1139              (when compatible
1140                (princ compatible)
1141                (terpri)
1142                (terpri))
1143              (unless (and obsolete aliases)
1144                (let ((doc (function-documentation function t)))
1145                  (princ "Documentation:\n")
1146                  (princ doc)
1147                  (unless (or (equal doc "")
1148                              (eq ?\n (aref doc (1- (length doc)))))
1149                    (terpri)))))))))
1150
1151
1152 ;;; [Obnoxious, whining people who complain very LOUDLY on Usenet
1153 ;;; are binding this to keys.]
1154 (defun describe-function-arglist (function)
1155   (interactive (list (or (function-at-point)
1156                          (error "no function call at point"))))
1157   (message nil)
1158   (message (function-arglist function)))
1159
1160
1161 (defun variable-at-point ()
1162   (ignore-errors
1163     (with-syntax-table emacs-lisp-mode-syntax-table
1164       (save-excursion
1165         (or (not (zerop (skip-syntax-backward "_w")))
1166             (eq (char-syntax (char-after (point))) ?w)
1167             (eq (char-syntax (char-after (point))) ?_)
1168             (forward-sexp -1))
1169         (skip-chars-forward "'")
1170         (let ((obj (read (current-buffer))))
1171           (and (symbolp obj) (boundp obj) obj))))))
1172
1173 (defun variable-obsolete-p (variable)
1174   "Return non-nil if VARIABLE is obsolete."
1175   (not (null (get variable 'byte-obsolete-variable))))
1176
1177 (defun variable-obsoleteness-doc (variable)
1178   "If VARIABLE is obsolete, return a string describing this."
1179   (let ((obsolete (get variable 'byte-obsolete-variable)))
1180     (if obsolete
1181         (format "Obsolete; %s"
1182                 (if (stringp obsolete)
1183                     obsolete
1184                   (format "use `%s' instead." obsolete))))))
1185
1186 (defun variable-compatible-p (variable)
1187   "Return non-nil if VARIABLE is Emacs compatible."
1188   (not (null (get variable 'byte-compatible-variable))))
1189
1190 (defun variable-compatibility-doc (variable)
1191   "If VARIABLE is Emacs compatible, return a string describing this."
1192   (let ((compatible (get variable 'byte-compatible-variable)))
1193     (if compatible
1194         (format "Emacs Compatible; %s"
1195                 (if (stringp compatible)
1196                     compatible
1197                   (format "use `%s' instead." compatible))))))
1198
1199 (defun built-in-variable-doc (variable)
1200   "Return a string describing whether VARIABLE is built-in."
1201   (let ((type (built-in-variable-type variable)))
1202     (case type
1203       (integer "a built-in integer variable")
1204       (const-integer "a built-in constant integer variable")
1205       (boolean "a built-in boolean variable")
1206       (const-boolean "a built-in constant boolean variable")
1207       (object "a simple built-in variable")
1208       (const-object "a simple built-in constant variable")
1209       (const-specifier "a built-in constant specifier variable")
1210       (current-buffer "a built-in buffer-local variable")
1211       (const-current-buffer "a built-in constant buffer-local variable")
1212       (default-buffer "a built-in default buffer-local variable")
1213       (selected-console "a built-in console-local variable")
1214       (const-selected-console "a built-in constant console-local variable")
1215       (default-console "a built-in default console-local variable")
1216       (t
1217        (if type "an unknown type of built-in variable?"
1218          "a variable declared in Lisp")))))
1219
1220 (defun describe-variable (variable)
1221   "Display the full documentation of VARIABLE (a symbol)."
1222   (interactive
1223    (let* ((v (variable-at-point))
1224           (val (let ((enable-recursive-minibuffers t))
1225                  (completing-read
1226                    (if v
1227                        (format "Describe variable (default %s): " v)
1228                        (gettext "Describe variable: "))
1229                    obarray 'boundp t nil 'variable-history))))
1230      (list (if (equal val "") v (intern val)))))
1231   (with-displaying-help-buffer
1232    (lambda ()
1233      (let ((origvar variable)
1234            aliases)
1235        (let ((print-escape-newlines t))
1236          (princ (format "`%s' is " (symbol-name variable)))
1237          (while (variable-alias variable)
1238            (let ((newvar (variable-alias variable)))
1239              (if aliases
1240                  ;; I18N3 Need gettext due to concat
1241                  (setq aliases
1242                        (concat aliases
1243                                (format "\n     which is an alias for `%s',"
1244                                        (symbol-name newvar))))
1245                (setq aliases
1246                      (format "an alias for `%s',"
1247                              (symbol-name newvar))))
1248              (setq variable newvar)))
1249          (if aliases
1250              (princ (format "%s" aliases)))
1251          (princ (built-in-variable-doc variable))
1252          (princ ".\n")
1253          (let ((file-name (describe-symbol-find-file variable)))
1254            (if file-name
1255                (princ (format "  -- loaded from \"%s\"\n" file-name))))
1256          (princ "\nValue: ")
1257          (if (not (boundp variable))
1258              (princ "void\n")
1259            (prin1 (symbol-value variable))
1260            (terpri))
1261          (terpri)
1262          (cond ((local-variable-p variable (current-buffer))
1263                 (let* ((void (cons nil nil))
1264                        (def (condition-case nil
1265                                 (default-value variable)
1266                               (error void))))
1267                   (princ "This value is specific to the current buffer.\n")
1268                   (if (local-variable-p variable nil)
1269                       (princ "(Its value is local to each buffer.)\n"))
1270                   (terpri)
1271                   (if (if (eq def void)
1272                           (boundp variable)
1273                         (not (eq (symbol-value variable) def)))
1274                       ;; #### I18N3 doesn't localize properly!
1275                       (progn (princ "Default-value: ")
1276                              (if (eq def void)
1277                                  (princ "void\n")
1278                                (prin1 def)
1279                                (terpri))
1280                              (terpri)))))
1281                ((local-variable-p variable (current-buffer) t)
1282                 (princ "Setting it would make its value buffer-local.\n\n"))))
1283        (princ "Documentation:")
1284        (terpri)
1285        (let ((doc (documentation-property variable 'variable-documentation))
1286              (obsolete (variable-obsoleteness-doc origvar))
1287              (compatible (variable-compatibility-doc origvar)))
1288          (when obsolete
1289            (princ obsolete)
1290            (terpri)
1291            (terpri))
1292          (when compatible
1293            (princ compatible)
1294            (terpri)
1295            (terpri))
1296          ;; don't bother to print anything if variable is obsolete and aliased.
1297          (when (or (not obsolete) (not aliases))
1298            (if doc
1299                ;; note: documentation-property calls substitute-command-keys.
1300                (princ doc)
1301              (princ "not documented as a variable."))))
1302        (terpri)))
1303    (format "variable `%s'" variable)))
1304
1305 (defun sorted-key-descriptions (keys &optional separator)
1306   "Sort and separate the key descriptions for KEYS.
1307 The sorting is done by length (shortest bindings first), and the bindings
1308 are separated with SEPARATOR (\", \" by default)."
1309   (mapconcat 'key-description
1310              (sort keys #'(lambda (x y)
1311                             (< (length x) (length y))))
1312              (or separator ", ")))
1313
1314 (defun where-is (definition &optional insert)
1315   "Print message listing key sequences that invoke specified command.
1316 Argument is a command definition, usually a symbol with a function definition.
1317 When run interactively, it defaults to any function found by
1318 `function-at-point'.
1319 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
1320   (interactive
1321    (let ((fn (function-at-point))
1322          (enable-recursive-minibuffers t)
1323          val)
1324      (setq val (read-command
1325                 (if fn (format "Where is command (default %s): " fn)
1326                   "Where is command: ")))
1327      (list (if (equal (symbol-name val) "")
1328                fn val)
1329            current-prefix-arg)))
1330   (let ((keys (where-is-internal definition)))
1331     (if keys
1332         (if insert
1333             (princ (format "%s (%s)" (sorted-key-descriptions keys)
1334                            definition) (current-buffer))
1335           (message "%s is on %s" definition (sorted-key-descriptions keys)))
1336       (if insert
1337           (princ (format (if (commandp definition) "M-x %s RET"
1338                            "M-: (%s ...)") definition) (current-buffer))
1339         (message "%s is not on any keys" definition))))
1340   nil)
1341
1342 ;; `locate-library' moved to "packages.el"
1343
1344 \f
1345 ;; Functions ported from C into Lisp in XEmacs
1346
1347 (defun describe-syntax ()
1348   "Describe the syntax specifications in the syntax table.
1349 The descriptions are inserted in a buffer, which is then displayed."
1350   (interactive)
1351   (with-displaying-help-buffer
1352    (lambda ()
1353      ;; defined in syntax.el
1354      (describe-syntax-table (syntax-table) standard-output))
1355    (format "syntax-table for %s" major-mode)))
1356
1357 (defun list-processes ()
1358   "Display a list of all processes.
1359 \(Any processes listed as Exited or Signaled are actually eliminated
1360 after the listing is made.)"
1361   (interactive)
1362   (with-output-to-temp-buffer "*Process List*"
1363     (set-buffer standard-output)
1364     (buffer-disable-undo standard-output)
1365     (make-local-variable 'truncate-lines)
1366     (setq truncate-lines t)
1367     ;;      00000000001111111111222222222233333333334444444444
1368     ;;      01234567890123456789012345678901234567890123456789
1369     ;; rewritten for I18N3.  This one should stay rewritten
1370     ;; so that the dashes will line up properly.
1371     (princ "Proc         Status   Buffer         Tty         Command\n----         ------   ------         ---         -------\n")
1372     (let ((tail (process-list)))
1373       (while tail
1374         (let* ((p (car tail))
1375                (pid (process-id p))
1376                (s (process-status p)))
1377           (setq tail (cdr tail))
1378           (princ (format "%-13s" (process-name p)))
1379           (princ s)
1380           (if (and (eq s 'exit) (/= (process-exit-status p) 0))
1381               (princ (format " %d" (process-exit-status p))))
1382           (if (memq s '(signal exit closed))
1383               ;; Do delete-exited-processes' work
1384               (delete-process p))
1385           (indent-to 22 1)              ;####
1386           (let ((b (process-buffer p)))
1387             (cond ((not b)
1388                    (princ "(none)"))
1389                   ((not (buffer-name b))
1390                    (princ "(killed)"))
1391                   (t
1392                    (princ (buffer-name b)))))
1393           (indent-to 37 1)              ;####
1394           (let ((tn (process-tty-name p)))
1395             (cond ((not tn)
1396                    (princ "(none)"))
1397                   (t
1398                    (princ (format "%s" tn)))))
1399           (indent-to 49 1)              ;####
1400           (if (not (integerp pid))
1401               (progn
1402                 (princ "network stream connection ")
1403                 (princ (car pid))
1404                 (princ "@")
1405                 (princ (cdr pid)))
1406             (let ((cmd (process-command p)))
1407               (while cmd
1408                 (princ (car cmd))
1409                 (setq cmd (cdr cmd))
1410                 (if cmd (princ " ")))))
1411           (terpri))))))
1412
1413 ;; Stop gap for 21.0 untill we do help-char etc properly.
1414 (defun help-keymap-with-help-key (keymap form)
1415   "Return a copy of KEYMAP with an help-key binding according to help-char
1416  invoking FORM like help-form.  An existing binding is not overridden.
1417  If FORM is nil then no binding is made."
1418   (let ((map (copy-keymap keymap))
1419         (key (if (characterp help-char)
1420                  (vector (character-to-event help-char))
1421                help-char)))
1422     (when (and form key (not (lookup-key map key)))
1423       (define-key map key
1424         `(lambda () (interactive) (help-print-help-form ,form))))
1425     map))
1426
1427 (defun help-print-help-form (form)
1428   (let ((string (eval form)))
1429     (if (stringp string)
1430         (with-displaying-help-buffer
1431          (insert string)))))
1432
1433
1434 ;;; help.el ends here