Difference between revisions of "Lisp editing"

From WikEmacs
Jump to navigation Jump to search
(started page on how to edit lisp code)
 
(→‎Tips and tricks: bind v to expand region)
Line 27: Line 27:
  
 
=== Tips and tricks ===
 
=== Tips and tricks ===
 +
 +
=== Integrate expand-region ===
 +
 +
[http://melpa.org/#/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.
 +
<source lang="lisp">
 +
;; 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)
 +
</source>
  
 
==== Make "dd" delete an expression ====
 
==== Make "dd" delete an expression ====

Revision as of 13:27, 16 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).


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.