Saturday, October 11, 2014

Re: How to use <script> attribute in map command?

Hi,

>> >>>>
>> >>>> On Friday, October 10, 2014 12:04:17 PM UTC+2, jiaxing_wang wrote:
>> >>>>> This wiki page:
>> >>>>>
>> >>>>> http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_3)
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>> says with the following maps in a script file:
>> >>>>>
>> >>>>>
>> >>>>> nnoremap \x /Topic
>> >>>>> nmap <script> ,f \x<CR>
>> >>>>>
>> >>>>>
>> >>>>> "In the second map command, ',f' is mapped to invoke \x which is replaced with '/Topic'.
>> >>>>>
>> >>>>> If the user or some other plugin has defined a mapping for '\x', then it is not used by ',f'."
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>> But I tried and find '\x' is not remapped to '/Topic' as the wiki says, so what does <script>
>> >>>>>
>> >>>>> really mean and how to use it?
>> >>>> Unfortunately the wiki example is wrong.
>> >>>>
>> >>>> Here's a short script that uses a mapping with <script>.
>> >>>>
>> >>>> function! s:SayHelloToUser() abort
>> >>>> echo printf("Hello %s!", $USER)
>> >>>> endfunction
>> >>>>
>> >>>> nmap <script> <Plug>SayHello :<C-U>call <SID>SayHelloToUser()<CR>
>> >>>>
>> >>>> The <Plug> mapping is the hook that a user can map to, eg by defining
>> >>>>
>> >>>> nmap <Leader>h <Plug>SayHello
>> >>>>
>> >>>> In the <Plug> mapping, <script> ensures that the right-hand side of the
>> >>>> mapping will never be affected by user-defined mappings.
>> >>>>
>> >>>> For example, if <script> were missing and somebody had defined a
>> >>>> command-line mode mapping for the character sequence 'cal', then the
>> >>>> mapping will likely fail because 'cal' is mapped to whatever the user
>> >>>> mapped it to. <script> protects from such disruptions.
>> >>> My example is a bit poor though, since in this case you might as well
>> >>> just use :nnoremap to be safe from side-effects. But I hope I got the
>> >>> point across.
>> >>>
>> >> Still don't understand the difference from :nnoremap, :-(
>> >> what's more, the :h :map-<script> seems to mean the same as the wiki:
>> >>
>> >> *:map-<script>* *:map-script*
>> >> If the first argument to one of these commands is "<script>" and it is used to
>> >> define a new mapping or abbreviation, the mapping will only remap characters
>> >> in the {rhs} using mappings that were defined local to a script, starting with
>> >> "<SID>". This can be used to avoid that mappings from outside a script
>> >> interfere (e.g., when CTRL-V is remapped in mswin.vim), but do use other
>> >> mappings defined in the script.
>> >> Note: ":map <script>" and ":noremap <script>" do the same thing. The
>> >> "<script>" overrules the command name. Using ":noremap <script>" is
>> >> preferred, because it's clearer that remapping is (mostly) disabled.
>> >>
>> >> thanks.
>> > Ok, let's reuse my example and add an intermediate mapping.
>> >
>> > nnoremap <SID>SayHelloRedirect :<C-U>call <SID>SayHelloToUser()<CR>
>> > nmap <script> <Plug>SayHello <SID>SayHelloRedirect
>> >
>> > In this case :nmap <script> (or :nnoremap <script>) allow the <Plug>
>> > mapping to be remapped to the intermediate mapping. This would not work
>> > with plain :nnoremap because it prevents all remapping.
>> But with <SID> used before SayHelloRedirect,
>> nmap <Plug>SayHello <SID>SayHelloRedirect
>> does the same thing, it will not be interfered by somebody mapping 'SayHelloRedirect',
>> so what interference is <script> used to prevent?
>>
>> forgive my paranoia ;-)
>> >
>> > Personally I never use <script> and I agree with ZyX that it is quite
>> > useless.
>> >
>> > You can find some discussion in the user manual, try this command:
>> >
>> > :h usr_41|/PIECES
>
> You're exactly right. <SID> is protection enough and that's why I don't
> see the point of using <script>.
>

To understand the need for the <script> attribute, try using the
following two maps:

nnoremap <SID>\x /Topic
nnoremap ,f <SID>\x

In the above case, the {rhs} is not remapped.

nnoremap <SID>\x /Topic
nnoremap <script> ,f <SID>\x

In the above case, the {rhs} is remapped (due to the use of <script>).

- Yegappan

>
> However, some people think differently. Try this command and you'll find
> that there are many instances of <script> in the Vim runtime files:
>
> :vim /<script>/ $VIMRUNTIME/**/*.vim
>
> I see there's even one I'm responsible for, shame on me :)
>

--
--
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.
For more options, visit https://groups.google.com/d/optout.

No comments: