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