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