Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / apropos.el
blobc7969afcb88609e2a3cc6192b8cb49d99e1ecdc2
1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
7 ;; Rewritten: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: help
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
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 (make-sparse-keymap)))
125 (set-keymap-parent map button-buffer-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 (define-key map " " 'scroll-up)
131 (define-key map "\177" 'scroll-down)
132 (define-key map "q" 'quit-window)
133 map)
134 "Keymap used in Apropos mode.")
136 (defvar apropos-mode-hook nil
137 "*Hook run when mode is turned on.")
139 (defvar apropos-pattern nil
140 "Apropos pattern as entered by user.")
142 (defvar apropos-pattern-quoted nil
143 "Apropos pattern passed through `regexp-quote'.")
145 (defvar apropos-words ()
146 "Current list of apropos words extracted from `apropos-pattern'.")
148 (defvar apropos-all-words ()
149 "Current list of words and synonyms.")
151 (defvar apropos-regexp nil
152 "Regexp used in current apropos run.")
154 (defvar apropos-all-words-regexp nil
155 "Regexp matching apropos-all-words.")
157 (defvar apropos-files-scanned ()
158 "List of elc files already scanned in current run of `apropos-documentation'.")
160 (defvar apropos-accumulator ()
161 "Alist of symbols already found in current apropos run.")
163 (defvar apropos-item ()
164 "Current item in or for `apropos-accumulator'.")
166 (defvar apropos-synonyms '(
167 ("find" "open" "edit")
168 ("kill" "cut")
169 ("yank" "paste")
170 ("region" "selection"))
171 "List of synonyms known by apropos.
172 Each element is a list of words where the first word is the standard Emacs
173 term, and the rest of the words are alternative terms.")
176 ;;; Button types used by apropos
178 (define-button-type 'apropos-symbol
179 'face apropos-symbol-face
180 'help-echo "mouse-2, RET: Display more help on this symbol"
181 'follow-link t
182 'action #'apropos-symbol-button-display-help
183 'skip t)
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 'help-echo "mouse-2, RET: Display more help on this function"
194 'follow-link t
195 'action (lambda (button)
196 (describe-function (button-get button 'apropos-symbol))))
198 (define-button-type 'apropos-macro
199 'apropos-label "Macro"
200 'help-echo "mouse-2, RET: Display more help on this macro"
201 'follow-link t
202 'action (lambda (button)
203 (describe-function (button-get button 'apropos-symbol))))
205 (define-button-type 'apropos-command
206 'apropos-label "Command"
207 'help-echo "mouse-2, RET: Display more help on this command"
208 'follow-link t
209 'action (lambda (button)
210 (describe-function (button-get button 'apropos-symbol))))
212 ;; We used to use `customize-variable-other-window' instead for a
213 ;; customizable variable, but that is slow. It is better to show an
214 ;; ordinary help buffer and let the user click on the customization
215 ;; button in that buffer, if he wants to.
216 ;; Likewise for `customize-face-other-window'.
217 (define-button-type 'apropos-variable
218 'apropos-label "Variable"
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 'help-echo "mouse-2, RET: Display more help on this face"
227 'follow-link t
228 'action (lambda (button)
229 (describe-face (button-get button 'apropos-symbol))))
231 (define-button-type 'apropos-group
232 'apropos-label "Group"
233 'help-echo "mouse-2, RET: Display more help on this group"
234 'follow-link t
235 'action (lambda (button)
236 (customize-group-other-window
237 (button-get button 'apropos-symbol))))
239 (define-button-type 'apropos-widget
240 'apropos-label "Widget"
241 'help-echo "mouse-2, RET: Display more help on this widget"
242 'follow-link t
243 'action (lambda (button)
244 (widget-browse-other-window (button-get button 'apropos-symbol))))
246 (define-button-type 'apropos-plist
247 'apropos-label "Plist"
248 'help-echo "mouse-2, RET: Display more help on this plist"
249 'follow-link t
250 'action (lambda (button)
251 (apropos-describe-plist (button-get button 'apropos-symbol))))
253 (defun apropos-next-label-button (pos)
254 "Return the next apropos label button after POS, or nil if there's none.
255 Will also return nil if more than one `apropos-symbol' button is encountered
256 before finding a label."
257 (let* ((button (next-button pos t))
258 (already-hit-symbol nil)
259 (label (and button (button-get button 'apropos-label)))
260 (type (and button (button-get button 'type))))
261 (while (and button
262 (not label)
263 (or (not (eq type 'apropos-symbol))
264 (not already-hit-symbol)))
265 (when (eq type 'apropos-symbol)
266 (setq already-hit-symbol t))
267 (setq button (next-button (button-start button)))
268 (when button
269 (setq label (button-get button 'apropos-label))
270 (setq type (button-get button 'type))))
271 (and label button)))
274 (defun apropos-words-to-regexp (words wild)
275 "Make regexp matching any two of the words in WORDS."
276 (concat "\\("
277 (mapconcat 'identity words "\\|")
278 "\\)"
279 (if (cdr words)
280 (concat wild
281 "\\("
282 (mapconcat 'identity words "\\|")
283 "\\)")
284 "")))
286 ;;;###autoload
287 (defun apropos-read-pattern (subject)
288 "Read an apropos pattern, either a word list or a regexp.
289 Returns the user pattern, either a list of words which are matched
290 literally, or a string which is used as a regexp to search for.
292 SUBJECT is a string that is included in the prompt to identify what
293 kind of objects to search."
294 (let ((pattern
295 (read-string (concat "Apropos " subject " (word list or regexp): "))))
296 (if (string-equal (regexp-quote pattern) pattern)
297 ;; Split into words
298 (split-string pattern "[ \t]+")
299 pattern)))
301 (defun apropos-parse-pattern (pattern)
302 "Rewrite a list of words to a regexp matching all permutations.
303 If PATTERN is a string, that means it is already a regexp.
304 This updates variables `apropos-pattern', `apropos-pattern-quoted',
305 `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
306 (setq apropos-words nil
307 apropos-all-words nil)
308 (if (consp pattern)
309 ;; We don't actually make a regexp matching all permutations.
310 ;; Instead, for e.g. "a b c", we make a regexp matching
311 ;; any combination of two or more words like this:
312 ;; (a|b|c).*(a|b|c) which may give some false matches,
313 ;; but as long as it also gives the right ones, that's ok.
314 (let ((words pattern))
315 (setq apropos-pattern (mapconcat 'identity pattern " ")
316 apropos-pattern-quoted (regexp-quote apropos-pattern))
317 (dolist (word words)
318 (let ((syn apropos-synonyms) (s word) (a word))
319 (while syn
320 (if (member word (car syn))
321 (progn
322 (setq a (mapconcat 'identity (car syn) "\\|"))
323 (if (member word (cdr (car syn)))
324 (setq s a))
325 (setq syn nil))
326 (setq syn (cdr syn))))
327 (setq apropos-words (cons s apropos-words)
328 apropos-all-words (cons a apropos-all-words))))
329 (setq apropos-all-words-regexp
330 (apropos-words-to-regexp apropos-all-words ".+"))
331 (setq apropos-regexp
332 (apropos-words-to-regexp apropos-words ".*?")))
333 (setq apropos-pattern-quoted (regexp-quote pattern)
334 apropos-all-words-regexp pattern
335 apropos-pattern pattern
336 apropos-regexp pattern)))
339 (defun apropos-calc-scores (str words)
340 "Return apropos scores for string STR matching WORDS.
341 Value is a list of offsets of the words into the string."
342 (let (scores i)
343 (if words
344 (dolist (word words scores)
345 (if (setq i (string-match word str))
346 (setq scores (cons i scores))))
347 ;; Return list of start and end position of regexp
348 (and (string-match apropos-pattern str)
349 (list (match-beginning 0) (match-end 0))))))
351 (defun apropos-score-str (str)
352 "Return apropos score for string STR."
353 (if str
354 (let* ((l (length str))
355 (score (- (/ l 10))))
356 (dolist (s (apropos-calc-scores str apropos-all-words) score)
357 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
360 (defun apropos-score-doc (doc)
361 "Return apropos score for documentation string DOC."
362 (let ((l (length doc)))
363 (if (> l 0)
364 (let ((score 0) i)
365 (when (setq i (string-match apropos-pattern-quoted doc))
366 (setq score 10000))
367 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
368 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
369 0)))
371 (defun apropos-score-symbol (symbol &optional weight)
372 "Return apropos score for SYMBOL."
373 (setq symbol (symbol-name symbol))
374 (let ((score 0)
375 (l (length symbol)))
376 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
377 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
379 (defun apropos-true-hit (str words)
380 "Return t if STR is a genuine hit.
381 This may fail if only one of the keywords is matched more than once.
382 This requires that at least 2 keywords (unless only one was given)."
383 (or (not str)
384 (not words)
385 (not (cdr words))
386 (> (length (apropos-calc-scores str words)) 1)))
388 (defun apropos-false-hit-symbol (symbol)
389 "Return t if SYMBOL is not really matched by the current keywords."
390 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
392 (defun apropos-false-hit-str (str)
393 "Return t if STR is not really matched by the current keywords."
394 (not (apropos-true-hit str apropos-words)))
396 (defun apropos-true-hit-doc (doc)
397 "Return t if DOC is really matched by the current keywords."
398 (apropos-true-hit doc apropos-all-words))
400 (define-derived-mode apropos-mode fundamental-mode "Apropos"
401 "Major mode for following hyperlinks in output of apropos commands.
403 \\{apropos-mode-map}")
405 ;;;###autoload
406 (defun apropos-variable (pattern &optional do-all)
407 "Show user variables that match PATTERN.
408 PATTERN can be a word, a list of words (separated by spaces),
409 or a regexp (using some regexp special characters). If it is a word,
410 search for matches for that word as a substring. If it is a list of words,
411 search for matches for any two (or more) of those words.
413 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
414 normal variables."
415 (interactive (list (apropos-read-pattern
416 (if (or current-prefix-arg apropos-do-all)
417 "variable" "user option"))
418 current-prefix-arg))
419 (apropos-command pattern nil
420 (if (or do-all apropos-do-all)
421 #'(lambda (symbol)
422 (and (boundp symbol)
423 (get symbol 'variable-documentation)))
424 'user-variable-p)))
426 ;; For auld lang syne:
427 ;;;###autoload
428 (defalias 'command-apropos 'apropos-command)
429 ;;;###autoload
430 (defun apropos-command (pattern &optional do-all var-predicate)
431 "Show commands (interactively callable functions) that match PATTERN.
432 PATTERN can be a word, a list of words (separated by spaces),
433 or a regexp (using some regexp special characters). If it is a word,
434 search for matches for that word as a substring. If it is a list of words,
435 search for matches for any two (or more) of those words.
437 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
438 noninteractive functions.
440 If VAR-PREDICATE is non-nil, show only variables, and only those that
441 satisfy the predicate VAR-PREDICATE.
443 When called from a Lisp program, a string PATTERN is used as a regexp,
444 while a list of strings is used as a word list."
445 (interactive (list (apropos-read-pattern
446 (if (or current-prefix-arg apropos-do-all)
447 "command or function" "command"))
448 current-prefix-arg))
449 (apropos-parse-pattern pattern)
450 (let ((message
451 (let ((standard-output (get-buffer-create "*Apropos*")))
452 (print-help-return-message 'identity))))
453 (or do-all (setq do-all apropos-do-all))
454 (setq apropos-accumulator
455 (apropos-internal apropos-regexp
456 (or var-predicate
457 ;; We used to use `functionp' here, but this
458 ;; rules out macros. `fboundp' rules in
459 ;; keymaps, but it seems harmless.
460 (if do-all 'fboundp 'commandp))))
461 (let ((tem apropos-accumulator))
462 (while tem
463 (if (or (get (car tem) 'apropos-inhibit)
464 (apropos-false-hit-symbol (car tem)))
465 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
466 (setq tem (cdr tem))))
467 (let ((p apropos-accumulator)
468 doc symbol score)
469 (while p
470 (setcar p (list
471 (setq symbol (car p))
472 (setq score (apropos-score-symbol symbol))
473 (unless var-predicate
474 (if (fboundp symbol)
475 (if (setq doc (documentation symbol t))
476 (progn
477 (setq score (+ score (apropos-score-doc doc)))
478 (substring doc 0 (string-match "\n" doc)))
479 "(not documented)")))
480 (and var-predicate
481 (funcall var-predicate symbol)
482 (if (setq doc (documentation-property
483 symbol 'variable-documentation t))
484 (progn
485 (setq score (+ score (apropos-score-doc doc)))
486 (substring doc 0
487 (string-match "\n" doc)))))))
488 (setcar (cdr (car p)) score)
489 (setq p (cdr p))))
490 (and (apropos-print t nil nil t)
491 message
492 (message "%s" message))))
495 ;;;###autoload
496 (defun apropos-documentation-property (symbol property raw)
497 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
498 (condition-case ()
499 (let ((doc (documentation-property symbol property raw)))
500 (if doc (substring doc 0 (string-match "\n" doc))
501 "(not documented)"))
502 (error "(error retrieving documentation)")))
505 ;;;###autoload
506 (defun apropos (pattern &optional do-all)
507 "Show all meaningful Lisp symbols whose names match PATTERN.
508 Symbols are shown if they are defined as functions, variables, or
509 faces, or if they have nonempty property lists.
511 PATTERN can be a word, a list of words (separated by spaces),
512 or a regexp (using some regexp special characters). If it is a word,
513 search for matches for that word as a substring. If it is a list of words,
514 search for matches for any two (or more) of those words.
516 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
517 consider all symbols (if they match PATTERN).
519 Returns list of symbols and documentation found."
520 (interactive (list (apropos-read-pattern "symbol")
521 current-prefix-arg))
522 (apropos-parse-pattern pattern)
523 (apropos-symbols-internal
524 (apropos-internal apropos-regexp
525 (and (not do-all)
526 (not apropos-do-all)
527 (lambda (symbol)
528 (or (fboundp symbol)
529 (boundp symbol)
530 (facep symbol)
531 (symbol-plist symbol)))))
532 (or do-all apropos-do-all)))
534 (defun apropos-symbols-internal (symbols keys &optional text)
535 ;; Filter out entries that are marked as apropos-inhibit.
536 (let ((all nil))
537 (dolist (symbol symbols)
538 (unless (get symbol 'apropos-inhibit)
539 (push symbol all)))
540 (setq symbols all))
541 (let ((apropos-accumulator
542 (mapcar
543 (lambda (symbol)
544 (let (doc properties)
545 (list
546 symbol
547 (apropos-score-symbol symbol)
548 (when (fboundp symbol)
549 (if (setq doc (condition-case nil
550 (documentation symbol t)
551 (void-function
552 "(alias for undefined function)")
553 (error
554 "(can't retrieve function documentation)")))
555 (substring doc 0 (string-match "\n" doc))
556 "(not documented)"))
557 (when (boundp symbol)
558 (apropos-documentation-property
559 symbol 'variable-documentation t))
560 (when (setq properties (symbol-plist symbol))
561 (setq doc (list (car properties)))
562 (while (setq properties (cdr (cdr properties)))
563 (setq doc (cons (car properties) doc)))
564 (mapconcat #'symbol-name (nreverse doc) " "))
565 (when (get symbol 'widget-type)
566 (apropos-documentation-property
567 symbol 'widget-documentation t))
568 (when (facep symbol)
569 (apropos-documentation-property
570 symbol 'face-documentation t))
571 (when (get symbol 'custom-group)
572 (apropos-documentation-property
573 symbol 'group-documentation t)))))
574 symbols)))
575 (apropos-print keys nil text)))
578 ;;;###autoload
579 (defun apropos-value (pattern &optional do-all)
580 "Show all symbols whose value's printed representation matches PATTERN.
581 PATTERN can be a word, a list of words (separated by spaces),
582 or a regexp (using some regexp special characters). If it is a word,
583 search for matches for that word as a substring. If it is a list of words,
584 search for matches for any two (or more) of those words.
586 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
587 at the function and at the names and values of properties.
588 Returns list of symbols and values found."
589 (interactive (list (apropos-read-pattern "value")
590 current-prefix-arg))
591 (apropos-parse-pattern pattern)
592 (or do-all (setq do-all apropos-do-all))
593 (setq apropos-accumulator ())
594 (let (f v p)
595 (mapatoms
596 (lambda (symbol)
597 (setq f nil v nil p nil)
598 (or (memq symbol '(apropos-regexp
599 apropos-pattern apropos-all-words-regexp
600 apropos-words apropos-all-words
601 do-all apropos-accumulator
602 symbol f v p))
603 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
604 (if do-all
605 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
606 p (apropos-format-plist symbol "\n " t)))
607 (if (apropos-false-hit-str v)
608 (setq v nil))
609 (if (apropos-false-hit-str f)
610 (setq f nil))
611 (if (apropos-false-hit-str p)
612 (setq p nil))
613 (if (or f v p)
614 (setq apropos-accumulator (cons (list symbol
615 (+ (apropos-score-str f)
616 (apropos-score-str v)
617 (apropos-score-str p))
618 f v p)
619 apropos-accumulator))))))
620 (apropos-print nil "\n----------------\n"))
623 ;;;###autoload
624 (defun apropos-documentation (pattern &optional do-all)
625 "Show symbols whose documentation contains matches for PATTERN.
626 PATTERN can be a word, a list of words (separated by spaces),
627 or a regexp (using some regexp special characters). If it is a word,
628 search for matches for that word as a substring. If it is a list of words,
629 search for matches for any two (or more) of those words.
631 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
632 documentation that is not stored in the documentation file and show key
633 bindings.
634 Returns list of symbols and documentation found."
635 (interactive (list (apropos-read-pattern "documentation")
636 current-prefix-arg))
637 (apropos-parse-pattern pattern)
638 (or do-all (setq do-all apropos-do-all))
639 (setq apropos-accumulator () apropos-files-scanned ())
640 (let ((standard-input (get-buffer-create " apropos-temp"))
641 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
642 f v sf sv)
643 (unwind-protect
644 (save-excursion
645 (set-buffer standard-input)
646 (apropos-documentation-check-doc-file)
647 (if do-all
648 (mapatoms
649 (lambda (symbol)
650 (setq f (apropos-safe-documentation symbol)
651 v (get symbol 'variable-documentation))
652 (if (integerp v) (setq v))
653 (setq f (apropos-documentation-internal f)
654 v (apropos-documentation-internal v))
655 (setq sf (apropos-score-doc f)
656 sv (apropos-score-doc v))
657 (if (or f v)
658 (if (setq apropos-item
659 (cdr (assq symbol apropos-accumulator)))
660 (progn
661 (if f
662 (progn
663 (setcar (nthcdr 1 apropos-item) f)
664 (setcar apropos-item (+ (car apropos-item) sf))))
665 (if v
666 (progn
667 (setcar (nthcdr 2 apropos-item) v)
668 (setcar apropos-item (+ (car apropos-item) sv)))))
669 (setq apropos-accumulator
670 (cons (list symbol
671 (+ (apropos-score-symbol symbol 2) sf sv)
672 f v)
673 apropos-accumulator)))))))
674 (apropos-print nil "\n----------------\n" nil t))
675 (kill-buffer standard-input))))
678 (defun apropos-value-internal (predicate symbol function)
679 (if (funcall predicate symbol)
680 (progn
681 (setq symbol (prin1-to-string (funcall function symbol)))
682 (if (string-match apropos-regexp symbol)
683 (progn
684 (if apropos-match-face
685 (put-text-property (match-beginning 0) (match-end 0)
686 'face apropos-match-face
687 symbol))
688 symbol)))))
690 (defun apropos-documentation-internal (doc)
691 (if (consp doc)
692 (apropos-documentation-check-elc-file (car doc))
693 (if (and doc
694 (string-match apropos-all-words-regexp doc)
695 (apropos-true-hit-doc doc))
696 (when apropos-match-face
697 (setq doc (substitute-command-keys (copy-sequence doc)))
698 (if (or (string-match apropos-pattern-quoted doc)
699 (string-match apropos-all-words-regexp doc))
700 (put-text-property (match-beginning 0)
701 (match-end 0)
702 'face apropos-match-face doc))
703 doc))))
705 (defun apropos-format-plist (pl sep &optional compare)
706 (setq pl (symbol-plist pl))
707 (let (p p-out)
708 (while pl
709 (setq p (format "%s %S" (car pl) (nth 1 pl)))
710 (if (or (not compare) (string-match apropos-regexp p))
711 (if apropos-property-face
712 (put-text-property 0 (length (symbol-name (car pl)))
713 'face apropos-property-face p))
714 (setq p nil))
715 (if p
716 (progn
717 (and compare apropos-match-face
718 (put-text-property (match-beginning 0) (match-end 0)
719 'face apropos-match-face
721 (setq p-out (concat p-out (if p-out sep) p))))
722 (setq pl (nthcdr 2 pl)))
723 p-out))
726 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
728 (defun apropos-documentation-check-doc-file ()
729 (let (type symbol (sepa 2) sepb)
730 (insert ?\^_)
731 (backward-char)
732 (insert-file-contents (concat doc-directory internal-doc-file-name))
733 (forward-char)
734 (while (save-excursion
735 (setq sepb (search-forward "\^_"))
736 (not (eobp)))
737 (beginning-of-line 2)
738 (if (save-restriction
739 (narrow-to-region (point) (1- sepb))
740 (re-search-forward apropos-all-words-regexp nil t))
741 (progn
742 (goto-char (1+ sepa))
743 (setq type (if (eq ?F (preceding-char))
744 2 ; function documentation
745 3) ; variable documentation
746 symbol (read)
747 doc (buffer-substring (1+ (point)) (1- sepb)))
748 (when (apropos-true-hit-doc doc)
749 (or (and (setq apropos-item (assq symbol apropos-accumulator))
750 (setcar (cdr apropos-item)
751 (apropos-score-doc doc)))
752 (setq apropos-item (list symbol
753 (+ (apropos-score-symbol symbol 2)
754 (apropos-score-doc doc))
755 nil nil)
756 apropos-accumulator (cons apropos-item
757 apropos-accumulator)))
758 (when apropos-match-face
759 (setq doc (substitute-command-keys doc))
760 (if (or (string-match apropos-pattern-quoted doc)
761 (string-match apropos-all-words-regexp doc))
762 (put-text-property (match-beginning 0)
763 (match-end 0)
764 'face apropos-match-face doc)))
765 (setcar (nthcdr type apropos-item) doc))))
766 (setq sepa (goto-char sepb)))))
768 (defun apropos-documentation-check-elc-file (file)
769 (if (member file apropos-files-scanned)
771 (let (symbol doc beg end this-is-a-variable)
772 (setq apropos-files-scanned (cons file apropos-files-scanned))
773 (erase-buffer)
774 (insert-file-contents file)
775 (while (search-forward "\n#@" nil t)
776 ;; Read the comment length, and advance over it.
777 (setq end (read)
778 beg (1+ (point))
779 end (+ (point) end -1))
780 (forward-char)
781 (if (save-restriction
782 ;; match ^ and $ relative to doc string
783 (narrow-to-region beg end)
784 (re-search-forward apropos-all-words-regexp nil t))
785 (progn
786 (goto-char (+ end 2))
787 (setq doc (buffer-substring beg end)
788 end (- (match-end 0) beg)
789 beg (- (match-beginning 0) beg))
790 (when (apropos-true-hit-doc doc)
791 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
792 symbol (progn
793 (skip-chars-forward "(a-z")
794 (forward-char)
795 (read))
796 symbol (if (consp symbol)
797 (nth 1 symbol)
798 symbol))
799 (if (if this-is-a-variable
800 (get symbol 'variable-documentation)
801 (and (fboundp symbol) (apropos-safe-documentation symbol)))
802 (progn
803 (or (and (setq apropos-item (assq symbol apropos-accumulator))
804 (setcar (cdr apropos-item)
805 (+ (cadr apropos-item) (apropos-score-doc doc))))
806 (setq apropos-item (list symbol
807 (+ (apropos-score-symbol symbol 2)
808 (apropos-score-doc doc))
809 nil nil)
810 apropos-accumulator (cons apropos-item
811 apropos-accumulator)))
812 (when apropos-match-face
813 (setq doc (substitute-command-keys doc))
814 (if (or (string-match apropos-pattern-quoted doc)
815 (string-match apropos-all-words-regexp doc))
816 (put-text-property (match-beginning 0)
817 (match-end 0)
818 'face apropos-match-face doc)))
819 (setcar (nthcdr (if this-is-a-variable 3 2)
820 apropos-item)
821 doc))))))))))
825 (defun apropos-safe-documentation (function)
826 "Like `documentation', except it avoids calling `get_doc_string'.
827 Will return nil instead."
828 (while (and function (symbolp function))
829 (setq function (if (fboundp function)
830 (symbol-function function))))
831 (if (eq (car-safe function) 'macro)
832 (setq function (cdr function)))
833 (setq function (if (byte-code-function-p function)
834 (if (> (length function) 4)
835 (aref function 4))
836 (if (eq (car-safe function) 'autoload)
837 (nth 2 function)
838 (if (eq (car-safe function) 'lambda)
839 (if (stringp (nth 2 function))
840 (nth 2 function)
841 (if (stringp (nth 3 function))
842 (nth 3 function)))))))
843 (if (integerp function)
845 function))
848 (defun apropos-print (do-keys spacing &optional text nosubst)
849 "Output result of apropos searching into buffer `*Apropos*'.
850 The value of `apropos-accumulator' is the list of items to output.
851 Each element should have the format
852 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
853 The return value is the list that was in `apropos-accumulator', sorted
854 alphabetically by symbol name; but this function also sets
855 `apropos-accumulator' to nil before returning.
857 If SPACING is non-nil, it should be a string; separate items with that string.
858 If non-nil TEXT is a string that will be printed as a heading."
859 (if (null apropos-accumulator)
860 (message "No apropos matches for `%s'" apropos-pattern)
861 (setq apropos-accumulator
862 (sort apropos-accumulator
863 (lambda (a b)
864 ;; Don't sort by score if user can't see the score.
865 ;; It would be confusing. -- rms.
866 (if apropos-sort-by-scores
867 (or (> (cadr a) (cadr b))
868 (and (= (cadr a) (cadr b))
869 (string-lessp (car a) (car b))))
870 (string-lessp (car a) (car b))))))
871 (with-output-to-temp-buffer "*Apropos*"
872 (let ((p apropos-accumulator)
873 (old-buffer (current-buffer))
874 symbol item)
875 (set-buffer standard-output)
876 (apropos-mode)
877 (if (display-mouse-p)
878 (insert
879 "If moving the mouse over text changes the text's color, "
880 "you can click\n"
881 "mouse-2 (second button from right) on that text to "
882 "get more information.\n"))
883 (insert "In this buffer, go to the name of the command, or function,"
884 " or variable,\n"
885 (substitute-command-keys
886 "and type \\[apropos-follow] to get full documentation.\n\n"))
887 (if text (insert text "\n\n"))
888 (while (consp p)
889 (when (and spacing (not (bobp)))
890 (princ spacing))
891 (setq apropos-item (car p)
892 symbol (car apropos-item)
893 p (cdr p))
894 ;; Insert dummy score element for backwards compatibility with 21.x
895 ;; apropos-item format.
896 (if (not (numberp (cadr apropos-item)))
897 (setq apropos-item
898 (cons (car apropos-item)
899 (cons nil (cdr apropos-item)))))
900 (insert-text-button (symbol-name symbol)
901 'type 'apropos-symbol
902 ;; Can't use default, since user may have
903 ;; changed the variable!
904 ;; Just say `no' to variables containing faces!
905 'face apropos-symbol-face)
906 (if (and (eq apropos-sort-by-scores 'verbose)
907 (cadr apropos-item))
908 (insert " (" (number-to-string (cadr apropos-item)) ") "))
909 ;; Calculate key-bindings if we want them.
910 (and do-keys
911 (commandp symbol)
912 (not (eq symbol 'self-insert-command))
913 (indent-to 30 1)
914 (if (let ((keys
915 (save-excursion
916 (set-buffer old-buffer)
917 (where-is-internal symbol)))
918 filtered)
919 ;; Copy over the list of key sequences,
920 ;; omitting any that contain a buffer or a frame.
921 (while keys
922 (let ((key (car keys))
923 (i 0)
924 loser)
925 (while (< i (length key))
926 (if (or (framep (aref key i))
927 (bufferp (aref key i)))
928 (setq loser t))
929 (setq i (1+ i)))
930 (or loser
931 (setq filtered (cons key filtered))))
932 (setq keys (cdr keys)))
933 (setq item filtered))
934 ;; Convert the remaining keys to a string and insert.
935 (insert
936 (mapconcat
937 (lambda (key)
938 (setq key (condition-case ()
939 (key-description key)
940 (error)))
941 (if apropos-keybinding-face
942 (put-text-property 0 (length key)
943 'face apropos-keybinding-face
944 key))
945 key)
946 item ", "))
947 (insert "M-x ... RET")
948 (when apropos-keybinding-face
949 (put-text-property (- (point) 11) (- (point) 8)
950 'face apropos-keybinding-face)
951 (put-text-property (- (point) 3) (point)
952 'face apropos-keybinding-face))))
953 (terpri)
954 (apropos-print-doc 2
955 (if (commandp symbol)
956 'apropos-command
957 (if (apropos-macrop symbol)
958 'apropos-macro
959 'apropos-function))
960 (not nosubst))
961 (apropos-print-doc 3 'apropos-variable (not nosubst))
962 (apropos-print-doc 7 'apropos-group t)
963 (apropos-print-doc 6 'apropos-face t)
964 (apropos-print-doc 5 'apropos-widget t)
965 (apropos-print-doc 4 'apropos-plist nil))
966 (setq buffer-read-only t))))
967 (prog1 apropos-accumulator
968 (setq apropos-accumulator ()))) ; permit gc
971 (defun apropos-macrop (symbol)
972 "Return t if SYMBOL is a Lisp macro."
973 (and (fboundp symbol)
974 (consp (setq symbol
975 (symbol-function symbol)))
976 (or (eq (car symbol) 'macro)
977 (if (eq (car symbol) 'autoload)
978 (memq (nth 4 symbol)
979 '(macro t))))))
982 (defun apropos-print-doc (i type do-keys)
983 (if (stringp (setq i (nth i apropos-item)))
984 (progn
985 (insert " ")
986 (insert-text-button (button-type-get type 'apropos-label)
987 'type type
988 ;; Can't use the default button face, since
989 ;; user may have changed the variable!
990 ;; Just say `no' to variables containing faces!
991 'face apropos-label-face
992 'apropos-symbol (car apropos-item))
993 (insert ": ")
994 (insert (if do-keys (substitute-command-keys i) i))
995 (or (bolp) (terpri)))))
998 (defun apropos-follow ()
999 "Invokes any button at point, otherwise invokes the nearest label button."
1000 (interactive)
1001 (button-activate
1002 (or (apropos-next-label-button (line-beginning-position))
1003 (error "There is nothing to follow here"))))
1006 (defun apropos-describe-plist (symbol)
1007 "Display a pretty listing of SYMBOL's plist."
1008 (help-setup-xref (list 'apropos-describe-plist symbol) (interactive-p))
1009 (with-help-window (help-buffer)
1010 (set-buffer standard-output)
1011 (princ "Symbol ")
1012 (prin1 symbol)
1013 (princ "'s plist is\n (")
1014 (if apropos-symbol-face
1015 (put-text-property (+ (point-min) 7) (- (point) 14)
1016 'face apropos-symbol-face))
1017 (insert (apropos-format-plist symbol "\n "))
1018 (princ ")")))
1021 (provide 'apropos)
1023 ;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
1024 ;;; apropos.el ends here