* NEWS: explain new behavior and arguments of `key-binding' and
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
0d30b337 3;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
aaef169d 4;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: help, internal
e5167999 8
433ae6f6
RS
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
433ae6f6
RS
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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
433ae6f6 25
d9ecc911
ER
26;;; Commentary:
27
a1c9f209 28;; This code implements GNU Emacs' on-line help system, the one invoked by
95ac0a6f 29;; `M-x help-for-help'.
d9ecc911 30
e5167999
ER
31;;; Code:
32
8aa3a187
RS
33;; Get the macro make-help-screen when this is compiled,
34;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 35(eval-when-compile (require 'help-macro))
788d62cf
MB
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)
41b8542b 40
604aa5f0
SM
41(defvar help-map
42 (let ((map (make-sparse-keymap)))
43 (define-key map (char-to-string help-char) 'help-for-help)
44 (define-key map [help] 'help-for-help)
45 (define-key map [f1] 'help-for-help)
46 (define-key map "." 'display-local-help)
47 (define-key map "?" 'help-for-help)
48
49 (define-key map "\C-c" 'describe-copying)
50 (define-key map "\C-d" 'describe-distribution)
51 (define-key map "\C-e" 'view-emacs-problems)
52 (define-key map "\C-f" 'view-emacs-FAQ)
53 (define-key map "\C-m" 'view-order-manuals)
54 (define-key map "\C-n" 'view-emacs-news)
55 (define-key map "\C-p" 'describe-project)
56 (define-key map "\C-t" 'view-todo)
57 (define-key map "\C-w" 'describe-no-warranty)
58
59 ;; This does not fit the pattern, but it is natural given the C-\ command.
60 (define-key map "\C-\\" 'describe-input-method)
61
62 (define-key map "C" 'describe-coding-system)
63 (define-key map "F" 'Info-goto-emacs-command-node)
64 (define-key map "I" 'describe-input-method)
65 (define-key map "K" 'Info-goto-emacs-key-command-node)
66 (define-key map "L" 'describe-language-environment)
67 (define-key map "S" 'info-lookup-symbol)
68
69 (define-key map "a" 'apropos-command)
70 (define-key map "b" 'describe-bindings)
71 (define-key map "c" 'describe-key-briefly)
72 (define-key map "d" 'apropos-documentation)
73 (define-key map "e" 'view-echo-area-messages)
74 (define-key map "f" 'describe-function)
75 (define-key map "h" 'view-hello-file)
76
77 (define-key map "i" 'info)
78 (define-key map "4i" 'info-other-window)
79
80 (define-key map "k" 'describe-key)
81 (define-key map "l" 'view-lossage)
82 (define-key map "m" 'describe-mode)
83 (define-key map "n" 'view-emacs-news)
84 (define-key map "p" 'finder-by-keyword)
85 (define-key map "r" 'info-emacs-manual)
86 (define-key map "s" 'describe-syntax)
87 (define-key map "t" 'help-with-tutorial)
88 (define-key map "w" 'where-is)
89 (define-key map "v" 'describe-variable)
90 (define-key map "q" 'help-quit)
91 map)
433ae6f6
RS
92 "Keymap for characters following the Help key.")
93
e17d2fd1 94(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
95(define-key global-map [help] 'help-command)
96(define-key global-map [f1] 'help-command)
433ae6f6
RS
97(fset 'help-command help-map)
98
3e9c095d
RS
99(autoload 'finder-by-keyword "finder"
100 "Find packages matching a given keyword." t)
06b98c51 101
e25e90b4
DP
102;; insert-button makes the action nil if it is not store somewhere
103(defvar help-button-cache nil)
104
0cf0d828 105\f
2fc9d9f4 106(defun help-quit ()
3120a677 107 "Just exit from the Help command's command loop."
2fc9d9f4
RS
108 (interactive)
109 nil)
110
01364a75
RS
111(defvar help-return-method nil
112 "What to do to \"exit\" the help buffer.
113This is a list
a8e7142c
EZ
114 (WINDOW . t) delete the selected window (and possibly its frame,
115 see `quit-window' and `View-quit'), go to WINDOW.
01364a75
RS
116 (WINDOW . quit-window) do quit-window, then select WINDOW.
117 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
118
433ae6f6
RS
119(defun print-help-return-message (&optional function)
120 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
121This function assumes that `standard-output' is the help buffer.
122It computes a message, and applies the optional argument FUNCTION to it.
a8e7142c
EZ
123If FUNCTION is nil, it applies `message', thus displaying the message.
124In addition, this function sets up `help-return-method', which see, that
125specifies what to do when the user exits the help buffer."
433ae6f6 126 (and (not (get-buffer-window standard-output))
d536293f 127 (let ((first-message
a8e7142c
EZ
128 (cond ((or
129 pop-up-frames
130 (special-display-p (buffer-name standard-output)))
01364a75 131 (setq help-return-method (cons (selected-window) t))
d536293f
RS
132 ;; If the help output buffer is a special display buffer,
133 ;; don't say anything about how to get rid of it.
134 ;; First of all, the user will do that with the window
135 ;; manager, not with Emacs.
136 ;; Secondly, the buffer has not been displayed yet,
137 ;; so we don't know whether its frame will be selected.
d536293f 138 nil)
f3ad2fc8
GM
139 (display-buffer-reuse-frames
140 (setq help-return-method (cons (selected-window)
141 'quit-window))
142 nil)
d536293f 143 ((not (one-window-p t))
01364a75
RS
144 (setq help-return-method
145 (cons (selected-window) 'quit-window))
cb0b6766 146 "Type \\[display-buffer] RET to restore the other window.")
d536293f 147 (pop-up-windows
01364a75 148 (setq help-return-method (cons (selected-window) t))
d536293f
RS
149 "Type \\[delete-other-windows] to remove help window.")
150 (t
01364a75
RS
151 (setq help-return-method
152 (list (selected-window) (window-buffer)
153 (window-start) (window-point)))
d536293f
RS
154 "Type \\[switch-to-buffer] RET to remove help window."))))
155 (funcall (or function 'message)
156 (concat
157 (if first-message
376b2a24
DL
158 (substitute-command-keys first-message))
159 (if first-message " ")
125a8d70
RS
160 ;; If the help buffer will go in a separate frame,
161 ;; it's no use mentioning a command to scroll, so don't.
7b057a3d
EZ
162 (if (or pop-up-windows
163 (special-display-p (buffer-name standard-output)))
125a8d70 164 nil
a1c9f209 165 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
166 ;; Say how to scroll this window.
167 (substitute-command-keys
168 "\\[scroll-up] to scroll the help.")
169 ;; Say how to scroll some other window.
6e7f5182 170 (substitute-command-keys
125a8d70 171 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 172
433ae6f6
RS
173;; So keyboard macro definitions are documented correctly
174(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
175
a4bdcdf3
RS
176(defalias 'help 'help-for-help-internal)
177;; find-function can find this.
178(defalias 'help-for-help 'help-for-help-internal)
179;; It can't find this, but nobody will look.
180(make-help-screen help-for-help-internal
d50d22f1 181 "a b c C e f F i I k C-k l L m p r s t v w C-c C-d C-f C-n C-p C-t C-w . or ? :"
788d62cf
MB
182 "You have typed %THIS-KEY%, the help character. Type a Help option:
183\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
184
f609f345 185a command-apropos. Type a list of words or a regexp; it shows a list of
c6f20f15 186 commands whose names match. See also the apropos command.
f609f345
RS
187b describe-bindings. Display a table of all key bindings.
188c describe-key-briefly. Type a key sequence;
189 it displays the command name run by that key sequence.
190C describe-coding-system. Type the name of the coding system to describe,
191 or just RET to describe the ones currently in use.
192d apropos-documentation. Type a pattern (a list of words or a regexp), and
193 it shows a list of functions, variables, and other items whose
194 documentation matches that pattern. See also the apropos command.
195e view-echo-area-messages. Go to the buffer that logs echo-area messages.
196f describe-function. Type a function name and you see its documentation.
197F Info-goto-emacs-command-node. Type a command name;
198 it goes to the on-line manual's section that describes the command.
54391990 199h Display the HELLO file which illustrates various scripts.
f609f345 200i info. The Info documentation reader: read on-line manuals.
788d62cf
MB
201I describe-input-method. Describe a specific input method (if you type
202 its name) or the current input method (if you type just RET).
f609f345 203k describe-key. Type a key sequence;
3ee341ec 204 it displays the full documentation for that key sequence.
f609f345
RS
205K Info-goto-emacs-key-command-node. Type a key sequence;
206 it goes to the on-line manual's section that describes
3ee341ec 207 the command bound to that key.
788d62cf
MB
208l view-lossage. Show last 100 characters you typed.
209L describe-language-environment. This describes either a
210 specific language environment (if you type its name)
211 or the current language environment (if you type just RET).
3ee341ec 212m describe-mode. Display documentation of current minor modes,
788d62cf 213 and the current major mode, including their special commands.
3ee341ec 214n view-emacs-news. Display news of recent Emacs changes.
788d62cf 215p finder-by-keyword. Find packages matching a given topic keyword.
d50d22f1 216r info-emacs-manual. Display the Emacs manual in Info mode.
788d62cf 217s describe-syntax. Display contents of syntax table, plus explanations.
f609f345
RS
218S info-lookup-symbol. Type a symbol; it goes to that symbol in the
219 on-line manual for the programming language used in this buffer.
788d62cf
MB
220t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
221v describe-variable. Type name of a variable;
222 it displays the variable's documentation and value.
f609f345 223w where-is. Type a command name; it displays which keystrokes
788d62cf 224 invoke that command.
7ada28ac
LT
225. display-local-help. Display any available local help at point
226 in the echo area.
788d62cf 227
4f16ea85 228C-c Display Emacs copying permission (GNU General Public License).
788d62cf 229C-d Display Emacs ordering information.
4f16ea85
RS
230C-e Display info about Emacs problems.
231C-f Display the Emacs FAQ.
4f16ea85 232C-m Display how to order printed Emacs manuals.
788d62cf
MB
233C-n Display news of recent Emacs changes.
234C-p Display information about the GNU project.
1ebc1f01 235C-t Display the Emacs TODO list.
788d62cf
MB
236C-w Display information on absence of warranty for GNU Emacs."
237 help-map)
238
239\f
240
241(defun function-called-at-point ()
242 "Return a function around point or else called by the list containing point.
243If that doesn't give a function, return nil."
542e904c
JL
244 (or (with-syntax-table emacs-lisp-mode-syntax-table
245 (or (condition-case ()
246 (save-excursion
247 (or (not (zerop (skip-syntax-backward "_w")))
248 (eq (char-syntax (following-char)) ?w)
249 (eq (char-syntax (following-char)) ?_)
250 (forward-sexp -1))
251 (skip-chars-forward "'")
252 (let ((obj (read (current-buffer))))
253 (and (symbolp obj) (fboundp obj) obj)))
254 (error nil))
255 (condition-case ()
256 (save-excursion
257 (save-restriction
258 (narrow-to-region (max (point-min)
259 (- (point) 1000)) (point-max))
260 ;; Move up to surrounding paren, then after the open.
261 (backward-up-list 1)
262 (forward-char 1)
263 ;; If there is space here, this is probably something
264 ;; other than a real Lisp function call, so ignore it.
265 (if (looking-at "[ \t]")
266 (error "Probably not a Lisp function call"))
267 (let ((obj (read (current-buffer))))
268 (and (symbolp obj) (fboundp obj) obj))))
269 (error nil))))
270 (let* ((str (find-tag-default))
918f2e56
JL
271 (sym (if str (intern-soft str))))
272 (if (and sym (fboundp sym))
273 sym
274 (save-match-data
275 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
276 (setq sym (intern-soft (match-string 1 str)))
277 (and (fboundp sym) sym)))))))
788d62cf
MB
278
279\f
280;;; `User' help functions
281
433ae6f6
RS
282(defun describe-distribution ()
283 "Display info on how to obtain the latest version of GNU Emacs."
284 (interactive)
9e0e631c 285 (view-file (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
286
287(defun describe-copying ()
288 "Display info on how you may redistribute copies of GNU Emacs."
289 (interactive)
64f3b7d3 290 (view-file (expand-file-name "COPYING" data-directory))
433ae6f6
RS
291 (goto-char (point-min)))
292
76766f2d
RS
293(defun describe-project ()
294 "Display info on the GNU project."
295 (interactive)
64f3b7d3 296 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
76766f2d
RS
297 (goto-char (point-min)))
298
433ae6f6
RS
299(defun describe-no-warranty ()
300 "Display info on all the kinds of warranty Emacs does NOT have."
301 (interactive)
302 (describe-copying)
303 (let (case-fold-search)
304 (search-forward "NO WARRANTY")
305 (recenter 0)))
306
61c6b658 307(defun describe-prefix-bindings ()
c7cba9cb
RS
308 "Describe the bindings of the prefix used to reach this command.
309The prefix described consists of all but the last event
310of the key sequence that ran this command."
61c6b658 311 (interactive)
ccc06dcc
KH
312 (let* ((key (this-command-keys)))
313 (describe-bindings
314 (if (stringp key)
315 (substring key 0 (1- (length key)))
316 (let ((prefix (make-vector (1- (length key)) nil))
317 (i 0))
318 (while (< i (length prefix))
319 (aset prefix i (aref key i))
320 (setq i (1+ i)))
321 prefix)))))
788d62cf 322;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 323;; run describe-prefix-bindings.
61c6b658
RS
324(setq prefix-help-command 'describe-prefix-bindings)
325
e38cc268 326(defun view-emacs-news (&optional version)
382d018a 327 "Display info on recent changes to Emacs.
598ea453 328With argument, display info only for the selected version."
382d018a 329 (interactive "P")
e38cc268
KS
330 (unless version
331 (setq version emacs-major-version))
332 (when (consp version)
333 (let* ((all-versions
334 (let (res)
335 (mapcar
336 (lambda (file)
337 (with-temp-buffer
338 (insert-file-contents
339 (expand-file-name file data-directory))
340 (while (re-search-forward
341 (if (member file '("NEWS.18" "NEWS.1-17"))
342 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
343 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
344 (setq res (cons (match-string-no-properties 1) res)))))
345 (cons "NEWS"
346 (directory-files data-directory nil
347 "^NEWS\\.[0-9][-0-9]*$" nil)))
348 (sort (delete-dups res) (lambda (a b) (string< b a)))))
8c9fc4be 349 (current (car all-versions)))
e38cc268
KS
350 (setq version (completing-read
351 (format "Read NEWS for the version (default %s): " current)
352 all-versions nil nil nil nil current))
353 (if (integerp (string-to-number version))
354 (setq version (string-to-number version))
355 (unless (or (member version all-versions)
356 (<= (string-to-number version) (string-to-number current)))
357 (error "No news about version %s" version)))))
358 (when (integerp version)
359 (cond ((<= version 12)
360 (setq version (format "1.%d" version)))
361 ((<= version 18)
362 (setq version (format "%d" version)))
363 ((> version emacs-major-version)
364 (error "No news about emacs %d (yet)" version))))
365 (let* ((vn (if (stringp version)
366 (string-to-number version)
367 version))
368 (file (cond
369 ((>= vn emacs-major-version) "NEWS")
370 ((< vn 18) "NEWS.1-17")
8c9fc4be
KS
371 (t (format "NEWS.%d" vn))))
372 res)
e38cc268
KS
373 (view-file (expand-file-name file data-directory))
374 (widen)
375 (goto-char (point-min))
376 (when (stringp version)
377 (when (re-search-forward
378 (concat (if (< vn 19)
379 "Changes in Emacs[ \t]*"
380 "^\* [^0-9\n]*") version "$")
381 nil t)
382 (beginning-of-line)
383 (narrow-to-region
384 (point)
385 (save-excursion
386 (while (and (setq res
387 (re-search-forward
388 (if (< vn 19)
389 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
390 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
391 (equal (match-string-no-properties 1) version)))
392 (or res (goto-char (point-max)))
393 (beginning-of-line)
394 (point)))))))
395
433ae6f6 396
1ebc1f01
RS
397(defun view-todo (&optional arg)
398 "Display the Emacs TODO list."
399 (interactive "P")
400 (view-file (expand-file-name "TODO" data-directory)))
401
4f16ea85
RS
402(defun view-echo-area-messages ()
403 "View the log of recent echo-area messages: the `*Messages*' buffer.
404The number of messages retained in that buffer
405is specified by the variable `message-log-max'."
406 (interactive)
407 (switch-to-buffer (get-buffer-create "*Messages*")))
408
754084bb
GM
409(defun view-order-manuals ()
410 "Display the Emacs ORDERS file."
411 (interactive)
64f3b7d3 412 (view-file (expand-file-name "ORDERS" data-directory))
38790755 413 (goto-address))
754084bb 414
7ee71cf1
RS
415(defun view-emacs-FAQ ()
416 "Display the Emacs Frequently Asked Questions (FAQ) file."
417 (interactive)
94ea540b 418 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 419 (info "(efaq)"))
7ee71cf1 420
4cbff657
DL
421(defun view-emacs-problems ()
422 "Display info on known problems with Emacs and possible workarounds."
423 (interactive)
424 (view-file (expand-file-name "PROBLEMS" data-directory)))
425
433ae6f6 426(defun view-lossage ()
50b57199
EZ
427 "Display last 100 input keystrokes.
428
429To record all your input on a file, use `open-dribble-file'."
433ae6f6 430 (interactive)
3e5929af
SM
431 (help-setup-xref (list #'view-lossage) (interactive-p))
432 (with-output-to-temp-buffer (help-buffer)
02dfca16
SM
433 (princ (mapconcat (lambda (key)
434 (if (or (integerp key) (symbolp key) (listp key))
435 (single-key-description key)
436 (prin1-to-string key nil)))
298a7c8c
RS
437 (recent-keys)
438 " "))
b0fbf754 439 (with-current-buffer standard-output
433ae6f6
RS
440 (goto-char (point-min))
441 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
442 (when (search-forward " " nil t)
443 (delete-char -1))
444 (insert "\n")))
433ae6f6
RS
445 (print-help-return-message)))
446
788d62cf
MB
447\f
448;; Key bindings
433ae6f6 449
4c45295b 450(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
451 "Show a list of all defined keys, and their definitions.
452We put that list in a buffer, and display the buffer.
453
454The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
455then we display only bindings that start with that prefix.
456The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
457to display (default, the current buffer). BUFFER can be a buffer
458or a buffer name."
3e5929af 459 (interactive)
4c45295b 460 (or buffer (setq buffer (current-buffer)))
3e5929af 461 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
4c45295b 462 (with-current-buffer buffer
3e5929af 463 (describe-bindings-internal nil prefix)))
a8ad43aa 464
94ea540b
SM
465;; This function used to be in keymap.c.
466(defun describe-bindings-internal (&optional menus prefix)
467 "Show a list of all defined keys, and their definitions.
468We put that list in a buffer, and display the buffer.
469
470The optional argument MENUS, if non-nil, says to mention menu bindings.
471\(Ordinarily these are omitted from the output.)
472The optional argument PREFIX, if non-nil, should be a key sequence;
473then we display only bindings that start with that prefix."
474 (interactive)
475 (let ((buf (current-buffer)))
476 (with-output-to-temp-buffer "*Help*"
477 (with-current-buffer standard-output
478 (describe-buffer-bindings buf prefix menus)))))
479
e88a2c59 480(defun where-is (definition &optional insert)
b2c85790 481 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
482Argument is a command definition, usually a symbol with a function definition.
483If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
484 (interactive
485 (let ((fn (function-called-at-point))
788d62cf 486 (enable-recursive-minibuffers t)
54c0b967 487 val)
3829bcc5
SM
488 (setq val (completing-read
489 (if fn
490 (format "Where is command (default %s): " fn)
491 "Where is command: ")
492 obarray 'commandp t))
493 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
7a698dc1 494 (let ((func (indirect-function definition))
3829bcc5 495 (defs nil)
7a698dc1 496 (standard-output (if insert (current-buffer) t)))
740b479c 497 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
498 (mapatoms (lambda (symbol)
499 (and (fboundp symbol)
500 (not (eq symbol definition))
d92c2757
RS
501 (eq func (condition-case ()
502 (indirect-function symbol)
503 (error symbol)))
3829bcc5 504 (push symbol defs))))
740b479c
RS
505 ;; Look at all the symbols--first DEFINITION,
506 ;; then its aliases.
507 (dolist (symbol (cons definition defs))
508 (let* ((remapped (command-remapping symbol))
509 (keys (where-is-internal
510 symbol overriding-local-map nil nil remapped))
511 (keys (mapconcat 'key-description keys ", "))
512 string)
513 (setq string
514 (if insert
515 (if (> (length keys) 0)
516 (if remapped
517 (format "%s (%s) (remapped from %s)"
518 keys remapped symbol)
519 (format "%s (%s)" keys symbol))
520 (format "M-x %s RET" symbol))
521 (if (> (length keys) 0)
522 (if remapped
523 (format "%s is remapped to %s which is on %s"
a5f43550 524 symbol remapped keys)
740b479c
RS
525 (format "%s is on %s" symbol keys))
526 ;; If this is the command the user asked about,
527 ;; and it is not on any key, say so.
528 ;; For other symbols, its aliases, say nothing
529 ;; about them unless they are on keys.
530 (if (eq symbol definition)
531 (format "%s is not on any key" symbol)))))
532 (when string
533 (unless (eq symbol definition)
534 (princ ";\n its alias "))
535 (princ string)))))
54c0b967
RS
536 nil)
537
02dfca16
SM
538(defun help-key-description (key untranslated)
539 (let ((string (key-description key)))
ae1bb8ac
SM
540 (if (or (not untranslated)
541 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
542 string
543 (let ((otherstring (key-description untranslated)))
544 (if (equal string otherstring)
545 string
546 (format "%s (translated from %s)" string otherstring))))))
71296446 547
6527c983
EZ
548(defun describe-key-briefly (&optional key insert untranslated)
549 "Print the name of the function KEY invokes. KEY is a string.
550If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
551If non-nil, UNTRANSLATED is a vector of the untranslated events.
552It can also be a number in which case the untranslated events from
553the last key hit are used.
554
555If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 556temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
557 (interactive
558 (let ((enable-disabled-menus-and-buttons t)
559 (cursor-in-echo-area t)
560 saved-yank-menu)
561 (unwind-protect
562 (let (key)
563 ;; If yank-menu is empty, populate it temporarily, so that
564 ;; "Select and Paste" menu can generate a complete event.
565 (when (null (cdr yank-menu))
566 (setq saved-yank-menu (copy-sequence yank-menu))
567 (menu-bar-update-yank-menu "(any string)" nil))
568 (setq key (read-key-sequence "Describe key (or click or menu item): "))
46d91fa0
CY
569 ;; If KEY is a down-event, read and discard the
570 ;; corresponding up-event.
571 (if (and (vectorp key)
572 (eventp (elt key 0))
573 (memq 'down (event-modifiers (elt key 0))))
574 (read-event))
2c8ed538
RS
575 (list
576 key
774a814f
RS
577 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
578 1))
2c8ed538
RS
579 ;; Put yank-menu back as it was, if we changed it.
580 (when saved-yank-menu
581 (setq yank-menu (copy-sequence saved-yank-menu))
582 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
583 (if (numberp untranslated)
584 (setq untranslated (this-single-command-raw-keys)))
400a1b1f 585 (save-excursion
788d62cf
MB
586 (let ((modifiers (event-modifiers (aref key 0)))
587 (standard-output (if insert (current-buffer) t))
588 window position)
589 ;; For a mouse button event, go to the button it applies to
590 ;; to get the right key bindings. And go to the right place
591 ;; in case the keymap depends on where you clicked.
592 (if (or (memq 'click modifiers) (memq 'down modifiers)
593 (memq 'drag modifiers))
594 (setq window (posn-window (event-start (aref key 0)))
595 position (posn-point (event-start (aref key 0)))))
596 (if (windowp window)
597 (progn
598 (set-buffer (window-buffer window))
599 (goto-char position)))
600 ;; Ok, now look up the key and name the command.
bcd8e1a9 601 (let ((defn (key-binding key t))
36178ae8 602 key-desc)
2c8ed538
RS
603 ;; Handle the case where we faked an entry in "Select and Paste" menu.
604 (if (and (eq defn nil)
605 (stringp (aref key (1- (length key))))
606 (eq (key-binding (substring key 0 -1)) 'yank-menu))
607 (setq defn 'menu-bar-select-yank))
36178ae8
RS
608 ;; Don't bother user with strings from (e.g.) the select-paste menu.
609 (if (stringp (aref key (1- (length key))))
610 (aset key (1- (length key)) "(any string)"))
59d922a4
LK
611 (if (and (> (length untranslated) 0)
612 (stringp (aref untranslated (1- (length untranslated)))))
36178ae8
RS
613 (aset untranslated (1- (length untranslated))
614 "(any string)"))
615 ;; Now describe the key, perhaps as changed.
616 (setq key-desc (help-key-description key untranslated))
d61ac4a6 617 (if (or (null defn) (integerp defn) (equal defn 'undefined))
788d62cf 618 (princ (format "%s is undefined" key-desc))
80d553d4
RS
619 (princ (format (if (windowp window)
620 "%s at that spot runs the command %s"
621 "%s runs the command %s")
788d62cf
MB
622 key-desc
623 (if (symbolp defn) defn (prin1-to-string defn)))))))))
96ede6b2 624
6527c983
EZ
625(defun describe-key (&optional key untranslated up-event)
626 "Display documentation of the function invoked by KEY.
627KEY can be any kind of a key sequence; it can include keyboard events,
628mouse events, and/or menu events. When calling from a program,
629pass KEY as a string or a vector.
630
631If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
632It can also be a number, in which case the untranslated events from
633the last key sequence entered are used.
634UP-EVENT is the up-event that was discarded by reading KEY, or nil.
635
636If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 637temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
638 (interactive
639 (let ((enable-disabled-menus-and-buttons t)
640 (cursor-in-echo-area t)
641 saved-yank-menu)
642 (unwind-protect
643 (let (key)
644 ;; If yank-menu is empty, populate it temporarily, so that
645 ;; "Select and Paste" menu can generate a complete event.
646 (when (null (cdr yank-menu))
647 (setq saved-yank-menu (copy-sequence yank-menu))
648 (menu-bar-update-yank-menu "(any string)" nil))
649 (setq key (read-key-sequence "Describe key (or click or menu item): "))
650 (list
651 key
652 (prefix-numeric-value current-prefix-arg)
653 ;; If KEY is a down-event, read the corresponding up-event
654 ;; and use it as the third argument.
aa38f98e
CY
655 (if (and (vectorp key)
656 (eventp (elt key 0))
657 (memq 'down (event-modifiers (elt key 0))))
2c8ed538
RS
658 (read-event))))
659 ;; Put yank-menu back as it was, if we changed it.
660 (when saved-yank-menu
661 (setq yank-menu (copy-sequence saved-yank-menu))
662 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
663 (if (numberp untranslated)
664 (setq untranslated (this-single-command-raw-keys)))
788d62cf
MB
665 (save-excursion
666 (let ((modifiers (event-modifiers (aref key 0)))
667 window position)
668 ;; For a mouse button event, go to the button it applies to
669 ;; to get the right key bindings. And go to the right place
670 ;; in case the keymap depends on where you clicked.
671 (if (or (memq 'click modifiers) (memq 'down modifiers)
672 (memq 'drag modifiers))
673 (setq window (posn-window (event-start (aref key 0)))
674 position (posn-point (event-start (aref key 0)))))
94ea540b 675 (when (windowp window)
788d62cf 676 (set-buffer (window-buffer window))
94ea540b 677 (goto-char position))
bcd8e1a9 678 (let ((defn (key-binding key t)))
2c8ed538
RS
679 ;; Handle the case where we faked an entry in "Select and Paste" menu.
680 (if (and (eq defn nil)
681 (stringp (aref key (1- (length key))))
682 (eq (key-binding (substring key 0 -1)) 'yank-menu))
683 (setq defn 'menu-bar-select-yank))
d61ac4a6 684 (if (or (null defn) (integerp defn) (equal defn 'undefined))
02dfca16 685 (message "%s is undefined" (help-key-description key untranslated))
3e5929af 686 (help-setup-xref (list #'describe-function defn) (interactive-p))
36178ae8
RS
687 ;; Don't bother user with strings from (e.g.) the select-paste menu.
688 (if (stringp (aref key (1- (length key))))
689 (aset key (1- (length key)) "(any string)"))
ab3fdfd6
RS
690 (if (and untranslated
691 (stringp (aref untranslated (1- (length untranslated)))))
36178ae8
RS
692 (aset untranslated (1- (length untranslated))
693 "(any string)"))
3e5929af 694 (with-output-to-temp-buffer (help-buffer)
02dfca16 695 (princ (help-key-description key untranslated))
788d62cf
MB
696 (if (windowp window)
697 (princ " at that spot"))
698 (princ " runs the command ")
699 (prin1 defn)
700 (princ "\n which is ")
3e5929af 701 (describe-function-1 defn)
4cf9f027 702 (when up-event
aa38f98e 703 (let ((type (event-basic-type up-event))
72b64ad5 704 (hdr "\n\n-------------- up event ---------------\n\n")
4dfb2678 705 defn sequence
72b64ad5 706 mouse-1-tricky mouse-1-remapped)
4dfb2678 707 (setq sequence (vector up-event))
aa38f98e 708 (when (and (eq type 'mouse-1)
72b64ad5
KS
709 (windowp window)
710 mouse-1-click-follows-link
711 (not (eq mouse-1-click-follows-link 'double))
4dfb2678
CY
712 (setq mouse-1-remapped
713 (with-current-buffer (window-buffer window)
714 (mouse-on-link-p (posn-point
715 (event-start up-event))))))
aa38f98e
CY
716 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
717 (> mouse-1-click-follows-link 0)))
4dfb2678
CY
718 (cond ((stringp mouse-1-remapped)
719 (setq sequence mouse-1-remapped))
720 ((vectorp mouse-1-remapped)
721 (setcar up-event (elt mouse-1-remapped 0)))
722 (t (setcar up-event 'mouse-2))))
bcd8e1a9 723 (setq defn (key-binding sequence))
4cf9f027 724 (unless (or (null defn) (integerp defn) (equal defn 'undefined))
72b64ad5
KS
725 (princ (if mouse-1-tricky
726 "\n\n----------------- up-event (short click) ----------------\n\n"
727 hdr))
728 (setq hdr nil)
aa38f98e 729 (princ (symbol-name type))
4cf9f027
KS
730 (if (windowp window)
731 (princ " at that spot"))
72b64ad5
KS
732 (if mouse-1-remapped
733 (princ " is remapped to <mouse-2>\n which" ))
4cf9f027
KS
734 (princ " runs the command ")
735 (prin1 defn)
736 (princ "\n which is ")
72b64ad5
KS
737 (describe-function-1 defn))
738 (when mouse-1-tricky
aa38f98e 739 (setcar up-event 'mouse-1)
bcd8e1a9 740 (setq defn (key-binding (vector up-event)))
aa38f98e 741 (unless (or (null defn) (integerp defn) (eq defn 'undefined))
72b64ad5
KS
742 (princ (or hdr
743 "\n\n----------------- up-event (long click) ----------------\n\n"))
aa38f98e 744 (princ "Pressing mouse-1")
72b64ad5
KS
745 (if (windowp window)
746 (princ " at that spot"))
747 (princ (format " for longer than %d milli-seconds\n"
aa38f98e 748 mouse-1-click-follows-link))
72b64ad5
KS
749 (princ " runs the command ")
750 (prin1 defn)
751 (princ "\n which is ")
1b12fa9d
RS
752 (describe-function-1 defn)))))
753 (print-help-return-message)))))))
400a1b1f 754\f
788d62cf
MB
755(defun describe-mode (&optional buffer)
756 "Display documentation of current major mode and minor modes.
efde809a
JPW
757A brief summary of the minor modes comes first, followed by the
758major mode description. This is followed by detailed
759descriptions of the minor modes, each on a separate page.
760
761For this to work correctly for a minor mode, the mode's indicator
762variable \(listed in `minor-mode-alist') must also be a function
763whose documentation describes the minor mode."
400a1b1f 764 (interactive)
dd39c336
SM
765 (unless buffer (setq buffer (current-buffer)))
766 (help-setup-xref (list #'describe-mode buffer)
9639be74
RS
767 (interactive-p))
768 ;; For the sake of help-do-xref and help-xref-go-back,
769 ;; don't switch buffers before calling `help-buffer'.
3e5929af 770 (with-output-to-temp-buffer (help-buffer)
dd39c336 771 (with-current-buffer buffer
f6c57ef6 772 (let (minor-modes)
dd39c336
SM
773 ;; Older packages do not register in minor-mode-list but only in
774 ;; minor-mode-alist.
775 (dolist (x minor-mode-alist)
776 (setq x (car x))
777 (unless (memq x minor-mode-list)
778 (push x minor-mode-list)))
f6c57ef6
RS
779 ;; Find enabled minor mode we will want to mention.
780 (dolist (mode minor-mode-list)
781 ;; Document a minor mode if it is listed in minor-mode-alist,
782 ;; non-nil, and has a function definition.
af5f4483
SM
783 (let ((fmode (or (get mode :minor-mode-function) mode)))
784 (and (boundp mode) (symbol-value mode)
785 (fboundp fmode)
786 (let ((pretty-minor-mode
787 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
788 (symbol-name fmode))
789 (capitalize
790 (substring (symbol-name fmode)
791 0 (match-beginning 0)))
792 fmode)))
793 (push (list fmode pretty-minor-mode
794 (format-mode-line (assq mode minor-mode-alist)))
795 minor-modes)))))
f6c57ef6
RS
796 (setq minor-modes
797 (sort minor-modes
af5f4483 798 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 799 (when minor-modes
71723367 800 (princ "Enabled minor modes:\n")
e25e90b4
DP
801 (make-local-variable 'help-button-cache)
802 (with-current-buffer standard-output
803 (dolist (mode minor-modes)
af5f4483
SM
804 (let ((mode-function (nth 0 mode))
805 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
806 (indicator (nth 2 mode)))
807 (add-text-properties 0 (length pretty-minor-mode)
808 '(face bold) pretty-minor-mode)
809 (save-excursion
810 (goto-char (point-max))
811 (princ "\n\f\n")
812 (push (point-marker) help-button-cache)
813 ;; Document the minor modes fully.
814 (insert pretty-minor-mode)
71723367
RS
815 (princ (format " minor mode (%s):\n"
816 (if (zerop (length indicator))
817 "no indicator"
818 (format "indicator%s"
d8a869ea 819 indicator))))
e25e90b4 820 (princ (documentation mode-function)))
e25e90b4
DP
821 (insert-button pretty-minor-mode
822 'action (car help-button-cache)
72b64ad5 823 'follow-link t
e25e90b4 824 'help-echo "mouse-2, RET: show full information")
71723367
RS
825 (newline)))
826 (forward-line -1)
827 (fill-paragraph nil)
828 (forward-line 1))
829
830 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 831 ;; Document the major mode.
e25e90b4
DP
832 (let ((mode mode-name))
833 (with-current-buffer standard-output
4e6d3170
SM
834 (let ((start (point)))
835 (insert (format-mode-line mode))
836 (add-text-properties start (point) '(face bold)))))
f6c57ef6 837 (princ " mode:\n")
e25e90b4 838 (princ (documentation major-mode)))
9639be74 839 (print-help-return-message))))
400a1b1f 840
f6c57ef6 841
8e864068 842(defun describe-minor-mode (minor-mode)
335028c3
MY
843 "Display documentation of a minor mode given as MINOR-MODE.
844MINOR-MODE can be a minor mode symbol or a minor mode indicator string
845appeared on the mode-line."
7ada28ac 846 (interactive (list (completing-read
335028c3
MY
847 "Minor mode: "
848 (nconc
849 (describe-minor-mode-completion-table-for-symbol)
850 (describe-minor-mode-completion-table-for-indicator)
851 ))))
852 (if (symbolp minor-mode)
853 (setq minor-mode (symbol-name minor-mode)))
854 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
855 (indicators (describe-minor-mode-completion-table-for-indicator)))
856 (cond
857 ((member minor-mode symbols)
858 (describe-minor-mode-from-symbol (intern minor-mode)))
859 ((member minor-mode indicators)
860 (describe-minor-mode-from-indicator minor-mode))
861 (t
862 (error "No such minor mode: %s" minor-mode)))))
863
7ada28ac 864;; symbol
335028c3
MY
865(defun describe-minor-mode-completion-table-for-symbol ()
866 ;; In order to list up all minor modes, minor-mode-list
867 ;; is used here instead of minor-mode-alist.
868 (delq nil (mapcar 'symbol-name minor-mode-list)))
869(defun describe-minor-mode-from-symbol (symbol)
870 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 871 (interactive (list (intern (completing-read
335028c3
MY
872 "Minor mode symbol: "
873 (describe-minor-mode-completion-table-for-symbol)))))
874 (if (fboundp symbol)
875 (describe-function symbol)
876 (describe-variable symbol)))
877
878;; indicator
879(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 880 (delq nil
335028c3
MY
881 (mapcar (lambda (x)
882 (let ((i (format-mode-line x)))
883 ;; remove first space if existed
884 (cond
885 ((= 0 (length i))
886 nil)
887 ((eq (aref i 0) ?\ )
888 (substring i 1))
7ada28ac 889 (t
335028c3
MY
890 i))))
891 minor-mode-alist)))
8e864068 892(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
893 "Display documentation of a minor mode specified by INDICATOR.
894If you call this function interactively, you can give indicator which
895is currently activated with completion."
7ada28ac
LT
896 (interactive (list
897 (completing-read
8e864068 898 "Minor mode indicator: "
335028c3 899 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
900 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
901 (if minor-mode
335028c3 902 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
903 (error "Cannot find minor mode for `%s'" indicator))))
904
905(defun lookup-minor-mode-from-indicator (indicator)
906 "Return a minor mode symbol from its indicator on the modeline."
335028c3 907 ;; remove first space if existed
7ada28ac 908 (if (and (< 0 (length indicator))
335028c3
MY
909 (eq (aref indicator 0) ?\ ))
910 (setq indicator (substring indicator 1)))
8e864068
JB
911 (let ((minor-modes minor-mode-alist)
912 result)
913 (while minor-modes
914 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 915 (anindicator (format-mode-line
335028c3
MY
916 (car (cdr (car minor-modes))))))
917 ;; remove first space if existed
7ada28ac 918 (if (and (stringp anindicator)
335028c3
MY
919 (> (length anindicator) 0)
920 (eq (aref anindicator 0) ?\ ))
921 (setq anindicator (substring anindicator 1)))
922 (if (equal indicator anindicator)
8e864068
JB
923 (setq result minor-mode
924 minor-modes nil)
925 (setq minor-modes (cdr minor-modes)))))
926 result))
927
48ce3c22
RS
928\f
929;;; Automatic resizing of temporary buffers.
930
4483ddc5 931(defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
4e6d3170 932 "Maximum height of a window displaying a temporary buffer.
90e357ae
RS
933This is effective only when Temp Buffer Resize mode is enabled.
934The value is the maximum height (in lines) which `resize-temp-buffer-window'
48ce3c22 935will give to a window displaying a temporary buffer.
90e357ae
RS
936It can also be a function to be called to choose the height for such a buffer.
937It gets one argumemt, the buffer, and should return a positive integer."
48ce3c22
RS
938 :type '(choice integer function)
939 :group 'help
940 :version "20.4")
941
4e1ede6c
SM
942(define-minor-mode temp-buffer-resize-mode
943 "Toggle the mode which makes windows smaller for temporary buffers.
48ce3c22
RS
944With prefix argument ARG, turn the resizing of windows displaying temporary
945buffers on if ARG is positive or off otherwise.
4e1ede6c
SM
946This makes the window the right height for its contents, but never
947more than `temp-buffer-max-height' nor less than `window-min-height'.
948This applies to `help', `apropos' and `completion' buffers, and some others."
b0fbf754 949 :global t :group 'help
4e1ede6c 950 (if temp-buffer-resize-mode
57f43907 951 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
952 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
953 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 954 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22
RS
955
956(defun resize-temp-buffer-window ()
957 "Resize the current window to fit its contents.
4483ddc5 958Will not make it higher than `temp-buffer-max-height' nor smaller than
b2c85790 959`window-min-height'. Do nothing if it is the only window on its frame, if it
48ce3c22
RS
960is not as wide as the frame or if some of the window's contents are scrolled
961out of view."
962 (unless (or (one-window-p 'nomini)
963 (not (pos-visible-in-window-p (point-min)))
964 (/= (frame-width) (window-width)))
d9c30bdf
MB
965 (fit-window-to-buffer
966 (selected-window)
967 (if (functionp temp-buffer-max-height)
968 (funcall temp-buffer-max-height (current-buffer))
969 temp-buffer-max-height))))
48ce3c22 970
172892e3
JB
971\f
972(provide 'help)
973
dd39c336 974;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1a06eabd 975;;; help.el ends here