Difference between revisions of "Lisp editing"

From WikEmacs
Jump to navigation Jump to search
m (right title depth)
(→‎With evil-mode: evil-smartparens)
Line 24: Line 24:
  
 
(you might want to '''M-x package-refresh-content''' beforehand).
 
(you might want to '''M-x package-refresh-content''' beforehand).
 +
 +
=== Evil-smartparens ===
 +
 +
[https://github.com/expez/evil-smartparens evil-smartparens] is a minor mode which makes [[evil]] play nicely with '''smartparens'''. It helps keeping expressions balanced when you type evil commands like '''dd'''.
 +
 +
It is available on [[Melpa]].
 +
 +
Let's take the example of '''dd''', which in evil-mode means "delete the current line". Let's consider the following expression, where the cursor is represented by "|":
 +
 +
<source lang="lisp">
 +
(let| ((foo 1.01))
 +
  (frobnicate foo))
 +
</source>
 +
 +
If we hit '''dd''' then nothing will happen. But If we move down a line and hit '''dd''' then we will see:
 +
 +
<source lang="lisp">
 +
(let ((foo 1.01))
 +
  )
 +
</source>
 +
 +
The last parenthesis is preserved to keep our s-expression correct. You can see many more examples on the project's page.
  
  

Revision as of 17:29, 21 September 2015

Tips on how to edit lisp code efficiently.

Built-in modes

With evil-mode

Lispy, a vi-like paredit

Lispy makes it easier to edit lisp code when we use evil-mode.

It offers very short keybindings. For example, going to the next parenthesis is bound to j, s moves down the current expression, e to eval and xe to edebug, xd turns current lambda into a defun, etc

The price to pay is that the bindings are only active when:

  • the point is before an open paren: (, [ or {
  • the point is after a close paren: ), ] or }
  • the region is active

Installation

Via MELPA:

   M-x package-install RET lispy RET

(you might want to M-x package-refresh-content beforehand).

Evil-smartparens

evil-smartparens is a minor mode which makes evil play nicely with smartparens. It helps keeping expressions balanced when you type evil commands like dd.

It is available on Melpa.

Let's take the example of dd, which in evil-mode means "delete the current line". Let's consider the following expression, where the cursor is represented by "|":

(let| ((foo 1.01))
  (frobnicate foo))

If we hit dd then nothing will happen. But If we move down a line and hit dd then we will see:

(let ((foo 1.01))
  )

The last parenthesis is preserved to keep our s-expression correct. You can see many more examples on the project's page.


Tips and tricks

Integrate expand-region

expand-region is handy because it permits to expand the region by semantic units. For example, in the string (hello "foo | oo"), calling it the first will select "foo", then "\"foo\"", then "hello \"foooo\"", then the whole expression with the parenthesis.

The following snippet binds "v" to expand-region when the region is active.

;; https://emacs.stackexchange.com/questions/16614/make-evil-mode-more-lisp-friendly
(defun evil-visual-char-or-expand-region ()
  (interactive)
  (if (region-active-p)
        (call-interactively 'er/expand-region)
    (evil-visual-char)))

(define-key evil-normal-state-map "v" 'evil-visual-char-or-expand-region)
(define-key evil-visual-state-map "v" 'evil-visual-char-or-expand-region)
(define-key evil-visual-state-map [escape] 'evil-visual-char)

Make "dd" delete an expression

di( or da( isn't specific to lisp, they are just evil text objects.

See also

The emacs lisp page.