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 Emacs' 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" 'apropos-command)
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)
71 (define-key help-map "\C-i" 'info-lookup-symbol)
73 (define-key help-map "l" 'view-lossage)
75 (define-key help-map "m" 'describe-mode)
77 (define-key help-map "\C-n" 'view-emacs-news)
78 (define-key help-map "n" 'view-emacs-news)
80 (define-key help-map "p" 'finder-by-keyword)
81 (autoload 'finder-by-keyword "finder"
82 "Find packages matching a given keyword." t)
84 (define-key help-map "s" 'describe-syntax)
86 (define-key help-map "t" 'help-with-tutorial)
88 (define-key help-map "w" 'where-is)
90 (define-key help-map "v" 'describe-variable)
92 (define-key help-map "q" 'help-quit)
94 (defvar help-font-lock-keywords
96 (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
100 (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
101 '(1 (if (match-beginning 3)
102 font-lock-function-name-face
103 font-lock-variable-name-face)))
105 ;; Words inside `' which tend to be symbol names.
106 (list (concat "`\\(" sym-char sym-char "+\\)'")
107 1 'font-lock-reference-face t)
109 ;; CLisp `:' keywords as references.
110 (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t))))
111 "Default expressions to highlight in Help mode.")
114 "Major mode for viewing help text.
115 Entry to this mode runs the normal hook `help-mode-hook'.
119 (kill-all-local-variables)
120 (use-local-map help-mode-map)
121 (setq mode-name "Help")
122 (setq major-mode 'help-mode)
123 (make-local-variable 'font-lock-defaults)
124 (setq font-lock-defaults '(help-font-lock-keywords))
126 (run-hooks 'help-mode-hook))
132 (defun help-with-tutorial (&optional arg)
133 "Select the Emacs learn-by-doing tutorial.
134 A tutorial written in the current primary language is selected.
135 If there's no tutorial in the language, \"TUTORIAL\" is selected.
136 With arg, users are asked to select language."
138 (let (lang filename file)
140 (or (setq lang (read-language-name 'tutorial "Language: "))
141 (error "No tutorial file of the specified language"))
142 (setq lang primary-language))
143 (setq filename (or (get-language-info lang 'tutorial)
145 (setq file (expand-file-name (concat "~/" filename)))
146 (delete-other-windows)
147 (if (get-file-buffer file)
148 (switch-to-buffer (get-file-buffer file))
149 (switch-to-buffer (create-file-buffer file))
150 (setq buffer-file-name file)
151 (setq default-directory (expand-file-name "~/"))
152 (setq buffer-auto-save-file-name nil)
153 (insert-file-contents (expand-file-name filename data-directory))
154 (goto-char (point-min))
155 (search-forward "\n<<")
157 (delete-region (point) (progn (end-of-line) (point)))
158 (let ((n (- (window-height (selected-window))
159 (count-lines (point-min) (point))
163 ;; Some people get confused by the large gap.
165 (insert "[Middle of page left blank for didactic purposes. "
166 "Text continues below]")
167 (newline (- n (/ n 2)))))
168 (goto-char (point-min))
169 (set-buffer-modified-p nil))))
171 (defun describe-key-briefly (key &optional insert)
172 "Print the name of the function KEY invokes. KEY is a string.
173 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
174 (interactive "kDescribe key briefly: \nP")
175 ;; If this key seq ends with a down event, discard the
176 ;; following click or drag event. Otherwise that would
177 ;; erase the message.
178 (let ((type (aref key (1- (length key)))))
179 (if (listp type) (setq type (car type)))
181 (memq 'down (event-modifiers type))
184 (let ((modifiers (event-modifiers (aref key 0)))
185 (standard-output (if insert (current-buffer) t))
187 ;; For a mouse button event, go to the button it applies to
188 ;; to get the right key bindings. And go to the right place
189 ;; in case the keymap depends on where you clicked.
190 (if (or (memq 'click modifiers) (memq 'down modifiers)
191 (memq 'drag modifiers))
192 (setq window (posn-window (event-start (aref key 0)))
193 position (posn-point (event-start (aref key 0)))))
196 (set-buffer (window-buffer window))
197 (goto-char position)))
198 ;; Ok, now look up the key and name the command.
199 (let ((defn (key-binding key))
200 (key-desc (key-description key)))
201 (if (or (null defn) (integerp defn))
202 (princ (format "%s is undefined" key-desc))
203 (princ (format (if insert
206 "%s at that spot runs the command %s"
207 "%s runs the command %s"))
209 (if (symbolp defn) defn (prin1-to-string defn)))))))))
211 (defun print-help-return-message (&optional function)
212 "Display or return message saying how to restore windows after help command.
213 Computes a message and applies the optional argument FUNCTION to it.
214 If FUNCTION is nil, applies `message' to it, thus printing it."
215 (and (not (get-buffer-window standard-output))
217 (cond ((special-display-p (buffer-name standard-output))
218 ;; If the help output buffer is a special display buffer,
219 ;; don't say anything about how to get rid of it.
220 ;; First of all, the user will do that with the window
221 ;; manager, not with Emacs.
222 ;; Secondly, the buffer has not been displayed yet,
223 ;; so we don't know whether its frame will be selected.
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 (special-display-p (buffer-name standard-output))
241 (if (same-window-p (buffer-name standard-output))
242 ;; Say how to scroll this window.
243 (substitute-command-keys
244 "\\[scroll-up] to scroll the help.")
245 ;; Say how to scroll some other window.
246 (substitute-command-keys
247 "\\[scroll-other-window] to scroll the help."))))))))
249 (defun describe-key (key)
250 "Display documentation of the function invoked by KEY. KEY is a string."
251 (interactive "kDescribe key: ")
252 ;; If this key seq ends with a down event, discard the
253 ;; following click or drag event. Otherwise that would
254 ;; erase the message.
255 (let ((type (aref key (1- (length key)))))
256 (if (listp type) (setq type (car type)))
258 (memq 'down (event-modifiers type))
261 (let ((modifiers (event-modifiers (aref key 0)))
263 ;; For a mouse button event, go to the button it applies to
264 ;; to get the right key bindings. And go to the right place
265 ;; in case the keymap depends on where you clicked.
266 (if (or (memq 'click modifiers) (memq 'down modifiers)
267 (memq 'drag modifiers))
268 (setq window (posn-window (event-start (aref key 0)))
269 position (posn-point (event-start (aref key 0)))))
272 (set-buffer (window-buffer window))
273 (goto-char position)))
274 (let ((defn (key-binding key)))
275 (if (or (null defn) (integerp defn))
276 (message "%s is undefined" (key-description key))
277 (with-output-to-temp-buffer "*Help*"
278 (princ (key-description key))
280 (princ " at that spot"))
281 (princ " runs the command ")
284 (let ((doc (documentation defn)))
288 (princ "not documented")))
290 (set-buffer standard-output)
292 (print-help-return-message)))))))
294 (defun describe-mode ()
295 "Display documentation of current major mode and minor modes.
296 For this to work correctly for a minor mode, the mode's indicator variable
297 \(listed in `minor-mode-alist') must also be a function whose documentation
298 describes the minor mode."
300 (with-output-to-temp-buffer "*Help*"
301 (let ((minor-modes minor-mode-alist)
304 (let* ((minor-mode (car (car minor-modes)))
305 (indicator (car (cdr (car minor-modes)))))
306 ;; Document a minor mode if it is listed in minor-mode-alist,
307 ;; bound locally in this buffer, non-nil, and has a function
309 (if (and (symbol-value minor-mode)
310 (fboundp minor-mode))
311 (let ((pretty-minor-mode minor-mode))
312 (if (string-match "-mode$" (symbol-name minor-mode))
313 (setq pretty-minor-mode
315 (substring (symbol-name minor-mode)
316 0 (match-beginning 0)))))
317 (while (and indicator (symbolp indicator))
318 (setq indicator (symbol-value indicator)))
320 (princ "The minor modes are described first,
321 followed by the major mode, which is described on the last page.\n\f\n"))
323 (princ (format "%s minor mode (%s):\n"
326 (format "indicator%s" indicator)
328 (princ (documentation minor-mode))
330 (setq minor-modes (cdr minor-modes))))
333 (princ (documentation major-mode))
335 (set-buffer standard-output)
337 (print-help-return-message)))
339 ;; So keyboard macro definitions are documented correctly
340 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
342 (defun describe-distribution ()
343 "Display info on how to obtain the latest version of GNU Emacs."
346 (expand-file-name "DISTRIB" data-directory)))
348 (defun describe-copying ()
349 "Display info on how you may redistribute copies of GNU Emacs."
352 (expand-file-name "COPYING" data-directory))
353 (goto-char (point-min)))
355 (defun describe-project ()
356 "Display info on the GNU project."
359 (expand-file-name "GNU" data-directory))
360 (goto-char (point-min)))
362 (defun describe-no-warranty ()
363 "Display info on all the kinds of warranty Emacs does NOT have."
366 (let (case-fold-search)
367 (search-forward "NO WARRANTY")
370 (defun describe-prefix-bindings ()
371 "Describe the bindings of the prefix used to reach this command.
372 The prefix described consists of all but the last event
373 of the key sequence that ran this command."
375 (let* ((key (this-command-keys)))
378 (substring key 0 (1- (length key)))
379 (let ((prefix (make-vector (1- (length key)) nil))
381 (while (< i (length prefix))
382 (aset prefix i (aref key i))
385 ;; Make C-h after a prefix, when not specifically bound,
386 ;; run describe-prefix-bindings.
387 (setq prefix-help-command 'describe-prefix-bindings)
389 (defun view-emacs-news ()
390 "Display info on recent changes to Emacs."
392 (find-file-read-only (expand-file-name "NEWS" data-directory)))
394 (defun view-emacs-FAQ ()
395 "Display the Emacs Frequently Asked Questions (FAQ) file."
397 (find-file-read-only (expand-file-name "FAQ" data-directory)))
399 (defun view-lossage ()
400 "Display last 100 input keystrokes."
402 (with-output-to-temp-buffer "*Help*"
403 (princ (mapconcat (function (lambda (key)
404 (if (or (integerp key)
407 (single-key-description key)
408 (prin1-to-string key nil))))
412 (set-buffer standard-output)
413 (goto-char (point-min))
414 (while (progn (move-to-column 50) (not (eobp)))
415 (search-forward " " nil t)
418 (print-help-return-message)))
420 (defalias 'help 'help-for-help)
421 (make-help-screen help-for-help
422 "a b c C f F C-f i I k C-k l L m n p s t v w C-c C-d C-n C-p C-w; ? for help:"
423 "You have typed \\[help-command], the help character. Type a Help option:
424 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
426 a command-apropos. Give a substring, and see a list of commands
427 (functions interactively callable) that contain
428 that substring. See also the apropos command.
429 b describe-bindings. Display table of all key bindings.
430 c describe-key-briefly. Type a command key sequence;
431 it prints the function name that sequence runs.
432 C describe-coding-system. This describes either a specific coding system
433 (if you type its name) or the coding systems currently in use
434 (if you type just RET).
435 f describe-function. Type a function name and get documentation of it.
436 C-f Info-goto-emacs-command-node. Type a function name;
437 it takes you to the Info node for that command.
438 i info. The info documentation reader.
439 I describe-input-method. Describe a specific input method (if you type
440 its name) or the current input method (if you type just RET).
441 k describe-key. Type a command key sequence;
442 it displays the full documentation.
443 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
444 it takes you to the Info node for the command bound to that key.
445 l view-lossage. Shows last 100 characters you typed.
446 L describe-language-environment. This describes either the a
447 specific language environment (if you type its name)
448 or the current language environment (if you type just RET).
449 m describe-mode. Print documentation of current major mode,
450 which describes the commands peculiar to it.
451 n view-emacs-news. Shows emacs news file.
452 p finder-by-keyword. Find packages matching a given topic keyword.
453 s describe-syntax. Display contents of syntax table, plus explanations
454 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
455 v describe-variable. Type name of a variable;
456 it displays the variable's documentation and value.
457 w where-is. Type command name; it prints which keystrokes
460 F Display the frequently asked questions file.
461 h Display the HELLO file which illustrates various scripts.
462 C-c Display Emacs copying permission (General Public License).
463 C-d Display Emacs ordering information.
464 C-n Display news of recent Emacs changes.
465 C-p Display information about the GNU project.
466 C-w Display information on absence of warranty for GNU Emacs."
469 ;; Return a function which is called by the list containing point.
470 ;; If that gives no function, return a function whose name is around point.
471 ;; If that doesn't give a function, return nil.
472 (defun function-called-at-point ()
473 (or (condition-case ()
476 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
480 (setq obj (read (current-buffer)))
481 (and (symbolp obj) (fboundp obj) obj))))
484 (let ((stab (syntax-table)))
487 (set-syntax-table emacs-lisp-mode-syntax-table)
488 (or (not (zerop (skip-syntax-backward "_w")))
489 (eq (char-syntax (following-char)) ?w)
490 (eq (char-syntax (following-char)) ?_)
492 (skip-chars-forward "'")
493 (let ((obj (read (current-buffer))))
494 (and (symbolp obj) (fboundp obj) obj)))
495 (set-syntax-table stab)))
498 (defun describe-function-find-file (function)
499 (let ((files load-history)
502 (if (memq function (cdr (car files)))
503 (setq file (car (car files)) files nil))
504 (setq files (cdr files)))
507 (defun describe-function (function)
508 "Display the full documentation of FUNCTION (a symbol)."
510 (let ((fn (function-called-at-point))
511 (enable-recursive-minibuffers t)
513 (setq val (completing-read (if fn
514 (format "Describe function (default %s): " fn)
515 "Describe function: ")
517 (list (if (equal val "")
520 (with-output-to-temp-buffer "*Help*"
522 ;; Use " is " instead of a colon so that
523 ;; it is easier to get out the function name using forward-sexp.
525 (let* ((def (symbol-function function))
527 (beg (if (commandp def) "an interactive " "a ")))
528 (princ (cond ((or (stringp def)
532 (concat beg "built-in function"))
533 ((byte-code-function-p def)
534 (concat beg "compiled Lisp function"))
536 (format "alias for `%s'" def))
537 ((eq (car-safe def) 'lambda)
538 (concat beg "Lisp function"))
539 ((eq (car-safe def) 'macro)
541 ((eq (car-safe def) 'mocklisp)
542 "a mocklisp function")
543 ((eq (car-safe def) 'autoload)
544 (setq file-name (nth 1 def))
545 (format "%s autoloaded Lisp %s"
546 (if (commandp def) "an interactive" "an")
547 (if (nth 4 def) "macro" "function")
551 (setq file-name (describe-function-find-file function)))
555 ;; We used to add .el to the file name,
556 ;; but that's completely wrong when the user used load-file.
561 (let ((arglist (cond ((byte-code-function-p def)
562 (car (append def nil)))
563 ((eq (car-safe def) 'lambda)
568 (princ (cons function
569 (mapcar (lambda (arg)
570 (if (memq arg '(&optional &rest))
572 (intern (upcase (symbol-name arg)))))
575 (let ((doc (documentation function)))
579 (princ "not documented"))))
580 (print-help-return-message)
582 (set-buffer standard-output)
584 ;; Return the text we displayed.
586 (message "You didn't specify a function")))
588 ;; We return 0 if we can't find a variable to return.
589 (defun variable-at-point ()
591 (let ((stab (syntax-table)))
594 (set-syntax-table emacs-lisp-mode-syntax-table)
595 (or (not (zerop (skip-syntax-backward "_w")))
596 (eq (char-syntax (following-char)) ?w)
597 (eq (char-syntax (following-char)) ?_)
599 (skip-chars-forward "'")
600 (let ((obj (read (current-buffer))))
601 (or (and (symbolp obj) (boundp obj) obj)
603 (set-syntax-table stab)))
606 (defun describe-variable (variable)
607 "Display the full documentation of VARIABLE (a symbol).
608 Returns the documentation as a string, also."
610 (let ((v (variable-at-point))
611 (enable-recursive-minibuffers t)
613 (setq val (completing-read (if (symbolp v)
614 (format "Describe variable (default %s): " v)
615 "Describe variable: ")
617 (list (if (equal val "")
619 (if (symbolp variable)
621 (with-output-to-temp-buffer "*Help*"
623 (if (not (boundp variable))
628 (princ "'s value is ")
630 (pp (symbol-value variable))
632 (if (local-variable-p variable)
634 (princ (format "Local in buffer %s; " (buffer-name)))
635 (if (not (default-boundp variable))
636 (princ "globally void")
637 (princ "global value is ")
639 (pp (default-value variable)))
643 (set-buffer standard-output)
644 (if (> (count-lines (point-min) (point-max)) 10)
646 (goto-char (point-min))
650 (delete-region (point) (progn (end-of-line) (point)))
651 (insert "'s value is shown below.\n\n")
653 (insert "\n\nValue:"))))))
654 (princ "Documentation:")
656 (let ((doc (documentation-property variable 'variable-documentation)))
657 (princ (or doc "not documented as a variable.")))
658 (print-help-return-message)
660 (set-buffer standard-output)
662 ;; Return the text we displayed.
664 (message "You did not specify a variable")))
666 (defun where-is (definition &optional insert)
667 "Print message listing key sequences that invoke specified command.
668 Argument is a command definition, usually a symbol with a function definition.
669 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
671 (let ((fn (function-called-at-point))
672 (enable-recursive-minibuffers t)
674 (setq val (completing-read (if fn
675 (format "Where is command (default %s): " fn)
676 "Where is command: ")
678 (list (if (equal val "")
680 current-prefix-arg)))
681 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
682 (keys1 (mapconcat 'key-description keys ", "))
683 (standard-output (if insert (current-buffer) t)))
685 (if (> (length keys1) 0)
686 (princ (format "%s (%s)" keys1 definition))
687 (princ (format "M-x %s RET" definition)))
688 (if (> (length keys1) 0)
689 (princ (format "%s is on %s" definition keys1))
690 (princ (format "%s is not on any key" definition)))))
693 (defun locate-library (library &optional nosuffix path interactive-call)
694 "Show the precise file name of Emacs library LIBRARY.
695 This command searches the directories in `load-path' like `M-x load-library'
696 to find the file that `M-x load-library RET LIBRARY RET' would load.
697 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
698 to the specified name LIBRARY.
700 If the optional third arg PATH is specified, that list of directories
701 is used instead of `load-path'."
702 (interactive (list (read-string "Locate library: ")
711 (let ((try (expand-file-name (concat library suf) dir)))
712 (and (file-readable-p try)
713 (null (file-directory-p try))
716 (throw 'answer try)))))
719 (let ((basic '(".elc" ".el" ""))
720 (compressed '(".Z" ".gz" "")))
721 ;; If autocompression mode is on,
722 ;; consider all combinations of library suffixes
723 ;; and compression suffixes.
724 (if (rassq 'jka-compr-handler file-name-handler-alist)
726 (mapcar (lambda (compelt)
727 (mapcar (lambda (baselt)
728 (concat baselt compelt))
732 (or path load-path)))
733 (and interactive-call
735 (message "Library is file %s" result)
736 (message "No library %s in search path" library)))
739 ;;; help.el ends here