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 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>
The help says... "If another script would also map 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 in the rhs of the 3rd mapping? I.e.,
:call Add(...)
Update: After much discussion with a patient and helpful @Peter Rincker, I now think the reason for the extra map level has become clear, but in the simple example I presented above, there's no need for it. The full example in the Vim help, however, had another map, which I neglected to copy into my question. The complete example looks like this:
map ,c <Plug>TypecorrAdd
noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
noremap <unique> <script> <Plug>TypecorrAdd <SID>Add
noremenu <script> Plugin.Add\ Correction <SID>Add
Note that, in this full example, 2 distinct maps use <SID>Add
in their rhs. The only purpose (AFAICT) of creating the map from <SID>Add
is to avoid duplicating the code in its rhs in those 2 maps (normal map <Plug>TypecorrAdd
and menu command Plugin.Add Correction
). IOW, the extra (3rd) level of indirection seems to be more about code reuse than about hygiene or script insulation.