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