Bind f1 and help like C-h.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
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
29 ;;; Code:
30
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))
34
35 (defvar help-map (make-sparse-keymap)
36 "Keymap for characters following the Help key.")
37
38 (defvar help-mode-map (make-sparse-keymap)
39 "Keymap for help mode.")
40
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)
45
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)
50
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)
56
57 (define-key help-map "b" 'describe-bindings)
58
59 (define-key help-map "c" 'describe-key-briefly)
60 (define-key help-map "k" 'describe-key)
61
62 (define-key help-map "d" 'describe-function)
63 (define-key help-map "f" 'describe-function)
64
65 (define-key help-map "F" 'view-emacs-FAQ)
66
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)
70
71 (define-key help-map "l" 'view-lossage)
72
73 (define-key help-map "m" 'describe-mode)
74
75 (define-key help-map "\C-n" 'view-emacs-news)
76 (define-key help-map "n" 'view-emacs-news)
77
78 (define-key help-map "p" 'finder-by-keyword)
79 (autoload 'finder-by-keyword "finder"
80 "Find packages matching a given keyword." t)
81
82 (define-key help-map "s" 'describe-syntax)
83
84 (define-key help-map "t" 'help-with-tutorial)
85
86 (define-key help-map "w" 'where-is)
87
88 (define-key help-map "v" 'describe-variable)
89
90 (define-key help-map "q" 'help-quit)
91
92 (defun help-mode ()
93 "Major mode for viewing help text.
94 Entry to this mode runs the normal hook `help-mode-hook'.
95 Commands:
96 \\{help-mode-map}"
97 (interactive)
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))
103
104 (defun help-quit ()
105 (interactive)
106 nil)
107
108 (defun help-with-tutorial ()
109 "Select the Emacs learn-by-doing tutorial."
110 (interactive)
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<<")
122 (beginning-of-line)
123 (delete-region (point) (progn (end-of-line) (point)))
124 (let ((n (- (window-height (selected-window))
125 (count-lines (point-min) (point))
126 6)))
127 (if (< n 12)
128 (newline n)
129 ;; Some people get confused by the large gap.
130 (newline (/ n 2))
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))))
136
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)))
145 (and (symbolp type)
146 (memq 'down (event-modifiers type))
147 (read-event)))
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))))))
154
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))
160 (let ((first-message
161 (cond ((or (member (buffer-name standard-output)
162 special-display-buffer-names)
163 (assoc (buffer-name standard-output)
164 special-display-buffer-names)
165 (let (found
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)))
173 (setq found t))
174 (setq tail (cdr tail)))
175 found))
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.
184 nil)
185 ((not (one-window-p t))
186 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
187 (pop-up-windows
188 "Type \\[delete-other-windows] to remove help window.")
189 (t
190 "Type \\[switch-to-buffer] RET to remove help window."))))
191 (funcall (or function 'message)
192 (concat
193 (if first-message
194 (substitute-command-keys first-message)
195 "")
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)))
204 nil
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."))))))))
216
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)))
225 (and (symbolp type)
226 (memq 'down (event-modifiers type))
227 (read-event)))
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*"
232 (prin1 defn)
233 (princ ":\n")
234 (if (documentation defn)
235 (princ (documentation defn))
236 (princ "not documented"))
237 (save-excursion
238 (set-buffer standard-output)
239 (help-mode))
240 (print-help-return-message)))))
241
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."
247 (interactive)
248 (with-output-to-temp-buffer "*Help*"
249 (let ((minor-modes minor-mode-alist)
250 (locals (buffer-local-variables)))
251 (while minor-modes
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
257 ;; definition.
258 (if (and local-binding
259 (cdr 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
264 (capitalize
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))
272 (princ "\n\n"))))
273 (setq minor-modes (cdr minor-modes))))
274 (princ mode-name)
275 (princ " mode:\n")
276 (princ (documentation major-mode))
277 (save-excursion
278 (set-buffer standard-output)
279 (help-mode))
280 (print-help-return-message)))
281
282 ;; So keyboard macro definitions are documented correctly
283 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
284
285 (defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
287 (interactive)
288 (find-file-read-only
289 (expand-file-name "DISTRIB" data-directory)))
290
291 (defun describe-copying ()
292 "Display info on how you may redistribute copies of GNU Emacs."
293 (interactive)
294 (find-file-read-only
295 (expand-file-name "COPYING" data-directory))
296 (goto-char (point-min)))
297
298 (defun describe-project ()
299 "Display info on the GNU project."
300 (interactive)
301 (find-file-read-only
302 (expand-file-name "GNU" data-directory))
303 (goto-char (point-min)))
304
305 (defun describe-no-warranty ()
306 "Display info on all the kinds of warranty Emacs does NOT have."
307 (interactive)
308 (describe-copying)
309 (let (case-fold-search)
310 (search-forward "NO WARRANTY")
311 (recenter 0)))
312
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."
317 (interactive)
318 (let* ((key (this-command-keys)))
319 (describe-bindings
320 (if (stringp key)
321 (substring key 0 (1- (length key)))
322 (let ((prefix (make-vector (1- (length key)) nil))
323 (i 0))
324 (while (< i (length prefix))
325 (aset prefix i (aref key i))
326 (setq i (1+ i)))
327 prefix)))))
328 ;; Make C-h after a prefix, when not specifically bound,
329 ;; run describe-prefix-bindings.
330 (setq prefix-help-command 'describe-prefix-bindings)
331
332 (defun view-emacs-news ()
333 "Display info on recent changes to Emacs."
334 (interactive)
335 (find-file-read-only (expand-file-name "NEWS" data-directory)))
336
337 (defun view-emacs-FAQ ()
338 "Display the Emacs Frequently Asked Questions (FAQ) file."
339 (interactive)
340 (find-file-read-only (expand-file-name "FAQ" data-directory)))
341
342 (defun view-lossage ()
343 "Display last 100 input keystrokes."
344 (interactive)
345 (with-output-to-temp-buffer "*Help*"
346 (princ (mapconcat (function (lambda (key)
347 (if (or (integerp key)
348 (symbolp key)
349 (listp key))
350 (single-key-description key)
351 (prin1-to-string key nil))))
352 (recent-keys)
353 " "))
354 (save-excursion
355 (set-buffer standard-output)
356 (goto-char (point-min))
357 (while (progn (move-to-column 50) (not (eobp)))
358 (search-forward " " nil t)
359 (insert "\n"))
360 (help-mode))
361 (print-help-return-message)))
362
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.)
368
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
394 invoke that command.
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."
400 help-map)
401
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 ()
407 (save-excursion
408 (save-restriction
409 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
410 (backward-up-list 1)
411 (forward-char 1)
412 (let (obj)
413 (setq obj (read (current-buffer)))
414 (and (symbolp obj) (fboundp obj) obj))))
415 (error nil))
416 (condition-case ()
417 (save-excursion
418 (forward-sexp -1)
419 (skip-chars-forward "'")
420 (let ((obj (read (current-buffer))))
421 (and (symbolp obj) (fboundp obj) obj)))
422 (error nil))))
423
424 (defun describe-function-find-file (function)
425 (let ((files load-history)
426 file functions)
427 (while files
428 (if (memq function (cdr (car files)))
429 (setq file (car (car files)) files nil))
430 (setq files (cdr files)))
431 file))
432
433 (defun describe-function (function)
434 "Display the full documentation of FUNCTION (a symbol)."
435 (interactive
436 (let ((fn (function-called-at-point))
437 (enable-recursive-minibuffers t)
438 val)
439 (setq val (completing-read (if fn
440 (format "Describe function (default %s): " fn)
441 "Describe function: ")
442 obarray 'fboundp t))
443 (list (if (equal val "")
444 fn (intern val)))))
445 (with-output-to-temp-buffer "*Help*"
446 (prin1 function)
447 (princ ": ")
448 (let* ((def (symbol-function function))
449 (beg (if (commandp def) "an interactive " "a ")))
450 (princ (cond ((or (stringp def)
451 (vectorp def))
452 "a keyboard macro")
453 ((subrp def)
454 (concat beg "built-in function"))
455 ((byte-code-function-p def)
456 (concat beg "compiled Lisp function"))
457 ((symbolp def)
458 (format "alias for `%s'" def))
459 ((eq (car-safe def) 'lambda)
460 (concat beg "Lisp function"))
461 ((eq (car-safe def) 'macro)
462 "a Lisp 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.
470 ;;; (nth 1 def)
471 ))
472 (t "")))
473 (let ((file (describe-function-find-file function)))
474 (if file
475 (progn
476 (princ " in `")
477 ;; We used to add .el to the file name,
478 ;; but that's completely wrong when the user used load-file.
479 (princ file)
480 (princ "'"))))
481 (princ ".")
482 (terpri)
483 (let ((arglist (cond ((byte-code-function-p def)
484 (car (append def nil)))
485 ((eq (car-safe def) 'lambda)
486 (nth 1 def))
487 (t t))))
488 (if (listp arglist)
489 (progn
490 (princ (cons function
491 (mapcar (lambda (arg)
492 (if (memq arg '(&optional &rest))
493 arg
494 (intern (upcase (symbol-name arg)))))
495 arglist)))
496 (terpri))))
497 (if (documentation function)
498 (progn (terpri)
499 (princ (documentation function)))
500 (princ "not documented"))
501 )
502 (print-help-return-message)
503 (save-excursion
504 (set-buffer standard-output)
505 (help-mode)
506 ;; Return the text we displayed.
507 (buffer-string))))
508
509 (defun variable-at-point ()
510 (condition-case ()
511 (save-excursion
512 (forward-sexp -1)
513 (skip-chars-forward "'")
514 (let ((obj (read (current-buffer))))
515 (and (symbolp obj) (boundp obj) obj)))
516 (error nil)))
517
518 (defun describe-variable (variable)
519 "Display the full documentation of VARIABLE (a symbol).
520 Returns the documentation as a string, also."
521 (interactive
522 (let ((v (variable-at-point))
523 (enable-recursive-minibuffers t)
524 val)
525 (setq val (completing-read (if v
526 (format "Describe variable (default %s): " v)
527 "Describe variable: ")
528 obarray 'boundp t))
529 (list (if (equal val "")
530 v (intern val)))))
531 (with-output-to-temp-buffer "*Help*"
532 (prin1 variable)
533 (if (not (boundp variable))
534 (princ " is void")
535 (princ "'s value is ")
536 (prin1 (symbol-value variable)))
537 (terpri)
538 (if (local-variable-p variable)
539 (progn
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)))
545 (terpri)))
546 (terpri)
547 (princ "Documentation:")
548 (terpri)
549 (let ((doc (documentation-property variable 'variable-documentation)))
550 (if doc
551 (princ (substitute-command-keys doc))
552 (princ "not documented as a variable.")))
553 (print-help-return-message)
554 (save-excursion
555 (set-buffer standard-output)
556 (help-mode)
557 ;; Return the text we displayed.
558 (buffer-string))))
559
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."
563 (interactive
564 (let ((fn (function-called-at-point))
565 (enable-recursive-minibuffers t)
566 val)
567 (setq val (completing-read (if fn
568 (format "Where is command (default %s): " fn)
569 "Where is command: ")
570 obarray 'fboundp t))
571 (list (if (equal val "")
572 fn (intern 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)))
578 nil)
579
580 (defun command-apropos (string)
581 "Like apropos but lists only symbols that are names of commands
582 \(interactively callable functions). Argument REGEXP is a regular expression
583 that is matched against command symbol names. Returns list of symbols and
584 documentation found."
585 (interactive "sCommand apropos (regexp): ")
586 (let ((message
587 (let ((standard-output (get-buffer-create "*Help*")))
588 (print-help-return-message 'identity))))
589 (if (apropos string t 'commandp t)
590 (and message (message message)))))
591
592 (defun locate-library (library &optional nosuffix)
593 "Show the full path name of Emacs library LIBRARY.
594 This command searches the directories in `load-path' like `M-x load-library'
595 to find the file that `M-x load-library RET LIBRARY RET' would load.
596 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
597 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
598 (interactive "sLocate library: ")
599 (catch 'answer
600 (mapcar
601 '(lambda (dir)
602 (mapcar
603 '(lambda (suf)
604 (let ((try (expand-file-name (concat library suf) dir)))
605 (and (file-readable-p try)
606 (null (file-directory-p try))
607 (progn
608 (message "Library is file %s" try)
609 (throw 'answer try)))))
610 (if nosuffix '("") '(".elc" ".el" ""))))
611 load-path)
612 (message "No library %s in search path" library)
613 nil))
614
615 ;;; help.el ends here