(dired-do-touch, dired-clean-directory, dired-run-shell-command)
[emacs.git] / lisp / apropos.el
blobe5904e73b717fe5eb4fc201fcdcf40743a91b12f
1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989,94,1995,2001,02,03,2004 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Rewritten: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: help
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; The ideas for this package were derived from the C code in
29 ;; src/keymap.c and elsewhere. The functions in this file should
30 ;; always be byte-compiled for speed. Someone should rewrite this in
31 ;; C (as part of src/keymap.c) for speed.
33 ;; The idea for super-apropos is based on the original implementation
34 ;; by Lynn Slater <lrs@esl.com>.
36 ;; History:
37 ;; Fixed bug, current-local-map can return nil.
38 ;; Change, doesn't calculate key-bindings unless needed.
39 ;; Added super-apropos capability, changed print functions.
40 ;;; Made fast-apropos and super-apropos share code.
41 ;;; Sped up fast-apropos again.
42 ;; Added apropos-do-all option.
43 ;;; Added fast-command-apropos.
44 ;; Changed doc strings to comments for helping functions.
45 ;;; Made doc file buffer read-only, buried it.
46 ;; Only call substitute-command-keys if do-all set.
48 ;; Optionally use configurable faces to make the output more legible.
49 ;; Differentiate between command, function and macro.
50 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
51 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
52 ;; Apropos-documentation (ex super-apropos) now finds all it should.
53 ;; New apropos-value snoops through all values and optionally plists.
54 ;; Reading DOC file doesn't load nroff.
55 ;; Added hypertext following of documentation, mouse-2 on variable gives value
56 ;; from buffer in active window.
58 ;;; Code:
60 (require 'button)
61 (eval-when-compile (require 'cl))
63 (defgroup apropos nil
64 "Apropos commands for users and programmers"
65 :group 'help
66 :prefix "apropos")
68 ;; I see a degradation of maybe 10-20% only.
69 (defcustom apropos-do-all nil
70 "*Whether the apropos commands should do more.
72 Slows them down more or less. Set this non-nil if you have a fast machine."
73 :group 'apropos
74 :type 'boolean)
77 (defcustom apropos-symbol-face 'bold
78 "*Face for symbol name in Apropos output, or nil for none."
79 :group 'apropos
80 :type 'face)
82 (defcustom apropos-keybinding-face 'underline
83 "*Face for lists of keybinding in Apropos output, or nil for none."
84 :group 'apropos
85 :type 'face)
87 (defcustom apropos-label-face 'italic
88 "*Face for label (`Command', `Variable' ...) in Apropos output.
89 A value of nil means don't use any special font for them, and also
90 turns off mouse highlighting."
91 :group 'apropos
92 :type 'face)
94 (defcustom apropos-property-face 'bold-italic
95 "*Face for property name in apropos output, or nil for none."
96 :group 'apropos
97 :type 'face)
99 (defcustom apropos-match-face 'secondary-selection
100 "*Face for matching text in Apropos documentation/value, or nil for none.
101 This applies when you look for matches in the documentation or variable value
102 for the regexp; the part that matches gets displayed in this font."
103 :group 'apropos
104 :type 'face)
106 (defcustom apropos-sort-by-scores nil
107 "*Non-nil means sort matches by scores; best match is shown first.
108 The computed score is shown for each match."
109 :group 'apropos
110 :type 'boolean)
112 (defvar apropos-mode-map
113 (let ((map (make-sparse-keymap)))
114 (set-keymap-parent map button-buffer-map)
115 ;; Use `apropos-follow' instead of just using the button
116 ;; definition of RET, so that users can use it anywhere in an
117 ;; apropos item, not just on top of a button.
118 (define-key map "\C-m" 'apropos-follow)
119 (define-key map " " 'scroll-up)
120 (define-key map "\177" 'scroll-down)
121 (define-key map "q" 'quit-window)
122 map)
123 "Keymap used in Apropos mode.")
125 (defvar apropos-mode-hook nil
126 "*Hook run when mode is turned on.")
128 (defvar apropos-regexp nil
129 "Regexp used in current apropos run.")
131 (defvar apropos-orig-regexp nil
132 "Regexp as entered by user.")
134 (defvar apropos-all-regexp nil
135 "Regexp matching apropos-all-words.")
137 (defvar apropos-files-scanned ()
138 "List of elc files already scanned in current run of `apropos-documentation'.")
140 (defvar apropos-accumulator ()
141 "Alist of symbols already found in current apropos run.")
143 (defvar apropos-item ()
144 "Current item in or for `apropos-accumulator'.")
146 (defvar apropos-synonyms '(
147 ("find" "open" "edit")
148 ("kill" "cut")
149 ("yank" "paste"))
150 "List of synonyms known by apropos.
151 Each element is a list of words where the first word is the standard emacs
152 term, and the rest of the words are alternative terms.")
154 (defvar apropos-words ()
155 "Current list of words.")
157 (defvar apropos-all-words ()
158 "Current list of words and synonyms.")
161 ;;; Button types used by apropos
163 (define-button-type 'apropos-symbol
164 'face apropos-symbol-face
165 'help-echo "mouse-2, RET: Display more help on this symbol"
166 'action #'apropos-symbol-button-display-help
167 'skip t)
169 (defun apropos-symbol-button-display-help (button)
170 "Display further help for the `apropos-symbol' button BUTTON."
171 (button-activate
172 (or (apropos-next-label-button (button-start button))
173 (error "There is nothing to follow for `%s'" (button-label button)))))
175 (define-button-type 'apropos-function
176 'apropos-label "Function"
177 'action (lambda (button)
178 (describe-function (button-get button 'apropos-symbol)))
179 'help-echo "mouse-2, RET: Display more help on this function")
180 (define-button-type 'apropos-macro
181 'apropos-label "Macro"
182 'action (lambda (button)
183 (describe-function (button-get button 'apropos-symbol)))
184 'help-echo "mouse-2, RET: Display more help on this macro")
185 (define-button-type 'apropos-command
186 'apropos-label "Command"
187 'action (lambda (button)
188 (describe-function (button-get button 'apropos-symbol)))
189 'help-echo "mouse-2, RET: Display more help on this command")
191 ;; We used to use `customize-variable-other-window' instead for a
192 ;; customizable variable, but that is slow. It is better to show an
193 ;; ordinary help buffer and let the user click on the customization
194 ;; button in that buffer, if he wants to.
195 ;; Likewise for `customize-face-other-window'.
196 (define-button-type 'apropos-variable
197 'apropos-label "Variable"
198 'help-echo "mouse-2, RET: Display more help on this variable"
199 'action (lambda (button)
200 (describe-variable (button-get button 'apropos-symbol))))
202 (define-button-type 'apropos-face
203 'apropos-label "Face"
204 'help-echo "mouse-2, RET: Display more help on this face"
205 'action (lambda (button)
206 (describe-face (button-get button 'apropos-symbol))))
208 (define-button-type 'apropos-group
209 'apropos-label "Group"
210 'help-echo "mouse-2, RET: Display more help on this group"
211 'action (lambda (button)
212 (customize-group-other-window
213 (button-get button 'apropos-symbol))))
215 (define-button-type 'apropos-widget
216 'apropos-label "Widget"
217 'help-echo "mouse-2, RET: Display more help on this widget"
218 'action (lambda (button)
219 (widget-browse-other-window (button-get button 'apropos-symbol))))
221 (define-button-type 'apropos-plist
222 'apropos-label "Plist"
223 'help-echo "mouse-2, RET: Display more help on this plist"
224 'action (lambda (button)
225 (apropos-describe-plist (button-get button 'apropos-symbol))))
227 (defun apropos-next-label-button (pos)
228 "Return the next apropos label button after POS, or nil if there's none.
229 Will also return nil if more than one `apropos-symbol' button is encountered
230 before finding a label."
231 (let* ((button (next-button pos t))
232 (already-hit-symbol nil)
233 (label (and button (button-get button 'apropos-label)))
234 (type (and button (button-get button 'type))))
235 (while (and button
236 (not label)
237 (or (not (eq type 'apropos-symbol))
238 (not already-hit-symbol)))
239 (when (eq type 'apropos-symbol)
240 (setq already-hit-symbol t))
241 (setq button (next-button (button-start button)))
242 (when button
243 (setq label (button-get button 'apropos-label))
244 (setq type (button-get button 'type))))
245 (and label button)))
248 (defun apropos-words-to-regexp (words wild)
249 "Make regexp matching any two of the words in WORDS."
250 (concat "\\("
251 (mapconcat 'identity words "\\|")
252 "\\)"
253 (if (cdr words)
254 (concat wild
255 "\\("
256 (mapconcat 'identity words "\\|")
257 "\\)")
258 "")))
260 (defun apropos-rewrite-regexp (regexp)
261 "Rewrite a list of words to a regexp matching all permutations.
262 If REGEXP is already a regexp, don't modify it."
263 (setq apropos-orig-regexp regexp)
264 (setq apropos-words () apropos-all-words ())
265 (if (string-equal (regexp-quote regexp) regexp)
266 ;; We don't actually make a regexp matching all permutations.
267 ;; Instead, for e.g. "a b c", we make a regexp matching
268 ;; any combination of two or more words like this:
269 ;; (a|b|c).*(a|b|c) which may give some false matches,
270 ;; but as long as it also gives the right ones, that's ok.
271 (let ((words (split-string regexp "[ \t]+")))
272 (dolist (word words)
273 (let ((syn apropos-synonyms) (s word) (a word))
274 (while syn
275 (if (member word (car syn))
276 (progn
277 (setq a (mapconcat 'identity (car syn) "\\|"))
278 (if (member word (cdr (car syn)))
279 (setq s a))
280 (setq syn nil))
281 (setq syn (cdr syn))))
282 (setq apropos-words (cons s apropos-words)
283 apropos-all-words (cons a apropos-all-words))))
284 (setq apropos-all-regexp (apropos-words-to-regexp apropos-all-words ".+"))
285 (apropos-words-to-regexp apropos-words ".*?"))
286 (setq apropos-all-regexp regexp)))
288 (defun apropos-calc-scores (str words)
289 "Return apropos scores for string STR matching WORDS.
290 Value is a list of offsets of the words into the string."
291 (let ((scores ())
293 (if words
294 (dolist (word words scores)
295 (if (setq i (string-match word str))
296 (setq scores (cons i scores))))
297 ;; Return list of start and end position of regexp
298 (string-match apropos-regexp str)
299 (list (match-beginning 0) (match-end 0)))))
301 (defun apropos-score-str (str)
302 "Return apropos score for string STR."
303 (if str
304 (let* (
305 (l (length str))
306 (score (- (/ l 10)))
308 (dolist (s (apropos-calc-scores str apropos-all-words) score)
309 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
312 (defun apropos-score-doc (doc)
313 "Return apropos score for documentation string DOC."
314 (if doc
315 (let ((score 0)
316 (l (length doc))
318 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
319 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
322 (defun apropos-score-symbol (symbol &optional weight)
323 "Return apropos score for SYMBOL."
324 (setq symbol (symbol-name symbol))
325 (let ((score 0)
326 (l (length symbol))
328 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
329 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
331 (defun apropos-true-hit (str words)
332 "Return t if STR is a genuine hit.
333 This may fail if only one of the keywords is matched more than once.
334 This requires that at least 2 keywords (unless only one was given)."
335 (or (not str)
336 (not words)
337 (not (cdr words))
338 (> (length (apropos-calc-scores str words)) 1)))
340 (defun apropos-false-hit-symbol (symbol)
341 "Return t if SYMBOL is not really matched by the current keywords."
342 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
344 (defun apropos-false-hit-str (str)
345 "Return t if STR is not really matched by the current keywords."
346 (not (apropos-true-hit str apropos-words)))
348 (defun apropos-true-hit-doc (doc)
349 "Return t if DOC is really matched by the current keywords."
350 (apropos-true-hit doc apropos-all-words))
352 (define-derived-mode apropos-mode fundamental-mode "Apropos"
353 "Major mode for following hyperlinks in output of apropos commands.
355 \\{apropos-mode-map}")
357 ;;;###autoload
358 (defun apropos-variable (regexp &optional do-all)
359 "Show user variables that match REGEXP.
360 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also show
361 normal variables."
362 (interactive (list (read-string
363 (concat "Apropos "
364 (if (or current-prefix-arg apropos-do-all)
365 "variable"
366 "user option")
367 " (regexp or words): "))
368 current-prefix-arg))
369 (apropos-command regexp nil
370 (if (or do-all apropos-do-all)
371 #'(lambda (symbol)
372 (and (boundp symbol)
373 (get symbol 'variable-documentation)))
374 'user-variable-p)))
376 ;; For auld lang syne:
377 ;;;###autoload
378 (defalias 'command-apropos 'apropos-command)
379 ;;;###autoload
380 (defun apropos-command (apropos-regexp &optional do-all var-predicate)
381 "Show commands (interactively callable functions) that match APROPOS-REGEXP.
382 With optional prefix DO-ALL, or if `apropos-do-all' is non-nil, also show
383 noninteractive functions.
385 If VAR-PREDICATE is non-nil, show only variables, and only those that
386 satisfy the predicate VAR-PREDICATE."
387 (interactive (list (read-string (concat
388 "Apropos command "
389 (if (or current-prefix-arg
390 apropos-do-all)
391 "or function ")
392 "(regexp or words): "))
393 current-prefix-arg))
394 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
395 (let ((message
396 (let ((standard-output (get-buffer-create "*Apropos*")))
397 (print-help-return-message 'identity))))
398 (or do-all (setq do-all apropos-do-all))
399 (setq apropos-accumulator
400 (apropos-internal apropos-regexp
401 (or var-predicate
402 (if do-all 'functionp 'commandp))))
403 (let ((tem apropos-accumulator))
404 (while tem
405 (if (or (get (car tem) 'apropos-inhibit)
406 (apropos-false-hit-symbol (car tem)))
407 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
408 (setq tem (cdr tem))))
409 (let ((p apropos-accumulator)
410 doc symbol score)
411 (while p
412 (setcar p (list
413 (setq symbol (car p))
414 (setq score (apropos-score-symbol symbol))
415 (unless var-predicate
416 (if (functionp symbol)
417 (if (setq doc (documentation symbol t))
418 (progn
419 (setq score (+ score (apropos-score-doc doc)))
420 (substring doc 0 (string-match "\n" doc)))
421 "(not documented)")))
422 (and var-predicate
423 (funcall var-predicate symbol)
424 (if (setq doc (documentation-property
425 symbol 'variable-documentation t))
426 (progn
427 (setq score (+ score (apropos-score-doc doc)))
428 (substring doc 0
429 (string-match "\n" doc)))))))
430 (setcar (cdr (car p)) score)
431 (setq p (cdr p))))
432 (and (apropos-print t nil)
433 message
434 (message message))))
437 ;;;###autoload
438 (defun apropos-documentation-property (symbol property raw)
439 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
440 (condition-case ()
441 (let ((doc (documentation-property symbol property raw)))
442 (if doc (substring doc 0 (string-match "\n" doc))
443 "(not documented)"))
444 (error "(error retrieving documentation)")))
447 ;;;###autoload
448 (defun apropos (apropos-regexp &optional do-all)
449 "Show all bound symbols whose names match APROPOS-REGEXP.
450 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also
451 show unbound symbols and key bindings, which is a little more
452 time-consuming. Returns list of symbols and documentation found."
453 (interactive "sApropos symbol (regexp or words): \nP")
454 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
455 (apropos-symbols-internal
456 (apropos-internal apropos-regexp
457 (and (not do-all)
458 (not apropos-do-all)
459 (lambda (symbol)
460 (or (fboundp symbol)
461 (boundp symbol)
462 (facep symbol)
463 (symbol-plist symbol)))))
464 (or do-all apropos-do-all)))
466 (defun apropos-symbols-internal (symbols keys &optional text)
467 ;; Filter out entries that are marked as apropos-inhibit.
468 (let ((all nil))
469 (dolist (symbol symbols)
470 (unless (get symbol 'apropos-inhibit)
471 (push symbol all)))
472 (setq symbols all))
473 (let ((apropos-accumulator
474 (mapcar
475 (lambda (symbol)
476 (let (doc properties)
477 (list
478 symbol
479 (apropos-score-symbol symbol)
480 (when (fboundp symbol)
481 (if (setq doc (condition-case nil
482 (documentation symbol t)
483 (void-function
484 "(alias for undefined function)")
485 (error
486 "(can't retrieve function documentation)")))
487 (substring doc 0 (string-match "\n" doc))
488 "(not documented)"))
489 (when (boundp symbol)
490 (apropos-documentation-property
491 symbol 'variable-documentation t))
492 (when (setq properties (symbol-plist symbol))
493 (setq doc (list (car properties)))
494 (while (setq properties (cdr (cdr properties)))
495 (setq doc (cons (car properties) doc)))
496 (mapconcat #'symbol-name (nreverse doc) " "))
497 (when (get symbol 'widget-type)
498 (apropos-documentation-property
499 symbol 'widget-documentation t))
500 (when (facep symbol)
501 (apropos-documentation-property
502 symbol 'face-documentation t))
503 (when (get symbol 'custom-group)
504 (apropos-documentation-property
505 symbol 'group-documentation t)))))
506 symbols)))
507 (apropos-print keys nil text)))
510 ;;;###autoload
511 (defun apropos-value (apropos-regexp &optional do-all)
512 "Show all symbols whose value's printed image matches APROPOS-REGEXP.
513 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also looks
514 at the function and at the names and values of properties.
515 Returns list of symbols and values found."
516 (interactive "sApropos value (regexp or words): \nP")
517 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
518 (or do-all (setq do-all apropos-do-all))
519 (setq apropos-accumulator ())
520 (let (f v p)
521 (mapatoms
522 (lambda (symbol)
523 (setq f nil v nil p nil)
524 (or (memq symbol '(apropos-regexp
525 apropos-orig-regexp apropos-all-regexp
526 apropos-words apropos-all-words
527 do-all apropos-accumulator
528 symbol f v p))
529 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
530 (if do-all
531 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
532 p (apropos-format-plist symbol "\n " t)))
533 (if (apropos-false-hit-str v)
534 (setq v nil))
535 (if (apropos-false-hit-str f)
536 (setq f nil))
537 (if (apropos-false-hit-str p)
538 (setq p nil))
539 (if (or f v p)
540 (setq apropos-accumulator (cons (list symbol
541 (+ (apropos-score-str f)
542 (apropos-score-str v)
543 (apropos-score-str p))
544 f v p)
545 apropos-accumulator))))))
546 (apropos-print nil "\n----------------\n"))
549 ;;;###autoload
550 (defun apropos-documentation (apropos-regexp &optional do-all)
551 "Show symbols whose documentation contain matches for APROPOS-REGEXP.
552 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also use
553 documentation that is not stored in the documentation file and show key
554 bindings.
555 Returns list of symbols and documentation found."
556 (interactive "sApropos documentation (regexp or words): \nP")
557 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
558 (or do-all (setq do-all apropos-do-all))
559 (setq apropos-accumulator () apropos-files-scanned ())
560 (let ((standard-input (get-buffer-create " apropos-temp"))
561 f v sf sv)
562 (unwind-protect
563 (save-excursion
564 (set-buffer standard-input)
565 (apropos-documentation-check-doc-file)
566 (if do-all
567 (mapatoms
568 (lambda (symbol)
569 (setq f (apropos-safe-documentation symbol)
570 v (get symbol 'variable-documentation))
571 (if (integerp v) (setq v))
572 (setq f (apropos-documentation-internal f)
573 v (apropos-documentation-internal v))
574 (setq sf (apropos-score-doc f)
575 sv (apropos-score-doc v))
576 (if (or f v)
577 (if (setq apropos-item
578 (cdr (assq symbol apropos-accumulator)))
579 (progn
580 (if f
581 (progn
582 (setcar (nthcdr 1 apropos-item) f)
583 (setcar apropos-item (+ (car apropos-item) sf))))
584 (if v
585 (progn
586 (setcar (nthcdr 2 apropos-item) v)
587 (setcar apropos-item (+ (car apropos-item) sv)))))
588 (setq apropos-accumulator
589 (cons (list symbol
590 (+ (apropos-score-symbol symbol 2) sf sv)
591 f v)
592 apropos-accumulator)))))))
593 (apropos-print nil "\n----------------\n"))
594 (kill-buffer standard-input))))
597 (defun apropos-value-internal (predicate symbol function)
598 (if (funcall predicate symbol)
599 (progn
600 (setq symbol (prin1-to-string (funcall function symbol)))
601 (if (string-match apropos-regexp symbol)
602 (progn
603 (if apropos-match-face
604 (put-text-property (match-beginning 0) (match-end 0)
605 'face apropos-match-face
606 symbol))
607 symbol)))))
609 (defun apropos-documentation-internal (doc)
610 (if (consp doc)
611 (apropos-documentation-check-elc-file (car doc))
612 (and doc
613 (string-match apropos-all-regexp doc)
614 (save-match-data (apropos-true-hit-doc doc))
615 (progn
616 (if apropos-match-face
617 (put-text-property (match-beginning 0)
618 (match-end 0)
619 'face apropos-match-face
620 (setq doc (copy-sequence doc))))
621 doc))))
623 (defun apropos-format-plist (pl sep &optional compare)
624 (setq pl (symbol-plist pl))
625 (let (p p-out)
626 (while pl
627 (setq p (format "%s %S" (car pl) (nth 1 pl)))
628 (if (or (not compare) (string-match apropos-regexp p))
629 (if apropos-property-face
630 (put-text-property 0 (length (symbol-name (car pl)))
631 'face apropos-property-face p))
632 (setq p nil))
633 (if p
634 (progn
635 (and compare apropos-match-face
636 (put-text-property (match-beginning 0) (match-end 0)
637 'face apropos-match-face
639 (setq p-out (concat p-out (if p-out sep) p))))
640 (setq pl (nthcdr 2 pl)))
641 p-out))
644 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
646 (defun apropos-documentation-check-doc-file ()
647 (let (type symbol (sepa 2) sepb beg end)
648 (insert ?\^_)
649 (backward-char)
650 (insert-file-contents (concat doc-directory internal-doc-file-name))
651 (forward-char)
652 (while (save-excursion
653 (setq sepb (search-forward "\^_"))
654 (not (eobp)))
655 (beginning-of-line 2)
656 (if (save-restriction
657 (narrow-to-region (point) (1- sepb))
658 (re-search-forward apropos-all-regexp nil t))
659 (progn
660 (setq beg (match-beginning 0)
661 end (point))
662 (goto-char (1+ sepa))
663 (setq type (if (eq ?F (preceding-char))
664 2 ; function documentation
665 3) ; variable documentation
666 symbol (read)
667 beg (- beg (point) 1)
668 end (- end (point) 1)
669 doc (buffer-substring (1+ (point)) (1- sepb)))
670 (when (apropos-true-hit-doc doc)
671 (or (and (setq apropos-item (assq symbol apropos-accumulator))
672 (setcar (cdr apropos-item)
673 (+ (cadr apropos-item) (apropos-score-doc doc))))
674 (setq apropos-item (list symbol
675 (+ (apropos-score-symbol symbol 2)
676 (apropos-score-doc doc))
677 nil nil)
678 apropos-accumulator (cons apropos-item
679 apropos-accumulator)))
680 (if apropos-match-face
681 (put-text-property beg end 'face apropos-match-face doc))
682 (setcar (nthcdr type apropos-item) doc))))
683 (setq sepa (goto-char sepb)))))
685 (defun apropos-documentation-check-elc-file (file)
686 (if (member file apropos-files-scanned)
688 (let (symbol doc beg end this-is-a-variable)
689 (setq apropos-files-scanned (cons file apropos-files-scanned))
690 (erase-buffer)
691 (insert-file-contents file)
692 (while (search-forward "\n#@" nil t)
693 ;; Read the comment length, and advance over it.
694 (setq end (read)
695 beg (1+ (point))
696 end (+ (point) end -1))
697 (forward-char)
698 (if (save-restriction
699 ;; match ^ and $ relative to doc string
700 (narrow-to-region beg end)
701 (re-search-forward apropos-all-regexp nil t))
702 (progn
703 (goto-char (+ end 2))
704 (setq doc (buffer-substring beg end)
705 end (- (match-end 0) beg)
706 beg (- (match-beginning 0) beg))
707 (when (apropos-true-hit-doc doc)
708 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
709 symbol (progn
710 (skip-chars-forward "(a-z")
711 (forward-char)
712 (read))
713 symbol (if (consp symbol)
714 (nth 1 symbol)
715 symbol))
716 (if (if this-is-a-variable
717 (get symbol 'variable-documentation)
718 (and (fboundp symbol) (apropos-safe-documentation symbol)))
719 (progn
720 (or (and (setq apropos-item (assq symbol apropos-accumulator))
721 (setcar (cdr apropos-item)
722 (+ (cadr apropos-item) (apropos-score-doc doc))))
723 (setq apropos-item (list symbol
724 (+ (apropos-score-symbol symbol 2)
725 (apropos-score-doc doc))
726 nil nil)
727 apropos-accumulator (cons apropos-item
728 apropos-accumulator)))
729 (if apropos-match-face
730 (put-text-property beg end 'face apropos-match-face
731 doc))
732 (setcar (nthcdr (if this-is-a-variable 3 2)
733 apropos-item)
734 doc))))))))))
738 (defun apropos-safe-documentation (function)
739 "Like `documentation', except it avoids calling `get_doc_string'.
740 Will return nil instead."
741 (while (and function (symbolp function))
742 (setq function (if (fboundp function)
743 (symbol-function function))))
744 (if (eq (car-safe function) 'macro)
745 (setq function (cdr function)))
746 (setq function (if (byte-code-function-p function)
747 (if (> (length function) 4)
748 (aref function 4))
749 (if (eq (car-safe function) 'autoload)
750 (nth 2 function)
751 (if (eq (car-safe function) 'lambda)
752 (if (stringp (nth 2 function))
753 (nth 2 function)
754 (if (stringp (nth 3 function))
755 (nth 3 function)))))))
756 (if (integerp function)
758 function))
761 (defun apropos-print (do-keys spacing &optional text)
762 "Output result of apropos searching into buffer `*Apropos*'.
763 The value of `apropos-accumulator' is the list of items to output.
764 Each element should have the format
765 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
766 The return value is the list that was in `apropos-accumulator', sorted
767 alphabetically by symbol name; but this function also sets
768 `apropos-accumulator' to nil before returning.
770 If SPACING is non-nil, it should be a string; separate items with that string.
771 If non-nil TEXT is a string that will be printed as a heading."
772 (if (null apropos-accumulator)
773 (message "No apropos matches for `%s'" apropos-orig-regexp)
774 (setq apropos-accumulator
775 (sort apropos-accumulator
776 (lambda (a b)
777 ;; Don't sort by score if user can't see the score.
778 ;; It would be confusing. -- rms.
779 (if apropos-sort-by-scores
780 (or (> (cadr a) (cadr b))
781 (and (= (cadr a) (cadr b))
782 (string-lessp (car a) (car b))))
783 (string-lessp (car a) (car b))))))
784 (with-output-to-temp-buffer "*Apropos*"
785 (let ((p apropos-accumulator)
786 (old-buffer (current-buffer))
787 symbol item)
788 (set-buffer standard-output)
789 (apropos-mode)
790 (if (display-mouse-p)
791 (insert
792 "If moving the mouse over text changes the text's color, "
793 "you can click\n"
794 "mouse-2 (second button from right) on that text to "
795 "get more information.\n"))
796 (insert "In this buffer, go to the name of the command, or function,"
797 " or variable,\n"
798 (substitute-command-keys
799 "and type \\[apropos-follow] to get full documentation.\n\n"))
800 (if text (insert text "\n\n"))
801 (while (consp p)
802 (when (and spacing (not (bobp)))
803 (princ spacing))
804 (setq apropos-item (car p)
805 symbol (car apropos-item)
806 p (cdr p))
807 (insert-text-button (symbol-name symbol)
808 'type 'apropos-symbol
809 ;; Can't use default, since user may have
810 ;; changed the variable!
811 ;; Just say `no' to variables containing faces!
812 'face apropos-symbol-face)
813 (if apropos-sort-by-scores
814 (insert " (" (number-to-string (cadr apropos-item)) ") "))
815 ;; Calculate key-bindings if we want them.
816 (and do-keys
817 (commandp symbol)
818 (indent-to 30 1)
819 (if (let ((keys
820 (save-excursion
821 (set-buffer old-buffer)
822 (where-is-internal symbol)))
823 filtered)
824 ;; Copy over the list of key sequences,
825 ;; omitting any that contain a buffer or a frame.
826 (while keys
827 (let ((key (car keys))
828 (i 0)
829 loser)
830 (while (< i (length key))
831 (if (or (framep (aref key i))
832 (bufferp (aref key i)))
833 (setq loser t))
834 (setq i (1+ i)))
835 (or loser
836 (setq filtered (cons key filtered))))
837 (setq keys (cdr keys)))
838 (setq item filtered))
839 ;; Convert the remaining keys to a string and insert.
840 (insert
841 (mapconcat
842 (lambda (key)
843 (setq key (condition-case ()
844 (key-description key)
845 (error)))
846 (if apropos-keybinding-face
847 (put-text-property 0 (length key)
848 'face apropos-keybinding-face
849 key))
850 key)
851 item ", "))
852 (insert "M-x")
853 (put-text-property (- (point) 3) (point)
854 'face apropos-keybinding-face)
855 (insert " " (symbol-name symbol) " ")
856 (insert "RET")
857 (put-text-property (- (point) 3) (point)
858 'face apropos-keybinding-face)))
859 (terpri)
860 (apropos-print-doc 2
861 (if (commandp symbol)
862 'apropos-command
863 (if (apropos-macrop symbol)
864 'apropos-macro
865 'apropos-function))
867 (apropos-print-doc 3 'apropos-variable t)
868 (apropos-print-doc 7 'apropos-group t)
869 (apropos-print-doc 6 'apropos-face t)
870 (apropos-print-doc 5 'apropos-widget t)
871 (apropos-print-doc 4 'apropos-plist nil))
872 (setq buffer-read-only t))))
873 (prog1 apropos-accumulator
874 (setq apropos-accumulator ()))) ; permit gc
877 (defun apropos-macrop (symbol)
878 "T if SYMBOL is a Lisp macro."
879 (and (fboundp symbol)
880 (consp (setq symbol
881 (symbol-function symbol)))
882 (or (eq (car symbol) 'macro)
883 (if (eq (car symbol) 'autoload)
884 (memq (nth 4 symbol)
885 '(macro t))))))
888 (defun apropos-print-doc (i type do-keys)
889 (if (stringp (setq i (nth i apropos-item)))
890 (progn
891 (insert " ")
892 (insert-text-button (button-type-get type 'apropos-label)
893 'type type
894 ;; Can't use the default button face, since
895 ;; user may have changed the variable!
896 ;; Just say `no' to variables containing faces!
897 'face apropos-label-face
898 'apropos-symbol (car apropos-item))
899 (insert ": ")
900 (insert (if do-keys (substitute-command-keys i) i))
901 (or (bolp) (terpri)))))
904 (defun apropos-follow ()
905 "Invokes any button at point, otherwise invokes the nearest label button."
906 (interactive)
907 (button-activate
908 (or (apropos-next-label-button (line-beginning-position))
909 (error "There is nothing to follow here"))))
912 (defun apropos-describe-plist (symbol)
913 "Display a pretty listing of SYMBOL's plist."
914 (help-setup-xref (list 'apropos-describe-plist symbol) (interactive-p))
915 (with-output-to-temp-buffer (help-buffer)
916 (set-buffer standard-output)
917 (princ "Symbol ")
918 (prin1 symbol)
919 (princ "'s plist is\n (")
920 (if apropos-symbol-face
921 (put-text-property (+ (point-min) 7) (- (point) 14)
922 'face apropos-symbol-face))
923 (insert (apropos-format-plist symbol "\n "))
924 (princ ")")
925 (print-help-return-message)))
928 (provide 'apropos)
930 ;;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
931 ;;; apropos.el ends here