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