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