On 2021-07-07, lgc wrote:
>> Any idea what would be the best way to convert the snippet above?
>
> Move the `i` variable in the script-local namespace.
Thanks for the thorough explanation. For now, I am keeping a function
instead of a def. But, as you say:
> FWIW, I would still refactor the
> I try not to use global variables within functions. Is there an
> alternative enabling non-use of global variables?
Yes, use block-local, function-local, or script-local variables. Those
don't need any explicit scope.
> Also, I thought the new Vim9 Vimscript was getting rid of the need for
>
> 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
> >Well, this works:
> >
> >def Fix_beginfigs()
> > g:index = 1
> > g/^beginfig(\d*);$/s//\='beginfig(' .. g:index .. ');'/ | g:index =
> > g:index + 1 unlet g:index
> >enddef
> >
> >However, using a legacy function would not be worse. The global
> >command does most of the work, what
Bram Moolenaar said on Sun, 04 Jul 2021 23:30:01 +0200
>Well, this works:
>
>def Fix_beginfigs()
> g:index = 1
> g/^beginfig(\d*);$/s//\='beginfig(' .. g:index .. ');'/ | g:index =
> g:index + 1 unlet g:index
>enddef
>
>However, using a legacy function would not be worse. The global
>command
> 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
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