Remove redundant (require 'timer) in midnight.el
[emacs.git] / lisp / apropos.el
blob734b2fb2f6bb84911f26baf0e532704d3dd6ae81
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)
7 ;; Keywords: help
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
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>.
35 ;; History:
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.
57 ;;; Code:
59 (require 'button)
60 (eval-when-compile (require 'cl))
62 (defgroup apropos nil
63 "Apropos commands for users and programmers."
64 :group 'help
65 :prefix "apropos")
67 ;; I see a degradation of maybe 10-20% only.
68 (defcustom apropos-do-all nil
69 "Whether the apropos commands should do more.
71 Slows them down more or less. Set this non-nil if you have a fast machine."
72 :group 'apropos
73 :type 'boolean)
76 (defcustom apropos-symbol-face 'bold
77 "Face for symbol name in Apropos output, or nil for none."
78 :group 'apropos
79 :type 'face)
81 (defcustom apropos-keybinding-face 'underline
82 "Face for lists of keybinding in Apropos output, or nil for none."
83 :group 'apropos
84 :type 'face)
86 (defcustom apropos-label-face '(italic variable-pitch)
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."
90 :group 'apropos
91 :type 'face)
93 (defcustom apropos-property-face 'bold-italic
94 "Face for property name in apropos output, or nil for none."
95 :group 'apropos
96 :type 'face)
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."
102 :group 'apropos
103 :type 'face)
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."
109 :group 'apropos
110 :type '(choice (const :tag "off" nil)
111 (const :tag "on" t)
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."
118 :group 'apropos
119 :type '(choice (const :tag "off" nil)
120 (const :tag "on" t)
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)
130 map)
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.")
160 (defvar apropos-item ()
161 "Current item in or for `apropos-accumulator'.")
163 (defvar apropos-synonyms '(
164 ("find" "open" "edit")
165 ("kill" "cut")
166 ("yank" "paste")
167 ("region" "selection"))
168 "List of synonyms known by apropos.
169 Each element is a list of words where the first word is the standard Emacs
170 term, and the rest of the words are alternative terms.")
173 ;;; Button types used by apropos
175 (define-button-type 'apropos-symbol
176 'face apropos-symbol-face
177 'help-echo "mouse-2, RET: Display more help on this symbol"
178 'follow-link t
179 'action #'apropos-symbol-button-display-help)
181 (defun apropos-symbol-button-display-help (button)
182 "Display further help for the `apropos-symbol' button BUTTON."
183 (button-activate
184 (or (apropos-next-label-button (button-start button))
185 (error "There is nothing to follow for `%s'" (button-label button)))))
187 (define-button-type 'apropos-function
188 'apropos-label "Function"
189 'apropos-short-label "f"
190 'help-echo "mouse-2, RET: Display more help on this function"
191 'follow-link t
192 'action (lambda (button)
193 (describe-function (button-get button 'apropos-symbol))))
195 (define-button-type 'apropos-macro
196 'apropos-label "Macro"
197 'apropos-short-label "m"
198 'help-echo "mouse-2, RET: Display more help on this macro"
199 'follow-link t
200 'action (lambda (button)
201 (describe-function (button-get button 'apropos-symbol))))
203 (define-button-type 'apropos-command
204 'apropos-label "Command"
205 'apropos-short-label "c"
206 'help-echo "mouse-2, RET: Display more help on this command"
207 'follow-link t
208 'action (lambda (button)
209 (describe-function (button-get button 'apropos-symbol))))
211 ;; We used to use `customize-variable-other-window' instead for a
212 ;; customizable variable, but that is slow. It is better to show an
213 ;; ordinary help buffer and let the user click on the customization
214 ;; button in that buffer, if he wants to.
215 ;; Likewise for `customize-face-other-window'.
216 (define-button-type 'apropos-variable
217 'apropos-label "Variable"
218 'apropos-short-label "v"
219 'help-echo "mouse-2, RET: Display more help on this variable"
220 'follow-link t
221 'action (lambda (button)
222 (describe-variable (button-get button 'apropos-symbol))))
224 (define-button-type 'apropos-face
225 'apropos-label "Face"
226 'apropos-short-label "F"
227 'help-echo "mouse-2, RET: Display more help on this face"
228 'follow-link t
229 'action (lambda (button)
230 (describe-face (button-get button 'apropos-symbol))))
232 (define-button-type 'apropos-group
233 'apropos-label "Group"
234 'apropos-short-label "g"
235 'help-echo "mouse-2, RET: Display more help on this group"
236 'follow-link t
237 'action (lambda (button)
238 (customize-group-other-window
239 (button-get button 'apropos-symbol))))
241 (define-button-type 'apropos-widget
242 'apropos-label "Widget"
243 'apropos-short-label "w"
244 'help-echo "mouse-2, RET: Display more help on this widget"
245 'follow-link t
246 'action (lambda (button)
247 (widget-browse-other-window (button-get button 'apropos-symbol))))
249 (define-button-type 'apropos-plist
250 'apropos-label "Plist"
251 'apropos-short-label "p"
252 'help-echo "mouse-2, RET: Display more help on this plist"
253 'follow-link t
254 'action (lambda (button)
255 (apropos-describe-plist (button-get button 'apropos-symbol))))
257 (define-button-type 'apropos-library
258 'help-echo "mouse-2, RET: Display more help on this library"
259 'follow-link t
260 'action (lambda (button)
261 (apropos-library (button-get button 'apropos-symbol))))
263 (defun apropos-next-label-button (pos)
264 "Return the next apropos label button after POS, or nil if there's none.
265 Will also return nil if more than one `apropos-symbol' button is encountered
266 before finding a label."
267 (let* ((button (next-button pos t))
268 (already-hit-symbol nil)
269 (label (and button (button-get button 'apropos-label)))
270 (type (and button (button-get button 'type))))
271 (while (and button
272 (not label)
273 (or (not (eq type 'apropos-symbol))
274 (not already-hit-symbol)))
275 (when (eq type 'apropos-symbol)
276 (setq already-hit-symbol t))
277 (setq button (next-button (button-start button)))
278 (when button
279 (setq label (button-get button 'apropos-label))
280 (setq type (button-get button 'type))))
281 (and label button)))
284 (defun apropos-words-to-regexp (words wild)
285 "Make regexp matching any two of the words in WORDS."
286 (concat "\\("
287 (mapconcat 'identity words "\\|")
288 "\\)"
289 (if (cdr words)
290 (concat wild
291 "\\("
292 (mapconcat 'identity words "\\|")
293 "\\)")
294 "")))
296 ;;;###autoload
297 (defun apropos-read-pattern (subject)
298 "Read an apropos pattern, either a word list or a regexp.
299 Returns the user pattern, either a list of words which are matched
300 literally, or a string which is used as a regexp to search for.
302 SUBJECT is a string that is included in the prompt to identify what
303 kind of objects to search."
304 (let ((pattern
305 (read-string (concat "Apropos " subject " (word list or regexp): "))))
306 (if (string-equal (regexp-quote pattern) pattern)
307 ;; Split into words
308 (split-string pattern "[ \t]+")
309 pattern)))
311 (defun apropos-parse-pattern (pattern)
312 "Rewrite a list of words to a regexp matching all permutations.
313 If PATTERN is a string, that means it is already a regexp.
314 This updates variables `apropos-pattern', `apropos-pattern-quoted',
315 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
316 (setq apropos-words nil
317 apropos-all-words nil)
318 (if (consp pattern)
319 ;; We don't actually make a regexp matching all permutations.
320 ;; Instead, for e.g. "a b c", we make a regexp matching
321 ;; any combination of two or more words like this:
322 ;; (a|b|c).*(a|b|c) which may give some false matches,
323 ;; but as long as it also gives the right ones, that's ok.
324 (let ((words pattern))
325 (setq apropos-pattern (mapconcat 'identity pattern " ")
326 apropos-pattern-quoted (regexp-quote apropos-pattern))
327 (dolist (word words)
328 (let ((syn apropos-synonyms) (s word) (a word))
329 (while syn
330 (if (member word (car syn))
331 (progn
332 (setq a (mapconcat 'identity (car syn) "\\|"))
333 (if (member word (cdr (car syn)))
334 (setq s a))
335 (setq syn nil))
336 (setq syn (cdr syn))))
337 (setq apropos-words (cons s apropos-words)
338 apropos-all-words (cons a apropos-all-words))))
339 (setq apropos-all-words-regexp
340 (apropos-words-to-regexp apropos-all-words ".+"))
341 (setq apropos-regexp
342 (apropos-words-to-regexp apropos-words ".*?")))
343 (setq apropos-pattern-quoted (regexp-quote pattern)
344 apropos-all-words-regexp pattern
345 apropos-pattern pattern
346 apropos-regexp pattern)))
349 (defun apropos-calc-scores (str words)
350 "Return apropos scores for string STR matching WORDS.
351 Value is a list of offsets of the words into the string."
352 (let (scores i)
353 (if words
354 (dolist (word words scores)
355 (if (setq i (string-match word str))
356 (setq scores (cons i scores))))
357 ;; Return list of start and end position of regexp
358 (and (string-match apropos-pattern str)
359 (list (match-beginning 0) (match-end 0))))))
361 (defun apropos-score-str (str)
362 "Return apropos score for string STR."
363 (if str
364 (let* ((l (length str))
365 (score (- (/ l 10))))
366 (dolist (s (apropos-calc-scores str apropos-all-words) score)
367 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
370 (defun apropos-score-doc (doc)
371 "Return apropos score for documentation string DOC."
372 (let ((l (length doc)))
373 (if (> l 0)
374 (let ((score 0) i)
375 (when (setq i (string-match apropos-pattern-quoted doc))
376 (setq score 10000))
377 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
378 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
379 0)))
381 (defun apropos-score-symbol (symbol &optional weight)
382 "Return apropos score for SYMBOL."
383 (setq symbol (symbol-name symbol))
384 (let ((score 0)
385 (l (length symbol)))
386 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
387 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
389 (defun apropos-true-hit (str words)
390 "Return t if STR is a genuine hit.
391 This may fail if only one of the keywords is matched more than once.
392 This requires that at least 2 keywords (unless only one was given)."
393 (or (not str)
394 (not words)
395 (not (cdr words))
396 (> (length (apropos-calc-scores str words)) 1)))
398 (defun apropos-false-hit-symbol (symbol)
399 "Return t if SYMBOL is not really matched by the current keywords."
400 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
402 (defun apropos-false-hit-str (str)
403 "Return t if STR is not really matched by the current keywords."
404 (not (apropos-true-hit str apropos-words)))
406 (defun apropos-true-hit-doc (doc)
407 "Return t if DOC is really matched by the current keywords."
408 (apropos-true-hit doc apropos-all-words))
410 (define-derived-mode apropos-mode special-mode "Apropos"
411 "Major mode for following hyperlinks in output of apropos commands.
413 \\{apropos-mode-map}")
415 (defvar apropos-multi-type t
416 "If non-nil, this apropos query concerns multiple types.
417 This is used to decide whether to print the result's type or not.")
419 ;;;###autoload
420 (defun apropos-variable (pattern &optional do-all)
421 "Show user variables that match PATTERN.
422 PATTERN can be a word, a list of words (separated by spaces),
423 or a regexp (using some regexp special characters). If it is a word,
424 search for matches for that word as a substring. If it is a list of words,
425 search for matches for any two (or more) of those words.
427 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
428 normal variables."
429 (interactive (list (apropos-read-pattern
430 (if (or current-prefix-arg apropos-do-all)
431 "variable" "user option"))
432 current-prefix-arg))
433 (apropos-command pattern nil
434 (if (or do-all apropos-do-all)
435 #'(lambda (symbol)
436 (and (boundp symbol)
437 (get symbol 'variable-documentation)))
438 'user-variable-p)))
440 ;; For auld lang syne:
441 ;;;###autoload
442 (defalias 'command-apropos 'apropos-command)
443 ;;;###autoload
444 (defun apropos-command (pattern &optional do-all var-predicate)
445 "Show commands (interactively callable functions) that match PATTERN.
446 PATTERN can be a word, a list of words (separated by spaces),
447 or a regexp (using some regexp special characters). If it is a word,
448 search for matches for that word as a substring. If it is a list of words,
449 search for matches for any two (or more) of those words.
451 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
452 noninteractive functions.
454 If VAR-PREDICATE is non-nil, show only variables, and only those that
455 satisfy the predicate VAR-PREDICATE.
457 When called from a Lisp program, a string PATTERN is used as a regexp,
458 while a list of strings is used as a word list."
459 (interactive (list (apropos-read-pattern
460 (if (or current-prefix-arg apropos-do-all)
461 "command or function" "command"))
462 current-prefix-arg))
463 (apropos-parse-pattern pattern)
464 (let ((message
465 (let ((standard-output (get-buffer-create "*Apropos*")))
466 (help-print-return-message 'identity))))
467 (or do-all (setq do-all apropos-do-all))
468 (setq apropos-accumulator
469 (apropos-internal apropos-regexp
470 (or var-predicate
471 ;; We used to use `functionp' here, but this
472 ;; rules out macros. `fboundp' rules in
473 ;; keymaps, but it seems harmless.
474 (if do-all 'fboundp 'commandp))))
475 (let ((tem apropos-accumulator))
476 (while tem
477 (if (or (get (car tem) 'apropos-inhibit)
478 (apropos-false-hit-symbol (car tem)))
479 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
480 (setq tem (cdr tem))))
481 (let ((p apropos-accumulator)
482 doc symbol score)
483 (while p
484 (setcar p (list
485 (setq symbol (car p))
486 (setq score (apropos-score-symbol symbol))
487 (unless var-predicate
488 (if (fboundp symbol)
489 (if (setq doc (condition-case nil
490 (documentation symbol t)
491 (error 'error)))
492 ;; Eg alias to undefined function.
493 (if (eq doc 'error)
494 "(documentation error)"
495 (setq score (+ score (apropos-score-doc doc)))
496 (substring doc 0 (string-match "\n" doc)))
497 "(not documented)")))
498 (and var-predicate
499 (funcall var-predicate symbol)
500 (if (setq doc (documentation-property
501 symbol 'variable-documentation t))
502 (progn
503 (setq score (+ score (apropos-score-doc doc)))
504 (substring doc 0
505 (string-match "\n" doc)))))))
506 (setcar (cdr (car p)) score)
507 (setq p (cdr p))))
508 (and (let ((apropos-multi-type do-all))
509 (apropos-print t nil nil t))
510 message
511 (message "%s" message))))
514 ;;;###autoload
515 (defun apropos-documentation-property (symbol property raw)
516 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
517 (condition-case ()
518 (let ((doc (documentation-property symbol property raw)))
519 (if doc (substring doc 0 (string-match "\n" doc))
520 "(not documented)"))
521 (error "(error retrieving documentation)")))
524 ;;;###autoload
525 (defun apropos (pattern &optional do-all)
526 "Show all meaningful Lisp symbols whose names match PATTERN.
527 Symbols are shown if they are defined as functions, variables, or
528 faces, or if they have nonempty property lists.
530 PATTERN can be a word, a list of words (separated by spaces),
531 or a regexp (using some regexp special characters). If it is a word,
532 search for matches for that word as a substring. If it is a list of words,
533 search for matches for any two (or more) of those words.
535 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
536 consider all symbols (if they match PATTERN).
538 Returns list of symbols and documentation found."
539 (interactive (list (apropos-read-pattern "symbol")
540 current-prefix-arg))
541 (apropos-parse-pattern pattern)
542 (apropos-symbols-internal
543 (apropos-internal apropos-regexp
544 (and (not do-all)
545 (not apropos-do-all)
546 (lambda (symbol)
547 (or (fboundp symbol)
548 (boundp symbol)
549 (facep symbol)
550 (symbol-plist symbol)))))
551 (or do-all apropos-do-all)))
553 (defun apropos-library-button (sym)
554 (if (null sym)
555 "<nothing>"
556 (let ((name (copy-sequence (symbol-name sym))))
557 (make-text-button name nil
558 'type 'apropos-library
559 'face apropos-symbol-face
560 'apropos-symbol name)
561 name)))
563 ;;;###autoload
564 (defun apropos-library (file)
565 "List the variables and functions defined by library FILE.
566 FILE should be one of the libraries currently loaded and should
567 thus be found in `load-history'."
568 (interactive
569 (let* ((libs (delq nil (mapcar 'car load-history)))
570 (libs
571 (nconc (delq nil
572 (mapcar
573 (lambda (l)
574 (setq l (file-name-nondirectory l))
575 (while
576 (not (equal (setq l (file-name-sans-extension l))
577 l)))
579 libs))
580 libs)))
581 (list (completing-read "Describe library: " libs nil t))))
582 (let ((symbols nil)
583 ;; (autoloads nil)
584 (provides nil)
585 (requires nil)
586 (lh-entry (assoc file load-history)))
587 (unless lh-entry
588 ;; `file' may be the "shortname".
589 (let ((lh load-history)
590 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file)
591 "\\(\\.\\|\\'\\)")))
592 (while (and lh (null lh-entry))
593 (if (and (caar lh) (string-match re (caar lh)))
594 (setq lh-entry (car lh))
595 (setq lh (cdr lh)))))
596 (unless lh-entry (error "Unknown library `%s'" file)))
597 (dolist (x (cdr lh-entry))
598 (case (car-safe x)
599 ;; (autoload (push (cdr x) autoloads))
600 (require (push (cdr x) requires))
601 (provide (push (cdr x) provides))
602 (t (push (or (cdr-safe x) x) symbols))))
603 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
604 (apropos-symbols-internal
605 symbols apropos-do-all
606 (concat
607 (format "Library `%s' provides: %s\nand requires: %s"
608 file
609 (mapconcat 'apropos-library-button
610 (or provides '(nil)) " and ")
611 (mapconcat 'apropos-library-button
612 (or requires '(nil)) " and ")))))))
614 (defun apropos-symbols-internal (symbols keys &optional text)
615 ;; Filter out entries that are marked as apropos-inhibit.
616 (let ((all nil))
617 (dolist (symbol symbols)
618 (unless (get symbol 'apropos-inhibit)
619 (push symbol all)))
620 (setq symbols all))
621 (let ((apropos-accumulator
622 (mapcar
623 (lambda (symbol)
624 (let (doc properties)
625 (list
626 symbol
627 (apropos-score-symbol symbol)
628 (when (fboundp symbol)
629 (if (setq doc (condition-case nil
630 (documentation symbol t)
631 (void-function
632 "(alias for undefined function)")
633 (error
634 "(can't retrieve function documentation)")))
635 (substring doc 0 (string-match "\n" doc))
636 "(not documented)"))
637 (when (boundp symbol)
638 (apropos-documentation-property
639 symbol 'variable-documentation t))
640 (when (setq properties (symbol-plist symbol))
641 (setq doc (list (car properties)))
642 (while (setq properties (cdr (cdr properties)))
643 (setq doc (cons (car properties) doc)))
644 (mapconcat #'symbol-name (nreverse doc) " "))
645 (when (get symbol 'widget-type)
646 (apropos-documentation-property
647 symbol 'widget-documentation t))
648 (when (facep symbol)
649 (let ((alias (get symbol 'face-alias)))
650 (if alias
651 (if (facep alias)
652 (format "%slias for the face `%s'."
653 (if (get symbol 'obsolete-face)
654 "Obsolete a"
655 "A")
656 alias)
657 ;; Never happens in practice because fails
658 ;; (facep symbol) test.
659 "(alias for undefined face)")
660 (apropos-documentation-property
661 symbol 'face-documentation t))))
662 (when (get symbol 'custom-group)
663 (apropos-documentation-property
664 symbol 'group-documentation t)))))
665 symbols)))
666 (apropos-print keys nil text)))
669 ;;;###autoload
670 (defun apropos-value (pattern &optional do-all)
671 "Show all symbols whose value's printed representation matches PATTERN.
672 PATTERN can be a word, a list of words (separated by spaces),
673 or a regexp (using some regexp special characters). If it is a word,
674 search for matches for that word as a substring. If it is a list of words,
675 search for matches for any two (or more) of those words.
677 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
678 at the function and at the names and values of properties.
679 Returns list of symbols and values found."
680 (interactive (list (apropos-read-pattern "value")
681 current-prefix-arg))
682 (apropos-parse-pattern pattern)
683 (or do-all (setq do-all apropos-do-all))
684 (setq apropos-accumulator ())
685 (let (f v p)
686 (mapatoms
687 (lambda (symbol)
688 (setq f nil v nil p nil)
689 (or (memq symbol '(apropos-regexp
690 apropos-pattern apropos-all-words-regexp
691 apropos-words apropos-all-words
692 do-all apropos-accumulator
693 symbol f v p))
694 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
695 (if do-all
696 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
697 p (apropos-format-plist symbol "\n " t)))
698 (if (apropos-false-hit-str v)
699 (setq v nil))
700 (if (apropos-false-hit-str f)
701 (setq f nil))
702 (if (apropos-false-hit-str p)
703 (setq p nil))
704 (if (or f v p)
705 (setq apropos-accumulator (cons (list symbol
706 (+ (apropos-score-str f)
707 (apropos-score-str v)
708 (apropos-score-str p))
709 f v p)
710 apropos-accumulator))))))
711 (let ((apropos-multi-type do-all))
712 (apropos-print nil "\n----------------\n")))
715 ;;;###autoload
716 (defun apropos-documentation (pattern &optional do-all)
717 "Show symbols whose documentation contains matches for PATTERN.
718 PATTERN can be a word, a list of words (separated by spaces),
719 or a regexp (using some regexp special characters). If it is a word,
720 search for matches for that word as a substring. If it is a list of words,
721 search for matches for any two (or more) of those words.
723 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
724 documentation that is not stored in the documentation file and show key
725 bindings.
726 Returns list of symbols and documentation found."
727 (interactive (list (apropos-read-pattern "documentation")
728 current-prefix-arg))
729 (apropos-parse-pattern pattern)
730 (or do-all (setq do-all apropos-do-all))
731 (setq apropos-accumulator () apropos-files-scanned ())
732 (let ((standard-input (get-buffer-create " apropos-temp"))
733 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
734 f v sf sv)
735 (unwind-protect
736 (with-current-buffer standard-input
737 (apropos-documentation-check-doc-file)
738 (if do-all
739 (mapatoms
740 (lambda (symbol)
741 (setq f (apropos-safe-documentation symbol)
742 v (get symbol 'variable-documentation))
743 (if (integerp v) (setq v))
744 (setq f (apropos-documentation-internal f)
745 v (apropos-documentation-internal v))
746 (setq sf (apropos-score-doc f)
747 sv (apropos-score-doc v))
748 (if (or f v)
749 (if (setq apropos-item
750 (cdr (assq symbol apropos-accumulator)))
751 (progn
752 (if f
753 (progn
754 (setcar (nthcdr 1 apropos-item) f)
755 (setcar apropos-item (+ (car apropos-item) sf))))
756 (if v
757 (progn
758 (setcar (nthcdr 2 apropos-item) v)
759 (setcar apropos-item (+ (car apropos-item) sv)))))
760 (setq apropos-accumulator
761 (cons (list symbol
762 (+ (apropos-score-symbol symbol 2) sf sv)
763 f v)
764 apropos-accumulator)))))))
765 (apropos-print nil "\n----------------\n" nil t))
766 (kill-buffer standard-input))))
769 (defun apropos-value-internal (predicate symbol function)
770 (if (funcall predicate symbol)
771 (progn
772 (setq symbol (prin1-to-string (funcall function symbol)))
773 (if (string-match apropos-regexp symbol)
774 (progn
775 (if apropos-match-face
776 (put-text-property (match-beginning 0) (match-end 0)
777 'face apropos-match-face
778 symbol))
779 symbol)))))
781 (defun apropos-documentation-internal (doc)
782 (if (consp doc)
783 (apropos-documentation-check-elc-file (car doc))
784 (if (and doc
785 (string-match apropos-all-words-regexp doc)
786 (apropos-true-hit-doc doc))
787 (when apropos-match-face
788 (setq doc (substitute-command-keys (copy-sequence doc)))
789 (if (or (string-match apropos-pattern-quoted doc)
790 (string-match apropos-all-words-regexp doc))
791 (put-text-property (match-beginning 0)
792 (match-end 0)
793 'face apropos-match-face doc))
794 doc))))
796 (defun apropos-format-plist (pl sep &optional compare)
797 (setq pl (symbol-plist pl))
798 (let (p p-out)
799 (while pl
800 (setq p (format "%s %S" (car pl) (nth 1 pl)))
801 (if (or (not compare) (string-match apropos-regexp p))
802 (if apropos-property-face
803 (put-text-property 0 (length (symbol-name (car pl)))
804 'face apropos-property-face p))
805 (setq p nil))
806 (if p
807 (progn
808 (and compare apropos-match-face
809 (put-text-property (match-beginning 0) (match-end 0)
810 'face apropos-match-face
812 (setq p-out (concat p-out (if p-out sep) p))))
813 (setq pl (nthcdr 2 pl)))
814 p-out))
817 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
819 (defun apropos-documentation-check-doc-file ()
820 (let (type symbol (sepa 2) sepb doc)
821 (insert ?\^_)
822 (backward-char)
823 (insert-file-contents (concat doc-directory internal-doc-file-name))
824 (forward-char)
825 (while (save-excursion
826 (setq sepb (search-forward "\^_"))
827 (not (eobp)))
828 (beginning-of-line 2)
829 (if (save-restriction
830 (narrow-to-region (point) (1- sepb))
831 (re-search-forward apropos-all-words-regexp nil t))
832 (progn
833 (goto-char (1+ sepa))
834 (setq type (if (eq ?F (preceding-char))
835 2 ; function documentation
836 3) ; variable documentation
837 symbol (read)
838 doc (buffer-substring (1+ (point)) (1- sepb)))
839 (when (and (apropos-true-hit-doc doc)
840 ;; The DOC file lists all built-in funcs and vars.
841 ;; If any are not currently bound, they can
842 ;; only be platform-specific stuff (eg NS) not
843 ;; in use on the current platform.
844 ;; So we exclude them.
845 (cond ((= 3 type) (boundp symbol))
846 ((= 2 type) (fboundp symbol))))
847 (or (and (setq apropos-item (assq symbol apropos-accumulator))
848 (setcar (cdr apropos-item)
849 (apropos-score-doc doc)))
850 (setq apropos-item (list symbol
851 (+ (apropos-score-symbol symbol 2)
852 (apropos-score-doc doc))
853 nil nil)
854 apropos-accumulator (cons apropos-item
855 apropos-accumulator)))
856 (when apropos-match-face
857 (setq doc (substitute-command-keys doc))
858 (if (or (string-match apropos-pattern-quoted doc)
859 (string-match apropos-all-words-regexp doc))
860 (put-text-property (match-beginning 0)
861 (match-end 0)
862 'face apropos-match-face doc)))
863 (setcar (nthcdr type apropos-item) doc))))
864 (setq sepa (goto-char sepb)))))
866 (defun apropos-documentation-check-elc-file (file)
867 (if (member file apropos-files-scanned)
869 (let (symbol doc beg end this-is-a-variable)
870 (setq apropos-files-scanned (cons file apropos-files-scanned))
871 (erase-buffer)
872 (insert-file-contents file)
873 (while (search-forward "\n#@" nil t)
874 ;; Read the comment length, and advance over it.
875 (setq end (read)
876 beg (1+ (point))
877 end (+ (point) end -1))
878 (forward-char)
879 (if (save-restriction
880 ;; match ^ and $ relative to doc string
881 (narrow-to-region beg end)
882 (re-search-forward apropos-all-words-regexp nil t))
883 (progn
884 (goto-char (+ end 2))
885 (setq doc (buffer-substring beg end)
886 end (- (match-end 0) beg)
887 beg (- (match-beginning 0) beg))
888 (when (apropos-true-hit-doc doc)
889 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
890 symbol (progn
891 (skip-chars-forward "(a-z")
892 (forward-char)
893 (read))
894 symbol (if (consp symbol)
895 (nth 1 symbol)
896 symbol))
897 (if (if this-is-a-variable
898 (get symbol 'variable-documentation)
899 (and (fboundp symbol) (apropos-safe-documentation symbol)))
900 (progn
901 (or (and (setq apropos-item (assq symbol apropos-accumulator))
902 (setcar (cdr apropos-item)
903 (+ (cadr apropos-item) (apropos-score-doc doc))))
904 (setq apropos-item (list symbol
905 (+ (apropos-score-symbol symbol 2)
906 (apropos-score-doc doc))
907 nil nil)
908 apropos-accumulator (cons apropos-item
909 apropos-accumulator)))
910 (when apropos-match-face
911 (setq doc (substitute-command-keys doc))
912 (if (or (string-match apropos-pattern-quoted doc)
913 (string-match apropos-all-words-regexp doc))
914 (put-text-property (match-beginning 0)
915 (match-end 0)
916 'face apropos-match-face doc)))
917 (setcar (nthcdr (if this-is-a-variable 3 2)
918 apropos-item)
919 doc))))))))))
923 (defun apropos-safe-documentation (function)
924 "Like `documentation', except it avoids calling `get_doc_string'.
925 Will return nil instead."
926 (while (and function (symbolp function))
927 (setq function (if (fboundp function)
928 (symbol-function function))))
929 (if (eq (car-safe function) 'macro)
930 (setq function (cdr function)))
931 (setq function (if (byte-code-function-p function)
932 (if (> (length function) 4)
933 (aref function 4))
934 (if (eq (car-safe function) 'autoload)
935 (nth 2 function)
936 (if (eq (car-safe function) 'lambda)
937 (if (stringp (nth 2 function))
938 (nth 2 function)
939 (if (stringp (nth 3 function))
940 (nth 3 function)))))))
941 (if (integerp function)
943 function))
945 (defcustom apropos-compact-layout nil
946 "If non-nil, use a single line per binding."
947 :type 'boolean)
949 (defun apropos-print (do-keys spacing &optional text nosubst)
950 "Output result of apropos searching into buffer `*Apropos*'.
951 The value of `apropos-accumulator' is the list of items to output.
952 Each element should have the format
953 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
954 The return value is the list that was in `apropos-accumulator', sorted
955 alphabetically by symbol name; but this function also sets
956 `apropos-accumulator' to nil before returning.
958 If SPACING is non-nil, it should be a string; separate items with that string.
959 If non-nil TEXT is a string that will be printed as a heading."
960 (if (null apropos-accumulator)
961 (message "No apropos matches for `%s'" apropos-pattern)
962 (setq apropos-accumulator
963 (sort apropos-accumulator
964 (lambda (a b)
965 ;; Don't sort by score if user can't see the score.
966 ;; It would be confusing. -- rms.
967 (if apropos-sort-by-scores
968 (or (> (cadr a) (cadr b))
969 (and (= (cadr a) (cadr b))
970 (string-lessp (car a) (car b))))
971 (string-lessp (car a) (car b))))))
972 (with-output-to-temp-buffer "*Apropos*"
973 (let ((p apropos-accumulator)
974 (old-buffer (current-buffer))
975 (inhibit-read-only t)
976 symbol item)
977 (set-buffer standard-output)
978 (apropos-mode)
979 (if (display-mouse-p)
980 (insert
981 "If moving the mouse over text changes the text's color, "
982 "you can click\n"
983 "or press return on that text to get more information.\n"))
984 (insert "In this buffer, go to the name of the command, or function,"
985 " or variable,\n"
986 (substitute-command-keys
987 "and type \\[apropos-follow] to get full documentation.\n\n"))
988 (if text (insert text "\n\n"))
989 (dolist (apropos-item p)
990 (when (and spacing (not (bobp)))
991 (princ spacing))
992 (setq symbol (car apropos-item))
993 ;; Insert dummy score element for backwards compatibility with 21.x
994 ;; apropos-item format.
995 (if (not (numberp (cadr apropos-item)))
996 (setq apropos-item
997 (cons (car apropos-item)
998 (cons nil (cdr apropos-item)))))
999 (insert-text-button (symbol-name symbol)
1000 'type 'apropos-symbol
1001 'skip apropos-multi-type
1002 ;; Can't use default, since user may have
1003 ;; changed the variable!
1004 ;; Just say `no' to variables containing faces!
1005 'face apropos-symbol-face)
1006 (if (and (eq apropos-sort-by-scores 'verbose)
1007 (cadr apropos-item))
1008 (insert " (" (number-to-string (cadr apropos-item)) ") "))
1009 ;; Calculate key-bindings if we want them.
1010 (unless apropos-compact-layout
1011 (and do-keys
1012 (commandp symbol)
1013 (not (eq symbol 'self-insert-command))
1014 (indent-to 30 1)
1015 (if (let ((keys
1016 (with-current-buffer old-buffer
1017 (where-is-internal symbol)))
1018 filtered)
1019 ;; Copy over the list of key sequences,
1020 ;; omitting any that contain a buffer or a frame.
1021 ;; FIXME: Why omit keys that contain buffers and
1022 ;; frames? This looks like a bad workaround rather
1023 ;; than a proper fix. Does anybod know what problem
1024 ;; this is trying to address? --Stef
1025 (dolist (key keys)
1026 (let ((i 0)
1027 loser)
1028 (while (< i (length key))
1029 (if (or (framep (aref key i))
1030 (bufferp (aref key i)))
1031 (setq loser t))
1032 (setq i (1+ i)))
1033 (or loser
1034 (push key filtered))))
1035 (setq item filtered))
1036 ;; Convert the remaining keys to a string and insert.
1037 (insert
1038 (mapconcat
1039 (lambda (key)
1040 (setq key (condition-case ()
1041 (key-description key)
1042 (error)))
1043 (if apropos-keybinding-face
1044 (put-text-property 0 (length key)
1045 'face apropos-keybinding-face
1046 key))
1047 key)
1048 item ", "))
1049 (insert "M-x ... RET")
1050 (when apropos-keybinding-face
1051 (put-text-property (- (point) 11) (- (point) 8)
1052 'face apropos-keybinding-face)
1053 (put-text-property (- (point) 3) (point)
1054 'face apropos-keybinding-face))))
1055 (terpri))
1056 (apropos-print-doc 2
1057 (if (commandp symbol)
1058 'apropos-command
1059 (if (apropos-macrop symbol)
1060 'apropos-macro
1061 'apropos-function))
1062 (not nosubst))
1063 (apropos-print-doc 3 'apropos-variable (not nosubst))
1064 (apropos-print-doc 7 'apropos-group t)
1065 (apropos-print-doc 6 'apropos-face t)
1066 (apropos-print-doc 5 'apropos-widget t)
1067 (apropos-print-doc 4 'apropos-plist nil))
1068 (set (make-local-variable 'truncate-partial-width-windows) t)
1069 (set (make-local-variable 'truncate-lines) t))))
1070 (prog1 apropos-accumulator
1071 (setq apropos-accumulator ()))) ; permit gc
1073 (defun apropos-macrop (symbol)
1074 "Return t if SYMBOL is a Lisp macro."
1075 (and (fboundp symbol)
1076 (consp (setq symbol
1077 (symbol-function symbol)))
1078 (or (eq (car symbol) 'macro)
1079 (if (eq (car symbol) 'autoload)
1080 (memq (nth 4 symbol)
1081 '(macro t))))))
1084 (defun apropos-print-doc (i type do-keys)
1085 (when (stringp (setq i (nth i apropos-item)))
1086 (if apropos-compact-layout
1087 (insert (propertize "\t" 'display '(space :align-to 32)) " ")
1088 (insert " "))
1089 (if (null apropos-multi-type)
1090 ;; If the query is only for a single type, there's no point
1091 ;; writing it over and over again. Insert a blank button, and
1092 ;; put the 'apropos-label property there (needed by
1093 ;; apropos-symbol-button-display-help).
1094 (insert-text-button
1095 " " 'type type 'skip t
1096 'face 'default 'apropos-symbol (car apropos-item))
1097 (insert-text-button
1098 (if apropos-compact-layout
1099 (format "<%s>" (button-type-get type 'apropos-short-label))
1100 (button-type-get type 'apropos-label))
1101 'type type
1102 ;; Can't use the default button face, since user may have changed the
1103 ;; variable! Just say `no' to variables containing faces!
1104 'face apropos-label-face
1105 'apropos-symbol (car apropos-item))
1106 (insert (if apropos-compact-layout " " ": ")))
1107 (insert (if do-keys (substitute-command-keys i) i))
1108 (or (bolp) (terpri))))
1110 (defun apropos-follow ()
1111 "Invokes any button at point, otherwise invokes the nearest label button."
1112 (interactive)
1113 (button-activate
1114 (or (apropos-next-label-button (line-beginning-position))
1115 (error "There is nothing to follow here"))))
1118 (defun apropos-describe-plist (symbol)
1119 "Display a pretty listing of SYMBOL's plist."
1120 (help-setup-xref (list 'apropos-describe-plist symbol)
1121 (called-interactively-p 'interactive))
1122 (with-help-window (help-buffer)
1123 (set-buffer standard-output)
1124 (princ "Symbol ")
1125 (prin1 symbol)
1126 (princ "'s plist is\n (")
1127 (if apropos-symbol-face
1128 (put-text-property (+ (point-min) 7) (- (point) 14)
1129 'face apropos-symbol-face))
1130 (insert (apropos-format-plist symbol "\n "))
1131 (princ ")")))
1134 (provide 'apropos)
1136 ;;; apropos.el ends here