Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add maps provider to list key mappings #293

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions autoload/clap/provider/maps.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
" Author: Mark Wu <markplace@gmail.com>
" Description: List the maps.

let s:save_cpo = &cpoptions
set cpoptions&vim

" Derived from fzf.vim
let s:allowed_mode = ['n', 'i', 'x', 'o']

function! s:align_pairs(list) abort
let maxlen = 0
let pairs = []
for elem in a:list
let match = matchlist(elem, '^\(\S*\)\s*\(.*\)$')
let [_, k, v] = match[0:2]
let maxlen = max([maxlen, len(k)])
call add(pairs, [k, substitute(v, '^\*\?[@ ]\?', '', '')])
endfor
let maxlen = min([maxlen, 35])
return map(pairs, "printf('%-'.maxlen.'s', v:val[0]).' '.v:val[1]")
endfunction

function! s:maps_source() abort
let mode = get(g:clap.context, 'mode', 'n')
if index(s:allowed_mode, mode) == -1
let mode = 'n'
endif

let s:map_gv = mode ==# 'x' ? 'gv' : ''
let s:map_cnt = v:count == 0 ? '' : v:count
let s:map_reg = empty(v:register) ? '' : ('"'.v:register)
let s:map_op = mode ==# 'o' ? v:operator : ''

redir => cout
silent execute 'verbose' mode.'map'
redir END
let list = []
let curr = ''
for line in split(cout, "\n")
if line =~# "^\t"
let src = ' '.join(reverse(reverse(split(split(line)[-1], '/'))[0:2]), '/')
call add(list, printf('%s %s', curr, src))
let curr = ''
else
let curr = line[3:]
endif
endfor
if !empty(curr)
call add(list, curr)
endif
return sort(s:align_pairs(list))
endfunction

function! s:maps_sink(selected) abort
let key = matchstr(a:selected, '^\S*')
redraw
call feedkeys(s:map_gv.s:map_cnt.s:map_reg, 'n')
call feedkeys(s:map_op.
\ substitute(key, '<[^ >]\+>', '\=eval("\"\\".submatch(0)."\"")', 'g'))
endfunction

let s:maps = {}
let s:maps.sink = function('s:maps_sink')
let s:maps.source = function('s:maps_source')

let g:clap#provider#maps# = s:maps

let &cpoptions = s:save_cpo
unlet s:save_cpo
11 changes: 11 additions & 0 deletions doc/clap-support.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ Supported Providers & Tools *clap-support*
*:Clap-providers*
:Clap providers List the clap providers.


*:Clap-maps*
:Clap maps List the maps.

Use `Clap maps ++mode=[n|i|x|o]` to list key mappings
for different mode.

By default `Clap maps` without `++mode` argument will
list key mappings for normal mode.


*:Clap-marks*
:Clap marks List Marks
Support preview
Expand Down