]> code.delx.au - gnu-emacs/blob - lisp/apropos.el
(apropos-print): Use apropos-local-map as buffer's map,
[gnu-emacs] / 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 ;; Keywords: help
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; The ideas for this package were derived from the C code in
28 ;; src/keymap.c and elsewhere. The functions in this file should
29 ;; always be byte-compiled for speed. Someone should rewrite this in
30 ;; C (as part of src/keymap.c) for speed.
31
32 ;; The idea for super-apropos is based on the original implementation
33 ;; by Lynn Slater <lrs@esl.com>.
34
35 ;; History:
36 ;; Fixed bug, current-local-map can return nil.
37 ;; Change, doesn't calculate key-bindings unless needed.
38 ;; Added super-apropos capability, changed print functions.
39 ;;; Made fast-apropos and super-apropos share code.
40 ;;; Sped up fast-apropos again.
41 ;; Added apropos-do-all option.
42 ;;; Added fast-command-apropos.
43 ;; Changed doc strings to comments for helping functions.
44 ;;; Made doc file buffer read-only, buried it.
45 ;; Only call substitute-command-keys if do-all set.
46
47 ;; Optionally use configurable faces to make the output more legible.
48 ;; Differentiate between command, function and macro.
49 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
50 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
51 ;; Apropos-documentation (ex super-apropos) now finds all it should.
52 ;; New apropos-value snoops through all values and optionally plists.
53 ;; Reading DOC file doesn't load nroff.
54 ;; Added hypertext following of documentation, mouse-2 on variable gives value
55 ;; from buffer in active window.
56
57 ;;; Code:
58
59 ;; I see a degradation of maybe 10-20% only.
60 (defvar apropos-do-all nil
61 "*Whether the apropos commands should do more.
62 Slows them down more or less. Set this non-nil if you have a fast machine.")
63
64
65 (defvar apropos-symbol-face (if window-system 'bold)
66 "*Face for symbol name in apropos output or `nil'.
67 This looks good, but slows down the commands several times.")
68
69 (defvar apropos-keybinding-face (if window-system 'underline)
70 "*Face for keybinding display in apropos output or `nil'.
71 This looks good, but slows down the commands several times.")
72
73 (defvar apropos-label-face (if window-system 'italic)
74 "*Face for label (Command, Variable ...) in apropos output or `nil'.
75 If this is `nil' no mouse highlighting occurs.
76 This looks good, but slows down the commands several times.
77 When this is a face name, as it is initially, it gets transformed to a
78 text-property list for efficiency.")
79
80 (defvar apropos-property-face (if window-system 'bold-italic)
81 "*Face for property name in apropos output or `nil'.
82 This looks good, but slows down the commands several times.")
83
84 (defvar apropos-match-face (if window-system 'highlight)
85 "*Face for matching part in apropos-documentation/value output or `nil'.
86 This looks good, but slows down the commands several times.")
87
88
89 (defvar apropos-local-map
90 (let ((map (make-sparse-keymap)))
91 (define-key map "\C-m" 'apropos-follow)
92 (define-key map [mouse-2] 'apropos-mouse-follow)
93 (define-key map [down-mouse-2] nil)
94 map)
95 "Local map active when displaying apropos output.")
96
97
98 (defvar apropos-regexp nil
99 "Regexp used in current apropos run.")
100
101 (defvar apropos-files-scanned ()
102 "List of elc files already scanned in current run of `apropos-documentaion'.")
103
104 (defvar apropos-accumulator ()
105 "Alist of symbols already found in current apropos run.")
106
107 (defvar apropos-item ()
108 "Current item in or for apropos-accumulator.")
109 \f
110 ;; For auld lang syne:
111 ;;;###autoload
112 (fset 'command-apropos 'apropos-command)
113 ;;;###autoload
114 (defun apropos-command (apropos-regexp &optional do-all)
115 "Shows commands (interactively callable functions) that match REGEXP.
116 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
117 variables."
118 (interactive (list (read-string (concat "Apropos command "
119 (if (or current-prefix-arg
120 apropos-do-all)
121 "or variable ")
122 "(regexp): "))
123 current-prefix-arg))
124 (let ((message
125 (let ((standard-output (get-buffer-create "*Help*")))
126 (print-help-return-message 'identity))))
127 (or do-all (setq do-all apropos-do-all))
128 (setq apropos-accumulator
129 (apropos-internal apropos-regexp
130 (if do-all
131 (lambda (symbol) (or (commandp symbol)
132 (user-variable-p symbol)))
133 'commandp)))
134 (if (apropos-print
135 t
136 (lambda (p)
137 (let (doc symbol)
138 (while p
139 (setcar p (list
140 (setq symbol (car p))
141 (if (commandp symbol)
142 (if (setq doc (documentation symbol t))
143 (substring doc 0 (string-match "\n" doc))
144 "(not documented)"))
145 (and do-all
146 (user-variable-p symbol)
147 (if (setq doc (documentation-property
148 symbol 'variable-documentation t))
149 (substring doc 0
150 (string-match "\n" doc))))))
151 (setq p (cdr p)))))
152 nil)
153 (and message (message message)))))
154
155
156 ;;;###autoload
157 (defun apropos (apropos-regexp &optional do-all)
158 "Show all bound symbols whose names match REGEXP.
159 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
160 symbols and key bindings, which is a little more time-consuming.
161 Returns list of symbols and documentation found."
162 (interactive "sApropos symbol (regexp): \nP")
163 (setq apropos-accumulator
164 (apropos-internal apropos-regexp
165 (and (not do-all)
166 (not apropos-do-all)
167 (lambda (symbol)
168 (or (fboundp symbol)
169 (boundp symbol)
170 (symbol-plist symbol))))))
171 (apropos-print
172 (or do-all apropos-do-all)
173 (lambda (p)
174 (let (symbol doc)
175 (while p
176 (setcar p (list
177 (setq symbol (car p))
178 (if (fboundp symbol)
179 (if (setq doc (documentation symbol t))
180 (substring doc 0 (string-match "\n" doc))
181 "(not documented)"))
182 (if (boundp symbol)
183 (if (setq doc (documentation-property
184 symbol 'variable-documentation t))
185 (substring doc 0
186 (string-match "\n" doc))
187 "(not documented)"))
188 (if (setq doc (symbol-plist symbol))
189 (if (eq (setq doc (/ (length doc) 2)) 1)
190 "1 property"
191 (concat doc " properties")))))
192 (setq p (cdr p)))))
193 nil))
194
195
196 ;;;###autoload
197 (defun apropos-value (apropos-regexp &optional do-all)
198 "Show all symbols whose value's printed image matches REGEXP.
199 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
200 at the function and at the names and values of properties.
201 Returns list of symbols and values found."
202 (interactive "sApropos value (regexp): \nP")
203 (or do-all (setq do-all apropos-do-all))
204 (setq apropos-accumulator ())
205 (let (f v p)
206 (mapatoms
207 (lambda (symbol)
208 (setq f nil v nil p nil)
209 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
210 symbol f v p))
211 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
212 (if do-all
213 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
214 p (apropos-format-plist symbol "\n " t)))
215 (if (or f v p)
216 (setq apropos-accumulator (cons (list symbol f v p)
217 apropos-accumulator))))))
218 (apropos-print nil nil t))
219
220
221 ;;;###autoload
222 (defun apropos-documentation (apropos-regexp &optional do-all)
223 "Show symbols whose names or documentation contain matches for REGEXP.
224 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
225 documentation that is not stored in the documentation file and show key
226 bindings.
227 Returns list of symbols and documentation found."
228 (interactive "sApropos documentation (regexp): \nP")
229 (or do-all (setq do-all apropos-do-all))
230 (setq apropos-accumulator () apropos-files-scanned ())
231 (let ((standard-input (get-buffer-create " apropos-temp"))
232 f v)
233 (unwind-protect
234 (save-excursion
235 (set-buffer standard-input)
236 (apropos-documentation-check-doc-file)
237 (if do-all
238 (mapatoms
239 (lambda (symbol)
240 (setq f (apropos-safe-documentation symbol)
241 v (get symbol 'variable-documentation)
242 v (if (integerp v) nil v))
243 (or (string-match apropos-regexp (symbol-name symbol))
244 (setq f (apropos-documentation-internal f)
245 v (apropos-documentation-internal v)))
246 (if (or f v)
247 (if (setq apropos-item
248 (cdr (assq symbol apropos-accumulator)))
249 (progn
250 (if f
251 (setcar apropos-item f))
252 (if v
253 (setcar (cdr apropos-item) v)))
254 (setq apropos-accumulator
255 (cons (list symbol f v)
256 apropos-accumulator)))))))
257 (apropos-print do-all nil t))
258 (kill-buffer standard-input))))
259
260 \f
261 (defun apropos-value-internal (predicate symbol function)
262 (if (funcall predicate symbol)
263 (progn
264 (setq symbol (prin1-to-string (funcall function symbol)))
265 (if (string-match apropos-regexp symbol)
266 (progn
267 (if apropos-match-face
268 (put-text-property (match-beginning 0) (match-end 0)
269 'face apropos-match-face
270 symbol))
271 symbol)))))
272
273 (defun apropos-documentation-internal (doc)
274 (if (consp doc)
275 (apropos-documentation-check-elc-file (car doc))
276 (and doc
277 (string-match apropos-regexp doc)
278 (progn
279 (if apropos-match-face
280 (put-text-property (match-beginning 0)
281 (match-end 0)
282 'face apropos-match-face
283 (setq doc (copy-sequence doc))))
284 doc))))
285
286 (defun apropos-format-plist (pl sep &optional compare)
287 (setq pl (symbol-plist pl))
288 (let (p p-out)
289 (while pl
290 (setq p (format "%s %S" (car pl) (nth 1 pl)))
291 (if (or (not compare) (string-match apropos-regexp p))
292 (if apropos-property-face
293 (put-text-property 0 (length (symbol-name (car pl)))
294 'face apropos-property-face p))
295 (setq p nil))
296 (if p
297 (progn
298 (and compare apropos-match-face
299 (put-text-property (match-beginning 0) (match-end 0)
300 'face apropos-match-face
301 p))
302 (setq p-out (concat p-out (if p-out sep) p))))
303 (setq pl (nthcdr 2 pl)))
304 p-out))
305
306
307 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
308
309 (defun apropos-documentation-check-doc-file ()
310 (let (type symbol beg end)
311 (insert-file-contents (concat doc-directory internal-doc-file-name))
312 (while (re-search-forward apropos-regexp nil t)
313 (setq beg (match-beginning 0)
314 end (point))
315 (search-backward "\C-_")
316 (if (> (point) beg)
317 ()
318 (or (setq type (if (eq ?F (char-after (1+ (point))))
319 1 ;function documentation
320 2) ;variable documentation
321 symbol (prog2
322 (forward-char 2)
323 (read))
324 beg (- beg (point) 1)
325 end (- end (point) 1)
326 doc (buffer-substring
327 (1+ (point))
328 (if (search-forward "\C-_" nil 'move)
329 (1- (point))
330 (point)))
331 apropos-item (assq symbol apropos-accumulator))
332 (setq apropos-item (list symbol nil nil)
333 apropos-accumulator (cons apropos-item apropos-accumulator)))
334 (and apropos-match-face
335 (>= beg 0)
336 (put-text-property beg end 'face apropos-match-face doc))
337 (setcar (nthcdr type apropos-item) doc)))))
338
339 (defun apropos-documentation-check-elc-file (file)
340 (if (member file apropos-files-scanned)
341 nil
342 (let (symbol doc beg end end1 this-is-a-variable)
343 (setq apropos-files-scanned (cons file apropos-files-scanned))
344 (erase-buffer)
345 (insert-file-contents file)
346 (while (search-forward "\n#@" nil t)
347 ;; Read the comment length, and advance over it.
348 (setq end (read)
349 beg (point)
350 end (+ (point) end 1))
351 (if (re-search-forward apropos-regexp end t)
352 (progn
353 (goto-char end)
354 (setq doc (buffer-substring (1+ beg) (- end 2))
355 end1 (- (match-end 0) beg 1)
356 beg (- (match-beginning 0) beg 1)
357 this-is-a-variable (looking-at "(defvar\\|(defconst")
358 symbol (progn
359 (skip-chars-forward "(a-z")
360 (forward-char 1)
361 (read))
362 symbol (if (consp symbol)
363 (nth 1 symbol)
364 symbol))
365 (if (if this-is-a-variable
366 (get symbol 'variable-documentation)
367 (and (fboundp symbol) (apropos-safe-documentation symbol)))
368 (progn
369 (or (setq apropos-item (assq symbol apropos-accumulator))
370 (setq apropos-item (list symbol nil nil)
371 apropos-accumulator (cons apropos-item
372 apropos-accumulator)))
373 (if apropos-match-face
374 (put-text-property beg end1 'face apropos-match-face
375 doc))
376 (setcar (nthcdr (if this-is-a-variable 2 1)
377 apropos-item)
378 doc)))))
379 (goto-char end)))))
380
381
382
383 (defun apropos-safe-documentation (function)
384 "Like documentation, except it avoids calling `get_doc_string'.
385 Will return nil instead."
386 (while (and function (symbolp function))
387 (setq function (if (fboundp function)
388 (symbol-function function))))
389 (if (eq (car-safe function) 'macro)
390 (setq function (cdr function)))
391 (setq function (if (byte-code-function-p function)
392 (if (> (length function) 4)
393 (aref function 4))
394 (if (eq (car-safe function) 'autoload)
395 (nth 2 function)
396 (if (eq (car-safe function) 'lambda)
397 (if (stringp (nth 2 function))
398 (nth 2 function)
399 (if (stringp (nth 3 function))
400 (nth 3 function)))))))
401 (if (integerp function)
402 nil
403 function))
404
405
406
407 (defun apropos-print (do-keys doc-fn spacing)
408 "Output result of various apropos commands with `apropos-regexp'.
409 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
410 of apropos-accumulator and may modify it resulting in (symbol fn-doc
411 var-doc [plist-doc]). Returns sorted list of symbols and documentation
412 found."
413 (if (null apropos-accumulator)
414 (message "No apropos matches for `%s'" apropos-regexp)
415 (if doc-fn
416 (funcall doc-fn apropos-accumulator))
417 (setq apropos-accumulator
418 (sort apropos-accumulator (lambda (a b)
419 (string-lessp (car a) (car b)))))
420 (and apropos-label-face
421 (symbolp apropos-label-face)
422 (setq apropos-label-face `(face ,apropos-label-face
423 mouse-face highlight)))
424 (with-output-to-temp-buffer "*Apropos*"
425 (let ((p apropos-accumulator)
426 (old-buffer (current-buffer))
427 symbol item point1 point2)
428 (save-excursion
429 (set-buffer standard-output)
430 (if window-system
431 (insert (substitute-command-keys
432 "Click \\<apropos-local-map>\\[apropos-mouse-follow] to get full documentation.\n")))
433 (insert (substitute-command-keys
434 "In this buffer, type \\<apropos-local-map>\\[apropos-follow] to get full documentation.\n\n"))
435 (use-local-map apropos-local-map)
436 (while (consp p)
437 (or (not spacing) (bobp) (terpri))
438 (setq apropos-item (car p)
439 symbol (car apropos-item)
440 p (cdr p)
441 point1 (point))
442 (princ symbol) ;print symbol name
443 (setq point2 (point))
444 ;; don't calculate key-bindings unless needed
445 (and do-keys
446 (commandp symbol)
447 (indent-to 30 1)
448 (insert
449 (if (setq item (save-excursion
450 (set-buffer old-buffer)
451 (where-is-internal symbol)))
452 (mapconcat
453 (if apropos-keybinding-face
454 (lambda (key)
455 (setq key (key-description key))
456 (put-text-property 0 (length key)
457 'face apropos-keybinding-face
458 key)
459 key)
460 'key-description)
461 item ", ")
462 "(not bound to any keys)")))
463 (terpri)
464 ;; only now so we don't propagate text attributes all over
465 (put-text-property point1 (1+ point1) 'item
466 (if (eval `(or ,@(cdr apropos-item)))
467 (car apropos-item)
468 apropos-item))
469 (if apropos-symbol-face
470 (put-text-property point1 point2 'face apropos-symbol-face))
471 (apropos-print-doc 'describe-function 1
472 (if (commandp symbol)
473 "Command"
474 (if (apropos-macrop symbol)
475 "Macro"
476 "Function"))
477 do-keys)
478 (apropos-print-doc 'describe-variable 2
479 "Variable" do-keys)
480 (apropos-print-doc 'apropos-describe-plist 3
481 "Plist" nil))))))
482 (prog1 apropos-accumulator
483 (setq apropos-accumulator ()))) ; permit gc
484
485
486 (defun apropos-macrop (symbol)
487 "T if SYMBOL is a Lisp macro."
488 (and (fboundp symbol)
489 (consp (setq symbol
490 (symbol-function symbol)))
491 (or (eq (car symbol) 'macro)
492 (if (eq (car symbol) 'autoload)
493 (memq (nth 4 symbol)
494 '(macro t))))))
495
496
497 (defun apropos-print-doc (action i str do-keys)
498 (if (stringp (setq i (nth i apropos-item)))
499 (progn
500 (insert " ")
501 (put-text-property (- (point) 2) (1- (point))
502 'action action)
503 (insert str ": ")
504 (if apropos-label-face
505 (add-text-properties (- (point) (length str) 2)
506 (1- (point))
507 apropos-label-face))
508 (insert (if do-keys (substitute-command-keys i) i))
509 (or (bolp) (terpri)))))
510
511
512 (defun apropos-mouse-follow (event)
513 (interactive "e")
514 (let ((other (if (eq (current-buffer) (get-buffer "*Help*"))
515 ()
516 (current-buffer))))
517 (set-buffer (window-buffer (posn-window (event-start event))))
518 (goto-char (posn-point (event-start event)))
519 ;; somehow when clicking with the point in another window, undoes badly
520 (undo-boundary)
521 (apropos-follow other)))
522
523
524 (defun apropos-follow (&optional other)
525 (interactive)
526 (let ((point (point))
527 (item (get-text-property (point) 'item))
528 action action-point)
529 (or item
530 (setq item (if (bobp)
531 ()
532 (previous-single-property-change (point) 'item))
533 item (get-text-property
534 (1- (goto-char
535 (if item
536 item
537 (1+ (next-single-property-change (point) 'item)))))
538 'item)))
539 (if (consp item)
540 (error "%s is just a lonely symbol" (car item)))
541 (while (if (setq action-point
542 (next-single-property-change (point) 'action))
543 (<= action-point point))
544 (goto-char (1+ action-point))
545 (setq action action-point))
546 (funcall
547 (prog1 (get-text-property (or action action-point (point)) 'action)
548 (if other (set-buffer other)))
549 item)))
550
551
552
553 (defun apropos-describe-plist (symbol)
554 "Display a pretty listing of SYMBOL's plist."
555 (with-output-to-temp-buffer "*Help*"
556 (set-buffer standard-output)
557 (princ "Symbol ")
558 (prin1 symbol)
559 (princ "'s plist is\n (")
560 (if apropos-symbol-face
561 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
562 (insert (apropos-format-plist symbol "\n "))
563 (princ ")")))
564
565 ;;; apropos.el ends here