Share variables between Two vim9 def functions

22 views
Skip to first unread message

Nicolas

unread,
Oct 18, 2022, 12:50:24 PM10/18/22
to vim_use
Hi,

All written in vim9script, I wonder how sharing variable between two def vim9 functions out of declaring these funcs as this :    
  • the first one imported and used throught _vimrc autocommand on BufEnter
  • the second one is declared inside the first one and is mapped just after in the code on event such as mouse double click.
The second one func, it needs variable dict computed by the first one on each BufEnter event.


In vimfiles/helper/autoload/helper.vim I got 
export def UnderLineHeaders(): void
  var headers: dict<string> = {}      
   .. some stuff
   ..  

  def g:OpeningHeader(): void       
     exe 'edit ' .. headers['goodIdxHeader'] ~path to the header file.
  enddef
  nnoremap <buffer> <2-Leftmouse> :call g:OpeningHeader()<CR>     <<< be carefull :)
enddef

In _vimrc i got:
  import autoload './vimfiles/plugged/helper.vim/autoload/helper.vim' as that
  autocmd BufEnter *.cpp,*.h thatUnderLineHeaders()                  <<< be carefull :)



Out of global var, I think there is a better way  to share something between first exported func and triggered on BufEnter and second one which needs variable computed of this triggered func, second one which occurs and called on mapped double click mouse event .


Thank you for your help
I can share the entire vimscript if needed.
Nicolas

Tommy Bollman

unread,
Oct 20, 2022, 3:23:46 AM10/20/22
to vim...@googlegroups.com
I don't get what you mean really, I'd use a g:global variable personally, and not think more of it.

Hth

Tommy

--
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/86cfe413-7cc8-43b1-bc2c-446398bba367n%40googlegroups.com.

Nikolaï

unread,
Oct 22, 2022, 8:47:05 AM10/22/22
to vim_use
Hi Tommy

Vim9script introduces the fact of having to use a global to pass values ​​between a P function, producing its values, used for example in an autocommand and a second C function consuming its data / values ​​which would be called on a key mapping for example.  The fact is that going through globals is not very elegant and forced to create a new global named different in each new case explained above.  So I was looking for another way.

Nicolas

Bram Moolenaar

unread,
Oct 28, 2022, 5:44:16 PM10/28/22
to vim...@googlegroups.com, Nikolaï

> Vim9script introduces the fact of having to use a global to pass values
> ​​between a P function, producing its values, used for example in an
> autocommand and a second C function consuming its data / values ​​which
> would be called on a key mapping for example. The fact is that going
> through globals is not very elegant and forced to create a new global named
> different in each new case explained above. So I was looking for another
> way.

With Vim9 script the recommended way is to put the variable in one
script where it is exported, and then import it whereever you want to
use it. That way you have control over where the value is changed, make
it a constant, etc. See help for "vim9-scopes" and ":import".

--
Westheimer's Discovery:
A couple of months in the laboratory can
frequently save a couple of hours in the library.

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

Nicolas

unread,
Oct 28, 2022, 5:50:28 PM10/28/22
to vim_use
OK export Var and import from files that i know reduce variable consumption scope. 
Entiendo. 
Thank you a lot Bram, Courage. 
Nicolas

Reply all
Reply to author
Forward
0 new messages