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>
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/>.
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>.
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.
60 (eval-when-compile (require 'cl
))
63 "Apropos commands for users and programmers."
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."
76 (defcustom apropos-symbol-face
'bold
77 "*Face for symbol name in Apropos output, or nil for none."
81 (defcustom apropos-keybinding-face
'underline
82 "*Face for lists of keybinding in Apropos output, or nil for none."
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."
93 (defcustom apropos-property-face
'bold-italic
94 "*Face for property name in apropos output, or nil for none."
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."
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."
110 :type
'(choice (const :tag
"off" nil
)
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."
119 :type
'(choice (const :tag
"off" nil
)
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
)
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")
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"
182 'action
#'apropos-symbol-button-display-help
185 (defun apropos-symbol-button-display-help (button)
186 "Display further help for the `apropos-symbol' button BUTTON."
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"
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"
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"
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"
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"
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"
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"
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"
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
))))
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
)))
269 (setq label
(button-get button
'apropos-label
))
270 (setq type
(button-get button
'type
))))
274 (defun apropos-words-to-regexp (words wild
)
275 "Make regexp matching any two of the words in WORDS."
277 (mapconcat 'identity words
"\\|")
282 (mapconcat 'identity words
"\\|")
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."
295 (read-string (concat "Apropos " subject
" (word list or regexp): "))))
296 (if (string-equal (regexp-quote pattern
) pattern
)
298 (split-string pattern
"[ \t]+")
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
)
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
))
318 (let ((syn apropos-synonyms
) (s word
) (a word
))
320 (if (member word
(car syn
))
322 (setq a
(mapconcat 'identity
(car syn
) "\\|"))
323 (if (member word
(cdr (car syn
)))
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
".+"))
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."
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."
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
)))
365 (when (setq i
(string-match apropos-pattern-quoted doc
))
367 (dolist (s (apropos-calc-scores doc apropos-all-words
) score
)
368 (setq score
(+ score
50 (/ (* (- l s
) 50) l
)))))
371 (defun apropos-score-symbol (symbol &optional weight
)
372 "Return apropos score for SYMBOL."
373 (setq symbol
(symbol-name 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)."
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}")
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
415 (interactive (list (apropos-read-pattern
416 (if (or current-prefix-arg apropos-do-all
)
417 "variable" "user option"))
419 (apropos-command pattern nil
420 (if (or do-all apropos-do-all
)
423 (get symbol
'variable-documentation
)))
426 ;; For auld lang syne:
428 (defalias 'command-apropos
'apropos-command
)
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"))
449 (apropos-parse-pattern pattern
)
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
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
))
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
)
471 (setq symbol
(car p
))
472 (setq score
(apropos-score-symbol symbol
))
473 (unless var-predicate
475 (if (setq doc
(documentation symbol t
))
477 (setq score
(+ score
(apropos-score-doc doc
)))
478 (substring doc
0 (string-match "\n" doc
)))
479 "(not documented)")))
481 (funcall var-predicate symbol
)
482 (if (setq doc
(documentation-property
483 symbol
'variable-documentation t
))
485 (setq score
(+ score
(apropos-score-doc doc
)))
487 (string-match "\n" doc
)))))))
488 (setcar (cdr (car p
)) score
)
490 (and (apropos-print t nil nil t
)
492 (message "%s" message
))))
496 (defun apropos-documentation-property (symbol property raw
)
497 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
499 (let ((doc (documentation-property symbol property raw
)))
500 (if doc
(substring doc
0 (string-match "\n" doc
))
502 (error "(error retrieving documentation)")))
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")
522 (apropos-parse-pattern pattern
)
523 (apropos-symbols-internal
524 (apropos-internal apropos-regexp
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.
537 (dolist (symbol symbols
)
538 (unless (get symbol
'apropos-inhibit
)
541 (let ((apropos-accumulator
544 (let (doc properties
)
547 (apropos-score-symbol symbol
)
548 (when (fboundp symbol
)
549 (if (setq doc
(condition-case nil
550 (documentation symbol t
)
552 "(alias for undefined function)")
554 "(can't retrieve function documentation)")))
555 (substring doc
0 (string-match "\n" doc
))
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
))
569 (apropos-documentation-property
570 symbol
'face-documentation t
))
571 (when (get symbol
'custom-group
)
572 (apropos-documentation-property
573 symbol
'group-documentation t
)))))
575 (apropos-print keys nil text
)))
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")
591 (apropos-parse-pattern pattern
)
592 (or do-all
(setq do-all apropos-do-all
))
593 (setq apropos-accumulator
())
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
603 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
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
)
609 (if (apropos-false-hit-str f
)
611 (if (apropos-false-hit-str p
)
614 (setq apropos-accumulator
(cons (list symbol
615 (+ (apropos-score-str f
)
616 (apropos-score-str v
)
617 (apropos-score-str p
))
619 apropos-accumulator
))))))
620 (apropos-print nil
"\n----------------\n"))
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
634 Returns list of symbols and documentation found."
635 (interactive (list (apropos-read-pattern "documentation")
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
)
645 (set-buffer standard-input
)
646 (apropos-documentation-check-doc-file)
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
))
658 (if (setq apropos-item
659 (cdr (assq symbol apropos-accumulator
)))
663 (setcar (nthcdr 1 apropos-item
) f
)
664 (setcar apropos-item
(+ (car apropos-item
) sf
))))
667 (setcar (nthcdr 2 apropos-item
) v
)
668 (setcar apropos-item
(+ (car apropos-item
) sv
)))))
669 (setq apropos-accumulator
671 (+ (apropos-score-symbol symbol
2) sf sv
)
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
)
681 (setq symbol
(prin1-to-string (funcall function symbol
)))
682 (if (string-match apropos-regexp symbol
)
684 (if apropos-match-face
685 (put-text-property (match-beginning 0) (match-end 0)
686 'face apropos-match-face
690 (defun apropos-documentation-internal (doc)
692 (apropos-documentation-check-elc-file (car 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)
702 'face apropos-match-face doc
))
705 (defun apropos-format-plist (pl sep
&optional compare
)
706 (setq pl
(symbol-plist 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
))
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
)))
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
)
732 (insert-file-contents (concat doc-directory internal-doc-file-name
))
734 (while (save-excursion
735 (setq sepb
(search-forward "\^_"))
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
))
742 (goto-char (1+ sepa
))
743 (setq type
(if (eq ?F
(preceding-char))
744 2 ; function documentation
745 3) ; variable documentation
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
))
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)
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
))
774 (insert-file-contents file
)
775 (while (search-forward "\n#@" nil t
)
776 ;; Read the comment length, and advance over it.
779 end
(+ (point) end -
1))
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
))
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\\) ")
793 (skip-chars-forward "(a-z")
796 symbol
(if (consp symbol
)
799 (if (if this-is-a-variable
800 (get symbol
'variable-documentation
)
801 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
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
))
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)
818 'face apropos-match-face doc
)))
819 (setcar (nthcdr (if this-is-a-variable
3 2)
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)
836 (if (eq (car-safe function
) 'autoload
)
838 (if (eq (car-safe function
) 'lambda
)
839 (if (stringp (nth 2 function
))
841 (if (stringp (nth 3 function
))
842 (nth 3 function
)))))))
843 (if (integerp 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
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))
875 (set-buffer standard-output
)
877 (if (display-mouse-p)
879 "If moving the mouse over text changes the text's color, "
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,"
885 (substitute-command-keys
886 "and type \\[apropos-follow] to get full documentation.\n\n"))
887 (if text
(insert text
"\n\n"))
889 (when (and spacing
(not (bobp)))
891 (setq apropos-item
(car p
)
892 symbol
(car apropos-item
)
894 ;; Insert dummy score element for backwards compatibility with 21.x
895 ;; apropos-item format.
896 (if (not (numberp (cadr 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
)
908 (insert " (" (number-to-string (cadr apropos-item
)) ") "))
909 ;; Calculate key-bindings if we want them.
912 (not (eq symbol
'self-insert-command
))
916 (set-buffer old-buffer
)
917 (where-is-internal symbol
)))
919 ;; Copy over the list of key sequences,
920 ;; omitting any that contain a buffer or a frame.
922 (let ((key (car keys
))
925 (while (< i
(length key
))
926 (if (or (framep (aref key i
))
927 (bufferp (aref key i
)))
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.
938 (setq key
(condition-case ()
939 (key-description key
)
941 (if apropos-keybinding-face
942 (put-text-property 0 (length key
)
943 'face apropos-keybinding-face
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
))))
955 (if (commandp symbol
)
957 (if (apropos-macrop symbol
)
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
)
975 (symbol-function symbol
)))
976 (or (eq (car symbol
) 'macro
)
977 (if (eq (car symbol
) 'autoload
)
982 (defun apropos-print-doc (i type do-keys
)
983 (if (stringp (setq i
(nth i apropos-item
)))
986 (insert-text-button (button-type-get type
'apropos-label
)
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
))
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."
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
)
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 "))
1023 ;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
1024 ;;; apropos.el ends here