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