(tooltip-delay): Decrease to 0.7.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
30
31 ;;; Code:
32
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro))
36
37 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
39 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
40
41 (defvar help-map (make-sparse-keymap)
42 "Keymap for characters following the Help key.")
43
44 (define-key global-map (char-to-string help-char) 'help-command)
45 (define-key global-map [help] 'help-command)
46 (define-key global-map [f1] 'help-command)
47 (fset 'help-command help-map)
48
49 (define-key help-map (char-to-string help-char) 'help-for-help)
50 (define-key help-map [help] 'help-for-help)
51 (define-key help-map [f1] 'help-for-help)
52 (define-key help-map "?" 'help-for-help)
53
54 (define-key help-map "\C-c" 'describe-copying)
55 (define-key help-map "\C-d" 'describe-distribution)
56 (define-key help-map "\C-w" 'describe-no-warranty)
57 (define-key help-map "\C-p" 'describe-project)
58 (define-key help-map "a" 'apropos-command)
59
60 (define-key help-map "b" 'describe-bindings)
61
62 (define-key help-map "c" 'describe-key-briefly)
63 (define-key help-map "k" 'describe-key)
64
65 (define-key help-map "d" 'describe-function)
66 (define-key help-map "f" 'describe-function)
67
68 (define-key help-map "F" 'view-emacs-FAQ)
69
70 (define-key help-map "i" 'info)
71 (define-key help-map "4i" 'info-other-window)
72 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
73 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
74 (define-key help-map "\C-i" 'info-lookup-symbol)
75
76 (define-key help-map "l" 'view-lossage)
77
78 (define-key help-map "m" 'describe-mode)
79 (define-key help-map "\C-m" 'view-order-manuals)
80
81 (define-key help-map "\C-n" 'view-emacs-news)
82 (define-key help-map "n" 'view-emacs-news)
83
84 (define-key help-map "p" 'finder-by-keyword)
85 (autoload 'finder-by-keyword "finder"
86 "Find packages matching a given keyword." t)
87
88 (define-key help-map "P" 'view-emacs-problems)
89
90 (define-key help-map "s" 'describe-syntax)
91
92 (define-key help-map "t" 'help-with-tutorial)
93
94 (define-key help-map "w" 'where-is)
95
96 (define-key help-map "v" 'describe-variable)
97
98 (define-key help-map "q" 'help-quit)
99
100 \f
101 (defun help-quit ()
102 "Just exit from the Help command's command loop."
103 (interactive)
104 nil)
105
106 (defvar help-return-method nil
107 "What to do to \"exit\" the help buffer.
108 This is a list
109 (WINDOW . t) delete the selected window, go to WINDOW.
110 (WINDOW . quit-window) do quit-window, then select WINDOW.
111 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
112
113 (defun print-help-return-message (&optional function)
114 "Display or return message saying how to restore windows after help command.
115 Computes a message and applies the optional argument FUNCTION to it.
116 If FUNCTION is nil, applies `message' to it, thus printing it."
117 (and (not (get-buffer-window standard-output))
118 (let ((first-message
119 (cond ((special-display-p (buffer-name standard-output))
120 (setq help-return-method (cons (selected-window) t))
121 ;; If the help output buffer is a special display buffer,
122 ;; don't say anything about how to get rid of it.
123 ;; First of all, the user will do that with the window
124 ;; manager, not with Emacs.
125 ;; Secondly, the buffer has not been displayed yet,
126 ;; so we don't know whether its frame will be selected.
127 nil)
128 (display-buffer-reuse-frames
129 (setq help-return-method (cons (selected-window)
130 'quit-window))
131 nil)
132 ((not (one-window-p t))
133 (setq help-return-method
134 (cons (selected-window) 'quit-window))
135 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
136 (pop-up-windows
137 (setq help-return-method (cons (selected-window) t))
138 "Type \\[delete-other-windows] to remove help window.")
139 (t
140 (setq help-return-method
141 (list (selected-window) (window-buffer)
142 (window-start) (window-point)))
143 "Type \\[switch-to-buffer] RET to remove help window."))))
144 (funcall (or function 'message)
145 (concat
146 (if first-message
147 (substitute-command-keys first-message))
148 (if first-message " ")
149 ;; If the help buffer will go in a separate frame,
150 ;; it's no use mentioning a command to scroll, so don't.
151 (if (special-display-p (buffer-name standard-output))
152 nil
153 (if (same-window-p (buffer-name standard-output))
154 ;; Say how to scroll this window.
155 (substitute-command-keys
156 "\\[scroll-up] to scroll the help.")
157 ;; Say how to scroll some other window.
158 (substitute-command-keys
159 "\\[scroll-other-window] to scroll the help."))))))))
160
161 ;; So keyboard macro definitions are documented correctly
162 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
163
164 (defalias 'help 'help-for-help)
165 (make-help-screen help-for-help
166 "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:"
167 "You have typed %THIS-KEY%, the help character. Type a Help option:
168 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
169
170 a command-apropos. Give a substring, and see a list of commands
171 (functions interactively callable) that contain
172 that substring. See also the apropos command.
173 b describe-bindings. Display table of all key bindings.
174 c describe-key-briefly. Type a command key sequence;
175 it prints the function name that sequence runs.
176 C describe-coding-system. This describes either a specific coding system
177 (if you type its name) or the coding systems currently in use
178 (if you type just RET).
179 f describe-function. Type a function name and get documentation of it.
180 C-f Info-goto-emacs-command-node. Type a function name;
181 it takes you to the Info node for that command.
182 i info. The info documentation reader.
183 I describe-input-method. Describe a specific input method (if you type
184 its name) or the current input method (if you type just RET).
185 C-i info-lookup-symbol. Display the definition of a specific symbol
186 as found in the manual for the language this buffer is written in.
187 k describe-key. Type a command key sequence;
188 it displays the full documentation.
189 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
190 it takes you to the Info node for the command bound to that key.
191 l view-lossage. Show last 100 characters you typed.
192 L describe-language-environment. This describes either a
193 specific language environment (if you type its name)
194 or the current language environment (if you type just RET).
195 m describe-mode. Print documentation of current minor modes,
196 and the current major mode, including their special commands.
197 n view-emacs-news. Display news of recent Emacs changes.
198 p finder-by-keyword. Find packages matching a given topic keyword.
199 s describe-syntax. Display contents of syntax table, plus explanations.
200 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
201 v describe-variable. Type name of a variable;
202 it displays the variable's documentation and value.
203 w where-is. Type command name; it prints which keystrokes
204 invoke that command.
205
206 F Display the frequently asked questions file.
207 h Display the HELLO file which illustrates various scripts.
208 C-c Display Emacs copying permission (General Public License).
209 C-d Display Emacs ordering information.
210 C-n Display news of recent Emacs changes.
211 C-p Display information about the GNU project.
212 C-w Display information on absence of warranty for GNU Emacs."
213 help-map)
214
215 \f
216
217 (defun function-called-at-point ()
218 "Return a function around point or else called by the list containing point.
219 If that doesn't give a function, return nil."
220 (with-syntax-table emacs-lisp-mode-syntax-table
221 (or (condition-case ()
222 (save-excursion
223 (or (not (zerop (skip-syntax-backward "_w")))
224 (eq (char-syntax (following-char)) ?w)
225 (eq (char-syntax (following-char)) ?_)
226 (forward-sexp -1))
227 (skip-chars-forward "'")
228 (let ((obj (read (current-buffer))))
229 (and (symbolp obj) (fboundp obj) obj)))
230 (error nil))
231 (condition-case ()
232 (save-excursion
233 (save-restriction
234 (narrow-to-region (max (point-min)
235 (- (point) 1000)) (point-max))
236 ;; Move up to surrounding paren, then after the open.
237 (backward-up-list 1)
238 (forward-char 1)
239 ;; If there is space here, this is probably something
240 ;; other than a real Lisp function call, so ignore it.
241 (if (looking-at "[ \t]")
242 (error "Probably not a Lisp function call"))
243 (let ((obj (read (current-buffer))))
244 (and (symbolp obj) (fboundp obj) obj))))
245 (error nil)))))
246
247 \f
248 ;;; `User' help functions
249
250 (defun describe-distribution ()
251 "Display info on how to obtain the latest version of GNU Emacs."
252 (interactive)
253 (find-file-read-only
254 (expand-file-name "DISTRIB" data-directory)))
255
256 (defun describe-copying ()
257 "Display info on how you may redistribute copies of GNU Emacs."
258 (interactive)
259 (find-file-read-only
260 (expand-file-name "COPYING" data-directory))
261 (goto-char (point-min)))
262
263 (defun describe-project ()
264 "Display info on the GNU project."
265 (interactive)
266 (find-file-read-only
267 (expand-file-name "THE-GNU-PROJECT" data-directory))
268 (goto-char (point-min)))
269
270 (defun describe-no-warranty ()
271 "Display info on all the kinds of warranty Emacs does NOT have."
272 (interactive)
273 (describe-copying)
274 (let (case-fold-search)
275 (search-forward "NO WARRANTY")
276 (recenter 0)))
277
278 (defun describe-prefix-bindings ()
279 "Describe the bindings of the prefix used to reach this command.
280 The prefix described consists of all but the last event
281 of the key sequence that ran this command."
282 (interactive)
283 (let* ((key (this-command-keys)))
284 (describe-bindings
285 (if (stringp key)
286 (substring key 0 (1- (length key)))
287 (let ((prefix (make-vector (1- (length key)) nil))
288 (i 0))
289 (while (< i (length prefix))
290 (aset prefix i (aref key i))
291 (setq i (1+ i)))
292 prefix)))))
293 ;; Make C-h after a prefix, when not specifically bound,
294 ;; run describe-prefix-bindings.
295 (setq prefix-help-command 'describe-prefix-bindings)
296
297 (defun view-emacs-news (&optional arg)
298 "Display info on recent changes to Emacs.
299 With numeric argument, display information on correspondingly older changes."
300 (interactive "P")
301 (let* ((arg (if arg (prefix-numeric-value arg) 0))
302 (file (cond ((eq arg 0) "NEWS")
303 ((eq arg 1) "ONEWS")
304 (t
305 (nth (- arg 2)
306 (nreverse (directory-files data-directory
307 nil "^ONEWS\\.[0-9]+$"
308 nil)))))))
309 (if file
310 (find-file-read-only (expand-file-name file data-directory))
311 (error "No such old news"))))
312
313 (defun view-order-manuals ()
314 "Display the Emacs ORDERS file."
315 (interactive)
316 (find-file-read-only (expand-file-name "ORDERS" data-directory))
317 (goto-address))
318
319 (defun view-emacs-FAQ ()
320 "Display the Emacs Frequently Asked Questions (FAQ) file."
321 (interactive)
322 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
323 (info "(efaq)"))
324
325 (defun view-emacs-problems ()
326 "Display info on known problems with Emacs and possible workarounds."
327 (interactive)
328 (view-file (expand-file-name "PROBLEMS" data-directory)))
329
330 (defun view-lossage ()
331 "Display last 100 input keystrokes.
332
333 To record all your input on a file, use `open-dribble-file'."
334 (interactive)
335 (help-setup-xref (list #'view-lossage) (interactive-p))
336 (with-output-to-temp-buffer (help-buffer)
337 (princ (mapconcat (function (lambda (key)
338 (if (or (integerp key)
339 (symbolp key)
340 (listp key))
341 (single-key-description key)
342 (prin1-to-string key nil))))
343 (recent-keys)
344 " "))
345 (with-current-buffer standard-output
346 (goto-char (point-min))
347 (while (progn (move-to-column 50) (not (eobp)))
348 (search-forward " " nil t)
349 (insert "\n")))
350 (print-help-return-message)))
351
352 \f
353 ;; Key bindings
354
355 (defun describe-bindings (&optional prefix buffer)
356 "Show a list of all defined keys, and their definitions.
357 We put that list in a buffer, and display the buffer.
358
359 The optional argument PREFIX, if non-nil, should be a key sequence;
360 then we display only bindings that start with that prefix.
361 The optional argument BUFFER specifies which buffer's bindings
362 to display (default, the current buffer)."
363 (interactive)
364 (or buffer (setq buffer (current-buffer)))
365 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
366 (with-current-buffer buffer
367 (describe-bindings-internal nil prefix)))
368
369 ;; This function used to be in keymap.c.
370 (defun describe-bindings-internal (&optional menus prefix)
371 "Show a list of all defined keys, and their definitions.
372 We put that list in a buffer, and display the buffer.
373
374 The optional argument MENUS, if non-nil, says to mention menu bindings.
375 \(Ordinarily these are omitted from the output.)
376 The optional argument PREFIX, if non-nil, should be a key sequence;
377 then we display only bindings that start with that prefix."
378 (interactive)
379 (let ((buf (current-buffer)))
380 (with-output-to-temp-buffer "*Help*"
381 (with-current-buffer standard-output
382 (describe-buffer-bindings buf prefix menus)))))
383
384 (defun where-is (definition &optional insert)
385 "Print message listing key sequences that invoke the command DEFINITION.
386 Argument is a command definition, usually a symbol with a function definition.
387 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
388 (interactive
389 (let ((fn (function-called-at-point))
390 (enable-recursive-minibuffers t)
391 val)
392 (setq val (completing-read (if fn
393 (format "Where is command (default %s): " fn)
394 "Where is command: ")
395 obarray 'commandp t))
396 (list (if (equal val "")
397 fn (intern val))
398 current-prefix-arg)))
399 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
400 (keys1 (mapconcat 'key-description keys ", "))
401 (standard-output (if insert (current-buffer) t)))
402 (if insert
403 (if (> (length keys1) 0)
404 (princ (format "%s (%s)" keys1 definition))
405 (princ (format "M-x %s RET" definition)))
406 (if (> (length keys1) 0)
407 (princ (format "%s is on %s" definition keys1))
408 (princ (format "%s is not on any key" definition)))))
409 nil)
410
411 (defun string-key-binding (key)
412 "Value is the binding of KEY in a string.
413 If KEY is an event on a string, and that string has a `local-map'
414 or `keymap' property, return the binding of KEY in the string's keymap."
415 (let* ((defn nil)
416 (start (when (vectorp key)
417 (if (memq (aref key 0) '(mode-line header-line))
418 (event-start (aref key 1))
419 (and (consp (aref key 0))
420 (event-start (aref key 0))))))
421 (string-info (and (consp start) (nth 4 start))))
422 (when string-info
423 (let* ((string (car string-info))
424 (pos (cdr string-info))
425 (local-map (and (> pos 0)
426 (< pos (length string))
427 (or (get-text-property pos 'local-map string)
428 (get-text-property pos 'keymap string)))))
429 (setq defn (and local-map (lookup-key local-map key)))))
430 defn))
431
432 (defun describe-key-briefly (key &optional insert)
433 "Print the name of the function KEY invokes. KEY is a string.
434 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
435 (interactive "kDescribe key briefly: \nP")
436 (save-excursion
437 (let ((modifiers (event-modifiers (aref key 0)))
438 (standard-output (if insert (current-buffer) t))
439 window position)
440 ;; For a mouse button event, go to the button it applies to
441 ;; to get the right key bindings. And go to the right place
442 ;; in case the keymap depends on where you clicked.
443 (if (or (memq 'click modifiers) (memq 'down modifiers)
444 (memq 'drag modifiers))
445 (setq window (posn-window (event-start (aref key 0)))
446 position (posn-point (event-start (aref key 0)))))
447 (if (windowp window)
448 (progn
449 (set-buffer (window-buffer window))
450 (goto-char position)))
451 ;; Ok, now look up the key and name the command.
452 (let ((defn (or (string-key-binding key)
453 (key-binding key)))
454 (key-desc (key-description key)))
455 (if (or (null defn) (integerp defn))
456 (princ (format "%s is undefined" key-desc))
457 (princ (format (if insert
458 "`%s' (`%s')"
459 (if (windowp window)
460 "%s at that spot runs the command %s"
461 "%s runs the command %s"))
462 key-desc
463 (if (symbolp defn) defn (prin1-to-string defn)))))))))
464
465
466 (defun describe-key (key)
467 "Display documentation of the function invoked by KEY. KEY is a string."
468 (interactive "kDescribe key: ")
469 (save-excursion
470 (let ((modifiers (event-modifiers (aref key 0)))
471 window position)
472 ;; For a mouse button event, go to the button it applies to
473 ;; to get the right key bindings. And go to the right place
474 ;; in case the keymap depends on where you clicked.
475 (if (or (memq 'click modifiers) (memq 'down modifiers)
476 (memq 'drag modifiers))
477 (setq window (posn-window (event-start (aref key 0)))
478 position (posn-point (event-start (aref key 0)))))
479 (when (windowp window)
480 (set-buffer (window-buffer window))
481 (goto-char position))
482 (let ((defn (or (string-key-binding key) (key-binding key))))
483 (if (or (null defn) (integerp defn))
484 (message "%s is undefined" (key-description key))
485 (help-setup-xref (list #'describe-function defn) (interactive-p))
486 (with-output-to-temp-buffer (help-buffer)
487 (princ (key-description key))
488 (if (windowp window)
489 (princ " at that spot"))
490 (princ " runs the command ")
491 (prin1 defn)
492 (princ "\n which is ")
493 (describe-function-1 defn)
494 (print-help-return-message)))))))
495
496 \f
497 (defun describe-mode (&optional buffer)
498 "Display documentation of current major mode and minor modes.
499 The major mode description comes first, followed by the minor modes,
500 each on a separate page.
501 For this to work correctly for a minor mode, the mode's indicator variable
502 \(listed in `minor-mode-alist') must also be a function whose documentation
503 describes the minor mode."
504 (interactive)
505 (when buffer (set-buffer buffer))
506 (help-setup-xref (list #'describe-mode (current-buffer)) (interactive-p))
507 (with-output-to-temp-buffer (help-buffer)
508 (when minor-mode-alist
509 (princ "The major mode is described first.
510 For minor modes, see following pages.\n\n"))
511 (princ mode-name)
512 (princ " mode:\n")
513 (princ (documentation major-mode))
514 (let ((minor-modes minor-mode-alist))
515 (while minor-modes
516 (let* ((minor-mode (car (car minor-modes)))
517 (indicator (car (cdr (car minor-modes)))))
518 ;; Document a minor mode if it is listed in minor-mode-alist,
519 ;; bound locally in this buffer, non-nil, and has a function
520 ;; definition.
521 (if (and (boundp minor-mode)
522 (symbol-value minor-mode)
523 (fboundp minor-mode))
524 (let ((pretty-minor-mode minor-mode))
525 (if (string-match "\\(-minor\\)?-mode\\'"
526 (symbol-name minor-mode))
527 (setq pretty-minor-mode
528 (capitalize
529 (substring (symbol-name minor-mode)
530 0 (match-beginning 0)))))
531 (while (and indicator (symbolp indicator)
532 (boundp indicator)
533 (not (eq indicator (symbol-value indicator))))
534 (setq indicator (symbol-value indicator)))
535 (princ "\n\f\n")
536 (princ (format "%s minor mode (%s):\n"
537 pretty-minor-mode
538 (if indicator
539 (format "indicator%s" indicator)
540 "no indicator")))
541 (princ (documentation minor-mode)))))
542 (setq minor-modes (cdr minor-modes))))
543 (print-help-return-message)))
544
545 \f
546 ;;; Automatic resizing of temporary buffers.
547
548 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
549 "*Maximum height of a window displaying a temporary buffer.
550 This is the maximum height (in text lines) which `resize-temp-buffer-window'
551 will give to a window displaying a temporary buffer.
552 It can also be a function which will be called with the object corresponding
553 to the buffer to be displayed as argument and should return an integer
554 positive number."
555 :type '(choice integer function)
556 :group 'help
557 :version "20.4")
558
559 (define-minor-mode temp-buffer-resize-mode
560 "Toggle the mode which makes windows smaller for temporary buffers.
561 With prefix argument ARG, turn the resizing of windows displaying temporary
562 buffers on if ARG is positive or off otherwise.
563 This makes the window the right height for its contents, but never
564 more than `temp-buffer-max-height' nor less than `window-min-height'.
565 This applies to `help', `apropos' and `completion' buffers, and some others."
566 :global t :group 'help
567 (if temp-buffer-resize-mode
568 ;; `help-make-xrefs' may add a `back' button and thus increase the
569 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
570 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
571 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
572
573 (defun resize-temp-buffer-window ()
574 "Resize the current window to fit its contents.
575 Will not make it higher than `temp-buffer-max-height' nor smaller than
576 `window-min-height'. Do nothing if it is the only window on its frame, if it
577 is not as wide as the frame or if some of the window's contents are scrolled
578 out of view."
579 (unless (or (one-window-p 'nomini)
580 (not (pos-visible-in-window-p (point-min)))
581 (/= (frame-width) (window-width)))
582 (fit-window-to-buffer
583 (selected-window)
584 (if (functionp temp-buffer-max-height)
585 (funcall temp-buffer-max-height (current-buffer))
586 temp-buffer-max-height))))
587
588 ;; Provide this for the sake of define-minor-mode which generates
589 ;; defcustoms which require 'help'.
590 (provide 'help)
591
592 ;;; help.el ends here