(universal-coding-system-argument): Improve prompt strings.
[emacs.git] / lisp / apropos.el
blob34691a977441b623142039c3af08da0b18f88e47
1 ;;; apropos.el --- apropos commands for users and programmers.
3 ;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Rewritten: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
7 ;; Keywords: help
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; The ideas for this package were derived from the C code in
29 ;; src/keymap.c and elsewhere. The functions in this file should
30 ;; always be byte-compiled for speed. Someone should rewrite this in
31 ;; C (as part of src/keymap.c) for speed.
33 ;; The idea for super-apropos is based on the original implementation
34 ;; by Lynn Slater <lrs@esl.com>.
36 ;; History:
37 ;; Fixed bug, current-local-map can return nil.
38 ;; Change, doesn't calculate key-bindings unless needed.
39 ;; Added super-apropos capability, changed print functions.
40 ;;; Made fast-apropos and super-apropos share code.
41 ;;; Sped up fast-apropos again.
42 ;; Added apropos-do-all option.
43 ;;; Added fast-command-apropos.
44 ;; Changed doc strings to comments for helping functions.
45 ;;; Made doc file buffer read-only, buried it.
46 ;; Only call substitute-command-keys if do-all set.
48 ;; Optionally use configurable faces to make the output more legible.
49 ;; Differentiate between command, function and macro.
50 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
51 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
52 ;; Apropos-documentation (ex super-apropos) now finds all it should.
53 ;; New apropos-value snoops through all values and optionally plists.
54 ;; Reading DOC file doesn't load nroff.
55 ;; Added hypertext following of documentation, mouse-2 on variable gives value
56 ;; from buffer in active window.
58 ;;; Code:
60 (defgroup apropos nil
61 "Apropos commands for users and programmers"
62 :group 'Help
63 :prefix "apropos")
65 ;; I see a degradation of maybe 10-20% only.
66 (defcustom apropos-do-all nil
67 "*Whether the apropos commands should do more.
69 Slows them down more or less. Set this non-nil if you have a fast machine."
70 :group 'apropos
71 :type 'boolean)
74 (defcustom apropos-symbol-face (if window-system 'bold)
75 "*Face for symbol name in apropos output or `nil'.
76 This looks good, but slows down the commands several times."
77 :group 'apropos
78 :type 'face)
80 (defcustom apropos-keybinding-face (if window-system 'underline)
81 "*Face for keybinding display in apropos output or `nil'.
82 This looks good, but slows down the commands several times."
83 :group 'apropos
84 :type 'face)
86 (defcustom apropos-label-face (if window-system '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 :group 'apropos
93 :type 'face)
95 (defcustom apropos-property-face (if window-system 'bold-italic)
96 "*Face for property name in apropos output or `nil'.
97 This looks good, but slows down the commands several times."
98 :group 'apropos
99 :type 'face)
101 (defcustom apropos-match-face (if window-system 'secondary-selection)
102 "*Face for matching part in apropos-documentation/value output or `nil'.
103 This looks good, but slows down the commands several times."
104 :group 'apropos
105 :type 'face)
108 (defvar apropos-mode-map
109 (let ((map (make-sparse-keymap)))
110 (define-key map "\C-m" 'apropos-follow)
111 (define-key map " " 'scroll-up)
112 (define-key map "\177" 'scroll-down)
113 (define-key map [mouse-2] 'apropos-mouse-follow)
114 (define-key map [down-mouse-2] nil)
115 map)
116 "Keymap used in Apropos mode.")
119 (defvar apropos-regexp nil
120 "Regexp used in current apropos run.")
122 (defvar apropos-files-scanned ()
123 "List of elc files already scanned in current run of `apropos-documentation'.")
125 (defvar apropos-accumulator ()
126 "Alist of symbols already found in current apropos run.")
128 (defvar apropos-item ()
129 "Current item in or for apropos-accumulator.")
131 (defun apropos-mode ()
132 "Major mode for following hyperlinks in output of apropos commands.
134 \\{apropos-mode-map}"
135 (interactive)
136 (kill-all-local-variables)
137 (use-local-map apropos-mode-map)
138 (setq major-mode 'apropos-mode
139 mode-name "Apropos"))
142 ;; For auld lang syne:
143 ;;;###autoload
144 (fset 'command-apropos 'apropos-command)
145 ;;;###autoload
146 (defun apropos-command (apropos-regexp &optional do-all)
147 "Shows commands (interactively callable functions) that match REGEXP.
148 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
149 variables."
150 (interactive (list (read-string (concat "Apropos command "
151 (if (or current-prefix-arg
152 apropos-do-all)
153 "or variable ")
154 "(regexp): "))
155 current-prefix-arg))
156 (let ((message
157 (let ((standard-output (get-buffer-create "*Apropos*")))
158 (print-help-return-message 'identity))))
159 (or do-all (setq do-all apropos-do-all))
160 (setq apropos-accumulator
161 (apropos-internal apropos-regexp
162 (if do-all
163 (lambda (symbol) (or (commandp symbol)
164 (user-variable-p symbol)))
165 'commandp)))
166 (if (apropos-print
168 (lambda (p)
169 (let (doc symbol)
170 (while p
171 (setcar p (list
172 (setq symbol (car p))
173 (if (commandp symbol)
174 (if (setq doc (documentation symbol t))
175 (substring doc 0 (string-match "\n" doc))
176 "(not documented)"))
177 (and do-all
178 (user-variable-p symbol)
179 (if (setq doc (documentation-property
180 symbol 'variable-documentation t))
181 (substring doc 0
182 (string-match "\n" doc))))))
183 (setq p (cdr p)))))
184 nil)
185 (and message (message message)))))
188 ;;;###autoload
189 (defun apropos (apropos-regexp &optional do-all)
190 "Show all bound symbols whose names match REGEXP.
191 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
192 symbols and key bindings, which is a little more time-consuming.
193 Returns list of symbols and documentation found."
194 (interactive "sApropos symbol (regexp): \nP")
195 (setq apropos-accumulator
196 (apropos-internal apropos-regexp
197 (and (not do-all)
198 (not apropos-do-all)
199 (lambda (symbol)
200 (or (fboundp symbol)
201 (boundp symbol)
202 (facep symbol)
203 (symbol-plist symbol))))))
204 (apropos-print
205 (or do-all apropos-do-all)
206 (lambda (p)
207 (let (symbol doc properties)
208 (while p
209 (setcar p (list
210 (setq symbol (car p))
211 (when (fboundp symbol)
212 (if (setq doc (documentation symbol t))
213 (substring doc 0 (string-match "\n" doc))
214 "(not documented)"))
215 (when (boundp symbol)
216 (if (setq doc (documentation-property
217 symbol 'variable-documentation t))
218 (substring doc 0 (string-match "\n" doc))
219 "(not documented)"))
220 (when (setq properties (symbol-plist symbol))
221 (setq doc (list (car properties)))
222 (while (setq properties (cdr (cdr properties)))
223 (setq doc (cons (car properties) doc)))
224 (mapconcat #'symbol-name (nreverse doc) " "))
225 (when (get symbol 'widget-type)
226 (if (setq doc (documentation-property
227 symbol 'widget-documentation t))
228 (substring doc 0
229 (string-match "\n" doc))
230 "(not documented)"))
231 (when (facep symbol)
232 (if (setq doc (documentation-property
233 symbol 'face-documentation t))
234 (substring doc 0
235 (string-match "\n" doc))
236 "(not documented)"))
237 (when (get symbol 'custom-group)
238 (if (setq doc (documentation-property
239 symbol 'group-documentation t))
240 (substring doc 0
241 (string-match "\n" doc))
242 "(not documented)"))))
243 (setq p (cdr p)))))
244 nil))
247 ;;;###autoload
248 (defun apropos-value (apropos-regexp &optional do-all)
249 "Show all symbols whose value's printed image matches REGEXP.
250 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
251 at the function and at the names and values of properties.
252 Returns list of symbols and values found."
253 (interactive "sApropos value (regexp): \nP")
254 (or do-all (setq do-all apropos-do-all))
255 (setq apropos-accumulator ())
256 (let (f v p)
257 (mapatoms
258 (lambda (symbol)
259 (setq f nil v nil p nil)
260 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
261 symbol f v p))
262 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
263 (if do-all
264 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
265 p (apropos-format-plist symbol "\n " t)))
266 (if (or f v p)
267 (setq apropos-accumulator (cons (list symbol f v p)
268 apropos-accumulator))))))
269 (apropos-print nil nil t))
272 ;;;###autoload
273 (defun apropos-documentation (apropos-regexp &optional do-all)
274 "Show symbols whose documentation contain matches for REGEXP.
275 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
276 documentation that is not stored in the documentation file and show key
277 bindings.
278 Returns list of symbols and documentation found."
279 (interactive "sApropos documentation (regexp): \nP")
280 (or do-all (setq do-all apropos-do-all))
281 (setq apropos-accumulator () apropos-files-scanned ())
282 (let ((standard-input (get-buffer-create " apropos-temp"))
283 f v)
284 (unwind-protect
285 (save-excursion
286 (set-buffer standard-input)
287 (apropos-documentation-check-doc-file)
288 (if do-all
289 (mapatoms
290 (lambda (symbol)
291 (setq f (apropos-safe-documentation symbol)
292 v (get symbol 'variable-documentation))
293 (if (integerp v) (setq v))
294 (setq f (apropos-documentation-internal f)
295 v (apropos-documentation-internal v))
296 (if (or f v)
297 (if (setq apropos-item
298 (cdr (assq symbol apropos-accumulator)))
299 (progn
300 (if f
301 (setcar apropos-item f))
302 (if v
303 (setcar (cdr apropos-item) v)))
304 (setq apropos-accumulator
305 (cons (list symbol f v)
306 apropos-accumulator)))))))
307 (apropos-print nil nil t))
308 (kill-buffer standard-input))))
311 (defun apropos-value-internal (predicate symbol function)
312 (if (funcall predicate symbol)
313 (progn
314 (setq symbol (prin1-to-string (funcall function symbol)))
315 (if (string-match apropos-regexp symbol)
316 (progn
317 (if apropos-match-face
318 (put-text-property (match-beginning 0) (match-end 0)
319 'face apropos-match-face
320 symbol))
321 symbol)))))
323 (defun apropos-documentation-internal (doc)
324 (if (consp doc)
325 (apropos-documentation-check-elc-file (car doc))
326 (and doc
327 (string-match apropos-regexp doc)
328 (progn
329 (if apropos-match-face
330 (put-text-property (match-beginning 0)
331 (match-end 0)
332 'face apropos-match-face
333 (setq doc (copy-sequence doc))))
334 doc))))
336 (defun apropos-format-plist (pl sep &optional compare)
337 (setq pl (symbol-plist pl))
338 (let (p p-out)
339 (while pl
340 (setq p (format "%s %S" (car pl) (nth 1 pl)))
341 (if (or (not compare) (string-match apropos-regexp p))
342 (if apropos-property-face
343 (put-text-property 0 (length (symbol-name (car pl)))
344 'face apropos-property-face p))
345 (setq p nil))
346 (if p
347 (progn
348 (and compare apropos-match-face
349 (put-text-property (match-beginning 0) (match-end 0)
350 'face apropos-match-face
352 (setq p-out (concat p-out (if p-out sep) p))))
353 (setq pl (nthcdr 2 pl)))
354 p-out))
357 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
359 (defun apropos-documentation-check-doc-file ()
360 (let (type symbol (sepa 2) sepb beg end)
361 (insert ?\^_)
362 (backward-char)
363 (insert-file-contents (concat doc-directory internal-doc-file-name))
364 (forward-char)
365 (while (save-excursion
366 (setq sepb (search-forward "\^_"))
367 (not (eobp)))
368 (beginning-of-line 2)
369 (if (save-restriction
370 (narrow-to-region (point) (1- sepb))
371 (re-search-forward apropos-regexp nil t))
372 (progn
373 (setq beg (match-beginning 0)
374 end (point))
375 (goto-char (1+ sepa))
376 (or (setq type (if (eq ?F (preceding-char))
377 1 ; function documentation
378 2) ; variable documentation
379 symbol (read)
380 beg (- beg (point) 1)
381 end (- end (point) 1)
382 doc (buffer-substring (1+ (point)) (1- sepb))
383 apropos-item (assq symbol apropos-accumulator))
384 (setq apropos-item (list symbol nil nil)
385 apropos-accumulator (cons apropos-item
386 apropos-accumulator)))
387 (if apropos-match-face
388 (put-text-property beg end 'face apropos-match-face doc))
389 (setcar (nthcdr type apropos-item) doc)))
390 (setq sepa (goto-char sepb)))))
392 (defun apropos-documentation-check-elc-file (file)
393 (if (member file apropos-files-scanned)
395 (let (symbol doc beg end this-is-a-variable)
396 (setq apropos-files-scanned (cons file apropos-files-scanned))
397 (erase-buffer)
398 (insert-file-contents file)
399 (while (search-forward "\n#@" nil t)
400 ;; Read the comment length, and advance over it.
401 (setq end (read)
402 beg (1+ (point))
403 end (+ (point) end -1))
404 (forward-char)
405 (if (save-restriction
406 ;; match ^ and $ relative to doc string
407 (narrow-to-region beg end)
408 (re-search-forward apropos-regexp nil t))
409 (progn
410 (goto-char (+ end 2))
411 (setq doc (buffer-substring beg end)
412 end (- (match-end 0) beg)
413 beg (- (match-beginning 0) beg)
414 this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
415 symbol (progn
416 (skip-chars-forward "(a-z")
417 (forward-char)
418 (read))
419 symbol (if (consp symbol)
420 (nth 1 symbol)
421 symbol))
422 (if (if this-is-a-variable
423 (get symbol 'variable-documentation)
424 (and (fboundp symbol) (apropos-safe-documentation symbol)))
425 (progn
426 (or (setq apropos-item (assq symbol apropos-accumulator))
427 (setq apropos-item (list symbol nil nil)
428 apropos-accumulator (cons apropos-item
429 apropos-accumulator)))
430 (if apropos-match-face
431 (put-text-property beg end 'face apropos-match-face
432 doc))
433 (setcar (nthcdr (if this-is-a-variable 2 1)
434 apropos-item)
435 doc)))))))))
439 (defun apropos-safe-documentation (function)
440 "Like documentation, except it avoids calling `get_doc_string'.
441 Will return nil instead."
442 (while (and function (symbolp function))
443 (setq function (if (fboundp function)
444 (symbol-function function))))
445 (if (eq (car-safe function) 'macro)
446 (setq function (cdr function)))
447 (setq function (if (byte-code-function-p function)
448 (if (> (length function) 4)
449 (aref function 4))
450 (if (eq (car-safe function) 'autoload)
451 (nth 2 function)
452 (if (eq (car-safe function) 'lambda)
453 (if (stringp (nth 2 function))
454 (nth 2 function)
455 (if (stringp (nth 3 function))
456 (nth 3 function)))))))
457 (if (integerp function)
459 function))
463 (defun apropos-print (do-keys doc-fn spacing)
464 "Output result of various apropos commands with `apropos-regexp'.
465 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
466 of apropos-accumulator and may modify it resulting in (symbol fn-doc
467 var-doc [plist-doc]). Returns sorted list of symbols and documentation
468 found."
469 (if (null apropos-accumulator)
470 (message "No apropos matches for `%s'" apropos-regexp)
471 (if doc-fn
472 (funcall doc-fn apropos-accumulator))
473 (setq apropos-accumulator
474 (sort apropos-accumulator (lambda (a b)
475 (string-lessp (car a) (car b)))))
476 (and apropos-label-face
477 (symbolp apropos-label-face)
478 (setq apropos-label-face `(face ,apropos-label-face
479 mouse-face highlight)))
480 (with-output-to-temp-buffer "*Apropos*"
481 (let ((p apropos-accumulator)
482 (old-buffer (current-buffer))
483 symbol item point1 point2)
484 (set-buffer standard-output)
485 (apropos-mode)
486 (if window-system
487 (insert "If you move the mouse over text that changes color,\n"
488 (substitute-command-keys
489 "you can click \\[apropos-mouse-follow] to get more information.\n")))
490 (insert (substitute-command-keys
491 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
492 (while (consp p)
493 (or (not spacing) (bobp) (terpri))
494 (setq apropos-item (car p)
495 symbol (car apropos-item)
496 p (cdr p)
497 point1 (point))
498 (princ symbol) ; print symbol name
499 (setq point2 (point))
500 ;; Calculate key-bindings if we want them.
501 (and do-keys
502 (commandp symbol)
503 (indent-to 30 1)
504 (if (let ((keys
505 (save-excursion
506 (set-buffer old-buffer)
507 (where-is-internal symbol)))
508 filtered)
509 ;; Copy over the list of key sequences,
510 ;; omitting any that contain a buffer or a frame.
511 (while keys
512 (let ((key (car keys))
513 (i 0)
514 loser)
515 (while (< i (length key))
516 (if (or (framep (aref key i))
517 (bufferp (aref key i)))
518 (setq loser t))
519 (setq i (1+ i)))
520 (or loser
521 (setq filtered (cons key filtered))))
522 (setq keys (cdr keys)))
523 (setq item filtered))
524 ;; Convert the remaining keys to a string and insert.
525 (insert
526 (mapconcat
527 (lambda (key)
528 (setq key (key-description key))
529 (if apropos-keybinding-face
530 (put-text-property 0 (length key)
531 'face apropos-keybinding-face
532 key))
533 key)
534 item ", "))
535 (insert "M-x")
536 (put-text-property (- (point) 3) (point)
537 'face apropos-keybinding-face)
538 (insert " " (symbol-name symbol) " ")
539 (insert "RET")
540 (put-text-property (- (point) 3) (point)
541 'face apropos-keybinding-face)))
542 (terpri)
543 ;; only now so we don't propagate text attributes all over
544 (put-text-property point1 point2 'item
545 (if (eval `(or ,@(cdr apropos-item)))
546 (car apropos-item)
547 apropos-item))
548 (if apropos-symbol-face
549 (put-text-property point1 point2 'face apropos-symbol-face))
550 (apropos-print-doc 'describe-function 1
551 (if (commandp symbol)
552 "Command"
553 (if (apropos-macrop symbol)
554 "Macro"
555 "Function"))
556 do-keys)
557 (if (get symbol 'custom-type)
558 (apropos-print-doc 'customize-variable-other-window 2
559 "User Option" do-keys)
560 (apropos-print-doc 'describe-variable 2
561 "Variable" do-keys))
562 (apropos-print-doc 'customize-group-other-window 6 "Group" do-keys)
563 (apropos-print-doc 'customize-face-other-window 5 "Face" do-keys)
564 (apropos-print-doc 'widget-browse-other-window 4 "Widget" do-keys)
565 (apropos-print-doc 'apropos-describe-plist 3
566 "Plist" nil)))))
567 (prog1 apropos-accumulator
568 (setq apropos-accumulator ()))) ; permit gc
571 (defun apropos-macrop (symbol)
572 "T if SYMBOL is a Lisp macro."
573 (and (fboundp symbol)
574 (consp (setq symbol
575 (symbol-function symbol)))
576 (or (eq (car symbol) 'macro)
577 (if (eq (car symbol) 'autoload)
578 (memq (nth 4 symbol)
579 '(macro t))))))
582 (defun apropos-print-doc (action i str do-keys)
583 (if (stringp (setq i (nth i apropos-item)))
584 (progn
585 (insert " ")
586 (put-text-property (- (point) 2) (1- (point))
587 'action action)
588 (insert str ": ")
589 (if apropos-label-face
590 (add-text-properties (- (point) (length str) 2)
591 (1- (point))
592 apropos-label-face))
593 (insert (if do-keys (substitute-command-keys i) i))
594 (or (bolp) (terpri)))))
597 (defun apropos-mouse-follow (event)
598 (interactive "e")
599 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
601 (current-buffer))))
602 (save-excursion
603 (set-buffer (window-buffer (posn-window (event-start event))))
604 (goto-char (posn-point (event-start event)))
605 (or (and (not (eobp)) (get-text-property (point) 'mouse-face))
606 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
607 (error "There is nothing to follow here"))
608 (apropos-follow other))))
611 (defun apropos-follow (&optional other)
612 (interactive)
613 (let* (;; Properties are always found at the beginning of the line.
614 (bol (save-excursion (beginning-of-line) (point)))
615 ;; If there is no `item' property here, look behind us.
616 (item (get-text-property bol 'item))
617 (item-at (if item nil (previous-single-property-change bol 'item)))
618 ;; Likewise, if there is no `action' property here, look in front.
619 (action (get-text-property bol 'action))
620 (action-at (if action nil (next-single-property-change bol 'action))))
621 (and (null item) item-at
622 (setq item (get-text-property (1- item-at) 'item)))
623 (and (null action) action-at
624 (setq action (get-text-property action-at 'action)))
625 (if (not (and item action))
626 (error "There is nothing to follow here"))
627 (if (consp item) (error "There is nothing to follow in `%s'" (car item)))
628 (if other (set-buffer other))
629 (funcall action item)))
633 (defun apropos-describe-plist (symbol)
634 "Display a pretty listing of SYMBOL's plist."
635 (with-output-to-temp-buffer "*Help*"
636 (set-buffer standard-output)
637 (princ "Symbol ")
638 (prin1 symbol)
639 (princ "'s plist is\n (")
640 (if apropos-symbol-face
641 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
642 (insert (apropos-format-plist symbol "\n "))
643 (princ ")")
644 (print-help-return-message)))
646 (provide 'apropos)
648 ;;; apropos.el ends here