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@xemacs.org>
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs 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 ;; SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: Last synched with FSF 19.34, diverged since.
29 ;; The ideas for this package were derived from the C code in
30 ;; src/keymap.c and elsewhere. The functions in this file should
31 ;; always be byte-compiled for speed. Someone should rewrite this in
32 ;; C (as part of src/keymap.c) for speed.
34 ;; The idea for super-apropos is based on the original implementation
35 ;; by Lynn Slater <lrs@esl.com>.
39 ;; Fixed bug, current-local-map can return nil.
40 ;; Change, doesn't calculate key-bindings unless needed.
41 ;; Added super-apropos capability, changed print functions.
42 ;;; Made fast-apropos and super-apropos share code.
43 ;;; Sped up fast-apropos again.
44 ;; Added apropos-do-all option.
45 ;;; Added fast-command-apropos.
46 ;; Changed doc strings to comments for helping functions.
47 ;;; Made doc file buffer read-only, buried it.
48 ;; Only call substitute-command-keys if do-all set.
50 ;; Optionally use configurable faces to make the output more legible.
51 ;; Differentiate between command, function and macro.
52 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
53 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
54 ;; Apropos-documentation (ex super-apropos) now finds all it should.
55 ;; New apropos-value snoops through all values and optionally plists.
56 ;; Reading DOC file doesn't load nroff.
57 ;; Added hypertext following of documentation, mouse-2 on variable gives value
58 ;; from buffer in active window.
59 ;; Added apropos-rewrite-regexp from FSF apropos.
62 ;; I see a degradation of maybe 10-20% only.
63 ;; [sb -- FSF protects the face declarations with `if window-system'
64 ;; I see no reason why we should do so]
65 (defvar apropos-do-all nil
66 "*Whether the apropos commands should do more.
67 Slows them down more or less. Set this non-nil if you have a fast machine.")
70 (defvar apropos-symbol-face (if-boundp 'font-lock-keyword-face
71 font-lock-keyword-face
73 "*Face for symbol name in apropos output or `nil'.
74 This looks good, but slows down the commands several times.")
77 (defvar apropos-keybinding-face (if-boundp 'font-lock-string-face
80 "*Face for keybinding display in apropos output or `nil'.
81 This looks good, but slows down the commands several times.")
84 (defvar apropos-label-face (if-boundp 'font-lock-comment-face
85 font-lock-comment-face
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.")
94 (defvar apropos-property-face (if-boundp 'font-lock-variable-name-face
95 font-lock-variable-name-face
97 "*Face for property name in apropos output or `nil'.
98 This looks good, but slows down the commands several times.")
100 (defvar apropos-match-face 'secondary-selection
101 "*Face for matching part in apropos-documentation/value output or `nil'.
102 This looks good, but slows down the commands several times.")
105 (defvar apropos-mode-map
106 (let ((map (make-sparse-keymap)))
107 (define-key map [(control m)] 'apropos-follow)
108 (define-key map [return] 'apropos-follow)
109 (define-key map [(button2up)] 'apropos-mouse-follow)
110 (define-key map [(button2)] 'undefined)
112 "Keymap used in Apropos mode.")
115 (defvar apropos-regexp nil
116 "Regexp used in current apropos run.")
118 (defvar apropos-files-scanned ()
119 "List of elc files already scanned in current run of `apropos-documentation'.")
121 (defvar apropos-accumulator ()
122 "Alist of symbols already found in current apropos run.")
124 (defvar apropos-item ()
125 "Current item in or for apropos-accumulator.")
127 (defvar apropos-synonyms '(
128 ("find" "open" "edit")
131 "List of synonyms known by apropos.
132 Each element is a list of words where the first word is the standard emacs
133 term, and the rest of the words are alternative terms.")
136 (defvar apropos-mode-hook nil) ; XEmacs
138 (defcustom apropos-rewrite-regexps nil ; SXEmacs
139 "*Non-nil mean regexps with spaces are rewritten to match all words permutations."
142 (defun apropos-mode ()
143 "Major mode for following hyperlinks in output of apropos commands.
145 \\{apropos-mode-map}"
147 (kill-all-local-variables)
148 (use-local-map apropos-mode-map)
149 (setq major-mode 'apropos-mode
151 (run-hooks 'apropos-mode-hook)) ; XEmacs
153 (defun apropos-words-to-regexp (words wild)
154 "Make regexp matching any two of the words in WORDS."
156 (mapconcat 'identity words "\\|")
161 (mapconcat 'identity words "\\|")
165 (defun apropos-rewrite-regexp (regexp)
166 "Rewrite a list of words to a regexp matching all permutations.
167 If REGEXP is already a regexp, don't modify it.
168 Also if `apropos-rewrite-regexps' is nil, don't modify it."
169 (with-boundp '(apropos-orig-regexp apropos-words apropos-all-words
171 (if (not apropos-rewrite-regexps)
173 (setq apropos-orig-regexp regexp)
174 (setq apropos-words () apropos-all-words ())
175 (if (string-equal (regexp-quote regexp) regexp)
176 ;; We don't actually make a regexp matching all permutations.
177 ;; Instead, for e.g. "a b c", we make a regexp matching
178 ;; any combination of two or more words like this:
179 ;; (a|b|c).*(a|b|c) which may give some false matches,
180 ;; but as long as it also gives the right ones, that's ok.
181 (let ((words (split-string regexp "[ \t]+")))
183 (let ((syn apropos-synonyms) (s word) (a word))
185 (if (member word (car syn))
187 (setq a (mapconcat 'identity (car syn) "\\|"))
188 (if (member word (cdr (car syn)))
191 (setq syn (cdr syn))))
192 (setq apropos-words (cons s apropos-words)
193 apropos-all-words (cons a apropos-all-words))))
194 (setq apropos-all-regexp (apropos-words-to-regexp apropos-all-words ".+"))
195 (apropos-words-to-regexp apropos-words ".*?"))
196 (setq apropos-all-regexp regexp)))))
198 ;; For auld lang syne:
200 (fset 'command-apropos 'apropos-command)
203 (defun apropos-command (apropos-regexp &optional do-all)
204 "Shows commands (interactively callable functions) that match REGEXP.
205 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
207 ;; XEmacs: All code related to special treatment of buffer has been removed
208 (interactive (list (read-string (concat "Apropos command "
209 (if (or current-prefix-arg
214 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
215 (or do-all (setq do-all apropos-do-all))
216 (setq apropos-accumulator
217 (apropos-internal apropos-regexp
219 (lambda (symbol) (or (commandp symbol)
220 (user-variable-p symbol)))
228 (setq symbol (car p))
229 (if (commandp symbol)
231 ;; XEmacs change: if obsolete,
232 ;; only mention that.
233 (or (function-obsoleteness-doc symbol)
235 (documentation symbol t)
236 (void-function "(aliased to undefined function)")
237 (error "(unexpected error from `documention')"))))
238 (substring doc 0 (string-match "\n" doc))
241 (user-variable-p symbol)
244 ;; XEmacs change: if obsolete,
245 ;; only mention that.
246 (variable-obsoleteness-doc symbol)
247 (documentation-property
248 symbol 'variable-documentation t)))
250 (string-match "\n" doc))))))
256 (defun apropos (apropos-regexp &optional do-all)
257 "Show all bound symbols whose names match REGEXP.
258 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
259 symbols and key bindings, which is a little more time-consuming.
260 Returns list of symbols and documentation found."
261 (interactive "sApropos symbol (regexp): \nP")
262 ;; XEmacs change: hitting ENTER by mistake is a common mess-up and
263 ;; shouldn't make Emacs hang for a long time trying to list all symbols.
264 (or (> (length apropos-regexp) 0)
265 (error "Must pass non-empty regexp to `apropos'"))
266 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
267 (setq apropos-accumulator
268 (apropos-internal apropos-regexp
275 (symbol-plist symbol))))))
277 (or do-all apropos-do-all)
282 (setq symbol (car p))
285 ;; XEmacs change: if obsolete,
286 ;; only mention that.
287 (or (function-obsoleteness-doc symbol)
289 (documentation symbol t)
290 (void-function "(aliased to undefined function)")
291 (error "(unexpected error from `documention')"))))
292 (substring doc 0 (string-match "\n" doc))
297 ;; XEmacs change: if obsolete,
298 ;; only mention that.
299 (variable-obsoleteness-doc symbol)
300 (documentation-property
301 symbol 'variable-documentation t)))
303 (string-match "\n" doc))
305 (if (setq doc (symbol-plist symbol))
306 (if (eq (/ (length doc) 2) 1)
307 (format "1 property (%s)" (car doc))
308 (format "%d properties" (/ (length doc) 2))))
309 (if (get symbol 'widget-type)
310 (if (setq doc (documentation-property
311 symbol 'widget-documentation t))
313 (string-match "\n" doc))
315 (if (find-face symbol)
316 (if (setq doc (face-doc-string symbol))
318 (string-match "\n" doc))
320 (when (get symbol 'custom-group)
321 (if (setq doc (documentation-property
322 symbol 'group-documentation t))
324 (string-match "\n" doc))
325 "(not documented)"))))
331 (defun apropos-value (apropos-regexp &optional do-all)
332 "Show all symbols whose value's printed image matches REGEXP.
333 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
334 at the function and at the names and values of properties.
335 Returns list of symbols and values found."
336 (interactive "sApropos value (regexp): \nP")
337 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
338 (or do-all (setq do-all apropos-do-all))
339 (setq apropos-accumulator ())
343 (setq f nil v nil p nil)
344 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
346 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
348 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
349 p (apropos-format-plist symbol "\n " t)))
351 (setq apropos-accumulator (cons (list symbol f v p)
352 apropos-accumulator))))))
353 (apropos-print nil nil t))
357 (defun apropos-documentation (apropos-regexp &optional do-all)
358 "Show symbols whose documentation contain matches for REGEXP.
359 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
360 documentation that is not stored in the documentation file and show key
362 Returns list of symbols and documentation found."
363 (interactive "sApropos documentation (regexp): \nP")
364 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
365 (or do-all (setq do-all apropos-do-all))
366 (setq apropos-accumulator () apropos-files-scanned ())
367 (let ((standard-input (get-buffer-create " apropos-temp"))
371 (set-buffer standard-input)
372 (apropos-documentation-check-doc-file)
376 (setq f (apropos-safe-documentation symbol)
377 v (get symbol 'variable-documentation))
378 (when (integerp v) (setq v nil))
379 (setq f (apropos-documentation-internal f)
380 v (apropos-documentation-internal v))
382 (if (setq apropos-item
383 (cdr (assq symbol apropos-accumulator)))
386 (setcar apropos-item f))
388 (setcar (cdr apropos-item) v)))
389 (setq apropos-accumulator
390 (cons (list symbol f v)
391 apropos-accumulator)))))))
392 (apropos-print nil nil t))
393 (kill-buffer standard-input))))
396 (defun apropos-value-internal (predicate symbol function)
397 (if (funcall predicate symbol)
399 (setq symbol (prin1-to-string (funcall function symbol)))
400 (if (string-match apropos-regexp symbol)
402 (if apropos-match-face
403 (put-text-property (match-beginning 0) (match-end 0)
404 'face apropos-match-face
408 (defun apropos-documentation-internal (doc)
410 (apropos-documentation-check-elc-file (car doc))
412 (string-match apropos-regexp doc)
414 (if apropos-match-face
415 (put-text-property (match-beginning 0)
417 'face apropos-match-face
418 (setq doc (copy-sequence doc))))
421 (defun apropos-format-plist (pl sep &optional compare)
422 (setq pl (symbol-plist pl))
425 (setq p (format "%s %S" (car pl) (nth 1 pl)))
426 (if (or (not compare) (string-match apropos-regexp p))
427 (if apropos-property-face
428 (put-text-property 0 (length (symbol-name (car pl)))
429 'face apropos-property-face p))
433 (and compare apropos-match-face
434 (put-text-property (match-beginning 0) (match-end 0)
435 'face apropos-match-face
437 (setq p-out (concat p-out (if p-out sep) p))))
438 (setq pl (nthcdr 2 pl)))
442 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
444 (defun apropos-documentation-check-doc-file ()
445 (let (type symbol (sepa 2) sepb start end doc)
448 (insert-file-contents (concat doc-directory internal-doc-file-name))
450 (while (save-excursion
451 (setq sepb (search-forward "\^_"))
453 (beginning-of-line 2)
454 (if (save-restriction
455 (narrow-to-region (point) (1- sepb))
456 (re-search-forward apropos-regexp nil t))
458 (setq start (match-beginning 0)
460 (goto-char (1+ sepa))
461 (or (setq type (if (eq ?F (preceding-char))
462 1 ; function documentation
463 2) ; variable documentation
465 start (- start (point) 1)
466 end (- end (point) 1)
467 doc (buffer-substring (1+ (point)) (1- sepb))
468 apropos-item (assq symbol apropos-accumulator))
469 (setq apropos-item (list symbol nil nil)
470 apropos-accumulator (cons apropos-item
471 apropos-accumulator)))
472 (if apropos-match-face
473 (put-text-property start end 'face apropos-match-face doc))
474 (setcar (nthcdr type apropos-item) doc)))
475 (setq sepa (goto-char sepb)))))
477 (defun apropos-documentation-check-elc-file (file)
478 (if (member file apropos-files-scanned)
480 (let (symbol doc start end this-is-a-variable)
481 (setq apropos-files-scanned (cons file apropos-files-scanned))
483 (insert-file-contents file)
484 (while (search-forward "\n#@" nil t)
485 ;; Read the comment length, and advance over it.
488 end (+ (point) end -1))
490 (if (save-restriction
491 ;; match ^ and $ relative to doc string
492 (narrow-to-region start end)
493 (re-search-forward apropos-regexp nil t))
495 (goto-char (+ end 2))
496 (setq doc (buffer-substring start end)
497 end (- (match-end 0) start)
498 start (- (match-beginning 0) start)
499 this-is-a-variable (looking-at #r"(def\(var\|const\) ")
501 (skip-chars-forward "(a-z")
504 symbol (if (consp symbol)
507 (if (if this-is-a-variable
508 (get symbol 'variable-documentation)
509 (and (fboundp symbol) (apropos-safe-documentation symbol)))
511 (or (setq apropos-item (assq symbol apropos-accumulator))
512 (setq apropos-item (list symbol nil nil)
513 apropos-accumulator (cons apropos-item
514 apropos-accumulator)))
515 (if apropos-match-face
516 (put-text-property start end 'face apropos-match-face
518 (setcar (nthcdr (if this-is-a-variable 2 1)
524 (defun apropos-safe-documentation (function)
525 "Like documentation, except it avoids calling `get_doc_string'.
526 Will return nil instead."
527 (while (and function (symbolp function))
528 (setq function (if (fboundp function)
529 (symbol-function function))))
530 (if (eq (car-safe function) 'macro)
531 (setq function (cdr function)))
532 ;; XEmacs change from: (setq function (if (byte-code-function-p function)
533 (setq function (if (compiled-function-p function)
534 (if (fboundp 'compiled-function-doc-string)
535 (compiled-function-doc-string function)
536 (if (> (length function) 4)
538 (if (eq (car-safe function) 'autoload)
540 (if (eq (car-safe function) 'lambda)
541 (if (stringp (nth 2 function))
543 (if (stringp (nth 3 function))
544 (nth 3 function)))))))
545 (if (integerp function)
551 (defun apropos-print (do-keys doc-fn spacing)
552 "Output result of various apropos commands with `apropos-regexp'.
553 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
554 of apropos-accumulator and may modify it resulting in (symbol fn-doc
555 var-doc [plist-doc]). Returns sorted list of symbols and documentation
557 (if (null apropos-accumulator)
558 (message "No apropos matches for `%s'" apropos-regexp)
560 (funcall doc-fn apropos-accumulator))
561 (setq apropos-accumulator
562 (sort apropos-accumulator (lambda (a b)
563 (string-lessp (car a) (car b)))))
564 (and apropos-label-face
565 (or (symbolp apropos-label-face)
566 (facep apropos-label-face)) ; XEmacs
567 (setq apropos-label-face `(face ,apropos-label-face
568 mouse-face highlight)))
569 (let ((help-buffer-prefix-string "Apropos"))
570 (with-displaying-help-buffer
572 (with-current-buffer standard-output
573 (run-hooks 'apropos-mode-hook)
574 (let ((p apropos-accumulator)
575 (old-buffer (current-buffer))
576 symbol item point1 point2)
577 ;; Mostly useless but to provide better keymap
578 ;; explanation. help-mode-map will be used instead.
579 (use-local-map apropos-mode-map)
580 ;; XEmacs change from (if window-system
581 (if (device-on-window-system-p)
583 (princ "If you move the mouse over text that changes color,\n")
584 (princ (substitute-command-keys
585 "you can click \\[apropos-mouse-follow] to get more information.\n"))))
586 (princ (substitute-command-keys
587 "Type \\[apropos-follow] in this buffer to get full documentation.\n\n"))
589 (or (not spacing) (bobp) (terpri))
590 (setq apropos-item (car p)
591 symbol (car apropos-item)
594 (princ symbol) ; print symbol name
595 (setq point2 (point))
596 ;; Calculate key-bindings if we want them.
602 (set-buffer old-buffer)
603 (where-is-internal symbol)))
605 ;; Copy over the list of key sequences,
606 ;; omitting any that contain a buffer or a frame.
608 (let ((key (car keys))
611 (while (< i (length key))
612 (if (or (framep (aref key i))
613 (bufferp (aref key i)))
617 (setq filtered (cons key filtered))))
618 (setq keys (cdr keys)))
619 (setq item filtered))
620 ;; Convert the remaining keys to a string and insert.
624 (setq key (key-description key))
625 (if apropos-keybinding-face
626 (put-text-property 0 (length key)
627 'face apropos-keybinding-face
633 (put-text-property (- (point) 3) (point)
634 'face apropos-keybinding-face)
635 (princ (format " %s " (symbol-name symbol)))
637 (put-text-property (- (point) 3) (point)
638 'face apropos-keybinding-face)))
640 ;; only now so we don't propagate text attributes all over
641 (put-text-property point1 point2 'item
642 (if (eval `(or ,@(cdr apropos-item)))
645 (if apropos-symbol-face
646 (put-text-property point1 point2 'face apropos-symbol-face))
647 ;; Add text-property on symbol, too.
648 (put-text-property point1 point2 'keymap apropos-mode-map)
649 (apropos-print-doc 'describe-function 1
650 (if (commandp symbol)
652 (if (apropos-macrop symbol)
656 (if (get symbol 'custom-type)
657 (apropos-print-doc 'customize-variable-other-window 2
658 "User Option" do-keys)
659 (apropos-print-doc 'describe-variable 2
661 (apropos-print-doc 'customize-other-window 6 "Group" do-keys)
662 (apropos-print-doc 'customize-face-other-window 5 "Face" do-keys)
663 (apropos-print-doc 'widget-browse-other-window 4 "Widget" do-keys)
664 (apropos-print-doc 'apropos-describe-plist 3
667 (prog1 apropos-accumulator
668 (setq apropos-accumulator ())))) ; permit gc
671 (defun apropos-macrop (symbol)
672 "Return t if SYMBOL is a Lisp macro."
673 (and (fboundp symbol)
675 (symbol-function symbol)))
676 (or (eq (car symbol) 'macro)
677 (if (eq (car symbol) 'autoload)
682 (defun apropos-print-doc (action i str do-keys)
683 (with-current-buffer standard-output
684 (if (stringp (setq i (nth i apropos-item)))
687 (put-text-property (- (point) 2) (1- (point))
690 (if apropos-label-face
691 (add-text-properties (- (point) (length str) 2)
694 (add-text-properties (- (point) (length str) 2)
696 (list 'keymap apropos-mode-map))
697 (insert (if do-keys (substitute-command-keys i) i))
698 (or (bolp) (terpri))))))
700 (defun apropos-mouse-follow (event)
702 ;; XEmacs change: We're using the standard help buffer code now, don't
703 ;; do special tricks about trying to preserve current-buffer about mouse
707 ;; XEmacs change from:
708 ;; (set-buffer (window-buffer (posn-window (event-start event))))
709 ;; (goto-char (posn-point (event-start event)))
710 (set-buffer (event-buffer event))
711 (goto-char (event-closest-point event))
712 ;; XEmacs change: following code seems useless
713 ;;(or (and (not (eobp)) (get-text-property (point) 'mouse-face))
714 ;; (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
715 ;; (error "There is nothing to follow here"))
719 (defun apropos-follow (&optional other)
721 (let* (;; Properties are always found at the beginning of the line.
722 (bol (save-excursion (beginning-of-line) (point)))
723 ;; If there is no `item' property here, look behind us.
724 (item (get-text-property bol 'item))
725 (item-at (if item nil (previous-single-property-change bol 'item)))
726 ;; Likewise, if there is no `action' property here, look in front.
727 (action (get-text-property bol 'action))
728 (action-at (if action nil (next-single-property-change bol 'action))))
729 (and (null item) item-at
730 (setq item (get-text-property (1- item-at) 'item)))
731 (and (null action) action-at
732 (setq action (get-text-property action-at 'action)))
733 (if (not (and item action))
734 (error "There is nothing to follow here"))
735 (if (consp item) (error "There is nothing to follow in `%s'" (car item)))
736 (if other (set-buffer other))
737 (funcall action item)))
741 (defun apropos-describe-plist (symbol)
742 "Display a pretty listing of SYMBOL's plist."
743 (let ((help-buffer-prefix-string "Apropos-plist"))
744 (with-displaying-help-buffer
746 (run-hooks 'apropos-mode-hook)
749 (princ "'s plist is\n (")
750 (with-current-buffer standard-output
751 (if apropos-symbol-face
752 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face)))
753 (princ (apropos-format-plist symbol "\n "))
756 (print-help-return-message))
757 (symbol-name symbol))))
759 (provide 'apropos) ; XEmacs
761 ;;; apropos.el ends here