(top-level): Revert previous change: require cl when compiling.
[emacs.git] / lisp / emacs-lisp / eldoc.el
blob710ff821f1dcb698fea23001e1c33abe94f0c1fb
1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Noah Friedman <friedman@splode.com>
7 ;; Maintainer: friedman@splode.com
8 ;; Keywords: extensions
9 ;; Created: 1995-10-06
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This program was inspired by the behavior of the "mouse documentation
29 ;; window" on many Lisp Machine systems; as you type a function's symbol
30 ;; name as part of a sexp, it will print the argument list for that
31 ;; function. Behavior is not identical; for example, you need not actually
32 ;; type the function name, you need only move point around in a sexp that
33 ;; calls it. Also, if point is over a documented variable, it will print
34 ;; the one-line documentation for that variable instead, to remind you of
35 ;; that variable's meaning.
37 ;; One useful way to enable this minor mode is to put the following in your
38 ;; .emacs:
40 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
41 ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
42 ;; (add-hook 'ielm-mode-hook 'turn-on-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'.
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 (defface eldoc-highlight-function-argument
103 '((t (:inherit bold)))
104 "Face used for the argument at point in a function's argument list."
105 :group 'eldoc)
107 ;;; No user options below here.
109 (defvar eldoc-message-commands-table-size 31
110 "This is used by `eldoc-add-command' to initialize `eldoc-message-commands'
111 as an obarray.
112 It should probably never be necessary to do so, but if you
113 choose to increase the number of buckets, you must do so before loading
114 this file since the obarray is initialized at load time.
115 Remember to keep it a prime number to improve hash performance.")
117 (defconst eldoc-message-commands
118 (make-vector eldoc-message-commands-table-size 0)
119 "Commands after which it is appropriate to print in the echo area.
120 Eldoc does not try to print function arglists, etc. after just any command,
121 because some commands print their own messages in the echo area and these
122 functions would instantly overwrite them. But `self-insert-command' as well
123 as most motion commands are good candidates.
124 This variable contains an obarray of symbols; do not manipulate it
125 directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.")
127 (defconst eldoc-last-data (make-vector 3 nil)
128 "Bookkeeping; elements are as follows:
129 0 - contains the last symbol read from the buffer.
130 1 - contains the string last displayed in the echo area for variables,
131 or argument string for functions.
132 2 - 'function if function args, 'variable if variable documentation.")
133 (defvar eldoc-last-message nil)
135 (defvar eldoc-timer nil "eldoc's timer object.")
137 (defvar eldoc-current-idle-delay eldoc-idle-delay
138 "Idle time delay currently in use by timer.
139 This is used to determine if `eldoc-idle-delay' is changed by the user.")
142 ;;;###autoload
143 (define-minor-mode eldoc-mode
144 "Toggle ElDoc mode on or off.
145 In ElDoc mode, the echo area displays information about a
146 function or variable in the text where point is. If point is
147 on a documented variable, it displays the first line of that
148 variable's doc string. Otherwise it displays the argument list
149 of the function called in the expression point is on.
151 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
152 :group 'eldoc :lighter eldoc-minor-mode-string
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 command `eldoc-mode')."
164 (interactive)
165 (eldoc-mode 1))
168 (defun eldoc-schedule-timer ()
169 (or (and eldoc-timer
170 (memq eldoc-timer timer-idle-list))
171 (setq eldoc-timer
172 (run-with-idle-timer eldoc-idle-delay t
173 'eldoc-print-current-symbol-info)))
175 ;; If user has changed the idle delay, update the timer.
176 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
177 (setq eldoc-current-idle-delay eldoc-idle-delay)
178 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
180 (defun eldoc-message (&rest args)
181 (let ((omessage eldoc-last-message))
182 (setq eldoc-last-message
183 (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
184 ((null (car args)) nil)
185 ;; If only one arg, no formatting to do, so put it in
186 ;; eldoc-last-message so eq test above might succeed on
187 ;; subsequent calls.
188 ((null (cdr args)) (car args))
189 (t (apply 'format args))))
190 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
191 ;; are recorded in a log. Do not put eldoc messages in that log since
192 ;; they are Legion.
193 ;; Emacs way of preventing log messages.
194 (let ((message-log-max nil))
195 (cond (eldoc-last-message (message "%s" eldoc-last-message))
196 (omessage (message nil)))))
197 eldoc-last-message)
199 ;; This function goes on pre-command-hook for XEmacs or when using idle
200 ;; timers in Emacs. Motion commands clear the echo area for some reason,
201 ;; which make eldoc messages flicker or disappear just before motion
202 ;; begins. This function reprints the last eldoc message immediately
203 ;; before the next command executes, which does away with the flicker.
204 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
205 (defun eldoc-pre-command-refresh-echo-area ()
206 (and eldoc-last-message
207 (if (eldoc-display-message-no-interference-p)
208 (eldoc-message eldoc-last-message)
209 (setq eldoc-last-message nil))))
211 ;; Decide whether now is a good time to display a message.
212 (defun eldoc-display-message-p ()
213 (and (eldoc-display-message-no-interference-p)
214 ;; If this-command is non-nil while running via an idle
215 ;; timer, we're still in the middle of executing a command,
216 ;; e.g. a query-replace where it would be annoying to
217 ;; overwrite the echo area.
218 (and (not this-command)
219 (symbolp last-command)
220 (intern-soft (symbol-name last-command)
221 eldoc-message-commands))))
223 ;; Check various conditions about the current environment that might make
224 ;; it undesirable to print eldoc messages right this instant.
225 (defun eldoc-display-message-no-interference-p ()
226 (and eldoc-mode
227 (not executing-kbd-macro)
228 (not (and (boundp 'edebug-active) edebug-active))
229 ;; Having this mode operate in an active minibuffer/echo area causes
230 ;; interference with what's going on there.
231 (not cursor-in-echo-area)
232 (not (eq (selected-window) (minibuffer-window)))))
235 ;;;###autoload
236 (defvar eldoc-documentation-function nil
237 "If non-nil, function to call to return doc string.
238 The function of no args should return a one-line string for displaying
239 doc about a function etc. appropriate to the context around point.
240 It should return nil if there's no doc appropriate for the context.
241 Typically doc is returned if point is on a function-like name or in its
242 arg list.
244 This variable is expected to be made buffer-local by modes (other than
245 Emacs Lisp mode) that support Eldoc.")
247 (defun eldoc-print-current-symbol-info ()
248 (condition-case err
249 (and (eldoc-display-message-p)
250 (if eldoc-documentation-function
251 (eldoc-message (funcall eldoc-documentation-function))
252 (let* ((current-symbol (eldoc-current-symbol))
253 (current-fnsym (eldoc-fnsym-in-current-sexp))
254 (doc (cond
255 ((null current-fnsym)
256 nil)
257 ((eq current-symbol (car current-fnsym))
258 (or (apply 'eldoc-get-fnsym-args-string
259 current-fnsym)
260 (eldoc-get-var-docstring current-symbol)))
262 (or (eldoc-get-var-docstring current-symbol)
263 (apply 'eldoc-get-fnsym-args-string
264 current-fnsym))))))
265 (eldoc-message doc))))
266 ;; This is run from post-command-hook or some idle timer thing,
267 ;; so we need to be careful that errors aren't ignored.
268 (error (message "eldoc error: %s" err))))
270 (defun eldoc-get-fnsym-args-string (sym &optional index)
271 "Return a string containing the parameter list of the function SYM.
272 If SYM is a subr and no arglist is obtainable from the docstring
273 or elsewhere, return a 1-line docstring. Calls the functions
274 `eldoc-function-argstring-format' and
275 `eldoc-highlight-function-argument' to format the result. The
276 former calls `eldoc-argument-case'; the latter gives the
277 function name `font-lock-function-name-face', and optionally
278 highlights argument number INDEX. "
279 (let (args doc)
280 (cond ((not (and sym (symbolp sym) (fboundp sym))))
281 ((and (eq sym (aref eldoc-last-data 0))
282 (eq 'function (aref eldoc-last-data 2)))
283 (setq doc (aref eldoc-last-data 1)))
284 ((setq doc (help-split-fundoc (documentation sym t) sym))
285 (setq args (car doc))
286 ;; Remove any enclosing (), since e-function-argstring adds them.
287 (string-match "\\`[^ )]* ?" args)
288 (setq args (substring args (match-end 0)))
289 (if (string-match ")\\'" args)
290 (setq args (substring args 0 -1))))
292 (setq args (help-function-arglist sym))))
293 (if args
294 ;; Stringify, and store before highlighting, downcasing, etc.
295 ;; FIXME should truncate before storing.
296 (eldoc-last-data-store sym (setq args (eldoc-function-argstring args))
297 'function)
298 (setq args doc)) ; use stored value
299 ;; Change case, highlight, truncate.
300 (if args
301 (eldoc-highlight-function-argument
302 sym (eldoc-function-argstring-format args) index))))
304 (defun eldoc-highlight-function-argument (sym args index)
305 "Highlight argument INDEX in ARGS list for function SYM.
306 In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
307 (let ((start nil)
308 (end 0)
309 (argument-face 'eldoc-highlight-function-argument))
310 ;; Find the current argument in the argument string. We need to
311 ;; handle `&rest' and informal `...' properly.
313 ;; FIXME: What to do with optional arguments, like in
314 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
315 ;; The problem is there is no robust way to determine if
316 ;; the current argument is indeed a docstring.
317 (while (and index (>= index 1))
318 (if (string-match "[^ ()]+" args end)
319 (progn
320 (setq start (match-beginning 0)
321 end (match-end 0))
322 (let ((argument (match-string 0 args)))
323 (cond ((string= argument "&rest")
324 ;; All the rest arguments are the same.
325 (setq index 1))
326 ((string= argument "&optional"))
327 ((string-match "\\.\\.\\.$" argument)
328 (setq index 0))
330 (setq index (1- index))))))
331 (setq end (length args)
332 start (1- end)
333 argument-face 'font-lock-warning-face
334 index 0)))
335 (let ((doc args))
336 (when start
337 (setq doc (copy-sequence args))
338 (add-text-properties start end (list 'face argument-face) doc))
339 (setq doc (eldoc-docstring-format-sym-doc
340 sym doc 'font-lock-function-name-face))
341 doc)))
343 ;; Return a string containing a brief (one-line) documentation string for
344 ;; the variable.
345 (defun eldoc-get-var-docstring (sym)
346 (when sym
347 (cond ((and (eq sym (aref eldoc-last-data 0))
348 (eq 'variable (aref eldoc-last-data 2)))
349 (aref eldoc-last-data 1))
351 (let ((doc (documentation-property sym 'variable-documentation t)))
352 (cond (doc
353 (setq doc (eldoc-docstring-format-sym-doc
354 sym (eldoc-docstring-first-line doc)
355 'font-lock-variable-name-face))
356 (eldoc-last-data-store sym doc 'variable)))
357 doc)))))
359 (defun eldoc-last-data-store (symbol doc type)
360 (aset eldoc-last-data 0 symbol)
361 (aset eldoc-last-data 1 doc)
362 (aset eldoc-last-data 2 type))
364 ;; Note that any leading `*' in the docstring (which indicates the variable
365 ;; is a user option) is removed.
366 (defun eldoc-docstring-first-line (doc)
367 (and (stringp doc)
368 (substitute-command-keys
369 (save-match-data
370 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
371 (cond ((string-match "\n" doc)
372 (substring doc start (match-beginning 0)))
373 ((zerop start) doc)
374 (t (substring doc start))))))))
376 ;; If the entire line cannot fit in the echo area, the symbol name may be
377 ;; truncated or eliminated entirely from the output to make room for the
378 ;; description.
379 (defun eldoc-docstring-format-sym-doc (sym doc face)
380 (save-match-data
381 (let* ((name (symbol-name sym))
382 (ea-multi eldoc-echo-area-use-multiline-p)
383 ;; Subtract 1 from window width since emacs will not write
384 ;; any chars to the last column, or in later versions, will
385 ;; cause a wraparound and resize of the echo area.
386 (ea-width (1- (window-width (minibuffer-window))))
387 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
388 (cond ((or (<= strip 0)
389 (eq ea-multi t)
390 (and ea-multi (> (length doc) ea-width)))
391 (format "%s: %s" (propertize name 'face face) doc))
392 ((> (length doc) ea-width)
393 (substring (format "%s" doc) 0 ea-width))
394 ((>= strip (length name))
395 (format "%s" doc))
397 ;; Show the end of the partial symbol name, rather
398 ;; than the beginning, since the former is more likely
399 ;; to be unique given package namespace conventions.
400 (setq name (substring name strip))
401 (format "%s: %s" (propertize name 'face face) doc))))))
404 ;; Return a list of current function name and argument index.
405 (defun eldoc-fnsym-in-current-sexp ()
406 (save-excursion
407 (let ((argument-index (1- (eldoc-beginning-of-sexp))))
408 ;; If we are at the beginning of function name, this will be -1.
409 (when (< argument-index 0)
410 (setq argument-index 0))
411 ;; Don't do anything if current word is inside a string.
412 (if (= (or (char-after (1- (point))) 0) ?\")
414 (list (eldoc-current-symbol) argument-index)))))
416 ;; Move to the beginnig of current sexp. Return the number of nested
417 ;; sexp the point was over or after.
418 (defun eldoc-beginning-of-sexp ()
419 (let ((parse-sexp-ignore-comments t)
420 (num-skipped-sexps 0))
421 (condition-case err
422 (progn
423 ;; First account for the case the point is directly over a
424 ;; beginning of a nested sexp.
425 (condition-case err
426 (let ((p (point)))
427 (forward-sexp -1)
428 (forward-sexp 1)
429 (when (< (point) p)
430 (setq num-skipped-sexps 1)))
431 (error))
432 (while
433 (let ((p (point)))
434 (forward-sexp -1)
435 (when (< (point) p)
436 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
437 (error))
438 num-skipped-sexps))
440 ;; returns nil unless current word is an interned symbol.
441 (defun eldoc-current-symbol ()
442 (let ((c (char-after (point))))
443 (and c
444 (memq (char-syntax c) '(?w ?_))
445 (intern-soft (current-word)))))
447 ;; Do indirect function resolution if possible.
448 (defun eldoc-symbol-function (fsym)
449 (let ((defn (and (fboundp fsym)
450 (symbol-function fsym))))
451 (and (symbolp defn)
452 (condition-case err
453 (setq defn (indirect-function fsym))
454 (error (setq defn nil))))
455 defn))
457 (defun eldoc-function-argstring (arglist)
458 "Return ARGLIST as a string enclosed by ().
459 ARGLIST is either a string, or a list of strings or symbols."
460 (cond ((stringp arglist))
461 ((not (listp arglist))
462 (setq arglist nil))
463 ((symbolp (car arglist))
464 (setq arglist
465 (mapconcat (lambda (s) (symbol-name s))
466 arglist " ")))
467 ((stringp (car arglist))
468 (setq arglist
469 (mapconcat (lambda (s) s)
470 arglist " "))))
471 (if arglist
472 (format "(%s)" arglist)))
474 (defun eldoc-function-argstring-format (argstring)
475 "Apply `eldoc-argument-case' to each word in ARGSTRING.
476 The words \"&rest\", \"&optional\" are returned unchanged."
477 (mapconcat (lambda (s)
478 (if (member s '("&optional" "&rest"))
480 (funcall eldoc-argument-case s)))
481 (split-string argstring "[][ ()]+" t) " "))
484 ;; When point is in a sexp, the function args are not reprinted in the echo
485 ;; area after every possible interactive command because some of them print
486 ;; their own messages in the echo area; the eldoc functions would instantly
487 ;; overwrite them unless it is more restrained.
488 ;; These functions do display-command table management.
490 (defun eldoc-add-command (&rest cmds)
491 (dolist (name cmds)
492 (and (symbolp name)
493 (setq name (symbol-name name)))
494 (set (intern name eldoc-message-commands) t)))
496 (defun eldoc-add-command-completions (&rest names)
497 (dolist (name names)
498 (apply 'eldoc-add-command (all-completions name obarray 'commandp))))
500 (defun eldoc-remove-command (&rest cmds)
501 (dolist (name cmds)
502 (and (symbolp name)
503 (setq name (symbol-name name)))
504 (unintern name eldoc-message-commands)))
506 (defun eldoc-remove-command-completions (&rest names)
507 (dolist (name names)
508 (apply 'eldoc-remove-command
509 (all-completions name eldoc-message-commands))))
512 ;; Prime the command list.
513 (eldoc-add-command-completions
514 "backward-" "beginning-of-" "move-beginning-of-" "delete-other-windows"
515 "delete-window" "handle-select-window"
516 "end-of-" "move-end-of-" "exchange-point-and-mark" "forward-"
517 "indent-for-tab-command" "goto-" "mark-page" "mark-paragraph"
518 "mouse-set-point" "move-" "pop-global-mark" "next-" "other-window"
519 "previous-" "recenter" "scroll-" "self-insert-command"
520 "split-window-" "up-list" "down-list")
522 (provide 'eldoc)
524 ;; arch-tag: c9a58f9d-2055-46c1-9b82-7248b71a8375
525 ;;; eldoc.el ends here