]> code.delx.au - gnu-emacs/blob - lisp/apropos.el
(apropos-print): Fix message about mouse command.
[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 'secondary-selection)
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-mode-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 "Keymap used in Apropos mode.")
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-documentation'.")
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 (defun apropos-mode ()
111 "Major mode for following hyperlinks in output of apropos commands.
112
113 \\{apropos-mode-map}"
114 (interactive)
115 (kill-all-local-variables)
116 (use-local-map apropos-mode-map)
117 (setq major-mode 'apropos-mode
118 mode-name "Apropos"))
119
120
121 ;; For auld lang syne:
122 ;;;###autoload
123 (fset 'command-apropos 'apropos-command)
124 ;;;###autoload
125 (defun apropos-command (apropos-regexp &optional do-all)
126 "Shows commands (interactively callable functions) that match REGEXP.
127 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
128 variables."
129 (interactive (list (read-string (concat "Apropos command "
130 (if (or current-prefix-arg
131 apropos-do-all)
132 "or variable ")
133 "(regexp): "))
134 current-prefix-arg))
135 (let ((message
136 (let ((standard-output (get-buffer-create "*Apropos*")))
137 (print-help-return-message 'identity))))
138 (or do-all (setq do-all apropos-do-all))
139 (setq apropos-accumulator
140 (apropos-internal apropos-regexp
141 (if do-all
142 (lambda (symbol) (or (commandp symbol)
143 (user-variable-p symbol)))
144 'commandp)))
145 (if (apropos-print
146 t
147 (lambda (p)
148 (let (doc symbol)
149 (while p
150 (setcar p (list
151 (setq symbol (car p))
152 (if (commandp symbol)
153 (if (setq doc (documentation symbol t))
154 (substring doc 0 (string-match "\n" doc))
155 "(not documented)"))
156 (and do-all
157 (user-variable-p symbol)
158 (if (setq doc (documentation-property
159 symbol 'variable-documentation t))
160 (substring doc 0
161 (string-match "\n" doc))))))
162 (setq p (cdr p)))))
163 nil)
164 (and message (message message)))))
165
166
167 ;;;###autoload
168 (defun apropos (apropos-regexp &optional do-all)
169 "Show all bound symbols whose names match REGEXP.
170 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
171 symbols and key bindings, which is a little more time-consuming.
172 Returns list of symbols and documentation found."
173 (interactive "sApropos symbol (regexp): \nP")
174 (setq apropos-accumulator
175 (apropos-internal apropos-regexp
176 (and (not do-all)
177 (not apropos-do-all)
178 (lambda (symbol)
179 (or (fboundp symbol)
180 (boundp symbol)
181 (symbol-plist symbol))))))
182 (apropos-print
183 (or do-all apropos-do-all)
184 (lambda (p)
185 (let (symbol doc)
186 (while p
187 (setcar p (list
188 (setq symbol (car p))
189 (if (fboundp symbol)
190 (if (setq doc (documentation symbol t))
191 (substring doc 0 (string-match "\n" doc))
192 "(not documented)"))
193 (if (boundp symbol)
194 (if (setq doc (documentation-property
195 symbol 'variable-documentation t))
196 (substring doc 0
197 (string-match "\n" doc))
198 "(not documented)"))
199 (if (setq doc (symbol-plist symbol))
200 (if (eq (/ (length doc) 2) 1)
201 (format "1 property (%s)" (car doc))
202 (concat (/ (length doc) 2) " properties")))))
203 (setq p (cdr p)))))
204 nil))
205
206
207 ;;;###autoload
208 (defun apropos-value (apropos-regexp &optional do-all)
209 "Show all symbols whose value's printed image matches REGEXP.
210 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
211 at the function and at the names and values of properties.
212 Returns list of symbols and values found."
213 (interactive "sApropos value (regexp): \nP")
214 (or do-all (setq do-all apropos-do-all))
215 (setq apropos-accumulator ())
216 (let (f v p)
217 (mapatoms
218 (lambda (symbol)
219 (setq f nil v nil p nil)
220 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
221 symbol f v p))
222 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
223 (if do-all
224 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
225 p (apropos-format-plist symbol "\n " t)))
226 (if (or f v p)
227 (setq apropos-accumulator (cons (list symbol f v p)
228 apropos-accumulator))))))
229 (apropos-print nil nil t))
230
231
232 ;;;###autoload
233 (defun apropos-documentation (apropos-regexp &optional do-all)
234 "Show symbols whose documentation contain matches for REGEXP.
235 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
236 documentation that is not stored in the documentation file and show key
237 bindings.
238 Returns list of symbols and documentation found."
239 (interactive "sApropos documentation (regexp): \nP")
240 (or do-all (setq do-all apropos-do-all))
241 (setq apropos-accumulator () apropos-files-scanned ())
242 (let ((standard-input (get-buffer-create " apropos-temp"))
243 f v)
244 (unwind-protect
245 (save-excursion
246 (set-buffer standard-input)
247 (apropos-documentation-check-doc-file)
248 (if do-all
249 (mapatoms
250 (lambda (symbol)
251 (setq f (apropos-safe-documentation symbol)
252 v (get symbol 'variable-documentation))
253 (if (integerp v) (setq v))
254 (setq f (apropos-documentation-internal f)
255 v (apropos-documentation-internal v))
256 (if (or f v)
257 (if (setq apropos-item
258 (cdr (assq symbol apropos-accumulator)))
259 (progn
260 (if f
261 (setcar apropos-item f))
262 (if v
263 (setcar (cdr apropos-item) v)))
264 (setq apropos-accumulator
265 (cons (list symbol f v)
266 apropos-accumulator)))))))
267 (apropos-print nil nil t))
268 (kill-buffer standard-input))))
269
270 \f
271 (defun apropos-value-internal (predicate symbol function)
272 (if (funcall predicate symbol)
273 (progn
274 (setq symbol (prin1-to-string (funcall function symbol)))
275 (if (string-match apropos-regexp symbol)
276 (progn
277 (if apropos-match-face
278 (put-text-property (match-beginning 0) (match-end 0)
279 'face apropos-match-face
280 symbol))
281 symbol)))))
282
283 (defun apropos-documentation-internal (doc)
284 (if (consp doc)
285 (apropos-documentation-check-elc-file (car doc))
286 (and doc
287 (string-match apropos-regexp doc)
288 (progn
289 (if apropos-match-face
290 (put-text-property (match-beginning 0)
291 (match-end 0)
292 'face apropos-match-face
293 (setq doc (copy-sequence doc))))
294 doc))))
295
296 (defun apropos-format-plist (pl sep &optional compare)
297 (setq pl (symbol-plist pl))
298 (let (p p-out)
299 (while pl
300 (setq p (format "%s %S" (car pl) (nth 1 pl)))
301 (if (or (not compare) (string-match apropos-regexp p))
302 (if apropos-property-face
303 (put-text-property 0 (length (symbol-name (car pl)))
304 'face apropos-property-face p))
305 (setq p nil))
306 (if p
307 (progn
308 (and compare apropos-match-face
309 (put-text-property (match-beginning 0) (match-end 0)
310 'face apropos-match-face
311 p))
312 (setq p-out (concat p-out (if p-out sep) p))))
313 (setq pl (nthcdr 2 pl)))
314 p-out))
315
316
317 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
318
319 (defun apropos-documentation-check-doc-file ()
320 (let (type symbol (sepa 2) sepb beg end)
321 (insert ?\^_)
322 (backward-char)
323 (insert-file-contents (concat doc-directory internal-doc-file-name))
324 (forward-char)
325 (while (save-excursion
326 (setq sepb (search-forward "\^_"))
327 (not (eobp)))
328 (beginning-of-line 2)
329 (if (save-restriction
330 (narrow-to-region (point) (1- sepb))
331 (re-search-forward apropos-regexp nil t))
332 (progn
333 (setq beg (match-beginning 0)
334 end (point))
335 (goto-char (1+ sepa))
336 (or (setq type (if (eq ?F (preceding-char))
337 1 ; function documentation
338 2) ; variable documentation
339 symbol (read)
340 beg (- beg (point) 1)
341 end (- end (point) 1)
342 doc (buffer-substring (1+ (point)) (1- sepb))
343 apropos-item (assq symbol apropos-accumulator))
344 (setq apropos-item (list symbol nil nil)
345 apropos-accumulator (cons apropos-item
346 apropos-accumulator)))
347 (if apropos-match-face
348 (put-text-property beg end 'face apropos-match-face doc))
349 (setcar (nthcdr type apropos-item) doc)))
350 (setq sepa (goto-char sepb)))))
351
352 (defun apropos-documentation-check-elc-file (file)
353 (if (member file apropos-files-scanned)
354 nil
355 (let (symbol doc beg end this-is-a-variable)
356 (setq apropos-files-scanned (cons file apropos-files-scanned))
357 (erase-buffer)
358 (insert-file-contents file)
359 (while (search-forward "\n#@" nil t)
360 ;; Read the comment length, and advance over it.
361 (setq end (read)
362 beg (1+ (point))
363 end (+ (point) end -1))
364 (forward-char)
365 (if (save-restriction
366 ;; match ^ and $ relative to doc string
367 (narrow-to-region beg end)
368 (re-search-forward apropos-regexp nil t))
369 (progn
370 (goto-char (+ end 2))
371 (setq doc (buffer-substring beg end)
372 end (- (match-end 0) beg)
373 beg (- (match-beginning 0) beg)
374 this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
375 symbol (progn
376 (skip-chars-forward "(a-z")
377 (forward-char)
378 (read))
379 symbol (if (consp symbol)
380 (nth 1 symbol)
381 symbol))
382 (if (if this-is-a-variable
383 (get symbol 'variable-documentation)
384 (and (fboundp symbol) (apropos-safe-documentation symbol)))
385 (progn
386 (or (setq apropos-item (assq symbol apropos-accumulator))
387 (setq apropos-item (list symbol nil nil)
388 apropos-accumulator (cons apropos-item
389 apropos-accumulator)))
390 (if apropos-match-face
391 (put-text-property beg end 'face apropos-match-face
392 doc))
393 (setcar (nthcdr (if this-is-a-variable 2 1)
394 apropos-item)
395 doc)))))))))
396
397
398
399 (defun apropos-safe-documentation (function)
400 "Like documentation, except it avoids calling `get_doc_string'.
401 Will return nil instead."
402 (while (and function (symbolp function))
403 (setq function (if (fboundp function)
404 (symbol-function function))))
405 (if (eq (car-safe function) 'macro)
406 (setq function (cdr function)))
407 (setq function (if (byte-code-function-p function)
408 (if (> (length function) 4)
409 (aref function 4))
410 (if (eq (car-safe function) 'autoload)
411 (nth 2 function)
412 (if (eq (car-safe function) 'lambda)
413 (if (stringp (nth 2 function))
414 (nth 2 function)
415 (if (stringp (nth 3 function))
416 (nth 3 function)))))))
417 (if (integerp function)
418 nil
419 function))
420
421
422
423 (defun apropos-print (do-keys doc-fn spacing)
424 "Output result of various apropos commands with `apropos-regexp'.
425 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
426 of apropos-accumulator and may modify it resulting in (symbol fn-doc
427 var-doc [plist-doc]). Returns sorted list of symbols and documentation
428 found."
429 (if (null apropos-accumulator)
430 (message "No apropos matches for `%s'" apropos-regexp)
431 (if doc-fn
432 (funcall doc-fn apropos-accumulator))
433 (setq apropos-accumulator
434 (sort apropos-accumulator (lambda (a b)
435 (string-lessp (car a) (car b)))))
436 (and apropos-label-face
437 (symbolp apropos-label-face)
438 (setq apropos-label-face `(face ,apropos-label-face
439 mouse-face highlight)))
440 (with-output-to-temp-buffer "*Apropos*"
441 (let ((p apropos-accumulator)
442 (old-buffer (current-buffer))
443 symbol item point1 point2)
444 (set-buffer standard-output)
445 (apropos-mode)
446 (if window-system
447 (insert (substitute-command-keys
448 "If you move the mouse over text that changes color,\n"
449 "you can click \\[apropos-mouse-follow] to get more information.\n")))
450 (insert (substitute-command-keys
451 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
452 (while (consp p)
453 (or (not spacing) (bobp) (terpri))
454 (setq apropos-item (car p)
455 symbol (car apropos-item)
456 p (cdr p)
457 point1 (point))
458 (princ symbol) ; print symbol name
459 (setq point2 (point))
460 ;; don't calculate key-bindings unless needed
461 (and do-keys
462 (commandp symbol)
463 (indent-to 30 1)
464 (insert
465 (if (setq item (save-excursion
466 (set-buffer old-buffer)
467 (where-is-internal symbol)))
468 (mapconcat
469 (if apropos-keybinding-face
470 (lambda (key)
471 (setq key (key-description key))
472 (put-text-property 0 (length key)
473 'face apropos-keybinding-face
474 key)
475 key)
476 'key-description)
477 item ", ")
478 "(not bound to any keys)")))
479 (terpri)
480 ;; only now so we don't propagate text attributes all over
481 (put-text-property point1 point2 'item
482 (if (eval `(or ,@(cdr apropos-item)))
483 (car apropos-item)
484 apropos-item))
485 (if apropos-symbol-face
486 (put-text-property point1 point2 'face apropos-symbol-face))
487 (apropos-print-doc 'describe-function 1
488 (if (commandp symbol)
489 "Command"
490 (if (apropos-macrop symbol)
491 "Macro"
492 "Function"))
493 do-keys)
494 (apropos-print-doc 'describe-variable 2
495 "Variable" do-keys)
496 (apropos-print-doc 'apropos-describe-plist 3
497 "Plist" nil)))))
498 (prog1 apropos-accumulator
499 (setq apropos-accumulator ()))) ; permit gc
500
501
502 (defun apropos-macrop (symbol)
503 "T if SYMBOL is a Lisp macro."
504 (and (fboundp symbol)
505 (consp (setq symbol
506 (symbol-function symbol)))
507 (or (eq (car symbol) 'macro)
508 (if (eq (car symbol) 'autoload)
509 (memq (nth 4 symbol)
510 '(macro t))))))
511
512
513 (defun apropos-print-doc (action i str do-keys)
514 (if (stringp (setq i (nth i apropos-item)))
515 (progn
516 (insert " ")
517 (put-text-property (- (point) 2) (1- (point))
518 'action action)
519 (insert str ": ")
520 (if apropos-label-face
521 (add-text-properties (- (point) (length str) 2)
522 (1- (point))
523 apropos-label-face))
524 (insert (if do-keys (substitute-command-keys i) i))
525 (or (bolp) (terpri)))))
526
527
528 (defun apropos-mouse-follow (event)
529 (interactive "e")
530 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
531 ()
532 (current-buffer))))
533 (save-excursion
534 (set-buffer (window-buffer (posn-window (event-start event))))
535 (goto-char (posn-point (event-start event)))
536 (or (and (not (eobp)) (get-text-property (point) 'mouse-face))
537 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
538 (error "There is nothing to follow here"))
539 (apropos-follow other))))
540
541
542 (defun apropos-follow (&optional other)
543 (interactive)
544 (let* (;; Properties are always found at the beginning of the line.
545 (bol (save-excursion (beginning-of-line) (point)))
546 ;; If there is no `item' property here, look behind us.
547 (item (get-text-property bol 'item))
548 (item-at (if item nil (previous-single-property-change bol 'item)))
549 ;; Likewise, if there is no `action' property here, look in front.
550 (action (get-text-property bol 'action))
551 (action-at (if action nil (next-single-property-change bol 'action))))
552 (and (null item) item-at
553 (setq item (get-text-property (1- item-at) 'item)))
554 (and (null action) action-at
555 (setq action (get-text-property action-at 'action)))
556 (if (not (and item action))
557 (error "There is nothing to follow here"))
558 (if (consp item) (error "There is nothing to follow in `%s'" (car item)))
559 (if other (set-buffer other))
560 (funcall action item)))
561
562
563
564 (defun apropos-describe-plist (symbol)
565 "Display a pretty listing of SYMBOL's plist."
566 (with-output-to-temp-buffer "*Help*"
567 (set-buffer standard-output)
568 (princ "Symbol ")
569 (prin1 symbol)
570 (princ "'s plist is\n (")
571 (if apropos-symbol-face
572 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
573 (insert (apropos-format-plist symbol "\n "))
574 (princ ")")
575 (print-help-return-message)))
576
577 ;;; apropos.el ends here