2015-12-02 14:23:39 +00:00
#+TITLE : Emacs Lisp layer
2015-06-10 16:44:30 +00:00
2019-05-07 08:21:07 +00:00
#+TAGS : dsl|layer|lisp|programming
2019-05-05 17:26:40 +00:00
2016-10-06 11:35:19 +00:00
[[file:img/emacs.png ]]
2015-06-10 16:44:30 +00:00
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description ][Description ]]
2017-10-01 13:06:53 +00:00
- [[#features ][Features: ]]
2017-05-22 14:16:12 +00:00
- [[#install ][Install ]]
- [[#auto-compile ][Auto-compile ]]
- [[#working-with-lisp-files-barfage-slurpage--more ][Working with lisp files (barfage, slurpage & more) ]]
- [[#debugging-elisp ][Debugging Elisp ]]
2017-10-25 11:52:19 +00:00
- [[#nameless ][Nameless ]]
2017-12-18 04:25:04 +00:00
- [[#aliases ][Aliases ]]
2019-06-01 17:13:30 +00:00
- [[#structurally-safe-editing ][Structurally safe editing ]]
2017-05-22 14:16:12 +00:00
- [[#key-bindings ][Key bindings ]]
2017-10-25 11:52:19 +00:00
- [[#additional-testing-functions-with-overseer ][Additional testing functions with overseer ]]
2017-05-22 14:16:12 +00:00
- [[#additional-evaluation-functions ][Additional evaluation functions ]]
- [[#format-code ][Format code ]]
- [[#debugging ][Debugging ]]
2020-04-18 08:55:55 +00:00
- [[#refactoring-with-emr ][Refactoring with emr ]]
2015-06-10 16:44:30 +00:00
* Description
This layer gathers all the configuration related to emacs-lisp. This should
always be in your dotfile, it is not recommended to uninstall it.
2017-10-01 13:06:53 +00:00
** Features:
- Auto-completion using company
- Linting using flycheck integration
2019-04-10 03:45:05 +00:00
- Linting package file metadata using [[https://github.com/purcell/flycheck-package ][flycheck-package ]]
2017-10-01 13:06:53 +00:00
- Repl support via =IELM=
- Support for specific lisp navigation styles via =emacs-lisp-mode=
- Auto-compile via [[https://github.com/tarsius/auto-compile ][auto-compile ]] package
2019-06-01 17:13:30 +00:00
- Debugging via [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Edebug.html#Edebug ][edebug ]]
2017-10-25 11:52:19 +00:00
- Ert test runner with [[https://github.com/tonini/overseer.el ][overseer ]]
2018-01-04 06:34:23 +00:00
- Nameless package prefix with optional [[https://github.com/Malabarba/Nameless ][nameless ]]
2019-06-01 17:13:30 +00:00
- Structurally safe editing using optional [[https://github.com/luxbock/evil-cleverparens ][evil-cleverparens ]]
2018-06-11 03:24:44 +00:00
- Visual feedback when evaluation using [[https://github.com/hchbaw/eval-sexp-fu.el ][eval-sexp-fu ]]
2017-10-01 13:06:53 +00:00
2015-06-10 16:44:30 +00:00
* Install
2016-01-06 05:21:55 +00:00
To use this configuration layer, add it to your =~/.spacemacs= . You will need to
add =emacs-lisp= to the existing =dotspacemacs-configuration-layers= list in this
file.
2015-06-10 16:44:30 +00:00
2015-10-15 12:18:53 +00:00
* Auto-compile
This layer adds the [[https://github.com/tarsius/auto-compile ][auto-compile ]] package to automatically keep the byte-compiled
version of your Emacs lisp files synced with the uncompiled version on every
save. If there are any compiler errors in the file, you will see a counter show
up in the mode line. You can hover over these numbers to get a description or
view the compiler output with the ~SPC m c l~ key binding. To disable the
feature use this line in your =dotspacemacs/user-config= function.
#+BEGIN_SRC emacs-lisp
(remove-hook 'emacs-lisp-mode-hook 'auto-compile-mode)
#+END_SRC
You can also exclude the =auto-compile= package.
2016-12-24 16:56:24 +00:00
* Working with lisp files (barfage, slurpage & more)
2016-11-22 20:53:50 +00:00
Spacemacs comes with a special =lisp-state= for working with lisp code that
2019-05-26 20:58:52 +00:00
supports slurpage, barfage and more tools you'll likely want when working with
2015-10-09 10:15:35 +00:00
lisp.
2015-10-08 09:53:03 +00:00
2015-10-09 10:15:35 +00:00
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.
2015-10-08 09:53:03 +00:00
2017-02-12 18:48:23 +00:00
* Debugging Elisp
2020-11-13 23:55:20 +00:00
Here is an interactive quick start to debug Emacs Lisp from an =emacs-lisp-mode= buffer.
2017-02-12 18:48:23 +00:00
2020-11-13 23:55:20 +00:00
To follow this tutorial open the code of the following source block in an
=emacs-lisp-mode= buffer. One way to do this is to first find this emacs-lisp
layer documentation file in Spacemacs using ~SPC h l emacs-lisp RET~ . This will
open the current file in =space-doc-mode= . Toggle off =space-doc-mode= using =,
T V= so that Spacemacs will switch to =org-mode= . Now you can place the cursor
somewhere inside the code block and press ~, '~ to open the code in an
2020-11-22 08:56:33 +00:00
interactive =emacs-lisp-buffer= .
2017-02-12 18:48:23 +00:00
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
(defun helloworld (name)
(let ((n (subroutine name)))
(message (format "Hello world, %s!" name))))
2017-02-12 18:48:23 +00:00
2018-09-19 03:54:47 +00:00
(defun subroutine (s)
(concat "my dear " s))
2017-02-12 18:48:23 +00:00
2018-09-19 03:54:47 +00:00
(helloworld "Spacemacs")
2017-02-12 18:48:23 +00:00
#+END_SRC
2018-09-19 03:54:47 +00:00
Steps:
2020-11-13 23:55:20 +00:00
Now from within the =emacs-lisp-buffer=
1) Evaluate each sexp by putting your point in each of them and press ~, e f~ or
press ~, e b~ to evaluate all code in the buffer at once.
2) To debug the =helloworld= function, put your cursor on the =defun= keyword and
2018-09-19 03:54:47 +00:00
2017-02-12 18:48:23 +00:00
press ~SPC m d f~ (or ~, d f~ ), it will put a breakpoint on the function (we say
that we instrumentalise this function) so whenever the Lisp interpreter
encounters this function it will start the debugger.
2020-11-13 23:55:20 +00:00
3) Then go to the closing parenthesis of =(helloworld "Spacemacs")= and press
2018-09-19 03:54:47 +00:00
2017-02-12 18:48:23 +00:00
~, e e~ to evaluate it, if you are using =vim= editing style you end up in
evilified state otherwise you end up in emacs state and =*Debugging*= is
displayed in the mode line.
2020-11-13 23:55:20 +00:00
4) Press ~s~ to go to next step up to the opening parenthesis of
2018-10-17 04:03:28 +00:00
=(subroutine name)= ,
2020-11-13 23:55:20 +00:00
5) Press ~i~ to go into the =subroutine= where you can press ~s~ to step in
2017-02-12 18:48:23 +00:00
2018-09-19 03:54:47 +00:00
function or press ~o~ to go out of it.
2020-11-13 23:55:20 +00:00
6) Press ~a~ to stop debugging.
2017-02-12 18:48:23 +00:00
2017-10-25 11:52:19 +00:00
* Nameless
2017-12-18 04:25:04 +00:00
Nameless hides package namespaces in your emacs-lisp code, and replaces it by
2018-01-06 07:51:46 +00:00
leading ~>~ It can be toggled with ~SPC m T n~ .
2017-10-25 11:52:19 +00:00
2018-01-11 23:20:19 +00:00
By default =nameless= is deactivated, to enable it set the layer variable
=emacs-lisp-hide-namespace-prefix= to =t= .
NOTE: =nameless= is known to cause problems when spacemacs is used inside a
terminal window, use with caution.
2017-10-25 17:45:56 +00:00
2017-12-18 04:25:04 +00:00
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
2018-01-11 23:20:19 +00:00
(emacs-lisp :variables emacs-lisp-hide-namespace-prefix t)))
2017-12-18 04:25:04 +00:00
#+END_SRC
** Aliases
This layer defines some global aliases for Spacemacs:
| alias | namespace |
|-------+---------------------|
2018-01-06 07:51:46 +00:00
| .S | dotspacemacs |
| CL | configuration-layer |
2017-12-18 04:25:04 +00:00
| S | spacemacs |
| SB | spacemacs-buffer |
You can define additional alias in function =dotspacemacs/user-config= of your
dotfile:
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(add-to-list 'nameless-global-aliases '("N" . "nameless"))
2017-12-18 04:25:04 +00:00
#+END_SRC
2017-10-25 11:52:19 +00:00
2019-06-01 17:13:30 +00:00
* Structurally safe editing
2018-01-04 06:55:54 +00:00
This layer adds support for =evil-cleverparens= which allows to safely edit
2018-01-04 06:34:23 +00:00
lisp code by keeping the s-expressions balanced.
2018-01-04 06:55:54 +00:00
By default this mode is not activated. You can turn it on locally on the active
buffer with ~SPC m T s~ (=s= for safe).
2018-01-04 06:34:23 +00:00
To turn it on automatically for all =emacs-lisp= buffers call the following
function in your =dotspacemacs/user-config= function:
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(spacemacs/toggle-evil-safe-lisp-structural-editing-on-register-hook-emacs-lisp-mode)
2018-01-04 06:34:23 +00:00
#+END_SRC
or to enable it for all supported modes:
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(spacemacs/toggle-evil-safe-lisp-structural-editing-on-register-hooks)
2018-01-04 06:34:23 +00:00
#+END_SRC
When enabled the symbol =🆂= should be displayed in the mode-line.
2016-12-24 16:56:24 +00:00
* Key bindings
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-02-11 20:36:16 +00:00
|----------------------------+--------------------------------------------------------|
| ~SPC m g g~ | go to definition of symbol under point |
| ~SPC m g G~ | go to definition of symbol under point in other window |
| ~SPC m h h~ | describe symbol at point |
| ~SPC m c c~ | byte compile the current file |
| ~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 b~ | evaluate current buffer |
| ~SPC m e C~ | evaluate current =defun= or =setq= |
| ~SPC m e e~ | evaluate sexp before point |
| ~SPC m e f~ | evaluation current function |
| ~SPC m e r~ | evaluate current region |
| ~SPC m , ~ | toggle =lisp state= |
| ~SPC m t b~ | run tests of current buffer |
| ~SPC m t q~ | run =ert= |
| ~SPC m d m~ | open [[https://github.com/joddie/macrostep][macrostep]] transient-state |
2017-10-25 11:52:19 +00:00
| ~SPC m :~ | toggle nameless minor mode |
** Additional testing functions with overseer
Function related to test are present under the ~SPC m t~ prefix:
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-10-25 11:52:19 +00:00
|-------------+---------------|
| ~SPC m t a~ | overseer test |
| ~SPC m t A~ | test debug |
| ~SPC m t t~ | run test |
| ~SPC m t b~ | test buffer |
| ~SPC m t f~ | test file |
| ~SPC m t g~ | test tags |
| ~SPC m t p~ | test prompt |
| ~SPC m t q~ | test quiet |
| ~SPC m t h~ | test help |
2016-12-24 16:56:24 +00:00
2017-02-12 17:50:07 +00:00
** Additional evaluation functions
2016-12-24 16:56:24 +00:00
If =smartparens= is used the following additional key bindings are available:
2015-06-10 16:44:30 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2016-12-24 16:56:24 +00:00
|-------------+------------------------------|
| ~SPC m e c~ | evaluate sexp around point |
| ~SPC m e s~ | evaluate symbol around point |
2015-06-10 16:44:30 +00:00
2017-02-11 21:31:56 +00:00
** Format code
2017-08-18 19:07:43 +00:00
The [[https://github.com/syl20bnr/spacemacs/blob/develop/layers/%2Bemacs/semantic/README.org ][semantic ]] layer should be installed for these key bindings to become active.
2015-06-10 16:44:30 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+-------------------------|
| ~SPC m = b~ | format current buffer |
2019-07-27 02:59:42 +00:00
| ~SPC m = d~ | format current function |
2015-06-10 16:44:30 +00:00
| ~SPC m = o~ | format all on one line |
| ~SPC m = s~ | format current sexp |
2017-02-11 21:31:56 +00:00
** Debugging
2017-02-12 18:48:23 +00:00
To start debugging:
2017-02-11 21:31:56 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-02-11 21:31:56 +00:00
|-------------+------------------------------------------------------------------------|
| ~SPC m d f~ | on a =defun= symbol toggle on the instrumentalisation of the function |
| ~SPC m d F~ | on a =defun= symbol toggle off the instrumentalisation of the function |
| ~SPC m d t~ | insert =(debug)= to print the stack trace and re-evaluate the function |
2017-02-12 18:48:23 +00:00
In =edebug-mode= (=*Debugging*= is displayed in the minor modes segment of the
mode line)
2020-08-16 02:12:26 +00:00
| Key binding | Description |
|-------------+----------------------------|
| ~s~ | step |
| ~i~ | step in |
| ~o~ | step out |
2020-09-04 16:29:07 +00:00
| ~S~ | next |
2020-08-16 02:12:26 +00:00
| ~f~ | forward-sexp |
| ~H~ | goto here |
| ~I~ | instrument callee |
2020-09-04 16:29:07 +00:00
| ~c~ | go |
2020-08-16 02:12:26 +00:00
| ~C~ | fast continue |
| ~t~ | trace |
| ~T~ | fast trace |
| ~q~ | quit |
| ~Q~ | quit nonstop |
2020-09-04 16:29:07 +00:00
| ~a~ | stop |
2020-08-16 02:12:26 +00:00
| ~b~ | set breakpoint |
| ~u~ | unset breakpoint |
| ~B~ | next breakpoint |
| ~x~ | set conditional breakpoint |
| ~r~ | previous result |
| ~e~ | evaluate expression |
| ~C-x C-e~ | evaluate last sexp |
2020-09-04 16:29:07 +00:00
| ~w~ | where |
| ~?~ | help |
2020-08-16 02:12:26 +00:00
| ~d~ | backtrace |
2017-02-12 18:48:23 +00:00
2021-03-17 01:45:07 +00:00
In =edebug-eval-mode=
| Key binding | Description |
|-------------+------------------------------|
| ~SPC m g w~ | where |
| ~SPC m a~ | delete evaluation item |
| ~SPC m k~ | delete evaluation item |
| ~SPC m ,~ | update evaluation list |
| ~SPC m c~ | update evaluation list |
| ~SPC m e e~ | evaluate last sexp |
| ~SPC m e E~ | evaluate last sexp and print |
2017-02-12 18:48:23 +00:00
In =debugger-mode= (=Debugger= is displayed in major mode segment of the mode
line)
2020-09-04 16:29:07 +00:00
| Key binding | Description |
|-------------+--------------------------|
| ~<tab>~ | forward |
| ~S-<tab>~ | backward |
| ~RET~ | backtrace help follow |
| ~p~ | backtrace backward frame |
| ~c~ | continue |
| ~R~ | record expression |
| ~d~ | step through |
| ~e~ | eval expression |
| ~J~ | jump |
| ~L~ | list functions |
| ~b~ | frame |
| ~r~ | return value |
| ~u~ | frame clear |
| ~C-v~ | backtrace toggle locals |
| ~q~ | quit |
2020-04-18 08:55:55 +00:00
** Refactoring with emr
2020-05-06 21:12:48 +00:00
| Key binding | Description |
|---------------+---------------------------|
| ~SPC m r f e~ | implement function |
| ~SPC m r f d~ | find unused definitions |
| ~SPC m r e f~ | extract function |
| ~SPC m r e v~ | extract variable |
| ~SPC m r e l~ | extract to let |
| ~SPC m r e c~ | extract constant |
| ~SPC m r e a~ | extract autoload |
| ~SPC m r i v~ | inline variable |
| ~SPC m r i s~ | inline let variable |
| ~SPC m r i f~ | inline function |
| ~SPC m r i a~ | insert autoload directive |
| ~SPC m r d l~ | delete let binding form |
| ~SPC m r d d~ | delete unused definition |
| ~SPC m e w~ | eval and replace |