-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
vi.sh
8120 lines (7337 loc) · 263 KB
/
vi.sh
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
#!/bin/bash
# Note: bind (INITIALIZE_DEFMAP) の中から再帰的に呼び出されうるので、
# 先に ble-edit/bind/load-editing-mode:vi を上書きする必要がある。
ble/is-function ble-edit/bind/load-editing-mode:vi && return 0
function ble-edit/bind/load-editing-mode:vi { :; }
# 2020-04-29 force update (rename ble-decode/keymap/.register)
# 2021-01-25 force update (change mapping of C-w and M-w)
# 2021-04-26 force update (rename ble/decode/keymap#.register)
source "$_ble_base/keymap/vi_digraph.sh"
## @bleopt keymap_vi_macro_depth
bleopt/declare -n keymap_vi_macro_depth 64
## @fn ble/keymap:vi/k2c key
## @var[out] ret
function ble/keymap:vi/k2c {
local key=$1
local flag=$((key&_ble_decode_MaskFlag)) char=$((key&_ble_decode_MaskChar))
if ((flag==0&&(32<=char&&char<_ble_decode_FunctionKeyBase))); then
ret=$char
return 0
elif ((flag==_ble_decode_Ctrl&&63<=char&&char<128&&(char&0x1F)!=0)); then
((char=char==63?127:char&0x1F))
ret=$char
return 0
else
return 1
fi
}
#------------------------------------------------------------------------------
# utils
## @fn ble/string#index-of-chars text chars [index]
## 文字集合に含まれる文字を、文字列中で順方向に探索します。
## @fn ble/string#last-index-of-chars text chars [index]
## 文字集合に含まれる文字を、文字列中で逆方向に探索します。
##
## @param[in] text
## 検索する対象の文字列を指定します。
## @param[in] chars
## 検索する文字の集合を指定します
## @param[in] index
## text の内の検索開始位置を指定します。
## @var[out] ret
##
function ble/string#index-of-chars {
local chars=$2 index=${3:-0}
local text=${1:index}
local cut=${text%%["$chars"]*}
if ((${#cut}<${#text})); then
((ret=index+${#cut}))
return 0
else
ret=-1
return 1
fi
}
function ble/string#last-index-of-chars {
local text=$1 chars=$2 index=$3
[[ $index ]] && text=${text::index}
local cut=${text%["$chars"]*}
if ((${#cut}<${#text})); then
((ret=${#cut}))
return 0
else
ret=-1
return 1
fi
}
## @fn ble-edit/content/nonbol-eolp text
## @var[out] ret
function ble-edit/content/nonbol-eolp {
local pos=${1:-$_ble_edit_ind}
! ble-edit/content/bolp "$pos" && ble-edit/content/eolp "$pos"
}
## @fn ble/keymap:vi/string#encode-rot13 text
## @var[out] ret
function ble/keymap:vi/string#encode-rot13 {
local text=$1
local -a buff=() ch
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [A-Z] ]]; then
ch=${_ble_util_string_upper_list%%"$ch"*}
ch=${_ble_util_string_upper_list:(${#ch}+13)%26:1}
elif [[ $ch == [a-z] ]]; then
ch=${_ble_util_string_lower_list%%"$ch"*}
ch=${_ble_util_string_lower_list:(${#ch}+13)%26:1}
fi
ble/array#push buff "$ch"
done
IFS= builtin eval 'ret="${buff[*]-}"'
}
#------------------------------------------------------------------------------
# constants
_ble_keymap_vi_REX_WORD=$'[a-zA-Z0-9_]+|[!-/:-@[-`{-~]+|[^ \t\na-zA-Z0-9!-/:-@[-`{-~]+'
#------------------------------------------------------------------------------
# vi_imap/__default__, vi-command/decompose-meta
function ble/widget/vi_imap/__default__ {
local flag=$((KEYS[0]&_ble_decode_MaskFlag)) code=$((KEYS[0]&_ble_decode_MaskChar))
# メタ修飾付きの入力 M-key は ESC + key に分解する
if ((flag&_ble_decode_Meta)); then
ble/keymap:vi/imap-repeat/pop
local esc=27 # ESC
# local esc=$((_ble_decode_Ctrl|0x5b)) # もしくは C-[
ble/decode/widget/skip-lastwidget
ble/decode/widget/redispatch-by-keys "$esc" $((KEYS[0]&~_ble_decode_Meta)) "${KEYS[@]:1}"
return 0
fi
# Control 修飾された文字 C-@ - C-\, C-? は制御文字 \000 - \037, \177 に戻して挿入
if local ret; ble/keymap:vi/k2c "${KEYS[0]}"; then
local -a KEYS; KEYS=("$ret")
ble/widget/self-insert
return 0
fi
return 125
}
function ble/widget/vi-command/decompose-meta {
local flag=$((KEYS[0]&_ble_decode_MaskFlag)) code=$((KEYS[0]&_ble_decode_MaskChar))
# メタ修飾付きの入力 M-key は ESC + key に分解する
if ((flag&_ble_decode_Meta)); then
local esc=$((_ble_decode_Ctrl|0x5b)) # C-[ (もしくは esc=27 ESC?)
ble/decode/widget/skip-lastwidget
ble/decode/widget/redispatch-by-keys "$esc" $((KEYS[0]&~_ble_decode_Meta)) "${KEYS[@]:1}"
return 0
fi
return 125
}
function ble/widget/vi_omap/__default__ {
ble/widget/vi-command/decompose-meta || ble/widget/vi-command/bell
return 0
}
function ble/widget/vi_omap/cancel {
ble/keymap:vi/adjust-command-mode
return 0
}
#------------------------------------------------------------------------------
# repeat
## @var _ble_keymap_vi_irepeat_count
## 挿入モードに入る時に指定された引数を記録する。
_ble_keymap_vi_irepeat_count=
## @arr _ble_keymap_vi_irepeat
## 挿入モードに入るときに指定された引数が 1 より大きい時、
## 後で操作を繰り返すために操作内容を記録する配列。
##
## 各要素は keys:widget の形式を持つ。
## keys は空白区切りの key (整数値) の列、つまり ${KEYS[*]} である。
## widget は実際に呼び出す WIDGET の内容である。
##
_ble_keymap_vi_irepeat=()
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_irepeat_count \
_ble_keymap_vi_irepeat
function ble/keymap:vi/imap-repeat/pop {
local top_index=$((${#_ble_keymap_vi_irepeat[*]}-1))
((top_index>=0)) && builtin unset -v '_ble_keymap_vi_irepeat[top_index]'
}
function ble/keymap:vi/imap-repeat/push {
local IFS=$_ble_term_IFS
ble/array#push _ble_keymap_vi_irepeat "${KEYS[*]-}:$WIDGET"
}
function ble/keymap:vi/imap-repeat/reset {
local count=${1-}
_ble_keymap_vi_irepeat_count=
_ble_keymap_vi_irepeat=()
((count>1)) && _ble_keymap_vi_irepeat_count=$count
}
function ble/keymap:vi/imap-repeat/process {
if ((_ble_keymap_vi_irepeat_count>1)); then
local repeat=$_ble_keymap_vi_irepeat_count
local -a widgets; widgets=("${_ble_keymap_vi_irepeat[@]}")
local i widget
for ((i=1;i<repeat;i++)); do
for widget in "${widgets[@]}"; do
ble/decode/widget/call "${widget#*:}" ${widget%%:*}
done
done
fi
}
function ble/keymap:vi/imap/invoke-widget {
local WIDGET=$1
local -a KEYS; KEYS=("${@:2}")
ble/keymap:vi/imap-repeat/push
builtin eval -- "$WIDGET"
}
## @arr _ble_keymap_vi_imap_white_list
## 引数を指定して入った挿入モードを抜けるときの繰り返しで許されるコマンドのリスト
_ble_keymap_vi_imap_white_list=(
self-insert
batch-insert
nop
magic-space
delete-backward-{c,f,s,u}word
copy{,-forward,-backward}-{c,f,s,u}word
copy-region{,-or}
clear-screen
command-help
display-shell-version
redraw-line
)
function ble/keymap:vi/imap/is-command-white {
if [[ $1 == ble/widget/self-insert ]]; then
# frequently used command is checked first
return 0
elif [[ $1 == ble/widget/* ]]; then
local IFS=$_ble_term_IFS
local cmd=${1#ble/widget/}; cmd=${cmd%%["$_ble_term_IFS"]*}
[[ $cmd == vi_imap/* || " ${_ble_keymap_vi_imap_white_list[*]} " == *" $cmd "* ]] && return 0
fi
return 1
}
function ble/widget/vi_imap/__before_widget__ {
if ble/keymap:vi/imap/is-command-white "$WIDGET"; then
ble/keymap:vi/imap-repeat/push
else
if ((_ble_keymap_vi_mark_edit_dbeg>=0)); then
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record-insert
ble/keymap:vi/mark/start-edit-area
fi
ble/keymap:vi/imap-repeat/reset
fi
}
#------------------------------------------------------------------------------
# vi_imap/complete
function ble/widget/vi_imap/complete {
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/undo/add more
ble/widget/complete "$@"
}
function ble/keymap:vi/complete/insert.hook {
[[ $_ble_decode_keymap == vi_imap ||
$_ble_decode_keymap == auto_complete ]] || return 1
local original=${comp_text:insert_beg:insert_end-insert_beg}
local q="'" Q="'\''"
local WIDGET="ble/widget/complete-insert '${original//$q/$Q}' '${insert//$q/$Q}' '${suffix//$q/$Q}'"
ble/keymap:vi/imap-repeat/push
[[ $_ble_decode_keymap == vi_imap ]] &&
ble/keymap:vi/undo/add more
}
blehook complete_insert+=ble/keymap:vi/complete/insert.hook
function ble-decode/keymap:vi_imap/bind-complete {
ble-bind -f 'C-i' 'vi_imap/complete'
ble-bind -f 'TAB' 'vi_imap/complete'
ble-bind -f 'C-TAB' 'menu-complete'
ble-bind -f 'S-C-i' 'menu-complete backward'
ble-bind -f 'S-TAB' 'menu-complete backward'
ble-bind -f 'auto_complete_enter' 'auto-complete-enter'
ble-bind -f 'C-x /' 'menu-complete context=filename'
ble-bind -f 'C-x ~' 'menu-complete context=username'
ble-bind -f 'C-x $' 'menu-complete context=variable'
ble-bind -f 'C-x @' 'menu-complete context=hostname'
ble-bind -f 'C-x !' 'menu-complete context=command'
ble-bind -f 'C-]' 'sabbrev-expand'
ble-bind -f 'C-x C-r' 'dabbrev-expand'
ble-bind -f 'C-x *' 'complete insert_all:context=glob'
ble-bind -f 'C-x g' 'complete show_menu:context=glob'
}
#------------------------------------------------------------------------------
# modes
## @var _ble_keymap_vi_insert_overwrite
## 挿入モードに入った時の上書き文字
_ble_keymap_vi_insert_overwrite=
## @var _ble_keymap_vi_insert_leave
## 挿入モードから抜ける時に実行する関数を設定します
_ble_keymap_vi_insert_leave=
## @var _ble_keymap_vi_single_command
## ノーマルモードにおいて 1 つコマンドを実行したら
## 元の挿入モードに戻るモード (C-o) にいるかどうかを表します。
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_insert_overwrite \
_ble_keymap_vi_insert_leave \
_ble_keymap_vi_single_command \
_ble_keymap_vi_single_command_overwrite
## @bleopt keymap_vi_mode_string_nmap
## ノーマルモードの時に表示する文字列を指定します。
## 空文字列を指定したときは何も表示しません。
bleopt/declare -n keymap_vi_mode_string_nmap $'\e[1m~\e[m'
bleopt/declare -o keymap_vi_nmap_name keymap_vi_mode_string_nmap
bleopt/declare -v term_vi_imap ''
bleopt/declare -v term_vi_nmap ''
bleopt/declare -v term_vi_omap ''
bleopt/declare -v term_vi_xmap ''
bleopt/declare -v term_vi_smap ''
bleopt/declare -v term_vi_cmap ''
bleopt/declare -v keymap_vi_imap_cursor ''
bleopt/declare -v keymap_vi_nmap_cursor ''
bleopt/declare -v keymap_vi_omap_cursor ''
bleopt/declare -v keymap_vi_xmap_cursor ''
bleopt/declare -v keymap_vi_smap_cursor ''
bleopt/declare -v keymap_vi_cmap_cursor ''
function ble/keymap:vi/.process-cursor-options {
local keymap=${FUNCNAME[1]#bleopt/check:keymap_}; keymap=${keymap%_cursor}
ble-bind -m "$keymap" --cursor "$value"
local locate=$'\e[32m'$bleopt_source:$bleopt_lineno$'\e[m'
ble/util/print-lines \
"bleopt ($locate): The option 'keymap_${keymap}_cursor' has been removed." \
" Please use 'ble-bind -m $keymap --cursor $value' instead." >&2
}
function bleopt/check:keymap_vi_imap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_nmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_omap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_xmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_smap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/check:keymap_vi_cmap_cursor { ble/keymap:vi/.process-cursor-options; }
function bleopt/obsolete:keymap_vi_imap_cursor { :; }
function bleopt/obsolete:keymap_vi_nmap_cursor { :; }
function bleopt/obsolete:keymap_vi_omap_cursor { :; }
function bleopt/obsolete:keymap_vi_xmap_cursor { :; }
function bleopt/obsolete:keymap_vi_smap_cursor { :; }
function bleopt/obsolete:keymap_vi_cmap_cursor { :; }
bleopt/declare -v keymap_vi_mode_show 1
function bleopt/check:keymap_vi_mode_show {
local bleopt_keymap_vi_mode_show=$value
[[ $_ble_attached ]] &&
ble/keymap:vi/update-mode-name
return 0
}
bleopt/declare -v keymap_vi_mode_update_prompt ''
bleopt/declare -v keymap_vi_mode_name_insert 'INSERT'
bleopt/declare -v keymap_vi_mode_name_replace 'REPLACE'
bleopt/declare -v keymap_vi_mode_name_vreplace 'VREPLACE'
bleopt/declare -v keymap_vi_mode_name_visual 'VISUAL'
bleopt/declare -v keymap_vi_mode_name_select 'SELECT'
bleopt/declare -v keymap_vi_mode_name_linewise 'LINE'
bleopt/declare -v keymap_vi_mode_name_blockwise 'BLOCK'
## @fn ble/keymap:vi/script/get-mode
## @var[out] mode
function ble/keymap:vi/script/get-mode {
mode=
local kmap=$_ble_decode_keymap
[[ $kmap == auto_complete ]] && kmap=vi_imap
# /[iR^R]?/
if [[ $_ble_keymap_vi_single_command || $kmap == vi_imap ]]; then
local overwrite=
if [[ $kmap == vi_imap ]]; then
overwrite=$_ble_edit_overwrite_mode
elif [[ $kmap == vi_[noxs]map ]]; then
overwrite=$_ble_keymap_vi_single_command_overwrite
fi
case $overwrite in
('') mode=i ;;
(R) mode=R ;;
(*) mode=$'\x12' ;; # C-r
esac
fi
# /[nvV^VsS^S]?/
case $kmap:${_ble_edit_mark_active%+} in
(vi_xmap:vi_line) mode=$mode'V' ;;
(vi_xmap:vi_block)mode=$mode$'\x16' ;; # C-v
(vi_xmap:*) mode=$mode'v' ;;
(vi_smap:vi_line) mode=$mode'S' ;;
(vi_smap:vi_block)mode=$mode$'\x13' ;; # C-s
(vi_smap:*) mode=$mode's' ;;
(vi_[no]map:*) mode=$mode'n' ;;
(vi_cmap:*) mode=$mode'c' ;;
(vi_imap:*) ;;
(*:*) mode=$mode'?' ;;
esac
}
function ble/keymap:vi/update-mode-name {
local kmap=$_ble_decode_keymap cursor=
if [[ $kmap == vi_imap ]]; then
ble/util/buffer "$bleopt_term_vi_imap"
elif [[ $kmap == vi_nmap ]]; then
ble/util/buffer "$bleopt_term_vi_nmap"
elif [[ $kmap == vi_xmap ]]; then
ble/util/buffer "$bleopt_term_vi_xmap"
elif [[ $kmap == vi_smap ]]; then
ble/util/buffer "$bleopt_term_vi_smap"
elif [[ $kmap == vi_omap ]]; then
ble/util/buffer "$bleopt_term_vi_omap"
elif [[ $kmap == vi_cmap ]]; then
ble/edit/info/default text ''
ble/util/buffer "$bleopt_term_vi_cmap"
return 0
fi
if [[ $bleopt_keymap_vi_mode_update_prompt ]] || ble/util/test-rl-variable show-mode-in-prompt; then
ble/prompt/clear
fi
local name=
if [[ $bleopt_keymap_vi_mode_show ]]; then
local show= overwrite=
if [[ $kmap == vi_imap ]]; then
show=1 overwrite=$_ble_edit_overwrite_mode
elif [[ $_ble_keymap_vi_single_command && ( $kmap == vi_nmap || $kmap == vi_omap ) ]]; then
show=1 overwrite=$_ble_keymap_vi_single_command_overwrite
elif [[ $kmap == vi_[xs]map ]]; then
show=x overwrite=$_ble_keymap_vi_single_command_overwrite
else
name=$bleopt_keymap_vi_mode_string_nmap
fi
fi
if [[ $show ]]; then
if [[ $overwrite == R ]]; then
name=$bleopt_keymap_vi_mode_name_replace
elif [[ $overwrite ]]; then
name=$bleopt_keymap_vi_mode_name_vreplace
else
name=$bleopt_keymap_vi_mode_name_insert
fi
if [[ $_ble_keymap_vi_single_command ]]; then
local ret; ble/string#tolower "$name"; name="($ret)"
fi
if [[ $show == x ]]; then
local mark_type=${_ble_edit_mark_active%+}
local visual_name=$bleopt_keymap_vi_mode_name_visual
[[ $kmap == vi_smap ]] && visual_name=$bleopt_keymap_vi_mode_name_select
if [[ $mark_type == vi_line ]]; then
visual_name=$visual_name' '$bleopt_keymap_vi_mode_name_linewise
elif [[ $mark_type == vi_block ]]; then
visual_name=$visual_name' '$bleopt_keymap_vi_mode_name_blockwise
fi
if [[ $_ble_keymap_vi_single_command ]]; then
name="$name $visual_name"
else
name=$visual_name
fi
fi
name=$'\e[1m-- '$name$' --\e[m'
fi
if [[ $_ble_keymap_vi_reg_record ]]; then
name=$name${name:+' '}$'\e[1;31mREC @'$_ble_keymap_vi_reg_record_char$'\e[m'
elif [[ $_ble_edit_kbdmacro_record ]]; then
name=$name${name:+' '}$'\e[1;31mREC\e[m'
fi
ble/edit/info/default ansi "$name" # 6ms
}
function ble/widget/vi_imap/normal-mode.impl {
local opts=$1
# finalize insert mode
ble/keymap:vi/mark/set-local-mark 94 "$_ble_edit_ind" # `^
ble/keymap:vi/mark/end-edit-area
[[ :$opts: == *:InsertLeave:* ]] && builtin eval -- "$_ble_keymap_vi_insert_leave"
# set up normal mode
_ble_edit_mark_active=
_ble_edit_overwrite_mode=
_ble_keymap_vi_insert_leave=
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
ble-edit/content/bolp || ((_ble_edit_ind--))
ble/decode/keymap/push vi_nmap
}
function ble/widget/vi_imap/normal-mode {
ble-edit/content/clear-arg
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/imap-repeat/process
ble/keymap:vi/repeat/record-insert
ble/widget/vi_imap/normal-mode.impl InsertLeave
ble/keymap:vi/update-mode-name
return 0
}
function ble/widget/vi_imap/normal-mode-without-insert-leave {
ble-edit/content/clear-arg
ble/keymap:vi/imap-repeat/pop
ble/keymap:vi/repeat/record-insert
ble/widget/vi_imap/normal-mode.impl
ble/keymap:vi/update-mode-name
return 0
}
function ble/widget/vi_imap/single-command-mode {
ble-edit/content/clear-arg
local single_command=1
local single_command_overwrite=$_ble_edit_overwrite_mode
ble-edit/content/eolp && _ble_keymap_vi_single_command=2
ble/keymap:vi/imap-repeat/pop
ble/widget/vi_imap/normal-mode.impl
_ble_keymap_vi_single_command=$single_command
_ble_keymap_vi_single_command_overwrite=$single_command_overwrite
ble/keymap:vi/update-mode-name
return 0
}
## @fn ble/keymap:vi/needs-eol-fix
##
## Note: この関数を使った後は ble/keymap:vi/adjust-command-mode を呼び出す必要がある。
## そうしないとノーマルモードにおいてありえない位置にカーソルが来ることになる。
##
function ble/keymap:vi/needs-eol-fix {
[[ $_ble_decode_keymap == vi_nmap || $_ble_decode_keymap == vi_omap ]] || return 1
[[ $_ble_keymap_vi_single_command ]] && return 1
local index=${1:-$_ble_edit_ind}
ble-edit/content/nonbol-eolp "$index"
}
function ble/keymap:vi/adjust-command-mode {
if [[ $_ble_decode_keymap == vi_[xs]map ]]; then
# 移動コマンドが来たら末尾拡張を無効にする。
# 移動コマンドはここを通るはず…
ble/keymap:vi/xmap/remove-eol-extension
fi
local kmap_popped=
if [[ $_ble_decode_keymap == vi_omap ]]; then
ble/decode/keymap/pop
kmap_popped=1
fi
# search による mark の設定・解除
if [[ $_ble_keymap_vi_search_activate ]]; then
if [[ $_ble_decode_keymap != vi_[xs]map ]]; then
_ble_edit_mark_active=$_ble_keymap_vi_search_activate
fi
_ble_keymap_vi_search_matched=1
_ble_keymap_vi_search_activate=
else
[[ $_ble_edit_mark_active == vi_search ]] && _ble_edit_mark_active=
((_ble_keymap_vi_search_matched)) && _ble_keymap_vi_search_matched=
fi
if [[ $_ble_decode_keymap == vi_nmap && $_ble_keymap_vi_single_command ]]; then
if ((_ble_keymap_vi_single_command==2)); then
local index=$((_ble_edit_ind+1))
ble-edit/content/nonbol-eolp "$index" && _ble_edit_ind=$index
fi
ble/widget/vi_nmap/.insert-mode 1 "$_ble_keymap_vi_single_command_overwrite" resume
ble/keymap:vi/repeat/clear-insert
elif [[ $kmap_popped ]]; then
ble/keymap:vi/update-mode-name
fi
return 0
}
function ble/widget/vi-command/bell {
ble/widget/.bell "$1"
ble/keymap:vi/adjust-command-mode
return 0
}
## @fn ble/widget/vi_nmap/.insert-mode [arg [overwrite [opts]]]
## @param[in] arg
## @param[in] overwrite
## @param[in] opts
function ble/widget/vi_nmap/.insert-mode {
[[ $_ble_decode_keymap == vi_[xs]map ]] && ble/decode/keymap/pop
[[ $_ble_decode_keymap == vi_omap ]] && ble/decode/keymap/pop
local arg=$1 overwrite=$2
ble/keymap:vi/imap-repeat/reset "$arg"
_ble_edit_mark_active=
_ble_edit_overwrite_mode=$overwrite
_ble_keymap_vi_insert_leave=
_ble_keymap_vi_insert_overwrite=$overwrite
_ble_keymap_vi_single_command=
_ble_keymap_vi_single_command_overwrite=
_ble_keymap_vi_search_matched=
ble/decode/keymap/pop
ble/keymap:vi/update-mode-name
ble/keymap:vi/mark/start-edit-area
if [[ :$opts: != *:resume:* ]]; then
ble/keymap:vi/mark/commit-edit-area "$_ble_edit_ind" "$_ble_edit_ind"
fi
}
function ble/widget/vi_nmap/insert-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/append-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
if ! ble-edit/content/eolp; then
((_ble_edit_ind++))
fi
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/append-mode-at-end-of-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret; ble-edit/content/find-logical-eol
_ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/insert-mode-at-beginning-of-line {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret; ble-edit/content/find-logical-bol
_ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/insert-mode-at-first-non-space {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi-command/first-non-space
[[ ${_ble_edit_str:_ble_edit_ind:1} == [$' \t'] ]] &&
((_ble_edit_ind++)) # 逆eol補正
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
# nmap: gi
function ble/widget/vi_nmap/insert-mode-at-previous-point {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
local ret
ble/keymap:vi/mark/get-local-mark 94 && _ble_edit_ind=$ret
ble/widget/vi_nmap/.insert-mode "$ARG"
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/replace-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG" R
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/virtual-replace-mode {
local ARG FLAG REG; ble/keymap:vi/get-arg 1
ble/widget/vi_nmap/.insert-mode "$ARG" 1
ble/keymap:vi/repeat/record
return 0
}
function ble/widget/vi_nmap/accept-line {
ble/keymap:vi/clear-arg
ble/widget/vi_nmap/.insert-mode
ble/keymap:vi/repeat/clear-insert
[[ $_ble_keymap_vi_reg_record ]] &&
ble/widget/vi_nmap/record-register
ble/widget/default/accept-line
}
function ble/widget/vi-command/edit-and-execute-command {
ble/keymap:vi/clear-arg
ble/widget/vi_nmap/.insert-mode
ble/keymap:vi/repeat/clear-insert
[[ $_ble_keymap_vi_reg_record ]] &&
ble/widget/vi_nmap/record-register
ble/widget/edit-and-execute-command
}
#------------------------------------------------------------------------------
# args
#
# arg : 0-9 d y c
# command : dd yy cc [dyc]0 Y S
_ble_keymap_vi_oparg=
_ble_keymap_vi_opfunc=
_ble_keymap_vi_reg=
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_vi_oparg \
_ble_keymap_vi_opfunc \
_ble_keymap_vi_reg
# ble/keymap:vi における _ble_edit_kill_ring の扱いついて
#
# _ble_edit_kill_type=L のとき
# 行指向の切り取り文字列であることを表し、
# _ble_edit_kill_ring の末端には必ず改行文字が来ると仮定して良い。
#
# _ble_edit_kill_type=B:* の形式をしているとき、
# 矩形の切り取り文字列であることを表し、
# _ble_edit_kill_ring は改行区切りで各行を連結したものである。
# 末端には改行文字は付加しない。末端に改行文字があるときは、
# それは付加された改行ではなく、最後に空行があることを意味する。
#
# _ble_edit_kill_type の 2 文字目以降は数字を空白区切りで並べたもので、
# 各数字は _ble_edit_kill_ring 内の各行に対応する。
# 意味は、行の途中に挿入する際に矩形を保つために右に補填するべき空白の数である。
# 行末に挿入する際にはこの空白の補填は起こらないことに注意する。
#
# _ble_edit_kill_type= (空文字列) もしくは それ意外の場合は
# 通常の切り取り文字列であることを表す。
# _ble_edit_kill_ring は任意の文字列である。
#
_ble_keymap_vi_register=()
_ble_keymap_vi_register_onplay=
## @fn ble/keymap:vi/clear-arg
function ble/keymap:vi/clear-arg {
_ble_edit_arg=
_ble_keymap_vi_oparg=
_ble_keymap_vi_opfunc=
_ble_keymap_vi_reg=
}
## @fn ble/keymap:vi/get-arg [default_value]; ARG FLAG REG
##
## 引数の内容について
## vi_nmap, vi_xmap, vi_smap においては FLAG は空であると仮定して良い。
## vi_omap においては FLAG は非空である。
## get-arg{,-reg} を呼び出すことによって空になる。
## つまり vi_omap においてこの関数を呼び出したとき、vi_omap から vi_nmap に戻る必要がある。
## これは通例 ble/keymap:vi/adjust-command-mode によって実施される。
##
function ble/keymap:vi/get-arg {
local default_value=$1
REG=$_ble_keymap_vi_reg
FLAG=$_ble_keymap_vi_opfunc
if [[ ! $_ble_edit_arg && ! $_ble_keymap_vi_oparg ]]; then
ARG=$default_value
else
ARG=$((10#${_ble_edit_arg:-1}*10#${_ble_keymap_vi_oparg:-1}))
fi
ble/keymap:vi/clear-arg
}
## @fn ble/keymap:vi/register#load reg
function ble/keymap:vi/register#load {
local reg=$1
if [[ $reg ]] && ((reg!=34)); then
if [[ $reg == 37 ]]; then # "%
ble-edit/content/push-kill-ring "$HISTFILE" ''
return 0
fi
local value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
ble-edit/content/push-kill-ring "${value#*/}" "${value%%/*}"
return 0
else
ble-edit/content/push-kill-ring
return 1
fi
fi
}
## @fn ble/keymap:vi/register#set reg type content
function ble/keymap:vi/register#set {
local reg=$1 type=$2 content=$3
# type = L は行指向の値
# type = B は矩形指向の値
# type = '' は文字指向の値
# type = q はキーボードマクロ
#
# Note: 実際に記録される type は以下の何れかである。
# type = L
# type = B:*
# type = ''
# 追記の場合
if [[ $reg == +* ]]; then
local value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
local otype=${value%%/*}
local oring=${value#*/}
if [[ $otype == L ]]; then
if [[ $type == q ]]; then
type=L content=${oring%$'\n'}$content # V + * → V
else
type=L content=$oring$content # V + * → V
fi
elif [[ $type == L ]]; then
type=L content=$oring$'\n'$content # C-v + V, v + V → V
elif [[ $otype == B:* ]]; then
if [[ $type == B:* ]]; then
type=$otype' '${type#B:}
content=$oring$'\n'$content # C-v + C-v → C-v
elif [[ $type == q ]]; then
local ret; ble/string#count-char "$content" $'\n'
ble/string#repeat ' 0' "$ret"
type=$otype$ret
content=$oring$$content # C-v + q → C-v
else
local ret; ble/string#count-char "$content" $'\n'
ble/string#repeat ' 0' $((ret+1))
type=$otype$ret
content=$oring$'\n'$content # C-v + v → C-v
fi
else
type= content=$oring$content # v + C-v, v + v, v + q → v
fi
fi
fi
[[ $type == L && $content != *$'\n' ]] && content=$content$'\n'
local suppress_default=
[[ $type == q ]] && type= suppress_default=1
if [[ ! $reg ]] || ((reg==34)); then # ""
# unnamed register
ble-edit/content/push-kill-ring "$content" "$type"
return 0
elif ((reg==58||reg==46||reg==37||reg==126)); then # ": ". "% "~
# read only register
ble/widget/.bell "attempted to write on a read-only register #$reg"
return 1
elif ((reg==95)); then # "_
# black hole register
return 0
else
if [[ ! $suppress_default ]]; then
ble-edit/content/push-kill-ring "$content" "$type"
fi
_ble_keymap_vi_register[reg]=$type/$content
return 0
fi
}
## @fn ble/keymap:vi/register#set-yank reg type content
## レジスタ "0 に文字列を登録します。
##
function ble/keymap:vi/register#set-yank {
ble/keymap:vi/register#set "$@" || return 1
local reg=$1 type=$2 content=$3
if [[ $reg == '' || $reg == 34 ]]; then
ble/keymap:vi/register#set 48 "$type" "$content" # "0
fi
}
## @fn ble/keymap:vi/register#set-edit reg type content
## レジスタ "1 に文字列を登録します。
##
## content に改行が含まれる場合、または、特定の WIDGET の時、
## 元々レジスタ "1 - "8 にあった内容をレジスタ "2 - "9 に移動し、
## 新しい文字列をレジスタ "1 に登録します。
## それ以外の時、新しい文字列はレジスタ "- に登録します。
##
_ble_keymap_vi_register_49_widget_list=(
# %
ble/widget/vi-command/search-matchpair-or
ble/widget/vi-command/percentage-line
# `
ble/widget/vi-command/goto-mark
# / ? n N
ble/widget/vi-command/search-forward
ble/widget/vi-command/search-backward
ble/widget/vi-command/search-repeat
ble/widget/vi-command/search-reverse-repeat
# ( ) { }
# ToDo
)
function ble/keymap:vi/register#set-edit {
ble/keymap:vi/register#set "$@" || return 1
local reg=$1 type=$2 content=$3
if [[ $reg == '' || $reg == 34 ]]; then
local IFS=$_ble_term_IFS
local widget=${WIDGET%%["$_ble_term_IFS"]*}
if [[ $content == *$'\n'* || " $widget " == " ${_ble_keymap_vi_register_49_widget_list[*]} " ]]; then
local n
for ((n=9;n>=2;n--)); do
_ble_keymap_vi_register[48+n]=${_ble_keymap_vi_register[48+n-1]}
done
ble/keymap:vi/register#set 49 "$type" "$content" # "1
else
ble/keymap:vi/register#set 45 "$type" "$content" # "-
fi
fi
}
function ble/keymap:vi/register#play {
local reg=$1 value
if [[ $reg ]] && ((reg!=34)); then
value=${_ble_keymap_vi_register[reg]}
if [[ $value == */* ]]; then
value=${value#*/}
else
value=
return 1
fi
else
value=$_ble_edit_kill_ring
fi
local _ble_keymap_vi_register_onplay=1
local ret; ble/decode/charlog#decode "$value"
ble/widget/.MACRO "${ret[@]}"
return 0
}
## @fn ble/keymap:vi/register#dump/escape text
## @var[out] ret
function ble/keymap:vi/register#dump/escape {
local text=$1
local out= i=0 iN=${#text}
while ((i<iN)); do
local tail=${text:i}
if ble/util/isprint+ "$tail"; then
out=$out$BASH_REMATCH
((i+=${#BASH_REMATCH}))
else
ble/util/s2c "$tail"
local code=$ret
if ((code<32)); then
ble/util/c2s $((code+64))
out=$out$_ble_term_rev^$ret$_ble_term_sgr0
elif ((code==127)); then
out=$out$_ble_term_rev^?$_ble_term_sgr0
elif ((128<=code&&code<160)); then
ble/util/c2s $((code-64))
out=$out${_ble_term_rev}M-^$ret$_ble_term_sgr0
else
out=$out${tail::1}
fi
((i++))
fi
done
ret=$out
}
function ble/keymap:vi/register#dump {
local k ret out=
local value type content
for k in 34 "${!_ble_keymap_vi_register[@]}"; do
if ((k==34)); then
type=$_ble_edit_kill_type
content=$_ble_edit_kill_ring
else
value=${_ble_keymap_vi_register[k]}
type=${value%%/*} content=${value#*/}
fi
ble/util/c2s "$k"; k=$ret
case $type in
(L) type=line ;;
(B:*) type=block ;;
(*) type=char ;;
esac
ble/keymap:vi/register#dump/escape "$content"; content=$ret
out=$out'"'$k' ('$type') '$content$'\n'
done
ble/edit/info/show ansi "$out"
return 0
}
function ble/widget/vi-command:reg { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:regi { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:regis { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:regist { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:registe { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:register { ble/keymap:vi/register#dump; }
function ble/widget/vi-command:registers { ble/keymap:vi/register#dump; }
function ble/widget/vi-command/append-arg {
local ret ch=$1
if [[ ! $ch ]]; then
local code=$((KEYS[0]&_ble_decode_MaskChar))
((code==0)) && return 1
ble/util/c2s "$code"; ch=$ret
fi
ble/util/assert '[[ ! ${ch//[0-9]} ]]'
# 0
if [[ $ch == 0 && ! $_ble_edit_arg ]]; then
ble/widget/vi-command/beginning-of-line