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