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) |
122955bf | 56 | (define-key help-map "a" 'apropos-command) |
433ae6f6 RS |
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) | |
32884eab | 71 | (define-key help-map "\C-i" 'info-lookup-symbol) |
433ae6f6 RS |
72 | |
73 | (define-key help-map "l" 'view-lossage) | |
74 | ||
75 | (define-key help-map "m" 'describe-mode) | |
76 | ||
77 | (define-key help-map "\C-n" 'view-emacs-news) | |
78 | (define-key help-map "n" 'view-emacs-news) | |
79 | ||
06b98c51 | 80 | (define-key help-map "p" 'finder-by-keyword) |
3e9c095d RS |
81 | (autoload 'finder-by-keyword "finder" |
82 | "Find packages matching a given keyword." t) | |
06b98c51 | 83 | |
433ae6f6 RS |
84 | (define-key help-map "s" 'describe-syntax) |
85 | ||
86 | (define-key help-map "t" 'help-with-tutorial) | |
87 | ||
88 | (define-key help-map "w" 'where-is) | |
89 | ||
90 | (define-key help-map "v" 'describe-variable) | |
91 | ||
2fc9d9f4 RS |
92 | (define-key help-map "q" 'help-quit) |
93 | ||
507fb916 | 94 | (defvar help-font-lock-keywords |
0e0ee795 RS |
95 | (eval-when-compile |
96 | (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]")) | |
97 | (list | |
98 | ;; | |
99 | ;; The symbol itself. | |
100 | (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)") | |
8743d4cb RS |
101 | '(1 (if (match-beginning 3) |
102 | font-lock-function-name-face | |
b8077811 | 103 | font-lock-variable-name-face))) |
0e0ee795 RS |
104 | ;; |
105 | ;; Words inside `' which tend to be symbol names. | |
106 | (list (concat "`\\(" sym-char sym-char "+\\)'") | |
107 | 1 'font-lock-reference-face t) | |
108 | ;; | |
109 | ;; CLisp `:' keywords as references. | |
110 | (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t)))) | |
507fb916 SM |
111 | "Default expressions to highlight in Help mode.") |
112 | ||
afaa65e4 KH |
113 | (defun help-mode () |
114 | "Major mode for viewing help text. | |
115 | Entry to this mode runs the normal hook `help-mode-hook'. | |
116 | Commands: | |
117 | \\{help-mode-map}" | |
118 | (interactive) | |
119 | (kill-all-local-variables) | |
120 | (use-local-map help-mode-map) | |
121 | (setq mode-name "Help") | |
122 | (setq major-mode 'help-mode) | |
507fb916 | 123 | (make-local-variable 'font-lock-defaults) |
dd7d85ba | 124 | (setq font-lock-defaults '(help-font-lock-keywords)) |
42499979 | 125 | (view-mode) |
afaa65e4 KH |
126 | (run-hooks 'help-mode-hook)) |
127 | ||
2fc9d9f4 RS |
128 | (defun help-quit () |
129 | (interactive) | |
130 | nil) | |
131 | ||
0634ea78 KH |
132 | (defun help-with-tutorial (&optional arg) |
133 | "Select the Emacs learn-by-doing tutorial. | |
134 | A tutorial written in the current primary language is selected. | |
135 | If there's no tutorial in the language, \"TUTORIAL\" is selected. | |
136 | With arg, users are asked to select language." | |
137 | (interactive "P") | |
7c9b148e KH |
138 | (let (lang filename file) |
139 | (if arg | |
140 | (or (setq lang (read-language-name 'tutorial "Language: ")) | |
141 | (error "No tutorial file of the specified language")) | |
142 | (setq lang primary-language)) | |
143 | (setq filename (or (get-language-info lang 'tutorial) | |
144 | "TUTORIAL")) | |
145 | (setq file (expand-file-name (concat "~/" filename))) | |
433ae6f6 RS |
146 | (delete-other-windows) |
147 | (if (get-file-buffer file) | |
148 | (switch-to-buffer (get-file-buffer file)) | |
149 | (switch-to-buffer (create-file-buffer file)) | |
150 | (setq buffer-file-name file) | |
151 | (setq default-directory (expand-file-name "~/")) | |
79058860 | 152 | (setq buffer-auto-save-file-name nil) |
0634ea78 | 153 | (insert-file-contents (expand-file-name filename data-directory)) |
433ae6f6 RS |
154 | (goto-char (point-min)) |
155 | (search-forward "\n<<") | |
156 | (beginning-of-line) | |
157 | (delete-region (point) (progn (end-of-line) (point))) | |
857a1de6 | 158 | (let ((n (- (window-height (selected-window)) |
433ae6f6 | 159 | (count-lines (point-min) (point)) |
857a1de6 | 160 | 6))) |
d0da2301 | 161 | (if (< n 12) |
857a1de6 KH |
162 | (newline n) |
163 | ;; Some people get confused by the large gap. | |
164 | (newline (/ n 2)) | |
165 | (insert "[Middle of page left blank for didactic purposes. " | |
166 | "Text continues below]") | |
167 | (newline (- n (/ n 2))))) | |
433ae6f6 RS |
168 | (goto-char (point-min)) |
169 | (set-buffer-modified-p nil)))) | |
170 | ||
e88a2c59 RS |
171 | (defun describe-key-briefly (key &optional insert) |
172 | "Print the name of the function KEY invokes. KEY is a string. | |
173 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer." | |
174 | (interactive "kDescribe key briefly: \nP") | |
5f296b78 RS |
175 | ;; If this key seq ends with a down event, discard the |
176 | ;; following click or drag event. Otherwise that would | |
177 | ;; erase the message. | |
178 | (let ((type (aref key (1- (length key))))) | |
179 | (if (listp type) (setq type (car type))) | |
180 | (and (symbolp type) | |
181 | (memq 'down (event-modifiers type)) | |
fca4b775 | 182 | (read-event))) |
fc558e4d RS |
183 | (save-excursion |
184 | (let ((modifiers (event-modifiers (aref key 0))) | |
e88a2c59 | 185 | (standard-output (if insert (current-buffer) t)) |
fc558e4d RS |
186 | window position) |
187 | ;; For a mouse button event, go to the button it applies to | |
188 | ;; to get the right key bindings. And go to the right place | |
189 | ;; in case the keymap depends on where you clicked. | |
190 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
191 | (memq 'drag modifiers)) | |
192 | (setq window (posn-window (event-start (aref key 0))) | |
193 | position (posn-point (event-start (aref key 0))))) | |
194 | (if (windowp window) | |
195 | (progn | |
196 | (set-buffer (window-buffer window)) | |
197 | (goto-char position))) | |
198 | ;; Ok, now look up the key and name the command. | |
e88a2c59 RS |
199 | (let ((defn (key-binding key)) |
200 | (key-desc (key-description key))) | |
fc558e4d | 201 | (if (or (null defn) (integerp defn)) |
e88a2c59 RS |
202 | (princ (format "%s is undefined" key-desc)) |
203 | (princ (format (if insert | |
204 | "%s (%s)" | |
205 | (if (windowp window) | |
206 | "%s at that spot runs the command %s" | |
207 | "%s runs the command %s")) | |
208 | key-desc | |
209 | (if (symbolp defn) defn (prin1-to-string defn))))))))) | |
433ae6f6 RS |
210 | |
211 | (defun print-help-return-message (&optional function) | |
212 | "Display or return message saying how to restore windows after help command. | |
213 | Computes a message and applies the optional argument FUNCTION to it. | |
214 | If FUNCTION is nil, applies `message' to it, thus printing it." | |
215 | (and (not (get-buffer-window standard-output)) | |
d536293f | 216 | (let ((first-message |
a1c9f209 | 217 | (cond ((special-display-p (buffer-name standard-output)) |
d536293f RS |
218 | ;; If the help output buffer is a special display buffer, |
219 | ;; don't say anything about how to get rid of it. | |
220 | ;; First of all, the user will do that with the window | |
221 | ;; manager, not with Emacs. | |
222 | ;; Secondly, the buffer has not been displayed yet, | |
223 | ;; so we don't know whether its frame will be selected. | |
d536293f RS |
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. | |
a1c9f209 | 239 | (if (special-display-p (buffer-name standard-output)) |
125a8d70 | 240 | nil |
a1c9f209 | 241 | (if (same-window-p (buffer-name standard-output)) |
125a8d70 RS |
242 | ;; Say how to scroll this window. |
243 | (substitute-command-keys | |
244 | "\\[scroll-up] to scroll the help.") | |
245 | ;; Say how to scroll some other window. | |
6e7f5182 | 246 | (substitute-command-keys |
125a8d70 | 247 | "\\[scroll-other-window] to scroll the help.")))))))) |
433ae6f6 RS |
248 | |
249 | (defun describe-key (key) | |
250 | "Display documentation of the function invoked by KEY. KEY is a string." | |
251 | (interactive "kDescribe key: ") | |
5f296b78 RS |
252 | ;; If this key seq ends with a down event, discard the |
253 | ;; following click or drag event. Otherwise that would | |
254 | ;; erase the message. | |
255 | (let ((type (aref key (1- (length key))))) | |
256 | (if (listp type) (setq type (car type))) | |
257 | (and (symbolp type) | |
258 | (memq 'down (event-modifiers type)) | |
259 | (read-event))) | |
fc558e4d RS |
260 | (save-excursion |
261 | (let ((modifiers (event-modifiers (aref key 0))) | |
262 | window position) | |
263 | ;; For a mouse button event, go to the button it applies to | |
264 | ;; to get the right key bindings. And go to the right place | |
265 | ;; in case the keymap depends on where you clicked. | |
266 | (if (or (memq 'click modifiers) (memq 'down modifiers) | |
267 | (memq 'drag modifiers)) | |
268 | (setq window (posn-window (event-start (aref key 0))) | |
269 | position (posn-point (event-start (aref key 0))))) | |
270 | (if (windowp window) | |
271 | (progn | |
272 | (set-buffer (window-buffer window)) | |
273 | (goto-char position))) | |
274 | (let ((defn (key-binding key))) | |
275 | (if (or (null defn) (integerp defn)) | |
276 | (message "%s is undefined" (key-description key)) | |
277 | (with-output-to-temp-buffer "*Help*" | |
278 | (princ (key-description key)) | |
279 | (if (windowp window) | |
280 | (princ " at that spot")) | |
281 | (princ " runs the command ") | |
282 | (prin1 defn) | |
283 | (princ ":\n") | |
fb3fc9b8 RS |
284 | (let ((doc (documentation defn))) |
285 | (if doc | |
286 | (progn (terpri) | |
287 | (princ doc)) | |
288 | (princ "not documented"))) | |
fc558e4d RS |
289 | (save-excursion |
290 | (set-buffer standard-output) | |
291 | (help-mode)) | |
292 | (print-help-return-message))))))) | |
433ae6f6 | 293 | |
ad023904 RS |
294 | (defun describe-mode () |
295 | "Display documentation of current major mode and minor modes. | |
433ae6f6 | 296 | For this to work correctly for a minor mode, the mode's indicator variable |
61c6b658 | 297 | \(listed in `minor-mode-alist') must also be a function whose documentation |
433ae6f6 | 298 | describes the minor mode." |
7192540b | 299 | (interactive) |
433ae6f6 | 300 | (with-output-to-temp-buffer "*Help*" |
7192540b | 301 | (let ((minor-modes minor-mode-alist) |
ddbe99e0 | 302 | (first t)) |
7192540b RS |
303 | (while minor-modes |
304 | (let* ((minor-mode (car (car minor-modes))) | |
ddbe99e0 | 305 | (indicator (car (cdr (car minor-modes))))) |
7192540b RS |
306 | ;; Document a minor mode if it is listed in minor-mode-alist, |
307 | ;; bound locally in this buffer, non-nil, and has a function | |
308 | ;; definition. | |
ddbe99e0 | 309 | (if (and (symbol-value minor-mode) |
7192540b RS |
310 | (fboundp minor-mode)) |
311 | (let ((pretty-minor-mode minor-mode)) | |
312 | (if (string-match "-mode$" (symbol-name minor-mode)) | |
313 | (setq pretty-minor-mode | |
314 | (capitalize | |
315 | (substring (symbol-name minor-mode) | |
316 | 0 (match-beginning 0))))) | |
317 | (while (and indicator (symbolp indicator)) | |
318 | (setq indicator (symbol-value indicator))) | |
83f86594 RS |
319 | (if first |
320 | (princ "The minor modes are described first, | |
321 | followed by the major mode, which is described on the last page.\n\f\n")) | |
322 | (setq first nil) | |
2ef581f3 RS |
323 | (princ (format "%s minor mode (%s):\n" |
324 | pretty-minor-mode | |
325 | (if indicator | |
326 | (format "indicator%s" indicator) | |
327 | "no indicator"))) | |
7192540b | 328 | (princ (documentation minor-mode)) |
83f86594 | 329 | (princ "\n\f\n")))) |
7192540b | 330 | (setq minor-modes (cdr minor-modes)))) |
433ae6f6 | 331 | (princ mode-name) |
ad023904 | 332 | (princ " mode:\n") |
433ae6f6 | 333 | (princ (documentation major-mode)) |
c46bbd92 KH |
334 | (save-excursion |
335 | (set-buffer standard-output) | |
336 | (help-mode)) | |
433ae6f6 RS |
337 | (print-help-return-message))) |
338 | ||
339 | ;; So keyboard macro definitions are documented correctly | |
340 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
341 | ||
342 | (defun describe-distribution () | |
343 | "Display info on how to obtain the latest version of GNU Emacs." | |
344 | (interactive) | |
345 | (find-file-read-only | |
1e6dacf6 | 346 | (expand-file-name "DISTRIB" data-directory))) |
433ae6f6 RS |
347 | |
348 | (defun describe-copying () | |
349 | "Display info on how you may redistribute copies of GNU Emacs." | |
350 | (interactive) | |
351 | (find-file-read-only | |
1e6dacf6 | 352 | (expand-file-name "COPYING" data-directory)) |
433ae6f6 RS |
353 | (goto-char (point-min))) |
354 | ||
76766f2d RS |
355 | (defun describe-project () |
356 | "Display info on the GNU project." | |
357 | (interactive) | |
358 | (find-file-read-only | |
359 | (expand-file-name "GNU" data-directory)) | |
360 | (goto-char (point-min))) | |
361 | ||
433ae6f6 RS |
362 | (defun describe-no-warranty () |
363 | "Display info on all the kinds of warranty Emacs does NOT have." | |
364 | (interactive) | |
365 | (describe-copying) | |
366 | (let (case-fold-search) | |
367 | (search-forward "NO WARRANTY") | |
368 | (recenter 0))) | |
369 | ||
61c6b658 | 370 | (defun describe-prefix-bindings () |
c7cba9cb RS |
371 | "Describe the bindings of the prefix used to reach this command. |
372 | The prefix described consists of all but the last event | |
373 | of the key sequence that ran this command." | |
61c6b658 | 374 | (interactive) |
ccc06dcc KH |
375 | (let* ((key (this-command-keys))) |
376 | (describe-bindings | |
377 | (if (stringp key) | |
378 | (substring key 0 (1- (length key))) | |
379 | (let ((prefix (make-vector (1- (length key)) nil)) | |
380 | (i 0)) | |
381 | (while (< i (length prefix)) | |
382 | (aset prefix i (aref key i)) | |
383 | (setq i (1+ i))) | |
384 | prefix))))) | |
c7cba9cb RS |
385 | ;; Make C-h after a prefix, when not specifically bound, |
386 | ;; run describe-prefix-bindings. | |
61c6b658 RS |
387 | (setq prefix-help-command 'describe-prefix-bindings) |
388 | ||
433ae6f6 RS |
389 | (defun view-emacs-news () |
390 | "Display info on recent changes to Emacs." | |
391 | (interactive) | |
1e6dacf6 | 392 | (find-file-read-only (expand-file-name "NEWS" data-directory))) |
433ae6f6 | 393 | |
7ee71cf1 RS |
394 | (defun view-emacs-FAQ () |
395 | "Display the Emacs Frequently Asked Questions (FAQ) file." | |
396 | (interactive) | |
397 | (find-file-read-only (expand-file-name "FAQ" data-directory))) | |
398 | ||
433ae6f6 RS |
399 | (defun view-lossage () |
400 | "Display last 100 input keystrokes." | |
401 | (interactive) | |
402 | (with-output-to-temp-buffer "*Help*" | |
298a7c8c RS |
403 | (princ (mapconcat (function (lambda (key) |
404 | (if (or (integerp key) | |
405 | (symbolp key) | |
406 | (listp key)) | |
407 | (single-key-description key) | |
408 | (prin1-to-string key nil)))) | |
409 | (recent-keys) | |
410 | " ")) | |
433ae6f6 RS |
411 | (save-excursion |
412 | (set-buffer standard-output) | |
413 | (goto-char (point-min)) | |
414 | (while (progn (move-to-column 50) (not (eobp))) | |
415 | (search-forward " " nil t) | |
c46bbd92 KH |
416 | (insert "\n")) |
417 | (help-mode)) | |
433ae6f6 RS |
418 | (print-help-return-message))) |
419 | ||
2fc9d9f4 | 420 | (defalias 'help 'help-for-help) |
41b8542b | 421 | (make-help-screen help-for-help |
a30a106b | 422 | "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:" |
76766f2d | 423 | "You have typed \\[help-command], the help character. Type a Help option: |
efcce2d2 | 424 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) |
433ae6f6 | 425 | |
21ee8c42 RM |
426 | a command-apropos. Give a substring, and see a list of commands |
427 | (functions interactively callable) that contain | |
428 | that substring. See also the apropos command. | |
af6a9de9 RS |
429 | b describe-bindings. Display table of all key bindings. |
430 | c describe-key-briefly. Type a command key sequence; | |
21ee8c42 | 431 | it prints the function name that sequence runs. |
a30a106b RS |
432 | C describe-coding-system. This describes either a specific coding system |
433 | (if you type its name) or the coding systems currently in use | |
434 | (if you type just RET). | |
af6a9de9 | 435 | f describe-function. Type a function name and get documentation of it. |
21ee8c42 RM |
436 | C-f Info-goto-emacs-command-node. Type a function name; |
437 | it takes you to the Info node for that command. | |
af6a9de9 | 438 | i info. The info documentation reader. |
a30a106b RS |
439 | I describe-input-method. Describe a specific input method (if you type |
440 | its name) or the current input method (if you type just RET). | |
af6a9de9 | 441 | k describe-key. Type a command key sequence; |
21ee8c42 RM |
442 | it displays the full documentation. |
443 | C-k Info-goto-emacs-key-command-node. Type a command key sequence; | |
444 | it takes you to the Info node for the command bound to that key. | |
af6a9de9 | 445 | l view-lossage. Shows last 100 characters you typed. |
a30a106b RS |
446 | L describe-language-environment. This describes either the a |
447 | specific language environment (if you type its name) | |
448 | or the current language environment (if you type just RET). | |
af6a9de9 | 449 | m describe-mode. Print documentation of current major mode, |
21ee8c42 | 450 | which describes the commands peculiar to it. |
af6a9de9 RS |
451 | n view-emacs-news. Shows emacs news file. |
452 | p finder-by-keyword. Find packages matching a given topic keyword. | |
453 | s describe-syntax. Display contents of syntax table, plus explanations | |
454 | t help-with-tutorial. Select the Emacs learn-by-doing tutorial. | |
455 | v describe-variable. Type name of a variable; | |
21ee8c42 | 456 | it displays the variable's documentation and value. |
af6a9de9 | 457 | w where-is. Type command name; it prints which keystrokes |
21ee8c42 | 458 | invoke that command. |
a30a106b RS |
459 | |
460 | F Display the frequently asked questions file. | |
461 | h Display the HELLO file which illustrates various scripts. | |
462 | C-c Display Emacs copying permission (General Public License). | |
463 | C-d Display Emacs ordering information. | |
464 | C-n Display news of recent Emacs changes. | |
465 | C-p Display information about the GNU project. | |
466 | C-w Display information on absence of warranty for GNU Emacs." | |
41b8542b | 467 | help-map) |
433ae6f6 RS |
468 | |
469 | ;; Return a function which is called by the list containing point. | |
470 | ;; If that gives no function, return a function whose name is around point. | |
471 | ;; If that doesn't give a function, return nil. | |
472 | (defun function-called-at-point () | |
473 | (or (condition-case () | |
474 | (save-excursion | |
475 | (save-restriction | |
476 | (narrow-to-region (max (point-min) (- (point) 1000)) (point-max)) | |
477 | (backward-up-list 1) | |
478 | (forward-char 1) | |
479 | (let (obj) | |
480 | (setq obj (read (current-buffer))) | |
481 | (and (symbolp obj) (fboundp obj) obj)))) | |
482 | (error nil)) | |
483 | (condition-case () | |
914a48d0 RS |
484 | (let ((stab (syntax-table))) |
485 | (unwind-protect | |
486 | (save-excursion | |
487 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
488 | (or (not (zerop (skip-syntax-backward "_w"))) | |
489 | (eq (char-syntax (following-char)) ?w) | |
490 | (eq (char-syntax (following-char)) ?_) | |
491 | (forward-sexp -1)) | |
492 | (skip-chars-forward "'") | |
493 | (let ((obj (read (current-buffer)))) | |
494 | (and (symbolp obj) (fboundp obj) obj))) | |
495 | (set-syntax-table stab))) | |
433ae6f6 RS |
496 | (error nil)))) |
497 | ||
ca5ed196 RS |
498 | (defun describe-function-find-file (function) |
499 | (let ((files load-history) | |
500 | file functions) | |
501 | (while files | |
502 | (if (memq function (cdr (car files))) | |
503 | (setq file (car (car files)) files nil)) | |
504 | (setq files (cdr files))) | |
505 | file)) | |
506 | ||
433ae6f6 RS |
507 | (defun describe-function (function) |
508 | "Display the full documentation of FUNCTION (a symbol)." | |
509 | (interactive | |
510 | (let ((fn (function-called-at-point)) | |
511 | (enable-recursive-minibuffers t) | |
512 | val) | |
513 | (setq val (completing-read (if fn | |
514 | (format "Describe function (default %s): " fn) | |
515 | "Describe function: ") | |
516 | obarray 'fboundp t)) | |
517 | (list (if (equal val "") | |
518 | fn (intern val))))) | |
00d3de8e RS |
519 | (if function |
520 | (with-output-to-temp-buffer "*Help*" | |
521 | (prin1 function) | |
522 | (princ ": ") | |
523 | (let* ((def (symbol-function function)) | |
524 | file-name | |
525 | (beg (if (commandp def) "an interactive " "a "))) | |
526 | (princ (cond ((or (stringp def) | |
527 | (vectorp def)) | |
528 | "a keyboard macro") | |
529 | ((subrp def) | |
530 | (concat beg "built-in function")) | |
531 | ((byte-code-function-p def) | |
532 | (concat beg "compiled Lisp function")) | |
533 | ((symbolp def) | |
534 | (format "alias for `%s'" def)) | |
535 | ((eq (car-safe def) 'lambda) | |
536 | (concat beg "Lisp function")) | |
537 | ((eq (car-safe def) 'macro) | |
538 | "a Lisp macro") | |
539 | ((eq (car-safe def) 'mocklisp) | |
540 | "a mocklisp function") | |
541 | ((eq (car-safe def) 'autoload) | |
542 | (setq file-name (nth 1 def)) | |
543 | (format "%s autoloaded Lisp %s" | |
544 | (if (commandp def) "an interactive" "an") | |
545 | (if (nth 4 def) "macro" "function") | |
546 | )) | |
547 | (t ""))) | |
548 | (or file-name | |
549 | (setq file-name (describe-function-find-file function))) | |
550 | (if file-name | |
551 | (progn | |
552 | (princ " in `") | |
553 | ;; We used to add .el to the file name, | |
554 | ;; but that's completely wrong when the user used load-file. | |
555 | (princ file-name) | |
556 | (princ "'"))) | |
557 | (princ ".") | |
558 | (terpri) | |
559 | (let ((arglist (cond ((byte-code-function-p def) | |
560 | (car (append def nil))) | |
561 | ((eq (car-safe def) 'lambda) | |
562 | (nth 1 def)) | |
563 | (t t)))) | |
564 | (if (listp arglist) | |
565 | (progn | |
566 | (princ (cons function | |
567 | (mapcar (lambda (arg) | |
568 | (if (memq arg '(&optional &rest)) | |
569 | arg | |
570 | (intern (upcase (symbol-name arg))))) | |
571 | arglist))) | |
572 | (terpri)))) | |
573 | (let ((doc (documentation function))) | |
574 | (if doc | |
575 | (progn (terpri) | |
576 | (princ doc)) | |
577 | (princ "not documented")))) | |
578 | (print-help-return-message) | |
579 | (save-excursion | |
580 | (set-buffer standard-output) | |
581 | (help-mode) | |
582 | ;; Return the text we displayed. | |
583 | (buffer-string))) | |
584 | (message "You didn't specify a function"))) | |
585 | ||
586 | ;; We return 0 if we can't find a variable to return. | |
433ae6f6 RS |
587 | (defun variable-at-point () |
588 | (condition-case () | |
914a48d0 RS |
589 | (let ((stab (syntax-table))) |
590 | (unwind-protect | |
591 | (save-excursion | |
592 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
593 | (or (not (zerop (skip-syntax-backward "_w"))) | |
594 | (eq (char-syntax (following-char)) ?w) | |
595 | (eq (char-syntax (following-char)) ?_) | |
596 | (forward-sexp -1)) | |
597 | (skip-chars-forward "'") | |
598 | (let ((obj (read (current-buffer)))) | |
00d3de8e RS |
599 | (or (and (symbolp obj) (boundp obj) obj) |
600 | 0))) | |
914a48d0 | 601 | (set-syntax-table stab))) |
00d3de8e | 602 | (error 0))) |
433ae6f6 RS |
603 | |
604 | (defun describe-variable (variable) | |
605 | "Display the full documentation of VARIABLE (a symbol). | |
606 | Returns the documentation as a string, also." | |
607 | (interactive | |
608 | (let ((v (variable-at-point)) | |
609 | (enable-recursive-minibuffers t) | |
610 | val) | |
00d3de8e | 611 | (setq val (completing-read (if (symbolp v) |
433ae6f6 RS |
612 | (format "Describe variable (default %s): " v) |
613 | "Describe variable: ") | |
614 | obarray 'boundp t)) | |
615 | (list (if (equal val "") | |
616 | v (intern val))))) | |
00d3de8e | 617 | (if (symbolp variable) |
9a656d19 RS |
618 | (let (valvoid) |
619 | (with-output-to-temp-buffer "*Help*" | |
620 | (prin1 variable) | |
621 | (if (not (boundp variable)) | |
622 | (progn | |
623 | (princ " is void") | |
624 | (terpri) | |
625 | (setq valvoid t)) | |
626 | (princ "'s value is ") | |
627 | (terpri) | |
628 | (pp (symbol-value variable)) | |
629 | (terpri)) | |
630 | (if (local-variable-p variable) | |
631 | (progn | |
632 | (princ (format "Local in buffer %s; " (buffer-name))) | |
633 | (if (not (default-boundp variable)) | |
634 | (princ "globally void") | |
635 | (princ "global value is ") | |
636 | (terpri) | |
637 | (pp (default-value variable))) | |
638 | (terpri))) | |
639 | (terpri) | |
640 | (save-current-buffer | |
641 | (set-buffer standard-output) | |
642 | (if (> (count-lines (point-min) (point-max)) 10) | |
643 | (progn | |
644 | (goto-char (point-min)) | |
645 | (if valvoid | |
646 | (forward-line 1) | |
647 | (forward-sexp 1) | |
648 | (delete-region (point) (progn (end-of-line) (point))) | |
649 | (insert "'s value is shown below.\n\n") | |
650 | (save-excursion | |
651 | (insert "\n\nValue:")))))) | |
652 | (princ "Documentation:") | |
653 | (terpri) | |
654 | (let ((doc (documentation-property variable 'variable-documentation))) | |
655 | (princ (or doc "not documented as a variable."))) | |
656 | (print-help-return-message) | |
657 | (save-excursion | |
658 | (set-buffer standard-output) | |
659 | (help-mode) | |
660 | ;; Return the text we displayed. | |
661 | (buffer-string)))) | |
00d3de8e | 662 | (message "You did not specify a variable"))) |
433ae6f6 | 663 | |
e88a2c59 | 664 | (defun where-is (definition &optional insert) |
54c0b967 | 665 | "Print message listing key sequences that invoke specified command. |
e88a2c59 RS |
666 | Argument is a command definition, usually a symbol with a function definition. |
667 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer." | |
54c0b967 RS |
668 | (interactive |
669 | (let ((fn (function-called-at-point)) | |
670 | (enable-recursive-minibuffers t) | |
671 | val) | |
672 | (setq val (completing-read (if fn | |
673 | (format "Where is command (default %s): " fn) | |
674 | "Where is command: ") | |
675 | obarray 'fboundp t)) | |
676 | (list (if (equal val "") | |
e88a2c59 RS |
677 | fn (intern val)) |
678 | current-prefix-arg))) | |
54c0b967 | 679 | (let* ((keys (where-is-internal definition overriding-local-map nil nil)) |
e88a2c59 RS |
680 | (keys1 (mapconcat 'key-description keys ", ")) |
681 | (standard-output (if insert (current-buffer) t))) | |
682 | (if insert | |
683 | (if (> (length keys1) 0) | |
684 | (princ (format "%s (%s)" keys1 definition)) | |
685 | (princ (format "M-x %s RET" definition))) | |
686 | (if (> (length keys1) 0) | |
687 | (princ (format "%s is on %s" definition keys1)) | |
688 | (princ (format "%s is not on any key" definition))))) | |
54c0b967 RS |
689 | nil) |
690 | ||
a130d829 | 691 | (defun locate-library (library &optional nosuffix path interactive-call) |
2747503c | 692 | "Show the precise file name of Emacs library LIBRARY. |
433ae6f6 RS |
693 | This command searches the directories in `load-path' like `M-x load-library' |
694 | to find the file that `M-x load-library RET LIBRARY RET' would load. | |
695 | Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el' | |
9dc176a0 RS |
696 | to the specified name LIBRARY. |
697 | ||
698 | If the optional third arg PATH is specified, that list of directories | |
699 | is used instead of `load-path'." | |
a130d829 RS |
700 | (interactive (list (read-string "Locate library: ") |
701 | nil nil | |
702 | t)) | |
703 | (let (result) | |
704 | (catch 'answer | |
705 | (mapcar | |
a1c9f209 EN |
706 | (lambda (dir) |
707 | (mapcar | |
708 | (lambda (suf) | |
709 | (let ((try (expand-file-name (concat library suf) dir))) | |
710 | (and (file-readable-p try) | |
711 | (null (file-directory-p try)) | |
712 | (progn | |
713 | (setq result try) | |
714 | (throw 'answer try))))) | |
715 | (if nosuffix | |
716 | '("") | |
717 | (let ((basic '(".elc" ".el" "")) | |
718 | (compressed '(".Z" ".gz" ""))) | |
719 | ;; If autocompression mode is on, | |
720 | ;; consider all combinations of library suffixes | |
721 | ;; and compression suffixes. | |
722 | (if (rassq 'jka-compr-handler file-name-handler-alist) | |
723 | (apply 'nconc | |
724 | (mapcar (lambda (compelt) | |
725 | (mapcar (lambda (baselt) | |
726 | (concat baselt compelt)) | |
727 | basic)) | |
728 | compressed)) | |
729 | basic))))) | |
a130d829 RS |
730 | (or path load-path))) |
731 | (and interactive-call | |
732 | (if result | |
733 | (message "Library is file %s" result) | |
734 | (message "No library %s in search path" library))) | |
735 | result)) | |
1a06eabd ER |
736 | |
737 | ;;; help.el ends here |