Wednesday, February 16, 2022

Re: Vim9 - Can not get compile to recognize a deep variable

> I have read through the Vim9.txt, and can not find a solution to this issue.
> When I call Parse(), the script version with the entire command on one line (|) gives error 'E121: Undefined variable: udp' in function add().
> Both commands work if used on the command line, so its particular to running in a Vim9 script.
>
> This works:
> def g:Parse(): void
> var udp: list<string>
>
> :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
> :3substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
> # :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
> # | substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
>
> echo udp
> enddef
>
> This does not work:
> def g:Parse(): void
> var udp: list<string>
>
> # :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
> # :3substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
> :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
> | substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
>
> echo udp
> enddef

This happens because when compiling the function, when the :substitute
command is separate, the compiler recognizes the "\=" in the
substitution. Then it works.

When joining the two lines the compiler only sees the ":global" command
and doesn't check for the "\=" argument.

This is nearly impossible to fix, because parsing the ":global" command
requires parsing just about any Ex command. That can only be done at
runtime, and then the local variable is not visible.

You'll have to use a workaround. You could make the "udp" variable a
global. That is what would happen in legacy script as well.

--
ERROR 047: Keyboard not found. Press RETURN to continue.

/// Bram Moolenaar -- Bram@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 ///

--
--
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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20220216221529.BD9C41C0FE0%40moolenaar.net.

No comments:

Post a Comment