Created
March 6, 2018 02:50
Star
You must be signed in to star a gist
Interactive input() in simple way (Vim)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:UNIQUE = sha256(expand('<sfile>')) | |
function! s:iinput(callback, prompt, ...) abort | |
let text = a:0 > 0 ? a:1 : '' | |
let comp = a:0 > 1 ? a:2 : v:null | |
let exit = 0 | |
let previous = '' | |
execute printf('cnoremap <buffer> <CR> %sCR<CR>', s:UNIQUE) | |
execute printf('cnoremap <buffer> <Esc> %sESC<CR>', s:UNIQUE) | |
try | |
while 1 | |
call timer_start(100, { -> feedkeys("\<CR>", 'n') }) | |
let r = comp is v:null | |
\ ? input(a:prompt, text) | |
\ : input(a:prompt, text, comp) | |
if r =~# s:UNIQUE . 'ESC$' | |
return v:null | |
elseif r =~# s:UNIQUE . 'CR$' | |
let r = matchstr(r, '.*\ze' . s:UNIQUE . 'CR$') | |
call a:callback(r) | |
return r | |
else | |
call a:callback(r) | |
endif | |
let text = r | |
endwhile | |
finally | |
cunmap <buffer> <CR> | |
cunmap <buffer> <Esc> | |
endtry | |
endfunction | |
vnew | |
function! s:callback(text) abort | |
call setline('$', a:text) | |
redraw | |
endfunction | |
sleep 1m | |
echomsg printf('RESULT: %s', s:iinput(funcref('s:callback'), '> ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment