* calendar/todo-mode.el: Fix two bugs.
[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))
5b9925ae
SM
488 (with-help-window (help-buffer)
489 ;; Be aware that `describe-buffer-bindings' puts its output into
490 ;; the current buffer.
491 (with-current-buffer (help-buffer)
492 (describe-buffer-bindings buffer prefix))))
a8ad43aa 493
94ea540b
SM
494;; This function used to be in keymap.c.
495(defun describe-bindings-internal (&optional menus prefix)
496 "Show a list of all defined keys, and their definitions.
497We put that list in a buffer, and display the buffer.
498
499The optional argument MENUS, if non-nil, says to mention menu bindings.
500\(Ordinarily these are omitted from the output.)
501The optional argument PREFIX, if non-nil, should be a key sequence;
502then we display only bindings that start with that prefix."
5b9925ae 503 (declare (obsolete describe-buffer-bindings "24.4"))
94ea540b 504 (let ((buf (current-buffer)))
880e6158 505 (with-help-window (help-buffer)
b92631bf
MR
506 ;; Be aware that `describe-buffer-bindings' puts its output into
507 ;; the current buffer.
508 (with-current-buffer (help-buffer)
509 (describe-buffer-bindings buf prefix menus)))))
94ea540b 510
e88a2c59 511(defun where-is (definition &optional insert)
b2c85790 512 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
513Argument is a command definition, usually a symbol with a function definition.
514If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
515 (interactive
516 (let ((fn (function-called-at-point))
788d62cf 517 (enable-recursive-minibuffers t)
54c0b967 518 val)
3829bcc5
SM
519 (setq val (completing-read
520 (if fn
521 (format "Where is command (default %s): " fn)
522 "Where is command: ")
523 obarray 'commandp t))
524 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
463d75ac 525 (unless definition (error "No command"))
7a698dc1 526 (let ((func (indirect-function definition))
3829bcc5 527 (defs nil)
e3ce671f 528 (standard-output (if insert (current-buffer) standard-output)))
740b479c 529 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
530 (mapatoms (lambda (symbol)
531 (and (fboundp symbol)
532 (not (eq symbol definition))
d92c2757
RS
533 (eq func (condition-case ()
534 (indirect-function symbol)
535 (error symbol)))
3829bcc5 536 (push symbol defs))))
740b479c
RS
537 ;; Look at all the symbols--first DEFINITION,
538 ;; then its aliases.
539 (dolist (symbol (cons definition defs))
540 (let* ((remapped (command-remapping symbol))
541 (keys (where-is-internal
542 symbol overriding-local-map nil nil remapped))
543 (keys (mapconcat 'key-description keys ", "))
544 string)
545 (setq string
546 (if insert
547 (if (> (length keys) 0)
548 (if remapped
549 (format "%s (%s) (remapped from %s)"
550 keys remapped symbol)
551 (format "%s (%s)" keys symbol))
552 (format "M-x %s RET" symbol))
553 (if (> (length keys) 0)
554 (if remapped
555 (format "%s is remapped to %s which is on %s"
a5f43550 556 symbol remapped keys)
740b479c
RS
557 (format "%s is on %s" symbol keys))
558 ;; If this is the command the user asked about,
559 ;; and it is not on any key, say so.
560 ;; For other symbols, its aliases, say nothing
561 ;; about them unless they are on keys.
562 (if (eq symbol definition)
563 (format "%s is not on any key" symbol)))))
564 (when string
565 (unless (eq symbol definition)
566 (princ ";\n its alias "))
567 (princ string)))))
54c0b967
RS
568 nil)
569
02dfca16
SM
570(defun help-key-description (key untranslated)
571 (let ((string (key-description key)))
ae1bb8ac
SM
572 (if (or (not untranslated)
573 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
574 string
575 (let ((otherstring (key-description untranslated)))
576 (if (equal string otherstring)
577 string
578 (format "%s (translated from %s)" string otherstring))))))
71296446 579
6527c983
EZ
580(defun describe-key-briefly (&optional key insert untranslated)
581 "Print the name of the function KEY invokes. KEY is a string.
582If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
583If non-nil, UNTRANSLATED is a vector of the untranslated events.
584It can also be a number in which case the untranslated events from
585the last key hit are used.
586
587If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 588temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
589 (interactive
590 (let ((enable-disabled-menus-and-buttons t)
591 (cursor-in-echo-area t)
592 saved-yank-menu)
593 (unwind-protect
594 (let (key)
595 ;; If yank-menu is empty, populate it temporarily, so that
596 ;; "Select and Paste" menu can generate a complete event.
597 (when (null (cdr yank-menu))
598 (setq saved-yank-menu (copy-sequence yank-menu))
599 (menu-bar-update-yank-menu "(any string)" nil))
600 (setq key (read-key-sequence "Describe key (or click or menu item): "))
74c582e6
CY
601 ;; Clear the echo area message (Bug#7014).
602 (message nil)
46d91fa0 603 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
604 ;; corresponding up-event. Note that there are also
605 ;; down-events on scroll bars and mode lines: the actual
606 ;; event then is in the second element of the vector.
607 (and (vectorp key)
badf89ea
RS
608 (let ((last-idx (1- (length key))))
609 (and (eventp (aref key last-idx))
610 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 611 (read-event))
2c8ed538
RS
612 (list
613 key
774a814f
RS
614 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
615 1))
2c8ed538
RS
616 ;; Put yank-menu back as it was, if we changed it.
617 (when saved-yank-menu
618 (setq yank-menu (copy-sequence saved-yank-menu))
619 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
620 (if (numberp untranslated)
621 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
622 (let* ((event (if (and (symbolp (aref key 0))
623 (> (length key) 1)
624 (consp (aref key 1)))
625 (aref key 1)
626 (aref key 0)))
627 (modifiers (event-modifiers event))
b2cba41e 628 (standard-output (if insert (current-buffer) standard-output))
24a27882
KS
629 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
630 (memq 'drag modifiers)) " at that spot" ""))
631 (defn (key-binding key t))
632 key-desc)
633 ;; Handle the case where we faked an entry in "Select and Paste" menu.
634 (if (and (eq defn nil)
635 (stringp (aref key (1- (length key))))
636 (eq (key-binding (substring key 0 -1)) 'yank-menu))
637 (setq defn 'menu-bar-select-yank))
638 ;; Don't bother user with strings from (e.g.) the select-paste menu.
639 (if (stringp (aref key (1- (length key))))
640 (aset key (1- (length key)) "(any string)"))
641 (if (and (> (length untranslated) 0)
642 (stringp (aref untranslated (1- (length untranslated)))))
643 (aset untranslated (1- (length untranslated)) "(any string)"))
644 ;; Now describe the key, perhaps as changed.
645 (setq key-desc (help-key-description key untranslated))
646 (if (or (null defn) (integerp defn) (equal defn 'undefined))
647 (princ (format "%s%s is undefined" key-desc mouse-msg))
648 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 649
6527c983
EZ
650(defun describe-key (&optional key untranslated up-event)
651 "Display documentation of the function invoked by KEY.
652KEY can be any kind of a key sequence; it can include keyboard events,
653mouse events, and/or menu events. When calling from a program,
654pass KEY as a string or a vector.
655
656If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
657It can also be a number, in which case the untranslated events from
658the last key sequence entered are used.
659UP-EVENT is the up-event that was discarded by reading KEY, or nil.
660
661If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 662temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
663 (interactive
664 (let ((enable-disabled-menus-and-buttons t)
665 (cursor-in-echo-area t)
666 saved-yank-menu)
667 (unwind-protect
668 (let (key)
669 ;; If yank-menu is empty, populate it temporarily, so that
670 ;; "Select and Paste" menu can generate a complete event.
671 (when (null (cdr yank-menu))
672 (setq saved-yank-menu (copy-sequence yank-menu))
673 (menu-bar-update-yank-menu "(any string)" nil))
674 (setq key (read-key-sequence "Describe key (or click or menu item): "))
675 (list
676 key
677 (prefix-numeric-value current-prefix-arg)
c3f82997 678 ;; If KEY is a down-event, read and include the
badf89ea
RS
679 ;; corresponding up-event. Note that there are also
680 ;; down-events on scroll bars and mode lines: the actual
681 ;; event then is in the second element of the vector.
91a2acb2 682 (and (vectorp key)
badf89ea
RS
683 (let ((last-idx (1- (length key))))
684 (and (eventp (aref key last-idx))
685 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 686 (or (and (eventp (aref key 0))
98da283b
CY
687 (memq 'down (event-modifiers (aref key 0)))
688 ;; However, for the C-down-mouse-2 popup
689 ;; menu, there is no subsequent up-event. In
690 ;; this case, the up-event is the next
691 ;; element in the supplied vector.
692 (= (length key) 1))
91a2acb2
DK
693 (and (> (length key) 1)
694 (eventp (aref key 1))
695 (memq 'down (event-modifiers (aref key 1)))))
696 (read-event))))
2c8ed538
RS
697 ;; Put yank-menu back as it was, if we changed it.
698 (when saved-yank-menu
699 (setq yank-menu (copy-sequence saved-yank-menu))
700 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
701 (if (numberp untranslated)
702 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
703 (let* ((event (aref key (if (and (symbolp (aref key 0))
704 (> (length key) 1)
705 (consp (aref key 1)))
706 1
707 0)))
91a2acb2 708 (modifiers (event-modifiers event))
24a27882
KS
709 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
710 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8
KS
711 (defn (key-binding key t))
712 defn-up defn-up-tricky ev-type
713 mouse-1-remapped mouse-1-tricky)
91a2acb2 714
05ca18a8 715 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 716 (when (and (eq defn nil)
91a2acb2
DK
717 (stringp (aref key (1- (length key))))
718 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
719 (setq defn 'menu-bar-select-yank))
720 (if (or (null defn) (integerp defn) (equal defn 'undefined))
721 (message "%s%s is undefined"
722 (help-key-description key untranslated) mouse-msg)
32226619
JB
723 (help-setup-xref (list #'describe-function defn)
724 (called-interactively-p 'interactive))
24a27882
KS
725 ;; Don't bother user with strings from (e.g.) the select-paste menu.
726 (when (stringp (aref key (1- (length key))))
727 (aset key (1- (length key)) "(any string)"))
728 (when (and untranslated
91a2acb2 729 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
730 (aset untranslated (1- (length untranslated))
731 "(any string)"))
732 ;; Need to do this before erasing *Help* buffer in case event
733 ;; is a mouse click in an existing *Help* buffer.
734 (when up-event
735 (setq ev-type (event-basic-type up-event))
736 (let ((sequence (vector up-event)))
737 (when (and (eq ev-type 'mouse-1)
738 mouse-1-click-follows-link
739 (not (eq mouse-1-click-follows-link 'double))
740 (setq mouse-1-remapped
741 (mouse-on-link-p (event-start up-event))))
742 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
743 (> mouse-1-click-follows-link 0)))
744 (cond ((stringp mouse-1-remapped)
745 (setq sequence mouse-1-remapped))
746 ((vectorp mouse-1-remapped)
747 (setcar up-event (elt mouse-1-remapped 0)))
748 (t (setcar up-event 'mouse-2))))
749 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
750 (when mouse-1-tricky
751 (setq sequence (vector up-event))
752 (aset sequence 0 'mouse-1)
753 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
cde56121 754 (with-help-window (help-buffer)
24a27882
KS
755 (princ (help-key-description key untranslated))
756 (princ (format "\
b96817c3 757%s runs the command %S, which is "
24a27882
KS
758 mouse-msg defn))
759 (describe-function-1 defn)
05ca18a8 760 (when up-event
24a27882
KS
761 (unless (or (null defn-up)
762 (integerp defn-up)
763 (equal defn-up 'undefined))
764 (princ (format "
765
766----------------- up-event %s----------------
767
e8579ebc 768%s%s%s runs the command %S, which is "
24a27882 769 (if mouse-1-tricky "(short click) " "")
e8579ebc
CY
770 (key-description (vector up-event))
771 mouse-msg
24a27882 772 (if mouse-1-remapped
b96817c3 773 " is remapped to <mouse-2>, which" "")
24a27882
KS
774 defn-up))
775 (describe-function-1 defn-up))
776 (unless (or (null defn-up-tricky)
777 (integerp defn-up-tricky)
778 (eq defn-up-tricky 'undefined))
779 (princ (format "
780
781----------------- up-event (long click) ----------------
782
783Pressing <%S>%s for longer than %d milli-seconds
b96817c3 784runs the command %S, which is "
24a27882
KS
785 ev-type mouse-msg
786 mouse-1-click-follows-link
787 defn-up-tricky))
cde56121 788 (describe-function-1 defn-up-tricky)))))))
400a1b1f 789\f
788d62cf
MB
790(defun describe-mode (&optional buffer)
791 "Display documentation of current major mode and minor modes.
efde809a
JPW
792A brief summary of the minor modes comes first, followed by the
793major mode description. This is followed by detailed
794descriptions of the minor modes, each on a separate page.
795
796For this to work correctly for a minor mode, the mode's indicator
797variable \(listed in `minor-mode-alist') must also be a function
27d1f87a
CY
798whose documentation describes the minor mode.
799
800If called from Lisp with a non-nil BUFFER argument, display
801documentation for the major and minor modes of that buffer."
f3b5dd74 802 (interactive "@")
dd39c336
SM
803 (unless buffer (setq buffer (current-buffer)))
804 (help-setup-xref (list #'describe-mode buffer)
32226619 805 (called-interactively-p 'interactive))
9639be74
RS
806 ;; For the sake of help-do-xref and help-xref-go-back,
807 ;; don't switch buffers before calling `help-buffer'.
cde56121 808 (with-help-window (help-buffer)
dd39c336 809 (with-current-buffer buffer
f6c57ef6 810 (let (minor-modes)
dd39c336
SM
811 ;; Older packages do not register in minor-mode-list but only in
812 ;; minor-mode-alist.
813 (dolist (x minor-mode-alist)
814 (setq x (car x))
815 (unless (memq x minor-mode-list)
816 (push x minor-mode-list)))
f6c57ef6
RS
817 ;; Find enabled minor mode we will want to mention.
818 (dolist (mode minor-mode-list)
819 ;; Document a minor mode if it is listed in minor-mode-alist,
820 ;; non-nil, and has a function definition.
af5f4483
SM
821 (let ((fmode (or (get mode :minor-mode-function) mode)))
822 (and (boundp mode) (symbol-value mode)
823 (fboundp fmode)
824 (let ((pretty-minor-mode
825 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
826 (symbol-name fmode))
827 (capitalize
828 (substring (symbol-name fmode)
829 0 (match-beginning 0)))
830 fmode)))
831 (push (list fmode pretty-minor-mode
832 (format-mode-line (assq mode minor-mode-alist)))
833 minor-modes)))))
f6c57ef6
RS
834 (setq minor-modes
835 (sort minor-modes
af5f4483 836 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 837 (when minor-modes
71723367 838 (princ "Enabled minor modes:\n")
e25e90b4
DP
839 (make-local-variable 'help-button-cache)
840 (with-current-buffer standard-output
841 (dolist (mode minor-modes)
af5f4483
SM
842 (let ((mode-function (nth 0 mode))
843 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
844 (indicator (nth 2 mode)))
845 (add-text-properties 0 (length pretty-minor-mode)
846 '(face bold) pretty-minor-mode)
847 (save-excursion
848 (goto-char (point-max))
849 (princ "\n\f\n")
850 (push (point-marker) help-button-cache)
851 ;; Document the minor modes fully.
852 (insert pretty-minor-mode)
71723367
RS
853 (princ (format " minor mode (%s):\n"
854 (if (zerop (length indicator))
855 "no indicator"
856 (format "indicator%s"
d8a869ea 857 indicator))))
e25e90b4 858 (princ (documentation mode-function)))
e25e90b4
DP
859 (insert-button pretty-minor-mode
860 'action (car help-button-cache)
72b64ad5 861 'follow-link t
e25e90b4 862 'help-echo "mouse-2, RET: show full information")
71723367
RS
863 (newline)))
864 (forward-line -1)
865 (fill-paragraph nil)
866 (forward-line 1))
867
868 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 869 ;; Document the major mode.
e25e90b4
DP
870 (let ((mode mode-name))
871 (with-current-buffer standard-output
4e6d3170 872 (let ((start (point)))
145fe412 873 (insert (format-mode-line mode nil nil buffer))
4e6d3170 874 (add-text-properties start (point) '(face bold)))))
9d05d1ba
JB
875 (princ " mode")
876 (let* ((mode major-mode)
877 (file-name (find-lisp-object-file-name mode nil)))
878 (when file-name
879 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
880 ;; Make a hyperlink to the library.
881 (with-current-buffer standard-output
882 (save-excursion
883 (re-search-backward "`\\([^`']+\\)'" nil t)
884 (help-xref-button 1 'help-function-def mode file-name)))))
885 (princ ":\n")
309d5b43 886 (princ (documentation major-mode)))))
6460e534 887 ;; For the sake of IELM and maybe others
309d5b43 888 nil)
400a1b1f 889
f6c57ef6 890
8e864068 891(defun describe-minor-mode (minor-mode)
335028c3
MY
892 "Display documentation of a minor mode given as MINOR-MODE.
893MINOR-MODE can be a minor mode symbol or a minor mode indicator string
894appeared on the mode-line."
7ada28ac 895 (interactive (list (completing-read
335028c3
MY
896 "Minor mode: "
897 (nconc
898 (describe-minor-mode-completion-table-for-symbol)
899 (describe-minor-mode-completion-table-for-indicator)
900 ))))
901 (if (symbolp minor-mode)
902 (setq minor-mode (symbol-name minor-mode)))
903 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
904 (indicators (describe-minor-mode-completion-table-for-indicator)))
905 (cond
906 ((member minor-mode symbols)
907 (describe-minor-mode-from-symbol (intern minor-mode)))
908 ((member minor-mode indicators)
909 (describe-minor-mode-from-indicator minor-mode))
910 (t
911 (error "No such minor mode: %s" minor-mode)))))
912
7ada28ac 913;; symbol
335028c3
MY
914(defun describe-minor-mode-completion-table-for-symbol ()
915 ;; In order to list up all minor modes, minor-mode-list
916 ;; is used here instead of minor-mode-alist.
917 (delq nil (mapcar 'symbol-name minor-mode-list)))
357f93d2 918
335028c3
MY
919(defun describe-minor-mode-from-symbol (symbol)
920 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 921 (interactive (list (intern (completing-read
335028c3
MY
922 "Minor mode symbol: "
923 (describe-minor-mode-completion-table-for-symbol)))))
924 (if (fboundp symbol)
925 (describe-function symbol)
926 (describe-variable symbol)))
927
928;; indicator
929(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 930 (delq nil
335028c3
MY
931 (mapcar (lambda (x)
932 (let ((i (format-mode-line x)))
933 ;; remove first space if existed
934 (cond
935 ((= 0 (length i))
936 nil)
94318c8a 937 ((eq (aref i 0) ?\s)
335028c3 938 (substring i 1))
7ada28ac 939 (t
335028c3
MY
940 i))))
941 minor-mode-alist)))
357f93d2 942
8e864068 943(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
944 "Display documentation of a minor mode specified by INDICATOR.
945If you call this function interactively, you can give indicator which
946is currently activated with completion."
7ada28ac
LT
947 (interactive (list
948 (completing-read
8e864068 949 "Minor mode indicator: "
335028c3 950 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
951 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
952 (if minor-mode
335028c3 953 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
954 (error "Cannot find minor mode for `%s'" indicator))))
955
956(defun lookup-minor-mode-from-indicator (indicator)
37269466 957 "Return a minor mode symbol from its indicator on the mode line."
335028c3 958 ;; remove first space if existed
7ada28ac 959 (if (and (< 0 (length indicator))
94318c8a 960 (eq (aref indicator 0) ?\s))
335028c3 961 (setq indicator (substring indicator 1)))
8e864068
JB
962 (let ((minor-modes minor-mode-alist)
963 result)
964 (while minor-modes
965 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 966 (anindicator (format-mode-line
335028c3
MY
967 (car (cdr (car minor-modes))))))
968 ;; remove first space if existed
7ada28ac 969 (if (and (stringp anindicator)
335028c3 970 (> (length anindicator) 0)
94318c8a 971 (eq (aref anindicator 0) ?\s))
335028c3
MY
972 (setq anindicator (substring anindicator 1)))
973 (if (equal indicator anindicator)
8e864068
JB
974 (setq result minor-mode
975 minor-modes nil)
976 (setq minor-modes (cdr minor-modes)))))
977 result))
48ce3c22
RS
978\f
979;;; Automatic resizing of temporary buffers.
ef654460
MR
980(defcustom temp-buffer-max-height
981 (lambda (buffer)
982 (if (eq (selected-window) (frame-root-window))
983 (/ (x-display-pixel-height) (frame-char-height) 2)
984 (/ (- (frame-height) 2) 2)))
4e6d3170 985 "Maximum height of a window displaying a temporary buffer.
90e357ae 986This is effective only when Temp Buffer Resize mode is enabled.
357f93d2
MR
987The value is the maximum height (in lines) which
988`resize-temp-buffer-window' will give to a window displaying a
989temporary buffer. It can also be a function to be called to
e4920bc9 990choose the height for such a buffer. It gets one argument, the
357f93d2
MR
991buffer, and should return a positive integer. At the time the
992function is called, the window to be resized is selected."
48ce3c22
RS
993 :type '(choice integer function)
994 :group 'help
6ba6a3e5 995 :version "24.3")
ef654460 996
880e6158
MR
997(defcustom temp-buffer-max-width
998 (lambda (buffer)
999 (if (eq (selected-window) (frame-root-window))
1000 (/ (x-display-pixel-width) (frame-char-width) 2)
1001 (/ (- (frame-width) 2) 2)))
1002 "Maximum width of a window displaying a temporary buffer.
1003This is effective only when Temp Buffer Resize mode is enabled.
1004The value is the maximum width (in columns) which
1005`resize-temp-buffer-window' will give to a window displaying a
1006temporary buffer. It can also be a function to be called to
1007choose the width for such a buffer. It gets one argument, the
1008buffer, and should return a positive integer. At the time the
1009function is called, the window to be resized is selected."
1010 :type '(choice integer function)
1011 :group 'help
1012 :version "24.4")
1013
4e1ede6c 1014(define-minor-mode temp-buffer-resize-mode
c5e28e39 1015 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
06e21633
CY
1016With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1017is positive, and disable it otherwise. If called from Lisp,
1018enable the mode if ARG is omitted or nil.
1019
1020When Temp Buffer Resize mode is enabled, the windows in which we
c5e28e39 1021show a temporary buffer are automatically resized in height to
06e21633
CY
1022fit the buffer's contents, but never more than
1023`temp-buffer-max-height' nor less than `window-min-height'.
357f93d2 1024
8e17c9ba
MR
1025A window is resized only if it has been specially created for the
1026buffer. Windows that have shown another buffer before are not
5938d519
MR
1027resized. A frame is resized only if `fit-frame-to-buffer' is
1028non-nil.
8e17c9ba 1029
357f93d2
MR
1030This mode is used by `help', `apropos' and `completion' buffers,
1031and some others."
b0fbf754 1032 :global t :group 'help
4e1ede6c 1033 (if temp-buffer-resize-mode
57f43907 1034 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
1035 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1036 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 1037 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22 1038
c5e28e39
MR
1039(defun resize-temp-buffer-window (&optional window)
1040 "Resize WINDOW to fit its contents.
880e6158
MR
1041WINDOW must be a live window and defaults to the selected one.
1042Do not resize if WINDOW was not created by `display-buffer'.
1043
1044If WINDOW is part of a vertical combination, restrain its new
1045size by `temp-buffer-max-height' and do not resize if its minimum
1046accessible position is scrolled out of view. If WINDOW is part
1047of a horizontal combination, restrain its new size by
1048`temp-buffer-max-width'. In both cases, the value of the option
1049`fit-window-to-buffer-horizontally' can inhibit resizing.
1050
1051If WINDOW is the root window of its frame, resize the frame
1052provided `fit-frame-to-buffer' is non-nil."
c5e28e39 1053 (setq window (window-normalize-window window t))
9d3aa82c
JB
1054 (let ((height (if (functionp temp-buffer-max-height)
1055 (with-selected-window window
1056 (funcall temp-buffer-max-height (window-buffer)))
1057 temp-buffer-max-height))
880e6158
MR
1058 (width (if (functionp temp-buffer-max-width)
1059 (with-selected-window window
1060 (funcall temp-buffer-max-width (window-buffer)))
1061 temp-buffer-max-width))
9d3aa82c 1062 (quit-cadr (cadr (window-parameter window 'quit-restore))))
880e6158
MR
1063 ;; Resize WINDOW iff it was made by `display-buffer'.
1064 (when (or (and (eq quit-cadr 'window)
1065 (or (and (window-combined-p window)
1066 (not (eq fit-window-to-buffer-horizontally
1067 'only))
1068 (pos-visible-in-window-p (point-min) window))
1069 (and (window-combined-p window t)
1070 fit-window-to-buffer-horizontally)))
1071 (and (eq quit-cadr 'frame)
1072 fit-frame-to-buffer
1073 (eq window (frame-root-window window))))
1074 (fit-window-to-buffer window height nil width))))
48ce3c22 1075
357f93d2 1076;;; Help windows.
cde56121
MR
1077(defcustom help-window-select 'other
1078 "Non-nil means select help window for viewing.
1079Choices are:
1080 never (nil) Select help window only if there is no other window
1081 on its frame.
1082 other Select help window unless the selected window is the
357f93d2 1083 only other window on the help window's frame.
cde56121
MR
1084 always (t) Always select the help window.
1085
1086This option has effect if and only if the help window was created
1087by `with-help-window'"
1088 :type '(choice (const :tag "never (nil)" nil)
1089 (const :tag "other" other)
1090 (const :tag "always (t)" t))
1091 :group 'help
1092 :version "23.1")
1093
c89926a5
CY
1094(defcustom help-enable-auto-load t
1095 "Whether Help commands can perform autoloading.
1096If non-nil, whenever \\[describe-function] is called for an
1097autoloaded function whose docstring contains any key substitution
1098construct (see `substitute-command-keys'), the library is loaded,
1099so that the documentation can show the right key bindings."
1100 :type 'boolean
1101 :group 'help
2a1e2476 1102 :version "24.3")
c89926a5 1103
357f93d2 1104(defun help-window-display-message (quit-part window &optional scroll)
cde56121
MR
1105 "Display message telling how to quit and scroll help window.
1106QUIT-PART is a string telling how to quit the help window WINDOW.
357f93d2
MR
1107Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1108SCROLL equal `other' means tell how to scroll the \"other\"
1109window."
cde56121
MR
1110 (let ((scroll-part
1111 (cond
357f93d2
MR
1112 ;; If we don't have QUIT-PART we probably reuse a window
1113 ;; showing the same buffer so we don't show any message.
1114 ((not quit-part) nil)
cde56121
MR
1115 ((pos-visible-in-window-p
1116 (with-current-buffer (window-buffer window)
357f93d2
MR
1117 (point-max)) window t)
1118 ;; Buffer end is at least partially visible, no need to talk
1119 ;; about scrolling.
cde56121 1120 ".")
357f93d2
MR
1121 ((eq scroll 'other)
1122 ", \\[scroll-other-window] to scroll help.")
1123 (scroll ", \\[scroll-up] to scroll help."))))
f6e7ec02 1124 (message "%s"
cde56121
MR
1125 (substitute-command-keys (concat quit-part scroll-part)))))
1126
880e6158
MR
1127(defun help-window-setup (window &optional value)
1128 "Set up help window WINDOW for `with-help-window'.
1129WINDOW is the window used for displaying the help buffer.
1130Return VALUE."
1131 (let* ((help-buffer (when (window-live-p window)
1132 (window-buffer window)))
1133 (help-setup (when (window-live-p window)
1134 (car (window-parameter window 'quit-restore)))))
357f93d2
MR
1135 (when help-buffer
1136 ;; Handle `help-window-point-marker'.
1137 (when (eq (marker-buffer help-window-point-marker) help-buffer)
880e6158 1138 (set-window-point window help-window-point-marker)
357f93d2
MR
1139 ;; Reset `help-window-point-marker'.
1140 (set-marker help-window-point-marker nil))
cde56121 1141
cde56121 1142 (cond
880e6158 1143 ((or (eq window (selected-window))
357f93d2 1144 (and (or (eq help-window-select t)
cf4eacfd 1145 (eq help-setup 'frame)
357f93d2 1146 (and (eq help-window-select 'other)
880e6158 1147 (eq (window-frame window) (selected-frame))
357f93d2 1148 (> (length (window-list nil 'no-mini)) 2)))
880e6158 1149 (select-window window)))
357f93d2
MR
1150 ;; The help window is or gets selected ...
1151 (help-window-display-message
1152 (cond
cf4eacfd 1153 ((eq help-setup 'window)
357f93d2 1154 ;; ... and is new, ...
f678e0b6 1155 "Type \"q\" to delete help window")
cf4eacfd 1156 ((eq help-setup 'frame)
880e6158 1157 "Type \"q\" to quit the help frame")
cf4eacfd 1158 ((eq help-setup 'other)
357f93d2
MR
1159 ;; ... or displayed some other buffer before.
1160 "Type \"q\" to restore previous buffer"))
880e6158
MR
1161 window t))
1162 ((and (eq (window-frame window) (selected-frame))
357f93d2
MR
1163 (= (length (window-list nil 'no-mini)) 2))
1164 ;; There are two windows on the help window's frame and the
1165 ;; other one is the selected one.
1166 (help-window-display-message
1167 (cond
cf4eacfd 1168 ((eq help-setup 'window)
357f93d2 1169 "Type \\[delete-other-windows] to delete the help window")
cf4eacfd 1170 ((eq help-setup 'other)
f678e0b6 1171 "Type \"q\" in help window to restore its previous buffer"))
880e6158 1172 window 'other))
cde56121 1173 (t
f678e0b6 1174 ;; The help window is not selected ...
357f93d2 1175 (help-window-display-message
f678e0b6 1176 (cond
cf4eacfd 1177 ((eq help-setup 'window)
f678e0b6
MR
1178 ;; ... and is new, ...
1179 "Type \"q\" in help window to delete it")
cf4eacfd 1180 ((eq help-setup 'other)
f678e0b6
MR
1181 ;; ... or displayed some other buffer before.
1182 "Type \"q\" in help window to restore previous buffer"))
880e6158
MR
1183 window))))
1184 ;; Return VALUE.
1185 value))
cde56121 1186
880e6158 1187;; `with-help-window' is a wrapper for `with-temp-buffer-window'
cde56121
MR
1188;; providing the following additional twists:
1189
880e6158 1190;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
b92631bf 1191;; adds cross references (via `help-mode-finish').
880e6158
MR
1192
1193;; (2) It issues a message telling how to scroll and quit the help
b92631bf 1194;; window (via `help-window-setup').
cde56121 1195
880e6158 1196;; (3) An option (customizable via `help-window-select') to select the
b92631bf 1197;; help window automatically.
cde56121 1198
880e6158 1199;; (4) A marker (`help-window-point-marker') to move point in the help
b92631bf 1200;; window to an arbitrary buffer position.
cde56121 1201(defmacro with-help-window (buffer-name &rest body)
b92631bf
MR
1202 "Display buffer named BUFFER-NAME in a help window.
1203Evaluate the forms in BODY with standard output bound to a buffer
1204called BUFFER-NAME (creating it if it does not exist), put that
1205buffer in `help-mode', display the buffer in a window (see
1206`with-temp-buffer-window' for details) and issue a message how to
1207deal with that \"help\" window when it's no more needed. Select
1208the help window if the current value of the user option
1209`help-window-select' says so. Return last value in BODY."
cde56121 1210 (declare (indent 1) (debug t))
357f93d2 1211 `(progn
357f93d2
MR
1212 ;; Make `help-window-point-marker' point nowhere. The only place
1213 ;; where this should be set to a buffer position is within BODY.
cde56121 1214 (set-marker help-window-point-marker nil)
880e6158
MR
1215 (let ((temp-buffer-window-setup-hook
1216 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1217 (temp-buffer-window-show-hook
1218 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1219 (with-temp-buffer-window
1220 ,buffer-name nil 'help-window-setup (progn ,@body)))))
cbb59342
CY
1221
1222;; Called from C, on encountering `help-char' when reading a char.
1223;; Don't print to *Help*; that would clobber Help history.
1224(defun help-form-show ()
1225 "Display the output of a non-nil `help-form'."
1226 (let ((msg (eval help-form)))
1227 (if (stringp msg)
1228 (with-output-to-temp-buffer " *Char Help*"
1229 (princ msg)))))
56759cf1
GM
1230
1231\f
1232;; The following functions used to be in help-fns.el, which is not preloaded.
1233;; But for various reasons, they are more widely needed, so they were
1234;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1235
1236(defun help-split-fundoc (docstring def)
1237 "Split a function DOCSTRING into the actual doc and the usage info.
1238Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1239is a string describing the argument list of DEF, such as
1240\"(apply FUNCTION &rest ARGUMENTS)\".
1241DEF is the function whose usage we're looking for in DOCSTRING."
1242 ;; Functions can get the calling sequence at the end of the doc string.
1243 ;; In cases where `function' has been fset to a subr we can't search for
1244 ;; function's name in the doc string so we use `fn' as the anonymous
1245 ;; function name instead.
1246 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1247 (cons (format "(%s%s"
1248 ;; Replace `fn' with the actual function name.
1249 (if (symbolp def) def "anonymous")
1250 (match-string 1 docstring))
1251 (unless (zerop (match-beginning 0))
1252 (substring docstring 0 (match-beginning 0))))))
1253
1254(defun help-add-fundoc-usage (docstring arglist)
1255 "Add the usage info to DOCSTRING.
1256If DOCSTRING already has a usage info, then just return it unchanged.
1257The usage info is built from ARGLIST. DOCSTRING can be nil.
1258ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1259 (unless (stringp docstring) (setq docstring ""))
1260 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1261 (eq arglist t))
1262 docstring
1263 (concat docstring
1264 (if (string-match "\n?\n\\'" docstring)
1265 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1266 "\n\n")
1267 (if (and (stringp arglist)
1268 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1269 (concat "(fn" (match-string 1 arglist) ")")
1270 (format "%S" (help-make-usage 'fn arglist))))))
1271
1272(defun help-function-arglist (def &optional preserve-names)
1273 "Return a formal argument list for the function DEF.
1274IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1275the same names as used in the original source code, when possible."
1276 ;; Handle symbols aliased to other symbols.
1277 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1278 ;; If definition is a macro, find the function inside it.
1279 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1280 (cond
1281 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1282 ((eq (car-safe def) 'lambda) (nth 1 def))
1283 ((eq (car-safe def) 'closure) (nth 2 def))
1284 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1285 (subrp def))
1286 (or (when preserve-names
1287 (let* ((doc (condition-case nil (documentation def) (error nil)))
1288 (docargs (if doc (car (help-split-fundoc doc nil))))
1289 (arglist (if docargs
1290 (cdar (read-from-string (downcase docargs)))))
1291 (valid t))
1292 ;; Check validity.
1293 (dolist (arg arglist)
1294 (unless (and (symbolp arg)
1295 (let ((name (symbol-name arg)))
1296 (if (eq (aref name 0) ?&)
1297 (memq arg '(&rest &optional))
1298 (not (string-match "\\." name)))))
1299 (setq valid nil)))
1300 (when valid arglist)))
1301 (let* ((args-desc (if (not (subrp def))
1302 (aref def 0)
1303 (let ((a (subr-arity def)))
1304 (logior (car a)
1305 (if (numberp (cdr a))
1306 (lsh (cdr a) 8)
1307 (lsh 1 7))))))
1308 (max (lsh args-desc -8))
1309 (min (logand args-desc 127))
1310 (rest (logand args-desc 128))
1311 (arglist ()))
1312 (dotimes (i min)
1313 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1314 (when (> max min)
1315 (push '&optional arglist)
1316 (dotimes (i (- max min))
1317 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1318 arglist)))
1319 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1320 (nreverse arglist))))
1321 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1322 "[Arg list not available until function definition is loaded.]")
1323 (t t)))
1324
1325(defun help-make-usage (function arglist)
1326 (cons (if (symbolp function) function 'anonymous)
1327 (mapcar (lambda (arg)
1328 (if (not (symbolp arg)) arg
1329 (let ((name (symbol-name arg)))
1330 (cond
1331 ((string-match "\\`&" name) arg)
1332 ((string-match "\\`_" name)
1333 (intern (upcase (substring name 1))))
1334 (t (intern (upcase name)))))))
1335 arglist)))
1336
cde56121 1337\f
172892e3
JB
1338(provide 'help)
1339
1a06eabd 1340;;; help.el ends here