Friday, March 18, 2011

The Secret of Paredit

As I mentioned previously, I celebrated the arrival of Guile 2.0 by installing Geiser. At the same time I installed Paredit. "It's fabulous;" "It's awesome;" "No lisper, regardless of preferred dialect, should even think of editing without it," they said. With praise like that, who was I to refuse? Except that I found it really hard to use. Entering code was easy and it was nice how it kept all the parentheses and quotes balanced. The problem was with making changes.

I'd start with code like

(sis)
(boom)
(bah!)

then suddenly realize that I don't want to execute that code unless do_cheers is true. I'd need to edit the code to be

(when (do_cheers)
  (sis)
  (boom)
  (bah!))

How to do that? First, I'd do what I always do: I'd put the cursor in front of (sis) and type ( but I'd end up with

()(sis)
(boom)
(bah!)

Not what I wanted. I'd check the cheat sheet and discover M-(. That sounded like just what I needed but I'd end up with

(when (do_cheers)
  (sis))
(boom)
(bah!)

If I tried to delete that last parenthesis on the second line, the cursor would just skip over it. Finally, I'd just toggle off paredit-mode and fix things up by hand.

By now you're thinking, "Geez, what a luser! Use slurpage already." I did see that on the cheat sheet but I didn't really focus on it because I read it as barfage and snarfage and to me and my coevals, snarf & barf has the specific meaning of cut and paste and that, obviously, was not what I was looking for. Eventually, I went looking on the web and found this very helpful post by Aaron Feng that showed me what to do: just put my cursor in front of (sis), type (when (do_cheers), and then C-) three times to suck the three forms into the when form. Simple when you know the secret.

I also found this informative slide show that discusses many of the features of Paredit not on the cheat sheet. It turns out, for example, that you can kill a single parenthesis.

The take away from this post is that you should learn about slurpage and barfage if you're going to use Paredit. It makes correcting your code easy and using Paredit a pleasure instead of a chore.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete