-
Notifications
You must be signed in to change notification settings - Fork 0
/
wstxtfun.zsh
401 lines (355 loc) · 8.31 KB
/
wstxtfun.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
# position functions
wstxtfun-pos() {
local pos=$1
local text="$2"
local text_end=${#text}
local ws_row=1
local ws_col=1
for i in {1..$text_end}; do
if [[ $i -gt $pos ]]; then
break
fi
if [[ $text[i] == $'\n' ]]; then
ws_row=$((ws_row+1))
ws_col=1
elif [[ $text[i] == $'\t' ]]; then
local rest=$(( (ws_col-1) % 8 ))
ws_col=$((ws_col+8-rest))
else
ws_col=$((ws_col+1))
fi
done
echo $ws_row $ws_col
}
wstxtfun-yx-pos()
{
local y=$1
local x=$2
local text="$3"
local lp=$(wstxtfun-line2pos $y "$text")
local len=$(wstxtfun-line-len $y "$text")
if [[ $x -le $len ]]; then
echo $((lp+x-2))
else
echo $((lp+len-1))
fi
}
# character functions
wstxtfun-prev-printable() {
local pos=$1
local text="$2"
local text_end=${#text}
local i=$pos
while [[ ! "$text[i]" =~ [[:graph:]] && $i -ge 1 ]]; do
i=$((i-1))
done
echo $i
}
wstxtfun-next-printable() {
local pos=$1
local text="$2"
local text_end=${#text}
local i=$((pos+1))
while [[ ! "$text[i]" =~ [[:graph:]] && $i -le $text_end ]]; do
i=$((i+1))
done
echo $((i-1))
}
# word functions
wstxtfun-prev-word() {
local pos=$1
local text="$2"
# skip until next word
local i=$pos
while [[ ! "$text[i]" =~ [[:alnum:]] && $i -ge 1 ]]; do
i=$((i-1))
done
# skip word or end of text
while [[ "$text[i]" =~ [[:alnum:]] && $i -ge 1 ]]; do
i=$((i-1))
done
echo $i
}
wstxtfun-next-word() {
local pos=$1
local text="$2"
local text_end=${#text}
# skip word or end of text
local i=$((pos+1))
while [[ "$text[i]" =~ [[:alnum:]] && $i -le $text_end ]]; do
i=$((i+1))
done
# skip until next word
while [[ ! "$text[i]" =~ [[:alnum:]] && $i -le $text_end ]]; do
i=$((i+1))
done
echo $((i-1))
}
wstxtfun-end-word() {
local pos=$1
local text="$2"
local text_end=${#text}
local i=$((pos+1))
while [[ "$text[i]" =~ [[:alnum:]] && $i -le $text_end ]]; do
i=$((i+1))
done
echo $((i-1))
}
# line functions
wstxtfun-line-start() {
local pos=$1
local text="$2"
local i=$pos
while [[ ! "$text[i]" = $'\n' && $i -ge 1 ]]; do
i=$((i-1))
done
echo $i
}
wstxtfun-real-col() {
local col=$1
local tabwidth=$2
local text="$3"
local textlen=${#text}
if [[ "$col" = 0 ]]; then
echo 0
ws-debug WSTXTFUN_REAL_COL_A: col=$col res=0
return
fi
if [[ "$col" -gt $textlen ]]; then
echo $textlen
ws-debug WSTXTFUN_REAL_COL_B: col=$col res=$textlen
return
fi
local res=0
for i in {1..$col}; do
if [[ "$text[i]" = $'\t' ]]; then
res=$((res+tabwidth-res%tabwidth))
elif [[ "$text[i]" = $'\n' ]]; then
res=0
else
res=$((res+1))
fi
done
ws-debug WSTXTFUN_REAL_COL_C col=$col res=$res
echo $res
}
wstxtfun-line-end() {
local pos=$1
local text="$2"
local text_end=${#text}
local i=$((pos+1))
while [[ ! "$text[i]" = $'\n' && $i -le $text_end ]]; do
i=$((i+1))
done
echo $(ws-min $((i-1)) $text_end)
}
wstxtfun-line2pos() {
local line=$1
local text="$2"
local i=1
local curr=1
local text_end=${#text}
while [[ $curr -lt $line && $i -le $text_end ]]; do
if [[ "$text[i]" = $'\n' ]]; then
curr=$((curr+1))
fi
i=$((i+1))
done
echo $i
}
wstxtfun-nlines() {
echo ${1:gs/\\n//} | wc -l # don't count fake new lines
}
wstxtfun-pos2line() {
local pos=$1
local text="$2"
local i=1
local curr=1
local text_end=${#text}
while [[ $i -lt $pos && $i -le $text_end ]]; do
if [[ "$text[i]" = $'\n' ]]; then
curr=$((curr+1))
fi
i=$((i+1))
done
echo $curr
}
wstxtfun-line-len() {
local line=$1
local text="$2"
local text_end=${#text}
local begin=$(wstxtfun-line2pos $line "$text")
local i=$begin
while [[ ! "$text[i]" = $'\n' && $i -le $text_end ]]; do
i=$((i+1))
done
echo $((i-begin))
}
wstxtfun-line-last-pos() {
local line=$1
local text="$2"
local text_end=${#text}
local begin=$(wstxtfun-line2pos $line "$text")
local i=$begin
while [[ ! "$text[i]" = $'\n' && $i -le $text_end ]]; do
i=$((i+1))
done
echo $((i-1))
}
wstxtfun-test-pcre() {
if type pcre_match > /dev/null 2>&1
then
return 0
else
zmodload zsh/pcre > /dev/null 2>&1
return $?
fi
}
# sentence functions
wstxtfun-prev-sentence() {
local pos=$1
local text="$2"
if ! wstxtfun-test-pcre; then
return
fi
local x=$(wstxtfun-prev-word $pos "$text")
pcre_compile -m -x "(\\.|!|\\?)[[:punct:][:space:]]*(\s{2}|\t|\n|\Z)[[:punct:][:space:]]*"
local lastb2=-1
if pcre_match -b -- $text; then
while [[ $? -eq 0 ]] do
local b=($=ZPCRE_OP)
if [[ $b[1] -gt $x ]]; then
echo $lastb2
break;
fi
lastb2=$b[2]
pcre_match -b -n $b[2] -- $text
done
if [[ ! $lastb2 -eq -1 ]]; then
echo $lastb2
else
echo 0
fi
else
echo 0
fi
}
wstxtfun-next-sentence() {
local pos=$1
local text="$2"
if ! wstxtfun-test-pcre; then
return
fi
# find next alphanumeric character
pcre_compile -m -x "[[:alnum:]]"
if pcre_match -b -n $pos -- $text; then
local b=($=ZPCRE_OP)
local pos2=$b[1]
else
echo ${#text}
return
fi
# find sentence end
pcre_compile -m -x "(\\.|!|\\?)[[:punct:][:space:]]*(\s{2}|\t|\n)"
if pcre_match -b -n $pos2 -- $text; then
local b2=($=ZPCRE_OP)
echo $b2[1]
else
echo ${#text}
fi
}
# get sentence begin / end from the position
wstxtfun-sentence-pos() {
local pos=$1
local text="$2"
local text_end=${#text}
if ! wstxtfun-test-pcre; then
return
fi
local i=1
while [[ ! "$text[i]" =~ "[[:alnum:]]" && $i -le $text_end ]]; do
i=$((i+1))
done
if [[ $pos -lt $((i-1)) || $i -gt $text_end ]]; then
echo -1
return
fi
local from=$i
local to=-1
local esen=-1
pcre_compile -m -x "(\\.|!|\\?)[[:punct:][:space:]]*(\s{2}|\t|\n|\Z)[[:punct:][:space:]]*"
if pcre_match -b -n $from -- $text; then
while [[ $? -eq 0 ]] do
local b=($=ZPCRE_OP)
if [[ $b[2] -gt $pos || ($b[2] -eq $pos && $pos -eq $text_end)]]; then
to=$(($b[2]+1))
esen=$(($b[1]+1))
break;
fi
from=$(($b[2]+1))
pcre_match -b -n $b[2] -- $text
done
if [[ $to -eq -1 ]]; then
echo -1
else
echo $from $esen $to
fi
else
echo -1
fi
}
# paragraph functions
wstxtfun-prev-paragraph() {
local pos=$1
local text="$2"
if ! wstxtfun-test-pcre; then
return
fi
# find an alnum character
local i=$pos
while [[ ! "$text[i]" =~ [[:alnum:]] && $i -ge 1 ]]; do
i=$((i-1))
done
# go to first empty line before $i
pcre_compile -m -x "\n[[:space:]\\\\]*\n"
local lastb1=-1
if pcre_match -b -- $text; then
while [[ $? -eq 0 ]]; do
local b=($=ZPCRE_OP)
ws-debug b1=$b[1] b2=$b[2]
if [[ $b[1] -gt $i ]]; then
break
fi
lastb1=$b[1]
pcre_match -b -n $b[2] -- $text
done
if [[ $lastb1 -eq -1 ]]; then
echo 0
else
echo $((lastb1+1))
fi
else
echo 0
fi
}
wstxtfun-next-paragraph() {
local pos=$1
local text="$2"
local text_end=${#text}
if ! wstxtfun-test-pcre; then
return
fi
# find an alnum character
local i=$pos
while [[ ! "$text[i]" =~ [[:alnum:]] && $i -le $text_end ]]; do
i=$((i+1))
done
# go to first empty line before $i
pcre_compile -m -x "\n[[:space:]\\\\]*\n"
if pcre_match -b -n $i -- $text; then
local b=($=ZPCRE_OP)
echo $(($b[1]+1))
else
echo $text_end
fi
}