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