Last active
August 29, 2022 11:47
-
-
Save kat0h/6c0029bb07d48ca52ab559d4b8f2670c to your computer and use it in GitHub Desktop.
Vimのバッファでカウンターを実装するサンプル
This file contains hidden or 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
function! s:State(bufid) abort | |
let l:counter = 0 | |
let l:bufid = a:bufid | |
function! Inc() closure | |
let l:counter += 1 | |
call Flash() | |
endfunction | |
function! Dec() closure | |
let l:counter -= 1 | |
call Flash() | |
endfunction | |
function! Flash() closure | |
call setbufline(l:bufid, 1, string(l:counter)) | |
endfunction | |
return #{ | |
\Inc: funcref('Inc'), | |
\Dec: funcref('Dec'), | |
\Flash: funcref('Flash'), | |
\} | |
endfunction | |
function! g:NewBuffer() abort | |
execute 'new' fnameescape("Counter") | |
setlocal modifiable | |
silent %delete _ | |
setlocal nomodified | |
setlocal buftype=nofile bufhidden=wipe | |
let b:state = s:State(bufnr()) | |
nnoremap <buffer>k <cmd>call b:state.Inc()<CR> | |
nnoremap <buffer>j <cmd>call b:state.Dec()<CR> | |
call setline(2, "j/k to up/down counter") | |
call b:state.Flash() | |
endfunction | |
call g:NewBuffer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment