~rabbits/left

ref: 1e5c083349fcc8c1a242b140c563556ae0064762 left/src/main.tal -rw-r--r-- 24.0 KiB
1e5c0833neauoire Minor optimizations 2 months ago
                                                                                
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
( Left
	
	mouse1 - select
	mouse2 - find
	alt+mouse2 - find from top
	ctrl+mouse2 - cut and find
	backspace/delete - erase
	escape - deselect
	arrow - move
	ctrl+arrow - jump
	alt+arrow - hop
	shift+arrow - scale selection
	ctrl+h - toggle syntax highlight

	ctrl+n - new     ctrl+s - save
	ctrl+r - rename  ctrl+o - open
	ctrl+c - copy    ctrl+v - paste
	ctrl+x - cut     ctrl+a - select all )

%+  { ADD }  %-  { SUB }  %*  { MUL }  %/  { DIV }
%<  { LTH }  %>  { GTH }  %=  { EQU }  %!  { NEQ }
%++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 }
%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }

%10// { #04 SFT2 }

%DEBUG  { ;print-hex JSR2 #0a .Console/write DEO }
%DEBUG2 { SWP ;print-hex JSR2 ;print-hex JSR2 #0a .Console/write DEO }

%TOGGLE { DUP LDZ #00 = SWP STZ }
%TOS { #00 SWP }
%INCR { SWP #01 + SWP }
%INCR2 { SWP2 #0001 ++ SWP2 }
%DECR2 { SWP2 #0001 -- SWP2 }
%RTN { JMP2r }
%GTS2 { #8000 ++ SWP2 #8000 ++ << }

%SELECTION-LENGTH { .selection/to LDZ2 .selection/from LDZ2 -- }
%SET-STATE { #01 .state/changed STZ ;draw-state JSR2 }
%RESET-STATE { #00 .state/changed STZ ;draw-state JSR2 }
%RELEASE-MOUSE { #00 .Mouse/state DEO }
%RESET-SELECTION { .selection/from LDZ2 ;select-addr JSR2 }
%ERASE-SELECTION { .selection/from LDZ2 SELECTION-LENGTH STH2k ++ EOF STH2r ;shiftl JSR2 }

%EOF { ;document/body ;document/length LDA2 ++ }
%SPACE-AVAILABLE { #fff0 ;document/length LDA2 ;document/body ++ -- }

%IS-SPACER   { #21 < } ( char -- flag )
%IS-LABEL    { STHk #40 = STHr #26 = #0000 !! } ( char -- flag )
%NOT-LINEBREAK { STHk #0d = STHr #0a = #0000 == } ( char -- flag )

( devices )

|00 @System     &vector $2 &pad $6 &r $2 &g $2 &b $2 
|10 @Console    &vector $2 &read $1 &pad    $5 &write $1 
|20 @Screen     &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &color $1 
|80 @Controller &vector $2 &button $1 &key $1 
|90 @Mouse      &vector $2 &x $2 &y $2 &state $1 &wheel $1 
|a0 @File       &vector $2 &success $2 &offset $2 &pad $2 &name $2 &length $2 &load $2 &save $2 

( variables )

|0000

@state
	&timer $1
	&trap $1
	&blink $1
	&changed $1
@path 
	&length $1
	&name $20
@selection 
	&from $2 &to $2
@cursor
	&x $2 &y $2
	&last $1
@textarea
	&x0 $2 &y0 $2
	&x1 $2 &y1 $2
	&lines $2
	&font $1
	&highlight $1
@scrollbar
	&x0 $2 &y0 $2
	&x1 $2 &y1 $2
@widget
	&x0 $2 &y0 $2
	&x1 $2 &y1 $2
@scroll
	&x $2 &y $2 
	&addr $2

( program )

|0100 

	( theme ) 
	#f076 .System/r DEO2 
	#f0c6 .System/g DEO2 
	#f096 .System/b DEO2

	( vectors ) 
	;on-frame .Screen/vector DEO2
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2

	( place )
	#0010 .textarea/x0 STZ2
	#0020 DUP2 .textarea/y0 STZ2
	.Screen/width DEI2 #0018 -- .textarea/x1 STZ2
	.Screen/height DEI2 #0008 -- DUP2 .textarea/y1 STZ2
	SWP2 -- 10// .textarea/lines STZ2
	.Screen/width DEI2 DUP2 #0008 -- .scrollbar/x0 STZ2 .scrollbar/x1 STZ2
	#0018 .scrollbar/y0 STZ2
	.Screen/height DEI2 .scrollbar/y1 STZ2
	.Screen/width DEI2 #0030 -- DUP2 .widget/x0 STZ2 #0018 ++ .widget/x1 STZ2
	#0004 DUP2 .widget/y0 STZ2 #0010 ++ .widget/y1 STZ2

	;untitled-txt ;new-file JSR2
	
	( defaults )
	#00 .textarea/highlight STZ
	#0000 ;scroll-to JSR2
	;document/body ;select-addr JSR2

	( theme support )
	;load-theme JSR2

BRK

@on-frame ( -> )
	
	.state/trap LDZ #01 JCN [ BRK ]

	( incr ) .state/timer LDZ #01 + DUP .state/timer STZ 

	#08 ! ,&no-toggle JCN
		.state/blink TOGGLE
		#23 .state/blink LDZ #09 * + ;draw-filepath JSR2
		#00 .state/timer STZ 
		&no-toggle
	
BRK

@on-button ( -> )
	
	( ignore empty )
	.Controller DEI2 #0000 !! #01 JCN [ BRK ]

	( trap )
	.state/trap LDZ ,on-button-trap JCN

	.Controller/button DEI 
		DUP #f0 AND ;on-button-arrow JCN2
		#01 AND ;on-control JCN2
	.Controller/key DEI 
		DUP ;on-button-key JCN2 
		POP

BRK

@on-button-trap ( -> )
	
	.Controller/key DEI
	DUP ,&continue JCN
		POP BRK
		&continue
	DUP ( backspace ) #08 ! ,&no-edit-backspace JCN
		( clamp ) .path/length LDZ #00 = ,&edit-end JCN
		#20 ;draw-filepath JSR2
		.path/length LDZ #01 - STHk .path/length STZ
		#00 .path/name STHr + STZ
		#21 ;draw-filepath JSR2 POP BRK
		&no-edit-backspace
	DUP ( special ) #20 > ,&no-edit-enter JCN
		#00 .state/trap STZ
		#21 ;draw-filepath JSR2 POP BRK
		&no-edit-enter
	( clamp ) .path/length LDZ #1f = ,&edit-end JCN
	DUP .path/name .path/length LDZ STHk + STZ
	STHr #01 + STHk .path/length STZ
	#00 .path/name STHr + STZ
	#23 ;draw-filepath JSR2
	&edit-end
	POP

BRK

@on-control ( -> )

	.Controller/key DEI
	( blank )
	DUP #00 ! ,&continue JCN
		POP BRK
		&continue
	DUP ( ctrl+a ) #61 ! ,&no-selectall JCN
		;clear-textarea JSR2
		;document/body EOF ;select-range JSR2
		;draw-textarea JSR2
		POP BRK
		&no-selectall
	DUP ( ctrl+h ) #68 ! ,&no-toggle-highlight JCN
		;clear-textarea JSR2
		.textarea/highlight TOGGLE
		;draw-textarea JSR2
		POP BRK
		&no-toggle-highlight
	DUP ( ctrl+f ) #66 ! ,&no-toggle-fixedwidth JCN
		;clear-textarea JSR2
		.textarea/font TOGGLE
		;draw-textarea JSR2
		POP BRK
		&no-toggle-fixedwidth
	( copy/paste/cut )
	DUP ( ctrl+c ) #63 ! ,&no-copy JCN
		;copy-snarf JSR2 
		;draw-widget/memory JSR2 
		POP BRK 
		&no-copy
	DUP ( ctrl+v ) #76 ! ,&no-paste JCN
		;paste-snarf JSR2 
		;draw-widget/memory JSR2 
		POP BRK 
		&no-paste
	DUP ( ctrl+x ) #78 ! ,&no-cut JCN
		;cut-snarf JSR2 
		;draw-widget/memory JSR2 
		POP BRK 
		&no-cut
	( document )
	DUP ( ctrl+r ) #72 ! ,&no-name JCN
		.state/trap TOGGLE
		#00 .state/blink STZ 
		#23 ;draw-filepath JSR2 
		POP BRK 
		&no-name
	DUP ( ctrl+o ) #6f ! ,&no-open JCN
		;clear-textarea JSR2
		;load-file JSR2
		.selection/from LDZ2 ;select-addr/no-clear JSR2 
		POP BRK 
		&no-open
	DUP ( ctrl+s ) #73 ! ,&no-save JCN
		;save-file JSR2 
		;draw-state JSR2 
		POP BRK 
		&no-save
	DUP ( ctrl+n ) #6e ! ,&no-make JCN
		#20 ;draw-filepath JSR2
		;clear-textarea JSR2
		#0000 ;set-scroll JSR2
		;untitled-txt ;new-file JSR2
		;document/body ;select-addr/no-clear JSR2
		#21 ;draw-filepath JSR2 
		&no-make
	POP 

BRK

@on-button-arrow ( button -> )
	
	DUP #04 AND ,&shift JCN

	.selection/from LDZ2 
	( bring button on top ) ROT 
	,arrow-to-addr JSR
	;select-addr JSR2 
	( remove button ) POP 
	;draw-widget/selection JSR2
	BRK

	&shift
	.selection/to LDZ2 
	( bring button on top ) ROT 
	,arrow-to-addr JSR
	;select-until-addr JSR2 
	( remove button ) POP 
	;draw-widget/size JSR2

BRK

@arrow-to-addr ( addr* button -- button addr* )

	DUP #01 AND ;&ctrl JCN2
	DUP #02 AND ;&alt JCN2
	#04 SFT
	( no mods )
	DUP #01 ! ,&no-up JCN
		ROT ROT #0002 -- ( LB ) #0a ;find-prev-char JSR2 #0001 ++ RTN
		&no-up
	DUP #02 ! ,&no-down JCN
		ROT ROT #0001 ++ ( LB ) #0a ;find-next-char JSR2 #0001 ++ RTN
		&no-down
	DUP #04 ! ,&no-left JCN
		ROT ROT #0001 -- RTN
		&no-left
	DUP #08 ! ,&no-right JCN
		ROT ROT #0001 ++ RTN
		&no-right
	&ctrl
	#04 SFT
	DUP #01 ! ,&no-prev-label JCN
		ROT ROT #0001 -- ( @ ) #40 ;find-prev-char JSR2 RTN
		&no-prev-label
	DUP #02 ! ,&no-next-label JCN
		ROT ROT #0001 ++ ( @ ) #40 ;find-next-char JSR2 RTN
		&no-next-label
	DUP #04 ! ,&no-linestart JCN
		ROT ROT #0001 -- ( LB ) #0a ;find-prev-char JSR2 #0001 ++ RTN
		&no-linestart
	DUP #08 ! ,&no-lineend JCN
		ROT ROT #0001 ++ ( LB ) #0a ;find-next-char JSR2 RTN
		&no-lineend
	&alt
	#04 SFT
	DUP #04 ! ,&no-leap-left JCN
		ROT ROT #0001 -- ( LB ) #20 ;find-prev-char JSR2 RTN
		&no-leap-left
	DUP #08 ! ,&no-leap-right JCN
		ROT ROT #0001 ++ ( LB ) #20 ;find-next-char JSR2 #0001 ++ RTN
		&no-leap-right
	ROT ROT

RTN

@on-button-key ( key -> )
		
	DUP #0d ! ,&no-linebreak JCN
		POP #0a
		&no-linebreak
	DUP #08 = ,&erase JCN
	DUP #1b = ,&escape JCN
		
	&insert ( key -- )
		SPACE-AVAILABLE #0000 >> ,&no-clamp-to JCN 
			POP BRK
			&no-clamp-to
		;clear-textarea JSR2
		.selection/from LDZ2 SELECTION-LENGTH DUP2 #0001 == TOS -- STH2k ++ EOF STH2r ;shiftl JSR2
		.selection/from LDZ2 EOF #0001 ;shiftr JSR2
		.selection/from LDZ2 STH2k STA
		STH2r #0001 ++ ;select-addr/no-clear JSR2
		;draw-widget/memory JSR2
	BRK

	&escape ( key -- )
		POP
		RESET-SELECTION
		;load-theme JSR2
	BRK

	&erase ( key -- )
		POP 
		SELECTION-LENGTH #0001 >> ,&erase-multiple JCN
		.selection/from LDZ2 ;document/body >> ,&erase-single JCN
	BRK

	&erase-single ( -- )
		;clear-textarea JSR2
		.selection/from LDZ2 #0001 -- DUP2 STH2k .selection/from STZ2 
			#0001 ++ .selection/to STZ2
		.selection/from LDZ2 #0001 ++ EOF #0001 ;shiftl JSR2
		STH2r ;select-addr/no-clear JSR2
		;draw-widget/memory JSR2
	BRK

	&erase-multiple ( -- )
		;clear-textarea JSR2
		ERASE-SELECTION
		.selection/from LDZ2 #0001 ++ .selection/to STZ2
		;draw-textarea JSR2
		;draw-widget/memory JSR2
	BRK

BRK

@on-mouse ( -> )
	
	( clear last cursor )
	.cursor/x LDZ2 .Screen/x DEO2
	.cursor/y LDZ2 .Screen/y DEO2
	#30 .Screen/color DEO

	( draw new cursor )
	.Mouse/x DEI2 DUP2 .cursor/x STZ2 .Screen/x DEO2
	.Mouse/y DEI2 DUP2 .cursor/y STZ2 .Screen/y DEO2
	;cursor-icn .Screen/addr DEO2
	.Mouse/state DEI #00 ! #02 * [ #31 + .Screen/color DEO ]

	( wheel )
	.Mouse/wheel DEI
	DUP #00 = ,&no-scroll JCN
		DUP #ff = TOS #0002 ** #ffff ++ .scroll/y LDZ2 ++ ;scroll-to JSR2
		&no-scroll
	POP

	.Mouse/y DEI2 #0018 << ,&skip JCN
		.Mouse/x DEI2
		DUP2 .Screen/width DEI2 #0008 -- >> ;on-mouse-scrollbar JCN2
		DUP2 #0010 >> ;on-mouse-textarea JCN2
		POP
		&skip

	( release-record ) #00 DUP .Mouse/state DEO .cursor/last STZ
	
BRK

@on-mouse-textarea ( x* -> )

	POP2

	.Mouse/state DEI DUP .cursor/last LDZ 
		DUP2 #1000 !! ,&no-down-alt JCN
			.selection/from LDZ2 #0001 ++ 
			( alt-click: start from top ) 
			.Controller/button DEI #02 ! ,&no-alt-find JCN 
				POP2 ;document/body
				&no-alt-find
			;find JSR2 
			DUP2 #0000 == ,&no-result JCN
				.Controller/button DEI #01 ! ,&no-ctrl-find JCN 
					SELECTION-LENGTH STH2
					;cut-snarf JSR2
					STH2kr --
					.selection/from LDZ2 STH2r ++ .selection/to STZ2
					&no-ctrl-find
				DUP2 DUP2 SELECTION-LENGTH ++ ;select-range JSR2
				&no-result
			POP2
			RELEASE-MOUSE
			POP2 POP BRK
			&no-down-alt
		DUP2 #0000 == ;&end JCN2
		DUP2 #0001 !! ,&no-release JCN
			SELECTION-LENGTH #0001 == ,&end JCN
			;draw-widget/size JSR2
			,&end JMP 
			&no-release
		DUP2 #0100 !! ,&no-down JCN
			( mouse-down )
			,touch-to-addr JSR
			( shift ) .Controller/button DEI #04 = ,&on-shift JCN
			;select-addr JSR2
			,&end JMP 
			( shift down )
			&on-shift
			;select-until-addr JSR2
			,&end JMP 
			&no-down
		( drag )
		,touch-to-addr JSR ;select-until-addr JSR2
	&end
	POP2
	.cursor/last STZ

BRK

@touch-to-addr ( -- addr* )
	
	( get x offset )
	.Mouse/x DEI2 .scroll/x LDZ2 ++ .textarea/x0 LDZ2 -- STH2
	#0000 STH2

	( get y offset )
	.Mouse/y DEI2 .textarea/y0 LDZ2 -- 10// STH2
	#0000 
	.scroll/addr LDZ2
	&while
		OVR2 STH2kr == ,&reached JCN
		LDAk #0a ! ,&no-linebreak JCN
			INCR2
			&no-linebreak
		#0001 ++
		LDAk ,&while JCN
	&reached
	SWP2 POP2 POP2r

	( count char width )
	DUP2 #0a ;find-next-char JSR2
	&loop
		( get character ) OVR2 ;scan-font JSR2 LDA TOS
		( get width ) ;draw-uf2/font LDA2 ++ LDA 
		( incr ) STH2r ROT TOS ++ STH2
		LTH2kr STHr ,&end JCN
		INCR2
		LTH2k ,&loop JCN
	&end
	POP2 POP2r POP2r

RTN

@on-mouse-scrollbar ( x* -> )

	POP2
	( skip unclick ) .Mouse/state DEI #01 JCN [ BRK ]

	.Mouse/y DEI2 .scrollbar/y0 LDZ2 #0008 ++ << ,&up JCN
	.Mouse/y DEI2 .scrollbar/y1 LDZ2 #0008 -- << ,&jump JCN

	&down
		.scroll/y LDZ2 #0001 ++
		RELEASE-MOUSE ,&end JMP
	&up
		.scroll/y LDZ2 #0000 !! #01 JCN [ BRK ]
		.scroll/y LDZ2 #0001 --
		RELEASE-MOUSE ,&end JMP
	&jump
		.Mouse/y DEI2 .scrollbar/y0 LDZ2 #0008 ++ -- .textarea/lines LDZ2 ** 
	&end
		,scroll-to JSR

BRK

( actions )

@scroll-to ( line* -- )
	
	DUP2 ,get-lines JSR >> ,&skip JCN
	;clear-textarea JSR2
	DUP2 ,set-scroll JSR
	;draw-textarea JSR2
	;draw-widget/scroll JSR2
	&skip
	POP2

RTN

@get-lines ( -- line* )
		
	.scroll/y LDZ2
	.scroll/addr LDZ2
	&loop
		LDAk NOT-LINEBREAK ,&no-linebreak JCN
			INCR2
			&no-linebreak
		#0001 ++
		LDAk ,&loop JCN
	POP2

RTN

@set-scroll ( line* -- )
	
	DUP2 .scroll/y STZ2	
	,line-to-addr JSR .scroll/addr STZ2
	;draw-scrollbar JSR2

RTN

@line-to-addr ( line* -- addr* )

	DUP2 #0000 !! ,&not-top JCN
		POP2 ;document/body RTN
		&not-top
	STH2
	#0000 STH2
	;document/body
	&while
		LDAk NOT-LINEBREAK ,&continue JCN
			STH2r #0001 ++ STH2
			EQU2kr STHr #00 = ,&continue JCN
				POP2r POP2r #0001 ++ RTN
				&continue
		#0001 ++
		LDAk ,&while JCN
	POP2r POP2r

RTN

@select-addr ( addr* -- )
	
	;clear-textarea JSR2
	&no-clear
	;clamp-addr JSR2 
		DUP2 .selection/from STZ2 
		DUP2 #0001 ++ .selection/to STZ2
	,follow-selection JSR
	;draw-textarea JSR2

RTN	

@select-until-addr ( addr* -- )
	
	;clear-textarea JSR2
	;clamp-addr JSR2 
	( min )
	.selection/from LDZ2 #0001 ++ GTH2k ,&no-min JCN
		SWP2
		&no-min
	POP2 DUP2 .selection/to STZ2
	,follow-selection JSR
	;draw-textarea JSR2
	;draw-widget/selection JSR2

RTN	

@select-range ( from* to* -- )
	
	;clear-textarea JSR2
	&no-clear
	SWP2 ;clamp-addr JSR2 .selection/from STZ2 
	( min )
	.selection/from LDZ2 #0001 ++ GTH2k ,&no-min JCN
		SWP2
		&no-min
	POP2
	DUP2 ;clamp-addr JSR2 .selection/to STZ2 
	,follow-selection JSR
	;draw-textarea JSR2

RTN

@follow-selection ( addr* -- )
	
	( get line at addr* )
	#0000 STH2
	;document/body SWP2
	&loop
		OVR2 LDA NOT-LINEBREAK ,&no-linebreak JCN
			STH2r #0001 ++ STH2
			&no-linebreak
		INCR2 LTH2k ,&loop JCN
	POP2 POP2 
	STH2r

	( scroll-y )
	DUP2 .scroll/y LDZ2 .textarea/lines LDZ2 ++ << ,&no-below JCN
		DUP2 .scroll/y LDZ2 .textarea/lines LDZ2 ++ -- 
		.scroll/y LDZ2 ++ ;set-scroll JSR2
		&no-below
	DUP2 .scroll/y LDZ2 >> ,&no-above JCN
		DUP2 ;set-scroll JSR2
		&no-above
	POP2

	( scroll-x )
	.selection/to LDZ2 #0001 -- ,addr-to-posx JSR
	DUP2 .textarea/x1 LDZ2 .scroll/x LDZ2 ++ #0040 -- << ,&no-right JCN
		DUP2 .textarea/x1 LDZ2 #0040 -- -- .scroll/x STZ2
		&no-right
	DUP2 .scroll/x LDZ2 >> ,&no-left JCN
		DUP2 .scroll/x STZ2
		&no-left
	POP2

RTN

@addr-to-posx ( addr* -- pixels* )
		
	#0000 STH2
	DUP2 #0001 -- #0a ;find-prev-char JSR2 SWP2
	&loop
		EQU2k ,&end JCN
		( get character ) OVR2 ;scan-font JSR2 LDA TOS
		( get width ) ;draw-uf2/font LDA2 ++ LDA 
		( incr ) STH2r ROT TOS ++ STH2
		INCR2
		LTH2k ,&loop JCN
	&end
	POP2 POP2
	STH2r

RTN

@find ( from* -- addr* )
		
	SELECTION-LENGTH STH2
	&while
		DUP2 STH2kr ,&compare JSR #00 = ,&no-found JCN
			POP2r RTN
			&no-found
		#0001 ++
		LDAk ,&while JCN
	POP2 POP2r #0000
	RTN
	&compare ( addr* length* -- flag )
		SWP2 STH2
		#0000 SWP2
		&scan
			OVR2 STH2kr ++ LDA STH
			OVR2 .selection/from LDZ2 ++ LDA STHr = ,&continue JCN
				POP2 POP2 POP2r #00 RTN
				&continue
			INCR2 LTH2k ,&scan JCN
		POP2 POP2 POP2r
		#01

RTN

@find-prev-char ( addr* char -- addr* )
	
	STH
	&while
		LDAk STHkr = ,&end JCN
		#0001 --
		LDAk ,&while JCN
	&end
	POPr

RTN

@find-next-char ( addr* char -- addr* )
	
	STH
	&while
		LDAk STHkr = ,&end JCN
		#0001 ++
		LDAk ,&while JCN
	&end
	POPr

RTN

@shiftr ( from* to* length* -- )
	
	STH2
	SWP2 #0001 --
	&loop
		OVR2 DUP2 LDA 
		ROT ROT STH2kr ++ STA 
		DECR2 GTH2k ,&loop JCN
	POP2 POP2
	;document/length LDA2 STH2r ++ ;document/length STA2

RTN

@shiftl ( from* to* length* -- )
	
	STH2
	#0001 ++
	&loop
		OVR2 DUP2 LDA
		ROT ROT STH2kr -- STA 
		INCR2 LTH2k ,&loop JCN
	POP2 POP2

	;document/length LDA2 STH2r -- ;document/length STA2
	
RTN

@wiper ( addr* -- )

	&loop
		DUP2 #00 ROT ROT STA
		#0001 ++ 
		DUP2 #0000 == ,&loop JCN
	POP2

RTN

@clamp-addr ( addr* -- addr* )

	;document/body GTH2k ,&no-clamp-start JCN
		SWP2  
		&no-clamp-start
	POP2
	EOF LTH2k ,&no-clamp-end JCN
		SWP2
		&no-clamp-end
	POP2

RTN

( Drawing )

@redraw ( -- )

	#0000 .Screen/width DEI2 #0018 #01 ;line-hor JSR2
	#21 ;draw-filepath JSR2
	;draw-widget/memory JSR2
	;draw-textarea JSR2

RTN

@draw-short ( x* y* addr* color -- )
	
	STH
		STHk #04 SFT ,&parse JSR ,&2 STR
		STHr #0f AND ,&parse JSR ,&3 STR
		STHk #04 SFT ,&parse JSR ,&0 STR
		STHr #0f AND ,&parse JSR ,&1 STR
	;&0 ;font-data-bold STHr ;draw-uf2 JSR2 RTN
	&parse ( value -- char )
		DUP #09 > ,&above JCN 
			#30 + RTN 
		&above 
			#09 - #60 + RTN
	&0 $1 &1 $1 &2 $1 &3 $2

RTN

@draw-widget ( -- )

	&scroll
		,&init JSR .scroll/y LDZ2 #22 ,draw-short JSR 
		RTN
	&selection
		,&init JSR .selection/from LDZ2 ;document/body -- #23 ;draw-short JSR2
		RTN
	&size
		,&init JSR SELECTION-LENGTH #22 ;draw-short JSR2
		RTN
	&memory
		,&init JSR SPACE-AVAILABLE #21 ;draw-short JSR2 
		RTN
	&init
		#fff0 .Screen/addr DEO2
		.widget/x0 LDZ2 .widget/x1 LDZ2
		&loop
			OVR2 .Screen/x DEO2
			.widget/y0 LDZ2 STH2k .Screen/y DEO2
			#20 .Screen/color DEO
			STH2r #0008 ++ .Screen/y DEO2
			#20 .Screen/color DEO
			INCR2 LTH2k ,&loop JCN
		POP2 POP2
		.widget/x0 LDZ2 .widget/y0 LDZ2
		RTN

RTN

@draw-state ( -- )

RTN

@draw-filepath ( color -- )
		
	;font-data-bold ;draw-uf2/font STA2
	STH #0010 #0004 ;path/name ;font-data-bold STHr ;draw-uf2 JSR2

RTN

@clear-textarea ( -- )
	
	.textarea/x0 LDZ2 .textarea/y0 LDZ2 
		.scroll/addr LDZ2 ;clear-uf2-multiline JSR2

RTN

@draw-textarea ( -- )
	
	.textarea/x0 LDZ2 .textarea/y0 LDZ2 
		.scroll/addr LDZ2 ;draw-uf2-multiline JSR2

RTN

@scan-font ( addr* )
		
	.textarea/font LDZ ,&fixed-width JCN
	.textarea/highlight LDZ ,&default JCN
	LDAk ( left-paren ) #28 ! ,&no-paren-open JCN
		#01 ,&commented STR
		,&default JMP
		&no-paren-open
	DUP2 #0001 -- LDA ( right-paren ) #29 ! ,&no-paren-close JCN
		#00 ,&commented STR
		,&default JMP
		&no-paren-close
	LDAk IS-SPACER #01 JCN [ RTN ]
	,&commented LDR ,&end JCN
	DUP2 #0001 ++ LDA #40 < ,&bold JCN
	DUP2 #0001 ++ LDA IS-LABEL ,&bold JCN
	&default
		;font-data-regular ;draw-uf2/font STA2 RTN
	&bold
		;font-data-bold ;draw-uf2/font STA2 RTN
	&fixed-width
		;font-data-fixed ;draw-uf2/font STA2 RTN
	&end
		RTN
	&commented $1

RTN

@scan-color ( addr* )
	
	.textarea/highlight LDZ ,&default JCN
	LDAk ( left-paren ) #28 ! ,&no-paren-open JCN
		#01 ,&commented STR
		#23 ;draw-uf2-multiline/color STA RTN
		&no-paren-open
	DUP2 #0001 -- LDA ( right-paren ) #29 ! ,&no-paren-close JCN
		#00 ,&commented STR
		,&default JMP
		&no-paren-close
	LDAk IS-SPACER #01 JCN [ RTN ]
	,&commented LDR ,&end JCN
	DUP2 #0001 ++ LDA IS-LABEL ,&label JCN
	&default
		#21 ;draw-uf2-multiline/color STA RTN
	&label
		#22 ;draw-uf2-multiline/color STA RTN
	&end
		RTN
	&commented $1

RTN

@clear-uf2-multiline ( x* y* text* -- )
	 
	;font-data-regular ;draw-uf2/font STA2
	#00 ;scan-font/commented STA
	SWP2 .Screen/y DEO2
	SWP2 .scroll/x LDZ2 -- DUP2 .Screen/x DEO2 SWP2
	&while
		;scan-font JSR2
		( do not overdraw )
		.Screen/x DEI2 .textarea/x1 LDZ2 GTS2 ,&outside JCN
			LDAk #20 ;draw-uf2/sprite JSR2
			&outside
		LDAk NOT-LINEBREAK ,&no-linebreak JCN
			( move down ) #0010 .scroll/x LDZ2 -- .Screen/x DEO2
			( incr y ) .Screen/y DEI2 #0010 ++ DUP2 .Screen/y DEO2 
			( stop at screen bottom ) .textarea/y1 LDZ2 >> ,&end JCN 
			&no-linebreak
		( incr addr ) #0001 ++
		LDAk ,&while JCN
	&end
	POP2 POP2
	#00 #20 ;draw-uf2/sprite JSR2

RTN

@draw-uf2-multiline ( x* y* text* -- )
	
	;font-data-regular ;draw-uf2/font STA2
	#00 ;scan-font/commented STA
	#21 ,&color STR
	SWP2 .Screen/y DEO2
	SWP2 .scroll/x LDZ2 -- DUP2 .Screen/x DEO2 SWP2
	&while
		;scan-color JSR2
		;scan-font JSR2
		( do not overdraw )
		.Screen/x DEI2 .textarea/x1 LDZ2 GTS2 ,&outside JCN
			,&get-color JSR STH LDAk STHr ;draw-uf2/sprite JSR2
			&outside
		LDAk NOT-LINEBREAK ,&no-linebreak JCN
			#0010 .scroll/x LDZ2 -- .Screen/x DEO2
			.Screen/y DEI2 #0010 ++ DUP2 .Screen/y DEO2 
			.textarea/y1 LDZ2 >> ,&end JCN 
			&no-linebreak
		#0001 ++
		LDAk ,&while JCN
	&end
	POP2 POP2
	#00 ,&color LDR ;draw-uf2/sprite JSR2
	RTN
	&color $1
	&get-color ( addr* -- addr* color )
		DUP2 .selection/from LDZ2 << ,&unselected JCN
		DUP2 .selection/to LDZ2 #0001 -- >> ,&unselected JCN
		#28 RTN
		&unselected
			,&color LDR RTN

RTN

@draw-uf2 ( x* y* text* font* color -- )
	
	STH 
	,&font STR2
	SWP2 .Screen/y DEO2
	SWP2 DUP2 .Screen/x DEO2 SWP2
	&while
		( draw ) LDAk STHkr ,&sprite JSR
		( incr ) #0001 ++
		LDAk ,&while JCN
	POP2 POP2 POPr
	RTN

	&font $2

	&get-addr ( char -- addr* )
		DUP #09 = ,&tab JCN ( tab )
		DUP #0a = ,&linebreak JCN ( linebreak )
		DUP #0d = ,&linebreak JCN ( linebreak )
		DUP #1f > ,&valid JCN
			POP ;unknown-icn RTN
		&invisible
			POP ;invisible-icn RTN
		&linebreak
			POP ;linebreak-icn RTN
		&tab
			POP ;tab-icn RTN
		&valid
		#20 - TOS #0020 ** ,&font LDR2 
		#0100 ++ ++
	RTN

	&sprite ( char color -- )
		STH
		( get addr ) STHk ,&get-addr JSR .Screen/addr DEO2
		( get width ) STHkr TOS ,&font LDR2 
		++ LDA 
		TOS SWPr
			( left-top ) STHkr .Screen/color DEO
			.Screen/y DEI2 #0008 ++ .Screen/y DEO2
			.Screen/addr DEI2 #0010 ++ .Screen/addr DEO2
			( left-bottom ) STHkr .Screen/color DEO
			.Screen/x DEI2 #0008 ++ .Screen/x DEO2
			.Screen/y DEI2 #0008 -- .Screen/y DEO2
			DUP #08 < ,&thin JCN
			.Screen/addr DEI2 #0008 -- .Screen/addr DEO2
			( right-top )  STHkr .Screen/color DEO
			.Screen/addr DEI2 #0010 ++ .Screen/addr DEO2
			.Screen/y DEI2 #0008 ++ .Screen/y DEO2
			( right-bottom ) STHkr .Screen/color DEO
			.Screen/y DEI2 #0008 -- .Screen/y DEO2 &thin
		SWPr
		( use width ) .Screen/x DEI2 ++ #0008 -- .Screen/x DEO2
		POPr POPr
	RTN

RTN

@draw-scrollbar ( -- )
	
	.scrollbar/x0 LDZ2 .Screen/x DEO2
	;scrollbar-icns/bg .Screen/addr DEO2

	( fill )
	#0020 .Screen/height DEI2 #0008 --
	&loop
		SWP2 
			DUP2 .Screen/y DEO2
			#23 .Screen/color DEO
		#0008 ++ SWP2
		LTH2k ,&loop JCN
	POP2 POP2
 
	( position )
	;scrollbar-icns/fg .Screen/addr DEO2
	;get-lines JSR2 .textarea/lines LDZ2 //
	.scrollbar/y1 LDZ2 .scrollbar/y0 LDZ2 -- #0016 --
	SWP2 --

	.scroll/y LDZ2 .textarea/lines LDZ2 // .scrollbar/y0 LDZ2 ++ #0008 ++ SWP2 OVR2 ++
	&stretch
		OVR2 .Screen/y DEO2
		#21 .Screen/color DEO
		INCR2 LTH2k ,&stretch JCN
	POP2 POP2

	( scroll-up )
	.scrollbar/y0 LDZ2 .Screen/y DEO2
	;scrollbar-icns/up .Screen/addr DEO2
	( draw ) #24 .Screen/color DEO

	( scroll-down )
	.scrollbar/y1 LDZ2 #0008 -- .Screen/y DEO2
	;scrollbar-icns/down .Screen/addr DEO2
	( draw ) #24 .Screen/color DEO

RTN

( document )

@new-file ( default* -- )
	
	;document/body ;wiper JSR2
	#0000 ;document/body STA2
	#0000 ;document/length STA2
	( rename )
	STH2
	#00 #0c
	&loop
		OVR DUP TOS STH2kr ++ LDA
		SWP .path/name + STZ
		INCR LTHk ,&loop JCN
	( end ) #00 SWP .path/name + STZ 
	.path/length STZ 
	POP2r
	RESET-STATE

RTN

@load-file ( -- )

	;document/body ;wiper JSR2
	;path/name .File/name DEO2 
	#fff0 ;document/body -- .File/length DEO2 
	;document/body .File/load DEO2
	RESET-STATE
	( null ) #00 ;document/body .File/success DEI2 ++ STA
	.File/success DEI2 ;document/length STA2

RTN

@save-file ( -- )

	;path/name .File/name DEO2 
	( get file length )
	;document/body STH2k
	&loop
		#0001 ++
		LDAk ,&loop JCN
	STH2r --
	 .File/length DEO2 
	;document/body .File/save DEO2
	RESET-STATE

RTN

@theme-txt ".theme $1

@load-theme ( -- )

	;theme-txt .File/name DEO2 
	#0006 .File/length DEO2 
	#fffa .File/load DEO2

	.File/success DEI2 #0006 !! ,&ignore JCN
		#fffa LDA2 .System/r DEO2
		#fffc LDA2 .System/g DEO2
		#fffe LDA2 .System/b DEO2
		&ignore
	;redraw JSR2

RTN

( snarf )

@snarf-txt
	".snarf $1

@cut-snarf ( -- )
	
	SELECTION-LENGTH #0001 >> ,&continue JCN
		POP2r RTN
		&continue

	;clear-textarea JSR2
	,copy-snarf JSR
	ERASE-SELECTION
	RESET-SELECTION
	;draw-textarea JSR2

RTN

@copy-snarf ( -- )
	
	SELECTION-LENGTH STH2k #0001 >> ,&continue JCN
		POP2r RTN
		&continue

	;snarf-txt .File/name DEO2 
	STH2r .File/length DEO2 
	.selection/from LDZ2 .File/save DEO2

RTN

@paste-snarf ( -- )
	
	;clear-textarea JSR2
	( erase selection ) .selection/from LDZ2 SELECTION-LENGTH DUP2 #0001 == TOS -- STH2k ++ EOF STH2r ;shiftl JSR2
	( push right ) .selection/from LDZ2 EOF SPACE-AVAILABLE STH2k ;shiftr JSR2
	;snarf-txt .File/name DEO2 
	STH2kr .File/length DEO2 
	.selection/from LDZ2 .File/load DEO2 
	( pull left ) .selection/from LDZ2 STH2kr ++ EOF STH2r .File/success DEI2 -- ;shiftl JSR2
	( cap ) #00 EOF STA
	.selection/from LDZ2 .File/success DEI2 ++ ;select-addr/no-clear JSR2 

RTN

( generics )

@line-hor ( x0* x1* y* color -- )
	
	STH .Screen/y DEO2
	&loop
		( save ) OVR2 .Screen/x DEO2
		( draw ) STHkr .Screen/color DEO
		( incr ) SWP2 #0002 ++ SWP2
		LTH2k ,&loop JCN
	POP2 POP2 POPr

RTN

@print-hex ( value -- )
	
	STHk #04 SFT ,&parse JSR .Console/write DEO
	STHr #0f AND ,&parse JSR .Console/write DEO
	RTN
	&parse ( value -- char )
		DUP #09 > ,&above JCN #30 + RTN &above #09 - #60 + RTN

RTN

@untitled-txt  
	"untitled.txt $1

@cursor-icn     
	80c0 e0f0 f8e0 1000

@scrollbar-icns
	&bg   aa55 aa55 aa55 aa55
	&fg   ffff ffff ffff ffff
	&up   0018 3c7e 1818 1800
	&down 0018 1818 7e3c 1800