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