Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow configuring which_key per buffer #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions autoload/which_key.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ let g:which_key#TYPE = s:TYPE

let s:should_note_winid = exists('*win_getid')

let s:buffer_cache = {}
let s:cur_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')
let g:which_key#extensions = map(
\ split(globpath(s:cur_dir.'/which_key/extensions', '*'), '\n'),
\ 'fnamemodify(v:val, '':t:r'')')

function! which_key#register(prefix, dict) abort
let key = a:prefix ==? '<Space>' ? ' ' : a:prefix
let val = a:dict
Expand Down Expand Up @@ -43,6 +49,50 @@ function! s:handle_char_on_start_is_ok(c) abort
endif
endfunction

function! which_key#start_buffer(vis, ...) abort
let key = '<buffer>'
if empty(s:buffer_cache) || g:which_key_run_map_on_popup
let s:buffer_cache = {}
call which_key#mappings#parse(key, s:buffer_cache, s:vis ==# 'gv' ? 1 : 0)
endif

let native = s:buffer_cache

try
let l:extension_name = s:get_extension_name()
let l:ext_config = g:which_key#extensions#{l:extension_name}#config
catch /^Vim\%((\a\+)\)\=:E121/
echom v:exception
endtry

if exists('b:which_key')
let buffer_which_key = copy(b:which_key)
if get(b:, 'which_key_no_native', 0)
let s:runtime = buffer_which_key
else
call s:merge(buffer_which_key, native)
let s:runtime = buffer_which_key
endif
else

if exists('l:ext_config')
if !get(l:ext_config, 'no_native', v:false)
call s:merge(l:ext_config['mappings'], native)
endif
let s:runtime = l:ext_config['mappings']
else
let s:runtime = native
endif
endif

call which_key#window#show(s:runtime)
endfunction

" Some plugins use the filetype name like coc-explorer.
function! s:get_extension_name() abort
return substitute(&filetype, '-', '_', 'g')
endfunction

function! which_key#start(vis, bang, prefix) " {{{
let s:vis = a:vis ? 'gv' : ''
let s:count = v:count != 0 ? v:count : ''
Expand All @@ -52,6 +102,13 @@ function! which_key#start(vis, bang, prefix) " {{{
let g:which_key_origin_winid = win_getid()
endif

if a:prefix == '<buffer>'
\ || exists('b:which_key')
\ || index(g:which_key#extensions, s:get_extension_name()) > -1
call which_key#start_buffer(a:vis)
return
endif

if a:bang
let s:runtime = a:prefix
let s:last_runtime_stack = [copy(s:runtime)]
Expand Down
25 changes: 25 additions & 0 deletions autoload/which_key/extensions/coc_explorer.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let s:mappings = {
\ 'h': ['<Plug>(coc-explorer-key-n-h)', 'collapse node'],
\ 'l': ['<Plug>(coc-explorer-key-n-l)', 'expand node'],
\ 'o': ['<Plug>(coc-explorer-key-n-o)', 'expand & collapse node'],
\ 'e': ['<Plug>(coc-explorer-key-n-e)', 'open'],
\ 'gs': ['<Plug>(coc-explorer-key-n-gs)', 'reveal buffer in explorer'],
\ 'gp': ['<Plug>(coc-explorer-key-n-gp)', 'preview'],
\ 'y': ['<Plug>(coc-explorer-key-n-y)', 'copy full filepath to clipboard'],
\ 'Y': ['<Plug>(coc-explorer-key-n-Y)', 'copy filename to clipboard'],
\ 'c': ['<Plug>(coc-explorer-key-n-c)', 'copy file for paste'],
\ 'x': ['<Plug>(coc-explorer-key-n-x)', 'cut file for paste'],
\ 'p': ['<Plug>(coc-explorer-key-n-p)', 'paste files to here'],
\ 'd': ['<Plug>(coc-explorer-key-n-d)', 'move file or directory to trash'],
\ 'D': ['<Plug>(coc-explorer-key-n-D)', 'delete file or directory forever'],
\ 'a': ['<Plug>(coc-explorer-key-n-a)', 'add a new file'],
\ 'A': ['<Plug>(coc-explorer-key-n-A)', 'add a new directory'],
\ 'r': ['<Plug>(coc-explorer-key-n-r)', 'rename'],
\ '.': ['<Plug>(coc-explorer-key-n-.)', 'toggle hidden'],
\ 'R': ['<Plug>(coc-explorer-key-n-R)', 'refresh'],
\ }

let g:which_key#extensions#coc_explorer#config = {
\ 'no_native': v:true,
\ 'mappings': s:mappings,
\ }
30 changes: 30 additions & 0 deletions autoload/which_key/extensions/nerdtree.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let s:mappings = {
\ 't': 'open in new tab',
\ 'T': 'open in new tab silently',
\ 'i': 'open split',
\ 's': 'open vsplit',
\ 'gs': 'preview vsplit',
\ 'o': 'open & close node',
\ 'O': 'recursively open node',
\ 'x': 'close parent of node',
\ 'X': 'close all child nodes of current node recursively',
\ 'e': 'explore selected dir',
\ 'P': 'go to root',
\ 'p': 'go to parent',
\ 'K': 'go to first child',
\ 'J': 'go to last child',
\ 'C': 'change tree root to the selected dir',
\ 'u': 'move tree root up a dir',
\ 'U': 'move tree root up a dir but leave old root open',
\ 'r': 'refresh cursur dir',
\ 'R': 'refresh current root',
\ 'm': 'Show menu',
\ 'q': 'Close the NERDTree window',
\ 'A': 'Zoom (maximize-minimize) the NERDTree window',
\ '?': 'toggle help',
\ }

let g:which_key#extensions#nerdtree#config = {
\ 'no_native': v:false,
\ 'mappings': s:mappings,
\ }
7 changes: 7 additions & 0 deletions autoload/which_key/extensions/vista.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let g:which_key#extensions#vista#config = {
\ 'mappings': {
\ '/': 'open-symbols-finder',
\ 'q': 'close-vista-sidebar',
\ '<CR>': 'jump-or-fold',
\ }
\ }
7 changes: 7 additions & 0 deletions autoload/which_key/extensions/vista_kind.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let g:which_key#extensions#vista_kind#config = {
\ 'mappings': {
\ '/': 'open-symbols-finder',
\ 'q': 'close-vista-sidebar',
\ '<CR>': 'jump-or-fold',
\ }
\ }