-
Notifications
You must be signed in to change notification settings - Fork 35
/
evil-cleverparens.el
2249 lines (2026 loc) · 81.5 KB
/
evil-cleverparens.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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; evil-cleverparens.el --- Evil friendly minor-mode for editing lisp.
;;
;; Copyright (C) 2015 Olli Piepponen
;;
;; Author: Olli Piepponen <[email protected]>
;; URL: https://github.com/emacs-evil/evil-cleverparens
;; Keywords: convenience, emulations
;; Version: 0.1.0
;; Package-Requires: ((evil "1.0") (paredit "1") (smartparens "1.6.1") (emacs "24.4") (dash "2.12.0"))
;;
;; This file is NOT part of GNU Emacs.
;;
;; This file is free software (MIT License)
;;; Commentary:
;; Use Vim/evil like modal editing with Lisp without screwing up the structure
;; of your code. Tries to offer useful alternatives for behavior which would
;; otherwise be destructive.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'evil)
(require 'paredit)
(require 'smartparens)
(require 'subr-x)
(require 'evil-cleverparens-text-objects)
(require 'evil-cleverparens-util)
;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgroup evil-cleverparens nil
"`evil-mode' for handling your parentheses with a mix of `smartparens' and `paredit'"
:group 'smartparens)
(defcustom evil-cleverparens-threshold 1500
"If the region being operated on is larger than this we cop out.
Quite a bit of work gets done to ensure the region being worked
is in an safe state, so this lets us sarifice safety for a snappy
editor on slower computers.
Even on a large computer you shouldn't set this too high or your
computer will freeze when copying large files out of Emacs.
This is a feature copied from `evil-smartparens'."
:group 'evil-cleverparens
:type 'number)
(defcustom evil-cleverparens-complete-parens-in-yanked-region nil
"Determines how to handle yanking a region containing
unbalanced expressions. If this value is non-nil, a yanked
region containing missing parentheses will include the missing
parens appended to the end."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-move-skip-delimiters t
"Determines whether parentheses and other delimiters are
considered symbols or not. The effect this has is that when
enabled (default), the by-symbol navigation commands happily
travel through these delimiters. This can be handy if you are
already used to using evil-cleverparenses parentheses navigation
commands."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-swap-move-by-word-and-symbol nil
"If true, the keys w, e, b, and ge will be bound to the
evil-cleverparens movement by symbol commands, and the regular
evil move by word commands will be bound to W, E, B and gE respectively."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-drag-ignore-lines nil
"Controls whether top-level sexps should be swapped with other
sexps or should lines be considered as well."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-drag-comment-blocks t
"Controls whether to drag by top-level comment block or a
single line when point is inside a multi-line top-level command
block."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-indent-afterwards t
"Controls whether to automatically indent when performing
commands that alter the structure of the surrounding code.
Enabled by default."
:group 'evil-cleverparens
:type 'boolean)
(defcustom evil-cleverparens-use-regular-insert nil
"Determines whether to use `evil-insert' or `evil-cp-insert'."
:group 'evil-cleverparens
:type 'boolean)
(defvar evil-cp--override nil
"Should the next command skip region checks?")
(defvar evil-cp--inserted-space-after-round-open nil
"Helper var for `evil-cp-insert'.")
(defcustom evil-cleverparens-use-additional-bindings t
"Should additional bindings be enabled."
:type 'boolean
:group 'evil-cleverparens)
(defcustom evil-cleverparens-use-additional-movement-keys t
"Should additional movement keys, mostly those related to
parentheses navigation, be enabled."
:type 'boolean
:group 'evil-cleverparens)
(defcustom evil-cleverparens-use-s-and-S t
"Should we bind s and S, thus shadowing any existing bindings
to these keys."
:type 'boolean
:group 'evil-cleverparens)
(defcustom evil-cleverparens-enabled-hook nil
"Called after `evil-cleverparens-mode' is turned on."
:type 'hook
:group 'evil-cleverparens)
(defcustom evil-cleverparens-disabled-hook nil
"Called after `evil-cleverparens-mode' is turned off."
:type 'hook
:group 'evil-cleverparens)
;;; Overriding ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun evil-cp--override ()
"Can be used as a predicate to determine if the next operation
should default to using regular evil functions. Resets the
`evil-cp--override' variable back to nil."
(prog1 (or evil-cp--override
(evil-cp--region-too-expensive-to-check))
(setq evil-cp--override nil)))
(defun evil-cp-override ()
"Calling this function will have evil-cleverparens default to
the regular evil equivalent of the next command that gets called."
(interactive)
(setq evil-cp--override t))
(defun evil-cp--region-too-expensive-to-check ()
"When it takes prohobitively long to check region we cop out.
This is a feature copied from `evil-smartparens'."
(when (region-active-p)
(> (abs (- (region-beginning) (region-end)))
evil-cleverparens-threshold)))
(defun evil-cp--fail ()
"Error out with a friendly message."
(error "Can't find a safe region to act on."))
;;; Evil Operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun evil-cp--splice-form ()
"Evil friendly version of splice that takes care of the
location of point, which also works for strings. Unlike
`sp-splice-sexp', this one doesn't perform clean-up for empty
forms."
(evil-cp--guard-point-inc-string
(-when-let (ok (sp-get-enclosing-sexp 1))
(if (equal ";" (sp-get ok :prefix))
(sp-get ok
(goto-char :beg)
(-when-let (enc (sp-get-enclosing-sexp 1))
(sp--unwrap-sexp enc t)))
(sp--unwrap-sexp ok t)))))
(defun evil-cp-delete-or-splice-region (beg end)
"Deletes a region of text by splicing parens and deleting other
chars. Does not splice strings unless the whole expression is
contained within the region."
(let ((chars-left (- end beg)))
(while (> chars-left 0)
(cond
((evil-cp--point-in-string-or-comment)
(if (sp-get-comment-bounds)
(let* ((ending (cdr (or (sp-get-comment-bounds)
(sp-get-quoted-string-bounds))))
(diff (- (min end ending) (point))))
(delete-char diff)
(setq chars-left (- chars-left diff)))
(progn
(evil-cp--splice-form)
(cl-decf chars-left))))
((evil-cp--looking-at-opening-p)
(let ((other-paren (evil-cp--matching-paren-pos)))
(if (< (point) other-paren end)
(let ((char-count (sp-get (sp-get-sexp) (- :end :beg))))
(delete-char char-count)
(setq chars-left (- chars-left char-count)))
(evil-cp--splice-form)
(cl-decf chars-left))))
((evil-cp--looking-at-string-opening-p)
(let ((other-quote (evil-cp--matching-paren-pos)))
(if (< (point) other-quote end)
(let ((char-count (sp-get (sp-get-string) (- :end :beg))))
(delete-char char-count)
(setq chars-left (- chars-left char-count)))
(if (= 1 (- end (point)))
(evil-cp--splice-form)
(forward-char))
(cl-decf chars-left))))
((evil-cp--looking-at-paren-p)
(evil-cp--splice-form)
(cl-decf chars-left))
((evil-cp--looking-at-string-delimiter-p)
(delete-char 1)
(cl-decf chars-left))
(t
(delete-char 1)
(cl-decf chars-left))))))
(evil-define-operator evil-cp-delete-char-or-splice (beg end type register yank-handler)
"Deletes the region by splicing parentheses / string delimiters
and deleting other characters. Can be overriden by
`evil-cp-override' in visual-mode."
:motion evil-forward-char
(interactive "<R><x>")
(cond ((or (evil-cp--balanced-block-p beg end)
(evil-cp--override))
(evil-delete-char beg end type register))
((eq type 'block)
(evil-cp--yank-rectangle beg end register yank-handler)
(evil-apply-on-block #'evil-cp-delete-or-splice-region beg end nil))
(t
(if evil-cleverparens-complete-parens-in-yanked-region
(evil-cp-yank beg end type register yank-handler)
(evil-yank beg end type register yank-handler))
(evil-cp-delete-or-splice-region beg end))))
(evil-define-operator evil-cp-delete-char-or-splice-backwards
(beg end type register yank-handler)
"Just like `evil-cp-delete-char-or-splice' but acts on the preceding character."
:motion evil-backward-char
(interactive "<R><x>")
(evil-cp-delete-char-or-splice beg end type register yank-handler))
(evil-define-command evil-cp-delete-backward-word ()
"Like `evil-delete-backward-word' with added behavior when
called in front of an opening delimiter, in which case the
enclosing form gets deleted."
(if (evil-cp--looking-at-any-opening-p (1- (point)))
(let ((enc (evil-cp--get-enclosing-bounds)))
(delete-region (car enc) (cdr enc)))
(evil-delete-backward-word)))
;; Originally from:
;; http://emacs.stackexchange.com/questions/777/closing-all-pending-parenthesis
(defun evil-cp--insert-missing-parentheses (backp)
"Calling this function in a buffer with unbalanced parentheses
will have the missing parentheses be inserted at the end of the
buffer if BACKP is nil and at the beginning if it is true."
(let ((closing nil))
;; fix for the degenerate case of nothing but closing parens
(when backp (save-excursion (insert " ")))
(save-excursion
(while (condition-case nil
(progn
(if (not backp)
(backward-up-list)
(forward-char)
(up-list)
(backward-char))
(let* ((syntax (syntax-after (point)))
(head (car syntax)))
(cond
((eq head (if backp 5 4))
(setq closing (cons (cdr syntax) closing)))
((member head '(7 8))
(setq closing (cons (char-after (point)) closing)))))
t)
((scan-error) nil))))
(when backp (goto-char (point-min)))
(apply #'insert (if backp closing (nreverse closing)))
;; see above
(when backp (delete-char 1))))
(defun evil-cp--close-missing-parens (text)
"Takes a text object and inserts missing parentheses first at
the end of the text, and then, if the expression is still
unbalanced, will insert the rest of the missing parens at the
beginning."
(with-temp-buffer
(insert text)
(goto-char (point-max))
(evil-cp--insert-missing-parentheses nil)
(when (not (sp-region-ok-p (point-min) (point-max)))
(goto-char (point-min))
(evil-cp--insert-missing-parentheses t))
(buffer-string)))
(defun evil-cp--region-has-unbalanced-string-p (beg end)
"Predicate for checking if a region contains an unbalanced string."
(not (cl-evenp (count-matches (sp--get-stringlike-regexp) beg end))))
(defun evil-cp--yank-characters
(beg end &optional register yank-handler add-parens-p)
"Yank characters while keeping parentheses balanced. The
optional ADD-PARENS-P arg determines how to handle the missing
parentheses: if nil, the non-balanced parens are
ignored. Otherwise they are added to the start/beginning of the
region."
(unless (evil-cp--region-has-unbalanced-string-p beg end)
(let ((text (string-trim (filter-buffer-substring beg end))))
(when (not (evil-cp--text-balanced-p text))
(setq text (evil-cp--close-missing-parens text)))
(when yank-handler
(setq text (propertize text 'yank-handler (list yank-handler))))
(when register
(evil-set-register register text))
(when evil-was-yanked-without-register
(evil-set-register ?0 text)) ; "0 register contains last yanked text
(unless (eq register ?_)
(kill-new text)))))
(defun evil-cp--yank-rectangle (beg end &optional register yank-handler)
"Saves the rectangle defined by BEG and END into the kill-ring,
while keeping the parentheses of the region balanced."
(let ((lines (list nil)))
(evil-apply-on-rectangle #'extract-rectangle-line beg end lines)
(setq lines (nreverse (cdr lines)))
(let* ((yank-handler (list (or yank-handler #'evil-yank-block-handler)
lines
nil
'evil-delete-yanked-rectangle))
(joined (mapconcat #'identity lines "\n"))
(text (if (evil-cp--text-balanced-p joined)
joined
(evil-cp--close-missing-parens joined)))
(text (propertize text 'yank-handler yank-handler)))
(when register (evil-set-register register text))
(when evil-was-yanked-without-register (evil-set-register ?0 text))
(unless (eq register ?_)
(kill-new text)))))
(defun evil-cp--swap-regions (r1 r2)
"Transposes the regions where R1 and R2 are cons-pairs where
the car is the start and the cdr is the end of each respective
region."
(when (and r1 r2)
(transpose-regions (cdr-safe r1)
(car-safe r1)
(cdr-safe r2)
(car-safe r2))))
(defun evil-cp--new-beginning (beg end &optional shrink)
"Return a new value for BEG if POINT is inside an empty sexp.
If SHRINK is t we try to shrink the region until it is balanced
by decrementing BEG.
Copied from `evil-smartparens'."
(if (not shrink)
(min beg
(if (sp-point-in-empty-sexp)
(evil-cp--point-after
(sp-backward-up-sexp))
(point-max)))
(let ((region (string-trim (buffer-substring-no-properties beg end))))
(unless (string-blank-p region)
(cond ((sp-point-in-empty-sexp)
;; expand region if we're in an empty sexp
(setf end (save-excursion (sp-backward-up-sexp) (point))))
;; reduce region if it's unbalanced due to selecting too much
(t (while (not (or (sp-region-ok-p beg end)
(= beg end)))
(cl-incf beg)))))
(when (= beg end)
(evil-cp--fail)))
beg))
(defun evil-cp--new-ending (beg end &optional no-error)
"Find the largest safe region delimited by BEG END.
Copied from `evil-smartparens'."
(let ((region (string-trim (buffer-substring-no-properties beg end))))
(unless (string-blank-p region)
(cond ((sp-point-in-empty-sexp)
;; expand region if we're in an empty sexp
(setf end (save-excursion (sp-up-sexp) (point))))
;; reduce region if it's unbalanced due to selecting too much
(t (while (not (or (sp-region-ok-p beg end)
(= beg end)))
(cl-decf end))))))
(if (and (not no-error) (= beg end))
(evil-cp--fail)
end))
(defun evil-cp--ignoring-yank (beg end type register yank-handler)
"This is a version of yank ignores unbalanced parentheses to
keep the region safe.
Copied from `evil-smartparens'."
(save-excursion
(condition-case nil
(let ((new-beg (evil-cp--new-beginning beg end))
(new-end (evil-cp--new-ending beg end)))
(if (and (= new-end end)
(= new-beg beg))
(evil-yank beg end type register yank-handler)
(evil-yank new-beg new-end type register yank-handler)))
(error (let* ((beg (evil-cp--new-beginning beg end :shrink))
(end (evil-cp--new-ending beg end)))
(evil-yank beg end type register yank-handler))))))
(defun evil-cp-yank-form-handler (text)
"Copy of `evil-yank-line-handler' that handles the lack of
newlines at the end of forms."
(let* ((pcount (or evil-paste-count 1))
(text (apply #'concat
(cdr (-interleave
(make-list pcount "\n")
(make-list pcount (string-trim text))))))
(opoint (point)))
(remove-list-of-text-properties
0 (length text) yank-excluded-properties text)
(cond
((eq this-command 'evil-paste-before)
(evil-move-beginning-of-line)
(evil-move-mark (point))
(insert text "\n")
(setq evil-last-paste
(list 'evil-paste-before
evil-paste-count
opoint
(mark t)
(point)))
(evil-set-marker ?\[ (mark))
(evil-set-marker ?\] (1- (point)))
(evil-exchange-point-and-mark)
(back-to-indentation))
((eq this-command 'evil-paste-after)
(evil-move-end-of-line)
(evil-move-mark (point))
(insert "\n")
(insert text)
(evil-set-marker ?\[ (1+ (mark)))
(evil-set-marker ?\] (1- (point)))
(setq evil-last-paste
(list 'evil-paste-after
evil-paste-count
opoint
(mark t)
(point)))
(evil-move-mark (1+ (mark t)))
(evil-exchange-point-and-mark)
(back-to-indentation))
(t
(insert text)))))
(evil-define-operator evil-cp-yank (beg end type register yank-handler)
"Saves the characters in motion into the kill-ring while
respecting parentheses."
:move-point nil
:repeat nil
(interactive "<R><x><y>")
(let ((evil-was-yanked-without-register
(and evil-was-yanked-without-register (not register)))
(safep (sp-region-ok-p beg end)))
(cond
;; block which is balanced
((and (eq type 'block) (evil-cp--balanced-block-p beg end))
(evil-yank-rectangle beg end register yank-handler))
;; unbalanced block, add parens
((and (eq type 'block)
evil-cleverparens-complete-parens-in-yanked-region)
(evil-cp--yank-rectangle beg end register yank-handler))
;; unbalanced block, ignoring parens
((eq type 'block)
(evil-cp--fail))
;; balanced region, or override
((or safep (evil-cp--override))
(evil-yank beg end type register yank-handler))
;; unbalanced line, fill parens
((and (eq type 'line)
evil-cleverparens-complete-parens-in-yanked-region)
(evil-cp--yank-characters beg end register
'evil-cp-yank-form-handler))
((eq type 'line)
(let ((beg (+ beg (save-excursion ; skip preceding whitespace
(beginning-of-line)
(sp-forward-whitespace t)))))
(evil-cp--ignoring-yank beg end type register
'evil-yank-line-handler)))
;; unbalanced, fill missing
(evil-cleverparens-complete-parens-in-yanked-region
(evil-cp--yank-characters beg end register yank-handler))
;; unbalanced, ignore extra
(t
(evil-cp--ignoring-yank beg end type register yank-handler)))))
(evil-define-operator evil-cp-yank-line (beg end type register)
"Acts like `paredit-copy-sexp-as-kill'."
:motion evil-line
:move-point nil
(interactive "<R><x>")
(cond
((evil-visual-state-p)
(let ((beg (line-beginning-position))
(end (if (eq type 'line)
(1- end)
(save-excursion
(goto-char end)
(line-end-position)))))
(evil-exit-visual-state)
(evil-cp--yank-characters beg end type register)))
;; Copied and modified from paredit.el
((paredit-in-string-p)
(save-excursion
(when (paredit-in-string-escape-p)
(backward-char))
(evil-yank-characters (point)
(min (line-end-position)
(cdr (paredit-string-start+end-points)))
register)))
((paredit-in-comment-p)
(evil-yank-characters (point) (line-end-position) register))
((save-excursion (paredit-skip-whitespace t (line-end-position))
(or (eolp) (eq (char-after) ?\; )))
(save-excursion
(if (paredit-in-char-p)
(backward-char)))
(evil-yank-characters (point) (line-end-position) register))
(t
(save-excursion
(if (paredit-in-char-p)
(backward-char 2))
(let ((beginning (point))
(eol (line-end-position)))
(let ((end-of-list-p (paredit-forward-sexps-to-kill beginning eol)))
(when end-of-list-p
(up-list)
(backward-char))
(evil-yank-characters
beginning
(cond
(kill-whole-line
(or (save-excursion
(paredit-skip-whitespace t)
(and (not (eq (char-after) ?\; ))
(point)))
(line-end-position)))
((and (not end-of-list-p)
(eq (line-end-position) eol))
eol)
(t
(point)))
register)))))))
(defun evil-cp--delete-characters (beg end)
"Deletes everything except unbalanced parentheses / string
delimiters in the region defined by BEG and END."
(let ((chars-left (- end beg))
(end (set-marker (make-marker) end)))
(goto-char beg)
(while (> chars-left 0)
(cond
((evil-cp--point-in-comment)
(let* ((ending (cdr (or (sp-get-comment-bounds)
(sp-get-quoted-string-bounds))))
(diff (- (min end ending) (point))))
(delete-char diff)
(setq chars-left (- chars-left diff))))
((evil-cp--looking-at-any-opening-p)
(let ((p (point))
(other-end (evil-cp--matching-paren-pos)))
;; matching paren is in the range of the command
(if (<= p other-end end)
;; 1+ makes the char-count inclusive
(let ((char-count (1+ (- other-end p))))
(delete-char char-count)
(setq chars-left (- chars-left char-count)))
(forward-char)
(cl-decf chars-left))))
((and (evil-cp--looking-at-escape-p) (< 1 chars-left))
(delete-char 2)
(cl-decf chars-left 2))
((and (evil-cp--looking-at-any-closing-p) (= chars-left 1))
(cl-decf chars-left))
((evil-cp--looking-at-any-closing-p)
(forward-char 1)
(cl-decf chars-left))
(t
(delete-char 1)
(cl-decf chars-left))))
(set-marker end nil)))
(defun evil-cp-first-non-blank-non-opening ()
"Like `evil-first-non-blank' but also skips opening parentheses
and string delimiters."
(interactive)
(evil-first-non-blank)
(while (and (evil-cp--looking-at-any-opening-p)
(<= (point) (line-end-position)))
(forward-char)))
(defun evil-cp--act-until-closing (beg end action)
"Do ACTION on all balanced expressions, starting at BEG.
Stop ACTION when the first unbalanced closing delimeter or eol is reached."
(goto-char beg)
(let ((endp nil)
(end (set-marker (make-marker) end)))
(while (not endp)
(cond
((<= end (point)) (setq endp t))
((evil-cp--looking-at-any-opening-p)
(let ((other-end (evil-cp--matching-paren-pos)))
;; matching paren is in the range of the command
(let ((char-count
(evil-cp--guard-point
(sp-get (sp-get-enclosing-sexp)
(- :end :beg)))))
(funcall action char-count))))
((evil-cp--looking-at-any-closing-p)
(setq endp t))
(t (funcall action 1))))
(set-marker end nil)))
(evil-define-operator evil-cp-delete-line (beg end type register yank-handler)
"Kills the balanced expressions on the line until the eol."
:motion nil
:keep-visual t
:move-point nil
;; TODO this could take a count, like `evil-delete-line'
(interactive "<R><x>")
(when (and (evil-visual-state-p) (eq type 'inclusive))
(let ((range (evil-expand
beg end
(if (and evil-respect-visual-line-mode visual-line-mode)
'screen-line 'line))))
(setq beg (car range)
end (cadr range)
type (evil-type range))))
(unless beg (setq beg (point)))
(unless end (setq end (line-end-position)))
(cond ((evil-cp-region-ok-p beg end)
(evil-delete-line beg end type register yank-handler))
((paredit-in-string-p)
(save-excursion
(when (paredit-in-string-escape-p)
(backward-char))
(let ((beg (point))
(end (min (line-end-position)
(cdr (paredit-string-start+end-points)))))
(evil-yank-characters beg end register)
(delete-region beg end))))
((paredit-in-comment-p)
(let ((beg (point))
(end (line-end-position)))
(evil-yank-characters beg end register)
(delete-region beg end)))
((evil-visual-state-p)
(progn (evil-force-normal-state)
(goto-char beg)
(evil-cp-delete-line beg end type register yank-handler)))
((and (evil-cp--looking-at-any-closing-p)
(evil-cp--looking-at-empty-form))
(evil-cp-delete-enclosing 1))
((save-excursion (paredit-skip-whitespace t (line-end-position))
(or (eolp) (eq (char-after) ?\; )))
(when (paredit-in-char-p) (backward-char))
;; `kill-line' inlined from from `simple.el'
;; this works but is not very evilidiomatic
(kill-region
(point)
(progn
(when (eobp) (evil-signal-at-eob))
(let ((end (save-excursion (end-of-visible-line) (point))))
(if (or (save-excursion
;; If trailing whitespace is visible,
;; don't treat it as nothing.
(unless show-trailing-whitespace
(skip-chars-forward " \t" end))
(= (point) end))
(and kill-whole-line (bolp)))
(forward-visible-line 1)
(goto-char end)))
(point))))
(t
(save-excursion
(when (paredit-in-char-p) (backward-char 2))
(let* ((beg (point))
(end (progn (evil-cp--act-until-closing beg end #'forward-char) (point))))
(evil-yank-characters beg end register)
(evil-cp--act-until-closing beg end #'delete-char))))))
(evil-define-operator evil-cp-delete (beg end type register yank-handler)
"A version of `evil-delete' that attempts to leave the region
it's acting on with balanced parentheses. The behavior of
kill-ring is determined by the
`evil-cleverparens-complete-parens-in-yanked-region' variable."
:move-point nil
(interactive "<R><x><y>")
(let ((safep (evil-cp-region-ok-p beg end)))
(cond ((or (= beg end)
(evil-cp--override)
(and (eq type 'block) (evil-cp--balanced-block-p beg end))
(and safep (not (eq type 'block))))
(evil-delete beg end type register yank-handler))
((eq type 'line)
(evil-cp-yank beg end type register yank-handler)
(goto-char beg)
(save-excursion
(evil-cp--delete-characters
(+ beg
(save-excursion ; skip preceding whitespace
(beginning-of-line)
(sp-forward-whitespace t)))
(if (save-excursion (goto-char end) (eobp))
end
(1- end))))
(when (and evil-cleverparens-indent-afterwards
(evil-cp--inside-any-form-p))
(save-excursion
(evil-cp--backward-up-list)
(indent-sexp)))
(if (save-excursion
(skip-chars-forward "\t ")
(evil-cp--looking-at-any-closing-p))
(when (save-excursion
(forward-line -1)
(not (evil-cp--comment-block?)))
(forward-line -1)
(join-line 1)
(forward-line 1))
(join-line 1)))
(t (evil-cp-yank beg end type register yank-handler)
(evil-cp--delete-characters beg end))))
(when (and (eq type 'line)
(called-interactively-p 'any))
(evil-cp-first-non-blank-non-opening)
(when (and (not evil-start-of-line)
evil-operator-start-col
;; Special exceptions to ever saving column:
(not (memq evil-this-motion '(evil-forward-word-begin
evil-forward-WORD-begin
evil-cp-forward-symbol-begin))))
(move-to-column evil-operator-start-col))))
(evil-define-operator evil-cp-change (beg end type register yank-handler delete-func)
"Call `evil-change' while keeping parentheses balanced."
(interactive "<R><x><y>")
(if (or (= beg end)
(evil-cp--override)
(and (eq type 'block) (evil-cp--balanced-block-p beg end))
(and (sp-region-ok-p beg end) (not (eq type 'block))))
(evil-change beg end type register yank-handler delete-func)
(let ((delete-func (or delete-func #'evil-cp-delete))
(nlines (1+ (- (line-number-at-pos end)
(line-number-at-pos beg))))
(opoint (save-excursion
(goto-char beg)
(line-beginning-position))))
(cond ((eq type 'line)
(save-excursion
(evil-cp--delete-characters
(+ beg
(save-excursion
(beginning-of-line)
(sp-forward-whitespace t)))
(1- end)))
(evil-cp-first-non-blank-non-opening)
(indent-according-to-mode)
(evil-insert 1))
((eq type 'block)
(evil-cp-delete beg end type register yank-handler)
(evil-insert 1 nlines))
(t
(funcall delete-func beg end type register yank-handler)
(evil-insert 1))))))
(evil-define-operator evil-cp-change-line (beg end type register yank-handler)
"Change to end of line while respecting parentheses."
:motion evil-end-of-line
(interactive "<R><x><y>")
(evil-cp-change beg end type register yank-handler #'evil-cp-delete-line))
(evil-define-operator evil-cp-change-whole-line (beg end type register yank-handler)
"Change whole line while respecting parentheses."
:motion evil-line
(interactive "<R><x>")
(if (sp-region-ok-p beg end)
(evil-change beg end type register yank-handler)
(evil-cp-first-non-blank-non-opening)
(evil-cp-change beg end type register yank-handler #'evil-cp-delete-line)))
;;; Movement ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(evil-define-motion evil-cp-forward-sexp (count)
"Motion for moving forward by a sexp via `sp-forward-sexp'."
:jump t
:type exclusive
(let ((count (or count 1)))
(when (evil-eolp) (forward-char))
(sp-forward-sexp count)))
(evil-define-motion evil-cp-backward-sexp (count)
"Motion for moving backwward by a sexp via `sp-backward-sexp'."
:jump t
:type exclusive
(let ((count (or count 1)))
(sp-backward-sexp count)))
(evil-define-motion evil-cp-beginning-of-defun (count)
"Motion for moving to the beginning of a defun (i.e. a top
level sexp)."
:jump t
:type inclusive
(let ((count (or count 1)))
(beginning-of-defun count)))
(evil-define-motion evil-cp-end-of-defun (count)
"Motion for moving to the end of a defun (i.e. a top level sexp)."
:jump t
:type inclusive
(let ((count (or count 1)))
(if (evil-cp--looking-at-closing-p)
(forward-char 2))
(end-of-defun count)
(if (looking-back "[ \t\n]" 1) (skip-chars-backward " \t\n"))
(backward-char 1)))
;; TODO: this looks ugly
(defun evil-cp--paren-navigation-helper (move-dir paren-side)
(let ((move-fn (cl-case move-dir
(:next 'forward-char)
(:previous 'backward-char)))
(the-end (cl-case move-dir
(:next 'point-max)
(:previous 'point-min)))
(paren-p (cl-case paren-side
(:opening 'evil-cp--looking-at-any-opening-p)
(:closing 'evil-cp--looking-at-any-closing-p)))
(pt-orig (point))
done-p)
(when (funcall paren-p) (funcall move-fn))
(while (not done-p)
(cond
((funcall paren-p)
(setq done-p t))
((= (point) (funcall the-end))
(goto-char pt-orig)
(setq done-p t))
(t
(funcall move-fn))))))
(evil-define-motion evil-cp-next-opening (count)
"Motion for moving to the next open parentheses."
:move-point nil
:type inclusive
(let ((count (or count 1)))
(dotimes (_ count)
(evil-cp--paren-navigation-helper :next :opening))))
(evil-define-motion evil-cp-previous-opening (count)
"Motion for moving to the previous open parentheses."
:move-point nil
:type inclusive
(let ((count (or count 1)))
(dotimes (_ count)
(evil-cp--paren-navigation-helper :previous :opening))))
(evil-define-motion evil-cp-next-closing (count)
"Motion for moving to the next closing parentheses."
:move-point nil
:type inclusive
(let ((count (or count 1)))
(dotimes (_ count)
(evil-cp--paren-navigation-helper :next :closing))))
(evil-define-motion evil-cp-previous-closing (count)
"Motion for moving to the previous closing parentheses."
:move-point nil
:type inclusive
(let ((count (or count 1)))
(dotimes (_ count)
(evil-cp--paren-navigation-helper :previous :closing))))
(evil-define-motion evil-cp-backward-up-sexp (count)
"Motion for moving backward up to the previous level of
parentheses. Basically just wraps `sp-backward-up-sexp' as an
evil-motion."
:move-point nil
:type inclusive
(let ((count (or count 1)))
;; for some reason calling `sp-backward-up-sexp' with a large `count'
;; doesn't move the point at all
(dotimes (_ count)
(sp-backward-up-sexp))))
(evil-define-motion evil-cp-up-sexp (count)
"Motion for moving up to the previous level of parentheses.
The same as `sp-up-sexp', but leaves the point on top of the
closing paren."
:move-point nil
:type inclusive
(let ((count (or count 1))
success)
(when (evil-cp--looking-at-any-closing-p) (forward-char))
(dotimes (_ count)
(and (sp-up-sexp) (setq success t)))
(and success (backward-char))))
(defun evil-cp--strict-forward-symbol (&optional count)
"Move forward COUNT symbols.
Like `forward-symbol' but stricter. Skips sexp prefixes."
(let ((mmode-prefix (cdr (assoc major-mode sp-sexp-prefix))))
(cond ((< 0 count)
(forward-symbol 1)
(if (and (eq (car mmode-prefix) 'regexp)
(looking-at-p (evil-cp--get-opening-regexp))
(looking-back (cadr mmode-prefix) 1))
(evil-cp--strict-forward-symbol count)
(evil-cp--strict-forward-symbol (1- count))))
((> 0 count)
(forward-symbol -1)
(if (and (eq (car mmode-prefix) 'regexp)
(looking-at-p (string-join (list (cadr mmode-prefix)
(evil-cp--get-opening-regexp)))))
(evil-cp--strict-forward-symbol count)
(evil-cp--strict-forward-symbol (1+ count)))))))
(defun forward-evil-cp-symbol (&optional count)
"Move forward COUNT \"WORDS\".
Moves point COUNT WORDS forward or (- COUNT) WORDS backward if
COUNT is negative. Point is placed after the end of the WORD (if
forward) or at the first character of the WORD (if backward). A
WORD is a sequence of non-whitespace characters
'[^\\n\\r\\t\\f ]', or an empty line matching ^$."
(evil-forward-nearest
count
#'(lambda (&optional cnt)
(let ((pnt (point)))
(evil-cp--strict-forward-symbol cnt)
(if (= pnt (point)) cnt 0)))
#'forward-evil-empty-line))
;; incomplete
(defun forward-evil-cp-word (&optional count)
"TODO: This is exactly the same as `forward-evil-word' and not used
for anything right now. It would be nice to skip delimiters on
the small-word movements as well but I couldn't figure out how to
change this to make it work. Pull requests welcome."
(evil-forward-nearest
count
#'(lambda (&optional cnt)
(let ((word-separating-categories evil-cjk-word-separating-categories)
(word-combining-categories evil-cjk-word-combining-categories)
(pnt (point)))
(forward-word cnt)
(if (= pnt (point)) cnt 0)))
#'(lambda (&optional cnt)