forked from meow-edit/meow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meow-command.el
1676 lines (1455 loc) · 54.4 KB
/
meow-command.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
;;; meow-commands.el --- Commands in Meow -*- lexical-binding: t -*-
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Implementation for all commands in Meow.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'seq)
(require 'meow-var)
(require 'meow-util)
(require 'meow-visual)
(require 'meow-thing)
(require 'meow-beacon)
(require 'meow-keypad)
(require 'array)
(defun meow--execute-kbd-macro (kbd-macro)
"Execute KBD-MACRO."
(when-let ((ret (key-binding (read-kbd-macro kbd-macro))))
(cond
((commandp ret)
(call-interactively ret))
((and (not meow-use-keypad-when-execute-kbd) (keymapp ret))
(set-transient-map ret nil nil))
((and meow-use-keypad-when-execute-kbd (keymapp ret))
(meow-keypad-start-with kbd-macro)))))
(defun meow--selection-fallback ()
"Run selection fallback commands."
(if-let ((fallback (alist-get this-command meow-selection-command-fallback)))
(call-interactively fallback)
(error "No selection")))
(defun meow--pop-selection ()
"Pop a selection from variable `meow--selection-history' and activate."
(when meow--selection-history
(let ((sel (pop meow--selection-history)))
(meow--select-without-history sel))))
(defun meow--make-selection (type mark pos &optional expand)
"Make a selection with TYPE, MARK and POS.
The direction of selection is MARK -> POS."
(if (and (region-active-p) expand)
(let ((orig-mark (mark))
(orig-pos (point)))
(if (< mark pos)
(list type (min orig-mark orig-pos) pos)
(list type (max orig-mark orig-pos) pos)))
(list type mark pos)))
(defun meow--select (selection &optional backward)
"Mark the SELECTION."
(unless (region-active-p)
(meow--cancel-selection))
(let ((sel-type (car selection))
(mark (cadr selection))
(pos (caddr selection)))
(if meow--selection
(unless (equal meow--selection (car meow--selection-history))
(push meow--selection meow--selection-history))
;; Used to restore the position where we starting selection
(push (meow--make-selection nil (point) (point))
meow--selection-history))
(goto-char (if backward mark pos))
(when sel-type
(push-mark (if backward pos mark) t t)
(setq meow--selection selection))))
(defun meow--select-without-history (selection)
"Mark the SELECTION without recording it in `meow--selection-history'."
(let ((sel-type (car selection))
(mark (cadr selection))
(pos (caddr selection)))
(goto-char pos)
(if (not sel-type)
(progn
(deactivate-mark)
(message "No previous selection.")
(meow--cancel-selection))
(push-mark mark t t)
(setq meow--selection selection))))
(defun meow--cancel-selection ()
"Cancel current selection, clear selection history and deactivate the mark."
(setq meow--selection-history nil
meow--selection nil)
(deactivate-mark t))
(defun meow-undo ()
"Cancel current selection then undo."
(interactive)
(when (region-active-p)
(meow--cancel-selection))
(meow--execute-kbd-macro meow--kbd-undo))
(defun meow-undo-in-selection ()
"Cancel undo in current region."
(interactive)
(when (region-active-p)
(meow--execute-kbd-macro meow--kbd-undo)))
(defun meow-pop-selection ()
(interactive)
(meow--with-selection-fallback
(meow--pop-selection)
(when (and (region-active-p) meow--expand-nav-function)
(meow--maybe-highlight-num-positions))))
(defun meow-pop-all-selection ()
(interactive)
(while (meow--pop-selection)))
;;; exchange mark and point
(defun meow-reverse ()
"Just exchange point and mark.
This command supports `meow-selection-command-fallback'."
(interactive)
(meow--with-selection-fallback
(exchange-point-and-mark)
(if (member last-command
'(meow-visit meow-search meow-mark-symbol meow-mark-word))
(meow--highlight-regexp-in-buffer (car regexp-search-ring))
(meow--maybe-highlight-num-positions))))
;;; Buffer
(defun meow-find-ref ()
"Xref find."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-find-ref))
(defun meow-pop-marker ()
"Pop marker."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-pop-marker))
;;; Clipboards
(defun meow-clipboard-yank ()
"Yank system clipboard."
(interactive)
(call-interactively #'clipboard-yank))
(defun meow-clipboard-kill ()
"Kill to system clipboard."
(interactive)
(call-interactively #'clipboard-kill-region))
(defun meow-clipboard-save ()
"Save to system clipboard."
(interactive)
(call-interactively #'clipboard-kill-ring-save))
(defun meow-save (_arg)
"Copy, like command `kill-ring-save'.
This command supports `meow-selection-command-fallback'.
Use prefix argument to save to secondary selection instead of kill-ring."
(interactive "P")
(meow--with-selection-fallback
(let ((select-enable-clipboard meow-use-clipboard))
(meow--prepare-region-for-kill)
(meow--execute-kbd-macro meow--kbd-kill-ring-save))))
(defun meow-save-append ()
"Copy, like command `kill-ring-save' but append to lastest kill.
This command supports `meow-selection-command-fallback'."
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(meow--prepare-region-for-kill)
(let ((s (buffer-substring-no-properties (region-beginning) (region-end))))
(kill-append (meow--prepare-string-for-kill-append s) nil)
(deactivate-mark t))))
(defun meow-save-empty ()
"Copy an empty string, can be used with `meow-save-append' or `meow-kill-append'."
(interactive)
(kill-new ""))
(defun meow-save-char ()
"Copy current char."
(interactive)
(when (< (point) (point-max))
(save-mark-and-excursion
(goto-char (point))
(push-mark (1+ (point)) t t)
(meow--execute-kbd-macro meow--kbd-kill-ring-save))))
(defun meow-yank ()
"Yank."
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(meow--execute-kbd-macro meow--kbd-yank)))
(defun meow-yank-pop ()
"Pop yank."
(interactive)
(when (meow--allow-modify-p)
(meow--execute-kbd-macro meow--kbd-yank-pop)))
;;; Quit
(defun meow-cancel-selection ()
"Cancel selection.
This command supports `meow-selection-command-fallback'."
(interactive)
(meow--with-selection-fallback
(meow--cancel-selection)))
(defun meow-keyboard-quit ()
"Keyboard quit."
(interactive)
(if (region-active-p)
(deactivate-mark t)
(meow--execute-kbd-macro meow--kbd-keyboard-quit)))
(defun meow-quit ()
"Quit current window or buffer."
(interactive)
(if (> (seq-length (window-list (selected-frame))) 1)
(delete-window)
(previous-buffer)))
;;; Comment
(defun meow-comment ()
"Comment region or comment line."
(interactive)
(when (meow--allow-modify-p)
(meow--execute-kbd-macro meow--kbd-comment)))
;;; Delete Operations
(defun meow-kill ()
"Kill region.
This command supports `meow-selection-command-fallback'."
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(meow--with-selection-fallback
(cond
((equal '(expand . join) (meow--selection-type))
(delete-indentation nil (region-beginning) (region-end)))
(t
(meow--prepare-region-for-kill)
(meow--execute-kbd-macro meow--kbd-kill-region)))))))
(defun meow-kill-append (_arg)
"Kill region and append to latest kill.
This command supports `meow-selection-command-fallback'."
(interactive "P")
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(meow--with-selection-fallback
(cond
((equal '(expand . join) (meow--selection-type))
(delete-indentation nil (region-beginning) (region-end)))
(t
(meow--prepare-region-for-kill)
(let ((s (buffer-substring-no-properties (region-beginning) (region-end))))
(delete-region (region-beginning) (region-end))
(kill-append (meow--prepare-string-for-kill-append s) nil))))))))
(defun meow-C-k (_arg)
"Run command on C-k."
(interactive "P")
(meow--execute-kbd-macro meow--kbd-kill-line))
(defun meow-kill-whole-line (_arg)
(interactive "P")
(when (meow--allow-modify-p)
(meow--execute-kbd-macro meow--kbd-kill-whole-line)))
(defun meow-backspace ()
"Backward delete one char."
(interactive)
(when (meow--allow-modify-p)
(call-interactively #'backward-delete-char)))
(defun meow-C-d ()
"Run command on C-d."
(interactive)
(meow--execute-kbd-macro meow--kbd-delete-char))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PAGE UP&DOWN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow-page-up ()
"Page up."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-scoll-down))
(defun meow-page-down ()
"Page down."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-scoll-up))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PARENTHESIS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow-forward-slurp ()
"Forward slurp sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-forward-slurp))
(defun meow-backward-slurp ()
"Backward slurp sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-backward-slurp))
(defun meow-forward-barf ()
"Forward barf sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-forward-barf))
(defun meow-backward-barf ()
"Backward barf sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-backward-barf))
(defun meow-raise-sexp ()
"Raise sexp."
(interactive)
(meow--cancel-selection)
(let ((bounds (bounds-of-thing-at-point 'sexp)))
(when bounds
(goto-char (car bounds))))
(meow--execute-kbd-macro meow--kbd-raise-sexp))
(defun meow-transpose-sexp ()
"Transpose sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-transpose-sexp))
(defun meow-split-sexp ()
"Split sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-split-sexp))
(defun meow-join-sexp ()
"Split sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-join-sexp))
(defun meow-splice-sexp ()
"Splice sexp."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-splice-sexp))
(defun meow-wrap-round ()
"Wrap round paren."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-wrap-round))
(defun meow-wrap-square ()
"Wrap square paren."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-wrap-square))
(defun meow-wrap-curly ()
"Wrap curly paren."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-wrap-curly))
(defun meow-wrap-string ()
"Wrap string."
(interactive)
(meow--cancel-selection)
(meow--execute-kbd-macro meow--kbd-wrap-string))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; STATE TOGGLE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow-insert-exit ()
"Switch to NORMAL state."
(interactive)
(cond
((meow-keypad-mode-p)
(meow--exit-keypad-state))
((meow-insert-mode-p)
(when overwrite-mode
(overwrite-mode -1))
(meow--switch-state 'normal))))
(defun meow-temp-normal ()
"Switch to navigation-only NORMAL state."
(interactive)
(when (meow-motion-mode-p)
(message "Enter temporary normal mode")
(setq meow--temp-normal t)
(meow--switch-state 'normal)))
(defun meow-insert ()
"Move to the start of selection, switch to INSERT state."
(interactive)
(if meow--temp-normal
(progn
(message "Quit temporary normal mode")
(meow--switch-state 'motion))
(meow--direction-backward)
(meow--cancel-selection)
(meow--switch-state 'insert)))
(defun meow-append ()
"Move to the end of selection, switch to INSERT state."
(interactive)
(if meow--temp-normal
(progn
(message "Quit temporary normal mode")
(meow--switch-state 'motion))
(if (not (region-active-p))
(when (and meow-use-cursor-position-hack
(< (point) (point-max)))
(forward-char 1))
(meow--direction-forward)
(meow--cancel-selection))
(meow--switch-state 'insert)))
(defun meow-open-above ()
"Open a newline above and switch to INSERT state."
(interactive)
(if meow--temp-normal
(progn
(message "Quit temporary normal mode")
(meow--switch-state 'motion))
(meow--switch-state 'insert)
(goto-char (line-beginning-position))
(save-mark-and-excursion
(newline))
;; (save-mark-and-excursion
;; (insert "\n"))
(indent-for-tab-command)))
(defun meow-open-below ()
"Open a newline below and switch to INSERT state."
(interactive)
(if meow--temp-normal
(progn
(message "Quit temporary normal mode")
(meow--switch-state 'motion))
(meow--switch-state 'insert)
(goto-char (line-end-position))
(meow--execute-kbd-macro "RET")))
(defun meow-change ()
"Kill current selection and switch to INSERT state.
This command supports `meow-selection-command-fallback'."
(interactive)
(when (meow--allow-modify-p)
(meow--with-selection-fallback
(delete-region (region-beginning) (region-end))
(meow--switch-state 'insert)
(setq-local meow--insert-pos (point)))))
(defun meow-change-char ()
"Delete current char and switch to INSERT state."
(interactive)
(when (< (point) (point-max))
(meow--execute-kbd-macro meow--kbd-delete-char)
(meow--switch-state 'insert)
(setq-local meow--insert-pos (point))))
(defun meow-change-save ()
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(when (and (meow--allow-modify-p) (region-active-p))
(kill-region (region-beginning) (region-end))
(meow--switch-state 'insert)
(setq-local meow--insert-pos (point)))))
(defun meow-replace ()
"Replace current selection with yank.
This command supports `meow-selection-command-fallback'."
(interactive)
(meow--with-selection-fallback
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (region-beginning) (region-end))
(insert s))))))
(defun meow-replace-char ()
"Replace current char with selection."
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(when (< (point) (point-max))
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (point) (1+ (point)))
(insert s)))))
(defun meow-replace-save ()
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(when-let ((curr (pop kill-ring-yank-pointer)))
(let ((s (string-trim-right curr "\n")))
(setq kill-ring kill-ring-yank-pointer)
(if (region-active-p)
(let ((old (save-mark-and-excursion
(meow--prepare-region-for-kill)
(buffer-substring-no-properties (region-beginning) (region-end)))))
(progn
(delete-region (region-beginning) (region-end))
(insert s)
(kill-new old)))
(insert s)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CHAR MOVEMENT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow-left ()
"Move to left.
Will cancel all other selection, except char selection. "
(interactive)
(when (and (region-active-p)
(not (equal '(expand . char) (meow--selection-type))))
(meow-cancel-selection))
(meow--execute-kbd-macro meow--kbd-backward-char))
(defun meow-right ()
"Move to right.
Will cancel all other selection, except char selection. "
(interactive)
(let ((ra (region-active-p)))
(when (and ra
(not (equal '(expand . char) (meow--selection-type))))
(meow-cancel-selection))
(when (or (not meow-use-cursor-position-hack)
(not ra)
(equal '(expand . char) (meow--selection-type)))
(meow--execute-kbd-macro meow--kbd-forward-char))))
(defun meow-left-expand ()
"Activate char selection, then move left."
(interactive)
(if (region-active-p)
(thread-first
(meow--make-selection '(expand . char) (mark) (point))
(meow--select))
(when meow-use-cursor-position-hack
(forward-char 1))
(thread-first
(meow--make-selection '(expand . char) (point) (point))
(meow--select)))
(meow--execute-kbd-macro meow--kbd-backward-char))
(defun meow-right-expand ()
"Activate char selection, then move right."
(interactive)
(if (region-active-p)
(thread-first
(meow--make-selection '(expand . char) (mark) (point))
(meow--select))
(thread-first
(meow--make-selection '(expand . char) (point) (point))
(meow--select)))
(meow--execute-kbd-macro meow--kbd-forward-char))
(defun meow-prev (arg)
"Move to the previous line.
Will cancel all other selection, except char selection.
Use with universal argument to move to the first line of buffer.
Use with numeric argument to move multiple lines at once."
(interactive "P")
(unless (equal (meow--selection-type) '(expand . char))
(meow--cancel-selection))
(cond
((meow--with-universal-argument-p arg)
(goto-char (point-min)))
(t
(setq this-command #'previous-line)
(meow--execute-kbd-macro meow--kbd-backward-line))))
(defun meow-next (arg)
"Move to the next line.
Will cancel all other selection, except char selection.
Use with universal argument to move to the last line of buffer.
Use with numeric argument to move multiple lines at once."
(interactive "P")
(unless (equal (meow--selection-type) '(expand . char))
(meow--cancel-selection))
(cond
((meow--with-universal-argument-p arg)
(goto-char (point-max)))
(t
(setq this-command #'next-line)
(meow--execute-kbd-macro meow--kbd-forward-line))))
(defun meow-prev-expand (arg)
"Activate char selection, then move to the previous line.
See `meow-prev-line' for how prefix arguments work."
(interactive "P")
(if (region-active-p)
(thread-first
(meow--make-selection '(expand . char) (mark) (point))
(meow--select))
(thread-first
(meow--make-selection '(expand . char) (point) (point))
(meow--select)))
(cond
((meow--with-universal-argument-p arg)
(goto-char (point-min)))
(t
(setq this-command #'previous-line)
(meow--execute-kbd-macro meow--kbd-backward-line))))
(defun meow-next-expand (arg)
"Activate char selection, then move to the next line.
See `meow-next-line' for how prefix arguments work."
(interactive "P")
(if (region-active-p)
(thread-first
(meow--make-selection '(expand . char) (mark) (point))
(meow--select))
(thread-first
(meow--make-selection '(expand . char) (point) (point))
(meow--select)))
(cond
((meow--with-universal-argument-p arg)
(goto-char (point-max)))
(t
(setq this-command #'next-line)
(meow--execute-kbd-macro meow--kbd-forward-line))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; WORD/SYMBOL MOVEMENT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow-mark-word (n)
"Mark current word under cursor.
A expandable word selection will be created. `meow-next-word' and
`meow-back-word' can be used for expanding.
The content of selection will be quoted to regexp, then pushed into
`regexp-search-ring' which be read by `meow-search' and other commands.
This command will also provide highlighting for same occurs.
Use negative argument to create a backward selection."
(interactive "p")
(let* ((bounds (bounds-of-thing-at-point 'word))
(beg (car bounds))
(end (cdr bounds)))
(when beg
(thread-first
(meow--make-selection '(expand . word) beg end)
(meow--select (< n 0)))
(let ((search (format "\\<%s\\>" (regexp-quote (buffer-substring-no-properties beg end)))))
(meow--push-search search)
(meow--highlight-regexp-in-buffer search)))))
(defun meow-mark-symbol (n)
"Mark current symbol under cursor.
This command works similar to `meow-mark-word'."
(interactive "p")
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(beg (car bounds))
(end (cdr bounds)))
(when beg
(thread-first
(meow--make-selection '(expand . word) beg end)
(meow--select (< n 0)))
(let ((search (format "\\_<%s\\_>" (regexp-quote (buffer-substring-no-properties beg end)))))
(meow--push-search search)
(meow--highlight-regexp-in-buffer search)))))
(defun meow--forward-symbol-1 ()
(when (forward-symbol 1)
(meow--hack-cursor-pos (point))))
(defun meow--backward-symbol-1 ()
(let ((pos (point)))
(forward-symbol -1)
(when (not (= pos (point)))
(point))))
(defun meow--fix-word-selection-mark (pos mark)
"Return new mark for a word select.
This will shrink the word selection only contains
word/symbol constituent character and whitespaces."
(save-mark-and-excursion
(goto-char pos)
(if (> mark pos)
(progn (skip-syntax-forward "-_w" mark)
(point))
(skip-syntax-backward "-_w" mark)
(point))))
(defun meow--forward-word-1 ()
(when (forward-word)
(meow--hack-cursor-pos (point))))
(defun meow--backward-word-1 ()
(when (forward-word -1)
(point)))
(defun meow-next-word (n)
"Select to the end of the next Nth word.
A non-expandable, word selection will be created.
To select continuous words, use following approaches:
1. start the selection with `meow-mark-word'.
2. use prefix digit arguments.
3. use `meow-expand' after this command.
"
(interactive "p")
(unless (equal 'word (cdr (meow--selection-type)))
(meow--cancel-selection))
(let* ((expand (equal '(expand . word) (meow--selection-type)))
(_ (when expand (meow--direction-forward)))
(type (if expand '(expand . word) '(select . word)))
(m (point))
(p (save-mark-and-excursion
(when (forward-word n)
(point)))))
(when p
(thread-first
(meow--make-selection type (meow--fix-word-selection-mark p m) p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-word-1 . meow--forward-word-1)))))
(defun meow-next-symbol (n)
"Select to the end of the next Nth symbol.
A non-expandable, word selection will be created.
There's no symbol selection type in Meow.
To select continuous symbols, use following approaches:
1. start the selection with `meow-mark-symbol'.
2. use prefix digit arguments.
3. use `meow-expand' after this command."
(interactive "p")
(unless (equal 'word (cdr (meow--selection-type)))
(meow--cancel-selection))
(let* ((expand (equal '(expand . word) (meow--selection-type)))
(_ (when expand (meow--direction-forward)))
(type (if expand '(expand . word) '(select . word)))
(m (point))
(p (save-mark-and-excursion
(when (forward-symbol n)
(point)))))
(when p
(thread-first
(meow--make-selection type (meow--fix-word-selection-mark p m) p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-symbol-1 . meow--forward-symbol-1)))))
(defun meow-back-word (n)
"Select to the beginning the previous Nth word.
A non-expandable word selection will be created.
This command works similar to `meow-next-word'."
(interactive "p")
(unless (equal 'word (cdr (meow--selection-type)))
(meow--cancel-selection))
(let* ((expand (equal '(expand . word) (meow--selection-type)))
(_ (when expand (meow--direction-backward)))
(type (if expand '(expand . word) '(select . word)))
(m (point))
(p (save-mark-and-excursion
(when (backward-word n)
(point)))))
(when p
(thread-first
(meow--make-selection type (meow--fix-word-selection-mark p m) p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-word-1 . meow--forward-word-1)))))
(defun meow-back-symbol (n)
"Select to the beginning the previous Nth symbol.
A non-expandable word selection will be created.
This command works similar to `meow-next-symbol'."
(interactive "p")
(unless (equal 'word (cdr (meow--selection-type)))
(meow--cancel-selection))
(meow--direction-backward)
(let* ((expand (equal '(expand . word) (meow--selection-type)))
(_ (when expand (meow--direction-backward)))
(type (if expand '(expand . word) '(select . word)))
(m (point))
(p (save-mark-and-excursion
(forward-symbol (- n))
(unless (= (point) m)
(point)))))
(when p
(thread-first
(meow--make-selection type (meow--fix-word-selection-mark p m) p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-symbol-1 . meow--forward-symbol-1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; LINE SELECTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow--forward-line-1 ()
(let ((orig (point)))
(forward-line 1)
(if meow--expanding-p
(progn
(goto-char (line-end-position))
(line-end-position))
(when (< orig (line-beginning-position))
(line-beginning-position)))))
(defun meow--backward-line-1 ()
(forward-line -1)
(line-beginning-position))
(defun meow-line (n &optional expand)
"Select the current line, eol is not included.
Create selection with type (expand . line).
For the selection with type (expand . line), expand it by line.
For the selection with other types, cancel it.
Prefix:
numeric, repeat times.
"
(interactive "p")
(unless (or expand (equal '(expand . line) (meow--selection-type)))
(meow--cancel-selection))
(let* ((orig (mark))
(n (if (meow--direction-backward-p)
(- n)
n))
(forward (> n 0)))
(cond
((region-active-p)
(let (p)
(save-mark-and-excursion
(forward-line n)
(goto-char
(if forward
(setq p (line-end-position))
(setq p (line-beginning-position)))))
(thread-first
(meow--make-selection '(expand . line) orig p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-line-1 . meow--forward-line-1))))
(t
(let ((m (if forward
(line-beginning-position)
(line-end-position)))
(p (save-mark-and-excursion
(if forward
(progn
(forward-line (1- n))
(line-end-position))
(progn
(forward-line (1+ n))
(when (meow--empty-line-p)
(backward-char 1))
(line-beginning-position))))))
(thread-first
(meow--make-selection '(expand . line) m p expand)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-line-1 . meow--forward-line-1)))))))
(defun meow-line-expand (n)
"Like `meow-line', but always expand."
(interactive "p")
(meow-line n t))
(defun meow-goto-line ()
"Goto line, recenter and select that line."
(interactive)
(call-interactively meow-goto-line-function)
(let ((beg (line-beginning-position))
(end (line-end-position)))
(thread-first
(meow--make-selection '(expand . line) beg end nil)
(meow--select))
(recenter)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; BLOCK
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun meow--backward-block ()
(let ((orig-pos (point))
(pos (save-mark-and-excursion
(let ((depth (car (syntax-ppss))))
(while (and (re-search-backward "\\s(" nil t)
(> (car (syntax-ppss)) depth)))
(when (= (car (syntax-ppss)) depth)
(point))))))
(when (and pos (not (= orig-pos pos)))
(goto-char pos))))
(defun meow--forward-block ()
(let ((orig-pos (point))
(pos (save-mark-and-excursion
(let ((depth (car (syntax-ppss))))
(while (and (re-search-forward "\\s)" nil t)
(> (car (syntax-ppss)) depth)))
(when (= (car (syntax-ppss)) depth)
(point))))))
(when (and pos (not (= orig-pos pos)))
(goto-char pos)
(meow--hack-cursor-pos (point)))))
(defun meow-block (arg)
"Mark the block or expand to parent block."
(interactive "P")
(unless (equal 'block (cdr (meow--selection-type)))
(meow--cancel-selection))
(let ((ra (region-active-p))
(back (xor (meow--direction-backward-p) (< (prefix-numeric-value arg) 0)))
(depth (car (syntax-ppss)))
(orig-pos (point))
p m)
(save-mark-and-excursion
(while (and (if back (re-search-backward "\\s(" nil t) (re-search-forward "\\s)" nil t))
(or (meow--in-string-p)
(if ra (>= (car (syntax-ppss)) depth) (> (car (syntax-ppss)) depth)))))
(when (and (if ra (< (car (syntax-ppss)) depth) (<= (car (syntax-ppss)) depth))
(not (= (point) orig-pos)))
(setq p (point))
(when (ignore-errors (forward-list (if back 1 -1)))
(setq m (point)))))
(when (and p m)
(thread-first
(meow--make-selection '(expand . block) m p)
(meow--select))
(meow--maybe-highlight-num-positions '(meow--backward-block . meow--forward-block)))))
(defun meow-to-block (arg)
"Expand to next block.
Will create selection with type (expand . block)."
(interactive "P")