Commit | Line | Data |
---|---|---|
1a06eabd ER |
1 | ;;; help.el --- help commands for Emacs |
2 | ||
f03b7c7d | 3 | ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002 |
754084bb | 4 | ;; Free Software Foundation, Inc. |
3a801d0c | 5 | |
e5167999 | 6 | ;; Maintainer: FSF |
fd7fa35a | 7 | ;; Keywords: help, internal |
e5167999 | 8 | |
433ae6f6 RS |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
433ae6f6 RS |
14 | ;; any later version. |
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 | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
433ae6f6 | 25 | |
d9ecc911 ER |
26 | ;;; Commentary: |
27 | ||
a1c9f209 | 28 | ;; This code implements GNU Emacs' on-line help system, the one invoked by |
95ac0a6f | 29 | ;; `M-x help-for-help'. |
d9ecc911 | 30 | |
e5167999 ER |
31 | ;;; Code: |
32 | ||
8aa3a187 RS |
33 | ;; Get the macro make-help-screen when this is compiled, |
34 | ;; or run interpreted, but not when the compiled code is loaded. | |
b1fe9304 | 35 | (eval-when-compile (require 'help-macro)) |
788d62cf MB |
36 | |
37 | ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'. | |
38 | (add-hook 'temp-buffer-setup-hook 'help-mode-setup) | |
39 | (add-hook 'temp-buffer-show-hook 'help-mode-finish) | |
41b8542b | 40 | |
433ae6f6 RS |
41 | (defvar help-map (make-sparse-keymap) |
42 | "Keymap for characters following the Help key.") | |
43 | ||
e17d2fd1 | 44 | (define-key global-map (char-to-string help-char) 'help-command) |
0af3df1c RS |
45 | (define-key global-map [help] 'help-command) |
46 | (define-key global-map [f1] 'help-command) | |
433ae6f6 RS |
47 | (fset 'help-command help-map) |
48 | ||
e17d2fd1 | 49 | (define-key help-map (char-to-string help-char) 'help-for-help) |
0af3df1c RS |
50 | (define-key help-map [help] 'help-for-help) |
51 | (define-key help-map [f1] 'help-for-help) | |
6b61353c | 52 | (define-key help-map "." 'display-local-help) |
433ae6f6 RS |
53 | (define-key help-map "?" 'help-for-help) |
54 | ||
55 | (define-key help-map "\C-c" 'describe-copying) | |
56 | (define-key help-map "\C-d" 'describe-distribution) | |
4f16ea85 RS |
57 | (define-key help-map "\C-e" 'view-emacs-problems) |
58 | (define-key help-map "\C-f" 'view-emacs-FAQ) | |
4f16ea85 RS |
59 | (define-key help-map "\C-m" 'view-order-manuals) |
60 | (define-key help-map "\C-n" 'view-emacs-news) | |
76766f2d | 61 | (define-key help-map "\C-p" 'describe-project) |
1ebc1f01 | 62 | (define-key help-map "\C-t" 'view-todo) |
4f16ea85 RS |
63 | (define-key help-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 help-map "\C-\\" 'describe-input-method) | |
67 | ||
68 | (define-key help-map "C" 'describe-coding-system) | |
69 | (define-key help-map "F" 'Info-goto-emacs-command-node) | |
70 | (define-key help-map "I" 'describe-input-method) | |
71 | (define-key help-map "K" 'Info-goto-emacs-key-command-node) | |
72 | (define-key help-map "L" 'describe-language-environment) | |
73 | (define-key help-map "S" 'info-lookup-symbol) | |
74 | ||
122955bf | 75 | (define-key help-map "a" 'apropos-command) |
433ae6f6 RS |
76 | |
77 | (define-key help-map "b" 'describe-bindings) | |
78 | ||
79 | (define-key help-map "c" 'describe-key-briefly) | |
433ae6f6 | 80 | |
4f16ea85 RS |
81 | (define-key help-map "e" 'view-echo-area-messages) |
82 | ||
433ae6f6 RS |
83 | (define-key help-map "f" 'describe-function) |
84 | ||
4f16ea85 | 85 | (define-key help-map "h" 'view-hello-file) |
7ee71cf1 | 86 | |
433ae6f6 | 87 | (define-key help-map "i" 'info) |
4b08b7ed | 88 | (define-key help-map "4i" 'info-other-window) |
4f16ea85 RS |
89 | |
90 | (define-key help-map "k" 'describe-key) | |
433ae6f6 RS |
91 | |
92 | (define-key help-map "l" 'view-lossage) | |
93 | ||
94 | (define-key help-map "m" 'describe-mode) | |
95 | ||
433ae6f6 RS |
96 | (define-key help-map "n" 'view-emacs-news) |
97 | ||
06b98c51 | 98 | (define-key help-map "p" 'finder-by-keyword) |
3e9c095d RS |
99 | (autoload 'finder-by-keyword "finder" |
100 | "Find packages matching a given keyword." t) | |
06b98c51 | 101 | |
d009b6e9 RS |
102 | (define-key help-map "r" 'info-emacs-manual) |
103 | ||
433ae6f6 RS |
104 | (define-key help-map "s" 'describe-syntax) |
105 | ||
106 | (define-key help-map "t" 'help-with-tutorial) | |
107 | ||
108 | (define-key help-map "w" 'where-is) | |
109 | ||
110 | (define-key help-map "v" 'describe-variable) | |
111 | ||
2fc9d9f4 RS |
112 | (define-key help-map "q" 'help-quit) |
113 | ||
0cf0d828 | 114 | \f |
2fc9d9f4 | 115 | (defun help-quit () |
3120a677 | 116 | "Just exit from the Help command's command loop." |
2fc9d9f4 RS |
117 | (interactive) |
118 | nil) | |
119 | ||
01364a75 RS |
120 | (defvar help-return-method nil |
121 | "What to do to \"exit\" the help buffer. | |
122 | This is a list | |
123 | (WINDOW . t) delete the selected window, go to WINDOW. | |
124 | (WINDOW . quit-window) do quit-window, then select WINDOW. | |
125 | (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.") | |
126 | ||
433ae6f6 RS |
127 | (defun print-help-return-message (&optional function) |
128 | "Display or return message saying how to restore windows after help command. | |
d009b6e9 RS |
129 | This function assumes that `standard-output' is the help buffer. |
130 | It computes a message, and applies the optional argument FUNCTION to it. | |
131 | If FUNCTION is nil, it applies `message', thus displaying the message." | |
433ae6f6 | 132 | (and (not (get-buffer-window standard-output)) |
d536293f | 133 | (let ((first-message |
a1c9f209 | 134 | (cond ((special-display-p (buffer-name standard-output)) |
01364a75 | 135 | (setq help-return-method (cons (selected-window) t)) |
d536293f RS |
136 | ;; If the help output buffer is a special display buffer, |
137 | ;; don't say anything about how to get rid of it. | |
138 | ;; First of all, the user will do that with the window | |
139 | ;; manager, not with Emacs. | |
140 | ;; Secondly, the buffer has not been displayed yet, | |
141 | ;; so we don't know whether its frame will be selected. | |
d536293f | 142 | nil) |
f3ad2fc8 GM |
143 | (display-buffer-reuse-frames |
144 | (setq help-return-method (cons (selected-window) | |
145 | 'quit-window)) | |
146 | nil) | |
d536293f | 147 | ((not (one-window-p t)) |
01364a75 RS |
148 | (setq help-return-method |
149 | (cons (selected-window) 'quit-window)) | |
d536293f RS |
150 | "Type \\[switch-to-buffer-other-window] RET to restore the other window.") |
151 | (pop-up-windows | |
01364a75 | 152 | (setq help-return-method (cons (selected-window) t)) |
d536293f RS |
153 | "Type \\[delete-other-windows] to remove help window.") |
154 | (t | |
01364a75 RS |
155 | (setq help-return-method |
156 | (list (selected-window) (window-buffer) | |
157 | (window-start) (window-point))) | |
d536293f RS |
158 | "Type \\[switch-to-buffer] RET to remove help window.")))) |
159 | (funcall (or function 'message) | |
160 | (concat | |
161 | (if first-message | |
376b2a24 DL |
162 | (substitute-command-keys first-message)) |
163 | (if first-message " ") | |
125a8d70 RS |
164 | ;; If the help buffer will go in a separate frame, |
165 | ;; it's no use mentioning a command to scroll, so don't. | |
a1c9f209 | 166 | (if (special-display-p (buffer-name standard-output)) |
125a8d70 | 167 | nil |
a1c9f209 | 168 | (if (same-window-p (buffer-name standard-output)) |
125a8d70 RS |
169 | ;; Say how to scroll this window. |
170 | (substitute-command-keys | |
171 | "\\[scroll-up] to scroll the help.") | |
172 | ;; Say how to scroll some other window. | |
6e7f5182 | 173 | (substitute-command-keys |
125a8d70 | 174 | "\\[scroll-other-window] to scroll the help.")))))))) |
433ae6f6 | 175 | |
433ae6f6 RS |
176 | ;; So keyboard macro definitions are documented correctly |
177 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
178 | ||
788d62cf MB |
179 | (defalias 'help 'help-for-help) |
180 | (make-help-screen help-for-help | |
6b61353c | 181 | "a b c C e f F i I k C-k l L m p s t v w C-c C-d C-f C-n C-p C-t C-w . or ? :" |
788d62cf MB |
182 | "You have typed %THIS-KEY%, the help character. Type a Help option: |
183 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) | |
184 | ||
185 | a command-apropos. Give a substring, and see a list of commands | |
186 | (functions interactively callable) that contain | |
187 | that substring. See also the apropos command. | |
188 | b describe-bindings. Display table of all key bindings. | |
189 | c describe-key-briefly. Type a command key sequence; | |
190 | it prints the function name that sequence runs. | |
191 | C describe-coding-system. This describes either a specific coding system | |
192 | (if you type its name) or the coding systems currently in use | |
193 | (if you type just RET). | |
4f16ea85 | 194 | e view-echo-area-messages. Show the `*Messages*' buffer. |
788d62cf | 195 | f describe-function. Type a function name and get documentation of it. |
4f16ea85 | 196 | F Info-goto-emacs-command-node. Type a function name; |
788d62cf | 197 | it takes you to the Info node for that command. |
54391990 | 198 | h Display the HELLO file which illustrates various scripts. |
788d62cf MB |
199 | i info. The info documentation reader. |
200 | I describe-input-method. Describe a specific input method (if you type | |
201 | its name) or the current input method (if you type just RET). | |
788d62cf MB |
202 | k describe-key. Type a command key sequence; |
203 | it displays the full documentation. | |
4f16ea85 | 204 | K Info-goto-emacs-key-command-node. Type a command key sequence; |
788d62cf MB |
205 | it takes you to the Info node for the command bound to that key. |
206 | l view-lossage. Show last 100 characters you typed. | |
207 | L describe-language-environment. This describes either a | |
208 | specific language environment (if you type its name) | |
209 | or the current language environment (if you type just RET). | |
210 | m describe-mode. Print documentation of current minor modes, | |
211 | and the current major mode, including their special commands. | |
788d62cf MB |
212 | p finder-by-keyword. Find packages matching a given topic keyword. |
213 | s describe-syntax. Display contents of syntax table, plus explanations. | |
4f16ea85 RS |
214 | S info-lookup-symbol. Display the definition of a specific symbol |
215 | as found in the manual for the language this buffer is written in. | |
788d62cf MB |
216 | t help-with-tutorial. Select the Emacs learn-by-doing tutorial. |
217 | v describe-variable. Type name of a variable; | |
218 | it displays the variable's documentation and value. | |
219 | w where-is. Type command name; it prints which keystrokes | |
220 | invoke that command. | |
6b61353c KH |
221 | . display-local-help. Display any available local help at point |
222 | in the echo area. | |
788d62cf | 223 | |
4f16ea85 | 224 | C-c Display Emacs copying permission (GNU General Public License). |
788d62cf | 225 | C-d Display Emacs ordering information. |
4f16ea85 RS |
226 | C-e Display info about Emacs problems. |
227 | C-f Display the Emacs FAQ. | |
4f16ea85 | 228 | C-m Display how to order printed Emacs manuals. |
788d62cf MB |
229 | C-n Display news of recent Emacs changes. |
230 | C-p Display information about the GNU project. | |
1ebc1f01 | 231 | C-t Display the Emacs TODO list. |
788d62cf MB |
232 | C-w Display information on absence of warranty for GNU Emacs." |
233 | help-map) | |
234 | ||
235 | \f | |
236 | ||
237 | (defun function-called-at-point () | |
238 | "Return a function around point or else called by the list containing point. | |
239 | If that doesn't give a function, return nil." | |
240 | (with-syntax-table emacs-lisp-mode-syntax-table | |
241 | (or (condition-case () | |
242 | (save-excursion | |
243 | (or (not (zerop (skip-syntax-backward "_w"))) | |
244 | (eq (char-syntax (following-char)) ?w) | |
245 | (eq (char-syntax (following-char)) ?_) | |
246 | (forward-sexp -1)) | |
247 | (skip-chars-forward "'") | |
248 | (let ((obj (read (current-buffer)))) | |
249 | (and (symbolp obj) (fboundp obj) obj))) | |
250 | (error nil)) | |
251 | (condition-case () | |
252 | (save-excursion | |
253 | (save-restriction | |
254 | (narrow-to-region (max (point-min) | |
255 | (- (point) 1000)) (point-max)) | |
256 | ;; Move up to surrounding paren, then after the open. | |
257 | (backward-up-list 1) | |
258 | (forward-char 1) | |
259 | ;; If there is space here, this is probably something | |
260 | ;; other than a real Lisp function call, so ignore it. | |
261 | (if (looking-at "[ \t]") | |
262 | (error "Probably not a Lisp function call")) | |
263 | (let ((obj (read (current-buffer)))) | |
264 | (and (symbolp obj) (fboundp obj) obj)))) | |
265 | (error nil))))) | |
266 | ||
267 | \f | |
268 | ;;; `User' help functions | |
269 | ||
433ae6f6 RS |
270 | (defun describe-distribution () |
271 | "Display info on how to obtain the latest version of GNU Emacs." | |
272 | (interactive) | |
9e0e631c | 273 | (view-file (expand-file-name "DISTRIB" data-directory))) |
433ae6f6 RS |
274 | |
275 | (defun describe-copying () | |
276 | "Display info on how you may redistribute copies of GNU Emacs." | |
277 | (interactive) | |
64f3b7d3 | 278 | (view-file (expand-file-name "COPYING" data-directory)) |
433ae6f6 RS |
279 | (goto-char (point-min))) |
280 | ||
76766f2d RS |
281 | (defun describe-project () |
282 | "Display info on the GNU project." | |
283 | (interactive) | |
64f3b7d3 | 284 | (view-file (expand-file-name "THE-GNU-PROJECT" data-directory)) |
76766f2d RS |
285 | (goto-char (point-min))) |
286 | ||
433ae6f6 RS |
287 | (defun describe-no-warranty () |
288 | "Display info on all the kinds of warranty Emacs does NOT have." | |
289 | (interactive) | |
290 | (describe-copying) | |
291 | (let (case-fold-search) | |
292 | (search-forward "NO WARRANTY") | |
293 | (recenter 0))) | |
294 | ||
61c6b658 | 295 | (defun describe-prefix-bindings () |
c7cba9cb RS |
296 | "Describe the bindings of the prefix used to reach this command. |
297 | The prefix described consists of all but the last event | |
298 | of the key sequence that ran this command." | |
61c6b658 | 299 | (interactive) |
ccc06dcc KH |
300 | (let* ((key (this-command-keys))) |
301 | (describe-bindings | |
302 | (if (stringp key) | |
303 | (substring key 0 (1- (length key))) | |
304 | (let ((prefix (make-vector (1- (length key)) nil)) | |
305 | (i 0)) | |
306 | (while (< i (length prefix)) | |
307 | (aset prefix i (aref key i)) | |
308 | (setq i (1+ i))) | |
309 | prefix))))) | |
788d62cf | 310 | ;; Make C-h after a prefix, when not specifically bound, |
c7cba9cb | 311 | ;; run describe-prefix-bindings. |
61c6b658 RS |
312 | (setq prefix-help-command 'describe-prefix-bindings) |
313 | ||
382d018a RS |
314 | (defun view-emacs-news (&optional arg) |
315 | "Display info on recent changes to Emacs. | |
f0753a5f | 316 | With numeric argument, display information on correspondingly older changes." |
382d018a | 317 | (interactive "P") |
f0753a5f GM |
318 | (let* ((arg (if arg (prefix-numeric-value arg) 0)) |
319 | (file (cond ((eq arg 0) "NEWS") | |
320 | ((eq arg 1) "ONEWS") | |
321 | (t | |
322 | (nth (- arg 2) | |
323 | (nreverse (directory-files data-directory | |
975f82c9 | 324 | nil "^ONEWS\\.[0-9]+$" |
f0753a5f GM |
325 | nil))))))) |
326 | (if file | |
64f3b7d3 | 327 | (view-file (expand-file-name file data-directory)) |
f0753a5f | 328 | (error "No such old news")))) |
433ae6f6 | 329 | |
1ebc1f01 RS |
330 | (defun view-todo (&optional arg) |
331 | "Display the Emacs TODO list." | |
332 | (interactive "P") | |
333 | (view-file (expand-file-name "TODO" data-directory))) | |
334 | ||
4f16ea85 RS |
335 | (defun view-echo-area-messages () |
336 | "View the log of recent echo-area messages: the `*Messages*' buffer. | |
337 | The number of messages retained in that buffer | |
338 | is specified by the variable `message-log-max'." | |
339 | (interactive) | |
340 | (switch-to-buffer (get-buffer-create "*Messages*"))) | |
341 | ||
754084bb GM |
342 | (defun view-order-manuals () |
343 | "Display the Emacs ORDERS file." | |
344 | (interactive) | |
64f3b7d3 | 345 | (view-file (expand-file-name "ORDERS" data-directory)) |
38790755 | 346 | (goto-address)) |
754084bb | 347 | |
7ee71cf1 RS |
348 | (defun view-emacs-FAQ () |
349 | "Display the Emacs Frequently Asked Questions (FAQ) file." | |
350 | (interactive) | |
94ea540b | 351 | ;; (find-file-read-only (expand-file-name "FAQ" data-directory)) |
279b772d | 352 | (info "(efaq)")) |
7ee71cf1 | 353 | |
4cbff657 DL |
354 | (defun view-emacs-problems () |
355 | "Display info on known problems with Emacs and possible workarounds." | |
356 | (interactive) | |
357 | (view-file (expand-file-name "PROBLEMS" data-directory))) | |
358 | ||
433ae6f6 | 359 | (defun view-lossage () |
50b57199 EZ |
360 | "Display last 100 input keystrokes. |
361 | ||
362 | To record all your input on a file, use `open-dribble-file'." | |
433ae6f6 | 363 | (interactive) |
3e5929af SM |
364 | (help-setup-xref (list #'view-lossage) (interactive-p)) |
365 | (with-output-to-temp-buffer (help-buffer) | |
02dfca16 SM |
366 | (princ (mapconcat (lambda (key) |
367 | (if (or (integerp key) (symbolp key) (listp key)) | |
368 | (single-key-description key) | |
369 | (prin1-to-string key nil))) | |
298a7c8c RS |
370 | (recent-keys) |
371 | " ")) | |
b0fbf754 | 372 | (with-current-buffer standard-output |
433ae6f6 RS |
373 | (goto-char (point-min)) |
374 | (while (progn (move-to-column 50) (not (eobp))) | |
375 | (search-forward " " nil t) | |
3e5929af | 376 | (insert "\n"))) |
433ae6f6 RS |
377 | (print-help-return-message))) |
378 | ||
788d62cf MB |
379 | \f |
380 | ;; Key bindings | |
433ae6f6 | 381 | |
4c45295b | 382 | (defun describe-bindings (&optional prefix buffer) |
a8ad43aa RS |
383 | "Show a list of all defined keys, and their definitions. |
384 | We put that list in a buffer, and display the buffer. | |
385 | ||
386 | The optional argument PREFIX, if non-nil, should be a key sequence; | |
4c45295b KH |
387 | then we display only bindings that start with that prefix. |
388 | The optional argument BUFFER specifies which buffer's bindings | |
389 | to display (default, the current buffer)." | |
3e5929af | 390 | (interactive) |
4c45295b | 391 | (or buffer (setq buffer (current-buffer))) |
3e5929af | 392 | (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p)) |
4c45295b | 393 | (with-current-buffer buffer |
3e5929af | 394 | (describe-bindings-internal nil prefix))) |
a8ad43aa | 395 | |
94ea540b SM |
396 | ;; This function used to be in keymap.c. |
397 | (defun describe-bindings-internal (&optional menus prefix) | |
398 | "Show a list of all defined keys, and their definitions. | |
399 | We put that list in a buffer, and display the buffer. | |
400 | ||
401 | The optional argument MENUS, if non-nil, says to mention menu bindings. | |
402 | \(Ordinarily these are omitted from the output.) | |
403 | The optional argument PREFIX, if non-nil, should be a key sequence; | |
404 | then we display only bindings that start with that prefix." | |
405 | (interactive) | |
406 | (let ((buf (current-buffer))) | |
407 | (with-output-to-temp-buffer "*Help*" | |
408 | (with-current-buffer standard-output | |
409 | (describe-buffer-bindings buf prefix menus))))) | |
410 | ||
e88a2c59 | 411 | (defun where-is (definition &optional insert) |
b2c85790 | 412 | "Print message listing key sequences that invoke the command DEFINITION. |
e88a2c59 RS |
413 | Argument is a command definition, usually a symbol with a function definition. |
414 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer." | |
54c0b967 RS |
415 | (interactive |
416 | (let ((fn (function-called-at-point)) | |
788d62cf | 417 | (enable-recursive-minibuffers t) |
54c0b967 | 418 | val) |
3829bcc5 SM |
419 | (setq val (completing-read |
420 | (if fn | |
421 | (format "Where is command (default %s): " fn) | |
422 | "Where is command: ") | |
423 | obarray 'commandp t)) | |
424 | (list (if (equal val "") fn (intern val)) current-prefix-arg))) | |
7a698dc1 | 425 | (let ((func (indirect-function definition)) |
3829bcc5 | 426 | (defs nil) |
7a698dc1 | 427 | (standard-output (if insert (current-buffer) t))) |
3829bcc5 SM |
428 | (mapatoms (lambda (symbol) |
429 | (and (fboundp symbol) | |
430 | (not (eq symbol definition)) | |
d92c2757 RS |
431 | (eq func (condition-case () |
432 | (indirect-function symbol) | |
433 | (error symbol))) | |
3829bcc5 | 434 | (push symbol defs)))) |
7a698dc1 JB |
435 | (princ (mapconcat |
436 | #'(lambda (symbol) | |
023b93f6 | 437 | (let* ((remapped (command-remapping symbol)) |
3829bcc5 SM |
438 | (keys (where-is-internal |
439 | symbol overriding-local-map nil nil remapped)) | |
440 | (keys (mapconcat 'key-description keys ", "))) | |
7a698dc1 JB |
441 | (if insert |
442 | (if (> (length keys) 0) | |
443 | (if remapped | |
444 | (format "%s (%s) (remapped from %s)" | |
445 | keys remapped symbol) | |
446 | (format "%s (%s)" keys symbol)) | |
447 | (format "M-x %s RET" symbol)) | |
448 | (if (> (length keys) 0) | |
449 | (if remapped | |
450 | (format "%s is remapped to %s which is on %s" | |
451 | definition symbol keys) | |
452 | (format "%s is on %s" symbol keys)) | |
453 | (format "%s is not on any key" symbol))))) | |
3829bcc5 | 454 | (cons definition defs) |
7a698dc1 | 455 | ";\nand "))) |
54c0b967 RS |
456 | nil) |
457 | ||
788d62cf MB |
458 | (defun string-key-binding (key) |
459 | "Value is the binding of KEY in a string. | |
460 | If KEY is an event on a string, and that string has a `local-map' | |
461 | or `keymap' property, return the binding of KEY in the string's keymap." | |
462 | (let* ((defn nil) | |
463 | (start (when (vectorp key) | |
f03b7c7d GM |
464 | (if (memq (aref key 0) |
465 | '(mode-line header-line left-margin right-margin)) | |
788d62cf MB |
466 | (event-start (aref key 1)) |
467 | (and (consp (aref key 0)) | |
468 | (event-start (aref key 0)))))) | |
469 | (string-info (and (consp start) (nth 4 start)))) | |
470 | (when string-info | |
471 | (let* ((string (car string-info)) | |
472 | (pos (cdr string-info)) | |
f03b7c7d | 473 | (local-map (and (>= pos 0) |
788d62cf MB |
474 | (< pos (length string)) |
475 | (or (get-text-property pos 'local-map string) | |
476 | (get-text-property pos 'keymap string))))) | |
477 | (setq defn (and local-map (lookup-key local-map key))))) | |
478 | defn)) | |
1a06eabd | 479 | |
02dfca16 SM |
480 | (defun help-key-description (key untranslated) |
481 | (let ((string (key-description key))) | |
ae1bb8ac SM |
482 | (if (or (not untranslated) |
483 | (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e)))) | |
02dfca16 SM |
484 | string |
485 | (let ((otherstring (key-description untranslated))) | |
486 | (if (equal string otherstring) | |
487 | string | |
488 | (format "%s (translated from %s)" string otherstring)))))) | |
71296446 | 489 | |
02dfca16 | 490 | (defun describe-key-briefly (key &optional insert untranslated) |
788d62cf | 491 | "Print the name of the function KEY invokes. KEY is a string. |
02dfca16 SM |
492 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer. |
493 | If non-nil UNTRANSLATED is a vector of the untranslated events. | |
494 | It can also be a number in which case the untranslated events from | |
495 | the last key hit are used." | |
496 | (interactive "kDescribe key briefly: \nP\np") | |
497 | (if (numberp untranslated) | |
498 | (setq untranslated (this-single-command-raw-keys))) | |
400a1b1f | 499 | (save-excursion |
788d62cf MB |
500 | (let ((modifiers (event-modifiers (aref key 0))) |
501 | (standard-output (if insert (current-buffer) t)) | |
502 | window position) | |
503 | ;; For a mouse button event, go to the button it applies to | |
504 | ;; to get the right key bindings. And go to the right place | |
505 | ;; in case the keymap depends on where you clicked. | |
506 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
507 | (memq 'drag modifiers)) | |
508 | (setq window (posn-window (event-start (aref key 0))) | |
509 | position (posn-point (event-start (aref key 0))))) | |
510 | (if (windowp window) | |
511 | (progn | |
512 | (set-buffer (window-buffer window)) | |
513 | (goto-char position))) | |
514 | ;; Ok, now look up the key and name the command. | |
515 | (let ((defn (or (string-key-binding key) | |
516 | (key-binding key))) | |
02dfca16 | 517 | (key-desc (help-key-description key untranslated))) |
d61ac4a6 | 518 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
788d62cf | 519 | (princ (format "%s is undefined" key-desc)) |
80d553d4 RS |
520 | (princ (format (if (windowp window) |
521 | "%s at that spot runs the command %s" | |
522 | "%s runs the command %s") | |
788d62cf MB |
523 | key-desc |
524 | (if (symbolp defn) defn (prin1-to-string defn))))))))) | |
96ede6b2 | 525 | |
788d62cf | 526 | |
02dfca16 | 527 | (defun describe-key (key &optional untranslated) |
a0d32c10 RS |
528 | "Display documentation of the function invoked by KEY. |
529 | KEY should be a key sequence--when calling from a program, | |
02dfca16 SM |
530 | pass a string or a vector. |
531 | If non-nil UNTRANSLATED is a vector of the untranslated events. | |
532 | It can also be a number in which case the untranslated events from | |
533 | the last key hit are used." | |
534 | (interactive "kDescribe key: \np") | |
535 | (if (numberp untranslated) | |
536 | (setq untranslated (this-single-command-raw-keys))) | |
788d62cf MB |
537 | (save-excursion |
538 | (let ((modifiers (event-modifiers (aref key 0))) | |
539 | window position) | |
540 | ;; For a mouse button event, go to the button it applies to | |
541 | ;; to get the right key bindings. And go to the right place | |
542 | ;; in case the keymap depends on where you clicked. | |
543 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
544 | (memq 'drag modifiers)) | |
545 | (setq window (posn-window (event-start (aref key 0))) | |
546 | position (posn-point (event-start (aref key 0))))) | |
94ea540b | 547 | (when (windowp window) |
788d62cf | 548 | (set-buffer (window-buffer window)) |
94ea540b | 549 | (goto-char position)) |
788d62cf | 550 | (let ((defn (or (string-key-binding key) (key-binding key)))) |
d61ac4a6 | 551 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
02dfca16 | 552 | (message "%s is undefined" (help-key-description key untranslated)) |
3e5929af SM |
553 | (help-setup-xref (list #'describe-function defn) (interactive-p)) |
554 | (with-output-to-temp-buffer (help-buffer) | |
02dfca16 | 555 | (princ (help-key-description key untranslated)) |
788d62cf MB |
556 | (if (windowp window) |
557 | (princ " at that spot")) | |
558 | (princ " runs the command ") | |
559 | (prin1 defn) | |
560 | (princ "\n which is ") | |
3e5929af | 561 | (describe-function-1 defn) |
788d62cf | 562 | (print-help-return-message))))))) |
400a1b1f | 563 | |
400a1b1f | 564 | \f |
788d62cf MB |
565 | (defun describe-mode (&optional buffer) |
566 | "Display documentation of current major mode and minor modes. | |
6b61353c KH |
567 | A brief summary of the minor modes comes first, followed by the |
568 | major mode description. This is followed by detailed | |
569 | descriptions of the minor modes, each on a separate page. | |
570 | ||
571 | For this to work correctly for a minor mode, the mode's indicator | |
572 | variable \(listed in `minor-mode-alist') must also be a function | |
573 | whose documentation describes the minor mode." | |
400a1b1f | 574 | (interactive) |
9639be74 RS |
575 | (help-setup-xref (list #'describe-mode (or buffer (current-buffer))) |
576 | (interactive-p)) | |
577 | ;; For the sake of help-do-xref and help-xref-go-back, | |
578 | ;; don't switch buffers before calling `help-buffer'. | |
3e5929af | 579 | (with-output-to-temp-buffer (help-buffer) |
9639be74 RS |
580 | (save-excursion |
581 | (when buffer (set-buffer buffer)) | |
6b61353c KH |
582 | (let (minor-modes) |
583 | ;; Find enabled minor mode we will want to mention. | |
584 | (dolist (mode minor-mode-list) | |
585 | ;; Document a minor mode if it is listed in minor-mode-alist, | |
586 | ;; non-nil, and has a function definition. | |
587 | (and (boundp mode) (symbol-value mode) | |
588 | (fboundp mode) | |
589 | (let ((pretty-minor-mode mode) | |
590 | indicator) | |
591 | (if (string-match "\\(-minor\\)?-mode\\'" | |
592 | (symbol-name mode)) | |
593 | (setq pretty-minor-mode | |
594 | (capitalize | |
595 | (substring (symbol-name mode) | |
596 | 0 (match-beginning 0))))) | |
597 | (setq indicator (cadr (assq mode minor-mode-alist))) | |
598 | (while (and indicator (symbolp indicator) | |
599 | (boundp indicator) | |
600 | (not (eq indicator (symbol-value indicator)))) | |
601 | (setq indicator (symbol-value indicator))) | |
602 | (push (list pretty-minor-mode mode indicator) | |
603 | minor-modes)))) | |
604 | (if auto-fill-function | |
605 | (push '("Auto Fill" auto-fill-mode " Fill") | |
606 | minor-modes)) | |
607 | (setq minor-modes | |
608 | (sort minor-modes | |
609 | (lambda (a b) (string-lessp (car a) (car b))))) | |
610 | (when minor-modes | |
611 | (princ "Summary of minor modes:\n") | |
612 | (dolist (mode minor-modes) | |
613 | (let ((pretty-minor-mode (nth 0 mode)) | |
614 | (indicator (nth 2 mode))) | |
615 | (princ (format " %s minor mode (%s):\n" | |
616 | pretty-minor-mode | |
617 | (if indicator | |
618 | (format "indicator%s" indicator) | |
619 | "no indicator"))))) | |
620 | (princ "\n(Full information about these minor modes | |
621 | follows the description of the major mode.)\n\n")) | |
622 | ;; Document the major mode. | |
623 | (princ mode-name) | |
624 | (princ " mode:\n") | |
625 | (princ (documentation major-mode)) | |
626 | ;; Document the minor modes fully. | |
627 | (dolist (mode minor-modes) | |
628 | (let ((pretty-minor-mode (nth 0 mode)) | |
629 | (mode-function (nth 1 mode)) | |
630 | (indicator (nth 2 mode))) | |
631 | (princ "\n\f\n") | |
632 | (princ (format "%s minor mode (%s):\n" | |
633 | pretty-minor-mode | |
634 | (if indicator | |
635 | (format "indicator%s" indicator) | |
636 | "no indicator"))) | |
637 | (princ (documentation mode-function))))) | |
9639be74 | 638 | (print-help-return-message)))) |
400a1b1f | 639 | |
6b61353c | 640 | |
8e864068 | 641 | (defun describe-minor-mode (minor-mode) |
335028c3 MY |
642 | "Display documentation of a minor mode given as MINOR-MODE. |
643 | MINOR-MODE can be a minor mode symbol or a minor mode indicator string | |
644 | appeared on the mode-line." | |
6b61353c | 645 | (interactive (list (completing-read |
335028c3 MY |
646 | "Minor mode: " |
647 | (nconc | |
648 | (describe-minor-mode-completion-table-for-symbol) | |
649 | (describe-minor-mode-completion-table-for-indicator) | |
650 | )))) | |
651 | (if (symbolp minor-mode) | |
652 | (setq minor-mode (symbol-name minor-mode))) | |
653 | (let ((symbols (describe-minor-mode-completion-table-for-symbol)) | |
654 | (indicators (describe-minor-mode-completion-table-for-indicator))) | |
655 | (cond | |
656 | ((member minor-mode symbols) | |
657 | (describe-minor-mode-from-symbol (intern minor-mode))) | |
658 | ((member minor-mode indicators) | |
659 | (describe-minor-mode-from-indicator minor-mode)) | |
660 | (t | |
661 | (error "No such minor mode: %s" minor-mode))))) | |
662 | ||
6b61353c | 663 | ;; symbol |
335028c3 MY |
664 | (defun describe-minor-mode-completion-table-for-symbol () |
665 | ;; In order to list up all minor modes, minor-mode-list | |
666 | ;; is used here instead of minor-mode-alist. | |
667 | (delq nil (mapcar 'symbol-name minor-mode-list))) | |
668 | (defun describe-minor-mode-from-symbol (symbol) | |
669 | "Display documentation of a minor mode given as a symbol, SYMBOL" | |
6b61353c | 670 | (interactive (list (intern (completing-read |
335028c3 MY |
671 | "Minor mode symbol: " |
672 | (describe-minor-mode-completion-table-for-symbol))))) | |
673 | (if (fboundp symbol) | |
674 | (describe-function symbol) | |
675 | (describe-variable symbol))) | |
676 | ||
677 | ;; indicator | |
678 | (defun describe-minor-mode-completion-table-for-indicator () | |
6b61353c | 679 | (delq nil |
335028c3 MY |
680 | (mapcar (lambda (x) |
681 | (let ((i (format-mode-line x))) | |
682 | ;; remove first space if existed | |
683 | (cond | |
684 | ((= 0 (length i)) | |
685 | nil) | |
686 | ((eq (aref i 0) ?\ ) | |
687 | (substring i 1)) | |
6b61353c | 688 | (t |
335028c3 MY |
689 | i)))) |
690 | minor-mode-alist))) | |
8e864068 | 691 | (defun describe-minor-mode-from-indicator (indicator) |
335028c3 MY |
692 | "Display documentation of a minor mode specified by INDICATOR. |
693 | If you call this function interactively, you can give indicator which | |
694 | is currently activated with completion." | |
6b61353c KH |
695 | (interactive (list |
696 | (completing-read | |
8e864068 | 697 | "Minor mode indicator: " |
335028c3 | 698 | (describe-minor-mode-completion-table-for-indicator)))) |
8e864068 JB |
699 | (let ((minor-mode (lookup-minor-mode-from-indicator indicator))) |
700 | (if minor-mode | |
335028c3 | 701 | (describe-minor-mode-from-symbol minor-mode) |
8e864068 JB |
702 | (error "Cannot find minor mode for `%s'" indicator)))) |
703 | ||
704 | (defun lookup-minor-mode-from-indicator (indicator) | |
705 | "Return a minor mode symbol from its indicator on the modeline." | |
335028c3 | 706 | ;; remove first space if existed |
6b61353c | 707 | (if (and (< 0 (length indicator)) |
335028c3 MY |
708 | (eq (aref indicator 0) ?\ )) |
709 | (setq indicator (substring indicator 1))) | |
8e864068 JB |
710 | (let ((minor-modes minor-mode-alist) |
711 | result) | |
712 | (while minor-modes | |
713 | (let* ((minor-mode (car (car minor-modes))) | |
6b61353c | 714 | (anindicator (format-mode-line |
335028c3 MY |
715 | (car (cdr (car minor-modes)))))) |
716 | ;; remove first space if existed | |
6b61353c | 717 | (if (and (stringp anindicator) |
335028c3 MY |
718 | (> (length anindicator) 0) |
719 | (eq (aref anindicator 0) ?\ )) | |
720 | (setq anindicator (substring anindicator 1))) | |
721 | (if (equal indicator anindicator) | |
8e864068 JB |
722 | (setq result minor-mode |
723 | minor-modes nil) | |
724 | (setq minor-modes (cdr minor-modes))))) | |
725 | result)) | |
726 | ||
48ce3c22 RS |
727 | \f |
728 | ;;; Automatic resizing of temporary buffers. | |
729 | ||
4483ddc5 | 730 | (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2)) |
48ce3c22 RS |
731 | "*Maximum height of a window displaying a temporary buffer. |
732 | This is the maximum height (in text lines) which `resize-temp-buffer-window' | |
733 | will give to a window displaying a temporary buffer. | |
734 | It can also be a function which will be called with the object corresponding | |
735 | to the buffer to be displayed as argument and should return an integer | |
736 | positive number." | |
737 | :type '(choice integer function) | |
738 | :group 'help | |
739 | :version "20.4") | |
740 | ||
4e1ede6c SM |
741 | (define-minor-mode temp-buffer-resize-mode |
742 | "Toggle the mode which makes windows smaller for temporary buffers. | |
48ce3c22 RS |
743 | With prefix argument ARG, turn the resizing of windows displaying temporary |
744 | buffers on if ARG is positive or off otherwise. | |
4e1ede6c SM |
745 | This makes the window the right height for its contents, but never |
746 | more than `temp-buffer-max-height' nor less than `window-min-height'. | |
747 | This applies to `help', `apropos' and `completion' buffers, and some others." | |
b0fbf754 | 748 | :global t :group 'help |
4e1ede6c | 749 | (if temp-buffer-resize-mode |
57f43907 | 750 | ;; `help-make-xrefs' may add a `back' button and thus increase the |
4e1ede6c SM |
751 | ;; text size, so `resize-temp-buffer-window' must be run *after* it. |
752 | (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append) | |
8304a3bb | 753 | (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window))) |
48ce3c22 RS |
754 | |
755 | (defun resize-temp-buffer-window () | |
756 | "Resize the current window to fit its contents. | |
4483ddc5 | 757 | Will not make it higher than `temp-buffer-max-height' nor smaller than |
b2c85790 | 758 | `window-min-height'. Do nothing if it is the only window on its frame, if it |
48ce3c22 RS |
759 | is not as wide as the frame or if some of the window's contents are scrolled |
760 | out of view." | |
761 | (unless (or (one-window-p 'nomini) | |
762 | (not (pos-visible-in-window-p (point-min))) | |
763 | (/= (frame-width) (window-width))) | |
d9c30bdf MB |
764 | (fit-window-to-buffer |
765 | (selected-window) | |
766 | (if (functionp temp-buffer-max-height) | |
767 | (funcall temp-buffer-max-height (current-buffer)) | |
768 | temp-buffer-max-height)))) | |
48ce3c22 | 769 | |
5fbeea74 GM |
770 | ;; Provide this for the sake of define-minor-mode which generates |
771 | ;; defcustoms which require 'help'. | |
5fbeea74 GM |
772 | (provide 'help) |
773 | ||
6b61353c | 774 | ;;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423 |
1a06eabd | 775 | ;;; help.el ends here |