From c4558d43298e0517c4648e5f03fe45168a7ddc33 Mon Sep 17 00:00:00 2001 From: sloum Date: Sun, 12 Jan 2020 22:01:17 -0800 Subject: [PATCH] Updates file opening for nonexistent files and fixes cursor binding to right edge on line deletion --- hermes.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/hermes.c b/hermes.c index 9e9ba89..2b27ac5 100644 --- a/hermes.c +++ b/hermes.c @@ -574,7 +574,7 @@ void editorOpen(char *filepath) { editorSelectSyntaxHighlight(); - FILE *fp = fopen(filepath, "r"); + FILE *fp = fopen(filepath, "a+"); if (!fp) die("Open file"); 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) { int cond = end ? ((drawx + E.coloff == E.cx || E.cx == rowlen) && drawy == E.cy) : (drawx + E.coloff == E.vx && drawy == E.vy); @@ -703,8 +713,10 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); padding--; } - while (padding--) abAppend(ab, " ", 1); - abAppend(ab, welcome, welcomelen); + if (E.filename == NULL) { + while (padding--) abAppend(ab, " ", 1); + abAppend(ab, welcome, welcomelen); + } } else { abAppend(ab, "~", 1); } @@ -827,7 +839,8 @@ void editorDrawMsgBar(struct abuf *ab) { } void editorRefreshScreen() { - editorScroll(); + editorAdjustCursorX(); + editorScroll(); struct abuf ab = ABUF_INIT; @@ -1271,6 +1284,7 @@ void editorCommandKp(int c) { if (c == 'a') editorMoveCursor('l'); break; case 'd': + if (E.numRows < 1) break; if (E.mode == VisualMode) { editorDeleteSelection(); E.mode = CommandMode; @@ -1289,7 +1303,7 @@ void editorCommandKp(int c) { editorDelRow(E.cy); if (E.cy == E.numRows) E.cy--; 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--; } break;