(facemenu-active-faces):
[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 (view-find (expand-file-name "DISTRIB" data-directory)))
254
255 (defun describe-copying ()
256 "Display info on how you may redistribute copies of GNU Emacs."
257 (interactive)
258 (view-file (expand-file-name "COPYING" data-directory))
259 (goto-char (point-min)))
260
261 (defun describe-project ()
262 "Display info on the GNU project."
263 (interactive)
264 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
265 (goto-char (point-min)))
266
267 (defun describe-no-warranty ()
268 "Display info on all the kinds of warranty Emacs does NOT have."
269 (interactive)
270 (describe-copying)
271 (let (case-fold-search)
272 (search-forward "NO WARRANTY")
273 (recenter 0)))
274
275 (defun describe-prefix-bindings ()
276 "Describe the bindings of the prefix used to reach this command.
277 The prefix described consists of all but the last event
278 of the key sequence that ran this command."
279 (interactive)
280 (let* ((key (this-command-keys)))
281 (describe-bindings
282 (if (stringp key)
283 (substring key 0 (1- (length key)))
284 (let ((prefix (make-vector (1- (length key)) nil))
285 (i 0))
286 (while (< i (length prefix))
287 (aset prefix i (aref key i))
288 (setq i (1+ i)))
289 prefix)))))
290 ;; Make C-h after a prefix, when not specifically bound,
291 ;; run describe-prefix-bindings.
292 (setq prefix-help-command 'describe-prefix-bindings)
293
294 (defun view-emacs-news (&optional arg)
295 "Display info on recent changes to Emacs.
296 With numeric argument, display information on correspondingly older changes."
297 (interactive "P")
298 (let* ((arg (if arg (prefix-numeric-value arg) 0))
299 (file (cond ((eq arg 0) "NEWS")
300 ((eq arg 1) "ONEWS")
301 (t
302 (nth (- arg 2)
303 (nreverse (directory-files data-directory
304 nil "^ONEWS\\.[0-9]+$"
305 nil)))))))
306 (if file
307 (view-file (expand-file-name file data-directory))
308 (error "No such old news"))))
309
310 (defun view-order-manuals ()
311 "Display the Emacs ORDERS file."
312 (interactive)
313 (view-file (expand-file-name "ORDERS" data-directory))
314 (goto-address))
315
316 (defun view-emacs-FAQ ()
317 "Display the Emacs Frequently Asked Questions (FAQ) file."
318 (interactive)
319 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
320 (info "(efaq)"))
321
322 (defun view-emacs-problems ()
323 "Display info on known problems with Emacs and possible workarounds."
324 (interactive)
325 (view-file (expand-file-name "PROBLEMS" data-directory)))
326
327 (defun view-lossage ()
328 "Display last 100 input keystrokes.
329
330 To record all your input on a file, use `open-dribble-file'."
331 (interactive)
332 (help-setup-xref (list #'view-lossage) (interactive-p))
333 (with-output-to-temp-buffer (help-buffer)
334 (princ (mapconcat (function (lambda (key)
335 (if (or (integerp key)
336 (symbolp key)
337 (listp key))
338 (single-key-description key)
339 (prin1-to-string key nil))))
340 (recent-keys)
341 " "))
342 (with-current-buffer standard-output
343 (goto-char (point-min))
344 (while (progn (move-to-column 50) (not (eobp)))
345 (search-forward " " nil t)
346 (insert "\n")))
347 (print-help-return-message)))
348
349 \f
350 ;; Key bindings
351
352 (defun describe-bindings (&optional prefix buffer)
353 "Show a list of all defined keys, and their definitions.
354 We put that list in a buffer, and display the buffer.
355
356 The optional argument PREFIX, if non-nil, should be a key sequence;
357 then we display only bindings that start with that prefix.
358 The optional argument BUFFER specifies which buffer's bindings
359 to display (default, the current buffer)."
360 (interactive)
361 (or buffer (setq buffer (current-buffer)))
362 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
363 (with-current-buffer buffer
364 (describe-bindings-internal nil prefix)))
365
366 ;; This function used to be in keymap.c.
367 (defun describe-bindings-internal (&optional menus prefix)
368 "Show a list of all defined keys, and their definitions.
369 We put that list in a buffer, and display the buffer.
370
371 The optional argument MENUS, if non-nil, says to mention menu bindings.
372 \(Ordinarily these are omitted from the output.)
373 The optional argument PREFIX, if non-nil, should be a key sequence;
374 then we display only bindings that start with that prefix."
375 (interactive)
376 (let ((buf (current-buffer)))
377 (with-output-to-temp-buffer "*Help*"
378 (with-current-buffer standard-output
379 (describe-buffer-bindings buf prefix menus)))))
380
381 (defun where-is (definition &optional insert)
382 "Print message listing key sequences that invoke the command DEFINITION.
383 Argument is a command definition, usually a symbol with a function definition.
384 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
385 (interactive
386 (let ((fn (function-called-at-point))
387 (enable-recursive-minibuffers t)
388 val)
389 (setq val (completing-read (if fn
390 (format "Where is command (default %s): " fn)
391 "Where is command: ")
392 obarray 'commandp t))
393 (list (if (equal val "")
394 fn (intern val))
395 current-prefix-arg)))
396 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
397 (keys1 (mapconcat 'key-description keys ", "))
398 (standard-output (if insert (current-buffer) t)))
399 (if insert
400 (if (> (length keys1) 0)
401 (princ (format "%s (%s)" keys1 definition))
402 (princ (format "M-x %s RET" definition)))
403 (if (> (length keys1) 0)
404 (princ (format "%s is on %s" definition keys1))
405 (princ (format "%s is not on any key" definition)))))
406 nil)
407
408 (defun string-key-binding (key)
409 "Value is the binding of KEY in a string.
410 If KEY is an event on a string, and that string has a `local-map'
411 or `keymap' property, return the binding of KEY in the string's keymap."
412 (let* ((defn nil)
413 (start (when (vectorp key)
414 (if (memq (aref key 0) '(mode-line header-line))
415 (event-start (aref key 1))
416 (and (consp (aref key 0))
417 (event-start (aref key 0))))))
418 (string-info (and (consp start) (nth 4 start))))
419 (when string-info
420 (let* ((string (car string-info))
421 (pos (cdr string-info))
422 (local-map (and (> pos 0)
423 (< pos (length string))
424 (or (get-text-property pos 'local-map string)
425 (get-text-property pos 'keymap string)))))
426 (setq defn (and local-map (lookup-key local-map key)))))
427 defn))
428
429 (defun describe-key-briefly (key &optional insert)
430 "Print the name of the function KEY invokes. KEY is a string.
431 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
432 (interactive "kDescribe key briefly: \nP")
433 (save-excursion
434 (let ((modifiers (event-modifiers (aref key 0)))
435 (standard-output (if insert (current-buffer) t))
436 window position)
437 ;; For a mouse button event, go to the button it applies to
438 ;; to get the right key bindings. And go to the right place
439 ;; in case the keymap depends on where you clicked.
440 (if (or (memq 'click modifiers) (memq 'down modifiers)
441 (memq 'drag modifiers))
442 (setq window (posn-window (event-start (aref key 0)))
443 position (posn-point (event-start (aref key 0)))))
444 (if (windowp window)
445 (progn
446 (set-buffer (window-buffer window))
447 (goto-char position)))
448 ;; Ok, now look up the key and name the command.
449 (let ((defn (or (string-key-binding key)
450 (key-binding key)))
451 (key-desc (key-description key)))
452 (if (or (null defn) (integerp defn))
453 (princ (format "%s is undefined" key-desc))
454 (princ (format (if insert
455 "`%s' (`%s')"
456 (if (windowp window)
457 "%s at that spot runs the command %s"
458 "%s runs the command %s"))
459 key-desc
460 (if (symbolp defn) defn (prin1-to-string defn)))))))))
461
462
463 (defun describe-key (key)
464 "Display documentation of the function invoked by KEY.
465 KEY should be a key sequence--when calling from a program,
466 pass a string or a vector."
467 (interactive "kDescribe key: ")
468 (save-excursion
469 (let ((modifiers (event-modifiers (aref key 0)))
470 window position)
471 ;; For a mouse button event, go to the button it applies to
472 ;; to get the right key bindings. And go to the right place
473 ;; in case the keymap depends on where you clicked.
474 (if (or (memq 'click modifiers) (memq 'down modifiers)
475 (memq 'drag modifiers))
476 (setq window (posn-window (event-start (aref key 0)))
477 position (posn-point (event-start (aref key 0)))))
478 (when (windowp window)
479 (set-buffer (window-buffer window))
480 (goto-char position))
481 (let ((defn (or (string-key-binding key) (key-binding key))))
482 (if (or (null defn) (integerp defn))
483 (message "%s is undefined" (key-description key))
484 (help-setup-xref (list #'describe-function defn) (interactive-p))
485 (with-output-to-temp-buffer (help-buffer)
486 (princ (key-description key))
487 (if (windowp window)
488 (princ " at that spot"))
489 (princ " runs the command ")
490 (prin1 defn)
491 (princ "\n which is ")
492 (describe-function-1 defn)
493 (print-help-return-message)))))))
494
495 \f
496 (defun describe-mode (&optional buffer)
497 "Display documentation of current major mode and minor modes.
498 The major mode description comes first, followed by the minor modes,
499 each on a separate page.
500 For this to work correctly for a minor mode, the mode's indicator variable
501 \(listed in `minor-mode-alist') must also be a function whose documentation
502 describes the minor mode."
503 (interactive)
504 (when buffer (set-buffer buffer))
505 (help-setup-xref (list #'describe-mode (current-buffer)) (interactive-p))
506 (with-output-to-temp-buffer (help-buffer)
507 (when minor-mode-alist
508 (princ "The major mode is described first.
509 For minor modes, see following pages.\n\n"))
510 (princ mode-name)
511 (princ " mode:\n")
512 (princ (documentation major-mode))
513 (let ((minor-modes minor-mode-alist))
514 (while minor-modes
515 (let* ((minor-mode (car (car minor-modes)))
516 (indicator (car (cdr (car minor-modes)))))
517 ;; Document a minor mode if it is listed in minor-mode-alist,
518 ;; bound locally in this buffer, non-nil, and has a function
519 ;; definition.
520 (if (and (boundp minor-mode)
521 (symbol-value minor-mode)
522 (fboundp minor-mode))
523 (let ((pretty-minor-mode minor-mode))
524 (if (string-match "\\(-minor\\)?-mode\\'"
525 (symbol-name minor-mode))
526 (setq pretty-minor-mode
527 (capitalize
528 (substring (symbol-name minor-mode)
529 0 (match-beginning 0)))))
530 (while (and indicator (symbolp indicator)
531 (boundp indicator)
532 (not (eq indicator (symbol-value indicator))))
533 (setq indicator (symbol-value indicator)))
534 (princ "\n\f\n")
535 (princ (format "%s minor mode (%s):\n"
536 pretty-minor-mode
537 (if indicator
538 (format "indicator%s" indicator)
539 "no indicator")))
540 (princ (documentation minor-mode)))))
541 (setq minor-modes (cdr minor-modes))))
542 (print-help-return-message)))
543
544 \f
545 ;;; Automatic resizing of temporary buffers.
546
547 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
548 "*Maximum height of a window displaying a temporary buffer.
549 This is the maximum height (in text lines) which `resize-temp-buffer-window'
550 will give to a window displaying a temporary buffer.
551 It can also be a function which will be called with the object corresponding
552 to the buffer to be displayed as argument and should return an integer
553 positive number."
554 :type '(choice integer function)
555 :group 'help
556 :version "20.4")
557
558 (define-minor-mode temp-buffer-resize-mode
559 "Toggle the mode which makes windows smaller for temporary buffers.
560 With prefix argument ARG, turn the resizing of windows displaying temporary
561 buffers on if ARG is positive or off otherwise.
562 This makes the window the right height for its contents, but never
563 more than `temp-buffer-max-height' nor less than `window-min-height'.
564 This applies to `help', `apropos' and `completion' buffers, and some others."
565 :global t :group 'help
566 (if temp-buffer-resize-mode
567 ;; `help-make-xrefs' may add a `back' button and thus increase the
568 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
569 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
570 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
571
572 (defun resize-temp-buffer-window ()
573 "Resize the current window to fit its contents.
574 Will not make it higher than `temp-buffer-max-height' nor smaller than
575 `window-min-height'. Do nothing if it is the only window on its frame, if it
576 is not as wide as the frame or if some of the window's contents are scrolled
577 out of view."
578 (unless (or (one-window-p 'nomini)
579 (not (pos-visible-in-window-p (point-min)))
580 (/= (frame-width) (window-width)))
581 (fit-window-to-buffer
582 (selected-window)
583 (if (functionp temp-buffer-max-height)
584 (funcall temp-buffer-max-height (current-buffer))
585 temp-buffer-max-height))))
586
587 ;; Provide this for the sake of define-minor-mode which generates
588 ;; defcustoms which require 'help'.
589 (provide 'help)
590
591 ;;; help.el ends here