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.
This commit is contained in:
JD 2023-12-27 20:57:13 -05:00 committed by GitHub
parent e0d998dbc8
commit 46ceefb52b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -815,9 +815,15 @@ yankreg_T *get_yank_register(int regname, int mode)
{
yankreg_T *reg;
if (mode == YREG_PASTE && get_clipboard(regname, &reg, false)) {
if ((mode == YREG_PASTE || mode == YREG_PUT)
&& get_clipboard(regname, &reg, 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;
}

View File

@ -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)