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