質問

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

役に立ちましたか?

解決

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

他のヒント

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.

M-(

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

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

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).

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.

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top