Vim 9 script questions

23 views
Skip to first unread message

Salman Halim

unread,
Jun 3, 2021, 10:53:36 AM6/3/21
to Vim Users
Hello,

It's my understanding that Vim 9 functions will get compiled and will be faster than legacy functions, so I'm changing some stuff I've written, but I have some questions:

1. How can I use a Vim 9 function in a mapping? Something like: nmap abc <CMD>call Vim9Func()<cr>

2. Can I call a Vim 9 function defined in another script from a legacy Vim script? How?

3. Can a Vim 9 function be defined inside another function? I want to call an outer function that defines some variables and then returns a function that will have access to those variables, as in a closure or a partial function (where the first few arguments to the function are preset).

Examples of each would be great, if possible, please. I did look through the documentation, but wasn't able to work these out for myself. 

Thank you very much for any help!

--

Salman

Bram Moolenaar

unread,
Jun 3, 2021, 3:56:59 PM6/3/21
to vim...@googlegroups.com, Salman Halim

Salman Halim wrote:

> It's my understanding that Vim 9 functions will get compiled and will be
> faster than legacy functions, so I'm changing some stuff I've written, but
> I have some questions:
>
> 1. How can I use a Vim 9 function in a mapping? Something like: nmap abc
> <CMD>call Vim9Func()<cr>

Just like before.

> 2. Can I call a Vim 9 function defined in another script from a legacy Vim
> script? How?

Just call it. Keep in mind that in Vim9 script functions are
script-local by default. You can either prefix "g:" to make them global
or use some way to call the script-local function, e.g. by using
:import.

> 3. Can a Vim 9 function be defined inside another function? I want to call
> an outer function that defines some variables and then returns a function
> that will have access to those variables, as in a closure or a partial
> function (where the first few arguments to the function are preset).

This mostly works as you expect.

> Examples of each would be great, if possible, please. I did look through
> the documentation, but wasn't able to work these out for myself.

vim9script

def Test()
var text = 'asdf'

def Inner(): string
return text .. '1234'
enddef

echo Inner()
enddef

Test()

--
What do you get when you cross a joke with a rehtorical question?

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages