> Any idea what would be the best way to convert the snippet above?

Move the `i` variable in the script-local namespace.  That is, don't 
declare it inside the function, but at the script-level.  You can still 
reset it to a default value whenever the function is invoked:

    vim9script
    var i: number
    def FixBeginfig()
        i = 1
        global /^beginfig(\d*);$/ substitute//\='beginfig(' .. i .. ');'/ | 
++i
    enddef
    command -buffer -nargs=0 -bar FixBeginfigs FixBeginfig()

This works because – in a Vim9 script – all commands are executed in the 
Vim9 script context.  As a result, they have access to any Vim9 syntax, and 
to all items defined in the script-local namespace.

Whether they're executed from a global command, an autocmd, a user command, 
a simple `:execute`, ... it doesn't matter.

For this reason, in your user command definition, you don't need `:call` 
nor `<sid>`.  You don't need a bang after `:command` either since the patch 
v8.1.0573.

---

However, as mentioned earlier, there won't be a speed increase because the 
command is not compiled, just executed.  Like most (all?) commands executed 
by another command.  This is confirmed by the output of `:disassemble`:

    <SNR>1_FixBeginfig
        i = 1
       0 PUSHNR 1
       1 STORESCRIPT i-0 in /tmp/t.vim

        global /^beginfig(\d*);$/ substitute//\='beginfig(' .. i .. ');'/ | 
++i
       2 EXEC     global /^beginfig(\d*);$/ substitute//\='beginfig(' .. i 
.. ');'/ | ++i
       3 RETURN void

Notice that the second generated instruction is a simple `EXEC`.

FWIW, I would still refactor the code in Vim9.  Mainly because of type 
checking, and for better readability.



On Sunday, July 4, 2021 at 8:42:10 PM UTC+2 Lifepillar wrote:

> The MetaPost plugin in Vim contains the following definition to replace
> the n-th occurrence of `beginfig(...)` with `beginfig(n)`:
>
> function! s:fix_beginfigs()
> let i = 1
> g/^beginfig(\d*);$/s//\='beginfig('.i.');'/ | let i = i + 1
> endfunction
>
> command -nargs=0 FixBeginfigs call s:fix_beginfigs()
>
> On attempting to convert it to Vim9 script, I came up with this:
>
> def FixBeginfig()
> i = 1
> g/^beginfig(\d*);$/s//\='beginfig(' .. i .. ');'/ | i += 1
> enddef
>
> command! -buffer -nargs=0 -bar FixBeginfigs call <sid>FixBeginfig()
>
> But this results in 'Undefined variable: i', likely because the command
> is not evaluated in Vim9 script context.
>
> Any idea what would be the best way to convert the snippet above?
>
> Thanks,
> Life.
>
>
>

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_use/61934a7a-8e87-473c-93f4-9c4bf381f2ean%40googlegroups.com.

Reply via email to