Skip to content

Commit

Permalink
Clean up cmd_cut()
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarnes committed Jan 12, 2025
1 parent b082b6b commit 8cb2cf5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
29 changes: 21 additions & 8 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 0 additions & 20 deletions src/copy.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#include <stdlib.h>
#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)
{
Expand Down
13 changes: 11 additions & 2 deletions src/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "util/debug.h"
#include "util/macros.h"
#include "view.h"

Expand All @@ -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

0 comments on commit 8cb2cf5

Please sign in to comment.