Skip to content

Commit 3e26785

Browse files
committed
Fix the kill ring not working
The invalidation of the kill ring was not connected to a specific buffer and adding to _any_ buffer would invalidate the kill ring and prohibit pasting older pastes.
1 parent bf7c809 commit 3e26785

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

src/dged/buffer.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@
2323
#include <unistd.h>
2424
#include <wchar.h>
2525

26+
struct buffer_location {
27+
struct buffer *buffer;
28+
struct location at;
29+
bool valid;
30+
};
31+
2632
#define KILL_RING_SZ 64
2733
static struct kill_ring {
2834
struct text_chunk buffer[KILL_RING_SZ];
29-
struct location last_paste;
30-
bool paste_up_to_date;
35+
36+
struct buffer_location last_paste;
37+
3138
uint32_t curr_idx;
3239
uint32_t paste_idx;
3340
} g_kill_ring = {.curr_idx = 0,
3441
.buffer = {{0}},
35-
.last_paste = {0},
36-
.paste_idx = 0,
37-
.paste_up_to_date = false};
42+
.last_paste = {.valid = false},
43+
.paste_idx = 0};
3844

3945
HOOK_IMPL(create, create_hook_cb);
4046
HOOK_IMPL(destroy, destroy_hook_cb);
@@ -575,7 +581,9 @@ struct location buffer_add(struct buffer *buffer, struct location at,
575581
buffer->needs_render = true;
576582

577583
// invalidate last paste
578-
g_kill_ring.paste_up_to_date = false;
584+
if (g_kill_ring.last_paste.buffer == buffer) {
585+
g_kill_ring.last_paste.valid = false;
586+
}
579587

580588
struct location initial = at;
581589
struct location final = at;
@@ -1165,9 +1173,10 @@ static struct location paste(struct buffer *buffer, struct location at,
11651173
struct location new_loc = at;
11661174
struct text_chunk *curr = &g_kill_ring.buffer[idx];
11671175
if (curr->text != NULL) {
1168-
g_kill_ring.last_paste = at;
1176+
g_kill_ring.last_paste.at = at;
1177+
g_kill_ring.last_paste.buffer = buffer;
11691178
new_loc = buffer_add(buffer, at, curr->text, curr->nbytes);
1170-
g_kill_ring.paste_up_to_date = true;
1179+
g_kill_ring.last_paste.valid = true;
11711180
}
11721181

11731182
return new_loc;
@@ -1179,19 +1188,17 @@ struct location buffer_paste(struct buffer *buffer, struct location at) {
11791188
}
11801189

11811190
struct location buffer_paste_older(struct buffer *buffer, struct location at) {
1182-
if (g_kill_ring.paste_up_to_date) {
1191+
if (buffer == g_kill_ring.last_paste.buffer && g_kill_ring.last_paste.valid) {
11831192

11841193
// remove previous paste
1185-
buffer_delete(buffer, region_new(g_kill_ring.last_paste, at));
1194+
buffer_delete(buffer, region_new(g_kill_ring.last_paste.at, at));
11861195

1187-
// paste older
1188-
if (g_kill_ring.paste_idx > 0) {
1189-
--g_kill_ring.paste_idx;
1190-
} else {
1191-
g_kill_ring.paste_idx = g_kill_ring.curr_idx;
1192-
}
1196+
g_kill_ring.paste_idx = g_kill_ring.paste_idx > 0
1197+
? g_kill_ring.paste_idx - 1
1198+
: g_kill_ring.curr_idx;
11931199

1194-
return paste(buffer, g_kill_ring.last_paste, g_kill_ring.paste_idx);
1200+
// paste older
1201+
return paste(buffer, g_kill_ring.last_paste.at, g_kill_ring.paste_idx);
11951202

11961203
} else {
11971204
return buffer_paste(buffer, at);

src/dged/buffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ struct buffer {
7070
/** If true, text properties are not immediate */
7171
bool retain_properties;
7272

73+
/** When true, this buffer needs to be re-rendered. */
7374
bool needs_render;
7475

76+
/** Has this buffer been updated? */
7577
bool updated;
7678

79+
/** Is the buffer backed by a file? */
7780
bool file_backed;
7881

82+
/** Bulk additions prevents callbacks from being called. */
7983
bool bulk_adding;
8084

8185
/**

0 commit comments

Comments
 (0)