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