Merge from gnus--devo--0
[emacs.git] / lisp / emacs-lisp / eldoc.el
blobd5edbce3f1426c6fa6b75f6a4292316f8853c8ca
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, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This program was inspired by the behavior of the "mouse documentation
31 ;; window" on many Lisp Machine systems; as you type a function's symbol
32 ;; name as part of a sexp, it will print the argument list for that
33 ;; function. Behavior is not identical; for example, you need not actually
34 ;; type the function name, you need only move point around in a sexp that
35 ;; calls it. Also, if point is over a documented variable, it will print
36 ;; the one-line documentation for that variable instead, to remind you of
37 ;; that variable's meaning.
39 ;; One useful way to enable this minor mode is to put the following in your
40 ;; .emacs:
42 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
43 ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
44 ;; (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)
46 ;; Major modes for other languages may use Eldoc by defining an
47 ;; appropriate function as the buffer-local value of
48 ;; `eldoc-documentation-function'.
50 ;;; Code:
52 (require 'help-fns) ;For fundoc-usage handling functions.
54 (defgroup eldoc nil
55 "Show function arglist or variable docstring in echo area."
56 :group 'lisp
57 :group 'extensions)
59 (defcustom eldoc-idle-delay 0.50
60 "*Number of seconds of idle time to wait before printing.
61 If user input arrives before this interval of time has elapsed after the
62 last input, no documentation will be printed.
64 If this variable is set to 0, no idle time is required."
65 :type 'number
66 :group 'eldoc)
68 ;;;###autoload
69 (defcustom eldoc-minor-mode-string " ElDoc"
70 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
71 :type '(choice string (const :tag "None" nil))
72 :group 'eldoc)
74 (defcustom eldoc-argument-case 'upcase
75 "Case to display argument names of functions, as a symbol.
76 This has two preferred values: `upcase' or `downcase'.
77 Actually, any name of a function which takes a string as an argument and
78 returns another string is acceptable."
79 :type '(radio (function-item upcase)
80 (function-item downcase)
81 function)
82 :group 'eldoc)
84 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
85 "*Allow long eldoc messages to resize echo area display.
86 If value is t, never attempt to truncate messages; complete symbol name
87 and function arglist or 1-line variable documentation will be displayed
88 even if echo area must be resized to fit.
90 If value is any non-nil value other than t, symbol name may be truncated
91 if it will enable the function arglist or documentation string to fit on a
92 single line without resizing window. Otherwise, behavior is just like
93 former case.
95 If value is nil, messages are always truncated to fit in a single line of
96 display in the echo area. Function or variable symbol name may be
97 truncated to make more of the arglist or documentation string visible."
98 :type '(radio (const :tag "Always" t)
99 (const :tag "Never" nil)
100 (const :tag "Yes, but truncate symbol names if it will\
101 enable argument list to fit on one line" truncate-sym-name-if-fit))
102 :group 'eldoc)
104 (defface eldoc-highlight-function-argument
105 '((t (:inherit bold)))
106 "Face used for the argument at point in a function's argument list."
107 :group 'eldoc)
109 ;;; No user options below here.
111 (defvar eldoc-message-commands-table-size 31
112 "This is used by `eldoc-add-command' to initialize `eldoc-message-commands'
113 as an obarray.
114 It should probably never be necessary to do so, but if you
115 choose to increase the number of buckets, you must do so before loading
116 this file since the obarray is initialized at load time.
117 Remember to keep it a prime number to improve hash performance.")
119 (defconst eldoc-message-commands
120 (make-vector eldoc-message-commands-table-size 0)
121 "Commands after which it is appropriate to print in the echo area.
122 Eldoc does not try to print function arglists, etc. after just any command,
123 because some commands print their own messages in the echo area and these
124 functions would instantly overwrite them. But `self-insert-command' as well
125 as most motion commands are good candidates.
126 This variable contains an obarray of symbols; do not manipulate it
127 directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.")
129 (defconst eldoc-last-data (make-vector 3 nil)
130 "Bookkeeping; elements are as follows:
131 0 - contains the last symbol read from the buffer.
132 1 - contains the string last displayed in the echo area for variables,
133 or argument string for functions.
134 2 - 'function if function args, 'variable if variable documentation.")
135 (defvar eldoc-last-message nil)
137 (defvar eldoc-timer nil "eldoc's timer object.")
139 (defvar eldoc-current-idle-delay eldoc-idle-delay
140 "Idle time delay currently in use by timer.
141 This is used to determine if `eldoc-idle-delay' is changed by the user.")
144 ;;;###autoload
145 (define-minor-mode eldoc-mode
146 "Toggle ElDoc mode on or off.
147 In ElDoc mode, the echo area displays information about a
148 function or variable in the text where point is. If point is
149 on a documented variable, it displays the first line of that
150 variable's doc string. Otherwise it displays the argument list
151 of the function called in the expression point is on.
153 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
154 :group 'eldoc :lighter eldoc-minor-mode-string
155 (setq eldoc-last-message nil)
156 (if eldoc-mode
157 (progn
158 (add-hook 'post-command-hook 'eldoc-schedule-timer nil t)
159 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t))
160 (remove-hook 'post-command-hook 'eldoc-schedule-timer)
161 (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area)))
163 ;;;###autoload
164 (defun turn-on-eldoc-mode ()
165 "Unequivocally turn on ElDoc mode (see command `eldoc-mode')."
166 (interactive)
167 (eldoc-mode 1))
170 (defun eldoc-schedule-timer ()
171 (or (and eldoc-timer
172 (memq eldoc-timer timer-idle-list))
173 (setq eldoc-timer
174 (run-with-idle-timer eldoc-idle-delay t
175 'eldoc-print-current-symbol-info)))
177 ;; If user has changed the idle delay, update the timer.
178 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
179 (setq eldoc-current-idle-delay eldoc-idle-delay)
180 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
182 (defun eldoc-message (&rest args)
183 (let ((omessage eldoc-last-message))
184 (setq eldoc-last-message
185 (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
186 ((null (car args)) nil)
187 ;; If only one arg, no formatting to do, so put it in
188 ;; eldoc-last-message so eq test above might succeed on
189 ;; subsequent calls.
190 ((null (cdr args)) (car args))
191 (t (apply 'format args))))
192 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
193 ;; are recorded in a log. Do not put eldoc messages in that log since
194 ;; they are Legion.
195 ;; Emacs way of preventing log messages.
196 (let ((message-log-max nil))
197 (cond (eldoc-last-message (message "%s" eldoc-last-message))
198 (omessage (message nil)))))
199 eldoc-last-message)
201 ;; This function goes on pre-command-hook for XEmacs or when using idle
202 ;; timers in Emacs. Motion commands clear the echo area for some reason,
203 ;; which make eldoc messages flicker or disappear just before motion
204 ;; begins. This function reprints the last eldoc message immediately
205 ;; before the next command executes, which does away with the flicker.
206 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
207 (defun eldoc-pre-command-refresh-echo-area ()
208 (and eldoc-last-message
209 (if (eldoc-display-message-no-interference-p)
210 (eldoc-message eldoc-last-message)
211 (setq eldoc-last-message nil))))
213 ;; Decide whether now is a good time to display a message.
214 (defun eldoc-display-message-p ()
215 (and (eldoc-display-message-no-interference-p)
216 ;; If this-command is non-nil while running via an idle
217 ;; timer, we're still in the middle of executing a command,
218 ;; e.g. a query-replace where it would be annoying to
219 ;; overwrite the echo area.
220 (and (not this-command)
221 (symbolp last-command)
222 (intern-soft (symbol-name last-command)
223 eldoc-message-commands))))
225 ;; Check various conditions about the current environment that might make
226 ;; it undesirable to print eldoc messages right this instant.
227 (defun eldoc-display-message-no-interference-p ()
228 (and eldoc-mode
229 (not executing-kbd-macro)
230 (not (and (boundp 'edebug-active) edebug-active))
231 ;; Having this mode operate in an active minibuffer/echo area causes
232 ;; interference with what's going on there.
233 (not cursor-in-echo-area)
234 (not (eq (selected-window) (minibuffer-window)))))
237 ;;;###autoload
238 (defvar eldoc-documentation-function nil
239 "If non-nil, function to call to return doc string.
240 The function of no args should return a one-line string for displaying
241 doc about a function etc. appropriate to the context around point.
242 It should return nil if there's no doc appropriate for the context.
243 Typically doc is returned if point is on a function-like name or in its
244 arg list.
246 This variable is expected to be made buffer-local by modes (other than
247 Emacs Lisp mode) that support Eldoc.")
249 (defun eldoc-print-current-symbol-info ()
250 (condition-case err
251 (and (eldoc-display-message-p)
252 (if eldoc-documentation-function
253 (eldoc-message (funcall eldoc-documentation-function))
254 (let* ((current-symbol (eldoc-current-symbol))
255 (current-fnsym (eldoc-fnsym-in-current-sexp))
256 (doc (cond
257 ((null current-fnsym)
258 nil)
259 ((eq current-symbol (car current-fnsym))
260 (or (apply 'eldoc-get-fnsym-args-string
261 current-fnsym)
262 (eldoc-get-var-docstring current-symbol)))
264 (or (eldoc-get-var-docstring current-symbol)
265 (apply 'eldoc-get-fnsym-args-string
266 current-fnsym))))))
267 (eldoc-message doc))))
268 ;; This is run from post-command-hook or some idle timer thing,
269 ;; so we need to be careful that errors aren't ignored.
270 (error (message "eldoc error: %s" err))))
272 (defun eldoc-get-fnsym-args-string (sym &optional index)
273 "Return a string containing the parameter list of the function SYM.
274 If SYM is a subr and no arglist is obtainable from the docstring
275 or elsewhere, return a 1-line docstring. Calls the functions
276 `eldoc-function-argstring-format' and
277 `eldoc-highlight-function-argument' to format the result. The
278 former calls `eldoc-argument-case'; the latter gives the
279 function name `font-lock-function-name-face', and optionally
280 highlights argument number INDEX. "
281 (let (args doc)
282 (cond ((not (and sym (symbolp sym) (fboundp sym))))
283 ((and (eq sym (aref eldoc-last-data 0))
284 (eq 'function (aref eldoc-last-data 2)))
285 (setq doc (aref eldoc-last-data 1)))
286 ((setq doc (help-split-fundoc (documentation sym t) sym))
287 (setq args (car doc))
288 ;; Remove any enclosing (), since e-function-argstring adds them.
289 (string-match "\\`[^ )]* ?" args)
290 (setq args (substring args (match-end 0)))
291 (if (string-match ")\\'" args)
292 (setq args (substring args 0 -1))))
294 (setq args (help-function-arglist sym))))
295 (if args
296 ;; Stringify, and store before highlighting, downcasing, etc.
297 ;; FIXME should truncate before storing.
298 (eldoc-last-data-store sym (setq args (eldoc-function-argstring args))
299 'function)
300 (setq args doc)) ; use stored value
301 ;; Change case, highlight, truncate.
302 (if args
303 (eldoc-highlight-function-argument
304 sym (eldoc-function-argstring-format args) index))))
306 (defun eldoc-highlight-function-argument (sym args index)
307 "Highlight argument INDEX in ARGS list for function SYM.
308 In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
309 (let ((start nil)
310 (end 0)
311 (argument-face 'eldoc-highlight-function-argument))
312 ;; Find the current argument in the argument string. We need to
313 ;; handle `&rest' and informal `...' properly.
315 ;; FIXME: What to do with optional arguments, like in
316 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
317 ;; The problem is there is no robust way to determine if
318 ;; the current argument is indeed a docstring.
319 (while (and index (>= index 1))
320 (if (string-match "[^ ()]+" args end)
321 (progn
322 (setq start (match-beginning 0)
323 end (match-end 0))
324 (let ((argument (match-string 0 args)))
325 (cond ((string= argument "&rest")
326 ;; All the rest arguments are the same.
327 (setq index 1))
328 ((string= argument "&optional"))
329 ((string-match "\\.\\.\\.$" argument)
330 (setq index 0))
332 (setq index (1- index))))))
333 (setq end (length args)
334 start (1- end)
335 argument-face 'font-lock-warning-face
336 index 0)))
337 (let ((doc args))
338 (when start
339 (setq doc (copy-sequence args))
340 (add-text-properties start end (list 'face argument-face) doc))
341 (setq doc (eldoc-docstring-format-sym-doc
342 sym doc 'font-lock-function-name-face))
343 doc)))
345 ;; Return a string containing a brief (one-line) documentation string for
346 ;; the variable.
347 (defun eldoc-get-var-docstring (sym)
348 (when sym
349 (cond ((and (eq sym (aref eldoc-last-data 0))
350 (eq 'variable (aref eldoc-last-data 2)))
351 (aref eldoc-last-data 1))
353 (let ((doc (documentation-property sym 'variable-documentation t)))
354 (cond (doc
355 (setq doc (eldoc-docstring-format-sym-doc
356 sym (eldoc-docstring-first-line doc)
357 'font-lock-variable-name-face))
358 (eldoc-last-data-store sym doc 'variable)))
359 doc)))))
361 (defun eldoc-last-data-store (symbol doc type)
362 (aset eldoc-last-data 0 symbol)
363 (aset eldoc-last-data 1 doc)
364 (aset eldoc-last-data 2 type))
366 ;; Note that any leading `*' in the docstring (which indicates the variable
367 ;; is a user option) is removed.
368 (defun eldoc-docstring-first-line (doc)
369 (and (stringp doc)
370 (substitute-command-keys
371 (save-match-data
372 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
373 (cond ((string-match "\n" doc)
374 (substring doc start (match-beginning 0)))
375 ((zerop start) doc)
376 (t (substring doc start))))))))
378 ;; If the entire line cannot fit in the echo area, the symbol name may be
379 ;; truncated or eliminated entirely from the output to make room for the
380 ;; description.
381 (defun eldoc-docstring-format-sym-doc (sym doc face)
382 (save-match-data
383 (let* ((name (symbol-name sym))
384 (ea-multi eldoc-echo-area-use-multiline-p)
385 ;; Subtract 1 from window width since emacs will not write
386 ;; any chars to the last column, or in later versions, will
387 ;; cause a wraparound and resize of the echo area.
388 (ea-width (1- (window-width (minibuffer-window))))
389 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
390 (cond ((or (<= strip 0)
391 (eq ea-multi t)
392 (and ea-multi (> (length doc) ea-width)))
393 (format "%s: %s" (propertize name 'face face) doc))
394 ((> (length doc) ea-width)
395 (substring (format "%s" doc) 0 ea-width))
396 ((>= strip (length name))
397 (format "%s" doc))
399 ;; Show the end of the partial symbol name, rather
400 ;; than the beginning, since the former is more likely
401 ;; to be unique given package namespace conventions.
402 (setq name (substring name strip))
403 (format "%s: %s" (propertize name 'face face) doc))))))
406 ;; Return a list of current function name and argument index.
407 (defun eldoc-fnsym-in-current-sexp ()
408 (save-excursion
409 (let ((argument-index (1- (eldoc-beginning-of-sexp))))
410 ;; If we are at the beginning of function name, this will be -1.
411 (when (< argument-index 0)
412 (setq argument-index 0))
413 ;; Don't do anything if current word is inside a string.
414 (if (= (or (char-after (1- (point))) 0) ?\")
416 (list (eldoc-current-symbol) argument-index)))))
418 ;; Move to the beginnig of current sexp. Return the number of nested
419 ;; sexp the point was over or after.
420 (defun eldoc-beginning-of-sexp ()
421 (let ((parse-sexp-ignore-comments t)
422 (num-skipped-sexps 0))
423 (condition-case err
424 (progn
425 ;; First account for the case the point is directly over a
426 ;; beginning of a nested sexp.
427 (condition-case err
428 (let ((p (point)))
429 (forward-sexp -1)
430 (forward-sexp 1)
431 (when (< (point) p)
432 (setq num-skipped-sexps 1)))
433 (error))
434 (while
435 (let ((p (point)))
436 (forward-sexp -1)
437 (when (< (point) p)
438 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
439 (error))
440 num-skipped-sexps))
442 ;; returns nil unless current word is an interned symbol.
443 (defun eldoc-current-symbol ()
444 (let ((c (char-after (point))))
445 (and c
446 (memq (char-syntax c) '(?w ?_))
447 (intern-soft (current-word)))))
449 ;; Do indirect function resolution if possible.
450 (defun eldoc-symbol-function (fsym)
451 (let ((defn (and (fboundp fsym)
452 (symbol-function fsym))))
453 (and (symbolp defn)
454 (condition-case err
455 (setq defn (indirect-function fsym))
456 (error (setq defn nil))))
457 defn))
459 (defun eldoc-function-argstring (arglist)
460 "Return ARGLIST as a string enclosed by ().
461 ARGLIST is either a string, or a list of strings or symbols."
462 (cond ((stringp arglist))
463 ((not (listp arglist))
464 (setq arglist nil))
465 ((symbolp (car arglist))
466 (setq arglist
467 (mapconcat (lambda (s) (symbol-name s))
468 arglist " ")))
469 ((stringp (car arglist))
470 (setq arglist
471 (mapconcat (lambda (s) s)
472 arglist " "))))
473 (if arglist
474 (format "(%s)" arglist)))
476 (defun eldoc-function-argstring-format (argstring)
477 "Apply `eldoc-argument-case' to each word in ARGSTRING.
478 The words \"&rest\", \"&optional\" are returned unchanged."
479 (mapconcat (lambda (s)
480 (if (member s '("&optional" "&rest"))
482 (funcall eldoc-argument-case s)))
483 (split-string argstring "[][ ()]+" t) " "))
486 ;; When point is in a sexp, the function args are not reprinted in the echo
487 ;; area after every possible interactive command because some of them print
488 ;; their own messages in the echo area; the eldoc functions would instantly
489 ;; overwrite them unless it is more restrained.
490 ;; These functions do display-command table management.
492 (defun eldoc-add-command (&rest cmds)
493 (dolist (name cmds)
494 (and (symbolp name)
495 (setq name (symbol-name name)))
496 (set (intern name eldoc-message-commands) t)))
498 (defun eldoc-add-command-completions (&rest names)
499 (dolist (name names)
500 (apply 'eldoc-add-command (all-completions name obarray 'commandp))))
502 (defun eldoc-remove-command (&rest cmds)
503 (dolist (name cmds)
504 (and (symbolp name)
505 (setq name (symbol-name name)))
506 (unintern name eldoc-message-commands)))
508 (defun eldoc-remove-command-completions (&rest names)
509 (dolist (name names)
510 (apply 'eldoc-remove-command
511 (all-completions name eldoc-message-commands))))
514 ;; Prime the command list.
515 (eldoc-add-command-completions
516 "backward-" "beginning-of-" "move-beginning-of-" "delete-other-windows"
517 "delete-window" "handle-select-window"
518 "end-of-" "move-end-of-" "exchange-point-and-mark" "forward-"
519 "indent-for-tab-command" "goto-" "mark-page" "mark-paragraph"
520 "mouse-set-point" "move-" "pop-global-mark" "next-" "other-window"
521 "previous-" "recenter" "scroll-" "self-insert-command"
522 "split-window-" "up-list" "down-list")
524 (provide 'eldoc)
526 ;; arch-tag: c9a58f9d-2055-46c1-9b82-7248b71a8375
527 ;;; eldoc.el ends here