Commit | Line | Data |
---|---|---|
1a06eabd ER |
1 | ;;; help.el --- help commands for Emacs |
2 | ||
d733c5ec | 3 | ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc. |
3a801d0c | 4 | |
e5167999 | 5 | ;; Maintainer: FSF |
fd7fa35a | 6 | ;; Keywords: help, internal |
e5167999 | 7 | |
433ae6f6 RS |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
433ae6f6 RS |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
433ae6f6 | 24 | |
d9ecc911 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; This code implements GNU Emac's on-line help system, the one invoked by | |
28 | ;;`M-x help-for-help'. | |
29 | ||
e5167999 ER |
30 | ;;; Code: |
31 | ||
8aa3a187 RS |
32 | ;; Get the macro make-help-screen when this is compiled, |
33 | ;; or run interpreted, but not when the compiled code is loaded. | |
b1fe9304 | 34 | (eval-when-compile (require 'help-macro)) |
41b8542b | 35 | |
433ae6f6 RS |
36 | (defvar help-map (make-sparse-keymap) |
37 | "Keymap for characters following the Help key.") | |
38 | ||
afaa65e4 KH |
39 | (defvar help-mode-map (make-sparse-keymap) |
40 | "Keymap for help mode.") | |
41 | ||
e17d2fd1 | 42 | (define-key global-map (char-to-string help-char) 'help-command) |
0af3df1c RS |
43 | (define-key global-map [help] 'help-command) |
44 | (define-key global-map [f1] 'help-command) | |
433ae6f6 RS |
45 | (fset 'help-command help-map) |
46 | ||
e17d2fd1 | 47 | (define-key help-map (char-to-string help-char) 'help-for-help) |
0af3df1c RS |
48 | (define-key help-map [help] 'help-for-help) |
49 | (define-key help-map [f1] 'help-for-help) | |
433ae6f6 RS |
50 | (define-key help-map "?" 'help-for-help) |
51 | ||
52 | (define-key help-map "\C-c" 'describe-copying) | |
53 | (define-key help-map "\C-d" 'describe-distribution) | |
54 | (define-key help-map "\C-w" 'describe-no-warranty) | |
76766f2d | 55 | (define-key help-map "\C-p" 'describe-project) |
433ae6f6 RS |
56 | (define-key help-map "a" 'command-apropos) |
57 | ||
58 | (define-key help-map "b" 'describe-bindings) | |
59 | ||
60 | (define-key help-map "c" 'describe-key-briefly) | |
61 | (define-key help-map "k" 'describe-key) | |
62 | ||
63 | (define-key help-map "d" 'describe-function) | |
64 | (define-key help-map "f" 'describe-function) | |
65 | ||
7ee71cf1 RS |
66 | (define-key help-map "F" 'view-emacs-FAQ) |
67 | ||
433ae6f6 | 68 | (define-key help-map "i" 'info) |
e5d77022 JB |
69 | (define-key help-map "\C-f" 'Info-goto-emacs-command-node) |
70 | (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node) | |
433ae6f6 RS |
71 | |
72 | (define-key help-map "l" 'view-lossage) | |
73 | ||
74 | (define-key help-map "m" 'describe-mode) | |
75 | ||
76 | (define-key help-map "\C-n" 'view-emacs-news) | |
77 | (define-key help-map "n" 'view-emacs-news) | |
78 | ||
06b98c51 | 79 | (define-key help-map "p" 'finder-by-keyword) |
3e9c095d RS |
80 | (autoload 'finder-by-keyword "finder" |
81 | "Find packages matching a given keyword." t) | |
06b98c51 | 82 | |
433ae6f6 RS |
83 | (define-key help-map "s" 'describe-syntax) |
84 | ||
85 | (define-key help-map "t" 'help-with-tutorial) | |
86 | ||
87 | (define-key help-map "w" 'where-is) | |
88 | ||
89 | (define-key help-map "v" 'describe-variable) | |
90 | ||
2fc9d9f4 RS |
91 | (define-key help-map "q" 'help-quit) |
92 | ||
507fb916 SM |
93 | (defvar help-font-lock-keywords |
94 | (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]")) | |
95 | (list | |
96 | ;; | |
97 | ;; The symbol itself. | |
98 | (list (concat "\\`\\(" name-char "+\\)\\(:\\)?") | |
99 | '(1 (if (match-beginning 2) | |
100 | font-lock-function-name-face | |
101 | font-lock-variable-name-face) | |
102 | nil t)) | |
103 | ;; | |
104 | ;; Words inside `' which tend to be symbol names. | |
105 | (list (concat "`\\(" sym-char sym-char "+\\)'") | |
106 | 1 'font-lock-reference-face t) | |
107 | ;; | |
108 | ;; CLisp `:' keywords as references. | |
3d42d542 | 109 | (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t))) |
507fb916 SM |
110 | "Default expressions to highlight in Help mode.") |
111 | ||
afaa65e4 KH |
112 | (defun help-mode () |
113 | "Major mode for viewing help text. | |
114 | Entry to this mode runs the normal hook `help-mode-hook'. | |
115 | Commands: | |
116 | \\{help-mode-map}" | |
117 | (interactive) | |
118 | (kill-all-local-variables) | |
119 | (use-local-map help-mode-map) | |
120 | (setq mode-name "Help") | |
121 | (setq major-mode 'help-mode) | |
507fb916 SM |
122 | (make-local-variable 'font-lock-defaults) |
123 | (setq font-lock-defaults '(help-font-lock-keywords)) | |
42499979 | 124 | (view-mode) |
afaa65e4 KH |
125 | (run-hooks 'help-mode-hook)) |
126 | ||
2fc9d9f4 RS |
127 | (defun help-quit () |
128 | (interactive) | |
129 | nil) | |
130 | ||
433ae6f6 RS |
131 | (defun help-with-tutorial () |
132 | "Select the Emacs learn-by-doing tutorial." | |
133 | (interactive) | |
134 | (let ((file (expand-file-name "~/TUTORIAL"))) | |
135 | (delete-other-windows) | |
136 | (if (get-file-buffer file) | |
137 | (switch-to-buffer (get-file-buffer file)) | |
138 | (switch-to-buffer (create-file-buffer file)) | |
139 | (setq buffer-file-name file) | |
140 | (setq default-directory (expand-file-name "~/")) | |
79058860 | 141 | (setq buffer-auto-save-file-name nil) |
1e6dacf6 | 142 | (insert-file-contents (expand-file-name "TUTORIAL" data-directory)) |
433ae6f6 RS |
143 | (goto-char (point-min)) |
144 | (search-forward "\n<<") | |
145 | (beginning-of-line) | |
146 | (delete-region (point) (progn (end-of-line) (point))) | |
857a1de6 | 147 | (let ((n (- (window-height (selected-window)) |
433ae6f6 | 148 | (count-lines (point-min) (point)) |
857a1de6 | 149 | 6))) |
d0da2301 | 150 | (if (< n 12) |
857a1de6 KH |
151 | (newline n) |
152 | ;; Some people get confused by the large gap. | |
153 | (newline (/ n 2)) | |
154 | (insert "[Middle of page left blank for didactic purposes. " | |
155 | "Text continues below]") | |
156 | (newline (- n (/ n 2))))) | |
433ae6f6 RS |
157 | (goto-char (point-min)) |
158 | (set-buffer-modified-p nil)))) | |
159 | ||
160 | (defun describe-key-briefly (key) | |
161 | "Print the name of the function KEY invokes. KEY is a string." | |
162 | (interactive "kDescribe key briefly: ") | |
5f296b78 RS |
163 | ;; If this key seq ends with a down event, discard the |
164 | ;; following click or drag event. Otherwise that would | |
165 | ;; erase the message. | |
166 | (let ((type (aref key (1- (length key))))) | |
167 | (if (listp type) (setq type (car type))) | |
168 | (and (symbolp type) | |
169 | (memq 'down (event-modifiers type)) | |
fca4b775 | 170 | (read-event))) |
fc558e4d RS |
171 | (save-excursion |
172 | (let ((modifiers (event-modifiers (aref key 0))) | |
173 | window position) | |
174 | ;; For a mouse button event, go to the button it applies to | |
175 | ;; to get the right key bindings. And go to the right place | |
176 | ;; in case the keymap depends on where you clicked. | |
177 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
178 | (memq 'drag modifiers)) | |
179 | (setq window (posn-window (event-start (aref key 0))) | |
180 | position (posn-point (event-start (aref key 0))))) | |
181 | (if (windowp window) | |
182 | (progn | |
183 | (set-buffer (window-buffer window)) | |
184 | (goto-char position))) | |
185 | ;; Ok, now look up the key and name the command. | |
186 | (let ((defn (key-binding key))) | |
187 | (if (or (null defn) (integerp defn)) | |
188 | (message "%s is undefined" (key-description key)) | |
189 | (message (if (windowp window) | |
190 | "%s at that spot runs the command %s" | |
191 | "%s runs the command %s") | |
192 | (key-description key) | |
193 | (if (symbolp defn) defn (prin1-to-string defn)))))))) | |
433ae6f6 RS |
194 | |
195 | (defun print-help-return-message (&optional function) | |
196 | "Display or return message saying how to restore windows after help command. | |
197 | Computes a message and applies the optional argument FUNCTION to it. | |
198 | If FUNCTION is nil, applies `message' to it, thus printing it." | |
199 | (and (not (get-buffer-window standard-output)) | |
d536293f RS |
200 | (let ((first-message |
201 | (cond ((or (member (buffer-name standard-output) | |
202 | special-display-buffer-names) | |
34b0ef48 RS |
203 | (assoc (buffer-name standard-output) |
204 | special-display-buffer-names) | |
d536293f RS |
205 | (let (found |
206 | (tail special-display-regexps) | |
207 | (name (buffer-name standard-output))) | |
208 | (while (and tail (not found)) | |
afaa65e4 | 209 | (if (or (and (consp (car tail)) |
34b0ef48 RS |
210 | (string-match (car (car tail)) name)) |
211 | (and (stringp (car tail)) | |
212 | (string-match (car tail) name))) | |
d536293f RS |
213 | (setq found t)) |
214 | (setq tail (cdr tail))) | |
215 | found)) | |
216 | ;; If the help output buffer is a special display buffer, | |
217 | ;; don't say anything about how to get rid of it. | |
218 | ;; First of all, the user will do that with the window | |
219 | ;; manager, not with Emacs. | |
220 | ;; Secondly, the buffer has not been displayed yet, | |
221 | ;; so we don't know whether its frame will be selected. | |
222 | ;; Even the message about scrolling the help | |
223 | ;; might be wrong, but it seems worth showing it anyway. | |
224 | nil) | |
225 | ((not (one-window-p t)) | |
226 | "Type \\[switch-to-buffer-other-window] RET to restore the other window.") | |
227 | (pop-up-windows | |
228 | "Type \\[delete-other-windows] to remove help window.") | |
229 | (t | |
230 | "Type \\[switch-to-buffer] RET to remove help window.")))) | |
231 | (funcall (or function 'message) | |
232 | (concat | |
233 | (if first-message | |
234 | (substitute-command-keys first-message) | |
235 | "") | |
236 | (if first-message " " "") | |
125a8d70 RS |
237 | ;; If the help buffer will go in a separate frame, |
238 | ;; it's no use mentioning a command to scroll, so don't. | |
6e7f5182 | 239 | (if (or (member (buffer-name standard-output) |
125a8d70 | 240 | special-display-buffer-names) |
5cabce21 RS |
241 | (assoc (buffer-name standard-output) |
242 | special-display-buffer-names) | |
6e7f5182 | 243 | (memq t (mapcar '(lambda (elt) |
5cabce21 RS |
244 | (if (consp elt) |
245 | (setq elt (car elt))) | |
6e7f5182 | 246 | (string-match elt (buffer-name standard-output))) |
125a8d70 RS |
247 | special-display-regexps))) |
248 | nil | |
249 | (if (or (member (buffer-name standard-output) | |
250 | same-window-buffer-names) | |
5cabce21 RS |
251 | (assoc (buffer-name standard-output) |
252 | same-window-buffer-names) | |
125a8d70 | 253 | (memq t (mapcar '(lambda (elt) |
5cabce21 RS |
254 | (if (consp elt) |
255 | (setq elt (car elt))) | |
125a8d70 RS |
256 | (string-match elt (buffer-name standard-output))) |
257 | same-window-regexps))) | |
258 | ;; Say how to scroll this window. | |
259 | (substitute-command-keys | |
260 | "\\[scroll-up] to scroll the help.") | |
261 | ;; Say how to scroll some other window. | |
6e7f5182 | 262 | (substitute-command-keys |
125a8d70 | 263 | "\\[scroll-other-window] to scroll the help.")))))))) |
433ae6f6 RS |
264 | |
265 | (defun describe-key (key) | |
266 | "Display documentation of the function invoked by KEY. KEY is a string." | |
267 | (interactive "kDescribe key: ") | |
5f296b78 RS |
268 | ;; If this key seq ends with a down event, discard the |
269 | ;; following click or drag event. Otherwise that would | |
270 | ;; erase the message. | |
271 | (let ((type (aref key (1- (length key))))) | |
272 | (if (listp type) (setq type (car type))) | |
273 | (and (symbolp type) | |
274 | (memq 'down (event-modifiers type)) | |
275 | (read-event))) | |
fc558e4d RS |
276 | (save-excursion |
277 | (let ((modifiers (event-modifiers (aref key 0))) | |
278 | window position) | |
279 | ;; For a mouse button event, go to the button it applies to | |
280 | ;; to get the right key bindings. And go to the right place | |
281 | ;; in case the keymap depends on where you clicked. | |
282 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
283 | (memq 'drag modifiers)) | |
284 | (setq window (posn-window (event-start (aref key 0))) | |
285 | position (posn-point (event-start (aref key 0))))) | |
286 | (if (windowp window) | |
287 | (progn | |
288 | (set-buffer (window-buffer window)) | |
289 | (goto-char position))) | |
290 | (let ((defn (key-binding key))) | |
291 | (if (or (null defn) (integerp defn)) | |
292 | (message "%s is undefined" (key-description key)) | |
293 | (with-output-to-temp-buffer "*Help*" | |
294 | (princ (key-description key)) | |
295 | (if (windowp window) | |
296 | (princ " at that spot")) | |
297 | (princ " runs the command ") | |
298 | (prin1 defn) | |
299 | (princ ":\n") | |
fb3fc9b8 RS |
300 | (let ((doc (documentation defn))) |
301 | (if doc | |
302 | (progn (terpri) | |
303 | (princ doc)) | |
304 | (princ "not documented"))) | |
fc558e4d RS |
305 | (save-excursion |
306 | (set-buffer standard-output) | |
307 | (help-mode)) | |
308 | (print-help-return-message))))))) | |
433ae6f6 | 309 | |
ad023904 RS |
310 | (defun describe-mode () |
311 | "Display documentation of current major mode and minor modes. | |
433ae6f6 | 312 | For this to work correctly for a minor mode, the mode's indicator variable |
61c6b658 | 313 | \(listed in `minor-mode-alist') must also be a function whose documentation |
433ae6f6 | 314 | describes the minor mode." |
7192540b | 315 | (interactive) |
433ae6f6 | 316 | (with-output-to-temp-buffer "*Help*" |
7192540b | 317 | (let ((minor-modes minor-mode-alist) |
83f86594 | 318 | (first t) |
7192540b RS |
319 | (locals (buffer-local-variables))) |
320 | (while minor-modes | |
321 | (let* ((minor-mode (car (car minor-modes))) | |
322 | (indicator (car (cdr (car minor-modes)))) | |
323 | (local-binding (assq minor-mode locals))) | |
324 | ;; Document a minor mode if it is listed in minor-mode-alist, | |
325 | ;; bound locally in this buffer, non-nil, and has a function | |
326 | ;; definition. | |
327 | (if (and local-binding | |
328 | (cdr local-binding) | |
329 | (fboundp minor-mode)) | |
330 | (let ((pretty-minor-mode minor-mode)) | |
331 | (if (string-match "-mode$" (symbol-name minor-mode)) | |
332 | (setq pretty-minor-mode | |
333 | (capitalize | |
334 | (substring (symbol-name minor-mode) | |
335 | 0 (match-beginning 0))))) | |
336 | (while (and indicator (symbolp indicator)) | |
337 | (setq indicator (symbol-value indicator))) | |
83f86594 RS |
338 | (if first |
339 | (princ "The minor modes are described first, | |
340 | followed by the major mode, which is described on the last page.\n\f\n")) | |
341 | (setq first nil) | |
2ef581f3 RS |
342 | (princ (format "%s minor mode (%s):\n" |
343 | pretty-minor-mode | |
344 | (if indicator | |
345 | (format "indicator%s" indicator) | |
346 | "no indicator"))) | |
7192540b | 347 | (princ (documentation minor-mode)) |
83f86594 | 348 | (princ "\n\f\n")))) |
7192540b | 349 | (setq minor-modes (cdr minor-modes)))) |
433ae6f6 | 350 | (princ mode-name) |
ad023904 | 351 | (princ " mode:\n") |
433ae6f6 | 352 | (princ (documentation major-mode)) |
c46bbd92 KH |
353 | (save-excursion |
354 | (set-buffer standard-output) | |
355 | (help-mode)) | |
433ae6f6 RS |
356 | (print-help-return-message))) |
357 | ||
358 | ;; So keyboard macro definitions are documented correctly | |
359 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
360 | ||
361 | (defun describe-distribution () | |
362 | "Display info on how to obtain the latest version of GNU Emacs." | |
363 | (interactive) | |
364 | (find-file-read-only | |
1e6dacf6 | 365 | (expand-file-name "DISTRIB" data-directory))) |
433ae6f6 RS |
366 | |
367 | (defun describe-copying () | |
368 | "Display info on how you may redistribute copies of GNU Emacs." | |
369 | (interactive) | |
370 | (find-file-read-only | |
1e6dacf6 | 371 | (expand-file-name "COPYING" data-directory)) |
433ae6f6 RS |
372 | (goto-char (point-min))) |
373 | ||
76766f2d RS |
374 | (defun describe-project () |
375 | "Display info on the GNU project." | |
376 | (interactive) | |
377 | (find-file-read-only | |
378 | (expand-file-name "GNU" data-directory)) | |
379 | (goto-char (point-min))) | |
380 | ||
433ae6f6 RS |
381 | (defun describe-no-warranty () |
382 | "Display info on all the kinds of warranty Emacs does NOT have." | |
383 | (interactive) | |
384 | (describe-copying) | |
385 | (let (case-fold-search) | |
386 | (search-forward "NO WARRANTY") | |
387 | (recenter 0))) | |
388 | ||
61c6b658 | 389 | (defun describe-prefix-bindings () |
c7cba9cb RS |
390 | "Describe the bindings of the prefix used to reach this command. |
391 | The prefix described consists of all but the last event | |
392 | of the key sequence that ran this command." | |
61c6b658 | 393 | (interactive) |
ccc06dcc KH |
394 | (let* ((key (this-command-keys))) |
395 | (describe-bindings | |
396 | (if (stringp key) | |
397 | (substring key 0 (1- (length key))) | |
398 | (let ((prefix (make-vector (1- (length key)) nil)) | |
399 | (i 0)) | |
400 | (while (< i (length prefix)) | |
401 | (aset prefix i (aref key i)) | |
402 | (setq i (1+ i))) | |
403 | prefix))))) | |
c7cba9cb RS |
404 | ;; Make C-h after a prefix, when not specifically bound, |
405 | ;; run describe-prefix-bindings. | |
61c6b658 RS |
406 | (setq prefix-help-command 'describe-prefix-bindings) |
407 | ||
433ae6f6 RS |
408 | (defun view-emacs-news () |
409 | "Display info on recent changes to Emacs." | |
410 | (interactive) | |
1e6dacf6 | 411 | (find-file-read-only (expand-file-name "NEWS" data-directory))) |
433ae6f6 | 412 | |
7ee71cf1 RS |
413 | (defun view-emacs-FAQ () |
414 | "Display the Emacs Frequently Asked Questions (FAQ) file." | |
415 | (interactive) | |
416 | (find-file-read-only (expand-file-name "FAQ" data-directory))) | |
417 | ||
433ae6f6 RS |
418 | (defun view-lossage () |
419 | "Display last 100 input keystrokes." | |
420 | (interactive) | |
421 | (with-output-to-temp-buffer "*Help*" | |
298a7c8c RS |
422 | (princ (mapconcat (function (lambda (key) |
423 | (if (or (integerp key) | |
424 | (symbolp key) | |
425 | (listp key)) | |
426 | (single-key-description key) | |
427 | (prin1-to-string key nil)))) | |
428 | (recent-keys) | |
429 | " ")) | |
433ae6f6 RS |
430 | (save-excursion |
431 | (set-buffer standard-output) | |
432 | (goto-char (point-min)) | |
433 | (while (progn (move-to-column 50) (not (eobp))) | |
434 | (search-forward " " nil t) | |
c46bbd92 KH |
435 | (insert "\n")) |
436 | (help-mode)) | |
433ae6f6 RS |
437 | (print-help-return-message))) |
438 | ||
2fc9d9f4 | 439 | (defalias 'help 'help-for-help) |
41b8542b | 440 | (make-help-screen help-for-help |
224d56a3 | 441 | "a b c f C-f i k C-k l m n p s t v w C-c C-d C-n C-w, or ? for more help:" |
76766f2d | 442 | "You have typed \\[help-command], the help character. Type a Help option: |
efcce2d2 | 443 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) |
433ae6f6 | 444 | |
21ee8c42 RM |
445 | a command-apropos. Give a substring, and see a list of commands |
446 | (functions interactively callable) that contain | |
447 | that substring. See also the apropos command. | |
af6a9de9 RS |
448 | b describe-bindings. Display table of all key bindings. |
449 | c describe-key-briefly. Type a command key sequence; | |
21ee8c42 | 450 | it prints the function name that sequence runs. |
af6a9de9 | 451 | f describe-function. Type a function name and get documentation of it. |
21ee8c42 RM |
452 | C-f Info-goto-emacs-command-node. Type a function name; |
453 | it takes you to the Info node for that command. | |
7ee71cf1 | 454 | F view-emacs-FAQ. Shows emacs frequently asked questions file. |
af6a9de9 RS |
455 | i info. The info documentation reader. |
456 | k describe-key. Type a command key sequence; | |
21ee8c42 RM |
457 | it displays the full documentation. |
458 | C-k Info-goto-emacs-key-command-node. Type a command key sequence; | |
459 | it takes you to the Info node for the command bound to that key. | |
af6a9de9 RS |
460 | l view-lossage. Shows last 100 characters you typed. |
461 | m describe-mode. Print documentation of current major mode, | |
21ee8c42 | 462 | which describes the commands peculiar to it. |
af6a9de9 RS |
463 | n view-emacs-news. Shows emacs news file. |
464 | p finder-by-keyword. Find packages matching a given topic keyword. | |
465 | s describe-syntax. Display contents of syntax table, plus explanations | |
466 | t help-with-tutorial. Select the Emacs learn-by-doing tutorial. | |
467 | v describe-variable. Type name of a variable; | |
21ee8c42 | 468 | it displays the variable's documentation and value. |
af6a9de9 | 469 | w where-is. Type command name; it prints which keystrokes |
21ee8c42 | 470 | invoke that command. |
433ae6f6 RS |
471 | C-c print Emacs copying permission (General Public License). |
472 | C-d print Emacs ordering information. | |
473 | C-n print news of recent Emacs changes. | |
76766f2d | 474 | C-p print information about the GNU project. |
433ae6f6 | 475 | C-w print information on absence of warranty for GNU Emacs." |
41b8542b | 476 | help-map) |
433ae6f6 RS |
477 | |
478 | ;; Return a function which is called by the list containing point. | |
479 | ;; If that gives no function, return a function whose name is around point. | |
480 | ;; If that doesn't give a function, return nil. | |
481 | (defun function-called-at-point () | |
482 | (or (condition-case () | |
483 | (save-excursion | |
484 | (save-restriction | |
485 | (narrow-to-region (max (point-min) (- (point) 1000)) (point-max)) | |
486 | (backward-up-list 1) | |
487 | (forward-char 1) | |
488 | (let (obj) | |
489 | (setq obj (read (current-buffer))) | |
490 | (and (symbolp obj) (fboundp obj) obj)))) | |
491 | (error nil)) | |
492 | (condition-case () | |
914a48d0 RS |
493 | (let ((stab (syntax-table))) |
494 | (unwind-protect | |
495 | (save-excursion | |
496 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
497 | (or (not (zerop (skip-syntax-backward "_w"))) | |
498 | (eq (char-syntax (following-char)) ?w) | |
499 | (eq (char-syntax (following-char)) ?_) | |
500 | (forward-sexp -1)) | |
501 | (skip-chars-forward "'") | |
502 | (let ((obj (read (current-buffer)))) | |
503 | (and (symbolp obj) (fboundp obj) obj))) | |
504 | (set-syntax-table stab))) | |
433ae6f6 RS |
505 | (error nil)))) |
506 | ||
ca5ed196 RS |
507 | (defun describe-function-find-file (function) |
508 | (let ((files load-history) | |
509 | file functions) | |
510 | (while files | |
511 | (if (memq function (cdr (car files))) | |
512 | (setq file (car (car files)) files nil)) | |
513 | (setq files (cdr files))) | |
514 | file)) | |
515 | ||
433ae6f6 RS |
516 | (defun describe-function (function) |
517 | "Display the full documentation of FUNCTION (a symbol)." | |
518 | (interactive | |
519 | (let ((fn (function-called-at-point)) | |
520 | (enable-recursive-minibuffers t) | |
521 | val) | |
522 | (setq val (completing-read (if fn | |
523 | (format "Describe function (default %s): " fn) | |
524 | "Describe function: ") | |
525 | obarray 'fboundp t)) | |
526 | (list (if (equal val "") | |
527 | fn (intern val))))) | |
528 | (with-output-to-temp-buffer "*Help*" | |
529 | (prin1 function) | |
530 | (princ ": ") | |
531 | (let* ((def (symbol-function function)) | |
ad3ec252 | 532 | file-name |
433ae6f6 | 533 | (beg (if (commandp def) "an interactive " "a "))) |
7f26b180 RM |
534 | (princ (cond ((or (stringp def) |
535 | (vectorp def)) | |
6f6bfb85 | 536 | "a keyboard macro") |
433ae6f6 | 537 | ((subrp def) |
6f6bfb85 | 538 | (concat beg "built-in function")) |
dbc4e1c1 | 539 | ((byte-code-function-p def) |
6f6bfb85 | 540 | (concat beg "compiled Lisp function")) |
433ae6f6 | 541 | ((symbolp def) |
6f6bfb85 | 542 | (format "alias for `%s'" def)) |
433ae6f6 | 543 | ((eq (car-safe def) 'lambda) |
6f6bfb85 | 544 | (concat beg "Lisp function")) |
433ae6f6 | 545 | ((eq (car-safe def) 'macro) |
6f6bfb85 | 546 | "a Lisp macro") |
433ae6f6 | 547 | ((eq (car-safe def) 'mocklisp) |
6f6bfb85 | 548 | "a mocklisp function") |
433ae6f6 | 549 | ((eq (car-safe def) 'autoload) |
ad3ec252 | 550 | (setq file-name (nth 1 def)) |
6f6bfb85 | 551 | (format "%s autoloaded Lisp %s" |
762435dd | 552 | (if (commandp def) "an interactive" "an") |
433ae6f6 | 553 | (if (nth 4 def) "macro" "function") |
ab67260b | 554 | )) |
433ae6f6 | 555 | (t ""))) |
ad3ec252 RS |
556 | (or file-name |
557 | (setq file-name (describe-function-find-file function))) | |
558 | (if file-name | |
559 | (progn | |
560 | (princ " in `") | |
561 | ;; We used to add .el to the file name, | |
562 | ;; but that's completely wrong when the user used load-file. | |
563 | (princ file-name) | |
564 | (princ "'"))) | |
6f6bfb85 | 565 | (princ ".") |
4591cb90 | 566 | (terpri) |
7f26b180 RM |
567 | (let ((arglist (cond ((byte-code-function-p def) |
568 | (car (append def nil))) | |
569 | ((eq (car-safe def) 'lambda) | |
570 | (nth 1 def)) | |
571 | (t t)))) | |
572 | (if (listp arglist) | |
573 | (progn | |
574 | (princ (cons function | |
575 | (mapcar (lambda (arg) | |
576 | (if (memq arg '(&optional &rest)) | |
577 | arg | |
578 | (intern (upcase (symbol-name arg))))) | |
579 | arglist))) | |
580 | (terpri)))) | |
fb3fc9b8 RS |
581 | (let ((doc (documentation function))) |
582 | (if doc | |
583 | (progn (terpri) | |
584 | (princ doc)) | |
585 | (princ "not documented")))) | |
433ae6f6 | 586 | (print-help-return-message) |
c46bbd92 KH |
587 | (save-excursion |
588 | (set-buffer standard-output) | |
589 | (help-mode) | |
590 | ;; Return the text we displayed. | |
591 | (buffer-string)))) | |
433ae6f6 RS |
592 | |
593 | (defun variable-at-point () | |
594 | (condition-case () | |
914a48d0 RS |
595 | (let ((stab (syntax-table))) |
596 | (unwind-protect | |
597 | (save-excursion | |
598 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
599 | (or (not (zerop (skip-syntax-backward "_w"))) | |
600 | (eq (char-syntax (following-char)) ?w) | |
601 | (eq (char-syntax (following-char)) ?_) | |
602 | (forward-sexp -1)) | |
603 | (skip-chars-forward "'") | |
604 | (let ((obj (read (current-buffer)))) | |
605 | (and (symbolp obj) (boundp obj) obj))) | |
606 | (set-syntax-table stab))) | |
433ae6f6 RS |
607 | (error nil))) |
608 | ||
609 | (defun describe-variable (variable) | |
610 | "Display the full documentation of VARIABLE (a symbol). | |
611 | Returns the documentation as a string, also." | |
612 | (interactive | |
613 | (let ((v (variable-at-point)) | |
614 | (enable-recursive-minibuffers t) | |
615 | val) | |
616 | (setq val (completing-read (if v | |
617 | (format "Describe variable (default %s): " v) | |
618 | "Describe variable: ") | |
619 | obarray 'boundp t)) | |
620 | (list (if (equal val "") | |
621 | v (intern val))))) | |
622 | (with-output-to-temp-buffer "*Help*" | |
623 | (prin1 variable) | |
433ae6f6 | 624 | (if (not (boundp variable)) |
3e483e01 RS |
625 | (princ " is void") |
626 | (princ "'s value is ") | |
433ae6f6 | 627 | (prin1 (symbol-value variable))) |
7e76ae23 | 628 | (terpri) |
b872e1da | 629 | (if (local-variable-p variable) |
3e483e01 RS |
630 | (progn |
631 | (princ (format "Local in buffer %s; " (buffer-name))) | |
632 | (if (not (default-boundp variable)) | |
633 | (princ "globally void") | |
634 | (princ "global value is ") | |
635 | (prin1 (default-value variable))) | |
636 | (terpri))) | |
7e76ae23 | 637 | (terpri) |
433ae6f6 RS |
638 | (princ "Documentation:") |
639 | (terpri) | |
640 | (let ((doc (documentation-property variable 'variable-documentation))) | |
c8f98e6d | 641 | (princ (or doc "not documented as a variable."))) |
433ae6f6 | 642 | (print-help-return-message) |
c46bbd92 KH |
643 | (save-excursion |
644 | (set-buffer standard-output) | |
645 | (help-mode) | |
646 | ;; Return the text we displayed. | |
647 | (buffer-string)))) | |
433ae6f6 | 648 | |
54c0b967 RS |
649 | (defun where-is (definition) |
650 | "Print message listing key sequences that invoke specified command. | |
651 | Argument is a command definition, usually a symbol with a function definition." | |
652 | (interactive | |
653 | (let ((fn (function-called-at-point)) | |
654 | (enable-recursive-minibuffers t) | |
655 | val) | |
656 | (setq val (completing-read (if fn | |
657 | (format "Where is command (default %s): " fn) | |
658 | "Where is command: ") | |
659 | obarray 'fboundp t)) | |
660 | (list (if (equal val "") | |
661 | fn (intern val))))) | |
662 | (let* ((keys (where-is-internal definition overriding-local-map nil nil)) | |
663 | (keys1 (mapconcat 'key-description keys ", "))) | |
664 | (if (> (length keys1) 0) | |
665 | (message "%s is on %s" definition keys1) | |
666 | (message "%s is not on any key" definition))) | |
667 | nil) | |
668 | ||
433ae6f6 RS |
669 | (defun locate-library (library &optional nosuffix) |
670 | "Show the full path name of Emacs library LIBRARY. | |
671 | This command searches the directories in `load-path' like `M-x load-library' | |
672 | to find the file that `M-x load-library RET LIBRARY RET' would load. | |
673 | Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el' | |
674 | to the specified name LIBRARY (a la calling `load' instead of `load-library')." | |
675 | (interactive "sLocate library: ") | |
676 | (catch 'answer | |
677 | (mapcar | |
678 | '(lambda (dir) | |
679 | (mapcar | |
680 | '(lambda (suf) | |
681 | (let ((try (expand-file-name (concat library suf) dir))) | |
682 | (and (file-readable-p try) | |
683 | (null (file-directory-p try)) | |
684 | (progn | |
685 | (message "Library is file %s" try) | |
686 | (throw 'answer try))))) | |
687 | (if nosuffix '("") '(".elc" ".el" "")))) | |
688 | load-path) | |
689 | (message "No library %s in search path" library) | |
690 | nil)) | |
1a06eabd ER |
691 | |
692 | ;;; help.el ends here |