Christian J. Robinson wrote:
> > > Source the attached script to see the error. If I prefix Function() with
> > > <SID> it does work. Is this correct behavior?
> >
> > I cannot see an attachment.
>
> Oops, here it is.
Which is:
vim9script
def Foo(): string
return "bar"
enddef
vnoremap <leader>foo s<C-R>=Foo()<CR><ESC>
normal v\foo
A mapping does not keep the context of where it was defined, since
it changes typed keys into other keys, without knowing what they mean.
In Vim9 script a function is defined script-local by default.
While "<C-R>=Foo()" calls Foo() from the command line, thus it searches
for a global function.
You can either make Foo() global:
def g:Foo(): string
Or (recommended) prefix <SID> in the mapping. That will be replaced
when the mapping is defined, so that when the mapping is used the right
function can be found:
vnoremap <leader>foo s<C-R>=<SID>Foo()<CR><ESC>
--
An actual excerpt from a classified section of a city newspaper:
"Illiterate? Write today for free help!"