comparison lisp/utils/eldoc.el @ 98:0d2f883870bc r20-1b1

Import from CVS: tag r20-1b1
author cvs
date Mon, 13 Aug 2007 09:13:56 +0200
parents
children 4be1180a9e89
comparison
equal deleted inserted replaced
97:498bf5da1c90 98:0d2f883870bc
1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
2
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
6 ;; Maintainer: friedman@prep.ai.mit.edu
7 ;; Keywords: extensions
8 ;; Created: 1995-10-06
9
10 ;; $Id: eldoc.el,v 1.1 1997/02/14 19:59:51 steve Exp $
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; This program was inspired by the behavior of the "mouse documentation
32 ;; window" on many Lisp Machine systems; as you type a function's symbol
33 ;; name as part of a sexp, it will print the argument list for that
34 ;; function. Behavior is not identical; for example, you need not actually
35 ;; type the function name, you need only move point around in a sexp that
36 ;; calls it. Also, if point is over a documented variable, it will print
37 ;; the one-line documentation for that variable instead, to remind you of
38 ;; that variable's meaning.
39
40 ;; One useful way to enable this minor mode is to put the following in your
41 ;; .emacs:
42 ;;
43 ;; (autoload 'turn-on-eldoc-mode "eldoc" nil t)
44 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
45 ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
46
47 ;;; Code:
48
49 ;; Use idle timers if available in the version of emacs running.
50 ;; Please don't change this to use `require'; this package works as-is in
51 ;; XEmacs (which doesn't have timer.el as of 19.14), and I would like to
52 ;; maintain compatibility with that since I must use it sometimes. --Noah
53 (or (featurep 'timer)
54 (load "timer" t))
55
56 ;;;###autoload
57 (defvar eldoc-mode nil
58 "*If non-nil, show the defined parameters for the elisp function near point.
59
60 For the emacs lisp function at the beginning of the sexp which point is
61 within, show the defined parameters for the function in the echo area.
62 This information is extracted directly from the function or macro if it is
63 in pure lisp. If the emacs function is a subr, the parameters are obtained
64 from the documentation string if possible.
65
66 If point is over a documented variable, print that variable's docstring
67 instead.
68
69 This variable is buffer-local.")
70 (make-variable-buffer-local 'eldoc-mode)
71
72 (defconst eldoc-idle-delay 0.50
73 "*Number of seconds of idle time to wait before printing.
74 If user input arrives before this interval of time has elapsed after the
75 last input, no documentation will be printed.
76
77 If this variable is set to 0, no idle time is required.")
78
79 (defconst eldoc-minor-mode-string " ElDoc"
80 "*String to display in mode line when Eldoc Mode is enabled.")
81
82 ;; Put this minor mode on the global minor-mode-alist.
83 (or (assq 'eldoc-mode (default-value 'minor-mode-alist))
84 (setq-default minor-mode-alist
85 (append (default-value 'minor-mode-alist)
86 '((eldoc-mode eldoc-minor-mode-string)))))
87
88 (defconst eldoc-argument-case 'upcase
89 "Case to display argument names of functions, as a symbol.
90 This has two preferred values: `upcase' or `downcase'.
91 Actually, any name of a function which takes a string as an argument and
92 returns another string is acceptable.")
93
94 (defvar eldoc-message-commands nil
95 "*Commands after which it is appropriate to print in the echo area.
96
97 Eldoc does not try to print function arglists, etc. after just any command,
98 because some commands print their own messages in the echo area and these
99 functions would instantly overwrite them. But self-insert-command as well
100 as most motion commands are good candidates.
101
102 This variable contains an obarray of symbols; it is probably best to
103 manipulate this data structure with the commands `eldoc-add-command' and
104 `eldoc-remove-command'.")
105
106 (cond ((null eldoc-message-commands)
107 ;; If you increase the number of buckets, keep it a prime number.
108 (setq eldoc-message-commands (make-vector 31 0))
109 (let ((list '("self-insert-command"
110 "next-" "previous-"
111 "forward-" "backward-"
112 "beginning-of-" "end-of-"
113 "goto-"
114 "recenter"
115 "scroll-"
116 "mouse-set-point"))
117 (syms nil))
118 (while list
119 (setq syms (all-completions (car list) obarray 'fboundp))
120 (setq list (cdr list))
121 (while syms
122 (set (intern (car syms) eldoc-message-commands) t)
123 (setq syms (cdr syms)))))))
124
125 ;; Bookkeeping; the car contains the last symbol read from the buffer.
126 ;; The cdr contains the string last displayed in the echo area, so it can
127 ;; be printed again if necessary without reconsing.
128 (defvar eldoc-last-data '(nil . nil))
129
130 ;; Idle timers are supported in Emacs 19.31 and later.
131 (defconst eldoc-use-idle-timer-p (fboundp 'run-with-idle-timer))
132
133 ;; eldoc's timer object, if using idle timers
134 (defvar eldoc-timer nil)
135
136 ;; idle time delay currently in use by timer.
137 ;; This is used to determine if eldoc-idle-delay is changed by the user.
138 (defvar eldoc-current-idle-delay eldoc-idle-delay)
139
140 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages are
141 ;; recorded in a log. Do not put eldoc messages in that log since
142 ;; they are Legion.
143 (defmacro eldoc-message (&rest args)
144 (if (fboundp 'display-message)
145 ;; XEmacs 19.13 way of preventing log messages.
146 (list 'display-message '(quote no-log) (apply 'list 'format args))
147 (list 'let (list (list 'message-log-max 'nil))
148 (apply 'list 'message args))))
149
150
151 ;;;###autoload
152 (defun eldoc-mode (&optional prefix)
153 "*Enable or disable eldoc mode.
154 See documentation for the variable of the same name for more details.
155
156 If called interactively with no prefix argument, toggle current condition
157 of the mode.
158 If called with a positive or negative prefix argument, enable or disable
159 the mode, respectively."
160 (interactive "P")
161
162 (cond (eldoc-use-idle-timer-p
163 (add-hook 'post-command-hook 'eldoc-schedule-timer))
164 (t
165 ;; Use post-command-idle-hook if defined, otherwise use
166 ;; post-command-hook. The former is only proper to use in Emacs
167 ;; 19.30; that is the first version in which it appeared, but it
168 ;; was obsolesced by idle timers in Emacs 19.31.
169 (add-hook (if (boundp 'post-command-idle-hook)
170 'post-command-idle-hook
171 'post-command-hook)
172 'eldoc-print-current-symbol-info)))
173
174 (setq eldoc-mode (if prefix
175 (>= (prefix-numeric-value prefix) 0)
176 (not eldoc-mode)))
177
178 (and (interactive-p)
179 (if eldoc-mode
180 (message "eldoc-mode is enabled")
181 (message "eldoc-mode is disabled")))
182 eldoc-mode)
183
184 ;;;###autoload
185 (defun turn-on-eldoc-mode ()
186 "Unequivocally turn on eldoc-mode (see variable documentation)."
187 (interactive)
188 (eldoc-mode 1))
189
190 (defun eldoc-add-command (cmd)
191 "Add COMMAND to the list of commands which causes function arg display.
192 If called interactively, completion on defined commands is available.
193
194 When point is in a sexp, the function args are not reprinted in the echo
195 area after every possible interactive command because some of them print
196 their own messages in the echo area; the eldoc functions would instantly
197 overwrite them unless it is more restrained."
198 (interactive "CAdd function to eldoc message commands list: ")
199 (and (fboundp cmd)
200 (set (intern (symbol-name cmd) eldoc-message-commands) t)))
201
202 (defun eldoc-remove-command (cmd)
203 "Remove COMMAND from the list of commands which causes function arg display.
204 If called interactively, completion matches only those functions currently
205 in the list.
206
207 When point is in a sexp, the function args are not reprinted in the echo
208 area after every possible interactive command because some of them print
209 their own messages in the echo area; the eldoc functions would instantly
210 overwrite them unless it is more restrained."
211 (interactive (list (completing-read
212 "Remove function from eldoc message commands list: "
213 eldoc-message-commands 'boundp t)))
214 (and (symbolp cmd)
215 (setq cmd (symbol-name cmd)))
216 (if (fboundp 'unintern)
217 (unintern cmd eldoc-message-commands)
218 (let ((s (intern-soft cmd eldoc-message-commands)))
219 (and s
220 (makunbound s)))))
221
222 ;; Idle timers are part of Emacs 19.31 and later.
223 (defun eldoc-schedule-timer ()
224 (or (and eldoc-timer
225 (memq eldoc-timer timer-idle-list))
226 (setq eldoc-timer
227 (run-with-idle-timer eldoc-idle-delay t
228 'eldoc-print-current-symbol-info)))
229
230 ;; If user has changed the idle delay, update the timer.
231 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
232 (setq eldoc-current-idle-delay eldoc-idle-delay)
233 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
234
235
236 (defun eldoc-print-current-symbol-info ()
237 (and eldoc-mode
238 (not executing-kbd-macro)
239
240 ;; Having this mode operate in an active minibuffer makes it
241 ;; impossible to see what you're doing.
242 (not (eq (selected-window) (minibuffer-window)))
243
244 (cond (eldoc-use-idle-timer-p
245 (and (symbolp last-command)
246 (intern-soft (symbol-name last-command)
247 eldoc-message-commands)))
248 (t
249 ;; If we don't have idle timers, this function is
250 ;; running on post-command-hook directly; that means the
251 ;; user's last command is still on `this-command', and we
252 ;; must wait briefly for input to see whether to do display.
253 (and (symbolp this-command)
254 (intern-soft (symbol-name this-command)
255 eldoc-message-commands)
256 (sit-for eldoc-idle-delay))))
257
258 (let ((current-symbol (eldoc-current-symbol))
259 (current-fnsym (eldoc-fnsym-in-current-sexp)))
260 (cond ((eq current-symbol current-fnsym)
261 (eldoc-print-fnsym-args current-fnsym))
262 (t
263 (or (eldoc-print-var-docstring current-symbol)
264 (eldoc-print-fnsym-args current-fnsym)))))))
265
266 (defun eldoc-print-fnsym-args (&optional symbol)
267 (interactive)
268 (let ((sym (or symbol (eldoc-fnsym-in-current-sexp)))
269 (args nil))
270 (cond ((not (and (symbolp sym)
271 (fboundp sym))))
272 ((eq sym (car eldoc-last-data))
273 (setq args (cdr eldoc-last-data)))
274 ((subrp (eldoc-symbol-function sym))
275 (setq args (or (eldoc-function-argstring-from-docstring sym)
276 (eldoc-docstring-first-line (documentation sym t))))
277 (setcar eldoc-last-data sym)
278 (setcdr eldoc-last-data args))
279 (t
280 (setq args (eldoc-function-argstring sym))
281 (setcar eldoc-last-data sym)
282 (setcdr eldoc-last-data args)))
283 (and args
284 (eldoc-message "%s: %s" sym args))))
285
286 (defun eldoc-fnsym-in-current-sexp ()
287 (let* ((p (point))
288 (sym (progn
289 (while (and (eldoc-forward-sexp-safe -1)
290 (> (point) (point-min))))
291 (cond ((or (= (point) (point-min))
292 (memq (or (char-after (point)) 0)
293 '(?\( ?\"))
294 ;; If we hit a quotation mark before a paren, we
295 ;; are inside a specific string, not a list of
296 ;; symbols.
297 (eq (or (char-after (1- (point))) 0) ?\"))
298 nil)
299 (t (condition-case nil
300 (read (current-buffer))
301 (error nil)))))))
302 (goto-char p)
303 (and (symbolp sym)
304 sym)))
305
306 (defun eldoc-function-argstring (fn)
307 (let* ((prelim-def (eldoc-symbol-function fn))
308 (def (if (eq (car-safe prelim-def) 'macro)
309 (cdr prelim-def)
310 prelim-def))
311 (arglist (cond ((null def) nil)
312 ((byte-code-function-p def)
313 (if (fboundp 'compiled-function-arglist)
314 (funcall 'compiled-function-arglist def)
315 (aref def 0)))
316 ((eq (car-safe def) 'lambda)
317 (nth 1 def))
318 (t t))))
319 (eldoc-function-argstring-format arglist)))
320
321 (defun eldoc-function-argstring-format (arglist)
322 (cond ((not (listp arglist))
323 (setq arglist nil))
324 ((symbolp (car arglist))
325 (setq arglist
326 (mapcar (function (lambda (s)
327 (if (memq s '(&optional &rest))
328 (symbol-name s)
329 (funcall eldoc-argument-case
330 (symbol-name s)))))
331 arglist)))
332 ((stringp (car arglist))
333 (setq arglist
334 (mapcar (function (lambda (s)
335 (if (member s '("&optional" "&rest"))
336 s
337 (funcall eldoc-argument-case s))))
338 arglist))))
339 (concat "(" (mapconcat 'identity arglist " ") ")"))
340
341
342 (defun eldoc-print-var-docstring (&optional sym)
343 (or sym (setq sym (eldoc-current-symbol)))
344 (eldoc-print-docstring sym (documentation-property
345 sym 'variable-documentation t)))
346
347 ;; Print the brief (one-line) documentation string for the symbol.
348 (defun eldoc-print-docstring (symbol doc)
349 (and doc
350 (eldoc-message "%s" (eldoc-docstring-message symbol doc))))
351
352 ;; If the entire line cannot fit in the echo area, the variable name may be
353 ;; truncated or eliminated entirely from the output to make room.
354 ;; Any leading `*' in the docstring (which indicates the variable is a user
355 ;; option) is not printed."
356 (defun eldoc-docstring-message (symbol doc)
357 (and doc
358 (let ((name (symbol-name symbol)))
359 (setq doc (eldoc-docstring-first-line doc))
360 (save-match-data
361 (let* ((doclen (+ (length name) (length ": ") (length doc)))
362 ;; Subtract 1 from window width since emacs seems not to
363 ;; write any chars to the last column, at least for some
364 ;; terminal types.
365 (strip (- doclen (1- (window-width (minibuffer-window))))))
366 (cond ((> strip 0)
367 (let* ((len (length name)))
368 (cond ((>= strip len)
369 (format "%s" doc))
370 (t
371 (setq name (substring name 0 (- len strip)))
372 (format "%s: %s" name doc)))))
373 (t
374 (format "%s: %s" symbol doc))))))))
375
376 (defun eldoc-docstring-first-line (doc)
377 (save-match-data
378 (and (string-match "\n" doc)
379 (setq doc (substring doc 0 (match-beginning 0))))
380 (and (string-match "^\\*" doc)
381 (setq doc (substring doc 1))))
382 doc)
383
384
385 ;; Alist of predicate/action pairs.
386 ;; Each member of the list is a sublist consisting of a predicate function
387 ;; used to determine if the arglist for a function can be found using a
388 ;; certain pattern, and a function which returns the actual arglist from
389 ;; that docstring.
390 ;;
391 ;; The order in this table is significant, since later predicates may be
392 ;; more general than earlier ones.
393 ;;
394 ;; Compiler note for Emacs 19.29 and later: these functions will be
395 ;; compiled to bytecode, but can't be lazy-loaded even if you set
396 ;; byte-compile-dynamic; to do that would require making them named
397 ;; top-level defuns, and that's not particularly desirable either.
398 (defconst eldoc-function-argstring-from-docstring-method-table
399 (list
400 ;; Try first searching for args starting with symbol name.
401 ;; This is to avoid matching parenthetical remarks in e.g. sit-for.
402 (list (function (lambda (doc fn)
403 (string-match (format "^(%s[^\n)]*)$" fn) doc)))
404 (function (lambda (doc)
405 ;; end does not include trailing ")" sequence.
406 (let ((end (- (match-end 0) 1)))
407 (if (string-match " +" doc (match-beginning 0))
408 (substring doc (match-end 0) end)
409 "")))))
410
411 ;; Try again not requiring this symbol name in the docstring.
412 ;; This will be the case when looking up aliases.
413 (list (function (lambda (doc fn)
414 (string-match "^([^\n)]+)$" doc)))
415 (function (lambda (doc)
416 ;; end does not include trailing ")" sequence.
417 (let ((end (- (match-end 0) 1)))
418 (and (string-match " +" doc (match-beginning 0))
419 (substring doc (match-end 0) end))))))
420
421 ;; Emacs subr docstring style:
422 ;; (fn arg1 arg2 ...): description...
423 (list (function (lambda (doc fn)
424 (string-match "^([^\n)]+):" doc)))
425 (function (lambda (doc)
426 ;; end does not include trailing "):" sequence.
427 (let ((end (- (match-end 0) 2)))
428 (and (string-match " +" doc (match-beginning 0))
429 (substring doc (match-end 0) end))))))
430
431 ;; XEmacs subr docstring style:
432 ;; "arguments: (arg1 arg2 ...)
433 (list (function (lambda (doc fn)
434 (string-match "^arguments: (\\([^\n)]+\\))" doc)))
435 (function (lambda (doc)
436 ;; also skip leading paren, but the first word is
437 ;; actually an argument, not the function name.
438 (substring doc (match-beginning 1) (match-end 1)))))
439
440 ;; This finds the argstring for `condition-case'. Any others?
441 (list (function (lambda (doc fn)
442 (string-match
443 (format "^Usage looks like \\((%s[^\n)]*)\\)\\.$" fn)
444 doc)))
445 (function (lambda (doc)
446 ;; end does not include trailing ")" sequence.
447 (let ((end (- (match-end 1) 1)))
448 (and (string-match " +" doc (match-beginning 1))
449 (substring doc (match-end 0) end))))))
450
451 ;; This finds the argstring for `setq-default'. Any others?
452 (list (function (lambda (doc fn)
453 (string-match (format "^[ \t]+\\((%s[^\n)]*)\\)$" fn)
454 doc)))
455 (function (lambda (doc)
456 ;; end does not include trailing ")" sequence.
457 (let ((end (- (match-end 1) 1)))
458 (and (string-match " +" doc (match-beginning 1))
459 (substring doc (match-end 0) end))))))
460
461 ;; This finds the argstring for `start-process'. Any others?
462 (list (function (lambda (doc fn)
463 (string-match "^Args are +\\([^\n]+\\)$" doc)))
464 (function (lambda (doc)
465 (substring doc (match-beginning 1) (match-end 1)))))
466 ))
467
468 (defun eldoc-function-argstring-from-docstring (fn)
469 (let ((docstring (documentation fn 'raw))
470 (table eldoc-function-argstring-from-docstring-method-table)
471 (doc nil)
472 (doclist nil))
473 (save-match-data
474 (while table
475 (cond ((funcall (car (car table)) docstring fn)
476 (setq doc (funcall (car (cdr (car table))) docstring))
477 (setq table nil))
478 (t
479 (setq table (cdr table)))))
480
481 (cond ((not (stringp doc))
482 nil)
483 ((string-match "&" doc)
484 (let ((p 0)
485 (l (length doc)))
486 (while (< p l)
487 (cond ((string-match "[ \t\n]+" doc p)
488 (setq doclist
489 (cons (substring doc p (match-beginning 0))
490 doclist))
491 (setq p (match-end 0)))
492 (t
493 (setq doclist (cons (substring doc p) doclist))
494 (setq p l))))
495 (eldoc-function-argstring-format (nreverse doclist))))
496 (t
497 (concat "(" (funcall eldoc-argument-case doc) ")"))))))
498
499
500 ;; forward-sexp calls scan-sexps, which returns an error if it hits the
501 ;; beginning or end of the sexp. This returns nil instead.
502 (defun eldoc-forward-sexp-safe (&optional count)
503 "Move forward across one balanced expression (sexp).
504 With argument, do it that many times. Negative arg -COUNT means
505 move backward across COUNT balanced expressions.
506 Return distance in buffer moved, or nil."
507 (or count (setq count 1))
508 (condition-case err
509 (- (- (point) (progn
510 (let ((parse-sexp-ignore-comments t))
511 (forward-sexp count))
512 (point))))
513 (error nil)))
514
515 ;; Do indirect function resolution if possible.
516 (defun eldoc-symbol-function (fsym)
517 (let ((defn (and (fboundp fsym)
518 (symbol-function fsym))))
519 (and (symbolp defn)
520 (condition-case err
521 (setq defn (indirect-function fsym))
522 (error (setq defn nil))))
523 defn))
524
525 (defun eldoc-current-symbol ()
526 (let ((c (char-after (point))))
527 (and c
528 (memq (char-syntax c) '(?w ?_))
529 (intern-soft (current-word)))))
530
531 (provide 'eldoc)
532
533 ;;; eldoc.el ends here