1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
3 ;; Copyright (C) 1996, 97, 98, 99, 2000 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: extensions
10 ;; $Id: eldoc.el,v 1.19 2001/11/16 23:58:48 monnier Exp $
12 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
40 ;; One useful way to enable this minor mode is to put the following in your
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 ;; (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)
50 ;; Use idle timers if available in the version of emacs running.
51 ;; Please don't change this to use `require'; this package works
52 ;; as-is in XEmacs 19.14 and later and I am striving to maintain
53 ;; compatibility between emacs variants.
58 "Show function arglist or variable docstring in echo area."
62 (defcustom eldoc-idle-delay 0.50
63 "*Number of seconds of idle time to wait before printing.
64 If user input arrives before this interval of time has elapsed after the
65 last input, no documentation will be printed.
67 If this variable is set to 0, no idle time is required."
72 (defcustom eldoc-minor-mode-string " ElDoc"
73 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
74 :type '(choice string (const :tag "None" nil))
77 (defcustom eldoc-argument-case 'upcase
78 "Case to display argument names of functions, as a symbol.
79 This has two preferred values: `upcase' or `downcase'.
80 Actually, any name of a function which takes a string as an argument and
81 returns another string is acceptable."
82 :type '(radio (function-item upcase)
83 (function-item downcase)
87 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
88 "*Allow long eldoc messages to resize echo area display.
89 If value is `t', never attempt to truncate messages; complete symbol name
90 and function arglist or 1-line variable documentation will be displayed
91 even if echo area must be resized to fit.
93 If value is any non-nil value other than `t', symbol name may be truncated
94 if it will enable the function arglist or documentation string to fit on a
95 single line without resizing window. Otherwise, behavior is just like
98 If value is nil, messages are always truncated to fit in a single line of
99 display in the echo area. Function or variable symbol name may be
100 truncated to make more of the arglist or documentation string visible.
102 Non-nil values for this variable have no effect unless
103 `eldoc-echo-area-multiline-supported-p' is non-nil."
104 :type '(radio (const :tag "Always" t)
105 (const :tag "Never" nil)
106 (const :tag "Yes, but truncate symbol names if it will\
107 enable argument list to fit on one line" truncate-sym-name-if-fit))
110 ;;; No user options below here.
112 ;; Non-nil if this version of emacs supports dynamically resizable echo areas.
113 (defvar eldoc-echo-area-multiline-supported-p
114 (and (string-lessp "21" emacs-version)
116 (numberp (string-match "^GNU Emacs" (emacs-version))))))
118 ;; Commands after which it is appropriate to print in the echo area.
119 ;; Eldoc does not try to print function arglists, etc. after just any command,
120 ;; because some commands print their own messages in the echo area and these
121 ;; functions would instantly overwrite them. But self-insert-command as well
122 ;; as most motion commands are good candidates.
123 ;; This variable contains an obarray of symbols; do not manipulate it
124 ;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
125 (defvar eldoc-message-commands nil)
127 ;; This is used by eldoc-add-command to initialize eldoc-message-commands
129 ;; It should probably never be necessary to do so, but if you
130 ;; choose to increase the number of buckets, you must do so before loading
131 ;; this file since the obarray is initialized at load time.
132 ;; Remember to keep it a prime number to improve hash performance.
133 (defvar eldoc-message-commands-table-size 31)
135 ;; Bookkeeping; elements are as follows:
136 ;; 0 - contains the last symbol read from the buffer.
137 ;; 1 - contains the string last displayed in the echo area for that
138 ;; symbol, so it can be printed again if necessary without reconsing.
139 ;; 2 - 'function if function args, 'variable if variable documentation.
140 (defvar eldoc-last-data (make-vector 3 nil))
141 (defvar eldoc-last-message nil)
143 ;; Idle timers are supported in Emacs 19.31 and later.
144 (defvar eldoc-use-idle-timer-p (fboundp 'run-with-idle-timer))
146 ;; eldoc's timer object, if using idle timers
147 (defvar eldoc-timer nil)
149 ;; idle time delay currently in use by timer.
150 ;; This is used to determine if eldoc-idle-delay is changed by the user.
151 (defvar eldoc-current-idle-delay eldoc-idle-delay)
155 (define-minor-mode eldoc-mode
156 "Toggle ElDoc mode on or off.
157 Show the defined parameters for the elisp function near point.
159 For the emacs lisp function at the beginning of the sexp which point is
160 within, show the defined parameters for the function in the echo area.
161 This information is extracted directly from the function or macro if it is
162 in pure lisp. If the emacs function is a subr, the parameters are obtained
163 from the documentation string if possible.
165 If point is over a documented variable, print that variable's docstring
168 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
169 nil eldoc-minor-mode-string nil
170 (setq eldoc-last-message nil)
171 (cond (eldoc-use-idle-timer-p
172 (add-hook 'post-command-hook 'eldoc-schedule-timer)
173 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area))
175 ;; Use post-command-idle-hook if defined, otherwise use
176 ;; post-command-hook. The former is only proper to use in Emacs
177 ;; 19.30; that is the first version in which it appeared, but it
178 ;; was obsolesced by idle timers in Emacs 19.31.
179 (add-hook (if (boundp 'post-command-idle-hook)
180 'post-command-idle-hook
182 'eldoc-print-current-symbol-info t t)
183 ;; quick and dirty hack for seeing if this is XEmacs
184 (and (fboundp 'display-message)
185 (add-hook 'pre-command-hook
186 'eldoc-pre-command-refresh-echo-area t t)))))
189 (defun turn-on-eldoc-mode ()
190 "Unequivocally turn on eldoc-mode (see variable documentation)."
195 ;; Idle timers are part of Emacs 19.31 and later.
196 (defun eldoc-schedule-timer ()
198 (memq eldoc-timer timer-idle-list))
200 (run-with-idle-timer eldoc-idle-delay t
201 'eldoc-print-current-symbol-info)))
203 ;; If user has changed the idle delay, update the timer.
204 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
205 (setq eldoc-current-idle-delay eldoc-idle-delay)
206 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
208 (defun eldoc-message (&rest args)
209 (let ((omessage eldoc-last-message))
210 (cond ((eq (car args) eldoc-last-message))
213 (setq eldoc-last-message nil))
214 ;; If only one arg, no formatting to do so put it in
215 ;; eldoc-last-message so eq test above might succeed on
218 (setq eldoc-last-message (car args)))
220 (setq eldoc-last-message (apply 'format args))))
221 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
222 ;; are recorded in a log. Do not put eldoc messages in that log since
224 (cond ((fboundp 'display-message)
225 ;; XEmacs 19.13 way of preventing log messages.
226 (cond (eldoc-last-message
227 (display-message 'no-log eldoc-last-message))
229 (clear-message 'no-log))))
231 ;; Emacs way of preventing log messages.
232 (let ((message-log-max nil))
233 (cond (eldoc-last-message
234 (message "%s" eldoc-last-message))
239 ;; This function goes on pre-command-hook for XEmacs or when using idle
240 ;; timers in Emacs. Motion commands clear the echo area for some reason,
241 ;; which make eldoc messages flicker or disappear just before motion
242 ;; begins. This function reprints the last eldoc message immediately
243 ;; before the next command executes, which does away with the flicker.
244 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
245 (defun eldoc-pre-command-refresh-echo-area ()
246 (and eldoc-last-message
247 (if (eldoc-display-message-no-interference-p)
248 (eldoc-message eldoc-last-message)
249 (setq eldoc-last-message nil))))
251 ;; Decide whether now is a good time to display a message.
252 (defun eldoc-display-message-p ()
253 (and (eldoc-display-message-no-interference-p)
254 (cond (eldoc-use-idle-timer-p
255 ;; If this-command is non-nil while running via an idle
256 ;; timer, we're still in the middle of executing a command,
257 ;; e.g. a query-replace where it would be annoying to
258 ;; overwrite the echo area.
259 (and (not this-command)
260 (symbolp last-command)
261 (intern-soft (symbol-name last-command)
262 eldoc-message-commands)))
264 ;; If we don't have idle timers, this function is
265 ;; running on post-command-hook directly; that means the
266 ;; user's last command is still on `this-command', and we
267 ;; must wait briefly for input to see whether to do display.
268 (and (symbolp this-command)
269 (intern-soft (symbol-name this-command)
270 eldoc-message-commands)
271 (sit-for eldoc-idle-delay))))))
273 ;; Check various conditions about the current environment that might make
274 ;; it undesirable to print eldoc messages right this instant.
275 (defun eldoc-display-message-no-interference-p ()
277 (not executing-kbd-macro)
278 (not (and (boundp 'edebug-active) edebug-active))
279 ;; Having this mode operate in an active minibuffer/echo area causes
280 ;; interference with what's going on there.
281 (not cursor-in-echo-area)
282 (not (eq (selected-window) (minibuffer-window)))))
285 (defun eldoc-print-current-symbol-info ()
286 (and (eldoc-display-message-p)
287 (let* ((current-symbol (eldoc-current-symbol))
288 (current-fnsym (eldoc-fnsym-in-current-sexp))
289 (doc (cond ((eq current-symbol current-fnsym)
290 (or (eldoc-get-fnsym-args-string current-fnsym)
291 (eldoc-get-var-docstring current-symbol)))
293 (or (eldoc-get-var-docstring current-symbol)
294 (eldoc-get-fnsym-args-string current-fnsym))))))
295 (eldoc-message doc))))
297 ;; Return a string containing the function parameter list, or 1-line
298 ;; docstring if function is a subr and no arglist is obtainable from the
299 ;; docstring or elsewhere.
300 (defun eldoc-get-fnsym-args-string (sym)
306 ((and (eq sym (aref eldoc-last-data 0))
307 (eq 'function (aref eldoc-last-data 2)))
308 (setq doc (aref eldoc-last-data 1)))
309 ((subrp (eldoc-symbol-function sym))
310 (setq args (or (eldoc-function-argstring-from-docstring sym)
311 (eldoc-docstring-first-line (documentation sym t)))))
313 (setq args (eldoc-function-argstring sym))))
315 (setq doc (eldoc-docstring-format-sym-doc sym args))
316 (eldoc-last-data-store sym doc 'function)))
319 ;; Return a string containing a brief (one-line) documentation string for
321 (defun eldoc-get-var-docstring (sym)
322 (cond ((and (eq sym (aref eldoc-last-data 0))
323 (eq 'variable (aref eldoc-last-data 2)))
324 (aref eldoc-last-data 1))
326 (let ((doc (documentation-property sym 'variable-documentation t)))
328 (setq doc (eldoc-docstring-format-sym-doc
329 sym (eldoc-docstring-first-line doc)))
330 (eldoc-last-data-store sym doc 'variable)))
333 (defun eldoc-last-data-store (symbol doc type)
334 (aset eldoc-last-data 0 symbol)
335 (aset eldoc-last-data 1 doc)
336 (aset eldoc-last-data 2 type))
338 ;; Note that any leading `*' in the docstring (which indicates the variable
339 ;; is a user option) is removed.
340 (defun eldoc-docstring-first-line (doc)
342 (substitute-command-keys
344 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
345 (cond ((string-match "\n" doc)
346 (substring doc start (match-beginning 0)))
348 (t (substring doc start))))))))
350 ;; If the entire line cannot fit in the echo area, the symbol name may be
351 ;; truncated or eliminated entirely from the output to make room for the
353 (defun eldoc-docstring-format-sym-doc (sym doc)
355 (let* ((name (symbol-name sym))
356 (ea-multi (and eldoc-echo-area-multiline-supported-p
357 eldoc-echo-area-use-multiline-p))
358 ;; Subtract 1 from window width since emacs will not write
359 ;; any chars to the last column, or in later versions, will
360 ;; cause a wraparound and resize of the echo area.
361 (ea-width (1- (window-width (minibuffer-window))))
362 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
363 (cond ((or (<= strip 0)
365 (and ea-multi (> (length doc) ea-width)))
366 (format "%s: %s" sym doc))
367 ((> (length doc) ea-width)
368 (substring (format "%s" doc) 0 ea-width))
369 ((>= strip (length name))
372 ;; Show the end of the partial symbol name, rather
373 ;; than the beginning, since the former is more likely
374 ;; to be unique given package namespace conventions.
375 (setq name (substring name strip))
376 (format "%s: %s" name doc))))))
379 (defun eldoc-fnsym-in-current-sexp ()
381 (eldoc-beginning-of-sexp)
383 ;; Don't do anything if current word is inside a string.
384 (if (= (or (char-after (1- (point))) 0) ?\")
386 (eldoc-current-symbol))
389 (defun eldoc-beginning-of-sexp ()
390 (let ((parse-sexp-ignore-comments t))
394 (or (= (or (char-after (1- (point)))) ?\")
395 (> (point) (point-min)))))
398 ;; returns nil unless current word is an interned symbol.
399 (defun eldoc-current-symbol ()
400 (let ((c (char-after (point))))
402 (memq (char-syntax c) '(?w ?_))
403 (intern-soft (current-word)))))
405 ;; Do indirect function resolution if possible.
406 (defun eldoc-symbol-function (fsym)
407 (let ((defn (and (fboundp fsym)
408 (symbol-function fsym))))
411 (setq defn (indirect-function fsym))
412 (error (setq defn nil))))
415 (defun eldoc-function-arglist (fn)
416 (let* ((prelim-def (eldoc-symbol-function fn))
417 (def (if (eq (car-safe prelim-def) 'macro)
420 (arglist (cond ((null def) nil)
421 ((byte-code-function-p def)
422 (cond ((fboundp 'compiled-function-arglist)
423 (funcall 'compiled-function-arglist def))
426 ((eq (car-safe def) 'lambda)
431 (defun eldoc-function-argstring (fn)
432 (eldoc-function-argstring-format (eldoc-function-arglist fn)))
434 (defun eldoc-function-argstring-format (arglist)
435 (cond ((not (listp arglist))
437 ((symbolp (car arglist))
439 (mapcar (function (lambda (s)
440 (if (memq s '(&optional &rest))
442 (funcall eldoc-argument-case
445 ((stringp (car arglist))
447 (mapcar (function (lambda (s)
448 (if (member s '("&optional" "&rest"))
450 (funcall eldoc-argument-case s))))
452 (concat "(" (mapconcat 'identity arglist " ") ")"))
455 ;; Alist of predicate/action pairs.
456 ;; Each member of the list is a sublist consisting of a predicate function
457 ;; used to determine if the arglist for a function can be found using a
458 ;; certain pattern, and a function which returns the actual arglist from
461 ;; The order in this table is significant, since later predicates may be
462 ;; more general than earlier ones.
464 ;; Compiler note for Emacs/XEmacs versions which support dynamic loading:
465 ;; these functions will be compiled to bytecode, but can't be lazy-loaded
466 ;; even if you set byte-compile-dynamic; to do that would require making
467 ;; them named top-level defuns, which is not particularly desirable either.
468 (defvar eldoc-function-argstring-from-docstring-method-table
470 ;; Try first searching for args starting with symbol name.
471 ;; This is to avoid matching parenthetical remarks in e.g. sit-for.
472 (list (function (lambda (doc fn)
473 (string-match (format "^(%s[^\n)]*)$" fn) doc)))
474 (function (lambda (doc)
475 ;; end does not include trailing ")" sequence.
476 (let ((end (- (match-end 0) 1)))
477 (if (string-match " +" doc (match-beginning 0))
478 (substring doc (match-end 0) end)
481 ;; Try again not requiring this symbol name in the docstring.
482 ;; This will be the case when looking up aliases.
483 (list (function (lambda (doc fn)
484 ;; save-restriction has a pathological docstring in
486 (and (not (eq fn 'save-restriction))
487 (string-match "^([^\n)]+)$" doc))))
488 (function (lambda (doc)
489 ;; end does not include trailing ")" sequence.
490 (let ((end (- (match-end 0) 1)))
491 (and (string-match " +" doc (match-beginning 0))
492 (substring doc (match-end 0) end))))))
494 ;; Emacs subr docstring style:
495 ;; (fn arg1 arg2 ...): description...
496 (list (function (lambda (doc fn)
497 (string-match "^([^\n)]+):" doc)))
498 (function (lambda (doc)
499 ;; end does not include trailing "):" sequence.
500 (let ((end (- (match-end 0) 2)))
501 (and (string-match " +" doc (match-beginning 0))
502 (substring doc (match-end 0) end))))))
504 ;; XEmacs subr docstring style:
505 ;; "arguments: (arg1 arg2 ...)
506 (list (function (lambda (doc fn)
507 (string-match "^arguments: (\\([^\n)]+\\))" doc)))
508 (function (lambda (doc)
509 ;; also skip leading paren, but the first word is
510 ;; actually an argument, not the function name.
511 (substring doc (match-beginning 1) (match-end 1)))))
513 ;; This finds the argstring for `condition-case'. Any others?
514 (list (function (lambda (doc fn)
516 (format "^Usage looks like \\((%s[^\n)]*)\\)\\.$" fn)
518 (function (lambda (doc)
519 ;; end does not include trailing ")" sequence.
520 (let ((end (- (match-end 1) 1)))
521 (and (string-match " +" doc (match-beginning 1))
522 (substring doc (match-end 0) end))))))
524 ;; This finds the argstring for `setq-default'. Any others?
525 (list (function (lambda (doc fn)
526 (string-match (format "^[ \t]+\\((%s[^\n)]*)\\)$" fn)
528 (function (lambda (doc)
529 ;; end does not include trailing ")" sequence.
530 (let ((end (- (match-end 1) 1)))
531 (and (string-match " +" doc (match-beginning 1))
532 (substring doc (match-end 0) end))))))
534 ;; This finds the argstring for `start-process'. Any others?
535 (list (function (lambda (doc fn)
536 (string-match "^Args are +\\([^\n]+\\)$" doc)))
537 (function (lambda (doc)
538 (substring doc (match-beginning 1) (match-end 1)))))
540 ;; These common subrs don't have arglists in their docstrings. So cheat.
541 (list (function (lambda (doc fn)
542 (memq fn '(and or list + -))))
543 (function (lambda (doc)
544 ;; The value nil is a placeholder; otherwise, the
545 ;; following string may be compiled as a docstring,
546 ;; and not a return value for the function.
547 ;; In interpreted lisp form they are
548 ;; indistinguishable; it only matters for compiled
554 (defun eldoc-function-argstring-from-docstring (fn)
555 (let ((docstring (documentation fn 'raw))
556 (table eldoc-function-argstring-from-docstring-method-table)
561 (cond ((funcall (car (car table)) docstring fn)
562 (setq doc (funcall (car (cdr (car table))) docstring))
565 (setq table (cdr table)))))
567 (cond ((not (stringp doc))
569 ((string-match "&" doc)
573 (cond ((string-match "[ \t\n]+" doc p)
575 (cons (substring doc p (match-beginning 0))
577 (setq p (match-end 0)))
579 (setq doclist (cons (substring doc p) doclist))
581 (eldoc-function-argstring-format (nreverse doclist))))
583 (concat "(" (funcall eldoc-argument-case doc) ")"))))))
586 ;; When point is in a sexp, the function args are not reprinted in the echo
587 ;; area after every possible interactive command because some of them print
588 ;; their own messages in the echo area; the eldoc functions would instantly
589 ;; overwrite them unless it is more restrained.
590 ;; These functions do display-command table management.
592 (defun eldoc-add-command (&rest cmds)
593 (or eldoc-message-commands
594 (setq eldoc-message-commands
595 (make-vector eldoc-message-commands-table-size 0)))
599 (setq name (car cmds))
600 (setq cmds (cdr cmds))
602 (cond ((symbolp name)
604 (setq name (symbol-name sym)))
606 (setq sym (intern-soft name))))
610 (set (intern name eldoc-message-commands) t)))))
612 (defun eldoc-add-command-completions (&rest names)
614 (apply 'eldoc-add-command
615 (all-completions (car names) obarray 'fboundp))
616 (setq names (cdr names))))
618 (defun eldoc-remove-command (&rest cmds)
621 (setq name (car cmds))
622 (setq cmds (cdr cmds))
625 (setq name (symbol-name name)))
627 (if (fboundp 'unintern)
628 (unintern name eldoc-message-commands)
629 (let ((s (intern-soft name eldoc-message-commands)))
633 (defun eldoc-remove-command-completions (&rest names)
635 (apply 'eldoc-remove-command
636 (all-completions (car names) eldoc-message-commands))
637 (setq names (cdr names))))
640 ;; Prime the command list.
641 (eldoc-add-command-completions
642 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
643 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
644 "next-" "other-window" "previous-" "recenter" "scroll-"
645 "self-insert-command" "split-window-"
646 "up-list" "down-list")
650 ;;; eldoc.el ends here