Fixed selection code and deletion

This commit is contained in:
sloumdrone 2020-01-02 19:47:02 -08:00
parent dfcceadd04
commit 4667adbf8c
1 changed files with 44 additions and 7 deletions

View File

@ -698,8 +698,10 @@ void editorDrawRows(struct abuf *ab) {
unsigned char *hl = &E.row[filerow].hl[E.coloff]; unsigned char *hl = &E.row[filerow].hl[E.coloff];
int current_color = -1; int current_color = -1;
if (E.mode == VisualMode && len <= 0 && filerow == E.cy) { if (E.mode == VisualMode && len <= 0 && 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]; char vbuf[16];
int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", EDITOR_BG_COLOR); int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col);
abAppend(ab, vbuf, vclen); abAppend(ab, vbuf, vclen);
} }
int j; int j;
@ -726,14 +728,18 @@ void editorDrawRows(struct abuf *ab) {
current_color = -1; current_color = -1;
} }
if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { 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]; char vbuf[16];
int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", VISUAL_BG_COLOR); int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col);
abAppend(ab, vbuf, vclen); 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) { 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]; char vbuf[16];
int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", EDITOR_BG_COLOR); int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col);
abAppend(ab, vbuf, vclen); abAppend(ab, vbuf, vclen);
} }
} else { } else {
@ -745,14 +751,18 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, buf, clen); abAppend(ab, buf, clen);
} }
if (E.mode == VisualMode && j + E.coloff == E.vx && filerow == E.vy) { 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]; char vbuf[16];
int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", VISUAL_BG_COLOR); int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col);
abAppend(ab, vbuf, vclen); 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) { 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]; char vbuf[16];
int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", EDITOR_BG_COLOR); int vclen = snprintf(vbuf, sizeof(vbuf), "\033[48;5;%dm", col);
abAppend(ab, vbuf, vclen); abAppend(ab, vbuf, vclen);
} }
} }
@ -895,6 +905,27 @@ void editorNextWord() {
} }
} }
void editorDeleteSelection() {
int sx = E.cx;
int sy = E.cy;
int ex = E.vx;
int ey = E.vy;
if (E.cy < E.vy || (E.cx < E.vx && E.cy == E.vy)) {
sx = E.vx;
sy = E.vy;
ex = E.cx;
ey = E.cy;
E.cx = sx;
E.cy = sy;
}
if (sx < E.row[sy].size)
E.cx++;
while (E.cx != ex || E.cy != ey) {
editorDeleteChar();
}
}
void editorEndOfWord() { void editorEndOfWord() {
int x = E.cx; int x = E.cx;
int y = E.cy; int y = E.cy;
@ -1175,8 +1206,14 @@ void editorCommandKp(int c) {
break; break;
case 'd': case 'd':
if (!deleting) { if (!deleting) {
deleting++; if (E.mode == VisualMode) {
return; editorDeleteSelection();
E.mode = CommandMode;
break;
} else {
deleting++;
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);