-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsymex-evil.el
332 lines (305 loc) · 11.4 KB
/
symex-evil.el
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;;; symex-evil.el --- An evil way to edit Lisp symbolic expressions as trees -*- lexical-binding: t -*-
;; URL: https://github.com/countvajhula/symex.el
;; This program is "part of the world," in the sense described at
;; https://drym.org. From your perspective, this is no different than
;; MIT or BSD or other such "liberal" licenses that you may be
;; familiar with, that is to say, you are free to do whatever you like
;; with this program. It is much more than BSD or MIT, however, in
;; that it isn't a license at all but an idea about the world and how
;; economic systems could be set up so that everyone wins. Learn more
;; at drym.org.
;;
;; This work transcends traditional legal and economic systems, but
;; for the purposes of any such systems within which you may need to
;; operate:
;;
;; This is free and unencumbered software released into the public domain.
;; The authors relinquish any copyright claims on this work.
;;
;;; Commentary:
;; Evil modal frontend to symex.
;;; Code:
(require 'evil)
(require 'cl-lib)
(require 'symex-evil-support)
(require 'symex-ui)
(require 'symex-misc)
(require 'symex-transformations)
(require 'symex-interop)
(require 'symex-utils)
(defvar symex-editing-mode-map (make-sparse-keymap))
(define-minor-mode symex-editing-mode
"Minor mode to modulate keybindings in symex evil state."
:lighter " symex"
:keymap symex-editing-mode-map)
(evil-define-state symex
"Symex state."
:tag " <λ> "
:message "-- SYMEX --"
:enable (normal)
:exit-hook (symex-exit-mode))
(defun symex-evil-repeat-start-recording-advice (&rest _)
"Prepare the current command for recording the repetition.
This function is meant to advise `evil-repeat-pre-hook' which
starts recording a repeation during the `pre-command-hook', but
only records a repition when in normal or visual state. This
calls `evil-repeat-start' if the buffer is currently in symex
state."
(when evil-local-mode
(let ((repeat-type (evil-repeat-type this-command nil)))
;; `evil-repeat-pre-hook' has several paths that it can take and only one
;; of them results in `evil-repeat-start' being called. We only need to
;; account for the conditions which would have started recording repeat
;; information if the buffer were in normal state. That is to say, we only
;; start recording if:
;;
;; 1. The current command has a `:repeat' property that is non-`nil' and
;; not set to force abort a repitition
;; 2. The current command is not a mouse event
;; 3. The buffer is currently in symex state
(when (and repeat-type
(not (evil-repeat-force-abort-p repeat-type))
(not (evil-mouse-events-p (this-command-keys)))
(evil-symex-state-p))
(evil-repeat-start)))))
(defun symex-evil-repeat-stop-recording-advice (&rest _)
"Finish recording of repeat information for the current command.
This function is meant to advise `evil-repeat-post-hook' which
cleans up a recording during the `post-command-hook', but assumes
no recording was started unless the buffer is in normal or visual
state. This calls `evil-repeat-stop' if the buffer is currently
in symex state as well."
(when (and evil-local-mode evil-recording-repeat)
(let ((repeat-type (evil-repeat-type this-command t)))
;; We only need to call `evil-repeat-stop' if recording would have been
;; started by `symex-evil-repeat-start-recording-advice'. If recording was
;; started for any other reason, then it will already have been turned off
;; by `post-command-hook'. That is to say, we only stop recording if:
;;
;; 1. The current command has a `:repeat' property that is non-`nil' and
;; not set to force abort a repitition
;; 2. The current command is not a mouse event
;; 3. The buffer is currently in symex state
(when (and repeat-type
(not (evil-repeat-force-abort-p repeat-type))
(not (evil-mouse-events-p (this-command-keys)))
(evil-symex-state-p))
(evil-repeat-stop)))))
(defun symex-evil-repeat-preserve-state-advice (orig-fun &rest args)
"Return to symex state if necessary after calling ORIG-FUN.
This function is meant to advise `evil-repeat' which sets the
buffer to normal state after repeating a command. This first
checks whether the buffer is starting in symex state and, if so,
returns to symex after invoking ORIG-FUN with ARGS."
(let ((symex-state-p (evil-symex-state-p)))
(unwind-protect
(apply orig-fun args)
(when symex-state-p
(evil-symex-state)))))
(defun symex--evil-scroll-down ()
"Scroll down half a page.
This is needed because symex alters scroll margins upon mode entry to
ensure that the symex is always in focus. For some reason this winds
up causing evil's `evil-scroll-down` to scroll all the way to the
bottom of the buffer. So we temporarily override the scroll margin in
executing this command to get the expected behavior."
(interactive)
(let ((scroll-margin 0))
(evil-scroll-down nil)))
;; TODO: others that could accept a count argument:
;; simple insert/append
(defvar symex--evil-keyspec
'(("h" . symex-go-backward)
("j" . symex-go-down)
("k" . symex-go-up)
("l" . symex-go-forward)
("gj" . symex-next-visual-line)
("gk" . symex-previous-visual-line)
("(" . symex-create-round)
("[" . symex-create-square)
(")" . symex-wrap-round)
("]" . symex-wrap-square)
("C-'" . symex-cycle-quote)
("C-," . symex-cycle-unquote)
("`" . symex-add-quoting-level)
("C-`" . symex-remove-quoting-level)
("f" . symex-traverse-forward)
("b" . symex-traverse-backward)
("C-f" . symex-traverse-forward-more)
("C-b" . symex-traverse-backward-more)
("F" . symex-traverse-forward-skip)
("B" . symex-traverse-backward-skip)
("{" . symex-leap-backward)
("}" . symex-leap-forward)
("M-{" . symex-soar-backward)
("M-}" . symex-soar-forward)
("C-k" . symex-climb-branch)
("C-j" . symex-descend-branch)
("y" . symex-yank)
("Y" . symex-yank-remaining)
("p" . symex-paste-after)
("P" . symex-paste-before)
("x" . symex-delete)
("X" . symex-delete-backwards)
("D" . symex-delete-remaining)
("c" . symex-change)
("C" . symex-change-remaining)
("C--" . symex-clear)
("s" . symex-replace)
("S" . symex-change-delimiter)
("H" . symex-shift-backward)
("L" . symex-shift-forward)
("M-H" . symex-shift-backward-most)
("M-L" . symex-shift-forward-most)
("K" . paredit-raise-sexp) ; revisit kb
("C-S-j" . symex-emit-backward)
("C-(" . symex-capture-backward)
("C-S-h" . symex-capture-backward)
("C-{" . symex-emit-backward)
("C-S-l" . symex-capture-forward)
("C-}" . symex-emit-forward)
("C-S-k" . symex-emit-forward)
("C-)" . symex-capture-forward)
("z" . symex-swallow)
("Z" . symex-swallow-tail)
("e" . symex-evaluate)
("E" . symex-evaluate-remaining)
("C-M-e" . symex-evaluate-pretty)
("d" . symex-evaluate-definition)
("M-e" . symex-eval-recursive)
("T" . symex-evaluate-thunk)
("t" . symex-switch-to-scratch-buffer)
("M" . symex-switch-to-messages-buffer)
("r" . symex-repl)
("R" . symex-run)
("|" . symex-split)
("&" . symex-join)
("-" . symex-splice)
("o" . symex-open-line-after)
("O" . symex-open-line-before)
(">" . symex-insert-newline)
("<" . symex-join-lines-backwards)
("C->" . symex-append-newline)
("C-<" . symex-join-lines)
("C-S-o" . symex-append-newline)
("J" . symex-join-lines)
("M-J" . symex-collapse)
("M-<" . symex-collapse)
("M->" . symex-unfurl)
("C-M-<" . symex-collapse-remaining)
("C-M->" . symex-unfurl-remaining)
("0" . symex-goto-first)
("M-h" . symex-goto-first)
("$" . symex-goto-last)
("M-l" . symex-goto-last)
("M-j" . symex-goto-lowest)
("M-k" . symex-goto-highest)
("=" . symex-tidy)
("<tab>" . symex-tidy)
("C-=" . symex-tidy-remaining)
("C-<tab>" . symex-tidy-remaining)
("M-=" . symex-tidy-proper)
("M-<tab>" . symex-tidy-proper)
("A" . symex-append-after)
("a" . symex-insert-at-end)
("i" . symex-insert-at-beginning)
("I" . symex-insert-before)
("w" . symex-wrap)
("W" . symex-wrap-and-append)
("C-d" . symex--evil-scroll-down)
(";" . symex-comment)
("M-;" . symex-comment-remaining)
("C-;" . symex-eval-print) ; weird pre-offset (in both)
("s-;" . symex-evaluate)
("H-h" . symex--toggle-highlight) ; treats visual as distinct mode
("C-?" . symex-describe)
("<return>" . symex-enter-lower)
("<escape>" . symex-escape-higher))
"Key specification for symex evil state.")
(defvar symex--user-evil-keyspec nil
"User key specification overrides for symex evil state.")
(defvar symex--evil-repeatable-commands
'(paredit-raise-sexp
symex-add-quoting-level
symex-append-newline
symex-capture-backward
symex-capture-forward
symex-change
symex-clear
symex-comment
symex-comment-remaining
symex-create-round
symex-create-square
symex-cycle-quote
symex-cycle-unquote
symex-delete
symex-delete-backwards
symex-delete-remaining
symex-emit-backward
symex-emit-forward
symex-insert-newline
symex-join
symex-join-lines
symex-join-lines-backwards
symex-open-line-after
symex-open-line-before
symex-paste-after
symex-paste-before
symex-remove-quoting-level
symex-shift-backward
symex-shift-backward-most
symex-shift-forward
symex-shift-forward-most
symex-splice
symex-split
symex-swallow
symex-swallow-tail
symex-tidy
symex-wrap
symex-wrap-round
symex-wrap-square
symex-append-after
symex-change-delimiter
symex-change-remaining
symex-collapse
symex-collapse-remaining
symex-insert-at-beginning
symex-insert-at-end
symex-insert-before
symex-open-line-after
symex-open-line-before
symex-tidy-proper
symex-tidy-remaining
symex-unfurl
symex-unfurl-remaining
symex-wrap-and-append)
"Commands which should have their `:repeat' property set to t.")
(defun symex-evil-initialize ()
"Initialize evil modal interface."
(let ((keyspec (symex--combine-alists symex--user-evil-keyspec
symex--evil-keyspec)))
(symex--define-evil-keys-from-spec keyspec
symex-editing-mode-map))
(unless (symex--rigpa-enabled-p)
;; without rigpa (which would handle this for us), we need to
;; manage the editing minor mode and ensure that it is active
;; while in symex evil state and inactive when in other states
(add-hook 'evil-symex-state-exit-hook #'symex-disable-editing-minor-mode))
(advice-add 'evil-repeat-pre-hook
:after #'symex-evil-repeat-start-recording-advice)
(advice-add 'evil-repeat-post-hook
:after #'symex-evil-repeat-stop-recording-advice)
(advice-add 'evil-repeat
:around #'symex-evil-repeat-preserve-state-advice)
(dolist (fn symex--evil-repeatable-commands)
(evil-add-command-properties fn :repeat t)))
(defun symex-enable-editing-minor-mode ()
"Enable symex minor mode."
(symex-editing-mode 1))
(defun symex-disable-editing-minor-mode ()
"Disable symex minor mode."
(unless (member evil-next-state '(emacslike normallike))
;; these are "internal" state transitions, used in e.g. symex-evaluate
(symex-editing-mode -1)))
(provide 'symex-evil)
;;; symex-evil.el ends here