(tex-display-shell): Pass nil as arg to
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
d733c5ec 3;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: help, internal
e5167999 7
433ae6f6
RS
8;; This file is part of GNU Emacs.
9
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
433ae6f6
RS
13;; any later version.
14
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.
19
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.
23
d9ecc911
ER
24;;; Commentary:
25
26;; This code implements GNU Emac's on-line help system, the one invoked by
27;;`M-x help-for-help'.
28
e5167999
ER
29;;; Code:
30
8aa3a187
RS
31;; Get the macro make-help-screen when this is compiled,
32;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 33(eval-when-compile (require 'help-macro))
41b8542b 34
433ae6f6
RS
35(defvar help-map (make-sparse-keymap)
36 "Keymap for characters following the Help key.")
37
afaa65e4
KH
38(defvar help-mode-map (make-sparse-keymap)
39 "Keymap for help mode.")
40
e17d2fd1 41(define-key global-map (char-to-string help-char) 'help-command)
433ae6f6
RS
42(fset 'help-command help-map)
43
e17d2fd1 44(define-key help-map (char-to-string help-char) 'help-for-help)
433ae6f6
RS
45(define-key help-map "?" 'help-for-help)
46
47(define-key help-map "\C-c" 'describe-copying)
48(define-key help-map "\C-d" 'describe-distribution)
49(define-key help-map "\C-w" 'describe-no-warranty)
76766f2d 50(define-key help-map "\C-p" 'describe-project)
433ae6f6
RS
51(define-key help-map "a" 'command-apropos)
52
53(define-key help-map "b" 'describe-bindings)
54
55(define-key help-map "c" 'describe-key-briefly)
56(define-key help-map "k" 'describe-key)
57
58(define-key help-map "d" 'describe-function)
59(define-key help-map "f" 'describe-function)
60
61(define-key help-map "i" 'info)
e5d77022
JB
62(define-key help-map "\C-f" 'Info-goto-emacs-command-node)
63(define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
433ae6f6
RS
64
65(define-key help-map "l" 'view-lossage)
66
67(define-key help-map "m" 'describe-mode)
68
69(define-key help-map "\C-n" 'view-emacs-news)
70(define-key help-map "n" 'view-emacs-news)
71
06b98c51 72(define-key help-map "p" 'finder-by-keyword)
3e9c095d
RS
73(autoload 'finder-by-keyword "finder"
74 "Find packages matching a given keyword." t)
06b98c51 75
433ae6f6
RS
76(define-key help-map "s" 'describe-syntax)
77
78(define-key help-map "t" 'help-with-tutorial)
79
80(define-key help-map "w" 'where-is)
81
82(define-key help-map "v" 'describe-variable)
83
2fc9d9f4
RS
84(define-key help-map "q" 'help-quit)
85
afaa65e4
KH
86(defun help-mode ()
87 "Major mode for viewing help text.
88Entry to this mode runs the normal hook `help-mode-hook'.
89Commands:
90\\{help-mode-map}"
91 (interactive)
92 (kill-all-local-variables)
93 (use-local-map help-mode-map)
94 (setq mode-name "Help")
95 (setq major-mode 'help-mode)
96 (run-hooks 'help-mode-hook))
97
2fc9d9f4
RS
98(defun help-quit ()
99 (interactive)
100 nil)
101
433ae6f6
RS
102(defun help-with-tutorial ()
103 "Select the Emacs learn-by-doing tutorial."
104 (interactive)
105 (let ((file (expand-file-name "~/TUTORIAL")))
106 (delete-other-windows)
107 (if (get-file-buffer file)
108 (switch-to-buffer (get-file-buffer file))
109 (switch-to-buffer (create-file-buffer file))
110 (setq buffer-file-name file)
111 (setq default-directory (expand-file-name "~/"))
79058860 112 (setq buffer-auto-save-file-name nil)
1e6dacf6 113 (insert-file-contents (expand-file-name "TUTORIAL" data-directory))
433ae6f6
RS
114 (goto-char (point-min))
115 (search-forward "\n<<")
116 (beginning-of-line)
117 (delete-region (point) (progn (end-of-line) (point)))
857a1de6 118 (let ((n (- (window-height (selected-window))
433ae6f6 119 (count-lines (point-min) (point))
857a1de6 120 6)))
d0da2301 121 (if (< n 12)
857a1de6
KH
122 (newline n)
123 ;; Some people get confused by the large gap.
124 (newline (/ n 2))
125 (insert "[Middle of page left blank for didactic purposes. "
126 "Text continues below]")
127 (newline (- n (/ n 2)))))
433ae6f6
RS
128 (goto-char (point-min))
129 (set-buffer-modified-p nil))))
130
131(defun describe-key-briefly (key)
132 "Print the name of the function KEY invokes. KEY is a string."
133 (interactive "kDescribe key briefly: ")
5f296b78
RS
134 ;; If this key seq ends with a down event, discard the
135 ;; following click or drag event. Otherwise that would
136 ;; erase the message.
137 (let ((type (aref key (1- (length key)))))
138 (if (listp type) (setq type (car type)))
139 (and (symbolp type)
140 (memq 'down (event-modifiers type))
fca4b775 141 (read-event)))
433ae6f6
RS
142 (let ((defn (key-binding key)))
143 (if (or (null defn) (integerp defn))
144 (message "%s is undefined" (key-description key))
145 (message "%s runs the command %s"
146 (key-description key)
147 (if (symbolp defn) defn (prin1-to-string defn))))))
148
149(defun print-help-return-message (&optional function)
150 "Display or return message saying how to restore windows after help command.
151Computes a message and applies the optional argument FUNCTION to it.
152If FUNCTION is nil, applies `message' to it, thus printing it."
153 (and (not (get-buffer-window standard-output))
d536293f
RS
154 (let ((first-message
155 (cond ((or (member (buffer-name standard-output)
156 special-display-buffer-names)
34b0ef48
RS
157 (assoc (buffer-name standard-output)
158 special-display-buffer-names)
d536293f
RS
159 (let (found
160 (tail special-display-regexps)
161 (name (buffer-name standard-output)))
162 (while (and tail (not found))
afaa65e4 163 (if (or (and (consp (car tail))
34b0ef48
RS
164 (string-match (car (car tail)) name))
165 (and (stringp (car tail))
166 (string-match (car tail) name)))
d536293f
RS
167 (setq found t))
168 (setq tail (cdr tail)))
169 found))
170 ;; If the help output buffer is a special display buffer,
171 ;; don't say anything about how to get rid of it.
172 ;; First of all, the user will do that with the window
173 ;; manager, not with Emacs.
174 ;; Secondly, the buffer has not been displayed yet,
175 ;; so we don't know whether its frame will be selected.
176 ;; Even the message about scrolling the help
177 ;; might be wrong, but it seems worth showing it anyway.
178 nil)
179 ((not (one-window-p t))
180 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
181 (pop-up-windows
182 "Type \\[delete-other-windows] to remove help window.")
183 (t
184 "Type \\[switch-to-buffer] RET to remove help window."))))
185 (funcall (or function 'message)
186 (concat
187 (if first-message
188 (substitute-command-keys first-message)
189 "")
190 (if first-message " " "")
191 (substitute-command-keys
192 "\\[scroll-other-window] to scroll the help."))))))
433ae6f6
RS
193
194(defun describe-key (key)
195 "Display documentation of the function invoked by KEY. KEY is a string."
196 (interactive "kDescribe key: ")
5f296b78
RS
197 ;; If this key seq ends with a down event, discard the
198 ;; following click or drag event. Otherwise that would
199 ;; erase the message.
200 (let ((type (aref key (1- (length key)))))
201 (if (listp type) (setq type (car type)))
202 (and (symbolp type)
203 (memq 'down (event-modifiers type))
204 (read-event)))
433ae6f6
RS
205 (let ((defn (key-binding key)))
206 (if (or (null defn) (integerp defn))
207 (message "%s is undefined" (key-description key))
208 (with-output-to-temp-buffer "*Help*"
209 (prin1 defn)
210 (princ ":\n")
211 (if (documentation defn)
212 (princ (documentation defn))
213 (princ "not documented"))
c46bbd92
KH
214 (save-excursion
215 (set-buffer standard-output)
216 (help-mode))
433ae6f6
RS
217 (print-help-return-message)))))
218
ad023904
RS
219(defun describe-mode ()
220 "Display documentation of current major mode and minor modes.
433ae6f6 221For this to work correctly for a minor mode, the mode's indicator variable
61c6b658 222\(listed in `minor-mode-alist') must also be a function whose documentation
433ae6f6 223describes the minor mode."
7192540b 224 (interactive)
433ae6f6 225 (with-output-to-temp-buffer "*Help*"
7192540b
RS
226 (let ((minor-modes minor-mode-alist)
227 (locals (buffer-local-variables)))
228 (while minor-modes
229 (let* ((minor-mode (car (car minor-modes)))
230 (indicator (car (cdr (car minor-modes))))
231 (local-binding (assq minor-mode locals)))
232 ;; Document a minor mode if it is listed in minor-mode-alist,
233 ;; bound locally in this buffer, non-nil, and has a function
234 ;; definition.
235 (if (and local-binding
236 (cdr local-binding)
237 (fboundp minor-mode))
238 (let ((pretty-minor-mode minor-mode))
239 (if (string-match "-mode$" (symbol-name minor-mode))
240 (setq pretty-minor-mode
241 (capitalize
242 (substring (symbol-name minor-mode)
243 0 (match-beginning 0)))))
244 (while (and indicator (symbolp indicator))
245 (setq indicator (symbol-value indicator)))
246 (princ (format "%s minor mode (indicator%s):\n"
247 pretty-minor-mode indicator))
248 (princ (documentation minor-mode))
249 (princ "\n\n"))))
250 (setq minor-modes (cdr minor-modes))))
433ae6f6 251 (princ mode-name)
ad023904 252 (princ " mode:\n")
433ae6f6 253 (princ (documentation major-mode))
c46bbd92
KH
254 (save-excursion
255 (set-buffer standard-output)
256 (help-mode))
433ae6f6
RS
257 (print-help-return-message)))
258
259;; So keyboard macro definitions are documented correctly
260(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
261
262(defun describe-distribution ()
263 "Display info on how to obtain the latest version of GNU Emacs."
264 (interactive)
265 (find-file-read-only
1e6dacf6 266 (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
267
268(defun describe-copying ()
269 "Display info on how you may redistribute copies of GNU Emacs."
270 (interactive)
271 (find-file-read-only
1e6dacf6 272 (expand-file-name "COPYING" data-directory))
433ae6f6
RS
273 (goto-char (point-min)))
274
76766f2d
RS
275(defun describe-project ()
276 "Display info on the GNU project."
277 (interactive)
278 (find-file-read-only
279 (expand-file-name "GNU" data-directory))
280 (goto-char (point-min)))
281
433ae6f6
RS
282(defun describe-no-warranty ()
283 "Display info on all the kinds of warranty Emacs does NOT have."
284 (interactive)
285 (describe-copying)
286 (let (case-fold-search)
287 (search-forward "NO WARRANTY")
288 (recenter 0)))
289
61c6b658 290(defun describe-prefix-bindings ()
c7cba9cb
RS
291 "Describe the bindings of the prefix used to reach this command.
292The prefix described consists of all but the last event
293of the key sequence that ran this command."
61c6b658
RS
294 (interactive)
295 (let* ((key (this-command-keys))
296 (prefix (make-vector (1- (length key)) nil))
297 i)
298 (setq i 0)
299 (while (< i (length prefix))
300 (aset prefix i (aref key i))
301 (setq i (1+ i)))
302 (describe-bindings prefix)))
c7cba9cb
RS
303;; Make C-h after a prefix, when not specifically bound,
304;; run describe-prefix-bindings.
61c6b658
RS
305(setq prefix-help-command 'describe-prefix-bindings)
306
433ae6f6
RS
307(defun view-emacs-news ()
308 "Display info on recent changes to Emacs."
309 (interactive)
1e6dacf6 310 (find-file-read-only (expand-file-name "NEWS" data-directory)))
433ae6f6
RS
311
312(defun view-lossage ()
313 "Display last 100 input keystrokes."
314 (interactive)
315 (with-output-to-temp-buffer "*Help*"
298a7c8c
RS
316 (princ (mapconcat (function (lambda (key)
317 (if (or (integerp key)
318 (symbolp key)
319 (listp key))
320 (single-key-description key)
321 (prin1-to-string key nil))))
322 (recent-keys)
323 " "))
433ae6f6
RS
324 (save-excursion
325 (set-buffer standard-output)
326 (goto-char (point-min))
327 (while (progn (move-to-column 50) (not (eobp)))
328 (search-forward " " nil t)
c46bbd92
KH
329 (insert "\n"))
330 (help-mode))
433ae6f6
RS
331 (print-help-return-message)))
332
2fc9d9f4 333(defalias 'help 'help-for-help)
41b8542b 334(make-help-screen help-for-help
224d56a3 335 "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:"
76766f2d 336 "You have typed \\[help-command], the help character. Type a Help option:
efcce2d2 337\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
433ae6f6 338
21ee8c42
RM
339a command-apropos. Give a substring, and see a list of commands
340 (functions interactively callable) that contain
341 that substring. See also the apropos command.
af6a9de9
RS
342b describe-bindings. Display table of all key bindings.
343c describe-key-briefly. Type a command key sequence;
21ee8c42 344 it prints the function name that sequence runs.
af6a9de9 345f describe-function. Type a function name and get documentation of it.
21ee8c42
RM
346C-f Info-goto-emacs-command-node. Type a function name;
347 it takes you to the Info node for that command.
af6a9de9
RS
348i info. The info documentation reader.
349k describe-key. Type a command key sequence;
21ee8c42
RM
350 it displays the full documentation.
351C-k Info-goto-emacs-key-command-node. Type a command key sequence;
352 it takes you to the Info node for the command bound to that key.
af6a9de9
RS
353l view-lossage. Shows last 100 characters you typed.
354m describe-mode. Print documentation of current major mode,
21ee8c42 355 which describes the commands peculiar to it.
af6a9de9
RS
356n view-emacs-news. Shows emacs news file.
357p finder-by-keyword. Find packages matching a given topic keyword.
358s describe-syntax. Display contents of syntax table, plus explanations
359t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
360v describe-variable. Type name of a variable;
21ee8c42 361 it displays the variable's documentation and value.
af6a9de9 362w where-is. Type command name; it prints which keystrokes
21ee8c42 363 invoke that command.
433ae6f6
RS
364C-c print Emacs copying permission (General Public License).
365C-d print Emacs ordering information.
366C-n print news of recent Emacs changes.
76766f2d 367C-p print information about the GNU project.
433ae6f6 368C-w print information on absence of warranty for GNU Emacs."
41b8542b 369 help-map)
433ae6f6
RS
370
371;; Return a function which is called by the list containing point.
372;; If that gives no function, return a function whose name is around point.
373;; If that doesn't give a function, return nil.
374(defun function-called-at-point ()
375 (or (condition-case ()
376 (save-excursion
377 (save-restriction
378 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
379 (backward-up-list 1)
380 (forward-char 1)
381 (let (obj)
382 (setq obj (read (current-buffer)))
383 (and (symbolp obj) (fboundp obj) obj))))
384 (error nil))
385 (condition-case ()
386 (save-excursion
387 (forward-sexp -1)
388 (skip-chars-forward "'")
389 (let ((obj (read (current-buffer))))
390 (and (symbolp obj) (fboundp obj) obj)))
391 (error nil))))
392
ca5ed196
RS
393(defun describe-function-find-file (function)
394 (let ((files load-history)
395 file functions)
396 (while files
397 (if (memq function (cdr (car files)))
398 (setq file (car (car files)) files nil))
399 (setq files (cdr files)))
400 file))
401
433ae6f6
RS
402(defun describe-function (function)
403 "Display the full documentation of FUNCTION (a symbol)."
404 (interactive
405 (let ((fn (function-called-at-point))
406 (enable-recursive-minibuffers t)
407 val)
408 (setq val (completing-read (if fn
409 (format "Describe function (default %s): " fn)
410 "Describe function: ")
411 obarray 'fboundp t))
412 (list (if (equal val "")
413 fn (intern val)))))
414 (with-output-to-temp-buffer "*Help*"
415 (prin1 function)
416 (princ ": ")
417 (let* ((def (symbol-function function))
418 (beg (if (commandp def) "an interactive " "a ")))
7f26b180
RM
419 (princ (cond ((or (stringp def)
420 (vectorp def))
6f6bfb85 421 "a keyboard macro")
433ae6f6 422 ((subrp def)
6f6bfb85 423 (concat beg "built-in function"))
dbc4e1c1 424 ((byte-code-function-p def)
6f6bfb85 425 (concat beg "compiled Lisp function"))
433ae6f6 426 ((symbolp def)
6f6bfb85 427 (format "alias for `%s'" def))
433ae6f6 428 ((eq (car-safe def) 'lambda)
6f6bfb85 429 (concat beg "Lisp function"))
433ae6f6 430 ((eq (car-safe def) 'macro)
6f6bfb85 431 "a Lisp macro")
433ae6f6 432 ((eq (car-safe def) 'mocklisp)
6f6bfb85 433 "a mocklisp function")
433ae6f6 434 ((eq (car-safe def) 'autoload)
6f6bfb85 435 (format "%s autoloaded Lisp %s"
762435dd 436 (if (commandp def) "an interactive" "an")
433ae6f6 437 (if (nth 4 def) "macro" "function")
ab67260b
RS
438;;; Including the file name made this line too long.
439;;; (nth 1 def)
440 ))
433ae6f6 441 (t "")))
ca5ed196
RS
442 (let ((file (describe-function-find-file function)))
443 (if file
444 (progn
445 (princ " in `")
159a4895
RS
446 ;; We used to add .el to the file name,
447 ;; but that's completely wrong when the user used load-file.
a5743acc
RS
448 (princ file)
449 (princ "'"))))
6f6bfb85 450 (princ ".")
4591cb90 451 (terpri)
7f26b180
RM
452 (let ((arglist (cond ((byte-code-function-p def)
453 (car (append def nil)))
454 ((eq (car-safe def) 'lambda)
455 (nth 1 def))
456 (t t))))
457 (if (listp arglist)
458 (progn
459 (princ (cons function
460 (mapcar (lambda (arg)
461 (if (memq arg '(&optional &rest))
462 arg
463 (intern (upcase (symbol-name arg)))))
464 arglist)))
465 (terpri))))
4591cb90 466 (if (documentation function)
cdd672cc
RS
467 (progn (terpri)
468 (princ (documentation function)))
4591cb90 469 (princ "not documented"))
7f26b180 470 )
433ae6f6 471 (print-help-return-message)
c46bbd92
KH
472 (save-excursion
473 (set-buffer standard-output)
474 (help-mode)
475 ;; Return the text we displayed.
476 (buffer-string))))
433ae6f6
RS
477
478(defun variable-at-point ()
479 (condition-case ()
480 (save-excursion
481 (forward-sexp -1)
482 (skip-chars-forward "'")
483 (let ((obj (read (current-buffer))))
484 (and (symbolp obj) (boundp obj) obj)))
485 (error nil)))
486
487(defun describe-variable (variable)
488 "Display the full documentation of VARIABLE (a symbol).
489Returns the documentation as a string, also."
490 (interactive
491 (let ((v (variable-at-point))
492 (enable-recursive-minibuffers t)
493 val)
494 (setq val (completing-read (if v
495 (format "Describe variable (default %s): " v)
496 "Describe variable: ")
497 obarray 'boundp t))
498 (list (if (equal val "")
499 v (intern val)))))
500 (with-output-to-temp-buffer "*Help*"
501 (prin1 variable)
502 (princ "'s value is ")
503 (if (not (boundp variable))
504 (princ "void.")
505 (prin1 (symbol-value variable)))
7e76ae23
RS
506 (terpri)
507 (let ((locals (buffer-local-variables))
508 is-local)
509 (while locals
510 (if (or (eq variable (car locals))
511 (eq variable (car-safe (car locals))))
512 (setq is-local t locals nil))
513 (setq locals (cdr locals)))
514 (if is-local
515 (princ (format "Local in buffer %s\n" (buffer-name)))))
516 (terpri)
433ae6f6
RS
517 (princ "Documentation:")
518 (terpri)
519 (let ((doc (documentation-property variable 'variable-documentation)))
520 (if doc
521 (princ (substitute-command-keys doc))
522 (princ "not documented as a variable.")))
523 (print-help-return-message)
c46bbd92
KH
524 (save-excursion
525 (set-buffer standard-output)
526 (help-mode)
527 ;; Return the text we displayed.
528 (buffer-string))))
433ae6f6 529
54c0b967
RS
530(defun where-is (definition)
531 "Print message listing key sequences that invoke specified command.
532Argument is a command definition, usually a symbol with a function definition."
533 (interactive
534 (let ((fn (function-called-at-point))
535 (enable-recursive-minibuffers t)
536 val)
537 (setq val (completing-read (if fn
538 (format "Where is command (default %s): " fn)
539 "Where is command: ")
540 obarray 'fboundp t))
541 (list (if (equal val "")
542 fn (intern val)))))
543 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
544 (keys1 (mapconcat 'key-description keys ", ")))
545 (if (> (length keys1) 0)
546 (message "%s is on %s" definition keys1)
547 (message "%s is not on any key" definition)))
548 nil)
549
433ae6f6
RS
550(defun command-apropos (string)
551 "Like apropos but lists only symbols that are names of commands
552\(interactively callable functions). Argument REGEXP is a regular expression
553that is matched against command symbol names. Returns list of symbols and
554documentation found."
555 (interactive "sCommand apropos (regexp): ")
556 (let ((message
557 (let ((standard-output (get-buffer-create "*Help*")))
558 (print-help-return-message 'identity))))
cba9a9dc 559 (if (apropos string t 'commandp t)
78e3ef3c 560 (and message (message message)))))
433ae6f6
RS
561
562(defun locate-library (library &optional nosuffix)
563 "Show the full path name of Emacs library LIBRARY.
564This command searches the directories in `load-path' like `M-x load-library'
565to find the file that `M-x load-library RET LIBRARY RET' would load.
566Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
567to the specified name LIBRARY (a la calling `load' instead of `load-library')."
568 (interactive "sLocate library: ")
569 (catch 'answer
570 (mapcar
571 '(lambda (dir)
572 (mapcar
573 '(lambda (suf)
574 (let ((try (expand-file-name (concat library suf) dir)))
575 (and (file-readable-p try)
576 (null (file-directory-p try))
577 (progn
578 (message "Library is file %s" try)
579 (throw 'answer try)))))
580 (if nosuffix '("") '(".elc" ".el" ""))))
581 load-path)
582 (message "No library %s in search path" library)
583 nil))
1a06eabd
ER
584
585;;; help.el ends here