emacs-lisp: fix eval-last-sexp and add SPC m e C

Important evil advices for eval-last-sexp were removed for smartparens users,
it made eval-last-sexp not working correctly in normal state by evaluating the
sexp before the evil cursor and it was impossible to evaluate an sexp when the
cursor was on the last character of a line.

Also add SPC m e C which will execute the first defun or setq sexp encountered
before point.
This commit is contained in:
syl20bnr 2016-12-24 11:56:24 -05:00
parent 831b01fd8e
commit a5c87776d1
3 changed files with 62 additions and 50 deletions

View File

@ -6,9 +6,9 @@
- [[#description][Description]] - [[#description][Description]]
- [[#install][Install]] - [[#install][Install]]
- [[#auto-compile][Auto-compile]] - [[#auto-compile][Auto-compile]]
- [[#working-with-lisp-files-barfage-slurpage--more][Working with lisp files (barfage, slurpage & more)]]
- [[#key-bindings][Key bindings]] - [[#key-bindings][Key bindings]]
- [[#working-with-lisp-files-barfage-slurpage--more][Working with lisp files (barfage, slurpage & more)]] - [[#smartparens][smartparens]]
- [[#leader][Leader]]
- [[#srefactor][srefactor]] - [[#srefactor][srefactor]]
* Description * Description
@ -34,8 +34,7 @@ feature use this line in your =dotspacemacs/user-config= function.
You can also exclude the =auto-compile= package. You can also exclude the =auto-compile= package.
* Key bindings * Working with lisp files (barfage, slurpage & more)
** Working with lisp files (barfage, slurpage & more)
Spacemacs comes with a special ~lisp-state~ for working with lisp code that Spacemacs comes with a special ~lisp-state~ for working with lisp code that
supports slurpage, barfage and more tools you'll likely want when working with supports slurpage, barfage and more tools you'll likely want when working with
lisp. lisp.
@ -43,24 +42,32 @@ lisp.
As this state works the same for all files, the documentation is in global As this state works the same for all files, the documentation is in global
[[https://github.com/syl20bnr/spacemacs/blob/master/doc/DOCUMENTATION.org#lisp-key-bindings][DOCUMENTATION.org]]. In general, use ~SPC k~ to interact with the lisp-state. [[https://github.com/syl20bnr/spacemacs/blob/master/doc/DOCUMENTATION.org#lisp-key-bindings][DOCUMENTATION.org]]. In general, use ~SPC k~ to interact with the lisp-state.
** Leader * Key bindings
| Key Binding | Description | | Key Binding | Description |
|----------------------------+------------------------------------------------------------| |----------------------------+----------------------------------------|
| ~SPC m g g~ | go to definition of symbol under point | | ~SPC m g g~ | go to definition of symbol under point |
| ~SPC m h h~ | describe symbol at point | | ~SPC m h h~ | describe symbol at point |
| ~SPC m c c~ | byte compile the current file | | ~SPC m c c~ | byte compile the current file |
| ~SPC m c l~ | popup compile-log buffer | | ~SPC m c l~ | popup compile-log buffer |
| ~SPC m e $~ or ~SPC m e l~ | go to end of current line and evaluate | | ~SPC m e $~ or ~SPC m e l~ | go to end of current line and evaluate |
| ~SPC m e b~ | evaluate current buffer | | ~SPC m e b~ | evaluate current buffer |
| ~SPC m e c~ | evaluate current form (start with =defun=, =setq=, etc...) | | ~SPC m e C~ | evaluate current =defun= or =setq= |
| ~SPC m e e~ | evaluate sexp before point | | ~SPC m e e~ | evaluate sexp before point |
| ~SPC m e r~ | evaluate current region | | ~SPC m e f~ | evaluation current function |
| ~SPC m e f~ | evaluation current function | | ~SPC m e r~ | evaluate current region |
| ~SPC m ,~ | toggle =lisp state= | | ~SPC m ,~ | toggle =lisp state= |
| ~SPC m t b~ | run tests of current buffer | | ~SPC m t b~ | run tests of current buffer |
| ~SPC m t q~ | run =ert= | | ~SPC m t q~ | run =ert= |
| ~SPC m d m~ | open [[https://github.com/joddie/macrostep][macrostep]] transient-state | | ~SPC m d m~ | open [[https://github.com/joddie/macrostep][macrostep]] transient-state |
** smartparens
If =smartparens= is used the following additional key bindings are available:
| Key Binding | Description |
|-------------+------------------------------|
| ~SPC m e c~ | evaluate sexp around point |
| ~SPC m e s~ | evaluate symbol around point |
** srefactor ** srefactor
The [[file:../../semantic/README.org][semantic]] layer should be installed for these key bindings to become active. The [[file:../../semantic/README.org][semantic]] layer should be installed for these key bindings to become active.

View File

@ -29,3 +29,36 @@
(not (fboundp symb))) (not (fboundp symb)))
(find-variable-other-window symb) (find-variable-other-window symb)
(find-function-at-point)))) (find-function-at-point))))
;; smartparens integration
(defun spacemacs/eval-current-form-sp (&optional arg)
"Call `eval-last-sexp' after moving out of one level of
parentheses. Will exit any strings and/or comments first.
An optional ARG can be used which is passed to `sp-up-sexp' to move out of more
than one sexp.
Requires smartparens because all movement is done using `sp-up-sexp'."
(interactive "p")
(require 'smartparens)
(let ((evil-move-beyond-eol t))
;; evil-move-beyond-eol disables the evil advices around eval-last-sexp
(save-excursion
(let ((max 10))
(while (and (> max 0)
(sp-point-in-string-or-comment))
(decf max)
(sp-up-sexp)))
(sp-up-sexp arg)
(call-interactively 'eval-last-sexp))))
(defun spacemacs/eval-current-symbol-sp ()
"Call `eval-last-sexp' on the symbol around point.
Requires smartparens because all movement is done using `sp-forward-symbol'."
(interactive)
(require 'smartparens)
(let ((evil-move-beyond-eol t))
;; evil-move-beyond-eol disables the evil advices around eval-last-sexp
(save-excursion
(sp-forward-symbol)
(call-interactively 'eval-last-sexp))))

View File

@ -93,6 +93,7 @@
"cc" 'emacs-lisp-byte-compile "cc" 'emacs-lisp-byte-compile
"e$" 'lisp-state-eval-sexp-end-of-line "e$" 'lisp-state-eval-sexp-end-of-line
"eb" 'eval-buffer "eb" 'eval-buffer
"eC" 'spacemacs/eval-current-form
"ee" 'eval-last-sexp "ee" 'eval-last-sexp
"er" 'eval-region "er" 'eval-region
"ef" 'eval-defun "ef" 'eval-defun
@ -167,35 +168,6 @@
"=s" 'srefactor-lisp-format-sexp)))) "=s" 'srefactor-lisp-format-sexp))))
(defun emacs-lisp/post-init-smartparens () (defun emacs-lisp/post-init-smartparens ()
(advice-remove 'elisp--preceding-sexp 'evil--preceding-sexp)
(defun spacemacs/eval-current-form-sp (&optional arg)
"Call `eval-last-sexp' after moving out of one level of
parentheses. Will exit any strings and/or comments first.
Requires smartparens because all movement is done using
`sp-up-sexp'. An optional ARG can be used which is passed to
`sp-up-sexp' to move out of more than one sexp."
(interactive "p")
(require 'smartparens)
(save-excursion
(let ((max 10))
(while (and (> max 0)
(sp-point-in-string-or-comment))
(decf max)
(sp-up-sexp)))
(sp-up-sexp arg)
(call-interactively 'eval-last-sexp)))
(defun spacemacs/eval-current-symbol-sp ()
"Call `eval-last-sexp' on the symbol around point. Requires
smartparens because all movement is done using
`sp-forward-symbol'."
(interactive)
(require 'smartparens)
(save-excursion
(sp-forward-symbol)
(call-interactively 'eval-last-sexp)))
(dolist (mode '(emacs-lisp-mode lisp-interaction-mode)) (dolist (mode '(emacs-lisp-mode lisp-interaction-mode))
(spacemacs/set-leader-keys-for-major-mode mode (spacemacs/set-leader-keys-for-major-mode mode
"ec" 'spacemacs/eval-current-form-sp "ec" 'spacemacs/eval-current-form-sp