Spelling fixes.
[emacs.git] / lisp / apropos.el
blob9a372f27991afe5221c2c254fde76e1825b199cc
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 "Non nil means apropos commands will search more extensively.
70 This may be slower. This option affects the following commands:
72 `apropos-variable' will search all variables, not just user variables.
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."
85 :group 'apropos
86 :type 'boolean)
89 (defcustom apropos-symbol-face 'bold
90 "Face for symbol name in Apropos output, or nil for none."
91 :group 'apropos
92 :type 'face)
94 (defcustom apropos-keybinding-face 'underline
95 "Face for lists of keybinding in Apropos output, or nil for none."
96 :group 'apropos
97 :type 'face)
99 (defcustom apropos-label-face '(italic)
100 "Face for label (`Command', `Variable' ...) in Apropos output.
101 A value of nil means don't use any special font for them, and also
102 turns off mouse highlighting."
103 :group 'apropos
104 :type 'face)
106 (defcustom apropos-property-face 'bold-italic
107 "Face for property name in apropos output, or nil for none."
108 :group 'apropos
109 :type 'face)
111 (defcustom apropos-match-face 'match
112 "Face for matching text in Apropos documentation/value, or nil for none.
113 This applies when you look for matches in the documentation or variable value
114 for the pattern; the part that matches gets displayed in this font."
115 :group 'apropos
116 :type 'face)
118 (defcustom apropos-sort-by-scores nil
119 "Non-nil means sort matches by scores; best match is shown first.
120 This applies to all `apropos' commands except `apropos-documentation'.
121 If value is `verbose', the computed score is shown for each match."
122 :group 'apropos
123 :type '(choice (const :tag "off" nil)
124 (const :tag "on" t)
125 (const :tag "show scores" verbose)))
127 (defcustom apropos-documentation-sort-by-scores t
128 "Non-nil means sort matches by scores; best match is shown first.
129 This applies to `apropos-documentation' only.
130 If value is `verbose', the computed score is shown for each match."
131 :group 'apropos
132 :type '(choice (const :tag "off" nil)
133 (const :tag "on" t)
134 (const :tag "show scores" verbose)))
136 (defvar apropos-mode-map
137 (let ((map (copy-keymap button-buffer-map)))
138 (set-keymap-parent map special-mode-map)
139 ;; Use `apropos-follow' instead of just using the button
140 ;; definition of RET, so that users can use it anywhere in an
141 ;; apropos item, not just on top of a button.
142 (define-key map "\C-m" 'apropos-follow)
143 map)
144 "Keymap used in Apropos mode.")
146 (defvar apropos-mode-hook nil
147 "Hook run when mode is turned on.")
149 (defvar apropos-pattern nil
150 "Apropos pattern as entered by user.")
152 (defvar apropos-pattern-quoted nil
153 "Apropos pattern passed through `regexp-quote'.")
155 (defvar apropos-words ()
156 "Current list of apropos words extracted from `apropos-pattern'.")
158 (defvar apropos-all-words ()
159 "Current list of words and synonyms.")
161 (defvar apropos-regexp nil
162 "Regexp used in current apropos run.")
164 (defvar apropos-all-words-regexp nil
165 "Regexp matching apropos-all-words.")
167 (defvar apropos-files-scanned ()
168 "List of elc files already scanned in current run of `apropos-documentation'.")
170 (defvar apropos-accumulator ()
171 "Alist of symbols already found in current apropos run.
172 Each element has the form
174 (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
176 where SYMBOL is the symbol name, SCORE is its relevance score (a
177 number), FUN-DOC is the function docstring, VAR-DOC is the
178 variable docstring, PLIST is the list of the symbols names in the
179 property list, WIDGET-DOC is the widget docstring, FACE-DOC is
180 the face docstring, and CUS-GROUP-DOC is the custom group
181 docstring. Each docstring is either nil or a string.")
183 (defvar apropos-item ()
184 "Current item in or for `apropos-accumulator'.")
186 (defvar apropos-synonyms '(
187 ("find" "open" "edit")
188 ("kill" "cut")
189 ("yank" "paste")
190 ("region" "selection"))
191 "List of synonyms known by apropos.
192 Each element is a list of words where the first word is the standard Emacs
193 term, and the rest of the words are alternative terms.")
196 ;;; Button types used by apropos
198 (define-button-type 'apropos-symbol
199 'face apropos-symbol-face
200 'help-echo "mouse-2, RET: Display more help on this symbol"
201 'follow-link t
202 'action #'apropos-symbol-button-display-help)
204 (defun apropos-symbol-button-display-help (button)
205 "Display further help for the `apropos-symbol' button BUTTON."
206 (button-activate
207 (or (apropos-next-label-button (button-start button))
208 (error "There is nothing to follow for `%s'" (button-label button)))))
210 (define-button-type 'apropos-function
211 'apropos-label "Function"
212 'apropos-short-label "f"
213 'face '(font-lock-function-name-face button)
214 'help-echo "mouse-2, RET: Display more help on this function"
215 'follow-link t
216 'action (lambda (button)
217 (describe-function (button-get button 'apropos-symbol))))
219 (define-button-type 'apropos-macro
220 'apropos-label "Macro"
221 'apropos-short-label "m"
222 'face '(font-lock-function-name-face button)
223 'help-echo "mouse-2, RET: Display more help on this macro"
224 'follow-link t
225 'action (lambda (button)
226 (describe-function (button-get button 'apropos-symbol))))
228 (define-button-type 'apropos-command
229 'apropos-label "Command"
230 'apropos-short-label "c"
231 'face '(font-lock-function-name-face button)
232 'help-echo "mouse-2, RET: Display more help on this command"
233 'follow-link t
234 'action (lambda (button)
235 (describe-function (button-get button 'apropos-symbol))))
237 ;; We used to use `customize-variable-other-window' instead for a
238 ;; customizable variable, but that is slow. It is better to show an
239 ;; ordinary help buffer and let the user click on the customization
240 ;; button in that buffer, if he wants to.
241 ;; Likewise for `customize-face-other-window'.
242 (define-button-type 'apropos-variable
243 'apropos-label "Variable"
244 'apropos-short-label "v"
245 'face '(font-lock-variable-name-face button)
246 'help-echo "mouse-2, RET: Display more help on this variable"
247 'follow-link t
248 'action (lambda (button)
249 (describe-variable (button-get button 'apropos-symbol))))
251 (define-button-type 'apropos-face
252 'apropos-label "Face"
253 'apropos-short-label "F"
254 'face '(font-lock-variable-name-face button)
255 'help-echo "mouse-2, RET: Display more help on this face"
256 'follow-link t
257 'action (lambda (button)
258 (describe-face (button-get button 'apropos-symbol))))
260 (define-button-type 'apropos-group
261 'apropos-label "Group"
262 'apropos-short-label "g"
263 'face '(font-lock-builtin-face button)
264 'help-echo "mouse-2, RET: Display more help on this group"
265 'follow-link t
266 'action (lambda (button)
267 (customize-group-other-window
268 (button-get button 'apropos-symbol))))
270 (define-button-type 'apropos-widget
271 'apropos-label "Widget"
272 'apropos-short-label "w"
273 'face '(font-lock-builtin-face button)
274 'help-echo "mouse-2, RET: Display more help on this widget"
275 'follow-link t
276 'action (lambda (button)
277 (widget-browse-other-window (button-get button 'apropos-symbol))))
279 (define-button-type 'apropos-plist
280 'apropos-label "Properties"
281 'apropos-short-label "p"
282 'face '(font-lock-keyword-face button)
283 'help-echo "mouse-2, RET: Display more help on this plist"
284 'follow-link t
285 'action (lambda (button)
286 (apropos-describe-plist (button-get button 'apropos-symbol))))
288 (define-button-type 'apropos-library
289 'help-echo "mouse-2, RET: Display more help on this library"
290 'follow-link t
291 'action (lambda (button)
292 (apropos-library (button-get button 'apropos-symbol))))
294 (defun apropos-next-label-button (pos)
295 "Return the next apropos label button after POS, or nil if there's none.
296 Will also return nil if more than one `apropos-symbol' button is encountered
297 before finding a label."
298 (let* ((button (next-button pos t))
299 (already-hit-symbol nil)
300 (label (and button (button-get button 'apropos-label)))
301 (type (and button (button-get button 'type))))
302 (while (and button
303 (not label)
304 (or (not (eq type 'apropos-symbol))
305 (not already-hit-symbol)))
306 (when (eq type 'apropos-symbol)
307 (setq already-hit-symbol t))
308 (setq button (next-button (button-start button)))
309 (when button
310 (setq label (button-get button 'apropos-label))
311 (setq type (button-get button 'type))))
312 (and label button)))
315 (defun apropos-words-to-regexp (words wild)
316 "Make regexp matching any two of the words in WORDS."
317 (concat "\\("
318 (mapconcat 'identity words "\\|")
319 "\\)"
320 (if (cdr words)
321 (concat wild
322 "\\("
323 (mapconcat 'identity words "\\|")
324 "\\)")
325 "")))
327 ;;;###autoload
328 (defun apropos-read-pattern (subject)
329 "Read an apropos pattern, either a word list or a regexp.
330 Returns the user pattern, either a list of words which are matched
331 literally, or a string which is used as a regexp to search for.
333 SUBJECT is a string that is included in the prompt to identify what
334 kind of objects to search."
335 (let ((pattern
336 (read-string (concat "Apropos " subject " (word list or regexp): "))))
337 (if (string-equal (regexp-quote pattern) pattern)
338 ;; Split into words
339 (split-string pattern "[ \t]+")
340 pattern)))
342 (defun apropos-parse-pattern (pattern)
343 "Rewrite a list of words to a regexp matching all permutations.
344 If PATTERN is a string, that means it is already a regexp.
345 This updates variables `apropos-pattern', `apropos-pattern-quoted',
346 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
347 (setq apropos-words nil
348 apropos-all-words nil)
349 (if (consp pattern)
350 ;; We don't actually make a regexp matching all permutations.
351 ;; Instead, for e.g. "a b c", we make a regexp matching
352 ;; any combination of two or more words like this:
353 ;; (a|b|c).*(a|b|c) which may give some false matches,
354 ;; but as long as it also gives the right ones, that's ok.
355 (let ((words pattern))
356 (setq apropos-pattern (mapconcat 'identity pattern " ")
357 apropos-pattern-quoted (regexp-quote apropos-pattern))
358 (dolist (word words)
359 (let ((syn apropos-synonyms) (s word) (a word))
360 (while syn
361 (if (member word (car syn))
362 (progn
363 (setq a (mapconcat 'identity (car syn) "\\|"))
364 (if (member word (cdr (car syn)))
365 (setq s a))
366 (setq syn nil))
367 (setq syn (cdr syn))))
368 (setq apropos-words (cons s apropos-words)
369 apropos-all-words (cons a apropos-all-words))))
370 (setq apropos-all-words-regexp
371 (apropos-words-to-regexp apropos-all-words ".+"))
372 (setq apropos-regexp
373 (apropos-words-to-regexp apropos-words ".*?")))
374 (setq apropos-pattern-quoted (regexp-quote pattern)
375 apropos-all-words-regexp pattern
376 apropos-pattern pattern
377 apropos-regexp pattern)))
380 (defun apropos-calc-scores (str words)
381 "Return apropos scores for string STR matching WORDS.
382 Value is a list of offsets of the words into the string."
383 (let (scores i)
384 (if words
385 (dolist (word words scores)
386 (if (setq i (string-match word str))
387 (setq scores (cons i scores))))
388 ;; Return list of start and end position of regexp
389 (and (string-match apropos-pattern str)
390 (list (match-beginning 0) (match-end 0))))))
392 (defun apropos-score-str (str)
393 "Return apropos score for string STR."
394 (if str
395 (let* ((l (length str))
396 (score (- (/ l 10))))
397 (dolist (s (apropos-calc-scores str apropos-all-words) score)
398 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
401 (defun apropos-score-doc (doc)
402 "Return apropos score for documentation string DOC."
403 (let ((l (length doc)))
404 (if (> l 0)
405 (let ((score 0))
406 (when (string-match apropos-pattern-quoted doc)
407 (setq score 10000))
408 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
409 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
410 0)))
412 (defun apropos-score-symbol (symbol &optional weight)
413 "Return apropos score for SYMBOL."
414 (setq symbol (symbol-name symbol))
415 (let ((score 0)
416 (l (length symbol)))
417 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
418 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
420 (defun apropos-true-hit (str words)
421 "Return t if STR is a genuine hit.
422 This may fail if only one of the keywords is matched more than once.
423 This requires that at least 2 keywords (unless only one was given)."
424 (or (not str)
425 (not words)
426 (not (cdr words))
427 (> (length (apropos-calc-scores str words)) 1)))
429 (defun apropos-false-hit-symbol (symbol)
430 "Return t if SYMBOL is not really matched by the current keywords."
431 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
433 (defun apropos-false-hit-str (str)
434 "Return t if STR is not really matched by the current keywords."
435 (not (apropos-true-hit str apropos-words)))
437 (defun apropos-true-hit-doc (doc)
438 "Return t if DOC is really matched by the current keywords."
439 (apropos-true-hit doc apropos-all-words))
441 (define-derived-mode apropos-mode special-mode "Apropos"
442 "Major mode for following hyperlinks in output of apropos commands.
444 \\{apropos-mode-map}")
446 (defvar apropos-multi-type t
447 "If non-nil, this apropos query concerns multiple types.
448 This is used to decide whether to print the result's type or not.")
450 ;;;###autoload
451 (defun apropos-variable (pattern &optional do-all)
452 "Show user variables that match PATTERN.
453 PATTERN can be a word, a list of words (separated by spaces),
454 or a regexp (using some regexp special characters). If it is a word,
455 search for matches for that word as a substring. If it is a list of words,
456 search for matches for any two (or more) of those words.
458 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
459 normal variables."
460 (interactive (list (apropos-read-pattern
461 (if (or current-prefix-arg apropos-do-all)
462 "variable" "user option"))
463 current-prefix-arg))
464 (apropos-command pattern nil
465 (if (or do-all apropos-do-all)
466 #'(lambda (symbol)
467 (and (boundp symbol)
468 (get symbol 'variable-documentation)))
469 'user-variable-p)))
471 ;; For auld lang syne:
472 ;;;###autoload
473 (defalias 'command-apropos 'apropos-command)
474 ;;;###autoload
475 (defun apropos-command (pattern &optional do-all var-predicate)
476 "Show commands (interactively callable functions) that match PATTERN.
477 PATTERN can be a word, a list of words (separated by spaces),
478 or a regexp (using some regexp special characters). If it is a word,
479 search for matches for that word as a substring. If it is a list of words,
480 search for matches for any two (or more) of those words.
482 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
483 noninteractive functions.
485 If VAR-PREDICATE is non-nil, show only variables, and only those that
486 satisfy the predicate VAR-PREDICATE.
488 When called from a Lisp program, a string PATTERN is used as a regexp,
489 while a list of strings is used as a word list."
490 (interactive (list (apropos-read-pattern
491 (if (or current-prefix-arg apropos-do-all)
492 "command or function" "command"))
493 current-prefix-arg))
494 (apropos-parse-pattern pattern)
495 (let ((message
496 (let ((standard-output (get-buffer-create "*Apropos*")))
497 (help-print-return-message 'identity))))
498 (or do-all (setq do-all apropos-do-all))
499 (setq apropos-accumulator
500 (apropos-internal apropos-regexp
501 (or var-predicate
502 ;; We used to use `functionp' here, but this
503 ;; rules out macros. `fboundp' rules in
504 ;; keymaps, but it seems harmless.
505 (if do-all 'fboundp 'commandp))))
506 (let ((tem apropos-accumulator))
507 (while tem
508 (if (or (get (car tem) 'apropos-inhibit)
509 (apropos-false-hit-symbol (car tem)))
510 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
511 (setq tem (cdr tem))))
512 (let ((p apropos-accumulator)
513 doc symbol score)
514 (while p
515 (setcar p (list
516 (setq symbol (car p))
517 (setq score (apropos-score-symbol symbol))
518 (unless var-predicate
519 (if (fboundp symbol)
520 (if (setq doc (condition-case nil
521 (documentation symbol t)
522 (error 'error)))
523 ;; Eg alias to undefined function.
524 (if (eq doc 'error)
525 "(documentation error)"
526 (setq score (+ score (apropos-score-doc doc)))
527 (substring doc 0 (string-match "\n" doc)))
528 "(not documented)")))
529 (and var-predicate
530 (funcall var-predicate symbol)
531 (if (setq doc (documentation-property
532 symbol 'variable-documentation t))
533 (progn
534 (setq score (+ score (apropos-score-doc doc)))
535 (substring doc 0
536 (string-match "\n" doc)))))))
537 (setcar (cdr (car p)) score)
538 (setq p (cdr p))))
539 (and (let ((apropos-multi-type do-all))
540 (apropos-print t nil nil t))
541 message
542 (message "%s" message))))
545 ;;;###autoload
546 (defun apropos-documentation-property (symbol property raw)
547 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
548 (condition-case ()
549 (let ((doc (documentation-property symbol property raw)))
550 (if doc (substring doc 0 (string-match "\n" doc))
551 "(not documented)"))
552 (error "(error retrieving documentation)")))
555 ;;;###autoload
556 (defun apropos (pattern &optional do-all)
557 "Show all meaningful Lisp symbols whose names match PATTERN.
558 Symbols are shown if they are defined as functions, variables, or
559 faces, or if they have nonempty property lists.
561 PATTERN can be a word, a list of words (separated by spaces),
562 or a regexp (using some regexp special characters). If it is a word,
563 search for matches for that word as a substring. If it is a list of words,
564 search for matches for any two (or more) of those words.
566 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
567 consider all symbols (if they match PATTERN).
569 Returns list of symbols and documentation found."
570 (interactive (list (apropos-read-pattern "symbol")
571 current-prefix-arg))
572 (apropos-parse-pattern pattern)
573 (apropos-symbols-internal
574 (apropos-internal apropos-regexp
575 (and (not do-all)
576 (not apropos-do-all)
577 (lambda (symbol)
578 (or (fboundp symbol)
579 (boundp symbol)
580 (facep symbol)
581 (symbol-plist symbol)))))
582 (or do-all apropos-do-all)))
584 (defun apropos-library-button (sym)
585 (if (null sym)
586 "<nothing>"
587 (let ((name (copy-sequence (symbol-name sym))))
588 (make-text-button name nil
589 'type 'apropos-library
590 'face apropos-symbol-face
591 'apropos-symbol name)
592 name)))
594 ;;;###autoload
595 (defun apropos-library (file)
596 "List the variables and functions defined by library FILE.
597 FILE should be one of the libraries currently loaded and should
598 thus be found in `load-history'. If `apropos-do-all' is non-nil,
599 the output includes key-bindings of commands."
600 (interactive
601 (let* ((libs (delq nil (mapcar 'car load-history)))
602 (libs
603 (nconc (delq nil
604 (mapcar
605 (lambda (l)
606 (setq l (file-name-nondirectory l))
607 (while
608 (not (equal (setq l (file-name-sans-extension l))
609 l)))
611 libs))
612 libs)))
613 (list (completing-read "Describe library: " libs nil t))))
614 (let ((symbols nil)
615 ;; (autoloads nil)
616 (provides nil)
617 (requires nil)
618 (lh-entry (assoc file load-history)))
619 (unless lh-entry
620 ;; `file' may be the "shortname".
621 (let ((lh load-history)
622 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file)
623 "\\(\\.\\|\\'\\)")))
624 (while (and lh (null lh-entry))
625 (if (and (caar lh) (string-match re (caar lh)))
626 (setq lh-entry (car lh))
627 (setq lh (cdr lh)))))
628 (unless lh-entry (error "Unknown library `%s'" file)))
629 (dolist (x (cdr lh-entry))
630 (case (car-safe x)
631 ;; (autoload (push (cdr x) autoloads))
632 (require (push (cdr x) requires))
633 (provide (push (cdr x) provides))
634 (t (push (or (cdr-safe x) x) symbols))))
635 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
636 (apropos-symbols-internal
637 symbols apropos-do-all
638 (concat
639 (format "Library `%s' provides: %s\nand requires: %s"
640 file
641 (mapconcat 'apropos-library-button
642 (or provides '(nil)) " and ")
643 (mapconcat 'apropos-library-button
644 (or requires '(nil)) " and ")))))))
646 (defun apropos-symbols-internal (symbols keys &optional text)
647 ;; Filter out entries that are marked as apropos-inhibit.
648 (let ((all nil))
649 (dolist (symbol symbols)
650 (unless (get symbol 'apropos-inhibit)
651 (push symbol all)))
652 (setq symbols all))
653 (let ((apropos-accumulator
654 (mapcar
655 (lambda (symbol)
656 (let (doc properties)
657 (list
658 symbol
659 (apropos-score-symbol symbol)
660 (when (fboundp symbol)
661 (if (setq doc (condition-case nil
662 (documentation symbol t)
663 (void-function
664 "(alias for undefined function)")
665 (error
666 "(can't retrieve function documentation)")))
667 (substring doc 0 (string-match "\n" doc))
668 "(not documented)"))
669 (when (boundp symbol)
670 (apropos-documentation-property
671 symbol 'variable-documentation t))
672 (when (setq properties (symbol-plist symbol))
673 (setq doc (list (car properties)))
674 (while (setq properties (cdr (cdr properties)))
675 (setq doc (cons (car properties) doc)))
676 (mapconcat #'symbol-name (nreverse doc) " "))
677 (when (get symbol 'widget-type)
678 (apropos-documentation-property
679 symbol 'widget-documentation t))
680 (when (facep symbol)
681 (let ((alias (get symbol 'face-alias)))
682 (if alias
683 (if (facep alias)
684 (format "%slias for the face `%s'."
685 (if (get symbol 'obsolete-face)
686 "Obsolete a"
687 "A")
688 alias)
689 ;; Never happens in practice because fails
690 ;; (facep symbol) test.
691 "(alias for undefined face)")
692 (apropos-documentation-property
693 symbol 'face-documentation t))))
694 (when (get symbol 'custom-group)
695 (apropos-documentation-property
696 symbol 'group-documentation t)))))
697 symbols)))
698 (apropos-print keys nil text)))
701 ;;;###autoload
702 (defun apropos-value (pattern &optional do-all)
703 "Show all symbols whose value's printed representation matches PATTERN.
704 PATTERN can be a word, a list of words (separated by spaces),
705 or a regexp (using some regexp special characters). If it is a word,
706 search for matches for that word as a substring. If it is a list of words,
707 search for matches for any two (or more) of those words.
709 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
710 at function definitions (arguments, documentation and body) and at the
711 names and values of properties.
713 Returns list of symbols and values found."
714 (interactive (list (apropos-read-pattern "value")
715 current-prefix-arg))
716 (apropos-parse-pattern pattern)
717 (or do-all (setq do-all apropos-do-all))
718 (setq apropos-accumulator ())
719 (let (f v p)
720 (mapatoms
721 (lambda (symbol)
722 (setq f nil v nil p nil)
723 (or (memq symbol '(apropos-regexp
724 apropos-pattern apropos-all-words-regexp
725 apropos-words apropos-all-words
726 do-all apropos-accumulator
727 symbol f v p))
728 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
729 (if do-all
730 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
731 p (apropos-format-plist symbol "\n " t)))
732 (if (apropos-false-hit-str v)
733 (setq v nil))
734 (if (apropos-false-hit-str f)
735 (setq f nil))
736 (if (apropos-false-hit-str p)
737 (setq p nil))
738 (if (or f v p)
739 (setq apropos-accumulator (cons (list symbol
740 (+ (apropos-score-str f)
741 (apropos-score-str v)
742 (apropos-score-str p))
743 f v p)
744 apropos-accumulator))))))
745 (let ((apropos-multi-type do-all))
746 (apropos-print nil "\n----------------\n")))
749 ;;;###autoload
750 (defun apropos-documentation (pattern &optional do-all)
751 "Show symbols whose documentation contains matches for PATTERN.
752 PATTERN can be a word, a list of words (separated by spaces),
753 or a regexp (using some regexp special characters). If it is a word,
754 search for matches for that word as a substring. If it is a list of words,
755 search for matches for any two (or more) of those words.
757 Note that by default this command only searches in the file specified by
758 `internal-doc-file-name'; i.e., the etc/DOC file. With \\[universal-argument] prefix,
759 or if `apropos-do-all' is non-nil, it searches all currently defined
760 documentation strings.
762 Returns list of symbols and documentation found."
763 ;; The doc used to say that DO-ALL includes key-bindings info in the
764 ;; output, but I cannot see that that is true.
765 (interactive (list (apropos-read-pattern "documentation")
766 current-prefix-arg))
767 (apropos-parse-pattern pattern)
768 (or do-all (setq do-all apropos-do-all))
769 (setq apropos-accumulator () apropos-files-scanned ())
770 (let ((standard-input (get-buffer-create " apropos-temp"))
771 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
772 f v sf sv)
773 (unwind-protect
774 (with-current-buffer standard-input
775 (apropos-documentation-check-doc-file)
776 (if do-all
777 (mapatoms
778 (lambda (symbol)
779 (setq f (apropos-safe-documentation symbol)
780 v (get symbol 'variable-documentation))
781 (if (integerp v) (setq v))
782 (setq f (apropos-documentation-internal f)
783 v (apropos-documentation-internal v))
784 (setq sf (apropos-score-doc f)
785 sv (apropos-score-doc v))
786 (if (or f v)
787 (if (setq apropos-item
788 (cdr (assq symbol apropos-accumulator)))
789 (progn
790 (if f
791 (progn
792 (setcar (nthcdr 1 apropos-item) f)
793 (setcar apropos-item (+ (car apropos-item) sf))))
794 (if v
795 (progn
796 (setcar (nthcdr 2 apropos-item) v)
797 (setcar apropos-item (+ (car apropos-item) sv)))))
798 (setq apropos-accumulator
799 (cons (list symbol
800 (+ (apropos-score-symbol symbol 2) sf sv)
801 f v)
802 apropos-accumulator)))))))
803 (apropos-print nil "\n----------------\n" nil t))
804 (kill-buffer standard-input))))
807 (defun apropos-value-internal (predicate symbol function)
808 (if (funcall predicate symbol)
809 (progn
810 (setq symbol (prin1-to-string (funcall function symbol)))
811 (if (string-match apropos-regexp symbol)
812 (progn
813 (if apropos-match-face
814 (put-text-property (match-beginning 0) (match-end 0)
815 'face apropos-match-face
816 symbol))
817 symbol)))))
819 (defun apropos-documentation-internal (doc)
820 (if (consp doc)
821 (apropos-documentation-check-elc-file (car doc))
822 (if (and doc
823 (string-match apropos-all-words-regexp doc)
824 (apropos-true-hit-doc doc))
825 (when apropos-match-face
826 (setq doc (substitute-command-keys (copy-sequence doc)))
827 (if (or (string-match apropos-pattern-quoted doc)
828 (string-match apropos-all-words-regexp doc))
829 (put-text-property (match-beginning 0)
830 (match-end 0)
831 'face apropos-match-face doc))
832 doc))))
834 (defun apropos-format-plist (pl sep &optional compare)
835 (setq pl (symbol-plist pl))
836 (let (p p-out)
837 (while pl
838 (setq p (format "%s %S" (car pl) (nth 1 pl)))
839 (if (or (not compare) (string-match apropos-regexp p))
840 (if apropos-property-face
841 (put-text-property 0 (length (symbol-name (car pl)))
842 'face apropos-property-face p))
843 (setq p nil))
844 (if p
845 (progn
846 (and compare apropos-match-face
847 (put-text-property (match-beginning 0) (match-end 0)
848 'face apropos-match-face
850 (setq p-out (concat p-out (if p-out sep) p))))
851 (setq pl (nthcdr 2 pl)))
852 p-out))
855 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
857 (defun apropos-documentation-check-doc-file ()
858 (let (type symbol (sepa 2) sepb doc)
859 (insert ?\^_)
860 (backward-char)
861 (insert-file-contents (concat doc-directory internal-doc-file-name))
862 (forward-char)
863 (while (save-excursion
864 (setq sepb (search-forward "\^_"))
865 (not (eobp)))
866 (beginning-of-line 2)
867 (if (save-restriction
868 (narrow-to-region (point) (1- sepb))
869 (re-search-forward apropos-all-words-regexp nil t))
870 (progn
871 (goto-char (1+ sepa))
872 (setq type (if (eq ?F (preceding-char))
873 2 ; function documentation
874 3) ; variable documentation
875 symbol (read)
876 doc (buffer-substring (1+ (point)) (1- sepb)))
877 (when (and (apropos-true-hit-doc doc)
878 ;; The DOC file lists all built-in funcs and vars.
879 ;; If any are not currently bound, they can
880 ;; only be platform-specific stuff (eg NS) not
881 ;; in use on the current platform.
882 ;; So we exclude them.
883 (cond ((= 3 type) (boundp symbol))
884 ((= 2 type) (fboundp symbol))))
885 (or (and (setq apropos-item (assq symbol apropos-accumulator))
886 (setcar (cdr apropos-item)
887 (apropos-score-doc doc)))
888 (setq apropos-item (list symbol
889 (+ (apropos-score-symbol symbol 2)
890 (apropos-score-doc doc))
891 nil nil)
892 apropos-accumulator (cons apropos-item
893 apropos-accumulator)))
894 (when apropos-match-face
895 (setq doc (substitute-command-keys doc))
896 (if (or (string-match apropos-pattern-quoted doc)
897 (string-match apropos-all-words-regexp doc))
898 (put-text-property (match-beginning 0)
899 (match-end 0)
900 'face apropos-match-face doc)))
901 (setcar (nthcdr type apropos-item) doc))))
902 (setq sepa (goto-char sepb)))))
904 (defun apropos-documentation-check-elc-file (file)
905 (if (member file apropos-files-scanned)
907 (let (symbol doc beg end this-is-a-variable)
908 (setq apropos-files-scanned (cons file apropos-files-scanned))
909 (erase-buffer)
910 (insert-file-contents file)
911 (while (search-forward "\n#@" nil t)
912 ;; Read the comment length, and advance over it.
913 (setq end (read)
914 beg (1+ (point))
915 end (+ (point) end -1))
916 (forward-char)
917 (if (save-restriction
918 ;; match ^ and $ relative to doc string
919 (narrow-to-region beg end)
920 (re-search-forward apropos-all-words-regexp nil t))
921 (progn
922 (goto-char (+ end 2))
923 (setq doc (buffer-substring beg end)
924 end (- (match-end 0) beg)
925 beg (- (match-beginning 0) beg))
926 (when (apropos-true-hit-doc doc)
927 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
928 symbol (progn
929 (skip-chars-forward "(a-z")
930 (forward-char)
931 (read))
932 symbol (if (consp symbol)
933 (nth 1 symbol)
934 symbol))
935 (if (if this-is-a-variable
936 (get symbol 'variable-documentation)
937 (and (fboundp symbol) (apropos-safe-documentation symbol)))
938 (progn
939 (or (and (setq apropos-item (assq symbol apropos-accumulator))
940 (setcar (cdr apropos-item)
941 (+ (cadr apropos-item) (apropos-score-doc doc))))
942 (setq apropos-item (list symbol
943 (+ (apropos-score-symbol symbol 2)
944 (apropos-score-doc doc))
945 nil nil)
946 apropos-accumulator (cons apropos-item
947 apropos-accumulator)))
948 (when apropos-match-face
949 (setq doc (substitute-command-keys doc))
950 (if (or (string-match apropos-pattern-quoted doc)
951 (string-match apropos-all-words-regexp doc))
952 (put-text-property (match-beginning 0)
953 (match-end 0)
954 'face apropos-match-face doc)))
955 (setcar (nthcdr (if this-is-a-variable 3 2)
956 apropos-item)
957 doc))))))))))
961 (defun apropos-safe-documentation (function)
962 "Like `documentation', except it avoids calling `get_doc_string'.
963 Will return nil instead."
964 (while (and function (symbolp function))
965 (setq function (if (fboundp function)
966 (symbol-function function))))
967 (if (eq (car-safe function) 'macro)
968 (setq function (cdr function)))
969 (setq function (if (byte-code-function-p function)
970 (if (> (length function) 4)
971 (aref function 4))
972 (if (eq (car-safe function) 'autoload)
973 (nth 2 function)
974 (if (eq (car-safe function) 'lambda)
975 (if (stringp (nth 2 function))
976 (nth 2 function)
977 (if (stringp (nth 3 function))
978 (nth 3 function)))))))
979 (if (integerp function)
981 function))
983 (defcustom apropos-compact-layout nil
984 "If non-nil, use a single line per binding."
985 :type 'boolean)
987 (defun apropos-print (do-keys spacing &optional text nosubst)
988 "Output result of apropos searching into buffer `*Apropos*'.
989 The value of `apropos-accumulator' is the list of items to output.
990 Each element should have the format
991 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
992 The return value is the list that was in `apropos-accumulator', sorted
993 alphabetically by symbol name; but this function also sets
994 `apropos-accumulator' to nil before returning.
996 If SPACING is non-nil, it should be a string; separate items with that string.
997 If non-nil TEXT is a string that will be printed as a heading."
998 (if (null apropos-accumulator)
999 (message "No apropos matches for `%s'" apropos-pattern)
1000 (setq apropos-accumulator
1001 (sort apropos-accumulator
1002 (lambda (a b)
1003 ;; Don't sort by score if user can't see the score.
1004 ;; It would be confusing. -- rms.
1005 (if apropos-sort-by-scores
1006 (or (> (cadr a) (cadr b))
1007 (and (= (cadr a) (cadr b))
1008 (string-lessp (car a) (car b))))
1009 (string-lessp (car a) (car b))))))
1010 (with-output-to-temp-buffer "*Apropos*"
1011 (let ((p apropos-accumulator)
1012 (old-buffer (current-buffer))
1013 (inhibit-read-only t)
1014 symbol item)
1015 (set-buffer standard-output)
1016 (apropos-mode)
1017 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
1018 (if apropos-multi-type "a type label" "an entry")
1019 " to view its full documentation.\n\n")
1020 (if text (insert text "\n\n"))
1021 (dolist (apropos-item p)
1022 (when (and spacing (not (bobp)))
1023 (princ spacing))
1024 (setq symbol (car apropos-item))
1025 ;; Insert dummy score element for backwards compatibility with 21.x
1026 ;; apropos-item format.
1027 (if (not (numberp (cadr apropos-item)))
1028 (setq apropos-item
1029 (cons (car apropos-item)
1030 (cons nil (cdr apropos-item)))))
1031 (insert-text-button (symbol-name symbol)
1032 'type 'apropos-symbol
1033 'skip apropos-multi-type
1034 ;; Can't use default, since user may have
1035 ;; changed the variable!
1036 ;; Just say `no' to variables containing faces!
1037 'face apropos-symbol-face)
1038 (if (and (eq apropos-sort-by-scores 'verbose)
1039 (cadr apropos-item))
1040 (insert " (" (number-to-string (cadr apropos-item)) ") "))
1041 ;; Calculate key-bindings if we want them.
1042 (unless apropos-compact-layout
1043 (and do-keys
1044 (commandp symbol)
1045 (not (eq symbol 'self-insert-command))
1046 (indent-to 30 1)
1047 (if (let ((keys
1048 (with-current-buffer old-buffer
1049 (where-is-internal symbol)))
1050 filtered)
1051 ;; Copy over the list of key sequences,
1052 ;; omitting any that contain a buffer or a frame.
1053 ;; FIXME: Why omit keys that contain buffers and
1054 ;; frames? This looks like a bad workaround rather
1055 ;; than a proper fix. Does anybody know what problem
1056 ;; this is trying to address? --Stef
1057 (dolist (key keys)
1058 (let ((i 0)
1059 loser)
1060 (while (< i (length key))
1061 (if (or (framep (aref key i))
1062 (bufferp (aref key i)))
1063 (setq loser t))
1064 (setq i (1+ i)))
1065 (or loser
1066 (push key filtered))))
1067 (setq item filtered))
1068 ;; Convert the remaining keys to a string and insert.
1069 (insert
1070 (mapconcat
1071 (lambda (key)
1072 (setq key (condition-case ()
1073 (key-description key)
1074 (error)))
1075 (if apropos-keybinding-face
1076 (put-text-property 0 (length key)
1077 'face apropos-keybinding-face
1078 key))
1079 key)
1080 item ", "))
1081 (insert "M-x ... RET")
1082 (when apropos-keybinding-face
1083 (put-text-property (- (point) 11) (- (point) 8)
1084 'face apropos-keybinding-face)
1085 (put-text-property (- (point) 3) (point)
1086 'face apropos-keybinding-face))))
1087 (terpri))
1088 (apropos-print-doc 2
1089 (if (commandp symbol)
1090 'apropos-command
1091 (if (apropos-macrop symbol)
1092 'apropos-macro
1093 'apropos-function))
1094 (not nosubst))
1095 (apropos-print-doc 3 'apropos-variable (not nosubst))
1096 (apropos-print-doc 7 'apropos-group t)
1097 (apropos-print-doc 6 'apropos-face t)
1098 (apropos-print-doc 5 'apropos-widget t)
1099 (apropos-print-doc 4 'apropos-plist nil))
1100 (set (make-local-variable 'truncate-partial-width-windows) t)
1101 (set (make-local-variable 'truncate-lines) t))))
1102 (prog1 apropos-accumulator
1103 (setq apropos-accumulator ()))) ; permit gc
1105 (defun apropos-macrop (symbol)
1106 "Return t if SYMBOL is a Lisp macro."
1107 (and (fboundp symbol)
1108 (consp (setq symbol
1109 (symbol-function symbol)))
1110 (or (eq (car symbol) 'macro)
1111 (if (eq (car symbol) 'autoload)
1112 (memq (nth 4 symbol)
1113 '(macro t))))))
1116 (defun apropos-print-doc (i type do-keys)
1117 (let ((doc (nth i apropos-item)))
1118 (when (stringp doc)
1119 (if apropos-compact-layout
1120 (insert (propertize "\t" 'display '(space :align-to 32)) " ")
1121 (insert " "))
1122 (if apropos-multi-type
1123 (let ((button-face (button-type-get type 'face)))
1124 (unless (consp button-face)
1125 (setq button-face (list button-face)))
1126 (insert-text-button
1127 (if apropos-compact-layout
1128 (format "<%s>" (button-type-get type 'apropos-short-label))
1129 (button-type-get type 'apropos-label))
1130 'type type
1131 ;; Can't use the default button face, since user may have changed the
1132 ;; variable! Just say `no' to variables containing faces!
1133 'face (append button-face apropos-label-face)
1134 'apropos-symbol (car apropos-item))
1135 (insert (if apropos-compact-layout " " ": ")))
1137 ;; If the query is only for a single type, there's no point
1138 ;; writing it over and over again. Insert a blank button, and
1139 ;; put the 'apropos-label property there (needed by
1140 ;; apropos-symbol-button-display-help).
1141 (insert-text-button
1142 " " 'type type 'skip t
1143 'face 'default 'apropos-symbol (car apropos-item)))
1145 (let ((opoint (point))
1146 (ocol (current-column)))
1147 (cond ((equal doc "")
1148 (setq doc "(not documented)"))
1149 (do-keys
1150 (setq doc (substitute-command-keys doc))))
1151 (insert doc)
1152 (if (equal doc "(not documented)")
1153 (put-text-property opoint (point) 'font-lock-face 'shadow))
1154 ;; The labeling buttons might make the line too long, so fill it if
1155 ;; necessary.
1156 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column)
1157 emacs-lisp-docstring-fill-column
1158 fill-column)))
1159 (fill-prefix (make-string ocol ?\s)))
1160 (fill-region opoint (point) nil t)))
1161 (or (bolp) (terpri)))))
1163 (defun apropos-follow ()
1164 "Invokes any button at point, otherwise invokes the nearest label button."
1165 (interactive)
1166 (button-activate
1167 (or (apropos-next-label-button (line-beginning-position))
1168 (error "There is nothing to follow here"))))
1171 (defun apropos-describe-plist (symbol)
1172 "Display a pretty listing of SYMBOL's plist."
1173 (help-setup-xref (list 'apropos-describe-plist symbol)
1174 (called-interactively-p 'interactive))
1175 (with-help-window (help-buffer)
1176 (set-buffer standard-output)
1177 (princ "Symbol ")
1178 (prin1 symbol)
1179 (princ "'s plist is\n (")
1180 (if apropos-symbol-face
1181 (put-text-property (+ (point-min) 7) (- (point) 14)
1182 'face apropos-symbol-face))
1183 (insert (apropos-format-plist symbol "\n "))
1184 (princ ")")))
1187 (provide 'apropos)
1189 ;;; apropos.el ends here