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) | |
433ae6f6 RS |
52 | (define-key help-map "?" 'help-for-help) |
53 | ||
54 | (define-key help-map "\C-c" 'describe-copying) | |
55 | (define-key help-map "\C-d" 'describe-distribution) | |
4f16ea85 RS |
56 | (define-key help-map "\C-e" 'view-emacs-problems) |
57 | (define-key help-map "\C-f" 'view-emacs-FAQ) | |
4f16ea85 RS |
58 | (define-key help-map "\C-m" 'view-order-manuals) |
59 | (define-key help-map "\C-n" 'view-emacs-news) | |
76766f2d | 60 | (define-key help-map "\C-p" 'describe-project) |
4f16ea85 RS |
61 | (define-key help-map "\C-w" 'describe-no-warranty) |
62 | ||
63 | ;; This does not fit the pattern, but it is natural given the C-\ command. | |
64 | (define-key help-map "\C-\\" 'describe-input-method) | |
65 | ||
66 | (define-key help-map "C" 'describe-coding-system) | |
67 | (define-key help-map "F" 'Info-goto-emacs-command-node) | |
68 | (define-key help-map "I" 'describe-input-method) | |
69 | (define-key help-map "K" 'Info-goto-emacs-key-command-node) | |
70 | (define-key help-map "L" 'describe-language-environment) | |
71 | (define-key help-map "S" 'info-lookup-symbol) | |
72 | ||
122955bf | 73 | (define-key help-map "a" 'apropos-command) |
433ae6f6 RS |
74 | |
75 | (define-key help-map "b" 'describe-bindings) | |
76 | ||
77 | (define-key help-map "c" 'describe-key-briefly) | |
433ae6f6 | 78 | |
4f16ea85 RS |
79 | (define-key help-map "e" 'view-echo-area-messages) |
80 | ||
433ae6f6 RS |
81 | (define-key help-map "f" 'describe-function) |
82 | ||
4f16ea85 | 83 | (define-key help-map "h" 'view-hello-file) |
7ee71cf1 | 84 | |
433ae6f6 | 85 | (define-key help-map "i" 'info) |
4b08b7ed | 86 | (define-key help-map "4i" 'info-other-window) |
4f16ea85 RS |
87 | |
88 | (define-key help-map "k" 'describe-key) | |
433ae6f6 RS |
89 | |
90 | (define-key help-map "l" 'view-lossage) | |
91 | ||
92 | (define-key help-map "m" 'describe-mode) | |
93 | ||
433ae6f6 RS |
94 | (define-key help-map "n" 'view-emacs-news) |
95 | ||
06b98c51 | 96 | (define-key help-map "p" 'finder-by-keyword) |
3e9c095d RS |
97 | (autoload 'finder-by-keyword "finder" |
98 | "Find packages matching a given keyword." t) | |
06b98c51 | 99 | |
433ae6f6 RS |
100 | (define-key help-map "s" 'describe-syntax) |
101 | ||
102 | (define-key help-map "t" 'help-with-tutorial) | |
103 | ||
104 | (define-key help-map "w" 'where-is) | |
105 | ||
106 | (define-key help-map "v" 'describe-variable) | |
107 | ||
2fc9d9f4 RS |
108 | (define-key help-map "q" 'help-quit) |
109 | ||
0cf0d828 | 110 | \f |
2fc9d9f4 | 111 | (defun help-quit () |
3120a677 | 112 | "Just exit from the Help command's command loop." |
2fc9d9f4 RS |
113 | (interactive) |
114 | nil) | |
115 | ||
01364a75 RS |
116 | (defvar help-return-method nil |
117 | "What to do to \"exit\" the help buffer. | |
118 | This is a list | |
119 | (WINDOW . t) delete the selected window, go to WINDOW. | |
120 | (WINDOW . quit-window) do quit-window, then select WINDOW. | |
121 | (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.") | |
122 | ||
433ae6f6 RS |
123 | (defun print-help-return-message (&optional function) |
124 | "Display or return message saying how to restore windows after help command. | |
125 | Computes a message and applies the optional argument FUNCTION to it. | |
126 | If FUNCTION is nil, applies `message' to it, thus printing it." | |
127 | (and (not (get-buffer-window standard-output)) | |
d536293f | 128 | (let ((first-message |
a1c9f209 | 129 | (cond ((special-display-p (buffer-name standard-output)) |
01364a75 | 130 | (setq help-return-method (cons (selected-window) t)) |
d536293f RS |
131 | ;; If the help output buffer is a special display buffer, |
132 | ;; don't say anything about how to get rid of it. | |
133 | ;; First of all, the user will do that with the window | |
134 | ;; manager, not with Emacs. | |
135 | ;; Secondly, the buffer has not been displayed yet, | |
136 | ;; so we don't know whether its frame will be selected. | |
d536293f | 137 | nil) |
f3ad2fc8 GM |
138 | (display-buffer-reuse-frames |
139 | (setq help-return-method (cons (selected-window) | |
140 | 'quit-window)) | |
141 | nil) | |
d536293f | 142 | ((not (one-window-p t)) |
01364a75 RS |
143 | (setq help-return-method |
144 | (cons (selected-window) 'quit-window)) | |
d536293f RS |
145 | "Type \\[switch-to-buffer-other-window] RET to restore the other window.") |
146 | (pop-up-windows | |
01364a75 | 147 | (setq help-return-method (cons (selected-window) t)) |
d536293f RS |
148 | "Type \\[delete-other-windows] to remove help window.") |
149 | (t | |
01364a75 RS |
150 | (setq help-return-method |
151 | (list (selected-window) (window-buffer) | |
152 | (window-start) (window-point))) | |
d536293f RS |
153 | "Type \\[switch-to-buffer] RET to remove help window.")))) |
154 | (funcall (or function 'message) | |
155 | (concat | |
156 | (if first-message | |
376b2a24 DL |
157 | (substitute-command-keys first-message)) |
158 | (if first-message " ") | |
125a8d70 RS |
159 | ;; If the help buffer will go in a separate frame, |
160 | ;; it's no use mentioning a command to scroll, so don't. | |
a1c9f209 | 161 | (if (special-display-p (buffer-name standard-output)) |
125a8d70 | 162 | nil |
a1c9f209 | 163 | (if (same-window-p (buffer-name standard-output)) |
125a8d70 RS |
164 | ;; Say how to scroll this window. |
165 | (substitute-command-keys | |
166 | "\\[scroll-up] to scroll the help.") | |
167 | ;; Say how to scroll some other window. | |
6e7f5182 | 168 | (substitute-command-keys |
125a8d70 | 169 | "\\[scroll-other-window] to scroll the help.")))))))) |
433ae6f6 | 170 | |
433ae6f6 RS |
171 | ;; So keyboard macro definitions are documented correctly |
172 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
173 | ||
788d62cf MB |
174 | (defalias 'help 'help-for-help) |
175 | (make-help-screen help-for-help | |
4f16ea85 | 176 | "a b c C e f F C-f i I k C-k l L m p s t v w C-c C-d C-n C-p C-w or ? :" |
788d62cf MB |
177 | "You have typed %THIS-KEY%, the help character. Type a Help option: |
178 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) | |
179 | ||
180 | a command-apropos. Give a substring, and see a list of commands | |
181 | (functions interactively callable) that contain | |
182 | that substring. See also the apropos command. | |
183 | b describe-bindings. Display table of all key bindings. | |
184 | c describe-key-briefly. Type a command key sequence; | |
185 | it prints the function name that sequence runs. | |
186 | C describe-coding-system. This describes either a specific coding system | |
187 | (if you type its name) or the coding systems currently in use | |
188 | (if you type just RET). | |
4f16ea85 | 189 | e view-echo-area-messages. Show the `*Messages*' buffer. |
788d62cf | 190 | f describe-function. Type a function name and get documentation of it. |
4f16ea85 | 191 | F Info-goto-emacs-command-node. Type a function name; |
788d62cf | 192 | it takes you to the Info node for that command. |
54391990 | 193 | h Display the HELLO file which illustrates various scripts. |
788d62cf MB |
194 | i info. The info documentation reader. |
195 | I describe-input-method. Describe a specific input method (if you type | |
196 | its name) or the current input method (if you type just RET). | |
788d62cf MB |
197 | k describe-key. Type a command key sequence; |
198 | it displays the full documentation. | |
4f16ea85 | 199 | K Info-goto-emacs-key-command-node. Type a command key sequence; |
788d62cf MB |
200 | it takes you to the Info node for the command bound to that key. |
201 | l view-lossage. Show last 100 characters you typed. | |
202 | L describe-language-environment. This describes either a | |
203 | specific language environment (if you type its name) | |
204 | or the current language environment (if you type just RET). | |
205 | m describe-mode. Print documentation of current minor modes, | |
206 | and the current major mode, including their special commands. | |
788d62cf MB |
207 | p finder-by-keyword. Find packages matching a given topic keyword. |
208 | s describe-syntax. Display contents of syntax table, plus explanations. | |
4f16ea85 RS |
209 | S info-lookup-symbol. Display the definition of a specific symbol |
210 | as found in the manual for the language this buffer is written in. | |
788d62cf MB |
211 | t help-with-tutorial. Select the Emacs learn-by-doing tutorial. |
212 | v describe-variable. Type name of a variable; | |
213 | it displays the variable's documentation and value. | |
214 | w where-is. Type command name; it prints which keystrokes | |
215 | invoke that command. | |
216 | ||
4f16ea85 | 217 | C-c Display Emacs copying permission (GNU General Public License). |
788d62cf | 218 | C-d Display Emacs ordering information. |
4f16ea85 RS |
219 | C-e Display info about Emacs problems. |
220 | C-f Display the Emacs FAQ. | |
4f16ea85 | 221 | C-m Display how to order printed Emacs manuals. |
788d62cf MB |
222 | C-n Display news of recent Emacs changes. |
223 | C-p Display information about the GNU project. | |
224 | C-w Display information on absence of warranty for GNU Emacs." | |
225 | help-map) | |
226 | ||
227 | \f | |
228 | ||
229 | (defun function-called-at-point () | |
230 | "Return a function around point or else called by the list containing point. | |
231 | If that doesn't give a function, return nil." | |
232 | (with-syntax-table emacs-lisp-mode-syntax-table | |
233 | (or (condition-case () | |
234 | (save-excursion | |
235 | (or (not (zerop (skip-syntax-backward "_w"))) | |
236 | (eq (char-syntax (following-char)) ?w) | |
237 | (eq (char-syntax (following-char)) ?_) | |
238 | (forward-sexp -1)) | |
239 | (skip-chars-forward "'") | |
240 | (let ((obj (read (current-buffer)))) | |
241 | (and (symbolp obj) (fboundp obj) obj))) | |
242 | (error nil)) | |
243 | (condition-case () | |
244 | (save-excursion | |
245 | (save-restriction | |
246 | (narrow-to-region (max (point-min) | |
247 | (- (point) 1000)) (point-max)) | |
248 | ;; Move up to surrounding paren, then after the open. | |
249 | (backward-up-list 1) | |
250 | (forward-char 1) | |
251 | ;; If there is space here, this is probably something | |
252 | ;; other than a real Lisp function call, so ignore it. | |
253 | (if (looking-at "[ \t]") | |
254 | (error "Probably not a Lisp function call")) | |
255 | (let ((obj (read (current-buffer)))) | |
256 | (and (symbolp obj) (fboundp obj) obj)))) | |
257 | (error nil))))) | |
258 | ||
259 | \f | |
260 | ;;; `User' help functions | |
261 | ||
433ae6f6 RS |
262 | (defun describe-distribution () |
263 | "Display info on how to obtain the latest version of GNU Emacs." | |
264 | (interactive) | |
9e0e631c | 265 | (view-file (expand-file-name "DISTRIB" data-directory))) |
433ae6f6 RS |
266 | |
267 | (defun describe-copying () | |
268 | "Display info on how you may redistribute copies of GNU Emacs." | |
269 | (interactive) | |
64f3b7d3 | 270 | (view-file (expand-file-name "COPYING" data-directory)) |
433ae6f6 RS |
271 | (goto-char (point-min))) |
272 | ||
76766f2d RS |
273 | (defun describe-project () |
274 | "Display info on the GNU project." | |
275 | (interactive) | |
64f3b7d3 | 276 | (view-file (expand-file-name "THE-GNU-PROJECT" data-directory)) |
76766f2d RS |
277 | (goto-char (point-min))) |
278 | ||
433ae6f6 RS |
279 | (defun describe-no-warranty () |
280 | "Display info on all the kinds of warranty Emacs does NOT have." | |
281 | (interactive) | |
282 | (describe-copying) | |
283 | (let (case-fold-search) | |
284 | (search-forward "NO WARRANTY") | |
285 | (recenter 0))) | |
286 | ||
61c6b658 | 287 | (defun describe-prefix-bindings () |
c7cba9cb RS |
288 | "Describe the bindings of the prefix used to reach this command. |
289 | The prefix described consists of all but the last event | |
290 | of the key sequence that ran this command." | |
61c6b658 | 291 | (interactive) |
ccc06dcc KH |
292 | (let* ((key (this-command-keys))) |
293 | (describe-bindings | |
294 | (if (stringp key) | |
295 | (substring key 0 (1- (length key))) | |
296 | (let ((prefix (make-vector (1- (length key)) nil)) | |
297 | (i 0)) | |
298 | (while (< i (length prefix)) | |
299 | (aset prefix i (aref key i)) | |
300 | (setq i (1+ i))) | |
301 | prefix))))) | |
788d62cf | 302 | ;; Make C-h after a prefix, when not specifically bound, |
c7cba9cb | 303 | ;; run describe-prefix-bindings. |
61c6b658 RS |
304 | (setq prefix-help-command 'describe-prefix-bindings) |
305 | ||
382d018a RS |
306 | (defun view-emacs-news (&optional arg) |
307 | "Display info on recent changes to Emacs. | |
f0753a5f | 308 | With numeric argument, display information on correspondingly older changes." |
382d018a | 309 | (interactive "P") |
f0753a5f GM |
310 | (let* ((arg (if arg (prefix-numeric-value arg) 0)) |
311 | (file (cond ((eq arg 0) "NEWS") | |
312 | ((eq arg 1) "ONEWS") | |
313 | (t | |
314 | (nth (- arg 2) | |
315 | (nreverse (directory-files data-directory | |
975f82c9 | 316 | nil "^ONEWS\\.[0-9]+$" |
f0753a5f GM |
317 | nil))))))) |
318 | (if file | |
64f3b7d3 | 319 | (view-file (expand-file-name file data-directory)) |
f0753a5f | 320 | (error "No such old news")))) |
433ae6f6 | 321 | |
4f16ea85 RS |
322 | (defun view-echo-area-messages () |
323 | "View the log of recent echo-area messages: the `*Messages*' buffer. | |
324 | The number of messages retained in that buffer | |
325 | is specified by the variable `message-log-max'." | |
326 | (interactive) | |
327 | (switch-to-buffer (get-buffer-create "*Messages*"))) | |
328 | ||
754084bb GM |
329 | (defun view-order-manuals () |
330 | "Display the Emacs ORDERS file." | |
331 | (interactive) | |
64f3b7d3 | 332 | (view-file (expand-file-name "ORDERS" data-directory)) |
38790755 | 333 | (goto-address)) |
754084bb | 334 | |
7ee71cf1 RS |
335 | (defun view-emacs-FAQ () |
336 | "Display the Emacs Frequently Asked Questions (FAQ) file." | |
337 | (interactive) | |
94ea540b | 338 | ;; (find-file-read-only (expand-file-name "FAQ" data-directory)) |
279b772d | 339 | (info "(efaq)")) |
7ee71cf1 | 340 | |
4cbff657 DL |
341 | (defun view-emacs-problems () |
342 | "Display info on known problems with Emacs and possible workarounds." | |
343 | (interactive) | |
344 | (view-file (expand-file-name "PROBLEMS" data-directory))) | |
345 | ||
433ae6f6 | 346 | (defun view-lossage () |
50b57199 EZ |
347 | "Display last 100 input keystrokes. |
348 | ||
349 | To record all your input on a file, use `open-dribble-file'." | |
433ae6f6 | 350 | (interactive) |
3e5929af SM |
351 | (help-setup-xref (list #'view-lossage) (interactive-p)) |
352 | (with-output-to-temp-buffer (help-buffer) | |
02dfca16 SM |
353 | (princ (mapconcat (lambda (key) |
354 | (if (or (integerp key) (symbolp key) (listp key)) | |
355 | (single-key-description key) | |
356 | (prin1-to-string key nil))) | |
298a7c8c RS |
357 | (recent-keys) |
358 | " ")) | |
b0fbf754 | 359 | (with-current-buffer standard-output |
433ae6f6 RS |
360 | (goto-char (point-min)) |
361 | (while (progn (move-to-column 50) (not (eobp))) | |
362 | (search-forward " " nil t) | |
3e5929af | 363 | (insert "\n"))) |
433ae6f6 RS |
364 | (print-help-return-message))) |
365 | ||
788d62cf MB |
366 | \f |
367 | ;; Key bindings | |
433ae6f6 | 368 | |
4c45295b | 369 | (defun describe-bindings (&optional prefix buffer) |
a8ad43aa RS |
370 | "Show a list of all defined keys, and their definitions. |
371 | We put that list in a buffer, and display the buffer. | |
372 | ||
373 | The optional argument PREFIX, if non-nil, should be a key sequence; | |
4c45295b KH |
374 | then we display only bindings that start with that prefix. |
375 | The optional argument BUFFER specifies which buffer's bindings | |
376 | to display (default, the current buffer)." | |
3e5929af | 377 | (interactive) |
4c45295b | 378 | (or buffer (setq buffer (current-buffer))) |
3e5929af | 379 | (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p)) |
4c45295b | 380 | (with-current-buffer buffer |
3e5929af | 381 | (describe-bindings-internal nil prefix))) |
a8ad43aa | 382 | |
94ea540b SM |
383 | ;; This function used to be in keymap.c. |
384 | (defun describe-bindings-internal (&optional menus prefix) | |
385 | "Show a list of all defined keys, and their definitions. | |
386 | We put that list in a buffer, and display the buffer. | |
387 | ||
388 | The optional argument MENUS, if non-nil, says to mention menu bindings. | |
389 | \(Ordinarily these are omitted from the output.) | |
390 | The optional argument PREFIX, if non-nil, should be a key sequence; | |
391 | then we display only bindings that start with that prefix." | |
392 | (interactive) | |
393 | (let ((buf (current-buffer))) | |
394 | (with-output-to-temp-buffer "*Help*" | |
395 | (with-current-buffer standard-output | |
396 | (describe-buffer-bindings buf prefix menus))))) | |
397 | ||
e88a2c59 | 398 | (defun where-is (definition &optional insert) |
b2c85790 | 399 | "Print message listing key sequences that invoke the command DEFINITION. |
e88a2c59 RS |
400 | Argument is a command definition, usually a symbol with a function definition. |
401 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer." | |
54c0b967 RS |
402 | (interactive |
403 | (let ((fn (function-called-at-point)) | |
788d62cf | 404 | (enable-recursive-minibuffers t) |
54c0b967 RS |
405 | val) |
406 | (setq val (completing-read (if fn | |
407 | (format "Where is command (default %s): " fn) | |
408 | "Where is command: ") | |
d5f65532 | 409 | obarray 'commandp t)) |
54c0b967 | 410 | (list (if (equal val "") |
e88a2c59 RS |
411 | fn (intern val)) |
412 | current-prefix-arg))) | |
962ffad8 KS |
413 | (let* ((remapped (remap-command definition)) |
414 | (keys (where-is-internal definition overriding-local-map nil nil remapped)) | |
e88a2c59 RS |
415 | (keys1 (mapconcat 'key-description keys ", ")) |
416 | (standard-output (if insert (current-buffer) t))) | |
417 | (if insert | |
418 | (if (> (length keys1) 0) | |
962ffad8 KS |
419 | (if remapped |
420 | (princ (format "%s (%s) (remapped from %s)" keys1 remapped definition)) | |
290bfb66 | 421 | (princ (format "%s (%s)" keys1 definition))) |
e88a2c59 RS |
422 | (princ (format "M-x %s RET" definition))) |
423 | (if (> (length keys1) 0) | |
962ffad8 KS |
424 | (if remapped |
425 | (princ (format "%s is remapped to %s which is on %s" definition remapped keys1)) | |
290bfb66 | 426 | (princ (format "%s is on %s" definition keys1))) |
e88a2c59 | 427 | (princ (format "%s is not on any key" definition))))) |
54c0b967 RS |
428 | nil) |
429 | ||
788d62cf MB |
430 | (defun string-key-binding (key) |
431 | "Value is the binding of KEY in a string. | |
432 | If KEY is an event on a string, and that string has a `local-map' | |
433 | or `keymap' property, return the binding of KEY in the string's keymap." | |
434 | (let* ((defn nil) | |
435 | (start (when (vectorp key) | |
f03b7c7d GM |
436 | (if (memq (aref key 0) |
437 | '(mode-line header-line left-margin right-margin)) | |
788d62cf MB |
438 | (event-start (aref key 1)) |
439 | (and (consp (aref key 0)) | |
440 | (event-start (aref key 0)))))) | |
441 | (string-info (and (consp start) (nth 4 start)))) | |
442 | (when string-info | |
443 | (let* ((string (car string-info)) | |
444 | (pos (cdr string-info)) | |
f03b7c7d | 445 | (local-map (and (>= pos 0) |
788d62cf MB |
446 | (< pos (length string)) |
447 | (or (get-text-property pos 'local-map string) | |
448 | (get-text-property pos 'keymap string))))) | |
449 | (setq defn (and local-map (lookup-key local-map key))))) | |
450 | defn)) | |
1a06eabd | 451 | |
02dfca16 SM |
452 | (defun help-key-description (key untranslated) |
453 | (let ((string (key-description key))) | |
454 | (if (or (not untranslated) (eq (aref untranslated 0) ?\e)) | |
455 | string | |
456 | (let ((otherstring (key-description untranslated))) | |
457 | (if (equal string otherstring) | |
458 | string | |
459 | (format "%s (translated from %s)" string otherstring)))))) | |
460 | ||
461 | (defun describe-key-briefly (key &optional insert untranslated) | |
788d62cf | 462 | "Print the name of the function KEY invokes. KEY is a string. |
02dfca16 SM |
463 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer. |
464 | If non-nil UNTRANSLATED is a vector of the untranslated events. | |
465 | It can also be a number in which case the untranslated events from | |
466 | the last key hit are used." | |
467 | (interactive "kDescribe key briefly: \nP\np") | |
468 | (if (numberp untranslated) | |
469 | (setq untranslated (this-single-command-raw-keys))) | |
400a1b1f | 470 | (save-excursion |
788d62cf MB |
471 | (let ((modifiers (event-modifiers (aref key 0))) |
472 | (standard-output (if insert (current-buffer) t)) | |
473 | window position) | |
474 | ;; For a mouse button event, go to the button it applies to | |
475 | ;; to get the right key bindings. And go to the right place | |
476 | ;; in case the keymap depends on where you clicked. | |
477 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
478 | (memq 'drag modifiers)) | |
479 | (setq window (posn-window (event-start (aref key 0))) | |
480 | position (posn-point (event-start (aref key 0))))) | |
481 | (if (windowp window) | |
482 | (progn | |
483 | (set-buffer (window-buffer window)) | |
484 | (goto-char position))) | |
485 | ;; Ok, now look up the key and name the command. | |
486 | (let ((defn (or (string-key-binding key) | |
487 | (key-binding key))) | |
02dfca16 | 488 | (key-desc (help-key-description key untranslated))) |
d61ac4a6 | 489 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
788d62cf | 490 | (princ (format "%s is undefined" key-desc)) |
80d553d4 RS |
491 | (princ (format (if (windowp window) |
492 | "%s at that spot runs the command %s" | |
493 | "%s runs the command %s") | |
788d62cf MB |
494 | key-desc |
495 | (if (symbolp defn) defn (prin1-to-string defn))))))))) | |
96ede6b2 | 496 | |
788d62cf | 497 | |
02dfca16 | 498 | (defun describe-key (key &optional untranslated) |
a0d32c10 RS |
499 | "Display documentation of the function invoked by KEY. |
500 | KEY should be a key sequence--when calling from a program, | |
02dfca16 SM |
501 | pass a string or a vector. |
502 | If non-nil UNTRANSLATED is a vector of the untranslated events. | |
503 | It can also be a number in which case the untranslated events from | |
504 | the last key hit are used." | |
505 | (interactive "kDescribe key: \np") | |
506 | (if (numberp untranslated) | |
507 | (setq untranslated (this-single-command-raw-keys))) | |
788d62cf MB |
508 | (save-excursion |
509 | (let ((modifiers (event-modifiers (aref key 0))) | |
510 | window position) | |
511 | ;; For a mouse button event, go to the button it applies to | |
512 | ;; to get the right key bindings. And go to the right place | |
513 | ;; in case the keymap depends on where you clicked. | |
514 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
515 | (memq 'drag modifiers)) | |
516 | (setq window (posn-window (event-start (aref key 0))) | |
517 | position (posn-point (event-start (aref key 0))))) | |
94ea540b | 518 | (when (windowp window) |
788d62cf | 519 | (set-buffer (window-buffer window)) |
94ea540b | 520 | (goto-char position)) |
788d62cf | 521 | (let ((defn (or (string-key-binding key) (key-binding key)))) |
d61ac4a6 | 522 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
02dfca16 | 523 | (message "%s is undefined" (help-key-description key untranslated)) |
3e5929af SM |
524 | (help-setup-xref (list #'describe-function defn) (interactive-p)) |
525 | (with-output-to-temp-buffer (help-buffer) | |
02dfca16 | 526 | (princ (help-key-description key untranslated)) |
788d62cf MB |
527 | (if (windowp window) |
528 | (princ " at that spot")) | |
529 | (princ " runs the command ") | |
530 | (prin1 defn) | |
531 | (princ "\n which is ") | |
3e5929af | 532 | (describe-function-1 defn) |
788d62cf | 533 | (print-help-return-message))))))) |
400a1b1f | 534 | |
400a1b1f | 535 | \f |
788d62cf MB |
536 | (defun describe-mode (&optional buffer) |
537 | "Display documentation of current major mode and minor modes. | |
538 | The major mode description comes first, followed by the minor modes, | |
539 | each on a separate page. | |
540 | For this to work correctly for a minor mode, the mode's indicator variable | |
541 | \(listed in `minor-mode-alist') must also be a function whose documentation | |
542 | describes the minor mode." | |
400a1b1f | 543 | (interactive) |
9639be74 RS |
544 | (help-setup-xref (list #'describe-mode (or buffer (current-buffer))) |
545 | (interactive-p)) | |
546 | ;; For the sake of help-do-xref and help-xref-go-back, | |
547 | ;; don't switch buffers before calling `help-buffer'. | |
3e5929af | 548 | (with-output-to-temp-buffer (help-buffer) |
9639be74 RS |
549 | (save-excursion |
550 | (when buffer (set-buffer buffer)) | |
551 | (when minor-mode-alist | |
552 | (princ "The major mode is described first. | |
788d62cf | 553 | For minor modes, see following pages.\n\n")) |
9639be74 RS |
554 | (princ mode-name) |
555 | (princ " mode:\n") | |
556 | (princ (documentation major-mode)) | |
557 | (let ((minor-modes minor-mode-alist)) | |
558 | (while minor-modes | |
559 | (let* ((minor-mode (car (car minor-modes))) | |
560 | (indicator (car (cdr (car minor-modes))))) | |
561 | ;; Document a minor mode if it is listed in minor-mode-alist, | |
562 | ;; bound locally in this buffer, non-nil, and has a function | |
563 | ;; definition. | |
564 | (if (and (boundp minor-mode) | |
565 | (symbol-value minor-mode) | |
566 | (fboundp minor-mode)) | |
567 | (let ((pretty-minor-mode minor-mode)) | |
568 | (if (string-match "\\(-minor\\)?-mode\\'" | |
569 | (symbol-name minor-mode)) | |
570 | (setq pretty-minor-mode | |
571 | (capitalize | |
572 | (substring (symbol-name minor-mode) | |
573 | 0 (match-beginning 0))))) | |
574 | (while (and indicator (symbolp indicator) | |
575 | (boundp indicator) | |
576 | (not (eq indicator (symbol-value indicator)))) | |
577 | (setq indicator (symbol-value indicator))) | |
578 | (princ "\n\f\n") | |
579 | (princ (format "%s minor mode (%s):\n" | |
580 | pretty-minor-mode | |
581 | (if indicator | |
582 | (format "indicator%s" indicator) | |
583 | "no indicator"))) | |
584 | (princ (documentation minor-mode))))) | |
585 | (setq minor-modes (cdr minor-modes)))) | |
586 | (print-help-return-message)))) | |
400a1b1f | 587 | |
48ce3c22 RS |
588 | \f |
589 | ;;; Automatic resizing of temporary buffers. | |
590 | ||
4483ddc5 | 591 | (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2)) |
48ce3c22 RS |
592 | "*Maximum height of a window displaying a temporary buffer. |
593 | This is the maximum height (in text lines) which `resize-temp-buffer-window' | |
594 | will give to a window displaying a temporary buffer. | |
595 | It can also be a function which will be called with the object corresponding | |
596 | to the buffer to be displayed as argument and should return an integer | |
597 | positive number." | |
598 | :type '(choice integer function) | |
599 | :group 'help | |
600 | :version "20.4") | |
601 | ||
4e1ede6c SM |
602 | (define-minor-mode temp-buffer-resize-mode |
603 | "Toggle the mode which makes windows smaller for temporary buffers. | |
48ce3c22 RS |
604 | With prefix argument ARG, turn the resizing of windows displaying temporary |
605 | buffers on if ARG is positive or off otherwise. | |
4e1ede6c SM |
606 | This makes the window the right height for its contents, but never |
607 | more than `temp-buffer-max-height' nor less than `window-min-height'. | |
608 | This applies to `help', `apropos' and `completion' buffers, and some others." | |
b0fbf754 | 609 | :global t :group 'help |
4e1ede6c | 610 | (if temp-buffer-resize-mode |
57f43907 | 611 | ;; `help-make-xrefs' may add a `back' button and thus increase the |
4e1ede6c SM |
612 | ;; text size, so `resize-temp-buffer-window' must be run *after* it. |
613 | (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append) | |
8304a3bb | 614 | (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window))) |
48ce3c22 RS |
615 | |
616 | (defun resize-temp-buffer-window () | |
617 | "Resize the current window to fit its contents. | |
4483ddc5 | 618 | Will not make it higher than `temp-buffer-max-height' nor smaller than |
b2c85790 | 619 | `window-min-height'. Do nothing if it is the only window on its frame, if it |
48ce3c22 RS |
620 | is not as wide as the frame or if some of the window's contents are scrolled |
621 | out of view." | |
622 | (unless (or (one-window-p 'nomini) | |
623 | (not (pos-visible-in-window-p (point-min))) | |
624 | (/= (frame-width) (window-width))) | |
d9c30bdf MB |
625 | (fit-window-to-buffer |
626 | (selected-window) | |
627 | (if (functionp temp-buffer-max-height) | |
628 | (funcall temp-buffer-max-height (current-buffer)) | |
629 | temp-buffer-max-height)))) | |
48ce3c22 | 630 | |
5fbeea74 GM |
631 | ;; Provide this for the sake of define-minor-mode which generates |
632 | ;; defcustoms which require 'help'. | |
5fbeea74 GM |
633 | (provide 'help) |
634 | ||
1a06eabd | 635 | ;;; help.el ends here |