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