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