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