(vc-default-workfile-unchanged-p): Pass nil
[emacs.git] / lisp / emacs-lisp / eldoc.el
blobcbcd5b2a55512cc27ae809ec9172460c4179fd0e
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.24 2003/02/11 00:11:55 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)
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 (defgroup eldoc nil
53 "Show function arglist or variable docstring in echo area."
54 :group 'lisp
55 :group 'extensions)
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."
63 :type 'number
64 :group 'eldoc)
66 ;;;###autoload
67 (defcustom eldoc-minor-mode-string " ElDoc"
68 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
69 :type '(choice string (const :tag "None" nil))
70 :group 'eldoc)
72 (defcustom eldoc-argument-case 'upcase
73 "Case to display argument names of functions, as a symbol.
74 This has two preferred values: `upcase' or `downcase'.
75 Actually, any name of a function which takes a string as an argument and
76 returns another string is acceptable."
77 :type '(radio (function-item upcase)
78 (function-item downcase)
79 function)
80 :group 'eldoc)
82 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
83 "*Allow long eldoc messages to resize echo area display.
84 If value is t, never attempt to truncate messages; complete symbol name
85 and function arglist or 1-line variable documentation will be displayed
86 even if echo area must be resized to fit.
88 If value is any non-nil value other than t, symbol name may be truncated
89 if it will enable the function arglist or documentation string to fit on a
90 single line without resizing window. Otherwise, behavior is just like
91 former case.
93 If value is nil, messages are always truncated to fit in a single line of
94 display in the echo area. Function or variable symbol name may be
95 truncated to make more of the arglist or documentation string visible."
96 :type '(radio (const :tag "Always" t)
97 (const :tag "Never" nil)
98 (const :tag "Yes, but truncate symbol names if it will\
99 enable argument list to fit on one line" truncate-sym-name-if-fit))
100 :group 'eldoc)
102 ;;; No user options below here.
104 ;; Commands after which it is appropriate to print in the echo area.
105 ;; Eldoc does not try to print function arglists, etc. after just any command,
106 ;; because some commands print their own messages in the echo area and these
107 ;; functions would instantly overwrite them. But self-insert-command as well
108 ;; as most motion commands are good candidates.
109 ;; This variable contains an obarray of symbols; do not manipulate it
110 ;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
111 (defvar eldoc-message-commands nil)
113 ;; This is used by eldoc-add-command to initialize eldoc-message-commands
114 ;; as an obarray.
115 ;; It should probably never be necessary to do so, but if you
116 ;; choose to increase the number of buckets, you must do so before loading
117 ;; this file since the obarray is initialized at load time.
118 ;; Remember to keep it a prime number to improve hash performance.
119 (defvar eldoc-message-commands-table-size 31)
121 ;; Bookkeeping; elements are as follows:
122 ;; 0 - contains the last symbol read from the buffer.
123 ;; 1 - contains the string last displayed in the echo area for that
124 ;; symbol, so it can be printed again if necessary without reconsing.
125 ;; 2 - 'function if function args, 'variable if variable documentation.
126 (defvar eldoc-last-data (make-vector 3 nil))
127 (defvar eldoc-last-message nil)
129 ;; eldoc's timer object.
130 (defvar eldoc-timer nil)
132 ;; idle time delay currently in use by timer.
133 ;; This is used to determine if eldoc-idle-delay is changed by the user.
134 (defvar eldoc-current-idle-delay eldoc-idle-delay)
137 ;;;###autoload
138 (define-minor-mode eldoc-mode
139 "Toggle ElDoc mode on or off.
140 Show the defined parameters for the elisp function near point.
142 For the emacs lisp function at the beginning of the sexp which point is
143 within, show the defined parameters for the function in the echo area.
144 This information is extracted directly from the function or macro if it is
145 in pure lisp. If the emacs function is a subr, the parameters are obtained
146 from the documentation string if possible.
148 If point is over a documented variable, print that variable's docstring
149 instead.
151 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
152 nil eldoc-minor-mode-string nil
153 (setq eldoc-last-message nil)
154 (if eldoc-mode
155 (progn
156 (add-hook 'post-command-hook 'eldoc-schedule-timer nil t)
157 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t))
158 (remove-hook 'post-command-hook 'eldoc-schedule-timer)
159 (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area)))
161 ;;;###autoload
162 (defun turn-on-eldoc-mode ()
163 "Unequivocally turn on eldoc-mode (see variable documentation)."
164 (interactive)
165 (eldoc-mode 1))
168 ;; Idle timers are part of Emacs 19.31 and later.
169 (defun eldoc-schedule-timer ()
170 (or (and eldoc-timer
171 (memq eldoc-timer timer-idle-list))
172 (setq eldoc-timer
173 (run-with-idle-timer eldoc-idle-delay t
174 'eldoc-print-current-symbol-info)))
176 ;; If user has changed the idle delay, update the timer.
177 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
178 (setq eldoc-current-idle-delay eldoc-idle-delay)
179 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
181 (defun eldoc-message (&rest args)
182 (let ((omessage eldoc-last-message))
183 (setq eldoc-last-message
184 (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
185 ((null (car args)) nil)
186 ;; If only one arg, no formatting to do, so put it in
187 ;; eldoc-last-message so eq test above might succeed on
188 ;; subsequent calls.
189 ((null (cdr args)) (car args))
190 (t (apply 'format args))))
191 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
192 ;; are recorded in a log. Do not put eldoc messages in that log since
193 ;; they are Legion.
194 ;; Emacs way of preventing log messages.
195 (let ((message-log-max nil))
196 (cond (eldoc-last-message (message "%s" eldoc-last-message))
197 (omessage (message nil)))))
198 eldoc-last-message)
200 ;; This function goes on pre-command-hook for XEmacs or when using idle
201 ;; timers in Emacs. Motion commands clear the echo area for some reason,
202 ;; which make eldoc messages flicker or disappear just before motion
203 ;; begins. This function reprints the last eldoc message immediately
204 ;; before the next command executes, which does away with the flicker.
205 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
206 (defun eldoc-pre-command-refresh-echo-area ()
207 (and eldoc-last-message
208 (if (eldoc-display-message-no-interference-p)
209 (eldoc-message eldoc-last-message)
210 (setq eldoc-last-message nil))))
212 ;; Decide whether now is a good time to display a message.
213 (defun eldoc-display-message-p ()
214 (and (eldoc-display-message-no-interference-p)
215 ;; If this-command is non-nil while running via an idle
216 ;; timer, we're still in the middle of executing a command,
217 ;; e.g. a query-replace where it would be annoying to
218 ;; overwrite the echo area.
219 (and (not this-command)
220 (symbolp last-command)
221 (intern-soft (symbol-name last-command)
222 eldoc-message-commands))))
224 ;; Check various conditions about the current environment that might make
225 ;; it undesirable to print eldoc messages right this instant.
226 (defun eldoc-display-message-no-interference-p ()
227 (and eldoc-mode
228 (not executing-kbd-macro)
229 (not (and (boundp 'edebug-active) edebug-active))
230 ;; Having this mode operate in an active minibuffer/echo area causes
231 ;; interference with what's going on there.
232 (not cursor-in-echo-area)
233 (not (eq (selected-window) (minibuffer-window)))))
236 (defun eldoc-print-current-symbol-info ()
237 (condition-case err
238 (and (eldoc-display-message-p)
239 (let* ((current-symbol (eldoc-current-symbol))
240 (current-fnsym (eldoc-fnsym-in-current-sexp))
241 (doc (cond
242 ((eq current-symbol current-fnsym)
243 (or (eldoc-get-fnsym-args-string current-fnsym)
244 (eldoc-get-var-docstring current-symbol)))
246 (or (eldoc-get-var-docstring current-symbol)
247 (eldoc-get-fnsym-args-string current-fnsym))))))
248 (eldoc-message doc)))
249 ;; This is run from post-command-hook or some idle timer thing,
250 ;; so we need to be careful that errors aren't ignored.
251 (error (message "eldoc error: %s" err))))
253 ;; Return a string containing the function parameter list, or 1-line
254 ;; docstring if function is a subr and no arglist is obtainable from the
255 ;; docstring or elsewhere.
256 (defun eldoc-get-fnsym-args-string (sym)
257 (let ((args nil)
258 (doc nil))
259 (cond ((not (and sym (symbolp sym) (fboundp sym))))
260 ((and (eq sym (aref eldoc-last-data 0))
261 (eq 'function (aref eldoc-last-data 2)))
262 (setq doc (aref eldoc-last-data 1)))
263 ((setq doc (help-split-fundoc (documentation sym t) sym))
264 (setq args (car doc))
265 (string-match "\\`[^ )]* ?" args)
266 (setq args (concat "(" (substring args (match-end 0)))))
268 (setq args (eldoc-function-argstring sym))))
269 (cond (args
270 (setq doc (eldoc-docstring-format-sym-doc sym args))
271 (eldoc-last-data-store sym doc 'function)))
272 doc))
274 ;; Return a string containing a brief (one-line) documentation string for
275 ;; the variable.
276 (defun eldoc-get-var-docstring (sym)
277 (when sym
278 (cond ((and (eq sym (aref eldoc-last-data 0))
279 (eq 'variable (aref eldoc-last-data 2)))
280 (aref eldoc-last-data 1))
282 (let ((doc (documentation-property sym 'variable-documentation t)))
283 (cond (doc
284 (setq doc (eldoc-docstring-format-sym-doc
285 sym (eldoc-docstring-first-line doc)))
286 (eldoc-last-data-store sym doc 'variable)))
287 doc)))))
289 (defun eldoc-last-data-store (symbol doc type)
290 (aset eldoc-last-data 0 symbol)
291 (aset eldoc-last-data 1 doc)
292 (aset eldoc-last-data 2 type))
294 ;; Note that any leading `*' in the docstring (which indicates the variable
295 ;; is a user option) is removed.
296 (defun eldoc-docstring-first-line (doc)
297 (and (stringp doc)
298 (substitute-command-keys
299 (save-match-data
300 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
301 (cond ((string-match "\n" doc)
302 (substring doc start (match-beginning 0)))
303 ((zerop start) doc)
304 (t (substring doc start))))))))
306 ;; If the entire line cannot fit in the echo area, the symbol name may be
307 ;; truncated or eliminated entirely from the output to make room for the
308 ;; description.
309 (defun eldoc-docstring-format-sym-doc (sym doc)
310 (save-match-data
311 (let* ((name (symbol-name sym))
312 (ea-multi eldoc-echo-area-use-multiline-p)
313 ;; Subtract 1 from window width since emacs will not write
314 ;; any chars to the last column, or in later versions, will
315 ;; cause a wraparound and resize of the echo area.
316 (ea-width (1- (window-width (minibuffer-window))))
317 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
318 (cond ((or (<= strip 0)
319 (eq ea-multi t)
320 (and ea-multi (> (length doc) ea-width)))
321 (format "%s: %s" sym doc))
322 ((> (length doc) ea-width)
323 (substring (format "%s" doc) 0 ea-width))
324 ((>= strip (length name))
325 (format "%s" doc))
327 ;; Show the end of the partial symbol name, rather
328 ;; than the beginning, since the former is more likely
329 ;; to be unique given package namespace conventions.
330 (setq name (substring name strip))
331 (format "%s: %s" name doc))))))
334 (defun eldoc-fnsym-in-current-sexp ()
335 (let ((p (point)))
336 (eldoc-beginning-of-sexp)
337 (prog1
338 ;; Don't do anything if current word is inside a string.
339 (if (= (or (char-after (1- (point))) 0) ?\")
341 (eldoc-current-symbol))
342 (goto-char p))))
344 (defun eldoc-beginning-of-sexp ()
345 (let ((parse-sexp-ignore-comments t))
346 (condition-case err
347 (while (progn
348 (forward-sexp -1)
349 (or (= (char-before) ?\")
350 (> (point) (point-min)))))
351 (error nil))))
353 ;; returns nil unless current word is an interned symbol.
354 (defun eldoc-current-symbol ()
355 (let ((c (char-after (point))))
356 (and c
357 (memq (char-syntax c) '(?w ?_))
358 (intern-soft (current-word)))))
360 ;; Do indirect function resolution if possible.
361 (defun eldoc-symbol-function (fsym)
362 (let ((defn (and (fboundp fsym)
363 (symbol-function fsym))))
364 (and (symbolp defn)
365 (condition-case err
366 (setq defn (indirect-function fsym))
367 (error (setq defn nil))))
368 defn))
370 (defun eldoc-function-argstring (fn)
371 (eldoc-function-argstring-format (help-function-arglist fn)))
373 (defun eldoc-function-argstring-format (arglist)
374 (cond ((not (listp arglist))
375 (setq arglist nil))
376 ((symbolp (car arglist))
377 (setq arglist
378 (mapcar (function (lambda (s)
379 (if (memq s '(&optional &rest))
380 (symbol-name s)
381 (funcall eldoc-argument-case
382 (symbol-name s)))))
383 arglist)))
384 ((stringp (car arglist))
385 (setq arglist
386 (mapcar (function (lambda (s)
387 (if (member s '("&optional" "&rest"))
389 (funcall eldoc-argument-case s))))
390 arglist))))
391 (concat "(" (mapconcat 'identity arglist " ") ")"))
394 ;; When point is in a sexp, the function args are not reprinted in the echo
395 ;; area after every possible interactive command because some of them print
396 ;; their own messages in the echo area; the eldoc functions would instantly
397 ;; overwrite them unless it is more restrained.
398 ;; These functions do display-command table management.
400 (defun eldoc-add-command (&rest cmds)
401 (or eldoc-message-commands
402 (setq eldoc-message-commands
403 (make-vector eldoc-message-commands-table-size 0)))
405 (let (name sym)
406 (while cmds
407 (setq name (car cmds))
408 (setq cmds (cdr cmds))
410 (cond ((symbolp name)
411 (setq sym name)
412 (setq name (symbol-name sym)))
413 ((stringp name)
414 (setq sym (intern-soft name))))
416 (and (symbolp sym)
417 (fboundp sym)
418 (set (intern name eldoc-message-commands) t)))))
420 (defun eldoc-add-command-completions (&rest names)
421 (while names
422 (apply 'eldoc-add-command
423 (all-completions (car names) obarray 'fboundp))
424 (setq names (cdr names))))
426 (defun eldoc-remove-command (&rest cmds)
427 (let (name)
428 (while cmds
429 (setq name (car cmds))
430 (setq cmds (cdr cmds))
432 (and (symbolp name)
433 (setq name (symbol-name name)))
435 (unintern name eldoc-message-commands))))
437 (defun eldoc-remove-command-completions (&rest names)
438 (while names
439 (apply 'eldoc-remove-command
440 (all-completions (car names) eldoc-message-commands))
441 (setq names (cdr names))))
444 ;; Prime the command list.
445 (eldoc-add-command-completions
446 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
447 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
448 "next-" "other-window" "previous-" "recenter" "scroll-"
449 "self-insert-command" "split-window-"
450 "up-list" "down-list")
452 (provide 'eldoc)
454 ;;; eldoc.el ends here