-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlib-edit.muf
365 lines (315 loc) · 11.4 KB
/
lib-edit.muf
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
@program lib-edit
1 99999 d
1 i
( Stack Based String Range Editing Routines
start, end, pos, and dest are all with reference to the start of the range
that is towards the bottom of the stack. A 1 means the first item of the
range; the item deepest in the stack. offset is the number of stack items
between the top of the string range and the bottom parameter.
EDITsearch [ {rng} ... offset string start -- {rng} ... pos ]
Searches a range of strings for the first occurence of a substring. This
is case sensitive, and returns the line number of the first occurence
EDITreplace [ {rng} ... offset oldstr newstr start end -- {rng'} ... ]
Searches the range of strings for all occurences of a case sensitive
substring, and replaces them with new text.
EDITmove [ {rng} ... offset dest start end -- {rng'} ... ]
Moves text within a string range from one line to another location,
deleting the original.
EDITcopy [ {rng} ... offset dest start end -- {rng'} ... ]
Copies text within a string range from one line to another, inserting it
in the new location.
EDITlist [ {rng} ... offset nums? start end -- {rng} ... ]
Lists the given set of lines within a string range, with an int telling
it to prepending each line with a number and a colon. Ie:
"8: line eight."
EDITleft [ {rng} ... offset start end -- {rng'} ... ]
Left justify all the given lines within a string range.
EDITcenter [ {rng} ... offset cols start end -- {rng'} ... ]
Center justify all the given lines within a string range.
EDITright [ {rng} ... offset cols start end -- {rng'} ... ]
Right justify all the given lines within a string range.
EDITindent [ {rng} ... offset cols start end -- {rng'} ... ]
Indents all the given lines in a string range by COLS spaces. if COLS
is a negative integer, it undents by that many spaces. It will never
undent past left justification.
EDITfmt_rng [ {rng} ... offset cols start end -- {rng'} ... ]
Formats the given subrange in the string range to COLS columns. This
is similar to the UNIX fmt command, in that it splits long lines and
joins short lines. A line that contains only spaces is considered a
paragraph delimiter, and is not joined.
EDITjoin_rng [ {rng} ... offset start end -- {rng'} ... ]
Joins all the given lines in the string range together, and returns the
string range that results.
EDITshuffle [ {rng} -- {rng'} ]
Take a range of items on the stack and randomize their order.
EDITsort [ {rng} ascending? CaseSensitive? -- {rng'} ]
Alphabetically sorts strings with integers telling it whether to sort
in ascending or decending order, and if it should be case sensitive.
EDITjoin [ {rng} -- string ]
Join a range of strings on the stack into one string.
EDITdisplay [ {rng} -- ]
displays the range of strings on the stack to the user.
EDITsplit [ string splitchars rmargin wrapmargin -- {rng} ]
splits a string up into several lines in a range. The criterion
for where to split each line are as follows: It splits at the last
split character it can find between the rmargin and the wrapmargin.
If it cannot find one, then it splits at the rmargin.
EDITformat [ {rng} splitchars rmargin wrapmargin -- {rng'} ]
Takes a range and formats it similarly to the way that the UNIX fmt
command would, splitting long lines, and joining short ones.
)
$doccmd @list __PROG__=!@1-76
$include $lib/stackrng
: EDITforeach ( {str_rng} ... offset 'function data start end -- {str_rng'} )
( 'function must be addr of a [string data -- string] function)
5 pick 6 + pick dup 4 pick <
4 pick 4 pick > or if
pop pop pop pop pop pop exit
then
6 pick + 7 + 3 pick - dup 1 +
rotate 5 pick 7 pick execute
swap -1 * rotate
swap 1 + swap EDITforeach
;
: EDITsearch ( {rng} ... offset string start -- {rng} pos )
dup 4 pick 5 + pick > if pop pop pop 0 exit then
3 pick 5 + dup pick + over - pick 3 pick
instr if rot rot pop pop exit then
1 + EDITsearch
;
: EDITreplace ( {rng} ... offset oldstr newstr start end -- {rng'} )
over 6 pick 7 + pick > 3 pick 3 pick > or if
pop pop pop pop pop exit
then
5 pick 7 + dup pick + 3 pick - dup 1 + rotate
5 pick 7 pick subst swap -1 * rotate
swap 1 + swap EDITreplace
;
: EDITmove ( {rng} ... offset dest start end -- {rng'} )
3 pick over > if
rot over 4 pick - 1 + - rot rot
else
3 pick 3 pick >= if pop pop pop pop exit then
then
over - 1 + swap 4 pick 2 + rot rot sr-extractrng
( {rng'} ... offset dest {subrng} )
dup 3 + rotate over 3 + rotate
( {rng'} ... {rng2} offset dest )
sr-insertrng
;
: EDITcopy ( {rng} ... offset dest start end -- {rng'} )
over - 1 + swap 4 pick 2 + rot rot sr-copyrng
dup 3 + rotate over 3 + rotate
sr-insertrng
;
: EDITsort ( {rng} ascending? CaseSensitive? -- {rng'} )
if
if SORTTYPE_CASE_ASCEND
else SORTTYPE_CASE_DESCEND
then
else
if SORTTYPE_NOCASE_ASCEND
else SORTTYPE_NOCASE_DESCEND
then
then
var! sorttype
array_make sorttype @ array_sort array_vals
;
: EDITjoin ( {rng} -- string )
dup 2 < if pop exit then
rot striptail rot striplead
over dup strlen
dup if
1 - strcut pop
".!?" swap instr if " " else " " then
else
pop pop " "
then
swap strcat strcat swap
1 - EDITjoin
;
: EDITsplit-splitloop (string splitchars last -- string string)
over not if
swap pop
dup not if pop dup strlen then
strcut exit
then
swap 1 strcut rot rot 4 pick swap rinstr
over over < if swap then pop
EDITsplit-splitloop
;
: EDITsplit-split (string splitchars rmargin wrapmargin --
excess splitchars rmargin wrapmargin string)
4 rotate 3 pick strcut swap 3 pick strcut
(splitchars rmargin wrapmargin excess str wrap)
6 pick 0 EDITsplit-splitloop
rot rot strcat rot rot swap strcat
(splitchars rmargin wrapmargin str excess)
-5 rotate
;
: EDITsplit-loop ({rng} string splitchars rmargin wrapwargin -- {rng})
4 pick strlen 3 pick < if
pop pop pop
dup if swap 1 +
else pop
then exit
then
EDITsplit-split -6 rotate 5 rotate 1 + -5 rotate
EDITsplit-loop
;
: EDITsplit ( string splitchars rmargin wrapmargin -- {rng} )
0 -5 rotate EDITsplit-loop
;
: EDITformat-loop ( {rng} splitchars rmargin wrapmargin {rng2} -- {rng'} )
dup 5 + pick not if
dup 3 + dup rotate pop dup rotate pop
dup rotate pop dup rotate pop pop exit
then
dup 4 + 1 1 sr-extractrng pop
( {rng} splitchars rmargin wrapmargin {rng2} string )
dup striplead if
over 6 + dup pick swap dup pick swap 1 - pick
EDITsplit dup 2 + rotate + 1 - swap
( {rng} splitchars rmargin wrapmargin {rng2} string )
over 6 + pick dup if
3 pick + 6 + pick
dup striplead not
else pop "" 1
then
( {rng} splitchars rmargin wrapmargin {rng2} string nocat? )
if pop swap 1 +
else 2 EDITjoin over 6 + pick 3 pick + 5 + put
then
( {rng} splitchars rmargin wrapmargin {rng2} )
else
pop " " swap 1 +
then
EDITformat-loop
;
: EDITformat ( {rng} splitchars rmargin wrapmargin -- {rng'} )
0 EDITformat-loop
;
: EDITfmt_rng ( {str_rng} ... offset cols start end -- {str_rng'} ... )
over - 1 + over swap
({rng} ... off cols start start cnt )
5 pick 3 + swap rot sr-extractrng
({rng'} ... off cols start {srng})
"- " over 4 + rotate dup 20 - EDITformat
({rng'} ... off start {srng})
dup 3 + rotate over 3 + rotate
sr-insertrng
;
: EDITshuffle-innerloop ( {rng} shuffles loop -- {rng'} )
dup not if pop exit then
4 rotate 4 pick ( {rng} shuffles loop item cnt )
random 256 / swap % ( {rng} shuffles loop item rnd )
4 + -1 * rotate ( {rng} shuffles loop )
1 - EDITshuffle-innerloop
;
: EDITshuffle-outerloop ( {rng} shuffles -- {rng'} )
dup not if pop exit then
over EDITshuffle-innerloop
1 - EDITshuffle-outerloop
;
: EDITshuffle ( {rng} -- {rng'} )
8 EDITshuffle-outerloop
;
: EDITlist ( {rng} ... offset nums? start end -- {rng} ... )
over over >
3 pick 6 pick 7 + pick > or if
pop pop pop pop exit
then
4 pick 6 + dup pick + 3 pick - pick
4 pick if
" " 4 pick intostr strcat
dup strlen 3 - strcut
swap pop ": " strcat
swap strcat
then
dup not if pop " " then
me @ swap notify
swap 1 + swap EDITlist
;
: EDITdisplay ( {str_rng} -- )
dup if
dup 1 + rotate me @ swap notify
1 - EDITdisplay exit
then pop
;
: EDITleft-func (string null -- string )
pop striplead
;
: EDITleft ( {strrng} ... offset start end -- {strrng'} ... )
'EDITleft-func "" -4 rotate -4 rotate EDITforeach
;
: EDITcenter-func (string cols -- string )
swap strip dup strlen
dup 4 pick >= if
pop swap pop exit
then
rot swap - 2 /
" "
dup strcat dup strcat
swap strcut pop swap strcat
;
: EDITcenter ( {strrng} ... offset cols start end -- {strrng'} ... )
'EDITcenter-func -4 rotate EDITforeach
;
: EDITright-func (string cols -- string )
swap strip dup strlen
dup 4 pick >= if
pop swap pop exit
then
rot swap -
" "
dup strcat dup strcat
swap strcut pop swap strcat
;
: EDITright ( {strrng} ... offset cols start end -- {strrng'} ... )
'EDITright-func -4 rotate EDITforeach
;
: EDITindent-func (str cols -- str)
swap dup strlen swap striplead
dup strlen rot swap - rot +
dup 1 < if pop exit then
" "
dup strcat dup strcat
swap strcut pop swap strcat
;
: EDITindent ( {str_rng} ... offset cols start end -- {str_rng'} ... )
'EDITindent-func -4 rotate EDITforeach
;
: EDITjoin_rng ( {str_rng} ... offset start end -- {str_rng'} ... )
over - 1 + over
({rng} ... off start cnt start )
4 pick 2 + rot rot sr-extractrng
({rng'} ... off start {srng})
EDITjoin 1 4 rotate 4 rotate
sr-insertrng
;
public EDITcenter $libdef EDITcenter
public EDITcopy $libdef EDITcopy
public EDITdisplay $libdef EDITdisplay
public EDITformat $libdef EDITformat
public EDITfmt_rng $libdef EDITfmt_rng
public EDITindent $libdef EDITindent
public EDITjoin $libdef EDITjoin
public EDITjoin_rng $libdef EDITjoin_rng
public EDITleft $libdef EDITleft
public EDITlist $libdef EDITlist
public EDITmove $libdef EDITmove
public EDITreplace $libdef EDITreplace
public EDITright $libdef EDITright
public EDITsearch $libdef EDITsearch
public EDITshuffle $libdef EDITshuffle
public EDITsort $libdef EDITsort
public EDITsplit $libdef EDITsplit
.
c
q
@register lib-edit=lib/edit
@register #me lib-edit=tmp/prog1
@set $tmp/prog1=3
@set $tmp/prog1=L
@set $tmp/prog1=V
@register #me =tmp