Skip to content

Instantly share code, notes, and snippets.

@kat0h
Last active August 29, 2022 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kat0h/6c0029bb07d48ca52ab559d4b8f2670c to your computer and use it in GitHub Desktop.
Save kat0h/6c0029bb07d48ca52ab559d4b8f2670c to your computer and use it in GitHub Desktop.
Vimのバッファでカウンターを実装するサンプル
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