(describe-mode): Always show minor modes, at front.
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
b1fe9304 3;; Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: help, internal
e5167999 7
433ae6f6
RS
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
433ae6f6
RS
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
d9ecc911
ER
24;;; Commentary:
25
26;; This code implements GNU Emac's on-line help system, the one invoked by
27;;`M-x help-for-help'.
28
e5167999
ER
29;;; Code:
30
8aa3a187
RS
31;; Get the macro make-help-screen when this is compiled,
32;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 33(eval-when-compile (require 'help-macro))
41b8542b 34
433ae6f6
RS
35(defvar help-map (make-sparse-keymap)
36 "Keymap for characters following the Help key.")
37
e17d2fd1 38(define-key global-map (char-to-string help-char) 'help-command)
433ae6f6
RS
39(fset 'help-command help-map)
40
e17d2fd1 41(define-key help-map (char-to-string help-char) 'help-for-help)
433ae6f6
RS
42(define-key help-map "?" 'help-for-help)
43
44(define-key help-map "\C-c" 'describe-copying)
45(define-key help-map "\C-d" 'describe-distribution)
46(define-key help-map "\C-w" 'describe-no-warranty)
76766f2d 47(define-key help-map "\C-p" 'describe-project)
433ae6f6
RS
48(define-key help-map "a" 'command-apropos)
49
50(define-key help-map "b" 'describe-bindings)
51
52(define-key help-map "c" 'describe-key-briefly)
53(define-key help-map "k" 'describe-key)
54
55(define-key help-map "d" 'describe-function)
56(define-key help-map "f" 'describe-function)
57
58(define-key help-map "i" 'info)
e5d77022
JB
59(define-key help-map "\C-f" 'Info-goto-emacs-command-node)
60(define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
433ae6f6
RS
61
62(define-key help-map "l" 'view-lossage)
63
64(define-key help-map "m" 'describe-mode)
65
66(define-key help-map "\C-n" 'view-emacs-news)
67(define-key help-map "n" 'view-emacs-news)
68
06b98c51 69(define-key help-map "p" 'finder-by-keyword)
abe85b1b 70(autoload 'finder-by-keyword "finder")
06b98c51 71
433ae6f6
RS
72(define-key help-map "s" 'describe-syntax)
73
74(define-key help-map "t" 'help-with-tutorial)
75
76(define-key help-map "w" 'where-is)
77
78(define-key help-map "v" 'describe-variable)
79
2fc9d9f4
RS
80(define-key help-map "q" 'help-quit)
81
82(defun help-quit ()
83 (interactive)
84 nil)
85
433ae6f6
RS
86(defun help-with-tutorial ()
87 "Select the Emacs learn-by-doing tutorial."
88 (interactive)
89 (let ((file (expand-file-name "~/TUTORIAL")))
90 (delete-other-windows)
91 (if (get-file-buffer file)
92 (switch-to-buffer (get-file-buffer file))
93 (switch-to-buffer (create-file-buffer file))
94 (setq buffer-file-name file)
95 (setq default-directory (expand-file-name "~/"))
79058860 96 (setq buffer-auto-save-file-name nil)
1e6dacf6 97 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
433ae6f6
RS
98 (goto-char (point-min))
99 (search-forward "\n<<")
100 (beginning-of-line)
101 (delete-region (point) (progn (end-of-line) (point)))
857a1de6 102 (let ((n (- (window-height (selected-window))
433ae6f6 103 (count-lines (point-min) (point))
857a1de6 104 6)))
d0da2301 105 (if (< n 12)
857a1de6
KH
106 (newline n)
107 ;; Some people get confused by the large gap.
108 (newline (/ n 2))
109 (insert "[Middle of page left blank for didactic purposes. "
110 "Text continues below]")
111 (newline (- n (/ n 2)))))
433ae6f6
RS
112 (goto-char (point-min))
113 (set-buffer-modified-p nil))))
114
115(defun describe-key-briefly (key)
116 "Print the name of the function KEY invokes. KEY is a string."
117 (interactive "kDescribe key briefly: ")
5f296b78
RS
118 ;; If this key seq ends with a down event, discard the
119 ;; following click or drag event. Otherwise that would
120 ;; erase the message.
121 (let ((type (aref key (1- (length key)))))
122 (if (listp type) (setq type (car type)))
123 (and (symbolp type)
124 (memq 'down (event-modifiers type))
fca4b775 125 (read-event)))
433ae6f6
RS
126 (let ((defn (key-binding key)))
127 (if (or (null defn) (integerp defn))
128 (message "%s is undefined" (key-description key))
129 (message "%s runs the command %s"
130 (key-description key)
131 (if (symbolp defn) defn (prin1-to-string defn))))))
132
133(defun print-help-return-message (&optional function)
134 "Display or return message saying how to restore windows after help command.
135Computes a message and applies the optional argument FUNCTION to it.
136If FUNCTION is nil, applies `message' to it, thus printing it."
137 (and (not (get-buffer-window standard-output))
138 (funcall (or function 'message)
139 (concat
140 (substitute-command-keys
141 (if (one-window-p t)
142 (if pop-up-windows
143 "Type \\[delete-other-windows] to remove help window."
144 "Type \\[switch-to-buffer] RET to remove help window.")
145 "Type \\[switch-to-buffer-other-window] RET to restore the other window."))
146 (substitute-command-keys
147 " \\[scroll-other-window] to scroll the help.")))))
148
149(defun describe-key (key)
150 "Display documentation of the function invoked by KEY. KEY is a string."
151 (interactive "kDescribe key: ")
5f296b78
RS
152 ;; If this key seq ends with a down event, discard the
153 ;; following click or drag event. Otherwise that would
154 ;; erase the message.
155 (let ((type (aref key (1- (length key)))))
156 (if (listp type) (setq type (car type)))
157 (and (symbolp type)
158 (memq 'down (event-modifiers type))
159 (read-event)))
433ae6f6
RS
160 (let ((defn (key-binding key)))
161 (if (or (null defn) (integerp defn))
162 (message "%s is undefined" (key-description key))
163 (with-output-to-temp-buffer "*Help*"
164 (prin1 defn)
165 (princ ":\n")
166 (if (documentation defn)
167 (princ (documentation defn))
168 (princ "not documented"))
169 (print-help-return-message)))))
170
ad023904
RS
171(defun describe-mode ()
172 "Display documentation of current major mode and minor modes.
433ae6f6 173For this to work correctly for a minor mode, the mode's indicator variable
61c6b658 174\(listed in `minor-mode-alist') must also be a function whose documentation
433ae6f6 175describes the minor mode."
ad023904 176 (interactive "p")
433ae6f6 177 (with-output-to-temp-buffer "*Help*"
ad023904
RS
178 (if minor
179 (let ((minor-modes minor-mode-alist)
180 (locals (buffer-local-variables)))
181 (while minor-modes
182 (let* ((minor-mode (car (car minor-modes)))
183 (indicator (car (cdr (car minor-modes))))
184 (local-binding (assq minor-mode locals)))
185 ;; Document a minor mode if it is listed in minor-mode-alist,
186 ;; bound locally in this buffer, non-nil, and has a function
187 ;; definition.
188 (if (and local-binding
189 (cdr local-binding)
190 (fboundp minor-mode))
191 (let ((pretty-minor-mode minor-mode))
192 (if (string-match "-mode$" (symbol-name minor-mode))
193 (setq pretty-minor-mode
194 (capitalize
195 (substring (symbol-name minor-mode)
196 0 (match-beginning 0)))))
197 (while (and indicator (symbolp indicator))
198 (setq indicator (symbol-value indicator)))
199 (princ (format "%s minor mode (indicator%s):\n"
200 pretty-minor-mode indicator))
201 (princ (documentation minor-mode))
202 (princ "\n\n"))))
203 (setq minor-modes (cdr minor-modes)))))
433ae6f6 204 (princ mode-name)
ad023904 205 (princ " mode:\n")
433ae6f6 206 (princ (documentation major-mode))
433ae6f6
RS
207 (print-help-return-message)))
208
209;; So keyboard macro definitions are documented correctly
210(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
211
212(defun describe-distribution ()
213 "Display info on how to obtain the latest version of GNU Emacs."
214 (interactive)
215 (find-file-read-only
1e6dacf6 216 (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
217
218(defun describe-copying ()
219 "Display info on how you may redistribute copies of GNU Emacs."
220 (interactive)
221 (find-file-read-only
1e6dacf6 222 (expand-file-name "COPYING" data-directory))
433ae6f6
RS
223 (goto-char (point-min)))
224
76766f2d
RS
225(defun describe-project ()
226 "Display info on the GNU project."
227 (interactive)
228 (find-file-read-only
229 (expand-file-name "GNU" data-directory))
230 (goto-char (point-min)))
231
433ae6f6
RS
232(defun describe-no-warranty ()
233 "Display info on all the kinds of warranty Emacs does NOT have."
234 (interactive)
235 (describe-copying)
236 (let (case-fold-search)
237 (search-forward "NO WARRANTY")
238 (recenter 0)))
239
61c6b658 240(defun describe-prefix-bindings ()
c7cba9cb
RS
241 "Describe the bindings of the prefix used to reach this command.
242The prefix described consists of all but the last event
243of the key sequence that ran this command."
61c6b658
RS
244 (interactive)
245 (let* ((key (this-command-keys))
246 (prefix (make-vector (1- (length key)) nil))
247 i)
248 (setq i 0)
249 (while (< i (length prefix))
250 (aset prefix i (aref key i))
251 (setq i (1+ i)))
252 (describe-bindings prefix)))
c7cba9cb
RS
253;; Make C-h after a prefix, when not specifically bound,
254;; run describe-prefix-bindings.
61c6b658
RS
255(setq prefix-help-command 'describe-prefix-bindings)
256
433ae6f6
RS
257(defun view-emacs-news ()
258 "Display info on recent changes to Emacs."
259 (interactive)
1e6dacf6 260 (find-file-read-only (expand-file-name "NEWS" data-directory)))
433ae6f6
RS
261
262(defun view-lossage ()
263 "Display last 100 input keystrokes."
264 (interactive)
265 (with-output-to-temp-buffer "*Help*"
266 (princ (key-description (recent-keys)))
267 (save-excursion
268 (set-buffer standard-output)
269 (goto-char (point-min))
270 (while (progn (move-to-column 50) (not (eobp)))
271 (search-forward " " nil t)
272 (insert "\n")))
273 (print-help-return-message)))
274
2fc9d9f4 275(defalias 'help 'help-for-help)
41b8542b 276(make-help-screen help-for-help
224d56a3 277 "a b c f C-f i k C-k l m n p s t v w C-c C-d C-n C-w, or ? for more help:"
76766f2d 278 "You have typed \\[help-command], the help character. Type a Help option:
2fc9d9f4
RS
279\(Use \\<help-map>\\[scroll-up] or \\[scroll-down] to scroll through this text.
280Type \\<help-map>\\[help-quit] to exit the Help command.)
433ae6f6 281
21ee8c42
RM
282a command-apropos. Give a substring, and see a list of commands
283 (functions interactively callable) that contain
284 that substring. See also the apropos command.
af6a9de9
RS
285b describe-bindings. Display table of all key bindings.
286c describe-key-briefly. Type a command key sequence;
21ee8c42 287 it prints the function name that sequence runs.
af6a9de9 288f describe-function. Type a function name and get documentation of it.
21ee8c42
RM
289C-f Info-goto-emacs-command-node. Type a function name;
290 it takes you to the Info node for that command.
af6a9de9
RS
291i info. The info documentation reader.
292k describe-key. Type a command key sequence;
21ee8c42
RM
293 it displays the full documentation.
294C-k Info-goto-emacs-key-command-node. Type a command key sequence;
295 it takes you to the Info node for the command bound to that key.
af6a9de9
RS
296l view-lossage. Shows last 100 characters you typed.
297m describe-mode. Print documentation of current major mode,
21ee8c42 298 which describes the commands peculiar to it.
af6a9de9
RS
299n view-emacs-news. Shows emacs news file.
300p finder-by-keyword. Find packages matching a given topic keyword.
301s describe-syntax. Display contents of syntax table, plus explanations
302t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
303v describe-variable. Type name of a variable;
21ee8c42 304 it displays the variable's documentation and value.
af6a9de9 305w where-is. Type command name; it prints which keystrokes
21ee8c42 306 invoke that command.
433ae6f6
RS
307C-c print Emacs copying permission (General Public License).
308C-d print Emacs ordering information.
309C-n print news of recent Emacs changes.
76766f2d 310C-p print information about the GNU project.
433ae6f6 311C-w print information on absence of warranty for GNU Emacs."
41b8542b 312 help-map)
433ae6f6
RS
313
314;; Return a function which is called by the list containing point.
315;; If that gives no function, return a function whose name is around point.
316;; If that doesn't give a function, return nil.
317(defun function-called-at-point ()
318 (or (condition-case ()
319 (save-excursion
320 (save-restriction
321 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
322 (backward-up-list 1)
323 (forward-char 1)
324 (let (obj)
325 (setq obj (read (current-buffer)))
326 (and (symbolp obj) (fboundp obj) obj))))
327 (error nil))
328 (condition-case ()
329 (save-excursion
330 (forward-sexp -1)
331 (skip-chars-forward "'")
332 (let ((obj (read (current-buffer))))
333 (and (symbolp obj) (fboundp obj) obj)))
334 (error nil))))
335
336(defun describe-function (function)
337 "Display the full documentation of FUNCTION (a symbol)."
338 (interactive
339 (let ((fn (function-called-at-point))
340 (enable-recursive-minibuffers t)
341 val)
342 (setq val (completing-read (if fn
343 (format "Describe function (default %s): " fn)
344 "Describe function: ")
345 obarray 'fboundp t))
346 (list (if (equal val "")
347 fn (intern val)))))
348 (with-output-to-temp-buffer "*Help*"
349 (prin1 function)
350 (princ ": ")
351 (let* ((def (symbol-function function))
352 (beg (if (commandp def) "an interactive " "a ")))
7f26b180
RM
353 (princ (cond ((or (stringp def)
354 (vectorp def))
355 "a keyboard macro.")
433ae6f6
RS
356 ((subrp def)
357 (concat beg "built-in function."))
dbc4e1c1 358 ((byte-code-function-p def)
433ae6f6
RS
359 (concat beg "compiled Lisp function."))
360 ((symbolp def)
361 (format "alias for `%s'." def))
362 ((eq (car-safe def) 'lambda)
363 (concat beg "Lisp function."))
364 ((eq (car-safe def) 'macro)
365 "a Lisp macro.")
366 ((eq (car-safe def) 'mocklisp)
367 "a mocklisp function.")
368 ((eq (car-safe def) 'autoload)
762435dd
RS
369 (format "%s autoloaded Lisp %s."
370 (if (commandp def) "an interactive" "an")
433ae6f6 371 (if (nth 4 def) "macro" "function")
ab67260b
RS
372;;; Including the file name made this line too long.
373;;; (nth 1 def)
374 ))
433ae6f6 375 (t "")))
4591cb90 376 (terpri)
7f26b180
RM
377 (let ((arglist (cond ((byte-code-function-p def)
378 (car (append def nil)))
379 ((eq (car-safe def) 'lambda)
380 (nth 1 def))
381 (t t))))
382 (if (listp arglist)
383 (progn
384 (princ (cons function
385 (mapcar (lambda (arg)
386 (if (memq arg '(&optional &rest))
387 arg
388 (intern (upcase (symbol-name arg)))))
389 arglist)))
390 (terpri))))
4591cb90 391 (if (documentation function)
cdd672cc
RS
392 (progn (terpri)
393 (princ (documentation function)))
4591cb90 394 (princ "not documented"))
7f26b180 395 )
433ae6f6
RS
396 (print-help-return-message)
397 ;; Return the text we displayed.
398 (save-excursion (set-buffer standard-output) (buffer-string))))
399
400(defun variable-at-point ()
401 (condition-case ()
402 (save-excursion
403 (forward-sexp -1)
404 (skip-chars-forward "'")
405 (let ((obj (read (current-buffer))))
406 (and (symbolp obj) (boundp obj) obj)))
407 (error nil)))
408
409(defun describe-variable (variable)
410 "Display the full documentation of VARIABLE (a symbol).
411Returns the documentation as a string, also."
412 (interactive
413 (let ((v (variable-at-point))
414 (enable-recursive-minibuffers t)
415 val)
416 (setq val (completing-read (if v
417 (format "Describe variable (default %s): " v)
418 "Describe variable: ")
419 obarray 'boundp t))
420 (list (if (equal val "")
421 v (intern val)))))
422 (with-output-to-temp-buffer "*Help*"
423 (prin1 variable)
424 (princ "'s value is ")
425 (if (not (boundp variable))
426 (princ "void.")
427 (prin1 (symbol-value variable)))
428 (terpri) (terpri)
429 (princ "Documentation:")
430 (terpri)
431 (let ((doc (documentation-property variable 'variable-documentation)))
432 (if doc
433 (princ (substitute-command-keys doc))
434 (princ "not documented as a variable.")))
435 (print-help-return-message)
436 ;; Return the text we displayed.
437 (save-excursion (set-buffer standard-output) (buffer-string))))
438
54c0b967
RS
439(defun where-is (definition)
440 "Print message listing key sequences that invoke specified command.
441Argument is a command definition, usually a symbol with a function definition."
442 (interactive
443 (let ((fn (function-called-at-point))
444 (enable-recursive-minibuffers t)
445 val)
446 (setq val (completing-read (if fn
447 (format "Where is command (default %s): " fn)
448 "Where is command: ")
449 obarray 'fboundp t))
450 (list (if (equal val "")
451 fn (intern val)))))
452 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
453 (keys1 (mapconcat 'key-description keys ", ")))
454 (if (> (length keys1) 0)
455 (message "%s is on %s" definition keys1)
456 (message "%s is not on any key" definition)))
457 nil)
458
433ae6f6
RS
459(defun command-apropos (string)
460 "Like apropos but lists only symbols that are names of commands
461\(interactively callable functions). Argument REGEXP is a regular expression
462that is matched against command symbol names. Returns list of symbols and
463documentation found."
464 (interactive "sCommand apropos (regexp): ")
465 (let ((message
466 (let ((standard-output (get-buffer-create "*Help*")))
467 (print-help-return-message 'identity))))
78e3ef3c
RS
468 (if (apropos string t 'commandp)
469 (and message (message message)))))
433ae6f6
RS
470
471(defun locate-library (library &optional nosuffix)
472 "Show the full path name of Emacs library LIBRARY.
473This command searches the directories in `load-path' like `M-x load-library'
474to find the file that `M-x load-library RET LIBRARY RET' would load.
475Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
476to the specified name LIBRARY (a la calling `load' instead of `load-library')."
477 (interactive "sLocate library: ")
478 (catch 'answer
479 (mapcar
480 '(lambda (dir)
481 (mapcar
482 '(lambda (suf)
483 (let ((try (expand-file-name (concat library suf) dir)))
484 (and (file-readable-p try)
485 (null (file-directory-p try))
486 (progn
487 (message "Library is file %s" try)
488 (throw 'answer try)))))
489 (if nosuffix '("") '(".elc" ".el" ""))))
490 load-path)
491 (message "No library %s in search path" library)
492 nil))
1a06eabd
ER
493
494;;; help.el ends here