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
2733static 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
3945HOOK_IMPL (create , create_hook_cb );
4046HOOK_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
11811190struct 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 );
0 commit comments