Fixed crashing with cursor down at end of file. Belt and braces.

This commit is contained in:
Bobbi Webber-Manners 2020-07-13 00:05:15 -04:00
parent 2c0c746b9f
commit 1f338e98ca
1 changed files with 19 additions and 6 deletions

View File

@ -3,7 +3,6 @@
// Bobbi July 2020
/////////////////////////////////////////////////////////////////////////////
// TODO: it is possible to scroll off end of doc
// TODO: Convert tabs to spaces in load_file()
// TODO: The code doesn't check for error cases when calling gap buffer
// functions.
@ -218,6 +217,11 @@ void draw_screen(void) {
uint16_t startpos;
uint8_t rowsabove, cursorrow;
// Initialize all rows to length 1 to cover those rows that may have
// no text and would otherwise be unitialized.
for (rowsabove = 0; rowsabove < NROWS; ++rowsabove)
rowlen[rowsabove] = 1;
// First we have to scan back to work out where in the buffer to
// start drawing on the screen at the top left. This is at most
// CURSORROW * NCOLS chars, however we go a little bit further back
@ -491,14 +495,21 @@ void cursor_down(void) {
return;
}
}
for (i = 0; i < rowlen[cursrow] - curscol; ++i)
gapbuf[gapbegin++] = gapbuf[++gapend];
for (i = 0; i < rowlen[cursrow] - curscol; ++i) {
if (gapbegin < DATASIZE()) /// THIS STOPS IT CRASHING BUT MISALIGNED AFTER
gapbuf[gapbegin++] = gapbuf[++gapend];
else {
putchar(BELL);
return;
}
}
++cursrow;
// Short line ...
if (curscol >= rowlen[cursrow])
curscol = rowlen[cursrow] - 1;
for (i = 0; i < curscol; ++i)
gapbuf[gapbegin++] = gapbuf[++gapend];
if (gapbegin < DATASIZE()) /// THIS STOPS IT CRASHING BUT MISALIGNED AFTER
gapbuf[gapbegin++] = gapbuf[++gapend];
gotoxy(curscol, cursrow);
}
@ -602,8 +613,10 @@ int main() {
break;
default:
//printf("**%02x**", c);
insert_char(c);
update_after_insert_char();
if ((c >= 0x20) && (c < 0x80) || (c == EOL)) {
insert_char(c);
update_after_insert_char();
}
}
}
}