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