vim9script + map needs <SID>?

92 views
Skip to first unread message

JohnBeckett

unread,
Feb 8, 2022, 1:03:05 AM2/8/22
to vim_use
I can't find any documentation on precisely this point but I inferred that "<SID>" in the mapping below should not be needed. I hoped that would be true from wording (at :help vim9-differences) like the following, admittedly for an expression mapping. If <SID> is intended as required, perhaps that could be mentioned in the Vim 9 doc.

"When defining an expression mapping, the expression will be evaluated in the context of the script where it was defined."

vim9script
def Toggle(): void
  echo 'TOGGLE'
enddef
def Init(): void
  nnoremap <buffer> <F8> :call <SID>Toggle()<CR>j
  echo 'Press F8 to toggle selection.'
enddef
command! Init call Init()

To test, save the above in a file then enter ':so %' and ':Init'. Then press F8.

If "<SID>" is included, pressing F8 shows "TOGGLE". Otherwise I see "E117: Unknown function: Toggle".

John

Bram Moolenaar

unread,
Feb 8, 2022, 6:03:28 AM2/8/22
to vim...@googlegroups.com, JohnBeckett

John Beckett wrote:

> I can't find any documentation on precisely this point but I inferred that
> "<SID>" in the mapping below should not be needed. I hoped that would be
> true from wording (at :help vim9-differences) like the following,
> admittedly for an expression mapping. If <SID> is intended as required,
> perhaps that could be mentioned in the Vim 9 doc.
>
> "When defining an expression mapping, the expression will be evaluated in
> the context of the script where it was defined."

This mentions "expression". The evaluation can be done in the right
context, because it happens before the resulting text is put in the
typeahead buffer. For a normal mapping, the RHS is put in the typeahead
buffer as-is. There is no way to attach the context of the mapping to
it, it's just text.

> vim9script
> def Toggle(): void
> echo 'TOGGLE'
> enddef
> def Init(): void
> nnoremap <buffer> <F8> :call <SID>Toggle()<CR>j
> echo 'Press F8 to toggle selection.'
> enddef
> command! Init call Init()
>
> To test, save the above in a file then enter ':so %' and ':Init'. Then
> press F8.
>
> If "<SID>" is included, pressing F8 shows "TOGGLE". Otherwise I see "E117:
> Unknown function: Toggle".

That's correct, the <SID> part is changed to <SNR>123_, the script
number is prefixed. This happens when the mapping is defined, you can
see it in the output of ":map <F8>".

--
hundred-and-one symptoms of being an internet addict:
26. You check your mail. It says "no new messages." So you check it again.

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

JohnBeckett

unread,
Feb 8, 2022, 11:00:32 PM2/8/22
to vim_use
> This happens when the mapping is defined, you can
> see it in the output of ":map <F8>".

Of course, that clarifies it, thanks.

There are some very useful examples in Vim help but a new vim9examples.txt page would be good with some longer script examples showing how things should be done. I've just written something to select multiple non-contiguous lines using the fabulous text properties and it was a bit of a challenge although obvious in retrospect.

John
Reply all
Reply to author
Forward
0 new messages