Skip to content

Commit 0a4e6a1

Browse files
committed
Makes code a tiny bit more compact and adds comments
1 parent 443ad1c commit 0a4e6a1

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

autopairs.sh

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Show where the matching open paren is when inserting a closing one. Disabling
44
# as it hijacks the `)`, `]` and `}` characters to enable blinking.
5-
bind "set blink-matching-paren on"
5+
bind "set blink-matching-paren off"
66

77
function __autopair() {
88
local typed_char="$1"
@@ -11,33 +11,41 @@ function __autopair() {
1111
local cursor_char="${READLINE_LINE:READLINE_POINT:1}"
1212
local previous_char="${READLINE_LINE:READLINE_POINT-1:1}"
1313
local next_char="${READLINE_LINE:READLINE_POINT+1:1}"
14-
local num_of_char
1514

16-
local s
17-
s="${READLINE_LINE::READLINE_POINT}"
15+
local s="${READLINE_LINE::READLINE_POINT}"
1816

17+
# escaped character
1918
if [[ "$previous_char" == "\\" ]]; then
2019
s+="$typed_char"
20+
21+
# ''""``
2122
elif [[ "$opening_char" == "$closing_char" ]]; then
22-
num_of_char="${READLINE_LINE//\\$typed_char/}"
23+
local num_of_char="${READLINE_LINE//\\$typed_char/}"
2324
num_of_char="${num_of_char//[^$typed_char]/}"
2425
num_of_char="${#num_of_char}"
2526

2627
if [[ "$((num_of_char % 2))" -eq 1 ]]; then
2728
s+="$typed_char"
28-
elif [[ "$cursor_char" == "$closing_char" ]]; then
29-
:
30-
elif [[ "$((num_of_char % 2))" -eq 0 ]]; then
29+
else
3130
s+="$typed_char$typed_char"
3231
fi
32+
33+
# [{(
3334
elif [[ "$typed_char" == "$opening_char" ]]; then
35+
# TODO check right part string for balance
3436
s+="$opening_char$closing_char"
37+
38+
# [ | ]{ | }( | ) and pressing ]})
3539
elif [[ "$typed_char" == "$closing_char" && "$cursor_char" == " " &&
3640
"$next_char" == "$closing_char" ]]; then
3741
s+=' '
3842
((READLINE_POINT++))
43+
44+
# ]}): cursor is already on closing char
3945
elif [[ "$cursor_char" == "$closing_char" ]]; then
46+
# TODO check left and right string parts for balance
4047
:
48+
# ]})
4149
else
4250
s+="$typed_char"
4351
fi
@@ -56,28 +64,25 @@ function __autopair_space() {
5664
local next_char="${READLINE_LINE:READLINE_POINT+1:1}"
5765
local num_of_char
5866

59-
local s
60-
s="${READLINE_LINE::READLINE_POINT}"
67+
local s="${READLINE_LINE::READLINE_POINT}"
6168

62-
# https://unix.stackexchange.com/questions/213799#answer-213821
6369
# The user pressed space, so we want to print at least one space no matter
6470
# what. If magic-space is enabled on the space bar, send a magic space. If
6571
# not, send a regular space.
6672
if [[ "$magic_space_enabled_on_space" -eq 1 ]]; then
73+
# https://unix.stackexchange.com/questions/213799#answer-213821
6774
bind '"\e[0n": magic-space' && printf '\e[5n'
6875
else
6976
s+=' '
7077
((READLINE_POINT++))
7178
fi
7279

73-
local autopair_operated=false
74-
for pair in "${__pairs[@]:2}"; do
80+
for pair in "${__pairs[@]:3}"; do
7581
local opening_char="${pair:0:1}"
7682
local closing_char="${pair:1:1}"
7783

7884
if [[ "$previous_char" == "$opening_char" && "$cursor_char" == "$closing_char" ]]; then
7985
s+=" "
80-
autopair_operated=true
8186
break
8287
fi
8388
done
@@ -88,64 +93,54 @@ function __autopair_space() {
8893
}
8994

9095
function __autopair_remove() {
91-
local previous_char="${READLINE_LINE:READLINE_POINT-1:1}"
92-
local cursor_char="${READLINE_LINE:READLINE_POINT:1}"
93-
96+
# empty line or backspace at the start of line
9497
if [[ "${#READLINE_LINE}" -eq 0 || "$READLINE_POINT" -eq 0 ]]; then
9598
return
9699
fi
97100

98-
local s
99-
s="${READLINE_LINE::READLINE_POINT-1}"
100-
101-
local autopair_operated=false
101+
local s="${READLINE_LINE::READLINE_POINT-1}"
102+
local previous_char="${READLINE_LINE:READLINE_POINT-1:1}"
103+
local cursor_char="${READLINE_LINE:READLINE_POINT:1}"
102104
local pair
105+
local offset=0
106+
local loop_index=0
107+
local num_of_char
103108

104-
# double space in ()[]{}
105-
for pair in "${__pairs[@]:2}"; do
109+
for pair in "${__pairs[@]}"; do
106110
local minus_2_char="${READLINE_LINE:READLINE_POINT-2:1}"
107111
local next_char="${READLINE_LINE:READLINE_POINT+1:1}"
108112

113+
# ()[]{}: delete first space in double space (e.g. {A|B}, delete space "A")
109114
if [[ "$previous_char" == ' ' ]] \
110115
&& [[ "$cursor_char" == ' ' ]] \
111116
&& [[ "$minus_2_char" == "${pair:0:1}" ]] \
112117
&& [[ "$next_char" == "${pair:1:1}" ]]; then
113-
s+="${READLINE_LINE:READLINE_POINT+1}"
114-
autopair_operated=true
115-
fi
116-
done
117-
118-
# ()[]{}
119-
for pair in "${__pairs[@]:2}"; do
120-
if [[ "$previous_char" == "${pair:0:1}" ]] \
121-
&& [[ "$cursor_char" == "${pair:1:1}" ]]; then
122-
123-
s+="${READLINE_LINE:READLINE_POINT+1}"
124-
autopair_operated=true
118+
offset=1
125119
break
126-
fi
127-
done
128120

129-
# ""''
130-
for pair in "${__pairs[@]:0:2}"; do
131-
if [[ "$previous_char" == "${pair:0:1}" ]] \
121+
# all pairs: delete the opening
122+
elif [[ "$previous_char" == "${pair:0:1}" ]] \
132123
&& [[ "$cursor_char" == "${pair:1:1}" ]]; then
133124

134-
num_of_char="${READLINE_LINE//[^${pair:0:1}]/}"
135-
num_of_char="${#num_of_char}"
125+
# ''""``: delete results in balanced pairs on line
126+
if [[ "$loop_index" -lt 3 ]]; then
127+
num_of_char="${READLINE_LINE//[^${pair:0:1}]/}"
128+
num_of_char="${#num_of_char}"
136129

137-
if [[ "$((num_of_char % 2))" -eq 1 ]]; then
138-
break
130+
if [[ "$((num_of_char % 2))" -eq 1 ]]; then
131+
break
132+
fi
139133
fi
140134

141-
s+="${READLINE_LINE:READLINE_POINT+1}"
142-
autopair_operated=true
135+
# all pairs: delete whole pair
136+
offset=1
137+
break
143138
fi
139+
140+
((index++))
144141
done
145142

146-
if [[ "$autopair_operated" == 'false' ]]; then
147-
s+="${READLINE_LINE:READLINE_POINT}"
148-
fi
143+
s+="${READLINE_LINE:READLINE_POINT+$offset}"
149144

150145
READLINE_LINE="$s"
151146

@@ -161,7 +156,10 @@ __pairs=(
161156
'{}'
162157
)
163158

164-
for pair in "${__pairs[@]}"; do
159+
for pair in "${__pairs[@]:1:3}"; do
160+
bind -x "\"${pair:0:1}\": __autopair \\${pair:0:1} \\${pair:0:1} \\${pair:1:1}"
161+
done
162+
for pair in "${__pairs[@]:3}"; do
165163
bind -x "\"${pair:0:1}\": __autopair \\${pair:0:1} \\${pair:0:1} \\${pair:1:1}"
166164
bind -x "\"${pair:1:1}\": __autopair \\${pair:1:1} \\${pair:0:1} \\${pair:1:1}"
167165
done

0 commit comments

Comments
 (0)