forked from drym-org/symex.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymex-transformations.el
861 lines (756 loc) · 27.1 KB
/
symex-transformations.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
;;; symex-transformations.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:
;; Standard mutative operations to be performed on symexes.
;;; Code:
(require 'cl-lib)
(require 'paredit)
(require 'lispy)
(require 'evil)
(require 'evil-surround)
(require 'evil-cleverparens) ;; really only need cp-textobjects here
(require 'symex-primitives)
(require 'symex-utils)
(require 'symex-traversals)
(require 'symex-interop)
;;;;;;;;;;;;;;;;;;;;;;;
;;; TRANSFORMATIONS ;;;
;;;;;;;;;;;;;;;;;;;;;;;
(defun symex-delete (count)
"Delete COUNT symexes."
(interactive "p")
(let ((last-command nil) ; see symex-yank re: last-command
(start (point))
(end (symex--get-end-point count)))
(kill-region start end))
(cond ((or (symex--current-line-empty-p) ; ^<>$
(save-excursion (evil-last-non-blank) ; (<>$
(lispy-left-p))
(looking-at-p "\n")) ; (abc <>
(symex--join-to-next))
((save-excursion (back-to-indentation) ; ^<>)
(forward-char)
(lispy-right-p))
;; Cases 2 and 3 in issue #18
;; if the deleted symex is preceded by a comment line
;; or if the preceding symex is followed by a comment
;; on the same line, then don't attempt to join lines
(let ((original-position (point)))
(when (symex--go-backward)
(let ((previous-symex-end-pos (symex--get-end-point 1)))
(unless (symex--intervening-comment-line-p previous-symex-end-pos
original-position)
(goto-char previous-symex-end-pos)
;; ensure that there isn't a comment on the
;; preceding line before joining lines
(unless (condition-case nil
(progn (evil-find-char 1 ?\;)
t)
(error nil))
(symex--join-to-match lispy-right)
(symex--adjust-point)))))))
((save-excursion (forward-char) ; ... <>)
(lispy-right-p))
(symex--go-backward))
(t (symex--go-forward)))
(symex-select-nearest)
(symex-tidy))
(defun symex-delete-backwards (count)
"Delete COUNT symexes backwards."
(interactive "p")
(dotimes (_ count)
(when (symex--go-backward)
(symex-delete 1))))
(defun symex-delete-remaining ()
"Delete remaining symexes at this level."
(interactive)
(let ((count (symex--remaining-length)))
(symex-delete count)))
(defun symex-change (count)
"Change COUNT symexes."
(interactive "p")
(let ((start (point))
(end (symex--get-end-point count)))
(kill-region start end))
(symex-enter-lowest))
(defun symex-change-remaining ()
"Change remaining symexes at this level."
(interactive)
(let ((count (symex--remaining-length)))
(symex-change count)))
(defun symex--clear ()
"Helper to clear contents of symex."
(cond ((symex-opening-round-p)
(apply #'evil-delete (evil-inner-paren)))
((symex-opening-square-p)
(apply #'evil-delete (evil-inner-bracket)))
((symex-opening-curly-p)
(apply #'evil-delete (evil-inner-curly)))
((symex-string-p)
(apply #'evil-delete (evil-inner-double-quote)))
(t (kill-sexp))))
(defun symex-replace ()
"Replace contents of symex."
(interactive)
(symex--clear)
(when (or (symex-form-p) (symex-string-p))
(forward-char))
(symex-enter-lowest))
(defun symex-clear ()
"Clear contents of symex."
(interactive)
(symex--clear)
(symex-select-nearest)
(symex-tidy))
(defun symex--emit-backward ()
"Emit backward."
(when (and (lispy-left-p)
(not (symex-empty-list-p)))
(save-excursion
(symex--go-up) ; need to be inside the symex to emit and capture
(paredit-backward-barf-sexp 1))
(symex--go-forward)
(when (symex-empty-list-p)
(fixup-whitespace)
(re-search-forward lispy-left)
(symex--go-down))))
(defun symex-emit-backward (count)
"Emit backward, COUNT times."
(interactive "p")
(dotimes (_ count)
(symex--emit-backward)))
(defun symex--emit-forward ()
"Emit forward."
(when (and (lispy-left-p)
(not (symex-empty-list-p)))
(save-excursion
(symex--go-up) ; need to be inside the symex to emit and capture
(paredit-forward-barf-sexp 1))
(when (symex-empty-list-p)
(symex--go-forward)
(fixup-whitespace)
(re-search-backward lispy-left))))
(defun symex-emit-forward (count)
"Emit forward, COUNT times."
(interactive "p")
(dotimes (_ count)
(symex--emit-forward)))
(defun symex--capture-backward ()
"Capture from behind."
(when (lispy-left-p)
(if (symex-empty-list-p)
(forward-char)
(symex--go-up)) ; need to be inside the symex to emit and capture
;; paredit captures 1 ((|2 3)) -> (1 (2 3))
;; but we don't want to in this case since point indicates the
;; inner symex, which cannot capture, rather than the outer
;; one. Just a note for the future.
(paredit-backward-slurp-sexp 1)
(fixup-whitespace)
(symex--go-down)))
(defun symex-capture-backward (count)
"Capture from behind, COUNT times."
(interactive "p")
(dotimes (_ count)
(symex--capture-backward))
(symex-tidy))
(defun symex--capture-forward ()
"Capture from the front."
(when (lispy-left-p)
(save-excursion
(if (symex-empty-list-p)
(forward-char)
(symex--go-up)) ; need to be inside the symex to emit and capture
(lispy-forward-slurp-sexp 1))))
(defun symex-capture-forward (count)
"Capture from the front, COUNT times."
(interactive "p")
(dotimes (_ count)
(symex--capture-forward)))
(defun symex-split ()
"Split symex into two."
(interactive)
(paredit-split-sexp)
(forward-char)
(symex-select-nearest)
(symex-tidy))
(defun symex--join ()
"Merge symexes at the same level."
(save-excursion
(symex--go-forward)
(paredit-join-sexps)))
(defun symex-join (count)
"Merge COUNT symexes at the same level."
(interactive "p")
(dotimes (_ count)
(symex--join)))
(defun symex-join-lines (count)
"Join COUNT lines inside symex."
(interactive "p")
(dotimes (_ count)
(symex--join-lines)))
(defun symex-join-lines-backwards (count)
"Join COUNT lines backwards inside symex."
(interactive "p")
(dotimes (_ count)
(symex--join-lines t)))
(defun symex--join-lines (&optional backwards)
"Join lines inside symex.
If BACKWARDS is true, then joins current symex to previous one, otherwise,
by default, joins next symex to current one."
(let ((original-column (current-column)))
(if backwards
(progn (evil-previous-line)
(if (symex--current-line-empty-p)
(evil-join (line-beginning-position)
(1+ (line-beginning-position)))
(evil-join (line-beginning-position)
(line-end-position))))
(save-excursion (forward-sexp)
(evil-join (line-beginning-position)
(line-end-position))))
(unless (= (current-column)
original-column)
(forward-char)))
(symex-tidy))
(defun symex-yank (count)
"Yank (copy) COUNT symexes."
(interactive "p")
;; we set `last-command` here to avoid appending to the kill ring
;; when it's a delete followed by a yank. We want to treat each as
;; independent entries in the kill ring
(let ((last-command nil))
(let ((start (point))
(end (symex--get-end-point count)))
(copy-region-as-kill start end))))
(defun symex-yank-remaining ()
"Yank (copy) remaining symexes at this level."
(interactive)
(let ((count (symex--remaining-length)))
(symex-yank count)))
(defun symex--paste-before ()
"Paste before symex."
(interactive)
(let ((extra-to-append
(cond ((or (and (symex--point-at-indentation-p)
(not (bolp)))
(save-excursion (forward-sexp)
(eolp)))
"\n")
(t " "))))
(save-excursion
(save-excursion
(evil-paste-before nil nil)
(when evil-move-cursor-back
(forward-char))
(insert extra-to-append))
(symex--go-forward)
(symex-tidy))
(symex-tidy)))
(defun symex-paste-before (count)
"Paste before symex, COUNT times."
(interactive "p")
(setq this-command 'evil-paste-before)
(symex--with-undo-collapse
(dotimes (_ count)
(symex--paste-before))))
(defun symex--paste-after ()
"Paste after symex."
(interactive)
(let ((extra-to-prepend
(cond ((or (and (symex--point-at-indentation-p)
(not (bolp)))
(save-excursion (forward-sexp)
(eolp)))
"\n")
(t " "))))
(save-excursion
(forward-sexp)
(insert extra-to-prepend)
(evil-paste-before nil nil))
(symex--go-forward)
(symex-tidy)))
(defun symex-paste-after (count)
"Paste after symex, COUNT times."
(interactive "p")
(setq this-command 'evil-paste-after)
(symex--with-undo-collapse
(dotimes (_ count)
(symex--paste-after))))
(defun symex-open-line-after ()
"Open new line after symex."
(interactive)
(forward-sexp)
(newline-and-indent)
(symex-enter-lowest))
(defun symex-open-line-before ()
"Open new line before symex."
(interactive)
(newline-and-indent)
(evil-previous-line)
(indent-according-to-mode)
(evil-move-end-of-line)
(unless (or (symex--current-line-empty-p)
(save-excursion (backward-char)
(lispy-left-p)))
(insert " "))
(symex-enter-lowest))
(defun symex-append-after ()
"Append after symex (instead of vim's default of line)."
(interactive)
(forward-sexp) ; selected symexes will have the cursor on the starting paren
(insert " ")
(symex-enter-lowest))
(defun symex-insert-before ()
"Insert before symex (instead of vim's default at the start of line)."
(interactive)
(insert " ")
(backward-char)
(symex-enter-lowest))
(defun symex-insert-at-beginning ()
"Insert at beginning of symex."
(interactive)
(when (or (lispy-left-p)
(symex-string-p))
(forward-char))
(symex-enter-lowest))
(defun symex-insert-at-end ()
"Insert at end of symex."
(interactive)
(if (or (lispy-left-p)
(symex-string-p))
(progn (forward-sexp)
(backward-char))
(forward-sexp))
(symex-enter-lowest))
(defun symex-create (type)
"Create new symex (list).
New list delimiters are determined by the TYPE."
(save-excursion
(cond ((equal type 'round)
(insert "()"))
((equal type 'square)
(insert "[]"))
((equal type 'curly)
(insert "{}"))
((equal type 'angled)
(insert "<>"))))
(symex-tidy))
(defun symex-create-round ()
"Create new symex with round delimiters."
(interactive)
(symex-create 'round))
(defun symex-create-square ()
"Create new symex with square delimiters."
(interactive)
(symex-create 'square))
(defun symex-create-curly ()
"Create new symex with curly delimiters."
(interactive)
(symex-create 'curly))
(defun symex-create-angled ()
"Create new symex with angled delimiters."
(interactive)
(symex-create 'angled))
(defun symex-insert-newline (count)
"Insert COUNT newlines before symex."
(interactive "p")
(newline-and-indent count)
(symex-tidy))
(defun symex-append-newline (count)
"Append COUNT newlines after symex."
(interactive "p")
(save-excursion
(forward-sexp)
(newline-and-indent count)
(symex-tidy)))
(defun symex-swallow ()
"Swallow the head of the symex.
This consumes the head of the symex, putting the rest of its contents
in the parent symex."
(interactive)
(save-excursion
(symex--go-up)
(symex--go-forward)
(paredit-splice-sexp-killing-backward))
(symex-tidy))
(defun symex-swallow-tail ()
"Swallow the tail of the symex.
This consumes the tail of the symex, putting the head
in the parent symex."
(interactive)
(save-excursion
(symex--go-up)
(symex--go-forward)
(paredit-splice-sexp-killing-forward)
(symex--go-backward))
(symex-tidy))
(defun symex-splice ()
"Splice or 'clip' symex.
If the symex is a nested list, this operation eliminates the symex,
putting its contents in the parent symex. If the symex is an atom,
then no action is taken."
(interactive)
(when (or (lispy-left-p) (symex-string-p))
(if (or (symex-empty-list-p)
(symex-empty-string-p))
(symex-delete 1)
(save-excursion
(evil-surround-delete (char-after))
(symex--go-down)
(symex-tidy)))))
(defun symex-wrap-round ()
"Wrap with ()."
(interactive)
(paredit-wrap-round)
(symex--go-down))
(defun symex-wrap-square ()
"Wrap with []."
(interactive)
(paredit-wrap-square)
(symex--go-down))
(defun symex-wrap-curly ()
"Wrap with {}."
(interactive)
(paredit-wrap-curly)
(evil-find-char-backward nil 123))
(defun symex-wrap-angled ()
"Wrap with <>."
(interactive)
(paredit-wrap-angled)
(evil-find-char-backward nil 60))
(defun symex-wrap ()
"Wrap with containing symex."
(interactive)
(symex-wrap-round)
(symex-insert-at-beginning))
(defun symex-wrap-and-append ()
"Wrap with containing symex and append."
(interactive)
(symex-wrap-round)
(symex-insert-at-end))
(defun symex--shift-forward ()
"Move symex forward in current tree level."
(forward-sexp)
(condition-case nil
(progn (transpose-sexps 1)
(backward-sexp)
t)
(error (backward-sexp)
nil)))
(defun symex-shift-forward (count)
"Move symex forward COUNT times in current tree level."
(interactive "p")
(dotimes (_ count)
(symex--shift-forward)))
(defun symex-shift-forward-most ()
"Move symex backward COUNT times in current tree level."
(interactive)
(let ((col (current-column))
(row (line-number-at-pos))
(result t))
(while (and result
(or (= col (current-column))
(= row (line-number-at-pos))))
(setq result (symex--shift-forward)))
(unless (or (= col (current-column))
(= row (line-number-at-pos)))
(symex--shift-backward))))
(defun symex--shift-backward ()
"Move symex backward in current tree level."
(let ((move (symex--go-backward)))
(when move
(symex--shift-forward)
(symex--go-backward)
t)))
(defun symex-shift-backward (count)
"Move symex backward COUNT times in current tree level."
(interactive "p")
(dotimes (_ count) (symex--shift-backward)))
(defun symex-shift-backward-most ()
"Move symex backward COUNT times in current tree level."
(interactive)
(let ((col (current-column))
(row (line-number-at-pos))
(result t))
(while (and result
(or (= col (current-column))
(= row (line-number-at-pos))))
(setq result (symex--shift-backward)))
(unless (or (= col (current-column))
(= row (line-number-at-pos)))
(symex--shift-forward))))
(defun symex-change-delimiter ()
"Change delimiter enclosing current symex, e.g. round -> square brackets."
(interactive)
(if (or (lispy-left-p) (symex-string-p))
(evil-surround-change (following-char))
(let ((bounds (bounds-of-thing-at-point 'sexp)))
(evil-surround-region (car bounds) (cdr bounds) 'inclusive 40))))
(defun symex-comment (count)
"Comment out COUNT symexes."
(interactive "p")
(mark-sexp count)
(comment-dwim nil))
(defun symex-comment-remaining ()
"Comment out remaining symexes at this level."
(interactive)
(let ((count (symex--remaining-length)))
(symex-comment count)))
(defun symex--delete-prefix-helper (prefix-list)
"Delete the first prefix in PREFIX-LIST that matches the text at point.
The index of the deleted prefix is returned, or a negative value if no prefix
matched."
(if (null prefix-list)
-9999
(let ((prefix (car prefix-list))
(remaining-prefixes (cdr prefix-list)))
(if (looking-at-p prefix)
(progn
(delete-char (length prefix))
0)
(1+ (symex--delete-prefix-helper remaining-prefixes))))))
(defun symex--delete-prefix (prefix-list)
"Delete the longest prefix in PREFIX-LIST that matches the text at point.
The index of the deleted prefix is returned, or a negative value if no prefix
matched."
;; sort the prefix list by length so that we match the longest prefix
;; when there are many possible matches, e.g. ,@(...) should match
;; ,@ rather than ,
(let* ((sorted-prefix-list (sort (cl-copy-list prefix-list)
(lambda (a b)
(> (length a) (length b)))))
(sorted-idx (symex--delete-prefix-helper sorted-prefix-list)))
(if (>= sorted-idx 0)
;; since the sorted list is a permutation of the original
;; prefix list, map the returned sorted index to the
;; corresponding index on the original list
(cl-position (elt sorted-prefix-list sorted-idx)
prefix-list)
sorted-idx)))
(defun symex--insert-prefix (prefix-list index)
"Insert a prefix at point selected from a user-customized list of prefixes.
Inserts the prefix at position INDEX in PREFIX-LIST into the buffer at
point. If INDEX exceeds the length of the prefix list, then nothing
is inserted. This has the effect, when used in succession to
symex--delete-prefix, of returning the content to an unprefixed state
after it has cycled once through the prefixes."
(unless (>= index (length prefix-list))
(insert (elt prefix-list (max 0 index)))))
(defun symex--cycle-prefix (prefix-list index)
"Cycle through (and insert into the buffer) prefixes in PREFIX-LIST.
If INDEX is provided, insert the prefix at INDEX instead of cycling."
(save-excursion
(let ((deleted-index (symex--delete-prefix prefix-list)))
(if index
(symex--insert-prefix prefix-list index)
(symex--insert-prefix prefix-list (1+ deleted-index))))))
(defun symex-cycle-quote (index)
"Cycle through configured quoting prefixes in `symex-quote-prefix-list`.
If an INDEX is provided, then this replaces the existing prefix (if
any) with the prefix at position INDEX in the quoting prefix list
\(`symex-quote-prefix-list`). If no index is specified, this replaces
the existing prefix (if any) with the one that comes next in the
prefix list. If it goes past the end of the prefix list, the prefix is
removed entirely, restarting the cycle.
The INDEX begins at the 1st position in the prefix list, so the first
prefix in the list should be indicated with 1 rather than 0. This is
because 0 has a different meaning in symex mode, and is an unusual
prefix argument to use in Emacs functions. 1-indexed behavior is also
the more natural choice here in any case."
(interactive "P")
(symex--cycle-prefix symex-quote-prefix-list (and index (1- index)))
(symex-tidy))
(defun symex-cycle-unquote (index)
"Cycle through configured quoting prefixes in `symex-unquote-prefix-list`.
If an INDEX is provided, then this replaces the existing prefix (if
any) with the prefix at position INDEX in the unquoting prefix list
\(`symex-unquote-prefix-list`). If no index is specified, this
replaces the existing prefix (if any) with the one that comes next in
the prefix list. If it goes past the end of the prefix list, the
prefix is removed entirely, restarting the cycle.
The INDEX begins at the 1st position in the prefix list, so the first
prefix in the list should be indicated with 1 rather than 0. This is
because 0 has a different meaning in symex mode, and is an unusual
prefix argument to use in Emacs functions. 1-indexed behavior is also
the more natural choice here in any case."
(interactive "P")
(symex--cycle-prefix symex-unquote-prefix-list (and index (1- index)))
(symex-tidy))
(defun symex-remove-quoting-level ()
"Remove any quoting prefix at point, if present.
This removes either quoting or unquoting prefixes, and removes up to one
layer of quoting."
(interactive)
(unless (>= (symex--delete-prefix symex-quote-prefix-list)
0)
(symex--delete-prefix symex-unquote-prefix-list))
(symex-tidy))
(defun symex-add-quoting-level ()
"Add a quoting level."
(interactive)
(insert "'")
(symex-tidy))
(defun symex-quasiquote ()
"Quasiquote symex."
(interactive)
(insert "`")
(symex-tidy))
(defun symex-escape-quote ()
"Escape quote in quoted symex."
(interactive)
(insert ",")
(symex-tidy))
(defun symex-tidy ()
"Auto-indent symex and fix any whitespace."
(interactive)
(fixup-whitespace)
(when (save-excursion (looking-at-p "[[:space:]]"))
(forward-char))
(condition-case nil
(save-excursion
(forward-sexp)
(fixup-whitespace))
(error nil))
(condition-case err
(save-excursion
(apply #'evil-indent
(seq-take (evil-cp-a-form 1)
2)))
(error (message "[Symex] symex-tidy: suppressed error %S" err)
(let ((start (point))
(end (save-excursion (forward-sexp) (point))))
;; maybe we should just always use this instead
(save-excursion
(apply #'evil-indent
(list start end))))))
(symex-select-nearest))
(cl-defun symex--transform-in-isolation (traversal side-effect &key pre-traversal)
"Transform a symex in a temporary buffer and replace the original with it.
First traverses using PRE-TRAVERSAL if non-nil, then traverses using
TRAVERSAL and performs SIDE-EFFECT at each step. Note that the side
effect is not performed during the pre-traversal."
(kill-sexp 1)
(kill-new
(let (original-syntax-table)
;; In using a temp buffer to do the transformation here, we need to
;; ensure that it uses the syntax table of the original buffer, since
;; otherwise it doesn't necessarily treat characters the same way
;; as the original buffer does, separating, for example, characters like
;; `?` and `#` from the rest of the symbol during recursive indentation.
;;
;; The with-temp-buffer macro doesn't see the original syntax table
;; when it is lexically defined here, not sure why. Defining a
;; lexical scope here and then setting it dynamically via `setq`
;; seems to work
(setq original-syntax-table (syntax-table))
(with-temp-buffer
(with-syntax-table original-syntax-table
(yank)
(goto-char 0)
(symex-execute-traversal pre-traversal)
;; do it once first since it will be executed as a side-effect
;; _after_ each step in the traversal
(condition-case nil
(funcall side-effect)
(error nil))
(condition-case nil
(symex--do-while-traversing
side-effect
traversal)
(error nil))
(buffer-string)))))
(save-excursion (yank))
(symex-tidy))
(defun symex-tidy-proper ()
"Properly tidy things up.
This operates on the subtree indicated by the selection, rather than
on the entire tree. Ordinarily this would require 'remembering' the
initial location on the tree while traversing and collapsing the
subexpressions, a feature (memory) that is absent in the Symex DSL.
But the present implementation gets around the need for memory by
copying the subtree into a temporary buffer and indenting it as a
complete tree, and then replacing the original symex with the
indented version from the temporary buffer.
When memory is added to the DSL, this would probably have a simpler
implementation."
(interactive)
(symex--transform-in-isolation
symex--traversal-postorder-in-tree
#'symex-tidy
:pre-traversal (symex-traversal (circuit symex--traversal-preorder-in-tree))))
(defun symex-collapse ()
"Collapse a symex to a single line.
This operates on the subtree indicated by the selection, rather than
on the entire tree. Ordinarily this would require 'remembering' the
initial location on the tree while traversing and collapsing the
subexpressions, a feature (memory) that is absent in the Symex DSL.
But the present implementation gets around the need for memory by
copying the subtree into a temporary buffer and collapsing it as a
complete tree, and then replacing the original symex with the
collapsed version from the temporary buffer.
When memory is added to the DSL, this would probably have a simpler
implementation."
(interactive)
(symex--transform-in-isolation
(symex-traversal
(precaution symex--traversal-postorder-in-tree
(afterwards (not (at root)))))
(apply-partially #'symex--join-lines t)
:pre-traversal (symex-traversal (circuit symex--traversal-preorder-in-tree))))
(defun symex-collapse-remaining ()
"Collapse the remaining symexes to the current line."
(interactive)
(save-excursion
(let ((line (line-number-at-pos)))
(symex--do-while-traversing (lambda ()
(unless (= line (line-number-at-pos))
(symex--join-lines t)))
(symex-make-move 1 0)))))
(defun symex-unfurl-remaining ()
"Unfurl the remaining symexes so they each occupy separate lines."
(interactive)
(save-excursion
(symex--go-forward)
;; do it once first since it will be executed as a side-effect
;; _after_ each step in the traversal
(symex-insert-newline 1)
(symex--do-while-traversing (apply-partially #'symex-insert-newline 1)
(symex-make-move 1 0))))
(defun symex-tidy-remaining ()
"Tidy the remaining symexes."
(interactive)
(save-excursion
;; do it once first since it will be executed as a side-effect
;; _after_ each step in the traversal
(symex-tidy)
(symex--do-while-traversing #'symex-tidy
(symex-make-move 1 0)))
;; not sure why this nearest selection is needed, but eventually we may
;; just want to wrap every command with this at the end anyway, so,
;; in retrospect from that point, this wouldn't hurt
(symex-select-nearest))
(defun symex-unfurl ()
"Unfurl the constituent symexes so they each occupy separate lines."
(interactive)
(save-excursion
(symex--go-up)
(symex-unfurl-remaining)))
(provide 'symex-transformations)
;;; symex-transformations.el ends here