diff --git a/hermes.c b/hermes.c index 2595efb..9e9ba89 100644 --- a/hermes.c +++ b/hermes.c @@ -947,7 +947,7 @@ void editorCopyRange(int sx, int sy, int ex, int ey) { int totlen = 0; char *buf; - if (ey - sy == 0) { + if (ey == sy) { totlen = ex - sx + 1; buf = malloc(totlen); memcpy(buf, &E.row[sy].chars[sx], totlen); @@ -1314,25 +1314,28 @@ void editorCommandKp(int c) { } break; case 'w': - while (counter > 0) { - if (deleting) { - int start_x = E.cx; - int start_y = E.cy; - editorNextWord(); - int len = start_y == E.cy ? E.cx - start_x : E.row[start_y].size - start_x; - editorUpdatePaste(&E.row[start_y].chars[start_x], len); - if (E.cy != start_y) { - E.cy--; - E.cx = E.row[E.cy].size; - } - while (E.cx != start_x) { - editorDeleteChar(); - } - if (E.cx > E.row[E.cy].size - 1) E.cx--; - } else { + { + int start_x = E.cx; + int start_y = E.cy; + while (counter > 0) { editorNextWord(); + counter--; } - counter--; + if (!deleting) break; + int end_x, end_y; + if (E.cx == 0) { + if (E.cy == 0) break; + end_y = E.cy - 1; + end_x = E.row[E.cy].size - 1; + } else { + end_x = E.cx - 1; + end_y = E.cy; + } + editorCopyRange(start_x, start_y, end_x, end_y); + while (E.cx != start_x || E.cy != start_y) { + editorDeleteChar(); + } + if (E.cx > E.row[E.cy].size - 1) E.cx = E.row[E.cy].size - 1; } break; case 'P':