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() {
|
||||
if (E.cy == E.vy) {
|
||||
int sx = E.vx;
|
||||
int ex = E.cx;
|
||||
if (E.cx < E.vx) {
|
||||
sx = E.cx;
|
||||
ex = E.vx;
|
||||
void editorCopyRange(int sx, int sy, int ex, int ey) {
|
||||
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 = 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() {
|
||||
editorCopySelection();
|
||||
int sx = E.cx;
|
||||
int sy = E.cy;
|
||||
int ex = E.vx;
|
||||
|
@ -968,6 +997,7 @@ void editorDeleteSelection() {
|
|||
E.cx = sx;
|
||||
E.cy = sy;
|
||||
}
|
||||
editorCopyRange(sx, sy, ex, ey);
|
||||
|
||||
if (sx < E.row[sy].size)
|
||||
E.cx++;
|
||||
|
@ -1307,6 +1337,7 @@ void editorCommandKp(int c) {
|
|||
break;
|
||||
case 'P':
|
||||
case 'p':
|
||||
if (E.pastelen <= 0) break;
|
||||
{
|
||||
char open = c == 'p' ? 'o' : 'O';
|
||||
int startx = E.cx;
|
||||
|
@ -1397,7 +1428,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;
|
||||
|
|
Loading…
Reference in New Issue