1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994-1995, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Daniel Pfeiffer <occitan@esperanto.org> (rewrite)
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; The ideas for this package were derived from the C code in
28 ;; src/keymap.c and elsewhere. The functions in this file should
29 ;; always be byte-compiled for speed. Someone should rewrite this in
30 ;; C (as part of src/keymap.c) for speed.
32 ;; The idea for super-apropos is based on the original implementation
33 ;; by Lynn Slater <lrs@esl.com>.
36 ;; Fixed bug, current-local-map can return nil.
37 ;; Change, doesn't calculate key-bindings unless needed.
38 ;; Added super-apropos capability, changed print functions.
39 ;;; Made fast-apropos and super-apropos share code.
40 ;;; Sped up fast-apropos again.
41 ;; Added apropos-do-all option.
42 ;;; Added fast-command-apropos.
43 ;; Changed doc strings to comments for helping functions.
44 ;;; Made doc file buffer read-only, buried it.
45 ;; Only call substitute-command-keys if do-all set.
47 ;; Optionally use configurable faces to make the output more legible.
48 ;; Differentiate between command, function and macro.
49 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
50 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
51 ;; Apropos-documentation (ex super-apropos) now finds all it should.
52 ;; New apropos-value snoops through all values and optionally plists.
53 ;; Reading DOC file doesn't load nroff.
54 ;; Added hypertext following of documentation, mouse-2 on variable gives value
55 ;; from buffer in active window.
60 (eval-when-compile (require 'cl
))
63 "Apropos commands for users and programmers."
67 ;; I see a degradation of maybe 10-20% only.
68 (defcustom apropos-do-all nil
69 "Whether the apropos commands should do more.
71 Slows them down more or less. Set this non-nil if you have a fast machine."
76 (defcustom apropos-symbol-face
'bold
77 "Face for symbol name in Apropos output, or nil for none."
81 (defcustom apropos-keybinding-face
'underline
82 "Face for lists of keybinding in Apropos output, or nil for none."
86 (defcustom apropos-label-face
'(italic)
87 "Face for label (`Command', `Variable' ...) in Apropos output.
88 A value of nil means don't use any special font for them, and also
89 turns off mouse highlighting."
93 (defcustom apropos-property-face
'bold-italic
94 "Face for property name in apropos output, or nil for none."
98 (defcustom apropos-match-face
'match
99 "Face for matching text in Apropos documentation/value, or nil for none.
100 This applies when you look for matches in the documentation or variable value
101 for the pattern; the part that matches gets displayed in this font."
105 (defcustom apropos-sort-by-scores nil
106 "Non-nil means sort matches by scores; best match is shown first.
107 This applies to all `apropos' commands except `apropos-documentation'.
108 If value is `verbose', the computed score is shown for each match."
110 :type
'(choice (const :tag
"off" nil
)
112 (const :tag
"show scores" verbose
)))
114 (defcustom apropos-documentation-sort-by-scores t
115 "Non-nil means sort matches by scores; best match is shown first.
116 This applies to `apropos-documentation' only.
117 If value is `verbose', the computed score is shown for each match."
119 :type
'(choice (const :tag
"off" nil
)
121 (const :tag
"show scores" verbose
)))
123 (defvar apropos-mode-map
124 (let ((map (copy-keymap button-buffer-map
)))
125 (set-keymap-parent map special-mode-map
)
126 ;; Use `apropos-follow' instead of just using the button
127 ;; definition of RET, so that users can use it anywhere in an
128 ;; apropos item, not just on top of a button.
129 (define-key map
"\C-m" 'apropos-follow
)
131 "Keymap used in Apropos mode.")
133 (defvar apropos-mode-hook nil
134 "Hook run when mode is turned on.")
136 (defvar apropos-pattern nil
137 "Apropos pattern as entered by user.")
139 (defvar apropos-pattern-quoted nil
140 "Apropos pattern passed through `regexp-quote'.")
142 (defvar apropos-words
()
143 "Current list of apropos words extracted from `apropos-pattern'.")
145 (defvar apropos-all-words
()
146 "Current list of words and synonyms.")
148 (defvar apropos-regexp nil
149 "Regexp used in current apropos run.")
151 (defvar apropos-all-words-regexp nil
152 "Regexp matching apropos-all-words.")
154 (defvar apropos-files-scanned
()
155 "List of elc files already scanned in current run of `apropos-documentation'.")
157 (defvar apropos-accumulator
()
158 "Alist of symbols already found in current apropos run.
159 Each element has the form
161 (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
163 where SYMBOL is the symbol name, SCORE is its relevance score (a
164 number), FUN-DOC is the function docstring, VAR-DOC is the
165 variable docstring, PLIST is the list of the symbols names in the
166 property list, WIDGET-DOC is the widget docstring, FACE-DOC is
167 the face docstring, and CUS-GROUP-DOC is the custom group
168 docstring. Each docstring is either nil or a string.")
170 (defvar apropos-item
()
171 "Current item in or for `apropos-accumulator'.")
173 (defvar apropos-synonyms
'(
174 ("find" "open" "edit")
177 ("region" "selection"))
178 "List of synonyms known by apropos.
179 Each element is a list of words where the first word is the standard Emacs
180 term, and the rest of the words are alternative terms.")
183 ;;; Button types used by apropos
185 (define-button-type 'apropos-symbol
186 'face apropos-symbol-face
187 'help-echo
"mouse-2, RET: Display more help on this symbol"
189 'action
#'apropos-symbol-button-display-help
)
191 (defun apropos-symbol-button-display-help (button)
192 "Display further help for the `apropos-symbol' button BUTTON."
194 (or (apropos-next-label-button (button-start button
))
195 (error "There is nothing to follow for `%s'" (button-label button
)))))
197 (define-button-type 'apropos-function
198 'apropos-label
"Function"
199 'apropos-short-label
"f"
200 'face
'(font-lock-function-name-face button
)
201 'help-echo
"mouse-2, RET: Display more help on this function"
203 'action
(lambda (button)
204 (describe-function (button-get button
'apropos-symbol
))))
206 (define-button-type 'apropos-macro
207 'apropos-label
"Macro"
208 'apropos-short-label
"m"
209 'face
'(font-lock-function-name-face button
)
210 'help-echo
"mouse-2, RET: Display more help on this macro"
212 'action
(lambda (button)
213 (describe-function (button-get button
'apropos-symbol
))))
215 (define-button-type 'apropos-command
216 'apropos-label
"Command"
217 'apropos-short-label
"c"
218 'face
'(font-lock-function-name-face button
)
219 'help-echo
"mouse-2, RET: Display more help on this command"
221 'action
(lambda (button)
222 (describe-function (button-get button
'apropos-symbol
))))
224 ;; We used to use `customize-variable-other-window' instead for a
225 ;; customizable variable, but that is slow. It is better to show an
226 ;; ordinary help buffer and let the user click on the customization
227 ;; button in that buffer, if he wants to.
228 ;; Likewise for `customize-face-other-window'.
229 (define-button-type 'apropos-variable
230 'apropos-label
"Variable"
231 'apropos-short-label
"v"
232 'face
'(font-lock-variable-name-face button
)
233 'help-echo
"mouse-2, RET: Display more help on this variable"
235 'action
(lambda (button)
236 (describe-variable (button-get button
'apropos-symbol
))))
238 (define-button-type 'apropos-face
239 'apropos-label
"Face"
240 'apropos-short-label
"F"
241 'face
'(font-lock-variable-name-face button
)
242 'help-echo
"mouse-2, RET: Display more help on this face"
244 'action
(lambda (button)
245 (describe-face (button-get button
'apropos-symbol
))))
247 (define-button-type 'apropos-group
248 'apropos-label
"Group"
249 'apropos-short-label
"g"
250 'face
'(font-lock-builtin-face button
)
251 'help-echo
"mouse-2, RET: Display more help on this group"
253 'action
(lambda (button)
254 (customize-group-other-window
255 (button-get button
'apropos-symbol
))))
257 (define-button-type 'apropos-widget
258 'apropos-label
"Widget"
259 'apropos-short-label
"w"
260 'face
'(font-lock-builtin-face button
)
261 'help-echo
"mouse-2, RET: Display more help on this widget"
263 'action
(lambda (button)
264 (widget-browse-other-window (button-get button
'apropos-symbol
))))
266 (define-button-type 'apropos-plist
267 'apropos-label
"Properties"
268 'apropos-short-label
"p"
269 'face
'(font-lock-keyword-face button
)
270 'help-echo
"mouse-2, RET: Display more help on this plist"
272 'action
(lambda (button)
273 (apropos-describe-plist (button-get button
'apropos-symbol
))))
275 (define-button-type 'apropos-library
276 'help-echo
"mouse-2, RET: Display more help on this library"
278 'action
(lambda (button)
279 (apropos-library (button-get button
'apropos-symbol
))))
281 (defun apropos-next-label-button (pos)
282 "Return the next apropos label button after POS, or nil if there's none.
283 Will also return nil if more than one `apropos-symbol' button is encountered
284 before finding a label."
285 (let* ((button (next-button pos t
))
286 (already-hit-symbol nil
)
287 (label (and button
(button-get button
'apropos-label
)))
288 (type (and button
(button-get button
'type
))))
291 (or (not (eq type
'apropos-symbol
))
292 (not already-hit-symbol
)))
293 (when (eq type
'apropos-symbol
)
294 (setq already-hit-symbol t
))
295 (setq button
(next-button (button-start button
)))
297 (setq label
(button-get button
'apropos-label
))
298 (setq type
(button-get button
'type
))))
302 (defun apropos-words-to-regexp (words wild
)
303 "Make regexp matching any two of the words in WORDS."
305 (mapconcat 'identity words
"\\|")
310 (mapconcat 'identity words
"\\|")
315 (defun apropos-read-pattern (subject)
316 "Read an apropos pattern, either a word list or a regexp.
317 Returns the user pattern, either a list of words which are matched
318 literally, or a string which is used as a regexp to search for.
320 SUBJECT is a string that is included in the prompt to identify what
321 kind of objects to search."
323 (read-string (concat "Apropos " subject
" (word list or regexp): "))))
324 (if (string-equal (regexp-quote pattern
) pattern
)
326 (split-string pattern
"[ \t]+")
329 (defun apropos-parse-pattern (pattern)
330 "Rewrite a list of words to a regexp matching all permutations.
331 If PATTERN is a string, that means it is already a regexp.
332 This updates variables `apropos-pattern', `apropos-pattern-quoted',
333 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
334 (setq apropos-words nil
335 apropos-all-words nil
)
337 ;; We don't actually make a regexp matching all permutations.
338 ;; Instead, for e.g. "a b c", we make a regexp matching
339 ;; any combination of two or more words like this:
340 ;; (a|b|c).*(a|b|c) which may give some false matches,
341 ;; but as long as it also gives the right ones, that's ok.
342 (let ((words pattern
))
343 (setq apropos-pattern
(mapconcat 'identity pattern
" ")
344 apropos-pattern-quoted
(regexp-quote apropos-pattern
))
346 (let ((syn apropos-synonyms
) (s word
) (a word
))
348 (if (member word
(car syn
))
350 (setq a
(mapconcat 'identity
(car syn
) "\\|"))
351 (if (member word
(cdr (car syn
)))
354 (setq syn
(cdr syn
))))
355 (setq apropos-words
(cons s apropos-words
)
356 apropos-all-words
(cons a apropos-all-words
))))
357 (setq apropos-all-words-regexp
358 (apropos-words-to-regexp apropos-all-words
".+"))
360 (apropos-words-to-regexp apropos-words
".*?")))
361 (setq apropos-pattern-quoted
(regexp-quote pattern
)
362 apropos-all-words-regexp pattern
363 apropos-pattern pattern
364 apropos-regexp pattern
)))
367 (defun apropos-calc-scores (str words
)
368 "Return apropos scores for string STR matching WORDS.
369 Value is a list of offsets of the words into the string."
372 (dolist (word words scores
)
373 (if (setq i
(string-match word str
))
374 (setq scores
(cons i scores
))))
375 ;; Return list of start and end position of regexp
376 (and (string-match apropos-pattern str
)
377 (list (match-beginning 0) (match-end 0))))))
379 (defun apropos-score-str (str)
380 "Return apropos score for string STR."
382 (let* ((l (length str
))
383 (score (- (/ l
10))))
384 (dolist (s (apropos-calc-scores str apropos-all-words
) score
)
385 (setq score
(+ score
1000 (/ (* (- l s
) 1000) l
)))))
388 (defun apropos-score-doc (doc)
389 "Return apropos score for documentation string DOC."
390 (let ((l (length doc
)))
393 (when (string-match apropos-pattern-quoted doc
)
395 (dolist (s (apropos-calc-scores doc apropos-all-words
) score
)
396 (setq score
(+ score
50 (/ (* (- l s
) 50) l
)))))
399 (defun apropos-score-symbol (symbol &optional weight
)
400 "Return apropos score for SYMBOL."
401 (setq symbol
(symbol-name symbol
))
404 (dolist (s (apropos-calc-scores symbol apropos-words
) (* score
(or weight
3)))
405 (setq score
(+ score
(- 60 l
) (/ (* (- l s
) 60) l
))))))
407 (defun apropos-true-hit (str words
)
408 "Return t if STR is a genuine hit.
409 This may fail if only one of the keywords is matched more than once.
410 This requires that at least 2 keywords (unless only one was given)."
414 (> (length (apropos-calc-scores str words
)) 1)))
416 (defun apropos-false-hit-symbol (symbol)
417 "Return t if SYMBOL is not really matched by the current keywords."
418 (not (apropos-true-hit (symbol-name symbol
) apropos-words
)))
420 (defun apropos-false-hit-str (str)
421 "Return t if STR is not really matched by the current keywords."
422 (not (apropos-true-hit str apropos-words
)))
424 (defun apropos-true-hit-doc (doc)
425 "Return t if DOC is really matched by the current keywords."
426 (apropos-true-hit doc apropos-all-words
))
428 (define-derived-mode apropos-mode special-mode
"Apropos"
429 "Major mode for following hyperlinks in output of apropos commands.
431 \\{apropos-mode-map}")
433 (defvar apropos-multi-type t
434 "If non-nil, this apropos query concerns multiple types.
435 This is used to decide whether to print the result's type or not.")
438 (defun apropos-variable (pattern &optional do-all
)
439 "Show user variables that match PATTERN.
440 PATTERN can be a word, a list of words (separated by spaces),
441 or a regexp (using some regexp special characters). If it is a word,
442 search for matches for that word as a substring. If it is a list of words,
443 search for matches for any two (or more) of those words.
445 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
447 (interactive (list (apropos-read-pattern
448 (if (or current-prefix-arg apropos-do-all
)
449 "variable" "user option"))
451 (apropos-command pattern nil
452 (if (or do-all apropos-do-all
)
455 (get symbol
'variable-documentation
)))
458 ;; For auld lang syne:
460 (defalias 'command-apropos
'apropos-command
)
462 (defun apropos-command (pattern &optional do-all var-predicate
)
463 "Show commands (interactively callable functions) that match PATTERN.
464 PATTERN can be a word, a list of words (separated by spaces),
465 or a regexp (using some regexp special characters). If it is a word,
466 search for matches for that word as a substring. If it is a list of words,
467 search for matches for any two (or more) of those words.
469 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
470 noninteractive functions.
472 If VAR-PREDICATE is non-nil, show only variables, and only those that
473 satisfy the predicate VAR-PREDICATE.
475 When called from a Lisp program, a string PATTERN is used as a regexp,
476 while a list of strings is used as a word list."
477 (interactive (list (apropos-read-pattern
478 (if (or current-prefix-arg apropos-do-all
)
479 "command or function" "command"))
481 (apropos-parse-pattern pattern
)
483 (let ((standard-output (get-buffer-create "*Apropos*")))
484 (help-print-return-message 'identity
))))
485 (or do-all
(setq do-all apropos-do-all
))
486 (setq apropos-accumulator
487 (apropos-internal apropos-regexp
489 ;; We used to use `functionp' here, but this
490 ;; rules out macros. `fboundp' rules in
491 ;; keymaps, but it seems harmless.
492 (if do-all
'fboundp
'commandp
))))
493 (let ((tem apropos-accumulator
))
495 (if (or (get (car tem
) 'apropos-inhibit
)
496 (apropos-false-hit-symbol (car tem
)))
497 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
498 (setq tem
(cdr tem
))))
499 (let ((p apropos-accumulator
)
503 (setq symbol
(car p
))
504 (setq score
(apropos-score-symbol symbol
))
505 (unless var-predicate
507 (if (setq doc
(condition-case nil
508 (documentation symbol t
)
510 ;; Eg alias to undefined function.
512 "(documentation error)"
513 (setq score
(+ score
(apropos-score-doc doc
)))
514 (substring doc
0 (string-match "\n" doc
)))
515 "(not documented)")))
517 (funcall var-predicate symbol
)
518 (if (setq doc
(documentation-property
519 symbol
'variable-documentation t
))
521 (setq score
(+ score
(apropos-score-doc doc
)))
523 (string-match "\n" doc
)))))))
524 (setcar (cdr (car p
)) score
)
526 (and (let ((apropos-multi-type do-all
))
527 (apropos-print t nil nil t
))
529 (message "%s" message
))))
533 (defun apropos-documentation-property (symbol property raw
)
534 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
536 (let ((doc (documentation-property symbol property raw
)))
537 (if doc
(substring doc
0 (string-match "\n" doc
))
539 (error "(error retrieving documentation)")))
543 (defun apropos (pattern &optional do-all
)
544 "Show all meaningful Lisp symbols whose names match PATTERN.
545 Symbols are shown if they are defined as functions, variables, or
546 faces, or if they have nonempty property lists.
548 PATTERN can be a word, a list of words (separated by spaces),
549 or a regexp (using some regexp special characters). If it is a word,
550 search for matches for that word as a substring. If it is a list of words,
551 search for matches for any two (or more) of those words.
553 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
554 consider all symbols (if they match PATTERN).
556 Returns list of symbols and documentation found."
557 (interactive (list (apropos-read-pattern "symbol")
559 (apropos-parse-pattern pattern
)
560 (apropos-symbols-internal
561 (apropos-internal apropos-regexp
568 (symbol-plist symbol
)))))
569 (or do-all apropos-do-all
)))
571 (defun apropos-library-button (sym)
574 (let ((name (copy-sequence (symbol-name sym
))))
575 (make-text-button name nil
576 'type
'apropos-library
577 'face apropos-symbol-face
578 'apropos-symbol name
)
582 (defun apropos-library (file)
583 "List the variables and functions defined by library FILE.
584 FILE should be one of the libraries currently loaded and should
585 thus be found in `load-history'."
587 (let* ((libs (delq nil
(mapcar 'car load-history
)))
592 (setq l
(file-name-nondirectory l
))
594 (not (equal (setq l
(file-name-sans-extension l
))
599 (list (completing-read "Describe library: " libs nil t
))))
604 (lh-entry (assoc file load-history
)))
606 ;; `file' may be the "shortname".
607 (let ((lh load-history
)
608 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file
)
610 (while (and lh
(null lh-entry
))
611 (if (and (caar lh
) (string-match re
(caar lh
)))
612 (setq lh-entry
(car lh
))
613 (setq lh
(cdr lh
)))))
614 (unless lh-entry
(error "Unknown library `%s'" file
)))
615 (dolist (x (cdr lh-entry
))
617 ;; (autoload (push (cdr x) autoloads))
618 (require (push (cdr x
) requires
))
619 (provide (push (cdr x
) provides
))
620 (t (push (or (cdr-safe x
) x
) symbols
))))
621 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
622 (apropos-symbols-internal
623 symbols apropos-do-all
625 (format "Library `%s' provides: %s\nand requires: %s"
627 (mapconcat 'apropos-library-button
628 (or provides
'(nil)) " and ")
629 (mapconcat 'apropos-library-button
630 (or requires
'(nil)) " and ")))))))
632 (defun apropos-symbols-internal (symbols keys
&optional text
)
633 ;; Filter out entries that are marked as apropos-inhibit.
635 (dolist (symbol symbols
)
636 (unless (get symbol
'apropos-inhibit
)
639 (let ((apropos-accumulator
642 (let (doc properties
)
645 (apropos-score-symbol symbol
)
646 (when (fboundp symbol
)
647 (if (setq doc
(condition-case nil
648 (documentation symbol t
)
650 "(alias for undefined function)")
652 "(can't retrieve function documentation)")))
653 (substring doc
0 (string-match "\n" doc
))
655 (when (boundp symbol
)
656 (apropos-documentation-property
657 symbol
'variable-documentation t
))
658 (when (setq properties
(symbol-plist symbol
))
659 (setq doc
(list (car properties
)))
660 (while (setq properties
(cdr (cdr properties
)))
661 (setq doc
(cons (car properties
) doc
)))
662 (mapconcat #'symbol-name
(nreverse doc
) " "))
663 (when (get symbol
'widget-type
)
664 (apropos-documentation-property
665 symbol
'widget-documentation t
))
667 (let ((alias (get symbol
'face-alias
)))
670 (format "%slias for the face `%s'."
671 (if (get symbol
'obsolete-face
)
675 ;; Never happens in practice because fails
676 ;; (facep symbol) test.
677 "(alias for undefined face)")
678 (apropos-documentation-property
679 symbol
'face-documentation t
))))
680 (when (get symbol
'custom-group
)
681 (apropos-documentation-property
682 symbol
'group-documentation t
)))))
684 (apropos-print keys nil text
)))
688 (defun apropos-value (pattern &optional do-all
)
689 "Show all symbols whose value's printed representation matches PATTERN.
690 PATTERN can be a word, a list of words (separated by spaces),
691 or a regexp (using some regexp special characters). If it is a word,
692 search for matches for that word as a substring. If it is a list of words,
693 search for matches for any two (or more) of those words.
695 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
696 at the function and at the names and values of properties.
697 Returns list of symbols and values found."
698 (interactive (list (apropos-read-pattern "value")
700 (apropos-parse-pattern pattern
)
701 (or do-all
(setq do-all apropos-do-all
))
702 (setq apropos-accumulator
())
706 (setq f nil v nil p nil
)
707 (or (memq symbol
'(apropos-regexp
708 apropos-pattern apropos-all-words-regexp
709 apropos-words apropos-all-words
710 do-all apropos-accumulator
712 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
714 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
715 p
(apropos-format-plist symbol
"\n " t
)))
716 (if (apropos-false-hit-str v
)
718 (if (apropos-false-hit-str f
)
720 (if (apropos-false-hit-str p
)
723 (setq apropos-accumulator
(cons (list symbol
724 (+ (apropos-score-str f
)
725 (apropos-score-str v
)
726 (apropos-score-str p
))
728 apropos-accumulator
))))))
729 (let ((apropos-multi-type do-all
))
730 (apropos-print nil
"\n----------------\n")))
734 (defun apropos-documentation (pattern &optional do-all
)
735 "Show symbols whose documentation contains matches for PATTERN.
736 PATTERN can be a word, a list of words (separated by spaces),
737 or a regexp (using some regexp special characters). If it is a word,
738 search for matches for that word as a substring. If it is a list of words,
739 search for matches for any two (or more) of those words.
741 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
742 documentation that is not stored in the documentation file and show key
744 Returns list of symbols and documentation found."
745 (interactive (list (apropos-read-pattern "documentation")
747 (apropos-parse-pattern pattern
)
748 (or do-all
(setq do-all apropos-do-all
))
749 (setq apropos-accumulator
() apropos-files-scanned
())
750 (let ((standard-input (get-buffer-create " apropos-temp"))
751 (apropos-sort-by-scores apropos-documentation-sort-by-scores
)
754 (with-current-buffer standard-input
755 (apropos-documentation-check-doc-file)
759 (setq f
(apropos-safe-documentation symbol
)
760 v
(get symbol
'variable-documentation
))
761 (if (integerp v
) (setq v
))
762 (setq f
(apropos-documentation-internal f
)
763 v
(apropos-documentation-internal v
))
764 (setq sf
(apropos-score-doc f
)
765 sv
(apropos-score-doc v
))
767 (if (setq apropos-item
768 (cdr (assq symbol apropos-accumulator
)))
772 (setcar (nthcdr 1 apropos-item
) f
)
773 (setcar apropos-item
(+ (car apropos-item
) sf
))))
776 (setcar (nthcdr 2 apropos-item
) v
)
777 (setcar apropos-item
(+ (car apropos-item
) sv
)))))
778 (setq apropos-accumulator
780 (+ (apropos-score-symbol symbol
2) sf sv
)
782 apropos-accumulator
)))))))
783 (apropos-print nil
"\n----------------\n" nil t
))
784 (kill-buffer standard-input
))))
787 (defun apropos-value-internal (predicate symbol function
)
788 (if (funcall predicate symbol
)
790 (setq symbol
(prin1-to-string (funcall function symbol
)))
791 (if (string-match apropos-regexp symbol
)
793 (if apropos-match-face
794 (put-text-property (match-beginning 0) (match-end 0)
795 'face apropos-match-face
799 (defun apropos-documentation-internal (doc)
801 (apropos-documentation-check-elc-file (car doc
))
803 (string-match apropos-all-words-regexp doc
)
804 (apropos-true-hit-doc doc
))
805 (when apropos-match-face
806 (setq doc
(substitute-command-keys (copy-sequence doc
)))
807 (if (or (string-match apropos-pattern-quoted doc
)
808 (string-match apropos-all-words-regexp doc
))
809 (put-text-property (match-beginning 0)
811 'face apropos-match-face doc
))
814 (defun apropos-format-plist (pl sep
&optional compare
)
815 (setq pl
(symbol-plist pl
))
818 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
819 (if (or (not compare
) (string-match apropos-regexp p
))
820 (if apropos-property-face
821 (put-text-property 0 (length (symbol-name (car pl
)))
822 'face apropos-property-face p
))
826 (and compare apropos-match-face
827 (put-text-property (match-beginning 0) (match-end 0)
828 'face apropos-match-face
830 (setq p-out
(concat p-out
(if p-out sep
) p
))))
831 (setq pl
(nthcdr 2 pl
)))
835 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
837 (defun apropos-documentation-check-doc-file ()
838 (let (type symbol
(sepa 2) sepb doc
)
841 (insert-file-contents (concat doc-directory internal-doc-file-name
))
843 (while (save-excursion
844 (setq sepb
(search-forward "\^_"))
846 (beginning-of-line 2)
847 (if (save-restriction
848 (narrow-to-region (point) (1- sepb
))
849 (re-search-forward apropos-all-words-regexp nil t
))
851 (goto-char (1+ sepa
))
852 (setq type
(if (eq ?F
(preceding-char))
853 2 ; function documentation
854 3) ; variable documentation
856 doc
(buffer-substring (1+ (point)) (1- sepb
)))
857 (when (and (apropos-true-hit-doc doc
)
858 ;; The DOC file lists all built-in funcs and vars.
859 ;; If any are not currently bound, they can
860 ;; only be platform-specific stuff (eg NS) not
861 ;; in use on the current platform.
862 ;; So we exclude them.
863 (cond ((= 3 type
) (boundp symbol
))
864 ((= 2 type
) (fboundp symbol
))))
865 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
866 (setcar (cdr apropos-item
)
867 (apropos-score-doc doc
)))
868 (setq apropos-item
(list symbol
869 (+ (apropos-score-symbol symbol
2)
870 (apropos-score-doc doc
))
872 apropos-accumulator
(cons apropos-item
873 apropos-accumulator
)))
874 (when apropos-match-face
875 (setq doc
(substitute-command-keys doc
))
876 (if (or (string-match apropos-pattern-quoted doc
)
877 (string-match apropos-all-words-regexp doc
))
878 (put-text-property (match-beginning 0)
880 'face apropos-match-face doc
)))
881 (setcar (nthcdr type apropos-item
) doc
))))
882 (setq sepa
(goto-char sepb
)))))
884 (defun apropos-documentation-check-elc-file (file)
885 (if (member file apropos-files-scanned
)
887 (let (symbol doc beg end this-is-a-variable
)
888 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
890 (insert-file-contents file
)
891 (while (search-forward "\n#@" nil t
)
892 ;; Read the comment length, and advance over it.
895 end
(+ (point) end -
1))
897 (if (save-restriction
898 ;; match ^ and $ relative to doc string
899 (narrow-to-region beg end
)
900 (re-search-forward apropos-all-words-regexp nil t
))
902 (goto-char (+ end
2))
903 (setq doc
(buffer-substring beg end
)
904 end
(- (match-end 0) beg
)
905 beg
(- (match-beginning 0) beg
))
906 (when (apropos-true-hit-doc doc
)
907 (setq this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
909 (skip-chars-forward "(a-z")
912 symbol
(if (consp symbol
)
915 (if (if this-is-a-variable
916 (get symbol
'variable-documentation
)
917 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
919 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
920 (setcar (cdr apropos-item
)
921 (+ (cadr apropos-item
) (apropos-score-doc doc
))))
922 (setq apropos-item
(list symbol
923 (+ (apropos-score-symbol symbol
2)
924 (apropos-score-doc doc
))
926 apropos-accumulator
(cons apropos-item
927 apropos-accumulator
)))
928 (when apropos-match-face
929 (setq doc
(substitute-command-keys doc
))
930 (if (or (string-match apropos-pattern-quoted doc
)
931 (string-match apropos-all-words-regexp doc
))
932 (put-text-property (match-beginning 0)
934 'face apropos-match-face doc
)))
935 (setcar (nthcdr (if this-is-a-variable
3 2)
941 (defun apropos-safe-documentation (function)
942 "Like `documentation', except it avoids calling `get_doc_string'.
943 Will return nil instead."
944 (while (and function
(symbolp function
))
945 (setq function
(if (fboundp function
)
946 (symbol-function function
))))
947 (if (eq (car-safe function
) 'macro
)
948 (setq function
(cdr function
)))
949 (setq function
(if (byte-code-function-p function
)
950 (if (> (length function
) 4)
952 (if (eq (car-safe function
) 'autoload
)
954 (if (eq (car-safe function
) 'lambda
)
955 (if (stringp (nth 2 function
))
957 (if (stringp (nth 3 function
))
958 (nth 3 function
)))))))
959 (if (integerp function
)
963 (defcustom apropos-compact-layout nil
964 "If non-nil, use a single line per binding."
967 (defun apropos-print (do-keys spacing
&optional text nosubst
)
968 "Output result of apropos searching into buffer `*Apropos*'.
969 The value of `apropos-accumulator' is the list of items to output.
970 Each element should have the format
971 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
972 The return value is the list that was in `apropos-accumulator', sorted
973 alphabetically by symbol name; but this function also sets
974 `apropos-accumulator' to nil before returning.
976 If SPACING is non-nil, it should be a string; separate items with that string.
977 If non-nil TEXT is a string that will be printed as a heading."
978 (if (null apropos-accumulator
)
979 (message "No apropos matches for `%s'" apropos-pattern
)
980 (setq apropos-accumulator
981 (sort apropos-accumulator
983 ;; Don't sort by score if user can't see the score.
984 ;; It would be confusing. -- rms.
985 (if apropos-sort-by-scores
986 (or (> (cadr a
) (cadr b
))
987 (and (= (cadr a
) (cadr b
))
988 (string-lessp (car a
) (car b
))))
989 (string-lessp (car a
) (car b
))))))
990 (with-output-to-temp-buffer "*Apropos*"
991 (let ((p apropos-accumulator
)
992 (old-buffer (current-buffer))
993 (inhibit-read-only t
)
995 (set-buffer standard-output
)
997 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
998 (if apropos-multi-type
"a type label" "an entry")
999 " to view its full documentation.\n\n")
1000 (if text
(insert text
"\n\n"))
1001 (dolist (apropos-item p
)
1002 (when (and spacing
(not (bobp)))
1004 (setq symbol
(car apropos-item
))
1005 ;; Insert dummy score element for backwards compatibility with 21.x
1006 ;; apropos-item format.
1007 (if (not (numberp (cadr apropos-item
)))
1009 (cons (car apropos-item
)
1010 (cons nil
(cdr apropos-item
)))))
1011 (insert-text-button (symbol-name symbol
)
1012 'type
'apropos-symbol
1013 'skip apropos-multi-type
1014 ;; Can't use default, since user may have
1015 ;; changed the variable!
1016 ;; Just say `no' to variables containing faces!
1017 'face apropos-symbol-face
)
1018 (if (and (eq apropos-sort-by-scores
'verbose
)
1019 (cadr apropos-item
))
1020 (insert " (" (number-to-string (cadr apropos-item
)) ") "))
1021 ;; Calculate key-bindings if we want them.
1022 (unless apropos-compact-layout
1025 (not (eq symbol
'self-insert-command
))
1028 (with-current-buffer old-buffer
1029 (where-is-internal symbol
)))
1031 ;; Copy over the list of key sequences,
1032 ;; omitting any that contain a buffer or a frame.
1033 ;; FIXME: Why omit keys that contain buffers and
1034 ;; frames? This looks like a bad workaround rather
1035 ;; than a proper fix. Does anybod know what problem
1036 ;; this is trying to address? --Stef
1040 (while (< i
(length key
))
1041 (if (or (framep (aref key i
))
1042 (bufferp (aref key i
)))
1046 (push key filtered
))))
1047 (setq item filtered
))
1048 ;; Convert the remaining keys to a string and insert.
1052 (setq key
(condition-case ()
1053 (key-description key
)
1055 (if apropos-keybinding-face
1056 (put-text-property 0 (length key
)
1057 'face apropos-keybinding-face
1061 (insert "M-x ... RET")
1062 (when apropos-keybinding-face
1063 (put-text-property (- (point) 11) (- (point) 8)
1064 'face apropos-keybinding-face
)
1065 (put-text-property (- (point) 3) (point)
1066 'face apropos-keybinding-face
))))
1068 (apropos-print-doc 2
1069 (if (commandp symbol
)
1071 (if (apropos-macrop symbol
)
1075 (apropos-print-doc 3 'apropos-variable
(not nosubst
))
1076 (apropos-print-doc 7 'apropos-group t
)
1077 (apropos-print-doc 6 'apropos-face t
)
1078 (apropos-print-doc 5 'apropos-widget t
)
1079 (apropos-print-doc 4 'apropos-plist nil
))
1080 (set (make-local-variable 'truncate-partial-width-windows
) t
)
1081 (set (make-local-variable 'truncate-lines
) t
))))
1082 (prog1 apropos-accumulator
1083 (setq apropos-accumulator
()))) ; permit gc
1085 (defun apropos-macrop (symbol)
1086 "Return t if SYMBOL is a Lisp macro."
1087 (and (fboundp symbol
)
1089 (symbol-function symbol
)))
1090 (or (eq (car symbol
) 'macro
)
1091 (if (eq (car symbol
) 'autoload
)
1092 (memq (nth 4 symbol
)
1096 (defun apropos-print-doc (i type do-keys
)
1097 (let ((doc (nth i apropos-item
)))
1099 (if apropos-compact-layout
1100 (insert (propertize "\t" 'display
'(space :align-to
32)) " ")
1102 (if apropos-multi-type
1103 (let ((button-face (button-type-get type
'face
)))
1104 (unless (consp button-face
)
1105 (setq button-face
(list button-face
)))
1107 (if apropos-compact-layout
1108 (format "<%s>" (button-type-get type
'apropos-short-label
))
1109 (button-type-get type
'apropos-label
))
1111 ;; Can't use the default button face, since user may have changed the
1112 ;; variable! Just say `no' to variables containing faces!
1113 'face
(append button-face apropos-label-face
)
1114 'apropos-symbol
(car apropos-item
))
1115 (insert (if apropos-compact-layout
" " ": ")))
1117 ;; If the query is only for a single type, there's no point
1118 ;; writing it over and over again. Insert a blank button, and
1119 ;; put the 'apropos-label property there (needed by
1120 ;; apropos-symbol-button-display-help).
1122 " " 'type type
'skip t
1123 'face
'default
'apropos-symbol
(car apropos-item
)))
1125 (let ((opoint (point))
1126 (ocol (current-column)))
1127 (cond ((equal doc
"")
1128 (setq doc
"(not documented)"))
1130 (setq doc
(substitute-command-keys doc
))))
1132 (if (equal doc
"(not documented)")
1133 (put-text-property opoint
(point) 'font-lock-face
'shadow
))
1134 ;; The labeling buttons might make the line too long, so fill it if
1136 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column
)
1137 emacs-lisp-docstring-fill-column
1139 (fill-prefix (make-string ocol ?\s
)))
1140 (fill-region opoint
(point) nil t
)))
1141 (or (bolp) (terpri)))))
1143 (defun apropos-follow ()
1144 "Invokes any button at point, otherwise invokes the nearest label button."
1147 (or (apropos-next-label-button (line-beginning-position))
1148 (error "There is nothing to follow here"))))
1151 (defun apropos-describe-plist (symbol)
1152 "Display a pretty listing of SYMBOL's plist."
1153 (help-setup-xref (list 'apropos-describe-plist symbol
)
1154 (called-interactively-p 'interactive
))
1155 (with-help-window (help-buffer)
1156 (set-buffer standard-output
)
1159 (princ "'s plist is\n (")
1160 (if apropos-symbol-face
1161 (put-text-property (+ (point-min) 7) (- (point) 14)
1162 'face apropos-symbol-face
))
1163 (insert (apropos-format-plist symbol
"\n "))
1169 ;;; apropos.el ends here