From caf67b9a9dd495dd6c252fb65f195ca510d5ed67 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Fri, 3 Jan 2020 13:57:18 -0800 Subject: [PATCH] Single line yanking working --- hermes.c | 111 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 40 deletions(-) diff --git a/hermes.c b/hermes.c index 1d3cb49..7e34f4c 100644 --- a/hermes.c +++ b/hermes.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -671,6 +672,20 @@ void editorScroll() { } } +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); + + if (E.mode == VisualMode && cond) { + int color = end ? EDITOR_BG_COLOR : VISUAL_BG_COLOR; + if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) + color = end ? VISUAL_BG_COLOR : EDITOR_BG_COLOR; + char vbuf[16]; + int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", color); + abAppend(ab, vbuf, vclen); + } +} + void editorDrawRows(struct abuf *ab) { int y; for (y = 0; y < E.screenRows; y++) { @@ -727,21 +742,9 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "\033[39m", clen); current_color = -1; } - if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { - int col = VISUAL_BG_COLOR; - if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) col = EDITOR_BG_COLOR; - char vbuf[16]; - int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col); - abAppend(ab, vbuf, vclen); - } + editorDrawVisualBg(ab, 0, j, filerow, len); abAppend(ab, &c[j], 1); - if (E.mode == VisualMode && (j + E.coloff == E.cx || E.cx == len || !filerow) && filerow == E.cy) { - int col = EDITOR_BG_COLOR; - if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) col = VISUAL_BG_COLOR; - char vbuf[16]; - int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col); - abAppend(ab, vbuf, vclen); - } + editorDrawVisualBg(ab, 1, j, filerow, len); } else { int color = editorSyntaxToColor(hl[j]); if (color != current_color) { @@ -750,21 +753,9 @@ void editorDrawRows(struct abuf *ab) { int clen = snprintf(buf, sizeof(buf), "\033[38;5;%dm", color); abAppend(ab, buf, clen); } - if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { - int col = VISUAL_BG_COLOR; - if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) col = EDITOR_BG_COLOR; - char vbuf[16]; - int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col); - abAppend(ab, vbuf, vclen); - } + editorDrawVisualBg(ab, 0, j, filerow, len); abAppend(ab, &c[j], 1); - if (E.mode == VisualMode && (j + E.coloff == E.cx || E.cx == len || !filerow) && filerow == E.cy) { - int col = EDITOR_BG_COLOR; - if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) col = VISUAL_BG_COLOR; - char vbuf[16]; - int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col); - abAppend(ab, vbuf, vclen); - } + editorDrawVisualBg(ab, 1, j, filerow, len); } } abAppend(ab, "\033[39m", 5); @@ -954,6 +945,23 @@ void editorEndOfWord() { } } +void editorCopySelection() { + if (E.cy == E.vy) { + int sx = E.vx; + int ex = E.cx; + if (E.cx < E.vx) { + sx = E.cx; + ex = E.vx; + } + int len = ex - sx + 1; + char *buf = malloc(len); + memcpy(buf, &E.row[E.cy].chars[sx], len); + editorUpdatePaste(buf, len); + free(buf); + } + // TODO multiline copy... been having problems +} + // // input // ----- @@ -1113,6 +1121,7 @@ void editorCommandKp(int c) { 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; @@ -1127,6 +1136,7 @@ void editorCommandKp(int c) { } else { E.cy = 0; E.cx = 0; + E.rowoff = 0; } break; case 'G': @@ -1205,15 +1215,13 @@ void editorCommandKp(int c) { if (c == 'a') editorMoveCursor('l'); break; case 'd': - if (!deleting) { - if (E.mode == VisualMode) { - editorDeleteSelection(); - E.mode = CommandMode; - break; - } else { - deleting++; - return; - } + if (E.mode == VisualMode) { + editorDeleteSelection(); + E.mode = CommandMode; + break; + } else if (!deleting) { + deleting++; + return; } else { int sz = E.row[E.cy].size + 1; char *buf = malloc(sz); @@ -1225,6 +1233,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; counter--; } break; @@ -1282,7 +1291,7 @@ void editorCommandKp(int c) { } else { E.mode = InputMode; if (open == 'o') editorMoveCursor(ARROW_RIGHT); - } + } for (int i = 0; i < E.pastelen; i++) { editorInputKp(E.paste[i]); } @@ -1352,6 +1361,14 @@ void editorCommandKp(int c) { E.mode = VisualMode; } break; + case 'y': + if (E.mode == VisualMode) { + editorCopySelection(); + E.mode = CommandMode; + E.cy = E.vy; + E.cx = E.vx; + } + break; case '0': case '1': case '2': @@ -1433,6 +1450,20 @@ void editorProcessKeypress() { } } +void updateWindowSize(void) { + if (getWindowSize(&E.screenRows, &E.screenCols) == -1) + die("Screen resize"); + E.screenRows -= 2; +} + +void handleSigWinCh(int unused __attribute__((unused))) { + updateWindowSize(); + if (E.cy > E.screenRows) E.cy = E.screenRows - 1; + if (E.cx > E.screenCols) E.cx = E.screenCols - 1; + // TODO does rowoff need to be set here? + editorRefreshScreen(); +} + // // init // ---- @@ -1458,8 +1489,8 @@ void initEditor() { E.syntax = NULL; E.statusmsg[0] = '\0'; - if (getWindowSize(&E.screenRows, &E.screenCols) == -1) die("getWindowSize"); - E.screenRows -= 2; + updateWindowSize(); + signal(SIGWINCH, handleSigWinCh); } int main(int argc, char *argv[]) {