Sync to HEAD
[bpt/emacs.git] / lisp / emacs-lisp / eldoc.el
1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
2
3 ;; Copyright (C) 1996, 97, 98, 99, 2000, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: extensions
8 ;; Created: 1995-10-06
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This program was inspired by the behavior of the "mouse documentation
30 ;; window" on many Lisp Machine systems; as you type a function's symbol
31 ;; name as part of a sexp, it will print the argument list for that
32 ;; function. Behavior is not identical; for example, you need not actually
33 ;; type the function name, you need only move point around in a sexp that
34 ;; calls it. Also, if point is over a documented variable, it will print
35 ;; the one-line documentation for that variable instead, to remind you of
36 ;; that variable's meaning.
37
38 ;; One useful way to enable this minor mode is to put the following in your
39 ;; .emacs:
40 ;;
41 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
42 ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
43 ;; (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)
44
45 ;; Major modes for other languages may use Eldoc by defining an
46 ;; appropriate function as the buffer-local value of
47 ;; `eldoc-print-current-symbol-info-function'.
48
49 ;;; Code:
50
51 (require 'help-fns) ;For fundoc-usage handling functions.
52
53 (defgroup eldoc nil
54 "Show function arglist or variable docstring in echo area."
55 :group 'lisp
56 :group 'extensions)
57
58 (defcustom eldoc-idle-delay 0.50
59 "*Number of seconds of idle time to wait before printing.
60 If user input arrives before this interval of time has elapsed after the
61 last input, no documentation will be printed.
62
63 If this variable is set to 0, no idle time is required."
64 :type 'number
65 :group 'eldoc)
66
67 ;;;###autoload
68 (defcustom eldoc-minor-mode-string " ElDoc"
69 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
70 :type '(choice string (const :tag "None" nil))
71 :group 'eldoc)
72
73 (defcustom eldoc-argument-case 'upcase
74 "Case to display argument names of functions, as a symbol.
75 This has two preferred values: `upcase' or `downcase'.
76 Actually, any name of a function which takes a string as an argument and
77 returns another string is acceptable."
78 :type '(radio (function-item upcase)
79 (function-item downcase)
80 function)
81 :group 'eldoc)
82
83 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
84 "*Allow long eldoc messages to resize echo area display.
85 If value is t, never attempt to truncate messages; complete symbol name
86 and function arglist or 1-line variable documentation will be displayed
87 even if echo area must be resized to fit.
88
89 If value is any non-nil value other than t, symbol name may be truncated
90 if it will enable the function arglist or documentation string to fit on a
91 single line without resizing window. Otherwise, behavior is just like
92 former case.
93
94 If value is nil, messages are always truncated to fit in a single line of
95 display in the echo area. Function or variable symbol name may be
96 truncated to make more of the arglist or documentation string visible."
97 :type '(radio (const :tag "Always" t)
98 (const :tag "Never" nil)
99 (const :tag "Yes, but truncate symbol names if it will\
100 enable argument list to fit on one line" truncate-sym-name-if-fit))
101 :group 'eldoc)
102
103 ;;; No user options below here.
104
105 ;; Commands after which it is appropriate to print in the echo area.
106 ;; Eldoc does not try to print function arglists, etc. after just any command,
107 ;; because some commands print their own messages in the echo area and these
108 ;; functions would instantly overwrite them. But self-insert-command as well
109 ;; as most motion commands are good candidates.
110 ;; This variable contains an obarray of symbols; do not manipulate it
111 ;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
112 (defvar eldoc-message-commands nil)
113
114 ;; This is used by eldoc-add-command to initialize eldoc-message-commands
115 ;; as an obarray.
116 ;; It should probably never be necessary to do so, but if you
117 ;; choose to increase the number of buckets, you must do so before loading
118 ;; this file since the obarray is initialized at load time.
119 ;; Remember to keep it a prime number to improve hash performance.
120 (defvar eldoc-message-commands-table-size 31)
121
122 ;; Bookkeeping; elements are as follows:
123 ;; 0 - contains the last symbol read from the buffer.
124 ;; 1 - contains the string last displayed in the echo area for that
125 ;; symbol, so it can be printed again if necessary without reconsing.
126 ;; 2 - 'function if function args, 'variable if variable documentation.
127 (defvar eldoc-last-data (make-vector 3 nil))
128 (defvar eldoc-last-message nil)
129
130 ;; eldoc's timer object.
131 (defvar eldoc-timer nil)
132
133 ;; idle time delay currently in use by timer.
134 ;; This is used to determine if eldoc-idle-delay is changed by the user.
135 (defvar eldoc-current-idle-delay eldoc-idle-delay)
136
137 \f
138 ;;;###autoload
139 (define-minor-mode eldoc-mode
140 "Toggle ElDoc mode on or off.
141 Show the defined parameters for the elisp function near point.
142
143 For the emacs lisp function at the beginning of the sexp which point is
144 within, show the defined parameters for the function in the echo area.
145 This information is extracted directly from the function or macro if it is
146 in pure lisp. If the emacs function is a subr, the parameters are obtained
147 from the documentation string if possible.
148
149 If point is over a documented variable, print that variable's docstring
150 instead.
151
152 With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
153 nil eldoc-minor-mode-string nil
154 (setq eldoc-last-message nil)
155 (if eldoc-mode
156 (progn
157 (add-hook 'post-command-hook 'eldoc-schedule-timer nil t)
158 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t))
159 (remove-hook 'post-command-hook 'eldoc-schedule-timer)
160 (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area)))
161
162 ;;;###autoload
163 (defun turn-on-eldoc-mode ()
164 "Unequivocally turn on eldoc-mode (see variable documentation)."
165 (interactive)
166 (eldoc-mode 1))
167
168 \f
169 ;; Idle timers are part of Emacs 19.31 and later.
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)))
176
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))))
181
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)
200
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))))
212
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))))
224
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)))))
235
236 \f
237 (defvar eldoc-print-current-symbol-info-function nil
238 "If non-nil, function to call to return doc string.
239 The function of no args should return a one-line string for displaying
240 doc about a function etc. appropriate to the context around point.
241 It should return nil if there's no doc appropriate for the context.
242 Typically doc is returned if point is on a function-like name or in its
243 arg list.
244
245 This variable is expected to be made buffer-local by modes (other than
246 Emacs Lisp mode) that support Eldoc.")
247
248 (defun eldoc-print-current-symbol-info ()
249 (condition-case err
250 (and (eldoc-display-message-p)
251 (if eldoc-print-current-symbol-info-function
252 (eldoc-message (funcall eldoc-print-current-symbol-info-function))
253 (let* ((current-symbol (eldoc-current-symbol))
254 (current-fnsym (eldoc-fnsym-in-current-sexp))
255 (doc (cond
256 ((eq current-symbol current-fnsym)
257 (or (eldoc-get-fnsym-args-string current-fnsym)
258 (eldoc-get-var-docstring current-symbol)))
259 (t
260 (or (eldoc-get-var-docstring current-symbol)
261 (eldoc-get-fnsym-args-string 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 ;; Return a string containing the function parameter list, or 1-line
268 ;; docstring if function is a subr and no arglist is obtainable from the
269 ;; docstring or elsewhere.
270 (defun eldoc-get-fnsym-args-string (sym)
271 (let ((args nil)
272 (doc nil))
273 (cond ((not (and sym (symbolp sym) (fboundp sym))))
274 ((and (eq sym (aref eldoc-last-data 0))
275 (eq 'function (aref eldoc-last-data 2)))
276 (setq doc (aref eldoc-last-data 1)))
277 ((setq doc (help-split-fundoc (documentation sym t) sym))
278 (setq args (car doc))
279 (string-match "\\`[^ )]* ?" args)
280 (setq args (concat "(" (substring args (match-end 0)))))
281 (t
282 (setq args (eldoc-function-argstring sym))))
283 (cond (args
284 (setq doc (eldoc-docstring-format-sym-doc sym args))
285 (eldoc-last-data-store sym doc 'function)))
286 doc))
287
288 ;; Return a string containing a brief (one-line) documentation string for
289 ;; the variable.
290 (defun eldoc-get-var-docstring (sym)
291 (when sym
292 (cond ((and (eq sym (aref eldoc-last-data 0))
293 (eq 'variable (aref eldoc-last-data 2)))
294 (aref eldoc-last-data 1))
295 (t
296 (let ((doc (documentation-property sym 'variable-documentation t)))
297 (cond (doc
298 (setq doc (eldoc-docstring-format-sym-doc
299 sym (eldoc-docstring-first-line doc)))
300 (eldoc-last-data-store sym doc 'variable)))
301 doc)))))
302
303 (defun eldoc-last-data-store (symbol doc type)
304 (aset eldoc-last-data 0 symbol)
305 (aset eldoc-last-data 1 doc)
306 (aset eldoc-last-data 2 type))
307
308 ;; Note that any leading `*' in the docstring (which indicates the variable
309 ;; is a user option) is removed.
310 (defun eldoc-docstring-first-line (doc)
311 (and (stringp doc)
312 (substitute-command-keys
313 (save-match-data
314 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
315 (cond ((string-match "\n" doc)
316 (substring doc start (match-beginning 0)))
317 ((zerop start) doc)
318 (t (substring doc start))))))))
319
320 ;; If the entire line cannot fit in the echo area, the symbol name may be
321 ;; truncated or eliminated entirely from the output to make room for the
322 ;; description.
323 (defun eldoc-docstring-format-sym-doc (sym doc)
324 (save-match-data
325 (let* ((name (symbol-name sym))
326 (ea-multi eldoc-echo-area-use-multiline-p)
327 ;; Subtract 1 from window width since emacs will not write
328 ;; any chars to the last column, or in later versions, will
329 ;; cause a wraparound and resize of the echo area.
330 (ea-width (1- (window-width (minibuffer-window))))
331 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
332 (cond ((or (<= strip 0)
333 (eq ea-multi t)
334 (and ea-multi (> (length doc) ea-width)))
335 (format "%s: %s" sym doc))
336 ((> (length doc) ea-width)
337 (substring (format "%s" doc) 0 ea-width))
338 ((>= strip (length name))
339 (format "%s" doc))
340 (t
341 ;; Show the end of the partial symbol name, rather
342 ;; than the beginning, since the former is more likely
343 ;; to be unique given package namespace conventions.
344 (setq name (substring name strip))
345 (format "%s: %s" name doc))))))
346
347 \f
348 (defun eldoc-fnsym-in-current-sexp ()
349 (let ((p (point)))
350 (eldoc-beginning-of-sexp)
351 (prog1
352 ;; Don't do anything if current word is inside a string.
353 (if (= (or (char-after (1- (point))) 0) ?\")
354 nil
355 (eldoc-current-symbol))
356 (goto-char p))))
357
358 (defun eldoc-beginning-of-sexp ()
359 (let ((parse-sexp-ignore-comments t))
360 (condition-case err
361 (while (progn
362 (forward-sexp -1)
363 (or (= (char-before) ?\")
364 (> (point) (point-min)))))
365 (error nil))))
366
367 ;; returns nil unless current word is an interned symbol.
368 (defun eldoc-current-symbol ()
369 (let ((c (char-after (point))))
370 (and c
371 (memq (char-syntax c) '(?w ?_))
372 (intern-soft (current-word)))))
373
374 ;; Do indirect function resolution if possible.
375 (defun eldoc-symbol-function (fsym)
376 (let ((defn (and (fboundp fsym)
377 (symbol-function fsym))))
378 (and (symbolp defn)
379 (condition-case err
380 (setq defn (indirect-function fsym))
381 (error (setq defn nil))))
382 defn))
383
384 (defun eldoc-function-argstring (fn)
385 (eldoc-function-argstring-format (help-function-arglist fn)))
386
387 (defun eldoc-function-argstring-format (arglist)
388 (cond ((not (listp arglist))
389 (setq arglist nil))
390 ((symbolp (car arglist))
391 (setq arglist
392 (mapcar (function (lambda (s)
393 (if (memq s '(&optional &rest))
394 (symbol-name s)
395 (funcall eldoc-argument-case
396 (symbol-name s)))))
397 arglist)))
398 ((stringp (car arglist))
399 (setq arglist
400 (mapcar (function (lambda (s)
401 (if (member s '("&optional" "&rest"))
402 s
403 (funcall eldoc-argument-case s))))
404 arglist))))
405 (concat "(" (mapconcat 'identity arglist " ") ")"))
406
407 \f
408 ;; When point is in a sexp, the function args are not reprinted in the echo
409 ;; area after every possible interactive command because some of them print
410 ;; their own messages in the echo area; the eldoc functions would instantly
411 ;; overwrite them unless it is more restrained.
412 ;; These functions do display-command table management.
413
414 (defun eldoc-add-command (&rest cmds)
415 (or eldoc-message-commands
416 (setq eldoc-message-commands
417 (make-vector eldoc-message-commands-table-size 0)))
418
419 (let (name sym)
420 (while cmds
421 (setq name (car cmds))
422 (setq cmds (cdr cmds))
423
424 (cond ((symbolp name)
425 (setq sym name)
426 (setq name (symbol-name sym)))
427 ((stringp name)
428 (setq sym (intern-soft name))))
429
430 (and (symbolp sym)
431 (fboundp sym)
432 (set (intern name eldoc-message-commands) t)))))
433
434 (defun eldoc-add-command-completions (&rest names)
435 (while names
436 (apply 'eldoc-add-command
437 (all-completions (car names) obarray 'fboundp))
438 (setq names (cdr names))))
439
440 (defun eldoc-remove-command (&rest cmds)
441 (let (name)
442 (while cmds
443 (setq name (car cmds))
444 (setq cmds (cdr cmds))
445
446 (and (symbolp name)
447 (setq name (symbol-name name)))
448
449 (unintern name eldoc-message-commands))))
450
451 (defun eldoc-remove-command-completions (&rest names)
452 (while names
453 (apply 'eldoc-remove-command
454 (all-completions (car names) eldoc-message-commands))
455 (setq names (cdr names))))
456
457 \f
458 ;; Prime the command list.
459 (eldoc-add-command-completions
460 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
461 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
462 "next-" "other-window" "previous-" "recenter" "scroll-"
463 "self-insert-command" "split-window-"
464 "up-list" "down-list")
465
466 (provide 'eldoc)
467
468 ;;; arch-tag: c9a58f9d-2055-46c1-9b82-7248b71a8375
469 ;;; eldoc.el ends here