Update copyright.
[emacs.git] / lisp / emacs-lisp / eldoc.el
blob8f7fbb5493b9a5a7bdedf4e5568b02713f15caa2
1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
3 ;; Copyright (C) 1996, 97, 98, 99, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: extensions
8 ;; Created: 1995-10-06
10 ;; $Id: eldoc.el,v 1.22 2003/01/03 11:46:20 jpw 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)
17 ;; any later version.
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.
29 ;;; Commentary:
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
41 ;; .emacs:
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)
48 ;;; Code:
50 (require 'help-fns) ;For fundoc-usage handling functions.
52 ;; Use idle timers if available in the version of emacs running.
53 ;; Please don't change this to use `require'; this package works
54 ;; as-is in XEmacs 19.14 and later and I am striving to maintain
55 ;; compatibility between emacs variants.
56 (or (featurep 'timer)
57 (load "timer" t))
59 (defgroup eldoc nil
60 "Show function arglist or variable docstring in echo area."
61 :group 'lisp
62 :group 'extensions)
64 (defcustom eldoc-idle-delay 0.50
65 "*Number of seconds of idle time to wait before printing.
66 If user input arrives before this interval of time has elapsed after the
67 last input, no documentation will be printed.
69 If this variable is set to 0, no idle time is required."
70 :type 'number
71 :group 'eldoc)
73 ;;;###autoload
74 (defcustom eldoc-minor-mode-string " ElDoc"
75 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
76 :type '(choice string (const :tag "None" nil))
77 :group 'eldoc)
79 (defcustom eldoc-argument-case 'upcase
80 "Case to display argument names of functions, as a symbol.
81 This has two preferred values: `upcase' or `downcase'.
82 Actually, any name of a function which takes a string as an argument and
83 returns another string is acceptable."
84 :type '(radio (function-item upcase)
85 (function-item downcase)
86 function)
87 :group 'eldoc)
89 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
90 "*Allow long eldoc messages to resize echo area display.
91 If value is `t', never attempt to truncate messages; complete symbol name
92 and function arglist or 1-line variable documentation will be displayed
93 even if echo area must be resized to fit.
95 If value is any non-nil value other than `t', symbol name may be truncated
96 if it will enable the function arglist or documentation string to fit on a
97 single line without resizing window. Otherwise, behavior is just like
98 former case.
100 If value is nil, messages are always truncated to fit in a single line of
101 display in the echo area. Function or variable symbol name may be
102 truncated to make more of the arglist or documentation string visible.
104 Non-nil values for this variable have no effect unless
105 `eldoc-echo-area-multiline-supported-p' is non-nil."
106 :type '(radio (const :tag "Always" t)
107 (const :tag "Never" nil)
108 (const :tag "Yes, but truncate symbol names if it will\
109 enable argument list to fit on one line" truncate-sym-name-if-fit))
110 :group 'eldoc)
112 ;;; No user options below here.
114 ;; Non-nil if this version of emacs supports dynamically resizable echo areas.
115 (defvar eldoc-echo-area-multiline-supported-p
116 (and (string-lessp "21" emacs-version)
117 (save-match-data
118 (numberp (string-match "^GNU Emacs" (emacs-version))))))
120 ;; Commands after which it is appropriate to print in the echo area.
121 ;; Eldoc does not try to print function arglists, etc. after just any command,
122 ;; because some commands print their own messages in the echo area and these
123 ;; functions would instantly overwrite them. But self-insert-command as well
124 ;; as most motion commands are good candidates.
125 ;; This variable contains an obarray of symbols; do not manipulate it
126 ;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
127 (defvar eldoc-message-commands nil)
129 ;; This is used by eldoc-add-command to initialize eldoc-message-commands
130 ;; as an obarray.
131 ;; It should probably never be necessary to do so, but if you
132 ;; choose to increase the number of buckets, you must do so before loading
133 ;; this file since the obarray is initialized at load time.
134 ;; Remember to keep it a prime number to improve hash performance.
135 (defvar eldoc-message-commands-table-size 31)
137 ;; Bookkeeping; elements are as follows:
138 ;; 0 - contains the last symbol read from the buffer.
139 ;; 1 - contains the string last displayed in the echo area for that
140 ;; symbol, so it can be printed again if necessary without reconsing.
141 ;; 2 - 'function if function args, 'variable if variable documentation.
142 (defvar eldoc-last-data (make-vector 3 nil))
143 (defvar eldoc-last-message nil)
145 ;; Idle timers are supported in Emacs 19.31 and later.
146 (defvar eldoc-use-idle-timer-p (fboundp 'run-with-idle-timer))
148 ;; eldoc's timer object, if using idle timers
149 (defvar eldoc-timer nil)
151 ;; idle time delay currently in use by timer.
152 ;; This is used to determine if eldoc-idle-delay is changed by the user.
153 (defvar eldoc-current-idle-delay eldoc-idle-delay)
156 ;;;###autoload
157 (define-minor-mode eldoc-mode
158 "Toggle ElDoc mode on or off.
159 Show the defined parameters for the elisp function near point.
161 For the emacs lisp function at the beginning of the sexp which point is
162 within, show the defined parameters for the function in the echo area.
163 This information is extracted directly from the function or macro if it is
164 in pure lisp. If the emacs function is a subr, the parameters are obtained
165 from the documentation string if possible.
167 If point is over a documented variable, print that variable's docstring
168 instead.
170 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
171 nil eldoc-minor-mode-string nil
172 (setq eldoc-last-message nil)
173 (cond (eldoc-use-idle-timer-p
174 (add-hook 'post-command-hook 'eldoc-schedule-timer)
175 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area))
177 ;; Use post-command-idle-hook if defined, otherwise use
178 ;; post-command-hook. The former is only proper to use in Emacs
179 ;; 19.30; that is the first version in which it appeared, but it
180 ;; was obsolesced by idle timers in Emacs 19.31.
181 (add-hook (if (boundp 'post-command-idle-hook)
182 'post-command-idle-hook
183 'post-command-hook)
184 'eldoc-print-current-symbol-info t t)
185 ;; quick and dirty hack for seeing if this is XEmacs
186 (and (fboundp 'display-message)
187 (add-hook 'pre-command-hook
188 'eldoc-pre-command-refresh-echo-area t t)))))
190 ;;;###autoload
191 (defun turn-on-eldoc-mode ()
192 "Unequivocally turn on eldoc-mode (see variable documentation)."
193 (interactive)
194 (eldoc-mode 1))
197 ;; Idle timers are part of Emacs 19.31 and later.
198 (defun eldoc-schedule-timer ()
199 (or (and eldoc-timer
200 (memq eldoc-timer timer-idle-list))
201 (setq eldoc-timer
202 (run-with-idle-timer eldoc-idle-delay t
203 'eldoc-print-current-symbol-info)))
205 ;; If user has changed the idle delay, update the timer.
206 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
207 (setq eldoc-current-idle-delay eldoc-idle-delay)
208 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
210 (defun eldoc-message (&rest args)
211 (let ((omessage eldoc-last-message))
212 (cond ((eq (car args) eldoc-last-message))
213 ((or (null args)
214 (null (car args)))
215 (setq eldoc-last-message nil))
216 ;; If only one arg, no formatting to do so put it in
217 ;; eldoc-last-message so eq test above might succeed on
218 ;; subsequent calls.
219 ((null (cdr args))
220 (setq eldoc-last-message (car args)))
222 (setq eldoc-last-message (apply 'format args))))
223 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
224 ;; are recorded in a log. Do not put eldoc messages in that log since
225 ;; they are Legion.
226 (cond ((fboundp 'display-message)
227 ;; XEmacs 19.13 way of preventing log messages.
228 (cond (eldoc-last-message
229 (display-message 'no-log eldoc-last-message))
230 (omessage
231 (clear-message 'no-log))))
233 ;; Emacs way of preventing log messages.
234 (let ((message-log-max nil))
235 (cond (eldoc-last-message
236 (message "%s" eldoc-last-message))
237 (omessage
238 (message nil)))))))
239 eldoc-last-message)
241 ;; This function goes on pre-command-hook for XEmacs or when using idle
242 ;; timers in Emacs. Motion commands clear the echo area for some reason,
243 ;; which make eldoc messages flicker or disappear just before motion
244 ;; begins. This function reprints the last eldoc message immediately
245 ;; before the next command executes, which does away with the flicker.
246 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
247 (defun eldoc-pre-command-refresh-echo-area ()
248 (and eldoc-last-message
249 (if (eldoc-display-message-no-interference-p)
250 (eldoc-message eldoc-last-message)
251 (setq eldoc-last-message nil))))
253 ;; Decide whether now is a good time to display a message.
254 (defun eldoc-display-message-p ()
255 (and (eldoc-display-message-no-interference-p)
256 (cond (eldoc-use-idle-timer-p
257 ;; If this-command is non-nil while running via an idle
258 ;; timer, we're still in the middle of executing a command,
259 ;; e.g. a query-replace where it would be annoying to
260 ;; overwrite the echo area.
261 (and (not this-command)
262 (symbolp last-command)
263 (intern-soft (symbol-name last-command)
264 eldoc-message-commands)))
266 ;; If we don't have idle timers, this function is
267 ;; running on post-command-hook directly; that means the
268 ;; user's last command is still on `this-command', and we
269 ;; must wait briefly for input to see whether to do display.
270 (and (symbolp this-command)
271 (intern-soft (symbol-name this-command)
272 eldoc-message-commands)
273 (sit-for eldoc-idle-delay))))))
275 ;; Check various conditions about the current environment that might make
276 ;; it undesirable to print eldoc messages right this instant.
277 (defun eldoc-display-message-no-interference-p ()
278 (and eldoc-mode
279 (not executing-kbd-macro)
280 (not (and (boundp 'edebug-active) edebug-active))
281 ;; Having this mode operate in an active minibuffer/echo area causes
282 ;; interference with what's going on there.
283 (not cursor-in-echo-area)
284 (not (eq (selected-window) (minibuffer-window)))))
287 (defun eldoc-print-current-symbol-info ()
288 (condition-case err
289 (and (eldoc-display-message-p)
290 (let* ((current-symbol (eldoc-current-symbol))
291 (current-fnsym (eldoc-fnsym-in-current-sexp))
292 (doc (cond
293 ((eq current-symbol current-fnsym)
294 (or (eldoc-get-fnsym-args-string current-fnsym)
295 (eldoc-get-var-docstring current-symbol)))
297 (or (eldoc-get-var-docstring current-symbol)
298 (eldoc-get-fnsym-args-string current-fnsym))))))
299 (eldoc-message doc)))
300 ;; This is run from post-command-hook or some idle timer thing,
301 ;; so we need to be careful that errors aren't ignored.
302 (error (message "eldoc error: %s" err))))
304 ;; Return a string containing the function parameter list, or 1-line
305 ;; docstring if function is a subr and no arglist is obtainable from the
306 ;; docstring or elsewhere.
307 (defun eldoc-get-fnsym-args-string (sym)
308 (let ((args nil)
309 (doc nil))
310 (cond ((not (and sym
311 (symbolp sym)
312 (fboundp sym))))
313 ((and (eq sym (aref eldoc-last-data 0))
314 (eq 'function (aref eldoc-last-data 2)))
315 (setq doc (aref eldoc-last-data 1)))
316 ((setq doc (help-split-fundoc (documentation sym t) sym))
317 (setq args (car doc))
318 (string-match "\\`[^ )]* ?" args)
319 (setq args (concat "(" (substring args (match-end 0)))))
321 (setq args (eldoc-function-argstring sym))))
322 (cond (args
323 (setq doc (eldoc-docstring-format-sym-doc sym args))
324 (eldoc-last-data-store sym doc 'function)))
325 doc))
327 ;; Return a string containing a brief (one-line) documentation string for
328 ;; the variable.
329 (defun eldoc-get-var-docstring (sym)
330 (when sym
331 (cond ((and (eq sym (aref eldoc-last-data 0))
332 (eq 'variable (aref eldoc-last-data 2)))
333 (aref eldoc-last-data 1))
335 (let ((doc (documentation-property sym 'variable-documentation t)))
336 (cond (doc
337 (setq doc (eldoc-docstring-format-sym-doc
338 sym (eldoc-docstring-first-line doc)))
339 (eldoc-last-data-store sym doc 'variable)))
340 doc)))))
342 (defun eldoc-last-data-store (symbol doc type)
343 (aset eldoc-last-data 0 symbol)
344 (aset eldoc-last-data 1 doc)
345 (aset eldoc-last-data 2 type))
347 ;; Note that any leading `*' in the docstring (which indicates the variable
348 ;; is a user option) is removed.
349 (defun eldoc-docstring-first-line (doc)
350 (and (stringp doc)
351 (substitute-command-keys
352 (save-match-data
353 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
354 (cond ((string-match "\n" doc)
355 (substring doc start (match-beginning 0)))
356 ((zerop start) doc)
357 (t (substring doc start))))))))
359 ;; If the entire line cannot fit in the echo area, the symbol name may be
360 ;; truncated or eliminated entirely from the output to make room for the
361 ;; description.
362 (defun eldoc-docstring-format-sym-doc (sym doc)
363 (save-match-data
364 (let* ((name (symbol-name sym))
365 (ea-multi (and eldoc-echo-area-multiline-supported-p
366 eldoc-echo-area-use-multiline-p))
367 ;; Subtract 1 from window width since emacs will not write
368 ;; any chars to the last column, or in later versions, will
369 ;; cause a wraparound and resize of the echo area.
370 (ea-width (1- (window-width (minibuffer-window))))
371 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
372 (cond ((or (<= strip 0)
373 (eq ea-multi t)
374 (and ea-multi (> (length doc) ea-width)))
375 (format "%s: %s" sym doc))
376 ((> (length doc) ea-width)
377 (substring (format "%s" doc) 0 ea-width))
378 ((>= strip (length name))
379 (format "%s" doc))
381 ;; Show the end of the partial symbol name, rather
382 ;; than the beginning, since the former is more likely
383 ;; to be unique given package namespace conventions.
384 (setq name (substring name strip))
385 (format "%s: %s" name doc))))))
388 (defun eldoc-fnsym-in-current-sexp ()
389 (let ((p (point)))
390 (eldoc-beginning-of-sexp)
391 (prog1
392 ;; Don't do anything if current word is inside a string.
393 (if (= (or (char-after (1- (point))) 0) ?\")
395 (eldoc-current-symbol))
396 (goto-char p))))
398 (defun eldoc-beginning-of-sexp ()
399 (let ((parse-sexp-ignore-comments t))
400 (condition-case err
401 (while (progn
402 (forward-sexp -1)
403 (or (= (char-before) ?\")
404 (> (point) (point-min)))))
405 (error nil))))
407 ;; returns nil unless current word is an interned symbol.
408 (defun eldoc-current-symbol ()
409 (let ((c (char-after (point))))
410 (and c
411 (memq (char-syntax c) '(?w ?_))
412 (intern-soft (current-word)))))
414 ;; Do indirect function resolution if possible.
415 (defun eldoc-symbol-function (fsym)
416 (let ((defn (and (fboundp fsym)
417 (symbol-function fsym))))
418 (and (symbolp defn)
419 (condition-case err
420 (setq defn (indirect-function fsym))
421 (error (setq defn nil))))
422 defn))
424 (defun eldoc-function-argstring (fn)
425 (eldoc-function-argstring-format (help-function-arglist fn)))
427 (defun eldoc-function-argstring-format (arglist)
428 (cond ((not (listp arglist))
429 (setq arglist nil))
430 ((symbolp (car arglist))
431 (setq arglist
432 (mapcar (function (lambda (s)
433 (if (memq s '(&optional &rest))
434 (symbol-name s)
435 (funcall eldoc-argument-case
436 (symbol-name s)))))
437 arglist)))
438 ((stringp (car arglist))
439 (setq arglist
440 (mapcar (function (lambda (s)
441 (if (member s '("&optional" "&rest"))
443 (funcall eldoc-argument-case s))))
444 arglist))))
445 (concat "(" (mapconcat 'identity arglist " ") ")"))
448 ;; When point is in a sexp, the function args are not reprinted in the echo
449 ;; area after every possible interactive command because some of them print
450 ;; their own messages in the echo area; the eldoc functions would instantly
451 ;; overwrite them unless it is more restrained.
452 ;; These functions do display-command table management.
454 (defun eldoc-add-command (&rest cmds)
455 (or eldoc-message-commands
456 (setq eldoc-message-commands
457 (make-vector eldoc-message-commands-table-size 0)))
459 (let (name sym)
460 (while cmds
461 (setq name (car cmds))
462 (setq cmds (cdr cmds))
464 (cond ((symbolp name)
465 (setq sym name)
466 (setq name (symbol-name sym)))
467 ((stringp name)
468 (setq sym (intern-soft name))))
470 (and (symbolp sym)
471 (fboundp sym)
472 (set (intern name eldoc-message-commands) t)))))
474 (defun eldoc-add-command-completions (&rest names)
475 (while names
476 (apply 'eldoc-add-command
477 (all-completions (car names) obarray 'fboundp))
478 (setq names (cdr names))))
480 (defun eldoc-remove-command (&rest cmds)
481 (let (name)
482 (while cmds
483 (setq name (car cmds))
484 (setq cmds (cdr cmds))
486 (and (symbolp name)
487 (setq name (symbol-name name)))
489 (if (fboundp 'unintern)
490 (unintern name eldoc-message-commands)
491 (let ((s (intern-soft name eldoc-message-commands)))
492 (and s
493 (makunbound s)))))))
495 (defun eldoc-remove-command-completions (&rest names)
496 (while names
497 (apply 'eldoc-remove-command
498 (all-completions (car names) eldoc-message-commands))
499 (setq names (cdr names))))
502 ;; Prime the command list.
503 (eldoc-add-command-completions
504 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
505 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
506 "next-" "other-window" "previous-" "recenter" "scroll-"
507 "self-insert-command" "split-window-"
508 "up-list" "down-list")
510 (provide 'eldoc)
512 ;;; eldoc.el ends here