Tuesday, February 8, 2022

Re: vim9script + map needs ?

> 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

On Tuesday, February 8, 2022 at 10:03:28 PM UTC+11 Bram Moolenaar wrote:

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

--
--
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/b67f0710-3b50-4ea2-932b-2b6f4a249266n%40googlegroups.com.

No comments:

Post a Comment