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 (define-key global-map
[help] 'help-command)
43 (define-key global-map [f1] 'help-command)
44 (fset 'help-command help-map)
46 (define-key help-map (char-to-string help-char) 'help-for-help)
47 (define-key help-map [help] 'help-for-help
)
48 (define-key help-map
[f1] 'help-for-help)
49 (define-key help-map "?" 'help-for-help)
51 (define-key help-map "\C-c" 'describe-copying)
52 (define-key help-map "\C-d" 'describe-distribution)
53 (define-key help-map "\C-w" 'describe-no-warranty)
54 (define-key help-map "\C-p" 'describe-project)
55 (define-key help-map "a" 'command-apropos)
57 (define-key help-map "b" 'describe-bindings)
59 (define-key help-map "c" 'describe-key-briefly)
60 (define-key help-map "k" 'describe-key)
62 (define-key help-map "d" 'describe-function)
63 (define-key help-map "f" 'describe-function)
65 (define-key help-map "F" 'view-emacs-FAQ)
67 (define-key help-map "i" 'info)
68 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
69 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
71 (define-key help-map "l" 'view-lossage)
73 (define-key help-map "m" 'describe-mode)
75 (define-key help-map "\C-n" 'view-emacs-news)
76 (define-key help-map "n" 'view-emacs-news)
78 (define-key help-map "p" 'finder-by-keyword)
79 (autoload 'finder-by-keyword "finder"
80 "Find packages matching a given keyword." t)
82 (define-key help-map "s" 'describe-syntax)
84 (define-key help-map "t" 'help-with-tutorial)
86 (define-key help-map "w" 'where-is)
88 (define-key help-map "v" 'describe-variable)
90 (define-key help-map "q" 'help-quit)
93 "Major mode for viewing help text.
94 Entry to this mode runs the normal hook `help-mode-hook'.
98 (kill-all-local-variables)
99 (use-local-map help-mode-map)
100 (setq mode-name "Help")
101 (setq major-mode 'help-mode)
102 (run-hooks 'help-mode-hook))
108 (defun help-with-tutorial ()
109 "Select the Emacs learn-by-doing tutorial."
111 (let ((file (expand-file-name "~/TUTORIAL")))
112 (delete-other-windows)
113 (if (get-file-buffer file)
114 (switch-to-buffer (get-file-buffer file))
115 (switch-to-buffer (create-file-buffer file))
116 (setq buffer-file-name file)
117 (setq default-directory (expand-file-name "~/"))
118 (setq buffer-auto-save-file-name nil)
119 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
120 (goto-char (point-min))
121 (search-forward "\n<<")
123 (delete-region (point) (progn (end-of-line) (point)))
124 (let ((n (- (window-height (selected-window))
125 (count-lines (point-min) (point))
129 ;; Some people get confused by the large gap.
131 (insert "[Middle of page left blank for didactic purposes. "
132 "Text continues below]")
133 (newline (- n (/ n 2)))))
134 (goto-char (point-min))
135 (set-buffer-modified-p nil))))
137 (defun describe-key-briefly (key)
138 "Print the name of the function KEY invokes. KEY is a string."
139 (interactive "kDescribe key briefly: ")
140 ;; If this key seq ends with a down event, discard the
141 ;; following click or drag event. Otherwise that would
142 ;; erase the message.
143 (let ((type (aref key (1- (length key)))))
144 (if (listp type) (setq type (car type)))
146 (memq 'down (event-modifiers type))
148 (let ((defn (key-binding key)))
149 (if (or (null defn) (integerp defn))
150 (message "%s is undefined" (key-description key))
151 (message "%s runs the command %s"
152 (key-description key)
153 (if (symbolp defn) defn (prin1-to-string defn))))))
155 (defun print-help-return-message (&optional function)
156 "Display or return message saying how to restore windows after help command.
157 Computes a message and applies the optional argument FUNCTION to it.
158 If FUNCTION is nil, applies `message' to it, thus printing it."
159 (and (not (get-buffer-window standard-output))
161 (cond ((or (member (buffer-name standard-output)
162 special-display-buffer-names)
163 (assoc (buffer-name standard-output)
164 special-display-buffer-names)
166 (tail special-display-regexps)
167 (name (buffer-name standard-output)))
168 (while (and tail (not found))
169 (if (or (and (consp (car tail))
170 (string-match (car (car tail)) name))
171 (and (stringp (car tail))
172 (string-match (car tail) name)))
174 (setq tail (cdr tail)))
176 ;; If the help output buffer is a special display buffer,
177 ;; don't say anything about how to get rid of it.
178 ;; First of all, the user will do that with the window
179 ;; manager, not with Emacs.
180 ;; Secondly, the buffer has not been displayed yet,
181 ;; so we don't know whether its frame will be selected.
182 ;; Even the message about scrolling the help
183 ;; might be wrong, but it seems worth showing it anyway.
185 ((not (one-window-p t))
186 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
188 "Type \\[delete-other-windows] to remove help window.")
190 "Type \\[switch-to-buffer] RET to remove help window."))))
191 (funcall (or function 'message)
194 (substitute-command-keys first-message)
196 (if first-message " " "")
197 ;; If the help buffer will go in a separate frame,
198 ;; it's no use mentioning a command to scroll, so don't.
199 (if (or (member (buffer-name standard-output)
200 special-display-buffer-names)
201 (memq t (mapcar '(lambda (elt)
202 (string-match elt (buffer-name standard-output)))
203 special-display-regexps)))
205 (if (or (member (buffer-name standard-output)
206 same-window-buffer-names)
207 (memq t (mapcar '(lambda (elt)
208 (string-match elt (buffer-name standard-output)))
209 same-window-regexps)))
210 ;; Say how to scroll this window.
211 (substitute-command-keys
212 "\\[scroll-up] to scroll the help.")
213 ;; Say how to scroll some other window.
214 (substitute-command-keys
215 "\\[scroll-other-window] to scroll the help."))))))))
217 (defun describe-key (key)
218 "Display documentation of the function invoked by KEY. KEY is a string."
219 (interactive "kDescribe key: ")
220 ;; If this key seq ends with a down event, discard the
221 ;; following click or drag event. Otherwise that would
222 ;; erase the message.
223 (let ((type (aref key (1- (length key)))))
224 (if (listp type) (setq type (car type)))
226 (memq 'down (event-modifiers type))
228 (let ((defn (key-binding key)))
229 (if (or (null defn) (integerp defn))
230 (message "%s is undefined" (key-description key))
231 (with-output-to-temp-buffer "*Help*"
234 (if (documentation defn)
235 (princ (documentation defn))
236 (princ "not documented"))
238 (set-buffer standard-output)
240 (print-help-return-message)))))
242 (defun describe-mode ()
243 "Display documentation of current major mode and minor modes.
244 For this to work correctly for a minor mode, the mode's indicator variable
245 \(listed in `minor-mode-alist') must also be a function whose documentation
246 describes the minor mode."
248 (with-output-to-temp-buffer "*Help*"
249 (let ((minor-modes minor-mode-alist)
250 (locals (buffer-local-variables)))
252 (let* ((minor-mode (car (car minor-modes)))
253 (indicator (car (cdr (car minor-modes))))
254 (local-binding (assq minor-mode locals)))
255 ;; Document a minor mode if it is listed in minor-mode-alist,
256 ;; bound locally in this buffer, non-nil, and has a function
258 (if (and local-binding
260 (fboundp minor-mode))
261 (let ((pretty-minor-mode minor-mode))
262 (if (string-match "-mode$" (symbol-name minor-mode))
263 (setq pretty-minor-mode
265 (substring (symbol-name minor-mode)
266 0 (match-beginning 0)))))
267 (while (and indicator (symbolp indicator))
268 (setq indicator (symbol-value indicator)))
269 (princ (format "%s minor mode (indicator%s):\n"
270 pretty-minor-mode indicator))
271 (princ (documentation minor-mode))
273 (setq minor-modes (cdr minor-modes))))
276 (princ (documentation major-mode))
278 (set-buffer standard-output)
280 (print-help-return-message)))
282 ;; So keyboard macro definitions are documented correctly
283 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
285 (defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
289 (expand-file-name "DISTRIB" data-directory)))
291 (defun describe-copying ()
292 "Display info on how you may redistribute copies of GNU Emacs."
295 (expand-file-name "COPYING" data-directory))
296 (goto-char (point-min)))
298 (defun describe-project ()
299 "Display info on the GNU project."
302 (expand-file-name "GNU" data-directory))
303 (goto-char (point-min)))
305 (defun describe-no-warranty ()
306 "Display info on all the kinds of warranty Emacs does NOT have."
309 (let (case-fold-search)
310 (search-forward "NO WARRANTY")
313 (defun describe-prefix-bindings ()
314 "Describe the bindings of the prefix used to reach this command.
315 The prefix described consists of all but the last event
316 of the key sequence that ran this command."
318 (let* ((key (this-command-keys)))
321 (substring key 0 (1- (length key)))
322 (let ((prefix (make-vector (1- (length key)) nil))
324 (while (< i (length prefix))
325 (aset prefix i (aref key i))
328 ;; Make C-h after a prefix, when not specifically bound,
329 ;; run describe-prefix-bindings.
330 (setq prefix-help-command 'describe-prefix-bindings)
332 (defun view-emacs-news ()
333 "Display info on recent changes to Emacs."
335 (find-file-read-only (expand-file-name "NEWS" data-directory)))
337 (defun view-emacs-FAQ ()
338 "Display the Emacs Frequently Asked Questions (FAQ) file."
340 (find-file-read-only (expand-file-name "FAQ" data-directory)))
342 (defun view-lossage ()
343 "Display last 100 input keystrokes."
345 (with-output-to-temp-buffer "*Help*"
346 (princ (mapconcat (function (lambda (key)
347 (if (or (integerp key)
350 (single-key-description key)
351 (prin1-to-string key nil))))
355 (set-buffer standard-output)
356 (goto-char (point-min))
357 (while (progn (move-to-column 50) (not (eobp)))
358 (search-forward " " nil t)
361 (print-help-return-message)))
363 (defalias 'help 'help-for-help)
364 (make-help-screen help-for-help
365 "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:"
366 "You have typed \\[help-command], the help character. Type a Help option:
367 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
369 a command-apropos. Give a substring, and see a list of commands
370 (functions interactively callable) that contain
371 that substring. See also the apropos command.
372 b describe-bindings. Display table of all key bindings.
373 c describe-key-briefly. Type a command key sequence;
374 it prints the function name that sequence runs.
375 f describe-function. Type a function name and get documentation of it.
376 C-f Info-goto-emacs-command-node. Type a function name;
377 it takes you to the Info node for that command.
378 F view-emacs-FAQ. Shows emacs frequently asked questions file.
379 i info. The info documentation reader.
380 k describe-key. Type a command key sequence;
381 it displays the full documentation.
382 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
383 it takes you to the Info node for the command bound to that key.
384 l view-lossage. Shows last 100 characters you typed.
385 m describe-mode. Print documentation of current major mode,
386 which describes the commands peculiar to it.
387 n view-emacs-news. Shows emacs news file.
388 p finder-by-keyword. Find packages matching a given topic keyword.
389 s describe-syntax. Display contents of syntax table, plus explanations
390 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
391 v describe-variable. Type name of a variable;
392 it displays the variable's documentation and value.
393 w where-is. Type command name; it prints which keystrokes
395 C-c print Emacs copying permission (General Public License).
396 C-d print Emacs ordering information.
397 C-n print news of recent Emacs changes.
398 C-p print information about the GNU project.
399 C-w print information on absence of warranty for GNU Emacs."
402 ;; Return a function which is called by the list containing point.
403 ;; If that gives no function, return a function whose name is around point.
404 ;; If that doesn't give a function, return nil.
405 (defun function-called-at-point ()
406 (or (condition-case ()
409 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
413 (setq obj (read (current-buffer)))
414 (and (symbolp obj) (fboundp obj) obj))))
419 (skip-chars-forward "'")
420 (let ((obj (read (current-buffer))))
421 (and (symbolp obj) (fboundp obj) obj)))
424 (defun describe-function-find-file (function)
425 (let ((files load-history)
428 (if (memq function (cdr (car files)))
429 (setq file (car (car files)) files nil))
430 (setq files (cdr files)))
433 (defun describe-function (function)
434 "Display the full documentation of FUNCTION (a symbol)."
436 (let ((fn (function-called-at-point))
437 (enable-recursive-minibuffers t)
439 (setq val (completing-read (if fn
440 (format "Describe function (default %s): " fn)
441 "Describe function: ")
443 (list (if (equal val "")
445 (with-output-to-temp-buffer "*Help*"
448 (let* ((def (symbol-function function))
449 (beg (if (commandp def) "an interactive " "a ")))
450 (princ (cond ((or (stringp def)
454 (concat beg "built-in function"))
455 ((byte-code-function-p def)
456 (concat beg "compiled Lisp function"))
458 (format "alias for `%s'" def))
459 ((eq (car-safe def) 'lambda)
460 (concat beg "Lisp function"))
461 ((eq (car-safe def) 'macro)
463 ((eq (car-safe def) 'mocklisp)
464 "a mocklisp function")
465 ((eq (car-safe def) 'autoload)
466 (format "%s autoloaded Lisp %s"
467 (if (commandp def) "an interactive" "an")
468 (if (nth 4 def) "macro" "function")
469 ;;; Including the file name made this line too long.
473 (let ((file (describe-function-find-file function)))
477 ;; We used to add .el to the file name,
478 ;; but that's completely wrong when the user used load-file.
483 (let ((arglist (cond ((byte-code-function-p def)
484 (car (append def nil)))
485 ((eq (car-safe def) 'lambda)
490 (princ (cons function
491 (mapcar (lambda (arg)
492 (if (memq arg '(&optional &rest))
494 (intern (upcase (symbol-name arg)))))
497 (if (documentation function)
499 (princ (documentation function)))
500 (princ "not documented"))
502 (print-help-return-message)
504 (set-buffer standard-output)
506 ;; Return the text we displayed.
509 (defun variable-at-point ()
513 (skip-chars-forward "'")
514 (let ((obj (read (current-buffer))))
515 (and (symbolp obj) (boundp obj) obj)))
518 (defun describe-variable (variable)
519 "Display the full documentation of VARIABLE (a symbol).
520 Returns the documentation as a string, also."
522 (let ((v (variable-at-point))
523 (enable-recursive-minibuffers t)
525 (setq val (completing-read (if v
526 (format "Describe variable (default %s): " v)
527 "Describe variable: ")
529 (list (if (equal val "")
531 (with-output-to-temp-buffer "*Help*"
533 (if (not (boundp variable))
535 (princ "'s value is ")
536 (prin1 (symbol-value variable)))
538 (if (local-variable-p variable)
540 (princ (format "Local in buffer %s; " (buffer-name)))
541 (if (not (default-boundp variable))
542 (princ "globally void")
543 (princ "global value is ")
544 (prin1 (default-value variable)))
547 (princ "Documentation:")
549 (let ((doc (documentation-property variable 'variable-documentation)))
551 (princ (substitute-command-keys doc))
552 (princ "not documented as a variable.")))
553 (print-help-return-message)
555 (set-buffer standard-output)
557 ;; Return the text we displayed.
560 (defun where-is (definition)
561 "Print message listing key sequences that invoke specified command.
562 Argument is a command definition, usually a symbol with a function definition."
564 (let ((fn (function-called-at-point))
565 (enable-recursive-minibuffers t)
567 (setq val (completing-read (if fn
568 (format "Where is command (default %s): " fn)
569 "Where is command: ")
571 (list (if (equal val "")
573 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
574 (keys1 (mapconcat 'key-description keys ", ")))
575 (if (> (length keys1) 0)
576 (message "%s is on %s" definition keys1)
577 (message "%s is not on any key" definition)))
580 (defun locate-library (library &optional nosuffix)
581 "Show the full path name of Emacs library LIBRARY.
582 This command searches the directories in `load-path' like `M-x load-library'
583 to find the file that `M-x load-library RET LIBRARY RET' would load.
584 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
585 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
586 (interactive "sLocate library: ")
592 (let ((try (expand-file-name (concat library suf) dir)))
593 (and (file-readable-p try)
594 (null (file-directory-p try))
596 (message "Library is file %s" try)
597 (throw 'answer try)))))
598 (if nosuffix '("") '(".elc" ".el" ""))))
600 (message "No library %s in search path" library)
603 ;;; help.el ends here