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