-
Notifications
You must be signed in to change notification settings - Fork 23
/
lispyville.el
2153 lines (1978 loc) · 81.6 KB
/
lispyville.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
;;; lispyville.el --- A minor mode for integrating evil with lispy. -*- lexical-binding: t -*-
;; Author: Fox Kiester <noct@openmailbox.org>
;; URL: https://github.com/noctuid/lispyville
;; Created: March 03, 2016
;; Keywords: vim, evil, lispy, lisp, parentheses
;; Package-Requires: ((lispy "0") (evil "1.2.12") (cl-lib "0.5") (emacs "24.4"))
;; Version: 0.1
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; For more information see the README in the online repository.
;;; Code:
(require 'evil)
(require 'lispy)
(require 'cl-lib)
;; * Settings
(defgroup lispyville nil
"Provides a minor mode to integrate evil with lispy."
:group 'lispy
:prefix "lispyville-")
(defcustom lispyville-key-theme '(operators c-w)
"Determines the key theme initially set by lispyville.
Changing this variable will only have an effect by itself when done prior to
lispyville being loaded. Otherwise, `lispyville-set-key-theme' should be
called afterwards with no arguments. The user can also not set this variable
at all and simply use `lispyville-set-key-theme' with an argument after
lispyville has been loaded."
:group 'lispyville
:type
'(repeat :tag "Key Themes"
(choice
(const
:tag "Safe versions of evil operators."
operators)
(const
:tag "Safe version of `evil-delete-backward-word'."
c-w)
(const
:tag "Alternative to `evil-indent' that acts like `lispy-tab'."
prettify)
(const
:tag "Extra motions similar to those provided by cleverparens."
additional-movement)
(const
:tag "Safe version of `evil-commentary' for toggling comments."
commentary)
(const
:tag "Slurp/barf keybindings in the style of cleverparens."
slurp/barf-cp)
(const
:tag "Slurp/barf keybindings in the style of lispy."
slurp/barf-lispy)
(const
:tag "Extra commands similar to those provided by cleverparens."
additional)
(const
:tag "Extra insert commands similar to those provided by cleverparens."
additional-insert)
(const
:tag "Commands similar to those provided by \
vim-sexp-mappings-for-regular-people."
arrows)
(const
:tag "Command to enter normal state and cancel an active region."
escape)
(const
:tag "Lispy commands for marking."
mark)
(const
:tag "Lispy commands for marking that enter special instead of visual \
state."
mark-special)
(const
:tag "Commands for toggling between special and visual state and \
canceling a region."
mark-toggle)
(const
:tag "Insert a space when inserting after an opening delimiter or \
before a closing one."
insert))))
(defcustom lispyville-barf-stay-with-closing nil
"When non-nil, stay with the closing delimiter when barfing.
Specifically, this applies for `lispyville-barf' and `lispyville-<'
when barfing would move the delimiter behind the point. This option
only has an effect if `lispyville-commands-put-into-special' is nil."
:group 'lispyville
:type 'boolean)
(defvaralias 'lispyville-preferred-state 'lispyville-preferred-lispy-state)
(defcustom lispyville-preferred-lispy-state 'insert
"The preferred evil state for insertion and using lispy.
This is used by any command that should enter special to determine the correct
state."
:group 'lispyville
:type '(choice
(const :tag "Use insert state to get into special." insert)
(const :tag "Use emacs state to get into special." emacs)))
(defcustom lispyville-motions-put-into-special nil
"Applicable motions will enter insert or emacs state.
This will only happen when they are not called with an operator or in visual
mode."
:group 'lispyville
:type 'boolean)
(defcustom lispyville-commands-put-into-special nil
"Applicable commands will enter insert or emacs state."
:group 'lispyville
:type 'boolean)
(defcustom lispyville-no-alter-lispy-options nil
"Whether to to change certain lispy options when entering `lispyville-mode'.
By default, lispyville will set `lispy-safe-delte', `lispy-safe-copy',
`lispy-safe-delete', and `lispy-safe-actions-no-pull-delimiters-into-comments'
to t. To prevent lispyville from changing any lispy options, set this variable
to a non-nil value."
:group 'lispyville
:type 'boolean)
(defcustom lispyville-insert-states '(insert emacs hybrid iedit-insert)
"Insertion states that lispy special can be used from."
:group 'lispyville
:type 'list)
(defface lispyville-special-face
'((t :foreground "#aa4456"))
"Face for lispyville special mode line indicator."
:group 'lispyville)
(with-eval-after-load 'evil-surround
(add-to-list 'evil-surround-operator-alist '(lispyville-change . change))
(add-to-list 'evil-surround-operator-alist '(lispyville-delete . delete)))
;; https://github.com/noctuid/lispyville/pull/26
(when (boundp 'evil-change-commands)
(add-to-list 'evil-change-commands #'lispyville-change))
;;;###autoload
(define-minor-mode lispyville-mode
"A minor mode for integrating evil with lispy."
:lighter " LYVLE"
:keymap (make-sparse-keymap)
(when lispyville-mode
(evil-normalize-keymaps)
(unless lispyville-no-alter-lispy-options
(setq lispy-safe-delete t
lispy-safe-copy t
lispy-safe-paste t
lispy-safe-actions-no-pull-delimiters-into-comments t))))
;; * Helpers
(defun lispyville--in-string-p ()
"Return the bounds of the string at the point or nil.
Unlike `lispy--in-string-p', |\"\" is not considered to be inside the string."
(let ((string-bounds (lispy--bounds-string)))
(and string-bounds
(not (= (car string-bounds) (point)))
string-bounds)))
(defun lispyville--at-left-p ()
"Return whether the point is before an opening delimiter.
Opening delimiters inside strings and comments are ignored."
(and (not (lispy--in-comment-p))
(not (lispyville--in-string-p))
(or (looking-at lispy-left)
(looking-at "\""))
(not (looking-back "\\\\" (- (point) 2)))))
(defun lispyville--at-right-p ()
"Return whether the point is before a closing delimiter.
Closing delimiters inside strings and comments are ignored."
(and (not (lispy--in-comment-p))
(or (and (looking-at lispy-right)
(not (lispyville--in-string-p)))
(and (looking-at "\"")
(lispyville--in-string-p)))
(not (looking-back "\\\\" (- (point) 2)))))
(defun lispyville--after-left-p ()
"Return whether the point is after an opening delimiters.
Opening delimiters inside strings and comments are ignored."
(unless (bobp)
(save-excursion
(backward-char)
(lispyville--at-left-p))))
(defun lispyville--after-delimiter-p ()
"Return whether the point is after an opening or closing delimiter."
(unless (bobp)
(let ((lispy-delimiters (concat (substring lispy-right 0 -1)
"\""
(substring lispy-left 1))))
(and (not (lispy--in-string-or-comment-p))
(lispy-looking-back lispy-delimiters)
(save-excursion
(backward-char)
(not (looking-back "\\\\" (- (point) 2))))))))
(defun lispyville--yank-text (text &optional type register yank-handler)
"Like `evil-yank-characters' but takes TEXT directly instead of a region.
Depending on TYPE, this will behave like `evil-yank-characters',
`evil-yank-lines', or `evil-yank-rectangle'. REGISTER and YANK-HANDLER have the
same effect."
(let ((lines (split-string text "\n")))
(setq yank-handler
(cl-case type
(block (list (or yank-handler #'evil-yank-block-handler)
lines
t
'evil-delete-yanked-rectangle))
(line (list (or yank-handler
#'evil-yank-line-handler)
nil
t))
(t (when yank-handler
(list yank-handler)))))
;; ensure newline at end of text for line type
(when (and (eq type 'line)
(or (zerop (length text))
(/= (aref text (1- (length text))) ?\n)))
(setq text (concat text "\n")))
(when yank-handler
(setq 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 lispyville--safe-string (beg end &optional delete)
"Return the text between from BEG to END excluding unmatched delimiters.
When DELETE is non-nil, delete the safe part of the region."
(let ((safe-regions (lispy--find-safe-regions beg end))
safe-strings)
(dolist (safe-region safe-regions)
;; NOTE evil uses `filter-buffer-substring'
(push (filter-buffer-substring (car safe-region) (cdr safe-region))
safe-strings)
(when delete
(delete-region (car safe-region) (cdr safe-region))))
(apply #'concat safe-strings)))
;; TODO splice strings that aren't double quotes or have multiple delimiters on
;; either side
(defun lispyville--splice-string ()
"Splice a string at the point."
(cond ((and (not (bobp))
(save-excursion
(backward-char)
(lispy--in-string-p)))
(let ((right-quote (point)))
(lispy--exit-string)
(save-excursion
(goto-char right-quote)
(delete-char 1))
(delete-char 1)))
(t
(delete-char 1)
(while (progn (re-search-forward "\"" nil t)
(looking-back "\\\\\"" (- (point) 2))))
(delete-char -1))))
(defun lispyville--splice ()
"Like `lispy-splice' but also splice strings."
(save-excursion
(if (looking-at "\"")
(lispyville--splice-string)
(when (looking-at lispy-right)
(forward-char))
(lispy-splice 1))))
(defun lispyville--safe-string-splice (beg end)
"Like `lispyville--safe-string' except always deletes.
Instead of ignoring unmatched delimiters between BEG and END, this will splice
them."
(let ((beg-marker (save-excursion
(goto-char beg)
(point-marker)))
(end-marker (save-excursion
(goto-char end)
(point-marker)))
(unmatched-positions (lispy--find-unmatched-delimiters beg end))
safe-text)
(evil-exit-visual-state)
(save-excursion
(dolist (pos unmatched-positions)
(goto-char pos)
(lispyville--splice))
(setq safe-text (filter-buffer-substring beg-marker end-marker))
(delete-region beg-marker end-marker)
safe-text)))
(defvar lispyville--safe-strings nil
"Temporary list of safe strings for `lispyville--safe-string-rectangle'.")
(defun lispyville--safe-string-rectangle
(beg end &optional delete)
"Like `lispyville--safe-string' but operates on a rectangle/block.
BEG and END correspond to the start and beginning of the rectangle. DELETE is
passed to `lispyville--safe-string'. If DELETE is the symbol 'splice,
`lispyville--safe-string-splice' will be used instead."
(setq lispyville--safe-strings nil)
(apply-on-rectangle
(lambda (beg end delete)
(setq beg (save-excursion (move-to-column beg) (point)))
(setq end (save-excursion (move-to-column end) (point)))
(push (if (eq delete 'splice)
(lispyville--safe-string-splice beg end)
(lispyville--safe-string beg end delete))
lispyville--safe-strings))
beg end delete)
(apply #'concat
(lispy-interleave
"\n"
(nreverse lispyville--safe-strings))))
(defun lispyville--safe-manipulate
(beg end &optional type delete yank register yank-handler)
"Return the text from BEG to END excluding unmatched delimiters.
When DELETE is non-nil, delete the safe part of the region. When DELETE is the
symbol 'splice, splice unmatched delimiters instead of ignoring them. When YANK
is non-nil, also copy the safe text to the kill ring. REGISTER and YANK-HANDLER
are passed to `lispyville--yank-text' when YANK is non-nil."
;; special 'splice value
(let ((safe-string
(cl-case type
(block (lispyville--safe-string-rectangle beg end delete))
(t (if (eq delete 'splice)
(lispyville--safe-string-splice beg end)
(lispyville--safe-string beg end delete))))))
(when yank
(lispyville--yank-text safe-string type register yank-handler))
safe-string))
(defun lispyville--state-transition (&optional to-special)
"Transition from lispy special to evil normal state.
If the region is active, transition to visual state. If TO-SPECIAL is non-nil,
transition to lispy special instead. This function will ensure that the point is
correctly on or after a closing delimiter at the end of the transition."
(let ((regionp (region-active-p))
(mark (mark t)))
(cond (to-special
(cond (regionp)
((looking-at lispy-right)
(forward-char))
((not (or (lispy-left-p)
(lispy-right-p)))
(lispy-right 1)))
(evil-change-state lispyville-preferred-lispy-state)
(when regionp
(set-mark mark)))
(t
(let* ((point-last-p (> (point) mark))
evil-move-cursor-back)
;; if region active, this prevents from entering insert after exiting
;; visual state
(evil-normal-state nil)
(when (or (and regionp point-last-p)
(lispy-right-p))
(backward-char))
(when regionp
(if point-last-p
(evil-visual-char mark (point))
(evil-visual-char (1- mark) (point)))))))))
(defun lispyville--maybe-enter-special (&optional command)
"Potentially enter insert or emacs state to get into special.
The behavior depends on the value of `lispyville-motions-put-into-special'. If
COMMAND is non-nil, the behavior depends on the value of
`lispyville-commands-put-into-special' instead.
`lispyville-preferred-lispy-state' is used to determine whether to enter emacs
or insert state."
(when (and (if command
lispyville-commands-put-into-special
lispyville-motions-put-into-special)
(not (or (evil-operator-state-p)
(evil-visual-state-p))))
(lispyville--state-transition t)))
;; * Operators
(evil-define-operator lispyville-yank (beg end type register yank-handler)
"Like `evil-yank' but will not copy unmatched delimiters."
:move-point nil
:repeat nil
(interactive "<R><x><y>")
(let ((evil-was-yanked-without-register
(and evil-was-yanked-without-register (not register))))
(lispyville--safe-manipulate beg end type nil t register yank-handler)))
;; NOTE: Y, D, and C don't work with counts in evil by default already
(evil-define-operator lispyville-yank-line (beg end type register yank-handler)
"Copy to the end of the line ignoring unmatched delimiters.
This is not like the default `evil-yank-line'."
:motion nil
:keep-visual t
(interactive "<R><x>")
(let ((beg (or beg (point)))
(end (or end beg)))
;; act linewise in Visual state
(when (evil-visual-state-p)
(unless (memq type '(line block))
(let ((range (evil-expand beg end 'line)))
(setq beg (evil-range-beginning range)
end (evil-range-end range)
type (evil-type range))))
(evil-exit-visual-state))
(cond ((eq type 'block)
(let ((temporary-goal-column most-positive-fixnum)
(last-command 'next-line))
(lispyville-yank beg end type register yank-handler)))
((eq type 'line)
(lispyville-yank beg end type register yank-handler))
(t
(lispyville-yank beg (line-end-position)
type register yank-handler)))))
(defun lispyville--maybe-newline-and-indent ()
"Skip closing delimiters and call `newline-and-indent' with a trailing sexp.
Only add a newline if there is a sexp after the closing delimiters. Don't move
the point."
(save-excursion
(while (progn (forward-char)
(lispyville--at-right-p)))
(unless (looking-at "[[:space:]]*$")
(newline-and-indent))))
(defvar lispyville--cc nil
"Whether `lispyville-delete' has been called from `lispyville-change'.")
(evil-define-operator lispyville-delete (beg end type register yank-handler)
"Like `evil-delete' but will not delete/copy unmatched delimiters."
(interactive "<R><x><y>")
(unless register
(let ((text (filter-buffer-substring beg end)))
(unless (string-match-p "\n" text)
;; set the small delete register
(evil-set-register ?- (lispyville--safe-manipulate beg end)))))
(let ((evil-was-yanked-without-register nil))
(cond
((and (eq type 'line)
(not lispyville--cc))
;; at end of buffer delete previous newline since no newline after
;; current line
(when (and
;; check that there is no final newline
(= end (point-max))
(or (= beg end)
(/= (char-before end) ?\n))
;; there must be a previous newline
(/= beg (point-min))
(= (char-before beg) ?\n))
(setq beg (1- beg)))
(lispyville--safe-manipulate beg end type t t register yank-handler)
(when (and (lispyville--at-right-p)
(lispy-looking-back "^[[:space:]]*"))
(cond ((save-excursion
(forward-line -1)
(goto-char (line-end-position))
(lispy--in-comment-p))
(lispy--indent-for-tab)
;; don't pull delimiter(s) into comment
(lispyville--maybe-newline-and-indent))
(t
(join-line)
(while (progn (forward-char)
(lispyville--at-right-p)))
(newline-and-indent))))
(when (lispyville--at-left-p)
;; remove any extra whitespace (e.g. unmatched opening delimeter;
;; pulling up indented code)
(save-excursion
(lispyville-first-non-blank)
(delete-region (point)
(progn
(skip-chars-forward "[:space:]")
(point)))))
(when (called-interactively-p 'any)
(evil-first-non-blank)))
(t
(lispyville--safe-manipulate beg end type t t register yank-handler)))))
(evil-define-operator lispyville-delete-line
(beg end type register yank-handler)
"Like `evil-delete-line' but will not delete/copy unmatched delimiters."
:motion nil
:keep-visual t
(interactive "<R><x>")
(let* ((beg (or beg (point)))
(end (or end beg)))
;; act linewise in Visual state
(when (evil-visual-state-p)
(unless (memq type '(line block))
(let ((range (evil-expand beg end 'line)))
(setq beg (evil-range-beginning range)
end (evil-range-end range)
type (evil-type range))))
(evil-exit-visual-state))
(cond ((eq type 'block)
(let ((temporary-goal-column most-positive-fixnum)
(last-command 'next-line))
(lispyville-delete beg end 'block register yank-handler)))
((eq type 'line)
(lispyville-delete beg end type register yank-handler)
(when (called-interactively-p 'any)
(evil-first-non-blank)))
(t
(lispyville-delete beg (line-end-position)
type register yank-handler)))))
(evil-define-operator lispyville-delete-whole-line
(beg end type register yank-handler)
"Like `evil-delete-whole-line' but will not delete/copy unmatched delimiters."
:motion evil-line
(interactive "<R><x>")
(lispyville-delete beg end type register yank-handler))
(evil-define-operator lispyville-delete-char-or-splice
(beg end type register yank-handler)
"Deletes and copies the region by splicing unmatched delimiters."
:motion evil-forward-char
(interactive "<R><x>")
(lispyville--safe-manipulate beg end type 'splice t register yank-handler))
(evil-define-operator lispyville-delete-char-or-splice-backwards
(beg end type register yank-handler)
"Like `lispyville-delete-char-or-splice' but acts on the preceding character."
:motion evil-backward-char
(interactive "<R><x>")
(lispyville-delete-char-or-splice beg end type register yank-handler))
(evil-define-command lispyville-delete-backward-word ()
"Like `evil-delete-backward-word' but will not delete unmatched delimiters.
This will also act as `lispy-delete-backward' after delimiters."
(cond ((and (bolp) (not (bobp)))
(unless evil-backspace-join-lines
(user-error "Beginning of line"))
(delete-char -1))
((lispyville--after-delimiter-p)
(lispy-delete-backward 1))
(t
(lispyville-delete (max
(save-excursion
(evil-backward-word-begin)
(point))
(line-beginning-position))
(point)
'exclusive))))
(defun lispyville--open-here (count)
"Like `evil-open-above' except inserts at the point."
(interactive "p")
(unless (eq evil-want-fine-undo t)
(evil-start-undo-step))
(push (point) buffer-undo-list)
(setq evil-insert-count count
evil-insert-lines t
evil-insert-vcount nil)
(evil-insert-state 1)
(when evil-auto-indent
(indent-according-to-mode)))
(evil-define-operator lispyville-change
(beg end type register yank-handler delete-func)
"Like `evil-change' but will not delete/copy unmatched delimiters."
(interactive "<R><x><y>")
;; differences from `evil-change' are commented
(let ((delete-func (or delete-func #'lispyville-delete))
(nlines (1+ (evil-count-lines beg end)))
(opoint (save-excursion
(goto-char beg)
(line-beginning-position)))
(unmatched (lispy--find-unmatched-delimiters
(line-beginning-position)
(line-end-position)))
(lispyville--cc t))
(unless (eq evil-want-fine-undo t)
(evil-start-undo-step))
(funcall delete-func beg end type register yank-handler)
(cl-case type
(line
(cond ((lispyville--at-right-p)
(lispy--indent-for-tab)
(save-excursion
(while (progn (forward-char)
(lispyville--at-right-p)))
(newline-and-indent))
(evil-insert 1))
((and unmatched
(lispyville--at-left-p))
(while (progn (forward-char)
(lispyville--at-left-p)))
(save-excursion
(newline-and-indent))
(evil-insert 1))
((= opoint (point))
(evil-open-above 1))
(t
(newline-and-indent)
(evil-insert 1))))
(block (evil-insert 1 nlines))
(t
(evil-insert 1)))))
(evil-define-operator lispyville-change-line
(beg end type register yank-handler)
"Like `evil-change-line' but will not delete/copy unmatched delimiters."
:motion evil-end-of-line
(interactive "<R><x><y>")
(lispyville-change beg end type register yank-handler
#'lispyville-delete-line))
(evil-define-operator lispyville-change-whole-line
(beg end type register yank-handler)
"Change whole line while respecting parentheses."
:motion evil-line
(interactive "<R><x>")
(lispyville-change beg end type register yank-handler
#'lispyville-delete-whole-line))
(evil-define-operator lispyville-substitute (beg end type register)
"Acts like `lispyville-change' (cl when not in visual mode)."
:motion evil-forward-char
(interactive "<R><x>")
(lispyville-change beg end type register))
(defun lispyville--forward-list (&optional arg)
"Like `forward-list' but error if no more lists.
ARG has the same effect."
(interactive "^p")
(or arg (setq arg 1))
(let ((pos (scan-lists (point) arg 0)))
(if pos
(goto-char pos)
(error "No more lists"))))
(evil-define-operator lispyville-prettify (_beg end)
"Prettify lists from BEG to END."
:move-point nil
(interactive "<r>")
(let ((orig-pos (point)))
(evil-exit-visual-state)
(lispy--out-backward 1)
(while (and (ignore-errors (lispyville--forward-list))
(<= (save-excursion
(backward-list))
end))
(lispy--normalize-1))
(goto-char orig-pos)))
(evil-define-operator lispyville-comment-or-uncomment (beg end type)
"Like `evil-commentary' or `evilnc-comment-operator' but handles unmatched delimiters.
May insert newlines where necessary in order to move delimiters outside of the
commented regions and preserve the balanced structure. "
:move-point nil
(interactive "<R>")
(let ((end-marker (copy-marker (1+ end)))
(comment-combine-change-calls nil)) ;; workaround for undo-tree corruption issue in Emacs 27
(dolist (r (lispy--find-safe-regions beg end))
(comment-or-uncomment-region (car r) (cdr r)))
(delete-trailing-whitespace beg end-marker)))
(evil-define-operator lispyville-comment-or-uncomment-line (beg end type)
"Comment or uncomment lines while preserving structure."
:motion evil-line
:move-point nil
(interactive "<R>")
(lispyville-comment-or-uncomment beg end type))
(evil-define-operator lispyville-comment-and-clone-dwim (beg end type)
"Insert commented out copies of the right-most balanced portion within the specified region.
If the region is active (visual selection), apply to all subregions, not only the rightmost one."
:move-point nil
(interactive "<R>")
(save-excursion
(let ((end-marker (copy-marker (1+ end)))
(reg-active (region-active-p))
(comment-combine-change-calls nil)) ;; workaround for undo-tree corruption issue in Emacs 27
(when reg-active
(evil-normal-state))
(cl-loop
for (rb . re) in (lispy--find-safe-regions beg end) do
(let ((str (buffer-substring-no-properties rb re)))
(goto-char rb)
(insert str)
(when (> (length (string-trim str)) 0)
(comment-region rb (+ rb (length str)))
(unless reg-active
(cl-return)))))
(indent-region beg end-marker))))
(defun lispyville--join-between-comments ()
"Insert the contents of the next line between the non-commented
and commented regions of the current line.
If both lines have inline comments, place them after another in their original order,
and do not strip the comment characters."
(let ((inline-comment
(delete-and-extract-region
(save-excursion (forward-line 0)
(comment-search-forward (point-at-eol) 'no-error))
(point-at-eol))))
(join-line 1)
(save-excursion
(when (re-search-forward
(concat "[[:blank:]]*" comment-start-skip)
(point-at-eol) 'no-error)
(goto-char (match-beginning 0)))
(insert " " inline-comment)))
(indent-according-to-mode))
(evil-define-operator lispyville-join (beg end)
"Join the next line keeping inline comments out of the way.
If region is active, join all selected lines into one and splice all comments to the end."
:motion evil-line
(let ((count (count-lines beg end)))
(when (> count 1)
(setq count (1- count)))
(goto-char beg)
(dotimes (_ count)
(if (save-excursion (end-of-line) (lispy--in-comment-p))
;; Current line is a comment or ends in an inline comment
(cond
;; Edge case: end of buffer, do nothing
((= (point-at-eol) (point-max)) nil)
;; Empty line or interior of multi-line comment - join normally
((save-excursion
(forward-line 1)
(or (nth 4 (syntax-ppss))
(looking-at-p "[[:space:]]*$")))
(join-line 1))
;; Entire next line is also a comment - splice them together.
((save-excursion
(forward-line 1)
(looking-at (concat "[[:space:]]*" comment-start-skip)))
(end-of-line)
(delete-region (point) (match-end 0))
(fixup-whitespace))
;; Next line is non-empty and not a comment:
;; splice it between current line and the inline comment
(t (lispyville--join-between-comments)))
;; Normal case, no comments involved
(join-line 1)))))
;; ** Wrap Key Theme
(evil-define-operator lispyville-wrap-with-round (beg end)
"Insert ( at BEG and ) at END."
(interactive "<r>")
(lispy--mark (cons beg end))
(lispy-parens nil))
(evil-define-operator lispyville-wrap-with-brackets (beg end)
"Insert [ at BEG and ] at END."
(interactive "<r>")
(lispy--mark (cons beg end))
(lispy-brackets nil))
(evil-define-operator lispyville-wrap-with-braces (beg end)
"Insert { at BEG and } at END."
(interactive "<r>")
(lispy--mark (cons beg end))
(lispy-braces nil))
;; * Motions
;; ** Additional Movement Key Theme
(evil-define-motion lispyville-forward-sexp (count)
"This is an evil motion equivalent of `forward-sexp'."
;; TODO maybe forward-sexp-or-comment would be more useful
;; TODO also consider having it jump to the next level for failure
;; like evil-cp motion does
(forward-sexp (or count 1)))
(evil-define-motion lispyville-backward-sexp (count)
"This is an evil motion equivalent of `backward-sexp'."
(backward-sexp (or count 1)))
(evil-define-motion lispyville-beginning-of-defun (count)
"This is the evil motion equivalent of `beginning-of-defun'.
This won't jump to the beginning of the buffer if there is no paren there."
(beginning-of-defun (or count 1))
(lispyville--maybe-enter-special))
(evil-define-motion lispyville-end-of-defun (count)
"This is the evil motion equivalent of `end-of-defun'.
This won't jump to the end of the buffer if there is no paren there."
(end-of-defun (or count 1))
(re-search-backward lispy-right nil t)
(lispyville--maybe-enter-special))
(evil-define-motion lispyville-beginning-of-next-defun (count)
"Goto the beginning of the next top-level sexp COUNT times."
(end-of-defun (1+ (or count 1)))
(beginning-of-defun)
(lispyville--maybe-enter-special))
;; lispy-flow like (and reverse)
(defun lispyville--move-to-delimiter (count &optional type no-string)
"Move COUNT times to the next TYPE delimiter (right by default).
Move backwards when COUNT is negative. Unlike `evil-cp-next-closing', this won't
jump into comments or strings. When NO-STRING is non-nil, don't jump to string
delimiters. Return nil and don't move the point on failure."
(setq type (or type 'right))
(let ((positive (> count 0))
(regex (concat (if (eq type 'left)
(substring lispy-left 0 -1)
(substring lispy-right 0 -1))
(if no-string
"]"
"\"]")))
(initial-pos (point))
success)
(dotimes (_ (abs count))
(when positive
(forward-char))
(while
(and (if positive
(re-search-forward regex nil t)
(re-search-backward regex nil t))
(setq success t)
(save-excursion
(goto-char (match-beginning 0))
;; TODO handle "(" in case with NO-STRING nil
(or
(and no-string (lispy--in-string-p))
(lispy--in-comment-p)
(not (if (eq type 'left)
(lispyville--at-left-p)
(lispyville--at-right-p))))))
(setq success nil))
(if success
(goto-char (match-beginning 0))
(goto-char initial-pos)))
(unless (= (point) initial-pos)
(point))))
(evil-define-motion lispyville-next-opening (count)
"Move to the next opening delimiter COUNT times."
(lispyville--move-to-delimiter (or count 1) 'left)
(unless (looking-at "\"")
(lispyville--maybe-enter-special)))
(evil-define-motion lispyville-previous-opening (count)
"Move to the previous opening delimiter COUNT times."
(lispyville--move-to-delimiter (- (or count 1)) 'left)
(unless (looking-at "\"")
(lispyville--maybe-enter-special)))
(evil-define-motion lispyville-next-closing (count)
"Move to the next closing delimiter COUNT times."
(lispyville--move-to-delimiter (or count 1))
(unless (looking-at "\"")
(lispyville--maybe-enter-special)))
(evil-define-motion lispyville-previous-closing (count)
"Move to the previous closing delimiter COUNT times."
(lispyville--move-to-delimiter (- (or count 1)))
(unless (looking-at "\"")
(lispyville--maybe-enter-special)))
(evil-define-motion lispyville-backward-up-list (count)
"This is the evil motion equivalent of `lispy-left' (or `backward-up-list').
This is comparable to `evil-cp-backward-up-sexp'. It does not have lispy's
behavior on outlines."
(lispy-left (or count 1))
(lispyville--maybe-enter-special))
(defalias 'lispyville-left 'lispyville-backward-up-list)
(evil-define-motion lispyville-up-list (count)
"This is the evil motion equivalent of `lispy-right' (or `up-list').
This is comparable to `evil-cp-up-sexp'. It does not have lispy's behavior
on outlines. Unlike `up-list', it will keep the point on the closing delimiter."
(forward-char)
(lispy-right (or count 1))
(backward-char)
(lispyville--maybe-enter-special))
(defalias 'lispyville-right 'lispyville-up-list)
;; * Text Objects Key Theme
;; ** Atom (non-nestable)
;; save-excursion was changed to save-mark-and-excursion in 25.1
(eval-when-compile
(when (or (< emacs-major-version 25)
;; unlikely since 25.1 was the first official release
(and (= emacs-major-version 25) (< emacs-minor-version 1)))
(defmacro save-mark-and-excursion (&rest body)
`(save-excursion ,@body))))
(defun lispyville--bounds-atom ()
"Return the bounds of the atom at point or nil."
(let* ((comment-bounds (lispy--bounds-comment))
(string-bounds (lispyville--bounds-string))
;; includes quote unlike symbol
(sexp-bounds (save-excursion
(when (looking-at lispy-right)
(backward-char))
(bounds-of-thing-at-point 'sexp)))
(atom-bounds (when (and sexp-bounds
(not (equal sexp-bounds
(lispy--bounds-list))))
sexp-bounds)))
(or comment-bounds string-bounds atom-bounds)))
(put 'lispyville-atom 'bounds-of-thing-at-point #'lispyville--bounds-atom)
(evil-define-motion lispyville-forward-atom-begin (count)
"Go to the next atom or comment beginning COUNT times."
(or count (setq count 1))
(if (< count 0)
(lispyville-backward-atom-begin (- count))
(cl-dotimes (_ count)
(let ((orig-pos (point)))
;; move past the current atom
(ignore-errors (end-of-thing 'lispyville-atom))
;; move to next atom
(forward-symbol 1)
(unless (and (ignore-errors (beginning-of-thing 'lispyville-atom))
(not (<= (point) orig-pos)))
(goto-char orig-pos)
(cl-return))))))
(evil-define-motion lispyville-backward-atom-begin (count)
"Go to the previous atom or comment beginning COUNT times. "
(or count (setq count 1))
(if (< count 0)
(lispyville-forward-atom-begin (- count))
(cl-dotimes (_ count)
(let ((orig-pos (point)))
(forward-symbol -1)
(unless (and (ignore-errors (beginning-of-thing 'lispyville-atom))
(not (>= (point) orig-pos)))
(goto-char orig-pos)
(cl-return))))))
(evil-define-motion lispyville-forward-atom-end (count)
"Go to the next atom or comment end COUNT times."
(or count (setq count 1))
(if (< count 0)
(lispyville-backward-atom-end (- count))
(cl-dotimes (_ count)
(let ((orig-pos (point)))
(forward-symbol 1)
(unless (and (ignore-errors (end-of-thing 'lispyville-atom))
(not (<= (point) orig-pos)))
(goto-char orig-pos)
(cl-return))))))
(evil-define-motion lispyville-backward-atom-end (count)
"Go to the previous atom or comment end COUNT times."
(or count (setq count 1))
(if (< count 0)
(lispyville-forward-atom-end (- count))
(cl-dotimes (_ count)
(let ((orig-pos (point)))
;; move before the current atom
(ignore-errors (beginning-of-thing 'lispyville-atom))
;; move to previous atom
(forward-symbol -1)
(unless (and (ignore-errors (end-of-thing 'lispyville-atom))
(not (>= (point) orig-pos)))
(goto-char orig-pos)
(cl-return))))))
(evil-define-motion lispyville-forward-atom (count)
"Move forward across an atom COUNT times"
(or count (setq count 1))
(let ((orig-pos (point)))
(if (< count 0)
(lispyville-backward-atom-begin (- count))
(lispyville-forward-atom-end count))
(if (= (point) orig-pos)
1
;; evil wants 0 on success
0)))
(put 'lispyville-atom 'forward-op #'lispyville-forward-atom)
(evil-define-text-object lispyville-inner-atom (count &optional beg end type)
"Select inner atom.
This is comparable to `evil-inner-symbol' except that it will select entire
strings and comments."
(evil-select-inner-object 'lispyville-atom beg end type count))