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
7 ;; Maintainer: SL Baur <steve@altair.xemacs.org>
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Synched up with: Last synched with FSF 19.34, diverged since.
31 ;; The ideas for this package were derived from the C code in
32 ;; src/keymap.c and elsewhere. The functions in this file should
33 ;; always be byte-compiled for speed. Someone should rewrite this in
34 ;; C (as part of src/keymap.c) for speed.
36 ;; The idea for super-apropos is based on the original implementation
37 ;; by Lynn Slater <lrs@esl.com>.
41 ;; Fixed bug, current-local-map can return nil.
42 ;; Change, doesn't calculate key-bindings unless needed.
43 ;; Added super-apropos capability, changed print functions.
44 ;;; Made fast-apropos and super-apropos share code.
45 ;;; Sped up fast-apropos again.
46 ;; Added apropos-do-all option.
47 ;;; Added fast-command-apropos.
48 ;; Changed doc strings to comments for helping functions.
49 ;;; Made doc file buffer read-only, buried it.
50 ;; Only call substitute-command-keys if do-all set.
52 ;; Optionally use configurable faces to make the output more legible.
53 ;; Differentiate between command, function and macro.
54 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
55 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
56 ;; Apropos-documentation (ex super-apropos) now finds all it should.
57 ;; New apropos-value snoops through all values and optionally plists.
58 ;; Reading DOC file doesn't load nroff.
59 ;; Added hypertext following of documentation, mouse-2 on variable gives value
60 ;; from buffer in active window.
64 ;; I see a degradation of maybe 10-20% only.
65 ;; [sb -- FSF protects the face declarations with `if window-system'
66 ;; I see no reason why we should do so]
67 (defvar apropos-do-all nil
68 "*Whether the apropos commands should do more.
69 Slows them down more or less. Set this non-nil if you have a fast machine.")
72 (defvar apropos-symbol-face (if (boundp 'font-lock-keyword-face)
73 font-lock-keyword-face
75 "*Face for symbol name in apropos output or `nil'.
76 This looks good, but slows down the commands several times.")
79 (defvar apropos-keybinding-face (if (boundp 'font-lock-string-face)
82 "*Face for keybinding display in apropos output or `nil'.
83 This looks good, but slows down the commands several times.")
86 (defvar apropos-label-face (if (boundp 'font-lock-comment-face)
87 font-lock-comment-face
89 "*Face for label (Command, Variable ...) in apropos output or `nil'.
90 If this is `nil' no mouse highlighting occurs.
91 This looks good, but slows down the commands several times.
92 When this is a face name, as it is initially, it gets transformed to a
93 text-property list for efficiency.")
96 (defvar apropos-property-face (if (boundp 'font-lock-variable-name-face)
97 font-lock-variable-name-face
99 "*Face for property name in apropos output or `nil'.
100 This looks good, but slows down the commands several times.")
102 (defvar apropos-match-face 'secondary-selection
103 "*Face for matching part in apropos-documentation/value output or `nil'.
104 This looks good, but slows down the commands several times.")
107 (defvar apropos-mode-map
108 (let ((map (make-sparse-keymap)))
109 (define-key map [(control m)] 'apropos-follow)
110 (define-key map [(button2up)] 'apropos-mouse-follow)
111 (define-key map [(button2)] 'undefined)
113 "Keymap used in Apropos mode.")
116 (defvar apropos-regexp nil
117 "Regexp used in current apropos run.")
119 (defvar apropos-files-scanned ()
120 "List of elc files already scanned in current run of `apropos-documentation'.")
122 (defvar apropos-accumulator ()
123 "Alist of symbols already found in current apropos run.")
125 (defvar apropos-item ()
126 "Current item in or for apropos-accumulator.")
128 (defvar apropos-mode-hook nil) ; XEmacs
130 (defun apropos-mode ()
131 "Major mode for following hyperlinks in output of apropos commands.
133 \\{apropos-mode-map}"
135 (kill-all-local-variables)
136 (use-local-map apropos-mode-map)
137 (setq major-mode 'apropos-mode
139 (run-hooks 'apropos-mode-hook)) ; XEmacs
142 ;; For auld lang syne:
144 (fset 'command-apropos 'apropos-command)
147 (defun apropos-command (apropos-regexp &optional do-all)
148 "Shows commands (interactively callable functions) that match REGEXP.
149 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
151 ;; XEmacs: All code related to special treatment of buffer has been removed
152 (interactive (list (read-string (concat "Apropos command "
153 (if (or current-prefix-arg
158 (or do-all (setq do-all apropos-do-all))
159 (setq apropos-accumulator
160 (apropos-internal apropos-regexp
162 (lambda (symbol) (or (commandp symbol)
163 (user-variable-p symbol)))
171 (setq symbol (car p))
172 (if (commandp symbol)
174 ;; XEmacs change: if obsolete,
175 ;; only mention that.
176 (or (function-obsoleteness-doc symbol)
177 (documentation symbol t)))
178 (substring doc 0 (string-match "\n" doc))
181 (user-variable-p symbol)
184 ;; XEmacs change: if obsolete,
185 ;; only mention that.
186 (variable-obsoleteness-doc symbol)
187 (documentation-property
188 symbol 'variable-documentation t)))
190 (string-match "\n" doc))))))
196 (defun apropos (apropos-regexp &optional do-all)
197 "Show all bound symbols whose names match REGEXP.
198 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
199 symbols and key bindings, which is a little more time-consuming.
200 Returns list of symbols and documentation found."
201 (interactive "sApropos symbol (regexp): \nP")
202 ;; XEmacs change: hitting ENTER by mistake is a common mess-up and
203 ;; shouldn't make Emacs hang for a long time trying to list all symbols.
204 (or (> (length apropos-regexp) 0)
205 (error "Must pass non-empty regexp to `apropos'"))
206 (setq apropos-accumulator
207 (apropos-internal apropos-regexp
214 (symbol-plist symbol))))))
216 (or do-all apropos-do-all)
221 (setq symbol (car p))
224 ;; XEmacs change: if obsolete,
225 ;; only mention that.
226 (or (function-obsoleteness-doc symbol)
227 (documentation symbol t)))
228 (substring doc 0 (string-match "\n" doc))
233 ;; XEmacs change: if obsolete,
234 ;; only mention that.
235 (variable-obsoleteness-doc symbol)
236 (documentation-property
237 symbol 'variable-documentation t)))
239 (string-match "\n" doc))
241 (if (setq doc (symbol-plist symbol))
242 (if (eq (/ (length doc) 2) 1)
243 (format "1 property (%s)" (car doc))
244 (format "%d properties" (/ (length doc) 2))))
245 (if (get symbol 'widget-type)
246 (if (setq doc (documentation-property
247 symbol 'widget-documentation t))
249 (string-match "\n" doc))
251 (if (find-face symbol)
252 (if (setq doc (face-doc-string symbol))
254 (string-match "\n" doc))
256 (when (get symbol 'custom-group)
257 (if (setq doc (documentation-property
258 symbol 'group-documentation t))
260 (string-match "\n" doc))
261 "(not documented)"))))
267 (defun apropos-value (apropos-regexp &optional do-all)
268 "Show all symbols whose value's printed image matches REGEXP.
269 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
270 at the function and at the names and values of properties.
271 Returns list of symbols and values found."
272 (interactive "sApropos value (regexp): \nP")
273 (or do-all (setq do-all apropos-do-all))
274 (setq apropos-accumulator ())
278 (setq f nil v nil p nil)
279 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
281 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
283 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
284 p (apropos-format-plist symbol "\n " t)))
286 (setq apropos-accumulator (cons (list symbol f v p)
287 apropos-accumulator))))))
288 (apropos-print nil nil t))
292 (defun apropos-documentation (apropos-regexp &optional do-all)
293 "Show symbols whose documentation contain matches for REGEXP.
294 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
295 documentation that is not stored in the documentation file and show key
297 Returns list of symbols and documentation found."
298 (interactive "sApropos documentation (regexp): \nP")
299 (or do-all (setq do-all apropos-do-all))
300 (setq apropos-accumulator () apropos-files-scanned ())
301 (let ((standard-input (get-buffer-create " apropos-temp"))
305 (set-buffer standard-input)
306 (apropos-documentation-check-doc-file)
310 (setq f (apropos-safe-documentation symbol)
311 v (get symbol 'variable-documentation))
312 (when (integerp v) (setq v nil))
313 (setq f (apropos-documentation-internal f)
314 v (apropos-documentation-internal v))
316 (if (setq apropos-item
317 (cdr (assq symbol apropos-accumulator)))
320 (setcar apropos-item f))
322 (setcar (cdr apropos-item) v)))
323 (setq apropos-accumulator
324 (cons (list symbol f v)
325 apropos-accumulator)))))))
326 (apropos-print nil nil t))
327 (kill-buffer standard-input))))
330 (defun apropos-value-internal (predicate symbol function)
331 (if (funcall predicate symbol)
333 (setq symbol (prin1-to-string (funcall function symbol)))
334 (if (string-match apropos-regexp symbol)
336 (if apropos-match-face
337 (put-text-property (match-beginning 0) (match-end 0)
338 'face apropos-match-face
342 (defun apropos-documentation-internal (doc)
344 (apropos-documentation-check-elc-file (car doc))
346 (string-match apropos-regexp doc)
348 (if apropos-match-face
349 (put-text-property (match-beginning 0)
351 'face apropos-match-face
352 (setq doc (copy-sequence doc))))
355 (defun apropos-format-plist (pl sep &optional compare)
356 (setq pl (symbol-plist pl))
359 (setq p (format "%s %S" (car pl) (nth 1 pl)))
360 (if (or (not compare) (string-match apropos-regexp p))
361 (if apropos-property-face
362 (put-text-property 0 (length (symbol-name (car pl)))
363 'face apropos-property-face p))
367 (and compare apropos-match-face
368 (put-text-property (match-beginning 0) (match-end 0)
369 'face apropos-match-face
371 (setq p-out (concat p-out (if p-out sep) p))))
372 (setq pl (nthcdr 2 pl)))
376 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
378 (defun apropos-documentation-check-doc-file ()
379 (let (type symbol (sepa 2) sepb beg end)
382 (insert-file-contents (concat doc-directory internal-doc-file-name))
384 (while (save-excursion
385 (setq sepb (search-forward "\^_"))
387 (beginning-of-line 2)
388 (if (save-restriction
389 (narrow-to-region (point) (1- sepb))
390 (re-search-forward apropos-regexp nil t))
392 (setq beg (match-beginning 0)
394 (goto-char (1+ sepa))
395 (or (setq type (if (eq ?F (preceding-char))
396 1 ; function documentation
397 2) ; variable documentation
399 beg (- beg (point) 1)
400 end (- end (point) 1)
401 doc (buffer-substring (1+ (point)) (1- sepb))
402 apropos-item (assq symbol apropos-accumulator))
403 (setq apropos-item (list symbol nil nil)
404 apropos-accumulator (cons apropos-item
405 apropos-accumulator)))
406 (if apropos-match-face
407 (put-text-property beg end 'face apropos-match-face doc))
408 (setcar (nthcdr type apropos-item) doc)))
409 (setq sepa (goto-char sepb)))))
411 (defun apropos-documentation-check-elc-file (file)
412 (if (member file apropos-files-scanned)
414 (let (symbol doc beg end this-is-a-variable)
415 (setq apropos-files-scanned (cons file apropos-files-scanned))
417 (insert-file-contents file)
418 (while (search-forward "\n#@" nil t)
419 ;; Read the comment length, and advance over it.
422 end (+ (point) end -1))
424 (if (save-restriction
425 ;; match ^ and $ relative to doc string
426 (narrow-to-region beg end)
427 (re-search-forward apropos-regexp nil t))
429 (goto-char (+ end 2))
430 (setq doc (buffer-substring beg end)
431 end (- (match-end 0) beg)
432 beg (- (match-beginning 0) beg)
433 this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
435 (skip-chars-forward "(a-z")
438 symbol (if (consp symbol)
441 (if (if this-is-a-variable
442 (get symbol 'variable-documentation)
443 (and (fboundp symbol) (apropos-safe-documentation symbol)))
445 (or (setq apropos-item (assq symbol apropos-accumulator))
446 (setq apropos-item (list symbol nil nil)
447 apropos-accumulator (cons apropos-item
448 apropos-accumulator)))
449 (if apropos-match-face
450 (put-text-property beg end 'face apropos-match-face
452 (setcar (nthcdr (if this-is-a-variable 2 1)
458 (defun apropos-safe-documentation (function)
459 "Like documentation, except it avoids calling `get_doc_string'.
460 Will return nil instead."
461 (while (and function (symbolp function))
462 (setq function (if (fboundp function)
463 (symbol-function function))))
464 (if (eq (car-safe function) 'macro)
465 (setq function (cdr function)))
466 ;; XEmacs change from: (setq function (if (byte-code-function-p function)
467 (setq function (if (compiled-function-p function)
468 (if (fboundp 'compiled-function-doc-string)
469 (compiled-function-doc-string function)
470 (if (> (length function) 4)
472 (if (eq (car-safe function) 'autoload)
474 (if (eq (car-safe function) 'lambda)
475 (if (stringp (nth 2 function))
477 (if (stringp (nth 3 function))
478 (nth 3 function)))))))
479 (if (integerp function)
485 (defun apropos-print (do-keys doc-fn spacing)
486 "Output result of various apropos commands with `apropos-regexp'.
487 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
488 of apropos-accumulator and may modify it resulting in (symbol fn-doc
489 var-doc [plist-doc]). Returns sorted list of symbols and documentation
491 (if (null apropos-accumulator)
492 (message "No apropos matches for `%s'" apropos-regexp)
494 (funcall doc-fn apropos-accumulator))
495 (setq apropos-accumulator
496 (sort apropos-accumulator (lambda (a b)
497 (string-lessp (car a) (car b)))))
498 (and apropos-label-face
499 (or (symbolp apropos-label-face)
500 (facep apropos-label-face)) ; XEmacs
501 (setq apropos-label-face `(face ,apropos-label-face
502 mouse-face highlight)))
503 (let ((help-buffer-prefix-string "Apropos"))
504 (with-displaying-help-buffer
506 (with-current-buffer standard-output
507 (run-hooks 'apropos-mode-hook)
508 (let ((p apropos-accumulator)
509 (old-buffer (current-buffer))
510 symbol item point1 point2)
511 ;; XEmacs change from (if window-system
512 (if (device-on-window-system-p)
514 (princ "If you move the mouse over text that changes color,\n")
515 (princ (substitute-command-keys
516 "you can click \\[apropos-mouse-follow] to get more information.\n"))))
517 (princ (substitute-command-keys
518 "Type \\[apropos-follow] in this buffer to get full documentation.\n\n"))
520 (or (not spacing) (bobp) (terpri))
521 (setq apropos-item (car p)
522 symbol (car apropos-item)
525 (princ symbol) ; print symbol name
526 (setq point2 (point))
527 ;; Calculate key-bindings if we want them.
533 (set-buffer old-buffer)
534 (where-is-internal symbol)))
536 ;; Copy over the list of key sequences,
537 ;; omitting any that contain a buffer or a frame.
539 (let ((key (car keys))
542 (while (< i (length key))
543 (if (or (framep (aref key i))
544 (bufferp (aref key i)))
548 (setq filtered (cons key filtered))))
549 (setq keys (cdr keys)))
550 (setq item filtered))
551 ;; Convert the remaining keys to a string and insert.
555 (setq key (key-description key))
556 (if apropos-keybinding-face
557 (put-text-property 0 (length key)
558 'face apropos-keybinding-face
564 (put-text-property (- (point) 3) (point)
565 'face apropos-keybinding-face)
566 (princ (format " %s " (symbol-name symbol)))
568 (put-text-property (- (point) 3) (point)
569 'face apropos-keybinding-face)))
571 ;; only now so we don't propagate text attributes all over
572 (put-text-property point1 point2 'item
573 (if (eval `(or ,@(cdr apropos-item)))
576 (if apropos-symbol-face
577 (put-text-property point1 point2 'face apropos-symbol-face))
578 (apropos-print-doc 'describe-function 1
579 (if (commandp symbol)
581 (if (apropos-macrop symbol)
585 (if (get symbol 'custom-type)
586 (apropos-print-doc 'customize-variable-other-window 2
587 "User Option" do-keys)
588 (apropos-print-doc 'describe-variable 2
590 (apropos-print-doc 'customize-other-window 6 "Group" do-keys)
591 (apropos-print-doc 'customize-face-other-window 5 "Face" do-keys)
592 (apropos-print-doc 'widget-browse-other-window 4 "Widget" do-keys)
593 (apropos-print-doc 'apropos-describe-plist 3
596 (prog1 apropos-accumulator
597 (setq apropos-accumulator ())))) ; permit gc
600 (defun apropos-macrop (symbol)
601 "Return t if SYMBOL is a Lisp macro."
602 (and (fboundp symbol)
604 (symbol-function symbol)))
605 (or (eq (car symbol) 'macro)
606 (if (eq (car symbol) 'autoload)
611 (defun apropos-print-doc (action i str do-keys)
612 (with-current-buffer standard-output
613 (if (stringp (setq i (nth i apropos-item)))
616 (put-text-property (- (point) 2) (1- (point))
619 (if apropos-label-face
620 (add-text-properties (- (point) (length str) 2)
623 (add-text-properties (- (point) (length str) 2)
625 (list 'keymap apropos-mode-map))
626 (insert (if do-keys (substitute-command-keys i) i))
627 (or (bolp) (terpri))))))
629 (defun apropos-mouse-follow (event)
631 ;; XEmacs change: We're using the standard help buffer code now, don't
632 ;; do special tricks about trying to preserve current-buffer about mouse
636 ;; XEmacs change from:
637 ;; (set-buffer (window-buffer (posn-window (event-start event))))
638 ;; (goto-char (posn-point (event-start event)))
639 (set-buffer (event-buffer event))
640 (goto-char (event-closest-point event))
641 ;; XEmacs change: following code seems useless
642 ;;(or (and (not (eobp)) (get-text-property (point) 'mouse-face))
643 ;; (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
644 ;; (error "There is nothing to follow here"))
648 (defun apropos-follow (&optional other)
650 (let* (;; Properties are always found at the beginning of the line.
651 (bol (save-excursion (beginning-of-line) (point)))
652 ;; If there is no `item' property here, look behind us.
653 (item (get-text-property bol 'item))
654 (item-at (if item nil (previous-single-property-change bol 'item)))
655 ;; Likewise, if there is no `action' property here, look in front.
656 (action (get-text-property bol 'action))
657 (action-at (if action nil (next-single-property-change bol 'action))))
658 (and (null item) item-at
659 (setq item (get-text-property (1- item-at) 'item)))
660 (and (null action) action-at
661 (setq action (get-text-property action-at 'action)))
662 (if (not (and item action))
663 (error "There is nothing to follow here"))
664 (if (consp item) (error "There is nothing to follow in `%s'" (car item)))
665 (if other (set-buffer other))
666 (funcall action item)))
670 (defun apropos-describe-plist (symbol)
671 "Display a pretty listing of SYMBOL's plist."
672 (let ((help-buffer-prefix-string "Apropos-plist"))
673 (with-displaying-help-buffer
675 (run-hooks 'apropos-mode-hook)
678 (princ "'s plist is\n (")
679 (with-current-buffer standard-output
680 (if apropos-symbol-face
681 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face)))
682 (princ (apropos-format-plist symbol "\n "))
685 (print-help-return-message))
686 (symbol-name symbol))))
688 (provide 'apropos) ; XEmacs
690 ;;; apropos.el ends here