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" 'word-help)
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 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:"
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 f describe-function. Type a function name and get documentation of it.
433 C-f Info-goto-emacs-command-node. Type a function name;
434 it takes you to the Info node for that command.
435 F view-emacs-FAQ. Shows emacs frequently asked questions file.
436 i info. The info documentation reader.
437 k describe-key. Type a command key sequence;
438 it displays the full documentation.
439 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
440 it takes you to the Info node for the command bound to that key.
441 l view-lossage. Shows last 100 characters you typed.
442 m describe-mode. Print documentation of current major mode,
443 which describes the commands peculiar to it.
444 n view-emacs-news. Shows emacs news file.
445 p finder-by-keyword. Find packages matching a given topic keyword.
446 s describe-syntax. Display contents of syntax table, plus explanations
447 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
448 v describe-variable. Type name of a variable;
449 it displays the variable's documentation and value.
450 w where-is. Type command name; it prints which keystrokes
452 C-c print Emacs copying permission (General Public License).
453 C-d print Emacs ordering information.
454 C-n print news of recent Emacs changes.
455 C-p print information about the GNU project.
456 C-w print information on absence of warranty for GNU Emacs."
459 ;; Return a function which is called by the list containing point.
460 ;; If that gives no function, return a function whose name is around point.
461 ;; If that doesn't give a function, return nil.
462 (defun function-called-at-point ()
463 (or (condition-case ()
466 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
470 (setq obj (read (current-buffer)))
471 (and (symbolp obj) (fboundp obj) obj))))
474 (let ((stab (syntax-table)))
477 (set-syntax-table emacs-lisp-mode-syntax-table)
478 (or (not (zerop (skip-syntax-backward "_w")))
479 (eq (char-syntax (following-char)) ?w)
480 (eq (char-syntax (following-char)) ?_)
482 (skip-chars-forward "'")
483 (let ((obj (read (current-buffer))))
484 (and (symbolp obj) (fboundp obj) obj)))
485 (set-syntax-table stab)))
488 (defun describe-function-find-file (function)
489 (let ((files load-history)
492 (if (memq function (cdr (car files)))
493 (setq file (car (car files)) files nil))
494 (setq files (cdr files)))
497 (defun describe-function (function)
498 "Display the full documentation of FUNCTION (a symbol)."
500 (let ((fn (function-called-at-point))
501 (enable-recursive-minibuffers t)
503 (setq val (completing-read (if fn
504 (format "Describe function (default %s): " fn)
505 "Describe function: ")
507 (list (if (equal val "")
510 (with-output-to-temp-buffer "*Help*"
513 (let* ((def (symbol-function function))
515 (beg (if (commandp def) "an interactive " "a ")))
516 (princ (cond ((or (stringp def)
520 (concat beg "built-in function"))
521 ((byte-code-function-p def)
522 (concat beg "compiled Lisp function"))
524 (format "alias for `%s'" def))
525 ((eq (car-safe def) 'lambda)
526 (concat beg "Lisp function"))
527 ((eq (car-safe def) 'macro)
529 ((eq (car-safe def) 'mocklisp)
530 "a mocklisp function")
531 ((eq (car-safe def) 'autoload)
532 (setq file-name (nth 1 def))
533 (format "%s autoloaded Lisp %s"
534 (if (commandp def) "an interactive" "an")
535 (if (nth 4 def) "macro" "function")
539 (setq file-name (describe-function-find-file function)))
543 ;; We used to add .el to the file name,
544 ;; but that's completely wrong when the user used load-file.
549 (let ((arglist (cond ((byte-code-function-p def)
550 (car (append def nil)))
551 ((eq (car-safe def) 'lambda)
556 (princ (cons function
557 (mapcar (lambda (arg)
558 (if (memq arg '(&optional &rest))
560 (intern (upcase (symbol-name arg)))))
563 (let ((doc (documentation function)))
567 (princ "not documented"))))
568 (print-help-return-message)
570 (set-buffer standard-output)
572 ;; Return the text we displayed.
574 (message "You didn't specify a function")))
576 ;; We return 0 if we can't find a variable to return.
577 (defun variable-at-point ()
579 (let ((stab (syntax-table)))
582 (set-syntax-table emacs-lisp-mode-syntax-table)
583 (or (not (zerop (skip-syntax-backward "_w")))
584 (eq (char-syntax (following-char)) ?w)
585 (eq (char-syntax (following-char)) ?_)
587 (skip-chars-forward "'")
588 (let ((obj (read (current-buffer))))
589 (or (and (symbolp obj) (boundp obj) obj)
591 (set-syntax-table stab)))
594 (defun describe-variable (variable)
595 "Display the full documentation of VARIABLE (a symbol).
596 Returns the documentation as a string, also."
598 (let ((v (variable-at-point))
599 (enable-recursive-minibuffers t)
601 (setq val (completing-read (if (symbolp v)
602 (format "Describe variable (default %s): " v)
603 "Describe variable: ")
605 (list (if (equal val "")
607 (if (symbolp variable)
609 (with-output-to-temp-buffer "*Help*"
611 (if (not (boundp variable))
616 (princ "'s value is ")
618 (pp (symbol-value variable))
620 (if (local-variable-p variable)
622 (princ (format "Local in buffer %s; " (buffer-name)))
623 (if (not (default-boundp variable))
624 (princ "globally void")
625 (princ "global value is ")
627 (pp (default-value variable)))
631 (set-buffer standard-output)
632 (if (> (count-lines (point-min) (point-max)) 10)
634 (goto-char (point-min))
638 (delete-region (point) (progn (end-of-line) (point)))
639 (insert "'s value is shown below.\n\n")
641 (insert "\n\nValue:"))))))
642 (princ "Documentation:")
644 (let ((doc (documentation-property variable 'variable-documentation)))
645 (princ (or doc "not documented as a variable.")))
646 (print-help-return-message)
648 (set-buffer standard-output)
650 ;; Return the text we displayed.
652 (message "You did not specify a variable")))
654 (defun where-is (definition &optional insert)
655 "Print message listing key sequences that invoke specified command.
656 Argument is a command definition, usually a symbol with a function definition.
657 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
659 (let ((fn (function-called-at-point))
660 (enable-recursive-minibuffers t)
662 (setq val (completing-read (if fn
663 (format "Where is command (default %s): " fn)
664 "Where is command: ")
666 (list (if (equal val "")
668 current-prefix-arg)))
669 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
670 (keys1 (mapconcat 'key-description keys ", "))
671 (standard-output (if insert (current-buffer) t)))
673 (if (> (length keys1) 0)
674 (princ (format "%s (%s)" keys1 definition))
675 (princ (format "M-x %s RET" definition)))
676 (if (> (length keys1) 0)
677 (princ (format "%s is on %s" definition keys1))
678 (princ (format "%s is not on any key" definition)))))
681 (defun locate-library (library &optional nosuffix path interactive-call)
682 "Show the precise file name of Emacs library LIBRARY.
683 This command searches the directories in `load-path' like `M-x load-library'
684 to find the file that `M-x load-library RET LIBRARY RET' would load.
685 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
686 to the specified name LIBRARY.
688 If the optional third arg PATH is specified, that list of directories
689 is used instead of `load-path'."
690 (interactive (list (read-string "Locate library: ")
699 (let ((try (expand-file-name (concat library suf) dir)))
700 (and (file-readable-p try)
701 (null (file-directory-p try))
704 (throw 'answer try)))))
707 (let ((basic '(".elc" ".el" ""))
708 (compressed '(".Z" ".gz" "")))
709 ;; If autocompression mode is on,
710 ;; consider all combinations of library suffixes
711 ;; and compression suffixes.
712 (if (rassq 'jka-compr-handler file-name-handler-alist)
714 (mapcar (lambda (compelt)
715 (mapcar (lambda (baselt)
716 (concat baselt compelt))
720 (or path load-path)))
721 (and interactive-call
723 (message "Library is file %s" result)
724 (message "No library %s in search path" library)))
727 ;;; help.el ends here