1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994-1995, 2001-2018 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 <https://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."
134 :type
'(choice (const nil
) face
)
138 (defcustom apropos-sort-by-scores nil
139 "Non-nil means sort matches by scores; best match is shown first.
140 This applies to all `apropos' commands except `apropos-documentation'.
141 If value is `verbose', the computed score is shown for each match."
143 :type
'(choice (const :tag
"off" nil
)
145 (const :tag
"show scores" verbose
)))
147 (defcustom apropos-documentation-sort-by-scores t
148 "Non-nil means sort matches by scores; best match is shown first.
149 This applies to `apropos-documentation' only.
150 If value is `verbose', the computed score is shown for each match."
152 :type
'(choice (const :tag
"off" nil
)
154 (const :tag
"show scores" verbose
)))
156 (defvar apropos-mode-map
157 (let ((map (copy-keymap button-buffer-map
)))
158 (set-keymap-parent map special-mode-map
)
159 ;; Use `apropos-follow' instead of just using the button
160 ;; definition of RET, so that users can use it anywhere in an
161 ;; apropos item, not just on top of a button.
162 (define-key map
"\C-m" 'apropos-follow
)
164 "Keymap used in Apropos mode.")
166 (defvar apropos-mode-hook nil
167 "Hook run when mode is turned on.")
169 (defvar apropos-pattern nil
170 "Apropos pattern as entered by user.")
172 (defvar apropos-pattern-quoted nil
173 "Apropos pattern passed through `regexp-quote'.")
175 (defvar apropos-words
()
176 "Current list of apropos words extracted from `apropos-pattern'.")
178 (defvar apropos-all-words
()
179 "Current list of words and synonyms.")
181 (defvar apropos-regexp nil
182 "Regexp used in current apropos run.")
184 (defvar apropos-all-words-regexp nil
185 "Regexp matching `apropos-all-words'.")
187 (defvar apropos-files-scanned
()
188 "List of elc files already scanned in current run of `apropos-documentation'.")
190 (defvar apropos-accumulator
()
191 "Alist of symbols already found in current apropos run.
192 Each element has the form
194 (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
196 where SYMBOL is the symbol name, SCORE is its relevance score (a
197 number), FUN-DOC is the function docstring, VAR-DOC is the
198 variable docstring, PLIST is the list of the symbols names in the
199 property list, WIDGET-DOC is the widget docstring, FACE-DOC is
200 the face docstring, and CUS-GROUP-DOC is the custom group
201 docstring. Each docstring is either nil or a string.")
203 (defvar apropos-item
()
204 "Current item in or for `apropos-accumulator'.")
206 (defvar apropos-synonyms
'(
207 ("find" "open" "edit")
210 ("region" "selection"))
211 "List of synonyms known by apropos.
212 Each element is a list of words where the first word is the standard Emacs
213 term, and the rest of the words are alternative terms.")
216 ;;; Button types used by apropos
218 (define-button-type 'apropos-symbol
219 'face
'apropos-symbol
220 'help-echo
"mouse-2, RET: Display more help on this symbol"
222 'action
#'apropos-symbol-button-display-help
)
224 (defun apropos-symbol-button-display-help (button)
225 "Display further help for the `apropos-symbol' button BUTTON."
227 (or (apropos-next-label-button (button-start button
))
228 (error "There is nothing to follow for `%s'" (button-label button
)))))
230 (define-button-type 'apropos-function
231 'apropos-label
"Function"
232 'apropos-short-label
"f"
233 'face
'apropos-function-button
234 'help-echo
"mouse-2, RET: Display more help on this function"
236 'action
(lambda (button)
237 (describe-function (button-get button
'apropos-symbol
))))
239 (define-button-type 'apropos-macro
240 'apropos-label
"Macro"
241 'apropos-short-label
"m"
242 'face
'apropos-function-button
243 'help-echo
"mouse-2, RET: Display more help on this macro"
245 'action
(lambda (button)
246 (describe-function (button-get button
'apropos-symbol
))))
248 (define-button-type 'apropos-command
249 'apropos-label
"Command"
250 'apropos-short-label
"c"
251 'face
'apropos-function-button
252 'help-echo
"mouse-2, RET: Display more help on this command"
254 'action
(lambda (button)
255 (describe-function (button-get button
'apropos-symbol
))))
257 ;; We used to use `customize-variable-other-window' instead for a
258 ;; customizable variable, but that is slow. It is better to show an
259 ;; ordinary help buffer and let the user click on the customization
260 ;; button in that buffer, if he wants to.
261 ;; Likewise for `customize-face-other-window'.
262 (define-button-type 'apropos-variable
263 'apropos-label
"Variable"
264 'apropos-short-label
"v"
265 'face
'apropos-variable-button
266 'help-echo
"mouse-2, RET: Display more help on this variable"
268 'action
(lambda (button)
269 (describe-variable (button-get button
'apropos-symbol
))))
271 (define-button-type 'apropos-user-option
272 'apropos-label
"User option"
273 'apropos-short-label
"o"
274 'face
'apropos-user-option-button
275 'help-echo
"mouse-2, RET: Display more help on this user option"
277 'action
(lambda (button)
278 (describe-variable (button-get button
'apropos-symbol
))))
280 (define-button-type 'apropos-face
281 'apropos-label
"Face"
282 'apropos-short-label
"F"
283 'face
'(font-lock-variable-name-face button
)
284 'help-echo
"mouse-2, RET: Display more help on this face"
286 'action
(lambda (button)
287 (describe-face (button-get button
'apropos-symbol
))))
289 (define-button-type 'apropos-group
290 'apropos-label
"Group"
291 'apropos-short-label
"g"
292 'face
'apropos-misc-button
293 'help-echo
"mouse-2, RET: Display more help on this group"
295 'action
(lambda (button)
296 (customize-group-other-window
297 (button-get button
'apropos-symbol
))))
299 (define-button-type 'apropos-widget
300 'apropos-label
"Widget"
301 'apropos-short-label
"w"
302 'face
'apropos-misc-button
303 'help-echo
"mouse-2, RET: Display more help on this widget"
305 'action
(lambda (button)
306 (widget-browse-other-window (button-get button
'apropos-symbol
))))
308 (define-button-type 'apropos-plist
309 'apropos-label
"Properties"
310 'apropos-short-label
"p"
311 'face
'apropos-misc-button
312 'help-echo
"mouse-2, RET: Display more help on this plist"
314 'action
(lambda (button)
315 (apropos-describe-plist (button-get button
'apropos-symbol
))))
317 (define-button-type 'apropos-library
318 'help-echo
"mouse-2, RET: Display more help on this library"
320 'action
(lambda (button)
321 (apropos-library (button-get button
'apropos-symbol
))))
323 (defun apropos-next-label-button (pos)
324 "Return the next apropos label button after POS, or nil if there's none.
325 Will also return nil if more than one `apropos-symbol' button is encountered
326 before finding a label."
327 (let* ((button (next-button pos t
))
328 (already-hit-symbol nil
)
329 (label (and button
(button-get button
'apropos-label
)))
330 (type (and button
(button-get button
'type
))))
333 (or (not (eq type
'apropos-symbol
))
334 (not already-hit-symbol
)))
335 (when (eq type
'apropos-symbol
)
336 (setq already-hit-symbol t
))
337 (setq button
(next-button (button-start button
)))
339 (setq label
(button-get button
'apropos-label
))
340 (setq type
(button-get button
'type
))))
344 (defun apropos-words-to-regexp (words wild
)
345 "Make regexp matching any two of the words in WORDS.
346 WILD should be a subexpression matching wildcards between matches."
347 (setq words
(delete-dups (copy-sequence words
)))
348 (if (null (cdr words
))
352 (concat "\\(?:" w
"\\)" ;; parens for synonyms
355 (delq w
(copy-sequence words
))
362 (defun apropos-read-pattern (subject)
363 "Read an apropos pattern, either a word list or a regexp.
364 Returns the user pattern, either a list of words which are matched
365 literally, or a string which is used as a regexp to search for.
367 SUBJECT is a string that is included in the prompt to identify what
368 kind of objects to search."
370 (read-string (concat "Search for " subject
" (word list or regexp): "))))
371 (if (string-equal (regexp-quote pattern
) pattern
)
373 (or (split-string pattern
"[ \t]+" t
)
374 (user-error "No word list given"))
377 (defun apropos-parse-pattern (pattern)
378 "Rewrite a list of words to a regexp matching all permutations.
379 If PATTERN is a string, that means it is already a regexp.
380 This updates variables `apropos-pattern', `apropos-pattern-quoted',
381 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
382 (setq apropos-words nil
383 apropos-all-words nil
)
385 ;; We don't actually make a regexp matching all permutations.
386 ;; Instead, for e.g. "a b c", we make a regexp matching
387 ;; any combination of two or more words like this:
388 ;; (a|b|c).*(a|b|c) which may give some false matches,
389 ;; but as long as it also gives the right ones, that's ok.
390 (let ((words pattern
))
391 (setq apropos-pattern
(mapconcat 'identity pattern
" ")
392 apropos-pattern-quoted
(regexp-quote apropos-pattern
))
394 (let ((syn apropos-synonyms
) (s word
) (a word
))
396 (if (member word
(car syn
))
398 (setq a
(mapconcat 'identity
(car syn
) "\\|"))
399 (if (member word
(cdr (car syn
)))
402 (setq syn
(cdr syn
))))
403 (setq apropos-words
(cons s apropos-words
)
404 apropos-all-words
(cons a apropos-all-words
))))
405 (setq apropos-all-words-regexp
406 (apropos-words-to-regexp apropos-all-words
".+"))
408 (apropos-words-to-regexp apropos-words
".*?")))
409 (setq apropos-pattern-quoted
(regexp-quote pattern
)
410 apropos-all-words-regexp pattern
411 apropos-pattern pattern
412 apropos-regexp pattern
)))
414 (defun apropos-calc-scores (str words
)
415 "Return apropos scores for string STR matching WORDS.
416 Value is a list of offsets of the words into the string."
419 (dolist (word words scores
)
420 (if (setq i
(string-match word str
))
421 (setq scores
(cons i scores
))))
422 ;; Return list of start and end position of regexp
423 (and (string-match apropos-pattern str
)
424 (list (match-beginning 0) (match-end 0))))))
426 (defun apropos-score-str (str)
427 "Return apropos score for string STR."
429 (let* ((l (length str
))
430 (score (- (/ l
10))))
431 (dolist (s (apropos-calc-scores str apropos-all-words
) score
)
432 (setq score
(+ score
1000 (/ (* (- l s
) 1000) l
)))))
435 (defun apropos-score-doc (doc)
436 "Return apropos score for documentation string DOC."
437 (let ((l (length doc
)))
440 (when (string-match apropos-pattern-quoted doc
)
442 (dolist (s (apropos-calc-scores doc apropos-all-words
) score
)
443 (setq score
(+ score
50 (/ (* (- l s
) 50) l
)))))
446 (defun apropos-score-symbol (symbol &optional weight
)
447 "Return apropos score for SYMBOL."
448 (setq symbol
(symbol-name symbol
))
451 (dolist (s (apropos-calc-scores symbol apropos-words
) (* score
(or weight
3)))
452 (setq score
(+ score
(- 60 l
) (/ (* (- l s
) 60) l
))))))
454 (defun apropos-true-hit (str words
)
455 "Return t if STR is a genuine hit.
456 This may fail if only one of the keywords is matched more than once.
457 This requires at least two keywords (unless only one was given)."
461 (> (length (apropos-calc-scores str words
)) 1)))
463 (defun apropos-false-hit-symbol (symbol)
464 "Return t if SYMBOL is not really matched by the current keywords."
465 (not (apropos-true-hit (symbol-name symbol
) apropos-words
)))
467 (defun apropos-false-hit-str (str)
468 "Return t if STR is not really matched by the current keywords."
469 (not (apropos-true-hit str apropos-words
)))
471 (defun apropos-true-hit-doc (doc)
472 "Return t if DOC is really matched by the current keywords."
473 (apropos-true-hit doc apropos-all-words
))
475 (define-derived-mode apropos-mode special-mode
"Apropos"
476 "Major mode for following hyperlinks in output of apropos commands.
478 \\{apropos-mode-map}")
480 (defvar apropos-multi-type t
481 "If non-nil, this apropos query concerns multiple types.
482 This is used to decide whether to print the result's type or not.")
485 (defun apropos-user-option (pattern &optional do-all
)
486 "Show user options that match PATTERN.
487 PATTERN can be a word, a list of words (separated by spaces),
488 or a regexp (using some regexp special characters). If it is a word,
489 search for matches for that word as a substring. If it is a list of words,
490 search for matches for any two (or more) of those words.
492 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
493 variables, not just user options."
494 (interactive (list (apropos-read-pattern
495 (if (or current-prefix-arg apropos-do-all
)
496 "variable" "user option"))
498 (apropos-command pattern nil
499 (if (or do-all apropos-do-all
)
502 (get symbol
'variable-documentation
)))
503 'custom-variable-p
)))
506 (defun apropos-variable (pattern &optional do-not-all
)
507 "Show variables that match PATTERN.
508 With the optional argument DO-NOT-ALL non-nil (or when called
509 interactively with the prefix \\[universal-argument]), show user
510 options only, i.e. behave like `apropos-user-option'."
511 (interactive (list (apropos-read-pattern
512 (if current-prefix-arg
"user option" "variable"))
514 (let ((apropos-do-all (if do-not-all nil t
)))
515 (apropos-user-option pattern
)))
518 (defun apropos-local-variable (pattern &optional buffer
)
519 "Show buffer-local variables that match PATTERN.
520 Optional arg BUFFER (default: current buffer) is the buffer to check.
522 The output includes variables that are not yet set in BUFFER, but that
523 will be buffer-local when set."
524 (interactive (list (apropos-read-pattern "buffer-local variable")))
525 (unless buffer
(setq buffer
(current-buffer)))
526 (apropos-command pattern nil
(lambda (symbol)
527 (and (local-variable-if-set-p symbol
)
528 (get symbol
'variable-documentation
)))))
530 ;; For auld lang syne:
532 (defalias 'command-apropos
'apropos-command
)
534 (defun apropos-command (pattern &optional do-all var-predicate
)
535 "Show commands (interactively callable functions) that match PATTERN.
536 PATTERN can be a word, a list of words (separated by spaces),
537 or a regexp (using some regexp special characters). If it is a word,
538 search for matches for that word as a substring. If it is a list of words,
539 search for matches for any two (or more) of those words.
541 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
542 noninteractive functions.
544 If VAR-PREDICATE is non-nil, show only variables, and only those that
545 satisfy the predicate VAR-PREDICATE.
547 When called from a Lisp program, a string PATTERN is used as a regexp,
548 while a list of strings is used as a word list."
549 (interactive (list (apropos-read-pattern
550 (if (or current-prefix-arg apropos-do-all
)
551 "command or function" "command"))
553 (apropos-parse-pattern pattern
)
555 (let ((standard-output (get-buffer-create "*Apropos*")))
556 (help-print-return-message 'identity
))))
557 (or do-all
(setq do-all apropos-do-all
))
558 (setq apropos-accumulator
559 (apropos-internal apropos-regexp
561 ;; We used to use `functionp' here, but this
562 ;; rules out macros. `fboundp' rules in
563 ;; keymaps, but it seems harmless.
564 (if do-all
'fboundp
'commandp
))))
565 (let ((tem apropos-accumulator
))
567 (if (or (get (car tem
) 'apropos-inhibit
)
568 (apropos-false-hit-symbol (car tem
)))
569 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
570 (setq tem
(cdr tem
))))
571 (let ((p apropos-accumulator
)
575 (setq symbol
(car p
))
576 (setq score
(apropos-score-symbol symbol
))
577 (unless var-predicate
579 (if (setq doc
(condition-case nil
580 (documentation symbol t
)
582 ;; Eg alias to undefined function.
584 "(documentation error)"
585 (setq score
(+ score
(apropos-score-doc doc
)))
586 (substring doc
0 (string-match "\n" doc
)))
587 "(not documented)")))
589 (funcall var-predicate symbol
)
590 (if (setq doc
(documentation-property
591 symbol
'variable-documentation t
))
593 (setq score
(+ score
(apropos-score-doc doc
)))
595 (string-match "\n" doc
)))))))
596 (setcar (cdr (car p
)) score
)
598 (and (let ((apropos-multi-type do-all
))
599 (apropos-print t nil nil t
))
601 (message "%s" message
))))
605 (defun apropos-documentation-property (symbol property raw
)
606 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
608 (let ((doc (documentation-property symbol property raw
)))
609 (if doc
(substring doc
0 (string-match "\n" doc
))
611 (error "(error retrieving documentation)")))
615 (defun apropos (pattern &optional do-all
)
616 "Show all meaningful Lisp symbols whose names match PATTERN.
617 Symbols are shown if they are defined as functions, variables, or
618 faces, or if they have nonempty property lists.
620 PATTERN can be a word, a list of words (separated by spaces),
621 or a regexp (using some regexp special characters). If it is a word,
622 search for matches for that word as a substring. If it is a list of words,
623 search for matches for any two (or more) of those words.
625 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
626 consider all symbols (if they match PATTERN).
628 Returns list of symbols and documentation found."
629 (interactive (list (apropos-read-pattern "symbol")
631 (apropos-parse-pattern pattern
)
632 (apropos-symbols-internal
633 (apropos-internal apropos-regexp
640 (symbol-plist symbol
)))))
641 (or do-all apropos-do-all
)))
643 (defun apropos-library-button (sym)
646 (let ((name (copy-sequence (symbol-name sym
))))
647 (make-text-button name nil
648 'type
'apropos-library
649 'face
'apropos-symbol
650 'apropos-symbol name
)
654 (defun apropos-library (file)
655 "List the variables and functions defined by library FILE.
656 FILE should be one of the libraries currently loaded and should
657 thus be found in `load-history'. If `apropos-do-all' is non-nil,
658 the output includes key-bindings of commands."
660 (let* ((libs (delq nil
(mapcar 'car load-history
)))
665 (setq l
(file-name-nondirectory l
))
667 (not (equal (setq l
(file-name-sans-extension l
))
672 (list (completing-read "Describe library: " libs nil t
))))
677 (lh-entry (assoc file load-history
)))
679 ;; `file' may be the "shortname".
680 (let ((lh load-history
)
681 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file
)
683 (while (and lh
(null lh-entry
))
684 (if (and (caar lh
) (string-match re
(caar lh
)))
685 (setq lh-entry
(car lh
))
686 (setq lh
(cdr lh
)))))
687 (unless lh-entry
(error "Unknown library `%s'" file
)))
688 (dolist (x (cdr lh-entry
))
690 ;; (autoload (push (cdr x) autoloads))
691 (`require
(push (cdr x
) requires
))
692 (`provide
(push (cdr x
) provides
))
693 (`t nil
) ; Skip "was an autoload" entries.
694 ;; FIXME: Print information about each individual method: both
695 ;; its docstring and specializers (bug#21422).
696 (`cl-defmethod
(push (cadr x
) provides
))
697 (_ (push (or (cdr-safe x
) x
) symbols
))))
698 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
699 (apropos-symbols-internal
700 symbols apropos-do-all
703 "Library `%s' provides: %s\nand requires: %s"
705 (mapconcat 'apropos-library-button
706 (or provides
'(nil)) " and ")
707 (mapconcat 'apropos-library-button
708 (or requires
'(nil)) " and ")))))))
710 (defun apropos-symbols-internal (symbols keys
&optional text
)
711 ;; Filter out entries that are marked as apropos-inhibit.
713 (dolist (symbol symbols
)
714 (unless (get symbol
'apropos-inhibit
)
717 (let ((apropos-accumulator
720 (let (doc properties
)
723 (apropos-score-symbol symbol
)
724 (when (fboundp symbol
)
725 (if (setq doc
(condition-case nil
726 (documentation symbol t
)
728 "(alias for undefined function)")
730 "(can't retrieve function documentation)")))
731 (substring doc
0 (string-match "\n" doc
))
733 (when (boundp symbol
)
734 (apropos-documentation-property
735 symbol
'variable-documentation t
))
736 (when (setq properties
(symbol-plist symbol
))
737 (setq doc
(list (car properties
)))
738 (while (setq properties
(cdr (cdr properties
)))
739 (setq doc
(cons (car properties
) doc
)))
740 (mapconcat (lambda (p) (format "%s" p
)) (nreverse doc
) " "))
741 (when (get symbol
'widget-type
)
742 (apropos-documentation-property
743 symbol
'widget-documentation t
))
745 (let ((alias (get symbol
'face-alias
)))
749 "%slias for the face `%s'."
750 (if (get symbol
'obsolete-face
) "Obsolete a" "A")
752 ;; Never happens in practice because fails
753 ;; (facep symbol) test.
754 "(alias for undefined face)")
755 (apropos-documentation-property
756 symbol
'face-documentation t
))))
757 (when (get symbol
'custom-group
)
758 (apropos-documentation-property
759 symbol
'group-documentation t
)))))
761 (apropos-print keys nil text
)))
765 (defun apropos-value (pattern &optional do-all
)
766 "Show all symbols whose value's printed representation matches PATTERN.
767 PATTERN can be a word, a list of words (separated by spaces),
768 or a regexp (using some regexp special characters). If it is a word,
769 search for matches for that word as a substring. If it is a list of words,
770 search for matches for any two (or more) of those words.
772 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
773 at function definitions (arguments, documentation and body) and at the
774 names and values of properties.
776 Returns list of symbols and values found."
777 (interactive (list (apropos-read-pattern "value")
779 (apropos-parse-pattern pattern
)
780 (or do-all
(setq do-all apropos-do-all
))
781 (setq apropos-accumulator
())
785 (setq f nil v nil p nil
)
786 (or (memq symbol
'(apropos-regexp
787 apropos-pattern apropos-all-words-regexp
788 apropos-words apropos-all-words
789 do-all apropos-accumulator
791 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
793 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
794 p
(apropos-format-plist symbol
"\n " t
)))
795 (if (apropos-false-hit-str v
)
797 (if (apropos-false-hit-str f
)
799 (if (apropos-false-hit-str p
)
802 (setq apropos-accumulator
(cons (list symbol
803 (+ (apropos-score-str f
)
804 (apropos-score-str v
)
805 (apropos-score-str p
))
807 apropos-accumulator
))))))
808 (let ((apropos-multi-type do-all
))
809 (apropos-print nil
"\n----------------\n")))
812 (defun apropos-local-value (pattern &optional buffer
)
813 "Show buffer-local variables whose values match PATTERN.
814 This is like `apropos-value', but only for buffer-local variables.
815 Optional arg BUFFER (default: current buffer) is the buffer to check."
816 (interactive (list (apropos-read-pattern "value of buffer-local variable")))
817 (unless buffer
(setq buffer
(current-buffer)))
818 (apropos-parse-pattern pattern
)
819 (setq apropos-accumulator
())
823 (unless (memq symb
'(apropos-regexp apropos-pattern apropos-all-words-regexp
824 apropos-words apropos-all-words apropos-accumulator symb var
))
825 (setq var
(apropos-value-internal 'local-variable-if-set-p symb
'symbol-value
)))
826 (when (and (fboundp 'apropos-false-hit-str
) (apropos-false-hit-str var
))
829 (setq apropos-accumulator
(cons (list symb
(apropos-score-str var
) nil var
)
830 apropos-accumulator
))))))
831 (let ((apropos-multi-type nil
))
832 (if (> emacs-major-version
20)
834 nil
"\n----------------\n"
835 (format "Buffer `%s' has the following local variables\nmatching %s`%s':"
837 (if (consp pattern
) "keywords " "")
839 (apropos-print nil
"\n----------------\n"))))
842 (defun apropos-documentation (pattern &optional do-all
)
843 "Show symbols whose documentation contains matches for PATTERN.
844 PATTERN can be a word, a list of words (separated by spaces),
845 or a regexp (using some regexp special characters). If it is a word,
846 search for matches for that word as a substring. If it is a list of words,
847 search for matches for any two (or more) of those words.
849 Note that by default this command only searches in the file specified by
850 `internal-doc-file-name'; i.e., the etc/DOC file. With \\[universal-argument] prefix,
851 or if `apropos-do-all' is non-nil, it searches all currently defined
852 documentation strings.
854 Returns list of symbols and documentation found."
855 ;; The doc used to say that DO-ALL includes key-bindings info in the
856 ;; output, but I cannot see that that is true.
857 (interactive (list (apropos-read-pattern "documentation")
859 (apropos-parse-pattern pattern
)
860 (or do-all
(setq do-all apropos-do-all
))
861 (setq apropos-accumulator
() apropos-files-scanned
())
862 (let ((standard-input (get-buffer-create " apropos-temp"))
863 (apropos-sort-by-scores apropos-documentation-sort-by-scores
)
866 (with-current-buffer standard-input
867 (apropos-documentation-check-doc-file)
871 (setq f
(apropos-safe-documentation symbol
)
872 v
(get symbol
'variable-documentation
))
873 (if (integerp v
) (setq v nil
))
874 (setq f
(apropos-documentation-internal f
)
875 v
(apropos-documentation-internal v
))
876 (setq sf
(apropos-score-doc f
)
877 sv
(apropos-score-doc v
))
879 (if (setq apropos-item
880 (cdr (assq symbol apropos-accumulator
)))
884 (setcar (nthcdr 1 apropos-item
) f
)
885 (setcar apropos-item
(+ (car apropos-item
) sf
))))
888 (setcar (nthcdr 2 apropos-item
) v
)
889 (setcar apropos-item
(+ (car apropos-item
) sv
)))))
890 (setq apropos-accumulator
892 (+ (apropos-score-symbol symbol
2) sf sv
)
894 apropos-accumulator
)))))))
895 (apropos-print nil
"\n----------------\n" nil t
))
896 (kill-buffer standard-input
))))
899 (defun apropos-value-internal (predicate symbol function
)
900 (if (funcall predicate symbol
)
902 (setq symbol
(prin1-to-string (funcall function symbol
)))
903 (if (string-match apropos-regexp symbol
)
905 (if apropos-match-face
906 (put-text-property (match-beginning 0) (match-end 0)
907 'face apropos-match-face
911 (defun apropos-documentation-internal (doc)
914 (apropos-documentation-check-elc-file (car doc
)))
916 ;; Sanity check in case bad data sneaked into the
917 ;; documentation slot.
919 (string-match apropos-all-words-regexp doc
)
920 (apropos-true-hit-doc doc
))
921 (when apropos-match-face
922 (setq doc
(substitute-command-keys (copy-sequence doc
)))
923 (if (or (string-match apropos-pattern-quoted doc
)
924 (string-match apropos-all-words-regexp doc
))
925 (put-text-property (match-beginning 0)
927 'face apropos-match-face doc
))
930 (defun apropos-format-plist (pl sep
&optional compare
)
931 (setq pl
(symbol-plist pl
))
934 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
935 (if (or (not compare
) (string-match apropos-regexp p
))
936 (put-text-property 0 (length (symbol-name (car pl
)))
937 'face
'apropos-property p
)
941 (and compare apropos-match-face
942 (put-text-property (match-beginning 0) (match-end 0)
943 'face apropos-match-face
945 (setq p-out
(concat p-out
(if p-out sep
) p
))))
946 (setq pl
(nthcdr 2 pl
)))
950 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
952 (defun apropos-documentation-check-doc-file ()
953 (let (type symbol
(sepa 2) sepb doc
)
956 (insert-file-contents (concat doc-directory internal-doc-file-name
))
958 (while (save-excursion
959 (setq sepb
(search-forward "\^_"))
961 (beginning-of-line 2)
962 (if (save-restriction
963 (narrow-to-region (point) (1- sepb
))
964 (re-search-forward apropos-all-words-regexp nil t
))
966 (goto-char (1+ sepa
))
967 (setq type
(if (eq ?F
(preceding-char))
968 2 ; function documentation
969 3) ; variable documentation
971 doc
(buffer-substring (1+ (point)) (1- sepb
)))
972 (when (and (apropos-true-hit-doc doc
)
973 ;; The DOC file lists all built-in funcs and vars.
974 ;; If any are not currently bound, they can
975 ;; only be platform-specific stuff (eg NS) not
976 ;; in use on the current platform.
977 ;; So we exclude them.
978 (cond ((= 3 type
) (boundp symbol
))
979 ((= 2 type
) (fboundp symbol
))))
980 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
981 (setcar (cdr apropos-item
)
982 (apropos-score-doc doc
)))
983 (setq apropos-item
(list symbol
984 (+ (apropos-score-symbol symbol
2)
985 (apropos-score-doc doc
))
987 apropos-accumulator
(cons apropos-item
988 apropos-accumulator
)))
989 (when apropos-match-face
990 (setq doc
(substitute-command-keys doc
))
991 (if (or (string-match apropos-pattern-quoted doc
)
992 (string-match apropos-all-words-regexp doc
))
993 (put-text-property (match-beginning 0)
995 'face apropos-match-face doc
)))
996 (setcar (nthcdr type apropos-item
) doc
))))
997 (setq sepa
(goto-char sepb
)))))
999 (defun apropos-documentation-check-elc-file (file)
1000 (if (member file apropos-files-scanned
)
1002 (let (symbol doc beg end this-is-a-variable
)
1003 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
1005 (insert-file-contents file
)
1006 (while (search-forward "\n#@" nil t
)
1007 ;; Read the comment length, and advance over it.
1010 end
(+ (point) end -
1))
1012 (if (save-restriction
1013 ;; match ^ and $ relative to doc string
1014 (narrow-to-region beg end
)
1015 (re-search-forward apropos-all-words-regexp nil t
))
1017 (goto-char (+ end
2))
1018 (setq doc
(buffer-substring beg end
)
1019 end
(- (match-end 0) beg
)
1020 beg
(- (match-beginning 0) beg
))
1021 (when (apropos-true-hit-doc doc
)
1022 (setq this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
1024 (skip-chars-forward "(a-z")
1027 symbol
(if (consp symbol
)
1030 (if (if this-is-a-variable
1031 (get symbol
'variable-documentation
)
1032 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
1034 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
1035 (setcar (cdr apropos-item
)
1036 (+ (cadr apropos-item
) (apropos-score-doc doc
))))
1037 (setq apropos-item
(list symbol
1038 (+ (apropos-score-symbol symbol
2)
1039 (apropos-score-doc doc
))
1041 apropos-accumulator
(cons apropos-item
1042 apropos-accumulator
)))
1043 (when apropos-match-face
1044 (setq doc
(substitute-command-keys doc
))
1045 (if (or (string-match apropos-pattern-quoted doc
)
1046 (string-match apropos-all-words-regexp doc
))
1047 (put-text-property (match-beginning 0)
1049 'face apropos-match-face doc
)))
1050 (setcar (nthcdr (if this-is-a-variable
3 2)
1056 (defun apropos-safe-documentation (function)
1057 "Like `documentation', except it avoids calling `get_doc_string'.
1058 Will return nil instead."
1059 (while (and function
(symbolp function
))
1060 (setq function
(symbol-function function
)))
1061 (if (eq (car-safe function
) 'macro
)
1062 (setq function
(cdr function
)))
1063 (setq function
(if (byte-code-function-p function
)
1064 (if (> (length function
) 4)
1066 (if (autoloadp function
)
1068 (if (eq (car-safe function
) 'lambda
)
1069 (if (stringp (nth 2 function
))
1071 (if (stringp (nth 3 function
))
1072 (nth 3 function
)))))))
1073 (if (integerp function
)
1077 (defcustom apropos-compact-layout nil
1078 "If non-nil, use a single line per binding."
1081 (defun apropos-print (do-keys spacing
&optional text nosubst
)
1082 "Output result of apropos searching into buffer `*Apropos*'.
1083 The value of `apropos-accumulator' is the list of items to output.
1084 Each element should have the format
1085 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
1086 The return value is the list that was in `apropos-accumulator', sorted
1087 alphabetically by symbol name; but this function also sets
1088 `apropos-accumulator' to nil before returning.
1089 If DO-KEYS is non-nil, output the key bindings. If NOSUBST is
1090 nil, substitute \"ASCII quotes\" (i.e., grace accent and
1091 apostrophe) with curly quotes), and if non-nil, leave them alone.
1092 If SPACING is non-nil, it should be a string; separate items with
1093 that string. If non-nil, TEXT is a string that will be printed
1095 (if (null apropos-accumulator
)
1096 (message "No apropos matches for `%s'" apropos-pattern
)
1097 (setq apropos-accumulator
1098 (sort apropos-accumulator
1100 (if apropos-sort-by-scores
1101 (or (> (cadr a
) (cadr b
))
1102 (and (= (cadr a
) (cadr b
))
1103 (string-lessp (car a
) (car b
))))
1104 (string-lessp (car a
) (car b
))))))
1105 (with-output-to-temp-buffer "*Apropos*"
1106 (let ((p apropos-accumulator
)
1107 (old-buffer (current-buffer))
1108 (inhibit-read-only t
)
1111 (set-buffer standard-output
)
1113 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
1114 (if apropos-multi-type
"a type label" "an entry")
1115 " to view its full documentation.\n\n")
1116 (if text
(insert text
"\n\n"))
1117 (dolist (apropos-item p
)
1118 (when (and spacing
(not (bobp)))
1120 (setq symbol
(car apropos-item
))
1121 ;; Insert dummy score element for backwards compatibility with 21.x
1122 ;; apropos-item format.
1123 (if (not (numberp (cadr apropos-item
)))
1125 (cons (car apropos-item
)
1126 (cons nil
(cdr apropos-item
)))))
1127 (when (= (point) button-end
) (terpri))
1128 (insert-text-button (symbol-name symbol
)
1129 'type
'apropos-symbol
1130 'skip apropos-multi-type
1131 'face
'apropos-symbol
)
1132 (setq button-end
(point))
1133 (if (and (eq apropos-sort-by-scores
'verbose
)
1134 (cadr apropos-item
))
1135 (insert " (" (number-to-string (cadr apropos-item
)) ") "))
1136 ;; Calculate key-bindings if we want them.
1137 (unless apropos-compact-layout
1140 (not (eq symbol
'self-insert-command
))
1143 (with-current-buffer old-buffer
1144 (where-is-internal symbol
)))
1146 ;; Copy over the list of key sequences,
1147 ;; omitting any that contain a buffer or a frame.
1148 ;; FIXME: Why omit keys that contain buffers and
1149 ;; frames? This looks like a bad workaround rather
1150 ;; than a proper fix. Does anybody know what problem
1151 ;; this is trying to address? --Stef
1155 (while (< i
(length key
))
1156 (if (or (framep (aref key i
))
1157 (bufferp (aref key i
)))
1161 (push key filtered
))))
1162 (setq item filtered
))
1163 ;; Convert the remaining keys to a string and insert.
1167 (setq key
(condition-case ()
1168 (key-description key
)
1170 (put-text-property 0 (length key
)
1171 'face
'apropos-keybinding
1175 (insert "M-x ... RET")
1176 (put-text-property (- (point) 11) (- (point) 8)
1177 'face
'apropos-keybinding
)
1178 (put-text-property (- (point) 3) (point)
1179 'face
'apropos-keybinding
)))
1181 (apropos-print-doc 2
1182 (if (commandp symbol
)
1188 (apropos-print-doc 3
1189 (if (custom-variable-p symbol
)
1190 'apropos-user-option
1193 (apropos-print-doc 7 'apropos-group t
)
1194 (apropos-print-doc 6 'apropos-face t
)
1195 (apropos-print-doc 5 'apropos-widget t
)
1196 (apropos-print-doc 4 'apropos-plist nil
))
1197 (set (make-local-variable 'truncate-partial-width-windows
) t
)
1198 (set (make-local-variable 'truncate-lines
) t
))))
1199 (prog1 apropos-accumulator
1200 (setq apropos-accumulator
()))) ; permit gc
1202 (defun apropos-print-doc (i type do-keys
)
1203 (let ((doc (nth i apropos-item
)))
1205 (if apropos-compact-layout
1206 (insert (propertize "\t" 'display
'(space :align-to
32)) " ")
1208 (if apropos-multi-type
1209 (let ((button-face (button-type-get type
'face
)))
1210 (unless (consp button-face
)
1211 (setq button-face
(list button-face
)))
1213 (if apropos-compact-layout
1214 (format "<%s>" (button-type-get type
'apropos-short-label
))
1215 (button-type-get type
'apropos-label
))
1217 'apropos-symbol
(car apropos-item
))
1218 (insert (if apropos-compact-layout
" " ": ")))
1220 ;; If the query is only for a single type, there's no point
1221 ;; writing it over and over again. Insert a blank button, and
1222 ;; put the 'apropos-label property there (needed by
1223 ;; apropos-symbol-button-display-help).
1225 " " 'type type
'skip t
1226 'face
'default
'apropos-symbol
(car apropos-item
)))
1228 (let ((opoint (point))
1229 (ocol (current-column)))
1230 (cond ((equal doc
"")
1231 (setq doc
"(not documented)"))
1233 (setq doc
(substitute-command-keys doc
))))
1235 (if (equal doc
"(not documented)")
1236 (put-text-property opoint
(point) 'font-lock-face
'shadow
))
1237 ;; The labeling buttons might make the line too long, so fill it if
1239 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column
)
1240 emacs-lisp-docstring-fill-column
1242 (fill-prefix (make-string ocol ?\s
)))
1243 (fill-region opoint
(point) nil t
)))
1244 (or (bolp) (terpri)))))
1246 (defun apropos-follow ()
1247 "Invokes any button at point, otherwise invokes the nearest label button."
1250 (or (apropos-next-label-button (line-beginning-position))
1251 (error "There is nothing to follow here"))))
1254 (defun apropos-describe-plist (symbol)
1255 "Display a pretty listing of SYMBOL's plist."
1256 (help-setup-xref (list 'apropos-describe-plist symbol
)
1257 (called-interactively-p 'interactive
))
1258 (with-help-window (help-buffer)
1259 (set-buffer standard-output
)
1262 (princ (substitute-command-keys "'s plist is\n ("))
1263 (put-text-property (+ (point-min) 7) (- (point) 14)
1264 'face
'apropos-symbol
)
1265 (insert (apropos-format-plist symbol
"\n "))
1271 ;;; apropos.el ends here