From 0514c88053e83ff5d1bf3c6ba3cfa80565df368e Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Mon, 12 Sep 2022 21:56:35 -0600 Subject: fieldmarshal.vim: Add utility function for modifying motions --- plugin/commenter.vim | 64 ++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) (limited to 'plugin/commenter.vim') diff --git a/plugin/commenter.vim b/plugin/commenter.vim index 351e2dc..0681378 100644 --- a/plugin/commenter.vim +++ b/plugin/commenter.vim @@ -1,10 +1,11 @@ " Function for commenting out blocks of text. noremap cd set operatorfunc=commentg@ -noremap cdd set operatorfunc=commentg@_ +nnoremap cdd set operatorfunc=commentg@_ +nnoremap cD set operatorfunc=commentg@$ -noremap cD set operatorfunc=uncommentg@ -noremap cDD set operatorfunc=uncommentg@_ +noremap czd set operatorfunc=uncommentg@ +nnoremap czdd set operatorfunc=uncommentg@_ " filetype, linecomment, block start, block continue, blockend, blockpad, " inline_support @@ -15,37 +16,17 @@ let s:comment_style = { \ 'vim': ['"', '"', '"', '"', 0, 0] \ } -function! s:modifyreg(arg, cb) abort - let save_a = getreg('a') - let save_a_type = getregtype('a') - - if a:arg == 'line' - let vis = "'[V']" - elseif a:arg == 'block' - let vis = "`[\`]" - else - let vis = "`[v`]" - endif - silent! exec printf("norm! %s\"ay", vis) - - let ls = getreg('a', 1, 1) - let regtype = getregtype('a') - - let ls = a:cb.operate(ls) - - call setreg('a', ls, regtype) - silent! exec "norm! gv\"ap" - call setreg('a', save_a, save_a_type) -endfunction function! s:uncomment(arg, ...) abort - let [line_com, bstart, bcont, bend, bpad, inl] = get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0]) + let [line_com, bstart, bcont, bend, bpad, inl] = + \ get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0]) let l1 = line("'[") let l2 = line("']") if inl - silent exec printf('silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(bstart, '/')) + silent exec printf( + \ 'silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(bstart, '/')) silent exec printf('silent! %s,%s s/\V\zs\s\?%s\ze//', l1, l2, escape(bend, '/')) endif @@ -55,16 +36,13 @@ endfunction function! s:comment(arg, ...) abort let cb = { 'arg': a:arg } + normal! m` - function! cb.operate(ls) dict abort + function! cb.operate(ls, t) dict abort let [line_com, bstart, bcont, bend, bpad, inl] = get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0]) let smallest = 100 let ls = a:ls - if self.arg != 'line' && !inl - throw "Inline comments not allowed" - endif - if self.arg == 'line' || len(ls) > 1 for l in ls if ! (l =~ '^\s*$') @@ -86,20 +64,28 @@ function! s:comment(arg, ...) abort let i += 1 endwhile else + if self.arg != 'line' && !inl + throw "Inline comments not allowed" + endif + let ls[0] = substitute(ls[0], '^', bstart . ' ', '') let ls[-1] = substitute(ls[-1], '$', ' ' . bend, '') endif - else - let i = 0 - while i < len(ls) - let ls[i] = printf("%s %s %s", bstart, ls[i], bend) - let i += 1 - endwhile + let l = getline('.') + if l[len(l) - len(ls[0]):] ==# ls[0] + let ls[0] = printf("%s %s", line_com, ls[0]) + else + if !inl + throw "Inline comments not allowed" + endif + let ls[0] = printf("%s %s %s", bstart, ls[0], bend) + endif endif return ls endfunction - call s:modifyreg(a:arg, cb) + call fieldmarshal#modifytext(a:arg, cb) + norm `` endfunction -- cgit