passing vim9 vars to python3

19 views
Skip to first unread message

N i c o l a s

unread,
May 31, 2023, 6:50:20 AM5/31/23
to vim_use
Hi,

Seeing the legacy vimscript func Test_python3_vars() in src\testdir\test_python3.vim

Can you confirm that in vim9script, those setlines test are no possible to be written in same way to be passed to python3 world ?

func Test_python3_vars()
  let g:foo = 'bac'              -- << -- THE ONLY ONE CAN BE USED in vim9 ?
  let w:abc3 = 'def'            -- << -- NOT POSSIBLE in vim9
  let b:baz = 'bar'               -- << -- NOT POSSIBLE in vim9
  let t:bar = 'jkl'                   -- << -- NOT POSSIBLE in vim9


Beyond a simple variable, is it possible to move a VIM9 class to the python3 world?

Thank you
N i c o l a s

Bram Moolenaar

unread,
May 31, 2023, 7:48:23 AM5/31/23
to vim...@googlegroups.com, N i c o l a s

> Seeing the legacy vimscript func* Test_python3_vars()* in
> *src\testdir\test_python3.vim*,
>
> Can you confirm that in vim9script, those setlines test are no possible to
> be written in same way to be passed to python3 world ?
>
> func Test_python3_vars()
> let g:foo = 'bac' -- << -- THE ONLY ONE CAN BE USED in vim9 ?
> let w:abc3 = 'def' -- << -- NOT POSSIBLE in vim9
> let b:baz = 'bar' -- << -- NOT POSSIBLE in vim9
> let t:bar = 'jkl' -- << -- NOT POSSIBLE in vim9

I guess you mean that in Vim9 script and in a :def function the "let"
command doesn't work. You can just leave it out, e.g.:

w:abc3 = 'def'

> Beyond a simple variable, is it possible to move a VIM9 class to the
> python3 world?

You mean to pass a reference of the class and use it in Python? No,
that is not possible. The class implementation is only halfway, but
even when the work has been done a Python script will not be able to
access a Vim class directly. You can only use it in a Vim script
context, such as using "vim.eval()".

--
Do not trust atoms, they make up everything.

/// 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 ///

N i c o l a s

unread,
May 31, 2023, 8:42:51 AM5/31/23
to vim_use
Hi Bram,

Thank you I knew, this is working 

vim9script

def g:Py3_SimpleMinimalTest(yml_fpath: string): void
  w:yml_f = yml_fpath
  exe 'py3 print(sys.version)'
  exe 'py3 print(vim.current.window.vars[''yml_f''])'
enddef

and what about making this works

def g:Py3_SimpleMinimalTest(yml_fpath: string): void
  var yml_f:string  = yml_fpath
  exe 'py3 print(sys.version)'
  exe 'py3 print(vim.current.????.vars[''yml_f''])'
enddef

Thank you

Bram Moolenaar

unread,
May 31, 2023, 10:13:20 AM5/31/23
to vim...@googlegroups.com, N i c o l a s

> Thank you I knew, this is working
>
> vim9script
>
> def g:Py3_SimpleMinimalTest(yml_fpath: string): void
> w:yml_f = yml_fpath
> exe 'py3 print(sys.version)'
> exe 'py3 print(vim.current.window.vars[''yml_f''])'
> enddef
>
> and what about making this works
>
> def g:Py3_SimpleMinimalTest(yml_fpath: string): void
> *var yml_f:string =* yml_fpath
> exe 'py3 print(sys.version)'
> exe 'py3 print(vim.current.*????*.vars[''yml_f''])'
> enddef

Do you mean you want to access variables local to a compiled function by
name? That is not possible. The "l:" namespace that exists in legacy
functions was intentionally left out in compiled functions, because it
adds quite a bit of overhead. You can use a script-local variable to
make this work. It does mean you go outside of the function scope, but
it's still inside the scope of the script file. That means it won't
interfere with what happens in other scripts.

--
From "know your smileys":
<>:-) Bishop

N i c o l a s

unread,
May 31, 2023, 1:09:02 PM5/31/23
to vim_use
  • From vim9 help I see Functions and variables are script-local by default
             So s: is no mandatory no?, because if I put s: I see E1268: Cannot use s: in Vim9 script: s:yml_f = yml_fpath.

  • So I delete s: and obtain this
           3.12.0b1 (tags/v3.12.0b1:5612078, May 22 2023, 16:20:31) [MSC v.1934 64 bit (AMD64)]
           <vim.dictionary object at 0x0000020790DC9380>


          regarding this code : 
            vim9script

            def Py3_SimpleMinimalTest(yml_fpath: string): void
              var yml_f: string = yml_fpath
              exe 'py3 print(sys.version)'
              exe 'py3 print(vim.current.buffer.vars)'
              exe 'py3 print(vim.current.buffer.vars[''yml_f''])'
           enddef
           Py3_SimpleMinimalTest('foobar.yml')


         Then I got 
          3.12.0b1 (tags/v3.12.0b1:5612078, May 22 2023, 16:20:31) [MSC v.1934 64 bit (AMD64)]
          <vim.dictionary object at 0x0000020790DFFF00>
         
Traceback (most recent call last):
         File "<string>", line 1, in <module>
         KeyError: 'yml_f'

Bram Moolenaar

unread,
May 31, 2023, 5:30:14 PM5/31/23
to vim...@googlegroups.com, N i c o l a s

> - From vim9 help I see *Functions and variables are script-local by
> default*
>
> So s: is no mandatory no?, because if I put s: I see E1268:
> Cannot use s: in Vim9 script: s:yml_f = yml_fpath.

You need to declare the variable at the script level first. You cannot
create a script variable in a function. This is so that when accessing
the variable from a compiled function it knows where it is, it does not
need to be looked up by name (which is much faster).

> - So I delete s: and obtain this
>
> 3.12.0b1 (tags/v3.12.0b1:5612078, May 22 2023, 16:20:31) [MSC
> v.1934 64 bit (AMD64)]
> <vim.dictionary object at 0x0000020790DC9380>
>
> regarding this code :
> vim9script
>
> def Py3_SimpleMinimalTest(yml_fpath: string): void
> var yml_f: string = yml_fpath
> exe 'py3 print(sys.version)'
> exe 'py3 print(vim.current.buffer.vars)'
> exe 'py3 print(vim.current.buffer.vars[''yml_f''])'
> enddef

This will certainly not work, since "vim.current.buffer.vars[]" refers
to buffer-local variables. You could change the first line in this
function:
b:yml_f = yml_fpath

But is this really buffer related? And it does require a runtime lookup
by name. Using a script-local variable will work better.


--
There's no place like $(HOME)!
Reply all
Reply to author
Forward
0 new messages