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 (define-key global-map
(char-to-string help-char
) 'help-command
)
39 (fset 'help-command help-map
)
41 (define-key help-map
(char-to-string help-char
) 'help-for-help
)
42 (define-key help-map
"?" 'help-for-help
)
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
)
47 (define-key help-map
"\C-p" 'describe-project
)
48 (define-key help-map
"a" 'command-apropos
)
50 (define-key help-map
"b" 'describe-bindings
)
52 (define-key help-map
"c" 'describe-key-briefly
)
53 (define-key help-map
"k" 'describe-key
)
55 (define-key help-map
"d" 'describe-function
)
56 (define-key help-map
"f" 'describe-function
)
58 (define-key help-map
"i" 'info
)
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
)
62 (define-key help-map
"l" 'view-lossage
)
64 (define-key help-map
"m" 'describe-mode
)
66 (define-key help-map
"\C-n" 'view-emacs-news
)
67 (define-key help-map
"n" 'view-emacs-news
)
69 (define-key help-map
"p" 'finder-by-keyword
)
70 (autoload 'finder-by-keyword
"finder"
71 "Find packages matching a given keyword." t
)
73 (define-key help-map
"s" 'describe-syntax
)
75 (define-key help-map
"t" 'help-with-tutorial
)
77 (define-key help-map
"w" 'where-is
)
79 (define-key help-map
"v" 'describe-variable
)
81 (define-key help-map
"q" 'help-quit
)
87 (defun help-with-tutorial ()
88 "Select the Emacs learn-by-doing tutorial."
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 "~/"))
97 (setq buffer-auto-save-file-name nil
)
98 (insert-file-contents (expand-file-name "TUTORIAL" data-directory
))
99 (goto-char (point-min))
100 (search-forward "\n<<")
102 (delete-region (point) (progn (end-of-line) (point)))
103 (let ((n (- (window-height (selected-window))
104 (count-lines (point-min) (point))
108 ;; Some people get confused by the large gap.
110 (insert "[Middle of page left blank for didactic purposes. "
111 "Text continues below]")
112 (newline (- n
(/ n
2)))))
113 (goto-char (point-min))
114 (set-buffer-modified-p nil
))))
116 (defun describe-key-briefly (key)
117 "Print the name of the function KEY invokes. KEY is a string."
118 (interactive "kDescribe key briefly: ")
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
)))
125 (memq 'down
(event-modifiers type
))
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
))))))
134 (defun print-help-return-message (&optional function
)
135 "Display or return message saying how to restore windows after help command.
136 Computes a message and applies the optional argument FUNCTION to it.
137 If FUNCTION is nil, applies `message' to it, thus printing it."
138 (and (not (get-buffer-window standard-output
))
140 (cond ((or (member (buffer-name standard-output
)
141 special-display-buffer-names
)
142 (assoc (buffer-name standard-output
)
143 special-display-buffer-names
)
145 (tail special-display-regexps
)
146 (name (buffer-name standard-output
)))
147 (while (and tail
(not found
))
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
)))
153 (setq tail
(cdr tail
)))
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.
164 ((not (one-window-p t
))
165 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
167 "Type \\[delete-other-windows] to remove help window.")
169 "Type \\[switch-to-buffer] RET to remove help window."))))
170 (funcall (or function
'message
)
173 (substitute-command-keys first-message
)
175 (if first-message
" " "")
176 (substitute-command-keys
177 "\\[scroll-other-window] to scroll the help."))))))
179 (defun describe-key (key)
180 "Display documentation of the function invoked by KEY. KEY is a string."
181 (interactive "kDescribe key: ")
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
)))
188 (memq 'down
(event-modifiers type
))
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*"
196 (if (documentation defn
)
197 (princ (documentation defn
))
198 (princ "not documented"))
199 (print-help-return-message)))))
201 (defun describe-mode ()
202 "Display documentation of current major mode and minor modes.
203 For this to work correctly for a minor mode, the mode's indicator variable
204 \(listed in `minor-mode-alist') must also be a function whose documentation
205 describes the minor mode."
207 (with-output-to-temp-buffer "*Help*"
208 (let ((minor-modes minor-mode-alist
)
209 (locals (buffer-local-variables)))
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
217 (if (and 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
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
))
232 (setq minor-modes
(cdr minor-modes
))))
235 (princ (documentation major-mode
))
236 (print-help-return-message)))
238 ;; So keyboard macro definitions are documented correctly
239 (fset 'defining-kbd-macro
(symbol-function 'start-kbd-macro
))
241 (defun describe-distribution ()
242 "Display info on how to obtain the latest version of GNU Emacs."
245 (expand-file-name "DISTRIB" data-directory
)))
247 (defun describe-copying ()
248 "Display info on how you may redistribute copies of GNU Emacs."
251 (expand-file-name "COPYING" data-directory
))
252 (goto-char (point-min)))
254 (defun describe-project ()
255 "Display info on the GNU project."
258 (expand-file-name "GNU" data-directory
))
259 (goto-char (point-min)))
261 (defun describe-no-warranty ()
262 "Display info on all the kinds of warranty Emacs does NOT have."
265 (let (case-fold-search)
266 (search-forward "NO WARRANTY")
269 (defun describe-prefix-bindings ()
270 "Describe the bindings of the prefix used to reach this command.
271 The prefix described consists of all but the last event
272 of the key sequence that ran this command."
274 (let* ((key (this-command-keys))
275 (prefix (make-vector (1- (length key
)) nil
))
278 (while (< i
(length prefix
))
279 (aset prefix i
(aref key i
))
281 (describe-bindings prefix
)))
282 ;; Make C-h after a prefix, when not specifically bound,
283 ;; run describe-prefix-bindings.
284 (setq prefix-help-command
'describe-prefix-bindings
)
286 (defun view-emacs-news ()
287 "Display info on recent changes to Emacs."
289 (find-file-read-only (expand-file-name "NEWS" data-directory
)))
291 (defun view-lossage ()
292 "Display last 100 input keystrokes."
294 (with-output-to-temp-buffer "*Help*"
295 (princ (mapconcat (function (lambda (key)
296 (if (or (integerp key
)
299 (single-key-description key
)
300 (prin1-to-string key nil
))))
304 (set-buffer standard-output
)
305 (goto-char (point-min))
306 (while (progn (move-to-column 50) (not (eobp)))
307 (search-forward " " nil t
)
309 (print-help-return-message)))
311 (defalias 'help
'help-for-help
)
312 (make-help-screen help-for-help
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:"
314 "You have typed \\[help-command], the help character. Type a Help option:
315 \(Use \\<help-map>\\[scroll-up] or \\[scroll-down] to scroll through this text.
316 Type \\<help-map>\\[help-quit] to exit the Help command.)
318 a 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.
321 b describe-bindings. Display table of all key bindings.
322 c describe-key-briefly. Type a command key sequence;
323 it prints the function name that sequence runs.
324 f describe-function. Type a function name and get documentation of it.
325 C-f Info-goto-emacs-command-node. Type a function name;
326 it takes you to the Info node for that command.
327 i info. The info documentation reader.
328 k describe-key. Type a command key sequence;
329 it displays the full documentation.
330 C-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.
332 l view-lossage. Shows last 100 characters you typed.
333 m describe-mode. Print documentation of current major mode,
334 which describes the commands peculiar to it.
335 n view-emacs-news. Shows emacs news file.
336 p finder-by-keyword. Find packages matching a given topic keyword.
337 s describe-syntax. Display contents of syntax table, plus explanations
338 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
339 v describe-variable. Type name of a variable;
340 it displays the variable's documentation and value.
341 w where-is. Type command name; it prints which keystrokes
343 C-c print Emacs copying permission (General Public License).
344 C-d print Emacs ordering information.
345 C-n print news of recent Emacs changes.
346 C-p print information about the GNU project.
347 C-w print information on absence of warranty for GNU Emacs."
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 ()
357 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
361 (setq obj
(read (current-buffer)))
362 (and (symbolp obj
) (fboundp obj
) obj
))))
367 (skip-chars-forward "'")
368 (let ((obj (read (current-buffer))))
369 (and (symbolp obj
) (fboundp obj
) obj
)))
372 (defun describe-function-find-file (function)
373 (let ((files load-history
)
376 (if (memq function
(cdr (car files
)))
377 (setq file
(car (car files
)) files nil
))
378 (setq files
(cdr files
)))
381 (defun describe-function (function)
382 "Display the full documentation of FUNCTION (a symbol)."
384 (let ((fn (function-called-at-point))
385 (enable-recursive-minibuffers t
)
387 (setq val
(completing-read (if fn
388 (format "Describe function (default %s): " fn
)
389 "Describe function: ")
391 (list (if (equal val
"")
393 (with-output-to-temp-buffer "*Help*"
396 (let* ((def (symbol-function function
))
397 (beg (if (commandp def
) "an interactive " "a ")))
398 (princ (cond ((or (stringp def
)
402 (concat beg
"built-in function"))
403 ((byte-code-function-p def
)
404 (concat beg
"compiled Lisp function"))
406 (format "alias for `%s'" def
))
407 ((eq (car-safe def
) 'lambda
)
408 (concat beg
"Lisp function"))
409 ((eq (car-safe def
) 'macro
)
411 ((eq (car-safe def
) 'mocklisp
)
412 "a mocklisp function")
413 ((eq (car-safe def
) 'autoload
)
414 (format "%s autoloaded Lisp %s"
415 (if (commandp def
) "an interactive" "an")
416 (if (nth 4 def
) "macro" "function")
417 ;;; Including the file name made this line too long.
421 (let ((file (describe-function-find-file function
)))
429 (let ((arglist (cond ((byte-code-function-p def
)
430 (car (append def nil
)))
431 ((eq (car-safe def
) 'lambda
)
436 (princ (cons function
437 (mapcar (lambda (arg)
438 (if (memq arg
'(&optional
&rest
))
440 (intern (upcase (symbol-name arg
)))))
443 (if (documentation function
)
445 (princ (documentation function
)))
446 (princ "not documented"))
448 (print-help-return-message)
449 ;; Return the text we displayed.
450 (save-excursion (set-buffer standard-output
) (buffer-string))))
452 (defun variable-at-point ()
456 (skip-chars-forward "'")
457 (let ((obj (read (current-buffer))))
458 (and (symbolp obj
) (boundp obj
) obj
)))
461 (defun describe-variable (variable)
462 "Display the full documentation of VARIABLE (a symbol).
463 Returns the documentation as a string, also."
465 (let ((v (variable-at-point))
466 (enable-recursive-minibuffers t
)
468 (setq val
(completing-read (if v
469 (format "Describe variable (default %s): " v
)
470 "Describe variable: ")
472 (list (if (equal val
"")
474 (with-output-to-temp-buffer "*Help*"
476 (princ "'s value is ")
477 (if (not (boundp variable
))
479 (prin1 (symbol-value variable
)))
481 (let ((locals (buffer-local-variables))
484 (if (or (eq variable
(car locals
))
485 (eq variable
(car-safe (car locals
))))
486 (setq is-local t locals nil
))
487 (setq locals
(cdr locals
)))
489 (princ (format "Local in buffer %s\n" (buffer-name)))))
491 (princ "Documentation:")
493 (let ((doc (documentation-property variable
'variable-documentation
)))
495 (princ (substitute-command-keys doc
))
496 (princ "not documented as a variable.")))
497 (print-help-return-message)
498 ;; Return the text we displayed.
499 (save-excursion (set-buffer standard-output
) (buffer-string))))
501 (defun where-is (definition)
502 "Print message listing key sequences that invoke specified command.
503 Argument is a command definition, usually a symbol with a function definition."
505 (let ((fn (function-called-at-point))
506 (enable-recursive-minibuffers t
)
508 (setq val
(completing-read (if fn
509 (format "Where is command (default %s): " fn
)
510 "Where is command: ")
512 (list (if (equal val
"")
514 (let* ((keys (where-is-internal definition overriding-local-map nil nil
))
515 (keys1 (mapconcat 'key-description keys
", ")))
516 (if (> (length keys1
) 0)
517 (message "%s is on %s" definition keys1
)
518 (message "%s is not on any key" definition
)))
521 (defun command-apropos (string)
522 "Like apropos but lists only symbols that are names of commands
523 \(interactively callable functions). Argument REGEXP is a regular expression
524 that is matched against command symbol names. Returns list of symbols and
525 documentation found."
526 (interactive "sCommand apropos (regexp): ")
528 (let ((standard-output (get-buffer-create "*Help*")))
529 (print-help-return-message 'identity
))))
530 (if (apropos string t
'commandp t
)
531 (and message
(message message
)))))
533 (defun locate-library (library &optional nosuffix
)
534 "Show the full path name of Emacs library LIBRARY.
535 This command searches the directories in `load-path' like `M-x load-library'
536 to find the file that `M-x load-library RET LIBRARY RET' would load.
537 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
538 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
539 (interactive "sLocate library: ")
545 (let ((try (expand-file-name (concat library suf
) dir
)))
546 (and (file-readable-p try
)
547 (null (file-directory-p try
))
549 (message "Library is file %s" try
)
550 (throw 'answer try
)))))
551 (if nosuffix
'("") '(".elc" ".el" ""))))
553 (message "No library %s in search path" library
)
556 ;;; help.el ends here