-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzsh-helix-mode.zsh
2886 lines (2510 loc) · 81.6 KB
/
zsh-helix-mode.zsh
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
# Configurations ===============================================================
# Current default theme is catppuccin_mocha :)
# The plugin print these variables after mode changes
# Reset then pastel blue block cursor
: "${ZHM_CURSOR_NORMAL:=\e[0m\e[2 q\e]12;#B4BEFE\a}"
# Reset then pastel red block cursor
: "${ZHM_CURSOR_SELECT:=\e[0m\e[2 q\e]12;#F2CDCD\a}"
# Reset then white vertical blinking cursor
: "${ZHM_CURSOR_INSERT:=\e[0m\e[5 q\e]12;white\a}"
# Uses the syntax from https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting
: "${ZHM_STYLE_CURSOR_NORMAL:=fg=black,bg=#b4befe}"
: "${ZHM_STYLE_CURSOR_SELECT:=fg=black,bg=#f2cdcd}"
: "${ZHM_STYLE_CURSOR_INSERT:=fg=black,bg=#a6e3a1}"
: "${ZHM_STYLE_OTHER_CURSOR_NORMAL:=fg=black,bg=#878ec0}"
: "${ZHM_STYLE_OTHER_CURSOR_SELECT:=fg=black,bg=#b5a6a8}"
: "${ZHM_STYLE_OTHER_CURSOR_INSERT:=fg=black,bg=#7ea87f}"
: "${ZHM_STYLE_SELECTION:=fg=white,bg=#45475a}"
# Clipboard commands
if [[ -n $WAYLAND_DISPLAY ]]; then
: "${ZHM_CLIPBOARD_PIPE_CONTENT_TO:=wl-copy}"
: "${ZHM_CLIPBOARD_READ_CONTENT_FROM:=wl-paste --no-newline}"
elif [[ -n $DISPLAY ]]; then
: "${ZHM_CLIPBOARD_PIPE_CONTENT_TO:=xclip -sel clip}"
: "${ZHM_CLIPBOARD_READ_CONTENT_FROM:=xclip -o -sel clip}"
else
: "${ZHM_CLIPBOARD_PIPE_CONTENT_TO:=}"
: "${ZHM_CLIPBOARD_READ_CONTENT_FROM:=}"
fi
# Global variables =============================================================
export ZHM_MODE=insert
export ZHM_MULTILINE=0
ZHM_PRIMARY_CURSOR_IDX=1
zhm_cursors_pos=(0)
zhm_cursors_selection_left=(0)
zhm_cursors_selection_right=(0)
ZHM_RECORD_CHANGES=1
ZHM_PRIMARY_CURSOR_IDX_PREV=1
zhm_cursors_pos_prev=(0)
zhm_cursors_selection_left_prev=(0)
zhm_cursors_selection_right_prev=(0)
ZHM_CHANGES_HISTORY_IDX=1
zhm_changes_history_buffer=("")
zhm_changes_history_cursors_idx_starts_pre=(1)
zhm_changes_history_cursors_count_pre=(1)
zhm_changes_history_cursors_pos_pre=(0)
zhm_changes_history_cursors_selection_left_pre=(0)
zhm_changes_history_cursors_selection_right_pre=(0)
zhm_changes_history_primary_cursor_pre=(1)
zhm_changes_history_cursors_idx_starts_post=(1)
zhm_changes_history_cursors_count_post=(1)
zhm_changes_history_cursors_pos_post=(0)
zhm_changes_history_cursors_selection_left_post=(0)
zhm_changes_history_cursors_selection_right_post=(0)
zhm_changes_history_primary_cursor_post=(1)
zhm_cursors_last_moved_x=(0)
declare -gA zhm_registers
declare -gA zhm_registers_max
ZHM_ACCEPTING=0
ZHM_HOOK_IKNOWWHATIMDOING=0
ZHM_PROMPT_PREDISPLAY_OFFSET=0
ZHM_IN_PROMPT=0
ZHM_PROMPT_HOOK=
zhm_prompt_region_highlight=()
ZHM_BUFFER_BEFORE_PROMPT=
ZHM_MESSAGE_CLEAR_DELAY=0
ZHM_LAST_MOTION=""
ZHM_LAST_MOTION_CHAR=""
ZHM_CURRENT_REGISTER="\""
ZHM_JUST_SELECTED_REGISTER=0
# Utility functions ============================================================
function __zhm_read_register {
local register=$1
local idx=$2
case "$register" in
"_")
;;
"#")
printf '%s\n' "$idx"
;;
".")
printf '%s\n' "$BUFFER[$((zhm_cursors_selection_left[$idx] + 1)),$((zhm_cursors_selection_right[$idx] + 1))]"
;;
"%")
printf '%s\n' "$(pwd)"
;;
"+")
printf '%s\n' "$(eval $ZHM_CLIPBOARD_READ_CONTENT_FROM)"
;;
*)
if (( ! ${+zhm_registers_max["$register"]} )); then
return
fi
local idx=$(( idx <= zhm_registers_max["$register"] ? idx : zhm_registers_max["$register"] ))
print "${zhm_registers["${register}_${idx}"]}"
;;
esac
}
function __zhm_write_register {
local register=$1
shift
case "$register" in
"_"| "#" | "." | "%")
;;
"+")
printf '%s\n' "$@" | eval $ZHM_CLIPBOARD_PIPE_CONTENT_TO
;;
*)
for i in {1..$#}; do
zhm_registers["${register}_$i"]="${(P)i}"
done
zhm_registers_max["$register"]=$#
;;
esac
}
function __zhm_update_region_highlight {
region_highlight=(
"${(@)region_highlight:#*memo=zsh-helix-mode}"
"${(@)zhm_prompt_region_highlight}"
)
local offset=0
local prefix=""
if (( ZHM_IN_PROMPT == 1 )); then
prefix="P"
offset=$ZHM_PROMPT_PREDISPLAY_OFFSET
fi
for i in {1..$#zhm_cursors_pos}; do
local left=$zhm_cursors_selection_left[$i]
local right=$zhm_cursors_selection_right[$i]
local cursor=$zhm_cursors_pos[$i]
if (( cursor == left && cursor == right )); then
continue
elif (( cursor == left )) && [[ $ZHM_MODE != insert ]]; then
# ZHM_MODE != insert is necessary to have proper highlighting when
# cursor is a bar
# This is hardcoded. No one is going to use a bar for other mode.. right?
left=$((left + 1))
elif (( cursor == right )); then
right=$((right - 1))
fi
left=$(( left + offset ))
right=$(( right + 1 + offset ))
local highlight="$prefix$left $right $ZHM_STYLE_SELECTION memo=zsh-helix-mode"
region_highlight+=("$highlight")
done
for i in {1..$#zhm_cursors_pos}; do
local cursor=$(( zhm_cursors_pos[i] + offset ))
local cursor_right=$(( cursor + 1 ))
local style=
if (( i != ZHM_PRIMARY_CURSOR_IDX )); then
case $ZHM_MODE in
normal)
style="$ZHM_STYLE_OTHER_CURSOR_NORMAL"
;;
select)
style="$ZHM_STYLE_OTHER_CURSOR_SELECT"
;;
insert)
style="$ZHM_STYLE_OTHER_CURSOR_INSERT"
;;
esac
else
if (( ZHM_IN_PROMPT == 0 )); then
continue
fi
case $ZHM_MODE in
normal)
style="$ZHM_STYLE_CURSOR_NORMAL"
;;
select)
style="$ZHM_STYLE_CURSOR_SELECT"
;;
insert)
style="$ZHM_STYLE_CURSOR_INSERT"
;;
esac
fi
region_highlight+=("$prefix$cursor $cursor_right $style memo=zsh-helix-mode")
done
}
function __zhm_trim_history {
local keep=$((ZHM_CHANGES_HISTORY_IDX))
zhm_changes_history_buffer=("${(@)zhm_changes_history_buffer[1,$keep]}")
local idx=$((zhm_changes_history_cursors_idx_starts_pre[$((ZHM_CHANGES_HISTORY_IDX + 1))] - 1))
zhm_changes_history_cursors_pos_pre=($zhm_changes_history_cursors_pos_pre[1,$idx])
zhm_changes_history_cursors_selection_left_pre=($zhm_changes_history_cursors_selection_left_pre[1,$idx])
zhm_changes_history_cursors_selection_right_pre=($zhm_changes_history_cursors_selection_right_pre[1,$idx])
zhm_changes_history_cursors_idx_starts_pre=($zhm_changes_history_cursors_idx_starts_pre[1,$keep])
zhm_changes_history_cursors_count_pre=($zhm_changes_history_cursors_count_pre[1,$keep])
zhm_changes_history_primary_cursor_pre=($zhm_changes_history_primary_cursor_post[1,$keep])
local idx=$((zhm_changes_history_cursors_idx_starts_post[$((ZHM_CHANGES_HISTORY_IDX + 1))] - 1))
zhm_changes_history_cursors_pos_post=($zhm_changes_history_cursors_pos_post[1,$idx])
zhm_changes_history_cursors_selection_left_post=($zhm_changes_history_cursors_selection_left_post[1,$idx])
zhm_changes_history_cursors_selection_right_post=($zhm_changes_history_cursors_selection_right_post[1,$idx])
zhm_changes_history_cursors_idx_starts_post=($zhm_changes_history_cursors_idx_starts_post[1,$keep])
zhm_changes_history_cursors_count_post=($zhm_changes_history_cursors_count_post[1,$keep])
zhm_changes_history_primary_cursor_post=($zhm_changes_history_primary_cursor_post[1,$keep])
}
function __zhm_update_changes_history_pre {
zhm_changes_history_cursors_idx_starts_pre+=(
$(( ${#zhm_changes_history_cursors_pos_pre} + 1 ))
)
zhm_changes_history_cursors_count_pre+=(
"${#zhm_cursors_pos_prev}"
)
zhm_changes_history_cursors_pos_pre+=(
"${zhm_cursors_pos_prev[@]}"
)
zhm_changes_history_cursors_selection_left_pre+=(
"${zhm_cursors_selection_left_prev[@]}")
zhm_changes_history_cursors_selection_right_pre+=(
"${zhm_cursors_selection_right_prev[@]}"
)
zhm_changes_history_primary_cursor_pre+=(
$ZHM_PRIMARY_CURSOR_IDX_PREV
)
}
function __zhm_update_changes_history_post {
zhm_changes_history_buffer+=("$BUFFER")
zhm_changes_history_cursors_idx_starts_post+=(
$(( ${#zhm_changes_history_cursors_pos_post} + 1 ))
)
zhm_changes_history_cursors_count_post+=(
"${#zhm_cursors_pos}"
)
zhm_changes_history_cursors_pos_post+=(
"${zhm_cursors_pos[@]}"
)
zhm_changes_history_cursors_selection_left_post+=(
"${zhm_cursors_selection_left[@]}"
)
zhm_changes_history_cursors_selection_right_post+=(
"${zhm_cursors_selection_right[@]}"
)
zhm_changes_history_primary_cursor_post+=(
$ZHM_PRIMARY_CURSOR_IDX
)
ZHM_CHANGES_HISTORY_IDX=$((ZHM_CHANGES_HISTORY_IDX + 1))
}
function __zhm_mode_normal {
bindkey -A hxnor main
ZHM_MODE=normal
printf "$ZHM_CURSOR_NORMAL"
}
function __zhm_mode_select {
bindkey -A hxnor main
ZHM_MODE=select
printf "$ZHM_CURSOR_SELECT"
}
function __zhm_mode_insert {
bindkey -A hxins main
ZHM_MODE=insert
printf "$ZHM_CURSOR_INSERT"
}
function __zhm_goto {
local idx=$1
local cursor=$2
local left=$zhm_cursors_selection_left[$idx]
local right=$zhm_cursors_selection_right[$idx]
local prev_cursor=$zhm_cursors_pos[$idx]
if (( cursor < 0 )); then
cursor=0
fi
if (( cursor > ${#BUFFER} )); then
cursor=${#BUFFER}
fi
if (( idx == ZHM_PRIMARY_CURSOR_IDX )); then
CURSOR=$cursor
fi
zhm_cursors_pos[$idx]=$cursor
if [[ $ZHM_MODE != select ]]; then
zhm_cursors_selection_left[$idx]=$cursor
zhm_cursors_selection_right[$idx]=$cursor
elif (( prev_cursor == left )); then
if (( cursor <= right )); then
zhm_cursors_selection_left[$idx]=$cursor
else
zhm_cursors_selection_left[$idx]=$right
zhm_cursors_selection_right[$idx]=$cursor
fi
elif (( prev_cursor == right )); then
if (( cursor >= left)); then
zhm_cursors_selection_right[$idx]=$cursor
else
zhm_cursors_selection_right[$idx]=$left
zhm_cursors_selection_left[$idx]=$cursor
fi
fi
}
function __zhm_trailing_goto {
local idx=$1
local cursor=$2
if [[ $ZHM_MODE == select ]]; then
__zhm_goto $idx $cursor
return
fi
if (( cursor < 0 )); then
cursor=0
fi
if (( cursor > ${#BUFFER} )); then
cursor=${#BUFFER}
fi
local skip=$3
local left=$zhm_cursors_selection_left[$idx]
local right=$zhm_cursors_selection_right[$idx]
local prev_cursor=$zhm_cursors_pos[$idx]
if (( idx == ZHM_PRIMARY_CURSOR_IDX )); then
CURSOR=$cursor
fi
zhm_cursors_pos[$idx]=$cursor
if (( cursor > prev_cursor )); then
zhm_cursors_selection_left[$idx]=$((prev_cursor + skip))
zhm_cursors_selection_right[$idx]=$cursor
elif (( cursor < prev_cursor)); then
zhm_cursors_selection_right[$idx]=$((prev_cursor - skip))
zhm_cursors_selection_left[$idx]=$cursor
fi
}
function __zhm_update_last_moved {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_start=$(__zhm_find_line_start $cursor "$BUFFER")
zhm_cursors_last_moved_x[$i]=$((cursor - line_start))
done
}
function __zhm_merge_cursors {
if (( ${#zhm_cursors_pos} <= 1 )); then
return
fi
local i=0
while true; do
i=$((i + 1))
if (( (i + 1) > ${#zhm_cursors_pos} )); then
break
fi
local i_cursor=$zhm_cursors_pos[$i]
local i_left=$zhm_cursors_selection_left[$i]
local i_right=$zhm_cursors_selection_right[$i]
local j=$i
while true; do
j=$((j + 1))
if (( j > ${#zhm_cursors_pos} )); then
break
fi
local j_left=$zhm_cursors_selection_left[$j]
local j_right=$zhm_cursors_selection_right[$j]
if (( !(i_right < j_left || i_left > j_right) )); then
local new_left=$((i_left > j_left ? j_left : i_left))
local new_right=$((i_right > j_right ? i_right : j_right))
local new_cursor=$((i_cursor == i_right ? new_right : new_left))
zhm_cursors_pos[$i]=$new_cursor
zhm_cursors_selection_left[$i]=$new_left
zhm_cursors_selection_right[$i]=$new_right
zhm_cursors_pos[$j]=()
zhm_cursors_selection_left[$j]=()
zhm_cursors_selection_right[$j]=()
j=$((j - 1))
fi
done
done
}
function __zhm_prompt {
local prompt="$1"
ZHM_BUFFER_BEFORE_PROMPT="$BUFFER"
local prev_cursor="$CURSOR"
if (( ZHM_MULTILINE == 0 )); then
ZHM_PROMPT_PREDISPLAY_OFFSET=0
PREDISPLAY="$ZHM_BUFFER_BEFORE_PROMPT
$prompt"
else
ZHM_PROMPT_PREDISPLAY_OFFSET=16
PREDISPLAY="-- MULTILINE --
$ZHM_BUFFER_BEFORE_PROMPT
$prompt"
fi
BUFFER=""
zhm_prompt_region_highlight=()
for highlight in $region_highlight; do
if [[ $highlight =~ "memo=zsh-helix-mode" ]]; then
continue
fi
if [[ $highlight =~ "([0-9]*) ([0-9]*) " ]]; then
local left=$((match[1] + ZHM_PROMPT_PREDISPLAY_OFFSET))
local right=$((match[2] + ZHM_PROMPT_PREDISPLAY_OFFSET))
highlight="$left $right ${highlight:$MEND}"
fi
highlight="P${highlight/memo=*/memo=zsh-helix-mode}"
zhm_prompt_region_highlight+=(
"$highlight"
)
done
region_highlight=()
ZHM_PROMPT_HOOK="$2"
ZHM_IN_PROMPT=1
printf "$ZHM_CURSOR_INSERT"
bindkey -A hxprompt main
zle recursive-edit
local stat=$?
zhm_prompt_region_highlight=()
ZHM_IN_PROMPT=0
ZHM_PROMPT_HOOK=
if (( ZHM_MULTILINE == 0 )); then
PREDISPLAY=""
else
PREDISPLAY="-- MULTILINE --
"
fi
REPLY="$BUFFER"
BUFFER="$ZHM_BUFFER_BEFORE_PROMPT"
CURSOR="$prev_cursor"
case $ZHM_MODE in
normal)
__zhm_mode_normal
;;
select)
__zhm_mode_select
;;
insert)
__zhm_mode_insert
;;
esac
__zhm_update_region_highlight
return $stat
}
function __zhm_show_message {
zle -M "$1"
ZHM_MESSAGE_CLEAR_DELAY=2
}
function __zhm_find_line_end {
setopt localoptions rematchpcre
local pos=$1
local buffer="$2"
if [[ "${buffer:$pos}" =~ '\A[^\n]*\n|\A[^\n]*' ]]; then
printf '%s\n' $((pos + MEND - 1))
fi
}
function __zhm_find_line_start {
setopt localoptions rematchpcre
local pos=$1
local buffer="$2"
if [[ "${buffer:0:$((pos + 1))}" =~ '[^\n]*\n\z|[^\n]*\z' ]]; then
printf '%s\n' $((MBEGIN - 1))
fi
}
# Mode =========================================================================
function zhm_normal {
if [[ $ZHM_MODE == insert ]]; then
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
if (( cursor > zhm_cursors_selection_right[i] )); then
zhm_cursors_pos[$i]=$((cursor - 1))
fi
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
fi
__zhm_mode_normal
__zhm_update_region_highlight
}
function zhm_select {
if [[ $ZHM_MODE == select ]]; then
__zhm_mode_normal
else
__zhm_mode_select
fi
}
function zhm_multiline {
if (( ZHM_MULTILINE == 0 )); then
PREDISPLAY="-- MULTILINE --
"
ZHM_MULTILINE=1
else
PREDISPLAY=""
ZHM_MULTILINE=0
fi
}
function zhm_insert {
for i in {1..$#zhm_cursors_pos}; do
zhm_cursors_pos[$i]=$zhm_cursors_selection_left[$i]
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
__zhm_mode_insert
__zhm_update_region_highlight
}
function zhm_insert_at_line_end {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local pos=$(__zhm_find_line_end $cursor "$BUFFER")
if (( (pos + 1) >= ${#BUFFER} )); then
pos=$((pos + 1))
fi
zhm_cursors_pos[$i]=$pos
zhm_cursors_selection_left[$i]=$pos
zhm_cursors_selection_right[$i]=$pos
done
zhm_insert
}
function zhm_insert_at_line_start {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local pos=$(__zhm_find_line_start $cursor "$BUFFER")
zhm_cursors_pos[$i]=$pos
zhm_cursors_selection_left[$i]=$pos
zhm_cursors_selection_right[$i]=$pos
done
zhm_insert
}
function zhm_open_below {
local amount_inserted=0
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_end=$(__zhm_find_line_end $((cursor + amount_inserted)) "$BUFFER")
local at_buffer_end=$(( ${#BUFFER} == (line_end + 1) ))
BUFFER="${BUFFER:0:$((line_end + 1))}"$'\n'"${BUFFER:$((line_end + 1))}"
if (( at_buffer_end == 1 )); then
zhm_cursors_pos[$i]=$((line_end + 2))
else
zhm_cursors_pos[$i]=$((line_end + 1))
fi
zhm_cursors_selection_left[$i]=$zhm_cursors_pos[$i]
zhm_cursors_selection_right[$i]=$zhm_cursors_pos[$i]
amount_inserted=$((amount_inserted + 1))
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
__zhm_update_last_moved
__zhm_mode_insert
__zhm_update_region_highlight
}
function zhm_open_above {
local amount_inserted=0
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_start=$(__zhm_find_line_start $((cursor + amount_inserted)) "$BUFFER")
BUFFER="${BUFFER:0:$line_start}"$'\n'"${BUFFER:$line_start}"
zhm_cursors_pos[$i]=$line_start
zhm_cursors_selection_left[$i]=$zhm_cursors_pos[$i]
zhm_cursors_selection_right[$i]=$zhm_cursors_pos[$i]
amount_inserted=$((amount_inserted + 1))
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
__zhm_update_last_moved
__zhm_mode_insert
__zhm_update_region_highlight
}
function zhm_append {
for i in {1..$#zhm_cursors_pos}; do
zhm_cursors_pos[$i]=$((zhm_cursors_selection_right[i] + 1))
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
__zhm_mode_insert
__zhm_update_region_highlight
}
function zhm_change {
local content=()
for i in {1..$#zhm_cursors_pos}; do
local left=$zhm_cursors_selection_left[$i]
local right=$zhm_cursors_selection_right[$i]
content+="${BUFFER[$((left + 1)),$((right + 1))]}"
done
__zhm_write_register "$ZHM_CURRENT_REGISTER" "$content[@]"
local amount_deleted=0
for i in {1..$#zhm_cursors_pos}; do
local cursor=$((zhm_cursors_pos[i] - amount_deleted))
local left=$((zhm_cursors_selection_left[i] - amount_deleted))
local right=$((zhm_cursors_selection_right[i] - amount_deleted))
BUFFER="${BUFFER:0:$left}${BUFFER:$((right + 1))}"
zhm_cursors_selection_left[$i]=$left
zhm_cursors_selection_right[$i]=$left
zhm_cursors_pos[$i]=$left
amount_deleted=$((amount_deleted + right - left + 1))
done
CURSOR=$zhm_cursors_pos[$ZHM_PRIMARY_CURSOR_IDX]
__zhm_mode_insert
__zhm_update_region_highlight
}
function zhm_command_mode {
local REPLY=
__zhm_prompt ":"
local prompt_stat=$?
if (( prompt_stat != 0 )); then
return
fi
local command=(${=REPLY})
if (( ${#command} == 0 )); then
return
fi
case "${command[1]}" in
"o" | "open")
if (( ${#command} <= 1 )); then
return
fi
local content=
content="$(cat "${command[2]}" 2>&1)"
if (( $? != 0 )); then
__zhm_show_message "$content"
return
fi
BUFFER="$content"
CURSOR=0
zhm_cursors_pos=(0)
zhm_cursors_selection_left=(0)
zhm_cursors_selection_right=(0)
zhm_cursors_last_moved_x=(0)
;;
*)
__zhm_show_message "Unknown Command"
esac
}
# Movement =====================================================================
function zhm_move_left {
for i in {1..$#zhm_cursors_pos}; do
__zhm_goto $i $((zhm_cursors_pos[i] - 1))
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_right {
for i in {1..$#zhm_cursors_pos}; do
__zhm_goto $i $((zhm_cursors_pos[i] + 1))
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_down {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_end=$(__zhm_find_line_end $cursor "$BUFFER")
local next_line_start=$((line_end + 1))
if (( next_line_start >= ${#BUFFER} )); then
continue
fi
local next_line_end=$(__zhm_find_line_end $next_line_start "$BUFFER")
local last_moved_x=$zhm_cursors_last_moved_x[$i]
local goto=$((next_line_start + last_moved_x))
__zhm_goto $i $((goto <= next_line_end ? goto : next_line_end))
done
__zhm_update_region_highlight
}
function zhm_move_down_or_history_next {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_end=$(__zhm_find_line_end $cursor "$BUFFER")
local next_line_start=$((line_end + 1))
if (( next_line_start >= ${#BUFFER} )); then
if (( i == ZHM_PRIMARY_CURSOR_IDX )); then
zhm_history_next
return
fi
continue
fi
local next_line_end=$(__zhm_find_line_end $next_line_start "$BUFFER")
local last_moved_x=$zhm_cursors_last_moved_x[$i]
local goto=$((next_line_start + last_moved_x))
__zhm_goto $i $((goto <= next_line_end ? goto : next_line_end))
done
__zhm_update_region_highlight
}
function zhm_move_up {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_start=$(__zhm_find_line_start $cursor "$BUFFER")
local prev_line_end=$((line_start - 1))
if (( prev_line_end < 0 )); then
continue
fi
local prev_line_start=$(__zhm_find_line_end $prev_line_end "$BUFFER")
local last_moved_x=$zhm_cursors_last_moved_x[$i]
local goto=$((prev_line_start + last_moved_x))
__zhm_goto $i $((goto <= prev_line_end ? goto : prev_line_end))
done
__zhm_update_region_highlight
}
function zhm_move_up_or_history_prev {
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local line_start=$(__zhm_find_line_start $cursor "$BUFFER")
local prev_line_end=$((line_start - 1))
if (( prev_line_end < 0 )); then
if (( i == ZHM_PRIMARY_CURSOR_IDX )); then
zhm_history_prev
return
fi
continue
fi
local prev_line_start=$(__zhm_find_line_start $prev_line_end "$BUFFER")
local last_moved_x=$zhm_cursors_last_moved_x[$i]
local goto=$((prev_line_start + last_moved_x))
__zhm_goto $i $((goto <= prev_line_end ? goto : prev_line_end))
done
__zhm_update_region_highlight
}
function zhm_move_next_word_start {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ '^\n*(\w+[^\S\n]*|[^\w\s]+[^\S\n]*|[^\S\n]+)' ]]; then
local skip=$((mbegin[1] - 1))
local go=$((cursor + mend[1] - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${rbuffer:1} =~ '^\n*(\w+[^\S\n]*|[^\w\s]+[^\S\n]*|[^\S\n]+)' ]]; then
skip=$((mbegin[1]))
go=$((cursor + mend[1]))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_prev_word_start {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local lbuffer="${BUFFER:0:$((cursor + 1))}"
if [[ $lbuffer =~ '(\w+[^\S\n]*|[^\w\s]+[^\S\n]*|[^\S\n]+)\n*$' ]]; then
local skip=$((${#lbuffer} - mend[1]))
local go=$((mbegin - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${lbuffer:0:-1} =~ '(\w+[^\S\n]*|[^\w\s]+[^\S\n]*|[^\S\n]+)\n*$' ]]; then
skip=$((${#lbuffer} - mend[1]))
go=$((mbegin - 1))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_next_word_end {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ '^\n*([^\S\n]*\w+|[^\S\n]*[^\w\s]+|[^\S\n]+)' ]]; then
local skip=$((mbegin[1] - 1))
local go=$((cursor + mend[1] - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${rbuffer:1} =~ '^\n*([^\S\n]*\w+|[^\S\n]*[^\w\s]+|[^\S\n]+)' ]]; then
skip=$((mbegin[1]))
go=$((cursor + mend[1]))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_next_long_word_start {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ '^\n*(\S+[^\S\n]*|[^\S\n]+)' ]]; then
local skip=$((mbegin[1] - 1))
local go=$((cursor + mend[1] - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${rbuffer:1} =~ '^\n*(\S+[^\S\n]*|[^\S\n]+)' ]]; then
skip=$((mbegin[1]))
go=$((cursor + mend[1]))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_prev_long_word_start {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local lbuffer="${BUFFER:0:$((cursor + 1))}"
if [[ $lbuffer =~ '(\S+[^\S\n]*|[^\S\n]+)\n*$' ]]; then
local skip=$((${#lbuffer} - mend[1]))
local go=$((mbegin - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${lbuffer:0:-1} =~ '(\S+[^\S\n]*|[^\S\n]+)\n*$' ]]; then
skip=$((${#lbuffer} - mend[1]))
go=$((mbegin - 1))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_move_next_long_word_end {
setopt localoptions rematchpcre
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ '^\n*([^\S\n]*\S+|[^\S\n]+)' ]]; then
local skip=$((mbegin[1] - 1))
local go=$((cursor + mend[1] - 1))
if (( ${#match[1]} == 1 )) \
&& [[ ${rbuffer:1} =~ '^\n*([^\S\n]*\S+|[^\S\n]+)' ]]; then
skip=$((mbegin[1]))
go=$((cursor + mend[1]))
fi
__zhm_trailing_goto $i $go $skip
fi
done
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_find_till_char {
local char="$(read -ek 1)"
char="$(printf '%s' "$char" | sed 's/[.[\(*^$+?{|]/\\&/g')"
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ ".?$char?[^$char]*" ]]; then
__zhm_trailing_goto $i $((cursor + MEND - 1)) 0
fi
done
ZHM_LAST_MOTION="find_till"
ZHM_LAST_MOTION_CHAR="$char"
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_find_next_char {
local char="$(read -ek 1)"
char="$(printf '%s' "$char" | sed 's/[.[\(*^$+?{|]/\\&/g')"
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ "$char?[^$char]*$char" ]]; then
__zhm_trailing_goto $i $((cursor + MEND - 1)) 0
fi
done
ZHM_LAST_MOTION="find_next"
ZHM_LAST_MOTION_CHAR="$char"
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_till_prev_char {
local char="$(read -ek 1)"
char="$(printf '%s' "$char" | sed 's/[.[\(*^$+?{|]/\\&/g')"
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local lbuffer="${BUFFER:0:$cursor}"
if [[ $lbuffer =~ "[^$char]*$char?$" ]]; then
__zhm_trailing_goto $i $((MBEGIN - 1)) 0
fi
done
ZHM_LAST_MOTION="till_prev"
ZHM_LAST_MOTION_CHAR="$char"
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_find_prev_char {
local char="$(read -ek 1)"
char="$(printf '%s' "$char" | sed 's/[.[\(*^$+?{|]/\\&/g')"
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local lbuffer="${BUFFER:0:$cursor}"
if [[ $lbuffer =~ "${char}[^${char}]*$" ]]; then
__zhm_trailing_goto $i $((MBEGIN - 1)) 0
fi
done
ZHM_LAST_MOTION="find_prev"
ZHM_LAST_MOTION_CHAR="$char"
__zhm_update_last_moved
__zhm_update_region_highlight
}
function zhm_repeat_last_motion {
local char="$ZHM_LAST_MOTION_CHAR"
case "$ZHM_LAST_MOTION" in
"find_till")
for i in {1..$#zhm_cursors_pos}; do
local cursor=$zhm_cursors_pos[$i]
local rbuffer="${BUFFER:$cursor}"
if [[ $rbuffer =~ ".?$char?[^$char]*" ]]; then