Add arch taglines
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
f03b7c7d 3;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
754084bb 4;; 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
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
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
433ae6f6
RS
41(defvar help-map (make-sparse-keymap)
42 "Keymap for characters following the Help key.")
43
e17d2fd1 44(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
45(define-key global-map [help] 'help-command)
46(define-key global-map [f1] 'help-command)
433ae6f6
RS
47(fset 'help-command help-map)
48
e17d2fd1 49(define-key help-map (char-to-string help-char) 'help-for-help)
0af3df1c
RS
50(define-key help-map [help] 'help-for-help)
51(define-key help-map [f1] 'help-for-help)
433ae6f6
RS
52(define-key help-map "?" 'help-for-help)
53
54(define-key help-map "\C-c" 'describe-copying)
55(define-key help-map "\C-d" 'describe-distribution)
4f16ea85
RS
56(define-key help-map "\C-e" 'view-emacs-problems)
57(define-key help-map "\C-f" 'view-emacs-FAQ)
4f16ea85
RS
58(define-key help-map "\C-m" 'view-order-manuals)
59(define-key help-map "\C-n" 'view-emacs-news)
76766f2d 60(define-key help-map "\C-p" 'describe-project)
1ebc1f01 61(define-key help-map "\C-t" 'view-todo)
4f16ea85
RS
62(define-key help-map "\C-w" 'describe-no-warranty)
63
64;; This does not fit the pattern, but it is natural given the C-\ command.
65(define-key help-map "\C-\\" 'describe-input-method)
66
67(define-key help-map "C" 'describe-coding-system)
68(define-key help-map "F" 'Info-goto-emacs-command-node)
69(define-key help-map "I" 'describe-input-method)
70(define-key help-map "K" 'Info-goto-emacs-key-command-node)
71(define-key help-map "L" 'describe-language-environment)
72(define-key help-map "S" 'info-lookup-symbol)
73
122955bf 74(define-key help-map "a" 'apropos-command)
433ae6f6
RS
75
76(define-key help-map "b" 'describe-bindings)
77
78(define-key help-map "c" 'describe-key-briefly)
433ae6f6 79
4f16ea85
RS
80(define-key help-map "e" 'view-echo-area-messages)
81
433ae6f6
RS
82(define-key help-map "f" 'describe-function)
83
4f16ea85 84(define-key help-map "h" 'view-hello-file)
7ee71cf1 85
433ae6f6 86(define-key help-map "i" 'info)
4b08b7ed 87(define-key help-map "4i" 'info-other-window)
4f16ea85
RS
88
89(define-key help-map "k" 'describe-key)
433ae6f6
RS
90
91(define-key help-map "l" 'view-lossage)
92
93(define-key help-map "m" 'describe-mode)
94
433ae6f6
RS
95(define-key help-map "n" 'view-emacs-news)
96
06b98c51 97(define-key help-map "p" 'finder-by-keyword)
3e9c095d
RS
98(autoload 'finder-by-keyword "finder"
99 "Find packages matching a given keyword." t)
06b98c51 100
d009b6e9
RS
101(define-key help-map "r" 'info-emacs-manual)
102
433ae6f6
RS
103(define-key help-map "s" 'describe-syntax)
104
105(define-key help-map "t" 'help-with-tutorial)
106
107(define-key help-map "w" 'where-is)
108
109(define-key help-map "v" 'describe-variable)
110
2fc9d9f4
RS
111(define-key help-map "q" 'help-quit)
112
0cf0d828 113\f
2fc9d9f4 114(defun help-quit ()
3120a677 115 "Just exit from the Help command's command loop."
2fc9d9f4
RS
116 (interactive)
117 nil)
118
01364a75
RS
119(defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
121This is a list
122 (WINDOW . t) delete the selected window, go to WINDOW.
123 (WINDOW . quit-window) do quit-window, then select WINDOW.
124 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
125
433ae6f6
RS
126(defun print-help-return-message (&optional function)
127 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
128This function assumes that `standard-output' is the help buffer.
129It computes a message, and applies the optional argument FUNCTION to it.
130If FUNCTION is nil, it applies `message', thus displaying the message."
433ae6f6 131 (and (not (get-buffer-window standard-output))
d536293f 132 (let ((first-message
a1c9f209 133 (cond ((special-display-p (buffer-name standard-output))
01364a75 134 (setq help-return-method (cons (selected-window) t))
d536293f
RS
135 ;; If the help output buffer is a special display buffer,
136 ;; don't say anything about how to get rid of it.
137 ;; First of all, the user will do that with the window
138 ;; manager, not with Emacs.
139 ;; Secondly, the buffer has not been displayed yet,
140 ;; so we don't know whether its frame will be selected.
d536293f 141 nil)
f3ad2fc8
GM
142 (display-buffer-reuse-frames
143 (setq help-return-method (cons (selected-window)
144 'quit-window))
145 nil)
d536293f 146 ((not (one-window-p t))
01364a75
RS
147 (setq help-return-method
148 (cons (selected-window) 'quit-window))
d536293f
RS
149 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
150 (pop-up-windows
01364a75 151 (setq help-return-method (cons (selected-window) t))
d536293f
RS
152 "Type \\[delete-other-windows] to remove help window.")
153 (t
01364a75
RS
154 (setq help-return-method
155 (list (selected-window) (window-buffer)
156 (window-start) (window-point)))
d536293f
RS
157 "Type \\[switch-to-buffer] RET to remove help window."))))
158 (funcall (or function 'message)
159 (concat
160 (if first-message
376b2a24
DL
161 (substitute-command-keys first-message))
162 (if first-message " ")
125a8d70
RS
163 ;; If the help buffer will go in a separate frame,
164 ;; it's no use mentioning a command to scroll, so don't.
a1c9f209 165 (if (special-display-p (buffer-name standard-output))
125a8d70 166 nil
a1c9f209 167 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
168 ;; Say how to scroll this window.
169 (substitute-command-keys
170 "\\[scroll-up] to scroll the help.")
171 ;; Say how to scroll some other window.
6e7f5182 172 (substitute-command-keys
125a8d70 173 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 174
433ae6f6
RS
175;; So keyboard macro definitions are documented correctly
176(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
177
788d62cf
MB
178(defalias 'help 'help-for-help)
179(make-help-screen help-for-help
1ebc1f01 180 "a b c C e f F i I k C-k l L m p s t v w C-c C-d C-f C-n C-p C-t C-w or ? :"
788d62cf
MB
181 "You have typed %THIS-KEY%, the help character. Type a Help option:
182\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
183
184a command-apropos. Give a substring, and see a list of commands
185 (functions interactively callable) that contain
186 that substring. See also the apropos command.
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).
4f16ea85 193e view-echo-area-messages. Show the `*Messages*' buffer.
788d62cf 194f describe-function. Type a function name and get documentation of it.
4f16ea85 195F Info-goto-emacs-command-node. Type a function name;
788d62cf 196 it takes you to the Info node for that command.
54391990 197h Display the HELLO file which illustrates various scripts.
788d62cf
MB
198i info. The info documentation reader.
199I describe-input-method. Describe a specific input method (if you type
200 its name) or the current input method (if you type just RET).
788d62cf
MB
201k describe-key. Type a command key sequence;
202 it displays the full documentation.
4f16ea85 203K Info-goto-emacs-key-command-node. Type a command key sequence;
788d62cf
MB
204 it takes you to the Info node for the command bound to that key.
205l view-lossage. Show last 100 characters you typed.
206L describe-language-environment. This describes either a
207 specific language environment (if you type its name)
208 or the current language environment (if you type just RET).
209m describe-mode. Print documentation of current minor modes,
210 and the current major mode, including their special commands.
788d62cf
MB
211p finder-by-keyword. Find packages matching a given topic keyword.
212s describe-syntax. Display contents of syntax table, plus explanations.
4f16ea85
RS
213S info-lookup-symbol. Display the definition of a specific symbol
214 as found in the manual for the language this buffer is written in.
788d62cf
MB
215t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
216v describe-variable. Type name of a variable;
217 it displays the variable's documentation and value.
218w where-is. Type command name; it prints which keystrokes
219 invoke that command.
220
4f16ea85 221C-c Display Emacs copying permission (GNU General Public License).
788d62cf 222C-d Display Emacs ordering information.
4f16ea85
RS
223C-e Display info about Emacs problems.
224C-f Display the Emacs FAQ.
4f16ea85 225C-m Display how to order printed Emacs manuals.
788d62cf
MB
226C-n Display news of recent Emacs changes.
227C-p Display information about the GNU project.
1ebc1f01 228C-t Display the Emacs TODO list.
788d62cf
MB
229C-w Display information on absence of warranty for GNU Emacs."
230 help-map)
231
232\f
233
234(defun function-called-at-point ()
235 "Return a function around point or else called by the list containing point.
236If that doesn't give a function, return nil."
237 (with-syntax-table emacs-lisp-mode-syntax-table
238 (or (condition-case ()
239 (save-excursion
240 (or (not (zerop (skip-syntax-backward "_w")))
241 (eq (char-syntax (following-char)) ?w)
242 (eq (char-syntax (following-char)) ?_)
243 (forward-sexp -1))
244 (skip-chars-forward "'")
245 (let ((obj (read (current-buffer))))
246 (and (symbolp obj) (fboundp obj) obj)))
247 (error nil))
248 (condition-case ()
249 (save-excursion
250 (save-restriction
251 (narrow-to-region (max (point-min)
252 (- (point) 1000)) (point-max))
253 ;; Move up to surrounding paren, then after the open.
254 (backward-up-list 1)
255 (forward-char 1)
256 ;; If there is space here, this is probably something
257 ;; other than a real Lisp function call, so ignore it.
258 (if (looking-at "[ \t]")
259 (error "Probably not a Lisp function call"))
260 (let ((obj (read (current-buffer))))
261 (and (symbolp obj) (fboundp obj) obj))))
262 (error nil)))))
263
264\f
265;;; `User' help functions
266
433ae6f6
RS
267(defun describe-distribution ()
268 "Display info on how to obtain the latest version of GNU Emacs."
269 (interactive)
9e0e631c 270 (view-file (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
271
272(defun describe-copying ()
273 "Display info on how you may redistribute copies of GNU Emacs."
274 (interactive)
64f3b7d3 275 (view-file (expand-file-name "COPYING" data-directory))
433ae6f6
RS
276 (goto-char (point-min)))
277
76766f2d
RS
278(defun describe-project ()
279 "Display info on the GNU project."
280 (interactive)
64f3b7d3 281 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
76766f2d
RS
282 (goto-char (point-min)))
283
433ae6f6
RS
284(defun describe-no-warranty ()
285 "Display info on all the kinds of warranty Emacs does NOT have."
286 (interactive)
287 (describe-copying)
288 (let (case-fold-search)
289 (search-forward "NO WARRANTY")
290 (recenter 0)))
291
61c6b658 292(defun describe-prefix-bindings ()
c7cba9cb
RS
293 "Describe the bindings of the prefix used to reach this command.
294The prefix described consists of all but the last event
295of the key sequence that ran this command."
61c6b658 296 (interactive)
ccc06dcc
KH
297 (let* ((key (this-command-keys)))
298 (describe-bindings
299 (if (stringp key)
300 (substring key 0 (1- (length key)))
301 (let ((prefix (make-vector (1- (length key)) nil))
302 (i 0))
303 (while (< i (length prefix))
304 (aset prefix i (aref key i))
305 (setq i (1+ i)))
306 prefix)))))
788d62cf 307;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 308;; run describe-prefix-bindings.
61c6b658
RS
309(setq prefix-help-command 'describe-prefix-bindings)
310
382d018a
RS
311(defun view-emacs-news (&optional arg)
312 "Display info on recent changes to Emacs.
f0753a5f 313With numeric argument, display information on correspondingly older changes."
382d018a 314 (interactive "P")
f0753a5f
GM
315 (let* ((arg (if arg (prefix-numeric-value arg) 0))
316 (file (cond ((eq arg 0) "NEWS")
317 ((eq arg 1) "ONEWS")
318 (t
319 (nth (- arg 2)
320 (nreverse (directory-files data-directory
975f82c9 321 nil "^ONEWS\\.[0-9]+$"
f0753a5f
GM
322 nil)))))))
323 (if file
64f3b7d3 324 (view-file (expand-file-name file data-directory))
f0753a5f 325 (error "No such old news"))))
433ae6f6 326
1ebc1f01
RS
327(defun view-todo (&optional arg)
328 "Display the Emacs TODO list."
329 (interactive "P")
330 (view-file (expand-file-name "TODO" data-directory)))
331
4f16ea85
RS
332(defun view-echo-area-messages ()
333 "View the log of recent echo-area messages: the `*Messages*' buffer.
334The number of messages retained in that buffer
335is specified by the variable `message-log-max'."
336 (interactive)
337 (switch-to-buffer (get-buffer-create "*Messages*")))
338
754084bb
GM
339(defun view-order-manuals ()
340 "Display the Emacs ORDERS file."
341 (interactive)
64f3b7d3 342 (view-file (expand-file-name "ORDERS" data-directory))
38790755 343 (goto-address))
754084bb 344
7ee71cf1
RS
345(defun view-emacs-FAQ ()
346 "Display the Emacs Frequently Asked Questions (FAQ) file."
347 (interactive)
94ea540b 348 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 349 (info "(efaq)"))
7ee71cf1 350
4cbff657
DL
351(defun view-emacs-problems ()
352 "Display info on known problems with Emacs and possible workarounds."
353 (interactive)
354 (view-file (expand-file-name "PROBLEMS" data-directory)))
355
433ae6f6 356(defun view-lossage ()
50b57199
EZ
357 "Display last 100 input keystrokes.
358
359To record all your input on a file, use `open-dribble-file'."
433ae6f6 360 (interactive)
3e5929af
SM
361 (help-setup-xref (list #'view-lossage) (interactive-p))
362 (with-output-to-temp-buffer (help-buffer)
02dfca16
SM
363 (princ (mapconcat (lambda (key)
364 (if (or (integerp key) (symbolp key) (listp key))
365 (single-key-description key)
366 (prin1-to-string key nil)))
298a7c8c
RS
367 (recent-keys)
368 " "))
b0fbf754 369 (with-current-buffer standard-output
433ae6f6
RS
370 (goto-char (point-min))
371 (while (progn (move-to-column 50) (not (eobp)))
372 (search-forward " " nil t)
3e5929af 373 (insert "\n")))
433ae6f6
RS
374 (print-help-return-message)))
375
788d62cf
MB
376\f
377;; Key bindings
433ae6f6 378
4c45295b 379(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
380 "Show a list of all defined keys, and their definitions.
381We put that list in a buffer, and display the buffer.
382
383The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
384then we display only bindings that start with that prefix.
385The optional argument BUFFER specifies which buffer's bindings
386to display (default, the current buffer)."
3e5929af 387 (interactive)
4c45295b 388 (or buffer (setq buffer (current-buffer)))
3e5929af 389 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
4c45295b 390 (with-current-buffer buffer
3e5929af 391 (describe-bindings-internal nil prefix)))
a8ad43aa 392
94ea540b
SM
393;; This function used to be in keymap.c.
394(defun describe-bindings-internal (&optional menus prefix)
395 "Show a list of all defined keys, and their definitions.
396We put that list in a buffer, and display the buffer.
397
398The optional argument MENUS, if non-nil, says to mention menu bindings.
399\(Ordinarily these are omitted from the output.)
400The optional argument PREFIX, if non-nil, should be a key sequence;
401then we display only bindings that start with that prefix."
402 (interactive)
403 (let ((buf (current-buffer)))
404 (with-output-to-temp-buffer "*Help*"
405 (with-current-buffer standard-output
406 (describe-buffer-bindings buf prefix menus)))))
407
e88a2c59 408(defun where-is (definition &optional insert)
b2c85790 409 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
410Argument is a command definition, usually a symbol with a function definition.
411If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
412 (interactive
413 (let ((fn (function-called-at-point))
788d62cf 414 (enable-recursive-minibuffers t)
54c0b967 415 val)
3829bcc5
SM
416 (setq val (completing-read
417 (if fn
418 (format "Where is command (default %s): " fn)
419 "Where is command: ")
420 obarray 'commandp t))
421 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
7a698dc1 422 (let ((func (indirect-function definition))
3829bcc5 423 (defs nil)
7a698dc1 424 (standard-output (if insert (current-buffer) t)))
3829bcc5
SM
425 (mapatoms (lambda (symbol)
426 (and (fboundp symbol)
427 (not (eq symbol definition))
d92c2757
RS
428 (eq func (condition-case ()
429 (indirect-function symbol)
430 (error symbol)))
3829bcc5 431 (push symbol defs))))
7a698dc1
JB
432 (princ (mapconcat
433 #'(lambda (symbol)
023b93f6 434 (let* ((remapped (command-remapping symbol))
3829bcc5
SM
435 (keys (where-is-internal
436 symbol overriding-local-map nil nil remapped))
437 (keys (mapconcat 'key-description keys ", ")))
7a698dc1
JB
438 (if insert
439 (if (> (length keys) 0)
440 (if remapped
441 (format "%s (%s) (remapped from %s)"
442 keys remapped symbol)
443 (format "%s (%s)" keys symbol))
444 (format "M-x %s RET" symbol))
445 (if (> (length keys) 0)
446 (if remapped
447 (format "%s is remapped to %s which is on %s"
448 definition symbol keys)
449 (format "%s is on %s" symbol keys))
450 (format "%s is not on any key" symbol)))))
3829bcc5 451 (cons definition defs)
7a698dc1 452 ";\nand ")))
54c0b967
RS
453 nil)
454
788d62cf
MB
455(defun string-key-binding (key)
456 "Value is the binding of KEY in a string.
457If KEY is an event on a string, and that string has a `local-map'
458or `keymap' property, return the binding of KEY in the string's keymap."
459 (let* ((defn nil)
460 (start (when (vectorp key)
f03b7c7d
GM
461 (if (memq (aref key 0)
462 '(mode-line header-line left-margin right-margin))
788d62cf
MB
463 (event-start (aref key 1))
464 (and (consp (aref key 0))
465 (event-start (aref key 0))))))
466 (string-info (and (consp start) (nth 4 start))))
467 (when string-info
468 (let* ((string (car string-info))
469 (pos (cdr string-info))
f03b7c7d 470 (local-map (and (>= pos 0)
788d62cf
MB
471 (< pos (length string))
472 (or (get-text-property pos 'local-map string)
473 (get-text-property pos 'keymap string)))))
474 (setq defn (and local-map (lookup-key local-map key)))))
475 defn))
1a06eabd 476
02dfca16
SM
477(defun help-key-description (key untranslated)
478 (let ((string (key-description key)))
ae1bb8ac
SM
479 (if (or (not untranslated)
480 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
481 string
482 (let ((otherstring (key-description untranslated)))
483 (if (equal string otherstring)
484 string
485 (format "%s (translated from %s)" string otherstring))))))
71296446 486
02dfca16 487(defun describe-key-briefly (key &optional insert untranslated)
788d62cf 488 "Print the name of the function KEY invokes. KEY is a string.
02dfca16
SM
489If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
490If non-nil UNTRANSLATED is a vector of the untranslated events.
491It can also be a number in which case the untranslated events from
492the last key hit are used."
493 (interactive "kDescribe key briefly: \nP\np")
494 (if (numberp untranslated)
495 (setq untranslated (this-single-command-raw-keys)))
400a1b1f 496 (save-excursion
788d62cf
MB
497 (let ((modifiers (event-modifiers (aref key 0)))
498 (standard-output (if insert (current-buffer) t))
499 window position)
500 ;; For a mouse button event, go to the button it applies to
501 ;; to get the right key bindings. And go to the right place
502 ;; in case the keymap depends on where you clicked.
503 (if (or (memq 'click modifiers) (memq 'down modifiers)
504 (memq 'drag modifiers))
505 (setq window (posn-window (event-start (aref key 0)))
506 position (posn-point (event-start (aref key 0)))))
507 (if (windowp window)
508 (progn
509 (set-buffer (window-buffer window))
510 (goto-char position)))
511 ;; Ok, now look up the key and name the command.
512 (let ((defn (or (string-key-binding key)
513 (key-binding key)))
02dfca16 514 (key-desc (help-key-description key untranslated)))
d61ac4a6 515 (if (or (null defn) (integerp defn) (equal defn 'undefined))
788d62cf 516 (princ (format "%s is undefined" key-desc))
80d553d4
RS
517 (princ (format (if (windowp window)
518 "%s at that spot runs the command %s"
519 "%s runs the command %s")
788d62cf
MB
520 key-desc
521 (if (symbolp defn) defn (prin1-to-string defn)))))))))
96ede6b2 522
788d62cf 523
02dfca16 524(defun describe-key (key &optional untranslated)
a0d32c10
RS
525 "Display documentation of the function invoked by KEY.
526KEY should be a key sequence--when calling from a program,
02dfca16
SM
527pass a string or a vector.
528If non-nil UNTRANSLATED is a vector of the untranslated events.
529It can also be a number in which case the untranslated events from
530the last key hit are used."
531 (interactive "kDescribe key: \np")
532 (if (numberp untranslated)
533 (setq untranslated (this-single-command-raw-keys)))
788d62cf
MB
534 (save-excursion
535 (let ((modifiers (event-modifiers (aref key 0)))
536 window position)
537 ;; For a mouse button event, go to the button it applies to
538 ;; to get the right key bindings. And go to the right place
539 ;; in case the keymap depends on where you clicked.
540 (if (or (memq 'click modifiers) (memq 'down modifiers)
541 (memq 'drag modifiers))
542 (setq window (posn-window (event-start (aref key 0)))
543 position (posn-point (event-start (aref key 0)))))
94ea540b 544 (when (windowp window)
788d62cf 545 (set-buffer (window-buffer window))
94ea540b 546 (goto-char position))
788d62cf 547 (let ((defn (or (string-key-binding key) (key-binding key))))
d61ac4a6 548 (if (or (null defn) (integerp defn) (equal defn 'undefined))
02dfca16 549 (message "%s is undefined" (help-key-description key untranslated))
3e5929af
SM
550 (help-setup-xref (list #'describe-function defn) (interactive-p))
551 (with-output-to-temp-buffer (help-buffer)
02dfca16 552 (princ (help-key-description key untranslated))
788d62cf
MB
553 (if (windowp window)
554 (princ " at that spot"))
555 (princ " runs the command ")
556 (prin1 defn)
557 (princ "\n which is ")
3e5929af 558 (describe-function-1 defn)
788d62cf 559 (print-help-return-message)))))))
400a1b1f 560
400a1b1f 561\f
788d62cf
MB
562(defun describe-mode (&optional buffer)
563 "Display documentation of current major mode and minor modes.
564The major mode description comes first, followed by the minor modes,
565each on a separate page.
566For this to work correctly for a minor mode, the mode's indicator variable
567\(listed in `minor-mode-alist') must also be a function whose documentation
568describes the minor mode."
400a1b1f 569 (interactive)
9639be74
RS
570 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
571 (interactive-p))
572 ;; For the sake of help-do-xref and help-xref-go-back,
573 ;; don't switch buffers before calling `help-buffer'.
3e5929af 574 (with-output-to-temp-buffer (help-buffer)
9639be74
RS
575 (save-excursion
576 (when buffer (set-buffer buffer))
577 (when minor-mode-alist
578 (princ "The major mode is described first.
788d62cf 579For minor modes, see following pages.\n\n"))
9639be74
RS
580 (princ mode-name)
581 (princ " mode:\n")
582 (princ (documentation major-mode))
583 (let ((minor-modes minor-mode-alist))
584 (while minor-modes
585 (let* ((minor-mode (car (car minor-modes)))
586 (indicator (car (cdr (car minor-modes)))))
587 ;; Document a minor mode if it is listed in minor-mode-alist,
588 ;; bound locally in this buffer, non-nil, and has a function
589 ;; definition.
590 (if (and (boundp minor-mode)
591 (symbol-value minor-mode)
592 (fboundp minor-mode))
593 (let ((pretty-minor-mode minor-mode))
594 (if (string-match "\\(-minor\\)?-mode\\'"
595 (symbol-name minor-mode))
596 (setq pretty-minor-mode
597 (capitalize
598 (substring (symbol-name minor-mode)
599 0 (match-beginning 0)))))
600 (while (and indicator (symbolp indicator)
601 (boundp indicator)
602 (not (eq indicator (symbol-value indicator))))
603 (setq indicator (symbol-value indicator)))
604 (princ "\n\f\n")
605 (princ (format "%s minor mode (%s):\n"
606 pretty-minor-mode
607 (if indicator
608 (format "indicator%s" indicator)
609 "no indicator")))
610 (princ (documentation minor-mode)))))
611 (setq minor-modes (cdr minor-modes))))
612 (print-help-return-message))))
400a1b1f 613
8e864068 614(defun describe-minor-mode (minor-mode)
335028c3
MY
615 "Display documentation of a minor mode given as MINOR-MODE.
616MINOR-MODE can be a minor mode symbol or a minor mode indicator string
617appeared on the mode-line."
618 (interactive (list (completing-read
619 "Minor mode: "
620 (nconc
621 (describe-minor-mode-completion-table-for-symbol)
622 (describe-minor-mode-completion-table-for-indicator)
623 ))))
624 (if (symbolp minor-mode)
625 (setq minor-mode (symbol-name minor-mode)))
626 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
627 (indicators (describe-minor-mode-completion-table-for-indicator)))
628 (cond
629 ((member minor-mode symbols)
630 (describe-minor-mode-from-symbol (intern minor-mode)))
631 ((member minor-mode indicators)
632 (describe-minor-mode-from-indicator minor-mode))
633 (t
634 (error "No such minor mode: %s" minor-mode)))))
635
636;; symbol
637(defun describe-minor-mode-completion-table-for-symbol ()
638 ;; In order to list up all minor modes, minor-mode-list
639 ;; is used here instead of minor-mode-alist.
640 (delq nil (mapcar 'symbol-name minor-mode-list)))
641(defun describe-minor-mode-from-symbol (symbol)
642 "Display documentation of a minor mode given as a symbol, SYMBOL"
8e864068 643 (interactive (list (intern (completing-read
335028c3
MY
644 "Minor mode symbol: "
645 (describe-minor-mode-completion-table-for-symbol)))))
646 (if (fboundp symbol)
647 (describe-function symbol)
648 (describe-variable symbol)))
649
650;; indicator
651(defun describe-minor-mode-completion-table-for-indicator ()
652 (delq nil
653 (mapcar (lambda (x)
654 (let ((i (format-mode-line x)))
655 ;; remove first space if existed
656 (cond
657 ((= 0 (length i))
658 nil)
659 ((eq (aref i 0) ?\ )
660 (substring i 1))
661 (t
662 i))))
663 minor-mode-alist)))
8e864068 664(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
665 "Display documentation of a minor mode specified by INDICATOR.
666If you call this function interactively, you can give indicator which
667is currently activated with completion."
8e864068
JB
668 (interactive (list
669 (completing-read
670 "Minor mode indicator: "
335028c3 671 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
672 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
673 (if minor-mode
335028c3 674 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
675 (error "Cannot find minor mode for `%s'" indicator))))
676
677(defun lookup-minor-mode-from-indicator (indicator)
678 "Return a minor mode symbol from its indicator on the modeline."
335028c3 679 ;; remove first space if existed
8e864068 680 (if (and (< 0 (length indicator))
335028c3
MY
681 (eq (aref indicator 0) ?\ ))
682 (setq indicator (substring indicator 1)))
8e864068
JB
683 (let ((minor-modes minor-mode-alist)
684 result)
685 (while minor-modes
686 (let* ((minor-mode (car (car minor-modes)))
335028c3
MY
687 (anindicator (format-mode-line
688 (car (cdr (car minor-modes))))))
689 ;; remove first space if existed
8e864068 690 (if (and (stringp anindicator)
335028c3
MY
691 (> (length anindicator) 0)
692 (eq (aref anindicator 0) ?\ ))
693 (setq anindicator (substring anindicator 1)))
694 (if (equal indicator anindicator)
8e864068
JB
695 (setq result minor-mode
696 minor-modes nil)
697 (setq minor-modes (cdr minor-modes)))))
698 result))
699
48ce3c22
RS
700\f
701;;; Automatic resizing of temporary buffers.
702
4483ddc5 703(defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
48ce3c22
RS
704 "*Maximum height of a window displaying a temporary buffer.
705This is the maximum height (in text lines) which `resize-temp-buffer-window'
706will give to a window displaying a temporary buffer.
707It can also be a function which will be called with the object corresponding
708to the buffer to be displayed as argument and should return an integer
709positive number."
710 :type '(choice integer function)
711 :group 'help
712 :version "20.4")
713
4e1ede6c
SM
714(define-minor-mode temp-buffer-resize-mode
715 "Toggle the mode which makes windows smaller for temporary buffers.
48ce3c22
RS
716With prefix argument ARG, turn the resizing of windows displaying temporary
717buffers on if ARG is positive or off otherwise.
4e1ede6c
SM
718This makes the window the right height for its contents, but never
719more than `temp-buffer-max-height' nor less than `window-min-height'.
720This applies to `help', `apropos' and `completion' buffers, and some others."
b0fbf754 721 :global t :group 'help
4e1ede6c 722 (if temp-buffer-resize-mode
57f43907 723 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
724 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
725 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 726 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22
RS
727
728(defun resize-temp-buffer-window ()
729 "Resize the current window to fit its contents.
4483ddc5 730Will not make it higher than `temp-buffer-max-height' nor smaller than
b2c85790 731`window-min-height'. Do nothing if it is the only window on its frame, if it
48ce3c22
RS
732is not as wide as the frame or if some of the window's contents are scrolled
733out of view."
734 (unless (or (one-window-p 'nomini)
735 (not (pos-visible-in-window-p (point-min)))
736 (/= (frame-width) (window-width)))
d9c30bdf
MB
737 (fit-window-to-buffer
738 (selected-window)
739 (if (functionp temp-buffer-max-height)
740 (funcall temp-buffer-max-height (current-buffer))
741 temp-buffer-max-height))))
48ce3c22 742
5fbeea74
GM
743;; Provide this for the sake of define-minor-mode which generates
744;; defcustoms which require 'help'.
5fbeea74
GM
745(provide 'help)
746
ab5796a9 747;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1a06eabd 748;;; help.el ends here