aboutsummaryrefslogtreecommitdiff
path: root/autoload/clear_local_maps.vim
blob: 1668dfbfa4e82b56c90458af7839223307976cd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
" Get all buffer-local mappings into a string variable
function! clear_local_maps#GetBufferLocalMaps() abort
  redir => l:out
  map <buffer>
  redir END
  let g:clear_local_maps#buffer_local_maps = l:out
endfunction

" Clear all buffer-local mappings beginning with <LocalLeader>
function! clear_local_maps#ClearBufferLocalLeaderMaps() 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 clear_local_maps#GetBufferLocalMaps()
  let l:mappings = split(g:clear_local_maps#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 <buffer> '.l:sequence
    endif

  endfor

endfunction

" Wrapper around ClearLocalMaps with a shorter name
function! clear_local_maps#Clear() abort
  call clear_local_maps#ClearBufferLocalLeaderMaps()
endfunction