Wednesday, March 26, 2014

Re: How should I modify a normal mode map set by a plugin?

Ethan Hereth wrote:
> Ethan Alan
>
>
> On Tue, Mar 11, 2014 at 9:43 AM, Nikolay Pavlov <zyx.vim@gmail.com> wrote:
>> On Mar 11, 2014 11:33 AM, "Paul Isambert" <zappathustra@free.fr> wrote:
>>> Hello Ethan,
>>>
>>> Ethan Hereth <advocateddrummer@gmail.com> a écrit:
>>>> I have a question that I think should have a better answer.
>>>>
>>>> I used to have a couple normal mode maps in my .vimrc that went like
>>>> this:
>>>>
>>>> nnoremap n nzz
>>>> nnoremap N Nzz
>>>>
>>>> which, as I'm sure I don't need to tell most of you, causes the next
>>>> search result to be centered (usually) when the user presses n/N. These maps
>>>> worked great for a long time... Until I installed the IndexedSearch plugin,
>>>> which I do really like by the way. This broke my maps which annoyed me for
>>>> quite some time until today when I decided to track down the cause.
>>>>
>>>> Typing 'verbose map n' gave me the following:
>>>>
>>>> n n * :let v:errmsg=''<CR>:silent! norm! n<CR>:call
>>>> <SNR>34_ShowCurrentSearchIndex(0,'!')<CR>
>>>> Last set from ~/.vim/bundle/IndexedSearch/plugin/IndexedSearch.vim
>>>>
>>>> So now I see that the IndexedSearch plugin has stolen my maps. So, for
>>>> now I have changed the appropriate lines in the IndexedSearch.vim plugin as
>>>> follows to get back the previous behavior without losing IndexedSearch's
>>>> addition. So, I'm happy now.
>>>>
>>>> nnoremap <silent>n :let v:errmsg=''<cr>:silent! norm! nzz<cr>:call
>>>> <SID>ShowCurrentSearchIndex(0,'!')<cr>
>>>> nnoremap <silent>N :let v:errmsg=''<cr>:silent! norm! Nzz<cr>:call
>>>> <SID>ShowCurrentSearchIndex(0,'!')<cr>
>>>>
>>>> My question is: Is there a better way to do this? Instead of actually
>>>> modifying the plugin code I'd like to simply modify the map after
>>>> IndexedSearch has set it. I did skim both Google and ':help map' but nothing
>>>> popped out at me as answering my question.
>>> Add a plugin in your "after" directory, e.g.:
>>>
>>> " Beware, no 'noremap' this time!
>>> nmap n nzz
>>> nmap N Nzz
>>>
>>> in "$HOME/.vim/after/plugin/IndexedSearch.vim" (replace ".vim" with
>>> "vimfiles" if you're on Windows). Files in "after" are loaded, well,
>>> after anything else.
>> If there is no z or zz mapping this variant is not different from
>>
>> nnoremap n nzz
>> nnoremap N Nzz
>>
>> . You just suggest to disable plugin mapping. The only reason why this works
>> is because when mapping rhs starts with lhs this part is not remapped.
>>
>> Files in .vim/after/ directory are loaded just like files in .vim/. Just in
>> default &runtimepath option .vim/after/ directory is placed after all other
>> directories, but there nothing else special about it. Plugins like
>> pathogen/VAM/Vundle usually put other plugin's after/ (but not root)
>> directories after .vim/after so "after anything else" statement is no longer
>> true. It is not very common though for plugins to have after/ directories.
> Hey all, sorry for this late response. It turns out that neither of
> these approaches work for me, I've been busy and hadn't noticed that
> this map wasn't working like I wanted it to. I think that the reason I
> initially believed it did is that perhaps I had forgotten to reopen
> vim/resource the files... *blush*
>
> I can verify, Paul, that these maps are being sourced last in
> ~/.vim/after/... because if I try to modify the maps there they do
> override the IndexedSearch.vim mappings. So... I come back to my
> original question: can mappings like these be modified/appended
> without editing the script source?
Seems to me that the problem actually lies with the IndexedSearch
plugin, and you should report it to its maintainer.

The plugin should be using some mapping to <Plug>. At an untested guess,

if !hasmapto('<Plug>ShowCurrentSearchIndex')
nno <unique> n <Plug>ShowCurrentSearchIndex
endif
nno <silent> <Plug>ShowCurrentSearchIndex :let v:errmsg=''<cr>:silent!
norm! nzz<cr>:call <SID>ShowCurrentSearchIndex(0,'!')<cr>

instead of

nnoremap <silent>n :let v:errmsg=''<cr>:silent! norm! nzz<cr>:call <SID>ShowCurrentSearchIndex(0,'!')<cr>

That way, you as the user can redefine some mapping to use the ShowCurrentSearchIndex() call along with its no-error reporting.

eg. in your .vimrc: nno <leader>n <Plug>ShowCurrentSearchIndex

Ditto for other mappings contained therein. IMHO, its always a bad idea for a plugin to map single characters as doing so often causes conflicts (except in the case of special buffers where <buffer> modifiers are used, such as netrw is rife with).

Regards,
Chip Campbell

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