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