comparison lisp/packages/apropos.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
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 ;; Keywords: help
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
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.
25
26 ;;; Synched up with: FSF 19.31.
27
28 ;;; Commentary:
29
30 ;; The ideas for this package were derived from the C code in
31 ;; src/keymap.c and elsewhere. The functions in this file should
32 ;; always be byte-compiled for speed. Someone should rewrite this in
33 ;; C (as part of src/keymap.c) for speed.
34
35 ;; The idea for super-apropos is based on the original implementation
36 ;; by Lynn Slater <lrs@esl.com>.
37
38 ;; History:
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
60 ;;; Code:
61
62 ;; I see a degradation of maybe 10-20% only.
63 (defvar apropos-do-all nil
64 "*Whether the apropos commands should do more.
65 Slows them down more or less. Set this non-nil if you have a fast machine.")
66
67 (defvar apropos-symbol-face 'bold
68 "*Face for symbol name in apropos output or `nil'.
69 This looks good, but slows down the commands several times.")
70
71 (defvar apropos-keybinding-face 'underline
72 "*Face for keybinding display in apropos output or `nil'.
73 This looks good, but slows down the commands several times.")
74
75 (defvar apropos-label-face 'italic
76 "*Face for label (Command, Variable ...) in apropos output or `nil'.
77 If this is `nil' no mouse highlighting occurs.
78 This looks good, but slows down the commands several times.
79 When this is a face name, as it is initially, it gets transformed to a
80 text-property list for efficiency.")
81
82 (defvar apropos-property-face 'bold-italic
83 "*Face for property name in apropos output or `nil'.
84 This looks good, but slows down the commands several times.")
85
86 (defvar apropos-match-face 'secondary-selection
87 "*Face for matching part in apropos-documentation/value output or `nil'.
88 This looks good, but slows down the commands several times.")
89
90
91 (defvar apropos-mode-map
92 (let ((map (make-sparse-keymap)))
93 (define-key map "\C-m" 'apropos-follow)
94 (define-key map 'button2up 'apropos-mouse-follow)
95 (define-key map 'button2 'undefined)
96 map)
97 "Keymap used in Apropos mode.")
98
99
100 (defvar apropos-regexp nil
101 "Regexp used in current apropos run.")
102
103 (defvar apropos-files-scanned ()
104 "List of elc files already scanned in current run of `apropos-documentation'.")
105
106 (defvar apropos-accumulator ()
107 "Alist of symbols already found in current apropos run.")
108
109 (defvar apropos-item ()
110 "Current item in or for apropos-accumulator.")
111
112 (defun apropos-mode ()
113 "Major mode for following hyperlinks in output of apropos commands.
114
115 \\{apropos-mode-map}"
116 (interactive)
117 (kill-all-local-variables)
118 (use-local-map apropos-mode-map)
119 (setq major-mode 'apropos-mode
120 mode-name "Apropos"))
121
122
123 ;; For auld lang syne:
124 ;;;###autoload
125 (fset 'command-apropos 'apropos-command)
126 ;;;###autoload
127 (defun apropos-command (apropos-regexp &optional do-all)
128 "Shows commands (interactively callable functions) that match REGEXP.
129 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
130 variables."
131 (interactive (list (read-string (concat "Apropos command "
132 (if (or current-prefix-arg
133 apropos-do-all)
134 "or variable ")
135 "(regexp): "))
136 current-prefix-arg))
137 (let ((message
138 (let ((standard-output (get-buffer-create "*Apropos*")))
139 (print-help-return-message 'identity))))
140 (or do-all (setq do-all apropos-do-all))
141 (setq apropos-accumulator
142 (apropos-internal apropos-regexp
143 (if do-all
144 (lambda (symbol) (or (commandp symbol)
145 (user-variable-p symbol)))
146 'commandp)))
147 (if (apropos-print
148 t
149 (lambda (p)
150 (let (doc symbol)
151 (while p
152 (setcar p (list
153 (setq symbol (car p))
154 (if (commandp symbol)
155 (if (setq doc
156 ;; XEmacs change: if obsolete,
157 ;; only mention that.
158 (or (function-obsoleteness-doc symbol)
159 (documentation symbol t)))
160 (substring doc 0 (string-match "\n" doc))
161 "(not documented)"))
162 (and do-all
163 (user-variable-p symbol)
164 (if (setq doc
165 (or
166 ;; XEmacs change: if obsolete,
167 ;; only mention that.
168 (variable-obsoleteness-doc symbol)
169 (documentation-property
170 symbol 'variable-documentation t)))
171 (substring doc 0
172 (string-match "\n" doc))))))
173 (setq p (cdr p)))))
174 nil)
175 (and message (message message)))))
176
177
178 ;;;###autoload
179 (defun apropos (apropos-regexp &optional do-all)
180 "Show all bound symbols whose names match REGEXP.
181 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
182 symbols and key bindings, which is a little more time-consuming.
183 Returns list of symbols and documentation found."
184 (interactive "sApropos symbol (regexp): \nP")
185 ;; XEmacs change: hitting ENTER by mistake is a common mess-up and
186 ;; shouldn't make Emacs hang for a long time trying to list all symbols.
187 (or (> (length apropos-regexp) 0)
188 (error "Must pass non-empty regexp to `apropos'"))
189 (setq apropos-accumulator
190 (apropos-internal apropos-regexp
191 (and (not do-all)
192 (not apropos-do-all)
193 (lambda (symbol)
194 (or (fboundp symbol)
195 (boundp symbol)
196 (symbol-plist symbol))))))
197 (apropos-print
198 (or do-all apropos-do-all)
199 (lambda (p)
200 (let (symbol doc)
201 (while p
202 (setcar p (list
203 (setq symbol (car p))
204 (if (fboundp symbol)
205 (if (setq doc
206 ;; XEmacs change: if obsolete,
207 ;; only mention that.
208 (or (function-obsoleteness-doc symbol)
209 (documentation symbol t)))
210 (substring doc 0 (string-match "\n" doc))
211 "(not documented)"))
212 (if (boundp symbol)
213 (if (setq doc
214 (or
215 ;; XEmacs change: if obsolete,
216 ;; only mention that.
217 (variable-obsoleteness-doc symbol)
218 (documentation-property
219 symbol 'variable-documentation t)))
220 (substring doc 0
221 (string-match "\n" doc))
222 "(not documented)"))
223 (if (setq doc (symbol-plist symbol))
224 (if (eq (/ (length doc) 2) 1)
225 (format "1 property (%s)" (car doc))
226 (concat (/ (length doc) 2) " properties")))))
227 (setq p (cdr p)))))
228 nil))
229
230
231 ;;;###autoload
232 (defun apropos-value (apropos-regexp &optional do-all)
233 "Show all symbols whose value's printed image matches REGEXP.
234 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
235 at the function and at the names and values of properties.
236 Returns list of symbols and values found."
237 (interactive "sApropos value (regexp): \nP")
238 (or do-all (setq do-all apropos-do-all))
239 (setq apropos-accumulator ())
240 (let (f v p)
241 (mapatoms
242 (lambda (symbol)
243 (setq f nil v nil p nil)
244 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
245 symbol f v p))
246 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
247 (if do-all
248 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
249 p (apropos-format-plist symbol "\n " t)))
250 (if (or f v p)
251 (setq apropos-accumulator (cons (list symbol f v p)
252 apropos-accumulator))))))
253 (apropos-print nil nil t))
254
255
256 ;;;###autoload
257 (defun apropos-documentation (apropos-regexp &optional do-all)
258 "Show symbols whose documentation contain matches for REGEXP.
259 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
260 documentation that is not stored in the documentation file and show key
261 bindings.
262 Returns list of symbols and documentation found."
263 (interactive "sApropos documentation (regexp): \nP")
264 (or do-all (setq do-all apropos-do-all))
265 (setq apropos-accumulator () apropos-files-scanned ())
266 (let ((standard-input (get-buffer-create " apropos-temp"))
267 f v)
268 (unwind-protect
269 (save-excursion
270 (set-buffer standard-input)
271 (apropos-documentation-check-doc-file)
272 (if do-all
273 (mapatoms
274 (lambda (symbol)
275 (setq f (apropos-safe-documentation symbol)
276 v (get symbol 'variable-documentation))
277 (if (integerp v) (setq v))
278 (setq f (apropos-documentation-internal f)
279 v (apropos-documentation-internal v))
280 (if (or f v)
281 (if (setq apropos-item
282 (cdr (assq symbol apropos-accumulator)))
283 (progn
284 (if f
285 (setcar apropos-item f))
286 (if v
287 (setcar (cdr apropos-item) v)))
288 (setq apropos-accumulator
289 (cons (list symbol f v)
290 apropos-accumulator)))))))
291 (apropos-print nil nil t))
292 (kill-buffer standard-input))))
293
294
295 (defun apropos-value-internal (predicate symbol function)
296 (if (funcall predicate symbol)
297 (progn
298 (setq symbol (prin1-to-string (funcall function symbol)))
299 (if (string-match apropos-regexp symbol)
300 (progn
301 (if apropos-match-face
302 (put-text-property (match-beginning 0) (match-end 0)
303 'face apropos-match-face
304 symbol))
305 symbol)))))
306
307 (defun apropos-documentation-internal (doc)
308 (if (consp doc)
309 (apropos-documentation-check-elc-file (car doc))
310 (and doc
311 (string-match apropos-regexp doc)
312 (progn
313 (if apropos-match-face
314 (put-text-property (match-beginning 0)
315 (match-end 0)
316 'face apropos-match-face
317 (setq doc (copy-sequence doc))))
318 doc))))
319
320 (defun apropos-format-plist (pl sep &optional compare)
321 (setq pl (symbol-plist pl))
322 (let (p p-out)
323 (while pl
324 (setq p (format "%s %S" (car pl) (nth 1 pl)))
325 (if (or (not compare) (string-match apropos-regexp p))
326 (if apropos-property-face
327 (put-text-property 0 (length (symbol-name (car pl)))
328 'face apropos-property-face p))
329 (setq p nil))
330 (if p
331 (progn
332 (and compare apropos-match-face
333 (put-text-property (match-beginning 0) (match-end 0)
334 'face apropos-match-face
335 p))
336 (setq p-out (concat p-out (if p-out sep) p))))
337 (setq pl (nthcdr 2 pl)))
338 p-out))
339
340
341 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
342
343 (defun apropos-documentation-check-doc-file ()
344 (let (type symbol (sepa 2) sepb beg end)
345 (insert ?\^_)
346 (backward-char)
347 (insert-file-contents (concat doc-directory internal-doc-file-name))
348 (forward-char)
349 (while (save-excursion
350 (setq sepb (search-forward "\^_"))
351 (not (eobp)))
352 (beginning-of-line 2)
353 (if (save-restriction
354 (narrow-to-region (point) (1- sepb))
355 (re-search-forward apropos-regexp nil t))
356 (progn
357 (setq beg (match-beginning 0)
358 end (point))
359 (goto-char (1+ sepa))
360 (or (setq type (if (eq ?F (preceding-char))
361 1 ; function documentation
362 2) ; variable documentation
363 symbol (read)
364 beg (- beg (point) 1)
365 end (- end (point) 1)
366 doc (buffer-substring (1+ (point)) (1- sepb))
367 apropos-item (assq symbol apropos-accumulator))
368 (setq apropos-item (list symbol nil nil)
369 apropos-accumulator (cons apropos-item
370 apropos-accumulator)))
371 (if apropos-match-face
372 (put-text-property beg end 'face apropos-match-face doc))
373 (setcar (nthcdr type apropos-item) doc)))
374 (setq sepa (goto-char sepb)))))
375
376 (defun apropos-documentation-check-elc-file (file)
377 (if (member file apropos-files-scanned)
378 nil
379 (let (symbol doc beg end this-is-a-variable)
380 (setq apropos-files-scanned (cons file apropos-files-scanned))
381 (erase-buffer)
382 (insert-file-contents file)
383 (while (search-forward "\n#@" nil t)
384 ;; Read the comment length, and advance over it.
385 (setq end (read)
386 beg (1+ (point))
387 end (+ (point) end -1))
388 (forward-char)
389 (if (save-restriction
390 ;; match ^ and $ relative to doc string
391 (narrow-to-region beg end)
392 (re-search-forward apropos-regexp nil t))
393 (progn
394 (goto-char (+ end 2))
395 (setq doc (buffer-substring beg end)
396 end (- (match-end 0) beg)
397 beg (- (match-beginning 0) beg)
398 this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
399 symbol (progn
400 (skip-chars-forward "(a-z")
401 (forward-char)
402 (read))
403 symbol (if (consp symbol)
404 (nth 1 symbol)
405 symbol))
406 (if (if this-is-a-variable
407 (get symbol 'variable-documentation)
408 (and (fboundp symbol) (apropos-safe-documentation symbol)))
409 (progn
410 (or (setq apropos-item (assq symbol apropos-accumulator))
411 (setq apropos-item (list symbol nil nil)
412 apropos-accumulator (cons apropos-item
413 apropos-accumulator)))
414 (if apropos-match-face
415 (put-text-property beg end 'face apropos-match-face
416 doc))
417 (setcar (nthcdr (if this-is-a-variable 2 1)
418 apropos-item)
419 doc)))))))))
420
421
422
423 (defun apropos-safe-documentation (function)
424 "Like documentation, except it avoids calling `get_doc_string'.
425 Will return nil instead."
426 (while (and function (symbolp function))
427 (setq function (if (fboundp function)
428 (symbol-function function))))
429 (if (eq (car-safe function) 'macro)
430 (setq function (cdr function)))
431 ;; XEmacs change from: (setq function (if (byte-code-function-p function)
432 (setq function (if (compiled-function-p function)
433 (if (> (length function) 4)
434 (aref function 4))
435 (if (eq (car-safe function) 'autoload)
436 (nth 2 function)
437 (if (eq (car-safe function) 'lambda)
438 (if (stringp (nth 2 function))
439 (nth 2 function)
440 (if (stringp (nth 3 function))
441 (nth 3 function)))))))
442 (if (integerp function)
443 nil
444 function))
445
446
447
448 (defun apropos-print (do-keys doc-fn spacing)
449 "Output result of various apropos commands with `apropos-regexp'.
450 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
451 of apropos-accumulator and may modify it resulting in (symbol fn-doc
452 var-doc [plist-doc]). Returns sorted list of symbols and documentation
453 found."
454 (if (null apropos-accumulator)
455 (message "No apropos matches for `%s'" apropos-regexp)
456 (if doc-fn
457 (funcall doc-fn apropos-accumulator))
458 (setq apropos-accumulator
459 (sort apropos-accumulator (lambda (a b)
460 (string-lessp (car a) (car b)))))
461 (and apropos-label-face
462 (symbolp apropos-label-face)
463 (setq apropos-label-face `(face ,apropos-label-face
464 mouse-face highlight)))
465 (with-output-to-temp-buffer "*Apropos*"
466 (let ((p apropos-accumulator)
467 (old-buffer (current-buffer))
468 symbol item point1 point2)
469 (set-buffer standard-output)
470 (apropos-mode)
471 ;; XEmacs change from (if window-system
472 (if (device-on-window-system-p)
473 (insert "If you move the mouse over text that changes color,\n"
474 (substitute-command-keys
475 "you can click \\[apropos-mouse-follow] to get more information.\n")))
476 (insert (substitute-command-keys
477 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
478 (while (consp p)
479 (or (not spacing) (bobp) (terpri))
480 (setq apropos-item (car p)
481 symbol (car apropos-item)
482 p (cdr p)
483 point1 (point))
484 (princ symbol) ; print symbol name
485 (setq point2 (point))
486 ;; Calculate key-bindings if we want them.
487 (and do-keys
488 (commandp symbol)
489 (indent-to 30 1)
490 (if (let ((keys
491 (save-excursion
492 (set-buffer old-buffer)
493 (where-is-internal symbol)))
494 filtered)
495 ;; Copy over the list of key sequences,
496 ;; omitting any that contain a buffer or a frame.
497 (while keys
498 (let ((key (car keys))
499 (i 0)
500 loser)
501 (while (< i (length key))
502 (if (or (framep (aref key i))
503 (bufferp (aref key i)))
504 (setq loser t))
505 (setq i (1+ i)))
506 (or loser
507 (setq filtered (cons key filtered))))
508 (setq keys (cdr keys)))
509 (setq item filtered))
510 ;; Convert the remaining keys to a string and insert.
511 (insert
512 (mapconcat
513 (lambda (key)
514 (setq key (key-description key))
515 (if apropos-keybinding-face
516 (put-text-property 0 (length key)
517 'face apropos-keybinding-face
518 key))
519 key)
520 item ", "))
521 (insert "Type ")
522 (insert "M-x")
523 (put-text-property (- (point) 3) (point)
524 'face apropos-keybinding-face)
525 (insert " " (symbol-name symbol) " ")
526 (insert "RET")
527 (put-text-property (- (point) 3) (point)
528 'face apropos-keybinding-face)))
529 (terpri)
530 ;; only now so we don't propagate text attributes all over
531 (put-text-property point1 point2 'item
532 (if (eval `(or ,@(cdr apropos-item)))
533 (car apropos-item)
534 apropos-item))
535 (if apropos-symbol-face
536 (put-text-property point1 point2 'face apropos-symbol-face))
537 (apropos-print-doc 'describe-function 1
538 (if (commandp symbol)
539 "Command"
540 (if (apropos-macrop symbol)
541 "Macro"
542 "Function"))
543 do-keys)
544 (apropos-print-doc 'describe-variable 2
545 "Variable" do-keys)
546 (apropos-print-doc 'apropos-describe-plist 3
547 "Plist" nil)))))
548 (prog1 apropos-accumulator
549 (setq apropos-accumulator ()))) ; permit gc
550
551
552 (defun apropos-macrop (symbol)
553 "T if SYMBOL is a Lisp macro."
554 (and (fboundp symbol)
555 (consp (setq symbol
556 (symbol-function symbol)))
557 (or (eq (car symbol) 'macro)
558 (if (eq (car symbol) 'autoload)
559 (memq (nth 4 symbol)
560 '(macro t))))))
561
562
563 (defun apropos-print-doc (action i str do-keys)
564 (if (stringp (setq i (nth i apropos-item)))
565 (progn
566 (insert " ")
567 (put-text-property (- (point) 2) (1- (point))
568 'action action)
569 (insert str ": ")
570 (if apropos-label-face
571 (add-text-properties (- (point) (length str) 2)
572 (1- (point))
573 apropos-label-face))
574 (insert (if do-keys (substitute-command-keys i) i))
575 (or (bolp) (terpri)))))
576
577 (defun apropos-mouse-follow (event)
578 (interactive "e")
579 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
580 ()
581 (current-buffer))))
582 (save-excursion
583 ;; XEmacs change from:
584 ;; (set-buffer (window-buffer (posn-window (event-start event))))
585 ;; (goto-char (posn-point (event-start event)))
586 (set-buffer (event-buffer event))
587 (goto-char (event-closest-point event))
588 ;; XEmacs change: following code seems useless
589 ;;(or (and (not (eobp)) (get-text-property (point) 'mouse-face))
590 ;; (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
591 ;; (error "There is nothing to follow here"))
592 (apropos-follow other))))
593
594
595 (defun apropos-follow (&optional other)
596 (interactive)
597 (let* (;; Properties are always found at the beginning of the line.
598 (bol (save-excursion (beginning-of-line) (point)))
599 ;; If there is no `item' property here, look behind us.
600 (item (get-text-property bol 'item))
601 (item-at (if item nil (previous-single-property-change bol 'item)))
602 ;; Likewise, if there is no `action' property here, look in front.
603 (action (get-text-property bol 'action))
604 (action-at (if action nil (next-single-property-change bol 'action))))
605 (and (null item) item-at
606 (setq item (get-text-property (1- item-at) 'item)))
607 (and (null action) action-at
608 (setq action (get-text-property action-at 'action)))
609 (if (not (and item action))
610 (error "There is nothing to follow here"))
611 (if (consp item) (error "There is nothing to follow in `%s'" (car item)))
612 (if other (set-buffer other))
613 (funcall action item)))
614
615
616
617 (defun apropos-describe-plist (symbol)
618 "Display a pretty listing of SYMBOL's plist."
619 (with-output-to-temp-buffer "*Help*"
620 (set-buffer standard-output)
621 (princ "Symbol ")
622 (prin1 symbol)
623 (princ "'s plist is\n (")
624 (if apropos-symbol-face
625 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
626 (insert (apropos-format-plist symbol "\n "))
627 (princ ")")
628 (print-help-return-message)))
629
630 ;;; apropos.el ends here