@@ -31,8 +31,8 @@ drawTopBar = function()
31
31
text.color = pal.topBarText
32
32
text.row = screenH- 1 ; text.column = 0
33
33
print " " + _sourceFile
34
- print " " * (screenW - 20 - text.column)
35
- print " ^X: Exit Line : " + cursorY
34
+ print " " * (screenW - 17 - text.column)
35
+ print " ^Q: Quit L : " + ( cursorY + 1 )
36
36
print " " * (screenW - text.column)
37
37
end function
38
38
@@ -43,7 +43,7 @@ refreshLine = function(lineNum)
43
43
text.color = pal.text
44
44
text.row = screenLines - 1 - y; text.column = 0
45
45
if lineNum < data.len then
46
- s = data[lineNum].replace(TAB, " " ) [scrollX: ]
46
+ s = data[lineNum][scrollX: ]
47
47
if s.len < screenW then s = s + " " * (screenW - s.len)
48
48
if s.len > screenW then s = s[: screenW]
49
49
else
@@ -70,21 +70,24 @@ printLine = function(row, s)
70
70
end if
71
71
end function
72
72
73
- refreshDisplay = function ()
74
- row = screenLines - 1
75
- i = scrollY
73
+ refreshRow = function (screenRow)
76
74
text.backColor = pal.background
77
75
text.color = pal.text
78
- while i < scrollY + screenLines
79
- text.row = row; text.column = 0
80
- if i < data.len then
81
- s = data[i].replace(TAB, " " )[scrollX: ]
82
- else
83
- s = " "
84
- end if
85
- printLine row, s
86
- row = row - 1 ; i = i + 1
87
- end while
76
+ i = scrollY + screenLines - 1 - screenRow
77
+ text.row = screenRow; text.column = 0
78
+ if i < data.len then
79
+ s = data[i][scrollX: ]
80
+ else
81
+ s = " "
82
+ end if
83
+ printLine screenRow, s
84
+ if i == cursorY then outer.cursorShown = false
85
+ end function
86
+
87
+ refreshDisplay = function ()
88
+ for row in range(screenLines- 1 , 0 )
89
+ refreshRow row
90
+ end for
88
91
outer.cursorShown = false
89
92
text.backColor = color.clear
90
93
end function
@@ -178,20 +181,71 @@ nextWord = function(dir)
178
181
outer.cursorX = x
179
182
end function
180
183
181
- handleKey = function (k)
184
+ showCommand = function (keyPress, desc)
185
+ refreshRow 0
186
+ s = keyPress
187
+ if desc then s = s + " : " + desc
188
+ text.row = 0
189
+ text.column = screenW - s.len
190
+ text.color = color.black
191
+ text.backColor = " #FFFFAA"
192
+ print s[:- 1 ]
193
+ text.setCell screenW- 1 , 0 , s[- 1 ]
194
+ text.setCellColor screenW- 1 , 0 , text.color
195
+ text.setCellBackColor screenW- 1 , 0 , text.backColor
196
+ end function
197
+
198
+ handleControlKey = function (k)
182
199
kcode = k.code
183
-
184
- byWord = false
185
- // ToDo:
186
- byWord = key.pressed(" left ctrl" ) or key.pressed(" right ctrl" ) or
187
- key.pressed(" left alt" ) or key.pressed(" right alt" )
188
-
189
- hideCursor
190
- if kcode == 1 then // ctrl- A (start of line)
200
+ desc = " (undefined)"
201
+
202
+ anyAlt = key.pressed(" left alt" ) or key.pressed(" right alt" )
203
+ anyShift = key.pressed(" left shift" ) or key.pressed(" right shift" )
204
+
205
+ keyPress = " ^" + char(64 + kcode)
206
+ if kcode > 31 then keyPress = " ^" + char(kcode)
207
+ if anyShift then keyPress = " Shift-" + keyPress
208
+
209
+ if keyPress == " ^A" then // ctrl- A (start of line)
210
+ desc = " LineStart"
191
211
outer.cursorX = 0
192
- else if kcode == 5 then // ctrl- E (end of line)
212
+ else if keyPress == " ^E" then // ctrl- E (end of line)
213
+ desc = " LineEnd"
193
214
outer.cursorX = lineLen(cursorY)
194
- else if kcode == 17 then // left
215
+ else if keyPress == " ^U" then // Page Up
216
+ desc = " PageUp"
217
+ outer.cursorY = outer.cursorY - screenLines
218
+ else if keyPress == " ^D" then // Page Down
219
+ desc = " PageDown"
220
+ outer.cursorY = outer.cursorY + screenLines
221
+ else if keyPress == " ^Q" then // Escape or Ctrl+ X
222
+ desc = " Quit"
223
+ outer.quitting = true
224
+ end if
225
+
226
+ if keyPress then showCommand keyPress, desc
227
+ end function
228
+
229
+ handleKey = function (k, fakeControl= false )
230
+ anyCtrl = key.pressed(" left ctrl" ) or key.pressed(" right ctrl" ) or fakeControl
231
+ anyAlt = key.pressed(" left alt" ) or key.pressed(" right alt" )
232
+ byWord = anyAlt or anyCtrl
233
+ kcode = k.code
234
+ hideCursor
235
+
236
+ // Careful with 17 - 21: these are generated by the arrow keys,
237
+ // which may be used in conjunction with Control, but still
238
+ // should be treated differently than ^ Q, ^ R, etc.
239
+ isArrowKey = false
240
+ if kcode >= 17 and kcode <= 20 then
241
+ keys = [" left" , " right" , " up" , " down" ]
242
+ if key.pressed(keys[kcode- 17 ]) then isArrowKey = true
243
+ end if
244
+
245
+ if anyCtrl and not isArrowKey then
246
+ handleControlKey k
247
+ else if kcode == 17 then // left
248
+ keyPress = " "
195
249
outer.cursorX = cursorX - 1
196
250
if cursorX < 0 and cursorY > 0 then
197
251
outer.cursorY = cursorY - 1
@@ -200,6 +254,7 @@ handleKey = function(k)
200
254
if byWord then nextWord - 1
201
255
outer.idealCursorX = cursorX
202
256
else if kcode == 18 then // right
257
+ keyPress = " "
203
258
outer.cursorX = cursorX + 1
204
259
if cursorX > lineLen(cursorY) and cursorY < data.len then
205
260
outer.cursorY = cursorY + 1
@@ -208,14 +263,17 @@ handleKey = function(k)
208
263
if byWord then nextWord 1
209
264
outer.idealCursorX = cursorX
210
265
else if kcode == 19 then // up
266
+ keyPress = " "
211
267
if byWord then amount = screenLines else amount = 1
212
268
outer.cursorY = cursorY - amount
213
269
outer.cursorX = idealCursorX
214
270
else if kcode == 20 then // down
271
+ keyPress = " "
215
272
if byWord then amount = screenLines else amount = 1
216
273
outer.cursorY = cursorY + amount
217
274
outer.cursorX = idealCursorX
218
275
else if kcode == 10 then // return
276
+ keyPress = " "
219
277
if cursorY >= data.len then
220
278
data.push " "
221
279
else
@@ -226,6 +284,7 @@ handleKey = function(k)
226
284
outer.cursorX = 0
227
285
refreshDisplay
228
286
else if kcode == 127 then // forward- delete
287
+ keyPress = " "
229
288
if cursorX >= data[cursorY].len then
230
289
if cursorY < data.len- 1 then
231
290
data[cursorY] = data[cursorY] + data[cursorY+ 1 ]
@@ -241,6 +300,7 @@ handleKey = function(k)
241
300
refreshLine cursorY
242
301
end if
243
302
else if kcode == 8 then // backspace
303
+ keyPress = " "
244
304
if cursorX > 0 then
245
305
endPos = cursorX
246
306
outer.cursorX = cursorX - 1
@@ -257,14 +317,13 @@ handleKey = function(k)
257
317
outer.cursorX = x
258
318
refreshDisplay
259
319
end if
260
- else if kcode == 27 or kcode == 24 then // Escape or Ctrl + X
320
+ else if kcode == 27 then // Escape
261
321
outer.quitting = true
262
- else if kcode == 9 then // tab
263
- // for now, we' ll just have tab insert one or two spaces
264
- handleKey " "
265
- if cursorX % 2 then handleKey " "
266
- return
267
- else if kcode > 31 then // printable key
322
+ else if k >= " " then // printable key
323
+ anyAlt = key.pressed(" left alt" ) or key.pressed(" right alt" )
324
+ anyShift = key.pressed(" left shift" ) or key.pressed(" right shift" )
325
+ byWord = anyCtrl or anyAlt
326
+
268
327
if cursorY >= data.len then
269
328
data.push k
270
329
else if not data[cursorY] then
@@ -275,22 +334,32 @@ handleKey = function(k)
275
334
outer.cursorX = cursorX + 1
276
335
refreshLine cursorY
277
336
end if
337
+
278
338
limitCursor
279
339
scrollCursorIntoView
280
340
drawTopBar
281
- showCursor
282
-
283
- // for development & debugging:
284
- // text.row = screenH-1; text.column = screenW/2
285
- // text.backColor = pal.topBar; text.color = "#888888"
286
- // print kcode + (" byWord" * byWord + " " * (not byWord))
287
-
341
+ showCursor
288
342
end function
289
343
290
344
update = function ()
291
345
wantCursor = (time % 1 < 0.75 )
292
346
if wantCursor != cursorShown then showCursor wantCursor
293
- if key.available then handleKey key.get
347
+ if key.available then
348
+ handleKey key.get
349
+ else if key.pressed(" left ctrl" ) or key.pressed(" right ctrl" ) then
350
+ for c in " 1234567890[]/=\;-',.`"
351
+ if key.pressed(c) then
352
+ while key.pressed(c); yield; end while
353
+ handleControlKey c
354
+ end if
355
+ end for
356
+ else if key.pressed(" page up" ) then
357
+ while key.pressed(" page up" ); yield; end while
358
+ handleKey " U" , true
359
+ else if key.pressed(" page down" ) then
360
+ while key.pressed(" page down" ); yield; end while
361
+ handleKey " D" , true
362
+ end if
294
363
end function
295
364
296
365
// editText: main entry point.
@@ -299,7 +368,8 @@ editText = function(textToEdit)
299
368
if textToEdit == null then textToEdit = []
300
369
if textToEdit.len == 0 then textToEdit.push " "
301
370
outer.data = textToEdit
302
-
371
+ // We don' t handle tabs properly yet, so let' s just:
372
+ for i in data.indexes; data[i] = data[i].replace(TAB, " " ); end for
303
373
origColor = text.color
304
374
origBackColor = text.backColor
305
375
text.backColor = color.clear
@@ -308,13 +378,18 @@ editText = function(textToEdit)
308
378
drawTopBar
309
379
refreshDisplay
310
380
381
+ if not env.hasIndex(" bootOpts" ) then env.bootOpts = {}
382
+ env.bootOpts.controlC = false // disable Control- C to break
383
+
311
384
outer.quitting = false
312
385
while not quitting
313
386
yield
314
387
update
315
388
end while
316
389
key.clear
317
390
391
+ env.bootOpts.controlC = true // re- eable Control- C to break
392
+
318
393
text.delimiter = char(13 )
319
394
text.color = origColor
320
395
text.backColor = origBackColor
0 commit comments