* admin/grammars/Makefile.in (bootstrap-clean): Don't delete Makefile,
[bpt/emacs.git] / lisp / emacs-lisp / eldoc.el
CommitLineData
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
59If user input arrives before this interval of time has elapsed after the
60last input, no documentation will be printed.
61
a326c090
RS
62If 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.
68Changing 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.
80This has two preferred values: `upcase' or `downcase'.
81Actually, any name of a function which takes a string as an argument and
133ea5b2
GM
82returns another string is acceptable.
83
84Note that if `eldoc-documentation-function' is non-nil, this variable
85has 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 93If value is t, never attempt to truncate messages; complete symbol name
03a9c6d0
NF
94and function arglist or 1-line variable documentation will be displayed
95even if echo area must be resized to fit.
96
66d0d12c 97If value is any non-nil value other than t, symbol name may be truncated
03a9c6d0
NF
98if it will enable the function arglist or documentation string to fit on a
99single line without resizing window. Otherwise, behavior is just like
100former case.
101
102If value is nil, messages are always truncated to fit in a single line of
103display in the echo area. Function or variable symbol name may be
133ea5b2
GM
104truncated to make more of the arglist or documentation string visible.
105
106Note that if `eldoc-documentation-function' is non-nil, this variable
107has 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.
117Note that if `eldoc-documentation-function' is non-nil, this face
118has 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
125It should probably never be necessary to do so, but if you
126choose to increase the number of buckets, you must do so before loading
127this file since the obarray is initialized at load time.
128Remember 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 133ElDoc does not try to print function arglists, etc., after just any command,
c0752bdc 134because some commands print their own messages in the echo area and these
d9d31f0a 135functions would instantly overwrite them. But `self-insert-command' as well
c0752bdc
SM
136as most motion commands are good candidates.
137This variable contains an obarray of symbols; do not manipulate it
138directly. 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 154This 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.
158It 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).
174With a prefix argument ARG, enable ElDoc mode if ARG is positive,
175and disable it otherwise. If called from Lisp, enable ElDoc mode
176if ARG is omitted or nil.
177
178ElDoc mode is a buffer-local minor mode. When enabled, the echo
179area displays information about a function or variable in the
180text where point is. If point is on a documented variable, it
181displays the first line of that variable's doc string. Otherwise
182it displays the argument list of the function called in the
183expression 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.
218Otherwise 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
294b2b09
LL
301(defvar eldoc-documentation-function #'eldoc-documentation-function-default
302 "Function to call to return doc string.
a3d819fc
DL
303The function of no args should return a one-line string for displaying
304doc about a function etc. appropriate to the context around point.
305It should return nil if there's no doc appropriate for the context.
306Typically doc is returned if point is on a function-like name or in its
307arg list.
308
133ea5b2
GM
309The result is used as is, so the function must explicitly handle
310the variables `eldoc-argument-case' and `eldoc-echo-area-use-multiline-p',
311and the face `eldoc-highlight-function-argument', if they are to have any
312effect.
313
a3d819fc 314This variable is expected to be made buffer-local by modes (other than
fdb0e509 315Emacs 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))
294b2b09
LL
326 (eldoc-message (funcall eldoc-documentation-function)))))
327
328(defun eldoc-documentation-function-default ()
329 "Default value for `eldoc-documentation-function' (which see)."
330 (let ((current-symbol (eldoc-current-symbol))
331 (current-fnsym (eldoc-fnsym-in-current-sexp)))
332 (cond ((null current-fnsym)
333 nil)
334 ((eq current-symbol (car current-fnsym))
335 (or (apply #'eldoc-get-fnsym-args-string 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 current-fnsym))))))
44faf981 340
ae0195c0 341(defun eldoc-get-fnsym-args-string (sym &optional index)
e5eeb98c
GM
342 "Return a string containing the parameter list of the function SYM.
343If SYM is a subr and no arglist is obtainable from the docstring
ae0195c0
GM
344or elsewhere, return a 1-line docstring. Calls the functions
345`eldoc-function-argstring-format' and
346`eldoc-highlight-function-argument' to format the result. The
347former calls `eldoc-argument-case'; the latter gives the
348function name `font-lock-function-name-face', and optionally
133ea5b2 349highlights argument number INDEX."
8d6c1239 350 (let (args doc advertised)
d16ba2e7 351 (cond ((not (and sym (symbolp sym) (fboundp sym))))
e5eeb98c
GM
352 ((and (eq sym (aref eldoc-last-data 0))
353 (eq 'function (aref eldoc-last-data 2)))
354 (setq doc (aref eldoc-last-data 1)))
8d6c1239
SM
355 ((listp (setq advertised (gethash (indirect-function sym)
356 advertised-signature-table t)))
357 (setq args advertised))
a2dff4d3
SM
358 ((setq doc (help-split-fundoc (documentation sym t) sym))
359 (setq args (car doc))
e5eeb98c 360 ;; Remove any enclosing (), since e-function-argstring adds them.
a2dff4d3 361 (string-match "\\`[^ )]* ?" args)
e5eeb98c 362 (setq args (substring args (match-end 0)))
eec6d5b7 363 (if (string-match-p ")\\'" args)
e5eeb98c
GM
364 (setq args (substring args 0 -1))))
365 (t
366 (setq args (help-function-arglist sym))))
367 (if args
368 ;; Stringify, and store before highlighting, downcasing, etc.
369 ;; FIXME should truncate before storing.
370 (eldoc-last-data-store sym (setq args (eldoc-function-argstring args))
371 'function)
372 (setq args doc)) ; use stored value
373 ;; Change case, highlight, truncate.
374 (if args
375 (eldoc-highlight-function-argument
ae0195c0 376 sym (eldoc-function-argstring-format args) index))))
44faf981 377
ae0195c0
GM
378(defun eldoc-highlight-function-argument (sym args index)
379 "Highlight argument INDEX in ARGS list for function SYM.
380In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
1bed504a
SM
381 (let ((start nil)
382 (end 0)
43fcc18a 383 (argument-face 'eldoc-highlight-function-argument))
1bed504a
SM
384 ;; Find the current argument in the argument string. We need to
385 ;; handle `&rest' and informal `...' properly.
386 ;;
387 ;; FIXME: What to do with optional arguments, like in
388 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
389 ;; The problem is there is no robust way to determine if
390 ;; the current argument is indeed a docstring.
e5eeb98c 391 (while (and index (>= index 1))
1bed504a
SM
392 (if (string-match "[^ ()]+" args end)
393 (progn
394 (setq start (match-beginning 0)
395 end (match-end 0))
396 (let ((argument (match-string 0 args)))
397 (cond ((string= argument "&rest")
398 ;; All the rest arguments are the same.
399 (setq index 1))
400 ((string= argument "&optional"))
eec6d5b7 401 ((string-match-p "\\.\\.\\.$" argument)
1bed504a
SM
402 (setq index 0))
403 (t
404 (setq index (1- index))))))
405 (setq end (length args)
406 start (1- end)
407 argument-face 'font-lock-warning-face
408 index 0)))
409 (let ((doc args))
410 (when start
411 (setq doc (copy-sequence args))
412 (add-text-properties start end (list 'face argument-face) doc))
413 (setq doc (eldoc-docstring-format-sym-doc
587feed4
MH
414 sym doc (if (functionp sym) 'font-lock-function-name-face
415 'font-lock-keyword-face)))
1bed504a
SM
416 doc)))
417
44faf981
NF
418;; Return a string containing a brief (one-line) documentation string for
419;; the variable.
420(defun eldoc-get-var-docstring (sym)
2dba57c1
JPW
421 (when sym
422 (cond ((and (eq sym (aref eldoc-last-data 0))
423 (eq 'variable (aref eldoc-last-data 2)))
424 (aref eldoc-last-data 1))
425 (t
426 (let ((doc (documentation-property sym 'variable-documentation t)))
427 (cond (doc
428 (setq doc (eldoc-docstring-format-sym-doc
1bed504a
SM
429 sym (eldoc-docstring-first-line doc)
430 'font-lock-variable-name-face))
2dba57c1
JPW
431 (eldoc-last-data-store sym doc 'variable)))
432 doc)))))
44faf981
NF
433
434(defun eldoc-last-data-store (symbol doc type)
435 (aset eldoc-last-data 0 symbol)
436 (aset eldoc-last-data 1 doc)
437 (aset eldoc-last-data 2 type))
438
439;; Note that any leading `*' in the docstring (which indicates the variable
440;; is a user option) is removed.
441(defun eldoc-docstring-first-line (doc)
442 (and (stringp doc)
443 (substitute-command-keys
444 (save-match-data
4d1243c8
MR
445 ;; Don't use "^" in the regexp below since it may match
446 ;; anywhere in the doc-string.
447 (let ((start (if (string-match "\\`\\*" doc) (match-end 0) 0)))
44faf981
NF
448 (cond ((string-match "\n" doc)
449 (substring doc start (match-beginning 0)))
450 ((zerop start) doc)
451 (t (substring doc start))))))))
452
453;; If the entire line cannot fit in the echo area, the symbol name may be
454;; truncated or eliminated entirely from the output to make room for the
455;; description.
1bed504a 456(defun eldoc-docstring-format-sym-doc (sym doc face)
44faf981
NF
457 (save-match-data
458 (let* ((name (symbol-name sym))
d16ba2e7 459 (ea-multi eldoc-echo-area-use-multiline-p)
03a9c6d0
NF
460 ;; Subtract 1 from window width since emacs will not write
461 ;; any chars to the last column, or in later versions, will
462 ;; cause a wraparound and resize of the echo area.
463 (ea-width (1- (window-width (minibuffer-window))))
464 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
465 (cond ((or (<= strip 0)
466 (eq ea-multi t)
467 (and ea-multi (> (length doc) ea-width)))
1bed504a 468 (format "%s: %s" (propertize name 'face face) doc))
03a9c6d0
NF
469 ((> (length doc) ea-width)
470 (substring (format "%s" doc) 0 ea-width))
471 ((>= strip (length name))
472 (format "%s" doc))
44faf981 473 (t
03a9c6d0
NF
474 ;; Show the end of the partial symbol name, rather
475 ;; than the beginning, since the former is more likely
476 ;; to be unique given package namespace conventions.
477 (setq name (substring name strip))
1bed504a 478 (format "%s: %s" (propertize name 'face face) doc))))))
1b09702a 479
44faf981 480\f
1bed504a 481;; Return a list of current function name and argument index.
1b09702a 482(defun eldoc-fnsym-in-current-sexp ()
1bed504a
SM
483 (save-excursion
484 (let ((argument-index (1- (eldoc-beginning-of-sexp))))
485 ;; If we are at the beginning of function name, this will be -1.
486 (when (< argument-index 0)
487 (setq argument-index 0))
488 ;; Don't do anything if current word is inside a string.
489 (if (= (or (char-after (1- (point))) 0) ?\")
490 nil
491 (list (eldoc-current-symbol) argument-index)))))
492
91af3942 493;; Move to the beginning of current sexp. Return the number of nested
1bed504a 494;; sexp the point was over or after.
9d497c01 495(defun eldoc-beginning-of-sexp ()
1bed504a
SM
496 (let ((parse-sexp-ignore-comments t)
497 (num-skipped-sexps 0))
40f7e0e8 498 (condition-case _
1bed504a
SM
499 (progn
500 ;; First account for the case the point is directly over a
501 ;; beginning of a nested sexp.
40f7e0e8 502 (condition-case _
1bed504a
SM
503 (let ((p (point)))
504 (forward-sexp -1)
505 (forward-sexp 1)
506 (when (< (point) p)
507 (setq num-skipped-sexps 1)))
508 (error))
509 (while
510 (let ((p (point)))
511 (forward-sexp -1)
512 (when (< (point) p)
513 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
514 (error))
515 num-skipped-sexps))
9d497c01
NF
516
517;; returns nil unless current word is an interned symbol.
518(defun eldoc-current-symbol ()
519 (let ((c (char-after (point))))
520 (and c
521 (memq (char-syntax c) '(?w ?_))
522 (intern-soft (current-word)))))
523
524;; Do indirect function resolution if possible.
525(defun eldoc-symbol-function (fsym)
6bdd9204 526 (let ((defn (symbol-function fsym)))
9d497c01 527 (and (symbolp defn)
40f7e0e8 528 (condition-case _
9d497c01
NF
529 (setq defn (indirect-function fsym))
530 (error (setq defn nil))))
531 defn))
1b09702a 532
e5eeb98c
GM
533(defun eldoc-function-argstring (arglist)
534 "Return ARGLIST as a string enclosed by ().
535ARGLIST is either a string, or a list of strings or symbols."
536 (cond ((stringp arglist))
537 ((not (listp arglist))
538 (setq arglist nil))
539 ((symbolp (car arglist))
540 (setq arglist
541 (mapconcat (lambda (s) (symbol-name s))
542 arglist " ")))
543 ((stringp (car arglist))
544 (setq arglist
545 (mapconcat (lambda (s) s)
546 arglist " "))))
547 (if arglist
548 (format "(%s)" arglist)))
549
550(defun eldoc-function-argstring-format (argstring)
d9d31f0a 551 "Apply `eldoc-argument-case' to each word in ARGSTRING.
e5eeb98c
GM
552The words \"&rest\", \"&optional\" are returned unchanged."
553 (mapconcat (lambda (s)
eec6d5b7 554 (if (string-match-p "\\`(?&\\(?:optional\\|rest\\))?\\'" s)
e5eeb98c
GM
555 s
556 (funcall eldoc-argument-case s)))
eec6d5b7 557 (split-string argstring) " "))
c4e36c26 558
332ae8db 559\f
9d497c01
NF
560;; When point is in a sexp, the function args are not reprinted in the echo
561;; area after every possible interactive command because some of them print
562;; their own messages in the echo area; the eldoc functions would instantly
563;; overwrite them unless it is more restrained.
564;; These functions do display-command table management.
565
566(defun eldoc-add-command (&rest cmds)
c0752bdc
SM
567 (dolist (name cmds)
568 (and (symbolp name)
569 (setq name (symbol-name name)))
570 (set (intern name eldoc-message-commands) t)))
9d497c01
NF
571
572(defun eldoc-add-command-completions (&rest names)
c0752bdc
SM
573 (dolist (name names)
574 (apply 'eldoc-add-command (all-completions name obarray 'commandp))))
9d497c01
NF
575
576(defun eldoc-remove-command (&rest cmds)
c0752bdc
SM
577 (dolist (name cmds)
578 (and (symbolp name)
579 (setq name (symbol-name name)))
580 (unintern name eldoc-message-commands)))
9d497c01
NF
581
582(defun eldoc-remove-command-completions (&rest names)
c0752bdc 583 (dolist (name names)
9d497c01 584 (apply 'eldoc-remove-command
c0752bdc 585 (all-completions name eldoc-message-commands))))
9d497c01 586
44faf981 587\f
9d497c01
NF
588;; Prime the command list.
589(eldoc-add-command-completions
24f574a9
JB
590 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
591 "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-"
592 "handle-select-window" "indent-for-tab-command" "left-" "mark-page"
593 "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-"
594 "move-end-of-" "next-" "other-window" "pop-global-mark" "previous-"
595 "recenter" "right-" "scroll-" "self-insert-command" "split-window-"
596 "up-list")
1b09702a
NF
597
598(provide 'eldoc)
599
600;;; eldoc.el ends here