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