[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Eval current sexp?
From: |
Drew Adams |
Subject: |
RE: Eval current sexp? |
Date: |
Fri, 10 Dec 2010 18:04:32 -0800 |
> From: Elena Sent: Thursday, July 01, 2010 2:11 AM
> I can't find a function which evaluates the current (delimited) sexp.
> I mean that in: (defun fun () (setq var nil))
> no matter where the point is inside the "setq" expression, I'd like
> the "setq" sexp evaluated.
This should help. You might need to tweak it a bit - dunno.
You will need library `thingatpt+.el', which is here:
http://www.emacswiki.org/emacs/thingatpt%2b.el.
It defines function `sexp-nearest-point'.
(defun sexp-value-nearest-point (&optional msgp)
"Return the value of the list sexp nearest point.
Climbs the list hierarchy until it gets to a list sexp.
Interactively, echo the sexp as well as the value."
(interactive "p")
(save-excursion
(cond ((looking-at "\\s-*\\s(") (skip-syntax-forward "-"))
((looking-at "\\s)\\s-*") (skip-syntax-backward "-")))
(let ((sexp (sexp-nearest-point)))
(condition-case nil ; Handle an `up-list' error.
(while (not (listp sexp))
(up-list -1)
(setq sexp (sexp-nearest-point)))
(error sexp))
(let ((value (eval sexp)))
(when msgp (message "Value: %s, Sexp: %s" value sexp))
value))))