Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into support-per-buffer
  • Loading branch information
liuchengxu committed Oct 12, 2019
2 parents 7116157 + 838f583 commit 76bcb4f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 19 deletions.
46 changes: 37 additions & 9 deletions autoload/which_key.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ function! which_key#start(vis, bang, prefix) " {{{
catch /^Vim:Interrupt$/
return ''
endtry
" <Esc>, <C-[>: 27
if c == 27
if s:is_exit_code(c)
return ''
endif
let char = c == 9 ? '<Tab>' : nr2char(c)
Expand All @@ -113,6 +112,39 @@ function! which_key#start(vis, bang, prefix) " {{{
call which_key#window#open(s:runtime)
endfunction

" Argument: number
function! s:is_exit_code(raw_char) abort
if !exists('s:exit_code')
if exists('g:which_key_exit')
let ty = type(g:which_key_exit)
if ty == s:TYPE.number || ty == s:TYPE.string
let s:exit_code = [g:which_key_exit]
elseif ty == s:TYPE.list
let s:exit_code = g:which_key_exit
else
echohl ErrorMsg
echom '[which-key] '.a:raw_char.' is invalid for option g:which_key_exit'
echohl None
return 1
endif
else
" <Esc>, <C-[>: 27
let s:exit_code = [27]
endif
endif

for e in s:exit_code
let ty = type(e)
if ty == s:TYPE.number && e == a:raw_char
return 1
elseif ty == s:TYPE.string && e == nr2char(a:raw_char)
return 1
endif
endfor

return 0
endfunction

function! s:create_runtime(key)
let key = a:key
if has_key(s:desc, key)
Expand Down Expand Up @@ -213,8 +245,7 @@ function! s:getchar() abort
return ''
endtry

" <Esc>, <C-[>: 27
if c == 27
if s:is_exit_code(c)
call which_key#window#close()
redraw!
return ''
Expand All @@ -241,14 +272,11 @@ function! s:getchar() abort
return ''
endif

" <Tab>, <C-I> = 9
let input .= c == 9 ? '<Tab>' : nr2char(c)

let input .= which_key#util#parse_getchar(c)
if s:has_children(input)
while 1
if !s:wait_with_timeout(g:which_key_timeout)
let c = getchar()
let input .= c == 9 ? '<Tab>' : nr2char(c)
let input .= which_key#util#parse_getchar(getchar())
else
break
endif
Expand Down
31 changes: 29 additions & 2 deletions autoload/which_key/util.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
scriptencoding utf-8

let g:which_key#util#TYPE = {
\ 'string': type(''),
\ 'list': type([]),
\ 'dict': type({}),
\ 'number': type(0),
\ 'string': type(''),
\ 'funcref': type(function('call'))
\ }

Expand Down Expand Up @@ -65,7 +66,7 @@ endfunction

function! which_key#util#undefined(key) abort
echohl ErrorMsg
echom a:key.' is undefined'
echom '[which-key] '.a:key.' is undefined'
echohl None
endfunction

Expand All @@ -77,3 +78,29 @@ function! which_key#util#format(mapping) abort
" let l:ret = substitute(l:ret, '^<Plug>', '', '')
return l:ret
endfunction

