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() {
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);
void editorCopyRange(int sx, int sy, int ex, int ey) {
// TODO fix this function. Keeps freezing up. Not sure why.
int swap;
if (ey < sy || (sy == ey && ex < sx)) {
swap = sy;
sy = ey;
ey = swap;
swap = sx;
sx = ex;
ex = swap;
}
if (sy < 0 || ey > E.numRows) return;
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() {
editorCopySelection();
int sx = E.cx;
int sy = E.cy;
int ex = E.vx;
@ -968,6 +996,7 @@ void editorDeleteSelection() {
E.cx = sx;
E.cy = sy;
}
editorCopyRange(sx, sy, ex, ey);
if (sx < E.row[sy].size)
E.cx++;
@ -1307,10 +1336,11 @@ void editorCommandKp(int c) {
break;
case 'P':
case 'p':
if (E.pastelen <= 0) break;
{
char open = c == 'p' ? 'o' : 'O';
int startx = E.cx;
int multiline = 0;
int multiline = 1;
if (E.paste && E.paste[E.pastelen-1] == '\r') multiline = 1;
if (multiline) {
editorCommandKp(open);
@ -1397,7 +1427,7 @@ void editorCommandKp(int c) {
break;
case 'y':
if (E.mode == VisualMode) {
editorCopySelection();
editorCopyRange(E.cx, E.cy, E.vx, E.vy);
E.mode = CommandMode;
E.cy = E.vy;
E.cx = E.vx;