1 ;;; eldoc.el --- show function arglist or variable docstring in echo area -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This program was inspired by the behavior of the "mouse documentation
28 ;; window" on many Lisp Machine systems; as you type a function's symbol
29 ;; name as part of a sexp, it will print the argument list for that
30 ;; function. Behavior is not identical; for example, you need not actually
31 ;; type the function name, you need only move point around in a sexp that
32 ;; calls it. Also, if point is over a documented variable, it will print
33 ;; the one-line documentation for that variable instead, to remind you of
34 ;; that variable's meaning.
36 ;; One useful way to enable this minor mode is to put the following in your
39 ;; (add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
40 ;; (add-hook 'lisp-interaction-mode-hook 'eldoc-mode)
41 ;; (add-hook 'ielm-mode-hook 'eldoc-mode)
42 ;; (add-hook 'eval-expression-minibuffer-setup-hook 'eldoc-mode)
44 ;; Major modes for other languages may use ElDoc by defining an
45 ;; appropriate function as the buffer-local value of
46 ;; `eldoc-documentation-function'.
50 (require 'help-fns
) ;For fundoc-usage handling functions.
53 "Show function arglist or variable docstring in echo area."
57 (defcustom eldoc-idle-delay
0.50
58 "Number of seconds of idle time to wait before printing.
59 If user input arrives before this interval of time has elapsed after the
60 last input, no documentation will be printed.
62 If this variable is set to 0, no idle time is required."
66 (defcustom eldoc-print-after-edit nil
67 "If non-nil eldoc info is only shown when editing.
68 Changing the value requires toggling `eldoc-mode'."
73 (defcustom eldoc-minor-mode-string
(purecopy " ElDoc")
74 "String to display in mode line when ElDoc Mode is enabled; nil for none."
75 :type
'(choice string
(const :tag
"None" nil
))
78 (defcustom eldoc-argument-case
'upcase
79 "Case to display argument names of functions, as a symbol.
80 This has two preferred values: `upcase' or `downcase'.
81 Actually, any name of a function which takes a string as an argument and
82 returns another string is acceptable.
84 Note that if `eldoc-documentation-function' is non-nil, this variable
85 has no effect, unless the function handles it explicitly."
86 :type
'(radio (function-item upcase
)
87 (function-item downcase
)
91 (defcustom eldoc-echo-area-use-multiline-p
'truncate-sym-name-if-fit
92 "Allow long ElDoc messages to resize echo area display.
93 If value is t, never attempt to truncate messages; complete symbol name
94 and function arglist or 1-line variable documentation will be displayed
95 even if echo area must be resized to fit.
97 If value is any non-nil value other than t, symbol name may be truncated
98 if it will enable the function arglist or documentation string to fit on a
99 single line without resizing window. Otherwise, behavior is just like
102 If value is nil, messages are always truncated to fit in a single line of
103 display in the echo area. Function or variable symbol name may be
104 truncated to make more of the arglist or documentation string visible.
106 Note that if `eldoc-documentation-function' is non-nil, this variable
107 has no effect, unless the function handles it explicitly."
108 :type
'(radio (const :tag
"Always" t
)
109 (const :tag
"Never" nil
)
110 (const :tag
"Yes, but truncate symbol names if it will\
111 enable argument list to fit on one line" truncate-sym-name-if-fit
))
114 (defface eldoc-highlight-function-argument
115 '((t (:inherit bold
)))
116 "Face used for the argument at point in a function's argument list.
117 Note that if `eldoc-documentation-function' is non-nil, this face
118 has no effect, unless the function handles it explicitly."
121 ;;; No user options below here.
123 (defvar eldoc-message-commands-table-size
31
124 "Used by `eldoc-add-command' to initialize `eldoc-message-commands' obarray.
125 It should probably never be necessary to do so, but if you
126 choose to increase the number of buckets, you must do so before loading
127 this file since the obarray is initialized at load time.
128 Remember to keep it a prime number to improve hash performance.")
130 (defconst eldoc-message-commands
131 (make-vector eldoc-message-commands-table-size
0)
132 "Commands after which it is appropriate to print in the echo area.
133 ElDoc does not try to print function arglists, etc., after just any command,
134 because some commands print their own messages in the echo area and these
135 functions would instantly overwrite them. But `self-insert-command' as well
136 as most motion commands are good candidates.
137 This variable contains an obarray of symbols; do not manipulate it
138 directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.")
141 (defconst eldoc-last-data
(make-vector 3 nil
)
142 "Bookkeeping; elements are as follows:
143 0 - contains the last symbol read from the buffer.
144 1 - contains the string last displayed in the echo area for variables,
145 or argument string for functions.
146 2 - 'function if function args, 'variable if variable documentation.")
148 (defvar eldoc-last-message nil
)
150 (defvar eldoc-timer nil
"ElDoc's timer object.")
152 (defvar eldoc-current-idle-delay eldoc-idle-delay
153 "Idle time delay currently in use by timer.
154 This is used to determine if `eldoc-idle-delay' is changed by the user.")
156 (defvar eldoc-message-function
#'eldoc-minibuffer-message
157 "The function used by `eldoc-message' to display messages.
158 It should receive the same arguments as `message'.")
160 (defun eldoc-edit-message-commands ()
161 (let ((cmds (make-vector 31 0))
162 (re (regexp-opt '("delete" "insert" "edit" "electric" "newline"))))
163 (mapatoms (lambda (s)
165 (string-match-p re
(symbol-name s
))
166 (intern (symbol-name s
) cmds
)))
172 (define-minor-mode eldoc-mode
173 "Toggle echo area display of Lisp objects at point (ElDoc mode).
174 With a prefix argument ARG, enable ElDoc mode if ARG is positive,
175 and disable it otherwise. If called from Lisp, enable ElDoc mode
176 if ARG is omitted or nil.
178 ElDoc mode is a buffer-local minor mode. When enabled, the echo
179 area displays information about a function or variable in the
180 text where point is. If point is on a documented variable, it
181 displays the first line of that variable's doc string. Otherwise
182 it displays the argument list of the function called in the
183 expression point is on."
184 :group
'eldoc
:lighter eldoc-minor-mode-string
185 (setq eldoc-last-message nil
)
188 (when eldoc-print-after-edit
189 (setq-local eldoc-message-commands
(eldoc-edit-message-commands)))
190 (add-hook 'post-command-hook
'eldoc-schedule-timer nil t
)
191 (add-hook 'pre-command-hook
'eldoc-pre-command-refresh-echo-area nil t
))
192 (kill-local-variable 'eldoc-message-commands
)
193 (remove-hook 'post-command-hook
'eldoc-schedule-timer t
)
194 (remove-hook 'pre-command-hook
'eldoc-pre-command-refresh-echo-area t
)))
197 (define-obsolete-function-alias 'turn-on-eldoc-mode
'eldoc-mode
"24.4")
200 (defun eldoc-schedule-timer ()
202 (memq eldoc-timer timer-idle-list
))
206 (lambda () (and eldoc-mode
(eldoc-print-current-symbol-info))))))
208 ;; If user has changed the idle delay, update the timer.
209 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay
))
210 (setq eldoc-current-idle-delay eldoc-idle-delay
)
211 (timer-set-idle-time eldoc-timer eldoc-idle-delay t
))))
213 (defvar eldoc-mode-line-string nil
)
214 (put 'eldoc-mode-line-string
'risky-local-variable t
)
216 (defun eldoc-minibuffer-message (format-string &rest args
)
217 "Display messages in the mode-line when in the minibuffer.
218 Otherwise work like `message'."
221 (add-hook 'minibuffer-exit-hook
222 (lambda () (setq eldoc-mode-line-string nil
223 ;; http://debbugs.gnu.org/16920
224 eldoc-last-message nil
))
228 (or (window-in-direction 'above
(minibuffer-window))
229 (minibuffer-selected-window)
230 (get-largest-window)))
231 (unless (and (listp mode-line-format
)
232 (assq 'eldoc-mode-line-string mode-line-format
))
233 (setq mode-line-format
234 (list "" '(eldoc-mode-line-string
235 (" " eldoc-mode-line-string
" "))
237 (setq eldoc-mode-line-string
238 (when (stringp format-string
)
239 (apply 'format format-string args
)))
240 (force-mode-line-update)))
241 (apply 'message format-string args
)))
243 (defun eldoc-message (&rest args
)
244 (let ((omessage eldoc-last-message
))
245 (setq eldoc-last-message
246 (cond ((eq (car args
) eldoc-last-message
) eldoc-last-message
)
247 ((null (car args
)) nil
)
248 ;; If only one arg, no formatting to do, so put it in
249 ;; eldoc-last-message so eq test above might succeed on
251 ((null (cdr args
)) (car args
))
252 (t (apply 'format args
))))
253 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
254 ;; are recorded in a log. Do not put eldoc messages in that log since
256 ;; Emacs way of preventing log messages.
257 (let ((message-log-max nil
))
258 (cond (eldoc-last-message
259 (funcall eldoc-message-function
"%s" eldoc-last-message
))
260 (omessage (funcall eldoc-message-function nil
)))))
263 (defun eldoc--message-command-p (command)
264 (and (symbolp command
)
265 (intern-soft (symbol-name command
) eldoc-message-commands
)))
267 ;; This function goes on pre-command-hook for XEmacs or when using idle
268 ;; timers in Emacs. Motion commands clear the echo area for some reason,
269 ;; which make eldoc messages flicker or disappear just before motion
270 ;; begins. This function reprints the last eldoc message immediately
271 ;; before the next command executes, which does away with the flicker.
272 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
273 (defun eldoc-pre-command-refresh-echo-area ()
274 (and eldoc-last-message
275 (not (minibufferp)) ;We don't use the echo area when in minibuffer.
276 (if (and (eldoc-display-message-no-interference-p)
277 (eldoc--message-command-p this-command
))
278 (eldoc-message eldoc-last-message
)
279 ;; No need to call eldoc-message since the echo area will be cleared
280 ;; for us, but do note that the last-message will be gone.
281 (setq eldoc-last-message nil
))))
283 ;; Decide whether now is a good time to display a message.
284 (defun eldoc-display-message-p ()
285 (and (eldoc-display-message-no-interference-p)
286 ;; If this-command is non-nil while running via an idle
287 ;; timer, we're still in the middle of executing a command,
288 ;; e.g. a query-replace where it would be annoying to
289 ;; overwrite the echo area.
291 (eldoc--message-command-p last-command
)))
294 ;; Check various conditions about the current environment that might make
295 ;; it undesirable to print eldoc messages right this instant.
296 (defun eldoc-display-message-no-interference-p ()
297 (not (or executing-kbd-macro
(bound-and-true-p edebug-active
))))
301 (defvar eldoc-documentation-function nil
302 "If non-nil, function to call to return doc string.
303 The function of no args should return a one-line string for displaying
304 doc about a function etc. appropriate to the context around point.
305 It should return nil if there's no doc appropriate for the context.
306 Typically doc is returned if point is on a function-like name or in its
309 The result is used as is, so the function must explicitly handle
310 the variables `eldoc-argument-case' and `eldoc-echo-area-use-multiline-p',
311 and the face `eldoc-highlight-function-argument', if they are to have any
314 This variable is expected to be made buffer-local by modes (other than
315 Emacs Lisp mode) that support ElDoc.")
317 (defun eldoc-print-current-symbol-info ()
318 ;; This is run from post-command-hook or some idle timer thing,
319 ;; so we need to be careful that errors aren't ignored.
320 (with-demoted-errors "eldoc error: %s"
321 (and (or (eldoc-display-message-p)
322 ;; Erase the last message if we won't display a new one.
323 (when eldoc-last-message
326 (if eldoc-documentation-function
327 (eldoc-message (funcall eldoc-documentation-function
))
328 (let* ((current-symbol (eldoc-current-symbol))
329 (current-fnsym (eldoc-fnsym-in-current-sexp))
331 ((null current-fnsym
)
333 ((eq current-symbol
(car current-fnsym
))
334 (or (apply 'eldoc-get-fnsym-args-string
336 (eldoc-get-var-docstring current-symbol
)))
338 (or (eldoc-get-var-docstring current-symbol
)
339 (apply 'eldoc-get-fnsym-args-string
341 (eldoc-message doc
))))))
343 (defun eldoc-get-fnsym-args-string (sym &optional index
)
344 "Return a string containing the parameter list of the function SYM.
345 If SYM is a subr and no arglist is obtainable from the docstring
346 or elsewhere, return a 1-line docstring. Calls the functions
347 `eldoc-function-argstring-format' and
348 `eldoc-highlight-function-argument' to format the result. The
349 former calls `eldoc-argument-case'; the latter gives the
350 function name `font-lock-function-name-face', and optionally
351 highlights argument number INDEX."
352 (let (args doc advertised
)
353 (cond ((not (and sym
(symbolp sym
) (fboundp sym
))))
354 ((and (eq sym
(aref eldoc-last-data
0))
355 (eq 'function
(aref eldoc-last-data
2)))
356 (setq doc
(aref eldoc-last-data
1)))
357 ((listp (setq advertised
(gethash (indirect-function sym
)
358 advertised-signature-table t
)))
359 (setq args advertised
))
360 ((setq doc
(help-split-fundoc (documentation sym t
) sym
))
361 (setq args
(car doc
))
362 ;; Remove any enclosing (), since e-function-argstring adds them.
363 (string-match "\\`[^ )]* ?" args
)
364 (setq args
(substring args
(match-end 0)))
365 (if (string-match-p ")\\'" args
)
366 (setq args
(substring args
0 -
1))))
368 (setq args
(help-function-arglist sym
))))
370 ;; Stringify, and store before highlighting, downcasing, etc.
371 ;; FIXME should truncate before storing.
372 (eldoc-last-data-store sym
(setq args
(eldoc-function-argstring args
))
374 (setq args doc
)) ; use stored value
375 ;; Change case, highlight, truncate.
377 (eldoc-highlight-function-argument
378 sym
(eldoc-function-argstring-format args
) index
))))
380 (defun eldoc-highlight-function-argument (sym args index
)
381 "Highlight argument INDEX in ARGS list for function SYM.
382 In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
385 (argument-face 'eldoc-highlight-function-argument
))
386 ;; Find the current argument in the argument string. We need to
387 ;; handle `&rest' and informal `...' properly.
389 ;; FIXME: What to do with optional arguments, like in
390 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
391 ;; The problem is there is no robust way to determine if
392 ;; the current argument is indeed a docstring.
393 (while (and index
(>= index
1))
394 (if (string-match "[^ ()]+" args end
)
396 (setq start
(match-beginning 0)
398 (let ((argument (match-string 0 args
)))
399 (cond ((string= argument
"&rest")
400 ;; All the rest arguments are the same.
402 ((string= argument
"&optional"))
403 ((string-match-p "\\.\\.\\.$" argument
)
406 (setq index
(1- index
))))))
407 (setq end
(length args
)
409 argument-face
'font-lock-warning-face
413 (setq doc
(copy-sequence args
))
414 (add-text-properties start end
(list 'face argument-face
) doc
))
415 (setq doc
(eldoc-docstring-format-sym-doc
416 sym doc
(if (functionp sym
) 'font-lock-function-name-face
417 'font-lock-keyword-face
)))
420 ;; Return a string containing a brief (one-line) documentation string for
422 (defun eldoc-get-var-docstring (sym)
424 (cond ((and (eq sym
(aref eldoc-last-data
0))
425 (eq 'variable
(aref eldoc-last-data
2)))
426 (aref eldoc-last-data
1))
428 (let ((doc (documentation-property sym
'variable-documentation t
)))
430 (setq doc
(eldoc-docstring-format-sym-doc
431 sym
(eldoc-docstring-first-line doc
)
432 'font-lock-variable-name-face
))
433 (eldoc-last-data-store sym doc
'variable
)))
436 (defun eldoc-last-data-store (symbol doc type
)
437 (aset eldoc-last-data
0 symbol
)
438 (aset eldoc-last-data
1 doc
)
439 (aset eldoc-last-data
2 type
))
441 ;; Note that any leading `*' in the docstring (which indicates the variable
442 ;; is a user option) is removed.
443 (defun eldoc-docstring-first-line (doc)
445 (substitute-command-keys
447 ;; Don't use "^" in the regexp below since it may match
448 ;; anywhere in the doc-string.
449 (let ((start (if (string-match "\\`\\*" doc
) (match-end 0) 0)))
450 (cond ((string-match "\n" doc
)
451 (substring doc start
(match-beginning 0)))
453 (t (substring doc start
))))))))
455 ;; If the entire line cannot fit in the echo area, the symbol name may be
456 ;; truncated or eliminated entirely from the output to make room for the
458 (defun eldoc-docstring-format-sym-doc (sym doc face
)
460 (let* ((name (symbol-name sym
))
461 (ea-multi eldoc-echo-area-use-multiline-p
)
462 ;; Subtract 1 from window width since emacs will not write
463 ;; any chars to the last column, or in later versions, will
464 ;; cause a wraparound and resize of the echo area.
465 (ea-width (1- (window-width (minibuffer-window))))
466 (strip (- (+ (length name
) (length ": ") (length doc
)) ea-width
)))
467 (cond ((or (<= strip
0)
469 (and ea-multi
(> (length doc
) ea-width
)))
470 (format "%s: %s" (propertize name
'face face
) doc
))
471 ((> (length doc
) ea-width
)
472 (substring (format "%s" doc
) 0 ea-width
))
473 ((>= strip
(length name
))
476 ;; Show the end of the partial symbol name, rather
477 ;; than the beginning, since the former is more likely
478 ;; to be unique given package namespace conventions.
479 (setq name
(substring name strip
))
480 (format "%s: %s" (propertize name
'face face
) doc
))))))
483 ;; Return a list of current function name and argument index.
484 (defun eldoc-fnsym-in-current-sexp ()
486 (let ((argument-index (1- (eldoc-beginning-of-sexp))))
487 ;; If we are at the beginning of function name, this will be -1.
488 (when (< argument-index
0)
489 (setq argument-index
0))
490 ;; Don't do anything if current word is inside a string.
491 (if (= (or (char-after (1- (point))) 0) ?
\")
493 (list (eldoc-current-symbol) argument-index
)))))
495 ;; Move to the beginning of current sexp. Return the number of nested
496 ;; sexp the point was over or after.
497 (defun eldoc-beginning-of-sexp ()
498 (let ((parse-sexp-ignore-comments t
)
499 (num-skipped-sexps 0))
502 ;; First account for the case the point is directly over a
503 ;; beginning of a nested sexp.
509 (setq num-skipped-sexps
1)))
515 (setq num-skipped-sexps
(1+ num-skipped-sexps
))))))
519 ;; returns nil unless current word is an interned symbol.
520 (defun eldoc-current-symbol ()
521 (let ((c (char-after (point))))
523 (memq (char-syntax c
) '(?w ?_
))
524 (intern-soft (current-word)))))
526 ;; Do indirect function resolution if possible.
527 (defun eldoc-symbol-function (fsym)
528 (let ((defn (symbol-function fsym
)))
531 (setq defn
(indirect-function fsym
))
532 (error (setq defn nil
))))
535 (defun eldoc-function-argstring (arglist)
536 "Return ARGLIST as a string enclosed by ().
537 ARGLIST is either a string, or a list of strings or symbols."
538 (cond ((stringp arglist
))
539 ((not (listp arglist
))
541 ((symbolp (car arglist
))
543 (mapconcat (lambda (s) (symbol-name s
))
545 ((stringp (car arglist
))
547 (mapconcat (lambda (s) s
)
550 (format "(%s)" arglist
)))
552 (defun eldoc-function-argstring-format (argstring)
553 "Apply `eldoc-argument-case' to each word in ARGSTRING.
554 The words \"&rest\", \"&optional\" are returned unchanged."
555 (mapconcat (lambda (s)
556 (if (string-match-p "\\`(?&\\(?:optional\\|rest\\))?\\'" s
)
558 (funcall eldoc-argument-case s
)))
559 (split-string argstring
) " "))
562 ;; When point is in a sexp, the function args are not reprinted in the echo
563 ;; area after every possible interactive command because some of them print
564 ;; their own messages in the echo area; the eldoc functions would instantly
565 ;; overwrite them unless it is more restrained.
566 ;; These functions do display-command table management.
568 (defun eldoc-add-command (&rest cmds
)
571 (setq name
(symbol-name name
)))
572 (set (intern name eldoc-message-commands
) t
)))
574 (defun eldoc-add-command-completions (&rest names
)
576 (apply 'eldoc-add-command
(all-completions name obarray
'commandp
))))
578 (defun eldoc-remove-command (&rest cmds
)
581 (setq name
(symbol-name name
)))
582 (unintern name eldoc-message-commands
)))
584 (defun eldoc-remove-command-completions (&rest names
)
586 (apply 'eldoc-remove-command
587 (all-completions name eldoc-message-commands
))))
590 ;; Prime the command list.
591 (eldoc-add-command-completions
592 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
593 "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-"
594 "handle-select-window" "indent-for-tab-command" "left-" "mark-page"
595 "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-"
596 "move-end-of-" "next-" "other-window" "pop-global-mark" "previous-"
597 "recenter" "right-" "scroll-" "self-insert-command" "split-window-"
602 ;;; eldoc.el ends here