* lisp/help.el (where-is): Use `default' arg of completing-read.
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
00156f95 3;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
3a801d0c 4
34dc21db 5;; Maintainer: emacs-devel@gnu.org
fd7fa35a 6;; Keywords: help, internal
bd78fa1d 7;; Package: emacs
e5167999 8
433ae6f6
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
433ae6f6 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
433ae6f6
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
433ae6f6 23
d9ecc911
ER
24;;; Commentary:
25
44e97401 26;; This code implements GNU Emacs's on-line help system, the one invoked by
95ac0a6f 27;; `M-x help-for-help'.
d9ecc911 28
e5167999
ER
29;;; Code:
30
8aa3a187
RS
31;; Get the macro make-help-screen when this is compiled,
32;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 33(eval-when-compile (require 'help-macro))
788d62cf 34
cde56121
MR
35;; `help-window-point-marker' is a marker you can move to a valid
36;; position of the buffer shown in the help window in order to override
37;; the standard positioning mechanism (`point-min') chosen by
c5e28e39
MR
38;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
39;; `with-help-window' has this point nowhere before exiting. Currently
40;; used by `view-lossage' to assert that the last keystrokes are always
41;; visible.
7b01c8d7 42(defvar help-window-point-marker (make-marker)
357f93d2 43 "Marker to override default `window-point' in help windows.")
cde56121 44
604aa5f0
SM
45(defvar help-map
46 (let ((map (make-sparse-keymap)))
47 (define-key map (char-to-string help-char) 'help-for-help)
48 (define-key map [help] 'help-for-help)
49 (define-key map [f1] 'help-for-help)
50 (define-key map "." 'display-local-help)
51 (define-key map "?" 'help-for-help)
52
d4a18332 53 (define-key map "\C-a" 'about-emacs)
604aa5f0 54 (define-key map "\C-c" 'describe-copying)
7b01c8d7
KS
55 (define-key map "\C-d" 'view-emacs-debugging)
56 (define-key map "\C-e" 'view-external-packages)
604aa5f0
SM
57 (define-key map "\C-f" 'view-emacs-FAQ)
58 (define-key map "\C-m" 'view-order-manuals)
59 (define-key map "\C-n" 'view-emacs-news)
7b01c8d7
KS
60 (define-key map "\C-o" 'describe-distribution)
61 (define-key map "\C-p" 'view-emacs-problems)
62 (define-key map "\C-t" 'view-emacs-todo)
604aa5f0
SM
63 (define-key map "\C-w" 'describe-no-warranty)
64
65 ;; This does not fit the pattern, but it is natural given the C-\ command.
66 (define-key map "\C-\\" 'describe-input-method)
67
68 (define-key map "C" 'describe-coding-system)
69 (define-key map "F" 'Info-goto-emacs-command-node)
70 (define-key map "I" 'describe-input-method)
71 (define-key map "K" 'Info-goto-emacs-key-command-node)
72 (define-key map "L" 'describe-language-environment)
73 (define-key map "S" 'info-lookup-symbol)
74
75 (define-key map "a" 'apropos-command)
76 (define-key map "b" 'describe-bindings)
77 (define-key map "c" 'describe-key-briefly)
78 (define-key map "d" 'apropos-documentation)
79 (define-key map "e" 'view-echo-area-messages)
80 (define-key map "f" 'describe-function)
7b01c8d7 81 (define-key map "g" 'describe-gnu-project)
604aa5f0
SM
82 (define-key map "h" 'view-hello-file)
83
84 (define-key map "i" 'info)
85 (define-key map "4i" 'info-other-window)
86
87 (define-key map "k" 'describe-key)
88 (define-key map "l" 'view-lossage)
89 (define-key map "m" 'describe-mode)
90 (define-key map "n" 'view-emacs-news)
91 (define-key map "p" 'finder-by-keyword)
cb6c4991 92 (define-key map "P" 'describe-package)
604aa5f0
SM
93 (define-key map "r" 'info-emacs-manual)
94 (define-key map "s" 'describe-syntax)
95 (define-key map "t" 'help-with-tutorial)
96 (define-key map "w" 'where-is)
97 (define-key map "v" 'describe-variable)
98 (define-key map "q" 'help-quit)
99 map)
433ae6f6
RS
100 "Keymap for characters following the Help key.")
101
e17d2fd1 102(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
103(define-key global-map [help] 'help-command)
104(define-key global-map [f1] 'help-command)
433ae6f6
RS
105(fset 'help-command help-map)
106
e25e90b4
DP
107;; insert-button makes the action nil if it is not store somewhere
108(defvar help-button-cache nil)
109
0cf0d828 110\f
2fc9d9f4 111(defun help-quit ()
3120a677 112 "Just exit from the Help command's command loop."
2fc9d9f4
RS
113 (interactive)
114 nil)
115
01364a75
RS
116(defvar help-return-method nil
117 "What to do to \"exit\" the help buffer.
118This is a list
a8e7142c 119 (WINDOW . t) delete the selected window (and possibly its frame,
f3ada0ee 120 see `quit-window'), go to WINDOW.
01364a75
RS
121 (WINDOW . quit-window) do quit-window, then select WINDOW.
122 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
123
b3d8e4a0
SM
124(define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
125(defun help-print-return-message (&optional function)
433ae6f6 126 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
127This function assumes that `standard-output' is the help buffer.
128It computes a message, and applies the optional argument FUNCTION to it.
a8e7142c
EZ
129If FUNCTION is nil, it applies `message', thus displaying the message.
130In addition, this function sets up `help-return-method', which see, that
b92631bf
MR
131specifies what to do when the user exits the help buffer.
132
133Do not call this in the scope of `with-help-window'."
433ae6f6 134 (and (not (get-buffer-window standard-output))
d536293f 135 (let ((first-message
a8e7142c
EZ
136 (cond ((or
137 pop-up-frames
138 (special-display-p (buffer-name standard-output)))
01364a75 139 (setq help-return-method (cons (selected-window) t))
d536293f
RS
140 ;; If the help output buffer is a special display buffer,
141 ;; don't say anything about how to get rid of it.
142 ;; First of all, the user will do that with the window
143 ;; manager, not with Emacs.
144 ;; Secondly, the buffer has not been displayed yet,
145 ;; so we don't know whether its frame will be selected.
d536293f
RS
146 nil)
147 ((not (one-window-p t))
01364a75
RS
148 (setq help-return-method
149 (cons (selected-window) 'quit-window))
cb0b6766 150 "Type \\[display-buffer] RET to restore the other window.")
d536293f 151 (pop-up-windows
01364a75 152 (setq help-return-method (cons (selected-window) t))
d536293f
RS
153 "Type \\[delete-other-windows] to remove help window.")
154 (t
01364a75
RS
155 (setq help-return-method
156 (list (selected-window) (window-buffer)
157 (window-start) (window-point)))
d536293f
RS
158 "Type \\[switch-to-buffer] RET to remove help window."))))
159 (funcall (or function 'message)
160 (concat
161 (if first-message
376b2a24
DL
162 (substitute-command-keys first-message))
163 (if first-message " ")
125a8d70
RS
164 ;; If the help buffer will go in a separate frame,
165 ;; it's no use mentioning a command to scroll, so don't.
7b057a3d
EZ
166 (if (or pop-up-windows
167 (special-display-p (buffer-name standard-output)))
125a8d70 168 nil
a1c9f209 169 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
170 ;; Say how to scroll this window.
171 (substitute-command-keys
172 "\\[scroll-up] to scroll the help.")
173 ;; Say how to scroll some other window.
6e7f5182 174 (substitute-command-keys
125a8d70 175 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 176
433ae6f6
RS
177;; So keyboard macro definitions are documented correctly
178(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
179
a4bdcdf3
RS
180(defalias 'help 'help-for-help-internal)
181;; find-function can find this.
182(defalias 'help-for-help 'help-for-help-internal)
183;; It can't find this, but nobody will look.
184(make-help-screen help-for-help-internal
9cd39aff 185 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
ce9a0ccb
SM
186 ;; Don't purecopy this one, because it's not evaluated (it's
187 ;; directly used as a docstring in a function definition, so it'll
188 ;; be moved to the DOC file anyway: no need for purecopying it).
788d62cf
MB
189 "You have typed %THIS-KEY%, the help character. Type a Help option:
190\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
191
7b01c8d7
KS
192a PATTERN Show commands whose name matches the PATTERN (a list of words
193 or a regexp). See also the `apropos' command.
194b Display all key bindings.
195c KEYS Display the command name run by the given key sequence.
196C CODING Describe the given coding system, or RET for current ones.
197d PATTERN Show a list of functions, variables, and other items whose
198 documentation matches the PATTERN (a list of words or a regexp).
199e Go to the *Messages* buffer which logs echo-area messages.
200f FUNCTION Display documentation for the given function.
201F COMMAND Show the on-line manual's section that describes the command.
202g Display information about the GNU project.
203h Display the HELLO file which illustrates various scripts.
204i Start the Info documentation reader: read on-line manuals.
205I METHOD Describe a specific input method, or RET for current.
206k KEYS Display the full documentation for the key sequence.
207K KEYS Show the on-line manual's section for the command bound to KEYS.
90ad3541 208l Show last 300 input keystrokes (lossage).
7b01c8d7
KS
209L LANG-ENV Describes a specific language environment, or RET for current.
210m Display documentation of current minor modes and current major mode,
211 including their special commands.
212n Display news of recent Emacs changes.
213p TOPIC Find packages matching a given topic keyword.
0c633f13 214P PACKAGE Describe the given Emacs Lisp package.
7b01c8d7
KS
215r Display the Emacs manual in Info mode.
216s Display contents of current syntax table, plus explanations.
217S SYMBOL Show the section for the given symbol in the on-line manual
218 for the programming language used in this buffer.
219t Start the Emacs learn-by-doing tutorial.
220v VARIABLE Display the given variable's documentation and value.
221w COMMAND Display which keystrokes invoke the given command (where-is).
222. Display any available local help at point in the echo area.
223
224C-a Information about Emacs.
225C-c Emacs copying permission (GNU General Public License).
226C-d Instructions for debugging GNU Emacs.
227C-e External packages and information about Emacs.
228C-f Emacs FAQ.
229C-m How to order printed Emacs manuals.
230C-n News of recent Emacs changes.
231C-o Emacs ordering and distribution information.
232C-p Info about known Emacs problems.
233C-t Emacs TODO list.
ce9a0ccb 234C-w Information on absence of warranty for GNU Emacs."
788d62cf
MB
235 help-map)
236
237\f
238
239(defun function-called-at-point ()
240 "Return a function around point or else called by the list containing point.
241If that doesn't give a function, return nil."
2e6750c8
SM
242 (with-syntax-table emacs-lisp-mode-syntax-table
243 (or (condition-case ()
244 (save-excursion
245 (or (not (zerop (skip-syntax-backward "_w")))
246 (eq (char-syntax (following-char)) ?w)
247 (eq (char-syntax (following-char)) ?_)
248 (forward-sexp -1))
249 (skip-chars-forward "'")
250 (let ((obj (read (current-buffer))))
251 (and (symbolp obj) (fboundp obj) obj)))
252 (error nil))
253 (condition-case ()
254 (save-excursion
255 (save-restriction
256 (narrow-to-region (max (point-min)
257 (- (point) 1000)) (point-max))
258 ;; Move up to surrounding paren, then after the open.
259 (backward-up-list 1)
260 (forward-char 1)
261 ;; If there is space here, this is probably something
262 ;; other than a real Lisp function call, so ignore it.
263 (if (looking-at "[ \t]")
264 (error "Probably not a Lisp function call"))
265 (let ((obj (read (current-buffer))))
266 (and (symbolp obj) (fboundp obj) obj))))
267 (error nil))
268 (let* ((str (find-tag-default))
269 (sym (if str (intern-soft str))))
270 (if (and sym (fboundp sym))
271 sym
272 (save-match-data
273 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
274 (setq sym (intern-soft (match-string 1 str)))
275 (and (fboundp sym) sym))))))))
788d62cf
MB
276
277\f
278;;; `User' help functions
279
7b01c8d7
KS
280(defun view-help-file (file &optional dir)
281 (view-file (expand-file-name file (or dir data-directory)))
3ffb948c 282 (goto-address-mode 1)
7b01c8d7
KS
283 (goto-char (point-min)))
284
433ae6f6
RS
285(defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
287 (interactive)
7b01c8d7 288 (view-help-file "DISTRIB"))
433ae6f6
RS
289
290(defun describe-copying ()
291 "Display info on how you may redistribute copies of GNU Emacs."
292 (interactive)
7b01c8d7 293 (view-help-file "COPYING"))
433ae6f6 294
00156f95 295;; Maybe this command should just be removed.
7b01c8d7 296(defun describe-gnu-project ()
00156f95 297 "Browse online information on the GNU project."
76766f2d 298 (interactive)
00156f95 299 (browse-url "http://www.gnu.org/gnu/thegnuproject.html"))
76766f2d 300
4e44f5ce
KS
301(define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
302
433ae6f6
RS
303(defun describe-no-warranty ()
304 "Display info on all the kinds of warranty Emacs does NOT have."
305 (interactive)
306 (describe-copying)
307 (let (case-fold-search)
e7e2b26c
GM
308 (search-forward "Disclaimer of Warranty")
309 (forward-line 0)
433ae6f6
RS
310 (recenter 0)))
311
61c6b658 312(defun describe-prefix-bindings ()
c7cba9cb
RS
313 "Describe the bindings of the prefix used to reach this command.
314The prefix described consists of all but the last event
315of the key sequence that ran this command."
61c6b658 316 (interactive)
0f101663 317 (let ((key (this-command-keys)))
ccc06dcc
KH
318 (describe-bindings
319 (if (stringp key)
320 (substring key 0 (1- (length key)))
321 (let ((prefix (make-vector (1- (length key)) nil))
322 (i 0))
323 (while (< i (length prefix))
324 (aset prefix i (aref key i))
325 (setq i (1+ i)))
326 prefix)))))
788d62cf 327;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 328;; run describe-prefix-bindings.
61c6b658
RS
329(setq prefix-help-command 'describe-prefix-bindings)
330
e38cc268 331(defun view-emacs-news (&optional version)
382d018a 332 "Display info on recent changes to Emacs.
598ea453 333With argument, display info only for the selected version."
382d018a 334 (interactive "P")
e38cc268
KS
335 (unless version
336 (setq version emacs-major-version))
337 (when (consp version)
338 (let* ((all-versions
339 (let (res)
cc6650af 340 (mapc
e38cc268
KS
341 (lambda (file)
342 (with-temp-buffer
343 (insert-file-contents
344 (expand-file-name file data-directory))
345 (while (re-search-forward
346 (if (member file '("NEWS.18" "NEWS.1-17"))
347 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
348 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
349 (setq res (cons (match-string-no-properties 1) res)))))
350 (cons "NEWS"
351 (directory-files data-directory nil
352 "^NEWS\\.[0-9][-0-9]*$" nil)))
353 (sort (delete-dups res) (lambda (a b) (string< b a)))))
8c9fc4be 354 (current (car all-versions)))
e38cc268
KS
355 (setq version (completing-read
356 (format "Read NEWS for the version (default %s): " current)
357 all-versions nil nil nil nil current))
358 (if (integerp (string-to-number version))
359 (setq version (string-to-number version))
360 (unless (or (member version all-versions)
361 (<= (string-to-number version) (string-to-number current)))
362 (error "No news about version %s" version)))))
363 (when (integerp version)
364 (cond ((<= version 12)
365 (setq version (format "1.%d" version)))
366 ((<= version 18)
367 (setq version (format "%d" version)))
368 ((> version emacs-major-version)
e08b54f2 369 (error "No news about Emacs %d (yet)" version))))
e38cc268
KS
370 (let* ((vn (if (stringp version)
371 (string-to-number version)
372 version))
373 (file (cond
374 ((>= vn emacs-major-version) "NEWS")
375 ((< vn 18) "NEWS.1-17")
8c9fc4be
KS
376 (t (format "NEWS.%d" vn))))
377 res)
e38cc268
KS
378 (view-file (expand-file-name file data-directory))
379 (widen)
380 (goto-char (point-min))
381 (when (stringp version)
382 (when (re-search-forward
383 (concat (if (< vn 19)
384 "Changes in Emacs[ \t]*"
385 "^\* [^0-9\n]*") version "$")
386 nil t)
387 (beginning-of-line)
388 (narrow-to-region
389 (point)
390 (save-excursion
391 (while (and (setq res
392 (re-search-forward
393 (if (< vn 19)
394 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
395 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
396 (equal (match-string-no-properties 1) version)))
397 (or res (goto-char (point-max)))
398 (beginning-of-line)
399 (point)))))))
400
06b60517 401(defun view-emacs-todo (&optional _arg)
1ebc1f01
RS
402 "Display the Emacs TODO list."
403 (interactive "P")
7b01c8d7 404 (view-help-file "TODO"))
1ebc1f01 405
4e44f5ce
KS
406(define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
407
408
4f16ea85
RS
409(defun view-echo-area-messages ()
410 "View the log of recent echo-area messages: the `*Messages*' buffer.
411The number of messages retained in that buffer
412is specified by the variable `message-log-max'."
413 (interactive)
90582f05 414 (with-current-buffer (messages-buffer)
123ecb68
CY
415 (goto-char (point-max))
416 (display-buffer (current-buffer))))
4f16ea85 417
754084bb 418(defun view-order-manuals ()
458dbf5e 419 "Display information on how to buy printed copies of Emacs manuals."
754084bb 420 (interactive)
458dbf5e
GM
421;; (view-help-file "ORDERS")
422 (info "(emacs)Printed Books"))
754084bb 423
7ee71cf1
RS
424(defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
94ea540b 427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 428 (info "(efaq)"))
7ee71cf1 429
4cbff657
DL
430(defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
7b01c8d7
KS
433 (view-help-file "PROBLEMS"))
434
435(defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
439
e9be0a13 440;; This used to visit MORE.STUFF; maybe it should just be removed.
7b01c8d7 441(defun view-external-packages ()
e9be0a13 442 "Display info on where to get more Emacs packages."
7b01c8d7 443 (interactive)
e9be0a13 444 (info "(efaq)Packages that do not come with Emacs"))
4cbff657 445
433ae6f6 446(defun view-lossage ()
6b8d1c72 447 "Display last 300 input keystrokes.
50b57199 448
50ea0f87 449To record all your input, use `open-dribble-file'."
433ae6f6 450 (interactive)
32226619
JB
451 (help-setup-xref (list #'view-lossage)
452 (called-interactively-p 'interactive))
cde56121 453 (with-help-window (help-buffer)
02dfca16
SM
454 (princ (mapconcat (lambda (key)
455 (if (or (integerp key) (symbolp key) (listp key))
456 (single-key-description key)
457 (prin1-to-string key nil)))
298a7c8c
RS
458 (recent-keys)
459 " "))
b0fbf754 460 (with-current-buffer standard-output
433ae6f6
RS
461 (goto-char (point-min))
462 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
463 (when (search-forward " " nil t)
464 (delete-char -1))
cde56121
MR
465 (insert "\n"))
466 ;; jidanni wants to see the last keystrokes immediately.
467 (set-marker help-window-point-marker (point)))))
433ae6f6 468
788d62cf
MB
469\f
470;; Key bindings
433ae6f6 471
4c45295b 472(defun describe-bindings (&optional prefix buffer)
92c70367
LI
473 "Display a buffer showing a list of all defined keys, and their definitions.
474The keys are displayed in order of precedence.
a8ad43aa
RS
475
476The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
477then we display only bindings that start with that prefix.
478The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
479to display (default, the current buffer). BUFFER can be a buffer
480or a buffer name."
3e5929af 481 (interactive)
4c45295b 482 (or buffer (setq buffer (current-buffer)))
32226619
JB
483 (help-setup-xref (list #'describe-bindings prefix buffer)
484 (called-interactively-p 'interactive))
5b9925ae
SM
485 (with-help-window (help-buffer)
486 ;; Be aware that `describe-buffer-bindings' puts its output into
487 ;; the current buffer.
488 (with-current-buffer (help-buffer)
489 (describe-buffer-bindings buffer prefix))))
a8ad43aa 490
94ea540b
SM
491;; This function used to be in keymap.c.
492(defun describe-bindings-internal (&optional menus prefix)
493 "Show a list of all defined keys, and their definitions.
494We put that list in a buffer, and display the buffer.
495
496The optional argument MENUS, if non-nil, says to mention menu bindings.
497\(Ordinarily these are omitted from the output.)
498The optional argument PREFIX, if non-nil, should be a key sequence;
499then we display only bindings that start with that prefix."
5b9925ae 500 (declare (obsolete describe-buffer-bindings "24.4"))
94ea540b 501 (let ((buf (current-buffer)))
880e6158 502 (with-help-window (help-buffer)
b92631bf
MR
503 ;; Be aware that `describe-buffer-bindings' puts its output into
504 ;; the current buffer.
505 (with-current-buffer (help-buffer)
506 (describe-buffer-bindings buf prefix menus)))))
94ea540b 507
e88a2c59 508(defun where-is (definition &optional insert)
b2c85790 509 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
510Argument is a command definition, usually a symbol with a function definition.
511If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
512 (interactive
513 (let ((fn (function-called-at-point))
788d62cf 514 (enable-recursive-minibuffers t)
54c0b967 515 val)
3829bcc5
SM
516 (setq val (completing-read
517 (if fn
518 (format "Where is command (default %s): " fn)
519 "Where is command: ")
e016b8f1
CD
520 obarray 'commandp t nil nil
521 (and fn (symbol-name fn))))
522 (list (unless (equal val "") (intern val))
523 current-prefix-arg)))
463d75ac 524 (unless definition (error "No command"))
7a698dc1 525 (let ((func (indirect-function definition))
3829bcc5 526 (defs nil)
e3ce671f 527 (standard-output (if insert (current-buffer) standard-output)))
740b479c 528 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
529 (mapatoms (lambda (symbol)
530 (and (fboundp symbol)
531 (not (eq symbol definition))
d92c2757
RS
532 (eq func (condition-case ()
533 (indirect-function symbol)
534 (error symbol)))
3829bcc5 535 (push symbol defs))))
740b479c
RS
536 ;; Look at all the symbols--first DEFINITION,
537 ;; then its aliases.
538 (dolist (symbol (cons definition defs))
539 (let* ((remapped (command-remapping symbol))
540 (keys (where-is-internal
541 symbol overriding-local-map nil nil remapped))
542 (keys (mapconcat 'key-description keys ", "))
543 string)
544 (setq string
545 (if insert
546 (if (> (length keys) 0)
547 (if remapped
548 (format "%s (%s) (remapped from %s)"
549 keys remapped symbol)
550 (format "%s (%s)" keys symbol))
551 (format "M-x %s RET" symbol))
552 (if (> (length keys) 0)
553 (if remapped
554 (format "%s is remapped to %s which is on %s"
a5f43550 555 symbol remapped keys)
740b479c
RS
556 (format "%s is on %s" symbol keys))
557 ;; If this is the command the user asked about,
558 ;; and it is not on any key, say so.
559 ;; For other symbols, its aliases, say nothing
560 ;; about them unless they are on keys.
561 (if (eq symbol definition)
562 (format "%s is not on any key" symbol)))))
563 (when string
564 (unless (eq symbol definition)
565 (princ ";\n its alias "))
566 (princ string)))))
54c0b967
RS
567 nil)
568
02dfca16
SM
569(defun help-key-description (key untranslated)
570 (let ((string (key-description key)))
ae1bb8ac
SM
571 (if (or (not untranslated)
572 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
573 string
574 (let ((otherstring (key-description untranslated)))
575 (if (equal string otherstring)
576 string
577 (format "%s (translated from %s)" string otherstring))))))
71296446 578
6527c983
EZ
579(defun describe-key-briefly (&optional key insert untranslated)
580 "Print the name of the function KEY invokes. KEY is a string.
581If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
582If non-nil, UNTRANSLATED is a vector of the untranslated events.
583It can also be a number in which case the untranslated events from
584the last key hit are used.
585
586If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 587temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
588 (interactive
589 (let ((enable-disabled-menus-and-buttons t)
590 (cursor-in-echo-area t)
591 saved-yank-menu)
592 (unwind-protect
593 (let (key)
594 ;; If yank-menu is empty, populate it temporarily, so that
595 ;; "Select and Paste" menu can generate a complete event.
596 (when (null (cdr yank-menu))
597 (setq saved-yank-menu (copy-sequence yank-menu))
598 (menu-bar-update-yank-menu "(any string)" nil))
599 (setq key (read-key-sequence "Describe key (or click or menu item): "))
74c582e6
CY
600 ;; Clear the echo area message (Bug#7014).
601 (message nil)
46d91fa0 602 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
603 ;; corresponding up-event. Note that there are also
604 ;; down-events on scroll bars and mode lines: the actual
605 ;; event then is in the second element of the vector.
606 (and (vectorp key)
badf89ea
RS
607 (let ((last-idx (1- (length key))))
608 (and (eventp (aref key last-idx))
609 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 610 (read-event))
2c8ed538
RS
611 (list
612 key
774a814f
RS
613 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
614 1))
2c8ed538
RS
615 ;; Put yank-menu back as it was, if we changed it.
616 (when saved-yank-menu
617 (setq yank-menu (copy-sequence saved-yank-menu))
618 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
619 (if (numberp untranslated)
620 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
621 (let* ((event (if (and (symbolp (aref key 0))
622 (> (length key) 1)
623 (consp (aref key 1)))
624 (aref key 1)
625 (aref key 0)))
626 (modifiers (event-modifiers event))
b2cba41e 627 (standard-output (if insert (current-buffer) standard-output))
24a27882
KS
628 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
629 (memq 'drag modifiers)) " at that spot" ""))
630 (defn (key-binding key t))
631 key-desc)
632 ;; Handle the case where we faked an entry in "Select and Paste" menu.
633 (if (and (eq defn nil)
634 (stringp (aref key (1- (length key))))
635 (eq (key-binding (substring key 0 -1)) 'yank-menu))
636 (setq defn 'menu-bar-select-yank))
637 ;; Don't bother user with strings from (e.g.) the select-paste menu.
638 (if (stringp (aref key (1- (length key))))
639 (aset key (1- (length key)) "(any string)"))
640 (if (and (> (length untranslated) 0)
641 (stringp (aref untranslated (1- (length untranslated)))))
642 (aset untranslated (1- (length untranslated)) "(any string)"))
643 ;; Now describe the key, perhaps as changed.
644 (setq key-desc (help-key-description key untranslated))
645 (if (or (null defn) (integerp defn) (equal defn 'undefined))
646 (princ (format "%s%s is undefined" key-desc mouse-msg))
647 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 648
6527c983
EZ
649(defun describe-key (&optional key untranslated up-event)
650 "Display documentation of the function invoked by KEY.
651KEY can be any kind of a key sequence; it can include keyboard events,
652mouse events, and/or menu events. When calling from a program,
653pass KEY as a string or a vector.
654
655If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
656It can also be a number, in which case the untranslated events from
657the last key sequence entered are used.
658UP-EVENT is the up-event that was discarded by reading KEY, or nil.
659
660If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 661temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
662 (interactive
663 (let ((enable-disabled-menus-and-buttons t)
664 (cursor-in-echo-area t)
665 saved-yank-menu)
666 (unwind-protect
667 (let (key)
668 ;; If yank-menu is empty, populate it temporarily, so that
669 ;; "Select and Paste" menu can generate a complete event.
670 (when (null (cdr yank-menu))
671 (setq saved-yank-menu (copy-sequence yank-menu))
672 (menu-bar-update-yank-menu "(any string)" nil))
673 (setq key (read-key-sequence "Describe key (or click or menu item): "))
674 (list
675 key
676 (prefix-numeric-value current-prefix-arg)
c3f82997 677 ;; If KEY is a down-event, read and include the
badf89ea
RS
678 ;; corresponding up-event. Note that there are also
679 ;; down-events on scroll bars and mode lines: the actual
680 ;; event then is in the second element of the vector.
91a2acb2 681 (and (vectorp key)
badf89ea
RS
682 (let ((last-idx (1- (length key))))
683 (and (eventp (aref key last-idx))
684 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 685 (or (and (eventp (aref key 0))
98da283b
CY
686 (memq 'down (event-modifiers (aref key 0)))
687 ;; However, for the C-down-mouse-2 popup
688 ;; menu, there is no subsequent up-event. In
689 ;; this case, the up-event is the next
690 ;; element in the supplied vector.
691 (= (length key) 1))
91a2acb2
DK
692 (and (> (length key) 1)
693 (eventp (aref key 1))
694 (memq 'down (event-modifiers (aref key 1)))))
695 (read-event))))
2c8ed538
RS
696 ;; Put yank-menu back as it was, if we changed it.
697 (when saved-yank-menu
698 (setq yank-menu (copy-sequence saved-yank-menu))
699 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
700 (if (numberp untranslated)
701 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
702 (let* ((event (aref key (if (and (symbolp (aref key 0))
703 (> (length key) 1)
704 (consp (aref key 1)))
705 1
706 0)))
91a2acb2 707 (modifiers (event-modifiers event))
24a27882
KS
708 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
709 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8
KS
710 (defn (key-binding key t))
711 defn-up defn-up-tricky ev-type
712 mouse-1-remapped mouse-1-tricky)
91a2acb2 713
05ca18a8 714 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 715 (when (and (eq defn nil)
91a2acb2
DK
716 (stringp (aref key (1- (length key))))
717 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
718 (setq defn 'menu-bar-select-yank))
719 (if (or (null defn) (integerp defn) (equal defn 'undefined))
720 (message "%s%s is undefined"
721 (help-key-description key untranslated) mouse-msg)
32226619
JB
722 (help-setup-xref (list #'describe-function defn)
723 (called-interactively-p 'interactive))
24a27882
KS
724 ;; Don't bother user with strings from (e.g.) the select-paste menu.
725 (when (stringp (aref key (1- (length key))))
726 (aset key (1- (length key)) "(any string)"))
727 (when (and untranslated
91a2acb2 728 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
729 (aset untranslated (1- (length untranslated))
730 "(any string)"))
731 ;; Need to do this before erasing *Help* buffer in case event
732 ;; is a mouse click in an existing *Help* buffer.
733 (when up-event
734 (setq ev-type (event-basic-type up-event))
735 (let ((sequence (vector up-event)))
736 (when (and (eq ev-type 'mouse-1)
737 mouse-1-click-follows-link
738 (not (eq mouse-1-click-follows-link 'double))
739 (setq mouse-1-remapped
740 (mouse-on-link-p (event-start up-event))))
741 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
742 (> mouse-1-click-follows-link 0)))
743 (cond ((stringp mouse-1-remapped)
744 (setq sequence mouse-1-remapped))
745 ((vectorp mouse-1-remapped)
746 (setcar up-event (elt mouse-1-remapped 0)))
747 (t (setcar up-event 'mouse-2))))
748 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
749 (when mouse-1-tricky
750 (setq sequence (vector up-event))
751 (aset sequence 0 'mouse-1)
752 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
cde56121 753 (with-help-window (help-buffer)
24a27882
KS
754 (princ (help-key-description key untranslated))
755 (princ (format "\
b96817c3 756%s runs the command %S, which is "
24a27882
KS
757 mouse-msg defn))
758 (describe-function-1 defn)
05ca18a8 759 (when up-event
24a27882
KS
760 (unless (or (null defn-up)
761 (integerp defn-up)
762 (equal defn-up 'undefined))
763 (princ (format "
764
765----------------- up-event %s----------------
766
e8579ebc 767%s%s%s runs the command %S, which is "
24a27882 768 (if mouse-1-tricky "(short click) " "")
e8579ebc
CY
769 (key-description (vector up-event))
770 mouse-msg
24a27882 771 (if mouse-1-remapped
b96817c3 772 " is remapped to <mouse-2>, which" "")
24a27882
KS
773 defn-up))
774 (describe-function-1 defn-up))
775 (unless (or (null defn-up-tricky)
776 (integerp defn-up-tricky)
777 (eq defn-up-tricky 'undefined))
778 (princ (format "
779
780----------------- up-event (long click) ----------------
781
782Pressing <%S>%s for longer than %d milli-seconds
b96817c3 783runs the command %S, which is "
24a27882
KS
784 ev-type mouse-msg
785 mouse-1-click-follows-link
786 defn-up-tricky))
cde56121 787 (describe-function-1 defn-up-tricky)))))))
400a1b1f 788\f
788d62cf
MB
789(defun describe-mode (&optional buffer)
790 "Display documentation of current major mode and minor modes.
efde809a
JPW
791A brief summary of the minor modes comes first, followed by the
792major mode description. This is followed by detailed
793descriptions of the minor modes, each on a separate page.
794
795For this to work correctly for a minor mode, the mode's indicator
796variable \(listed in `minor-mode-alist') must also be a function
27d1f87a
CY
797whose documentation describes the minor mode.
798
799If called from Lisp with a non-nil BUFFER argument, display
800documentation for the major and minor modes of that buffer."
f3b5dd74 801 (interactive "@")
dd39c336
SM
802 (unless buffer (setq buffer (current-buffer)))
803 (help-setup-xref (list #'describe-mode buffer)
32226619 804 (called-interactively-p 'interactive))
9639be74
RS
805 ;; For the sake of help-do-xref and help-xref-go-back,
806 ;; don't switch buffers before calling `help-buffer'.
cde56121 807 (with-help-window (help-buffer)
dd39c336 808 (with-current-buffer buffer
f6c57ef6 809 (let (minor-modes)
dd39c336
SM
810 ;; Older packages do not register in minor-mode-list but only in
811 ;; minor-mode-alist.
812 (dolist (x minor-mode-alist)
813 (setq x (car x))
814 (unless (memq x minor-mode-list)
815 (push x minor-mode-list)))
f6c57ef6
RS
816 ;; Find enabled minor mode we will want to mention.
817 (dolist (mode minor-mode-list)
818 ;; Document a minor mode if it is listed in minor-mode-alist,
819 ;; non-nil, and has a function definition.
af5f4483
SM
820 (let ((fmode (or (get mode :minor-mode-function) mode)))
821 (and (boundp mode) (symbol-value mode)
822 (fboundp fmode)
823 (let ((pretty-minor-mode
824 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
825 (symbol-name fmode))
826 (capitalize
827 (substring (symbol-name fmode)
828 0 (match-beginning 0)))
829 fmode)))
830 (push (list fmode pretty-minor-mode
831 (format-mode-line (assq mode minor-mode-alist)))
832 minor-modes)))))
f6c57ef6
RS
833 (setq minor-modes
834 (sort minor-modes
af5f4483 835 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 836 (when minor-modes
71723367 837 (princ "Enabled minor modes:\n")
e25e90b4
DP
838 (make-local-variable 'help-button-cache)
839 (with-current-buffer standard-output
840 (dolist (mode minor-modes)
af5f4483
SM
841 (let ((mode-function (nth 0 mode))
842 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
843 (indicator (nth 2 mode)))
844 (add-text-properties 0 (length pretty-minor-mode)
845 '(face bold) pretty-minor-mode)
846 (save-excursion
847 (goto-char (point-max))
848 (princ "\n\f\n")
849 (push (point-marker) help-button-cache)
850 ;; Document the minor modes fully.
851 (insert pretty-minor-mode)
71723367
RS
852 (princ (format " minor mode (%s):\n"
853 (if (zerop (length indicator))
854 "no indicator"
855 (format "indicator%s"
d8a869ea 856 indicator))))
e25e90b4 857 (princ (documentation mode-function)))
e25e90b4
DP
858 (insert-button pretty-minor-mode
859 'action (car help-button-cache)
72b64ad5 860 'follow-link t
e25e90b4 861 'help-echo "mouse-2, RET: show full information")
71723367
RS
862 (newline)))
863 (forward-line -1)
864 (fill-paragraph nil)
865 (forward-line 1))
866
867 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 868 ;; Document the major mode.
e25e90b4
DP
869 (let ((mode mode-name))
870 (with-current-buffer standard-output
4e6d3170 871 (let ((start (point)))
145fe412 872 (insert (format-mode-line mode nil nil buffer))
4e6d3170 873 (add-text-properties start (point) '(face bold)))))
9d05d1ba
JB
874 (princ " mode")
875 (let* ((mode major-mode)
876 (file-name (find-lisp-object-file-name mode nil)))
877 (when file-name
878 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
879 ;; Make a hyperlink to the library.
880 (with-current-buffer standard-output
881 (save-excursion
882 (re-search-backward "`\\([^`']+\\)'" nil t)
883 (help-xref-button 1 'help-function-def mode file-name)))))
884 (princ ":\n")
309d5b43 885 (princ (documentation major-mode)))))
6460e534 886 ;; For the sake of IELM and maybe others
309d5b43 887 nil)
400a1b1f 888
f6c57ef6 889
8e864068 890(defun describe-minor-mode (minor-mode)
335028c3
MY
891 "Display documentation of a minor mode given as MINOR-MODE.
892MINOR-MODE can be a minor mode symbol or a minor mode indicator string
893appeared on the mode-line."
7ada28ac 894 (interactive (list (completing-read
335028c3
MY
895 "Minor mode: "
896 (nconc
897 (describe-minor-mode-completion-table-for-symbol)
898 (describe-minor-mode-completion-table-for-indicator)
899 ))))
900 (if (symbolp minor-mode)
901 (setq minor-mode (symbol-name minor-mode)))
902 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
903 (indicators (describe-minor-mode-completion-table-for-indicator)))
904 (cond
905 ((member minor-mode symbols)
906 (describe-minor-mode-from-symbol (intern minor-mode)))
907 ((member minor-mode indicators)
908 (describe-minor-mode-from-indicator minor-mode))
909 (t
910 (error "No such minor mode: %s" minor-mode)))))
911
7ada28ac 912;; symbol
335028c3
MY
913(defun describe-minor-mode-completion-table-for-symbol ()
914 ;; In order to list up all minor modes, minor-mode-list
915 ;; is used here instead of minor-mode-alist.
916 (delq nil (mapcar 'symbol-name minor-mode-list)))
357f93d2 917
335028c3
MY
918(defun describe-minor-mode-from-symbol (symbol)
919 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 920 (interactive (list (intern (completing-read
335028c3
MY
921 "Minor mode symbol: "
922 (describe-minor-mode-completion-table-for-symbol)))))
923 (if (fboundp symbol)
924 (describe-function symbol)
925 (describe-variable symbol)))
926
927;; indicator
928(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 929 (delq nil
335028c3
MY
930 (mapcar (lambda (x)
931 (let ((i (format-mode-line x)))
932 ;; remove first space if existed
933 (cond
934 ((= 0 (length i))
935 nil)
94318c8a 936 ((eq (aref i 0) ?\s)
335028c3 937 (substring i 1))
7ada28ac 938 (t
335028c3
MY
939 i))))
940 minor-mode-alist)))
357f93d2 941
8e864068 942(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
943 "Display documentation of a minor mode specified by INDICATOR.
944If you call this function interactively, you can give indicator which
945is currently activated with completion."
7ada28ac
LT
946 (interactive (list
947 (completing-read
8e864068 948 "Minor mode indicator: "
335028c3 949 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
950 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
951 (if minor-mode
335028c3 952 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
953 (error "Cannot find minor mode for `%s'" indicator))))
954
955(defun lookup-minor-mode-from-indicator (indicator)
37269466 956 "Return a minor mode symbol from its indicator on the mode line."
335028c3 957 ;; remove first space if existed
7ada28ac 958 (if (and (< 0 (length indicator))
94318c8a 959 (eq (aref indicator 0) ?\s))
335028c3 960 (setq indicator (substring indicator 1)))
8e864068
JB
961 (let ((minor-modes minor-mode-alist)
962 result)
963 (while minor-modes
964 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 965 (anindicator (format-mode-line
335028c3
MY
966 (car (cdr (car minor-modes))))))
967 ;; remove first space if existed
7ada28ac 968 (if (and (stringp anindicator)
335028c3 969 (> (length anindicator) 0)
94318c8a 970 (eq (aref anindicator 0) ?\s))
335028c3
MY
971 (setq anindicator (substring anindicator 1)))
972 (if (equal indicator anindicator)
8e864068
JB
973 (setq result minor-mode
974 minor-modes nil)
975 (setq minor-modes (cdr minor-modes)))))
976 result))
48ce3c22
RS
977\f
978;;; Automatic resizing of temporary buffers.
ef654460
MR
979(defcustom temp-buffer-max-height
980 (lambda (buffer)
981 (if (eq (selected-window) (frame-root-window))
982 (/ (x-display-pixel-height) (frame-char-height) 2)
983 (/ (- (frame-height) 2) 2)))
4e6d3170 984 "Maximum height of a window displaying a temporary buffer.
90e357ae 985This is effective only when Temp Buffer Resize mode is enabled.
357f93d2
MR
986The value is the maximum height (in lines) which
987`resize-temp-buffer-window' will give to a window displaying a
988temporary buffer. It can also be a function to be called to
e4920bc9 989choose the height for such a buffer. It gets one argument, the
357f93d2
MR
990buffer, and should return a positive integer. At the time the
991function is called, the window to be resized is selected."
48ce3c22
RS
992 :type '(choice integer function)
993 :group 'help
6ba6a3e5 994 :version "24.3")
ef654460 995
880e6158
MR
996(defcustom temp-buffer-max-width
997 (lambda (buffer)
998 (if (eq (selected-window) (frame-root-window))
999 (/ (x-display-pixel-width) (frame-char-width) 2)
1000 (/ (- (frame-width) 2) 2)))
1001 "Maximum width of a window displaying a temporary buffer.
1002This is effective only when Temp Buffer Resize mode is enabled.
1003The value is the maximum width (in columns) which
1004`resize-temp-buffer-window' will give to a window displaying a
1005temporary buffer. It can also be a function to be called to
1006choose the width for such a buffer. It gets one argument, the
1007buffer, and should return a positive integer. At the time the
1008function is called, the window to be resized is selected."
1009 :type '(choice integer function)
1010 :group 'help
1011 :version "24.4")
1012
4e1ede6c 1013(define-minor-mode temp-buffer-resize-mode
c5e28e39 1014 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
06e21633
CY
1015With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1016is positive, and disable it otherwise. If called from Lisp,
1017enable the mode if ARG is omitted or nil.
1018
1019When Temp Buffer Resize mode is enabled, the windows in which we
c5e28e39 1020show a temporary buffer are automatically resized in height to
06e21633
CY
1021fit the buffer's contents, but never more than
1022`temp-buffer-max-height' nor less than `window-min-height'.
357f93d2 1023
8e17c9ba
MR
1024A window is resized only if it has been specially created for the
1025buffer. Windows that have shown another buffer before are not
5938d519
MR
1026resized. A frame is resized only if `fit-frame-to-buffer' is
1027non-nil.
8e17c9ba 1028
357f93d2
MR
1029This mode is used by `help', `apropos' and `completion' buffers,
1030and some others."
b0fbf754 1031 :global t :group 'help
4e1ede6c 1032 (if temp-buffer-resize-mode
57f43907 1033 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
1034 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1035 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 1036 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22 1037
c5e28e39
MR
1038(defun resize-temp-buffer-window (&optional window)
1039 "Resize WINDOW to fit its contents.
880e6158
MR
1040WINDOW must be a live window and defaults to the selected one.
1041Do not resize if WINDOW was not created by `display-buffer'.
1042
1043If WINDOW is part of a vertical combination, restrain its new
1044size by `temp-buffer-max-height' and do not resize if its minimum
1045accessible position is scrolled out of view. If WINDOW is part
1046of a horizontal combination, restrain its new size by
1047`temp-buffer-max-width'. In both cases, the value of the option
1048`fit-window-to-buffer-horizontally' can inhibit resizing.
1049
1050If WINDOW is the root window of its frame, resize the frame
1051provided `fit-frame-to-buffer' is non-nil."
c5e28e39 1052 (setq window (window-normalize-window window t))
9d3aa82c
JB
1053 (let ((height (if (functionp temp-buffer-max-height)
1054 (with-selected-window window
1055 (funcall temp-buffer-max-height (window-buffer)))
1056 temp-buffer-max-height))
880e6158
MR
1057 (width (if (functionp temp-buffer-max-width)
1058 (with-selected-window window
1059 (funcall temp-buffer-max-width (window-buffer)))
1060 temp-buffer-max-width))
9d3aa82c 1061 (quit-cadr (cadr (window-parameter window 'quit-restore))))
880e6158
MR
1062 ;; Resize WINDOW iff it was made by `display-buffer'.
1063 (when (or (and (eq quit-cadr 'window)
1064 (or (and (window-combined-p window)
1065 (not (eq fit-window-to-buffer-horizontally
1066 'only))
1067 (pos-visible-in-window-p (point-min) window))
1068 (and (window-combined-p window t)
1069 fit-window-to-buffer-horizontally)))
1070 (and (eq quit-cadr 'frame)
1071 fit-frame-to-buffer
1072 (eq window (frame-root-window window))))
1073 (fit-window-to-buffer window height nil width))))
48ce3c22 1074
357f93d2 1075;;; Help windows.
cde56121
MR
1076(defcustom help-window-select 'other
1077 "Non-nil means select help window for viewing.
1078Choices are:
1079 never (nil) Select help window only if there is no other window
1080 on its frame.
1081 other Select help window unless the selected window is the
357f93d2 1082 only other window on the help window's frame.
cde56121
MR
1083 always (t) Always select the help window.
1084
1085This option has effect if and only if the help window was created
1086by `with-help-window'"
1087 :type '(choice (const :tag "never (nil)" nil)
1088 (const :tag "other" other)
1089 (const :tag "always (t)" t))
1090 :group 'help
1091 :version "23.1")
1092
c89926a5
CY
1093(defcustom help-enable-auto-load t
1094 "Whether Help commands can perform autoloading.
1095If non-nil, whenever \\[describe-function] is called for an
1096autoloaded function whose docstring contains any key substitution
1097construct (see `substitute-command-keys'), the library is loaded,
1098so that the documentation can show the right key bindings."
1099 :type 'boolean
1100 :group 'help
2a1e2476 1101 :version "24.3")
c89926a5 1102
357f93d2 1103(defun help-window-display-message (quit-part window &optional scroll)
cde56121
MR
1104 "Display message telling how to quit and scroll help window.
1105QUIT-PART is a string telling how to quit the help window WINDOW.
357f93d2
MR
1106Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1107SCROLL equal `other' means tell how to scroll the \"other\"
1108window."
cde56121
MR
1109 (let ((scroll-part
1110 (cond
357f93d2
MR
1111 ;; If we don't have QUIT-PART we probably reuse a window
1112 ;; showing the same buffer so we don't show any message.
1113 ((not quit-part) nil)
cde56121
MR
1114 ((pos-visible-in-window-p
1115 (with-current-buffer (window-buffer window)
357f93d2
MR
1116 (point-max)) window t)
1117 ;; Buffer end is at least partially visible, no need to talk
1118 ;; about scrolling.
cde56121 1119 ".")
357f93d2
MR
1120 ((eq scroll 'other)
1121 ", \\[scroll-other-window] to scroll help.")
1122 (scroll ", \\[scroll-up] to scroll help."))))
f6e7ec02 1123 (message "%s"
cde56121
MR
1124 (substitute-command-keys (concat quit-part scroll-part)))))
1125
880e6158
MR
1126(defun help-window-setup (window &optional value)
1127 "Set up help window WINDOW for `with-help-window'.
1128WINDOW is the window used for displaying the help buffer.
1129Return VALUE."
1130 (let* ((help-buffer (when (window-live-p window)
1131 (window-buffer window)))
1132 (help-setup (when (window-live-p window)
1133 (car (window-parameter window 'quit-restore)))))
357f93d2
MR
1134 (when help-buffer
1135 ;; Handle `help-window-point-marker'.
1136 (when (eq (marker-buffer help-window-point-marker) help-buffer)
880e6158 1137 (set-window-point window help-window-point-marker)
357f93d2
MR
1138 ;; Reset `help-window-point-marker'.
1139 (set-marker help-window-point-marker nil))
cde56121 1140
cde56121 1141 (cond
880e6158 1142 ((or (eq window (selected-window))
357f93d2 1143 (and (or (eq help-window-select t)
cf4eacfd 1144 (eq help-setup 'frame)
357f93d2 1145 (and (eq help-window-select 'other)
880e6158 1146 (eq (window-frame window) (selected-frame))
357f93d2 1147 (> (length (window-list nil 'no-mini)) 2)))
880e6158 1148 (select-window window)))
357f93d2
MR
1149 ;; The help window is or gets selected ...
1150 (help-window-display-message
1151 (cond
cf4eacfd 1152 ((eq help-setup 'window)
357f93d2 1153 ;; ... and is new, ...
f678e0b6 1154 "Type \"q\" to delete help window")
cf4eacfd 1155 ((eq help-setup 'frame)
880e6158 1156 "Type \"q\" to quit the help frame")
cf4eacfd 1157 ((eq help-setup 'other)
357f93d2
MR
1158 ;; ... or displayed some other buffer before.
1159 "Type \"q\" to restore previous buffer"))
880e6158
MR
1160 window t))
1161 ((and (eq (window-frame window) (selected-frame))
357f93d2
MR
1162 (= (length (window-list nil 'no-mini)) 2))
1163 ;; There are two windows on the help window's frame and the
1164 ;; other one is the selected one.
1165 (help-window-display-message
1166 (cond
cf4eacfd 1167 ((eq help-setup 'window)
357f93d2 1168 "Type \\[delete-other-windows] to delete the help window")
cf4eacfd 1169 ((eq help-setup 'other)
f678e0b6 1170 "Type \"q\" in help window to restore its previous buffer"))
880e6158 1171 window 'other))
cde56121 1172 (t
f678e0b6 1173 ;; The help window is not selected ...
357f93d2 1174 (help-window-display-message
f678e0b6 1175 (cond
cf4eacfd 1176 ((eq help-setup 'window)
f678e0b6
MR
1177 ;; ... and is new, ...
1178 "Type \"q\" in help window to delete it")
cf4eacfd 1179 ((eq help-setup 'other)
f678e0b6
MR
1180 ;; ... or displayed some other buffer before.
1181 "Type \"q\" in help window to restore previous buffer"))
880e6158
MR
1182 window))))
1183 ;; Return VALUE.
1184 value))
cde56121 1185
880e6158 1186;; `with-help-window' is a wrapper for `with-temp-buffer-window'
cde56121
MR
1187;; providing the following additional twists:
1188
880e6158 1189;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
b92631bf 1190;; adds cross references (via `help-mode-finish').
880e6158
MR
1191
1192;; (2) It issues a message telling how to scroll and quit the help
b92631bf 1193;; window (via `help-window-setup').
cde56121 1194
880e6158 1195;; (3) An option (customizable via `help-window-select') to select the
b92631bf 1196;; help window automatically.
cde56121 1197
880e6158 1198;; (4) A marker (`help-window-point-marker') to move point in the help
b92631bf 1199;; window to an arbitrary buffer position.
cde56121 1200(defmacro with-help-window (buffer-name &rest body)
b92631bf
MR
1201 "Display buffer named BUFFER-NAME in a help window.
1202Evaluate the forms in BODY with standard output bound to a buffer
1203called BUFFER-NAME (creating it if it does not exist), put that
1204buffer in `help-mode', display the buffer in a window (see
1205`with-temp-buffer-window' for details) and issue a message how to
1206deal with that \"help\" window when it's no more needed. Select
1207the help window if the current value of the user option
1208`help-window-select' says so. Return last value in BODY."
cde56121 1209 (declare (indent 1) (debug t))
357f93d2 1210 `(progn
357f93d2
MR
1211 ;; Make `help-window-point-marker' point nowhere. The only place
1212 ;; where this should be set to a buffer position is within BODY.
cde56121 1213 (set-marker help-window-point-marker nil)
880e6158
MR
1214 (let ((temp-buffer-window-setup-hook
1215 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1216 (temp-buffer-window-show-hook
1217 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1218 (with-temp-buffer-window
1219 ,buffer-name nil 'help-window-setup (progn ,@body)))))
cbb59342
CY
1220
1221;; Called from C, on encountering `help-char' when reading a char.
1222;; Don't print to *Help*; that would clobber Help history.
1223(defun help-form-show ()
1224 "Display the output of a non-nil `help-form'."
1225 (let ((msg (eval help-form)))
1226 (if (stringp msg)
1227 (with-output-to-temp-buffer " *Char Help*"
1228 (princ msg)))))
56759cf1
GM
1229
1230\f
1231;; The following functions used to be in help-fns.el, which is not preloaded.
1232;; But for various reasons, they are more widely needed, so they were
1233;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1234
1235(defun help-split-fundoc (docstring def)
1236 "Split a function DOCSTRING into the actual doc and the usage info.
1237Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1238is a string describing the argument list of DEF, such as
1239\"(apply FUNCTION &rest ARGUMENTS)\".
1240DEF is the function whose usage we're looking for in DOCSTRING."
1241 ;; Functions can get the calling sequence at the end of the doc string.
1242 ;; In cases where `function' has been fset to a subr we can't search for
1243 ;; function's name in the doc string so we use `fn' as the anonymous
1244 ;; function name instead.
1245 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1246 (cons (format "(%s%s"
1247 ;; Replace `fn' with the actual function name.
1248 (if (symbolp def) def "anonymous")
1249 (match-string 1 docstring))
1250 (unless (zerop (match-beginning 0))
1251 (substring docstring 0 (match-beginning 0))))))
1252
1253(defun help-add-fundoc-usage (docstring arglist)
1254 "Add the usage info to DOCSTRING.
1255If DOCSTRING already has a usage info, then just return it unchanged.
1256The usage info is built from ARGLIST. DOCSTRING can be nil.
1257ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1258 (unless (stringp docstring) (setq docstring ""))
1259 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1260 (eq arglist t))
1261 docstring
1262 (concat docstring
1263 (if (string-match "\n?\n\\'" docstring)
1264 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1265 "\n\n")
1266 (if (and (stringp arglist)
1267 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1268 (concat "(fn" (match-string 1 arglist) ")")
1269 (format "%S" (help-make-usage 'fn arglist))))))
1270
1271(defun help-function-arglist (def &optional preserve-names)
1272 "Return a formal argument list for the function DEF.
1273IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1274the same names as used in the original source code, when possible."
1275 ;; Handle symbols aliased to other symbols.
1276 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1277 ;; If definition is a macro, find the function inside it.
1278 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1279 (cond
1280 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1281 ((eq (car-safe def) 'lambda) (nth 1 def))
1282 ((eq (car-safe def) 'closure) (nth 2 def))
1283 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1284 (subrp def))
1285 (or (when preserve-names
1286 (let* ((doc (condition-case nil (documentation def) (error nil)))
1287 (docargs (if doc (car (help-split-fundoc doc nil))))
1288 (arglist (if docargs
1289 (cdar (read-from-string (downcase docargs)))))
1290 (valid t))
1291 ;; Check validity.
1292 (dolist (arg arglist)
1293 (unless (and (symbolp arg)
1294 (let ((name (symbol-name arg)))
1295 (if (eq (aref name 0) ?&)
1296 (memq arg '(&rest &optional))
1297 (not (string-match "\\." name)))))
1298 (setq valid nil)))
1299 (when valid arglist)))
1300 (let* ((args-desc (if (not (subrp def))
1301 (aref def 0)
1302 (let ((a (subr-arity def)))
1303 (logior (car a)
1304 (if (numberp (cdr a))
1305 (lsh (cdr a) 8)
1306 (lsh 1 7))))))
1307 (max (lsh args-desc -8))
1308 (min (logand args-desc 127))
1309 (rest (logand args-desc 128))
1310 (arglist ()))
1311 (dotimes (i min)
1312 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1313 (when (> max min)
1314 (push '&optional arglist)
1315 (dotimes (i (- max min))
1316 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1317 arglist)))
1318 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1319 (nreverse arglist))))
1320 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1321 "[Arg list not available until function definition is loaded.]")
1322 (t t)))
1323
1324(defun help-make-usage (function arglist)
1325 (cons (if (symbolp function) function 'anonymous)
1326 (mapcar (lambda (arg)
1327 (if (not (symbolp arg)) arg
1328 (let ((name (symbol-name arg)))
1329 (cond
1330 ((string-match "\\`&" name) arg)
1331 ((string-match "\\`_" name)
1332 (intern (upcase (substring name 1))))
1333 (t (intern (upcase name)))))))
1334 arglist)))
1335
cde56121 1336\f
172892e3
JB
1337(provide 'help)
1338
1a06eabd 1339;;; help.el ends here