vim9 autoloaded functions in expression register

12 views
Skip to first unread message

Maxim Kim

unread,
Mar 13, 2023, 8:37:44 PM3/13/23
to vim_dev
Is there a way to use vim9 autoloaded function in expression register?

The old way works:

vim9script
import autoload 'misc.vim'
iab <buffer> p! println!()<left><c-r>=misc#Eatchar('\s')<cr>

the dotted call doesn't:

vim9script
import autoload 'misc.vim'
iab <buffer> p! println!()<left><c-r>=misc.Eatchar('\s')<cr>

E121: Undefined variable: misc

Bram Moolenaar

unread,
Mar 16, 2023, 8:27:32 AM3/16/23
to vim...@googlegroups.com, Maxim Kim
When using a mapping or abbreviation the context of where it was defined
is not used. That is why you need to use <SID> for a script-local
function. This is different from user commands, where the context is
used.

Your "old way" example most likely works because "misc#" finds the
misc.vim script file in an autoload directory. The "import" line
doesn't matter (I haven't tried this though).

When using "misc.Eatchar()" it cannot find "misc" in the current
context. The script context where the abbreviation was defined is not
used.

I can't think of a good way to make this work. Being able to access
"misc", which is script-local in one specific script, from any other
context, means script-local isn't really "local" any more.

The best I can think of is to add a script-local function and use that:

vim9script
import autoload 'misc.vim'

def Eatchar(arg: string): string
return misc.Eatchar(arg)
enddef

iab <buffer> p! println!()<left><c-r>=<SID>Eatchar('\s')<cr>

Disclaimer: I haven't tried this.

--
MAN: Fetchez la vache!
GUARD: Quoi?
MAN: Fetchez la vache!
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

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

Maxim Kim

unread,
Mar 16, 2023, 8:38:26 AM3/16/23
to vim_dev

Your "old way" example most likely works because "misc#" finds the
misc.vim script file in an autoload directory. The "import" line
doesn't matter (I haven't tried this though).

Indeed, import doesn't matter, it was just a leftover from previous try and not needed here.
 
I can't think of a good way to make this work. Being able to access
"misc", which is script-local in one specific script, from any other
context, means script-local isn't really "local" any more.

I see. I guess it would probably require some trickery/hacks which will make it less robust.
 
The best I can think of is to add a script-local function and use that:

vim9script
import autoload 'misc.vim'

def Eatchar(arg: string): string
return misc.Eatchar(arg)
enddef

iab <buffer> p! println!()<left><c-r>=<SID>Eatchar('\s')<cr>

This would defeat the purpose of putting function into autoloaded dir. 
I think the misc#Eatchar() is better from user experience perspective.
Reply all
Reply to author
Forward
0 new messages