Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to wrap forms with an already written outer form in paredit

Suppose I have this code

(hello world)
(hi world)

Then it comes to my mind that I should wrap that with the following form:

(let (acc)
  <>
  (nreverse acc))

resulting in this code:

(let (acc)
  (hello world)
  (hi world)
  (nreverse acc))

Here is how I usually do the wrapping. First I write an incomplete outer form before the to-be-wrapped forms like this:

(let (acc))
(hello world)
(hi world)

Then I press C-) (paredit-forward-slurp-sexp) twice to slurp things:

(let (acc)
  (hello world)
  (hi world))

Then I finish up by adding (nreverse acc) at the end.

Now what I wonder is what if I started by first writing the complete outer form template like this?

(let (acc)
  (nreverse acc))
(hello world)
(hi world)

or like this

(hello world)
(hi world)
(let (acc)
  (nreverse acc))

Is there a short sequence of paredit or non-paredit commands that I can press to finish the job from that start?

I could move point, cut the two to-be-wrapped forms, move point, paste the forms. But I am wondering if there is a more convenient way.

I am satisfied with how I do the wrapping, but it feels like I might be missing some other neat editing tricks which are to be found from a different start.

like image 407
Jisang Yoo Avatar asked Sep 23 '13 19:09

Jisang Yoo


2 Answers

Instead of repeatedly using paredit-forward-slurp-sexp or kill-yank'ing the region, you can mark the region of sexps to enclose and use paredit-wrap-round M-(

If I were typing your example I would start with the last expression:

(hello world)
(hi world)
(nreverse acc)_

C-SPC C-M-a C-M-a C-M-a M-( yields:

(_(hello world)
  (hi world)
  (nreverse acc))

Insert let (acc) and finish with RET C-M-h TAB to reindentify.

There's probably a better way =), but even if you don't use transient-mark-mode you can supply other options to wrapping:

  • C-u wraps to the end of buffer or the enclosing list
  • C-u # wraps the following # expressions

See the docstring for paredit-wrap-sexp

like image 67
assem Avatar answered Nov 05 '22 23:11

assem


You might also want to look at redshank, a emacs package that does a bunch of useful things like this. Works with paredit very nicely.

like image 24
David Hodge Avatar answered Nov 05 '22 22:11

David Hodge