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