-
Notifications
You must be signed in to change notification settings - Fork 0
/
true-vim-terminal.plugin.bash
150 lines (140 loc) · 4.64 KB
/
true-vim-terminal.plugin.bash
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
#!/usr/bin/bash
{ [ -z "$TVT_DEMO" ] && [ -z "$TVT_TEST" ]; } || PS1="[TVT Demo \\W]\\$ "
# this CANNOT be ^W
# vim really hates passing it through consistently
# use showkey -a to make sure it's something passed through properly
#TVT_ESCAPE=""
# decimal ASCII code for IPC delimiter (default is unit separator)
#TVT_DELIMITER=31
# how long to sleep to finish drawing BUFFER in 10ms increments
# can be modified on-the-fly
#TVT_DRAW_SLEEP=1
# how long to sleep to let vim get new content in 10ms increments
# can be modified on-the-fly
#TVT_REDRAW_SLEEP=1
[ -n "$VIM_TERMINAL" ] && {
#{{{ tvt.tapi_feedkeys()
# don't use the x flag, it breaks things with terminal mode
#tvt.tapi_feedkeys() {
# text="$1"
# text="${text//\\/\\\\\\\\}"
# text="${text//\\\\\\\\</\\\\<}"
# text="${text//\"/\\\\\\\"}"
# printf '\033]51;["call","Tapi_TVT_Feedkeys",["%s","%s"]]\007' "$text" "$2"
#}
#}}}
#{{{ tvt.escape()
tvt.escape() {
[ -z "$TVT_TEST" ] || printf -- "-\n" >&4
local REPLY
local LBUFFER
local RBUFFER
#{{{ variable validation and parsing
[ "${BASH_VERSION::1}" != "4" ] && {
LBUFFER="${READLINE_LINE::$READLINE_POINT}"
RBUFFER="${READLINE_LINE:$READLINE_POINT}"
} || {
LBUFFER="$(printf "%s" "$READLINE_LINE" | head -c $READLINE_POINT)"
RBUFFER="$(printf "%s" "$READLINE_LINE" | { head -c $READLINE_POINT > /dev/null; cat; })"
}
[ "${TVT_DRAW_SLEEP#*[!0-9]}" = "$TVT_DRAW_SLEEP" ] || {
printf "TVT_DRAW_SLEEP is not a positive integer! Defaulting back to 1\n" >&2
TVT_DRAW_SLEEP=1
}
[ "${TVT_REDRAW_SLEEP#*[!0-9]}" = "$TVT_REDRAW_SLEEP" ] || {
printf "TVT_REDRAW_SLEEP is not a positive integer! Defaulting back to 1\n" >&2
TVT_REDRAW_SLEEP=1
}
local TVT_DRAW_SLEEP
TVT_DRAW_SLEEP="00$TVT_DRAW_SLEEP"
TVT_DRAW_SLEEP="${TVT_DRAW_SLEEP::-2}.${TVT_DRAW_SLEEP: -2}"
TVT_DRAW_SLEEP="${TVT_DRAW_SLEEP#${TVT_DRAW_SLEEP%%[!0]*}}"
[ "${TVT_DRAW_SLEEP::1}" != "." ] || TVT_DRAW_SLEEP="0$TVT_DRAW_SLEEP"
#}}}
# no terminal to answer these prompts in tests
[ -z "$TVT_TEST" ] && {
printf "${PS1@P}$LBUFFER"
printf "\033[6n"
IFS= read -r -d "R"
printf "$RBUFFER${REPLY}H"
# test buffers must be lower than width, though, which we can use
} || printf "\r\033[K${PS1@P}$LBUFFER$RBUFFER\r${PS1@P}$LBUFFER"
while true; do
sleep $TVT_DRAW_SLEEP
[ -z "$TVT_TEST" ] && printf '\033]51;["call","Tapi_TVT_Escape",[%d]]\007' "$TVT_REDRAW_SLEEP"
# adding this makes vim add a new line and I don't know why
# it has zero effect here anyway, only necessary in zsh
#sleep $TVT_REDRAW_SLEEP_HERE
REPLY=
while IFS= read -r -d "$TVT_DELIMITER" action; do
REPLY="$REPLY$action$TVT_DELIMITER"
[[ "${action::1}" =~ [01] ]] && break
done
while IFS= read -r -d "$TVT_DELIMITER" action; do
#{{{ do action
case "${action::1}" in
*)
action="${action:1}"
;;&
0) # done and stay in shell
printf "\r\033[K"
READLINE_LINE="$LBUFFER$RBUFFER"
[ "${BASH_VERSION::1}" != "4" ] && READLINE_POINT="${#LBUFFER}" || READLINE_POINT=$(printf "%s" "$LBUFFER" | wc -c)
[ -z "$TVT_TEST" ] || printf -- "-\n" >&4
return
;;
2) # set or [inc/dec]rement cursor position
READLINE_LINE="$LBUFFER$RBUFFER"
[[ "${action::1}" =~ [+-] ]] && READLINE_POINT=$((${#LBUFFER}$action)) || READLINE_POINT=$action
[ $READLINE_POINT -lt 0 ] && READLINE_POINT=0
LBUFFER="${READLINE_LINE::$READLINE_POINT}"
RBUFFER="${READLINE_LINE:$READLINE_POINT}"
;&
1|2) # redraw
# no terminal to answer these prompts in tests
[ -z "$TVT_TEST" ] && {
printf "\r\033[K${PS1@P}$LBUFFER"
printf "\033[6n"
IFS= read -r -d "R" <&3
printf "$RBUFFER${REPLY}H"
# test buffers must be lower than width, though, which we can use
} || printf "\r\033[K${PS1@P}$LBUFFER$RBUFFER\r${PS1@P}$LBUFFER"
;;&
1) # done and go back to vim
[ -z "$TVT_TEST" ] || printf -- "-\n" >&4
break
;;
3) # set LBUFFER
LBUFFER="$action"
;;
4) # set RBUFFER
RBUFFER="$action"
;;
5) # append to LBUFFER
LBUFFER="$LBUFFER$action"
;;
6) # prepend RBUFFER
RBUFFER="$action$RBUFFER"
;;
esac
#}}}
done 3<&1 <<< "$REPLY"
done
}
#}}}
TVT_DRAW_SLEEP="${TVT_DRAW_SLEEP:-1}"
TVT_REDRAW_SLEEP="${TVT_REDRAW_SLEEP:-1}"
if [ "${TVT_DELIMITER#*[!0-9]}" = "$TVT_DELIMITER" ]; then
if [ -n "$TVT_ESCAPE" ]; then
printf '\033]51;["call","Tapi_TVT_Init",[%d,"%s"]]\007' ${TVT_DELIMITER:-31} "$TVT_ESCAPE"
TVT_ESCAPE="${TVT_ESCAPE/^\[/\\033}"
TVT_ESCAPE="${TVT_ESCAPE/^/\\C-}"
bind -x "\"$TVT_ESCAPE\":tvt.escape"
else
printf "TVT_ESCAPE is not set!\n" >&2
fi
TVT_DELIMITER="$(printf "$(printf "\\%o" ${TVT_DELIMITER:-31})")"
else
printf "TVT_DELIMITER is not a positive integer!\n" >&2
fi
}