Saturday, July 31, 2010

Re: What key is bound to Something?

On Sat, Jul 31, 2010 at 2:32 AM, Tony Mechelynck
<antoine.mechelynck@gmail.com> wrote:
> On 31/07/10 02:32, Jakson A. Aquino wrote:
>>
>> On Fri, Jul 30, 2010 at 8:37 PM, Tony Mechelynck
>> <antoine.mechelynck@gmail.com>  wrote:
>>>
>>> On 31/07/10 00:12, Jakson A. Aquino wrote:
>>>>
>>>> Hi,
>>>>
>>>> I maintain a filetype plugin to R and the user can change the default
>>>> key bindings by putting in the vimrc commands like:
>>>>
>>>> map<F2>    <Plug>RStart
>>>>
>>>> How can I know in the ftplugin/r.vim what key is bound to
>>>> <Plug>RStart? The plugin adds a menu to gui versions of Vim and I
>>>> would like to tell in the menu all the key bindings, even the
>>>> customized ones.
>>>>
>>>> Thanks,
>>>>
>>>> Jakson Aquino
>>>>
>>>> Note: the plugin is here:
>>>> http://www.vim.org/scripts/script.php?script_id=2628
>>>>
>>>
>>> It is possible to get a list of all mappings i.e. all user-defined key
>>> bindings, see the thread "Writing :map and :map! to text file or
>>> something"
>>> started 2010-07-26 09:42 -0700 by someone named "Gary".
>>>
>>> For default bindings it's harder: you would have to compile a list from
>>> the
>>> various lists at ":help index.txt", write that statically that into your
>>> script (and update it whenever that helpfile changes), and, at runtime,
>>> *remove* from the list the keybindings which also have a user-defined
>>> mapping in the same mode.
>>
>> Thanks for answering! This was rather a wish than an important thing
>> to my plugin. I think it's overly complex to write the output of :map
>> to a file and then search the file for all<Plug>Something that my
>> plugin has just to know if there are a few custom key bindings. The
>> goal was to help users of GVim to remember the key binds by looking at
>> the menu, but I will assume that users who customize key bindings are
>> advanced enough to either remember them or do ':map' if any was
>> forgotten. i have already put in the plugin's documentation:
>>
>>    Custom key bindings are not shown in Vim's menu, but you can
>>    type :map to see the list of current mappings.
>>
>> Best regards,
>>
>> Jakson Aquino
>>
>
> You can easily know _if_ there is a map to <Plug>something:
>
>    if hasmapto("\<Plug>",'nvoilc') || hasmapto("\<Plug>",'nvoilc',1)
>        echo 'there is a mapping or abbrev to ''<Plug>something'''
>    endif
>
> see :help hasmapto()
>
> Similarly, to know if some particular {lhs} (let's say <F2> has ben used,
>
>        :map <F2>
> and/or
>        :map! <F2>
>
> will show you any mappings beginning with <F2>; you can capture the output
> even if using :silent, see :help :redir

Thanks! I ended up with this (which solves the problem to me):

nmap <F2> <Plug>RStart

redir => b:kblist
silent imap
silent vmap
silent nmap
redir END
let b:kblist2 = split(b:kblist, "\n")
unlet b:kblist
let b:imaplist = []
let b:vmaplist = []
let b:nmaplist = []
for i in b:kblist2
if i =~ "<Plug>R"
let si = split(i)
if len(si) == 3
if si[0] =~ "v"
call add(b:vmaplist, si)
endif
if si[0] =~ "i"
call add(b:imaplist, si)
endif
if si[0] =~ "n"
call add(b:nmaplist, si)
endif
else
if len(si) == 2
call add(b:nmaplist, si)
endif
endif
endif
endfor
unlet b:kblist2

function! RNMapCmd(plug)
for [el1, el2] in b:nmaplist
if el2 == a:plug
return el1
endif
endfor
endfunction

function! RIMapCmd(plug)
for [el1, el2, el3] in b:imaplist
if el3 == a:plug
return el2
endif
endfor
endfunction

function! RVMapCmd(plug)
for [el1, el2, el3] in b:vmaplist
if el3 == a:plug
return el2
endif
endfor
endfunction

" Example of usage:
echo "The key bounded to <Plug>RStart is " . RNMapCmd("<Plug>RStart")

Best regards,

Jakson Aquino

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

No comments:

Post a Comment