More warning fixes from Nelson
[sxemacs] / lisp / apropos.el
1 ;;; apropos.el --- apropos commands for users and programmers.
2
3 ;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc.
4
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>
8 ;; Keywords: help
9
10 ;; This file is part of SXEmacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Synched up with: Last synched with FSF 19.34, diverged since.
26
27 ;;; Commentary:
28
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.
33
34 ;; The idea for super-apropos is based on the original implementation
35 ;; by Lynn Slater <lrs@esl.com>.
36
37 ;;; ChangeLog:
38
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.
49
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.
60 ;;; Code:
61
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.")
68
69 ;; XEmacs addition
70 (defvar apropos-symbol-face (if-boundp 'font-lock-keyword-face
71                                 font-lock-keyword-face
72                               'bold)
73   "*Face for symbol name in apropos output or `nil'.
74 This looks good, but slows down the commands several times.")
75
76 ;; XEmacs addition
77 (defvar apropos-keybinding-face (if-boundp 'font-lock-string-face
78                                     font-lock-string-face
79                                   'underline)
80   "*Face for keybinding display in apropos output or `nil'.
81 This looks good, but slows down the commands several times.")
82
83 ;; XEmacs addition
84 (defvar apropos-label-face (if-boundp 'font-lock-comment-face
85                                font-lock-comment-face
86                              '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.")
92
93 ;; XEmacs addition
94 (defvar apropos-property-face (if-boundp 'font-lock-variable-name-face
95                                   font-lock-variable-name-face
96                                 'bold-italic)
97   "*Face for property name in apropos output or `nil'.
98 This looks good, but slows down the commands several times.")
99
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.")
103
104
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)
111     map)
112   "Keymap used in Apropos mode.")
113
114
115 (defvar apropos-regexp nil
116   "Regexp used in current apropos run.")
117
118 (defvar apropos-files-scanned ()
119   "List of elc files already scanned in current run of `apropos-documentation'.")
120
121 (defvar apropos-accumulator ()
122   "Alist of symbols already found in current apropos run.")
123
124 (defvar apropos-item ()
125   "Current item in or for apropos-accumulator.")
126
127 (defvar apropos-synonyms '(
128   ("find" "open" "edit")
129   ("kill" "cut")
130   ("yank" "paste"))
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.")
134
135 \f
136 (defvar apropos-mode-hook nil) ; XEmacs
137
138 (defcustom apropos-rewrite-regexps nil  ; SXEmacs
139   "*Non-nil mean regexps with spaces are rewritten to match all words permutations."
140   :type 'boolean)
141
142 (defun apropos-mode ()
143   "Major mode for following hyperlinks in output of apropos commands.
144
145 \\{apropos-mode-map}"
146   (interactive)
147   (kill-all-local-variables)
148   (use-local-map apropos-mode-map)
149   (setq major-mode 'apropos-mode
150         mode-name "Apropos")
151   (run-hooks 'apropos-mode-hook)) ; XEmacs
152
153 (defun apropos-words-to-regexp (words wild)
154   "Make regexp matching any two of the words in WORDS."
155   (concat "\\("
156           (mapconcat 'identity words "\\|")
157           "\\)"
158           (if (cdr words)
159               (concat wild
160                       "\\("
161                       (mapconcat 'identity words "\\|")
162                       "\\)")
163             "")))
164
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
170                                      apropos-all-regexp)
171     (if (not apropos-rewrite-regexps)
172         regexp
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]+")))
182             (dolist (word words)
183               (let ((syn apropos-synonyms) (s word) (a word))
184                 (while syn
185                   (if (member word (car syn))
186                       (progn
187                         (setq a (mapconcat 'identity (car syn) "\\|"))
188                         (if (member word (cdr (car syn)))
189                             (setq s a))
190                         (setq syn nil))
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)))))
197
198 ;; For auld lang syne:
199 ;;;###autoload
200 (fset 'command-apropos 'apropos-command)
201
202 ;;;###autoload
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
206 variables."
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
210                                                   apropos-do-all)
211                                               "or variable ")
212                                           "(regexp): "))
213                      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
218                           (if do-all
219                               (lambda (symbol) (or (commandp symbol)
220                                                    (user-variable-p symbol)))
221                             'commandp)))
222   (apropos-print
223    t
224    (lambda (p)
225      (let (doc symbol)
226        (while p
227          (setcar p (list
228                     (setq symbol (car p))
229                     (if (commandp symbol)
230                         (if (setq doc
231                                   ;; XEmacs change: if obsolete,
232                                   ;; only mention that.
233                                   (or (function-obsoleteness-doc symbol)
234                                       (condition-case nil
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))
239                           "(not documented)"))
240                     (and do-all
241                          (user-variable-p symbol)
242                          (if (setq doc
243                                    (or
244                                     ;; XEmacs change: if obsolete,
245                                     ;; only mention that.
246                                     (variable-obsoleteness-doc symbol)
247                                     (documentation-property
248                                      symbol 'variable-documentation t)))
249                              (substring doc 0
250                                             (string-match "\n" doc))))))
251          (setq p (cdr p)))))
252    nil))
253
254
255 ;;;###autoload
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
269                           (and (not do-all)
270                                (not apropos-do-all)
271                                (lambda (symbol)
272                                  (or (fboundp symbol)
273                                      (boundp symbol)
274                                      (find-face symbol)
275                                      (symbol-plist symbol))))))
276   (apropos-print
277    (or do-all apropos-do-all)
278    (lambda (p)
279      (let (symbol doc)
280        (while p
281          (setcar p (list
282                     (setq symbol (car p))
283                     (if (fboundp symbol)
284                         (if (setq doc
285                                   ;; XEmacs change: if obsolete,
286                                   ;; only mention that.
287                                   (or (function-obsoleteness-doc symbol)
288                                       (condition-case nil
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))
293                           "(not documented)"))
294                     (if (boundp symbol)
295                         (if (setq doc
296                                   (or
297                                    ;; XEmacs change: if obsolete,
298                                    ;; only mention that.
299                                    (variable-obsoleteness-doc symbol)
300                                    (documentation-property
301                                     symbol 'variable-documentation t)))
302                             (substring doc 0
303                                        (string-match "\n" doc))
304                           "(not documented)"))
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))
312                             (substring doc 0
313                                        (string-match "\n" doc))
314                           "(not documented)"))
315                     (if (find-face symbol)
316                         (if (setq doc (face-doc-string symbol))
317                             (substring doc 0
318                                        (string-match "\n" doc))
319                           "(not documented)"))
320                     (when (get symbol 'custom-group)
321                       (if (setq doc (documentation-property
322                                      symbol 'group-documentation t))
323                           (substring doc 0
324                                      (string-match "\n" doc))
325                         "(not documented)"))))
326          (setq p (cdr p)))))
327    nil))
328
329
330 ;;;###autoload
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 ())
340    (let (f v p)
341      (mapatoms
342       (lambda (symbol)
343         (setq f nil v nil p nil)
344         (or (memq symbol '(apropos-regexp do-all apropos-accumulator
345                                           symbol f v p))
346             (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
347         (if do-all
348             (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
349                   p (apropos-format-plist symbol "\n    " t)))
350         (if (or f v p)
351             (setq apropos-accumulator (cons (list symbol f v p)
352                                             apropos-accumulator))))))
353   (apropos-print nil nil t))
354
355
356 ;;;###autoload
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
361 bindings.
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"))
368         f v)
369     (unwind-protect
370         (save-excursion
371           (set-buffer standard-input)
372           (apropos-documentation-check-doc-file)
373           (if do-all
374               (mapatoms
375                (lambda (symbol)
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))
381                  (if (or f v)
382                      (if (setq apropos-item
383                                (cdr (assq symbol apropos-accumulator)))
384                          (progn
385                            (if f
386                                (setcar apropos-item f))
387                            (if v
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))))
394
395 \f
396 (defun apropos-value-internal (predicate symbol function)
397   (if (funcall predicate symbol)
398       (progn
399         (setq symbol (prin1-to-string (funcall function symbol)))
400         (if (string-match apropos-regexp symbol)
401             (progn
402               (if apropos-match-face
403                   (put-text-property (match-beginning 0) (match-end 0)
404                                      'face apropos-match-face
405                                      symbol))
406               symbol)))))
407
408 (defun apropos-documentation-internal (doc)
409   (if (consp doc)
410       (apropos-documentation-check-elc-file (car doc))
411     (and doc
412          (string-match apropos-regexp doc)
413          (progn
414            (if apropos-match-face
415                (put-text-property (match-beginning 0)
416                                   (match-end 0)
417                                   'face apropos-match-face
418                                   (setq doc (copy-sequence doc))))
419            doc))))
420
421 (defun apropos-format-plist (pl sep &optional compare)
422   (setq pl (symbol-plist pl))
423   (let (p p-out)
424     (while 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))
430         (setq p nil))
431       (if p
432           (progn
433             (and compare apropos-match-face
434                  (put-text-property (match-beginning 0) (match-end 0)
435                                     'face apropos-match-face
436                                     p))
437             (setq p-out (concat p-out (if p-out sep) p))))
438       (setq pl (nthcdr 2 pl)))
439     p-out))
440
441
442 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
443
444 (defun apropos-documentation-check-doc-file ()
445   (let (type symbol (sepa 2) sepb start end doc)
446     (insert ?\^_)
447     (backward-char)
448     (insert-file-contents (concat doc-directory internal-doc-file-name))
449     (forward-char)
450     (while (save-excursion
451              (setq sepb (search-forward "\^_"))
452              (not (eobp)))
453       (beginning-of-line 2)
454       (if (save-restriction
455             (narrow-to-region (point) (1- sepb))
456             (re-search-forward apropos-regexp nil t))
457           (progn
458             (setq start (match-beginning 0)
459                   end (point))
460             (goto-char (1+ sepa))
461             (or (setq type (if (eq ?F (preceding-char))
462                                1        ; function documentation
463                              2)         ; variable documentation
464                       symbol (read)
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)))))
476
477 (defun apropos-documentation-check-elc-file (file)
478   (if (member file apropos-files-scanned)
479       nil
480     (let (symbol doc start end this-is-a-variable)
481       (setq apropos-files-scanned (cons file apropos-files-scanned))
482       (erase-buffer)
483       (insert-file-contents file)
484       (while (search-forward "\n#@" nil t)
485         ;; Read the comment length, and advance over it.
486         (setq end (read)
487               start (1+ (point))
488               end (+ (point) end -1))
489         (forward-char)
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))
494             (progn
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\) ")
500                     symbol (progn
501                              (skip-chars-forward "(a-z")
502                              (forward-char)
503                              (read))
504                     symbol (if (consp symbol)
505                                (nth 1 symbol)
506                              symbol))
507               (if (if this-is-a-variable
508                       (get symbol 'variable-documentation)
509                     (and (fboundp symbol) (apropos-safe-documentation symbol)))
510                   (progn
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
517                                            doc))
518                     (setcar (nthcdr (if this-is-a-variable 2 1)
519                                     apropos-item)
520                             doc)))))))))
521
522
523
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)
537                            (aref function 4)))
538                    (if (eq (car-safe function) 'autoload)
539                        (nth 2 function)
540                      (if (eq (car-safe function) 'lambda)
541                          (if (stringp (nth 2 function))
542                              (nth 2 function)
543                            (if (stringp (nth 3 function))
544                                (nth 3 function)))))))
545   (if (integerp function)
546       nil
547     function))
548
549
550
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
556 found."
557   (if (null apropos-accumulator)
558       (message "No apropos matches for `%s'" apropos-regexp)
559     (if doc-fn
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
571        (lambda ()
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)
582                  (progn
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"))
588              (while (consp p)
589                (or (not spacing) (bobp) (terpri))
590                (setq apropos-item (car p)
591                      symbol (car apropos-item)
592                      p (cdr p)
593                      point1 (point))
594                (princ symbol)           ; print symbol name
595                (setq point2 (point))
596                ;; Calculate key-bindings if we want them.
597                (and do-keys
598                     (commandp symbol)
599                     (indent-to 30 1)
600                     (if (let ((keys
601                                (save-excursion
602                                  (set-buffer old-buffer)
603                                  (where-is-internal symbol)))
604                               filtered)
605                           ;; Copy over the list of key sequences,
606                           ;; omitting any that contain a buffer or a frame.
607                           (while keys
608                             (let ((key (car keys))
609                                   (i 0)
610                                   loser)
611                               (while (< i (length key))
612                                 (if (or (framep (aref key i))
613                                         (bufferp (aref key i)))
614                                     (setq loser t))
615                                 (setq i (1+ i)))
616                               (or loser
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.
621                         (princ
622                          (mapconcat
623                           (lambda (key)
624                             (setq key (key-description key))
625                             (if apropos-keybinding-face
626                                 (put-text-property 0 (length key)
627                                                    'face apropos-keybinding-face
628                                                    key))
629                             key)
630                           item ", "))
631                       (princ "Type ")
632                       (princ "M-x")
633                       (put-text-property (- (point) 3) (point)
634                                          'face apropos-keybinding-face)
635                       (princ (format " %s " (symbol-name symbol)))
636                       (princ "RET")
637                       (put-text-property (- (point) 3) (point)
638                                          'face apropos-keybinding-face)))
639                (terpri)
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)))
643                                       (car apropos-item)
644                                     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)
651                                       "Command"
652                                     (if (apropos-macrop symbol)
653                                         "Macro"
654                                       "Function"))
655                                   do-keys)
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
660                                     "Variable" do-keys))
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
665                                   "Plist" nil)))))
666        apropos-regexp))
667     (prog1 apropos-accumulator
668       (setq apropos-accumulator ()))))  ; permit gc
669
670
671 (defun apropos-macrop (symbol)
672   "Return t if SYMBOL is a Lisp macro."
673   (and (fboundp symbol)
674        (consp (setq symbol
675                     (symbol-function symbol)))
676        (or (eq (car symbol) 'macro)
677            (if (eq (car symbol) 'autoload)
678                (memq (nth 4 symbol)
679                      '(macro t))))))
680
681
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)))
685         (progn
686           (insert "  ")
687           (put-text-property (- (point) 2) (1- (point))
688                              'action action)
689           (insert str ": ")
690           (if apropos-label-face
691               (add-text-properties (- (point) (length str) 2)
692                                    (1- (point))
693                                    apropos-label-face))
694           (add-text-properties (- (point) (length str) 2)
695                                (1- (point))
696                                (list 'keymap apropos-mode-map))
697           (insert (if do-keys (substitute-command-keys i) i))
698           (or (bolp) (terpri))))))
699
700 (defun apropos-mouse-follow (event)
701   (interactive "e")
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
704   ;; clicks.
705
706   (save-excursion
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"))
716     (apropos-follow)))
717
718
719 (defun apropos-follow (&optional other)
720   (interactive)
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)))
738
739
740
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
745      (lambda ()
746        (run-hooks 'apropos-mode-hook)
747        (princ "Symbol ")
748        (prin1 symbol)
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  "))
754        (princ ")")
755        (terpri)
756        (print-help-return-message))
757      (symbol-name symbol))))
758
759 (provide 'apropos) ; XEmacs
760
761 ;;; apropos.el ends here