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