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