1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
7 ;; Rewritten: Daniel Pfeiffer <occitan@esperanto.org>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; The ideas for this package were derived from the C code in
30 ;; src/keymap.c and elsewhere. The functions in this file should
31 ;; always be byte-compiled for speed. Someone should rewrite this in
32 ;; C (as part of src/keymap.c) for speed.
34 ;; The idea for super-apropos is based on the original implementation
35 ;; by Lynn Slater <lrs@esl.com>.
38 ;; Fixed bug, current-local-map can return nil.
39 ;; Change, doesn't calculate key-bindings unless needed.
40 ;; Added super-apropos capability, changed print functions.
41 ;;; Made fast-apropos and super-apropos share code.
42 ;;; Sped up fast-apropos again.
43 ;; Added apropos-do-all option.
44 ;;; Added fast-command-apropos.
45 ;; Changed doc strings to comments for helping functions.
46 ;;; Made doc file buffer read-only, buried it.
47 ;; Only call substitute-command-keys if do-all set.
49 ;; Optionally use configurable faces to make the output more legible.
50 ;; Differentiate between command, function and macro.
51 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
52 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
53 ;; Apropos-documentation (ex super-apropos) now finds all it should.
54 ;; New apropos-value snoops through all values and optionally plists.
55 ;; Reading DOC file doesn't load nroff.
56 ;; Added hypertext following of documentation, mouse-2 on variable gives value
57 ;; from buffer in active window.
62 (eval-when-compile (require 'cl
))
65 "Apropos commands for users and programmers."
69 ;; I see a degradation of maybe 10-20% only.
70 (defcustom apropos-do-all nil
71 "*Whether the apropos commands should do more.
73 Slows them down more or less. Set this non-nil if you have a fast machine."
78 (defcustom apropos-symbol-face
'bold
79 "*Face for symbol name in Apropos output, or nil for none."
83 (defcustom apropos-keybinding-face
'underline
84 "*Face for lists of keybinding in Apropos output, or nil for none."
88 (defcustom apropos-label-face
'italic
89 "*Face for label (`Command', `Variable' ...) in Apropos output.
90 A value of nil means don't use any special font for them, and also
91 turns off mouse highlighting."
95 (defcustom apropos-property-face
'bold-italic
96 "*Face for property name in apropos output, or nil for none."
100 (defcustom apropos-match-face
'match
101 "*Face for matching text in Apropos documentation/value, or nil for none.
102 This applies when you look for matches in the documentation or variable value
103 for the pattern; the part that matches gets displayed in this font."
107 (defcustom apropos-sort-by-scores nil
108 "*Non-nil means sort matches by scores; best match is shown first.
109 This applies to all `apropos' commands except `apropos-documentation'.
110 If value is `verbose', the computed score is shown for each match."
112 :type
'(choice (const :tag
"off" nil
)
114 (const :tag
"show scores" verbose
)))
116 (defcustom apropos-documentation-sort-by-scores t
117 "*Non-nil means sort matches by scores; best match is shown first.
118 This applies to `apropos-documentation' only.
119 If value is `verbose', the computed score is shown for each match."
121 :type
'(choice (const :tag
"off" nil
)
123 (const :tag
"show scores" verbose
)))
125 (defvar apropos-mode-map
126 (let ((map (make-sparse-keymap)))
127 (set-keymap-parent map button-buffer-map
)
128 ;; Use `apropos-follow' instead of just using the button
129 ;; definition of RET, so that users can use it anywhere in an
130 ;; apropos item, not just on top of a button.
131 (define-key map
"\C-m" 'apropos-follow
)
132 (define-key map
" " 'scroll-up
)
133 (define-key map
"\177" 'scroll-down
)
134 (define-key map
"q" 'quit-window
)
136 "Keymap used in Apropos mode.")
138 (defvar apropos-mode-hook nil
139 "*Hook run when mode is turned on.")
141 (defvar apropos-pattern nil
142 "Apropos pattern as entered by user.")
144 (defvar apropos-pattern-quoted nil
145 "Apropos pattern passed through `regexp-quoute'.")
147 (defvar apropos-words
()
148 "Current list of apropos words extracted from `apropos-pattern'.")
150 (defvar apropos-all-words
()
151 "Current list of words and synonyms.")
153 (defvar apropos-regexp nil
154 "Regexp used in current apropos run.")
156 (defvar apropos-all-words-regexp nil
157 "Regexp matching apropos-all-words.")
159 (defvar apropos-files-scanned
()
160 "List of elc files already scanned in current run of `apropos-documentation'.")
162 (defvar apropos-accumulator
()
163 "Alist of symbols already found in current apropos run.")
165 (defvar apropos-item
()
166 "Current item in or for `apropos-accumulator'.")
168 (defvar apropos-synonyms
'(
169 ("find" "open" "edit")
172 "List of synonyms known by apropos.
173 Each element is a list of words where the first word is the standard emacs
174 term, and the rest of the words are alternative terms.")
177 ;;; Button types used by apropos
179 (define-button-type 'apropos-symbol
180 'face apropos-symbol-face
181 'help-echo
"mouse-2, RET: Display more help on this symbol"
183 'action
#'apropos-symbol-button-display-help
186 (defun apropos-symbol-button-display-help (button)
187 "Display further help for the `apropos-symbol' button BUTTON."
189 (or (apropos-next-label-button (button-start button
))
190 (error "There is nothing to follow for `%s'" (button-label button
)))))
192 (define-button-type 'apropos-function
193 'apropos-label
"Function"
194 'help-echo
"mouse-2, RET: Display more help on this function"
196 'action
(lambda (button)
197 (describe-function (button-get button
'apropos-symbol
))))
199 (define-button-type 'apropos-macro
200 'apropos-label
"Macro"
201 'help-echo
"mouse-2, RET: Display more help on this macro"
203 'action
(lambda (button)
204 (describe-function (button-get button
'apropos-symbol
))))
206 (define-button-type 'apropos-command
207 'apropos-label
"Command"
208 'help-echo
"mouse-2, RET: Display more help on this command"
210 'action
(lambda (button)
211 (describe-function (button-get button
'apropos-symbol
))))
213 ;; We used to use `customize-variable-other-window' instead for a
214 ;; customizable variable, but that is slow. It is better to show an
215 ;; ordinary help buffer and let the user click on the customization
216 ;; button in that buffer, if he wants to.
217 ;; Likewise for `customize-face-other-window'.
218 (define-button-type 'apropos-variable
219 'apropos-label
"Variable"
220 'help-echo
"mouse-2, RET: Display more help on this variable"
222 'action
(lambda (button)
223 (describe-variable (button-get button
'apropos-symbol
))))
225 (define-button-type 'apropos-face
226 'apropos-label
"Face"
227 'help-echo
"mouse-2, RET: Display more help on this face"
229 'action
(lambda (button)
230 (describe-face (button-get button
'apropos-symbol
))))
232 (define-button-type 'apropos-group
233 'apropos-label
"Group"
234 'help-echo
"mouse-2, RET: Display more help on this group"
236 'action
(lambda (button)
237 (customize-group-other-window
238 (button-get button
'apropos-symbol
))))
240 (define-button-type 'apropos-widget
241 'apropos-label
"Widget"
242 'help-echo
"mouse-2, RET: Display more help on this widget"
244 'action
(lambda (button)
245 (widget-browse-other-window (button-get button
'apropos-symbol
))))
247 (define-button-type 'apropos-plist
248 'apropos-label
"Plist"
249 'help-echo
"mouse-2, RET: Display more help on this plist"
251 'action
(lambda (button)
252 (apropos-describe-plist (button-get button
'apropos-symbol
))))
254 (defun apropos-next-label-button (pos)
255 "Return the next apropos label button after POS, or nil if there's none.
256 Will also return nil if more than one `apropos-symbol' button is encountered
257 before finding a label."
258 (let* ((button (next-button pos t
))
259 (already-hit-symbol nil
)
260 (label (and button
(button-get button
'apropos-label
)))
261 (type (and button
(button-get button
'type
))))
264 (or (not (eq type
'apropos-symbol
))
265 (not already-hit-symbol
)))
266 (when (eq type
'apropos-symbol
)
267 (setq already-hit-symbol t
))
268 (setq button
(next-button (button-start button
)))
270 (setq label
(button-get button
'apropos-label
))
271 (setq type
(button-get button
'type
))))
275 (defun apropos-words-to-regexp (words wild
)
276 "Make regexp matching any two of the words in WORDS."
278 (mapconcat 'identity words
"\\|")
283 (mapconcat 'identity words
"\\|")
288 (defun apropos-read-pattern (subject)
289 "Read an apropos pattern, either a word list or a regexp.
290 Returns the user pattern, either a list of words which are matched
291 literally, or a string which is used as a regexp to search for.
293 SUBJECT is a string that is included in the prompt to identify what
294 kind of objects to search."
296 (read-string (concat "Apropos " subject
" (word list or regexp): "))))
297 (if (string-equal (regexp-quote pattern
) pattern
)
299 (split-string pattern
"[ \t]+")
302 (defun apropos-parse-pattern (pattern)
303 "Rewrite a list of words to a regexp matching all permutations.
304 If PATTERN is a string, that means it is already a regexp."
305 (setq apropos-words nil
306 apropos-all-words nil
)
308 ;; We don't actually make a regexp matching all permutations.
309 ;; Instead, for e.g. "a b c", we make a regexp matching
310 ;; any combination of two or more words like this:
311 ;; (a|b|c).*(a|b|c) which may give some false matches,
312 ;; but as long as it also gives the right ones, that's ok.
313 (let ((words pattern
))
314 (setq apropos-pattern
(mapconcat 'identity pattern
" ")
315 apropos-pattern-quoted
(regexp-quote apropos-pattern
))
317 (let ((syn apropos-synonyms
) (s word
) (a word
))
319 (if (member word
(car syn
))
321 (setq a
(mapconcat 'identity
(car syn
) "\\|"))
322 (if (member word
(cdr (car syn
)))
325 (setq syn
(cdr syn
))))
326 (setq apropos-words
(cons s apropos-words
)
327 apropos-all-words
(cons a apropos-all-words
))))
328 (setq apropos-all-words-regexp
(apropos-words-to-regexp apropos-all-words
".+"))
329 (apropos-words-to-regexp apropos-words
".*?"))
330 (setq apropos-pattern-quoted
(regexp-quote pattern
)
331 apropos-all-words-regexp pattern
332 apropos-pattern pattern
)))
335 (defun apropos-calc-scores (str words
)
336 "Return apropos scores for string STR matching WORDS.
337 Value is a list of offsets of the words into the string."
340 (dolist (word words scores
)
341 (if (setq i
(string-match word str
))
342 (setq scores
(cons i scores
))))
343 ;; Return list of start and end position of regexp
344 (and (string-match apropos-pattern str
)
345 (list (match-beginning 0) (match-end 0))))))
347 (defun apropos-score-str (str)
348 "Return apropos score for string STR."
350 (let* ((l (length str
))
351 (score (- (/ l
10))))
352 (dolist (s (apropos-calc-scores str apropos-all-words
) score
)
353 (setq score
(+ score
1000 (/ (* (- l s
) 1000) l
)))))
356 (defun apropos-score-doc (doc)
357 "Return apropos score for documentation string DOC."
358 (let ((l (length doc
)))
361 (when (setq i
(string-match apropos-pattern-quoted doc
))
363 (dolist (s (apropos-calc-scores doc apropos-all-words
) score
)
364 (setq score
(+ score
50 (/ (* (- l s
) 50) l
)))))
367 (defun apropos-score-symbol (symbol &optional weight
)
368 "Return apropos score for SYMBOL."
369 (setq symbol
(symbol-name symbol
))
372 (dolist (s (apropos-calc-scores symbol apropos-words
) (* score
(or weight
3)))
373 (setq score
(+ score
(- 60 l
) (/ (* (- l s
) 60) l
))))))
375 (defun apropos-true-hit (str words
)
376 "Return t if STR is a genuine hit.
377 This may fail if only one of the keywords is matched more than once.
378 This requires that at least 2 keywords (unless only one was given)."
382 (> (length (apropos-calc-scores str words
)) 1)))
384 (defun apropos-false-hit-symbol (symbol)
385 "Return t if SYMBOL is not really matched by the current keywords."
386 (not (apropos-true-hit (symbol-name symbol
) apropos-words
)))
388 (defun apropos-false-hit-str (str)
389 "Return t if STR is not really matched by the current keywords."
390 (not (apropos-true-hit str apropos-words
)))
392 (defun apropos-true-hit-doc (doc)
393 "Return t if DOC is really matched by the current keywords."
394 (apropos-true-hit doc apropos-all-words
))
396 (define-derived-mode apropos-mode fundamental-mode
"Apropos"
397 "Major mode for following hyperlinks in output of apropos commands.
399 \\{apropos-mode-map}")
402 (defun apropos-variable (pattern &optional do-all
)
403 "Show user variables that match PATTERN.
404 PATTERN can be a word, a list of words (separated by spaces),
405 or a regexp (using some regexp special characters). If it is a word,
406 search for matches for that word as a substring. If it is a list of words,
407 search for matches for any two (or more) of those words.
409 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
411 (interactive (list (apropos-read-pattern
412 (if (or current-prefix-arg apropos-do-all
)
413 "variable" "user option"))
415 (apropos-command pattern nil
416 (if (or do-all apropos-do-all
)
419 (get symbol
'variable-documentation
)))
422 ;; For auld lang syne:
424 (defalias 'command-apropos
'apropos-command
)
426 (defun apropos-command (pattern &optional do-all var-predicate
)
427 "Show commands (interactively callable functions) that match PATTERN.
428 PATTERN can be a word, a list of words (separated by spaces),
429 or a regexp (using some regexp special characters). If it is a word,
430 search for matches for that word as a substring. If it is a list of words,
431 search for matches for any two (or more) of those words.
433 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
434 noninteractive functions.
436 If VAR-PREDICATE is non-nil, show only variables, and only those that
437 satisfy the predicate VAR-PREDICATE.
439 When called from a Lisp program, a string PATTERN is used as a regexp,
440 while a list of strings is used as a word list."
441 (interactive (list (apropos-read-pattern
442 (if (or current-prefix-arg apropos-do-all
)
443 "command or function" "command"))
445 (setq apropos-regexp
(apropos-parse-pattern pattern
))
447 (let ((standard-output (get-buffer-create "*Apropos*")))
448 (print-help-return-message 'identity
))))
449 (or do-all
(setq do-all apropos-do-all
))
450 (setq apropos-accumulator
451 (apropos-internal apropos-regexp
453 (if do-all
'functionp
'commandp
))))
454 (let ((tem apropos-accumulator
))
456 (if (or (get (car tem
) 'apropos-inhibit
)
457 (apropos-false-hit-symbol (car tem
)))
458 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
459 (setq tem
(cdr tem
))))
460 (let ((p apropos-accumulator
)
464 (setq symbol
(car p
))
465 (setq score
(apropos-score-symbol symbol
))
466 (unless var-predicate
467 (if (functionp symbol
)
468 (if (setq doc
(documentation symbol t
))
470 (setq score
(+ score
(apropos-score-doc doc
)))
471 (substring doc
0 (string-match "\n" doc
)))
472 "(not documented)")))
474 (funcall var-predicate symbol
)
475 (if (setq doc
(documentation-property
476 symbol
'variable-documentation t
))
478 (setq score
(+ score
(apropos-score-doc doc
)))
480 (string-match "\n" doc
)))))))
481 (setcar (cdr (car p
)) score
)
483 (and (apropos-print t nil nil t
)
485 (message "%s" message
))))
489 (defun apropos-documentation-property (symbol property raw
)
490 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
492 (let ((doc (documentation-property symbol property raw
)))
493 (if doc
(substring doc
0 (string-match "\n" doc
))
495 (error "(error retrieving documentation)")))
499 (defun apropos (pattern &optional do-all
)
500 "Show all bound symbols whose names match PATTERN.
501 PATTERN can be a word, a list of words (separated by spaces),
502 or a regexp (using some regexp special characters). If it is a word,
503 search for matches for that word as a substring. If it is a list of words,
504 search for matches for any two (or more) of those words.
506 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also
507 show unbound symbols and key bindings, which is a little more
508 time-consuming. Returns list of symbols and documentation found."
509 (interactive (list (apropos-read-pattern "symbol")
511 (setq apropos-regexp
(apropos-parse-pattern pattern
))
512 (apropos-symbols-internal
513 (apropos-internal apropos-regexp
520 (symbol-plist symbol
)))))
521 (or do-all apropos-do-all
)))
523 (defun apropos-symbols-internal (symbols keys
&optional text
)
524 ;; Filter out entries that are marked as apropos-inhibit.
526 (dolist (symbol symbols
)
527 (unless (get symbol
'apropos-inhibit
)
530 (let ((apropos-accumulator
533 (let (doc properties
)
536 (apropos-score-symbol symbol
)
537 (when (fboundp symbol
)
538 (if (setq doc
(condition-case nil
539 (documentation symbol t
)
541 "(alias for undefined function)")
543 "(can't retrieve function documentation)")))
544 (substring doc
0 (string-match "\n" doc
))
546 (when (boundp symbol
)
547 (apropos-documentation-property
548 symbol
'variable-documentation t
))
549 (when (setq properties
(symbol-plist symbol
))
550 (setq doc
(list (car properties
)))
551 (while (setq properties
(cdr (cdr properties
)))
552 (setq doc
(cons (car properties
) doc
)))
553 (mapconcat #'symbol-name
(nreverse doc
) " "))
554 (when (get symbol
'widget-type
)
555 (apropos-documentation-property
556 symbol
'widget-documentation t
))
558 (apropos-documentation-property
559 symbol
'face-documentation t
))
560 (when (get symbol
'custom-group
)
561 (apropos-documentation-property
562 symbol
'group-documentation t
)))))
564 (apropos-print keys nil text
)))
568 (defun apropos-value (pattern &optional do-all
)
569 "Show all symbols whose value's printed image matches PATTERN.
570 PATTERN can be a word, a list of words (separated by spaces),
571 or a regexp (using some regexp special characters). If it is a word,
572 search for matches for that word as a substring. If it is a list of words,
573 search for matches for any two (or more) of those words.
575 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
576 at the function and at the names and values of properties.
577 Returns list of symbols and values found."
578 (interactive (list (apropos-read-pattern "value")
580 (setq apropos-regexp
(apropos-parse-pattern pattern
))
581 (or do-all
(setq do-all apropos-do-all
))
582 (setq apropos-accumulator
())
586 (setq f nil v nil p nil
)
587 (or (memq symbol
'(apropos-regexp
588 apropos-pattern apropos-all-words-regexp
589 apropos-words apropos-all-words
590 do-all apropos-accumulator
592 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
594 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
595 p
(apropos-format-plist symbol
"\n " t
)))
596 (if (apropos-false-hit-str v
)
598 (if (apropos-false-hit-str f
)
600 (if (apropos-false-hit-str p
)
603 (setq apropos-accumulator
(cons (list symbol
604 (+ (apropos-score-str f
)
605 (apropos-score-str v
)
606 (apropos-score-str p
))
608 apropos-accumulator
))))))
609 (apropos-print nil
"\n----------------\n"))
613 (defun apropos-documentation (pattern &optional do-all
)
614 "Show symbols whose documentation contain matches for PATTERN.
615 PATTERN can be a word, a list of words (separated by spaces),
616 or a regexp (using some regexp special characters). If it is a word,
617 search for matches for that word as a substring. If it is a list of words,
618 search for matches for any two (or more) of those words.
620 With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
621 documentation that is not stored in the documentation file and show key
623 Returns list of symbols and documentation found."
624 (interactive (list (apropos-read-pattern "documentation")
626 (setq apropos-regexp
(apropos-parse-pattern pattern
))
627 (or do-all
(setq do-all apropos-do-all
))
628 (setq apropos-accumulator
() apropos-files-scanned
())
629 (let ((standard-input (get-buffer-create " apropos-temp"))
630 (apropos-sort-by-scores apropos-documentation-sort-by-scores
)
634 (set-buffer standard-input
)
635 (apropos-documentation-check-doc-file)
639 (setq f
(apropos-safe-documentation symbol
)
640 v
(get symbol
'variable-documentation
))
641 (if (integerp v
) (setq v
))
642 (setq f
(apropos-documentation-internal f
)
643 v
(apropos-documentation-internal v
))
644 (setq sf
(apropos-score-doc f
)
645 sv
(apropos-score-doc v
))
647 (if (setq apropos-item
648 (cdr (assq symbol apropos-accumulator
)))
652 (setcar (nthcdr 1 apropos-item
) f
)
653 (setcar apropos-item
(+ (car apropos-item
) sf
))))
656 (setcar (nthcdr 2 apropos-item
) v
)
657 (setcar apropos-item
(+ (car apropos-item
) sv
)))))
658 (setq apropos-accumulator
660 (+ (apropos-score-symbol symbol
2) sf sv
)
662 apropos-accumulator
)))))))
663 (apropos-print nil
"\n----------------\n" nil t
))
664 (kill-buffer standard-input
))))
667 (defun apropos-value-internal (predicate symbol function
)
668 (if (funcall predicate symbol
)
670 (setq symbol
(prin1-to-string (funcall function symbol
)))
671 (if (string-match apropos-regexp symbol
)
673 (if apropos-match-face
674 (put-text-property (match-beginning 0) (match-end 0)
675 'face apropos-match-face
679 (defun apropos-documentation-internal (doc)
681 (apropos-documentation-check-elc-file (car doc
))
683 (string-match apropos-all-words-regexp doc
)
684 (apropos-true-hit-doc doc
))
685 (when apropos-match-face
686 (setq doc
(substitute-command-keys (copy-sequence doc
)))
687 (if (or (string-match apropos-pattern-quoted doc
)
688 (string-match apropos-all-words-regexp doc
))
689 (put-text-property (match-beginning 0)
691 'face apropos-match-face doc
))
694 (defun apropos-format-plist (pl sep
&optional compare
)
695 (setq pl
(symbol-plist pl
))
698 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
699 (if (or (not compare
) (string-match apropos-regexp p
))
700 (if apropos-property-face
701 (put-text-property 0 (length (symbol-name (car pl
)))
702 'face apropos-property-face p
))
706 (and compare apropos-match-face
707 (put-text-property (match-beginning 0) (match-end 0)
708 'face apropos-match-face
710 (setq p-out
(concat p-out
(if p-out sep
) p
))))
711 (setq pl
(nthcdr 2 pl
)))
715 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
717 (defun apropos-documentation-check-doc-file ()
718 (let (type symbol
(sepa 2) sepb
)
721 (insert-file-contents (concat doc-directory internal-doc-file-name
))
723 (while (save-excursion
724 (setq sepb
(search-forward "\^_"))
726 (beginning-of-line 2)
727 (if (save-restriction
728 (narrow-to-region (point) (1- sepb
))
729 (re-search-forward apropos-all-words-regexp nil t
))
731 (goto-char (1+ sepa
))
732 (setq type
(if (eq ?F
(preceding-char))
733 2 ; function documentation
734 3) ; variable documentation
736 doc
(buffer-substring (1+ (point)) (1- sepb
)))
737 (when (apropos-true-hit-doc doc
)
738 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
739 (setcar (cdr apropos-item
)
740 (apropos-score-doc doc
)))
741 (setq apropos-item
(list symbol
742 (+ (apropos-score-symbol symbol
2)
743 (apropos-score-doc doc
))
745 apropos-accumulator
(cons apropos-item
746 apropos-accumulator
)))
747 (when apropos-match-face
748 (setq doc
(substitute-command-keys doc
))
749 (if (or (string-match apropos-pattern-quoted doc
)
750 (string-match apropos-all-words-regexp doc
))
751 (put-text-property (match-beginning 0)
753 'face apropos-match-face doc
)))
754 (setcar (nthcdr type apropos-item
) doc
))))
755 (setq sepa
(goto-char sepb
)))))
757 (defun apropos-documentation-check-elc-file (file)
758 (if (member file apropos-files-scanned
)
760 (let (symbol doc beg end this-is-a-variable
)
761 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
763 (insert-file-contents file
)
764 (while (search-forward "\n#@" nil t
)
765 ;; Read the comment length, and advance over it.
768 end
(+ (point) end -
1))
770 (if (save-restriction
771 ;; match ^ and $ relative to doc string
772 (narrow-to-region beg end
)
773 (re-search-forward apropos-all-words-regexp nil t
))
775 (goto-char (+ end
2))
776 (setq doc
(buffer-substring beg end
)
777 end
(- (match-end 0) beg
)
778 beg
(- (match-beginning 0) beg
))
779 (when (apropos-true-hit-doc doc
)
780 (setq this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
782 (skip-chars-forward "(a-z")
785 symbol
(if (consp symbol
)
788 (if (if this-is-a-variable
789 (get symbol
'variable-documentation
)
790 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
792 (or (and (setq apropos-item
(assq symbol apropos-accumulator
))
793 (setcar (cdr apropos-item
)
794 (+ (cadr apropos-item
) (apropos-score-doc doc
))))
795 (setq apropos-item
(list symbol
796 (+ (apropos-score-symbol symbol
2)
797 (apropos-score-doc doc
))
799 apropos-accumulator
(cons apropos-item
800 apropos-accumulator
)))
801 (when apropos-match-face
802 (setq doc
(substitute-command-keys doc
))
803 (if (or (string-match apropos-pattern-quoted doc
)
804 (string-match apropos-all-words-regexp doc
))
805 (put-text-property (match-beginning 0)
807 'face apropos-match-face doc
)))
808 (setcar (nthcdr (if this-is-a-variable
3 2)
814 (defun apropos-safe-documentation (function)
815 "Like `documentation', except it avoids calling `get_doc_string'.
816 Will return nil instead."
817 (while (and function
(symbolp function
))
818 (setq function
(if (fboundp function
)
819 (symbol-function function
))))
820 (if (eq (car-safe function
) 'macro
)
821 (setq function
(cdr function
)))
822 (setq function
(if (byte-code-function-p function
)
823 (if (> (length function
) 4)
825 (if (eq (car-safe function
) 'autoload
)
827 (if (eq (car-safe function
) 'lambda
)
828 (if (stringp (nth 2 function
))
830 (if (stringp (nth 3 function
))
831 (nth 3 function
)))))))
832 (if (integerp function
)
837 (defun apropos-print (do-keys spacing
&optional text nosubst
)
838 "Output result of apropos searching into buffer `*Apropos*'.
839 The value of `apropos-accumulator' is the list of items to output.
840 Each element should have the format
841 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
842 The return value is the list that was in `apropos-accumulator', sorted
843 alphabetically by symbol name; but this function also sets
844 `apropos-accumulator' to nil before returning.
846 If SPACING is non-nil, it should be a string; separate items with that string.
847 If non-nil TEXT is a string that will be printed as a heading."
848 (if (null apropos-accumulator
)
849 (message "No apropos matches for `%s'" apropos-pattern
)
850 (setq apropos-accumulator
851 (sort apropos-accumulator
853 ;; Don't sort by score if user can't see the score.
854 ;; It would be confusing. -- rms.
855 (if apropos-sort-by-scores
856 (or (> (cadr a
) (cadr b
))
857 (and (= (cadr a
) (cadr b
))
858 (string-lessp (car a
) (car b
))))
859 (string-lessp (car a
) (car b
))))))
860 (with-output-to-temp-buffer "*Apropos*"
861 (let ((p apropos-accumulator
)
862 (old-buffer (current-buffer))
864 (set-buffer standard-output
)
866 (if (display-mouse-p)
868 "If moving the mouse over text changes the text's color, "
870 "mouse-2 (second button from right) on that text to "
871 "get more information.\n"))
872 (insert "In this buffer, go to the name of the command, or function,"
874 (substitute-command-keys
875 "and type \\[apropos-follow] to get full documentation.\n\n"))
876 (if text
(insert text
"\n\n"))
878 (when (and spacing
(not (bobp)))
880 (setq apropos-item
(car p
)
881 symbol
(car apropos-item
)
883 ;; Insert dummy score element for backwards compatibility with 21.x
884 ;; apropos-item format.
885 (if (not (numberp (cadr apropos-item
)))
887 (cons (car apropos-item
)
888 (cons nil
(cdr apropos-item
)))))
889 (insert-text-button (symbol-name symbol
)
890 'type
'apropos-symbol
891 ;; Can't use default, since user may have
892 ;; changed the variable!
893 ;; Just say `no' to variables containing faces!
894 'face apropos-symbol-face
)
895 (if (and (eq apropos-sort-by-scores
'verbose
)
897 (insert " (" (number-to-string (cadr apropos-item
)) ") "))
898 ;; Calculate key-bindings if we want them.
904 (set-buffer old-buffer
)
905 (where-is-internal symbol
)))
907 ;; Copy over the list of key sequences,
908 ;; omitting any that contain a buffer or a frame.
910 (let ((key (car keys
))
913 (while (< i
(length key
))
914 (if (or (framep (aref key i
))
915 (bufferp (aref key i
)))
919 (setq filtered
(cons key filtered
))))
920 (setq keys
(cdr keys
)))
921 (setq item filtered
))
922 ;; Convert the remaining keys to a string and insert.
926 (setq key
(condition-case ()
927 (key-description key
)
929 (if apropos-keybinding-face
930 (put-text-property 0 (length key
)
931 'face apropos-keybinding-face
935 (insert "M-x ... RET")
936 (when apropos-keybinding-face
937 (put-text-property (- (point) 11) (- (point) 8)
938 'face apropos-keybinding-face
)
939 (put-text-property (- (point) 3) (point)
940 'face apropos-keybinding-face
))))
943 (if (commandp symbol
)
945 (if (apropos-macrop symbol
)
949 (apropos-print-doc 3 'apropos-variable
(not nosubst
))
950 (apropos-print-doc 7 'apropos-group t
)
951 (apropos-print-doc 6 'apropos-face t
)
952 (apropos-print-doc 5 'apropos-widget t
)
953 (apropos-print-doc 4 'apropos-plist nil
))
954 (setq buffer-read-only t
))))
955 (prog1 apropos-accumulator
956 (setq apropos-accumulator
()))) ; permit gc
959 (defun apropos-macrop (symbol)
960 "Return t if SYMBOL is a Lisp macro."
961 (and (fboundp symbol
)
963 (symbol-function symbol
)))
964 (or (eq (car symbol
) 'macro
)
965 (if (eq (car symbol
) 'autoload
)
970 (defun apropos-print-doc (i type do-keys
)
971 (if (stringp (setq i
(nth i apropos-item
)))
974 (insert-text-button (button-type-get type
'apropos-label
)
976 ;; Can't use the default button face, since
977 ;; user may have changed the variable!
978 ;; Just say `no' to variables containing faces!
979 'face apropos-label-face
980 'apropos-symbol
(car apropos-item
))
982 (insert (if do-keys
(substitute-command-keys i
) i
))
983 (or (bolp) (terpri)))))
986 (defun apropos-follow ()
987 "Invokes any button at point, otherwise invokes the nearest label button."
990 (or (apropos-next-label-button (line-beginning-position))
991 (error "There is nothing to follow here"))))
994 (defun apropos-describe-plist (symbol)
995 "Display a pretty listing of SYMBOL's plist."
996 (help-setup-xref (list 'apropos-describe-plist symbol
) (interactive-p))
997 (with-output-to-temp-buffer (help-buffer)
998 (set-buffer standard-output
)
1001 (princ "'s plist is\n (")
1002 (if apropos-symbol-face
1003 (put-text-property (+ (point-min) 7) (- (point) 14)
1004 'face apropos-symbol-face
))
1005 (insert (apropos-format-plist symbol
"\n "))
1007 (print-help-return-message)))
1012 ;;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
1013 ;;; apropos.el ends here