Merge branch 'copy-range' of sloum/hermes into master
This commit is contained in:
commit
53a74a964a
61
hermes.c
61
hermes.c
|
@ -937,25 +937,54 @@ void editorEndOfWord() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void editorCopySelection() {
|
void editorCopyRange(int sx, int sy, int ex, int ey) {
|
||||||
if (E.cy == E.vy) {
|
int swap;
|
||||||
int sx = E.vx;
|
if (ey < sy || (sy == ey && ex < sx)) {
|
||||||
int ex = E.cx;
|
swap = sy; sy = ey; ey = swap;
|
||||||
if (E.cx < E.vx) {
|
swap = sx; sx = ex; ex = swap;
|
||||||
sx = E.cx;
|
}
|
||||||
ex = E.vx;
|
if (sy < 0 || ey > E.numRows) return;
|
||||||
|
|
||||||
|
int totlen = 0;
|
||||||
|
char *buf;
|
||||||
|
if (ey - sy == 0) {
|
||||||
|
totlen = ex - sx + 1;
|
||||||
|
buf = malloc(totlen);
|
||||||
|
memcpy(buf, &E.row[sy].chars[sx], totlen);
|
||||||
|
} else {
|
||||||
|
for (int y = sy; y <= ey; y++) {
|
||||||
|
totlen += E.row[y].size + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
totlen -= sx;
|
||||||
|
totlen -= (E.row[ey].size - ex);
|
||||||
|
totlen++;
|
||||||
|
|
||||||
|
buf = malloc(totlen);
|
||||||
|
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, ex + 1);
|
||||||
|
p += ex + 1;
|
||||||
|
*p = '\r';
|
||||||
|
} else {
|
||||||
|
memcpy(p, E.row[row].chars, E.row[row].size);
|
||||||
|
p += E.row[row].size;
|
||||||
|
*p = '\r';
|
||||||
|
p++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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
|
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 +997,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,6 +1337,7 @@ 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;
|
||||||
|
@ -1397,7 +1428,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;
|
||||||
|
|
Loading…
Reference in New Issue