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