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