Updates file opening for nonexistent files and fixes cursor binding to right edge on line deletion

This commit is contained in:
sloum 2020-01-12 22:01:17 -08:00
parent 96d5cf4dc4
commit c4558d4329
1 changed files with 19 additions and 5 deletions

View File

@ -574,7 +574,7 @@ void editorOpen(char *filepath) {
editorSelectSyntaxHighlight(); editorSelectSyntaxHighlight();
FILE *fp = fopen(filepath, "r"); FILE *fp = fopen(filepath, "a+");
if (!fp) die("Open file"); if (!fp) die("Open file");
char *line = NULL; char *line = NULL;
@ -672,6 +672,16 @@ void editorScroll() {
} }
} }
void editorAdjustCursorX() {
erow *row = (E.cy >= E.numRows) ? NULL : &E.row[E.cy];
int rowlen = row ? row->size : 0;
if (E.cx > rowlen) {
E.cx = rowlen;
if (E.mode == CommandMode && rowlen > 0) E.cx--;
}
}
void editorDrawVisualBg(struct abuf *ab, int end, int drawx, int drawy, int rowlen) { void editorDrawVisualBg(struct abuf *ab, int end, int drawx, int drawy, int rowlen) {
int cond = end ? ((drawx + E.coloff == E.cx || E.cx == rowlen) && drawy == E.cy) : int cond = end ? ((drawx + E.coloff == E.cx || E.cx == rowlen) && drawy == E.cy) :
(drawx + E.coloff == E.vx && drawy == E.vy); (drawx + E.coloff == E.vx && drawy == E.vy);
@ -703,8 +713,10 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
padding--; padding--;
} }
while (padding--) abAppend(ab, " ", 1); if (E.filename == NULL) {
abAppend(ab, welcome, welcomelen); while (padding--) abAppend(ab, " ", 1);
abAppend(ab, welcome, welcomelen);
}
} else { } else {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
@ -827,7 +839,8 @@ void editorDrawMsgBar(struct abuf *ab) {
} }
void editorRefreshScreen() { void editorRefreshScreen() {
editorScroll(); editorAdjustCursorX();
editorScroll();
struct abuf ab = ABUF_INIT; struct abuf ab = ABUF_INIT;
@ -1271,6 +1284,7 @@ void editorCommandKp(int c) {
if (c == 'a') editorMoveCursor('l'); if (c == 'a') editorMoveCursor('l');
break; break;
case 'd': case 'd':
if (E.numRows < 1) break;
if (E.mode == VisualMode) { if (E.mode == VisualMode) {
editorDeleteSelection(); editorDeleteSelection();
E.mode = CommandMode; E.mode = CommandMode;
@ -1289,7 +1303,7 @@ void editorCommandKp(int c) {
editorDelRow(E.cy); editorDelRow(E.cy);
if (E.cy == E.numRows) E.cy--; if (E.cy == E.numRows) E.cy--;
if (E.cy < 0) E.cy = 0; if (E.cy < 0) E.cy = 0;
if (E.cx >= E.row[E.cy].size) E.cx = 0; // if (E.cx >= E.row[E.cy].size) E.cx = E.row[E.cy].size;
counter--; counter--;
} }
break; break;