(maphash, map-char-table): Use
[bpt/emacs.git] / lisp / emacs-lisp / eldoc.el
CommitLineData
1b09702a
NF
1;;; eldoc.el --- show function arglist or variable docstring in echo area
2
03a9c6d0 3;; Copyright (C) 1996, 97, 98, 99, 2000 Free Software Foundation, Inc.
1b09702a 4
44faf981
NF
5;; Author: Noah Friedman <friedman@splode.com>
6;; Maintainer: friedman@splode.com
1b09702a 7;; Keywords: extensions
1b09702a
NF
8;; Created: 1995-10-06
9
e3b2eba1 10;; $Id: eldoc.el,v 1.19 2001/11/16 23:58:48 monnier Exp $
1b09702a 11
332ae8db 12;; This file is part of GNU Emacs.
1b09702a 13
332ae8db 14;; GNU Emacs is free software; you can redistribute it and/or modify
1b09702a
NF
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
332ae8db
NF
18
19;; GNU Emacs is distributed in the hope that it will be useful,
1b09702a
NF
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
332ae8db 23
1b09702a 24;; You should have received a copy of the GNU General Public License
332ae8db
NF
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
1b09702a
NF
28
29;;; Commentary:
30
332ae8db
NF
31;; This program was inspired by the behavior of the "mouse documentation
32;; window" on many Lisp Machine systems; as you type a function's symbol
33;; name as part of a sexp, it will print the argument list for that
34;; function. Behavior is not identical; for example, you need not actually
35;; type the function name, you need only move point around in a sexp that
36;; calls it. Also, if point is over a documented variable, it will print
37;; the one-line documentation for that variable instead, to remind you of
38;; that variable's meaning.
1b09702a
NF
39
40;; One useful way to enable this minor mode is to put the following in your
41;; .emacs:
42;;
43;; (autoload 'turn-on-eldoc-mode "eldoc" nil t)
44;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
45;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
4fa07364 46;; (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)
1b09702a
NF
47
48;;; Code:
49
332ae8db 50;; Use idle timers if available in the version of emacs running.
03a9c6d0
NF
51;; Please don't change this to use `require'; this package works
52;; as-is in XEmacs 19.14 and later and I am striving to maintain
53;; compatibility between emacs variants.
332ae8db
NF
54(or (featurep 'timer)
55 (load "timer" t))
56
a326c090
RS
57(defgroup eldoc nil
58 "Show function arglist or variable docstring in echo area."
03a9c6d0 59 :group 'lisp
a326c090
RS
60 :group 'extensions)
61
a326c090 62(defcustom eldoc-idle-delay 0.50
1b09702a
NF
63 "*Number of seconds of idle time to wait before printing.
64If user input arrives before this interval of time has elapsed after the
65last input, no documentation will be printed.
66
a326c090
RS
67If this variable is set to 0, no idle time is required."
68 :type 'number
69 :group 'eldoc)
1b09702a 70
03a9c6d0 71;;;###autoload
a326c090 72(defcustom eldoc-minor-mode-string " ElDoc"
e3b2eba1
RS
73 "*String to display in mode line when Eldoc Mode is enabled; nil for none."
74 :type '(choice string (const :tag "None" nil))
a326c090 75 :group 'eldoc)
332ae8db 76
a326c090 77(defcustom eldoc-argument-case 'upcase
1b09702a
NF
78 "Case to display argument names of functions, as a symbol.
79This has two preferred values: `upcase' or `downcase'.
80Actually, any name of a function which takes a string as an argument and
a326c090 81returns another string is acceptable."
03a9c6d0
NF
82 :type '(radio (function-item upcase)
83 (function-item downcase)
84 function)
a326c090 85 :group 'eldoc)
1b09702a 86
03a9c6d0
NF
87(defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
88 "*Allow long eldoc messages to resize echo area display.
89If value is `t', never attempt to truncate messages; complete symbol name
90and function arglist or 1-line variable documentation will be displayed
91even if echo area must be resized to fit.
92
93If value is any non-nil value other than `t', symbol name may be truncated
94if it will enable the function arglist or documentation string to fit on a
95single line without resizing window. Otherwise, behavior is just like
96former case.
97
98If value is nil, messages are always truncated to fit in a single line of
99display in the echo area. Function or variable symbol name may be
100truncated to make more of the arglist or documentation string visible.
101
102Non-nil values for this variable have no effect unless
103`eldoc-echo-area-multiline-supported-p' is non-nil."
104 :type '(radio (const :tag "Always" t)
105 (const :tag "Never" nil)
106 (const :tag "Yes, but truncate symbol names if it will\
107 enable argument list to fit on one line" truncate-sym-name-if-fit))
108 :group 'eldoc)
109
110;;; No user options below here.
111
112;; Non-nil if this version of emacs supports dynamically resizable echo areas.
113(defvar eldoc-echo-area-multiline-supported-p
114 (and (string-lessp "21" emacs-version)
115 (save-match-data
116 (numberp (string-match "^GNU Emacs" (emacs-version))))))
9d497c01 117
c1286376
NF
118;; Commands after which it is appropriate to print in the echo area.
119;; Eldoc does not try to print function arglists, etc. after just any command,
120;; because some commands print their own messages in the echo area and these
121;; functions would instantly overwrite them. But self-insert-command as well
122;; as most motion commands are good candidates.
123;; This variable contains an obarray of symbols; do not manipulate it
124;; directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.
125(defvar eldoc-message-commands nil)
1b09702a 126
9d497c01
NF
127;; This is used by eldoc-add-command to initialize eldoc-message-commands
128;; as an obarray.
c1286376
NF
129;; It should probably never be necessary to do so, but if you
130;; choose to increase the number of buckets, you must do so before loading
131;; this file since the obarray is initialized at load time.
132;; Remember to keep it a prime number to improve hash performance.
133(defvar eldoc-message-commands-table-size 31)
1b09702a 134
44faf981
NF
135;; Bookkeeping; elements are as follows:
136;; 0 - contains the last symbol read from the buffer.
137;; 1 - contains the string last displayed in the echo area for that
138;; symbol, so it can be printed again if necessary without reconsing.
139;; 2 - 'function if function args, 'variable if variable documentation.
140(defvar eldoc-last-data (make-vector 3 nil))
9d497c01 141(defvar eldoc-last-message nil)
1b09702a 142
332ae8db 143;; Idle timers are supported in Emacs 19.31 and later.
c1286376 144(defvar eldoc-use-idle-timer-p (fboundp 'run-with-idle-timer))
bd3e1759 145
332ae8db
NF
146;; eldoc's timer object, if using idle timers
147(defvar eldoc-timer nil)
148
149;; idle time delay currently in use by timer.
150;; This is used to determine if eldoc-idle-delay is changed by the user.
151(defvar eldoc-current-idle-delay eldoc-idle-delay)
1b09702a
NF
152
153\f
154;;;###autoload
95193cc5
SM
155(define-minor-mode eldoc-mode
156 "Toggle ElDoc mode on or off.
157Show the defined parameters for the elisp function near point.
158
159For the emacs lisp function at the beginning of the sexp which point is
160within, show the defined parameters for the function in the echo area.
161This information is extracted directly from the function or macro if it is
162in pure lisp. If the emacs function is a subr, the parameters are obtained
163from the documentation string if possible.
164
165If point is over a documented variable, print that variable's docstring
166instead.
167
168With prefix ARG, turn ElDoc mode on if and only if ARG is positive."
169 nil eldoc-minor-mode-string nil
9d497c01 170 (setq eldoc-last-message nil)
332ae8db 171 (cond (eldoc-use-idle-timer-p
9d497c01
NF
172 (add-hook 'post-command-hook 'eldoc-schedule-timer)
173 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area))
332ae8db
NF
174 (t
175 ;; Use post-command-idle-hook if defined, otherwise use
176 ;; post-command-hook. The former is only proper to use in Emacs
177 ;; 19.30; that is the first version in which it appeared, but it
178 ;; was obsolesced by idle timers in Emacs 19.31.
179 (add-hook (if (boundp 'post-command-idle-hook)
03a9c6d0
NF
180 'post-command-idle-hook
181 'post-command-hook)
182 'eldoc-print-current-symbol-info t t)
9d497c01
NF
183 ;; quick and dirty hack for seeing if this is XEmacs
184 (and (fboundp 'display-message)
185 (add-hook 'pre-command-hook
95193cc5 186 'eldoc-pre-command-refresh-echo-area t t)))))
1b09702a
NF
187
188;;;###autoload
189(defun turn-on-eldoc-mode ()
190 "Unequivocally turn on eldoc-mode (see variable documentation)."
191 (interactive)
192 (eldoc-mode 1))
193
44faf981 194\f
332ae8db
NF
195;; Idle timers are part of Emacs 19.31 and later.
196(defun eldoc-schedule-timer ()
197 (or (and eldoc-timer
198 (memq eldoc-timer timer-idle-list))
199 (setq eldoc-timer
200 (run-with-idle-timer eldoc-idle-delay t
201 'eldoc-print-current-symbol-info)))
202
203 ;; If user has changed the idle delay, update the timer.
204 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
205 (setq eldoc-current-idle-delay eldoc-idle-delay)
206 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
207
9d497c01
NF
208(defun eldoc-message (&rest args)
209 (let ((omessage eldoc-last-message))
210 (cond ((eq (car args) eldoc-last-message))
211 ((or (null args)
212 (null (car args)))
213 (setq eldoc-last-message nil))
44faf981
NF
214 ;; If only one arg, no formatting to do so put it in
215 ;; eldoc-last-message so eq test above might succeed on
216 ;; subsequent calls.
217 ((null (cdr args))
218 (setq eldoc-last-message (car args)))
9d497c01
NF
219 (t
220 (setq eldoc-last-message (apply 'format args))))
221 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
222 ;; are recorded in a log. Do not put eldoc messages in that log since
223 ;; they are Legion.
44faf981
NF
224 (cond ((fboundp 'display-message)
225 ;; XEmacs 19.13 way of preventing log messages.
226 (cond (eldoc-last-message
227 (display-message 'no-log eldoc-last-message))
228 (omessage
229 (clear-message 'no-log))))
230 (t
231 ;; Emacs way of preventing log messages.
232 (let ((message-log-max nil))
233 (cond (eldoc-last-message
234 (message "%s" eldoc-last-message))
235 (omessage
236 (message nil)))))))
9d497c01
NF
237 eldoc-last-message)
238
44faf981
NF
239;; This function goes on pre-command-hook for XEmacs or when using idle
240;; timers in Emacs. Motion commands clear the echo area for some reason,
241;; which make eldoc messages flicker or disappear just before motion
242;; begins. This function reprints the last eldoc message immediately
243;; before the next command executes, which does away with the flicker.
244;; This doesn't seem to be required for Emacs 19.28 and earlier.
245(defun eldoc-pre-command-refresh-echo-area ()
246 (and eldoc-last-message
247 (if (eldoc-display-message-no-interference-p)
248 (eldoc-message eldoc-last-message)
249 (setq eldoc-last-message nil))))
4fa07364
NF
250
251;; Decide whether now is a good time to display a message.
252(defun eldoc-display-message-p ()
9d497c01 253 (and (eldoc-display-message-no-interference-p)
332ae8db 254 (cond (eldoc-use-idle-timer-p
4fa07364
NF
255 ;; If this-command is non-nil while running via an idle
256 ;; timer, we're still in the middle of executing a command,
257 ;; e.g. a query-replace where it would be annoying to
258 ;; overwrite the echo area.
259 (and (not this-command)
260 (symbolp last-command)
332ae8db
NF
261 (intern-soft (symbol-name last-command)
262 eldoc-message-commands)))
263 (t
264 ;; If we don't have idle timers, this function is
265 ;; running on post-command-hook directly; that means the
266 ;; user's last command is still on `this-command', and we
267 ;; must wait briefly for input to see whether to do display.
268 (and (symbolp this-command)
269 (intern-soft (symbol-name this-command)
270 eldoc-message-commands)
4fa07364 271 (sit-for eldoc-idle-delay))))))
1b09702a 272
03a9c6d0
NF
273;; Check various conditions about the current environment that might make
274;; it undesirable to print eldoc messages right this instant.
9d497c01
NF
275(defun eldoc-display-message-no-interference-p ()
276 (and eldoc-mode
277 (not executing-kbd-macro)
03a9c6d0 278 (not (and (boundp 'edebug-active) edebug-active))
9d497c01
NF
279 ;; Having this mode operate in an active minibuffer/echo area causes
280 ;; interference with what's going on there.
281 (not cursor-in-echo-area)
282 (not (eq (selected-window) (minibuffer-window)))))
283
44faf981
NF
284\f
285(defun eldoc-print-current-symbol-info ()
286 (and (eldoc-display-message-p)
287 (let* ((current-symbol (eldoc-current-symbol))
288 (current-fnsym (eldoc-fnsym-in-current-sexp))
289 (doc (cond ((eq current-symbol current-fnsym)
290 (or (eldoc-get-fnsym-args-string current-fnsym)
291 (eldoc-get-var-docstring current-symbol)))
292 (t
293 (or (eldoc-get-var-docstring current-symbol)
294 (eldoc-get-fnsym-args-string current-fnsym))))))
295 (eldoc-message doc))))
296
297;; Return a string containing the function parameter list, or 1-line
298;; docstring if function is a subr and no arglist is obtainable from the
299;; docstring or elsewhere.
300(defun eldoc-get-fnsym-args-string (sym)
301 (let ((args nil)
302 (doc nil))
9d497c01
NF
303 (cond ((not (and sym
304 (symbolp sym)
1b09702a 305 (fboundp sym))))
44faf981
NF
306 ((and (eq sym (aref eldoc-last-data 0))
307 (eq 'function (aref eldoc-last-data 2)))
308 (setq doc (aref eldoc-last-data 1)))
1b09702a 309 ((subrp (eldoc-symbol-function sym))
332ae8db 310 (setq args (or (eldoc-function-argstring-from-docstring sym)
44faf981 311 (eldoc-docstring-first-line (documentation sym t)))))
1b09702a 312 (t
44faf981
NF
313 (setq args (eldoc-function-argstring sym))))
314 (cond (args
315 (setq doc (eldoc-docstring-format-sym-doc sym args))
316 (eldoc-last-data-store sym doc 'function)))
317 doc))
318
319;; Return a string containing a brief (one-line) documentation string for
320;; the variable.
321(defun eldoc-get-var-docstring (sym)
322 (cond ((and (eq sym (aref eldoc-last-data 0))
323 (eq 'variable (aref eldoc-last-data 2)))
324 (aref eldoc-last-data 1))
325 (t
326 (let ((doc (documentation-property sym 'variable-documentation t)))
327 (cond (doc
328 (setq doc (eldoc-docstring-format-sym-doc
329 sym (eldoc-docstring-first-line doc)))
330 (eldoc-last-data-store sym doc 'variable)))
331 doc))))
332
333(defun eldoc-last-data-store (symbol doc type)
334 (aset eldoc-last-data 0 symbol)
335 (aset eldoc-last-data 1 doc)
336 (aset eldoc-last-data 2 type))
337
338;; Note that any leading `*' in the docstring (which indicates the variable
339;; is a user option) is removed.
340(defun eldoc-docstring-first-line (doc)
341 (and (stringp doc)
342 (substitute-command-keys
343 (save-match-data
344 (let ((start (if (string-match "^\\*" doc) (match-end 0) 0)))
345 (cond ((string-match "\n" doc)
346 (substring doc start (match-beginning 0)))
347 ((zerop start) doc)
348 (t (substring doc start))))))))
349
350;; If the entire line cannot fit in the echo area, the symbol name may be
351;; truncated or eliminated entirely from the output to make room for the
352;; description.
353(defun eldoc-docstring-format-sym-doc (sym doc)
354 (save-match-data
355 (let* ((name (symbol-name sym))
03a9c6d0
NF
356 (ea-multi (and eldoc-echo-area-multiline-supported-p
357 eldoc-echo-area-use-multiline-p))
358 ;; Subtract 1 from window width since emacs will not write
359 ;; any chars to the last column, or in later versions, will
360 ;; cause a wraparound and resize of the echo area.
361 (ea-width (1- (window-width (minibuffer-window))))
362 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
363 (cond ((or (<= strip 0)
364 (eq ea-multi t)
365 (and ea-multi (> (length doc) ea-width)))
366 (format "%s: %s" sym doc))
367 ((> (length doc) ea-width)
368 (substring (format "%s" doc) 0 ea-width))
369 ((>= strip (length name))
370 (format "%s" doc))
44faf981 371 (t
03a9c6d0
NF
372 ;; Show the end of the partial symbol name, rather
373 ;; than the beginning, since the former is more likely
374 ;; to be unique given package namespace conventions.
375 (setq name (substring name strip))
376 (format "%s: %s" name doc))))))
1b09702a 377
44faf981 378\f
1b09702a 379(defun eldoc-fnsym-in-current-sexp ()
9d497c01
NF
380 (let ((p (point)))
381 (eldoc-beginning-of-sexp)
382 (prog1
383 ;; Don't do anything if current word is inside a string.
384 (if (= (or (char-after (1- (point))) 0) ?\")
385 nil
386 (eldoc-current-symbol))
387 (goto-char p))))
388
389(defun eldoc-beginning-of-sexp ()
390 (let ((parse-sexp-ignore-comments t))
391 (condition-case err
392 (while (progn
393 (forward-sexp -1)
394 (or (= (or (char-after (1- (point)))) ?\")
395 (> (point) (point-min)))))
396 (error nil))))
397
398;; returns nil unless current word is an interned symbol.
399(defun eldoc-current-symbol ()
400 (let ((c (char-after (point))))
401 (and c
402 (memq (char-syntax c) '(?w ?_))
403 (intern-soft (current-word)))))
404
405;; Do indirect function resolution if possible.
406(defun eldoc-symbol-function (fsym)
407 (let ((defn (and (fboundp fsym)
408 (symbol-function fsym))))
409 (and (symbolp defn)
410 (condition-case err
411 (setq defn (indirect-function fsym))
412 (error (setq defn nil))))
413 defn))
1b09702a 414
03a9c6d0 415(defun eldoc-function-arglist (fn)
dd159a74
NF
416 (let* ((prelim-def (eldoc-symbol-function fn))
417 (def (if (eq (car-safe prelim-def) 'macro)
418 (cdr prelim-def)
419 prelim-def))
1b09702a 420 (arglist (cond ((null def) nil)
dd159a74 421 ((byte-code-function-p def)
03a9c6d0
NF
422 (cond ((fboundp 'compiled-function-arglist)
423 (funcall 'compiled-function-arglist def))
424 (t
425 (aref def 0))))
1b09702a
NF
426 ((eq (car-safe def) 'lambda)
427 (nth 1 def))
428 (t t))))
03a9c6d0
NF
429 arglist))
430
431(defun eldoc-function-argstring (fn)
432 (eldoc-function-argstring-format (eldoc-function-arglist fn)))
1b09702a 433
332ae8db
NF
434(defun eldoc-function-argstring-format (arglist)
435 (cond ((not (listp arglist))
436 (setq arglist nil))
437 ((symbolp (car arglist))
438 (setq arglist
439 (mapcar (function (lambda (s)
440 (if (memq s '(&optional &rest))
441 (symbol-name s)
442 (funcall eldoc-argument-case
443 (symbol-name s)))))
444 arglist)))
445 ((stringp (car arglist))
446 (setq arglist
447 (mapcar (function (lambda (s)
448 (if (member s '("&optional" "&rest"))
449 s
450 (funcall eldoc-argument-case s))))
451 arglist))))
452 (concat "(" (mapconcat 'identity arglist " ") ")"))
453
454\f
332ae8db
NF
455;; Alist of predicate/action pairs.
456;; Each member of the list is a sublist consisting of a predicate function
457;; used to determine if the arglist for a function can be found using a
458;; certain pattern, and a function which returns the actual arglist from
459;; that docstring.
460;;
461;; The order in this table is significant, since later predicates may be
462;; more general than earlier ones.
463;;
c1286376
NF
464;; Compiler note for Emacs/XEmacs versions which support dynamic loading:
465;; these functions will be compiled to bytecode, but can't be lazy-loaded
466;; even if you set byte-compile-dynamic; to do that would require making
467;; them named top-level defuns, which is not particularly desirable either.
468(defvar eldoc-function-argstring-from-docstring-method-table
332ae8db
NF
469 (list
470 ;; Try first searching for args starting with symbol name.
471 ;; This is to avoid matching parenthetical remarks in e.g. sit-for.
472 (list (function (lambda (doc fn)
473 (string-match (format "^(%s[^\n)]*)$" fn) doc)))
474 (function (lambda (doc)
475 ;; end does not include trailing ")" sequence.
476 (let ((end (- (match-end 0) 1)))
477 (if (string-match " +" doc (match-beginning 0))
478 (substring doc (match-end 0) end)
479 "")))))
480
481 ;; Try again not requiring this symbol name in the docstring.
482 ;; This will be the case when looking up aliases.
483 (list (function (lambda (doc fn)
c1286376
NF
484 ;; save-restriction has a pathological docstring in
485 ;; Emacs/XEmacs 19.
486 (and (not (eq fn 'save-restriction))
487 (string-match "^([^\n)]+)$" doc))))
332ae8db
NF
488 (function (lambda (doc)
489 ;; end does not include trailing ")" sequence.
490 (let ((end (- (match-end 0) 1)))
491 (and (string-match " +" doc (match-beginning 0))
492 (substring doc (match-end 0) end))))))
493
494 ;; Emacs subr docstring style:
495 ;; (fn arg1 arg2 ...): description...
496 (list (function (lambda (doc fn)
497 (string-match "^([^\n)]+):" doc)))
498 (function (lambda (doc)
499 ;; end does not include trailing "):" sequence.
500 (let ((end (- (match-end 0) 2)))
501 (and (string-match " +" doc (match-beginning 0))
502 (substring doc (match-end 0) end))))))
503
504 ;; XEmacs subr docstring style:
505 ;; "arguments: (arg1 arg2 ...)
506 (list (function (lambda (doc fn)
507 (string-match "^arguments: (\\([^\n)]+\\))" doc)))
508 (function (lambda (doc)
509 ;; also skip leading paren, but the first word is
510 ;; actually an argument, not the function name.
511 (substring doc (match-beginning 1) (match-end 1)))))
512
513 ;; This finds the argstring for `condition-case'. Any others?
514 (list (function (lambda (doc fn)
515 (string-match
516 (format "^Usage looks like \\((%s[^\n)]*)\\)\\.$" fn)
517 doc)))
518 (function (lambda (doc)
519 ;; end does not include trailing ")" sequence.
520 (let ((end (- (match-end 1) 1)))
521 (and (string-match " +" doc (match-beginning 1))
522 (substring doc (match-end 0) end))))))
523
524 ;; This finds the argstring for `setq-default'. Any others?
525 (list (function (lambda (doc fn)
526 (string-match (format "^[ \t]+\\((%s[^\n)]*)\\)$" fn)
527 doc)))
528 (function (lambda (doc)
529 ;; end does not include trailing ")" sequence.
530 (let ((end (- (match-end 1) 1)))
531 (and (string-match " +" doc (match-beginning 1))
532 (substring doc (match-end 0) end))))))
533
534 ;; This finds the argstring for `start-process'. Any others?
535 (list (function (lambda (doc fn)
536 (string-match "^Args are +\\([^\n]+\\)$" doc)))
537 (function (lambda (doc)
538 (substring doc (match-beginning 1) (match-end 1)))))
9d497c01 539
c1286376 540 ;; These common subrs don't have arglists in their docstrings. So cheat.
9d497c01
NF
541 (list (function (lambda (doc fn)
542 (memq fn '(and or list + -))))
543 (function (lambda (doc)
544 ;; The value nil is a placeholder; otherwise, the
545 ;; following string may be compiled as a docstring,
546 ;; and not a return value for the function.
547 ;; In interpreted lisp form they are
548 ;; indistinguishable; it only matters for compiled
549 ;; forms.
550 nil
551 "&rest args")))
332ae8db
NF
552 ))
553
1b09702a
NF
554(defun eldoc-function-argstring-from-docstring (fn)
555 (let ((docstring (documentation fn 'raw))
332ae8db 556 (table eldoc-function-argstring-from-docstring-method-table)
1b09702a 557 (doc nil)
332ae8db 558 (doclist nil))
1b09702a 559 (save-match-data
332ae8db
NF
560 (while table
561 (cond ((funcall (car (car table)) docstring fn)
562 (setq doc (funcall (car (cdr (car table))) docstring))
563 (setq table nil))
564 (t
565 (setq table (cdr table)))))
1b09702a
NF
566
567 (cond ((not (stringp doc))
568 nil)
569 ((string-match "&" doc)
570 (let ((p 0)
571 (l (length doc)))
572 (while (< p l)
573 (cond ((string-match "[ \t\n]+" doc p)
574 (setq doclist
575 (cons (substring doc p (match-beginning 0))
576 doclist))
577 (setq p (match-end 0)))
578 (t
579 (setq doclist (cons (substring doc p) doclist))
580 (setq p l))))
581 (eldoc-function-argstring-format (nreverse doclist))))
582 (t
583 (concat "(" (funcall eldoc-argument-case doc) ")"))))))
584
1b09702a 585\f
9d497c01
NF
586;; When point is in a sexp, the function args are not reprinted in the echo
587;; area after every possible interactive command because some of them print
588;; their own messages in the echo area; the eldoc functions would instantly
589;; overwrite them unless it is more restrained.
590;; These functions do display-command table management.
591
592(defun eldoc-add-command (&rest cmds)
593 (or eldoc-message-commands
594 (setq eldoc-message-commands
595 (make-vector eldoc-message-commands-table-size 0)))
596
597 (let (name sym)
598 (while cmds
599 (setq name (car cmds))
600 (setq cmds (cdr cmds))
601
602 (cond ((symbolp name)
603 (setq sym name)
604 (setq name (symbol-name sym)))
605 ((stringp name)
606 (setq sym (intern-soft name))))
607
608 (and (symbolp sym)
609 (fboundp sym)
610 (set (intern name eldoc-message-commands) t)))))
611
612(defun eldoc-add-command-completions (&rest names)
613 (while names
614 (apply 'eldoc-add-command
615 (all-completions (car names) obarray 'fboundp))
616 (setq names (cdr names))))
617
618(defun eldoc-remove-command (&rest cmds)
619 (let (name)
620 (while cmds
621 (setq name (car cmds))
622 (setq cmds (cdr cmds))
623
624 (and (symbolp name)
625 (setq name (symbol-name name)))
626
627 (if (fboundp 'unintern)
628 (unintern name eldoc-message-commands)
629 (let ((s (intern-soft name eldoc-message-commands)))
630 (and s
631 (makunbound s)))))))
632
633(defun eldoc-remove-command-completions (&rest names)
634 (while names
635 (apply 'eldoc-remove-command
636 (all-completions (car names) eldoc-message-commands))
637 (setq names (cdr names))))
638
44faf981 639\f
9d497c01
NF
640;; Prime the command list.
641(eldoc-add-command-completions
642 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
c1286376
NF
643 "end-of-" "forward-" "indent-for-tab-command" "goto-" "mouse-set-point"
644 "next-" "other-window" "previous-" "recenter" "scroll-"
bc74e94e
NF
645 "self-insert-command" "split-window-"
646 "up-list" "down-list")
1b09702a
NF
647
648(provide 'eldoc)
649
650;;; eldoc.el ends here