checkboxes.vim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. " vim: set fdm=marker et ts=4 sw=4 sts=4:
  2. function! pandoc#keyboard#checkboxes#Init() abort "{{{1
  3. " Toggle existing checkbox or insert checkbox.
  4. noremap <buffer> <silent> <Plug>(pandoc-keyboard-toggle-cb) :call pandoc#keyboard#checkboxes#Toggle()<cr>
  5. " Remove existing checkbox
  6. noremap <buffer> <silent> <Plug>(pandoc-keyboard-delete-cb) :call pandoc#keyboard#checkboxes#Delete()<cr>
  7. if g:pandoc#keyboard#use_default_mappings == 1 && index(g:pandoc#keyboard#blacklist_submodule_mappings, 'checkboxes') == -1
  8. nmap <buffer> <localleader>cb <Plug>(pandoc-keyboard-toggle-cb)
  9. vmap <buffer> <localleader>cb <Plug>(pandoc-keyboard-toggle-cb)
  10. nmap <buffer> <localleader>cd <Plug>(pandoc-keyboard-delete-cb)
  11. vmap <buffer> <localleader>cd <Plug>(pandoc-keyboard-delete-cb)
  12. endif
  13. endfunction
  14. " Functions: {{{1
  15. function! pandoc#keyboard#checkboxes#Toggle() abort "{{{2
  16. let l:line=getline('.')
  17. let l:curs=winsaveview()
  18. " match '- [ ]' and replace with '- [x]'
  19. if l:line=~?'^\s*\(-\|\*\|+\)\s*\[ \] .*'
  20. :call setline(line('.'), substitute(l:line, '\[ \]', '\[x\]', ''))
  21. " match '- [x]' and replace with '- [ ]'
  22. elseif l:line=~?'^\s*\(-\|\*\|+\)\s*\[x\] .*'
  23. :call setline(line('.'), substitute(l:line, '\[x\]', '\[ \]', ''))
  24. " match list item that does not have a [ at the beginning
  25. elseif l:line=~?'^\s*\(-\|\*\|+\)\s*[^\[].*'
  26. :call setline(line('.'), substitute(l:line, '^\s*\(-\|\*\|+\)', '\0 \[ \]', ''))
  27. endif
  28. call winrestview(l:curs)
  29. endfunction
  30. function! pandoc#keyboard#checkboxes#Delete() abort "{{{2
  31. let l:line=getline('.')
  32. let l:curs=winsaveview()
  33. if l:line=~?'^\s*\(-\|\*\|+\)\s*\[[^\]]\] .*'
  34. :call setline(line('.'), substitute(l:line, '^\s*\(-\|\*\|+\)\s*\zs\(\[[^\]]\]\) \ze', '', ''))
  35. endif
  36. call winrestview(l:curs)
  37. endfunction