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