(describe-bindings): New command (formerly in keymap.c).
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;;`M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35
36 (defvar help-map (make-sparse-keymap)
37 "Keymap for characters following the Help key.")
38
39 (defvar help-mode-map (make-sparse-keymap)
40 "Keymap for help mode.")
41
42 (define-key global-map (char-to-string help-char) 'help-command)
43 (define-key global-map [help] 'help-command)
44 (define-key global-map [f1] 'help-command)
45 (fset 'help-command help-map)
46
47 (define-key help-map (char-to-string help-char) 'help-for-help)
48 (define-key help-map [help] 'help-for-help)
49 (define-key help-map [f1] 'help-for-help)
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)
55 (define-key help-map "\C-p" 'describe-project)
56 (define-key help-map "a" 'apropos-command)
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
66 (define-key help-map "F" 'view-emacs-FAQ)
67
68 (define-key help-map "i" 'info)
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)
71 (define-key help-map "\C-i" 'info-lookup-symbol)
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
80 (define-key help-map "p" 'finder-by-keyword)
81 (autoload 'finder-by-keyword "finder"
82 "Find packages matching a given keyword." t)
83
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
92 (define-key help-map "q" 'help-quit)
93
94 (defvar help-font-lock-keywords
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 "+\\)\\(\\(:\\)\\|\\('\\)\\)")
101 '(1 (if (match-beginning 3)
102 font-lock-function-name-face
103 font-lock-variable-name-face)))
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))))
111 "Default expressions to highlight in Help mode.")
112
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)
123 (make-local-variable 'font-lock-defaults)
124 (setq font-lock-defaults '(help-font-lock-keywords))
125 (view-mode)
126 (make-local-variable 'view-no-disable-on-exit)
127 (setq view-no-disable-on-exit t)
128 (run-hooks 'help-mode-hook))
129
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
136 (defun help-quit ()
137 (interactive)
138 nil)
139
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
148 (defun help-with-tutorial (&optional arg)
149 "Select the Emacs learn-by-doing tutorial.
150 If there is a tutorial version written in the language
151 of the selected language environment, that version is used.
152 If there's no tutorial in that language, `TUTORIAL' is selected.
153 With arg, you are asked to choose which language."
154 (interactive "P")
155 (let (lang filename file)
156 (if arg
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"))
162 (setq lang current-language-environment))
163 (setq filename (or (cdr (assoc lang help-with-tutorial-alist))
164 "TUTORIAL"))
165 (setq file (expand-file-name (concat "~/" filename)))
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 "~/"))
172 (setq buffer-auto-save-file-name nil)
173 (insert-file-contents (expand-file-name filename data-directory))
174 (goto-char (point-min))
175 (search-forward "\n<<")
176 (beginning-of-line)
177 (delete-region (point) (progn (end-of-line) (point)))
178 (let ((n (- (window-height (selected-window))
179 (count-lines (point-min) (point))
180 6)))
181 (if (< n 12)
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)))))
188 (goto-char (point-min))
189 (set-buffer-modified-p nil))))
190
191 (defun describe-key-briefly (key &optional insert)
192 "Print the name of the function KEY invokes. KEY is a string.
193 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
194 (interactive "kDescribe key briefly: \nP")
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))
202 (read-event)))
203 (save-excursion
204 (let ((modifiers (event-modifiers (aref key 0)))
205 (standard-output (if insert (current-buffer) t))
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.
219 (let ((defn (key-binding key))
220 (key-desc (key-description key)))
221 (if (or (null defn) (integerp defn))
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)))))))))
230
231 (defun print-help-return-message (&optional function)
232 "Display or return message saying how to restore windows after help command.
233 Computes a message and applies the optional argument FUNCTION to it.
234 If FUNCTION is nil, applies `message' to it, thus printing it."
235 (and (not (get-buffer-window standard-output))
236 (let ((first-message
237 (cond ((special-display-p (buffer-name standard-output))
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.
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 " " "")
257 ;; If the help buffer will go in a separate frame,
258 ;; it's no use mentioning a command to scroll, so don't.
259 (if (special-display-p (buffer-name standard-output))
260 nil
261 (if (same-window-p (buffer-name standard-output))
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.
266 (substitute-command-keys
267 "\\[scroll-other-window] to scroll the help."))))))))
268
269 (defun describe-key (key)
270 "Display documentation of the function invoked by KEY. KEY is a string."
271 (interactive "kDescribe key: ")
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)))
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)
303 (princ "\n")
304 (let ((doc (documentation defn)))
305 (if doc
306 (progn (terpri)
307 (princ doc))
308 (princ "not documented")))
309 (print-help-return-message)))))))
310
311 (defun describe-mode ()
312 "Display documentation of current major mode and minor modes.
313 For this to work correctly for a minor mode, the mode's indicator variable
314 \(listed in `minor-mode-alist') must also be a function whose documentation
315 describes the minor mode."
316 (interactive)
317 (with-output-to-temp-buffer "*Help*"
318 (let ((minor-modes minor-mode-alist)
319 (first t))
320 (while minor-modes
321 (let* ((minor-mode (car (car minor-modes)))
322 (indicator (car (cdr (car minor-modes)))))
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 (symbol-value minor-mode)
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)))
336 (if first
337 (princ "The minor modes are described first,
338 followed by the major mode, which is described on the last page.\n\f\n"))
339 (setq first nil)
340 (princ (format "%s minor mode (%s):\n"
341 pretty-minor-mode
342 (if indicator
343 (format "indicator%s" indicator)
344 "no indicator")))
345 (princ (documentation minor-mode))
346 (princ "\n\f\n"))))
347 (setq minor-modes (cdr minor-modes))))
348 (princ mode-name)
349 (princ " mode:\n")
350 (princ (documentation major-mode))
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
360 (expand-file-name "DISTRIB" data-directory)))
361
362 (defun describe-copying ()
363 "Display info on how you may redistribute copies of GNU Emacs."
364 (interactive)
365 (find-file-read-only
366 (expand-file-name "COPYING" data-directory))
367 (goto-char (point-min)))
368
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
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
384 (defun describe-prefix-bindings ()
385 "Describe the bindings of the prefix used to reach this command.
386 The prefix described consists of all but the last event
387 of the key sequence that ran this command."
388 (interactive)
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)))))
399 ;; Make C-h after a prefix, when not specifically bound,
400 ;; run describe-prefix-bindings.
401 (setq prefix-help-command 'describe-prefix-bindings)
402
403 (defun view-emacs-news ()
404 "Display info on recent changes to Emacs."
405 (interactive)
406 (find-file-read-only (expand-file-name "NEWS" data-directory)))
407
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
413 (defun view-lossage ()
414 "Display last 100 input keystrokes."
415 (interactive)
416 (with-output-to-temp-buffer "*Help*"
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 " "))
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)
430 (insert "\n")))
431 (print-help-return-message)))
432
433 (defalias 'help 'help-for-help)
434 (make-help-screen help-for-help
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:"
436 "You have typed \\[help-command], the help character. Type a Help option:
437 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
438
439 a 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.
442 b describe-bindings. Display table of all key bindings.
443 c describe-key-briefly. Type a command key sequence;
444 it prints the function name that sequence runs.
445 C 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).
448 f describe-function. Type a function name and get documentation of it.
449 C-f Info-goto-emacs-command-node. Type a function name;
450 it takes you to the Info node for that command.
451 i info. The info documentation reader.
452 I describe-input-method. Describe a specific input method (if you type
453 its name) or the current input method (if you type just RET).
454 k describe-key. Type a command key sequence;
455 it displays the full documentation.
456 C-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.
458 l view-lossage. Shows last 100 characters you typed.
459 L 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).
462 m describe-mode. Print documentation of current minor modes,
463 and the current major mode, including their special commands.
464 n view-emacs-news. Shows emacs news file.
465 p finder-by-keyword. Find packages matching a given topic keyword.
466 s describe-syntax. Display contents of syntax table, plus explanations
467 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
468 v describe-variable. Type name of a variable;
469 it displays the variable's documentation and value.
470 w where-is. Type command name; it prints which keystrokes
471 invoke that command.
472
473 F Display the frequently asked questions file.
474 h Display the HELLO file which illustrates various scripts.
475 C-c Display Emacs copying permission (General Public License).
476 C-d Display Emacs ordering information.
477 C-n Display news of recent Emacs changes.
478 C-p Display information about the GNU project.
479 C-w Display information on absence of warranty for GNU Emacs."
480 help-map)
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 ()
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 ()
505 (save-excursion
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)))
513 (error nil)))
514 (set-syntax-table stab))))
515
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
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: ")
534 obarray 'fboundp t nil nil (symbol-name fn)))
535 (list (if (equal val "")
536 fn (intern val)))))
537 (if function
538 (with-output-to-temp-buffer "*Help*"
539 (prin1 function)
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 ")
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)
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))
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)
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.
609 (defun variable-at-point ()
610 (condition-case ()
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))))
621 (or (and (symbolp obj) (boundp obj) obj)
622 0)))
623 (set-syntax-table stab)))
624 (error 0)))
625
626 (defun describe-variable (variable)
627 "Display the full documentation of VARIABLE (a symbol).
628 Returns the documentation as a string, also."
629 (interactive
630 (let ((v (variable-at-point))
631 (enable-recursive-minibuffers t)
632 val)
633 (setq val (completing-read (if (symbolp v)
634 (format "Describe variable (default %s): " v)
635 "Describe variable: ")
636 obarray 'boundp t nil nil
637 (if (symbolp v) (symbol-name v))))
638 (list (if (equal val "")
639 v (intern val)))))
640 (if (symbolp variable)
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)
682 ;; Return the text we displayed.
683 (buffer-string))))
684 (message "You did not specify a variable")))
685
686 (defun describe-bindings (&optional prefix)
687 "Show a list of all defined keys, and their definitions.
688 We put that list in a buffer, and display the buffer.
689
690 The optional argument PREFIX, if non-nil, should be a key sequence;
691 then we display only bindings that start with that prefix."
692 (interactive "p")
693 (describe-bindings-internal nil prefix))
694
695 (defun where-is (definition &optional insert)
696 "Print message listing key sequences that invoke specified command.
697 Argument is a command definition, usually a symbol with a function definition.
698 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
699 (interactive
700 (let ((fn (function-called-at-point))
701 (enable-recursive-minibuffers t)
702 val)
703 (setq val (completing-read (if fn
704 (format "Where is command (default %s): " fn)
705 "Where is command: ")
706 obarray 'fboundp t))
707 (list (if (equal val "")
708 fn (intern val))
709 current-prefix-arg)))
710 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
711 (keys1 (mapconcat 'key-description keys ", "))
712 (standard-output (if insert (current-buffer) t)))
713 (if insert
714 (if (> (length keys1) 0)
715 (princ (format "%s (%s)" keys1 definition))
716 (princ (format "M-x %s RET" definition)))
717 (if (> (length keys1) 0)
718 (princ (format "%s is on %s" definition keys1))
719 (princ (format "%s is not on any key" definition)))))
720 nil)
721
722 (defun locate-library (library &optional nosuffix path interactive-call)
723 "Show the precise file name of Emacs library LIBRARY.
724 This command searches the directories in `load-path' like `M-x load-library'
725 to find the file that `M-x load-library RET LIBRARY RET' would load.
726 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
727 to the specified name LIBRARY.
728
729 If the optional third arg PATH is specified, that list of directories
730 is used instead of `load-path'."
731 (interactive (list (read-string "Locate library: ")
732 nil nil
733 t))
734 (let (result)
735 (catch 'answer
736 (mapcar
737 (lambda (dir)
738 (mapcar
739 (lambda (suf)
740 (let ((try (expand-file-name (concat library suf) dir)))
741 (and (file-readable-p try)
742 (null (file-directory-p try))
743 (progn
744 (setq result try)
745 (throw 'answer try)))))
746 (if nosuffix
747 '("")
748 '(".elc" ".el" "")
749 ;;; load doesn't handle this yet.
750 ;;; (let ((basic '(".elc" ".el" ""))
751 ;;; (compressed '(".Z" ".gz" "")))
752 ;;; ;; If autocompression mode is on,
753 ;;; ;; consider all combinations of library suffixes
754 ;;; ;; and compression suffixes.
755 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
756 ;;; (apply 'nconc
757 ;;; (mapcar (lambda (compelt)
758 ;;; (mapcar (lambda (baselt)
759 ;;; (concat baselt compelt))
760 ;;; basic))
761 ;;; compressed))
762 ;;; basic))
763 )))
764 (or path load-path)))
765 (and interactive-call
766 (if result
767 (message "Library is file %s" result)
768 (message "No library %s in search path" library)))
769 result))
770
771 ;;; help.el ends here