aboutsummaryrefslogtreecommitdiffhomepage
path: root/.vim/my/ftplugin/todolist.vim
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2020-09-08 06:14:51 +0900
committernsfisis <nsfisis@gmail.com>2020-09-08 06:14:51 +0900
commitd2732d24689189f02d1b3680b5c560a678321564 (patch)
tree40a474a7efa3a3642e9f679fd0d98eac979bb044 /.vim/my/ftplugin/todolist.vim
parent7574b72a50cf2f4658e7695afd3367fec028f5e1 (diff)
downloaddotfiles-d2732d24689189f02d1b3680b5c560a678321564.tar.gz
dotfiles-d2732d24689189f02d1b3680b5c560a678321564.tar.zst
dotfiles-d2732d24689189f02d1b3680b5c560a678321564.zip
Add .vim
Diffstat (limited to '.vim/my/ftplugin/todolist.vim')
-rw-r--r--.vim/my/ftplugin/todolist.vim71
1 files changed, 71 insertions, 0 deletions
diff --git a/.vim/my/ftplugin/todolist.vim b/.vim/my/ftplugin/todolist.vim
new file mode 100644
index 0000000..e65faa3
--- /dev/null
+++ b/.vim/my/ftplugin/todolist.vim
@@ -0,0 +1,71 @@
+scriptencoding utf-8
+
+
+if exists('b:did_ftplugin_todolist')
+ finish
+endif
+
+
+
+inoremap <silent> <buffer> <expr> <Space> <SID>checkbox(0, ' ')
+inoremap <silent> <buffer> <expr> x <SID>checkbox(1, 'x')
+
+nnoremap <silent> <buffer> <Plug>(todolist-toggle-checkbox) :<C-u>call <SID>toggle_checkbox()<CR>
+nnoremap <silent> <buffer> <Plug>(todolist-toggle-checkbox-rec) :<C-u>call <SID>toggle_checkbox_rec()<CR>
+
+
+function! s:checkbox(check, char)
+ let line = getline('.')
+ if line =~# '^\s*$' && len(line) % shiftwidth() == 0
+ if a:check
+ return '[x] '
+ else
+ return '[ ] '
+ endif
+ else
+ return a:char
+ endif
+endfunction
+
+
+function! s:toggle_checkbox()
+ call s:toggle_checkbox_internal(line('.'), -1)
+endfunction
+
+
+function! s:toggle_checkbox_rec()
+ let checked = s:toggle_checkbox_internal(line('.'), -1)
+ if checked == -1
+ return
+ endif
+ if line('.') == line('$')
+ return
+ endif
+
+ let indent_lv = indent('.')
+ for l in range(line('.') + 1, line('$'))
+ if indent(l) <= indent_lv
+ break
+ end
+ call s:toggle_checkbox_internal(l, checked)
+ endfor
+endfunction
+
+
+function! s:toggle_checkbox_internal(line_num, ck_force)
+ let line = getline(a:line_num)
+ let match = matchlist(line, '^\(\s*\)\[\([x ]\)\]\(.*\)')
+ if empty(match)
+ return -1
+ endif
+
+ let [whole, spaces, ck, rest; _] = match
+ let checked = a:ck_force == -1 ? ck == 'x' : a:ck_force
+ let line = spaces . '[' . (checked ? ' ' : 'x') . ']' . rest
+ call setline(a:line_num, line)
+ return checked
+endfunction
+
+
+
+let b:did_ftplugin_todolist = 1