1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
6 ;; Keywords: help, internal
8 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; This code implements GNU Emac's on-line help system, the one invoked by
27 ;;`M-x help-for-help'.
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
))
35 (defvar help-map
(make-sparse-keymap)
36 "Keymap for characters following the Help key.")
38 (defvar help-mode-map
(make-sparse-keymap)
39 "Keymap for help mode.")
41 (define-key global-map
(char-to-string help-char
) 'help-command
)
42 (fset 'help-command help-map
)
44 (define-key help-map
(char-to-string help-char
) 'help-for-help
)
45 (define-key help-map
"?" 'help-for-help
)
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
)
53 (define-key help-map
"b" 'describe-bindings
)
55 (define-key help-map
"c" 'describe-key-briefly
)
56 (define-key help-map
"k" 'describe-key
)
58 (define-key help-map
"d" 'describe-function
)
59 (define-key help-map
"f" 'describe-function
)
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
)
65 (define-key help-map
"l" 'view-lossage
)
67 (define-key help-map
"m" 'describe-mode
)
69 (define-key help-map
"\C-n" 'view-emacs-news
)
70 (define-key help-map
"n" 'view-emacs-news
)
72 (define-key help-map
"p" 'finder-by-keyword
)
73 (autoload 'finder-by-keyword
"finder"
74 "Find packages matching a given keyword." t
)
76 (define-key help-map
"s" 'describe-syntax
)
78 (define-key help-map
"t" 'help-with-tutorial
)
80 (define-key help-map
"w" 'where-is
)
82 (define-key help-map
"v" 'describe-variable
)
84 (define-key help-map
"q" 'help-quit
)
87 "Major mode for viewing help text.
88 Entry to this mode runs the normal hook `help-mode-hook'.
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
))
102 (defun help-with-tutorial ()
103 "Select the Emacs learn-by-doing tutorial."
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<<")
117 (delete-region (point) (progn (end-of-line) (point)))
118 (let ((n (- (window-height (selected-window))
119 (count-lines (point-min) (point))
123 ;; Some people get confused by the large gap.
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
))))
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
)))
140 (memq 'down
(event-modifiers type
))
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
))))))
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
))
155 (cond ((or (member (buffer-name standard-output
)
156 special-display-buffer-names
)
157 (assoc (buffer-name standard-output
)
158 special-display-buffer-names
)
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
)))
168 (setq tail
(cdr tail
)))
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.
179 ((not (one-window-p t
))
180 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
182 "Type \\[delete-other-windows] to remove help window.")
184 "Type \\[switch-to-buffer] RET to remove help window."))))
185 (funcall (or function
'message
)
188 (substitute-command-keys first-message
)
190 (if first-message
" " "")
191 (substitute-command-keys
192 "\\[scroll-other-window] to scroll the help."))))))
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
)))
203 (memq 'down
(event-modifiers type
))
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*"
211 (if (documentation defn
)
212 (princ (documentation defn
))
213 (princ "not documented"))
215 (set-buffer standard-output
)
217 (print-help-return-message)))))
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."
225 (with-output-to-temp-buffer "*Help*"
226 (let ((minor-modes minor-mode-alist
)
227 (locals (buffer-local-variables)))
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
235 (if (and 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
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
))
250 (setq minor-modes
(cdr minor-modes
))))
253 (princ (documentation major-mode
))
255 (set-buffer standard-output
)
257 (print-help-return-message)))
259 ;; So keyboard macro definitions are documented correctly
260 (fset 'defining-kbd-macro
(symbol-function 'start-kbd-macro
))
262 (defun describe-distribution ()
263 "Display info on how to obtain the latest version of GNU Emacs."
266 (expand-file-name "DISTRIB" data-directory
)))
268 (defun describe-copying ()
269 "Display info on how you may redistribute copies of GNU Emacs."
272 (expand-file-name "COPYING" data-directory
))
273 (goto-char (point-min)))
275 (defun describe-project ()
276 "Display info on the GNU project."
279 (expand-file-name "GNU" data-directory
))
280 (goto-char (point-min)))
282 (defun describe-no-warranty ()
283 "Display info on all the kinds of warranty Emacs does NOT have."
286 (let (case-fold-search)
287 (search-forward "NO WARRANTY")
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."
295 (let* ((key (this-command-keys))
296 (prefix (make-vector (1- (length key
)) nil
))
299 (while (< i
(length prefix
))
300 (aset prefix i
(aref key 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
)
307 (defun view-emacs-news ()
308 "Display info on recent changes to Emacs."
310 (find-file-read-only (expand-file-name "NEWS" data-directory
)))
312 (defun view-lossage ()
313 "Display last 100 input keystrokes."
315 (with-output-to-temp-buffer "*Help*"
316 (princ (mapconcat (function (lambda (key)
317 (if (or (integerp key
)
320 (single-key-description key
)
321 (prin1-to-string key nil
))))
325 (set-buffer standard-output
)
326 (goto-char (point-min))
327 (while (progn (move-to-column 50) (not (eobp)))
328 (search-forward " " nil t
)
331 (print-help-return-message)))
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 SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
339 a command-apropos. Give a substring, and see a list of commands
340 (functions interactively callable) that contain
341 that substring. See also the apropos command.
342 b describe-bindings. Display table of all key bindings.
343 c describe-key-briefly. Type a command key sequence;
344 it prints the function name that sequence runs.
345 f describe-function. Type a function name and get documentation of it.
346 C-f Info-goto-emacs-command-node. Type a function name;
347 it takes you to the Info node for that command.
348 i info. The info documentation reader.
349 k describe-key. Type a command key sequence;
350 it displays the full documentation.
351 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
352 it takes you to the Info node for the command bound to that key.
353 l view-lossage. Shows last 100 characters you typed.
354 m describe-mode. Print documentation of current major mode,
355 which describes the commands peculiar to it.
356 n view-emacs-news. Shows emacs news file.
357 p finder-by-keyword. Find packages matching a given topic keyword.
358 s describe-syntax. Display contents of syntax table, plus explanations
359 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
360 v describe-variable. Type name of a variable;
361 it displays the variable's documentation and value.
362 w where-is. Type command name; it prints which keystrokes
364 C-c print Emacs copying permission (General Public License).
365 C-d print Emacs ordering information.
366 C-n print news of recent Emacs changes.
367 C-p print information about the GNU project.
368 C-w print information on absence of warranty for GNU Emacs."
371 ;; Return a function which is called by the list containing point.
372 ;; If that gives no function, return a function whose name is around point.
373 ;; If that doesn't give a function, return nil.
374 (defun function-called-at-point ()
375 (or (condition-case ()
378 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
382 (setq obj
(read (current-buffer)))
383 (and (symbolp obj
) (fboundp obj
) obj
))))
388 (skip-chars-forward "'")
389 (let ((obj (read (current-buffer))))
390 (and (symbolp obj
) (fboundp obj
) obj
)))
393 (defun describe-function-find-file (function)
394 (let ((files load-history
)
397 (if (memq function
(cdr (car files
)))
398 (setq file
(car (car files
)) files nil
))
399 (setq files
(cdr files
)))
402 (defun describe-function (function)
403 "Display the full documentation of FUNCTION (a symbol)."
405 (let ((fn (function-called-at-point))
406 (enable-recursive-minibuffers t
)
408 (setq val
(completing-read (if fn
409 (format "Describe function (default %s): " fn
)
410 "Describe function: ")
412 (list (if (equal val
"")
414 (with-output-to-temp-buffer "*Help*"
417 (let* ((def (symbol-function function
))
418 (beg (if (commandp def
) "an interactive " "a ")))
419 (princ (cond ((or (stringp def
)
423 (concat beg
"built-in function"))
424 ((byte-code-function-p def
)
425 (concat beg
"compiled Lisp function"))
427 (format "alias for `%s'" def
))
428 ((eq (car-safe def
) 'lambda
)
429 (concat beg
"Lisp function"))
430 ((eq (car-safe def
) 'macro
)
432 ((eq (car-safe def
) 'mocklisp
)
433 "a mocklisp function")
434 ((eq (car-safe def
) 'autoload
)
435 (format "%s autoloaded Lisp %s"
436 (if (commandp def
) "an interactive" "an")
437 (if (nth 4 def
) "macro" "function")
438 ;;; Including the file name made this line too long.
442 (let ((file (describe-function-find-file function
)))
446 ;; We used to add .el to the file name,
447 ;; but that's completely wrong when the user used load-file.
452 (let ((arglist (cond ((byte-code-function-p def
)
453 (car (append def nil
)))
454 ((eq (car-safe def
) 'lambda
)
459 (princ (cons function
460 (mapcar (lambda (arg)
461 (if (memq arg
'(&optional
&rest
))
463 (intern (upcase (symbol-name arg
)))))
466 (if (documentation function
)
468 (princ (documentation function
)))
469 (princ "not documented"))
471 (print-help-return-message)
473 (set-buffer standard-output
)
475 ;; Return the text we displayed.
478 (defun variable-at-point ()
482 (skip-chars-forward "'")
483 (let ((obj (read (current-buffer))))
484 (and (symbolp obj
) (boundp obj
) obj
)))
487 (defun describe-variable (variable)
488 "Display the full documentation of VARIABLE (a symbol).
489 Returns the documentation as a string, also."
491 (let ((v (variable-at-point))
492 (enable-recursive-minibuffers t
)
494 (setq val
(completing-read (if v
495 (format "Describe variable (default %s): " v
)
496 "Describe variable: ")
498 (list (if (equal val
"")
500 (with-output-to-temp-buffer "*Help*"
502 (princ "'s value is ")
503 (if (not (boundp variable
))
505 (prin1 (symbol-value variable
)))
507 (let ((locals (buffer-local-variables))
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
)))
515 (princ (format "Local in buffer %s\n" (buffer-name)))))
517 (princ "Documentation:")
519 (let ((doc (documentation-property variable
'variable-documentation
)))
521 (princ (substitute-command-keys doc
))
522 (princ "not documented as a variable.")))
523 (print-help-return-message)
525 (set-buffer standard-output
)
527 ;; Return the text we displayed.
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."
534 (let ((fn (function-called-at-point))
535 (enable-recursive-minibuffers t
)
537 (setq val
(completing-read (if fn
538 (format "Where is command (default %s): " fn
)
539 "Where is command: ")
541 (list (if (equal 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
)))
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): ")
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
)))))
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: ")
574 (let ((try (expand-file-name (concat library suf
) dir
)))
575 (and (file-readable-p try
)
576 (null (file-directory-p try
))
578 (message "Library is file %s" try
)
579 (throw 'answer try
)))))
580 (if nosuffix
'("") '(".elc" ".el" ""))))
582 (message "No library %s in search path" library
)
585 ;;; help.el ends here