n = (long)tv_get_number(tv);
}
- if (opt_p_flags & P_FUNC && (tv->v_type == VAR_PARTIAL
+ if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
|| tv->v_type == VAR_FUNC))
{
// If the option can be set to a function reference or a lambda
if (rettv != NULL)
{
rettv->v_type = VAR_FUNC;
- rettv->vval.v_string = vim_strsave(ufunc->uf_name);
+ if (STRNCMP(name, "g:", 2) == 0)
+ // Keep the "g:", otherwise script-local may be
+ // assumed.
+ rettv->vval.v_string = vim_strsave(name);
+ else
+ rettv->vval.v_string = vim_strsave(ufunc->uf_name);
if (rettv->vval.v_string != NULL)
func_ref(ufunc->uf_name);
}
CheckScriptSuccess(lines)
enddef
+def Test_set_opfunc_to_global_function()
+ var lines =<< trim END
+ vim9script
+ def g:CountSpaces(type = ''): string
+ normal! '[V']y
+ g:result = getreg('"')->count(' ')
+ return ''
+ enddef
+ &operatorfunc = g:CountSpaces
+ new
+ 'a b c d e'->setline(1)
+ feedkeys("g@_", 'x')
+ assert_equal(4, g:result)
+ bwipe!
+ END
+ CheckScriptSuccess(lines)
+ &operatorfunc = ''
+enddef
+
def Test_lambda_type_allocated()
# Check that unreferencing a partial using a lambda can use the variable type
# after the lambda has been freed and does not leak memory.