function! s:m_char(char)
if a:char == '"'
return ["<M-\">", "\<M-\">"]
endif
let m_char = '<M-' . a:char . '>'
let m_char_code = eval('"\' . m_char . '"')
return [m_char, m_char_code]
endfunction

let s:chars = map(range(32, 126), 'nr2char(v:val)')
let g:which_key#util#special_keys = {"\<C-Space>": "<C-Space>"}
for c in s:chars
let [key, code] = s:m_char(c)
let g:which_key#util#special_keys[code] = key
endfor

function! which_key#util#parse_getchar(input)
if type(a:input) == g:which_key#util#TYPE.number
" <Tab>, <C-I> = 9
return a:input == 9 ? '<Tab>' : nr2char(a:input)
else
" Special characters
return g:which_key#util#special_keys[a:input]
endif
endfunction
29 changes: 22 additions & 7 deletions autoload/which_key/view.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let s:TYPE = g:which_key#util#TYPE
let s:displaynames = {
let s:default_displaynames = {
\ ' ': 'SPC',
\ '<C-H>': 'BS',
\ '<C-I>': 'TAB',
Expand All @@ -13,21 +13,35 @@ function! which_key#view#prepare(runtime) abort
return [layout, rows]
endfunction

function! which_key#view#get_displaynames()
if exists('g:which_key_display_names')
return g:which_key_display_names
else
return s:default_displaynames
endif
endfunction

function! s:calc_layout(mappings) abort " {{{
let layout = {}
let smap = filter(copy(a:mappings), 'v:key !=# "name" && !(type(v:val) == s:TYPE.list && v:val[1] == "which_key_ignore")')
let layout.n_items = len(smap)
let length = values(map(smap,
\ 'strdisplaywidth(get(s:displaynames, toupper(v:key), v:key).'.
\ '(type(v:val) == s:TYPE.dict ? get(v:val, "name", "") : v:val[1]))'))
let displaynames = which_key#view#get_displaynames()

let prefix_length = values(map(copy(smap),
\ 'strdisplaywidth(get(displaynames, toupper(v:key), v:key))'))
let suffix_length = values(map(smap,
\ 'strdisplaywidth(type(v:val) ==s:TYPE.dict ?'.
\ 'get(v:val, "name", "") : v:val[1])'))
let maxlength = max(length) + g:which_key_hspace
let maxlength = max(prefix_length) + max(suffix_length)
\ + strdisplaywidth(g:which_key_sep) + 2
if g:which_key_vertical
let layout.n_rows = winheight(0) - 2
let layout.n_cols = layout.n_items / layout.n_rows + (layout.n_items != layout.n_rows)
let layout.col_width = maxlength
let layout.win_dim = layout.n_cols * layout.col_width
else
let maxlength += g:which_key_hspace
let layout.n_cols = winwidth(0) / maxlength
let layout.n_rows = layout.n_items / layout.n_cols + (fmod(layout.n_items,layout.n_cols) > 0 ? 1 : 0)
let layout.col_width = winwidth(0) / layout.n_cols
Expand Down Expand Up @@ -55,10 +69,11 @@ function! s:create_rows(layout, mappings) abort
let col = 0
let smap = sort(filter(keys(mappings), 'v:val !=# "name"'),'1')
let displaynames = which_key#view#get_displaynames()
if get(g:, 'which_key_align_by_seperator', 1)
let key_max_len = 0
for k in smap
let key = get(s:displaynames, toupper(k), k)
let key = get(displaynames, toupper(k), k)
let width = strdisplaywidth(key)
if width > key_max_len
let key_max_len = width
Expand All @@ -67,7 +82,7 @@ function! s:create_rows(layout, mappings) abort
endif

for k in smap
let key = get(s:displaynames, toupper(k), k)
let key = get(displaynames, toupper(k), k)
let desc = type(mappings[k]) == s:TYPE.dict ? get(mappings[k], "name", "") : mappings[k][1]
if desc == 'which_key_ignore'
continue
Expand Down
32 changes: 32 additions & 0 deletions doc/vim-which-key.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CONTENTS *vim-which-key-contents*
1. Introduction........................................|vim-which-key-intro|
2. Usage...............................................|vim-which-key-usage|
3. Configuration......................................|vim-which-key-config|
3.1 Highlights..................................|vim-which-key-highlights|
4. Commands.........................................|vim-which-key-commands|
5. Functions.......................................|vim-which-key-functions|

Expand Down Expand Up @@ -270,6 +271,15 @@ same with option `timeoutlen`.
>
let g:which_key_timeout = 300
<
*g:which_key_exit*

Type: |Number| | |String| | |List|
Default: `["\<C-[>", "\<Esc>"]`

Exit which-key when the key is triggered. For example, use `<C-G>` to exit: >
let g:which_key_exit = "\<C-G>"
>
*g:which_key_ignore_invalid_key*
Type: |Number|
Default: `1`
Expand All @@ -284,6 +294,15 @@ If you wish to hide a mapping from the menu set its description to
nnoremap <leader>1 :1wincmd w<CR>
let g:which_key_map.1 = 'which_key_ignore'
>
*g:which_key_display_names*
Type: |Dict|
Default: `{ ' ': 'SPC', '<C-H>': 'BS', '<C-I>': 'TAB', '<TAB>': 'TAB', }`

Override the default symbols used for mappings. Dict keys should be all
uppercase.
>
let g:which_key_display_names = {'<CR>': '↵', '<TAB>': '⇆'}
<
==============================================================================
COMMANDS *vim-which-key-commands*

Expand All @@ -308,6 +327,19 @@ COMMANDS *vim-which-key-commands*

Same as |:WhichKey|, but opening the menu in visual mode.


------------------------------------------------------------------------------
|HIGHLIGHTS| *vim-which-key-highlights*

>
highlight default link WhichKey Function
highlight default link WhichKeySeperator DiffAdded
highlight default link WhichKeyGroup Keyword
highlight default link WhichKeyDesc Identifier
highlight default link WhichKeyFloating Pmenu
<

==============================================================================
FUNCTIONS *vim-which-key-functions*

Expand Down
2 changes: 1 addition & 1 deletion syntax/which_key.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let b:current_syntax = 'which_key'
let s:sep = which_key#util#get_sep()

execute 'syntax match WhichKeySeperator' '/'.s:sep.'/' 'contained'
execute 'syntax match WhichKey' '/[^\s].\{1,3}'.s:sep.'/' 'contains=WhichKeySeperator'
execute 'syntax match WhichKey' '/\(^\s*\|\s\{2,}\)\S.\{-}'.s:sep.'/' 'contains=WhichKeySeperator'
syntax match WhichKeyGroup / +[0-9A-Za-z_/-]*/
syntax region WhichKeyDesc start="^" end="$" contains=WhichKey, WhichKeyGroup, WhichKeySeperator

Expand Down

0 comments on commit 76bcb4f

Please sign in to comment.