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