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 ;; For auld lang syne:
144 (fset 'command-apropos
'apropos-command
)
146 (defun apropos-command (apropos-regexp &optional do-all
)
147 "Shows commands (interactively callable functions) that match REGEXP.
148 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
150 (interactive (list (read-string (concat "Apropos command "
151 (if (or current-prefix-arg
157 (let ((standard-output (get-buffer-create "*Apropos*")))
158 (print-help-return-message 'identity
))))
159 (or do-all
(setq do-all apropos-do-all
))
160 (setq apropos-accumulator
161 (apropos-internal apropos-regexp
163 (lambda (symbol) (or (commandp symbol
)
164 (user-variable-p symbol
)))
172 (setq symbol
(car p
))
173 (if (commandp symbol
)
174 (if (setq doc
(documentation symbol t
))
175 (substring doc
0 (string-match "\n" doc
))
178 (user-variable-p symbol
)
179 (if (setq doc
(documentation-property
180 symbol
'variable-documentation t
))
182 (string-match "\n" doc
))))))
185 (and message
(message message
)))))
189 (defun apropos (apropos-regexp &optional do-all
)
190 "Show all bound symbols whose names match REGEXP.
191 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
192 symbols and key bindings, which is a little more time-consuming.
193 Returns list of symbols and documentation found."
194 (interactive "sApropos symbol (regexp): \nP")
195 (setq apropos-accumulator
196 (apropos-internal apropos-regexp
203 (symbol-plist symbol
))))))
205 (or do-all apropos-do-all
)
207 (let (symbol doc properties
)
210 (setq symbol
(car p
))
211 (when (fboundp symbol
)
212 (if (setq doc
(documentation symbol t
))
213 (substring doc
0 (string-match "\n" doc
))
215 (when (boundp symbol
)
216 (if (setq doc
(documentation-property
217 symbol
'variable-documentation t
))
218 (substring doc
0 (string-match "\n" doc
))
220 (when (setq properties
(symbol-plist symbol
))
221 (setq doc
(list (car properties
)))
222 (while (setq properties
(cdr (cdr properties
)))
223 (setq doc
(cons (car properties
) doc
)))
224 (mapconcat #'symbol-name
(nreverse doc
) " "))
225 (when (get symbol
'widget-type
)
226 (if (setq doc
(documentation-property
227 symbol
'widget-documentation t
))
229 (string-match "\n" doc
))
232 (if (setq doc
(documentation-property
233 symbol
'face-documentation t
))
235 (string-match "\n" doc
))
237 (when (get symbol
'custom-group
)
238 (if (setq doc
(documentation-property
239 symbol
'group-documentation t
))
241 (string-match "\n" doc
))
242 "(not documented)"))))
248 (defun apropos-value (apropos-regexp &optional do-all
)
249 "Show all symbols whose value's printed image matches REGEXP.
250 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
251 at the function and at the names and values of properties.
252 Returns list of symbols and values found."
253 (interactive "sApropos value (regexp): \nP")
254 (or do-all
(setq do-all apropos-do-all
))
255 (setq apropos-accumulator
())
259 (setq f nil v nil p nil
)
260 (or (memq symbol
'(apropos-regexp do-all apropos-accumulator
262 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
264 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
265 p
(apropos-format-plist symbol
"\n " t
)))
267 (setq apropos-accumulator
(cons (list symbol f v p
)
268 apropos-accumulator
))))))
269 (apropos-print nil nil t
))
273 (defun apropos-documentation (apropos-regexp &optional do-all
)
274 "Show symbols whose documentation contain matches for REGEXP.
275 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
276 documentation that is not stored in the documentation file and show key
278 Returns list of symbols and documentation found."
279 (interactive "sApropos documentation (regexp): \nP")
280 (or do-all
(setq do-all apropos-do-all
))
281 (setq apropos-accumulator
() apropos-files-scanned
())
282 (let ((standard-input (get-buffer-create " apropos-temp"))
286 (set-buffer standard-input
)
287 (apropos-documentation-check-doc-file)
291 (setq f
(apropos-safe-documentation symbol
)
292 v
(get symbol
'variable-documentation
))
293 (if (integerp v
) (setq v
))
294 (setq f
(apropos-documentation-internal f
)
295 v
(apropos-documentation-internal v
))
297 (if (setq apropos-item
298 (cdr (assq symbol apropos-accumulator
)))
301 (setcar apropos-item f
))
303 (setcar (cdr apropos-item
) v
)))
304 (setq apropos-accumulator
305 (cons (list symbol f v
)
306 apropos-accumulator
)))))))
307 (apropos-print nil nil t
))
308 (kill-buffer standard-input
))))
311 (defun apropos-value-internal (predicate symbol function
)
312 (if (funcall predicate symbol
)
314 (setq symbol
(prin1-to-string (funcall function symbol
)))
315 (if (string-match apropos-regexp symbol
)
317 (if apropos-match-face
318 (put-text-property (match-beginning 0) (match-end 0)
319 'face apropos-match-face
323 (defun apropos-documentation-internal (doc)
325 (apropos-documentation-check-elc-file (car doc
))
327 (string-match apropos-regexp doc
)
329 (if apropos-match-face
330 (put-text-property (match-beginning 0)
332 'face apropos-match-face
333 (setq doc
(copy-sequence doc
))))
336 (defun apropos-format-plist (pl sep
&optional compare
)
337 (setq pl
(symbol-plist pl
))
340 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
341 (if (or (not compare
) (string-match apropos-regexp p
))
342 (if apropos-property-face
343 (put-text-property 0 (length (symbol-name (car pl
)))
344 'face apropos-property-face p
))
348 (and compare apropos-match-face
349 (put-text-property (match-beginning 0) (match-end 0)
350 'face apropos-match-face
352 (setq p-out
(concat p-out
(if p-out sep
) p
))))
353 (setq pl
(nthcdr 2 pl
)))
357 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
359 (defun apropos-documentation-check-doc-file ()
360 (let (type symbol
(sepa 2) sepb beg end
)
363 (insert-file-contents (concat doc-directory internal-doc-file-name
))
365 (while (save-excursion
366 (setq sepb
(search-forward "\^_"))
368 (beginning-of-line 2)
369 (if (save-restriction
370 (narrow-to-region (point) (1- sepb
))
371 (re-search-forward apropos-regexp nil t
))
373 (setq beg
(match-beginning 0)
375 (goto-char (1+ sepa
))
376 (or (setq type
(if (eq ?F
(preceding-char))
377 1 ; function documentation
378 2) ; variable documentation
380 beg
(- beg
(point) 1)
381 end
(- end
(point) 1)
382 doc
(buffer-substring (1+ (point)) (1- sepb
))
383 apropos-item
(assq symbol apropos-accumulator
))
384 (setq apropos-item
(list symbol nil nil
)
385 apropos-accumulator
(cons apropos-item
386 apropos-accumulator
)))
387 (if apropos-match-face
388 (put-text-property beg end
'face apropos-match-face doc
))
389 (setcar (nthcdr type apropos-item
) doc
)))
390 (setq sepa
(goto-char sepb
)))))
392 (defun apropos-documentation-check-elc-file (file)
393 (if (member file apropos-files-scanned
)
395 (let (symbol doc beg end this-is-a-variable
)
396 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
398 (insert-file-contents file
)
399 (while (search-forward "\n#@" nil t
)
400 ;; Read the comment length, and advance over it.
403 end
(+ (point) end -
1))
405 (if (save-restriction
406 ;; match ^ and $ relative to doc string
407 (narrow-to-region beg end
)
408 (re-search-forward apropos-regexp nil t
))
410 (goto-char (+ end
2))
411 (setq doc
(buffer-substring beg end
)
412 end
(- (match-end 0) beg
)
413 beg
(- (match-beginning 0) beg
)
414 this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
416 (skip-chars-forward "(a-z")
419 symbol
(if (consp symbol
)
422 (if (if this-is-a-variable
423 (get symbol
'variable-documentation
)
424 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
426 (or (setq apropos-item
(assq symbol apropos-accumulator
))
427 (setq apropos-item
(list symbol nil nil
)
428 apropos-accumulator
(cons apropos-item
429 apropos-accumulator
)))
430 (if apropos-match-face
431 (put-text-property beg end
'face apropos-match-face
433 (setcar (nthcdr (if this-is-a-variable
2 1)
439 (defun apropos-safe-documentation (function)
440 "Like documentation, except it avoids calling `get_doc_string'.
441 Will return nil instead."
442 (while (and function
(symbolp function
))
443 (setq function
(if (fboundp function
)
444 (symbol-function function
))))
445 (if (eq (car-safe function
) 'macro
)
446 (setq function
(cdr function
)))
447 (setq function
(if (byte-code-function-p function
)
448 (if (> (length function
) 4)
450 (if (eq (car-safe function
) 'autoload
)
452 (if (eq (car-safe function
) 'lambda
)
453 (if (stringp (nth 2 function
))
455 (if (stringp (nth 3 function
))
456 (nth 3 function
)))))))
457 (if (integerp function
)
463 (defun apropos-print (do-keys doc-fn spacing
)
464 "Output result of various apropos commands with `apropos-regexp'.
465 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
466 of apropos-accumulator and may modify it resulting in (symbol fn-doc
467 var-doc [plist-doc]). Returns sorted list of symbols and documentation
469 (if (null apropos-accumulator
)
470 (message "No apropos matches for `%s'" apropos-regexp
)
472 (funcall doc-fn apropos-accumulator
))
473 (setq apropos-accumulator
474 (sort apropos-accumulator
(lambda (a b
)
475 (string-lessp (car a
) (car b
)))))
476 (and apropos-label-face
477 (symbolp apropos-label-face
)
478 (setq apropos-label-face
`(face ,apropos-label-face
479 mouse-face highlight
)))
480 (with-output-to-temp-buffer "*Apropos*"
481 (let ((p apropos-accumulator
)
482 (old-buffer (current-buffer))
483 symbol item point1 point2
)
484 (set-buffer standard-output
)
487 (insert "If you move the mouse over text that changes color,\n"
488 (substitute-command-keys
489 "you can click \\[apropos-mouse-follow] to get more information.\n")))
490 (insert (substitute-command-keys
491 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
493 (or (not spacing
) (bobp) (terpri))
494 (setq apropos-item
(car p
)
495 symbol
(car apropos-item
)
498 (princ symbol
) ; print symbol name
499 (setq point2
(point))
500 ;; Calculate key-bindings if we want them.
506 (set-buffer old-buffer
)
507 (where-is-internal symbol
)))
509 ;; Copy over the list of key sequences,
510 ;; omitting any that contain a buffer or a frame.
512 (let ((key (car keys
))
515 (while (< i
(length key
))
516 (if (or (framep (aref key i
))
517 (bufferp (aref key i
)))
521 (setq filtered
(cons key filtered
))))
522 (setq keys
(cdr keys
)))
523 (setq item filtered
))
524 ;; Convert the remaining keys to a string and insert.
528 (setq key
(key-description key
))
529 (if apropos-keybinding-face
530 (put-text-property 0 (length key
)
531 'face apropos-keybinding-face
536 (put-text-property (- (point) 3) (point)
537 'face apropos-keybinding-face
)
538 (insert " " (symbol-name symbol
) " ")
540 (put-text-property (- (point) 3) (point)
541 'face apropos-keybinding-face
)))
543 ;; only now so we don't propagate text attributes all over
544 (put-text-property point1 point2
'item
545 (if (eval `(or ,@(cdr apropos-item
)))
548 (if apropos-symbol-face
549 (put-text-property point1 point2
'face apropos-symbol-face
))
550 (apropos-print-doc 'describe-function
1
551 (if (commandp symbol
)
553 (if (apropos-macrop symbol
)
557 (if (get symbol
'custom-type
)
558 (apropos-print-doc 'customize-variable-other-window
2
559 "User Option" do-keys
)
560 (apropos-print-doc 'describe-variable
2
562 (apropos-print-doc 'customize-group-other-window
6 "Group" do-keys
)
563 (apropos-print-doc 'customize-face-other-window
5 "Face" do-keys
)
564 (apropos-print-doc 'widget-browse-other-window
4 "Widget" do-keys
)
565 (apropos-print-doc 'apropos-describe-plist
3
567 (prog1 apropos-accumulator
568 (setq apropos-accumulator
()))) ; permit gc
571 (defun apropos-macrop (symbol)
572 "T if SYMBOL is a Lisp macro."
573 (and (fboundp symbol
)
575 (symbol-function symbol
)))
576 (or (eq (car symbol
) 'macro
)
577 (if (eq (car symbol
) 'autoload
)
582 (defun apropos-print-doc (action i str do-keys
)
583 (if (stringp (setq i
(nth i apropos-item
)))
586 (put-text-property (- (point) 2) (1- (point))
589 (if apropos-label-face
590 (add-text-properties (- (point) (length str
) 2)
593 (insert (if do-keys
(substitute-command-keys i
) i
))
594 (or (bolp) (terpri)))))
597 (defun apropos-mouse-follow (event)
599 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
603 (set-buffer (window-buffer (posn-window (event-start event
))))
604 (goto-char (posn-point (event-start event
)))
605 (or (and (not (eobp)) (get-text-property (point) 'mouse-face
))
606 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face
))
607 (error "There is nothing to follow here"))
608 (apropos-follow other
))))
611 (defun apropos-follow (&optional other
)
613 (let* (;; Properties are always found at the beginning of the line.
614 (bol (save-excursion (beginning-of-line) (point)))
615 ;; If there is no `item' property here, look behind us.
616 (item (get-text-property bol
'item
))
617 (item-at (if item nil
(previous-single-property-change bol
'item
)))
618 ;; Likewise, if there is no `action' property here, look in front.
619 (action (get-text-property bol
'action
))
620 (action-at (if action nil
(next-single-property-change bol
'action
))))
621 (and (null item
) item-at
622 (setq item
(get-text-property (1- item-at
) 'item
)))
623 (and (null action
) action-at
624 (setq action
(get-text-property action-at
'action
)))
625 (if (not (and item action
))
626 (error "There is nothing to follow here"))
627 (if (consp item
) (error "There is nothing to follow in `%s'" (car item
)))
628 (if other
(set-buffer other
))
629 (funcall action item
)))
633 (defun apropos-describe-plist (symbol)
634 "Display a pretty listing of SYMBOL's plist."
635 (with-output-to-temp-buffer "*Help*"
636 (set-buffer standard-output
)
639 (princ "'s plist is\n (")
640 (if apropos-symbol-face
641 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face
))
642 (insert (apropos-format-plist symbol
"\n "))
644 (print-help-return-message)))
648 ;;; apropos.el ends here