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 | ||
a1c9f209 | 27 | ;; This code implements GNU Emacs' on-line help system, the one invoked by |
d9ecc911 ER |
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 | 93 | (defvar help-font-lock-keywords |
0e0ee795 RS |
94 | (eval-when-compile |
95 | (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]")) | |
96 | (list | |
97 | ;; | |
98 | ;; The symbol itself. | |
99 | (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)") | |
8743d4cb RS |
100 | '(1 (if (match-beginning 3) |
101 | font-lock-function-name-face | |
b8077811 | 102 | font-lock-variable-name-face))) |
0e0ee795 RS |
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. | |
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 | 122 | (make-local-variable 'font-lock-defaults) |
dd7d85ba | 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 | 200 | (let ((first-message |
a1c9f209 | 201 | (cond ((special-display-p (buffer-name standard-output)) |
d536293f RS |
202 | ;; If the help output buffer is a special display buffer, |
203 | ;; don't say anything about how to get rid of it. | |
204 | ;; First of all, the user will do that with the window | |
205 | ;; manager, not with Emacs. | |
206 | ;; Secondly, the buffer has not been displayed yet, | |
207 | ;; so we don't know whether its frame will be selected. | |
d536293f RS |
208 | nil) |
209 | ((not (one-window-p t)) | |
210 | "Type \\[switch-to-buffer-other-window] RET to restore the other window.") | |
211 | (pop-up-windows | |
212 | "Type \\[delete-other-windows] to remove help window.") | |
213 | (t | |
214 | "Type \\[switch-to-buffer] RET to remove help window.")))) | |
215 | (funcall (or function 'message) | |
216 | (concat | |
217 | (if first-message | |
218 | (substitute-command-keys first-message) | |
219 | "") | |
220 | (if first-message " " "") | |
125a8d70 RS |
221 | ;; If the help buffer will go in a separate frame, |
222 | ;; it's no use mentioning a command to scroll, so don't. | |
a1c9f209 | 223 | (if (special-display-p (buffer-name standard-output)) |
125a8d70 | 224 | nil |
a1c9f209 | 225 | (if (same-window-p (buffer-name standard-output)) |
125a8d70 RS |
226 | ;; Say how to scroll this window. |
227 | (substitute-command-keys | |
228 | "\\[scroll-up] to scroll the help.") | |
229 | ;; Say how to scroll some other window. | |
6e7f5182 | 230 | (substitute-command-keys |
125a8d70 | 231 | "\\[scroll-other-window] to scroll the help.")))))))) |
433ae6f6 RS |
232 | |
233 | (defun describe-key (key) | |
234 | "Display documentation of the function invoked by KEY. KEY is a string." | |
235 | (interactive "kDescribe key: ") | |
5f296b78 RS |
236 | ;; If this key seq ends with a down event, discard the |
237 | ;; following click or drag event. Otherwise that would | |
238 | ;; erase the message. | |
239 | (let ((type (aref key (1- (length key))))) | |
240 | (if (listp type) (setq type (car type))) | |
241 | (and (symbolp type) | |
242 | (memq 'down (event-modifiers type)) | |
243 | (read-event))) | |
fc558e4d RS |
244 | (save-excursion |
245 | (let ((modifiers (event-modifiers (aref key 0))) | |
246 | window position) | |
247 | ;; For a mouse button event, go to the button it applies to | |
248 | ;; to get the right key bindings. And go to the right place | |
249 | ;; in case the keymap depends on where you clicked. | |
250 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
251 | (memq 'drag modifiers)) | |
252 | (setq window (posn-window (event-start (aref key 0))) | |
253 | position (posn-point (event-start (aref key 0))))) | |
254 | (if (windowp window) | |
255 | (progn | |
256 | (set-buffer (window-buffer window)) | |
257 | (goto-char position))) | |
258 | (let ((defn (key-binding key))) | |
259 | (if (or (null defn) (integerp defn)) | |
260 | (message "%s is undefined" (key-description key)) | |
261 | (with-output-to-temp-buffer "*Help*" | |
262 | (princ (key-description key)) | |
263 | (if (windowp window) | |
264 | (princ " at that spot")) | |
265 | (princ " runs the command ") | |
266 | (prin1 defn) | |
267 | (princ ":\n") | |
fb3fc9b8 RS |
268 | (let ((doc (documentation defn))) |
269 | (if doc | |
270 | (progn (terpri) | |
271 | (princ doc)) | |
272 | (princ "not documented"))) | |
fc558e4d RS |
273 | (save-excursion |
274 | (set-buffer standard-output) | |
275 | (help-mode)) | |
276 | (print-help-return-message))))))) | |
433ae6f6 | 277 | |
ad023904 RS |
278 | (defun describe-mode () |
279 | "Display documentation of current major mode and minor modes. | |
433ae6f6 | 280 | For this to work correctly for a minor mode, the mode's indicator variable |
61c6b658 | 281 | \(listed in `minor-mode-alist') must also be a function whose documentation |
433ae6f6 | 282 | describes the minor mode." |
7192540b | 283 | (interactive) |
433ae6f6 | 284 | (with-output-to-temp-buffer "*Help*" |
7192540b | 285 | (let ((minor-modes minor-mode-alist) |
83f86594 | 286 | (first t) |
7192540b RS |
287 | (locals (buffer-local-variables))) |
288 | (while minor-modes | |
289 | (let* ((minor-mode (car (car minor-modes))) | |
290 | (indicator (car (cdr (car minor-modes)))) | |
291 | (local-binding (assq minor-mode locals))) | |
292 | ;; Document a minor mode if it is listed in minor-mode-alist, | |
293 | ;; bound locally in this buffer, non-nil, and has a function | |
294 | ;; definition. | |
295 | (if (and local-binding | |
296 | (cdr local-binding) | |
297 | (fboundp minor-mode)) | |
298 | (let ((pretty-minor-mode minor-mode)) | |
299 | (if (string-match "-mode$" (symbol-name minor-mode)) | |
300 | (setq pretty-minor-mode | |
301 | (capitalize | |
302 | (substring (symbol-name minor-mode) | |
303 | 0 (match-beginning 0))))) | |
304 | (while (and indicator (symbolp indicator)) | |
305 | (setq indicator (symbol-value indicator))) | |
83f86594 RS |
306 | (if first |
307 | (princ "The minor modes are described first, | |
308 | followed by the major mode, which is described on the last page.\n\f\n")) | |
309 | (setq first nil) | |
2ef581f3 RS |
310 | (princ (format "%s minor mode (%s):\n" |
311 | pretty-minor-mode | |
312 | (if indicator | |
313 | (format "indicator%s" indicator) | |
314 | "no indicator"))) | |
7192540b | 315 | (princ (documentation minor-mode)) |
83f86594 | 316 | (princ "\n\f\n")))) |
7192540b | 317 | (setq minor-modes (cdr minor-modes)))) |
433ae6f6 | 318 | (princ mode-name) |
ad023904 | 319 | (princ " mode:\n") |
433ae6f6 | 320 | (princ (documentation major-mode)) |
c46bbd92 KH |
321 | (save-excursion |
322 | (set-buffer standard-output) | |
323 | (help-mode)) | |
433ae6f6 RS |
324 | (print-help-return-message))) |
325 | ||
326 | ;; So keyboard macro definitions are documented correctly | |
327 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
328 | ||
329 | (defun describe-distribution () | |
330 | "Display info on how to obtain the latest version of GNU Emacs." | |
331 | (interactive) | |
332 | (find-file-read-only | |
1e6dacf6 | 333 | (expand-file-name "DISTRIB" data-directory))) |
433ae6f6 RS |
334 | |
335 | (defun describe-copying () | |
336 | "Display info on how you may redistribute copies of GNU Emacs." | |
337 | (interactive) | |
338 | (find-file-read-only | |
1e6dacf6 | 339 | (expand-file-name "COPYING" data-directory)) |
433ae6f6 RS |
340 | (goto-char (point-min))) |
341 | ||
76766f2d RS |
342 | (defun describe-project () |
343 | "Display info on the GNU project." | |
344 | (interactive) | |
345 | (find-file-read-only | |
346 | (expand-file-name "GNU" data-directory)) | |
347 | (goto-char (point-min))) | |
348 | ||
433ae6f6 RS |
349 | (defun describe-no-warranty () |
350 | "Display info on all the kinds of warranty Emacs does NOT have." | |
351 | (interactive) | |
352 | (describe-copying) | |
353 | (let (case-fold-search) | |
354 | (search-forward "NO WARRANTY") | |
355 | (recenter 0))) | |
356 | ||
61c6b658 | 357 | (defun describe-prefix-bindings () |
c7cba9cb RS |
358 | "Describe the bindings of the prefix used to reach this command. |
359 | The prefix described consists of all but the last event | |
360 | of the key sequence that ran this command." | |
61c6b658 | 361 | (interactive) |
ccc06dcc KH |
362 | (let* ((key (this-command-keys))) |
363 | (describe-bindings | |
364 | (if (stringp key) | |
365 | (substring key 0 (1- (length key))) | |
366 | (let ((prefix (make-vector (1- (length key)) nil)) | |
367 | (i 0)) | |
368 | (while (< i (length prefix)) | |
369 | (aset prefix i (aref key i)) | |
370 | (setq i (1+ i))) | |
371 | prefix))))) | |
c7cba9cb RS |
372 | ;; Make C-h after a prefix, when not specifically bound, |
373 | ;; run describe-prefix-bindings. | |
61c6b658 RS |
374 | (setq prefix-help-command 'describe-prefix-bindings) |
375 | ||
433ae6f6 RS |
376 | (defun view-emacs-news () |
377 | "Display info on recent changes to Emacs." | |
378 | (interactive) | |
1e6dacf6 | 379 | (find-file-read-only (expand-file-name "NEWS" data-directory))) |
433ae6f6 | 380 | |
7ee71cf1 RS |
381 | (defun view-emacs-FAQ () |
382 | "Display the Emacs Frequently Asked Questions (FAQ) file." | |
383 | (interactive) | |
384 | (find-file-read-only (expand-file-name "FAQ" data-directory))) | |
385 | ||
433ae6f6 RS |
386 | (defun view-lossage () |
387 | "Display last 100 input keystrokes." | |
388 | (interactive) | |
389 | (with-output-to-temp-buffer "*Help*" | |
298a7c8c RS |
390 | (princ (mapconcat (function (lambda (key) |
391 | (if (or (integerp key) | |
392 | (symbolp key) | |
393 | (listp key)) | |
394 | (single-key-description key) | |
395 | (prin1-to-string key nil)))) | |
396 | (recent-keys) | |
397 | " ")) | |
433ae6f6 RS |
398 | (save-excursion |
399 | (set-buffer standard-output) | |
400 | (goto-char (point-min)) | |
401 | (while (progn (move-to-column 50) (not (eobp))) | |
402 | (search-forward " " nil t) | |
c46bbd92 KH |
403 | (insert "\n")) |
404 | (help-mode)) | |
433ae6f6 RS |
405 | (print-help-return-message))) |
406 | ||
2fc9d9f4 | 407 | (defalias 'help 'help-for-help) |
41b8542b | 408 | (make-help-screen help-for-help |
224d56a3 | 409 | "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 | 410 | "You have typed \\[help-command], the help character. Type a Help option: |
efcce2d2 | 411 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) |
433ae6f6 | 412 | |
21ee8c42 RM |
413 | a command-apropos. Give a substring, and see a list of commands |
414 | (functions interactively callable) that contain | |
415 | that substring. See also the apropos command. | |
af6a9de9 RS |
416 | b describe-bindings. Display table of all key bindings. |
417 | c describe-key-briefly. Type a command key sequence; | |
21ee8c42 | 418 | it prints the function name that sequence runs. |
af6a9de9 | 419 | f describe-function. Type a function name and get documentation of it. |
21ee8c42 RM |
420 | C-f Info-goto-emacs-command-node. Type a function name; |
421 | it takes you to the Info node for that command. | |
7ee71cf1 | 422 | F view-emacs-FAQ. Shows emacs frequently asked questions file. |
af6a9de9 RS |
423 | i info. The info documentation reader. |
424 | k describe-key. Type a command key sequence; | |
21ee8c42 RM |
425 | it displays the full documentation. |
426 | C-k Info-goto-emacs-key-command-node. Type a command key sequence; | |
427 | it takes you to the Info node for the command bound to that key. | |
af6a9de9 RS |
428 | l view-lossage. Shows last 100 characters you typed. |
429 | m describe-mode. Print documentation of current major mode, | |
21ee8c42 | 430 | which describes the commands peculiar to it. |
af6a9de9 RS |
431 | n view-emacs-news. Shows emacs news file. |
432 | p finder-by-keyword. Find packages matching a given topic keyword. | |
433 | s describe-syntax. Display contents of syntax table, plus explanations | |
434 | t help-with-tutorial. Select the Emacs learn-by-doing tutorial. | |
435 | v describe-variable. Type name of a variable; | |
21ee8c42 | 436 | it displays the variable's documentation and value. |
af6a9de9 | 437 | w where-is. Type command name; it prints which keystrokes |
21ee8c42 | 438 | invoke that command. |
433ae6f6 RS |
439 | C-c print Emacs copying permission (General Public License). |
440 | C-d print Emacs ordering information. | |
441 | C-n print news of recent Emacs changes. | |
76766f2d | 442 | C-p print information about the GNU project. |
433ae6f6 | 443 | C-w print information on absence of warranty for GNU Emacs." |
41b8542b | 444 | help-map) |
433ae6f6 RS |
445 | |
446 | ;; Return a function which is called by the list containing point. | |
447 | ;; If that gives no function, return a function whose name is around point. | |
448 | ;; If that doesn't give a function, return nil. | |
449 | (defun function-called-at-point () | |
450 | (or (condition-case () | |
451 | (save-excursion | |
452 | (save-restriction | |
453 | (narrow-to-region (max (point-min) (- (point) 1000)) (point-max)) | |
454 | (backward-up-list 1) | |
455 | (forward-char 1) | |
456 | (let (obj) | |
457 | (setq obj (read (current-buffer))) | |
458 | (and (symbolp obj) (fboundp obj) obj)))) | |
459 | (error nil)) | |
460 | (condition-case () | |
914a48d0 RS |
461 | (let ((stab (syntax-table))) |
462 | (unwind-protect | |
463 | (save-excursion | |
464 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
465 | (or (not (zerop (skip-syntax-backward "_w"))) | |
466 | (eq (char-syntax (following-char)) ?w) | |
467 | (eq (char-syntax (following-char)) ?_) | |
468 | (forward-sexp -1)) | |
469 | (skip-chars-forward "'") | |
470 | (let ((obj (read (current-buffer)))) | |
471 | (and (symbolp obj) (fboundp obj) obj))) | |
472 | (set-syntax-table stab))) | |
433ae6f6 RS |
473 | (error nil)))) |
474 | ||
ca5ed196 RS |
475 | (defun describe-function-find-file (function) |
476 | (let ((files load-history) | |
477 | file functions) | |
478 | (while files | |
479 | (if (memq function (cdr (car files))) | |
480 | (setq file (car (car files)) files nil)) | |
481 | (setq files (cdr files))) | |
482 | file)) | |
483 | ||
433ae6f6 RS |
484 | (defun describe-function (function) |
485 | "Display the full documentation of FUNCTION (a symbol)." | |
486 | (interactive | |
487 | (let ((fn (function-called-at-point)) | |
488 | (enable-recursive-minibuffers t) | |
489 | val) | |
490 | (setq val (completing-read (if fn | |
491 | (format "Describe function (default %s): " fn) | |
492 | "Describe function: ") | |
493 | obarray 'fboundp t)) | |
494 | (list (if (equal val "") | |
495 | fn (intern val))))) | |
496 | (with-output-to-temp-buffer "*Help*" | |
497 | (prin1 function) | |
498 | (princ ": ") | |
499 | (let* ((def (symbol-function function)) | |
ad3ec252 | 500 | file-name |
433ae6f6 | 501 | (beg (if (commandp def) "an interactive " "a "))) |
7f26b180 RM |
502 | (princ (cond ((or (stringp def) |
503 | (vectorp def)) | |
6f6bfb85 | 504 | "a keyboard macro") |
433ae6f6 | 505 | ((subrp def) |
6f6bfb85 | 506 | (concat beg "built-in function")) |
dbc4e1c1 | 507 | ((byte-code-function-p def) |
6f6bfb85 | 508 | (concat beg "compiled Lisp function")) |
433ae6f6 | 509 | ((symbolp def) |
6f6bfb85 | 510 | (format "alias for `%s'" def)) |
433ae6f6 | 511 | ((eq (car-safe def) 'lambda) |
6f6bfb85 | 512 | (concat beg "Lisp function")) |
433ae6f6 | 513 | ((eq (car-safe def) 'macro) |
6f6bfb85 | 514 | "a Lisp macro") |
433ae6f6 | 515 | ((eq (car-safe def) 'mocklisp) |
6f6bfb85 | 516 | "a mocklisp function") |
433ae6f6 | 517 | ((eq (car-safe def) 'autoload) |
ad3ec252 | 518 | (setq file-name (nth 1 def)) |
6f6bfb85 | 519 | (format "%s autoloaded Lisp %s" |
762435dd | 520 | (if (commandp def) "an interactive" "an") |
433ae6f6 | 521 | (if (nth 4 def) "macro" "function") |
ab67260b | 522 | )) |
433ae6f6 | 523 | (t ""))) |
ad3ec252 RS |
524 | (or file-name |
525 | (setq file-name (describe-function-find-file function))) | |
526 | (if file-name | |
527 | (progn | |
528 | (princ " in `") | |
529 | ;; We used to add .el to the file name, | |
530 | ;; but that's completely wrong when the user used load-file. | |
531 | (princ file-name) | |
532 | (princ "'"))) | |
6f6bfb85 | 533 | (princ ".") |
4591cb90 | 534 | (terpri) |
7f26b180 RM |
535 | (let ((arglist (cond ((byte-code-function-p def) |
536 | (car (append def nil))) | |
537 | ((eq (car-safe def) 'lambda) | |
538 | (nth 1 def)) | |
539 | (t t)))) | |
540 | (if (listp arglist) | |
541 | (progn | |
542 | (princ (cons function | |
543 | (mapcar (lambda (arg) | |
544 | (if (memq arg '(&optional &rest)) | |
545 | arg | |
546 | (intern (upcase (symbol-name arg))))) | |
547 | arglist))) | |
548 | (terpri)))) | |
fb3fc9b8 RS |
549 | (let ((doc (documentation function))) |
550 | (if doc | |
551 | (progn (terpri) | |
552 | (princ doc)) | |
553 | (princ "not documented")))) | |
433ae6f6 | 554 | (print-help-return-message) |
c46bbd92 KH |
555 | (save-excursion |
556 | (set-buffer standard-output) | |
557 | (help-mode) | |
558 | ;; Return the text we displayed. | |
559 | (buffer-string)))) | |
433ae6f6 RS |
560 | |
561 | (defun variable-at-point () | |
562 | (condition-case () | |
914a48d0 RS |
563 | (let ((stab (syntax-table))) |
564 | (unwind-protect | |
565 | (save-excursion | |
566 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
567 | (or (not (zerop (skip-syntax-backward "_w"))) | |
568 | (eq (char-syntax (following-char)) ?w) | |
569 | (eq (char-syntax (following-char)) ?_) | |
570 | (forward-sexp -1)) | |
571 | (skip-chars-forward "'") | |
572 | (let ((obj (read (current-buffer)))) | |
573 | (and (symbolp obj) (boundp obj) obj))) | |
574 | (set-syntax-table stab))) | |
433ae6f6 RS |
575 | (error nil))) |
576 | ||
577 | (defun describe-variable (variable) | |
578 | "Display the full documentation of VARIABLE (a symbol). | |
579 | Returns the documentation as a string, also." | |
580 | (interactive | |
581 | (let ((v (variable-at-point)) | |
582 | (enable-recursive-minibuffers t) | |
583 | val) | |
584 | (setq val (completing-read (if v | |
585 | (format "Describe variable (default %s): " v) | |
586 | "Describe variable: ") | |
587 | obarray 'boundp t)) | |
588 | (list (if (equal val "") | |
589 | v (intern val))))) | |
590 | (with-output-to-temp-buffer "*Help*" | |
591 | (prin1 variable) | |
433ae6f6 | 592 | (if (not (boundp variable)) |
3e483e01 RS |
593 | (princ " is void") |
594 | (princ "'s value is ") | |
433ae6f6 | 595 | (prin1 (symbol-value variable))) |
7e76ae23 | 596 | (terpri) |
b872e1da | 597 | (if (local-variable-p variable) |
3e483e01 RS |
598 | (progn |
599 | (princ (format "Local in buffer %s; " (buffer-name))) | |
600 | (if (not (default-boundp variable)) | |
601 | (princ "globally void") | |
602 | (princ "global value is ") | |
603 | (prin1 (default-value variable))) | |
604 | (terpri))) | |
7e76ae23 | 605 | (terpri) |
433ae6f6 RS |
606 | (princ "Documentation:") |
607 | (terpri) | |
608 | (let ((doc (documentation-property variable 'variable-documentation))) | |
c8f98e6d | 609 | (princ (or doc "not documented as a variable."))) |
433ae6f6 | 610 | (print-help-return-message) |
c46bbd92 KH |
611 | (save-excursion |
612 | (set-buffer standard-output) | |
613 | (help-mode) | |
614 | ;; Return the text we displayed. | |
615 | (buffer-string)))) | |
433ae6f6 | 616 | |
54c0b967 RS |
617 | (defun where-is (definition) |
618 | "Print message listing key sequences that invoke specified command. | |
619 | Argument is a command definition, usually a symbol with a function definition." | |
620 | (interactive | |
621 | (let ((fn (function-called-at-point)) | |
622 | (enable-recursive-minibuffers t) | |
623 | val) | |
624 | (setq val (completing-read (if fn | |
625 | (format "Where is command (default %s): " fn) | |
626 | "Where is command: ") | |
627 | obarray 'fboundp t)) | |
628 | (list (if (equal val "") | |
629 | fn (intern val))))) | |
630 | (let* ((keys (where-is-internal definition overriding-local-map nil nil)) | |
631 | (keys1 (mapconcat 'key-description keys ", "))) | |
632 | (if (> (length keys1) 0) | |
633 | (message "%s is on %s" definition keys1) | |
634 | (message "%s is not on any key" definition))) | |
635 | nil) | |
636 | ||
a130d829 | 637 | (defun locate-library (library &optional nosuffix path interactive-call) |
2747503c | 638 | "Show the precise file name of Emacs library LIBRARY. |
433ae6f6 RS |
639 | This command searches the directories in `load-path' like `M-x load-library' |
640 | to find the file that `M-x load-library RET LIBRARY RET' would load. | |
641 | Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el' | |
9dc176a0 RS |
642 | to the specified name LIBRARY. |
643 | ||
644 | If the optional third arg PATH is specified, that list of directories | |
645 | is used instead of `load-path'." | |
a130d829 RS |
646 | (interactive (list (read-string "Locate library: ") |
647 | nil nil | |
648 | t)) | |
649 | (let (result) | |
650 | (catch 'answer | |
651 | (mapcar | |
a1c9f209 EN |
652 | (lambda (dir) |
653 | (mapcar | |
654 | (lambda (suf) | |
655 | (let ((try (expand-file-name (concat library suf) dir))) | |
656 | (and (file-readable-p try) | |
657 | (null (file-directory-p try)) | |
658 | (progn | |
659 | (setq result try) | |
660 | (throw 'answer try))))) | |
661 | (if nosuffix | |
662 | '("") | |
663 | (let ((basic '(".elc" ".el" "")) | |
664 | (compressed '(".Z" ".gz" ""))) | |
665 | ;; If autocompression mode is on, | |
666 | ;; consider all combinations of library suffixes | |
667 | ;; and compression suffixes. | |
668 | (if (rassq 'jka-compr-handler file-name-handler-alist) | |
669 | (apply 'nconc | |
670 | (mapcar (lambda (compelt) | |
671 | (mapcar (lambda (baselt) | |
672 | (concat baselt compelt)) | |
673 | basic)) | |
674 | compressed)) | |
675 | basic))))) | |
a130d829 RS |
676 | (or path load-path))) |
677 | (and interactive-call | |
678 | (if result | |
679 | (message "Library is file %s" result) | |
680 | (message "No library %s in search path" library))) | |
681 | result)) | |
1a06eabd ER |
682 | |
683 | ;;; help.el ends here |