1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994-1995, 2001-2013 Free Software Foundation,
6 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
7 ;; Daniel Pfeiffer <occitan@esperanto.org> (rewrite)
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/>.
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>.
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.
63 "Apropos commands for users and programmers."
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."
88 (defface apropos-symbol
89 '((t (:inherit bold
)))
90 "Face for the symbol name in Apropos output."
94 (defface apropos-keybinding
95 '((t (:inherit underline
)))
96 "Face for lists of keybinding in Apropos output."
100 (defface apropos-property
101 '((t (:inherit font-lock-builtin-face
)))
102 "Face for property name in apropos output, or nil for none."
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."
112 (defface apropos-variable-button
113 '((t (:inherit
(font-lock-variable-name-face button
))))
114 "Button face indicating a variable in Apropos."
118 (defface apropos-user-option-button
119 '((t (:inherit
(font-lock-variable-name-face button
))))
120 "Button face indicating a user option in Apropos."
124 (defface apropos-misc-button
125 '((t (:inherit
(font-lock-constant-face button
))))
126 "Button face indicating a miscellaneous object type in Apropos."
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."
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."
142 :type
'(choice (const :tag
"off" nil
)
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."
151 :type
'(choice (const :tag
"off" nil
)
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
)
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")
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"
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."
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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
))))
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
)))
338 (setq label
(button-get button
'apropos-label
))
339 (setq type
(button-get button
'type
))))
343 (defun apropos-words-to-regexp (words wild
)
344 "Make regexp matching any two of the words in WORDS."
346 (mapconcat 'identity words
"\\|")
351 (mapconcat 'identity words
"\\|")
356 (defun apropos-read-pattern (subject)
357 "Read an apropos pattern, either a word list or a regexp.
358 Returns the user pattern, either a list of words which are matched
359 literally, or a string which is used as a regexp to search for.
361 SUBJECT is a string that is included in the prompt to identify what
362 kind of objects to search."
364 (read-string (concat "Search for " subject
" (word list or regexp): "))))
365 (if (string-equal (regexp-quote pattern
) pattern
)
367 (split-string pattern
"[ \t]+" t
)
370 (defun apropos-parse-pattern (pattern)
371 "Rewrite a list of words to a regexp matching all permutations.
372 If PATTERN is a string, that means it is already a regexp.
373 This updates variables `apropos-pattern', `apropos-pattern-quoted',
374 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
375 (setq apropos-words nil
376 apropos-all-words nil
)
378 ;; We don't actually make a regexp matching all permutations.
379 ;; Instead, for e.g. "a b c", we make a regexp matching
380 ;; any combination of two or more words like this:
381 ;; (a|b|c).*(a|b|c) which may give some false matches,
382 ;; but as long as it also gives the right ones, that's ok.
383 (let ((words pattern
))
384 (setq apropos-pattern
(mapconcat 'identity pattern
" ")
385 apropos-pattern-quoted
(regexp-quote apropos-pattern
))
387 (let ((syn apropos-synonyms
) (s word
) (a word
))
389 (if (member word
(car syn
))
391 (setq a
(mapconcat 'identity
(car syn
) "\\|"))
392 (if (member word
(cdr (car syn
)))
395 (setq syn
(cdr syn
))))
396 (setq apropos-words
(cons s apropos-words
)
397 apropos-all-words
(cons a apropos-all-words
))))
398 (setq apropos-all-words-regexp
399 (apropos-words-to-regexp apropos-all-words
".+"))
401 (apropos-words-to-regexp apropos-words
".*?")))
402 (setq apropos-pattern-quoted
(regexp-quote pattern
)
403 apropos-all-words-regexp pattern
404 apropos-pattern pattern
405 apropos-regexp pattern
)))
408 (defun apropos-calc-scores (str words
)
409 "Return apropos scores for string STR matching WORDS.
410 Value is a list of offsets of the words into the string."
413 (dolist (word words scores
)
414 (if (setq i
(string-match word str
))
415 (setq scores
(cons i scores
))))
416 ;; Return list of start and end position of regexp
417 (and (string-match apropos-pattern str
)
418 (list (match-beginning 0) (match-end 0))))))
420 (defun apropos-score-str (str)
421 "Return apropos score for string STR."
423 (let* ((l (length str
))
424 (score (- (/ l
10))))
425 (dolist (s (apropos-calc-scores str apropos-all-words
) score
)
426 (setq score
(+ score
1000 (/ (* (- l s
) 1000) l
)))))
429 (defun apropos-score-doc (doc)
430 "Return apropos score for documentation string DOC."
431 (let ((l (length doc
)))
434 (when (string-match apropos-pattern-quoted doc
)
436 (dolist (s (apropos-calc-scores doc apropos-all-words
) score
)
437 (setq score
(+ score
50 (/ (* (- l s
) 50) l
)))))
440 (defun apropos-score-symbol (symbol &optional weight
)
441 "Return apropos score for SYMBOL."
442 (setq symbol
(symbol-name symbol
))
445 (dolist (s (apropos-calc-scores symbol apropos-words
) (* score
(or weight
3)))
446 (setq score
(+ score
(- 60 l
) (/ (* (- l s
) 60) l
))))))
448 (defun apropos-true-hit (str words
)
449 "Return t if STR is a genuine hit.
450 This may fail if only one of the keywords is matched more than once.
451 This requires that at least 2 keywords (unless only one was given)."
455 (> (length (apropos-calc-scores str words
)) 1)))
457 (defun apropos-false-hit-symbol (symbol)
458 "Return t if SYMBOL is not really matched by the current keywords."
459 (not (apropos-true-hit (symbol-name symbol
) apropos-words
)))
461 (defun apropos-false-hit-str (str)
462 "Return t if STR is not really matched by the current keywords."
463 (not (apropos-true-hit str apropos-words
)))
465 (defun apropos-true-hit-doc (doc)
466 "Return t if DOC is really matched by the current keywords."
467 (apropos-true-hit doc apropos-all-words
))
469 (define-derived-mode apropos-mode special-mode
"Apropos"
470 "Major mode for following hyperlinks in output of apropos commands.
472 \\{apropos-mode-map}")
474 (defvar apropos-multi-type t
475 "If non-nil, this apropos query concerns multiple types.
476 This is used to decide whether to print the result's type or not.")
479 (defun apropos-user-option (pattern &optional do-all
)
480 "Show user options that match PATTERN.
481 PATTERN can be a word, a list of words (separated by spaces),
482 or a regexp (using some regexp special characters). If it is a word,
483 search for matches for that word as a substring. If it is a list of words,
484 search for matches for any two (or more) of those words.
486 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
487 variables, not just user options."
488 (interactive (list (apropos-read-pattern
489 (if (or current-prefix-arg apropos-do-all
)
490 "variable" "user option"))
492 (apropos-command pattern nil
493 (if (or do-all apropos-do-all
)
496 (get symbol
'variable-documentation
)))
497 'custom-variable-p
)))
500 (defun apropos-variable (pattern &optional do-not-all
)
501 "Show variables that match PATTERN.
502 When DO-NOT-ALL is not-nil, show user options only, i.e. behave
503 like `apropos-user-option'."
504 (interactive (list (apropos-read-pattern
505 (if current-prefix-arg
"user option" "variable"))
507 (let ((apropos-do-all (if do-not-all nil t
)))
508 (apropos-user-option pattern
)))
510 ;; For auld lang syne:
512 (defalias 'command-apropos
'apropos-command
)
514 (defun apropos-command (pattern &optional do-all var-predicate
)
515 "Show commands (interactively callable functions) that match PATTERN.
516 PATTERN can be a word, a list of words (separated by spaces),
517 or a regexp (using some regexp special characters). If it is a word,
518 search for matches for that word as a substring. If it is a list of words,
519 search for matches for any two (or more) of those words.
521 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
522 noninteractive functions.
524 If VAR-PREDICATE is non-nil, show only variables, and only those that
525 satisfy the predicate VAR-PREDICATE.
527 When called from a Lisp program, a string PATTERN is used as a regexp,
528 while a list of strings is used as a word list."
529 (interactive (list (apropos-read-pattern
530 (if (or current-prefix-arg apropos-do-all
)
531 "command or function" "command"))
533 (apropos-parse-pattern pattern
)
535 (let ((standard-output (get-buffer-create "*Apropos*")))
536 (help-print-return-message 'identity
))))
537 (or do-all
(setq do-all apropos-do-all
))
538 (setq apropos-accumulator
539 (apropos-internal apropos-regexp
541 ;; We used to use `functionp' here, but this
542 ;; rules out macros. `fboundp' rules in
543 ;; keymaps, but it seems harmless.
544 (if do-all
'fboundp
'commandp
))))
545 (let ((tem apropos-accumulator
))
547 (if (or (get (car tem
) 'apropos-inhibit
)
548 (apropos-false-hit-symbol (car tem
)))
549 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
550 (setq tem
(cdr tem
))))
551 (let ((p apropos-accumulator
)
555 (setq symbol
(car p
))
556 (setq score
(apropos-score-symbol symbol
))
557 (unless var-predicate
559 (if (setq doc
(condition-case nil
560 (documentation symbol t
)
562 ;; Eg alias to undefined function.
564 "(documentation error)"
565 (setq score
(+ score
(apropos-score-doc doc
)))
566 (substring doc
0 (string-match "\n" doc
)))
567 "(not documented)")))
569 (funcall var-predicate symbol
)
570 (if (setq doc
(documentation-property
571 symbol
'variable-documentation t
))
573 (setq score
(+ score
(apropos-score-doc doc
)))
575 (string-match "\n" doc
)))))))
576 (setcar (cdr (car p
)) score
)
578 (and (let ((apropos-multi-type do-all
))
579 (apropos-print t nil nil t
))
581 (message "%s" message
))))
585 (defun apropos-documentation-property (symbol property raw
)
586 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
588 (let ((doc (documentation-property symbol property raw
)))
589 (if doc
(substring doc
0 (string-match "\n" doc
))
591 (error "(error retrieving documentation)")))
595 (defun apropos (pattern &optional do-all
)
596 "Show all meaningful Lisp symbols whose names match PATTERN.
597 Symbols are shown if they are defined as functions, variables, or
598 faces, or if they have nonempty property lists.
600 PATTERN can be a word, a list of words (separated by spaces),
601 or a regexp (using some regexp special characters). If it is a word,
602 search for matches for that word as a substring. If it is a list of words,
603 search for matches for any two (or more) of those words.
605 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
606 consider all symbols (if they match PATTERN).
608 Returns list of symbols and documentation found."
609 (interactive (list (apropos-read-pattern "symbol")
611 (apropos-parse-pattern pattern
)
612 (apropos-symbols-internal
613 (apropos-internal apropos-regexp
620 (symbol-plist symbol
)))))
621 (or do-all apropos-do-all
)))
623 (defun apropos-library-button (sym)
626 (let ((name (copy-sequence (symbol-name sym
))))
627 (make-text-button name nil
628 'type
'apropos-library
629 'face
'apropos-symbol
630 'apropos-symbol name
)
634 (defun apropos-library (file)
635 "List the variables and functions defined by library FILE.
636 FILE should be one of the libraries currently loaded and should
637 thus be found in `load-history'. If `apropos-do-all' is non-nil,
638 the output includes key-bindings of commands."
640 (let* ((libs (delq nil
(mapcar 'car load-history
)))
645 (setq l
(file-name-nondirectory l
))
647 (not (equal (setq l
(file-name-sans-extension l
))
652 (list (completing-read "Describe library: " libs nil t
))))
657 (lh-entry (assoc file load-history
)))
659 ;; `file' may be the "shortname".
660 (let ((lh load-history
)
661 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file
)
663 (while (and lh
(null lh-entry
))
664 (if (and (caar lh
) (string-match re
(caar lh
)))
665 (setq lh-entry
(car lh
))
666 (setq lh
(cdr lh
)))))
667 (unless lh-entry
(error "Unknown library `%s'" file
)))
668 (dolist (x (cdr lh-entry
))
670 ;; (autoload (push (cdr x) autoloads))
671 (`require
(push (cdr x
) requires
))
672 (`provide
(push (cdr x
) provides
))
673 (_ (push (or (cdr-safe x
) x
) symbols
))))
674 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
675 (apropos-symbols-internal
676 symbols apropos-do-all
678 (format "Library `%s' provides: %s\nand requires: %s"
680 (mapconcat 'apropos-library-button
681 (or provides
'(nil)) " and ")
682 (mapconcat 'apropos-library-button
683 (or requires
'(nil)) " and ")))))))
685 (defun apropos-symbols-internal (symbols keys
&optional text
)
686 ;; Filter out entries that are marked as apropos-inhibit.
688 (dolist (symbol symbols
)
689 (unless (get symbol
'apropos-inhibit
)
692 (let ((apropos-accumulator
695 (let (doc properties
)
698 (apropos-score-symbol symbol
)
699 (when (fboundp symbol
)
700 (if (setq doc
(condition-case nil
701 (documentation symbol t
)
703 "(alias for undefined function)")
705 "(can't retrieve function documentation)")))
706 (substring doc
0 (string-match "\n" doc
))
708 (when (boundp symbol
)
709 (apropos-documentation-property
710 symbol
'variable-documentation t
))
711 (when (setq properties
(symbol-plist symbol
))
712 (setq doc
(list (car properties
)))
713 (while (setq properties
(cdr (cdr properties
)))
714 (setq doc
(cons (car properties
) doc
)))
715 (mapconcat #'symbol-name
(nreverse doc
) " "))
716 (when (get symbol
'widget-type
)
717 (apropos-documentation-property
718 symbol
'widget-documentation t
))
720 (let ((alias (get symbol
'face-alias
)))
723 (format "%slias for the face `%s'."
724 (if (get symbol
'obsolete-face
)
728 ;; Never happens in practice because fails
729 ;; (facep symbol) test.
730 "(alias for undefined face)")
731 (apropos-documentation-property
732 symbol
'face-documentation t
))))
733 (when (get symbol
'custom-group
)
734 (apropos-documentation-property
735 symbol
'group-documentation t
)))))
737 (apropos-print keys nil text
)))
741 (defun apropos-value (pattern &optional do-all
)
742 "Show all symbols whose value's printed representation matches PATTERN.
743 PATTERN can be a word, a list of words (separated by spaces),
744 or a regexp (using some regexp special characters). If it is a word,
745 search for matches for that word as a substring. If it is a list of words,
746 search for matches for any two (or more) of those words.
748 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
749 at function definitions (arguments, documentation and body) and at the
750 names and values of properties.
752 Returns list of symbols and values found."
753 (interactive (list (apropos-read-pattern "value")
755 (apropos-parse-pattern pattern
)
756 (or do-all
(setq do-all apropos-do-all
))
757 (setq apropos-accumulator
())
761 (setq f nil v nil p nil
)
762 (or (memq symbol
'(apropos-regexp
763 apropos-pattern apropos-all-words-regexp
764 apropos-words apropos-all-words
765 do-all apropos-accumulator
767 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
769 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
770 p
(apropos-format-plist symbol
"\n " t
)))
771 (if (apropos-false-hit-str v
)
773 (if (apropos-false-hit-str f
)
775 (if (apropos-false-hit-str p
)
778 (setq apropos-accumulator
(cons (list symbol
779 (+ (apropos-score-str f
)
780 (apropos-score-str v
)
781 (apropos-score-str p
))
783 apropos-accumulator
))))))
784 (let ((apropos-multi-type do-all
))
785 (apropos-print nil
"\n----------------\n")))
789 (defun apropos-documentation (pattern &optional do-all
)
790 "Show symbols whose documentation contains matches for PATTERN.
791 PATTERN can be a word, a list of words (separated by spaces),
792 or a regexp (using some regexp special characters). If it is a word,
793 search for matches for that word as a substring. If it is a list of words,
794 search for matches for any two (or more) of those words.
796 Note that by default this command only searches in the file specified by
797 `internal-doc-file-name'; i.e., the etc/DOC file. With \\[universal-argument] prefix,
798 or if `apropos-do-all' is non-nil, it searches all currently defined
799 documentation strings.
801 Returns list of symbols and documentation found."
802 ;; The doc used to say that DO-ALL includes key-bindings info in the
803 ;; output, but I cannot see that that is true.
804 (interactive (list (apropos-read-pattern "documentation")
806 (apropos-parse-pattern pattern
)
807 (or do-all
(setq do-all apropos-do-all
))
808 (setq apropos-accumulator
() apropos-files-scanned
())
809 (let ((standard-input (get-buffer-create " apropos-temp"))
810 (apropos-sort-by-scores apropos-documentation-sort-by-scores
)
813 (with-current-buffer standard-input
814 (apropos-documentation-check-doc-file)
818 (setq f
(apropos-safe-documentation symbol
)
819 v
(get symbol
'variable-documentation
))
820 (if (integerp v
) (setq v
))
821 (setq f
(apropos-documentation-internal f
)
822 v
(apropos-documentation-internal v
))
823 (setq sf
(apropos-score-doc f
)
824 sv
(apropos-score-doc v
))
826 (if (setq apropos-item
827 (cdr (assq symbol apropos-accumulator
)))
831 (setcar (nthcdr 1 apropos-item
) f
)
832 (setcar apropos-item
(+ (car apropos-item
) sf
))))
835 (setcar (nthcdr 2 apropos-item
) v
)
836 (setcar apropos-item
(+ (car apropos-item
) sv
)))))
837 (setq apropos-accumulator
839 (+ (apropos-score-symbol symbol
2) sf sv
)
841 apropos-accumulator
)))))))
842 (apropos-print nil
"\n----------------\n" nil t
))
843 (kill-buffer standard-input
))))
846 (defun apropos-value-internal (predicate symbol function
)
847 (if (funcall predicate symbol
)
849 (setq symbol
(prin1-to-string (funcall function symbol
)))
850 (if (string-match apropos-regexp symbol
)
852 (if apropos-match-face
853 (put-text-property (match-beginning 0) (match-end 0)
854 'face apropos-match-face
858 (defun apropos-documentation-internal (doc)
860 (apropos-documentation-check-elc-file (car doc
))
862 (string-match apropos-all-words-regexp doc
)
863 (apropos-true-hit-doc doc
))
864 (when apropos-match-face
865 (setq doc
(substitute-command-keys (copy-sequence doc
)))
866 (if (or (string-match apropos-pattern-quoted doc
)
867 (string-match apropos-all-words-regexp doc
))
868 (put-text-property (match-beginning 0)
870 'face apropos-match-face doc
))
873 (defun apropos-format-plist (pl sep
&optional compare
)
874 (setq pl
(symbol-plist pl
))
877 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
878 (if (or (not compare
) (string-match apropos-regexp p
))
879 (put-text-property 0 (length (symbol-name (car pl
)))
880 'face
'apropos-property p
)
884 (and compare apropos-match-face
885 (put-text-property (match-beginning 0) (match-end 0)
886 'face apropos-match-face
888 (setq p-out
(concat p-out
(if p-out sep
) p
))))
889 (setq pl
(nthcdr 2 pl
)))
893 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
895 (defun apropos-documentation-check-doc-file ()
896 (let (type symbol
(sepa 2) sepb doc
)
899 (insert-file-contents (concat doc-directory internal-doc-file-name
))
901 (while (save-excursion
902 (setq sepb
(search-forward "\^_"))
904 (beginning-of-line 2)
905 (if (save-restriction
906 (narrow-to-region (point) (1- sepb
))
907 (re-search-forward apropos-all-words-regexp nil t
))
909 (goto-char (1+ sepa
))
910 (setq type
(if (eq ?F
(preceding-char))
911 2 ; function documentation
912 3) ; variable documentation
914 doc
(buffer-substring (1+ (point)) (1- sepb
)))
915 (when (and (apropos-true-hit-doc doc
)
916 ;; The DOC file lists all built-in funcs and vars.
917 ;; If any are not currently bound, they can
918 ;; only be platform-specific stuff (eg NS) not
919 ;; in use on the current platform.
920 ;; So we exclude them.
921 (cond ((= 3 type
) (boundp symbol
))
922 ((= 2 type
) (fboundp symbol
))))
923 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
924 (setcar (cdr apropos-item
)
925 (apropos-score-doc doc
)))
926 (setq apropos-item
(list symbol
927 (+ (apropos-score-symbol symbol
2)
928 (apropos-score-doc doc
))
930 apropos-accumulator
(cons apropos-item
931 apropos-accumulator
)))
932 (when apropos-match-face
933 (setq doc
(substitute-command-keys doc
))
934 (if (or (string-match apropos-pattern-quoted doc
)
935 (string-match apropos-all-words-regexp doc
))
936 (put-text-property (match-beginning 0)
938 'face apropos-match-face doc
)))
939 (setcar (nthcdr type apropos-item
) doc
))))
940 (setq sepa
(goto-char sepb
)))))
942 (defun apropos-documentation-check-elc-file (file)
943 (if (member file apropos-files-scanned
)
945 (let (symbol doc beg end this-is-a-variable
)
946 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
948 (insert-file-contents file
)
949 (while (search-forward "\n#@" nil t
)
950 ;; Read the comment length, and advance over it.
953 end
(+ (point) end -
1))
955 (if (save-restriction
956 ;; match ^ and $ relative to doc string
957 (narrow-to-region beg end
)
958 (re-search-forward apropos-all-words-regexp nil t
))
960 (goto-char (+ end
2))
961 (setq doc
(buffer-substring beg end
)
962 end
(- (match-end 0) beg
)
963 beg
(- (match-beginning 0) beg
))
964 (when (apropos-true-hit-doc doc
)
965 (setq this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
967 (skip-chars-forward "(a-z")
970 symbol
(if (consp symbol
)
973 (if (if this-is-a-variable
974 (get symbol
'variable-documentation
)
975 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
977 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
978 (setcar (cdr apropos-item
)
979 (+ (cadr apropos-item
) (apropos-score-doc doc
))))
980 (setq apropos-item
(list symbol
981 (+ (apropos-score-symbol symbol
2)
982 (apropos-score-doc doc
))
984 apropos-accumulator
(cons apropos-item
985 apropos-accumulator
)))
986 (when apropos-match-face
987 (setq doc
(substitute-command-keys doc
))
988 (if (or (string-match apropos-pattern-quoted doc
)
989 (string-match apropos-all-words-regexp doc
))
990 (put-text-property (match-beginning 0)
992 'face apropos-match-face doc
)))
993 (setcar (nthcdr (if this-is-a-variable
3 2)
999 (defun apropos-safe-documentation (function)
1000 "Like `documentation', except it avoids calling `get_doc_string'.
1001 Will return nil instead."
1002 (while (and function
(symbolp function
))
1003 (setq function
(if (fboundp function
)
1004 (symbol-function function
))))
1005 (if (eq (car-safe function
) 'macro
)
1006 (setq function
(cdr function
)))
1007 (setq function
(if (byte-code-function-p function
)
1008 (if (> (length function
) 4)
1010 (if (autoloadp function
)
1012 (if (eq (car-safe function
) 'lambda
)
1013 (if (stringp (nth 2 function
))
1015 (if (stringp (nth 3 function
))
1016 (nth 3 function
)))))))
1017 (if (integerp function
)
1021 (defcustom apropos-compact-layout nil
1022 "If non-nil, use a single line per binding."
1025 (defun apropos-print (do-keys spacing
&optional text nosubst
)
1026 "Output result of apropos searching into buffer `*Apropos*'.
1027 The value of `apropos-accumulator' is the list of items to output.
1028 Each element should have the format
1029 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
1030 The return value is the list that was in `apropos-accumulator', sorted
1031 alphabetically by symbol name; but this function also sets
1032 `apropos-accumulator' to nil before returning.
1034 If SPACING is non-nil, it should be a string; separate items with that string.
1035 If non-nil TEXT is a string that will be printed as a heading."
1036 (if (null apropos-accumulator
)
1037 (message "No apropos matches for `%s'" apropos-pattern
)
1038 (setq apropos-accumulator
1039 (sort apropos-accumulator
1041 ;; Don't sort by score if user can't see the score.
1042 ;; It would be confusing. -- rms.
1043 (if apropos-sort-by-scores
1044 (or (> (cadr a
) (cadr b
))
1045 (and (= (cadr a
) (cadr b
))
1046 (string-lessp (car a
) (car b
))))
1047 (string-lessp (car a
) (car b
))))))
1048 (with-output-to-temp-buffer "*Apropos*"
1049 (let ((p apropos-accumulator
)
1050 (old-buffer (current-buffer))
1051 (inhibit-read-only t
)
1053 (set-buffer standard-output
)
1055 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
1056 (if apropos-multi-type
"a type label" "an entry")
1057 " to view its full documentation.\n\n")
1058 (if text
(insert text
"\n\n"))
1059 (dolist (apropos-item p
)
1060 (when (and spacing
(not (bobp)))
1062 (setq symbol
(car apropos-item
))
1063 ;; Insert dummy score element for backwards compatibility with 21.x
1064 ;; apropos-item format.
1065 (if (not (numberp (cadr apropos-item
)))
1067 (cons (car apropos-item
)
1068 (cons nil
(cdr apropos-item
)))))
1069 (insert-text-button (symbol-name symbol
)
1070 'type
'apropos-symbol
1071 'skip apropos-multi-type
1072 'face
'apropos-symbol
)
1073 (if (and (eq apropos-sort-by-scores
'verbose
)
1074 (cadr apropos-item
))
1075 (insert " (" (number-to-string (cadr apropos-item
)) ") "))
1076 ;; Calculate key-bindings if we want them.
1077 (unless apropos-compact-layout
1080 (not (eq symbol
'self-insert-command
))
1083 (with-current-buffer old-buffer
1084 (where-is-internal symbol
)))
1086 ;; Copy over the list of key sequences,
1087 ;; omitting any that contain a buffer or a frame.
1088 ;; FIXME: Why omit keys that contain buffers and
1089 ;; frames? This looks like a bad workaround rather
1090 ;; than a proper fix. Does anybody know what problem
1091 ;; this is trying to address? --Stef
1095 (while (< i
(length key
))
1096 (if (or (framep (aref key i
))
1097 (bufferp (aref key i
)))
1101 (push key filtered
))))
1102 (setq item filtered
))
1103 ;; Convert the remaining keys to a string and insert.
1107 (setq key
(condition-case ()
1108 (key-description key
)
1110 (put-text-property 0 (length key
)
1111 'face
'apropos-keybinding
1115 (insert "M-x ... RET")
1116 (put-text-property (- (point) 11) (- (point) 8)
1117 'face
'apropos-keybinding
)
1118 (put-text-property (- (point) 3) (point)
1119 'face
'apropos-keybinding
)))
1121 (apropos-print-doc 2
1122 (if (commandp symbol
)
1128 (apropos-print-doc 3
1129 (if (custom-variable-p symbol
)
1130 'apropos-user-option
1133 (apropos-print-doc 7 'apropos-group t
)
1134 (apropos-print-doc 6 'apropos-face t
)
1135 (apropos-print-doc 5 'apropos-widget t
)
1136 (apropos-print-doc 4 'apropos-plist nil
))
1137 (set (make-local-variable 'truncate-partial-width-windows
) t
)
1138 (set (make-local-variable 'truncate-lines
) t
))))
1139 (prog1 apropos-accumulator
1140 (setq apropos-accumulator
()))) ; permit gc
1142 (defun apropos-print-doc (i type do-keys
)
1143 (let ((doc (nth i apropos-item
)))
1145 (if apropos-compact-layout
1146 (insert (propertize "\t" 'display
'(space :align-to
32)) " ")
1148 (if apropos-multi-type
1149 (let ((button-face (button-type-get type
'face
)))
1150 (unless (consp button-face
)
1151 (setq button-face
(list button-face
)))
1153 (if apropos-compact-layout
1154 (format "<%s>" (button-type-get type
'apropos-short-label
))
1155 (button-type-get type
'apropos-label
))
1157 'apropos-symbol
(car apropos-item
))
1158 (insert (if apropos-compact-layout
" " ": ")))
1160 ;; If the query is only for a single type, there's no point
1161 ;; writing it over and over again. Insert a blank button, and
1162 ;; put the 'apropos-label property there (needed by
1163 ;; apropos-symbol-button-display-help).
1165 " " 'type type
'skip t
1166 'face
'default
'apropos-symbol
(car apropos-item
)))
1168 (let ((opoint (point))
1169 (ocol (current-column)))
1170 (cond ((equal doc
"")
1171 (setq doc
"(not documented)"))
1173 (setq doc
(substitute-command-keys doc
))))
1175 (if (equal doc
"(not documented)")
1176 (put-text-property opoint
(point) 'font-lock-face
'shadow
))
1177 ;; The labeling buttons might make the line too long, so fill it if
1179 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column
)
1180 emacs-lisp-docstring-fill-column
1182 (fill-prefix (make-string ocol ?\s
)))
1183 (fill-region opoint
(point) nil t
)))
1184 (or (bolp) (terpri)))))
1186 (defun apropos-follow ()
1187 "Invokes any button at point, otherwise invokes the nearest label button."
1190 (or (apropos-next-label-button (line-beginning-position))
1191 (error "There is nothing to follow here"))))
1194 (defun apropos-describe-plist (symbol)
1195 "Display a pretty listing of SYMBOL's plist."
1196 (help-setup-xref (list 'apropos-describe-plist symbol
)
1197 (called-interactively-p 'interactive
))
1198 (with-help-window (help-buffer)
1199 (set-buffer standard-output
)
1202 (princ "'s plist is\n (")
1203 (put-text-property (+ (point-min) 7) (- (point) 14)
1204 'face
'apropos-symbol
)
1205 (insert (apropos-format-plist symbol
"\n "))
1211 ;;; apropos.el ends here