From 39f37d7ffd80cad13bd8c765cb5f79180be17740 Mon Sep 17 00:00:00 2001 From: sloum Date: Tue, 14 Jan 2020 21:22:28 -0800 Subject: [PATCH] Reworked multipliers and deletion --- hermes.c | 220 ++++++++++++++++++++++++++----------------------------- 1 file changed, 104 insertions(+), 116 deletions(-) diff --git a/hermes.c b/hermes.c index 2b27ac5..ae107e8 100644 --- a/hermes.c +++ b/hermes.c @@ -1157,9 +1157,28 @@ void editorCommandKp(int c) { case 'k': case 'h': case 'l': - while (counter > 0) { - editorMoveCursor(c); - counter--; + { + int vx = E.cx; + int vy = E.cy; + while (counter > 0) { + editorMoveCursor(c); + counter--; + } + if (!deleting) break; + E.vx = vx; + E.vy = vy; + if (c == 'h' && E.vx > 0) E.vx--; + if (c == 'j') { + E.vx = 0; + E.cx = E.row[E.cy].size - 1; + } + if (c == 'k') { + E.vx = E.row[E.vy].size - 1; + E.cx = 0; + } + if (c == 'l' && E.cx > 0) E.cx--; + editorDeleteSelection(); + if (c == 'k' || c == 'j') editorDelRow(E.cy); } break; case '\033': @@ -1169,6 +1188,7 @@ void editorCommandKp(int c) { { int c = editorReadKey(); if (c) { + if (c == '\033') break; E.mode = InputMode; editorMoveCursor(ARROW_RIGHT); editorDeleteChar(); @@ -1179,67 +1199,42 @@ void editorCommandKp(int c) { } break; case 'g': - if (deleting) { - int totlen = 0; - int j; - for (j = 0; j <= E.cy; j++) - totlen += E.row[j].size + 1; - - char *buf = malloc(totlen); - char *p = buf; - - int count = E.cy; - E.cy = 0; - E.rowoff = 0; - while (count >= 0) { - memcpy(p, E.row[E.cy].chars, E.row[E.cy].size); - p += E.row[E.cy].size; - *p = '\r'; - p++; - - deleting = 1; - editorCommandKp('d'); - count--; - } - editorUpdatePaste(buf, totlen); - } else { + { + int vx = E.cx; + int vy = E.cy; E.cy = 0; E.cx = 0; E.rowoff = 0; + if (!deleting) break; + E.vx = vx; + E.vy = vy; + int extraRow = 0; + E.vx--; + if (E.vx < 0) { + if (E.vy < 1) break; + E.vy--; + E.vx = E.row[E.vy].size - 1; + extraRow = 1; + } + editorDeleteSelection(); + if (extraRow) editorDelRow(0); } break; case 'G': - if (deleting) { - int totlen = 0; - int j; - for (j = E.cy; j < E.numRows; j++) - totlen += E.row[j].size + 1; - - char *buf = malloc(totlen); - char *p = buf; - - int count = E.numRows - E.cy - 1; - if (count < 0) count = 0; - while (count >= 0) { - memcpy(p, E.row[E.cy].chars, E.row[E.cy].size); - p += E.row[E.cy].size; - *p = '\r'; - p++; - - deleting = 1; - editorCommandKp('d'); - count--; - } - editorUpdatePaste(buf, totlen); - } else { - if (E.numRows > E.screenRows) { - E.cy = E.numRows - 1; - E.rowoff = E.numRows - E.screenRows - 1; - } else { - E.cy = E.numRows - 1; - } + { + int vx = E.cx; + int vy = E.cy; + E.cy = E.numRows - 1; E.cx = E.row[E.cy].size - 1; if (E.cx < 0) E.cx = 0; + + if (!deleting) { + if (E.numRows > E.screenRows) E.rowoff = E.numRows - E.screenRows - 1; + break; + } + E.vx = vx; + E.vy = vy; + editorDeleteSelection(); } break; case '\r': @@ -1247,8 +1242,6 @@ void editorCommandKp(int c) { counter--; if (counter > E.numRows - 1) { E.cy = E.numRows - 1; - } else if (counter < 0) { - E.cy = 0; } else { E.cy = counter; } @@ -1293,19 +1286,21 @@ void editorCommandKp(int c) { deleting++; return; } else { - int sz = E.row[E.cy].size + 1; - char *buf = malloc(sz); - strcpy(buf, E.row[E.cy].chars); - buf[sz - 1] = '\r'; - editorUpdatePaste(buf, sz); - free(buf); + int start_x = E.cx; + int start_y = E.cy; + int end_x, end_y; + end_y = E.cy + counter; + if (end_y >= E.numRows) end_y = E.numRows - 1; + end_x = E.row[end_y].size - 1; + editorCopyRange(0, start_y, end_x, end_y); while (counter > 0) { 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 = E.row[E.cy].size; counter--; } + E.cx = start_x; + if (E.cx >= E.row[E.cy].size) E.cx = E.row[E.cy].size - 1; break; } case '$': @@ -1329,26 +1324,23 @@ void editorCommandKp(int c) { break; case 'w': { - int start_x = E.cx; - int start_y = E.cy; + int vx = E.cx; + int vy = E.cy; while (counter > 0) { editorNextWord(); counter--; } if (!deleting) break; - int end_x, end_y; + E.vx = vx; + E.vy = vy; if (E.cx == 0) { if (E.cy == 0) break; - end_y = E.cy - 1; - end_x = E.row[E.cy].size - 1; + E.cy--; + E.cx = E.row[E.cy].size - 1; } else { - end_x = E.cx - 1; - end_y = E.cy; - } - editorCopyRange(start_x, start_y, end_x, end_y); - while (E.cx != start_x || E.cy != start_y) { - editorDeleteChar(); + E.cx--; } + editorDeleteSelection(); if (E.cx > E.row[E.cy].size - 1) E.cx = E.row[E.cy].size - 1; } break; @@ -1378,54 +1370,50 @@ void editorCommandKp(int c) { break; } case 'b': - while (counter > 0) { - if (deleting) { - int start_x = E.cx; - int start_y = E.cy; - editorPrevWord(); - int end_x = E.cx; - int end_y = E.cy; - E.cx = start_x; - E.cy = start_y; - editorUpdatePaste(&E.row[E.cy].chars[end_x], start_x - end_x); - while (E.cy != end_y || E.cx != end_x) { - editorDeleteChar(); - } - } else { - editorPrevWord(); + { + int start_x = E.cx; + int start_y = E.cy; + if (start_x > 0) { + start_x--; + } else if (start_y > 0) { + start_y--; + start_x = E.row[start_y].size - 1; } - counter--; + while (counter > 0) { + editorPrevWord(); + counter--; + } + if (!deleting) break; + + E.vx = start_x; + E.vy = start_y; + editorDeleteSelection(); } break; case 'e': - while (counter > 0) { - if (deleting) { - int start_x = E.cx; - int start_y = E.cy; - editorEndOfWord(); - editorUpdatePaste(&E.row[E.cy].chars[start_x], E.cx - start_x); - while (E.cy != start_y || E.cx != start_x) { - editorDeleteChar(); - } - E.mode = InputMode; - editorMoveCursor(ARROW_RIGHT); - editorDeleteChar(); - E.mode = CommandMode; - if (E.cx == E.row[E.cy].size) editorMoveCursor(ARROW_LEFT); - } else { + { + int vx = E.cx; + int vy = E.cy; + while (counter > 0) { editorEndOfWord(); + counter--; } - counter--; + if (!deleting) break; + E.vx = vx; + E.vy = vy; + editorDeleteSelection(); } break; case '^': - if (deleting) { - editorUpdatePaste(&E.row[E.cy].chars[0], E.cx); - while (E.cx > 0) { - editorDeleteChar(); - } - } else { + { + int vx = E.cx; E.cx = 0; + if (deleting) { + E.vx = vx; + E.vy = E.cy; + if (E.vx > 0) E.vx--; + editorDeleteSelection(); + } } break; case 'v': @@ -1437,9 +1425,9 @@ void editorCommandKp(int c) { break; case 'V': if (E.mode == CommandMode) { - E.vx = 0; + E.vx = E.row[E.cy].size - 1; E.vy = E.cy; - E.cx = E.row[E.cy].size - 1; + E.cx = 0; E.mode = VisualMode; } break;