Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid extra space character when slurping first expression

When using Paredit, I'm often confronted with the following (pipe char represents cursor):

(foo |bar baz)

I want to enclose bar in parens, so I use paredit-wrap-around (M-() to get this:

(foo (|bar) baz)

The same applies when I want to put bar inside a string instead, I can use M-" to get this:

(foo "|bar" baz)

However, Clojure also uses square brackets [] and curly braces {} for vectors, maps and sets. These don't appear to have a paredit 'wrap around' command. So in this case, e.g. to put bar into a vector, I typically use the [ key to create a new vector:

(foo [|] bar baz)

followed by a paredit-forward-slurp-sexp (C-), which results in:

(foo [| bar] baz)

I'd like to remove the space character that has been inserted before bar. Is this possible? Why does paredit retain a space char when going from an empty expression to one that contains one item? Shouldn't the first item added to an S-expression always be pushed right against the opening paren?

like image 839
joelittlejohn Avatar asked Jun 30 '13 22:06

joelittlejohn


2 Answers

Paredit has, for quite a while, had paredit-wrap-curly and paredit-wrap-square. If you want to use them, simply bind them to a likely-sounding key, and have at it. If you're using such an old version of paredit that these functions don't exist, you should upgrade (but I don't think you are, since the slurp commands work with them).

like image 186
amalloy Avatar answered Nov 12 '22 19:11

amalloy


The latest development version of paredit binds M-[ to paredit-wrap-square which does exactly what you want. Emacs already binds M-{ is to backward-paragraph, but if you don't ever use that, you could customize paredit to shadow it:

(eval-after-load 'paredit
  '(progn
     (define-key paredit-mode-map (kbd "M-{") 'paredit-wrap-curly)))

If you want to delete all space around the point, you can always type M-\ for delete-horizontal-space.

like image 20
Erinn Baldwin Avatar answered Nov 12 '22 18:11

Erinn Baldwin