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