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