-
Notifications
You must be signed in to change notification settings - Fork 135
/
meow-command.el
1887 lines (1639 loc) · 62.2 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--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--set-mark (&optional location nomsg activate)
"As `push-mark', but don't push old mark to mark ring."
(setq location (or location (point)))
(if (or activate (not transient-mark-mode))
(set-mark location)
(set-marker (mark-marker) location))
(or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
(message "Mark set"))
nil)
(defun meow--select (selection &optional backward)
"Mark the SELECTION."
(let* ((old-sel-type (meow--selection-type))
(sel-type (car selection))
(beg (cadr selection))
(end (caddr selection))
(to-go (if backward beg end))
(to-mark (if backward end beg)))
(when sel-type
(if meow--selection
(unless (equal meow--selection (car meow--selection-history))
(push meow--selection meow--selection-history))
(push (meow--make-selection nil (point) (point)) meow--selection-history))
(cond
((null old-sel-type)
(goto-char to-go)
(push-mark to-mark t t))
(t
(goto-char to-go)
(set-mark to-mark)))
(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.
If there's a selection history, move the mark to the beginning position
in the history before deactivation."
(when meow--selection-history
(let ((orig-pos (cadar (last meow--selection-history))))
(set-marker (mark-marker) orig-pos)))
(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
(meow--execute-kbd-macro meow--kbd-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 ()
"Copy, like command `kill-ring-save'.
This command supports `meow-selection-command-fallback'."
(interactive)
(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 latest 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)
(quit-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 ()
"Kill region and append to latest kill.
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)
(let ((s (buffer-substring-no-properties (region-beginning) (region-end))))
(meow--delete-region (region-beginning) (region-end))
(kill-append (meow--prepare-string-for-kill-append s) nil))))))))
(defun meow-C-k ()
"Run command on C-k."
(interactive)
(meow--execute-kbd-macro meow--kbd-kill-line))
(defun meow-kill-whole-line ()
(interactive)
(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))
(defun meow-backward-kill-word (arg)
"Kill characters backward until the beginning of a `meow-word-thing'.
With argument ARG, do this that many times."
(interactive "p")
(meow-kill-word (- arg)))
(defun meow-kill-word (arg)
"Kill characters forward until the end of a `meow-word-thing'.
With argument ARG, do this that many times."
(interactive "p")
(meow-kill-thing meow-word-thing arg))
(defun meow-backward-kill-symbol (arg)
"Kill characters backward until the beginning of a `meow-symbol-thing'.
With argument ARG, do this that many times."
(interactive "p")
(meow-kill-symbol (- arg)))
(defun meow-kill-symbol (arg)
"Kill characters forward until the end of a `meow-symbol-thing'.
With argument ARG, do this that many times."
(interactive "p")
(meow-kill-thing meow-symbol-thing arg))
(defun meow-kill-thing (thing arg)
"Kill characters forward until the end of a THING.
With argument ARG, do this that many times."
(let ((start (point))
(end (progn (forward-thing thing arg) (point))))
(condition-case _
(kill-region start end)
((text-read-only buffer-read-only)
(condition-case err
(meow--delete-region start end)
(t (signal (car err) (cdr err))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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))
((and (meow-insert-mode-p)
(eq meow--beacon-defining-kbd-macro 'quick))
(setq meow--beacon-defining-kbd-macro nil)
(meow-beacon-insert-exit))
((meow-insert-mode-p)
(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)
(when meow-select-on-insert
(setq-local meow--insert-pos (point)))))
(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)
(when meow-select-on-append
(setq-local meow--insert-pos (point)))))
(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
;; (meow--insert "\n"))
(indent-according-to-mode)))
(defun meow-open-above-visual ()
"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 (meow--visual-line-beginning-position))
(save-mark-and-excursion
(newline))
(indent-according-to-mode)))
(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-open-below-visual ()
"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 (meow--visual-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)
(setq this-command #'meow-change)
(meow--with-selection-fallback
(meow--delete-region (region-beginning) (region-end))
(meow--switch-state 'insert)
(when meow-select-on-change
(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)
(when meow-select-on-change
(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)
(when meow-select-on-change
(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")))
(meow--delete-region (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(meow--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")))
(meow--delete-region (point) (1+ (point)))
(set-marker meow--replace-start-marker (point))
(meow--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
(meow--delete-region (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(meow--insert s)
(kill-new old)))
(set-marker meow--replace-start-marker (point))
(meow--insert s)))))))
(defun meow-replace-pop ()
"Like `yank-pop', but for `meow-replace'.
If this command is called after `meow-replace',
`meow-replace-char', `meow-replace-save', or itself, replace the
previous replacement with the next item in the `kill-ring'.
Unlike `yank-pop', this command does not rotate the `kill-ring'.
For that, see the command `rotate-yank-pointer'.
For custom commands, see also the user option
`meow-replace-pop-command-start-indexes'."
(interactive "*")
(unless kill-ring (user-error "Can't replace; kill ring is empty"))
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(setq meow--replace-pop-index
(cond
((eq last-command 'meow-replace-pop) (1+ meow--replace-pop-index))
((alist-get last-command meow-replace-pop-command-start-indexes))
(t (user-error "Can only run `meow-replace-pop' after itself or a command in `meow-replace-pop-command-start-indexes'"))))
(when (>= meow--replace-pop-index (length kill-ring))
(setq meow--replace-pop-index 0)
(message "`meow-replace-pop': Reached end of kill ring"))
(let ((txt (string-trim-right (current-kill meow--replace-pop-index t)
"\n")))
(meow--delete-region meow--replace-start-marker (point))
(set-marker meow--replace-start-marker (point))
(meow--insert txt))))
(setq this-command 'meow-replace-pop))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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))))
(defun meow-mark-thing (thing type &optional backward regexp-format)
"Make expandable selection of THING, with TYPE and forward/BACKWARD direction.
THING is a symbol usable by `forward-thing', which see.
TYPE is a symbol. Usual values are `word' or `line'.
The selection will be made in the \\='forward\\=' direction unless BACKWARD is
non-nil.
When REGEXP-FORMAT is non-nil and a string, the content of the selection will be
quoted to regexp, then pushed into `regexp-search-ring' which will be read by
`meow-search' and other commands. In this case, REGEXP-FORMAT is used as a
format-string to format the regexp-quoted selection content (which is passed as
a string to `format'). Further matches of this formatted search will be
highlighted in the buffer."
(let* ((bounds (bounds-of-thing-at-point thing))
(beg (car bounds))
(end (cdr bounds)))
(when beg
(thread-first
(meow--make-selection (cons 'expand type) beg end)
(meow--select backward))
(when (stringp regexp-format)
(let ((search (format regexp-format (regexp-quote (buffer-substring-no-properties beg end)))))
(meow--push-search search)
(meow--highlight-regexp-in-buffer search))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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")
(meow-mark-thing meow-word-thing 'word (< n 0) "\\<%s\\>"))
(defun meow-mark-symbol (n)
"Mark current symbol under cursor.
This command works similar to `meow-mark-word'."
(interactive "p")
(meow-mark-thing meow-symbol-thing 'symbol (< n 0) "\\_<%s\\_>"))
(defun meow--forward-thing-1 (thing)
(let ((pos (point)))
(forward-thing thing 1)
(when (not (= pos (point)))
(meow--hack-cursor-pos (point)))))
(defun meow--backward-thing-1 (thing)
(let ((pos (point)))
(forward-thing thing -1)
(when (not (= pos (point)))
(point))))
(defun meow--fix-thing-selection-mark (thing pos mark include-syntax)
"Return new mark for a selection of THING.
This will shrink the word selection only contains
those in INCLUDE-SYNTAX."
(let ((backward (> mark pos)))
(save-mark-and-excursion
(goto-char
(if backward pos
;; Point must be before the end of the word to get the bounds correctly
(1- pos)))
(let* ((bounds (bounds-of-thing-at-point thing))
(m (if backward
(min mark (cdr bounds))
(max mark (car bounds)))))
(save-mark-and-excursion
(goto-char m)
(if backward
(skip-syntax-forward include-syntax mark)
(skip-syntax-backward include-syntax mark))
(point))))))
(defun meow-next-thing (thing type n &optional include-syntax)
"Create non-expandable selection of TYPE to the end of the next Nth THING.
If N is negative, select to the beginning of the previous Nth thing instead."
(unless (equal type (cdr (meow--selection-type)))
(meow--cancel-selection))
(unless include-syntax
(setq include-syntax
(let ((thing-include-syntax (alist-get thing meow-next-thing-include-syntax)))
(if (> n 0)
(car thing-include-syntax)
(cadr thing-include-syntax)))))
(let* ((expand (equal (cons 'expand type) (meow--selection-type)))
(_ (when expand
(if (< n 0) (meow--direction-backward)
(meow--direction-forward))))
(new-type (if expand (cons 'expand type) (cons 'select type)))
(m (point))
(p (save-mark-and-excursion
(forward-thing thing n)
(unless (= (point) m)
(point)))))
(when p
(thread-first
(meow--make-selection
new-type
(meow--fix-thing-selection-mark thing p m include-syntax)
p
expand)
(meow--select))
(meow--maybe-highlight-num-positions
(cons (apply-partially #'meow--backward-thing-1 thing)
(apply-partially #'meow--forward-thing-1 thing))))))
(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")
(meow-next-thing meow-word-thing 'word n))
(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")
(meow-next-thing meow-symbol-thing 'symbol n))
(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")
(meow-next-thing meow-word-thing 'word (- n)))
(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")
(meow-next-thing meow-symbol-thing 'symbol (- n)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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")
(let* ((cancel-sel (not (or expand (equal '(expand . line) (meow--selection-type)))))
(backward (unless cancel-sel (meow--direction-backward-p)))
(orig (if cancel-sel (point) (mark t)))
(n (if backward
(- n)
n))
(forward (> n 0)))
(cond