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