Last active
October 7, 2015 16:37
-
-
Save kshenoy/3194467 to your computer and use it in GitHub Desktop.
Function to complement the maparg() function in Vim. Given the 'rhs' of a mapping, this function returns the 'lhs'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! MapKey( rhs, mode ) | |
" Description: Get LHS of a mapping. Inverse of maparg(). | |
" Note that hasmapto() returns a binary result while MapKey() returns the value of the LHS. | |
" Pass in a key sequence and the first letter of a vim mode. | |
" Returns key mapping mapped to it in that mode, else '' if none. | |
" Eg: | |
" :nnoremap <Tab> :bn<CR> | |
" :call Mapkey(':bn<CR>', 'n') | |
" returns <Tab> | |
" TODO: | |
" * Modify the function to be capable of returning multiple values. | |
" Eg. if multiple lhs mappings perform the same action then calling MapKey() should return all of them | |
execute 'redir => l:mappings | silent! ' . a:mode . 'map | redir END' | |
" Convert all text between angle-brackets to lowercase | |
" Required to recognize all case-variants of <c-A> and <C-a> as the same thing | |
" Note that Alt mappings are case-sensitive. However, this is not an issue as <A-x> is " replaced with its approriate | |
" keycode for eg. <A-a> becomes á | |
let l:rhs = substitute(a:rhs, '<[^>]\+>', "\\L\\0", 'g') | |
for l:map in split(l:mappings, '\n') | |
" Get rhs for each mapping | |
let l:lhs = split(l:map, '\s\+')[1] | |
let l:lhs_map = maparg(l:lhs, a:mode) | |
if substitute(l:lhs_map, '<[^>]\+>', "\\L\\0", 'g') ==# l:rhs | |
return l:lhs | |
endif | |
endfor | |
return '' | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment