6

I'm using Emacs in Paredit mode for Lisp code.

I'm trying to wrap a function call in println:

(square 5)
(println (square 5))

What ends up happening in paredit is this:

(square 5)
(println) (square 5)

There is no way i can delete closing paren of println and move it to the end.

The way i'm doing it now is to: 1. delete function call and yank it within println 2. write println without paren, visually select code and enclose in parens

(square 5)
println (square 5)
=> select block of code and type (
(println (square 5))

Both of these approaches are tedious. This seems to be a common problem anytime i write code inside out in Paredit. Any help would be appreciated

1
  • 1
    You'd probably do well to read the short manual, instead of asking questions for features that are well documented. Jan 5, 2013 at 15:04

7 Answers 7

8

paredit-wrap-round command may help (bound to M-( in the paredit version I use).

2
  • M-( results in (println) (square 5) if i type println first. I just realized i have to wrap around first and then type println which isn't natural. Thanks!
    – Sathish
    Jan 5, 2013 at 9:46
  • Wrapping the parameter before adding the function call is completely natural once you get used to writing lisp.
    – event_jr
    Jan 5, 2013 at 13:07
6

In contrast to the other answers, I tend to use Ctrl-Right for this: after you get

(println|) (square 5)

(where | is where the cursor is), I simply press Ctrl-Right to get the correct result.

0
5

M-(

You can call paredit-insert-html-examples to generate a HTML webpage cheatsheet. One version is here.

3

Three ways to wrap a print form around a square form, in step-by-step progressions. (two ways of the three are already mentioned in other answers)

(1) Cut & type & paste

(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; mark-sexp and kill-region
(+ | 4)
;;; type the print form and make sure cursor is at right place
(+ (print |) 4)
;;; paste
(+ (print (square 3)) 4)

(2) type & slurf

(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; type the print form and make sure cursor is at right place
(+ (print|) (square 3) 4)
;;; paredit-forward-slurp-sexp
(+ (print (square 3)) 4)

(3) wrap & type

(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; paredit-wrap-round
(+ (|(square 3)) 4)
;;; type print
(+ (print (square 3)) 4)

Cut & type & paste is the most tedious. It doesn't depend on paredit and is the easiest to generalize to the case of wrapping a complex outer form around more than one inner forms which may be in multiline format, for example, turning

(let ((x 1))
  (moo)
  (oink)
  (oink))

into

(let ((x 1))
  (moo)
  (mapc (lambda (x)
          (oink)
          (oink))
        (list 1 2 3)))

by wrapping a mapc-over-lambda form around two oinks.

Type & slurf seems to be the most known. It can be generalized to the mapc-over-lambda case as well.

Wrap & type is the most easiest to type. When you generalize this to the map-over-lambda case, you select two oinks, and press ( to wrap it, and finish writing the lambda form, and select the lambda form, press ( to wrap it, type mapc, and you can use C-M-f to cross over the lambda form and type (list 1 2 3).

2

In your case the solution is M-2 M-( or M-( C-).

1

There are several ways of doing this. The way that I normally do this is type

"("

which gets you

()println

and then C-S-) which gets you

(println)

The best way to use paredit to start is to have the paredit cheatsheet on a piece of paper stuck to the side of your monitor. Or, you could also install paredit-menu which adds a menu item with tooltips showing the same examples the cheetsheet does.

0

I use M-C-(, which will immediately wrap the following sexp in parens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.