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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This code implements GNU Emac's on-line help system, the one invoked by
28 ;;`M-x help-for-help'.
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro
))
36 (defvar help-map
(make-sparse-keymap)
37 "Keymap for characters following the Help key.")
39 (defvar help-mode-map
(make-sparse-keymap)
40 "Keymap for help mode.")
42 (define-key global-map
(char-to-string help-char
) 'help-command
)
43 (define-key global-map
[help] 'help-command)
44 (define-key global-map [f1] 'help-command)
45 (fset 'help-command help-map)
47 (define-key help-map (char-to-string help-char) 'help-for-help)
48 (define-key help-map [help] 'help-for-help
)
49 (define-key help-map
[f1] 'help-for-help)
50 (define-key help-map "?" 'help-for-help)
52 (define-key help-map "\C-c" 'describe-copying)
53 (define-key help-map "\C-d" 'describe-distribution)
54 (define-key help-map "\C-w" 'describe-no-warranty)
55 (define-key help-map "\C-p" 'describe-project)
56 (define-key help-map "a" 'command-apropos)
58 (define-key help-map "b" 'describe-bindings)
60 (define-key help-map "c" 'describe-key-briefly)
61 (define-key help-map "k" 'describe-key)
63 (define-key help-map "d" 'describe-function)
64 (define-key help-map "f" 'describe-function)
66 (define-key help-map "F" 'view-emacs-FAQ)
68 (define-key help-map "i" 'info)
69 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
70 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
72 (define-key help-map "l" 'view-lossage)
74 (define-key help-map "m" 'describe-mode)
76 (define-key help-map "\C-n" 'view-emacs-news)
77 (define-key help-map "n" 'view-emacs-news)
79 (define-key help-map "p" 'finder-by-keyword)
80 (autoload 'finder-by-keyword "finder"
81 "Find packages matching a given keyword." t)
83 (define-key help-map "s" 'describe-syntax)
85 (define-key help-map "t" 'help-with-tutorial)
87 (define-key help-map "w" 'where-is)
89 (define-key help-map "v" 'describe-variable)
91 (define-key help-map "q" 'help-quit)
93 (defvar help-font-lock-keywords
95 (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
99 (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
100 '(1 (if (match-beginning 3)
101 font-lock-function-name-face
102 font-lock-variable-name-face)))
104 ;; Words inside `' which tend to be symbol names.
105 (list (concat "`\\(" sym-char sym-char "+\\)'")
106 1 'font-lock-reference-face t)
108 ;; CLisp `:' keywords as references.
109 (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t))))
110 "Default expressions to highlight in Help mode.")
113 "Major mode for viewing help text.
114 Entry to this mode runs the normal hook `help-mode-hook'.
118 (kill-all-local-variables)
119 (use-local-map help-mode-map)
120 (setq mode-name "Help")
121 (setq major-mode 'help-mode)
122 (make-local-variable 'font-lock-defaults)
123 (setq font-lock-defaults '(help-font-lock-keywords))
125 (run-hooks 'help-mode-hook))
131 (defun help-with-tutorial ()
132 "Select the Emacs learn-by-doing tutorial."
134 (let ((file (expand-file-name "~/TUTORIAL")))
135 (delete-other-windows)
136 (if (get-file-buffer file)
137 (switch-to-buffer (get-file-buffer file))
138 (switch-to-buffer (create-file-buffer file))
139 (setq buffer-file-name file)
140 (setq default-directory (expand-file-name "~/"))
141 (setq buffer-auto-save-file-name nil)
142 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
143 (goto-char (point-min))
144 (search-forward "\n<<")
146 (delete-region (point) (progn (end-of-line) (point)))
147 (let ((n (- (window-height (selected-window))
148 (count-lines (point-min) (point))
152 ;; Some people get confused by the large gap.
154 (insert "[Middle of page left blank for didactic purposes. "
155 "Text continues below]")
156 (newline (- n (/ n 2)))))
157 (goto-char (point-min))
158 (set-buffer-modified-p nil))))
160 (defun describe-key-briefly (key)
161 "Print the name of the function KEY invokes. KEY is a string."
162 (interactive "kDescribe key briefly: ")
163 ;; If this key seq ends with a down event, discard the
164 ;; following click or drag event. Otherwise that would
165 ;; erase the message.
166 (let ((type (aref key (1- (length key)))))
167 (if (listp type) (setq type (car type)))
169 (memq 'down (event-modifiers type))
172 (let ((modifiers (event-modifiers (aref key 0)))
174 ;; For a mouse button event, go to the button it applies to
175 ;; to get the right key bindings. And go to the right place
176 ;; in case the keymap depends on where you clicked.
177 (if (or (memq 'click modifiers) (memq 'down modifiers)
178 (memq 'drag modifiers))
179 (setq window (posn-window (event-start (aref key 0)))
180 position (posn-point (event-start (aref key 0)))))
183 (set-buffer (window-buffer window))
184 (goto-char position)))
185 ;; Ok, now look up the key and name the command.
186 (let ((defn (key-binding key)))
187 (if (or (null defn) (integerp defn))
188 (message "%s is undefined" (key-description key))
189 (message (if (windowp window)
190 "%s at that spot runs the command %s"
191 "%s runs the command %s")
192 (key-description key)
193 (if (symbolp defn) defn (prin1-to-string defn))))))))
195 (defun print-help-return-message (&optional function)
196 "Display or return message saying how to restore windows after help command.
197 Computes a message and applies the optional argument FUNCTION to it.
198 If FUNCTION is nil, applies `message' to it, thus printing it."
199 (and (not (get-buffer-window standard-output))
201 (cond ((or (member (buffer-name standard-output)
202 special-display-buffer-names)
203 (assoc (buffer-name standard-output)
204 special-display-buffer-names)
206 (tail special-display-regexps)
207 (name (buffer-name standard-output)))
208 (while (and tail (not found))
209 (if (or (and (consp (car tail))
210 (string-match (car (car tail)) name))
211 (and (stringp (car tail))
212 (string-match (car tail) name)))
214 (setq tail (cdr tail)))
216 ;; If the help output buffer is a special display buffer,
217 ;; don't say anything about how to get rid of it.
218 ;; First of all, the user will do that with the window
219 ;; manager, not with Emacs.
220 ;; Secondly, the buffer has not been displayed yet,
221 ;; so we don't know whether its frame will be selected.
222 ;; Even the message about scrolling the help
223 ;; might be wrong, but it seems worth showing it anyway.
225 ((not (one-window-p t))
226 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
228 "Type \\[delete-other-windows] to remove help window.")
230 "Type \\[switch-to-buffer] RET to remove help window."))))
231 (funcall (or function 'message)
234 (substitute-command-keys first-message)
236 (if first-message " " "")
237 ;; If the help buffer will go in a separate frame,
238 ;; it's no use mentioning a command to scroll, so don't.
239 (if (or (member (buffer-name standard-output)
240 special-display-buffer-names)
241 (assoc (buffer-name standard-output)
242 special-display-buffer-names)
243 (memq t (mapcar '(lambda (elt)
245 (setq elt (car elt)))
246 (string-match elt (buffer-name standard-output)))
247 special-display-regexps)))
249 (if (or (member (buffer-name standard-output)
250 same-window-buffer-names)
251 (assoc (buffer-name standard-output)
252 same-window-buffer-names)
253 (memq t (mapcar '(lambda (elt)
255 (setq elt (car elt)))
256 (string-match elt (buffer-name standard-output)))
257 same-window-regexps)))
258 ;; Say how to scroll this window.
259 (substitute-command-keys
260 "\\[scroll-up] to scroll the help.")
261 ;; Say how to scroll some other window.
262 (substitute-command-keys
263 "\\[scroll-other-window] to scroll the help."))))))))
265 (defun describe-key (key)
266 "Display documentation of the function invoked by KEY. KEY is a string."
267 (interactive "kDescribe key: ")
268 ;; If this key seq ends with a down event, discard the
269 ;; following click or drag event. Otherwise that would
270 ;; erase the message.
271 (let ((type (aref key (1- (length key)))))
272 (if (listp type) (setq type (car type)))
274 (memq 'down (event-modifiers type))
277 (let ((modifiers (event-modifiers (aref key 0)))
279 ;; For a mouse button event, go to the button it applies to
280 ;; to get the right key bindings. And go to the right place
281 ;; in case the keymap depends on where you clicked.
282 (if (or (memq 'click modifiers) (memq 'down modifiers)
283 (memq 'drag modifiers))
284 (setq window (posn-window (event-start (aref key 0)))
285 position (posn-point (event-start (aref key 0)))))
288 (set-buffer (window-buffer window))
289 (goto-char position)))
290 (let ((defn (key-binding key)))
291 (if (or (null defn) (integerp defn))
292 (message "%s is undefined" (key-description key))
293 (with-output-to-temp-buffer "*Help*"
294 (princ (key-description key))
296 (princ " at that spot"))
297 (princ " runs the command ")
300 (let ((doc (documentation defn)))
304 (princ "not documented")))
306 (set-buffer standard-output)
308 (print-help-return-message)))))))
310 (defun describe-mode ()
311 "Display documentation of current major mode and minor modes.
312 For this to work correctly for a minor mode, the mode's indicator variable
313 \(listed in `minor-mode-alist') must also be a function whose documentation
314 describes the minor mode."
316 (with-output-to-temp-buffer "*Help*"
317 (let ((minor-modes minor-mode-alist)
319 (locals (buffer-local-variables)))
321 (let* ((minor-mode (car (car minor-modes)))
322 (indicator (car (cdr (car minor-modes))))
323 (local-binding (assq minor-mode locals)))
324 ;; Document a minor mode if it is listed in minor-mode-alist,
325 ;; bound locally in this buffer, non-nil, and has a function
327 (if (and local-binding
329 (fboundp minor-mode))
330 (let ((pretty-minor-mode minor-mode))
331 (if (string-match "-mode$" (symbol-name minor-mode))
332 (setq pretty-minor-mode
334 (substring (symbol-name minor-mode)
335 0 (match-beginning 0)))))
336 (while (and indicator (symbolp indicator))
337 (setq indicator (symbol-value indicator)))
339 (princ "The minor modes are described first,
340 followed by the major mode, which is described on the last page.\n\f\n"))
342 (princ (format "%s minor mode (%s):\n"
345 (format "indicator%s" indicator)
347 (princ (documentation minor-mode))
349 (setq minor-modes (cdr minor-modes))))
352 (princ (documentation major-mode))
354 (set-buffer standard-output)
356 (print-help-return-message)))
358 ;; So keyboard macro definitions are documented correctly
359 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
361 (defun describe-distribution ()
362 "Display info on how to obtain the latest version of GNU Emacs."
365 (expand-file-name "DISTRIB" data-directory)))
367 (defun describe-copying ()
368 "Display info on how you may redistribute copies of GNU Emacs."
371 (expand-file-name "COPYING" data-directory))
372 (goto-char (point-min)))
374 (defun describe-project ()
375 "Display info on the GNU project."
378 (expand-file-name "GNU" data-directory))
379 (goto-char (point-min)))
381 (defun describe-no-warranty ()
382 "Display info on all the kinds of warranty Emacs does NOT have."
385 (let (case-fold-search)
386 (search-forward "NO WARRANTY")
389 (defun describe-prefix-bindings ()
390 "Describe the bindings of the prefix used to reach this command.
391 The prefix described consists of all but the last event
392 of the key sequence that ran this command."
394 (let* ((key (this-command-keys)))
397 (substring key 0 (1- (length key)))
398 (let ((prefix (make-vector (1- (length key)) nil))
400 (while (< i (length prefix))
401 (aset prefix i (aref key i))
404 ;; Make C-h after a prefix, when not specifically bound,
405 ;; run describe-prefix-bindings.
406 (setq prefix-help-command 'describe-prefix-bindings)
408 (defun view-emacs-news ()
409 "Display info on recent changes to Emacs."
411 (find-file-read-only (expand-file-name "NEWS" data-directory)))
413 (defun view-emacs-FAQ ()
414 "Display the Emacs Frequently Asked Questions (FAQ) file."
416 (find-file-read-only (expand-file-name "FAQ" data-directory)))
418 (defun view-lossage ()
419 "Display last 100 input keystrokes."
421 (with-output-to-temp-buffer "*Help*"
422 (princ (mapconcat (function (lambda (key)
423 (if (or (integerp key)
426 (single-key-description key)
427 (prin1-to-string key nil))))
431 (set-buffer standard-output)
432 (goto-char (point-min))
433 (while (progn (move-to-column 50) (not (eobp)))
434 (search-forward " " nil t)
437 (print-help-return-message)))
439 (defalias 'help 'help-for-help)
440 (make-help-screen help-for-help
441 "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:"
442 "You have typed \\[help-command], the help character. Type a Help option:
443 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
445 a command-apropos. Give a substring, and see a list of commands
446 (functions interactively callable) that contain
447 that substring. See also the apropos command.
448 b describe-bindings. Display table of all key bindings.
449 c describe-key-briefly. Type a command key sequence;
450 it prints the function name that sequence runs.
451 f describe-function. Type a function name and get documentation of it.
452 C-f Info-goto-emacs-command-node. Type a function name;
453 it takes you to the Info node for that command.
454 F view-emacs-FAQ. Shows emacs frequently asked questions file.
455 i info. The info documentation reader.
456 k describe-key. Type a command key sequence;
457 it displays the full documentation.
458 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
459 it takes you to the Info node for the command bound to that key.
460 l view-lossage. Shows last 100 characters you typed.
461 m describe-mode. Print documentation of current major mode,
462 which describes the commands peculiar to it.
463 n view-emacs-news. Shows emacs news file.
464 p finder-by-keyword. Find packages matching a given topic keyword.
465 s describe-syntax. Display contents of syntax table, plus explanations
466 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
467 v describe-variable. Type name of a variable;
468 it displays the variable's documentation and value.
469 w where-is. Type command name; it prints which keystrokes
471 C-c print Emacs copying permission (General Public License).
472 C-d print Emacs ordering information.
473 C-n print news of recent Emacs changes.
474 C-p print information about the GNU project.
475 C-w print information on absence of warranty for GNU Emacs."
478 ;; Return a function which is called by the list containing point.
479 ;; If that gives no function, return a function whose name is around point.
480 ;; If that doesn't give a function, return nil.
481 (defun function-called-at-point ()
482 (or (condition-case ()
485 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
489 (setq obj (read (current-buffer)))
490 (and (symbolp obj) (fboundp obj) obj))))
493 (let ((stab (syntax-table)))
496 (set-syntax-table emacs-lisp-mode-syntax-table)
497 (or (not (zerop (skip-syntax-backward "_w")))
498 (eq (char-syntax (following-char)) ?w)
499 (eq (char-syntax (following-char)) ?_)
501 (skip-chars-forward "'")
502 (let ((obj (read (current-buffer))))
503 (and (symbolp obj) (fboundp obj) obj)))
504 (set-syntax-table stab)))
507 (defun describe-function-find-file (function)
508 (let ((files load-history)
511 (if (memq function (cdr (car files)))
512 (setq file (car (car files)) files nil))
513 (setq files (cdr files)))
516 (defun describe-function (function)
517 "Display the full documentation of FUNCTION (a symbol)."
519 (let ((fn (function-called-at-point))
520 (enable-recursive-minibuffers t)
522 (setq val (completing-read (if fn
523 (format "Describe function (default %s): " fn)
524 "Describe function: ")
526 (list (if (equal val "")
528 (with-output-to-temp-buffer "*Help*"
531 (let* ((def (symbol-function function))
533 (beg (if (commandp def) "an interactive " "a ")))
534 (princ (cond ((or (stringp def)
538 (concat beg "built-in function"))
539 ((byte-code-function-p def)
540 (concat beg "compiled Lisp function"))
542 (format "alias for `%s'" def))
543 ((eq (car-safe def) 'lambda)
544 (concat beg "Lisp function"))
545 ((eq (car-safe def) 'macro)
547 ((eq (car-safe def) 'mocklisp)
548 "a mocklisp function")
549 ((eq (car-safe def) 'autoload)
550 (setq file-name (nth 1 def))
551 (format "%s autoloaded Lisp %s"
552 (if (commandp def) "an interactive" "an")
553 (if (nth 4 def) "macro" "function")
557 (setq file-name (describe-function-find-file function)))
561 ;; We used to add .el to the file name,
562 ;; but that's completely wrong when the user used load-file.
567 (let ((arglist (cond ((byte-code-function-p def)
568 (car (append def nil)))
569 ((eq (car-safe def) 'lambda)
574 (princ (cons function
575 (mapcar (lambda (arg)
576 (if (memq arg '(&optional &rest))
578 (intern (upcase (symbol-name arg)))))
581 (let ((doc (documentation function)))
585 (princ "not documented"))))
586 (print-help-return-message)
588 (set-buffer standard-output)
590 ;; Return the text we displayed.
593 (defun variable-at-point ()
595 (let ((stab (syntax-table)))
598 (set-syntax-table emacs-lisp-mode-syntax-table)
599 (or (not (zerop (skip-syntax-backward "_w")))
600 (eq (char-syntax (following-char)) ?w)
601 (eq (char-syntax (following-char)) ?_)
603 (skip-chars-forward "'")
604 (let ((obj (read (current-buffer))))
605 (and (symbolp obj) (boundp obj) obj)))
606 (set-syntax-table stab)))
609 (defun describe-variable (variable)
610 "Display the full documentation of VARIABLE (a symbol).
611 Returns the documentation as a string, also."
613 (let ((v (variable-at-point))
614 (enable-recursive-minibuffers t)
616 (setq val (completing-read (if v
617 (format "Describe variable (default %s): " v)
618 "Describe variable: ")
620 (list (if (equal val "")
622 (with-output-to-temp-buffer "*Help*"
624 (if (not (boundp variable))
626 (princ "'s value is ")
627 (prin1 (symbol-value variable)))
629 (if (local-variable-p variable)
631 (princ (format "Local in buffer %s; " (buffer-name)))
632 (if (not (default-boundp variable))
633 (princ "globally void")
634 (princ "global value is ")
635 (prin1 (default-value variable)))
638 (princ "Documentation:")
640 (let ((doc (documentation-property variable 'variable-documentation)))
641 (princ (or doc "not documented as a variable.")))
642 (print-help-return-message)
644 (set-buffer standard-output)
646 ;; Return the text we displayed.
649 (defun where-is (definition)
650 "Print message listing key sequences that invoke specified command.
651 Argument is a command definition, usually a symbol with a function definition."
653 (let ((fn (function-called-at-point))
654 (enable-recursive-minibuffers t)
656 (setq val (completing-read (if fn
657 (format "Where is command (default %s): " fn)
658 "Where is command: ")
660 (list (if (equal val "")
662 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
663 (keys1 (mapconcat 'key-description keys ", ")))
664 (if (> (length keys1) 0)
665 (message "%s is on %s" definition keys1)
666 (message "%s is not on any key" definition)))
669 (defun locate-library (library &optional nosuffix)
670 "Show the full path name of Emacs library LIBRARY.
671 This command searches the directories in `load-path' like `M-x load-library'
672 to find the file that `M-x load-library RET LIBRARY RET' would load.
673 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
674 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
675 (interactive "sLocate library: ")
681 (let ((try (expand-file-name (concat library suf) dir)))
682 (and (file-readable-p try)
683 (null (file-directory-p try))
685 (message "Library is file %s" try)
686 (throw 'answer try)))))
687 (if nosuffix '("") '(".elc" ".el" ""))))
689 (message "No library %s in search path" library)
692 ;;; help.el ends here