* net/gnutls.el (gnutls-min-prime-bits): Default to 256.
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
44e97401 3;; Copyright (C) 1985-1986, 1993-1994, 1998-2012 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
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
42;; `with-output-to-temp-buffer'. `with-help-window' has this point
43;; nowhere before exiting. Currently used by `view-lossage' to assert
44;; that the last keystrokes are always visible.
7b01c8d7 45(defvar help-window-point-marker (make-marker)
357f93d2 46 "Marker to override default `window-point' in help windows.")
cde56121 47
604aa5f0
SM
48(defvar help-map
49 (let ((map (make-sparse-keymap)))
50 (define-key map (char-to-string help-char) 'help-for-help)
51 (define-key map [help] 'help-for-help)
52 (define-key map [f1] 'help-for-help)
53 (define-key map "." 'display-local-help)
54 (define-key map "?" 'help-for-help)
55
d4a18332 56 (define-key map "\C-a" 'about-emacs)
604aa5f0 57 (define-key map "\C-c" 'describe-copying)
7b01c8d7
KS
58 (define-key map "\C-d" 'view-emacs-debugging)
59 (define-key map "\C-e" 'view-external-packages)
604aa5f0
SM
60 (define-key map "\C-f" 'view-emacs-FAQ)
61 (define-key map "\C-m" 'view-order-manuals)
62 (define-key map "\C-n" 'view-emacs-news)
7b01c8d7
KS
63 (define-key map "\C-o" 'describe-distribution)
64 (define-key map "\C-p" 'view-emacs-problems)
65 (define-key map "\C-t" 'view-emacs-todo)
604aa5f0
SM
66 (define-key map "\C-w" 'describe-no-warranty)
67
68 ;; This does not fit the pattern, but it is natural given the C-\ command.
69 (define-key map "\C-\\" 'describe-input-method)
70
71 (define-key map "C" 'describe-coding-system)
72 (define-key map "F" 'Info-goto-emacs-command-node)
73 (define-key map "I" 'describe-input-method)
74 (define-key map "K" 'Info-goto-emacs-key-command-node)
75 (define-key map "L" 'describe-language-environment)
76 (define-key map "S" 'info-lookup-symbol)
77
78 (define-key map "a" 'apropos-command)
79 (define-key map "b" 'describe-bindings)
80 (define-key map "c" 'describe-key-briefly)
81 (define-key map "d" 'apropos-documentation)
82 (define-key map "e" 'view-echo-area-messages)
83 (define-key map "f" 'describe-function)
7b01c8d7 84 (define-key map "g" 'describe-gnu-project)
604aa5f0
SM
85 (define-key map "h" 'view-hello-file)
86
87 (define-key map "i" 'info)
88 (define-key map "4i" 'info-other-window)
89
90 (define-key map "k" 'describe-key)
91 (define-key map "l" 'view-lossage)
92 (define-key map "m" 'describe-mode)
93 (define-key map "n" 'view-emacs-news)
94 (define-key map "p" 'finder-by-keyword)
cb6c4991 95 (define-key map "P" 'describe-package)
604aa5f0
SM
96 (define-key map "r" 'info-emacs-manual)
97 (define-key map "s" 'describe-syntax)
98 (define-key map "t" 'help-with-tutorial)
99 (define-key map "w" 'where-is)
100 (define-key map "v" 'describe-variable)
101 (define-key map "q" 'help-quit)
102 map)
433ae6f6
RS
103 "Keymap for characters following the Help key.")
104
e17d2fd1 105(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
106(define-key global-map [help] 'help-command)
107(define-key global-map [f1] 'help-command)
433ae6f6
RS
108(fset 'help-command help-map)
109
e25e90b4
DP
110;; insert-button makes the action nil if it is not store somewhere
111(defvar help-button-cache nil)
112
0cf0d828 113\f
2fc9d9f4 114(defun help-quit ()
3120a677 115 "Just exit from the Help command's command loop."
2fc9d9f4
RS
116 (interactive)
117 nil)
118
01364a75
RS
119(defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
121This is a list
a8e7142c 122 (WINDOW . t) delete the selected window (and possibly its frame,
f3ada0ee 123 see `quit-window'), go to WINDOW.
01364a75
RS
124 (WINDOW . quit-window) do quit-window, then select WINDOW.
125 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
126
b3d8e4a0
SM
127(define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
128(defun help-print-return-message (&optional function)
433ae6f6 129 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
130This function assumes that `standard-output' is the help buffer.
131It computes a message, and applies the optional argument FUNCTION to it.
a8e7142c
EZ
132If FUNCTION is nil, it applies `message', thus displaying the message.
133In addition, this function sets up `help-return-method', which see, that
134specifies what to do when the user exits the help buffer."
433ae6f6 135 (and (not (get-buffer-window standard-output))
d536293f 136 (let ((first-message
a8e7142c
EZ
137 (cond ((or
138 pop-up-frames
139 (special-display-p (buffer-name standard-output)))
01364a75 140 (setq help-return-method (cons (selected-window) t))
d536293f
RS
141 ;; If the help output buffer is a special display buffer,
142 ;; don't say anything about how to get rid of it.
143 ;; First of all, the user will do that with the window
144 ;; manager, not with Emacs.
145 ;; Secondly, the buffer has not been displayed yet,
146 ;; so we don't know whether its frame will be selected.
d536293f 147 nil)
f3ad2fc8
GM
148 (display-buffer-reuse-frames
149 (setq help-return-method (cons (selected-window)
150 'quit-window))
151 nil)
d536293f 152 ((not (one-window-p t))
01364a75
RS
153 (setq help-return-method
154 (cons (selected-window) 'quit-window))
cb0b6766 155 "Type \\[display-buffer] RET to restore the other window.")
d536293f 156 (pop-up-windows
01364a75 157 (setq help-return-method (cons (selected-window) t))
d536293f
RS
158 "Type \\[delete-other-windows] to remove help window.")
159 (t
01364a75
RS
160 (setq help-return-method
161 (list (selected-window) (window-buffer)
162 (window-start) (window-point)))
d536293f
RS
163 "Type \\[switch-to-buffer] RET to remove help window."))))
164 (funcall (or function 'message)
165 (concat
166 (if first-message
376b2a24
DL
167 (substitute-command-keys first-message))
168 (if first-message " ")
125a8d70
RS
169 ;; If the help buffer will go in a separate frame,
170 ;; it's no use mentioning a command to scroll, so don't.
7b057a3d
EZ
171 (if (or pop-up-windows
172 (special-display-p (buffer-name standard-output)))
125a8d70 173 nil
a1c9f209 174 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
175 ;; Say how to scroll this window.
176 (substitute-command-keys
177 "\\[scroll-up] to scroll the help.")
178 ;; Say how to scroll some other window.
6e7f5182 179 (substitute-command-keys
125a8d70 180 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 181
433ae6f6
RS
182;; So keyboard macro definitions are documented correctly
183(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
184
a4bdcdf3
RS
185(defalias 'help 'help-for-help-internal)
186;; find-function can find this.
187(defalias 'help-for-help 'help-for-help-internal)
188;; It can't find this, but nobody will look.
189(make-help-screen help-for-help-internal
9cd39aff 190 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
ce9a0ccb
SM
191 ;; Don't purecopy this one, because it's not evaluated (it's
192 ;; directly used as a docstring in a function definition, so it'll
193 ;; be moved to the DOC file anyway: no need for purecopying it).
788d62cf
MB
194 "You have typed %THIS-KEY%, the help character. Type a Help option:
195\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
196
7b01c8d7
KS
197a PATTERN Show commands whose name matches the PATTERN (a list of words
198 or a regexp). See also the `apropos' command.
199b Display all key bindings.
200c KEYS Display the command name run by the given key sequence.
201C CODING Describe the given coding system, or RET for current ones.
202d PATTERN Show a list of functions, variables, and other items whose
203 documentation matches the PATTERN (a list of words or a regexp).
204e Go to the *Messages* buffer which logs echo-area messages.
205f FUNCTION Display documentation for the given function.
206F COMMAND Show the on-line manual's section that describes the command.
207g Display information about the GNU project.
208h Display the HELLO file which illustrates various scripts.
209i Start the Info documentation reader: read on-line manuals.
210I METHOD Describe a specific input method, or RET for current.
211k KEYS Display the full documentation for the key sequence.
212K KEYS Show the on-line manual's section for the command bound to KEYS.
90ad3541 213l Show last 300 input keystrokes (lossage).
7b01c8d7
KS
214L LANG-ENV Describes a specific language environment, or RET for current.
215m Display documentation of current minor modes and current major mode,
216 including their special commands.
217n Display news of recent Emacs changes.
218p TOPIC Find packages matching a given topic keyword.
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)
417 (switch-to-buffer (get-buffer-create "*Messages*")))
418
754084bb
GM
419(defun view-order-manuals ()
420 "Display the Emacs ORDERS file."
421 (interactive)
7b01c8d7 422 (view-help-file "ORDERS"))
754084bb 423
7ee71cf1
RS
424(defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
94ea540b 427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 428 (info "(efaq)"))
7ee71cf1 429
4cbff657
DL
430(defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
7b01c8d7
KS
433 (view-help-file "PROBLEMS"))
434
435(defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
439
440(defun view-external-packages ()
441 "Display external packages and information about Emacs."
442 (interactive)
443 (view-help-file "MORE.STUFF"))
4cbff657 444
433ae6f6 445(defun view-lossage ()
6b8d1c72 446 "Display last 300 input keystrokes.
50b57199
EZ
447
448To record all your input on a file, use `open-dribble-file'."
433ae6f6 449 (interactive)
32226619
JB
450 (help-setup-xref (list #'view-lossage)
451 (called-interactively-p 'interactive))
cde56121 452 (with-help-window (help-buffer)
02dfca16
SM
453 (princ (mapconcat (lambda (key)
454 (if (or (integerp key) (symbolp key) (listp key))
455 (single-key-description key)
456 (prin1-to-string key nil)))
298a7c8c
RS
457 (recent-keys)
458 " "))
b0fbf754 459 (with-current-buffer standard-output
433ae6f6
RS
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
462 (when (search-forward " " nil t)
463 (delete-char -1))
cde56121
MR
464 (insert "\n"))
465 ;; jidanni wants to see the last keystrokes immediately.
466 (set-marker help-window-point-marker (point)))))
433ae6f6 467
788d62cf
MB
468\f
469;; Key bindings
433ae6f6 470
4c45295b 471(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
472 "Show a list of all defined keys, and their definitions.
473We put that list in a buffer, and display the buffer.
474
475The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
476then we display only bindings that start with that prefix.
477The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
478to display (default, the current buffer). BUFFER can be a buffer
479or a buffer name."
3e5929af 480 (interactive)
4c45295b 481 (or buffer (setq buffer (current-buffer)))
32226619
JB
482 (help-setup-xref (list #'describe-bindings prefix buffer)
483 (called-interactively-p 'interactive))
4c45295b 484 (with-current-buffer buffer
3e5929af 485 (describe-bindings-internal nil prefix)))
a8ad43aa 486
94ea540b
SM
487;; This function used to be in keymap.c.
488(defun describe-bindings-internal (&optional menus prefix)
489 "Show a list of all defined keys, and their definitions.
490We put that list in a buffer, and display the buffer.
491
492The optional argument MENUS, if non-nil, says to mention menu bindings.
493\(Ordinarily these are omitted from the output.)
494The optional argument PREFIX, if non-nil, should be a key sequence;
495then we display only bindings that start with that prefix."
94ea540b 496 (let ((buf (current-buffer)))
cde56121 497 (with-help-window "*Help*"
94ea540b
SM
498 (with-current-buffer standard-output
499 (describe-buffer-bindings buf prefix menus)))))
500
e88a2c59 501(defun where-is (definition &optional insert)
b2c85790 502 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
503Argument is a command definition, usually a symbol with a function definition.
504If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
505 (interactive
506 (let ((fn (function-called-at-point))
788d62cf 507 (enable-recursive-minibuffers t)
54c0b967 508 val)
3829bcc5
SM
509 (setq val (completing-read
510 (if fn
511 (format "Where is command (default %s): " fn)
512 "Where is command: ")
513 obarray 'commandp t))
514 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
463d75ac 515 (unless definition (error "No command"))
7a698dc1 516 (let ((func (indirect-function definition))
3829bcc5 517 (defs nil)
e3ce671f 518 (standard-output (if insert (current-buffer) standard-output)))
740b479c 519 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
520 (mapatoms (lambda (symbol)
521 (and (fboundp symbol)
522 (not (eq symbol definition))
d92c2757
RS
523 (eq func (condition-case ()
524 (indirect-function symbol)
525 (error symbol)))
3829bcc5 526 (push symbol defs))))
740b479c
RS
527 ;; Look at all the symbols--first DEFINITION,
528 ;; then its aliases.
529 (dolist (symbol (cons definition defs))
530 (let* ((remapped (command-remapping symbol))
531 (keys (where-is-internal
532 symbol overriding-local-map nil nil remapped))
533 (keys (mapconcat 'key-description keys ", "))
534 string)
535 (setq string
536 (if insert
537 (if (> (length keys) 0)
538 (if remapped
539 (format "%s (%s) (remapped from %s)"
540 keys remapped symbol)
541 (format "%s (%s)" keys symbol))
542 (format "M-x %s RET" symbol))
543 (if (> (length keys) 0)
544 (if remapped
545 (format "%s is remapped to %s which is on %s"
a5f43550 546 symbol remapped keys)
740b479c
RS
547 (format "%s is on %s" symbol keys))
548 ;; If this is the command the user asked about,
549 ;; and it is not on any key, say so.
550 ;; For other symbols, its aliases, say nothing
551 ;; about them unless they are on keys.
552 (if (eq symbol definition)
553 (format "%s is not on any key" symbol)))))
554 (when string
555 (unless (eq symbol definition)
556 (princ ";\n its alias "))
557 (princ string)))))
54c0b967
RS
558 nil)
559
02dfca16
SM
560(defun help-key-description (key untranslated)
561 (let ((string (key-description key)))
ae1bb8ac
SM
562 (if (or (not untranslated)
563 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
564 string
565 (let ((otherstring (key-description untranslated)))
566 (if (equal string otherstring)
567 string
568 (format "%s (translated from %s)" string otherstring))))))
71296446 569
6527c983
EZ
570(defun describe-key-briefly (&optional key insert untranslated)
571 "Print the name of the function KEY invokes. KEY is a string.
572If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
573If non-nil, UNTRANSLATED is a vector of the untranslated events.
574It can also be a number in which case the untranslated events from
575the last key hit are used.
576
577If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 578temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
579 (interactive
580 (let ((enable-disabled-menus-and-buttons t)
581 (cursor-in-echo-area t)
582 saved-yank-menu)
583 (unwind-protect
584 (let (key)
585 ;; If yank-menu is empty, populate it temporarily, so that
586 ;; "Select and Paste" menu can generate a complete event.
587 (when (null (cdr yank-menu))
588 (setq saved-yank-menu (copy-sequence yank-menu))
589 (menu-bar-update-yank-menu "(any string)" nil))
590 (setq key (read-key-sequence "Describe key (or click or menu item): "))
46d91fa0 591 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
592 ;; corresponding up-event. Note that there are also
593 ;; down-events on scroll bars and mode lines: the actual
594 ;; event then is in the second element of the vector.
595 (and (vectorp key)
badf89ea
RS
596 (let ((last-idx (1- (length key))))
597 (and (eventp (aref key last-idx))
598 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 599 (read-event))
2c8ed538
RS
600 (list
601 key
774a814f
RS
602 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
603 1))
2c8ed538
RS
604 ;; Put yank-menu back as it was, if we changed it.
605 (when saved-yank-menu
606 (setq yank-menu (copy-sequence saved-yank-menu))
607 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
608 (if (numberp untranslated)
609 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
610 (let* ((event (if (and (symbolp (aref key 0))
611 (> (length key) 1)
612 (consp (aref key 1)))
613 (aref key 1)
614 (aref key 0)))
615 (modifiers (event-modifiers event))
b2cba41e 616 (standard-output (if insert (current-buffer) standard-output))
24a27882
KS
617 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
618 (memq 'drag modifiers)) " at that spot" ""))
619 (defn (key-binding key t))
620 key-desc)
621 ;; Handle the case where we faked an entry in "Select and Paste" menu.
622 (if (and (eq defn nil)
623 (stringp (aref key (1- (length key))))
624 (eq (key-binding (substring key 0 -1)) 'yank-menu))
625 (setq defn 'menu-bar-select-yank))
626 ;; Don't bother user with strings from (e.g.) the select-paste menu.
627 (if (stringp (aref key (1- (length key))))
628 (aset key (1- (length key)) "(any string)"))
629 (if (and (> (length untranslated) 0)
630 (stringp (aref untranslated (1- (length untranslated)))))
631 (aset untranslated (1- (length untranslated)) "(any string)"))
632 ;; Now describe the key, perhaps as changed.
633 (setq key-desc (help-key-description key untranslated))
634 (if (or (null defn) (integerp defn) (equal defn 'undefined))
635 (princ (format "%s%s is undefined" key-desc mouse-msg))
636 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 637
6527c983
EZ
638(defun describe-key (&optional key untranslated up-event)
639 "Display documentation of the function invoked by KEY.
640KEY can be any kind of a key sequence; it can include keyboard events,
641mouse events, and/or menu events. When calling from a program,
642pass KEY as a string or a vector.
643
644If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
645It can also be a number, in which case the untranslated events from
646the last key sequence entered are used.
647UP-EVENT is the up-event that was discarded by reading KEY, or nil.
648
649If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 650temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
651 (interactive
652 (let ((enable-disabled-menus-and-buttons t)
653 (cursor-in-echo-area t)
654 saved-yank-menu)
655 (unwind-protect
656 (let (key)
657 ;; If yank-menu is empty, populate it temporarily, so that
658 ;; "Select and Paste" menu can generate a complete event.
659 (when (null (cdr yank-menu))
660 (setq saved-yank-menu (copy-sequence yank-menu))
661 (menu-bar-update-yank-menu "(any string)" nil))
662 (setq key (read-key-sequence "Describe key (or click or menu item): "))
663 (list
664 key
665 (prefix-numeric-value current-prefix-arg)
c3f82997 666 ;; If KEY is a down-event, read and include the
badf89ea
RS
667 ;; corresponding up-event. Note that there are also
668 ;; down-events on scroll bars and mode lines: the actual
669 ;; event then is in the second element of the vector.
91a2acb2 670 (and (vectorp key)
badf89ea
RS
671 (let ((last-idx (1- (length key))))
672 (and (eventp (aref key last-idx))
673 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 674 (or (and (eventp (aref key 0))
98da283b
CY
675 (memq 'down (event-modifiers (aref key 0)))
676 ;; However, for the C-down-mouse-2 popup
677 ;; menu, there is no subsequent up-event. In
678 ;; this case, the up-event is the next
679 ;; element in the supplied vector.
680 (= (length key) 1))
91a2acb2
DK
681 (and (> (length key) 1)
682 (eventp (aref key 1))
683 (memq 'down (event-modifiers (aref key 1)))))
684 (read-event))))
2c8ed538
RS
685 ;; Put yank-menu back as it was, if we changed it.
686 (when saved-yank-menu
687 (setq yank-menu (copy-sequence saved-yank-menu))
688 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
689 (if (numberp untranslated)
690 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
691 (let* ((event (aref key (if (and (symbolp (aref key 0))
692 (> (length key) 1)
693 (consp (aref key 1)))
694 1
695 0)))
91a2acb2 696 (modifiers (event-modifiers event))
24a27882
KS
697 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
698 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8
KS
699 (defn (key-binding key t))
700 defn-up defn-up-tricky ev-type
701 mouse-1-remapped mouse-1-tricky)
91a2acb2 702
05ca18a8 703 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 704 (when (and (eq defn nil)
91a2acb2
DK
705 (stringp (aref key (1- (length key))))
706 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
707 (setq defn 'menu-bar-select-yank))
708 (if (or (null defn) (integerp defn) (equal defn 'undefined))
709 (message "%s%s is undefined"
710 (help-key-description key untranslated) mouse-msg)
32226619
JB
711 (help-setup-xref (list #'describe-function defn)
712 (called-interactively-p 'interactive))
24a27882
KS
713 ;; Don't bother user with strings from (e.g.) the select-paste menu.
714 (when (stringp (aref key (1- (length key))))
715 (aset key (1- (length key)) "(any string)"))
716 (when (and untranslated
91a2acb2 717 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
718 (aset untranslated (1- (length untranslated))
719 "(any string)"))
720 ;; Need to do this before erasing *Help* buffer in case event
721 ;; is a mouse click in an existing *Help* buffer.
722 (when up-event
723 (setq ev-type (event-basic-type up-event))
724 (let ((sequence (vector up-event)))
725 (when (and (eq ev-type 'mouse-1)
726 mouse-1-click-follows-link
727 (not (eq mouse-1-click-follows-link 'double))
728 (setq mouse-1-remapped
729 (mouse-on-link-p (event-start up-event))))
730 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
731 (> mouse-1-click-follows-link 0)))
732 (cond ((stringp mouse-1-remapped)
733 (setq sequence mouse-1-remapped))
734 ((vectorp mouse-1-remapped)
735 (setcar up-event (elt mouse-1-remapped 0)))
736 (t (setcar up-event 'mouse-2))))
737 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
738 (when mouse-1-tricky
739 (setq sequence (vector up-event))
740 (aset sequence 0 'mouse-1)
741 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
cde56121 742 (with-help-window (help-buffer)
24a27882
KS
743 (princ (help-key-description key untranslated))
744 (princ (format "\
b96817c3 745%s runs the command %S, which is "
24a27882
KS
746 mouse-msg defn))
747 (describe-function-1 defn)
05ca18a8 748 (when up-event
24a27882
KS
749 (unless (or (null defn-up)
750 (integerp defn-up)
751 (equal defn-up 'undefined))
752 (princ (format "
753
754----------------- up-event %s----------------
755
e8579ebc 756%s%s%s runs the command %S, which is "
24a27882 757 (if mouse-1-tricky "(short click) " "")
e8579ebc
CY
758 (key-description (vector up-event))
759 mouse-msg
24a27882 760 (if mouse-1-remapped
b96817c3 761 " is remapped to <mouse-2>, which" "")
24a27882
KS
762 defn-up))
763 (describe-function-1 defn-up))
764 (unless (or (null defn-up-tricky)
765 (integerp defn-up-tricky)
766 (eq defn-up-tricky 'undefined))
767 (princ (format "
768
769----------------- up-event (long click) ----------------
770
771Pressing <%S>%s for longer than %d milli-seconds
b96817c3 772runs the command %S, which is "
24a27882
KS
773 ev-type mouse-msg
774 mouse-1-click-follows-link
775 defn-up-tricky))
cde56121 776 (describe-function-1 defn-up-tricky)))))))
400a1b1f 777\f
788d62cf
MB
778(defun describe-mode (&optional buffer)
779 "Display documentation of current major mode and minor modes.
efde809a
JPW
780A brief summary of the minor modes comes first, followed by the
781major mode description. This is followed by detailed
782descriptions of the minor modes, each on a separate page.
783
784For this to work correctly for a minor mode, the mode's indicator
785variable \(listed in `minor-mode-alist') must also be a function
786whose documentation describes the minor mode."
f3b5dd74 787 (interactive "@")
dd39c336
SM
788 (unless buffer (setq buffer (current-buffer)))
789 (help-setup-xref (list #'describe-mode buffer)
32226619 790 (called-interactively-p 'interactive))
9639be74
RS
791 ;; For the sake of help-do-xref and help-xref-go-back,
792 ;; don't switch buffers before calling `help-buffer'.
cde56121 793 (with-help-window (help-buffer)
dd39c336 794 (with-current-buffer buffer
f6c57ef6 795 (let (minor-modes)
dd39c336
SM
796 ;; Older packages do not register in minor-mode-list but only in
797 ;; minor-mode-alist.
798 (dolist (x minor-mode-alist)
799 (setq x (car x))
800 (unless (memq x minor-mode-list)
801 (push x minor-mode-list)))
f6c57ef6
RS
802 ;; Find enabled minor mode we will want to mention.
803 (dolist (mode minor-mode-list)
804 ;; Document a minor mode if it is listed in minor-mode-alist,
805 ;; non-nil, and has a function definition.
af5f4483
SM
806 (let ((fmode (or (get mode :minor-mode-function) mode)))
807 (and (boundp mode) (symbol-value mode)
808 (fboundp fmode)
809 (let ((pretty-minor-mode
810 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
811 (symbol-name fmode))
812 (capitalize
813 (substring (symbol-name fmode)
814 0 (match-beginning 0)))
815 fmode)))
816 (push (list fmode pretty-minor-mode
817 (format-mode-line (assq mode minor-mode-alist)))
818 minor-modes)))))
f6c57ef6
RS
819 (setq minor-modes
820 (sort minor-modes
af5f4483 821 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 822 (when minor-modes
71723367 823 (princ "Enabled minor modes:\n")
e25e90b4
DP
824 (make-local-variable 'help-button-cache)
825 (with-current-buffer standard-output
826 (dolist (mode minor-modes)
af5f4483
SM
827 (let ((mode-function (nth 0 mode))
828 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
829 (indicator (nth 2 mode)))
830 (add-text-properties 0 (length pretty-minor-mode)
831 '(face bold) pretty-minor-mode)
832 (save-excursion
833 (goto-char (point-max))
834 (princ "\n\f\n")
835 (push (point-marker) help-button-cache)
836 ;; Document the minor modes fully.
837 (insert pretty-minor-mode)
71723367
RS
838 (princ (format " minor mode (%s):\n"
839 (if (zerop (length indicator))
840 "no indicator"
841 (format "indicator%s"
d8a869ea 842 indicator))))
e25e90b4 843 (princ (documentation mode-function)))
e25e90b4
DP
844 (insert-button pretty-minor-mode
845 'action (car help-button-cache)
72b64ad5 846 'follow-link t
e25e90b4 847 'help-echo "mouse-2, RET: show full information")
71723367
RS
848 (newline)))
849 (forward-line -1)
850 (fill-paragraph nil)
851 (forward-line 1))
852
853 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 854 ;; Document the major mode.
e25e90b4
DP
855 (let ((mode mode-name))
856 (with-current-buffer standard-output
4e6d3170 857 (let ((start (point)))
145fe412 858 (insert (format-mode-line mode nil nil buffer))
4e6d3170 859 (add-text-properties start (point) '(face bold)))))
9d05d1ba
JB
860 (princ " mode")
861 (let* ((mode major-mode)
862 (file-name (find-lisp-object-file-name mode nil)))
863 (when file-name
864 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
865 ;; Make a hyperlink to the library.
866 (with-current-buffer standard-output
867 (save-excursion
868 (re-search-backward "`\\([^`']+\\)'" nil t)
869 (help-xref-button 1 'help-function-def mode file-name)))))
870 (princ ":\n")
309d5b43 871 (princ (documentation major-mode)))))
6460e534 872 ;; For the sake of IELM and maybe others
309d5b43 873 nil)
400a1b1f 874
f6c57ef6 875
8e864068 876(defun describe-minor-mode (minor-mode)
335028c3
MY
877 "Display documentation of a minor mode given as MINOR-MODE.
878MINOR-MODE can be a minor mode symbol or a minor mode indicator string
879appeared on the mode-line."
7ada28ac 880 (interactive (list (completing-read
335028c3
MY
881 "Minor mode: "
882 (nconc
883 (describe-minor-mode-completion-table-for-symbol)
884 (describe-minor-mode-completion-table-for-indicator)
885 ))))
886 (if (symbolp minor-mode)
887 (setq minor-mode (symbol-name minor-mode)))
888 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
889 (indicators (describe-minor-mode-completion-table-for-indicator)))
890 (cond
891 ((member minor-mode symbols)
892 (describe-minor-mode-from-symbol (intern minor-mode)))
893 ((member minor-mode indicators)
894 (describe-minor-mode-from-indicator minor-mode))
895 (t
896 (error "No such minor mode: %s" minor-mode)))))
897
7ada28ac 898;; symbol
335028c3
MY
899(defun describe-minor-mode-completion-table-for-symbol ()
900 ;; In order to list up all minor modes, minor-mode-list
901 ;; is used here instead of minor-mode-alist.
902 (delq nil (mapcar 'symbol-name minor-mode-list)))
357f93d2 903
335028c3
MY
904(defun describe-minor-mode-from-symbol (symbol)
905 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 906 (interactive (list (intern (completing-read
335028c3
MY
907 "Minor mode symbol: "
908 (describe-minor-mode-completion-table-for-symbol)))))
909 (if (fboundp symbol)
910 (describe-function symbol)
911 (describe-variable symbol)))
912
913;; indicator
914(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 915 (delq nil
335028c3
MY
916 (mapcar (lambda (x)
917 (let ((i (format-mode-line x)))
918 ;; remove first space if existed
919 (cond
920 ((= 0 (length i))
921 nil)
94318c8a 922 ((eq (aref i 0) ?\s)
335028c3 923 (substring i 1))
7ada28ac 924 (t
335028c3
MY
925 i))))
926 minor-mode-alist)))
357f93d2 927
8e864068 928(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
929 "Display documentation of a minor mode specified by INDICATOR.
930If you call this function interactively, you can give indicator which
931is currently activated with completion."
7ada28ac
LT
932 (interactive (list
933 (completing-read
8e864068 934 "Minor mode indicator: "
335028c3 935 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
936 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
937 (if minor-mode
335028c3 938 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
939 (error "Cannot find minor mode for `%s'" indicator))))
940
941(defun lookup-minor-mode-from-indicator (indicator)
942 "Return a minor mode symbol from its indicator on the modeline."
335028c3 943 ;; remove first space if existed
7ada28ac 944 (if (and (< 0 (length indicator))
94318c8a 945 (eq (aref indicator 0) ?\s))
335028c3 946 (setq indicator (substring indicator 1)))
8e864068
JB
947 (let ((minor-modes minor-mode-alist)
948 result)
949 (while minor-modes
950 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 951 (anindicator (format-mode-line
335028c3
MY
952 (car (cdr (car minor-modes))))))
953 ;; remove first space if existed
7ada28ac 954 (if (and (stringp anindicator)
335028c3 955 (> (length anindicator) 0)
94318c8a 956 (eq (aref anindicator 0) ?\s))
335028c3
MY
957 (setq anindicator (substring anindicator 1)))
958 (if (equal indicator anindicator)
8e864068
JB
959 (setq result minor-mode
960 minor-modes nil)
961 (setq minor-modes (cdr minor-modes)))))
962 result))
48ce3c22
RS
963\f
964;;; Automatic resizing of temporary buffers.
4483ddc5 965(defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
4e6d3170 966 "Maximum height of a window displaying a temporary buffer.
90e357ae 967This is effective only when Temp Buffer Resize mode is enabled.
357f93d2
MR
968The value is the maximum height (in lines) which
969`resize-temp-buffer-window' will give to a window displaying a
970temporary buffer. It can also be a function to be called to
e4920bc9 971choose the height for such a buffer. It gets one argument, the
357f93d2
MR
972buffer, and should return a positive integer. At the time the
973function is called, the window to be resized is selected."
48ce3c22
RS
974 :type '(choice integer function)
975 :group 'help
976 :version "20.4")
977
4e1ede6c 978(define-minor-mode temp-buffer-resize-mode
06e21633
CY
979 "Toggle auto-shrinking temp buffer windows (Temp Buffer Resize mode).
980With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
981is positive, and disable it otherwise. If called from Lisp,
982enable the mode if ARG is omitted or nil.
983
984When Temp Buffer Resize mode is enabled, the windows in which we
985show a temporary buffer are automatically reduced in height to
986fit the buffer's contents, but never more than
987`temp-buffer-max-height' nor less than `window-min-height'.
357f93d2
MR
988
989This mode is used by `help', `apropos' and `completion' buffers,
990and some others."
b0fbf754 991 :global t :group 'help
4e1ede6c 992 (if temp-buffer-resize-mode
57f43907 993 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
994 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
995 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 996 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22
RS
997
998(defun resize-temp-buffer-window ()
2a5f11a2 999 "Resize the selected window to fit its contents.
357f93d2
MR
1000Will not make it higher than `temp-buffer-max-height' nor smaller
1001than `window-min-height'. Do nothing if the selected window is
1002not vertically combined or some of its contents are scrolled out
1003of view."
1004 (when (and (pos-visible-in-window-p (point-min))
3d8daefe 1005 (window-combined-p))
d9c30bdf 1006 (fit-window-to-buffer
357f93d2 1007 nil
d9c30bdf 1008 (if (functionp temp-buffer-max-height)
357f93d2 1009 (funcall temp-buffer-max-height (window-buffer))
d9c30bdf 1010 temp-buffer-max-height))))
48ce3c22 1011
357f93d2 1012;;; Help windows.
cde56121
MR
1013(defcustom help-window-select 'other
1014 "Non-nil means select help window for viewing.
1015Choices are:
1016 never (nil) Select help window only if there is no other window
1017 on its frame.
1018 other Select help window unless the selected window is the
357f93d2 1019 only other window on the help window's frame.
cde56121
MR
1020 always (t) Always select the help window.
1021
1022This option has effect if and only if the help window was created
1023by `with-help-window'"
1024 :type '(choice (const :tag "never (nil)" nil)
1025 (const :tag "other" other)
1026 (const :tag "always (t)" t))
1027 :group 'help
1028 :version "23.1")
1029
357f93d2 1030(defun help-window-display-message (quit-part window &optional scroll)
cde56121
MR
1031 "Display message telling how to quit and scroll help window.
1032QUIT-PART is a string telling how to quit the help window WINDOW.
357f93d2
MR
1033Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1034SCROLL equal `other' means tell how to scroll the \"other\"
1035window."
cde56121
MR
1036 (let ((scroll-part
1037 (cond
357f93d2
MR
1038 ;; If we don't have QUIT-PART we probably reuse a window
1039 ;; showing the same buffer so we don't show any message.
1040 ((not quit-part) nil)
cde56121
MR
1041 ((pos-visible-in-window-p
1042 (with-current-buffer (window-buffer window)
357f93d2
MR
1043 (point-max)) window t)
1044 ;; Buffer end is at least partially visible, no need to talk
1045 ;; about scrolling.
cde56121 1046 ".")
357f93d2
MR
1047 ((eq scroll 'other)
1048 ", \\[scroll-other-window] to scroll help.")
1049 (scroll ", \\[scroll-up] to scroll help."))))
f6e7ec02 1050 (message "%s"
cde56121
MR
1051 (substitute-command-keys (concat quit-part scroll-part)))))
1052
f678e0b6 1053(defun help-window-setup (help-window)
357f93d2 1054 "Set up help window for `with-help-window'.
f678e0b6
MR
1055HELP-WINDOW is the window used for displaying the help buffer."
1056 (let* ((help-buffer (when (window-live-p help-window)
357f93d2 1057 (window-buffer help-window)))
f678e0b6 1058 (help-setup (when (window-live-p help-window)
cf4eacfd 1059 (car (window-parameter help-window 'quit-restore)))))
357f93d2
MR
1060 (when help-buffer
1061 ;; Handle `help-window-point-marker'.
1062 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1063 (set-window-point help-window help-window-point-marker)
1064 ;; Reset `help-window-point-marker'.
1065 (set-marker help-window-point-marker nil))
cde56121 1066
cde56121 1067 (cond
357f93d2
MR
1068 ((or (eq help-window (selected-window))
1069 (and (or (eq help-window-select t)
cf4eacfd 1070 (eq help-setup 'frame)
357f93d2
MR
1071 (and (eq help-window-select 'other)
1072 (eq (window-frame help-window) (selected-frame))
1073 (> (length (window-list nil 'no-mini)) 2)))
1074 (select-window help-window)))
1075 ;; The help window is or gets selected ...
1076 (help-window-display-message
1077 (cond
cf4eacfd 1078 ((eq help-setup 'window)
357f93d2 1079 ;; ... and is new, ...
f678e0b6 1080 "Type \"q\" to delete help window")
cf4eacfd 1081 ((eq help-setup 'frame)
f678e0b6 1082 "Type \"q\" to delete help frame")
cf4eacfd 1083 ((eq help-setup 'other)
357f93d2
MR
1084 ;; ... or displayed some other buffer before.
1085 "Type \"q\" to restore previous buffer"))
1086 help-window t))
1087 ((and (eq (window-frame help-window) (selected-frame))
1088 (= (length (window-list nil 'no-mini)) 2))
1089 ;; There are two windows on the help window's frame and the
1090 ;; other one is the selected one.
1091 (help-window-display-message
1092 (cond
cf4eacfd 1093 ((eq help-setup 'window)
357f93d2 1094 "Type \\[delete-other-windows] to delete the help window")
cf4eacfd 1095 ((eq help-setup 'other)
f678e0b6 1096 "Type \"q\" in help window to restore its previous buffer"))
357f93d2 1097 help-window 'other))
cde56121 1098 (t
f678e0b6 1099 ;; The help window is not selected ...
357f93d2 1100 (help-window-display-message
f678e0b6 1101 (cond
cf4eacfd 1102 ((eq help-setup 'window)
f678e0b6
MR
1103 ;; ... and is new, ...
1104 "Type \"q\" in help window to delete it")
cf4eacfd 1105 ((eq help-setup 'other)
f678e0b6
MR
1106 ;; ... or displayed some other buffer before.
1107 "Type \"q\" in help window to restore previous buffer"))
1108 help-window))))))
cde56121
MR
1109
1110;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1111;; providing the following additional twists:
1112
1113;; (1) Issue more accurate messages telling how to scroll and quit the
1114;; help window.
1115
357f93d2 1116;; (2) An option (customizable via `help-window-select') to select the
cde56121
MR
1117;; help window automatically.
1118
357f93d2 1119;; (3) A marker (`help-window-point-marker') to move point in the help
cde56121
MR
1120;; window to an arbitrary buffer position.
1121
b3d8e4a0 1122;; Note: It's usually always wrong to use `help-print-return-message' in
cde56121
MR
1123;; the body of `with-help-window'.
1124(defmacro with-help-window (buffer-name &rest body)
357f93d2 1125 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
cde56121 1126Select help window if the actual value of the user option
4ee88440 1127`help-window-select' says so. Return last value in BODY."
cde56121 1128 (declare (indent 1) (debug t))
357f93d2 1129 `(progn
357f93d2
MR
1130 ;; Make `help-window-point-marker' point nowhere. The only place
1131 ;; where this should be set to a buffer position is within BODY.
cde56121 1132 (set-marker help-window-point-marker nil)
f678e0b6
MR
1133 (let* (help-window
1134 (temp-buffer-show-hook
1135 (cons (lambda () (setq help-window (selected-window)))
1136 temp-buffer-show-hook)))
1137 ;; Return value returned by `with-output-to-temp-buffer'.
1138 (prog1
1139 (with-output-to-temp-buffer ,buffer-name
1140 (progn ,@body))
1141 (help-window-setup help-window)))))
cbb59342
CY
1142
1143;; Called from C, on encountering `help-char' when reading a char.
1144;; Don't print to *Help*; that would clobber Help history.
1145(defun help-form-show ()
1146 "Display the output of a non-nil `help-form'."
1147 (let ((msg (eval help-form)))
1148 (if (stringp msg)
1149 (with-output-to-temp-buffer " *Char Help*"
1150 (princ msg)))))
cde56121 1151\f
172892e3
JB
1152(provide 'help)
1153
1a06eabd 1154;;; help.el ends here