From 5f73fa74a27145830177d4c9e24223efd70c222c Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 25 Jun 2018 13:53:32 +1200 Subject: Clear local leader maps on filetype change Iterate through all the buffer-local mappings each time the filetype changes, and clear any that begin with the local leader, using two autoloaded functions and one autoload variable for :redir. I really don't think it should be this hard. I hope I haven't missed something in the documentation that makes this easier. I thought maparg() or mapcheck() might do it, but no such luck. Maybe I can refactor this later. --- vim/autoload/vimrc.vim | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 vim/autoload/vimrc.vim (limited to 'vim/autoload') diff --git a/vim/autoload/vimrc.vim b/vim/autoload/vimrc.vim new file mode 100644 index 00000000..eaeefd5d --- /dev/null +++ b/vim/autoload/vimrc.vim @@ -0,0 +1,39 @@ +" Get all buffer-local mappings into a string variable +function! vimrc#GetBufferLocalMaps() abort + redir => l:out + map + redir END + let g:vimrc#buffer_local_maps = l:out +endfunction + +" Clear all buffer-local mappings beginning with +function! vimrc#ClearLocalLeaderMaps() abort + + " Do nothing if there isn't a defined local leader + if !exists('g:maplocalleader') + return + endif + + " Get all the buffer-local mappings into a list + silent call vimrc#GetBufferLocalMaps() + let l:mappings = split(g:vimrc#buffer_local_maps, '\n') + + " Iterate through the mappings + for l:mapping in l:mappings + + " Match the list mapping and mode; skip if no match + let l:matchlist = matchlist(l:mapping, '\m\C^\(.\)\s\+\(\S\+\)') + if !len(l:matchlist) + continue + endif + let l:mode = l:matchlist[1] + let l:sequence = l:matchlist[2] + + " If the mapping starts with our local leader, clear it + if stridx(l:sequence, g:maplocalleader) == 0 + execute l:mode.'unmap '.l:sequence + endif + + endfor + +endfunction -- cgit v1.2.3