[Emacs] Improve handling of eval defvars

This commit is contained in:
Ivan Malison 2016-11-02 18:23:03 -07:00
parent 537371d353
commit ed4f6aed58
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -2413,24 +2413,15 @@ Taken from http://endlessparentheses.com/eval-result-overlays-in-emacs-lisp.html
(point))))) (point)))))
#+END_SRC #+END_SRC
**** Reevalute defvars when running eval-last-sexp **** Reevalute defvars when running eval-last-sexp
What follows is a function that checks to see if what precedes point is a defvar We noflet elisp--preceding-sexp to munge defvars into sexps only for
and reevaluates it as a setq if it is eval-last-sexp.
#+BEGIN_SRC emacs-lisp
(defun imalison:defvar-at-point ()
(let* ((preceding-sexp (elisp--preceding-sexp)))
(when (and (listp preceding-sexp) (equal (car preceding-sexp) 'defvar))
preceding-sexp)))
(defun imalison:maybe-eval-defvar-as-setq ()
(interactive)
(let ((the-defvar (imalison:defvar-at-point)))
(when the-defvar
(eval `(setq ,@(cdr the-defvar))))))
#+END_SRC
Now we add advice to eval-last-sexp so that it has this behavior.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun imalison:maybe-setq-instead (fn &rest args) (defun imalison:maybe-setq-instead (fn &rest args)
(or (imalison:maybe-eval-defvar-as-setq) (noflet ((elisp--preceding-sexp (&rest preceding-args)
(let* ((preceding-sexp (apply this-fn preceding-args)))
(if (and (listp preceding-sexp) (equal (car preceding-sexp) 'defvar))
`(setq ,@(cdr preceding-sexp))
preceding-sexp))))
(apply fn args))) (apply fn args)))
(advice-add 'eval-last-sexp :around 'imalison:maybe-setq-instead) (advice-add 'eval-last-sexp :around 'imalison:maybe-setq-instead)