HideIfDef mode bug fixes and enhancements. This is #2 of 3 patches based
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
00156f95 3;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
3a801d0c 4
34dc21db 5;; Maintainer: emacs-devel@gnu.org
fd7fa35a 6;; Keywords: help, internal
bd78fa1d 7;; Package: emacs
e5167999 8
433ae6f6
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
433ae6f6 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
433ae6f6
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
433ae6f6 23
d9ecc911
ER
24;;; Commentary:
25
44e97401 26;; This code implements GNU Emacs's on-line help system, the one invoked by
95ac0a6f 27;; `M-x help-for-help'.
d9ecc911 28
e5167999
ER
29;;; Code:
30
8aa3a187
RS
31;; Get the macro make-help-screen when this is compiled,
32;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 33(eval-when-compile (require 'help-macro))
788d62cf 34
cde56121
MR
35;; `help-window-point-marker' is a marker you can move to a valid
36;; position of the buffer shown in the help window in order to override
37;; the standard positioning mechanism (`point-min') chosen by
c5e28e39
MR
38;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
39;; `with-help-window' has this point nowhere before exiting. Currently
40;; used by `view-lossage' to assert that the last keystrokes are always
41;; visible.
7b01c8d7 42(defvar help-window-point-marker (make-marker)
357f93d2 43 "Marker to override default `window-point' in help windows.")
cde56121 44
604aa5f0
SM
45(defvar help-map
46 (let ((map (make-sparse-keymap)))
47 (define-key map (char-to-string help-char) 'help-for-help)
48 (define-key map [help] 'help-for-help)
49 (define-key map [f1] 'help-for-help)
50 (define-key map "." 'display-local-help)
51 (define-key map "?" 'help-for-help)
52
d4a18332 53 (define-key map "\C-a" 'about-emacs)
604aa5f0 54 (define-key map "\C-c" 'describe-copying)
7b01c8d7
KS
55 (define-key map "\C-d" 'view-emacs-debugging)
56 (define-key map "\C-e" 'view-external-packages)
604aa5f0
SM
57 (define-key map "\C-f" 'view-emacs-FAQ)
58 (define-key map "\C-m" 'view-order-manuals)
59 (define-key map "\C-n" 'view-emacs-news)
7b01c8d7
KS
60 (define-key map "\C-o" 'describe-distribution)
61 (define-key map "\C-p" 'view-emacs-problems)
62 (define-key map "\C-t" 'view-emacs-todo)
604aa5f0
SM
63 (define-key map "\C-w" 'describe-no-warranty)
64
65 ;; This does not fit the pattern, but it is natural given the C-\ command.
66 (define-key map "\C-\\" 'describe-input-method)
67
68 (define-key map "C" 'describe-coding-system)
69 (define-key map "F" 'Info-goto-emacs-command-node)
70 (define-key map "I" 'describe-input-method)
71 (define-key map "K" 'Info-goto-emacs-key-command-node)
72 (define-key map "L" 'describe-language-environment)
73 (define-key map "S" 'info-lookup-symbol)
74
75 (define-key map "a" 'apropos-command)
76 (define-key map "b" 'describe-bindings)
77 (define-key map "c" 'describe-key-briefly)
78 (define-key map "d" 'apropos-documentation)
79 (define-key map "e" 'view-echo-area-messages)
80 (define-key map "f" 'describe-function)
7b01c8d7 81 (define-key map "g" 'describe-gnu-project)
604aa5f0
SM
82 (define-key map "h" 'view-hello-file)
83
84 (define-key map "i" 'info)
85 (define-key map "4i" 'info-other-window)
86
87 (define-key map "k" 'describe-key)
88 (define-key map "l" 'view-lossage)
89 (define-key map "m" 'describe-mode)
90 (define-key map "n" 'view-emacs-news)
91 (define-key map "p" 'finder-by-keyword)
cb6c4991 92 (define-key map "P" 'describe-package)
604aa5f0
SM
93 (define-key map "r" 'info-emacs-manual)
94 (define-key map "s" 'describe-syntax)
95 (define-key map "t" 'help-with-tutorial)
96 (define-key map "w" 'where-is)
97 (define-key map "v" 'describe-variable)
98 (define-key map "q" 'help-quit)
99 map)
433ae6f6
RS
100 "Keymap for characters following the Help key.")
101
e17d2fd1 102(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
103(define-key global-map [help] 'help-command)
104(define-key global-map [f1] 'help-command)
433ae6f6
RS
105(fset 'help-command help-map)
106
e25e90b4
DP
107;; insert-button makes the action nil if it is not store somewhere
108(defvar help-button-cache nil)
109
0cf0d828 110\f
2fc9d9f4 111(defun help-quit ()
3120a677 112 "Just exit from the Help command's command loop."
2fc9d9f4
RS
113 (interactive)
114 nil)
115
01364a75
RS
116(defvar help-return-method nil
117 "What to do to \"exit\" the help buffer.
118This is a list
a8e7142c 119 (WINDOW . t) delete the selected window (and possibly its frame,
f3ada0ee 120 see `quit-window'), go to WINDOW.
01364a75
RS
121 (WINDOW . quit-window) do quit-window, then select WINDOW.
122 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
123
b3d8e4a0
SM
124(define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
125(defun help-print-return-message (&optional function)
433ae6f6 126 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
127This function assumes that `standard-output' is the help buffer.
128It computes a message, and applies the optional argument FUNCTION to it.
a8e7142c
EZ
129If FUNCTION is nil, it applies `message', thus displaying the message.
130In addition, this function sets up `help-return-method', which see, that
b92631bf
MR
131specifies what to do when the user exits the help buffer.
132
133Do not call this in the scope of `with-help-window'."
433ae6f6 134 (and (not (get-buffer-window standard-output))
d536293f 135 (let ((first-message
a8e7142c
EZ
136 (cond ((or
137 pop-up-frames
138 (special-display-p (buffer-name standard-output)))
01364a75 139 (setq help-return-method (cons (selected-window) t))
d536293f
RS
140 ;; If the help output buffer is a special display buffer,
141 ;; don't say anything about how to get rid of it.
142 ;; First of all, the user will do that with the window
143 ;; manager, not with Emacs.
144 ;; Secondly, the buffer has not been displayed yet,
145 ;; so we don't know whether its frame will be selected.
d536293f
RS
146 nil)
147 ((not (one-window-p t))
01364a75
RS
148 (setq help-return-method
149 (cons (selected-window) 'quit-window))
cb0b6766 150 "Type \\[display-buffer] RET to restore the other window.")
d536293f 151 (pop-up-windows
01364a75 152 (setq help-return-method (cons (selected-window) t))
d536293f
RS
153 "Type \\[delete-other-windows] to remove help window.")
154 (t
01364a75
RS
155 (setq help-return-method
156 (list (selected-window) (window-buffer)
157 (window-start) (window-point)))
d536293f
RS
158 "Type \\[switch-to-buffer] RET to remove help window."))))
159 (funcall (or function 'message)
160 (concat
161 (if first-message
376b2a24
DL
162 (substitute-command-keys first-message))
163 (if first-message " ")
125a8d70
RS
164 ;; If the help buffer will go in a separate frame,
165 ;; it's no use mentioning a command to scroll, so don't.
7b057a3d
EZ
166 (if (or pop-up-windows
167 (special-display-p (buffer-name standard-output)))
125a8d70 168 nil
a1c9f209 169 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
170 ;; Say how to scroll this window.
171 (substitute-command-keys
172 "\\[scroll-up] to scroll the help.")
173 ;; Say how to scroll some other window.
6e7f5182 174 (substitute-command-keys
125a8d70 175 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 176
433ae6f6
RS
177;; So keyboard macro definitions are documented correctly
178(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
179
a4bdcdf3
RS
180(defalias 'help 'help-for-help-internal)
181;; find-function can find this.
182(defalias 'help-for-help 'help-for-help-internal)
183;; It can't find this, but nobody will look.
184(make-help-screen help-for-help-internal
9cd39aff 185 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
ce9a0ccb
SM
186 ;; Don't purecopy this one, because it's not evaluated (it's
187 ;; directly used as a docstring in a function definition, so it'll
188 ;; be moved to the DOC file anyway: no need for purecopying it).
788d62cf
MB
189 "You have typed %THIS-KEY%, the help character. Type a Help option:
190\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
191
7b01c8d7
KS
192a PATTERN Show commands whose name matches the PATTERN (a list of words
193 or a regexp). See also the `apropos' command.
194b Display all key bindings.
195c KEYS Display the command name run by the given key sequence.
196C CODING Describe the given coding system, or RET for current ones.
197d PATTERN Show a list of functions, variables, and other items whose
198 documentation matches the PATTERN (a list of words or a regexp).
199e Go to the *Messages* buffer which logs echo-area messages.
200f FUNCTION Display documentation for the given function.
201F COMMAND Show the on-line manual's section that describes the command.
202g Display information about the GNU project.
203h Display the HELLO file which illustrates various scripts.
204i Start the Info documentation reader: read on-line manuals.
205I METHOD Describe a specific input method, or RET for current.
206k KEYS Display the full documentation for the key sequence.
207K KEYS Show the on-line manual's section for the command bound to KEYS.
90ad3541 208l Show last 300 input keystrokes (lossage).
7b01c8d7
KS
209L LANG-ENV Describes a specific language environment, or RET for current.
210m Display documentation of current minor modes and current major mode,
211 including their special commands.
212n Display news of recent Emacs changes.
213p TOPIC Find packages matching a given topic keyword.
0c633f13 214P PACKAGE Describe the given Emacs Lisp package.
7b01c8d7
KS
215r Display the Emacs manual in Info mode.
216s Display contents of current syntax table, plus explanations.
217S SYMBOL Show the section for the given symbol in the on-line manual
218 for the programming language used in this buffer.
219t Start the Emacs learn-by-doing tutorial.
220v VARIABLE Display the given variable's documentation and value.
221w COMMAND Display which keystrokes invoke the given command (where-is).
222. Display any available local help at point in the echo area.
223
224C-a Information about Emacs.
225C-c Emacs copying permission (GNU General Public License).
226C-d Instructions for debugging GNU Emacs.
227C-e External packages and information about Emacs.
228C-f Emacs FAQ.
229C-m How to order printed Emacs manuals.
230C-n News of recent Emacs changes.
231C-o Emacs ordering and distribution information.
232C-p Info about known Emacs problems.
233C-t Emacs TODO list.
ce9a0ccb 234C-w Information on absence of warranty for GNU Emacs."
788d62cf
MB
235 help-map)
236
237\f
238
239(defun function-called-at-point ()
240 "Return a function around point or else called by the list containing point.
241If that doesn't give a function, return nil."
2e6750c8
SM
242 (with-syntax-table emacs-lisp-mode-syntax-table
243 (or (condition-case ()
244 (save-excursion
245 (or (not (zerop (skip-syntax-backward "_w")))
246 (eq (char-syntax (following-char)) ?w)
247 (eq (char-syntax (following-char)) ?_)
248 (forward-sexp -1))
249 (skip-chars-forward "'")
250 (let ((obj (read (current-buffer))))
251 (and (symbolp obj) (fboundp obj) obj)))
252 (error nil))
253 (condition-case ()
254 (save-excursion
255 (save-restriction
256 (narrow-to-region (max (point-min)
257 (- (point) 1000)) (point-max))
258 ;; Move up to surrounding paren, then after the open.
259 (backward-up-list 1)
260 (forward-char 1)
261 ;; If there is space here, this is probably something
262 ;; other than a real Lisp function call, so ignore it.
263 (if (looking-at "[ \t]")
264 (error "Probably not a Lisp function call"))
265 (let ((obj (read (current-buffer))))
266 (and (symbolp obj) (fboundp obj) obj))))
267 (error nil))
268 (let* ((str (find-tag-default))
269 (sym (if str (intern-soft str))))
270 (if (and sym (fboundp sym))
271 sym
272 (save-match-data
273 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
274 (setq sym (intern-soft (match-string 1 str)))
275 (and (fboundp sym) sym))))))))
788d62cf
MB
276
277\f
278;;; `User' help functions
279
7b01c8d7
KS
280(defun view-help-file (file &optional dir)
281 (view-file (expand-file-name file (or dir data-directory)))
3ffb948c 282 (goto-address-mode 1)
7b01c8d7
KS
283 (goto-char (point-min)))
284
433ae6f6
RS
285(defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
287 (interactive)
7b01c8d7 288 (view-help-file "DISTRIB"))
433ae6f6
RS
289
290(defun describe-copying ()
291 "Display info on how you may redistribute copies of GNU Emacs."
292 (interactive)
7b01c8d7 293 (view-help-file "COPYING"))
433ae6f6 294
00156f95 295;; Maybe this command should just be removed.
7b01c8d7 296(defun describe-gnu-project ()
00156f95 297 "Browse online information on the GNU project."
76766f2d 298 (interactive)
00156f95 299 (browse-url "http://www.gnu.org/gnu/thegnuproject.html"))
76766f2d 300
4e44f5ce
KS
301(define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
302
433ae6f6
RS
303(defun describe-no-warranty ()
304 "Display info on all the kinds of warranty Emacs does NOT have."
305 (interactive)
306 (describe-copying)
307 (let (case-fold-search)
e7e2b26c
GM
308 (search-forward "Disclaimer of Warranty")
309 (forward-line 0)
433ae6f6
RS
310 (recenter 0)))
311
61c6b658 312(defun describe-prefix-bindings ()
c7cba9cb
RS
313 "Describe the bindings of the prefix used to reach this command.
314The prefix described consists of all but the last event
315of the key sequence that ran this command."
61c6b658 316 (interactive)
0f101663 317 (let ((key (this-command-keys)))
ccc06dcc
KH
318 (describe-bindings
319 (if (stringp key)
320 (substring key 0 (1- (length key)))
321 (let ((prefix (make-vector (1- (length key)) nil))
322 (i 0))
323 (while (< i (length prefix))
324 (aset prefix i (aref key i))
325 (setq i (1+ i)))
326 prefix)))))
788d62cf 327;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 328;; run describe-prefix-bindings.
61c6b658
RS
329(setq prefix-help-command 'describe-prefix-bindings)
330
e38cc268 331(defun view-emacs-news (&optional version)
382d018a 332 "Display info on recent changes to Emacs.
598ea453 333With argument, display info only for the selected version."
382d018a 334 (interactive "P")
e38cc268
KS
335 (unless version
336 (setq version emacs-major-version))
337 (when (consp version)
338 (let* ((all-versions
339 (let (res)
cc6650af 340 (mapc
e38cc268
KS
341 (lambda (file)
342 (with-temp-buffer
343 (insert-file-contents
344 (expand-file-name file data-directory))
345 (while (re-search-forward
346 (if (member file '("NEWS.18" "NEWS.1-17"))
347 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
348 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
349 (setq res (cons (match-string-no-properties 1) res)))))
350 (cons "NEWS"
351 (directory-files data-directory nil
352 "^NEWS\\.[0-9][-0-9]*$" nil)))
353 (sort (delete-dups res) (lambda (a b) (string< b a)))))
8c9fc4be 354 (current (car all-versions)))
e38cc268
KS
355 (setq version (completing-read
356 (format "Read NEWS for the version (default %s): " current)
357 all-versions nil nil nil nil current))
358 (if (integerp (string-to-number version))
359 (setq version (string-to-number version))
360 (unless (or (member version all-versions)
361 (<= (string-to-number version) (string-to-number current)))
362 (error "No news about version %s" version)))))
363 (when (integerp version)
364 (cond ((<= version 12)
365 (setq version (format "1.%d" version)))
366 ((<= version 18)
367 (setq version (format "%d" version)))
368 ((> version emacs-major-version)
e08b54f2 369 (error "No news about Emacs %d (yet)" version))))
e38cc268
KS
370 (let* ((vn (if (stringp version)
371 (string-to-number version)
372 version))
373 (file (cond
374 ((>= vn emacs-major-version) "NEWS")
375 ((< vn 18) "NEWS.1-17")
8c9fc4be
KS
376 (t (format "NEWS.%d" vn))))
377 res)
e38cc268
KS
378 (view-file (expand-file-name file data-directory))
379 (widen)
380 (goto-char (point-min))
381 (when (stringp version)
382 (when (re-search-forward
383 (concat (if (< vn 19)
384 "Changes in Emacs[ \t]*"
385 "^\* [^0-9\n]*") version "$")
386 nil t)
387 (beginning-of-line)
388 (narrow-to-region
389 (point)
390 (save-excursion
391 (while (and (setq res
392 (re-search-forward
393 (if (< vn 19)
394 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
395 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
396 (equal (match-string-no-properties 1) version)))
397 (or res (goto-char (point-max)))
398 (beginning-of-line)
399 (point)))))))
400
06b60517 401(defun view-emacs-todo (&optional _arg)
1ebc1f01
RS
402 "Display the Emacs TODO list."
403 (interactive "P")
7b01c8d7 404 (view-help-file "TODO"))
1ebc1f01 405
4e44f5ce
KS
406(define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
407
408
4f16ea85
RS
409(defun view-echo-area-messages ()
410 "View the log of recent echo-area messages: the `*Messages*' buffer.
411The number of messages retained in that buffer
412is specified by the variable `message-log-max'."
413 (interactive)
90582f05 414 (with-current-buffer (messages-buffer)
123ecb68
CY
415 (goto-char (point-max))
416 (display-buffer (current-buffer))))
4f16ea85 417
754084bb 418(defun view-order-manuals ()
458dbf5e 419 "Display information on how to buy printed copies of Emacs manuals."
754084bb 420 (interactive)
458dbf5e
GM
421;; (view-help-file "ORDERS")
422 (info "(emacs)Printed Books"))
754084bb 423
7ee71cf1
RS
424(defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
94ea540b 427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 428 (info "(efaq)"))
7ee71cf1 429
4cbff657
DL
430(defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
7b01c8d7
KS
433 (view-help-file "PROBLEMS"))
434
435(defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
439
e9be0a13 440;; This used to visit MORE.STUFF; maybe it should just be removed.
7b01c8d7 441(defun view-external-packages ()
e9be0a13 442 "Display info on where to get more Emacs packages."
7b01c8d7 443 (interactive)
e9be0a13 444 (info "(efaq)Packages that do not come with Emacs"))
4cbff657 445
433ae6f6 446(defun view-lossage ()
6b8d1c72 447 "Display last 300 input keystrokes.
50b57199 448
50ea0f87 449To record all your input, use `open-dribble-file'."
433ae6f6 450 (interactive)
32226619
JB
451 (help-setup-xref (list #'view-lossage)
452 (called-interactively-p 'interactive))
cde56121 453 (with-help-window (help-buffer)
02dfca16
SM
454 (princ (mapconcat (lambda (key)
455 (if (or (integerp key) (symbolp key) (listp key))
456 (single-key-description key)
457 (prin1-to-string key nil)))
298a7c8c
RS
458 (recent-keys)
459 " "))
b0fbf754 460 (with-current-buffer standard-output
433ae6f6
RS
461 (goto-char (point-min))
462 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
463 (when (search-forward " " nil t)
464 (delete-char -1))
cde56121
MR
465 (insert "\n"))
466 ;; jidanni wants to see the last keystrokes immediately.
467 (set-marker help-window-point-marker (point)))))
433ae6f6 468
788d62cf
MB
469\f
470;; Key bindings
433ae6f6 471
4c45295b 472(defun describe-bindings (&optional prefix buffer)
92c70367
LI
473 "Display a buffer showing a list of all defined keys, and their definitions.
474The keys are displayed in order of precedence.
a8ad43aa
RS
475
476The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
477then we display only bindings that start with that prefix.
478The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
479to display (default, the current buffer). BUFFER can be a buffer
480or a buffer name."
3e5929af 481 (interactive)
4c45295b 482 (or buffer (setq buffer (current-buffer)))
32226619
JB
483 (help-setup-xref (list #'describe-bindings prefix buffer)
484 (called-interactively-p 'interactive))
5b9925ae
SM
485 (with-help-window (help-buffer)
486 ;; Be aware that `describe-buffer-bindings' puts its output into
487 ;; the current buffer.
488 (with-current-buffer (help-buffer)
489 (describe-buffer-bindings buffer prefix))))
a8ad43aa 490
94ea540b
SM
491;; This function used to be in keymap.c.
492(defun describe-bindings-internal (&optional menus prefix)
493 "Show a list of all defined keys, and their definitions.
494We put that list in a buffer, and display the buffer.
495
496The optional argument MENUS, if non-nil, says to mention menu bindings.
497\(Ordinarily these are omitted from the output.)
498The optional argument PREFIX, if non-nil, should be a key sequence;
499then we display only bindings that start with that prefix."
5b9925ae 500 (declare (obsolete describe-buffer-bindings "24.4"))
94ea540b 501 (let ((buf (current-buffer)))
880e6158 502 (with-help-window (help-buffer)
b92631bf
MR
503 ;; Be aware that `describe-buffer-bindings' puts its output into
504 ;; the current buffer.
505 (with-current-buffer (help-buffer)
506 (describe-buffer-bindings buf prefix menus)))))
94ea540b 507
e88a2c59 508(defun where-is (definition &optional insert)
b2c85790 509 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
510Argument is a command definition, usually a symbol with a function definition.
511If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
512 (interactive
513 (let ((fn (function-called-at-point))
788d62cf 514 (enable-recursive-minibuffers t)
54c0b967 515 val)
3829bcc5
SM
516 (setq val (completing-read
517 (if fn
518 (format "Where is command (default %s): " fn)
519 "Where is command: ")
e016b8f1
CD
520 obarray 'commandp t nil nil
521 (and fn (symbol-name fn))))
522 (list (unless (equal val "") (intern val))
523 current-prefix-arg)))
463d75ac 524 (unless definition (error "No command"))
7a698dc1 525 (let ((func (indirect-function definition))
3829bcc5 526 (defs nil)
e3ce671f 527 (standard-output (if insert (current-buffer) standard-output)))
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): "))
74c582e6
CY
600 ;; Clear the echo area message (Bug#7014).
601 (message nil)
46d91fa0 602 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
603 ;; corresponding up-event. Note that there are also
604 ;; down-events on scroll bars and mode lines: the actual
605 ;; event then is in the second element of the vector.
606 (and (vectorp key)
badf89ea
RS
607 (let ((last-idx (1- (length key))))
608 (and (eventp (aref key last-idx))
609 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 610 (read-event))
2c8ed538
RS
611 (list
612 key
774a814f
RS
613 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
614 1))
2c8ed538
RS
615 ;; Put yank-menu back as it was, if we changed it.
616 (when saved-yank-menu
617 (setq yank-menu (copy-sequence saved-yank-menu))
618 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
619 (if (numberp untranslated)
620 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
621 (let* ((event (if (and (symbolp (aref key 0))
622 (> (length key) 1)
623 (consp (aref key 1)))
624 (aref key 1)
625 (aref key 0)))
626 (modifiers (event-modifiers event))
b2cba41e 627 (standard-output (if insert (current-buffer) standard-output))
24a27882
KS
628 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
629 (memq 'drag modifiers)) " at that spot" ""))
630 (defn (key-binding key t))
631 key-desc)
632 ;; Handle the case where we faked an entry in "Select and Paste" menu.
633 (if (and (eq defn nil)
634 (stringp (aref key (1- (length key))))
635 (eq (key-binding (substring key 0 -1)) 'yank-menu))
636 (setq defn 'menu-bar-select-yank))
637 ;; Don't bother user with strings from (e.g.) the select-paste menu.
638 (if (stringp (aref key (1- (length key))))
639 (aset key (1- (length key)) "(any string)"))
640 (if (and (> (length untranslated) 0)
641 (stringp (aref untranslated (1- (length untranslated)))))
642 (aset untranslated (1- (length untranslated)) "(any string)"))
643 ;; Now describe the key, perhaps as changed.
644 (setq key-desc (help-key-description key untranslated))
645 (if (or (null defn) (integerp defn) (equal defn 'undefined))
646 (princ (format "%s%s is undefined" key-desc mouse-msg))
647 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 648
958d20d2
NR
649(defun help--key-binding-keymap (key &optional accept-default no-remap position)
650 "Return a keymap holding a binding for KEY within current keymaps.
651The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
652POSITION is as documented in the function `key-binding'."
653 (let* ((active-maps (current-active-maps t position))
654 map found)
655 ;; We loop over active maps like key-binding does.
656 (while (and
657 (not found)
658 (setq map (pop active-maps)))
659 (setq found (lookup-key map key accept-default))
660 (when (integerp found)
661 ;; The first `found' characters of KEY were found but not the
662 ;; whole sequence.
663 (setq found nil)))
664 (when found
665 (if (and (symbolp found)
666 (not no-remap)
667 (command-remapping found))
668 ;; The user might want to know in which map the binding is
447a92e7 669 ;; found, or in which map the remapping is found. The
958d20d2 670 ;; default is to show the latter.
447a92e7 671 (help--key-binding-keymap (vector 'remap found))
958d20d2
NR
672 map))))
673
674(defun help--binding-locus (key position)
675 "Describe in which keymap KEY is defined.
676Return a symbol pointing to that keymap if one exists ; otherwise
677return nil."
447a92e7 678 (let ((map (help--key-binding-keymap key t nil position)))
958d20d2
NR
679 (when map
680 (catch 'found
681 (let ((advertised-syms (nconc
682 (list 'overriding-terminal-local-map
683 'overriding-local-map)
684 (delq nil
685 (mapcar
686 (lambda (mode-and-map)
687 (let ((mode (car mode-and-map)))
688 (when (symbol-value mode)
689 (intern-soft
690 (format "%s-map" mode)))))
691 minor-mode-map-alist))
692 (list 'global-map
693 (intern-soft (format "%s-map" major-mode)))))
694 found)
695 ;; Look into these advertised symbols first.
696 (dolist (sym advertised-syms)
697 (when (and
698 (boundp sym)
699 (eq map (symbol-value sym)))
700 (throw 'found sym)))
701 ;; Only look in other symbols otherwise.
702 (mapatoms
703 (lambda (x)
704 (when (and (boundp x)
705 ;; Avoid let-bound symbols.
706 (special-variable-p x)
707 (eq (symbol-value x) map))
708 (throw 'found x))))
709 nil)))))
710
6527c983
EZ
711(defun describe-key (&optional key untranslated up-event)
712 "Display documentation of the function invoked by KEY.
713KEY can be any kind of a key sequence; it can include keyboard events,
714mouse events, and/or menu events. When calling from a program,
715pass KEY as a string or a vector.
716
717If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
718It can also be a number, in which case the untranslated events from
719the last key sequence entered are used.
720UP-EVENT is the up-event that was discarded by reading KEY, or nil.
721
722If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 723temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
724 (interactive
725 (let ((enable-disabled-menus-and-buttons t)
726 (cursor-in-echo-area t)
727 saved-yank-menu)
728 (unwind-protect
729 (let (key)
730 ;; If yank-menu is empty, populate it temporarily, so that
731 ;; "Select and Paste" menu can generate a complete event.
732 (when (null (cdr yank-menu))
733 (setq saved-yank-menu (copy-sequence yank-menu))
734 (menu-bar-update-yank-menu "(any string)" nil))
735 (setq key (read-key-sequence "Describe key (or click or menu item): "))
736 (list
737 key
738 (prefix-numeric-value current-prefix-arg)
c3f82997 739 ;; If KEY is a down-event, read and include the
badf89ea
RS
740 ;; corresponding up-event. Note that there are also
741 ;; down-events on scroll bars and mode lines: the actual
742 ;; event then is in the second element of the vector.
91a2acb2 743 (and (vectorp key)
badf89ea
RS
744 (let ((last-idx (1- (length key))))
745 (and (eventp (aref key last-idx))
746 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 747 (or (and (eventp (aref key 0))
98da283b
CY
748 (memq 'down (event-modifiers (aref key 0)))
749 ;; However, for the C-down-mouse-2 popup
750 ;; menu, there is no subsequent up-event. In
751 ;; this case, the up-event is the next
752 ;; element in the supplied vector.
753 (= (length key) 1))
91a2acb2
DK
754 (and (> (length key) 1)
755 (eventp (aref key 1))
756 (memq 'down (event-modifiers (aref key 1)))))
757 (read-event))))
2c8ed538
RS
758 ;; Put yank-menu back as it was, if we changed it.
759 (when saved-yank-menu
760 (setq yank-menu (copy-sequence saved-yank-menu))
761 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
762 (if (numberp untranslated)
763 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
764 (let* ((event (aref key (if (and (symbolp (aref key 0))
765 (> (length key) 1)
766 (consp (aref key 1)))
767 1
768 0)))
91a2acb2 769 (modifiers (event-modifiers event))
24a27882
KS
770 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
771 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8 772 (defn (key-binding key t))
958d20d2 773 key-locus key-locus-up key-locus-up-tricky
05ca18a8
KS
774 defn-up defn-up-tricky ev-type
775 mouse-1-remapped mouse-1-tricky)
91a2acb2 776
05ca18a8 777 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 778 (when (and (eq defn nil)
91a2acb2
DK
779 (stringp (aref key (1- (length key))))
780 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
781 (setq defn 'menu-bar-select-yank))
782 (if (or (null defn) (integerp defn) (equal defn 'undefined))
783 (message "%s%s is undefined"
784 (help-key-description key untranslated) mouse-msg)
32226619
JB
785 (help-setup-xref (list #'describe-function defn)
786 (called-interactively-p 'interactive))
24a27882
KS
787 ;; Don't bother user with strings from (e.g.) the select-paste menu.
788 (when (stringp (aref key (1- (length key))))
789 (aset key (1- (length key)) "(any string)"))
790 (when (and untranslated
91a2acb2 791 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
792 (aset untranslated (1- (length untranslated))
793 "(any string)"))
794 ;; Need to do this before erasing *Help* buffer in case event
795 ;; is a mouse click in an existing *Help* buffer.
796 (when up-event
797 (setq ev-type (event-basic-type up-event))
798 (let ((sequence (vector up-event)))
799 (when (and (eq ev-type 'mouse-1)
800 mouse-1-click-follows-link
801 (not (eq mouse-1-click-follows-link 'double))
802 (setq mouse-1-remapped
803 (mouse-on-link-p (event-start up-event))))
804 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
805 (> mouse-1-click-follows-link 0)))
806 (cond ((stringp mouse-1-remapped)
807 (setq sequence mouse-1-remapped))
808 ((vectorp mouse-1-remapped)
809 (setcar up-event (elt mouse-1-remapped 0)))
810 (t (setcar up-event 'mouse-2))))
811 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
958d20d2 812 (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
24a27882
KS
813 (when mouse-1-tricky
814 (setq sequence (vector up-event))
815 (aset sequence 0 'mouse-1)
958d20d2
NR
816 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
817 (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
818 (setq key-locus (help--binding-locus key (event-start event)))
cde56121 819 (with-help-window (help-buffer)
24a27882 820 (princ (help-key-description key untranslated))
958d20d2
NR
821 (princ (format "%s runs the command %S%s, which is "
822 mouse-msg defn (if key-locus
823 (format " (found in %s)" key-locus)
824 "")))
24a27882 825 (describe-function-1 defn)
05ca18a8 826 (when up-event
24a27882
KS
827 (unless (or (null defn-up)
828 (integerp defn-up)
829 (equal defn-up 'undefined))
830 (princ (format "
831
832----------------- up-event %s----------------
833
958d20d2 834%s%s%s runs the command %S%s, which is "
24a27882 835 (if mouse-1-tricky "(short click) " "")
e8579ebc
CY
836 (key-description (vector up-event))
837 mouse-msg
24a27882 838 (if mouse-1-remapped
b96817c3 839 " is remapped to <mouse-2>, which" "")
958d20d2
NR
840 defn-up (if key-locus-up
841 (format " (found in %s)" key-locus-up)
842 "")))
24a27882
KS
843 (describe-function-1 defn-up))
844 (unless (or (null defn-up-tricky)
845 (integerp defn-up-tricky)
846 (eq defn-up-tricky 'undefined))
847 (princ (format "
848
849----------------- up-event (long click) ----------------
850
851Pressing <%S>%s for longer than %d milli-seconds
958d20d2 852runs the command %S%s, which is "
24a27882
KS
853 ev-type mouse-msg
854 mouse-1-click-follows-link
958d20d2
NR
855 defn-up-tricky (if key-locus-up-tricky
856 (format " (found in %s)" key-locus-up-tricky)
857 "")))
cde56121 858 (describe-function-1 defn-up-tricky)))))))
400a1b1f 859\f
788d62cf
MB
860(defun describe-mode (&optional buffer)
861 "Display documentation of current major mode and minor modes.
efde809a
JPW
862A brief summary of the minor modes comes first, followed by the
863major mode description. This is followed by detailed
864descriptions of the minor modes, each on a separate page.
865
866For this to work correctly for a minor mode, the mode's indicator
867variable \(listed in `minor-mode-alist') must also be a function
27d1f87a
CY
868whose documentation describes the minor mode.
869
870If called from Lisp with a non-nil BUFFER argument, display
871documentation for the major and minor modes of that buffer."
f3b5dd74 872 (interactive "@")
dd39c336
SM
873 (unless buffer (setq buffer (current-buffer)))
874 (help-setup-xref (list #'describe-mode buffer)
32226619 875 (called-interactively-p 'interactive))
9639be74
RS
876 ;; For the sake of help-do-xref and help-xref-go-back,
877 ;; don't switch buffers before calling `help-buffer'.
cde56121 878 (with-help-window (help-buffer)
dd39c336 879 (with-current-buffer buffer
f6c57ef6 880 (let (minor-modes)
dd39c336
SM
881 ;; Older packages do not register in minor-mode-list but only in
882 ;; minor-mode-alist.
883 (dolist (x minor-mode-alist)
884 (setq x (car x))
885 (unless (memq x minor-mode-list)
886 (push x minor-mode-list)))
f6c57ef6
RS
887 ;; Find enabled minor mode we will want to mention.
888 (dolist (mode minor-mode-list)
889 ;; Document a minor mode if it is listed in minor-mode-alist,
890 ;; non-nil, and has a function definition.
af5f4483
SM
891 (let ((fmode (or (get mode :minor-mode-function) mode)))
892 (and (boundp mode) (symbol-value mode)
893 (fboundp fmode)
894 (let ((pretty-minor-mode
895 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
896 (symbol-name fmode))
897 (capitalize
898 (substring (symbol-name fmode)
899 0 (match-beginning 0)))
900 fmode)))
901 (push (list fmode pretty-minor-mode
902 (format-mode-line (assq mode minor-mode-alist)))
903 minor-modes)))))
f6c57ef6
RS
904 (setq minor-modes
905 (sort minor-modes
af5f4483 906 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 907 (when minor-modes
71723367 908 (princ "Enabled minor modes:\n")
e25e90b4
DP
909 (make-local-variable 'help-button-cache)
910 (with-current-buffer standard-output
911 (dolist (mode minor-modes)
af5f4483
SM
912 (let ((mode-function (nth 0 mode))
913 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
914 (indicator (nth 2 mode)))
915 (add-text-properties 0 (length pretty-minor-mode)
916 '(face bold) pretty-minor-mode)
917 (save-excursion
918 (goto-char (point-max))
919 (princ "\n\f\n")
920 (push (point-marker) help-button-cache)
921 ;; Document the minor modes fully.
922 (insert pretty-minor-mode)
71723367
RS
923 (princ (format " minor mode (%s):\n"
924 (if (zerop (length indicator))
925 "no indicator"
926 (format "indicator%s"
d8a869ea 927 indicator))))
e25e90b4 928 (princ (documentation mode-function)))
e25e90b4
DP
929 (insert-button pretty-minor-mode
930 'action (car help-button-cache)
72b64ad5 931 'follow-link t
e25e90b4 932 'help-echo "mouse-2, RET: show full information")
71723367
RS
933 (newline)))
934 (forward-line -1)
935 (fill-paragraph nil)
936 (forward-line 1))
937
938 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 939 ;; Document the major mode.
e25e90b4
DP
940 (let ((mode mode-name))
941 (with-current-buffer standard-output
4e6d3170 942 (let ((start (point)))
145fe412 943 (insert (format-mode-line mode nil nil buffer))
4e6d3170 944 (add-text-properties start (point) '(face bold)))))
9d05d1ba
JB
945 (princ " mode")
946 (let* ((mode major-mode)
947 (file-name (find-lisp-object-file-name mode nil)))
948 (when file-name
949 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
950 ;; Make a hyperlink to the library.
951 (with-current-buffer standard-output
952 (save-excursion
953 (re-search-backward "`\\([^`']+\\)'" nil t)
954 (help-xref-button 1 'help-function-def mode file-name)))))
955 (princ ":\n")
309d5b43 956 (princ (documentation major-mode)))))
6460e534 957 ;; For the sake of IELM and maybe others
309d5b43 958 nil)
400a1b1f 959
f6c57ef6 960
8e864068 961(defun describe-minor-mode (minor-mode)
335028c3
MY
962 "Display documentation of a minor mode given as MINOR-MODE.
963MINOR-MODE can be a minor mode symbol or a minor mode indicator string
964appeared on the mode-line."
7ada28ac 965 (interactive (list (completing-read
335028c3
MY
966 "Minor mode: "
967 (nconc
968 (describe-minor-mode-completion-table-for-symbol)
969 (describe-minor-mode-completion-table-for-indicator)
970 ))))
971 (if (symbolp minor-mode)
972 (setq minor-mode (symbol-name minor-mode)))
973 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
974 (indicators (describe-minor-mode-completion-table-for-indicator)))
975 (cond
976 ((member minor-mode symbols)
977 (describe-minor-mode-from-symbol (intern minor-mode)))
978 ((member minor-mode indicators)
979 (describe-minor-mode-from-indicator minor-mode))
980 (t
981 (error "No such minor mode: %s" minor-mode)))))
982
7ada28ac 983;; symbol
335028c3
MY
984(defun describe-minor-mode-completion-table-for-symbol ()
985 ;; In order to list up all minor modes, minor-mode-list
986 ;; is used here instead of minor-mode-alist.
987 (delq nil (mapcar 'symbol-name minor-mode-list)))
357f93d2 988
335028c3
MY
989(defun describe-minor-mode-from-symbol (symbol)
990 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 991 (interactive (list (intern (completing-read
335028c3
MY
992 "Minor mode symbol: "
993 (describe-minor-mode-completion-table-for-symbol)))))
994 (if (fboundp symbol)
995 (describe-function symbol)
996 (describe-variable symbol)))
997
998;; indicator
999(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 1000 (delq nil
335028c3
MY
1001 (mapcar (lambda (x)
1002 (let ((i (format-mode-line x)))
1003 ;; remove first space if existed
1004 (cond
1005 ((= 0 (length i))
1006 nil)
94318c8a 1007 ((eq (aref i 0) ?\s)
335028c3 1008 (substring i 1))
7ada28ac 1009 (t
335028c3
MY
1010 i))))
1011 minor-mode-alist)))
357f93d2 1012
8e864068 1013(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
1014 "Display documentation of a minor mode specified by INDICATOR.
1015If you call this function interactively, you can give indicator which
1016is currently activated with completion."
7ada28ac
LT
1017 (interactive (list
1018 (completing-read
8e864068 1019 "Minor mode indicator: "
335028c3 1020 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
1021 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1022 (if minor-mode
335028c3 1023 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
1024 (error "Cannot find minor mode for `%s'" indicator))))
1025
1026(defun lookup-minor-mode-from-indicator (indicator)
37269466 1027 "Return a minor mode symbol from its indicator on the mode line."
335028c3 1028 ;; remove first space if existed
7ada28ac 1029 (if (and (< 0 (length indicator))
94318c8a 1030 (eq (aref indicator 0) ?\s))
335028c3 1031 (setq indicator (substring indicator 1)))
8e864068
JB
1032 (let ((minor-modes minor-mode-alist)
1033 result)
1034 (while minor-modes
1035 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 1036 (anindicator (format-mode-line
335028c3
MY
1037 (car (cdr (car minor-modes))))))
1038 ;; remove first space if existed
7ada28ac 1039 (if (and (stringp anindicator)
335028c3 1040 (> (length anindicator) 0)
94318c8a 1041 (eq (aref anindicator 0) ?\s))
335028c3
MY
1042 (setq anindicator (substring anindicator 1)))
1043 (if (equal indicator anindicator)
8e864068
JB
1044 (setq result minor-mode
1045 minor-modes nil)
1046 (setq minor-modes (cdr minor-modes)))))
1047 result))
48ce3c22
RS
1048\f
1049;;; Automatic resizing of temporary buffers.
ef654460
MR
1050(defcustom temp-buffer-max-height
1051 (lambda (buffer)
1052 (if (eq (selected-window) (frame-root-window))
1053 (/ (x-display-pixel-height) (frame-char-height) 2)
1054 (/ (- (frame-height) 2) 2)))
4e6d3170 1055 "Maximum height of a window displaying a temporary buffer.
90e357ae 1056This is effective only when Temp Buffer Resize mode is enabled.
357f93d2
MR
1057The value is the maximum height (in lines) which
1058`resize-temp-buffer-window' will give to a window displaying a
1059temporary buffer. It can also be a function to be called to
e4920bc9 1060choose the height for such a buffer. It gets one argument, the
357f93d2
MR
1061buffer, and should return a positive integer. At the time the
1062function is called, the window to be resized is selected."
48ce3c22
RS
1063 :type '(choice integer function)
1064 :group 'help
6ba6a3e5 1065 :version "24.3")
ef654460 1066
880e6158
MR
1067(defcustom temp-buffer-max-width
1068 (lambda (buffer)
1069 (if (eq (selected-window) (frame-root-window))
1070 (/ (x-display-pixel-width) (frame-char-width) 2)
1071 (/ (- (frame-width) 2) 2)))
1072 "Maximum width of a window displaying a temporary buffer.
1073This is effective only when Temp Buffer Resize mode is enabled.
1074The value is the maximum width (in columns) which
1075`resize-temp-buffer-window' will give to a window displaying a
1076temporary buffer. It can also be a function to be called to
1077choose the width for such a buffer. It gets one argument, the
1078buffer, and should return a positive integer. At the time the
1079function is called, the window to be resized is selected."
1080 :type '(choice integer function)
1081 :group 'help
1082 :version "24.4")
1083
4e1ede6c 1084(define-minor-mode temp-buffer-resize-mode
c5e28e39 1085 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
06e21633
CY
1086With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1087is positive, and disable it otherwise. If called from Lisp,
1088enable the mode if ARG is omitted or nil.
1089
1090When Temp Buffer Resize mode is enabled, the windows in which we
c5e28e39 1091show a temporary buffer are automatically resized in height to
06e21633
CY
1092fit the buffer's contents, but never more than
1093`temp-buffer-max-height' nor less than `window-min-height'.
357f93d2 1094
8e17c9ba
MR
1095A window is resized only if it has been specially created for the
1096buffer. Windows that have shown another buffer before are not
5938d519
MR
1097resized. A frame is resized only if `fit-frame-to-buffer' is
1098non-nil.
8e17c9ba 1099
357f93d2
MR
1100This mode is used by `help', `apropos' and `completion' buffers,
1101and some others."
b0fbf754 1102 :global t :group 'help
4e1ede6c 1103 (if temp-buffer-resize-mode
57f43907 1104 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
1105 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1106 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 1107 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22 1108
c5e28e39
MR
1109(defun resize-temp-buffer-window (&optional window)
1110 "Resize WINDOW to fit its contents.
880e6158
MR
1111WINDOW must be a live window and defaults to the selected one.
1112Do not resize if WINDOW was not created by `display-buffer'.
1113
1114If WINDOW is part of a vertical combination, restrain its new
1115size by `temp-buffer-max-height' and do not resize if its minimum
1116accessible position is scrolled out of view. If WINDOW is part
1117of a horizontal combination, restrain its new size by
1118`temp-buffer-max-width'. In both cases, the value of the option
1119`fit-window-to-buffer-horizontally' can inhibit resizing.
1120
1121If WINDOW is the root window of its frame, resize the frame
1122provided `fit-frame-to-buffer' is non-nil."
c5e28e39 1123 (setq window (window-normalize-window window t))
9d3aa82c
JB
1124 (let ((height (if (functionp temp-buffer-max-height)
1125 (with-selected-window window
1126 (funcall temp-buffer-max-height (window-buffer)))
1127 temp-buffer-max-height))
880e6158
MR
1128 (width (if (functionp temp-buffer-max-width)
1129 (with-selected-window window
1130 (funcall temp-buffer-max-width (window-buffer)))
1131 temp-buffer-max-width))
9d3aa82c 1132 (quit-cadr (cadr (window-parameter window 'quit-restore))))
880e6158
MR
1133 ;; Resize WINDOW iff it was made by `display-buffer'.
1134 (when (or (and (eq quit-cadr 'window)
1135 (or (and (window-combined-p window)
1136 (not (eq fit-window-to-buffer-horizontally
1137 'only))
1138 (pos-visible-in-window-p (point-min) window))
1139 (and (window-combined-p window t)
1140 fit-window-to-buffer-horizontally)))
1141 (and (eq quit-cadr 'frame)
1142 fit-frame-to-buffer
1143 (eq window (frame-root-window window))))
1144 (fit-window-to-buffer window height nil width))))
48ce3c22 1145
357f93d2 1146;;; Help windows.
cde56121
MR
1147(defcustom help-window-select 'other
1148 "Non-nil means select help window for viewing.
1149Choices are:
1150 never (nil) Select help window only if there is no other window
1151 on its frame.
1152 other Select help window unless the selected window is the
357f93d2 1153 only other window on the help window's frame.
cde56121
MR
1154 always (t) Always select the help window.
1155
1156This option has effect if and only if the help window was created
1157by `with-help-window'"
1158 :type '(choice (const :tag "never (nil)" nil)
1159 (const :tag "other" other)
1160 (const :tag "always (t)" t))
1161 :group 'help
1162 :version "23.1")
1163
c89926a5
CY
1164(defcustom help-enable-auto-load t
1165 "Whether Help commands can perform autoloading.
1166If non-nil, whenever \\[describe-function] is called for an
1167autoloaded function whose docstring contains any key substitution
1168construct (see `substitute-command-keys'), the library is loaded,
1169so that the documentation can show the right key bindings."
1170 :type 'boolean
1171 :group 'help
2a1e2476 1172 :version "24.3")
c89926a5 1173
357f93d2 1174(defun help-window-display-message (quit-part window &optional scroll)
cde56121
MR
1175 "Display message telling how to quit and scroll help window.
1176QUIT-PART is a string telling how to quit the help window WINDOW.
357f93d2
MR
1177Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1178SCROLL equal `other' means tell how to scroll the \"other\"
1179window."
cde56121
MR
1180 (let ((scroll-part
1181 (cond
357f93d2
MR
1182 ;; If we don't have QUIT-PART we probably reuse a window
1183 ;; showing the same buffer so we don't show any message.
1184 ((not quit-part) nil)
cde56121
MR
1185 ((pos-visible-in-window-p
1186 (with-current-buffer (window-buffer window)
357f93d2
MR
1187 (point-max)) window t)
1188 ;; Buffer end is at least partially visible, no need to talk
1189 ;; about scrolling.
cde56121 1190 ".")
357f93d2
MR
1191 ((eq scroll 'other)
1192 ", \\[scroll-other-window] to scroll help.")
1193 (scroll ", \\[scroll-up] to scroll help."))))
f6e7ec02 1194 (message "%s"
cde56121
MR
1195 (substitute-command-keys (concat quit-part scroll-part)))))
1196
880e6158
MR
1197(defun help-window-setup (window &optional value)
1198 "Set up help window WINDOW for `with-help-window'.
1199WINDOW is the window used for displaying the help buffer.
1200Return VALUE."
1201 (let* ((help-buffer (when (window-live-p window)
1202 (window-buffer window)))
1203 (help-setup (when (window-live-p window)
1204 (car (window-parameter window 'quit-restore)))))
357f93d2
MR
1205 (when help-buffer
1206 ;; Handle `help-window-point-marker'.
1207 (when (eq (marker-buffer help-window-point-marker) help-buffer)
880e6158 1208 (set-window-point window help-window-point-marker)
357f93d2
MR
1209 ;; Reset `help-window-point-marker'.
1210 (set-marker help-window-point-marker nil))
cde56121 1211
cde56121 1212 (cond
880e6158 1213 ((or (eq window (selected-window))
357f93d2 1214 (and (or (eq help-window-select t)
cf4eacfd 1215 (eq help-setup 'frame)
357f93d2 1216 (and (eq help-window-select 'other)
880e6158 1217 (eq (window-frame window) (selected-frame))
357f93d2 1218 (> (length (window-list nil 'no-mini)) 2)))
880e6158 1219 (select-window window)))
357f93d2
MR
1220 ;; The help window is or gets selected ...
1221 (help-window-display-message
1222 (cond
cf4eacfd 1223 ((eq help-setup 'window)
357f93d2 1224 ;; ... and is new, ...
f678e0b6 1225 "Type \"q\" to delete help window")
cf4eacfd 1226 ((eq help-setup 'frame)
880e6158 1227 "Type \"q\" to quit the help frame")
cf4eacfd 1228 ((eq help-setup 'other)
357f93d2
MR
1229 ;; ... or displayed some other buffer before.
1230 "Type \"q\" to restore previous buffer"))
880e6158
MR
1231 window t))
1232 ((and (eq (window-frame window) (selected-frame))
357f93d2
MR
1233 (= (length (window-list nil 'no-mini)) 2))
1234 ;; There are two windows on the help window's frame and the
1235 ;; other one is the selected one.
1236 (help-window-display-message
1237 (cond
cf4eacfd 1238 ((eq help-setup 'window)
357f93d2 1239 "Type \\[delete-other-windows] to delete the help window")
cf4eacfd 1240 ((eq help-setup 'other)
f678e0b6 1241 "Type \"q\" in help window to restore its previous buffer"))
880e6158 1242 window 'other))
cde56121 1243 (t
f678e0b6 1244 ;; The help window is not selected ...
357f93d2 1245 (help-window-display-message
f678e0b6 1246 (cond
cf4eacfd 1247 ((eq help-setup 'window)
f678e0b6
MR
1248 ;; ... and is new, ...
1249 "Type \"q\" in help window to delete it")
cf4eacfd 1250 ((eq help-setup 'other)
f678e0b6
MR
1251 ;; ... or displayed some other buffer before.
1252 "Type \"q\" in help window to restore previous buffer"))
880e6158
MR
1253 window))))
1254 ;; Return VALUE.
1255 value))
cde56121 1256
880e6158 1257;; `with-help-window' is a wrapper for `with-temp-buffer-window'
cde56121
MR
1258;; providing the following additional twists:
1259
880e6158 1260;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
b92631bf 1261;; adds cross references (via `help-mode-finish').
880e6158
MR
1262
1263;; (2) It issues a message telling how to scroll and quit the help
b92631bf 1264;; window (via `help-window-setup').
cde56121 1265
880e6158 1266;; (3) An option (customizable via `help-window-select') to select the
b92631bf 1267;; help window automatically.
cde56121 1268
880e6158 1269;; (4) A marker (`help-window-point-marker') to move point in the help
b92631bf 1270;; window to an arbitrary buffer position.
cde56121 1271(defmacro with-help-window (buffer-name &rest body)
b92631bf
MR
1272 "Display buffer named BUFFER-NAME in a help window.
1273Evaluate the forms in BODY with standard output bound to a buffer
1274called BUFFER-NAME (creating it if it does not exist), put that
1275buffer in `help-mode', display the buffer in a window (see
1276`with-temp-buffer-window' for details) and issue a message how to
1277deal with that \"help\" window when it's no more needed. Select
1278the help window if the current value of the user option
1279`help-window-select' says so. Return last value in BODY."
cde56121 1280 (declare (indent 1) (debug t))
357f93d2 1281 `(progn
357f93d2
MR
1282 ;; Make `help-window-point-marker' point nowhere. The only place
1283 ;; where this should be set to a buffer position is within BODY.
cde56121 1284 (set-marker help-window-point-marker nil)
880e6158
MR
1285 (let ((temp-buffer-window-setup-hook
1286 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1287 (temp-buffer-window-show-hook
1288 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1289 (with-temp-buffer-window
1290 ,buffer-name nil 'help-window-setup (progn ,@body)))))
cbb59342
CY
1291
1292;; Called from C, on encountering `help-char' when reading a char.
1293;; Don't print to *Help*; that would clobber Help history.
1294(defun help-form-show ()
1295 "Display the output of a non-nil `help-form'."
1296 (let ((msg (eval help-form)))
1297 (if (stringp msg)
1298 (with-output-to-temp-buffer " *Char Help*"
1299 (princ msg)))))
56759cf1
GM
1300
1301\f
1302;; The following functions used to be in help-fns.el, which is not preloaded.
1303;; But for various reasons, they are more widely needed, so they were
1304;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1305
1306(defun help-split-fundoc (docstring def)
1307 "Split a function DOCSTRING into the actual doc and the usage info.
1308Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1309is a string describing the argument list of DEF, such as
1310\"(apply FUNCTION &rest ARGUMENTS)\".
1311DEF is the function whose usage we're looking for in DOCSTRING."
1312 ;; Functions can get the calling sequence at the end of the doc string.
1313 ;; In cases where `function' has been fset to a subr we can't search for
1314 ;; function's name in the doc string so we use `fn' as the anonymous
1315 ;; function name instead.
1316 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1317 (cons (format "(%s%s"
1318 ;; Replace `fn' with the actual function name.
1319 (if (symbolp def) def "anonymous")
1320 (match-string 1 docstring))
1321 (unless (zerop (match-beginning 0))
1322 (substring docstring 0 (match-beginning 0))))))
1323
1324(defun help-add-fundoc-usage (docstring arglist)
1325 "Add the usage info to DOCSTRING.
1326If DOCSTRING already has a usage info, then just return it unchanged.
1327The usage info is built from ARGLIST. DOCSTRING can be nil.
1328ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1329 (unless (stringp docstring) (setq docstring ""))
1330 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1331 (eq arglist t))
1332 docstring
1333 (concat docstring
1334 (if (string-match "\n?\n\\'" docstring)
1335 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1336 "\n\n")
1337 (if (and (stringp arglist)
1338 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1339 (concat "(fn" (match-string 1 arglist) ")")
1340 (format "%S" (help-make-usage 'fn arglist))))))
1341
1342(defun help-function-arglist (def &optional preserve-names)
1343 "Return a formal argument list for the function DEF.
1344IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1345the same names as used in the original source code, when possible."
1346 ;; Handle symbols aliased to other symbols.
1347 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1348 ;; If definition is a macro, find the function inside it.
1349 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1350 (cond
1351 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1352 ((eq (car-safe def) 'lambda) (nth 1 def))
1353 ((eq (car-safe def) 'closure) (nth 2 def))
1354 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1355 (subrp def))
1356 (or (when preserve-names
1357 (let* ((doc (condition-case nil (documentation def) (error nil)))
1358 (docargs (if doc (car (help-split-fundoc doc nil))))
1359 (arglist (if docargs
1360 (cdar (read-from-string (downcase docargs)))))
1361 (valid t))
1362 ;; Check validity.
1363 (dolist (arg arglist)
1364 (unless (and (symbolp arg)
1365 (let ((name (symbol-name arg)))
1366 (if (eq (aref name 0) ?&)
1367 (memq arg '(&rest &optional))
1368 (not (string-match "\\." name)))))
1369 (setq valid nil)))
1370 (when valid arglist)))
1371 (let* ((args-desc (if (not (subrp def))
1372 (aref def 0)
1373 (let ((a (subr-arity def)))
1374 (logior (car a)
1375 (if (numberp (cdr a))
1376 (lsh (cdr a) 8)
1377 (lsh 1 7))))))
1378 (max (lsh args-desc -8))
1379 (min (logand args-desc 127))
1380 (rest (logand args-desc 128))
1381 (arglist ()))
1382 (dotimes (i min)
1383 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1384 (when (> max min)
1385 (push '&optional arglist)
1386 (dotimes (i (- max min))
1387 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1388 arglist)))
1389 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1390 (nreverse arglist))))
1391 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1392 "[Arg list not available until function definition is loaded.]")
1393 (t t)))
1394
1395(defun help-make-usage (function arglist)
1396 (cons (if (symbolp function) function 'anonymous)
1397 (mapcar (lambda (arg)
1398 (if (not (symbolp arg)) arg
1399 (let ((name (symbol-name arg)))
1400 (cond
1401 ((string-match "\\`&" name) arg)
1402 ((string-match "\\`_" name)
1403 (intern (upcase (substring name 1))))
1404 (t (intern (upcase name)))))))
1405 arglist)))
1406
cde56121 1407\f
172892e3
JB
1408(provide 'help)
1409
1a06eabd 1410;;; help.el ends here