1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-24 02:10:05 +08:00

Handle input

This commit is contained in:
wsdjeg 2017-07-22 08:33:41 +08:00
parent b202edc9b6
commit 441cb9e1d4
2 changed files with 33 additions and 4 deletions

View File

@ -3,6 +3,7 @@ let s:self = {}
let s:self._keys = {} let s:self._keys = {}
let s:self._on_syntax = '' let s:self._on_syntax = ''
let s:self._title = 'Transient State' let s:self._title = 'Transient State'
let s:self._handle_inputs = {}
function! s:self.open() abort function! s:self.open() abort
noautocmd rightbelow split __transient_state__ noautocmd rightbelow split __transient_state__
@ -28,21 +29,37 @@ function! s:self.open() abort
call append(line('$'), '[KEY] exits state [KEY] will not exit') call append(line('$'), '[KEY] exits state [KEY] will not exit')
call self.highlight_keys(1, line('$') - 1, 1, 4) call self.highlight_keys(1, line('$') - 1, 1, 4)
call self.highlight_keys(0, line('$') - 1, 21, 24) call self.highlight_keys(0, line('$') - 1, 21, 24)
if winheight(0) > line('$')
exe 'resize ' . line('$')
endif
" move to prvious window " move to prvious window
wincmd w wincmd w
redraw! redraw!
while 1 while 1
let char = getchar() let char = self._getchar()
if char ==# "\<FocusLost>" || char ==# "\<FocusGained>" || char2nr(char) == 128 if char ==# "\<FocusLost>" || char ==# "\<FocusGained>" || char2nr(char) == 128
continue continue
endif endif
if !has_key(self._keys, char) if !has_key(self._handle_inputs, char)
break break
else
if type(self._handle_inputs[char]) == 2
call call(self._handle_inputs[char], [])
else
if type(self._handle_inputs[char]) == 1
exe self._handle_inputs[char]
endif
endif endif
endwhile endwhile
exe 'bd ' . self._bufid exe 'bd ' . self._bufid
endfunction endfunction
function! s:self._getchar(...) abort
let ret = call('getchar', a:000)
return (type(ret) == type(0) ? nr2char(ret) : ret)
endfunction
function! s:self.defind_keys(dict) abort function! s:self.defind_keys(dict) abort
let self._keys = a:dict let self._keys = a:dict
endfunction endfunction
@ -76,10 +93,22 @@ function! s:self._update_content() abort
let line = '' let line = ''
if !empty(left) if !empty(left)
let line .= '[' . left.key . '] ' . left.desc let line .= '[' . left.key . '] ' . left.desc
call self.highlight_keys(left.exit, i + 2, 1, 1 + len(left.key))
if !empty(left.cmd)
call extend(self._handle_inputs, {left.key : left.cmd})
elseif !empty(left.func)
call extend(self._handle_inputs, {left.key : left.func})
endif
endif endif
let line .= repeat(' ', 40 - len(line)) let line .= repeat(' ', 40 - len(line))
if !empty(right) if !empty(right)
let line .= '[' . right.key . '] ' . right.desc let line .= '[' . right.key . '] ' . right.desc
call self.highlight_keys(right.exit, i + 2, 41, 41 + len(right.key))
if !empty(right.cmd)
call extend(self._handle_inputs, {right.key : right.cmd})
elseif !empty(right.func)
call extend(self._handle_inputs, {right.key : right.func})
endif
endif endif
call append(line('$'), line) call append(line('$'), line)
endfor endfor

View File

@ -135,12 +135,12 @@ function! SpaceVim#layers#edit#config() abort
endfunction endfunction
function! s:move_text_down_transient_state() abort function! s:move_text_down_transient_state() abort
normal! "_ddp normal! ddp
call s:text_transient_state() call s:text_transient_state()
endfunction endfunction
function! s:move_text_up_transient_state() abort function! s:move_text_up_transient_state() abort
normal! "_ddkP normal! ddkP
call s:text_transient_state() call s:text_transient_state()
endfunction endfunction