Document `debugger-toggle-locals'.
[emacs.git] / lisp / apropos.el
blobb7c5aaddcb17c3bbe8d00dd76a8ccf716c86e072
1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994-1995, 2001-2013 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
7 ;; Daniel Pfeiffer <occitan@esperanto.org> (rewrite)
8 ;; Keywords: help
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; The ideas for this package were derived from the C code in
29 ;; src/keymap.c and elsewhere. The functions in this file should
30 ;; always be byte-compiled for speed. Someone should rewrite this in
31 ;; C (as part of src/keymap.c) for speed.
33 ;; The idea for super-apropos is based on the original implementation
34 ;; by Lynn Slater <lrs@esl.com>.
36 ;; History:
37 ;; Fixed bug, current-local-map can return nil.
38 ;; Change, doesn't calculate key-bindings unless needed.
39 ;; Added super-apropos capability, changed print functions.
40 ;; Made fast-apropos and super-apropos share code.
41 ;; Sped up fast-apropos again.
42 ;; Added apropos-do-all option.
43 ;; Added fast-command-apropos.
44 ;; Changed doc strings to comments for helping functions.
45 ;; Made doc file buffer read-only, buried it.
46 ;; Only call substitute-command-keys if do-all set.
48 ;; Optionally use configurable faces to make the output more legible.
49 ;; Differentiate between command, function and macro.
50 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
51 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
52 ;; Apropos-documentation (ex super-apropos) now finds all it should.
53 ;; New apropos-value snoops through all values and optionally plists.
54 ;; Reading DOC file doesn't load nroff.
55 ;; Added hypertext following of documentation, mouse-2 on variable gives value
56 ;; from buffer in active window.
58 ;;; Code:
60 (require 'button)
62 (defgroup apropos nil
63 "Apropos commands for users and programmers."
64 :group 'help
65 :prefix "apropos")
67 ;; I see a degradation of maybe 10-20% only.
68 (defcustom apropos-do-all nil
69 "Non nil means apropos commands will search more extensively.
70 This may be slower. This option affects the following commands:
72 `apropos-user-option' will search all variables, not just user options.
73 `apropos-command' will also search non-interactive functions.
74 `apropos' will search all symbols, not just functions, variables, faces,
75 and those with property lists.
76 `apropos-value' will also search in property lists and functions.
77 `apropos-documentation' will search all documentation strings, not just
78 those in the etc/DOC documentation file.
80 This option only controls the default behavior. Each of the above
81 commands also has an optional argument to request a more extensive search.
83 Additionally, this option makes the function `apropos-library'
84 include key-binding information in its output."
85 :group 'apropos
86 :type 'boolean)
88 (defface apropos-symbol
89 '((t (:inherit bold)))
90 "Face for the symbol name in Apropos output."
91 :group 'apropos
92 :version "24.3")
94 (defface apropos-keybinding
95 '((t (:inherit underline)))
96 "Face for lists of keybinding in Apropos output."
97 :group 'apropos
98 :version "24.3")
100 (defface apropos-property
101 '((t (:inherit font-lock-builtin-face)))
102 "Face for property name in apropos output, or nil for none."
103 :group 'apropos
104 :version "24.3")
106 (defface apropos-function-button
107 '((t (:inherit (font-lock-function-name-face button))))
108 "Button face indicating a function, macro, or command in Apropos."
109 :group 'apropos
110 :version "24.3")
112 (defface apropos-variable-button
113 '((t (:inherit (font-lock-variable-name-face button))))
114 "Button face indicating a variable in Apropos."
115 :group 'apropos
116 :version "24.3")
118 (defface apropos-user-option-button
119 '((t (:inherit (font-lock-variable-name-face button))))
120 "Button face indicating a user option in Apropos."
121 :group 'apropos
122 :version "24.4")
124 (defface apropos-misc-button
125 '((t (:inherit (font-lock-constant-face button))))
126 "Button face indicating a miscellaneous object type in Apropos."
127 :group 'apropos
128 :version "24.3")
130 (defcustom apropos-match-face 'match
131 "Face for matching text in Apropos documentation/value, or nil for none.
132 This applies when you look for matches in the documentation or variable value
133 for the pattern; the part that matches gets displayed in this font."
134 :group 'apropos
135 :version "24.3")
137 (defcustom apropos-sort-by-scores nil
138 "Non-nil means sort matches by scores; best match is shown first.
139 This applies to all `apropos' commands except `apropos-documentation'.
140 If value is `verbose', the computed score is shown for each match."
141 :group 'apropos
142 :type '(choice (const :tag "off" nil)
143 (const :tag "on" t)
144 (const :tag "show scores" verbose)))
146 (defcustom apropos-documentation-sort-by-scores t
147 "Non-nil means sort matches by scores; best match is shown first.
148 This applies to `apropos-documentation' only.
149 If value is `verbose', the computed score is shown for each match."
150 :group 'apropos
151 :type '(choice (const :tag "off" nil)
152 (const :tag "on" t)
153 (const :tag "show scores" verbose)))
155 (defvar apropos-mode-map
156 (let ((map (copy-keymap button-buffer-map)))
157 (set-keymap-parent map special-mode-map)
158 ;; Use `apropos-follow' instead of just using the button
159 ;; definition of RET, so that users can use it anywhere in an
160 ;; apropos item, not just on top of a button.
161 (define-key map "\C-m" 'apropos-follow)
162 map)
163 "Keymap used in Apropos mode.")
165 (defvar apropos-mode-hook nil
166 "Hook run when mode is turned on.")
168 (defvar apropos-pattern nil
169 "Apropos pattern as entered by user.")
171 (defvar apropos-pattern-quoted nil
172 "Apropos pattern passed through `regexp-quote'.")
174 (defvar apropos-words ()
175 "Current list of apropos words extracted from `apropos-pattern'.")
177 (defvar apropos-all-words ()
178 "Current list of words and synonyms.")
180 (defvar apropos-regexp nil
181 "Regexp used in current apropos run.")
183 (defvar apropos-all-words-regexp nil
184 "Regexp matching apropos-all-words.")
186 (defvar apropos-files-scanned ()
187 "List of elc files already scanned in current run of `apropos-documentation'.")
189 (defvar apropos-accumulator ()
190 "Alist of symbols already found in current apropos run.
191 Each element has the form
193 (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
195 where SYMBOL is the symbol name, SCORE is its relevance score (a
196 number), FUN-DOC is the function docstring, VAR-DOC is the
197 variable docstring, PLIST is the list of the symbols names in the
198 property list, WIDGET-DOC is the widget docstring, FACE-DOC is
199 the face docstring, and CUS-GROUP-DOC is the custom group
200 docstring. Each docstring is either nil or a string.")
202 (defvar apropos-item ()
203 "Current item in or for `apropos-accumulator'.")
205 (defvar apropos-synonyms '(
206 ("find" "open" "edit")
207 ("kill" "cut")
208 ("yank" "paste")
209 ("region" "selection"))
210 "List of synonyms known by apropos.
211 Each element is a list of words where the first word is the standard Emacs
212 term, and the rest of the words are alternative terms.")
215 ;;; Button types used by apropos
217 (define-button-type 'apropos-symbol
218 'face 'apropos-symbol
219 'help-echo "mouse-2, RET: Display more help on this symbol"
220 'follow-link t
221 'action #'apropos-symbol-button-display-help)
223 (defun apropos-symbol-button-display-help (button)
224 "Display further help for the `apropos-symbol' button BUTTON."
225 (button-activate
226 (or (apropos-next-label-button (button-start button))
227 (error "There is nothing to follow for `%s'" (button-label button)))))
229 (define-button-type 'apropos-function
230 'apropos-label "Function"
231 'apropos-short-label "f"
232 'face 'apropos-function-button
233 'help-echo "mouse-2, RET: Display more help on this function"
234 'follow-link t
235 'action (lambda (button)
236 (describe-function (button-get button 'apropos-symbol))))
238 (define-button-type 'apropos-macro
239 'apropos-label "Macro"
240 'apropos-short-label "m"
241 'face 'apropos-function-button
242 'help-echo "mouse-2, RET: Display more help on this macro"
243 'follow-link t
244 'action (lambda (button)
245 (describe-function (button-get button 'apropos-symbol))))
247 (define-button-type 'apropos-command
248 'apropos-label "Command"
249 'apropos-short-label "c"
250 'face 'apropos-function-button
251 'help-echo "mouse-2, RET: Display more help on this command"
252 'follow-link t
253 'action (lambda (button)
254 (describe-function (button-get button 'apropos-symbol))))
256 ;; We used to use `customize-variable-other-window' instead for a
257 ;; customizable variable, but that is slow. It is better to show an
258 ;; ordinary help buffer and let the user click on the customization
259 ;; button in that buffer, if he wants to.
260 ;; Likewise for `customize-face-other-window'.
261 (define-button-type 'apropos-variable
262 'apropos-label "Variable"
263 'apropos-short-label "v"
264 'face 'apropos-variable-button
265 'help-echo "mouse-2, RET: Display more help on this variable"
266 'follow-link t
267 'action (lambda (button)
268 (describe-variable (button-get button 'apropos-symbol))))
270 (define-button-type 'apropos-user-option
271 'apropos-label "User option"
272 'apropos-short-label "o"
273 'face 'apropos-user-option-button
274 'help-echo "mouse-2, RET: Display more help on this user option"
275 'follow-link t
276 'action (lambda (button)
277 (describe-variable (button-get button 'apropos-symbol))))
279 (define-button-type 'apropos-face
280 'apropos-label "Face"
281 'apropos-short-label "F"
282 'face '(font-lock-variable-name-face button)
283 'help-echo "mouse-2, RET: Display more help on this face"
284 'follow-link t
285 'action (lambda (button)
286 (describe-face (button-get button 'apropos-symbol))))
288 (define-button-type 'apropos-group
289 'apropos-label "Group"
290 'apropos-short-label "g"
291 'face 'apropos-misc-button
292 'help-echo "mouse-2, RET: Display more help on this group"
293 'follow-link t
294 'action (lambda (button)
295 (customize-group-other-window
296 (button-get button 'apropos-symbol))))
298 (define-button-type 'apropos-widget
299 'apropos-label "Widget"
300 'apropos-short-label "w"
301 'face 'apropos-misc-button
302 'help-echo "mouse-2, RET: Display more help on this widget"
303 'follow-link t
304 'action (lambda (button)
305 (widget-browse-other-window (button-get button 'apropos-symbol))))
307 (define-button-type 'apropos-plist
308 'apropos-label "Properties"
309 'apropos-short-label "p"
310 'face 'apropos-misc-button
311 'help-echo "mouse-2, RET: Display more help on this plist"
312 'follow-link t
313 'action (lambda (button)
314 (apropos-describe-plist (button-get button 'apropos-symbol))))
316 (define-button-type 'apropos-library
317 'help-echo "mouse-2, RET: Display more help on this library"
318 'follow-link t
319 'action (lambda (button)
320 (apropos-library (button-get button 'apropos-symbol))))
322 (defun apropos-next-label-button (pos)
323 "Return the next apropos label button after POS, or nil if there's none.
324 Will also return nil if more than one `apropos-symbol' button is encountered
325 before finding a label."
326 (let* ((button (next-button pos t))
327 (already-hit-symbol nil)
328 (label (and button (button-get button 'apropos-label)))
329 (type (and button (button-get button 'type))))
330 (while (and button
331 (not label)
332 (or (not (eq type 'apropos-symbol))
333 (not already-hit-symbol)))
334 (when (eq type 'apropos-symbol)
335 (setq already-hit-symbol t))
336 (setq button (next-button (button-start button)))
337 (when button
338 (setq label (button-get button 'apropos-label))
339 (setq type (button-get button 'type))))
340 (and label button)))
343 (defun apropos-words-to-regexp (words wild)
344 "Make regexp matching any two of the words in WORDS.
345 WILD should be a subexpression matching wildcards between matches."
346 (setq words (delete-dups (copy-sequence words)))
347 (if (null (cdr words))
348 (car words)
349 (mapconcat
350 (lambda (w)
351 (concat "\\(?:" w "\\)" ;; parens for synonyms
352 wild "\\(?:"
353 (mapconcat 'identity
354 (delq w (copy-sequence words))
355 "\\|")
356 "\\)"))
357 words
358 "\\|")))
360 ;;;###autoload
361 (defun apropos-read-pattern (subject)
362 "Read an apropos pattern, either a word list or a regexp.
363 Returns the user pattern, either a list of words which are matched
364 literally, or a string which is used as a regexp to search for.
366 SUBJECT is a string that is included in the prompt to identify what
367 kind of objects to search."
368 (let ((pattern
369 (read-string (concat "Search for " subject " (word list or regexp): "))))
370 (if (string-equal (regexp-quote pattern) pattern)
371 ;; Split into words
372 (split-string pattern "[ \t]+" t)
373 pattern)))
375 (defun apropos-parse-pattern (pattern)
376 "Rewrite a list of words to a regexp matching all permutations.
377 If PATTERN is a string, that means it is already a regexp.
378 This updates variables `apropos-pattern', `apropos-pattern-quoted',
379 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
380 (setq apropos-words nil
381 apropos-all-words nil)
382 (if (consp pattern)
383 ;; We don't actually make a regexp matching all permutations.
384 ;; Instead, for e.g. "a b c", we make a regexp matching
385 ;; any combination of two or more words like this:
386 ;; (a|b|c).*(a|b|c) which may give some false matches,
387 ;; but as long as it also gives the right ones, that's ok.
388 (let ((words pattern))
389 (setq apropos-pattern (mapconcat 'identity pattern " ")
390 apropos-pattern-quoted (regexp-quote apropos-pattern))
391 (dolist (word words)
392 (let ((syn apropos-synonyms) (s word) (a word))
393 (while syn
394 (if (member word (car syn))
395 (progn
396 (setq a (mapconcat 'identity (car syn) "\\|"))
397 (if (member word (cdr (car syn)))
398 (setq s a))
399 (setq syn nil))
400 (setq syn (cdr syn))))
401 (setq apropos-words (cons s apropos-words)
402 apropos-all-words (cons a apropos-all-words))))
403 (setq apropos-all-words-regexp
404 (apropos-words-to-regexp apropos-all-words ".+"))
405 (setq apropos-regexp
406 (apropos-words-to-regexp apropos-words ".*?")))
407 (setq apropos-pattern-quoted (regexp-quote pattern)
408 apropos-all-words-regexp pattern
409 apropos-pattern pattern
410 apropos-regexp pattern)))
413 (defun apropos-calc-scores (str words)
414 "Return apropos scores for string STR matching WORDS.
415 Value is a list of offsets of the words into the string."
416 (let (scores i)
417 (if words
418 (dolist (word words scores)
419 (if (setq i (string-match word str))
420 (setq scores (cons i scores))))
421 ;; Return list of start and end position of regexp
422 (and (string-match apropos-pattern str)
423 (list (match-beginning 0) (match-end 0))))))
425 (defun apropos-score-str (str)
426 "Return apropos score for string STR."
427 (if str
428 (let* ((l (length str))
429 (score (- (/ l 10))))
430 (dolist (s (apropos-calc-scores str apropos-all-words) score)
431 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
434 (defun apropos-score-doc (doc)
435 "Return apropos score for documentation string DOC."
436 (let ((l (length doc)))
437 (if (> l 0)
438 (let ((score 0))
439 (when (string-match apropos-pattern-quoted doc)
440 (setq score 10000))
441 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
442 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
443 0)))
445 (defun apropos-score-symbol (symbol &optional weight)
446 "Return apropos score for SYMBOL."
447 (setq symbol (symbol-name symbol))
448 (let ((score 0)
449 (l (length symbol)))
450 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
451 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
453 (defun apropos-true-hit (str words)
454 "Return t if STR is a genuine hit.
455 This may fail if only one of the keywords is matched more than once.
456 This requires that at least 2 keywords (unless only one was given)."
457 (or (not str)
458 (not words)
459 (not (cdr words))
460 (> (length (apropos-calc-scores str words)) 1)))
462 (defun apropos-false-hit-symbol (symbol)
463 "Return t if SYMBOL is not really matched by the current keywords."
464 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
466 (defun apropos-false-hit-str (str)
467 "Return t if STR is not really matched by the current keywords."
468 (not (apropos-true-hit str apropos-words)))
470 (defun apropos-true-hit-doc (doc)
471 "Return t if DOC is really matched by the current keywords."
472 (apropos-true-hit doc apropos-all-words))
474 (define-derived-mode apropos-mode special-mode "Apropos"
475 "Major mode for following hyperlinks in output of apropos commands.
477 \\{apropos-mode-map}")
479 (defvar apropos-multi-type t
480 "If non-nil, this apropos query concerns multiple types.
481 This is used to decide whether to print the result's type or not.")
483 ;;;###autoload
484 (defun apropos-user-option (pattern &optional do-all)
485 "Show user options that match PATTERN.
486 PATTERN can be a word, a list of words (separated by spaces),
487 or a regexp (using some regexp special characters). If it is a word,
488 search for matches for that word as a substring. If it is a list of words,
489 search for matches for any two (or more) of those words.
491 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
492 variables, not just user options."
493 (interactive (list (apropos-read-pattern
494 (if (or current-prefix-arg apropos-do-all)
495 "variable" "user option"))
496 current-prefix-arg))
497 (apropos-command pattern nil
498 (if (or do-all apropos-do-all)
499 #'(lambda (symbol)
500 (and (boundp symbol)
501 (get symbol 'variable-documentation)))
502 'custom-variable-p)))
504 ;;;###autoload
505 (defun apropos-variable (pattern &optional do-not-all)
506 "Show variables that match PATTERN.
507 When DO-NOT-ALL is not-nil, show user options only, i.e. behave
508 like `apropos-user-option'."
509 (interactive (list (apropos-read-pattern
510 (if current-prefix-arg "user option" "variable"))
511 current-prefix-arg))
512 (let ((apropos-do-all (if do-not-all nil t)))
513 (apropos-user-option pattern)))
515 ;; For auld lang syne:
516 ;;;###autoload
517 (defalias 'command-apropos 'apropos-command)
518 ;;;###autoload
519 (defun apropos-command (pattern &optional do-all var-predicate)
520 "Show commands (interactively callable functions) that match PATTERN.
521 PATTERN can be a word, a list of words (separated by spaces),
522 or a regexp (using some regexp special characters). If it is a word,
523 search for matches for that word as a substring. If it is a list of words,
524 search for matches for any two (or more) of those words.
526 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
527 noninteractive functions.
529 If VAR-PREDICATE is non-nil, show only variables, and only those that
530 satisfy the predicate VAR-PREDICATE.
532 When called from a Lisp program, a string PATTERN is used as a regexp,
533 while a list of strings is used as a word list."
534 (interactive (list (apropos-read-pattern
535 (if (or current-prefix-arg apropos-do-all)
536 "command or function" "command"))
537 current-prefix-arg))
538 (apropos-parse-pattern pattern)
539 (let ((message
540 (let ((standard-output (get-buffer-create "*Apropos*")))
541 (help-print-return-message 'identity))))
542 (or do-all (setq do-all apropos-do-all))
543 (setq apropos-accumulator
544 (apropos-internal apropos-regexp
545 (or var-predicate
546 ;; We used to use `functionp' here, but this
547 ;; rules out macros. `fboundp' rules in
548 ;; keymaps, but it seems harmless.
549 (if do-all 'fboundp 'commandp))))
550 (let ((tem apropos-accumulator))
551 (while tem
552 (if (or (get (car tem) 'apropos-inhibit)
553 (apropos-false-hit-symbol (car tem)))
554 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
555 (setq tem (cdr tem))))
556 (let ((p apropos-accumulator)
557 doc symbol score)
558 (while p
559 (setcar p (list
560 (setq symbol (car p))
561 (setq score (apropos-score-symbol symbol))
562 (unless var-predicate
563 (if (fboundp symbol)
564 (if (setq doc (condition-case nil
565 (documentation symbol t)
566 (error 'error)))
567 ;; Eg alias to undefined function.
568 (if (eq doc 'error)
569 "(documentation error)"
570 (setq score (+ score (apropos-score-doc doc)))
571 (substring doc 0 (string-match "\n" doc)))
572 "(not documented)")))
573 (and var-predicate
574 (funcall var-predicate symbol)
575 (if (setq doc (documentation-property
576 symbol 'variable-documentation t))
577 (progn
578 (setq score (+ score (apropos-score-doc doc)))
579 (substring doc 0
580 (string-match "\n" doc)))))))
581 (setcar (cdr (car p)) score)
582 (setq p (cdr p))))
583 (and (let ((apropos-multi-type do-all))
584 (apropos-print t nil nil t))
585 message
586 (message "%s" message))))
589 ;;;###autoload
590 (defun apropos-documentation-property (symbol property raw)
591 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
592 (condition-case ()
593 (let ((doc (documentation-property symbol property raw)))
594 (if doc (substring doc 0 (string-match "\n" doc))
595 "(not documented)"))
596 (error "(error retrieving documentation)")))
599 ;;;###autoload
600 (defun apropos (pattern &optional do-all)
601 "Show all meaningful Lisp symbols whose names match PATTERN.
602 Symbols are shown if they are defined as functions, variables, or
603 faces, or if they have nonempty property lists.
605 PATTERN can be a word, a list of words (separated by spaces),
606 or a regexp (using some regexp special characters). If it is a word,
607 search for matches for that word as a substring. If it is a list of words,
608 search for matches for any two (or more) of those words.
610 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
611 consider all symbols (if they match PATTERN).
613 Returns list of symbols and documentation found."
614 (interactive (list (apropos-read-pattern "symbol")
615 current-prefix-arg))
616 (apropos-parse-pattern pattern)
617 (apropos-symbols-internal
618 (apropos-internal apropos-regexp
619 (and (not do-all)
620 (not apropos-do-all)
621 (lambda (symbol)
622 (or (fboundp symbol)
623 (boundp symbol)
624 (facep symbol)
625 (symbol-plist symbol)))))
626 (or do-all apropos-do-all)))
628 (defun apropos-library-button (sym)
629 (if (null sym)
630 "<nothing>"
631 (let ((name (copy-sequence (symbol-name sym))))
632 (make-text-button name nil
633 'type 'apropos-library
634 'face 'apropos-symbol
635 'apropos-symbol name)
636 name)))
638 ;;;###autoload
639 (defun apropos-library (file)
640 "List the variables and functions defined by library FILE.
641 FILE should be one of the libraries currently loaded and should
642 thus be found in `load-history'. If `apropos-do-all' is non-nil,
643 the output includes key-bindings of commands."
644 (interactive
645 (let* ((libs (delq nil (mapcar 'car load-history)))
646 (libs
647 (nconc (delq nil
648 (mapcar
649 (lambda (l)
650 (setq l (file-name-nondirectory l))
651 (while
652 (not (equal (setq l (file-name-sans-extension l))
653 l)))
655 libs))
656 libs)))
657 (list (completing-read "Describe library: " libs nil t))))
658 (let ((symbols nil)
659 ;; (autoloads nil)
660 (provides nil)
661 (requires nil)
662 (lh-entry (assoc file load-history)))
663 (unless lh-entry
664 ;; `file' may be the "shortname".
665 (let ((lh load-history)
666 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file)
667 "\\(\\.\\|\\'\\)")))
668 (while (and lh (null lh-entry))
669 (if (and (caar lh) (string-match re (caar lh)))
670 (setq lh-entry (car lh))
671 (setq lh (cdr lh)))))
672 (unless lh-entry (error "Unknown library `%s'" file)))
673 (dolist (x (cdr lh-entry))
674 (pcase (car-safe x)
675 ;; (autoload (push (cdr x) autoloads))
676 (`require (push (cdr x) requires))
677 (`provide (push (cdr x) provides))
678 (_ (push (or (cdr-safe x) x) symbols))))
679 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
680 (apropos-symbols-internal
681 symbols apropos-do-all
682 (concat
683 (format "Library `%s' provides: %s\nand requires: %s"
684 file
685 (mapconcat 'apropos-library-button
686 (or provides '(nil)) " and ")
687 (mapconcat 'apropos-library-button
688 (or requires '(nil)) " and ")))))))
690 (defun apropos-symbols-internal (symbols keys &optional text)
691 ;; Filter out entries that are marked as apropos-inhibit.
692 (let ((all nil))
693 (dolist (symbol symbols)
694 (unless (get symbol 'apropos-inhibit)
695 (push symbol all)))
696 (setq symbols all))
697 (let ((apropos-accumulator
698 (mapcar
699 (lambda (symbol)
700 (let (doc properties)
701 (list
702 symbol
703 (apropos-score-symbol symbol)
704 (when (fboundp symbol)
705 (if (setq doc (condition-case nil
706 (documentation symbol t)
707 (void-function
708 "(alias for undefined function)")
709 (error
710 "(can't retrieve function documentation)")))
711 (substring doc 0 (string-match "\n" doc))
712 "(not documented)"))
713 (when (boundp symbol)
714 (apropos-documentation-property
715 symbol 'variable-documentation t))
716 (when (setq properties (symbol-plist symbol))
717 (setq doc (list (car properties)))
718 (while (setq properties (cdr (cdr properties)))
719 (setq doc (cons (car properties) doc)))
720 (mapconcat #'symbol-name (nreverse doc) " "))
721 (when (get symbol 'widget-type)
722 (apropos-documentation-property
723 symbol 'widget-documentation t))
724 (when (facep symbol)
725 (let ((alias (get symbol 'face-alias)))
726 (if alias
727 (if (facep alias)
728 (format "%slias for the face `%s'."
729 (if (get symbol 'obsolete-face)
730 "Obsolete a"
731 "A")
732 alias)
733 ;; Never happens in practice because fails
734 ;; (facep symbol) test.
735 "(alias for undefined face)")
736 (apropos-documentation-property
737 symbol 'face-documentation t))))
738 (when (get symbol 'custom-group)
739 (apropos-documentation-property
740 symbol 'group-documentation t)))))
741 symbols)))
742 (apropos-print keys nil text)))
745 ;;;###autoload
746 (defun apropos-value (pattern &optional do-all)
747 "Show all symbols whose value's printed representation matches PATTERN.
748 PATTERN can be a word, a list of words (separated by spaces),
749 or a regexp (using some regexp special characters). If it is a word,
750 search for matches for that word as a substring. If it is a list of words,
751 search for matches for any two (or more) of those words.
753 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
754 at function definitions (arguments, documentation and body) and at the
755 names and values of properties.
757 Returns list of symbols and values found."
758 (interactive (list (apropos-read-pattern "value")
759 current-prefix-arg))
760 (apropos-parse-pattern pattern)
761 (or do-all (setq do-all apropos-do-all))
762 (setq apropos-accumulator ())
763 (let (f v p)
764 (mapatoms
765 (lambda (symbol)
766 (setq f nil v nil p nil)
767 (or (memq symbol '(apropos-regexp
768 apropos-pattern apropos-all-words-regexp
769 apropos-words apropos-all-words
770 do-all apropos-accumulator
771 symbol f v p))
772 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
773 (if do-all
774 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
775 p (apropos-format-plist symbol "\n " t)))
776 (if (apropos-false-hit-str v)
777 (setq v nil))
778 (if (apropos-false-hit-str f)
779 (setq f nil))
780 (if (apropos-false-hit-str p)
781 (setq p nil))
782 (if (or f v p)
783 (setq apropos-accumulator (cons (list symbol
784 (+ (apropos-score-str f)
785 (apropos-score-str v)
786 (apropos-score-str p))
787 f v p)
788 apropos-accumulator))))))
789 (let ((apropos-multi-type do-all))
790 (apropos-print nil "\n----------------\n")))
793 ;;;###autoload
794 (defun apropos-documentation (pattern &optional do-all)
795 "Show symbols whose documentation contains matches for PATTERN.
796 PATTERN can be a word, a list of words (separated by spaces),
797 or a regexp (using some regexp special characters). If it is a word,
798 search for matches for that word as a substring. If it is a list of words,
799 search for matches for any two (or more) of those words.
801 Note that by default this command only searches in the file specified by
802 `internal-doc-file-name'; i.e., the etc/DOC file. With \\[universal-argument] prefix,
803 or if `apropos-do-all' is non-nil, it searches all currently defined
804 documentation strings.
806 Returns list of symbols and documentation found."
807 ;; The doc used to say that DO-ALL includes key-bindings info in the
808 ;; output, but I cannot see that that is true.
809 (interactive (list (apropos-read-pattern "documentation")
810 current-prefix-arg))
811 (apropos-parse-pattern pattern)
812 (or do-all (setq do-all apropos-do-all))
813 (setq apropos-accumulator () apropos-files-scanned ())
814 (let ((standard-input (get-buffer-create " apropos-temp"))
815 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
816 f v sf sv)
817 (unwind-protect
818 (with-current-buffer standard-input
819 (apropos-documentation-check-doc-file)
820 (if do-all
821 (mapatoms
822 (lambda (symbol)
823 (setq f (apropos-safe-documentation symbol)
824 v (get symbol 'variable-documentation))
825 (if (integerp v) (setq v))
826 (setq f (apropos-documentation-internal f)
827 v (apropos-documentation-internal v))
828 (setq sf (apropos-score-doc f)
829 sv (apropos-score-doc v))
830 (if (or f v)
831 (if (setq apropos-item
832 (cdr (assq symbol apropos-accumulator)))
833 (progn
834 (if f
835 (progn
836 (setcar (nthcdr 1 apropos-item) f)
837 (setcar apropos-item (+ (car apropos-item) sf))))
838 (if v
839 (progn
840 (setcar (nthcdr 2 apropos-item) v)
841 (setcar apropos-item (+ (car apropos-item) sv)))))
842 (setq apropos-accumulator
843 (cons (list symbol
844 (+ (apropos-score-symbol symbol 2) sf sv)
845 f v)
846 apropos-accumulator)))))))
847 (apropos-print nil "\n----------------\n" nil t))
848 (kill-buffer standard-input))))
851 (defun apropos-value-internal (predicate symbol function)
852 (if (funcall predicate symbol)
853 (progn
854 (setq symbol (prin1-to-string (funcall function symbol)))
855 (if (string-match apropos-regexp symbol)
856 (progn
857 (if apropos-match-face
858 (put-text-property (match-beginning 0) (match-end 0)
859 'face apropos-match-face
860 symbol))
861 symbol)))))
863 (defun apropos-documentation-internal (doc)
864 (if (consp doc)
865 (apropos-documentation-check-elc-file (car doc))
866 (if (and doc
867 (string-match apropos-all-words-regexp doc)
868 (apropos-true-hit-doc doc))
869 (when apropos-match-face
870 (setq doc (substitute-command-keys (copy-sequence doc)))
871 (if (or (string-match apropos-pattern-quoted doc)
872 (string-match apropos-all-words-regexp doc))
873 (put-text-property (match-beginning 0)
874 (match-end 0)
875 'face apropos-match-face doc))
876 doc))))
878 (defun apropos-format-plist (pl sep &optional compare)
879 (setq pl (symbol-plist pl))
880 (let (p p-out)
881 (while pl
882 (setq p (format "%s %S" (car pl) (nth 1 pl)))
883 (if (or (not compare) (string-match apropos-regexp p))
884 (put-text-property 0 (length (symbol-name (car pl)))
885 'face 'apropos-property p)
886 (setq p nil))
887 (if p
888 (progn
889 (and compare apropos-match-face
890 (put-text-property (match-beginning 0) (match-end 0)
891 'face apropos-match-face
893 (setq p-out (concat p-out (if p-out sep) p))))
894 (setq pl (nthcdr 2 pl)))
895 p-out))
898 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
900 (defun apropos-documentation-check-doc-file ()
901 (let (type symbol (sepa 2) sepb doc)
902 (insert ?\^_)
903 (backward-char)
904 (insert-file-contents (concat doc-directory internal-doc-file-name))
905 (forward-char)
906 (while (save-excursion
907 (setq sepb (search-forward "\^_"))
908 (not (eobp)))
909 (beginning-of-line 2)
910 (if (save-restriction
911 (narrow-to-region (point) (1- sepb))
912 (re-search-forward apropos-all-words-regexp nil t))
913 (progn
914 (goto-char (1+ sepa))
915 (setq type (if (eq ?F (preceding-char))
916 2 ; function documentation
917 3) ; variable documentation
918 symbol (read)
919 doc (buffer-substring (1+ (point)) (1- sepb)))
920 (when (and (apropos-true-hit-doc doc)
921 ;; The DOC file lists all built-in funcs and vars.
922 ;; If any are not currently bound, they can
923 ;; only be platform-specific stuff (eg NS) not
924 ;; in use on the current platform.
925 ;; So we exclude them.
926 (cond ((= 3 type) (boundp symbol))
927 ((= 2 type) (fboundp symbol))))
928 (or (and (setq apropos-item (assq symbol apropos-accumulator))
929 (setcar (cdr apropos-item)
930 (apropos-score-doc doc)))
931 (setq apropos-item (list symbol
932 (+ (apropos-score-symbol symbol 2)
933 (apropos-score-doc doc))
934 nil nil)
935 apropos-accumulator (cons apropos-item
936 apropos-accumulator)))
937 (when apropos-match-face
938 (setq doc (substitute-command-keys doc))
939 (if (or (string-match apropos-pattern-quoted doc)
940 (string-match apropos-all-words-regexp doc))
941 (put-text-property (match-beginning 0)
942 (match-end 0)
943 'face apropos-match-face doc)))
944 (setcar (nthcdr type apropos-item) doc))))
945 (setq sepa (goto-char sepb)))))
947 (defun apropos-documentation-check-elc-file (file)
948 (if (member file apropos-files-scanned)
950 (let (symbol doc beg end this-is-a-variable)
951 (setq apropos-files-scanned (cons file apropos-files-scanned))
952 (erase-buffer)
953 (insert-file-contents file)
954 (while (search-forward "\n#@" nil t)
955 ;; Read the comment length, and advance over it.
956 (setq end (read)
957 beg (1+ (point))
958 end (+ (point) end -1))
959 (forward-char)
960 (if (save-restriction
961 ;; match ^ and $ relative to doc string
962 (narrow-to-region beg end)
963 (re-search-forward apropos-all-words-regexp nil t))
964 (progn
965 (goto-char (+ end 2))
966 (setq doc (buffer-substring beg end)
967 end (- (match-end 0) beg)
968 beg (- (match-beginning 0) beg))
969 (when (apropos-true-hit-doc doc)
970 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
971 symbol (progn
972 (skip-chars-forward "(a-z")
973 (forward-char)
974 (read))
975 symbol (if (consp symbol)
976 (nth 1 symbol)
977 symbol))
978 (if (if this-is-a-variable
979 (get symbol 'variable-documentation)
980 (and (fboundp symbol) (apropos-safe-documentation symbol)))
981 (progn
982 (or (and (setq apropos-item (assq symbol apropos-accumulator))
983 (setcar (cdr apropos-item)
984 (+ (cadr apropos-item) (apropos-score-doc doc))))
985 (setq apropos-item (list symbol
986 (+ (apropos-score-symbol symbol 2)
987 (apropos-score-doc doc))
988 nil nil)
989 apropos-accumulator (cons apropos-item
990 apropos-accumulator)))
991 (when apropos-match-face
992 (setq doc (substitute-command-keys doc))
993 (if (or (string-match apropos-pattern-quoted doc)
994 (string-match apropos-all-words-regexp doc))
995 (put-text-property (match-beginning 0)
996 (match-end 0)
997 'face apropos-match-face doc)))
998 (setcar (nthcdr (if this-is-a-variable 3 2)
999 apropos-item)
1000 doc))))))))))
1004 (defun apropos-safe-documentation (function)
1005 "Like `documentation', except it avoids calling `get_doc_string'.
1006 Will return nil instead."
1007 (while (and function (symbolp function))
1008 (setq function (if (fboundp function)
1009 (symbol-function function))))
1010 (if (eq (car-safe function) 'macro)
1011 (setq function (cdr function)))
1012 (setq function (if (byte-code-function-p function)
1013 (if (> (length function) 4)
1014 (aref function 4))
1015 (if (autoloadp function)
1016 (nth 2 function)
1017 (if (eq (car-safe function) 'lambda)
1018 (if (stringp (nth 2 function))
1019 (nth 2 function)
1020 (if (stringp (nth 3 function))
1021 (nth 3 function)))))))
1022 (if (integerp function)
1024 function))
1026 (defcustom apropos-compact-layout nil
1027 "If non-nil, use a single line per binding."
1028 :type 'boolean)
1030 (defun apropos-print (do-keys spacing &optional text nosubst)
1031 "Output result of apropos searching into buffer `*Apropos*'.
1032 The value of `apropos-accumulator' is the list of items to output.
1033 Each element should have the format
1034 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
1035 The return value is the list that was in `apropos-accumulator', sorted
1036 alphabetically by symbol name; but this function also sets
1037 `apropos-accumulator' to nil before returning.
1039 If SPACING is non-nil, it should be a string; separate items with that string.
1040 If non-nil TEXT is a string that will be printed as a heading."
1041 (if (null apropos-accumulator)
1042 (message "No apropos matches for `%s'" apropos-pattern)
1043 (setq apropos-accumulator
1044 (sort apropos-accumulator
1045 (lambda (a b)
1046 ;; Don't sort by score if user can't see the score.
1047 ;; It would be confusing. -- rms.
1048 (if apropos-sort-by-scores
1049 (or (> (cadr a) (cadr b))
1050 (and (= (cadr a) (cadr b))
1051 (string-lessp (car a) (car b))))
1052 (string-lessp (car a) (car b))))))
1053 (with-output-to-temp-buffer "*Apropos*"
1054 (let ((p apropos-accumulator)
1055 (old-buffer (current-buffer))
1056 (inhibit-read-only t)
1057 symbol item)
1058 (set-buffer standard-output)
1059 (apropos-mode)
1060 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
1061 (if apropos-multi-type "a type label" "an entry")
1062 " to view its full documentation.\n\n")
1063 (if text (insert text "\n\n"))
1064 (dolist (apropos-item p)
1065 (when (and spacing (not (bobp)))
1066 (princ spacing))
1067 (setq symbol (car apropos-item))
1068 ;; Insert dummy score element for backwards compatibility with 21.x
1069 ;; apropos-item format.
1070 (if (not (numberp (cadr apropos-item)))
1071 (setq apropos-item
1072 (cons (car apropos-item)
1073 (cons nil (cdr apropos-item)))))
1074 (insert-text-button (symbol-name symbol)
1075 'type 'apropos-symbol
1076 'skip apropos-multi-type
1077 'face 'apropos-symbol)
1078 (if (and (eq apropos-sort-by-scores 'verbose)
1079 (cadr apropos-item))
1080 (insert " (" (number-to-string (cadr apropos-item)) ") "))
1081 ;; Calculate key-bindings if we want them.
1082 (unless apropos-compact-layout
1083 (and do-keys
1084 (commandp symbol)
1085 (not (eq symbol 'self-insert-command))
1086 (indent-to 30 1)
1087 (if (let ((keys
1088 (with-current-buffer old-buffer
1089 (where-is-internal symbol)))
1090 filtered)
1091 ;; Copy over the list of key sequences,
1092 ;; omitting any that contain a buffer or a frame.
1093 ;; FIXME: Why omit keys that contain buffers and
1094 ;; frames? This looks like a bad workaround rather
1095 ;; than a proper fix. Does anybody know what problem
1096 ;; this is trying to address? --Stef
1097 (dolist (key keys)
1098 (let ((i 0)
1099 loser)
1100 (while (< i (length key))
1101 (if (or (framep (aref key i))
1102 (bufferp (aref key i)))
1103 (setq loser t))
1104 (setq i (1+ i)))
1105 (or loser
1106 (push key filtered))))
1107 (setq item filtered))
1108 ;; Convert the remaining keys to a string and insert.
1109 (insert
1110 (mapconcat
1111 (lambda (key)
1112 (setq key (condition-case ()
1113 (key-description key)
1114 (error)))
1115 (put-text-property 0 (length key)
1116 'face 'apropos-keybinding
1117 key)
1118 key)
1119 item ", "))
1120 (insert "M-x ... RET")
1121 (put-text-property (- (point) 11) (- (point) 8)
1122 'face 'apropos-keybinding)
1123 (put-text-property (- (point) 3) (point)
1124 'face 'apropos-keybinding)))
1125 (terpri))
1126 (apropos-print-doc 2
1127 (if (commandp symbol)
1128 'apropos-command
1129 (if (macrop symbol)
1130 'apropos-macro
1131 'apropos-function))
1132 (not nosubst))
1133 (apropos-print-doc 3
1134 (if (custom-variable-p symbol)
1135 'apropos-user-option
1136 'apropos-variable)
1137 (not nosubst))
1138 (apropos-print-doc 7 'apropos-group t)
1139 (apropos-print-doc 6 'apropos-face t)
1140 (apropos-print-doc 5 'apropos-widget t)
1141 (apropos-print-doc 4 'apropos-plist nil))
1142 (set (make-local-variable 'truncate-partial-width-windows) t)
1143 (set (make-local-variable 'truncate-lines) t))))
1144 (prog1 apropos-accumulator
1145 (setq apropos-accumulator ()))) ; permit gc
1147 (defun apropos-print-doc (i type do-keys)
1148 (let ((doc (nth i apropos-item)))
1149 (when (stringp doc)
1150 (if apropos-compact-layout
1151 (insert (propertize "\t" 'display '(space :align-to 32)) " ")
1152 (insert " "))
1153 (if apropos-multi-type
1154 (let ((button-face (button-type-get type 'face)))
1155 (unless (consp button-face)
1156 (setq button-face (list button-face)))
1157 (insert-text-button
1158 (if apropos-compact-layout
1159 (format "<%s>" (button-type-get type 'apropos-short-label))
1160 (button-type-get type 'apropos-label))
1161 'type type
1162 'apropos-symbol (car apropos-item))
1163 (insert (if apropos-compact-layout " " ": ")))
1165 ;; If the query is only for a single type, there's no point
1166 ;; writing it over and over again. Insert a blank button, and
1167 ;; put the 'apropos-label property there (needed by
1168 ;; apropos-symbol-button-display-help).
1169 (insert-text-button
1170 " " 'type type 'skip t
1171 'face 'default 'apropos-symbol (car apropos-item)))
1173 (let ((opoint (point))
1174 (ocol (current-column)))
1175 (cond ((equal doc "")
1176 (setq doc "(not documented)"))
1177 (do-keys
1178 (setq doc (substitute-command-keys doc))))
1179 (insert doc)
1180 (if (equal doc "(not documented)")
1181 (put-text-property opoint (point) 'font-lock-face 'shadow))
1182 ;; The labeling buttons might make the line too long, so fill it if
1183 ;; necessary.
1184 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column)
1185 emacs-lisp-docstring-fill-column
1186 fill-column)))
1187 (fill-prefix (make-string ocol ?\s)))
1188 (fill-region opoint (point) nil t)))
1189 (or (bolp) (terpri)))))
1191 (defun apropos-follow ()
1192 "Invokes any button at point, otherwise invokes the nearest label button."
1193 (interactive)
1194 (button-activate
1195 (or (apropos-next-label-button (line-beginning-position))
1196 (error "There is nothing to follow here"))))
1199 (defun apropos-describe-plist (symbol)
1200 "Display a pretty listing of SYMBOL's plist."
1201 (help-setup-xref (list 'apropos-describe-plist symbol)
1202 (called-interactively-p 'interactive))
1203 (with-help-window (help-buffer)
1204 (set-buffer standard-output)
1205 (princ "Symbol ")
1206 (prin1 symbol)
1207 (princ "'s plist is\n (")
1208 (put-text-property (+ (point-min) 7) (- (point) 14)
1209 'face 'apropos-symbol)
1210 (insert (apropos-format-plist symbol "\n "))
1211 (princ ")")))
1214 (provide 'apropos)
1216 ;;; apropos.el ends here