Tuesday, December 13, 2016

Re: What is gained by the extra level of map indirection recommended in help?

2016-11-29 22:18 GMT+03:00 Brett Stahlman <brettstahlman@gmail.com>:
> The 'write-plugin' section of the help recommends the following
> 3-level map approach:
>
> map ,c <Plug>TypecorrAdd
> noremap <unique> <script> <Plug>TypecorrAdd <SID>Add
> noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
>
> I understand how it works, but it wasn't immediately clear to me what
> was being gained by the middle level of indirection: specifically the
> map to <SID>Add. What is the advantage of this strategy over the
> following 2-level approach?
>
> map ,c <Plug>TypecorrAdd
> noremap <unique> <script> <Plug>TypecorrAdd
> \ :call <SID>Add(expand("<cword>"), 1)<CR>

I would really suggest using single-level approach, where {lhs} is
defined based on global variables. The 2-level as well as 3-level
approach encourage (actually, leave no other choice) user to use :*map
and using :*map is most of time considered a bad practice: actually,
the only valid way to use :*map from my point of view is using :*map
when the whole {rhs} is mapped to something else and you know to what
(i.e. `:nmap ,c <Plug>foo` is fine, `:nmap ,c
<Plug>foo:nohlsearch<CR>` is bad).

Additionally, if you have a plugin with many mappings it may be
convenient for user to have two sets of global variables: one contains
a single variable which defines a common prefix (defaulting to e.g.
`<leader>c`), second defines per-mapping trailing part of {lhs}. This
way I e.g. could assign NERDCommenter to have `,c` as a leader in one
:let in place of writing

```VimL
for [s:rhs, s:lhs] in [['<plug>NERDCommenterComment', ',cc' ],
\ ['<plug>NERDCommenterToggle', ',c<space>'],
\ ['<plug>NERDCommenterMinimal', ',cm' ],
\ ['<plug>NERDCommenterSexy', ',cs' ],
\ ['<plug>NERDCommenterInvert', ',ci' ],
\ ['<plug>NERDCommenterYank', ',cy' ],
\ ['<plug>NERDCommenterAlignLeft', ',cl' ],
\ ['<plug>NERDCommenterAlignBoth', ',cb' ],
\ ['<plug>NERDCommenterNest', ',cn' ],
\ ['<plug>NERDCommenterUncomment', ',cu' ],
\ ['<plug>NERDCommenterToEOL', ',c$' ],
\ ['<plug>NERDCommenterAppend', ',cA' ],]
execute 'nmap <special>' s:lhs s:rhs
execute 'vmap <special>' s:lhs s:rhs
endfor
unlet s:lhs s:rhs
```

(and, no, I am not setting `mapleader` to comma, ever. I have plugins
whose mappings I do not want to see and it is much easier to stash
them away by letting them prefix their mappings by a backslash then
actively fight the mappings or determine how they can be disabled…
each time I happen to add a new plugin).

>
> The help says...
> "If another script would also map <SID>Add, it would get another
> script ID and thus define another mapping."
>
> While this is true, isn't this sort of script-uniqueness already
> guaranteed by the use of <SID> in the rhs of the 3rd mapping? I.e.,
> :call <SID>Add(...)<CR>
>
> Thanks,
> Brett S.
>
> --
> --
> 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.

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

Post a Comment