1 ;;; apropos.el --- apropos commands for users and programmers.
3 ;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Rewritten: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The ideas for this package were derived from the C code in
29 ;; src/keymap.c and elsewhere. The functions in this file should
30 ;; always be byte-compiled for speed. Someone should rewrite this in
31 ;; C (as part of src/keymap.c) for speed.
33 ;; The idea for super-apropos is based on the original implementation
34 ;; by Lynn Slater <lrs@esl.com>.
37 ;; Fixed bug, current-local-map can return nil.
38 ;; Change, doesn't calculate key-bindings unless needed.
39 ;; Added super-apropos capability, changed print functions.
40 ;;; Made fast-apropos and super-apropos share code.
41 ;;; Sped up fast-apropos again.
42 ;; Added apropos-do-all option.
43 ;;; Added fast-command-apropos.
44 ;; Changed doc strings to comments for helping functions.
45 ;;; Made doc file buffer read-only, buried it.
46 ;; Only call substitute-command-keys if do-all set.
48 ;; Optionally use configurable faces to make the output more legible.
49 ;; Differentiate between command, function and macro.
50 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
51 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
52 ;; Apropos-documentation (ex super-apropos) now finds all it should.
53 ;; New apropos-value snoops through all values and optionally plists.
54 ;; Reading DOC file doesn't load nroff.
55 ;; Added hypertext following of documentation, mouse-2 on variable gives value
56 ;; from buffer in active window.
61 "Apropos commands for users and programmers"
65 ;; I see a degradation of maybe 10-20% only.
66 (defcustom apropos-do-all nil
67 "*Whether the apropos commands should do more.
69 Slows them down more or less. Set this non-nil if you have a fast machine."
74 (defcustom apropos-symbol-face
(if window-system
'bold
)
75 "*Face for symbol name in apropos output or `nil'.
76 This looks good, but slows down the commands several times."
80 (defcustom apropos-keybinding-face
(if window-system
'underline
)
81 "*Face for keybinding display in apropos output or `nil'.
82 This looks good, but slows down the commands several times."
86 (defcustom apropos-label-face
(if window-system
'italic
)
87 "*Face for label (Command, Variable ...) in apropos output or `nil'.
88 If this is `nil' no mouse highlighting occurs.
89 This looks good, but slows down the commands several times.
90 When this is a face name, as it is initially, it gets transformed to a
91 text-property list for efficiency."
95 (defcustom apropos-property-face
(if window-system
'bold-italic
)
96 "*Face for property name in apropos output or `nil'.
97 This looks good, but slows down the commands several times."
101 (defcustom apropos-match-face
(if window-system
'secondary-selection
)
102 "*Face for matching part in apropos-documentation/value output or `nil'.
103 This looks good, but slows down the commands several times."
108 (defvar apropos-mode-map
109 (let ((map (make-sparse-keymap)))
110 (define-key map
"\C-m" 'apropos-follow
)
111 (define-key map
" " 'scroll-up
)
112 (define-key map
"\177" 'scroll-down
)
113 (define-key map
[mouse-2
] 'apropos-mouse-follow
)
114 (define-key map
[down-mouse-2
] nil
)
116 "Keymap used in Apropos mode.")
119 (defvar apropos-regexp nil
120 "Regexp used in current apropos run.")
122 (defvar apropos-files-scanned
()
123 "List of elc files already scanned in current run of `apropos-documentation'.")
125 (defvar apropos-accumulator
()
126 "Alist of symbols already found in current apropos run.")
128 (defvar apropos-item
()
129 "Current item in or for apropos-accumulator.")
131 (defun apropos-mode ()
132 "Major mode for following hyperlinks in output of apropos commands.
134 \\{apropos-mode-map}"
136 (kill-all-local-variables)
137 (use-local-map apropos-mode-map
)
138 (setq major-mode
'apropos-mode
139 mode-name
"Apropos"))
142 (defun apropos-variable (regexp)
143 (interactive (list (read-string "Apropos variable (regexp): ")))
144 (apropos-command regexp nil t
))
146 ;; For auld lang syne:
148 (fset 'command-apropos
'apropos-command
)
150 (defun apropos-command (apropos-regexp &optional do-all just-vars
)
151 "Show commands (interactively callable functions) that match REGEXP.
152 With optional prefix ARG, or if `apropos-do-all' is non-nil, also show
153 variables. If JUST-VARS is non-nil, show only variables."
154 (interactive (list (read-string (concat
156 (if (or current-prefix-arg
162 (let ((standard-output (get-buffer-create "*Apropos*")))
163 (print-help-return-message 'identity
))))
164 (or do-all
(setq do-all apropos-do-all
))
165 (setq apropos-accumulator
166 (apropos-internal apropos-regexp
168 (lambda (symbol) (or (commandp symbol
)
169 (user-variable-p symbol
)))
170 (if just-vars
'user-variable-p
172 (let ((tem apropos-accumulator
))
174 (if (get (car tem
) 'apropos-inhibit
)
175 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
176 (setq tem
(cdr tem
))))
183 (setq symbol
(car p
))
184 (if (or do-all
(not just-vars
))
185 (if (commandp symbol
)
186 (if (setq doc
(documentation symbol t
))
187 (substring doc
0 (string-match "\n" doc
))
188 "(not documented)")))
190 (user-variable-p symbol
)
191 (if (setq doc
(documentation-property
192 symbol
'variable-documentation t
))
194 (string-match "\n" doc
))))))
197 (and message
(message message
)))))
201 (defun apropos (apropos-regexp &optional do-all
)
202 "Show all bound symbols whose names match REGEXP.
203 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
204 symbols and key bindings, which is a little more time-consuming.
205 Returns list of symbols and documentation found."
206 (interactive "sApropos symbol (regexp): \nP")
207 (setq apropos-accumulator
208 (apropos-internal apropos-regexp
215 (symbol-plist symbol
))))))
216 (let ((tem apropos-accumulator
))
218 (if (get (car tem
) 'apropos-inhibit
)
219 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
220 (setq tem
(cdr tem
))))
222 (or do-all apropos-do-all
)
224 (let (symbol doc properties
)
227 (setq symbol
(car p
))
228 (when (fboundp symbol
)
229 (if (setq doc
(documentation symbol t
))
230 (substring doc
0 (string-match "\n" doc
))
232 (when (boundp symbol
)
233 (if (setq doc
(documentation-property
234 symbol
'variable-documentation t
))
235 (substring doc
0 (string-match "\n" doc
))
237 (when (setq properties
(symbol-plist symbol
))
238 (setq doc
(list (car properties
)))
239 (while (setq properties
(cdr (cdr properties
)))
240 (setq doc
(cons (car properties
) doc
)))
241 (mapconcat #'symbol-name
(nreverse doc
) " "))
242 (when (get symbol
'widget-type
)
243 (if (setq doc
(documentation-property
244 symbol
'widget-documentation t
))
246 (string-match "\n" doc
))
249 (if (setq doc
(documentation-property
250 symbol
'face-documentation t
))
252 (string-match "\n" doc
))
254 (when (get symbol
'custom-group
)
255 (if (setq doc
(documentation-property
256 symbol
'group-documentation t
))
258 (string-match "\n" doc
))
259 "(not documented)"))))
265 (defun apropos-value (apropos-regexp &optional do-all
)
266 "Show all symbols whose value's printed image matches REGEXP.
267 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
268 at the function and at the names and values of properties.
269 Returns list of symbols and values found."
270 (interactive "sApropos value (regexp): \nP")
271 (or do-all
(setq do-all apropos-do-all
))
272 (setq apropos-accumulator
())
276 (setq f nil v nil p nil
)
277 (or (memq symbol
'(apropos-regexp do-all apropos-accumulator
279 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
281 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
282 p
(apropos-format-plist symbol
"\n " t
)))
284 (setq apropos-accumulator
(cons (list symbol f v p
)
285 apropos-accumulator
))))))
286 (apropos-print nil nil t
))
290 (defun apropos-documentation (apropos-regexp &optional do-all
)
291 "Show symbols whose documentation contain matches for REGEXP.
292 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
293 documentation that is not stored in the documentation file and show key
295 Returns list of symbols and documentation found."
296 (interactive "sApropos documentation (regexp): \nP")
297 (or do-all
(setq do-all apropos-do-all
))
298 (setq apropos-accumulator
() apropos-files-scanned
())
299 (let ((standard-input (get-buffer-create " apropos-temp"))
303 (set-buffer standard-input
)
304 (apropos-documentation-check-doc-file)
308 (setq f
(apropos-safe-documentation symbol
)
309 v
(get symbol
'variable-documentation
))
310 (if (integerp v
) (setq v
))
311 (setq f
(apropos-documentation-internal f
)
312 v
(apropos-documentation-internal v
))
314 (if (setq apropos-item
315 (cdr (assq symbol apropos-accumulator
)))
318 (setcar apropos-item f
))
320 (setcar (cdr apropos-item
) v
)))
321 (setq apropos-accumulator
322 (cons (list symbol f v
)
323 apropos-accumulator
)))))))
324 (apropos-print nil nil t
))
325 (kill-buffer standard-input
))))
328 (defun apropos-value-internal (predicate symbol function
)
329 (if (funcall predicate symbol
)
331 (setq symbol
(prin1-to-string (funcall function symbol
)))
332 (if (string-match apropos-regexp symbol
)
334 (if apropos-match-face
335 (put-text-property (match-beginning 0) (match-end 0)
336 'face apropos-match-face
340 (defun apropos-documentation-internal (doc)
342 (apropos-documentation-check-elc-file (car doc
))
344 (string-match apropos-regexp doc
)
346 (if apropos-match-face
347 (put-text-property (match-beginning 0)
349 'face apropos-match-face
350 (setq doc
(copy-sequence doc
))))
353 (defun apropos-format-plist (pl sep
&optional compare
)
354 (setq pl
(symbol-plist pl
))
357 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
358 (if (or (not compare
) (string-match apropos-regexp p
))
359 (if apropos-property-face
360 (put-text-property 0 (length (symbol-name (car pl
)))
361 'face apropos-property-face p
))
365 (and compare apropos-match-face
366 (put-text-property (match-beginning 0) (match-end 0)
367 'face apropos-match-face
369 (setq p-out
(concat p-out
(if p-out sep
) p
))))
370 (setq pl
(nthcdr 2 pl
)))
374 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
376 (defun apropos-documentation-check-doc-file ()
377 (let (type symbol
(sepa 2) sepb beg end
)
380 (insert-file-contents (concat doc-directory internal-doc-file-name
))
382 (while (save-excursion
383 (setq sepb
(search-forward "\^_"))
385 (beginning-of-line 2)
386 (if (save-restriction
387 (narrow-to-region (point) (1- sepb
))
388 (re-search-forward apropos-regexp nil t
))
390 (setq beg
(match-beginning 0)
392 (goto-char (1+ sepa
))
393 (or (setq type
(if (eq ?F
(preceding-char))
394 1 ; function documentation
395 2) ; variable documentation
397 beg
(- beg
(point) 1)
398 end
(- end
(point) 1)
399 doc
(buffer-substring (1+ (point)) (1- sepb
))
400 apropos-item
(assq symbol apropos-accumulator
))
401 (setq apropos-item
(list symbol nil nil
)
402 apropos-accumulator
(cons apropos-item
403 apropos-accumulator
)))
404 (if apropos-match-face
405 (put-text-property beg end
'face apropos-match-face doc
))
406 (setcar (nthcdr type apropos-item
) doc
)))
407 (setq sepa
(goto-char sepb
)))))
409 (defun apropos-documentation-check-elc-file (file)
410 (if (member file apropos-files-scanned
)
412 (let (symbol doc beg end this-is-a-variable
)
413 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
415 (insert-file-contents file
)
416 (while (search-forward "\n#@" nil t
)
417 ;; Read the comment length, and advance over it.
420 end
(+ (point) end -
1))
422 (if (save-restriction
423 ;; match ^ and $ relative to doc string
424 (narrow-to-region beg end
)
425 (re-search-forward apropos-regexp nil t
))
427 (goto-char (+ end
2))
428 (setq doc
(buffer-substring beg end
)
429 end
(- (match-end 0) beg
)
430 beg
(- (match-beginning 0) beg
)
431 this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
433 (skip-chars-forward "(a-z")
436 symbol
(if (consp symbol
)
439 (if (if this-is-a-variable
440 (get symbol
'variable-documentation
)
441 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
443 (or (setq apropos-item
(assq symbol apropos-accumulator
))
444 (setq apropos-item
(list symbol nil nil
)
445 apropos-accumulator
(cons apropos-item
446 apropos-accumulator
)))
447 (if apropos-match-face
448 (put-text-property beg end
'face apropos-match-face
450 (setcar (nthcdr (if this-is-a-variable
2 1)
456 (defun apropos-safe-documentation (function)
457 "Like documentation, except it avoids calling `get_doc_string'.
458 Will return nil instead."
459 (while (and function
(symbolp function
))
460 (setq function
(if (fboundp function
)
461 (symbol-function function
))))
462 (if (eq (car-safe function
) 'macro
)
463 (setq function
(cdr function
)))
464 (setq function
(if (byte-code-function-p function
)
465 (if (> (length function
) 4)
467 (if (eq (car-safe function
) 'autoload
)
469 (if (eq (car-safe function
) 'lambda
)
470 (if (stringp (nth 2 function
))
472 (if (stringp (nth 3 function
))
473 (nth 3 function
)))))))
474 (if (integerp function
)
480 (defun apropos-print (do-keys doc-fn spacing
)
481 "Output result of various apropos commands with `apropos-regexp'.
482 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
483 of apropos-accumulator and may modify it resulting in (symbol fn-doc
484 var-doc [plist-doc]). Returns sorted list of symbols and documentation
486 (if (null apropos-accumulator
)
487 (message "No apropos matches for `%s'" apropos-regexp
)
489 (funcall doc-fn apropos-accumulator
))
490 (setq apropos-accumulator
491 (sort apropos-accumulator
(lambda (a b
)
492 (string-lessp (car a
) (car b
)))))
493 (and apropos-label-face
494 (symbolp apropos-label-face
)
495 (setq apropos-label-face
`(face ,apropos-label-face
496 mouse-face highlight
)))
497 (with-output-to-temp-buffer "*Apropos*"
498 (let ((p apropos-accumulator
)
499 (old-buffer (current-buffer))
500 symbol item point1 point2
)
501 (set-buffer standard-output
)
504 (insert "If you move the mouse over text that changes color,\n"
505 (substitute-command-keys
506 "you can click \\[apropos-mouse-follow] to get more information.\n")))
507 (insert (substitute-command-keys
508 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
510 (or (not spacing
) (bobp) (terpri))
511 (setq apropos-item
(car p
)
512 symbol
(car apropos-item
)
515 (princ symbol
) ; print symbol name
516 (setq point2
(point))
517 ;; Calculate key-bindings if we want them.
523 (set-buffer old-buffer
)
524 (where-is-internal symbol
)))
526 ;; Copy over the list of key sequences,
527 ;; omitting any that contain a buffer or a frame.
529 (let ((key (car keys
))
532 (while (< i
(length key
))
533 (if (or (framep (aref key i
))
534 (bufferp (aref key i
)))
538 (setq filtered
(cons key filtered
))))
539 (setq keys
(cdr keys
)))
540 (setq item filtered
))
541 ;; Convert the remaining keys to a string and insert.
545 (setq key
(key-description key
))
546 (if apropos-keybinding-face
547 (put-text-property 0 (length key
)
548 'face apropos-keybinding-face
553 (put-text-property (- (point) 3) (point)
554 'face apropos-keybinding-face
)
555 (insert " " (symbol-name symbol
) " ")
557 (put-text-property (- (point) 3) (point)
558 'face apropos-keybinding-face
)))
560 ;; only now so we don't propagate text attributes all over
561 (put-text-property point1 point2
'item
562 (if (eval `(or ,@(cdr apropos-item
)))
565 (if apropos-symbol-face
566 (put-text-property point1 point2
'face apropos-symbol-face
))
567 (apropos-print-doc 'describe-function
1
568 (if (commandp symbol
)
570 (if (apropos-macrop symbol
)
574 (if (get symbol
'custom-type
)
575 (apropos-print-doc 'customize-variable-other-window
2
577 (apropos-print-doc 'describe-variable
2
579 (apropos-print-doc 'customize-group-other-window
6 "Group" t
)
580 (apropos-print-doc 'customize-face-other-window
5 "Face" t
)
581 (apropos-print-doc 'widget-browse-other-window
4 "Widget" t
)
582 (apropos-print-doc 'apropos-describe-plist
3
584 (prog1 apropos-accumulator
585 (setq apropos-accumulator
()))) ; permit gc
588 (defun apropos-macrop (symbol)
589 "T if SYMBOL is a Lisp macro."
590 (and (fboundp symbol
)
592 (symbol-function symbol
)))
593 (or (eq (car symbol
) 'macro
)
594 (if (eq (car symbol
) 'autoload
)
599 (defun apropos-print-doc (action i str do-keys
)
600 (if (stringp (setq i
(nth i apropos-item
)))
603 (put-text-property (- (point) 2) (1- (point))
606 (if apropos-label-face
607 (add-text-properties (- (point) (length str
) 2)
610 (insert (if do-keys
(substitute-command-keys i
) i
))
611 (or (bolp) (terpri)))))
614 (defun apropos-mouse-follow (event)
616 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
620 (set-buffer (window-buffer (posn-window (event-start event
))))
621 (goto-char (posn-point (event-start event
)))
622 (or (and (not (eobp)) (get-text-property (point) 'mouse-face
))
623 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face
))
624 (error "There is nothing to follow here"))
625 (apropos-follow other
))))
628 (defun apropos-follow (&optional other
)
630 (let* (;; Properties are always found at the beginning of the line.
631 (bol (save-excursion (beginning-of-line) (point)))
632 ;; If there is no `item' property here, look behind us.
633 (item (get-text-property bol
'item
))
634 (item-at (if item nil
(previous-single-property-change bol
'item
)))
635 ;; Likewise, if there is no `action' property here, look in front.
636 (action (get-text-property bol
'action
))
637 (action-at (if action nil
(next-single-property-change bol
'action
))))
638 (and (null item
) item-at
639 (setq item
(get-text-property (1- item-at
) 'item
)))
640 (and (null action
) action-at
641 (setq action
(get-text-property action-at
'action
)))
642 (if (not (and item action
))
643 (error "There is nothing to follow here"))
644 (if (consp item
) (error "There is nothing to follow in `%s'" (car item
)))
645 (if other
(set-buffer other
))
646 (funcall action item
)))
650 (defun apropos-describe-plist (symbol)
651 "Display a pretty listing of SYMBOL's plist."
652 (with-output-to-temp-buffer "*Help*"
653 (set-buffer standard-output
)
656 (princ "'s plist is\n (")
657 (if apropos-symbol-face
658 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face
))
659 (insert (apropos-format-plist symbol
"\n "))
661 (print-help-return-message)))
665 ;;; apropos.el ends here