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