Skip to content

Commit 64d60eb

Browse files
committed
Check if buffer will overflow before adding a new line.
1 parent 462d3c4 commit 64d60eb

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/editor.bas

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ PROC InsertChar
168168
inc linLen
169169
ENDPROC
170170

171+
'-------------------------------------
172+
' Undo editing current line
173+
PROC UndoEditLine
174+
edited = 0
175+
put @@ATBEL
176+
exec CopyToEdit
177+
exec ForceDrawCurrentLine
178+
ENDPROC
179+
171180
'-------------------------------------
172181
' Save line being edited
173182
'
@@ -178,12 +187,20 @@ PROC SaveLine
178187
ptr = ScrAdr(lDraw+1) - 1
179188
newPtr = nptr - ptr
180189

190+
' Check if we have enough space in the buffer for the new file
191+
' TODO: This check will fail if the buffer is bigger than 32kB,
192+
' because the right side will be negative.
193+
if newPtr > dpeek(@MEMTOP) - MemEnd
194+
exec UndoEditLine
195+
exit
196+
endif
197+
198+
MemEnd = MemEnd + newPtr
181199
if newPtr < 0
182-
move ptr, nptr, MemEnd - ptr
200+
move ptr, nptr, MemEnd - nptr
183201
elif newPtr > 0
184-
-move ptr, nptr, MemEnd - ptr
202+
-move ptr, nptr, MemEnd - nptr
185203
endif
186-
MemEnd = MemEnd + newPtr
187204

188205
' Copy new line
189206
ptr = ScrAdr(lDraw)
@@ -303,8 +320,8 @@ ENDPROC
303320
'-------------------------------------
304321
' Moves the cursor down 1 line
305322
PROC CursorDown
323+
exec SaveLine
306324
if scrLine = 22
307-
exec SaveLine
308325
exec ScrollUp
309326
else
310327
inc scrLine
@@ -315,11 +332,11 @@ ENDPROC
315332
'-------------------------------------
316333
' Moves the cursor up 1 line
317334
PROC CursorUp
335+
exec SaveLine
318336
if scrLine
319337
dec scrLine
320338
dec line
321339
else
322-
exec SaveLine
323340
exec ScrollDown
324341
endif
325342
ENDPROC
@@ -827,13 +844,7 @@ PROC ProcessKeys
827844
'
828845
'--------- Control-Z (undo) -----
829846
elif key = $1A
830-
if edited
831-
edited = 0
832-
exec CopyToEdit
833-
exec ForceDrawCurrentLine
834-
else
835-
put @@ATBEL
836-
endif
847+
exec UndoEditLine
837848
'
838849
'--------- Escape ---------------
839850
elif key = $1B

0 commit comments

Comments
 (0)