Commit | Line | Data |
---|---|---|
1b09702a NF |
1 | ;;; eldoc.el --- show function arglist or variable docstring in echo area |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1996-2012 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 | ||
332ae8db | 10 | ;; This file is part of GNU Emacs. |
1b09702a | 11 | |
d6cba7ae | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
1b09702a | 13 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
332ae8db NF |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
1b09702a NF |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
332ae8db | 21 | |
1b09702a | 22 | ;; You should have received a copy of the GNU General Public License |
d6cba7ae | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1b09702a NF |
24 | |
25 | ;;; Commentary: | |
26 | ||
332ae8db NF |
27 | ;; This program was inspired by the behavior of the "mouse documentation |
28 | ;; window" on many Lisp Machine systems; as you type a function's symbol | |
29 | ;; name as part of a sexp, it will print the argument list for that | |
30 | ;; function. Behavior is not identical; for example, you need not actually | |
31 | ;; type the function name, you need only move point around in a sexp that | |
32 | ;; calls it. Also, if point is over a documented variable, it will print | |
33 | ;; the one-line documentation for that variable instead, to remind you of | |
34 | ;; that variable's meaning. | |
1b09702a NF |
35 | |
36 | ;; One useful way to enable this minor mode is to put the following in your | |
37 | ;; .emacs: | |
38 | ;; | |
1b09702a NF |
39 | ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode) |
40 | ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode) | |
4fa07364 | 41 | ;; (add-hook 'ielm-mode-hook 'turn-on-eldoc-mode) |
1b09702a | 42 | |
fdb0e509 | 43 | ;; Major modes for other languages may use ElDoc by defining an |
a3d819fc | 44 | ;; appropriate function as the buffer-local value of |
49c65071 | 45 | ;; `eldoc-documentation-function'. |
a3d819fc | 46 | |
1b09702a NF |
47 | ;;; Code: |
48 | ||
a2dff4d3 SM |
49 | (require 'help-fns) ;For fundoc-usage handling functions. |
50 | ||
a326c090 RS |
51 | (defgroup eldoc nil |
52 | "Show function arglist or variable docstring in echo area." | |
03a9c6d0 | 53 | :group 'lisp |
a326c090 RS |
54 | :group 'extensions) |
55 | ||
a326c090 | 56 | (defcustom eldoc-idle-delay 0.50 |
133ea5b2 | 57 | "Number of seconds of idle time to wait before printing. |
1b09702a NF |
58 | If user input arrives before this interval of time has elapsed after the |
59 | last input, no documentation will be printed. | |
60 | ||
a326c090 RS |
61 | If this variable is set to 0, no idle time is required." |
62 | :type 'number | |
63 | :group 'eldoc) | |
1b09702a | 64 | |
03a9c6d0 | 65 | ;;;###autoload |
aaa448c9 | 66 | (defcustom eldoc-minor-mode-string (purecopy " ElDoc") |
fdb0e509 | 67 | "String to display in mode line when ElDoc Mode is enabled; nil for none." |
e3b2eba1 | 68 | :type '(choice string (const :tag "None" nil)) |
a326c090 | 69 | :group 'eldoc) |
332ae8db | 70 | |
a326c090 | 71 | (defcustom eldoc-argument-case 'upcase |
1b09702a NF |
72 | "Case to display argument names of functions, as a symbol. |
73 | This has two preferred values: `upcase' or `downcase'. | |
74 | Actually, any name of a function which takes a string as an argument and | |
133ea5b2 GM |
75 | returns another string is acceptable. |
76 | ||
77 | Note that if `eldoc-documentation-function' is non-nil, this variable | |
78 | has no effect, unless the function handles it explicitly." | |
03a9c6d0 NF |
79 | :type '(radio (function-item upcase) |
80 | (function-item downcase) | |
81 | function) | |
a326c090 | 82 | :group 'eldoc) |
1b09702a | 83 | |
03a9c6d0 | 84 | (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit |
133ea5b2 | 85 | "Allow long ElDoc messages to resize echo area display. |
66d0d12c | 86 | If value is t, never attempt to truncate messages; complete symbol name |
03a9c6d0 NF |
87 | and function arglist or 1-line variable documentation will be displayed |
88 | even if echo area must be resized to fit. | |
89 | ||
66d0d12c | 90 | If value is any non-nil value other than t, symbol name may be truncated |
03a9c6d0 NF |
91 | if it will enable the function arglist or documentation string to fit on a |
92 | single line without resizing window. Otherwise, behavior is just like | |
93 | former case. | |
94 | ||
95 | If value is nil, messages are always truncated to fit in a single line of | |
96 | display in the echo area. Function or variable symbol name may be | |
133ea5b2 GM |
97 | truncated to make more of the arglist or documentation string visible. |
98 | ||
99 | Note that if `eldoc-documentation-function' is non-nil, this variable | |
100 | has no effect, unless the function handles it explicitly." | |
03a9c6d0 NF |
101 | :type '(radio (const :tag "Always" t) |
102 | (const :tag "Never" nil) | |
103 | (const :tag "Yes, but truncate symbol names if it will\ | |
104 | enable argument list to fit on one line" truncate-sym-name-if-fit)) | |
105 | :group 'eldoc) | |
106 | ||
43fcc18a GM |
107 | (defface eldoc-highlight-function-argument |
108 | '((t (:inherit bold))) | |
133ea5b2 GM |
109 | "Face used for the argument at point in a function's argument list. |
110 | Note that if `eldoc-documentation-function' is non-nil, this face | |
111 | has no effect, unless the function handles it explicitly." | |
43fcc18a GM |
112 | :group 'eldoc) |
113 | ||
03a9c6d0 NF |
114 | ;;; No user options below here. |
115 | ||
c0752bdc | 116 | (defvar eldoc-message-commands-table-size 31 |
133ea5b2 | 117 | "Used by `eldoc-add-command' to initialize `eldoc-message-commands' obarray. |
c0752bdc SM |
118 | It should probably never be necessary to do so, but if you |
119 | choose to increase the number of buckets, you must do so before loading | |
120 | this file since the obarray is initialized at load time. | |
121 | Remember to keep it a prime number to improve hash performance.") | |
122 | ||
123 | (defconst eldoc-message-commands | |
124 | (make-vector eldoc-message-commands-table-size 0) | |
125 | "Commands after which it is appropriate to print in the echo area. | |
fdb0e509 | 126 | ElDoc does not try to print function arglists, etc., after just any command, |
c0752bdc | 127 | because some commands print their own messages in the echo area and these |
d9d31f0a | 128 | functions would instantly overwrite them. But `self-insert-command' as well |
c0752bdc SM |
129 | as most motion commands are good candidates. |
130 | This variable contains an obarray of symbols; do not manipulate it | |
131 | directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.") | |
132 | ||
fdb0e509 | 133 | ;; Not a constant. |
c0752bdc SM |
134 | (defconst eldoc-last-data (make-vector 3 nil) |
135 | "Bookkeeping; elements are as follows: | |
136 | 0 - contains the last symbol read from the buffer. | |
1bed504a SM |
137 | 1 - contains the string last displayed in the echo area for variables, |
138 | or argument string for functions. | |
c0752bdc | 139 | 2 - 'function if function args, 'variable if variable documentation.") |
fdb0e509 | 140 | |
9d497c01 | 141 | (defvar eldoc-last-message nil) |
1b09702a | 142 | |
133ea5b2 | 143 | (defvar eldoc-timer nil "ElDoc's timer object.") |
332ae8db | 144 | |
c0752bdc | 145 | (defvar eldoc-current-idle-delay eldoc-idle-delay |
d9d31f0a | 146 | "Idle time delay currently in use by timer. |
c0752bdc | 147 | This is used to determine if `eldoc-idle-delay' is changed by the user.") |
1b09702a NF |
148 | |
149 | \f | |
150 | ;;;###autoload | |
95193cc5 | 151 | (define-minor-mode eldoc-mode |
ac6c8639 CY |
152 | "Toggle echo area display of Lisp objects at point (ElDoc mode). |
153 | With a prefix argument ARG, enable ElDoc mode if ARG is positive, | |
154 | and disable it otherwise. If called from Lisp, enable ElDoc mode | |
155 | if ARG is omitted or nil. | |
156 | ||
157 | ElDoc mode is a buffer-local minor mode. When enabled, the echo | |
158 | area displays information about a function or variable in the | |
159 | text where point is. If point is on a documented variable, it | |
160 | displays the first line of that variable's doc string. Otherwise | |
161 | it displays the argument list of the function called in the | |
162 | expression point is on." | |
90f5a5a0 | 163 | :group 'eldoc :lighter eldoc-minor-mode-string |
9d497c01 | 164 | (setq eldoc-last-message nil) |
d16ba2e7 SM |
165 | (if eldoc-mode |
166 | (progn | |
167 | (add-hook 'post-command-hook 'eldoc-schedule-timer nil t) | |
168 | (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t)) | |
169 | (remove-hook 'post-command-hook 'eldoc-schedule-timer) | |
170 | (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area))) | |
1b09702a NF |
171 | |
172 | ;;;###autoload | |
173 | (defun turn-on-eldoc-mode () | |
e850c5e1 | 174 | "Unequivocally turn on ElDoc mode (see command `eldoc-mode')." |
1b09702a NF |
175 | (interactive) |
176 | (eldoc-mode 1)) | |
177 | ||
44faf981 | 178 | \f |
332ae8db NF |
179 | (defun eldoc-schedule-timer () |
180 | (or (and eldoc-timer | |
181 | (memq eldoc-timer timer-idle-list)) | |
182 | (setq eldoc-timer | |
183 | (run-with-idle-timer eldoc-idle-delay t | |
184 | 'eldoc-print-current-symbol-info))) | |
185 | ||
186 | ;; If user has changed the idle delay, update the timer. | |
187 | (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay)) | |
188 | (setq eldoc-current-idle-delay eldoc-idle-delay) | |
189 | (timer-set-idle-time eldoc-timer eldoc-idle-delay t)))) | |
190 | ||
9d497c01 NF |
191 | (defun eldoc-message (&rest args) |
192 | (let ((omessage eldoc-last-message)) | |
66d0d12c | 193 | (setq eldoc-last-message |
d16ba2e7 SM |
194 | (cond ((eq (car args) eldoc-last-message) eldoc-last-message) |
195 | ((null (car args)) nil) | |
196 | ;; If only one arg, no formatting to do, so put it in | |
197 | ;; eldoc-last-message so eq test above might succeed on | |
198 | ;; subsequent calls. | |
199 | ((null (cdr args)) (car args)) | |
200 | (t (apply 'format args)))) | |
9d497c01 NF |
201 | ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages |
202 | ;; are recorded in a log. Do not put eldoc messages in that log since | |
203 | ;; they are Legion. | |
d16ba2e7 SM |
204 | ;; Emacs way of preventing log messages. |
205 | (let ((message-log-max nil)) | |
206 | (cond (eldoc-last-message (message "%s" eldoc-last-message)) | |
207 | (omessage (message nil))))) | |
9d497c01 NF |
208 | eldoc-last-message) |
209 | ||
44faf981 NF |
210 | ;; This function goes on pre-command-hook for XEmacs or when using idle |
211 | ;; timers in Emacs. Motion commands clear the echo area for some reason, | |
212 | ;; which make eldoc messages flicker or disappear just before motion | |
213 | ;; begins. This function reprints the last eldoc message immediately | |
214 | ;; before the next command executes, which does away with the flicker. | |
215 | ;; This doesn't seem to be required for Emacs 19.28 and earlier. | |
216 | (defun eldoc-pre-command-refresh-echo-area () | |
217 | (and eldoc-last-message | |
218 | (if (eldoc-display-message-no-interference-p) | |
219 | (eldoc-message eldoc-last-message) | |
220 | (setq eldoc-last-message nil)))) | |
4fa07364 NF |
221 | |
222 | ;; Decide whether now is a good time to display a message. | |
223 | (defun eldoc-display-message-p () | |
9d497c01 | 224 | (and (eldoc-display-message-no-interference-p) |
d16ba2e7 SM |
225 | ;; If this-command is non-nil while running via an idle |
226 | ;; timer, we're still in the middle of executing a command, | |
227 | ;; e.g. a query-replace where it would be annoying to | |
228 | ;; overwrite the echo area. | |
229 | (and (not this-command) | |
230 | (symbolp last-command) | |
231 | (intern-soft (symbol-name last-command) | |
232 | eldoc-message-commands)))) | |
1b09702a | 233 | |
03a9c6d0 NF |
234 | ;; Check various conditions about the current environment that might make |
235 | ;; it undesirable to print eldoc messages right this instant. | |
9d497c01 NF |
236 | (defun eldoc-display-message-no-interference-p () |
237 | (and eldoc-mode | |
238 | (not executing-kbd-macro) | |
03a9c6d0 | 239 | (not (and (boundp 'edebug-active) edebug-active)) |
9d497c01 NF |
240 | ;; Having this mode operate in an active minibuffer/echo area causes |
241 | ;; interference with what's going on there. | |
242 | (not cursor-in-echo-area) | |
243 | (not (eq (selected-window) (minibuffer-window))))) | |
244 | ||
44faf981 | 245 | \f |
4021f358 | 246 | ;;;###autoload |
0a3e46de | 247 | (defvar eldoc-documentation-function nil |
a3d819fc DL |
248 | "If non-nil, function to call to return doc string. |
249 | The function of no args should return a one-line string for displaying | |
250 | doc about a function etc. appropriate to the context around point. | |
251 | It should return nil if there's no doc appropriate for the context. | |
252 | Typically doc is returned if point is on a function-like name or in its | |
253 | arg list. | |
254 | ||
133ea5b2 GM |
255 | The result is used as is, so the function must explicitly handle |
256 | the variables `eldoc-argument-case' and `eldoc-echo-area-use-multiline-p', | |
257 | and the face `eldoc-highlight-function-argument', if they are to have any | |
258 | effect. | |
259 | ||
a3d819fc | 260 | This variable is expected to be made buffer-local by modes (other than |
fdb0e509 | 261 | Emacs Lisp mode) that support ElDoc.") |
a3d819fc | 262 | |
44faf981 | 263 | (defun eldoc-print-current-symbol-info () |
a2dff4d3 SM |
264 | (condition-case err |
265 | (and (eldoc-display-message-p) | |
0a3e46de RS |
266 | (if eldoc-documentation-function |
267 | (eldoc-message (funcall eldoc-documentation-function)) | |
a3d819fc DL |
268 | (let* ((current-symbol (eldoc-current-symbol)) |
269 | (current-fnsym (eldoc-fnsym-in-current-sexp)) | |
270 | (doc (cond | |
1bed504a SM |
271 | ((null current-fnsym) |
272 | nil) | |
273 | ((eq current-symbol (car current-fnsym)) | |
274 | (or (apply 'eldoc-get-fnsym-args-string | |
275 | current-fnsym) | |
a3d819fc DL |
276 | (eldoc-get-var-docstring current-symbol))) |
277 | (t | |
278 | (or (eldoc-get-var-docstring current-symbol) | |
1bed504a SM |
279 | (apply 'eldoc-get-fnsym-args-string |
280 | current-fnsym)))))) | |
a3d819fc | 281 | (eldoc-message doc)))) |
a2dff4d3 SM |
282 | ;; This is run from post-command-hook or some idle timer thing, |
283 | ;; so we need to be careful that errors aren't ignored. | |
284 | (error (message "eldoc error: %s" err)))) | |
44faf981 | 285 | |
ae0195c0 | 286 | (defun eldoc-get-fnsym-args-string (sym &optional index) |
e5eeb98c GM |
287 | "Return a string containing the parameter list of the function SYM. |
288 | If SYM is a subr and no arglist is obtainable from the docstring | |
ae0195c0 GM |
289 | or elsewhere, return a 1-line docstring. Calls the functions |
290 | `eldoc-function-argstring-format' and | |
291 | `eldoc-highlight-function-argument' to format the result. The | |
292 | former calls `eldoc-argument-case'; the latter gives the | |
293 | function name `font-lock-function-name-face', and optionally | |
133ea5b2 | 294 | highlights argument number INDEX." |
8d6c1239 | 295 | (let (args doc advertised) |
d16ba2e7 | 296 | (cond ((not (and sym (symbolp sym) (fboundp sym)))) |
e5eeb98c GM |
297 | ((and (eq sym (aref eldoc-last-data 0)) |
298 | (eq 'function (aref eldoc-last-data 2))) | |
299 | (setq doc (aref eldoc-last-data 1))) | |
8d6c1239 SM |
300 | ((listp (setq advertised (gethash (indirect-function sym) |
301 | advertised-signature-table t))) | |
302 | (setq args advertised)) | |
a2dff4d3 SM |
303 | ((setq doc (help-split-fundoc (documentation sym t) sym)) |
304 | (setq args (car doc)) | |
e5eeb98c | 305 | ;; Remove any enclosing (), since e-function-argstring adds them. |
a2dff4d3 | 306 | (string-match "\\`[^ )]* ?" args) |
e5eeb98c | 307 | (setq args (substring args (match-end 0))) |
eec6d5b7 | 308 | (if (string-match-p ")\\'" args) |
e5eeb98c GM |
309 | (setq args (substring args 0 -1)))) |
310 | (t | |
311 | (setq args (help-function-arglist sym)))) | |
312 | (if args | |
313 | ;; Stringify, and store before highlighting, downcasing, etc. | |
314 | ;; FIXME should truncate before storing. | |
315 | (eldoc-last-data-store sym (setq args (eldoc-function-argstring args)) | |
316 | 'function) | |
317 | (setq args doc)) ; use stored value | |
318 | ;; Change case, highlight, truncate. | |
319 | (if args | |
320 | (eldoc-highlight-function-argument | |
ae0195c0 | 321 | sym (eldoc-function-argstring-format args) index)))) |
44faf981 | 322 | |
ae0195c0 GM |
323 | (defun eldoc-highlight-function-argument (sym args index) |
324 | "Highlight argument INDEX in ARGS list for function SYM. | |
325 | In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'." | |
1bed504a SM |
326 | (let ((start nil) |
327 | (end 0) | |
43fcc18a | 328 | (argument-face 'eldoc-highlight-function-argument)) |
1bed504a SM |
329 | ;; Find the current argument in the argument string. We need to |
330 | ;; handle `&rest' and informal `...' properly. | |
331 | ;; | |
332 | ;; FIXME: What to do with optional arguments, like in | |
333 | ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case? | |
334 | ;; The problem is there is no robust way to determine if | |
335 | ;; the current argument is indeed a docstring. | |
e5eeb98c | 336 | (while (and index (>= index 1)) |
1bed504a SM |
337 | (if (string-match "[^ ()]+" args end) |
338 | (progn | |
339 | (setq start (match-beginning 0) | |
340 | end (match-end 0)) | |
341 | (let ((argument (match-string 0 args))) | |
342 | (cond ((string= argument "&rest") | |
343 | ;; All the rest arguments are the same. | |
344 | (setq index 1)) | |
345 | ((string= argument "&optional")) | |
eec6d5b7 | 346 | ((string-match-p "\\.\\.\\.$" argument) |
1bed504a SM |
347 | (setq index 0)) |
348 | (t | |
349 | (setq index (1- index)))))) | |
350 | (setq end (length args) | |
351 | start (1- end) | |
352 | argument-face 'font-lock-warning-face | |
353 | index 0))) | |
354 | (let ((doc args)) | |
355 | (when start | |
356 | (setq doc (copy-sequence args)) | |
357 | (add-text-properties start end (list 'face argument-face) doc)) | |
358 | (setq doc (eldoc-docstring-format-sym-doc | |
359 | sym doc 'font-lock-function-name-face)) | |
360 | doc))) | |
361 | ||
44faf981 NF |
362 | ;; Return a string containing a brief (one-line) documentation string for |
363 | ;; the variable. | |
364 | (defun eldoc-get-var-docstring (sym) | |
2dba57c1 JPW |
365 | (when sym |
366 | (cond ((and (eq sym (aref eldoc-last-data 0)) | |
367 | (eq 'variable (aref eldoc-last-data 2))) | |
368 | (aref eldoc-last-data 1)) | |
369 | (t | |
370 | (let ((doc (documentation-property sym 'variable-documentation t))) | |
371 | (cond (doc | |
372 | (setq doc (eldoc-docstring-format-sym-doc | |
1bed504a SM |
373 | sym (eldoc-docstring-first-line doc) |
374 | 'font-lock-variable-name-face)) | |
2dba57c1 JPW |
375 | (eldoc-last-data-store sym doc 'variable))) |
376 | doc))))) | |
44faf981 NF |
377 | |
378 | (defun eldoc-last-data-store (symbol doc type) | |
379 | (aset eldoc-last-data 0 symbol) | |
380 | (aset eldoc-last-data 1 doc) | |
381 | (aset eldoc-last-data 2 type)) | |
382 | ||
383 | ;; Note that any leading `*' in the docstring (which indicates the variable | |
384 | ;; is a user option) is removed. | |
385 | (defun eldoc-docstring-first-line (doc) | |
386 | (and (stringp doc) | |
387 | (substitute-command-keys | |
388 | (save-match-data | |
4d1243c8 MR |
389 | ;; Don't use "^" in the regexp below since it may match |
390 | ;; anywhere in the doc-string. | |
391 | (let ((start (if (string-match "\\`\\*" doc) (match-end 0) 0))) | |
44faf981 NF |
392 | (cond ((string-match "\n" doc) |
393 | (substring doc start (match-beginning 0))) | |
394 | ((zerop start) doc) | |
395 | (t (substring doc start)))))))) | |
396 | ||
397 | ;; If the entire line cannot fit in the echo area, the symbol name may be | |
398 | ;; truncated or eliminated entirely from the output to make room for the | |
399 | ;; description. | |
1bed504a | 400 | (defun eldoc-docstring-format-sym-doc (sym doc face) |
44faf981 NF |
401 | (save-match-data |
402 | (let* ((name (symbol-name sym)) | |
d16ba2e7 | 403 | (ea-multi eldoc-echo-area-use-multiline-p) |
03a9c6d0 NF |
404 | ;; Subtract 1 from window width since emacs will not write |
405 | ;; any chars to the last column, or in later versions, will | |
406 | ;; cause a wraparound and resize of the echo area. | |
407 | (ea-width (1- (window-width (minibuffer-window)))) | |
408 | (strip (- (+ (length name) (length ": ") (length doc)) ea-width))) | |
409 | (cond ((or (<= strip 0) | |
410 | (eq ea-multi t) | |
411 | (and ea-multi (> (length doc) ea-width))) | |
1bed504a | 412 | (format "%s: %s" (propertize name 'face face) doc)) |
03a9c6d0 NF |
413 | ((> (length doc) ea-width) |
414 | (substring (format "%s" doc) 0 ea-width)) | |
415 | ((>= strip (length name)) | |
416 | (format "%s" doc)) | |
44faf981 | 417 | (t |
03a9c6d0 NF |
418 | ;; Show the end of the partial symbol name, rather |
419 | ;; than the beginning, since the former is more likely | |
420 | ;; to be unique given package namespace conventions. | |
421 | (setq name (substring name strip)) | |
1bed504a | 422 | (format "%s: %s" (propertize name 'face face) doc)))))) |
1b09702a | 423 | |
44faf981 | 424 | \f |
1bed504a | 425 | ;; Return a list of current function name and argument index. |
1b09702a | 426 | (defun eldoc-fnsym-in-current-sexp () |
1bed504a SM |
427 | (save-excursion |
428 | (let ((argument-index (1- (eldoc-beginning-of-sexp)))) | |
429 | ;; If we are at the beginning of function name, this will be -1. | |
430 | (when (< argument-index 0) | |
431 | (setq argument-index 0)) | |
432 | ;; Don't do anything if current word is inside a string. | |
433 | (if (= (or (char-after (1- (point))) 0) ?\") | |
434 | nil | |
435 | (list (eldoc-current-symbol) argument-index))))) | |
436 | ||
91af3942 | 437 | ;; Move to the beginning of current sexp. Return the number of nested |
1bed504a | 438 | ;; sexp the point was over or after. |
9d497c01 | 439 | (defun eldoc-beginning-of-sexp () |
1bed504a SM |
440 | (let ((parse-sexp-ignore-comments t) |
441 | (num-skipped-sexps 0)) | |
9d497c01 | 442 | (condition-case err |
1bed504a SM |
443 | (progn |
444 | ;; First account for the case the point is directly over a | |
445 | ;; beginning of a nested sexp. | |
446 | (condition-case err | |
447 | (let ((p (point))) | |
448 | (forward-sexp -1) | |
449 | (forward-sexp 1) | |
450 | (when (< (point) p) | |
451 | (setq num-skipped-sexps 1))) | |
452 | (error)) | |
453 | (while | |
454 | (let ((p (point))) | |
455 | (forward-sexp -1) | |
456 | (when (< (point) p) | |
457 | (setq num-skipped-sexps (1+ num-skipped-sexps)))))) | |
458 | (error)) | |
459 | num-skipped-sexps)) | |
9d497c01 NF |
460 | |
461 | ;; returns nil unless current word is an interned symbol. | |
462 | (defun eldoc-current-symbol () | |
463 | (let ((c (char-after (point)))) | |
464 | (and c | |
465 | (memq (char-syntax c) '(?w ?_)) | |
466 | (intern-soft (current-word))))) | |
467 | ||
468 | ;; Do indirect function resolution if possible. | |
469 | (defun eldoc-symbol-function (fsym) | |
470 | (let ((defn (and (fboundp fsym) | |
471 | (symbol-function fsym)))) | |
472 | (and (symbolp defn) | |
473 | (condition-case err | |
474 | (setq defn (indirect-function fsym)) | |
475 | (error (setq defn nil)))) | |
476 | defn)) | |
1b09702a | 477 | |
e5eeb98c GM |
478 | (defun eldoc-function-argstring (arglist) |
479 | "Return ARGLIST as a string enclosed by (). | |
480 | ARGLIST is either a string, or a list of strings or symbols." | |
481 | (cond ((stringp arglist)) | |
482 | ((not (listp arglist)) | |
483 | (setq arglist nil)) | |
484 | ((symbolp (car arglist)) | |
485 | (setq arglist | |
486 | (mapconcat (lambda (s) (symbol-name s)) | |
487 | arglist " "))) | |
488 | ((stringp (car arglist)) | |
489 | (setq arglist | |
490 | (mapconcat (lambda (s) s) | |
491 | arglist " ")))) | |
492 | (if arglist | |
493 | (format "(%s)" arglist))) | |
494 | ||
495 | (defun eldoc-function-argstring-format (argstring) | |
d9d31f0a | 496 | "Apply `eldoc-argument-case' to each word in ARGSTRING. |
e5eeb98c GM |
497 | The words \"&rest\", \"&optional\" are returned unchanged." |
498 | (mapconcat (lambda (s) | |
eec6d5b7 | 499 | (if (string-match-p "\\`(?&\\(?:optional\\|rest\\))?\\'" s) |
e5eeb98c GM |
500 | s |
501 | (funcall eldoc-argument-case s))) | |
eec6d5b7 | 502 | (split-string argstring) " ")) |
c4e36c26 | 503 | |
332ae8db | 504 | \f |
9d497c01 NF |
505 | ;; When point is in a sexp, the function args are not reprinted in the echo |
506 | ;; area after every possible interactive command because some of them print | |
507 | ;; their own messages in the echo area; the eldoc functions would instantly | |
508 | ;; overwrite them unless it is more restrained. | |
509 | ;; These functions do display-command table management. | |
510 | ||
511 | (defun eldoc-add-command (&rest cmds) | |
c0752bdc SM |
512 | (dolist (name cmds) |
513 | (and (symbolp name) | |
514 | (setq name (symbol-name name))) | |
515 | (set (intern name eldoc-message-commands) t))) | |
9d497c01 NF |
516 | |
517 | (defun eldoc-add-command-completions (&rest names) | |
c0752bdc SM |
518 | (dolist (name names) |
519 | (apply 'eldoc-add-command (all-completions name obarray 'commandp)))) | |
9d497c01 NF |
520 | |
521 | (defun eldoc-remove-command (&rest cmds) | |
c0752bdc SM |
522 | (dolist (name cmds) |
523 | (and (symbolp name) | |
524 | (setq name (symbol-name name))) | |
525 | (unintern name eldoc-message-commands))) | |
9d497c01 NF |
526 | |
527 | (defun eldoc-remove-command-completions (&rest names) | |
c0752bdc | 528 | (dolist (name names) |
9d497c01 | 529 | (apply 'eldoc-remove-command |
c0752bdc | 530 | (all-completions name eldoc-message-commands)))) |
9d497c01 | 531 | |
44faf981 | 532 | \f |
9d497c01 NF |
533 | ;; Prime the command list. |
534 | (eldoc-add-command-completions | |
24f574a9 JB |
535 | "backward-" "beginning-of-" "delete-other-windows" "delete-window" |
536 | "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-" | |
537 | "handle-select-window" "indent-for-tab-command" "left-" "mark-page" | |
538 | "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-" | |
539 | "move-end-of-" "next-" "other-window" "pop-global-mark" "previous-" | |
540 | "recenter" "right-" "scroll-" "self-insert-command" "split-window-" | |
541 | "up-list") | |
1b09702a NF |
542 | |
543 | (provide 'eldoc) | |
544 | ||
545 | ;;; eldoc.el ends here |