Started copy range function but seg faulting

This commit is contained in:
sloumdrone 2020-01-06 23:15:30 -08:00
parent 8961833761
commit 30e62ff94c
1 changed files with 47 additions and 17 deletions

View File

@ -937,25 +937,53 @@ void editorEndOfWord() {
} }
} }
void editorCopySelection() { void editorCopyRange(int sx, int sy, int ex, int ey) {
if (E.cy == E.vy) { // TODO fix this function. Keeps freezing up. Not sure why.
int sx = E.vx; int swap;
int ex = E.cx; if (ey < sy || (sy == ey && ex < sx)) {
if (E.cx < E.vx) { swap = sy;
sx = E.cx; sy = ey;
ex = E.vx; ey = swap;
} swap = sx;
int len = ex - sx + 1; sx = ex;
char *buf = malloc(len); ex = swap;
memcpy(buf, &E.row[E.cy].chars[sx], len); }
editorUpdatePaste(buf, len); if (sy < 0 || ey > E.numRows) return;
free(buf);
int totlen;
for (int y = sy; y <= ey; y++) {
totlen += E.row[y].size;
if (y == sy) totlen -= sx;
if (y == ey) totlen -= (E.row[y].size - ex);
} }
// TODO multiline copy... been having problems totlen += ey - sy;
char *buf = malloc(totlen);
if (ey - sy == 0) {
memcpy(buf, &E.row[sy].chars[sx], totlen);
} else {
char *p = buf;
for (int row = sy; row <= ey; row++) {
if (row == sy) {
memcpy(p, &E.row[row].chars[sx], E.row[row].size - sx);
p += E.row[row].size - sx;
*p = '\r';
p++;
} else if (row == ey) {
memcpy(p, &E.row[row].chars, E.row[row].size - ex);
} else {
memcpy(p, E.row[row].chars, E.row[row].size);
p += E.row[row].size;
*p = '\r';
p++;
}
}
}
editorUpdatePaste(buf, totlen);
free(buf);
} }
void editorDeleteSelection() { void editorDeleteSelection() {
editorCopySelection();
int sx = E.cx; int sx = E.cx;
int sy = E.cy; int sy = E.cy;
int ex = E.vx; int ex = E.vx;
@ -968,6 +996,7 @@ void editorDeleteSelection() {
E.cx = sx; E.cx = sx;
E.cy = sy; E.cy = sy;
} }
editorCopyRange(sx, sy, ex, ey);
if (sx < E.row[sy].size) if (sx < E.row[sy].size)
E.cx++; E.cx++;
@ -1307,10 +1336,11 @@ void editorCommandKp(int c) {
break; break;
case 'P': case 'P':
case 'p': case 'p':
if (E.pastelen <= 0) break;
{ {
char open = c == 'p' ? 'o' : 'O'; char open = c == 'p' ? 'o' : 'O';
int startx = E.cx; int startx = E.cx;
int multiline = 0; int multiline = 1;
if (E.paste && E.paste[E.pastelen-1] == '\r') multiline = 1; if (E.paste && E.paste[E.pastelen-1] == '\r') multiline = 1;
if (multiline) { if (multiline) {
editorCommandKp(open); editorCommandKp(open);
@ -1397,7 +1427,7 @@ void editorCommandKp(int c) {
break; break;
case 'y': case 'y':
if (E.mode == VisualMode) { if (E.mode == VisualMode) {
editorCopySelection(); editorCopyRange(E.cx, E.cy, E.vx, E.vy);
E.mode = CommandMode; E.mode = CommandMode;
E.cy = E.vy; E.cy = E.vy;
E.cx = E.vx; E.cx = E.vx;