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