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