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