forked from jeffreytse/zsh-vi-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-vi-mode.zsh
3446 lines (2986 loc) · 84.7 KB
/
zsh-vi-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
# zsh-vi-mode.zsh -- A better and friendly vi(vim) mode for Zsh
# https://github.com/jeffreytse/zsh-vi-mode
#
# Copyright (c) 2020 Jeffrey Tse
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# All Settings
# Some of these variables should be set before sourcing this file.
#
# ZVM_CONFIG_FUNC
# the config function (default is `zvm_config`), if this config function
# exists, it will be called automatically, you can do some configurations
# in this aspect before you source this plugin.
#
# For example:
#
# ```zsh
# function zvm_config() {
# ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
# ZVM_VI_INSERT_ESCAPE_BINDKEY=jk
# }
#
# source ~/zsh-vi-mode.zsh
# ```
#
# ZVM_INIT_MODE
# the pugin initial mode (default is doing the initialization when the first
# new command line is starting. For doing the initialization instantly, you
# can set it to `sourcing`.
#
# ZVM_VI_ESCAPE_BINDKEY
# the vi escape key for all modes (default is ^[ => <ESC>), you can set it
# to whatever you like, such as `jj`, `jk` and so on.
#
# ZVM_VI_INSERT_ESCAPE_BINDKEY
# the vi escape key of insert mode (default is $ZVM_VI_ESCAPE_BINDKEY), you
# can set it to whatever, such as `jj`, `jk` and so on.
#
# ZVM_VI_VISUAL_ESCAPE_BINDKEY
# the vi escape key of visual mode (default is $ZVM_VI_ESCAPE_BINDKEY), you
# can set it to whatever, such as `jj`, `jk` and so on.
#
# ZVM_VI_OPPEND_ESCAPE_BINDKEY
# the vi escape key of operator pendding mode (default is
# $ZVM_VI_ESCAPE_BINDKEY), you can set it to whatever, such as `jj`, `jk`
# and so on.
#
# ZVM_VI_INSERT_MODE_LEGACY_UNDO:
# using legacy undo behavior in vi insert mode
#
# ZVM_VI_HIGHLIGHT_FOREGROUND:
# the behavior of highlight foreground (surrounds, visual-line, etc) in vi mode
#
# ZVM_VI_HIGHLIGHT_BACKGROUND:
# the behavior of highlight background (surrounds, visual-line, etc) in vi mode
#
# ZVM_VI_HIGHLIGHT_EXTRASTYLE:
# the behavior of highlight extra style (i.e. bold, underline) in vi mode
#
# For example:
# ZVM_VI_HIGHLIGHT_FOREGROUND=green # Color name
# ZVM_VI_HIGHLIGHT_FOREGROUND=#008800 # Hex value
# ZVM_VI_HIGHLIGHT_BACKGROUND=red # Color name
# ZVM_VI_HIGHLIGHT_BACKGROUND=#ff0000 # Hex value
# ZVM_VI_HIGHLIGHT_EXTRASTYLE=bold,underline # bold and underline
#
# ZVM_VI_SURROUND_BINDKEY
# the key binding mode for surround operating (default is 'classic')
#
# 1. 'classic' mode (verb->s->surround):
# S" Add " for visual selection
# ys" Add " for visual selection
# cs"' Change " to '
# ds" Delete "
#
# 2. 's-prefix' mode (s->verb->surround):
# sa" Add " for visual selection
# sd" Delete "
# sr"' Change " to '
#
# How to select surround text object?
# vi" Select the text object inside the quotes
# va( Select the text object including the brackets
#
# Then you can do any operation for the selection:
#
# 1. Add surrounds for text object
# vi" -> S[ or sa[ => "object" -> "[object]"
#
# 2. Delete/Yank/Change text object
# di( or vi( -> d
# ca( or va( -> c
# yi( or vi( -> y
#
# ZVM_READKEY_ENGINE
# the readkey engine for reading and processing the key events, and the
# below engines are supported:
# ZVM_READKEY_ENGINE_NEX (Default)
# ZVM_READKEY_ENGINE_ZLE
#
# the NEX is a better engine for reading and handling the key events than
# the Zsh's ZLE engine, currently the NEX engine is at beta stage, and
# you can change to Zsh's ZLE engine if you want.
#
# ZVM_KEYTIMEOUT:
# the key input timeout for waiting for next key (default is 0.4 seconds)
#
# ZVM_ESCAPE_KEYTIMEOUT:
# the key input timeout for waiting for next key if it is beginning with
# an escape character (default is 0.03 seconds), and this option is just
# available for the NEX readkey engine
#
# ZVM_LINE_INIT_MODE
# the setting for init mode of command line (default is empty), empty will
# keep the last command mode, for the first command line it will be insert
# mode, you can also set it to a specific vi mode to alway keep the mode
# for each command line
#
# For example:
# ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
# ZVM_LINE_INIT_MODE=$ZVM_MODE_NORMAL
#
# ZVM_LAZY_KEYBINDINGS:
# the setting for lazy keybindings (default is true), and lazy keybindings
# will postpone the keybindings of vicmd and visual keymaps to the first
# time entering normal mode
#
# ZVM_NORMAL_MODE_CURSOR:
# the prompt cursor in normal mode
#
# ZVM_INSERT_MODE_CURSOR:
# the prompt cursor in insert mode
#
# ZVM_VISUAL_MODE_CURSOR:
# the prompt cursor in visual mode
#
# ZVM_VISUAL_LINE_MODE_CURSOR:
# the prompt cursor in visual line mode
#
# ZVM_OPPEND_MODE_CURSOR:
# the prompt cursor in operator pending mode
#
# You can change the cursor style by below:
# ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLOCK
#
# and the below cursor style are supported:
# ZVM_CURSOR_USER_DEFAULT
# ZVM_CURSOR_BLOCK
# ZVM_CURSOR_UNDERLINE
# ZVM_CURSOR_BEAM
# ZVM_CURSOR_BLINKING_BLOCK
# ZVM_CURSOR_BLINKING_UNDERLINE
# ZVM_CURSOR_BLINKING_BEAM
#
# ZVM_VI_EDITOR
# the editor to edit your command line (default is $EDITOR)
#
# ZVM_TMPDIR
# the temporary directory (default is $TMPDIR, otherwise it's /tmp/)
#
# ZVM_TERM
# the term for handling terminal sequences, it's important for some
# terminal emulators to show cursor properly (default is $TERM)
#
# ZVM_CURSOR_STYLE_ENABLED
# enable the cursor style feature (default is true)
#
# Avoid sourcing plugin multiple times
command -v 'zvm_version' >/dev/null && return
# Plugin information
typeset -gr ZVM_NAME='zsh-vi-mode'
typeset -gr ZVM_DESCRIPTION='💻 A better and friendly vi(vim) mode plugin for ZSH.'
typeset -gr ZVM_REPOSITORY='https://github.com/jeffreytse/zsh-vi-mode'
typeset -gr ZVM_VERSION='0.8.4'
# Plugin initial status
ZVM_INIT_DONE=false
# Postpone reset prompt (i.e. postpone the widget `reset-prompt`)
# empty (No postponing)
# true (Enter postponing)
# false (Trigger reset prompt)
ZVM_POSTPONE_RESET_PROMPT=
# Operator pending mode
ZVM_OPPEND_MODE=false
# Insert mode could be
# `i` (insert)
# `a` (append)
# `I` (insert at the non-blank beginning of current line)
# `A` (append at the end of current line)
ZVM_INSERT_MODE='i'
# The mode could be the below value:
# `n` (normal)
# `i` (insert)
# `v` (visual)
# `vl` (visual-line)
ZVM_MODE=''
# The keys typed to invoke this widget, as a literal string
ZVM_KEYS=''
# The region hilight information
ZVM_REGION_HIGHLIGHT=()
# Default zvm readkey engines
ZVM_READKEY_ENGINE_NEX='nex'
ZVM_READKEY_ENGINE_ZLE='zle'
ZVM_READKEY_ENGINE_DEFAULT=$ZVM_READKEY_ENGINE_NEX
# Default alternative character for escape characters
ZVM_ESCAPE_SPACE='\s'
ZVM_ESCAPE_NEWLINE='^J'
# Default vi modes
ZVM_MODE_LAST=''
ZVM_MODE_NORMAL='n'
ZVM_MODE_INSERT='i'
ZVM_MODE_VISUAL='v'
ZVM_MODE_VISUAL_LINE='vl'
ZVM_MODE_REPLACE='r'
# Default cursor styles
ZVM_CURSOR_USER_DEFAULT='ud'
ZVM_CURSOR_BLOCK='bl'
ZVM_CURSOR_UNDERLINE='ul'
ZVM_CURSOR_BEAM='be'
ZVM_CURSOR_BLINKING_BLOCK='bbl'
ZVM_CURSOR_BLINKING_UNDERLINE='bul'
ZVM_CURSOR_BLINKING_BEAM='bbe'
# The commands need to be repeated
ZVM_REPEAT_MODE=false
ZVM_REPEAT_RESET=false
ZVM_REPEAT_COMMANDS=($ZVM_MODE_NORMAL i)
##########################################
# Initial all default settings
# Default config function
: ${ZVM_CONFIG_FUNC:='zvm_config'}
# Load config by calling the config function
if command -v "$ZVM_CONFIG_FUNC" >/dev/null; then
$ZVM_CONFIG_FUNC
fi
# Set the readkey engine (default is NEX engine)
: ${ZVM_READKEY_ENGINE:=$ZVM_READKEY_ENGINE_DEFAULT}
# Set key input timeout (default is 0.4 seconds)
: ${ZVM_KEYTIMEOUT:=0.4}
# Set the escape key timeout (default is 0.03 seconds)
: ${ZVM_ESCAPE_KEYTIMEOUT:=0.03}
# Set keybindings mode (default is true)
# The lazy keybindings will post the keybindings of vicmd and visual
# keymaps to the first time entering the normal mode
: ${ZVM_LAZY_KEYBINDINGS:=true}
# All keybindings for lazy loading
if $ZVM_LAZY_KEYBINDINGS; then
ZVM_LAZY_KEYBINDINGS_LIST=()
fi
# Set the cursor stlye in defferent vi modes, the value you could use
# the predefined value, such as $ZVM_CURSOR_BLOCK, $ZVM_CURSOR_BEAM,
# $ZVM_CURSOR_BLINKING_BLOCK and so on.
: ${ZVM_INSERT_MODE_CURSOR:=$ZVM_CURSOR_BEAM}
: ${ZVM_NORMAL_MODE_CURSOR:=$ZVM_CURSOR_BLOCK}
: ${ZVM_VISUAL_MODE_CURSOR:=$ZVM_CURSOR_BLOCK}
: ${ZVM_VISUAL_LINE_MODE_CURSOR:=$ZVM_CURSOR_BLOCK}
# Operator pending mode cursor style (default is underscore)
: ${ZVM_OPPEND_MODE_CURSOR:=$ZVM_CURSOR_UNDERLINE}
# Set the vi escape key (default is ^[ => <ESC>)
: ${ZVM_VI_ESCAPE_BINDKEY:=^[}
: ${ZVM_VI_INSERT_ESCAPE_BINDKEY:=$ZVM_VI_ESCAPE_BINDKEY}
: ${ZVM_VI_VISUAL_ESCAPE_BINDKEY:=$ZVM_VI_ESCAPE_BINDKEY}
: ${ZVM_VI_OPPEND_ESCAPE_BINDKEY:=$ZVM_VI_ESCAPE_BINDKEY}
# Set the line init mode (empty will keep the last mode)
# you can also set it to others, such as $ZVM_MODE_INSERT.
: ${ZVM_LINE_INIT_MODE:=$ZVM_MODE_LAST}
: ${ZVM_VI_INSERT_MODE_LEGACY_UNDO:=false}
: ${ZVM_VI_SURROUND_BINDKEY:=classic}
: ${ZVM_VI_HIGHLIGHT_BACKGROUND:=#cc0000}
: ${ZVM_VI_HIGHLIGHT_FOREGROUND:=#eeeeee}
: ${ZVM_VI_HIGHLIGHT_EXTRASTYLE:=default}
: ${ZVM_VI_EDITOR:=${EDITOR:-vim}}
: ${ZVM_TMPDIR:=${TMPDIR:-/tmp/}}
# Set the term for handling terminal sequences, it's important for some
# terminal emulators to show cursor properly (default is $TERM)
: ${ZVM_TERM:=${TERM:-xterm-256color}}
# Enable the cursor style feature
: ${ZVM_CURSOR_STYLE_ENABLED:=true}
# All the extra commands
zvm_before_init_commands=()
zvm_after_init_commands=()
zvm_before_select_vi_mode_commands=()
zvm_after_select_vi_mode_commands=()
zvm_before_lazy_keybindings_commands=()
zvm_after_lazy_keybindings_commands=()
# All the handlers for switching keyword
zvm_switch_keyword_handlers=(
zvm_switch_number
zvm_switch_boolean
zvm_switch_operator
zvm_switch_weekday
zvm_switch_month
)
# Display version information
function zvm_version() {
echo -e "$ZVM_NAME $ZVM_VERSION"
echo -e "\e[4m$ZVM_REPOSITORY\e[0m"
echo -e "$ZVM_DESCRIPTION"
}
# The widget wrapper
function zvm_widget_wrapper() {
local rawfunc=$1;
local func=$2;
local -i retval
$func "${@:3}"
return retval
}
# Define widget function
function zvm_define_widget() {
local widget=$1
local func=$2 || $1
local result=($(zle -l -L "${widget}"))
# Check if existing the same name
if [[ ${#result[@]} == 4 ]]; then
local rawfunc=${result[4]}
local wrapper="zvm_${widget}-wrapper"
eval "$wrapper() { zvm_widget_wrapper $rawfunc $func \"\$@\" }"
func=$wrapper
fi
zle -N $widget $func
}
# Get the keys typed to invoke this widget, as a literal string
function zvm_keys() {
local keys=${ZVM_KEYS:-$KEYS}
# Append the prefix of keys if it is visual or visual-line mode
case "${ZVM_MODE}" in
$ZVM_MODE_VISUAL)
if [[ "$keys" != v* ]]; then
keys="v${keys}"
fi
;;
$ZVM_MODE_VISUAL_LINE)
if [[ "$keys" != V* ]]; then
keys="V${keys}"
fi
;;
esac
# Escape the newline and space characters, otherwise, we can't
# get the output from subshell correctly.
keys=${keys//$'\n'/$ZVM_ESCAPE_NEWLINE}
keys=${keys// /$ZVM_ESCAPE_SPACE}
echo $keys
}
# Find the widget on a specified bindkey
function zvm_find_bindkey_widget() {
local keymap=$1
local keys=$2
local prefix_mode=${3:-false}
retval=()
if $prefix_mode; then
local pos=0
local spos=3
local prefix_keys=
# Get the prefix keys
if [[ $prefix_keys ]]; then
prefix_keys=${prefix_keys:0:-1}
# If the last key is an escape key (e.g. \", \`, \\) we still
# need to remove the escape backslash `\`
if [[ ${prefix_keys: -1} == '\' ]]; then
prefix_keys=${prefix_keys:0:-1}
fi
fi
local result=$(bindkey -M ${keymap} -p "$prefix_keys")$'\n'
# Split string to array by newline
for ((i=$spos;i<$#result;i++)); do
# Save the last whitespace character of the line
# and continue continue handling while meeting `\n`
case "${result:$i:1}" in
' ') spos=$i; i=$i+1; continue;;
[$'\n']);;
*) continue;;
esac
# Check if it has the same prefix keys and retrieve the widgets
if [[ "${result:$((pos+1)):$#keys}" == "$keys" ]]; then
# Get the binding keys
local k=${result:$((pos+1)):$((spos-pos-2))}
# Escape spaces in key bindings (space -> $ZVM_ESCAPE_SPACE)
k=${k// /$ZVM_ESCAPE_SPACE}
retval+=($k ${result:$((spos+1)):$((i-spos-1))})
fi
# Save as new position
pos=$i+1
# Skip 3 characters
# One key and quotes at least (i.e \n"_" )
i=$i+3
done
else
local result=$(bindkey -M ${keymap} "$keys")
if [[ "${result: -14}" == ' undefined-key' ]]; then
return
fi
# Escape spaces in key bindings (space -> $ZVM_ESCAPE_SPACE)
for ((i=$#result;i>=0;i--)); do
# Backward find the first whitespace character
[[ "${result:$i:1}" == ' ' ]] || continue
# Retrieve the keys and widget
local k=${result:1:$i-2}
# Escape spaces in key bindings (space -> $ZVM_ESCAPE_SPACE)
k=${k// /$ZVM_ESCAPE_SPACE}
retval+=($k ${result:$i+1})
break
done
fi
}
# Read keys for retrieving widget
function zvm_readkeys() {
local keymap=$1
local key=${2:-$(zvm_keys)}
local keys=
local widget=
local result=
local pattern=
local timeout=
while :; do
# Keep reading key for escape character
if [[ "$key" == $'\e' ]]; then
while :; do
local k=
read -t $ZVM_ESCAPE_KEYTIMEOUT -k 1 k || break
key="${key}${k}"
done
fi
keys="${keys}${key}"
# Handle the pattern
if [[ -n "$key" ]]; then
# Transform the non-printed characters
local k=$(zvm_escape_non_printed_characters "${key}")
# Escape keys
# " -> \" It's a special character in bash syntax
# ` -> \` It's a special character in bash syntax
# <space> -> ` ` It's a special character in bash syntax
k=${k//\"/\\\"}
k=${k//\`/\\\`}
k=${k//$ZVM_ESCAPE_SPACE/ }
pattern="${pattern}${k}"
fi
# Find out widgets that match this key pattern
zvm_find_bindkey_widget $keymap "$pattern" true
result=(${retval[@]})
# Exit key input if there is only one widget matched
# or no more widget matched.
case ${#result[@]} in
2) key=; widget=${result[2]}; break;;
0) break;;
esac
# Evaluate the readkey timeout
# Special timeout for the escape sequence
if [[ "${keys}" == $'\e' ]]; then
timeout=$ZVM_ESCAPE_KEYTIMEOUT
# Check if there is any one custom escape sequence
for ((i=1; i<=${#result[@]}; i=i+2)); do
if [[ "${result[$i]}" =~ '^\^\[\[?[A-Z0-9]*~?\^\[' ]]; then
timeout=$ZVM_KEYTIMEOUT
break
fi
done
else
timeout=$ZVM_KEYTIMEOUT
fi
# Wait for reading next key, and we should save the widget
# as the final widget if it is full matching
key=
if [[ "${result[1]}" == "${pattern}" ]]; then
widget=${result[2]}
# Get current widget as final widget when reading key timeout
read -t $timeout -k 1 key || break
else
zvm_enter_oppend_mode
read -k 1 key
fi
done
# Exit operator pending mode
if $ZVM_OPPEND_MODE; then
zvm_exit_oppend_mode
fi
if [[ -z "$key" ]]; then
retval=(${keys} $widget)
else
retval=(${keys:0:-$#key} $widget $key)
fi
}
# Add key bindings
function zvm_bindkey() {
local keymap=$1
local keys=$2
local widget=$3
local params=$4
local key=
# We should bind keys with an existing widget
[[ -z $widget ]] && return
# If lazy keybindings is enabled, we need to add to the lazy list
if [[ ${ZVM_LAZY_KEYBINDINGS_LIST+x} && ${keymap} != viins ]]; then
keys=${keys//\"/\\\"}
keys=${keys//\`/\\\`}
ZVM_LAZY_KEYBINDINGS_LIST+=(
"${keymap} \"${keys}\" ${widget} \"${params}\""
)
return
fi
# Hanle the keybinding of NEX readkey engine
if [[ $ZVM_READKEY_ENGINE == $ZVM_READKEY_ENGINE_NEX ]]; then
# Get the first key (especially check if ctrl characters)
if [[ $#keys -gt 1 && "${keys:0:1}" == '^' ]]; then
key=${keys:0:2}
else
key=${keys:0:1}
fi
bindkey -M $keymap "${key}" zvm_readkeys_handler
fi
# Wrap params to a new widget
if [[ -n $params ]]; then
local suffix=$(zvm_string_to_hex $params)
eval "$widget:$suffix() { $widget $params }"
widget="$widget:$suffix"
zvm_define_widget $widget
fi
# Bind keys with with a widget
bindkey -M $keymap "${keys}" $widget
}
# Convert string to hexadecimal
function zvm_string_to_hex() {
local str=
for ((i=1;i<=$#1;i++)); do
str+=$(printf '%x' "'${1[$i]}")
done
echo "$str"
}
# Escape non-printed characters
function zvm_escape_non_printed_characters() {
local str=
for ((i=0;i<$#1;i++)); do
local c=${1:$i:1}
if [[ "$c" < ' ' ]]; then
local ord=$(($(printf '%d' "'$c")+64))
c=$(printf \\$(printf '%03o' $ord))
str="${str}^${c}"
elif [[ "$c" == '' ]]; then
str="${str}^?"
elif [[ "$c" == '