forked from SpringHan/sniem
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniem-operation.el
1498 lines (1367 loc) · 56.1 KB
/
sniem-operation.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
;;; sniem-operation.el --- Hands-eased united editing method -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; This file is not part of GNU Emacs
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Hands-eased united editing method
;;; Code:
(require 'kmacro)
(require 'sniem-var)
(require 'sniem-macro)
(require 'sniem-common)
(declare-function sniem-object-catch--get-second-char "sniem-object-catch")
(declare-function sniem-current-mode "sniem")
(declare-function sniem-change-mode "sniem")
(declare-function sniem-mark-content "sniem")
(declare-function sniem-lock-unlock-last-point "sniem")
(declare-function sniem-shift--not-alpha-p "sniem")
(defun sniem-insert ()
"Insert at the current point or the beginning of mark region."
(interactive)
(unless (eq (sniem-current-mode) 'insert)
(when (region-active-p)
(goto-char (region-beginning))
(deactivate-mark))
(sniem-change-mode 'insert)))
(defun sniem-insert-line ()
"Insert at the beginning of line."
(interactive)
(if (region-active-p)
(goto-char (1+ (region-beginning)))
(back-to-indentation))
(sniem-insert))
(defun sniem-append ()
"Append at the next point or the end of mark region."
(interactive)
(if (region-active-p)
(progn
(goto-char (region-end))
(deactivate-mark))
(unless (= (line-end-position) (point))
(forward-char)))
(sniem-insert))
(defun sniem-append-line ()
"Append at the end of line."
(interactive)
(if (region-active-p)
(progn
(goto-char (1- (region-end)))
(deactivate-mark))
(end-of-line))
(sniem-insert))
(defun sniem-open-line ()
"Open new line."
(interactive)
(goto-char (line-end-position))
(unless sniem-enter-command
(sniem-open-line--init-command))
(call-interactively sniem-enter-command)
(sniem-insert))
(defun sniem-open-line-previous ()
"Open new line."
(interactive)
(if (= (line-beginning-position) (point-min))
(progn
(goto-char (point-min))
(insert "\n")
(goto-char (point-min)))
(forward-line -1)
(goto-char (line-end-position))
(unless sniem-enter-command
(sniem-open-line--init-command))
(call-interactively sniem-enter-command))
(sniem-insert))
(defun sniem-open-line--init-command ()
"Initialize open line command."
(sniem-change-mode 'insert)
(setq-local sniem-enter-command (key-binding (kbd "RET"))))
(defun sniem-center (action)
"Center ACTION for sniem."
(interactive (list (read-char sniem-center-message)))
(pcase action
(?z (recenter nil t))
(?t (recenter-top-bottom 0))
(?b (recenter-top-bottom -1))))
(defun sniem-mark (type)
"Mark the object with action TYPE."
(interactive (list (read-char sniem-mark-message)))
(pcase type
(?l
(beginning-of-line)
(push-mark (point) t t)
(end-of-line)
(setq-local sniem-mark-line t))
(?p
(push-mark sniem-last-point t t)
(when sniem-last-point-locked
(sniem-lock-unlock-last-point)))
(?m (push-mark (point) t t))
(?f (mark-defun))
(?b (push-mark (point-min) t t)
(goto-char (point-max)))
(_
(let* ((space-mode (when (= type 32)
(progn
(setq type (read-char sniem-mark-message))
t)))
(expand-times (when (numberp current-prefix-arg)
current-prefix-arg))
(thing (pcase type
(?s 'symbol)
(?w 'word)
(_ (user-error "[Sniem]: The %s type is error!" type))))
(points (sniem-mark--bounds-of-thing-at-point
thing
(when (region-active-p)
(deactivate-mark)
(cons (region-beginning) (region-end)))
(when expand-times
expand-times))))
(when (and points space-mode
(save-mark-and-excursion
(let (l r)
(goto-char (car points))
(when (= (char-before) 32)
(setq l t))
(goto-char (cdr points))
(when (= (following-char) 32)
(setq r t))
(and l r))))
(setq points (cons (1- (car points)) (1+ (cdr points)))))
(when points
(goto-char (car points))
(push-mark (cdr points) t t))))))
(defun sniem-mark--bounds-of-thing-at-point (thing &optional expand expand-times)
"Get the bounds of theg THING a point.
THING can be `symbol' or `word'.
When EXPAND is non-nil, means expand the current selection.
And its format is like: (start-point . end-point).
EXPAND-TIMES is the dominant parameter for expanding times."
(let ((move-command 'forward-char)
(enter-point (point))
(current-char (following-char))
(split-char-p (lambda (c) (memq c '(32 9 10))))
start-point end-point can-enter stop-current-expanding)
(save-excursion
(pcase expand-times
(0 (setq expand-times 'inf))
;; Expand once defaultly when expanding
('nil (when expand
(setq expand-times 1))))
(when expand
(setq start-point (car expand)
end-point (cdr expand)))
(when (if (eq thing 'word)
(not (or (sniem-pair--pair-p current-char t t)
(funcall split-char-p current-char)))
(and (not (funcall split-char-p current-char))
(not (sniem-pair--pair-p current-char t))))
(setq can-enter t))
(when can-enter
(catch 'stop
;; When expanding, firstly goto the end-point to expand forward
(if expand
(goto-char end-point)
(funcall move-command))
(while t
(setq current-char (following-char))
;; Check whether the expanding can still be executed.
(if (not (null sniem-mark-next-expanding))
;; Handle multi-character expanding connectors.
(let ((remain-characters (if (eq move-command 'forward-char)
(cdr sniem-mark-next-expanding)
(reverse (cdr sniem-mark-next-expanding)))))
(cond ((null remain-characters)
(setq sniem-mark-next-expanding nil
stop-current-expanding nil)
(when (numberp expand-times)
(setq expand-times (1- expand-times))))
((string-equal (car remain-characters)
(char-to-string current-char))
(setf (cdr sniem-mark-next-expanding) (cdr remain-characters))
(setq stop-current-expanding nil))
(t
(sniem--goto-last-position move-command)
(setq sniem-mark-next-expanding nil
stop-current-expanding t))))
(setq stop-current-expanding (or (sniem-pair--pair-p
current-char (if expand-times
(gv-ref expand-times)
t)
(eq thing 'word))
(funcall split-char-p current-char))))
(when stop-current-expanding
(if (eq move-command 'forward-char)
(progn
(setq move-command 'backward-char
end-point (point))
(goto-char (if expand
start-point
enter-point)))
(setq start-point (1+ (point)))
(throw 'stop t)))
(when (and (eobp)
(eq move-command 'forward-char))
(unless (null sniem-mark-next-expanding)
(sniem--goto-last-position 'forward-char)
(setq sniem-mark-next-expanding nil))
(setq move-command 'backward-char
end-point (point))
(goto-char (if expand
start-point
enter-point)))
(when (and (bobp)
(eq move-command 'backward-char))
(unless (null sniem-mark-next-expanding)
(sniem--goto-last-position 'backward-char)
(setq sniem-mark-next-expanding nil))
(setq start-point (point))
(throw 'stop t))
(funcall move-command)))))
(when (and start-point end-point)
(cons start-point end-point))))
(defun sniem--goto-last-position (direction)
"Goto last position with current DIRECTION.
This function can be used after useless expanding for
multi-character connnectors."
(let ((goto-direction (if (eq direction 'forward-char)
'backward-char
'forward-char)))
(unless (null sniem-mark-next-expanding)
(funcall goto-direction (- (car sniem-mark-next-expanding)
(length (cdr sniem-mark-next-expanding)))))))
(defun sniem-split-line ()
"Split marked content to three lines."
(interactive)
(when (region-active-p)
(let ((start-overlay (make-overlay (region-beginning) (1+ (region-beginning))))
(end-overlay (make-overlay (1- (region-end)) (region-end)))
target-depth temp)
(unless sniem-enter-command
(sniem-open-line--init-command)
(sniem-change-mode 'normal))
;; Get the depth of target paren.
(deactivate-mark)
(goto-char (overlay-start start-overlay))
(setq target-depth (1+ (car (syntax-ppss))))
(forward-char)
(call-interactively sniem-enter-command)
(goto-char (overlay-start end-overlay))
(call-interactively sniem-enter-command)
;; Split the content line.
(goto-char (overlay-start start-overlay))
(catch 'find-se-stop
(while (search-forward "," nil t)
(unless (< (point) (overlay-start end-overlay))
(throw 'find-se-stop t))
(setq temp (syntax-ppss))
(when (and (= (car temp) target-depth)
(not (nth 3 temp))
(not (= (point) (line-end-position))))
(call-interactively sniem-enter-command))))
(goto-char (overlay-start start-overlay))
(delete-overlay start-overlay)
(delete-overlay end-overlay))))
(defun sniem-up-down-case ()
"Up or down case."
(interactive)
(if (region-active-p)
(let ((contents (buffer-substring-no-properties (region-beginning)
(region-end))))
(delete-region (region-beginning) (region-end))
(dolist (char (string-to-list contents))
(insert-char (if (eq (upcase char) char)
(downcase char)
(upcase char)))))
(let ((char (following-char)))
(delete-char 1)
(insert-char (if (eq (upcase char) char)
(downcase char)
(upcase char)))))
(sniem-search--refresh-overlay-display))
(defun sniem-replace-char (char)
"Replace the CHAR under cursor."
(interactive "c")
(delete-char 1)
(insert-char char)
(sniem-backward-char nil t)
(sniem-search--modify-cancel-selection))
(defun sniem-replace-word (&optional replace-word)
"Replace the word under cursor.
When Optional REPLACE-WORD is non-nil, replace original one with it."
(interactive)
(sniem-search--cancel-selection)
(let* ((word (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word t)))
(word-points (if (region-active-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'word)))
(marked-contents '("nil"))
replace-region-p replaced ovs ov c-ov tmp)
(when (and (region-active-p)
(car sniem-mark-content-overlay)
(listp (car sniem-mark-content-overlay))
(null replace-word))
(setq ovs (car sniem-mark-content-overlay))
(setq replace-region-p
(completing-read "Enter the marked-content or nil to input word: "
(progn
(dotimes (i (length ovs))
(setq ov (nth i ovs))
(setq marked-contents
(append marked-contents
(list (format
"%d- %s"
(1+ i)
(buffer-substring-no-properties
(overlay-start ov) (overlay-end ov)))))))
marked-contents))))
(if (and (not (string= "nil" replace-region-p))
(null replace-word))
(progn
(save-mark-and-excursion
(setq tmp (progn (string-match "^\\(.*\\)- \\(.*\\)" replace-region-p)
(match-string 1 replace-region-p))
c-ov (nth (1- (string-to-number tmp))
ovs)) ;`tmp' is the number of the overlay, `c-ov' is the overlay needs to replace.
(goto-char (overlay-start c-ov))
(setq replaced (substring replace-region-p (+ 2 (length tmp)))) ;Set the rest of the contents for replace.
(delete-region (overlay-start c-ov)
(overlay-end c-ov))
(insert word)
(delete-overlay c-ov)
(setq ov (make-overlay (- (point) (length word)) (point)))
(setf (car sniem-mark-content-overlay)
(append (list ov) (delete c-ov ovs)))
(overlay-put ov 'face 'region))
(delete-region (region-beginning) (region-end))
(deactivate-mark))
(setq replaced (or replace-word
(read-string "Enter the new word: " word)))
(delete-region (car word-points) (cdr word-points)))
(insert replaced)))
(defun sniem-delete-char ()
"Delete the char under cursor."
(interactive)
(if (eolp)
(delete-char -1)
(unless (or (= (following-char) 32)
(= (following-char) 10))
(kill-ring-save (point) (1+ (point))))
(delete-char 1))
(sniem-search--modify-cancel-selection t))
(defun sniem-delete (action)
"Delete ACTION."
(interactive (list (if sniem-delete-edit
(progn
(setq-local sniem-delete-edit nil)
?p)
(if (region-active-p)
t
(read-char sniem-delete-message)))))
(sniem-search--modify-cancel-selection t)
(pcase action
((pred symbolp)
(sniem-delete-region
(region-beginning)
(if (save-mark-and-excursion
(sniem-end-of-mark)
(and (not (eobp))
(= (point) (line-end-position))
sniem-mark-line))
(1+ (region-end))
(region-end))))
(100 (if (= (line-beginning-position) (line-end-position))
(progn
(if (bobp)
(delete-char 1)
(delete-char -1))
(when (eolp)
(forward-line)))
(sniem-delete-region (line-beginning-position)
(if (= (line-end-position) (point-max))
(line-end-position)
(1+ (line-end-position)))))
(when (eobp)
(beginning-of-line)))
(9 (sniem-delete-region (line-beginning-position) (line-end-position)))
(112 (sniem-delete-region sniem-last-point (point))
(when sniem-last-point-locked
(sniem-lock-unlock-last-point)))
(101 (setq-local sniem-delete-edit t)
(sniem-lock-unlock-last-point t))))
(defun sniem-delete-in-region ()
"Delete in region."
(interactive)
(when (region-active-p)
(when (= (point) (region-beginning))
(sniem-end-of-mark))
(push-mark (1+ (region-beginning)) t t)
(goto-char (1- (region-end)))
(sniem-delete t)))
(defun sniem-delete-region (start end)
"Like `delete-region', but it will eval `kill-ring-save' to copy the region.
Argument START is the start point of the region.
Argument END is the end point of the region."
(kill-ring-save start end)
(delete-region start end))
(defun sniem-change (action)
"Change contents.
Argument ACTION is the action of change."
(interactive (list (if sniem-change-edit
(progn
(setq-local sniem-change-edit nil)
112)
(if (region-active-p)
t
(read-char sniem-change-message)))))
(pcase action
((pred symbolp) (sniem-delete t) (sniem-insert))
(99 (sniem-delete 9) (indent-according-to-mode) (sniem-insert))
(112 (sniem-delete 112) (sniem-insert))
(101 (setq-local sniem-change-edit t)
(sniem-lock-unlock-last-point t))))
(defun sniem-change-in-region ()
"Change in region."
(interactive)
(when (region-active-p)
(when (= (point) (region-beginning))
(sniem-end-of-mark))
(push-mark (1+ (region-beginning)) t t)
(goto-char (1- (region-end)))
(sniem-change t)))
(defun sniem-yank (action &optional special)
"Yank ACTION.
If SPECIAL is non-nil, yank it to the special clipboard."
(interactive (list (if (region-active-p)
t
(read-char sniem-yank-message))))
(let ((yank-function (if special
#'sniem-yank--special
#'kill-ring-save)))
(pcase action
((pred symbolp) (funcall yank-function (region-beginning) (region-end)))
(121 (funcall yank-function (line-beginning-position)
(if (= (point-max) (line-end-position))
(line-end-position)
(1+ (line-end-position)))))
(112 (funcall yank-function sniem-last-point (point))
(when sniem-last-point-locked
(sniem-lock-unlock-last-point))))))
(defun sniem-yank-in-region (&optional special)
"Yank in region.
If SPECIAL is non-nil, yank it to the special clipboard."
(interactive)
(when (region-active-p)
(funcall (if special
#'sniem-yank--special
#'kill-ring-save)
(1+ (region-beginning)) (1- (region-end)))))
(defun sniem-yank--special (beg end)
"Yank content from BEG to END into special clipboard."
(setq sniem-special-clipboard
(append sniem-special-clipboard
(list
(buffer-substring-no-properties
beg end))))
(when (region-active-p)
(deactivate-mark)))
(defun sniem-paste (&optional n special)
"Paste the N content in `kill-ring'.
If SPECIAL is non-nil, paste from the special clipboard."
(interactive "P")
(let ((i 0)
;; For region content replace
(regionp (when (region-active-p)
(cons (region-beginning) (region-end))))
(tmp (current-kill 0)))
;; Append content copied from other applications into the kill-ring
(unless (string-equal tmp (car kill-ring))
(setq kill-ring (append (list tmp) kill-ring)))
(unless n
;; Have a check for operation of page or the selection of target content.
(while (or (null n) (stringp n))
(setq n (char-to-string
(read-char (format "%s:%d%s"
(sniem-paste--output-contents
i
special)
(1+ (/ i 4))
sniem-paste-message))))
(pcase n
("n" (setq i (+ i 4)))
("p" (if (>= i 4)
(setq i (- i 4))
(setq n 1)))
("q" (keyboard-quit))
(_ (setq n (string-to-number n))))))
(sniem-search--modify-cancel-selection t) ;Exit search status
(when regionp
(goto-char (cdr regionp))
(push-mark (car regionp) t t)
(sniem-delete t))
(insert (nth (if (and regionp
(null special))
(+ n i)
(1- (+ n i)))
;; Select the clipboard
(if special
sniem-special-clipboard
kill-ring)))))
(defun sniem-paste--output-contents (n special)
"Output contents for `sniem-paste'.
Argument N is the page of the contents.
If SPECIAL is non-nil, yank it to the special clipboard."
(let (content c tmp)
(dotimes (i 4)
;; `c' is a single content that will be show.
(setq c (format "%d: %s"
(1+ i)
(ignore-errors ;For the condition when the result is nil
(replace-regexp-in-string ;Replace `\n' to space
"\n"
" "
(sniem-paste--remove-ln
(nth (+ i n)
(if special
sniem-special-clipboard
kill-ring)))))))
;; Limit the length of content that will be shown
(when (> (length c) (frame-width))
(setq c (concat
(substring c 0 (- (frame-width) 4))
"...")))
(setq content (concat content c "\n")))
content))
(defun sniem-paste--remove-ln (string)
"Remove \n which is at the end of STRING.
Of course, the precondition is that STRING includes it."
(if (string-suffix-p "\n" string)
(substring string 0 -1)
string))
(defun sniem-paste-in-region ()
"Paste the `kill-ring' content in region."
(interactive)
(when (region-active-p)
(when (= (region-beginning) (point))
(sniem-end-of-mark))
(push-mark (1+ (region-beginning)) t t)
(goto-char (1- (region-end)))
(sniem-paste)))
(defun sniem-join ()
"Change LINE to one line."
(interactive)
(sniem-search--cancel-selection)
(if (region-active-p)
(let ((start-overlay (make-overlay (region-beginning) (1+ (region-beginning))))
(end-overlay (make-overlay (1- (region-end)) (region-end))))
(deactivate-mark)
(goto-char (overlay-start end-overlay))
(back-to-indentation)
(while (> (point) (overlay-start start-overlay))
(pcase (char-before)
((or 10 32 9) (delete-backward-char 1))
(c
(when (and (= c 44)
(not (= (point) (overlay-start end-overlay))))
(insert " "))
(back-to-indentation))))
(goto-char (overlay-start start-overlay))
(delete-overlay start-overlay)
(delete-overlay end-overlay))
(let ((last-point (point)))
(if (bolp)
(if (save-mark-and-excursion
(forward-line -1)
(= (line-beginning-position) (line-end-position)))
(forward-line -1)
(backward-char))
(backward-char)
(unless (or (= 10 (following-char))
(= 32 (following-char))
(= 9 (following-char)))
(user-error "[Sniem]: The current position doesn't need join!")))
(while (or (= 10 (following-char))
(= 32 (following-char))
(= 9 (following-char)))
(backward-char))
(forward-char)
(push-mark last-point t t))))
(defun sniem-macro (action)
"Macro ACTION."
(interactive (list (unless defining-kbd-macro
(read-char sniem-macro-message))))
(catch 'stop
(when defining-kbd-macro
(end-kbd-macro)
;; If the `sniem-kmacro-range' is exists, call the macro to the lines
(when sniem-kmacro-range
(sniem-macro--more-line-execute t)))
(when (region-active-p)
(sniem-search--cancel-selection)
(if (= action ?q)
(if (= (line-number-at-pos (region-beginning))
(line-number-at-pos (region-end)))
(setq-local sniem-kmacro-mark-content
(buffer-substring-no-properties (region-beginning) (region-end)))
(setq-local sniem-kmacro-range
(make-overlay
(save-mark-and-excursion
(goto-char (region-beginning))
(forward-line)
(line-beginning-position))
(region-end)))
(goto-char (region-beginning))
(deactivate-mark))
(if (= (line-number-at-pos (region-beginning))
(line-number-at-pos (region-end)))
(setq-local sniem-kmacro-mark-content
(buffer-substring-no-properties (region-beginning) (region-end)))
(sniem-macro--more-line-execute))))
(pcase action
(113 (call-interactively #'start-kbd-macro))
(101 (if sniem-locked-macro
(call-interactively sniem-locked-macro)
(call-last-kbd-macro)))
(110 (call-interactively #'name-last-kbd-macro))
(105 (let ((macro (sniem-macro--get-kbd-macros))
(file-content ""))
(unless sniem-macro-file
(setq sniem-macro-file (read-file-name "Enter the macro file you want: ")))
(if (file-exists-p sniem-macro-file)
(with-temp-buffer
(insert-file-contents sniem-macro-file)
(setq file-content (buffer-string)))
(make-empty-file sniem-macro-file))
(with-temp-file sniem-macro-file
(goto-char (point-min))
(insert-kbd-macro macro)
(insert file-content))))
(108 (setq sniem-locked-macro
(if sniem-locked-macro
nil
(sniem-macro--get-kbd-macros)))
(message "[Sniem]: %s locked macro."
(if sniem-locked-macro
"Set"
"Unset")))
(46 (setq sniem-locked-macro (sniem-macro--get-kbd-macros)))
(99 (call-interactively (sniem-macro--get-kbd-macros)))
(59 (when sniem-kmacro-range
(delete-overlay sniem-kmacro-range)
(setq-local sniem-kmacro-range nil))
(when sniem-kmacro-mark-content
(setq-local sniem-kmacro-mark-content nil))))))
(defun sniem-macro--more-line-execute (&optional overlay-p)
"More line execute macro.
When OVERLAY-P is non-nil, use `sniem-macro-range'."
(sniem-macro--apply-to-lines (if overlay-p
(overlay-start sniem-kmacro-range)
(save-mark-and-excursion
(sniem-beg-of-mark)
(line-beginning-position)))
(if overlay-p
(overlay-end sniem-kmacro-range)
(save-mark-and-excursion
(sniem-end-of-mark)
(when (= (line-beginning-position)
(line-end-position))
(forward-line))
(line-end-position)))
sniem-locked-macro)
(when overlay-p
(delete-overlay sniem-kmacro-range)
(setq-local sniem-kmacro-range nil))
;; To skip a unneccesary execution.
(throw 'stop t))
(defun sniem-macro--apply-to-lines (top bottom &optional macro)
"Apply the MACRO to lines from TOP to BOTTOM."
(when (region-active-p)
(deactivate-mark))
(when (> top bottom)
(let ((tmp top))
(setq top bottom
bottom tmp)))
(unless macro
(setq macro last-kbd-macro))
(goto-char top)
(let (top-line end-line move-line)
(setq top-line (line-number-at-pos top)
end-line (line-number-at-pos bottom))
(save-mark-and-excursion
(goto-char bottom)
(when (= (line-beginning-position) (line-end-position))
(setq end-line (1- end-line))))
(setq bottom (- end-line top-line))
(message "%S, %S" top bottom)
(while (>= bottom 0)
(setq move-line (make-overlay (line-beginning-position) (1+ (line-end-position))))
(execute-kbd-macro macro)
(goto-char (overlay-end move-line))
(delete-overlay move-line)
(setq bottom (1- bottom)))))
(defun sniem-macro--get-kbd-macros ()
"Get the kbd macro from kmacros."
(intern
(completing-read "Enter the macro: "
obarray
#'kmacro-keyboard-macro-p
t)))
(defun sniem-pair (prefix &optional add)
"Modify the region's pair.
Argument PREFIX is the prefix of the pair.
Optional Argument ADD means forced to add the pair."
(interactive (list (let ((var (read-char sniem-pair-message)))
(cond ((= var 97) ;Forcibly add mode
(cons (read-char
(if sniem-pair-message
"Enter the pair:"
nil))
t))
((= var 115) ;Add or delete space mode
(cons t 's))
(t var)))))
(sniem-search--cancel-selection)
(if (eq (cdr-safe prefix) 's)
(save-mark-and-excursion
(sniem-space))
(when (cdr-safe prefix)
(setq add t
prefix (car prefix)))
(let ((second (unless (= 32 prefix)
(sniem-object-catch--get-second-char (char-to-string prefix))))
(prefix-point (region-beginning))
(second-point (region-end))
(prefix-char (buffer-substring-no-properties (region-beginning) (1+ (region-beginning)))))
(if (and (null second)
(/= prefix 32))
(user-error "[Sniem]: The pair is not exists in `sniem-object-catch-global-symbol-alist'!")
(save-mark-and-excursion
(goto-char prefix-point)
(when (and (null add)
(sniem-pair--pair-p prefix-char))
(delete-char 1))
(unless (= prefix 32)
(insert prefix))
(goto-char (if (= prefix 32)
(1- second-point)
second-point))
(if (and (null add)
(sniem-pair--pair-p prefix-char))
(delete-char -1)
(forward-char))
(unless (= prefix 32)
(insert second)))))))
(defun sniem-pair--pair-p (char-string &optional connector-check wordp)
"Check if CHAR-STRING is pair.
When CONNECTOR-CHECK is t, check if the pair is a special
connector of current mode after check pair.
When CONNECTOR-CHECK is a list (pointer),
check if CHAR-STRING is expansion connector
after normal check failed.
When WORDP is non-nil, connectors will be regarded as pair."
(if wordp
(sniem-shift--not-alpha-p char-string t)
(when (characterp char-string)
(setq char-string (char-to-string char-string)))
(let (pairp connectorp connectors)
(catch 'result
(dolist (pair sniem-object-catch-global-symbol-alist)
(when (and (stringp (car pair))
(or (string= (car pair) char-string)
(string= (cdr pair) char-string)))
(throw 'result (setq pairp t))))
(dolist (mode-pair (alist-get major-mode
sniem-object-catch-global-symbol-alist))
(when (or (equal (car mode-pair) char-string)
(equal (cdr mode-pair) char-string))
(throw 'result (setq pairp t)))))
(when connector-check
(setq connectors (sniem-mark--mode-alist-get))
(setq connectorp (sniem--mems char-string
(append (alist-get 'global sniem-mark-connectors)
connectors)))
(when (and (null connectorp)
(consp connector-check)
(not (eq 0 (gv-deref connector-check))))
(catch 'stop
(dolist (c (plist-get connectors :expand))
(if (stringp c)
(when (string-equal c char-string)
(setq connectorp t)
;; Decrease the expanding times.
(let ((expand-times (gv-deref connector-check)))
(when (numberp expand-times)
(setf (gv-deref connector-check) (1- expand-times))))
(throw 'stop t))
(cond ((string-equal (car c) char-string)
(setq connectorp t
sniem-mark-next-expanding (cons (length c) (cdr c)))
(throw 'stop t))
((string-equal (car (last c)) char-string)
(setq connectorp t
sniem-mark-next-expanding (cons (length c) (butlast c)))
(throw 'stop t))))))))
(or (and pairp (null connectorp))
(and (null connectorp)
(sniem-shift--not-alpha-p char-string t))))))
(defun sniem-mark--mode-alist-get ()
"Try getting the connectors of current mode."
(catch 'connectors
(dolist (item sniem-mark-connectors)
(when (derived-mode-p (car item))
(throw 'connectors (cdr item))))))
(defun sniem-search (content &optional regexp-search)
"Search the CONTENT.
When REGEXP-SEARCH is non-nil, add regexp for content automatically."
(interactive (list (completing-read "Enter the search content: "
regexp-search-ring)
current-prefix-arg))
(when regexp-search
(setq content (sniem-search--regexp-content content t)))
(unless (ignore-errors
(sniem-next-word nil nil content))
(deactivate-mark)
(sniem-search--cancel-selection)
(unless (ignore-errors
(sniem-prev-word nil nil content))
(deactivate-mark)
(sniem-search--cancel-selection)
(message "[Sniem]: The content %S is not exsits in current buffer." content))))
(defun sniem--string-equal (string1 string2)
"Like `string-equal', but don't care the case.
STRING1 and STRING2 are the strings to compair."
(string-equal (downcase string1) (downcase string2)))
(defun sniem-space ()
"Add or delete space around the marked region."
(interactive)
(let ((marked-content (buffer-substring-no-properties
(region-beginning)
(region-end))))
(if (and (string-prefix-p " " marked-content)
(string-suffix-p " " marked-content))
(sniem-replace-word (substring marked-content 1 -1))
(sniem-replace-word (format " %s " marked-content)))))
;;; Motions
(sniem-define-motion sniem-beginning-of-line ()
"Beginning of line."
(if (bolp)
(call-interactively #'indent-for-tab-command)
(beginning-of-line)))
(sniem-define-motion sniem-end-of-line ()
"End of line."
(end-of-line))
(sniem-define-motion sniem-forward-char (&optional n)
"Forward char."
(interactive "P")
(setq n (or n 1))
(catch 'end
(while (/= n 0)
(if (eolp)
(throw 'end t)
(forward-char)
(setq n (1- n))))))
(sniem-define-motion sniem-5-forward-char ()
"Eval `sniem-forward-char' 5 times."
(sniem-forward-char 5 t))
(sniem-define-motion sniem-backward-char (&optional n)
"Backward char."
(interactive "P")
(setq n (or n 1))
(catch 'beg
(while (/= n 0)
(if (bolp)
(throw 'beg t)
(backward-char)
(setq n (1- n))))))
(sniem-define-motion sniem-5-backward-char ()
"Eval `sniem-backward-char' 5 times."
(sniem-backward-char 5 t))
(sniem-define-motion sniem-prev-line (&optional n)
"Previous line."
(interactive "P")
(let ((line (line-number-at-pos)))
(setq n (or n 1))
(unless (bobp)
(sniem-line-move (- n)))
(when (and (region-active-p) sniem-mark-line)
(while (= (line-number-at-pos) line)
(sniem-line-move -1))
(if (= (region-beginning) (point))
(beginning-of-line)
(end-of-line)))))
(sniem-define-motion sniem-5-prev-line ()
"Eval `sniem-prev-line' 5 times."
(sniem-prev-line 5 t))
(sniem-define-motion sniem-next-line (&optional n)
"Next line."
(interactive "P")
(let ((line (line-number-at-pos)))
(setq n (or n 1))
(unless (eobp)