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