1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
3 ;; Copyright (C) 1996, 1997, 1998 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.5 2002/01/10 11:36:26 youngs 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 as-is in
52 ;; XEmacs (which doesn't have timer.el as of 19.14), and I would like to
53 ;; maintain compatibility with that since I must use it sometimes. --Noah
58 "Show function arglist or variable docstring in echo area."
62 (defcustom eldoc-mode nil
63 "*If non-nil, show the defined parameters for the elisp function near point.
65 For the emacs lisp function at the beginning of the sexp which point is
66 within, show the defined parameters for the function in the echo area.
67 This information is extracted directly from the function or macro if it is
68 in pure lisp. If the emacs function is a subr, the parameters are obtained
69 from the documentation string if possible.
71 If point is over a documented variable, print that variable's docstring
74 This variable is buffer-local."
77 (make-variable-buffer-local 'eldoc-mode)
79 (defcustom eldoc-idle-delay 0.50
80 "*Number of seconds of idle time to wait before printing.
81 If user input arrives before this interval of time has elapsed after the
82 last input, no documentation will be printed.
84 If this variable is set to 0, no idle time is required."
88 (defcustom eldoc-minor-mode-string " ElDoc"
89 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
91 (const :tag "none" nil))
94 ;; Put this minor mode on the global minor-mode-alist.
95 (if (fboundp 'add-minor-mode)
96 (add-minor-mode 'eldoc-mode 'eldoc-minor-mode-string)
97 (or (assq 'eldoc-mode (default-value 'minor-mode-alist))
98 (setq-default minor-mode-alist
99 (append (default-value 'minor-mode-alist)
100 '((eldoc-mode eldoc-minor-mode-string))))))
102 (defcustom eldoc-argument-case 'upcase
103 "Case to display argument names of functions, as a symbol.
104 This has two preferred values: `upcase' or `downcase'.
105 Actually, any name of a function which takes a string as an argument and
106 returns another string is acceptable."
107 :type '(choice (const upcase) (const downcase))
110 ;; No user options below here.
112 ;; Commands after which it is appropriate to print in the echo area.
113 ;; Eldoc does not try to print function arglists, etc. after just any command,
114 ;; because some commands print their own messages in the echo area and these
115 ;; functions would instantly overwrite them. But self-insert-command as well
116 ;; as most motion commands are good candidates.
117 ;; This variable contains an obarray of symbols; do not manipulate it
118 ;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
119 (defvar eldoc-message-commands nil)
121 ;; This is used by eldoc-add-command to initialize eldoc-message-commands
123 ;; It should probably never be necessary to do so, but if you
124 ;; choose to increase the number of buckets, you must do so before loading
125 ;; this file since the obarray is initialized at load time.
126 ;; Remember to keep it a prime number to improve hash performance.
127 (defvar eldoc-message-commands-table-size 31)
129 ;; Bookkeeping; elements are as follows:
130 ;; 0 - contains the last symbol read from the buffer.
131 ;; 1 - contains the string last displayed in the echo area for that
132 ;; symbol, so it can be printed again if necessary without reconsing.
133 ;; 2 - 'function if function args, 'variable if variable documentation.
134 (defvar eldoc-last-data (make-vector 3 nil))
135 (defvar eldoc-last-message nil)
137 ;; Idle timers are supported in Emacs 19.31 and later.
138 (defvar eldoc-use-idle-timer-p (fboundp 'run-with-idle-timer))
140 ;; eldoc's timer object, if using idle timers
141 (defvar eldoc-timer nil)
143 ;; idle time delay currently in use by timer.
144 ;; This is used to determine if eldoc-idle-delay is changed by the user.
145 (defvar eldoc-current-idle-delay eldoc-idle-delay)
149 (defun eldoc-mode (&optional prefix)
150 "*Enable or disable eldoc mode.
151 See documentation for the variable of the same name for more details.
153 If called interactively with no prefix argument, toggle current condition
155 If called with a positive or negative prefix argument, enable or disable
156 the mode, respectively."
158 (setq eldoc-last-message nil)
159 (cond (eldoc-use-idle-timer-p
160 (add-hook 'post-command-hook 'eldoc-schedule-timer)
161 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area))
163 ;; Use post-command-idle-hook if defined, otherwise use
164 ;; post-command-hook. The former is only proper to use in Emacs
165 ;; 19.30; that is the first version in which it appeared, but it
166 ;; was obsolesced by idle timers in Emacs 19.31.
167 (add-hook (if (boundp 'post-command-idle-hook)
168 'post-command-idle-hook
170 'eldoc-print-current-symbol-info)
171 ;; quick and dirty hack for seeing if this is XEmacs
172 (and (fboundp 'display-message)
173 (add-hook 'pre-command-hook
174 'eldoc-pre-command-refresh-echo-area))))
175 (setq eldoc-mode (if prefix
176 (>= (prefix-numeric-value prefix) 0)
180 (message "eldoc-mode is enabled")
181 (message "eldoc-mode is disabled")))
185 (defun turn-on-eldoc-mode ()
186 "Unequivocally turn on eldoc-mode (see variable documentation)."
191 ;; Idle timers are part of Emacs 19.31 and later.
192 (defun eldoc-schedule-timer ()
194 (memq eldoc-timer timer-idle-list))
196 (run-with-idle-timer eldoc-idle-delay t
197 'eldoc-print-current-symbol-info)))
199 ;; If user has changed the idle delay, update the timer.
200 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
201 (setq eldoc-current-idle-delay eldoc-idle-delay)
202 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
204 (defun eldoc-message (&rest args)
205 (let ((omessage eldoc-last-message))
206 (cond ((eq (car args) eldoc-last-message))
209 (setq eldoc-last-message nil))
210 ;; If only one arg, no formatting to do so put it in
211 ;; eldoc-last-message so eq test above might succeed on
214 (setq eldoc-last-message (car args)))
216 (setq eldoc-last-message (apply 'format args))))
217 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
218 ;; are recorded in a log. Do not put eldoc messages in that log since
220 (cond ((fboundp 'display-message)
221 ;; XEmacs 19.13 way of preventing log messages.
222 (cond (eldoc-last-message
223 (display-message 'no-log eldoc-last-message))
225 (clear-message 'no-log))))
227 ;; Emacs way of preventing log messages.
228 (let ((message-log-max nil))
229 (cond (eldoc-last-message
230 (message "%s" eldoc-last-message))
235 ;; This function goes on pre-command-hook for XEmacs or when using idle
236 ;; timers in Emacs. Motion commands clear the echo area for some reason,
237 ;; which make eldoc messages flicker or disappear just before motion
238 ;; begins. This function reprints the last eldoc message immediately
239 ;; before the next command executes, which does away with the flicker.
240 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
241 (defun eldoc-pre-command-refresh-echo-area ()
242 (and eldoc-last-message
243 (if (eldoc-display-message-no-interference-p)
244 (eldoc-message eldoc-last-message)
245 (setq eldoc-last-message nil))))
247 ;; Decide whether now is a good time to display a message.
248 (defun eldoc-display-message-p ()
249 (and (eldoc-display-message-no-interference-p)
250 (cond (eldoc-use-idle-timer-p
251 ;; If this-command is non-nil while running via an idle
252 ;; timer, we're still in the middle of executing a command,
253 ;; e.g. a query-replace where it would be annoying to
254 ;; overwrite the echo area.
255 (and (not this-command)
256 (symbolp last-command)
257 (intern-soft (symbol-name last-command)
258 eldoc-message-commands)))
260 ;; If we don't have idle timers, this function is
261 ;; running on post-command-hook directly; that means the
262 ;; user's last command is still on `this-command', and we
263 ;; must wait briefly for input to see whether to do display.
264 (and (symbolp this-command)
265 (intern-soft (symbol-name this-command)
266 eldoc-message-commands)
267 (sit-for eldoc-idle-delay))))))
269 (defun eldoc-display-message-no-interference-p ()
271 (not executing-kbd-macro)
272 ;; Having this mode operate in an active minibuffer/echo area causes
273 ;; interference with what's going on there.
274 (not cursor-in-echo-area)
275 (not (eq (selected-window) (minibuffer-window)))))
278 (defun eldoc-get-doc ()
279 "Return the doc for the current symbol."
280 (let ((current-symbol (eldoc-current-symbol))
281 (current-fnsym (eldoc-fnsym-in-current-sexp)))
282 (cond ((eq current-symbol current-fnsym)
283 (or (eldoc-get-fnsym-args-string current-fnsym)
284 (eldoc-get-var-docstring current-symbol)))
286 (or (eldoc-get-var-docstring current-symbol)
287 (eldoc-get-fnsym-args-string current-fnsym))))))
289 (defun eldoc-print-current-symbol-info ()
290 (and (eldoc-display-message-p)
291 (eldoc-message (eldoc-get-doc))))
293 (defun eldoc-insert-elisp-func-template (doc)
294 "Insert function template extracted from an eldoc help message."
298 (error "could not find doc.")
299 (if (string-match "[^(]*(\\(.*\\))[^)]*" doc)
301 (insert (substring doc (match-beginning 1) (match-end 1)) ")"))
302 (message "Cannot find args, none?"))))
304 (defun eldoc-doc (&optional insert-template)
305 "Display simple help summary in echo area on demand.
306 If INSERT-TEMPLATE is non-nil (interactively with prefix arg) then insert a
307 function template at point.
308 @todo can we add possibility of specifying what to get help on?"
310 (let ((doc (eldoc-get-doc)))
312 (eldoc-insert-elisp-func-template doc)
313 (message "%s" (or doc
314 (format "No doc for `%s'" (eldoc-current-symbol)))))))
316 ;; Return a string containing the function parameter list, or 1-line
317 ;; docstring if function is a subr and no arglist is obtainable from the
318 ;; docstring or elsewhere.
319 (defun eldoc-get-fnsym-args-string (sym)
325 ((and (eq sym (aref eldoc-last-data 0))
326 (eq 'function (aref eldoc-last-data 2)))
327 (setq doc (aref eldoc-last-data 1)))
328 ((subrp (eldoc-symbol-function sym))
329 (setq args (or (eldoc-function-argstring-from-docstring sym)
330 (eldoc-docstring-first-line (documentation sym t)))))
332 (setq args (eldoc-function-argstring sym))))
334 (setq doc (eldoc-docstring-format-sym-doc sym args))
335 (eldoc-last-data-store sym doc 'function)))
338 ;; Return a string containing a brief (one-line) documentation string for
340 (defun eldoc-get-var-docstring (sym)
341 (cond ((and (eq sym (aref eldoc-last-data 0))
342 (eq 'variable (aref eldoc-last-data 2)))
343 (aref eldoc-last-data 1))
345 (let ((doc (documentation-property sym 'variable-documentation t)))
347 (setq doc (eldoc-docstring-format-sym-doc
348 sym (eldoc-docstring-first-line doc)))
349 (eldoc-last-data-store sym doc 'variable)))
352 (defun eldoc-last-data-store (symbol doc type)
353 (aset eldoc-last-data 0 symbol)
354 (aset eldoc-last-data 1 doc)
355 (aset eldoc-last-data 2 type))
357 ;; Note that any leading `*' in the docstring (which indicates the variable
358 ;; is a user option) is removed.
359 (defun eldoc-docstring-first-line (doc)
361 (substitute-command-keys
363 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
364 (cond ((string-match "\n" doc)
365 (substring doc start (match-beginning 0)))
367 (t (substring doc start))))))))
369 ;; If the entire line cannot fit in the echo area, the symbol name may be
370 ;; truncated or eliminated entirely from the output to make room for the
372 (defun eldoc-docstring-format-sym-doc (sym doc)
374 (let* ((name (symbol-name sym))
375 (doclen (+ (length name) (length ": ") (length doc)))
376 ;; Subtract 1 from window width since emacs seems not to write
377 ;; any chars to the last column, at least for some terminal types.
378 (strip (- doclen (1- (window-width (minibuffer-window))))))
380 (let* ((len (length name)))
381 (cond ((>= strip len)
384 ;;(setq name (substring name 0 (- len strip)))
386 ;; Show the end of the partial symbol name, rather
387 ;; than the beginning, since the former is more likely
388 ;; to be unique given package namespace conventions.
389 (setq name (substring name strip))
390 (format "%s: %s" name doc)))))
392 (format "%s: %s" sym doc))))))
395 (defun eldoc-fnsym-in-current-sexp ()
397 (eldoc-beginning-of-sexp)
399 ;; Don't do anything if current word is inside a string.
400 (if (= (or (char-after (1- (point))) 0) ?\")
402 (eldoc-current-symbol))
405 (defun eldoc-beginning-of-sexp ()
406 (let ((parse-sexp-ignore-comments t))
410 (or (= (or (char-after (1- (point)))) ?\")
411 (> (point) (point-min)))))
414 ;; returns nil unless current word is an interned symbol.
415 (defun eldoc-current-symbol ()
416 (let ((c (char-after (point))))
418 (memq (char-syntax c) '(?w ?_))
419 (intern-soft (current-word)))))
421 ;; Do indirect function resolution if possible.
422 (defun eldoc-symbol-function (fsym)
423 (let ((defn (and (fboundp fsym)
424 (symbol-function fsym))))
427 (setq defn (indirect-function fsym))
428 (error (setq defn nil))))
431 (defun eldoc-function-argstring (fn)
432 (let* ((prelim-def (eldoc-symbol-function fn))
433 (def (if (eq (car-safe prelim-def) 'macro)
436 (arglist (cond ((null def) nil)
437 ((byte-code-function-p def)
438 (if (fboundp 'compiled-function-arglist)
439 (funcall 'compiled-function-arglist def)
441 ((eq (car-safe def) 'lambda)
444 (eldoc-function-argstring-format arglist)))
446 (defun eldoc-function-argstring-format (arglist)
447 (cond ((not (listp arglist))
449 ((symbolp (car arglist))
451 (mapcar (function (lambda (s)
452 (if (memq s '(&optional &rest))
454 (funcall eldoc-argument-case
457 ((stringp (car arglist))
459 (mapcar (function (lambda (s)
460 (if (member s '("&optional" "&rest"))
462 (funcall eldoc-argument-case s))))
464 (concat "(" (mapconcat 'identity arglist " ") ")"))
467 ;; Alist of predicate/action pairs.
468 ;; Each member of the list is a sublist consisting of a predicate function
469 ;; used to determine if the arglist for a function can be found using a
470 ;; certain pattern, and a function which returns the actual arglist from
473 ;; The order in this table is significant, since later predicates may be
474 ;; more general than earlier ones.
476 ;; Compiler note for Emacs/XEmacs versions which support dynamic loading:
477 ;; these functions will be compiled to bytecode, but can't be lazy-loaded
478 ;; even if you set byte-compile-dynamic; to do that would require making
479 ;; them named top-level defuns, which is not particularly desirable either.
480 (defvar eldoc-function-argstring-from-docstring-method-table
482 ;; Try first searching for args starting with symbol name.
483 ;; This is to avoid matching parenthetical remarks in e.g. sit-for.
484 (list (function (lambda (doc fn)
485 (string-match (format "^(%s[^\n)]*)$" fn) doc)))
486 (function (lambda (doc)
487 ;; end does not include trailing ")" sequence.
488 (let ((end (- (match-end 0) 1)))
489 (if (string-match " +" doc (match-beginning 0))
490 (substring doc (match-end 0) end)
493 ;; Try again not requiring this symbol name in the docstring.
494 ;; This will be the case when looking up aliases.
495 (list (function (lambda (doc fn)
496 ;; save-restriction has a pathological docstring in
498 (and (not (eq fn 'save-restriction))
499 (string-match "^([^\n)]+)$" doc))))
500 (function (lambda (doc)
501 ;; end does not include trailing ")" sequence.
502 (let ((end (- (match-end 0) 1)))
503 (and (string-match " +" doc (match-beginning 0))
504 (substring doc (match-end 0) end))))))
506 ;; Emacs subr docstring style:
507 ;; (fn arg1 arg2 ...): description...
508 (list (function (lambda (doc fn)
509 (string-match "^([^\n)]+):" doc)))
510 (function (lambda (doc)
511 ;; end does not include trailing "):" sequence.
512 (let ((end (- (match-end 0) 2)))
513 (and (string-match " +" doc (match-beginning 0))
514 (substring doc (match-end 0) end))))))
516 ;; XEmacs subr docstring style:
517 ;; "arguments: (arg1 arg2 ...)
518 (list (function (lambda (doc fn)
519 (string-match "^arguments: (\\([^\n)]+\\))" doc)))
520 (function (lambda (doc)
521 ;; also skip leading paren, but the first word is
522 ;; actually an argument, not the function name.
523 (substring doc (match-beginning 1) (match-end 1)))))
525 ;; This finds the argstring for `condition-case'. Any others?
526 (list (function (lambda (doc fn)
528 (format "^Usage looks like \\((%s[^\n)]*)\\)\\.$" fn)
530 (function (lambda (doc)
531 ;; end does not include trailing ")" sequence.
532 (let ((end (- (match-end 1) 1)))
533 (and (string-match " +" doc (match-beginning 1))
534 (substring doc (match-end 0) end))))))
536 ;; This finds the argstring for `setq-default'. Any others?
537 (list (function (lambda (doc fn)
538 (string-match (format "^[ \t]+\\((%s[^\n)]*)\\)$" fn)
540 (function (lambda (doc)
541 ;; end does not include trailing ")" sequence.
542 (let ((end (- (match-end 1) 1)))
543 (and (string-match " +" doc (match-beginning 1))
544 (substring doc (match-end 0) end))))))
546 ;; This finds the argstring for `start-process'. Any others?
547 (list (function (lambda (doc fn)
548 (string-match "^Args are +\\([^\n]+\\)$" doc)))
549 (function (lambda (doc)
550 (substring doc (match-beginning 1) (match-end 1)))))
552 ;; These common subrs don't have arglists in their docstrings. So cheat.
553 (list (function (lambda (doc fn)
554 (memq fn '(and or list + -))))
555 (function (lambda (doc)
556 ;; The value nil is a placeholder; otherwise, the
557 ;; following string may be compiled as a docstring,
558 ;; and not a return value for the function.
559 ;; In interpreted lisp form they are
560 ;; indistinguishable; it only matters for compiled
566 (defun eldoc-function-argstring-from-docstring (fn)
567 (let ((docstring (documentation fn 'raw))
568 (table eldoc-function-argstring-from-docstring-method-table)
573 (cond ((funcall (car (car table)) docstring fn)
574 (setq doc (funcall (car (cdr (car table))) docstring))
577 (setq table (cdr table)))))
579 (cond ((not (stringp doc))
581 ((string-match "&" doc)
585 (cond ((string-match "[ \t\n]+" doc p)
587 (cons (substring doc p (match-beginning 0))
589 (setq p (match-end 0)))
591 (setq doclist (cons (substring doc p) doclist))
593 (eldoc-function-argstring-format (nreverse doclist))))
595 (concat "(" (funcall eldoc-argument-case doc) ")"))))))
598 ;; When point is in a sexp, the function args are not reprinted in the echo
599 ;; area after every possible interactive command because some of them print
600 ;; their own messages in the echo area; the eldoc functions would instantly
601 ;; overwrite them unless it is more restrained.
602 ;; These functions do display-command table management.
604 (defun eldoc-add-command (&rest cmds)
605 (or eldoc-message-commands
606 (setq eldoc-message-commands
607 (make-vector eldoc-message-commands-table-size 0)))
611 (setq name (car cmds))
612 (setq cmds (cdr cmds))
614 (cond ((symbolp name)
616 (setq name (symbol-name sym)))
618 (setq sym (intern-soft name))))
622 (set (intern name eldoc-message-commands) t)))))
624 (defun eldoc-add-command-completions (&rest names)
626 (apply 'eldoc-add-command
627 (all-completions (car names) obarray 'fboundp))
628 (setq names (cdr names))))
630 (defun eldoc-remove-command (&rest cmds)
633 (setq name (car cmds))
634 (setq cmds (cdr cmds))
637 (setq name (symbol-name name)))
639 (if (fboundp 'unintern)
640 (unintern name eldoc-message-commands)
641 (let ((s (intern-soft name eldoc-message-commands)))
645 (defun eldoc-remove-command-completions (&rest names)
647 (apply 'eldoc-remove-command
648 (all-completions (car names) eldoc-message-commands))
649 (setq names (cdr names))))
652 ;; Prime the command list.
653 (eldoc-add-command-completions
654 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
655 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
656 "next-" "other-window" "previous-" "recenter" "scroll-"
657 "self-insert-command" "split-window-"
658 "up-list" "down-list")
662 ;;; eldoc.el ends here