-
Notifications
You must be signed in to change notification settings - Fork 2
/
paredit-outline.txt
209 lines (148 loc) · 6.37 KB
/
paredit-outline.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# paredit (parenthetical deftness)
## 1. Intro
Some programming languages, most famously Lisp, require heavy use of
parentheses. A good editor will provide a few tools to help you with
this. A great editor (such as Emacs with paredit installed) will go
further, which is what we'll explore here.
## 2. Insertion and Deletion
Let's write some Emacs Lisp.
C-x C-f init.el
M-x paredit-mode
down to line 2
insert: (defun awesome-p () t)
Now you'll notice that as you type open parens, the closing ones are
inserted for you. This is no real surprise, as it's something many
other editors provide. But we're just getting started.
point to right after paredit-
C-k
The next thing you'll notice is that deleting works differently. When
you press C-k to kill a line, the whole line doesn't always get
deleted. Paredit is doing its best to make sure that the structure of
your code remains valid. It knows you probably didn't want to actually
kill the whole line, just everything up to the closing paren.
insert: numbers (list 1 (+ 2 \n 3 4))
point to before (+
C-k
If the rest of the line contains an expression that spans many lines,
it will remove the whole thing instead of just up to the end of the
line.
Press )
And pressing close paren won't insert one, but just jumps to the close
of the current expression instead.
backspace through 1 and list
Pressing backspace will pass through the parens and only delete elements.
backspace again
But once a pair of parens is empty, then deleting one of them deletes
the other.
C-b
insert: [1 2 3]
So far everything that works with parentheses also applies to other
matched characters. Double-quotes, square brackets, and curly braces
(if your language uses them) all behave similarly.
C-e
insert "hey look: [unmatched)!"
Of course, paredit knows that these rules don't apply when you're
inside a string or a comment, so it doesn't try to enforce its
structure there.
## 3. Workarounds
Now these features are helpful, but they assume that the file you're
working with has a valid structure. For various reasons, that's not
always true. Let's see what happens when the rules are violated.
Open file invalid.el
The first thing to note is that paredit won't even activate if it
detects unbalanced characters in a file you're opening. So let's fix
it and activate paredit manually.
Insert paren in front of message
M-x paredit-mode
You can still get a document in a bad state if you don't watch
out. Killing a region with C-w does not enforce the rules, so remember
that when you use it, you're stepping outside the bounds of paredit
and should be a little more careful.
Mark (message and C-w it
C-e
See how the end of the line is highlighted differently? That's
show-paren-mode indicating that things are unbalanced. It's not part
of paredit, but it's definitely worth enabling.
Go to the beginning of the string.
C-q (
Another thing that's helpful to remember is that Emacs lets you prefix
a key with C-q to insert it literally rather than activating whatever
the key is bound to. Use this if you need to insert a lone paren to
fix things. You can also prefix backspace with C-u to force it.
## 4. Wrangling (depth-changing)
open rooms.clj
point at right before mire.rooms
M-(
Mark "declare rooms"
Press (
You can wrap the next expression in parens with M-(. If you want to
wrap multiple expressions, simply mark them and then hit (.
TODO: mention M-r?
Back to mire.rooms
M-s
If you're inside a list and want to merge it with its parent, use M-s
to splice.
Back to "declare rooms"; point in rooms
C-S-]
C-S-0
Of course we can't neglect to mention the imaginatively named "barf"
and "slurp" commands. If you're inside a list, you can "barf" the last
expression out of the list. The reverse operation "slurps" the next
element outside into the list. Yum!
C-S-[
C-S-9
Barfing and slurping have forward and backward variations.
TODO: chart showing alternatives
If you're running Emacs in a terminal, it may not be capable of
entering these keys, so you can use the alternate arrow-key versions.
Point to front of rooms
M-S-s
M-S-j
This is pretty straightforward; just use M-S-s and M-S-j to split and
join lists.
## 5. Other Languages
Open concourse.js
M-x paredit-mode
[1, 2, 3].map()
Point over 2
C-k
insert: 4
While paredit-mode was designed to work with Lisp languages, it can be
used in others as well. It works with most modes based on cc-mode
(including espresso-mode for Javascript), but there are problems with
js2-mode. It also works in ruby-mode. The list modification commands
don't expect list elements to need commas between them, so this is not
ideal. Other than that, the functionality it provides is quite
helpful.
## 6. Installation and Enabling
If you use the Emacs Starter Kit, you've got Paredit already
installed. Otherwise hit up the Emacs Lisp Package Archive, or ELPA
for a copy. ELPA can be downloaded from http://tromey.com/elpa.
[Show installation]
You'll still need to choose which modes to enable it for though. Add
hooks for that:
(add-hook 'emacs-lisp-mode-hook 'enable-paredit-mode)
(add-hook 'scheme-mode-hook 'enable-paredit-mode)
(add-hook 'clojure-mode-hook 'enable-paredit-mode)
(add-hook 'ruby-mode-hook 'esk-paredit-nonlisp)
(add-hook 'espresso-mode-hook 'esk-paredit-nonlisp)
Add a hook for each mode for which you want paredit activated, and
you're good to go.
The esk-paredit-nonlisp function customizes and enables paredit for
non-Lisp languages. It's included in the Starter Kit, but if you want
to use it elsewhere, it looks like this:
[TODO: This only works with my patched paredit! Get it upstream or
rework this section.]
(defun esk-paredit-nonlisp ()
"Turn on paredit mode for non-lisps."
(set (make-local-variable paredit-space-delimiter-chars) (list ?\"))
(paredit-mode +1))
## 7. Conclusion
Hopefully now you've picked up some techniques that will make you more
effective in your coding.
If you're interested in learning more about Emacs or Lisp, check out
my PeepCode screencasts, each available for $9:
[Show each URL in a browser.]
Meet Emacs - http://peepcode.com/products/meet-emacs
Functional Programming with Clojure - http://peepcode.com/products/functional-programming-with-clojure
Thanks for watching!