-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwstext.zsh
479 lines (405 loc) · 13 KB
/
wstext.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
# Character move functions
wstext-char-left() {
local pos=${(P)wstext_posvar}
local newpos=$(ws-max 0 $((pos-1)))
eval "$wstext_posvar=$newpos"
wstext-upd
}
wstext-char-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local newpos=$(ws-min $text_end $((pos+1)))
eval "$wstext_posvar=$newpos"
wstext-upd
}
wstext-prev-word() {
local pos=${(P)wstext_posvar}
local prev=$(wstxtfun-prev-word $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$prev"
wstext-upd
}
wstext-next-word() {
local pos=${(P)wstext_posvar}
local next=$(wstxtfun-next-word $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$next"
wstext-upd
}
wstext-end-word() {
local pos=${(P)wstext_posvar}
local end_word=$(wstxtfun-end-word $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$end_word"
wstext-upd
}
# Line functions
wstext-line-start() {
local pos=${(P)wstext_posvar}
local line_start=$(wstxtfun-line-start $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$line_start"
wstext-upd
}
wstext-line-end() {
local pos=${(P)wstext_posvar}
local line_end=$(wstxtfun-line-end $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$line_end"
wstext-upd
}
wstext-jump-lines() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local jump=$1
local nlines=$2
local line_begin=$3
local ws_row
local ws_col
read ws_row ws_col <<< $(wstxtfun-pos $pos "$text")
local new_row=$((ws_row+jump))
if [[ $new_row -lt 0 ]]; then
ws-debug WSTEXT_JUMP_LINES: BAD ARGUMENT: jump back too big: jump=$jump
new_row=0
elif [[ $new_row -gt $nlines ]]; then
ws-debug WSTEXT_JUMP_LINES: BAD ARGUMENT: jump forward too big: jump=$jump
new_row=$nlines
fi
if [[ -n $line_begin ]]; then
ws_col=1
fi
if [[ $nlines -ge $new_row ]]; then
local new_pos=$(wstxtfun-yx-pos $new_row $ws_col "$text")
eval "$wstext_posvar=$new_pos"
wstext-upd
fi
}
# Sentence functions (end-of-sentence: dot-space-space or dot-newline)
wstext-prev-sentence() {
local pos=${(P)wstext_posvar}
local prev_sentence_pos=$(wstxt-prev-sentence $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$prev_sentence_pos"
wstext-upd
}
wstext-next-sentence() {
local pos=${(P)wstext_posvar}
local next_sentece_pos=$(wstxt-next-sentence $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$next_sentence_pos"
wstext-upd
}
wstext-upd() {
if typeset -f $wstext_updfnvar > /dev/null; then
$wstext_updfnvar "$@"
fi
}
# Previous paragraph: find previous empty line or start of text
wstext-prev-paragraph() {
local pos=${(P)wstext_posvar}
local prev_paragraph_pos=$(wstxtfun-prev-paragraph-pos $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$prev_paragraph_pos"
wstext-upd
}
# Next paragraph: find next empty line or end of text
wstext-next-paragraph() {
local pos=${(P)wstext_posvar}
local next_paragraph_pos=$(wstxtfun-next-paragraph $pos "${(P)wstext_textvar}")
eval "$wstext_posvar=$next_paragraph_pos"
wstext-upd
}
# start document
wstext-start-document() {
eval "$wstext_posvar=0"
wstext-upd
}
# end document
wstext-end-document() {
local text="${(P)wstext_textvar}"
local text_end=${#text}
eval "$wstext_posvar=$text_end"
wstext-upd
}
# TODO: start document, end document
# Delete character functions
wstext-del-char-left() {
local pos=${(P)wstext_posvar}
if [[ $pos -eq 0 ]]; then
eval "$wstext_posvar=$pos"
else
local text="${(P)wstext_textvar}"
local text_end=${#text}
wstext-delete $pos $pos
eval "$wstext_posvar=$((pos-1))"
wstext-upd true
fi
}
wstext-del-char-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
if [[ $pos -lt $text_end ]]; then
wstext-delete $((pos+1)) $((pos+1))
fi
eval "$wstext_posvar=$pos"
wstext-upd true
}
# Delete word functions
wstext-del-word-left() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local from=$(wstxtfun-prev-word $pos "$text")
wstext-delete $((from+1)) $((pos))
eval "$wstext_posvar=$from"
wstext-upd true
}
# what it does: at word start: delete word, delete all non-printable characters that follow
# word middle: delete until end of word (do not touch any printable or non-printable characters that follow)
# non-word printable: delete 1 char + delete non-printable that follow
# non-word non-printable: delete all non-printable that follow cursor position
wstext-del-word-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local word_begin=$(wstxtfun-prev-word $((pos+2)) "$text")
local word_end=$(wstxtfun-end-word $word_begin "$text")
local next_word=$(wstxtfun-next-word $pos "$text")
local text_end=${#text}
if [[ $pos -le $word_begin ]]; then
local del_end=$(wstxtfun-next-printable $word_end "$text")
ws-debug DEL_WORD_RIGHT: pos=$pos word_begin=$word_begin del_end=$del_end
if [[ $pos -eq 0 && $del_end -eq 0 ]]; then
wstext-delete 0 1
else
wstext-delete $((pos+1)) $del_end
fi
elif [[ $pos -lt $word_end ]]; then
wstext-delete $((pos+1)) $word_end
else
local next_printable=$(wstxtfun-next-printable $((pos+1)) "$text")
if [[ $next_printable -eq $next_word ]]; then
wstext-delete $((pos+1)) $((next_printable-1))
else
wstext-delete $((pos+1)) $((next_printable))
fi
fi
eval "$wstext_posvar=$pos"
wstext-upd true
}
# delete whole word if inside a word and characters and non-printable outside
wstext-del-word() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local word_begin=$(wstxtfun-prev-word $((pos+2)) "$text")
local word_end=$(wstxtfun-end-word $word_begin "$text")
local to=$(wstxtfun-next-word $pos "$text")
local text_end=${#text}
if [[ $pos -lt $word_end ]]; then
local del_end=$(wstxtfun-next-printable $word_end "$text")
local from=$(ws-min $word_begin $pos)
wstext-delete $((from+1)) $((del_end))
eval "$wstext_posvar=$from"
else
local prev_printable=$(wstxtfun-prev-printable $pos "$text")
local next_printable=$(wstxt-next-printable $((pos+1)) "$text")
wstext-delete $((prev_printable+1)) $((next_printable))
eval "$wstext_posvar=$prev_printable"
fi
wstext-upd true
}
# Delete line functions
wstext-del-line-left() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local from=$(wstxtfun-line-start $pos "$text")
wstext-delete $((from+1)) $pos
eval "$wstext_posvar=$from"
wstext-upd true
}
wstext-del-line-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local begin=$(wstxtfun-line-start $pos "$text")
local to=$(wstxtfun-line-end $pos "$text")
if [[ $begin -eq $pos && $to -lt $text_end ]]; then
wstext-delete $((pos+1)) $((to+1))
else
wstext-delete $((pos+1)) $to
fi
eval "$wstext_posvar=$pos"
wstext-upd true
}
wstext-del-line() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local from=$(wstxtfun-line-start $pos "$text")
local to=$(wstxtfun-line-end $pos "$text")
if [[ $to -lt $text_end ]]; then
wstext-delete $((from+1)) $((to+1))
else
wstext-delete $((from+1)) $to
fi
eval "$wstext_posvar=$from"
wstext-upd true
}
# Delete sentence functions
wstext-del-sentence-left() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
# getting the positions of the current sentence
local sp=($(wstxtfun-sentence-pos $pos "$text"))
# return if outside of any sentence
if [[ $sp[1] -eq -1 ]]; then
unset wstext_pos
return
fi
# if at the beginning, delete previous sentence
if [[ $((pos+1)) -eq $sp[1] && $pos -gt 0 ]]; then
ws-debug DEL_SENTENCE_LEFT: Delete Previous Sentence
local from=$(wstxtfun-prev-sentence $pos "$text")
ws-debug text=\"$text\" from=$from pos=$pos text_end=$text_end
wstext-delete $((from+1)) $pos
eval "$wstext_posvar=$from"
wstext-upd
else
# if in the middle, delete the beginning of the sentence
wstext-delete $sp[1] $pos
eval "$wstext_posvar=$(($sp[1]-1))"
wstext-upd true
fi
}
# deletes from the current position till the stops
wstext-del-sentence-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local sp=($(wstxtfun-sentence-pos $pos "$text"))
if [[ ! $sp[1] -eq -1 ]]; then
wstext-delete $((pos+1)) $((sp[2]-1))
eval "$wstext_posvar=$pos"
wstext-upd true
else
eval "$wstext_posvar=$pos"
fi
}
wstext-del-sentence() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local sp=($(wstxtfun-sentence-pos $pos "$text"))
if [[ ! $sp[1] -eq -1 ]]; then
wstext-delete $sp[1] $((sp[3]-1))
eval "$wstext_posvar=$(($sp[1]-1))"
wstext-upd true
else
eval "$wstext_posvar=$pos"
fi
}
# Delete paragraph functions
wstext-del-paragraph-left() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local prevp=$(wstxtfun-prev-paragraph $pos "$text")
local from=$(wstxtfun-line-end $prevp "$text")
wstext-delete $((from+1)) $pos
eval "$wstext_posvar=$from"
wstext-upd true
}
wstext-del-paragraph-right() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local to=$(wstxtfun-next-paragraph $pos "$text")
wstext-delete $((pos+1)) $((to-1))
eval "$wstext_posvar=$pos"
wstext-upd true
}
wstext-del-paragraph() {
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
local text_end=${#text}
local from=$(wstxtfun-prev-paragraph $pos "$text")
# find the beginning of the empty line for $to
local next_paragraph=$(wstxtfun-next-paragraph $pos "$text")
# find the end of the line for the $to
local i=next_paragraph
while [[ ! "$text[i]" = $'\n' && $i -le $text_end ]]; do
i=$((i+1))
done
local to=$i
wstext-delete $((from+1)) $to
eval "$wstext_posvar=$from"
wstext-upd true
}
# marks insert/delete functions
wstext-marks-move-insert() {
local pos=$1
local len=$2
ws-debug MARKS_MOVE_INSERT: pos=$pos len=$len
ws-debug MARKS_MOVE_INSERT: wstext_marksvar=${(P)wstext_marksvar}
declare -A marks
marks=(${(@Pkv)wstext_marksvar})
for m_name m_pos in "${(@kv)marks}"; do
# ws-debug MARKS_MOVE_INSERT: m_name=$m_name m_pos=$m_pos
if [[ $m_pos -gt $pos ]]; then
# ws-debug MARKS_MOVE_INSERT: move mark $m_name from $m_pos to $((m_pos+len))
eval ${wstext_marksvar}"[$m_name]"=$((m_pos+len))
fi
done
}
wstext-marks-move-delete() {
local pos=$1
local len=$2
ws-debug MARKS_MOVE_DELETE: pos=$pos len=$len
declare -A marks
marks=(${(@Pkv)wstext_marksvar})
for m_name m_pos in "${(@kv)marks}"; do
ws-debug MARKS_MOVE_DELETE: m_name=$m_name m_pos=$m_pos
if [[ $m_pos -lt $pos ]]; then
continue
elif [[ $m_pos -lt $((pos+len)) ]]; then
ws-debug MARKS_MOVE_DELETE'[1]': move mark $m_name from $m_pos to $pos
eval ${wstext_marksvar}"[$m_name]"=$pos
else
ws-debug MARKS_MOVE_DELETE'[2]': move mark $m_name from $m_pos to $((m_pos-len))
eval ${wstext_marksvar}"[$m_name]"=$((m_pos-len))
fi
done
}
# Insert function
wstext-insert() {
local str="$1"
local pos=${(P)wstext_posvar}
local text="${(P)wstext_textvar}"
if [[ $pos -eq 0 ]]; then
ws-defvar $wstext_textvar "$str$text"
else
ws-defvar $wstext_textvar "$text[1,pos]$str$text[pos+1,${#text}]"
fi
wstext-marks-move-insert $pos ${#str}
eval "$wstext_posvar=$((pos+${#str}))"
wstext-upd true
}
wstext-delete() {
local from=$1
local to=$2
local text="${(P)wstext_textvar}"
local text_len=${#text}
wsblock-delupd $((from-1)) $to
ws-debug WSTEXT_DELETE: from=$from to=$to
wstext-marks-move-delete $((from-1)) $((to-from+1))
if [[ $((to-from)) -gt 1 ]]; then
ws_delbuf="$text[from, to]"
fi
if [[ $from -eq 0 ]]; then
if [[ $to -lt $((text_len)) ]]; then
ws-defvar $wstext_textvar "$text[to+1,text_len]"
fi
else
if [[ $to -eq $((text_len)) ]]; then
ws-defvar $wstext_textvar "$text[1,from-1]"
else
ws-defvar $wstext_textvar "$text[1,from-1]$text[to+1,text_len]"
fi
fi
}