Single line yanking working

This commit is contained in:
Brian Evans 2020-01-03 13:57:18 -08:00
parent 7d3dba06c3
commit caf67b9a9d
1 changed files with 71 additions and 40 deletions

111
hermes.c
View File

@ -8,6 +8,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
@ -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) { void editorDrawRows(struct abuf *ab) {
int y; int y;
for (y = 0; y < E.screenRows; y++) { for (y = 0; y < E.screenRows; y++) {
@ -727,21 +742,9 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "\033[39m", clen); abAppend(ab, "\033[39m", clen);
current_color = -1; current_color = -1;
} }
if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { editorDrawVisualBg(ab, 0, j, filerow, len);
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);
}
abAppend(ab, &c[j], 1); abAppend(ab, &c[j], 1);
if (E.mode == VisualMode && (j + E.coloff == E.cx || E.cx == len || !filerow) && filerow == E.cy) { editorDrawVisualBg(ab, 1, j, filerow, len);
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);
}
} else { } else {
int color = editorSyntaxToColor(hl[j]); int color = editorSyntaxToColor(hl[j]);
if (color != current_color) { if (color != current_color) {
@ -750,21 +753,9 @@ void editorDrawRows(struct abuf *ab) {
int clen = snprintf(buf, sizeof(buf), "\033[38;5;%dm", color); int clen = snprintf(buf, sizeof(buf), "\033[38;5;%dm", color);
abAppend(ab, buf, clen); abAppend(ab, buf, clen);
} }
if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { editorDrawVisualBg(ab, 0, j, filerow, len);
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);
}
abAppend(ab, &c[j], 1); abAppend(ab, &c[j], 1);
if (E.mode == VisualMode && (j + E.coloff == E.cx || E.cx == len || !filerow) && filerow == E.cy) { editorDrawVisualBg(ab, 1, j, filerow, len);
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);
}
} }
} }
abAppend(ab, "\033[39m", 5); 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 // input
// ----- // -----
@ -1113,6 +1121,7 @@ void editorCommandKp(int c) {
int count = E.cy; int count = E.cy;
E.cy = 0; E.cy = 0;
E.rowoff = 0;
while (count >= 0) { while (count >= 0) {
memcpy(p, E.row[E.cy].chars, E.row[E.cy].size); memcpy(p, E.row[E.cy].chars, E.row[E.cy].size);
p += E.row[E.cy].size; p += E.row[E.cy].size;
@ -1127,6 +1136,7 @@ void editorCommandKp(int c) {
} else { } else {
E.cy = 0; E.cy = 0;
E.cx = 0; E.cx = 0;
E.rowoff = 0;
} }
break; break;
case 'G': case 'G':
@ -1205,15 +1215,13 @@ void editorCommandKp(int c) {
if (c == 'a') editorMoveCursor('l'); if (c == 'a') editorMoveCursor('l');
break; break;
case 'd': case 'd':
if (!deleting) { if (E.mode == VisualMode) {
if (E.mode == VisualMode) { editorDeleteSelection();
editorDeleteSelection(); E.mode = CommandMode;
E.mode = CommandMode; break;
break; } else if (!deleting) {
} else { deleting++;
deleting++; return;
return;
}
} else { } else {
int sz = E.row[E.cy].size + 1; int sz = E.row[E.cy].size + 1;
char *buf = malloc(sz); char *buf = malloc(sz);
@ -1225,6 +1233,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;
counter--; counter--;
} }
break; break;
@ -1282,7 +1291,7 @@ void editorCommandKp(int c) {
} else { } else {
E.mode = InputMode; E.mode = InputMode;
if (open == 'o') editorMoveCursor(ARROW_RIGHT); if (open == 'o') editorMoveCursor(ARROW_RIGHT);
} }
for (int i = 0; i < E.pastelen; i++) { for (int i = 0; i < E.pastelen; i++) {
editorInputKp(E.paste[i]); editorInputKp(E.paste[i]);
} }
@ -1352,6 +1361,14 @@ void editorCommandKp(int c) {
E.mode = VisualMode; E.mode = VisualMode;
} }
break; break;
case 'y':
if (E.mode == VisualMode) {
editorCopySelection();
E.mode = CommandMode;
E.cy = E.vy;
E.cx = E.vx;
}
break;
case '0': case '0':
case '1': case '1':
case '2': 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 // init
// ---- // ----
@ -1458,8 +1489,8 @@ void initEditor() {
E.syntax = NULL; E.syntax = NULL;
E.statusmsg[0] = '\0'; E.statusmsg[0] = '\0';
if (getWindowSize(&E.screenRows, &E.screenCols) == -1) die("getWindowSize"); updateWindowSize();
E.screenRows -= 2; signal(SIGWINCH, handleSigWinCh);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {