From 46ceefb52be6b014dd27b6adfbca3e1c9dff0c0c Mon Sep 17 00:00:00 2001 From: JD <46619169+rudiejd@users.noreply.github.com> Date: Wed, 27 Dec 2023 20:57:13 -0500 Subject: [PATCH] fix(clipboard): make getreg() accurate for clipboard registers (#26740) Problem: getreg("*") / getreg("+") disagree with :registers. Solution: Avoid falling back to unnamed register if provider fails. --- src/nvim/ops.c | 10 ++++++++-- test/functional/provider/clipboard_spec.lua | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 8dc267cc45..4cce2e7ba6 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -815,9 +815,15 @@ yankreg_T *get_yank_register(int regname, int mode) { yankreg_T *reg; - if (mode == YREG_PASTE && get_clipboard(regname, ®, false)) { + if ((mode == YREG_PASTE || mode == YREG_PUT) + && get_clipboard(regname, ®, false)) { // reg is set to clipboard contents. return reg; + } else if (mode == YREG_PUT && (regname == '*' || regname == '+')) { + // in case clipboard not available and we aren't actually pasting, + // return an empty register + static yankreg_T empty_reg = { .y_array = NULL }; + return &empty_reg; } else if (mode != YREG_YANK && (regname == 0 || regname == '"' || regname == '*' || regname == '+') && y_previous != NULL) { @@ -4893,7 +4899,7 @@ void *get_reg_contents(int regname, int flags) return get_reg_wrap_one_line(xstrdup(retval), flags); } - yankreg_T *reg = get_yank_register(regname, YREG_PASTE); + yankreg_T *reg = get_yank_register(regname, YREG_PUT); if (reg->y_array == NULL) { return NULL; } diff --git a/test/functional/provider/clipboard_spec.lua b/test/functional/provider/clipboard_spec.lua index 3c2e358745..004ee6d106 100644 --- a/test/functional/provider/clipboard_spec.lua +++ b/test/functional/provider/clipboard_spec.lua @@ -752,4 +752,11 @@ describe('clipboard (with fake clipboard.vim)', function() expect('some some') eq('some', eval('getreg("*")')) end) + + it('does not fall back to unnamed register with getreg() #24257', function () + eval('setreg("", "wrong")') + command('let g:cliperror = 1') + eq('', eval('getreg("*")')) + eq('', eval('getreg("+")')) + end) end)