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