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