-
Notifications
You must be signed in to change notification settings - Fork 1
/
.autopair.zsh
164 lines (138 loc) · 4.17 KB
/
.autopair.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
#!/usr/bin/env zsh
# A widget that auto-inserts matching pairs in ZSH.
AUTOPAIR_INHIBIT_INIT=${AUTOPAIR_INHIBIT_INIT:-}
AUTOPAIR_BETWEEN_WHITESPACE=${AUTOPAIR_BETWEEN_WHITESPACE:-}
typeset -gA AUTOPAIR_PAIRS
AUTOPAIR_PAIRS=('`' '`' "'" "'" '"' '"' '{' '}' '[' ']' '(' ')')
typeset -gA AUTOPAIR_LBOUNDS
AUTOPAIR_LBOUNDS=('`' '`')
AUTOPAIR_LBOUNDS[all]='[.:/\!]'
AUTOPAIR_LBOUNDS[quotes]='[]})a-zA-Z0-9]'
AUTOPAIR_LBOUNDS[braces]=''
AUTOPAIR_LBOUNDS['"']='"'
AUTOPAIR_LBOUNDS["'"]="'"
typeset -gA AUTOPAIR_RBOUNDS
AUTOPAIR_RBOUNDS[all]='[[{(<,.:?/%$!a-zA-Z0-9]'
AUTOPAIR_RBOUNDS[quotes]='[a-zA-Z0-9]'
AUTOPAIR_RBOUNDS[braces]=''
####
ap-get-pair() {
if [[ -n "$1" ]]; then
echo "${AUTOPAIR_PAIRS[$1]}"
elif [[ -n "$2" ]]; then
local i
for i in ${(@k)AUTOPAIR_PAIRS}; do
[[ "$2" == "${AUTOPAIR_PAIRS[$i]}" ]] && echo "$i" && break
done
fi
}
ap-boundary-p() {
[[ -n "$1" && "$LBUFFER" =~ "$1$" ]] || [[ -n "$2" && "$RBUFFER" =~ "^$2" ]]
}
ap-next-to-boundary-p() {
local -a groups
groups=(all)
case "$1" in
\'|\"|\`) groups+=quotes ;;
\{|\[|\(|\<) groups+=braces ;;
esac
groups+="$1"
local group
for group in $groups; do
ap-boundary-p "$AUTOPAIR_LBOUNDS[$group]" "$AUTOPAIR_RBOUNDS[$group]" && return 0
done
return 1
}
# If provided pair is balanced in the buffer
ap-balanced-p() {
local lbuf="${LBUFFER//\\$1}"
local rbuf="${RBUFFER//\\$2}"
local llen="${#lbuf//[^$1]}"
local rlen="${#rbuf//[^$2]}"
(( $rlen == 0 && $llen == 0 )) && return 0
if [[ "$1" == "$2" ]]; then
(( $llen == $rlen || ($llen + $rlen) % 2 == 0 )) && return 0
else
local l2len="${#lbuf//[^$2]}"
local r2len="${#rbuf//[^$1]}"
local ltotal=$(( $llen - $l2len ))
local rtotal=$(( $rlen - $r2len ))
(( $ltotal < 0 )) && ltotal=0
(( $ltotal < $rtotal )) && return 1
return 0
fi
return 1
}
ap-can-pair-p() {
local rchar=$(ap-get-pair "$KEYS")
# Don't pair if pair doesn't exist
[[ -z "$rchar" ]] && return 1
# Force pair if surrounded by space/[BE]OL, regardless of boundaries/balance
[[ -n "$AUTOPAIR_BETWEEN_WHITESPACE" && \
"$LBUFFER" =~ "(^|[ ])$" && \
"$RBUFFER" =~ "^($|[ ])" ]] && return 0
# Don't pair quotes if the delimiters are unbalanced
! ap-balanced-p "$KEYS" "$rchar" && return 1
# Don't pair when in front of characters that likely signify the start of a string
# or path (i.e. boundary characters)
ap-next-to-boundary-p "$KEYS" "$rchar" && return 1
return 0
}
ap-can-skip-p() {
! [[ -n "$2" && "$RBUFFER[1]" == "$2" && "$LBUFFER[-1]" != '\' ]] && return 1
[[ "$1" == "$2" ]] && ! ap-balanced-p "$1" "$2" && return 1
return 0
}
ap-can-delete-p() {
local lchar="$LBUFFER[-1]"
local rchar=$(ap-get-pair "$lchar")
! [[ -n "$rchar" && "$RBUFFER[1]" == "$rchar" ]] && return 1
[[ "$lchar" == "$rchar" ]] && ! ap-balanced-p "$lchar" "$rchar" && return 1
return 0
}
autopair-self-insert() {
LBUFFER+="$1$2"
zle backward-char
}
autopair-insert() {
local rchar=$(ap-get-pair "$KEYS")
if [[ "$KEYS" == (\'|\"|\`) ]] && ap-can-skip-p "$KEYS" "$rchar"; then
zle forward-char
elif ap-can-pair-p; then
autopair-self-insert "$KEYS" "$rchar"
else
zle self-insert
fi
}
autopair-close() {
if ap-can-skip-p $(ap-get-pair "" "$KEYS") "$KEYS"
then zle forward-char
else zle self-insert
fi
}
autopair-delete() {
ap-can-delete-p && zle .delete-char
zle backward-delete-char
}
# Initialization
autopair-init() {
zle -N autopair-insert
zle -N autopair-close
zle -N autopair-delete
local i
for i in ${(@k)AUTOPAIR_PAIRS}; do
bindkey "$i" autopair-insert
bindkey -M isearch "$i" self-insert
done
local -a l
l=(')' '}' ']')
for i in $l; do
bindkey "$i" autopair-close
bindkey -M isearch "$i" self-insert
done
bindkey "^?" autopair-delete
bindkey "^h" autopair-delete
bindkey -M isearch "^?" backward-delete-char
bindkey -M isearch "^h" backward-delete-char
}
[[ -z "$AUTOPAIR_INHIBIT_INIT" ]] && autopair-init