-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathEditor.fs
236 lines (214 loc) · 4.3 KB
/
Editor.fs
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
######################################################
##
## Editor:
##
## A collapsing and expanding text editor panel
## which can be used in a variety of REPL/
## command line applications. Depends on Grid.fs,
## Sprites.fs and String.fs.
##
## John Earnest
##
######################################################
:array grid-buffer 2400 0
: save-grid ( -- )
grid-buffer
28 for
39 for
39 i -
28 j -
tile-grid@ @ over !
1 +
next
next
drop
;
: restore-grid ( offset -- )
40 * grid-buffer +
28 for
39 for
dup @
39 i -
28 j -
tile-grid@ !
1 +
next
next
drop
;
######################################################
##
## Editor Support Words:
##
## For the sake of simplicity I perform a number of
## repeated calculations here which limit the state
## of the text editor to the active buffer ('text')
## and a cursor position within that buffer.
##
######################################################
:const cursor-tile 271
:const cursor-sprite 129
:const text-offset 64
:const backspace 8
:const return 10
:const space 32
:const maxlines 28
:const text-size 1202
:array text text-size 0
:var cursor
: linestart 0 over 30 swap - tile-grid@ ; ( height -- height addr )
: keydelay 4 for sync next ; ( -- )
: max 2dup < if swap then drop ; ( a b -- max )
: min 2dup > if swap then drop ; ( a b -- min )
: height ( -- lines )
1 0 text >r
loop
( lines charcount | text-ptr )
i @ -if break then
i @ return = if
drop 1 + 0
else
1 +
dup 40 = if drop 1 + 0 then
then
r> 1 + >r
again
rdrop drop
;
: draw-buffer ( height -- )
linestart over 41 * space text-offset + fill
linestart text >r
loop
( height grid-ptr | text-ptr )
i @ -if break then
i @ return = if
drop 1 - linestart
else
i @ text-offset + over !
1 +
dup GP @ - 41 mod 40 = if drop 1 - linestart then
then
r> 1 + >r
again
rdrop 2drop
;
: cursor-pos ( height -- tx ty )
0 swap 30 swap - cursor @ text >r
loop
( tx ty charsleft | text-ptr )
dup -if break then
i @ return = if
>r
1 + swap drop 0 swap
r>
else
>r
swap 1 + swap
over 40 = if 1 + swap drop 0 swap then
r>
then
r> 1 + >r 1 -
again
drop rdrop
;
: cursor-set ( tx ty -- )
30 height - -
dup 0 < if 2drop 0 cursor ! exit then
dup height >= if 2drop text size cursor ! exit then
0 text >r loop
( ty charcount | text-ptr )
over 0 = if 2drop break then
i @ return = if
drop 1 - 0
else
1 +
dup 40 = if drop 1 - 0 then
then
r> 1 + >r
again
loop
( tx | text-ptr )
dup 0 = if drop break then
i @ -if drop break then
i @ return = if drop break then
1 - r> 1 + >r
again
r> text - cursor !
;
: trydelete ( -- )
cursor @ 1 < if exit then
cursor @ text + dup 1 - dup size <move
cursor @ 1 - cursor !
;
: tryinsert ( char -- )
dup backspace = if drop trydelete exit then
cursor @ text + dup 1 + over size >move
cursor @ text + !
cursor @ 1 + cursor !
height maxlines > if trydelete then
;
: cursor-line height cursor-pos swap drop ; ( -- num )
######################################################
##
## The Editor:
##
## Automatically preserves any previously existing
## grid contents and returns the updated string
## when 'return' is pressed with the cursor on the
## last line of text.
##
######################################################
: edit ( str -- str' )
text text-size 0 fill
text over size 1 + >move
0 28 cursor-set
save-grid
loop
# drain the keyboard buffer
loop
KB @ dup -1 = if drop break then
dup 3 = if
cursor-sprite hide
0 restore-grid
drop "BREAK" abort
then
dup return = if
drop cursor-line 29 = if
cursor-sprite hide
0 restore-grid
text exit
else
return tryinsert
then
else
tryinsert
then
again
# handle cursor keys
keys key-lf and if
cursor @ 1 - 0 max
cursor ! keydelay
then
keys key-rt and if
cursor @ 1 + text size min
cursor ! keydelay
then
keys key-up and if
height cursor-pos 1 -
cursor-set keydelay
then
keys key-dn and if
height cursor-pos 1 +
cursor-set keydelay
then
height
dup restore-grid
dup draw-buffer
# update the cursor
8x8 swap
cursor-tile swap
cursor-pos 8 * swap 8 * swap
cursor-sprite >sprite
sync
again
;