trebol-dev 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ Trebol [
2
+ title: "Undo and redo: the texts a file had, before and after the one it has now"
3
+ exports: [new-history remember undo-text redo-text can-undo? can-redo?]
4
+ ]
5
+
6
+ ; Every change gives a file new text, so going back is taking the text it had before. A history keeps,
7
+ ; for one file, the texts before the one now (past, the last is the newest) and those undone (future,
8
+ ; the last is the next to come back). A new change after an undo forgets what was undone.
9
+
10
+ history-size: 200 ; the oldest texts go past this many
11
+
12
+ new-history: does [make object! [past: copy [] future: copy []]]
13
+
14
+ remember: function [
15
+ "Keeps the text a file had before a change"
16
+ h [object!] was [string!] now [string!]
17
+ ] [
18
+ if was == now [exit]
19
+ append h/past was
20
+ if history-size < length? h/past [remove h/past]
21
+ clear h/future
22
+ ]
23
+
24
+ undo-text: function [
25
+ "The text before the one now, which is kept to come back by redo; none when there is none"
26
+ h [object!] now [string!]
27
+ ] [
28
+ if empty? h/past [return none]
29
+ append h/future now
30
+ take/last h/past
31
+ ]
32
+
33
+ redo-text: function [
34
+ "The text undone last, the one now kept to go back to again; none when there is none"
35
+ h [object!] now [string!]
36
+ ] [
37
+ if empty? h/future [return none]
38
+ append h/past now
39
+ take/last h/future
40
+ ]
41
+
42
+ can-undo?: function [h] [all [h not empty? h/past]]
43
+ can-redo?: function [h] [all [h not empty? h/future]]
package/app/model.trb ADDED
@@ -0,0 +1,611 @@
1
+ Trebol [
2
+ title: "The design of a file: its text, its values and faces, and the edits that change the text"
3
+ exports: [
4
+ design-of spot-of span-of face-range face-span face-code faces-of
5
+ splice cut replace-value remove-face insert-after insert-into move-face
6
+ set-face-text set-face-name face-label style-word toggle-flag
7
+ layout-of head-end block-span indent-at after-line shown-faces
8
+ pane-of own-block written-range values-at inside? set-screen screens-data add-screen piece? set-piece-label add-piece
9
+ set-face-label set-face-global
10
+ ]
11
+ ]
12
+
13
+ ; The text is the truth. A design is what can be read from it: its values (load/all), where each
14
+ ; sits (load-spans) and the faces of each layout in it, laid out without running anything
15
+ ; (ui-sketch). Every edit gives new text that changes only what it must, so comments, spacing and
16
+ ; order stay as they were written; the designer reads the new text again.
17
+
18
+ spot-move: function [
19
+ "Moves the cursor of a walk over the text to a character index, counting lines and columns as loaded-at does"
20
+ st [object!] text [string!] to [integer!]
21
+ ] [
22
+ while [st/i < to] [
23
+ c: pick text st/i
24
+ either c = newline [st/line: st/line + 1 st/col: 1] [
25
+ ; columns count UTF-16 units, as the lexer and editors do
26
+ st/col: st/col + either c > #"^(FFFF)" [2] [1]
27
+ ]
28
+ st/i: st/i + 1
29
+ ]
30
+ ]
31
+
32
+ design-of: function [
33
+ "What the designer reads from a file's text: values, where each sits, and the faces of its layouts"
34
+ text [string!]
35
+ ] [
36
+ values: load/all text
37
+ spans: load-spans text
38
+ ; load drops a script header; its two spans come first
39
+ header: none
40
+ if (length? spans) > length? values [
41
+ header: copy/part spans 2
42
+ spans: skip spans 2
43
+ ]
44
+ d: make object! [
45
+ text: none values: none header: none
46
+ spots: none ; "line:col" -> [span block index]
47
+ blocks: none ; [block spans own ...]: each loaded block, its values' spans and its own (none at the top)
48
+ layouts: none ; block of objects: kind block view problems
49
+ defs: none ; style -> style-def of the styles the file redefines
50
+ own: none ; the styles the file defines or redefines, that a layout writes
51
+ screens: none ; [screens-face its-screens ...]: all of them, whichever the preview shows
52
+ ]
53
+ d/text: text
54
+ d/values: values
55
+ d/header: header
56
+ d/spots: make map! []
57
+ d/blocks: copy []
58
+ st: make object! [i: 1 line: 1 col: 1]
59
+ ; spans in walk order are in text order: a block's own span comes before its values'
60
+ walk: function [vals sps own-span] [
61
+ append d/blocks reduce [vals sps own-span]
62
+ repeat n length? vals [
63
+ sp: pick sps n
64
+ own: either block? sp [first sp] [sp]
65
+ spot-move st text own/x
66
+ put d/spots rejoin [st/line ":" st/col] reduce [own vals n]
67
+ if block? sp [walk pick vals n next sp own]
68
+ ]
69
+ ]
70
+ walk values spans none
71
+ d/layouts: copy []
72
+ find-layouts: function [code] [
73
+ parse code [any [
74
+ p: ['view | 'layout | path! if (find [view layout] first first p)]
75
+ opt ['compose | path! if ('compose = first first next p)]
76
+ set b block! (
77
+ r: ui-sketch b
78
+ k: either path? first p [first first p] [first p]
79
+ append d/layouts make object! [kind: k block: b view: r/1 problems: r/2]
80
+ find-layouts b
81
+ )
82
+ ; stylize [view: view [...]] defines a style: its blocks are no layouts
83
+ | 'stylize block!
84
+ | set b [block! | paren!] (find-layouts b)
85
+ | skip
86
+ ]]
87
+ ]
88
+ ; the styles and rules the file defines, as check reads them, then its layouts with them
89
+ before: reduce [copy ui-style-defs copy styles]
90
+ run-own values 'rule
91
+ run-own values 'stylize
92
+ find-layouts values
93
+ ; every screen of a screens face: the preview shows one of them, the structure all
94
+ d/screens: copy []
95
+ keep-screens: function [fs] [foreach f fs [
96
+ if f/style = 'screens [append d/screens reduce [f copy any [f/pane []]]]
97
+ if block? f/pane [keep-screens f/pane]
98
+ ]]
99
+ foreach l d/layouts [if l/view [keep-screens l/view/pane]]
100
+ ; what the file defines anew stays known; a style it redefines is its own, kept in the design,
101
+ ; and the one before comes back, so the designer's own screen never changes (rule cannot
102
+ ; redefine one of Trebol's)
103
+ d/defs: make map! []
104
+ d/own: collect [foreach [k v] styles [unless same? v select before/2 k [keep k]]]
105
+ foreach [m old] reduce [ui-style-defs before/1 styles before/2] [
106
+ foreach k keys-of old [
107
+ unless same? select m k select old k [
108
+ if same? m ui-style-defs [put d/defs k select m k]
109
+ put m k select old k
110
+ ]
111
+ ]
112
+ ]
113
+ d
114
+ ]
115
+
116
+ ; Runs the rule or stylize blocks of a file's code, and those inside its blocks; one that fails is left out.
117
+ run-own: function [code [block! paren!] kind [word!] /local b n m] [
118
+ parse code [any [
119
+ 'stylize set b block! (if kind = 'stylize [attempt [stylize copy/deep b]])
120
+ | 'rule set n lit-word! set m [string! | map! | block!] set b block! (if kind = 'rule [attempt [rule to word! n m copy/deep b]])
121
+ | set b [block! | paren!] (run-own b kind)
122
+ | skip
123
+ ]]
124
+ ]
125
+
126
+ spot-of: function [
127
+ "Where a value sits, from its place (loaded-at, a face's place or written): [span block index], or none"
128
+ d [object!] place [string! none!]
129
+ ] [
130
+ unless place [return none]
131
+ ; a place may start with a file name: the line and column are the last two parts
132
+ parts: split place #":"
133
+ n: length? parts
134
+ if n < 2 [return none]
135
+ select d/spots rejoin [pick parts n - 1 ":" last parts]
136
+ ]
137
+
138
+ spans-of: function [
139
+ "The spans of the values of a loaded block of the design"
140
+ d [object!] b [block! paren!]
141
+ ] [
142
+ foreach [blk sps own] d/blocks [if same? head blk head b [return sps]]
143
+ none
144
+ ]
145
+
146
+ span-of: function [
147
+ "Where the value at an index of a loaded block sits in the text: index x length"
148
+ d [object!] b [block! paren!] n [integer!]
149
+ ] [
150
+ sp: pick spans-of d b n
151
+ either block? sp [first sp] [sp]
152
+ ]
153
+
154
+ layout-of: function ["The layout whose view a face is, or none" d [object!] f [object!]] [
155
+ foreach l d/layouts [if same? l/view f [return l]]
156
+ none
157
+ ]
158
+
159
+ head-end: function [
160
+ "The index of the last value before a layout's first face: its view's own words; 0 when there are none"
161
+ b [block!]
162
+ ] [
163
+ n: 1
164
+ while [n <= length? b] [
165
+ v: pick b n
166
+ if any [set-word? :v paren? :v 'global = :v all [word? :v select styles v]] [break]
167
+ n: n + 1
168
+ ]
169
+ n - 1
170
+ ]
171
+
172
+ written-range: function [
173
+ "Where a face was written: [block first last], indexes in the loaded block; none if made at run time"
174
+ d [object!] f [object!]
175
+ ] [
176
+ unless f/written [return none]
177
+ a: spot-of d f/written/1
178
+ z: spot-of d f/written/2
179
+ unless all [a z same? a/2 z/2] [return none]
180
+ reduce [a/2 a/3 z/3]
181
+ ]
182
+
183
+ own-block: function [
184
+ "The block whose first words are a face's own: a view's, or a screen's (home ^"Home^" [...]); none for others"
185
+ d [object!] f [object!]
186
+ ] [
187
+ if l: layout-of d f [return l/block]
188
+ if all [find [screen tab step] f/style r: written-range d f] [
189
+ b: pick r/1 r/3
190
+ if block? b [return b]
191
+ ]
192
+ none
193
+ ]
194
+
195
+ piece?: func ["Whether a face is a piece of the face it is in, written there with its label: a tab or a step" f] [
196
+ to logic! find [tab step] f/style
197
+ ]
198
+
199
+ set-piece-label: function ["The text with a tab's or a step's label changed" d [object!] f [object!] new [string!]] [
200
+ r: written-range d f
201
+ i: either word? pick r/1 r/2 [r/2 + 1] [r/2]
202
+ replace-value d r/1 i new
203
+ ]
204
+
205
+ add-piece: function [
206
+ "The text with a new, empty tab or step last in its tabs or steps: f is that face or one of its pieces"
207
+ d [object!] f [object!] kind [word!]
208
+ ] [
209
+ b: none
210
+ either piece? f [b: first written-range d f] [
211
+ r: face-range d f
212
+ foreach v copy/part at r/1 r/2 r/3 - r/2 + 1 [if block? v [b: v]]
213
+ ]
214
+ unless b [return none]
215
+ n: 1 + to integer! (length? b) / 2
216
+ label: rejoin [either kind = 'tab ["Tab "] ["Step "] n]
217
+ ; steps a machine leads name the state of each
218
+ either word? first b [
219
+ insert-into d b rejoin ["state-" n " " mold label " []"]
220
+ ] [insert-into d b rejoin [mold label " []"]]
221
+ ]
222
+
223
+ face-range: function [
224
+ "The values a face was written with: [block first last], indexes in the loaded block; none if made at run time"
225
+ d [object!] f [object!]
226
+ ] [
227
+ ; a view or a screen: the words before its first face; a tab or a step: none
228
+ if b: own-block d f [return reduce [b 1 either piece? f [0] [head-end b]]]
229
+ unless f/written [return none]
230
+ a: spot-of d f/written/1
231
+ z: spot-of d f/written/2
232
+ unless all [a z same? a/2 z/2] [return none]
233
+ reduce [a/2 a/3 z/3]
234
+ ]
235
+
236
+ face-span: function [
237
+ "Where the text of a face sits: index x length"
238
+ d [object!] f [object!]
239
+ ] [
240
+ if l: layout-of d f [return block-span d l/block]
241
+ if all [find [screen tab step] f/style r: written-range d f] [return values-at d r/1 r/2 r/3]
242
+ unless r: face-range d f [return none]
243
+ a: span-of d r/1 r/2
244
+ z: span-of d r/1 r/3
245
+ as-pair a/x z/x + z/y - a/x
246
+ ]
247
+
248
+ values-at: function ["Where the values from one index to another of a loaded block sit" d [object!] b [block!] from [integer!] to [integer!]] [
249
+ a: span-of d b from
250
+ z: span-of d b to
251
+ as-pair a/x z/x + z/y - a/x
252
+ ]
253
+
254
+ block-span: function ["Where a loaded block sits, its brackets too" d [object!] b [block!]] [
255
+ foreach [blk sps own] d/blocks [if same? head blk head b [return own]]
256
+ none
257
+ ]
258
+
259
+ face-code: function ["The text a face was written with" d [object!] f [object!]] [
260
+ s: face-span d f
261
+ copy/part at d/text s/x s/y
262
+ ]
263
+
264
+ pane-of: function ["The faces a face holds: every screen of a screens face" d [object!] f [object!]] [
265
+ any [select/same d/screens f all [block? f/pane f/pane]]
266
+ ]
267
+
268
+ faces-of: function [
269
+ "Every face of the design's layouts that was written in the text, in order, nested ones after their parent"
270
+ d [object!]
271
+ ] [
272
+ out: copy []
273
+ walk: function [fs] [
274
+ foreach f fs [
275
+ if face-range d f [append out f]
276
+ if fs: pane-of d f [walk fs]
277
+ ]
278
+ ]
279
+ foreach l d/layouts [if l/view [walk l/view/pane]]
280
+ out
281
+ ]
282
+
283
+ shown-faces: function [
284
+ "The faces the structure shows: each layout's view, then the faces written in it, as faces-of gives them"
285
+ d [object!]
286
+ ] [
287
+ out: copy []
288
+ fs: faces-of d
289
+ foreach l d/layouts [if l/view [
290
+ append out l/view
291
+ foreach f fs [if inside? d f l/view [append out f]]
292
+ ]]
293
+ out
294
+ ]
295
+
296
+ inside?: function [d [object!] f [object!] v [object!]] [
297
+ foreach g any [pane-of d v []] [if any [same? g f inside? d f g] [return true]]
298
+ false
299
+ ]
300
+
301
+ ; --- edits: each takes a design and gives the new text --------------------------------------
302
+
303
+ splice: function [
304
+ "The text with length characters at an index replaced by new text"
305
+ text [string!] at-index [integer!] len [integer!] new [string!]
306
+ ] [
307
+ t: copy text
308
+ change/part at t at-index new len
309
+ t
310
+ ]
311
+
312
+ line-start: function ["The index where the line holding an index starts" text [string!] n [integer!]] [
313
+ while [all [n > 1 newline <> pick text n - 1]] [n: n - 1]
314
+ n
315
+ ]
316
+
317
+ indent-at: function ["The spaces and tabs that start the line holding an index" text [string!] n [integer!]] [
318
+ s: line-start text n
319
+ e: s
320
+ while [all [e <= length? text find " ^-" pick text e]] [e: e + 1]
321
+ copy/part at text s e - s
322
+ ]
323
+
324
+ blank?: function ["Whether text holds only spaces and tabs" s [string!]] [
325
+ parse s [any [#" " | #"^-"]]
326
+ ]
327
+
328
+ after-line: function [
329
+ "Where to put a new line after a value that ends at an index: the end of its line when only spaces or a comment follow it"
330
+ text [string!] n [integer!]
331
+ ] [
332
+ rest: find at text n newline
333
+ tail-text: copy/part at text n either rest [(index? rest) - n] [length? at text n]
334
+ either any [blank? tail-text #";" = first trim/head copy tail-text] [n + length? tail-text] [n]
335
+ ]
336
+
337
+ more-on-line?: function ["Whether more than spaces or a comment follows an index on its line" text [string!] n [integer!]] [
338
+ rest: find at text n newline
339
+ tail-text: copy/part at text n either rest [(index? rest) - n] [length? at text n]
340
+ not any [blank? tail-text #";" = first trim/head copy tail-text]
341
+ ]
342
+
343
+ replace-value: function [
344
+ "The text with the value at an index of a loaded block written anew"
345
+ d [object!] b [block! paren!] n [integer!] new [any-type!]
346
+ ] [
347
+ s: span-of d b n
348
+ splice d/text s/x s/y mold :new
349
+ ]
350
+
351
+ cut: function [
352
+ "The text without the characters of a span, and its line too when nothing else is on it"
353
+ text [string!] s [pair!]
354
+ ] [
355
+ from: s/x
356
+ stop: s/x + s/y
357
+ before: copy/part at text line-start text from from - line-start text from
358
+ rest: find/tail at text stop newline
359
+ after: copy/part at text stop either rest [(index? rest) - 1 - stop] [length? at text stop]
360
+ ; trailing comments go with the value when it is alone on its line
361
+ if all [blank? before any [blank? after #";" = first trim/head copy after]] [
362
+ from: line-start text from
363
+ stop: either rest [index? rest] [1 + length? text]
364
+ ; the last line of the text has no newline after it: take the one before
365
+ if all [not rest from > 1] [from: from - 1]
366
+ return splice text from stop - from ""
367
+ ]
368
+ ; otherwise the spaces before it go too, or after it when it opens its block: [a b] -> [b]
369
+ either all [from > 1 find " ^-" pick text from - 1] [
370
+ while [all [from > 1 find " ^-" pick text from - 1]] [from: from - 1]
371
+ ] [
372
+ while [all [stop <= length? text find " ^-" pick text stop]] [stop: stop + 1]
373
+ ]
374
+ splice text from stop - from ""
375
+ ]
376
+
377
+ remove-face: function ["The text without a face" d [object!] f [object!]] [
378
+ cut d/text face-span d f
379
+ ]
380
+
381
+ ; code of several lines, each line after the first indented as the place it goes
382
+ indented: function [code [string!] indent [string!]] [
383
+ replace/all copy code newline rejoin [newline indent]
384
+ ]
385
+
386
+ insert-after: function [
387
+ "The text with new code on its own line after a face, indented as it is"
388
+ d [object!] f [object!] code [string!]
389
+ ] [
390
+ s: face-span d f
391
+ indent: indent-at d/text s/x
392
+ n: after-line d/text s/x + s/y
393
+ ; more is written after it on its line, as in row [a b]: beside it, as the line is written
394
+ if all [more-on-line? d/text s/x + s/y not find code newline] [return splice d/text n 0 rejoin [" " code]]
395
+ splice d/text n 0 rejoin [newline indent indented code indent]
396
+ ]
397
+
398
+ insert-into: function [
399
+ "The text with new code as the last value of a loaded block (a layout or a panel's block)"
400
+ d [object!] b [block!] code [string!]
401
+ ] [
402
+ owner: none
403
+ foreach [blk sps own] d/blocks [if same? head blk head b [owner: own]]
404
+ unless owner [return none]
405
+ close: owner/x + owner/y - 1 ; the ]
406
+ if empty? b [
407
+ ; [] becomes a block of lines: [, the code a step in, ] under the line it opens on
408
+ outer: indent-at d/text owner/x
409
+ inner: rejoin [outer " "]
410
+ return splice d/text owner/x + 1 close - owner/x - 1 rejoin [newline inner indented code inner newline outer]
411
+ ]
412
+ last-span: span-of d b length? b
413
+ indent: indent-at d/text last-span/x
414
+ n: after-line d/text last-span/x + last-span/y
415
+ ; a block on one line, [field "a"]: the new code goes beside the last value; code of more lines than
416
+ ; one opens the block onto lines, its ] under the line it opens on
417
+ if more-on-line? d/text last-span/x + last-span/y [
418
+ unless find code newline [return splice d/text n 0 rejoin [" " code]]
419
+ outer: indent-at d/text owner/x
420
+ inner: rejoin [outer " "]
421
+ return splice d/text n 0 rejoin [newline inner indented code inner newline outer]
422
+ ]
423
+ splice d/text n 0 rejoin [newline indent indented code indent]
424
+ ]
425
+
426
+ move-face: function [
427
+ "The text with a face moved after another face, or before it"
428
+ d [object!] f [object!] target [object!]
429
+ /before "Before the other face"
430
+ ] [
431
+ s: face-span d f
432
+ ; a comment after the face on its line goes with it
433
+ code: copy/part at d/text s/x (after-line d/text s/x + s/y) - s/x
434
+ t: face-span d target
435
+ if s = t [return d/text]
436
+ indent: indent-at d/text t/x
437
+ either before [
438
+ at-index: t/x
439
+ piece: rejoin [code newline indent]
440
+ ] [
441
+ at-index: after-line d/text t/x + t/y
442
+ piece: rejoin [newline indent code]
443
+ ]
444
+ ; the later place first, so the earlier one stays where it was
445
+ either at-index > s/x [
446
+ cut splice d/text at-index 0 piece s
447
+ ] [
448
+ splice cut d/text s at-index 0 piece
449
+ ]
450
+ ]
451
+
452
+ text-index: function [
453
+ "The index in its block of the string a face shows, or none; the first string after its style and name"
454
+ d [object!] f [object!]
455
+ ] [
456
+ r: face-range d f
457
+ b: r/1
458
+ n: r/2
459
+ while [n <= r/3] [
460
+ v: pick b n
461
+ if string? :v [return n]
462
+ ; the action block or anything after it is not the face's text
463
+ if block? :v [return none]
464
+ n: n + 1
465
+ ]
466
+ none
467
+ ]
468
+
469
+ style-index: function [
470
+ "The index in its block of the word a face was written with: after global and its label"
471
+ d [object!] f [object!]
472
+ ] [
473
+ r: face-range d f
474
+ n: r/2
475
+ if 'global = pick r/1 n [n: n + 1]
476
+ if set-word? pick r/1 n [n: n + 1]
477
+ n
478
+ ]
479
+
480
+ style-word: function ["The word a face was written with: submit for a submit button" d [object!] f [object!]] [
481
+ if layout-of d f [return 'view]
482
+ if own-block d f [return f/style]
483
+ r: face-range d f
484
+ pick r/1 style-index d f
485
+ ]
486
+
487
+ set-face-text: function [
488
+ "The text with the string a face shows changed, or added after its style and name"
489
+ d [object!] f [object!] new [string!]
490
+ ] [
491
+ if n: text-index d f [return replace-value d first face-range d f n new]
492
+ r: face-range d f
493
+ n: style-index d f
494
+ if all [f/name n < r/3 f/name = pick r/1 n + 1] [n: n + 1]
495
+ s: span-of d r/1 n
496
+ splice d/text s/x + s/y 0 rejoin [" " mold new]
497
+ ]
498
+
499
+ toggle-flag: function [
500
+ "The text with a flag word of a face taken out if it has it, or put after its text"
501
+ d [object!] f [object!] flag [word!]
502
+ ] [
503
+ r: face-range d f
504
+ n: r/2
505
+ while [n <= r/3] [
506
+ if all [word? pick r/1 n flag = pick r/1 n n > style-index d f] [return cut d/text span-of d r/1 n]
507
+ n: n + 1
508
+ ]
509
+ n: any [text-index d f style-index d f]
510
+ s: span-of d r/1 n
511
+ splice d/text s/x + s/y 0 rejoin [" " flag]
512
+ ]
513
+
514
+ face-label: function ["The word a face is set to, sign-up: panel, or none" d [object!] f [object!]] [
515
+ if own-block d f [return none]
516
+ r: face-range d f
517
+ v: pick r/1 r/2
518
+ if all [word? :v 'global = v] [v: pick r/1 r/2 + 1]
519
+ either set-word? :v [to word! v] [none]
520
+ ]
521
+
522
+ set-face-label: function [
523
+ "The text with the word a face is set to (who: field) changed, added before its style, or, none, taken out with global"
524
+ d [object!] f [object!] new [word! none!]
525
+ ] [
526
+ r: face-range d f
527
+ n: style-index d f
528
+ had: either set-word? pick r/1 n - 1 [n - 1] [none]
529
+ case [
530
+ all [had new] [replace-value d r/1 had to set-word! new]
531
+ had [
532
+ a: span-of d r/1 r/2 z: span-of d r/1 had
533
+ s: as-pair a/x z/x + z/y - a/x
534
+ ; and the space after it
535
+ if #" " = pick d/text s/x + s/y [s/y: s/y + 1]
536
+ splice d/text s/x s/y ""
537
+ ]
538
+ new [s: span-of d r/1 n splice d/text s/x 0 rejoin [new ": "]]
539
+ true [d/text]
540
+ ]
541
+ ]
542
+
543
+ set-face-global: function [
544
+ "The text with a face's word set globally (global who: field), or locally"
545
+ d [object!] f [object!] on [logic!]
546
+ ] [
547
+ r: face-range d f
548
+ was: 'global = pick r/1 r/2
549
+ case [
550
+ all [on not was] [s: span-of d r/1 r/2 splice d/text s/x 0 "global "]
551
+ all [was not on] [
552
+ s: span-of d r/1 r/2
553
+ if #" " = pick d/text s/x + s/y [s/y: s/y + 1]
554
+ splice d/text s/x s/y ""
555
+ ]
556
+ true [d/text]
557
+ ]
558
+ ]
559
+
560
+ set-screen: function [
561
+ "The text with a screen's name, title or arguments (none takes them out) changed: home ^"Home^" [id] [...]"
562
+ d [object!] f [object!] what [word!] new
563
+ ] [
564
+ r: written-range d f
565
+ b: r/1 i: r/2
566
+ args?: r/3 = (i + 3)
567
+ switch what [
568
+ name [replace-value d b i new]
569
+ title [replace-value d b i + 1 new]
570
+ args [
571
+ case [
572
+ all [args? any [none? new empty? new]] [
573
+ s: span-of d b i + 2
574
+ if #" " = pick d/text s/x + s/y [s/y: s/y + 1]
575
+ splice d/text s/x s/y ""
576
+ ]
577
+ args? [replace-value d b i + 2 new]
578
+ all [new not empty? new] [s: span-of d b r/3 splice d/text s/x 0 rejoin [mold new " "]]
579
+ true [d/text]
580
+ ]
581
+ ]
582
+ ]
583
+ ]
584
+
585
+ screens-data: function ["The block of a screens face's screens, or of the screens a screen is among" d [object!] f [object!]] [
586
+ if f/style = 'screen [return first written-range d f]
587
+ r: face-range d f
588
+ foreach v copy/part at r/1 r/2 r/3 - r/2 + 1 [if block? v [return v]]
589
+ none
590
+ ]
591
+
592
+ add-screen: function ["The text with a new, empty screen last among those of a screens face or screen" d [object!] f [object!]] [
593
+ unless b: screens-data d f [return none]
594
+ n: 1
595
+ while [find b to word! rejoin ["screen-" n]] [n: n + 1]
596
+ insert-into d b rejoin ["screen-" n " " mold rejoin ["Screen " n] " []"]
597
+ ]
598
+
599
+ set-face-name: function [
600
+ "The text with the name of a face's value changed, or added after its style; none takes it out"
601
+ d [object!] f [object!] new [word! none!]
602
+ ] [
603
+ r: face-range d f
604
+ n: style-index d f
605
+ if all [f/name n < r/3 f/name = pick r/1 n + 1] [
606
+ return either new [replace-value d r/1 n + 1 new] [cut d/text span-of d r/1 n + 1]
607
+ ]
608
+ unless new [return d/text]
609
+ s: span-of d r/1 n
610
+ splice d/text s/x + s/y 0 rejoin [" " new]
611
+ ]
@@ -0,0 +1,21 @@
1
+ Trebol [
2
+ title: "A file to move faces in: boxes, one of them empty"
3
+ exports: [move-sample face]
4
+ ]
5
+
6
+ import %model.trb
7
+
8
+ move-sample: {view [
9
+ heading "Join us"
10
+ column [
11
+ field name "Name" ; theirs
12
+ field email "Email"
13
+ ]
14
+ row [
15
+ button "Save"
16
+ ]
17
+ panel "Empty" []
18
+ ]}
19
+
20
+ ; the faces of the structure: 1 view, 2 heading, 3 column, 4 name, 5 email, 6 row, 7 Save, 8 panel
21
+ face: function [d n] [pick shown-faces d n]