diff --git a/src/commands.c b/src/commands.c index 69d25e5b..6139819f 100644 --- a/src/commands.c +++ b/src/commands.c @@ -560,20 +560,33 @@ static bool cmd_cut(EditorState *e, const CommandArgs *a) { BUG_ON(a->nr_args); View *view = e->view; - const long x = view_get_preferred_x(view); + long preferred_x = view_get_preferred_x(view); + size_t size; + bool line_copy; + if (view->selection) { - bool is_lines = view->selection == SELECT_LINES; - cut(&e->clipboard, view, prepare_selection(view), is_lines); - if (view->selection == SELECT_LINES) { - move_to_preferred_x(view, x); - } + line_copy = (view->selection == SELECT_LINES); + size = prepare_selection(view); unselect(view); } else { + line_copy = true; block_iter_bol(&view->cursor); BlockIter tmp = view->cursor; - cut(&e->clipboard, view, block_iter_eat_line(&tmp), true); - move_to_preferred_x(view, x); + size = block_iter_eat_line(&tmp); } + + if (size == 0) { + return true; + } + + char *buf = block_iter_get_bytes(&view->cursor, size); + record_copy(&e->clipboard, buf, size, line_copy); + buffer_delete_bytes(view, size); + + if (line_copy) { + move_to_preferred_x(view, preferred_x); + } + return true; } diff --git a/src/copy.c b/src/copy.c index 69f81d71..b8644a81 100644 --- a/src/copy.c +++ b/src/copy.c @@ -1,29 +1,9 @@ -#include #include "copy.h" #include "block-iter.h" #include "change.h" #include "insert.h" #include "move.h" #include "selection.h" -#include "util/debug.h" - -void record_copy(Clipboard *clip, char *buf, size_t len, bool is_lines) -{ - BUG_ON(len && !buf); - free(clip->buf); - clip->buf = buf; - clip->len = len; - clip->is_lines = is_lines; -} - -void cut(Clipboard *clip, View *view, size_t len, bool is_lines) -{ - if (len) { - char *buf = block_iter_get_bytes(&view->cursor, len); - record_copy(clip, buf, len, is_lines); - buffer_delete_bytes(view, len); - } -} void paste(Clipboard *clip, View *view, PasteLinesType type, bool move_after) { diff --git a/src/copy.h b/src/copy.h index 2a37f6a1..72bda2be 100644 --- a/src/copy.h +++ b/src/copy.h @@ -3,6 +3,8 @@ #include #include +#include +#include "util/debug.h" #include "util/macros.h" #include "view.h" @@ -18,8 +20,15 @@ typedef enum { PASTE_LINES_INLINE, } PasteLinesType; -void record_copy(Clipboard *clip, char *buf, size_t len, bool is_lines); -void cut(Clipboard *clip, View *view, size_t len, bool is_lines); +static inline void record_copy(Clipboard *clip, char *buf, size_t len, bool is_lines) +{ + BUG_ON(len && !buf); + free(clip->buf); + clip->buf = buf; // Takes ownership + clip->len = len; + clip->is_lines = is_lines; +} + void paste(Clipboard *clip, View *view, PasteLinesType type, bool move_after); #endif