" Function for commenting out blocks of text. noremap cd set operatorfunc=commentg@ nnoremap cdd set operatorfunc=commentg@_ nnoremap cD set operatorfunc=commentg@$ " Comment out the rest of the line and put the cursor into insert mode noremap cdC c(comment-change-object-i) " Comment out the line and go into insert mode before the first non-blank " character. noremap cdcc c(comment-change-object-I) onoremap (comment-change-object-i) call change_comment_obj('i') onoremap (comment-change-object-I) call change_comment_obj('I') onoremap i/ call inner_comment(v:operator) vnoremap i/ call inner_comment('') noremap czd set operatorfunc=uncommentg@ nnoremap czdd set operatorfunc=uncommentg@_ function! s:inner_comment(op) let [line_com, bstart, bcont, bend, bpad, inl] = \ get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0]) let lc = escape(line_com, '/') if getline('.') =~ printf('\V\^\s\*%s', lc) call search(printf('\V\^\s\*\S\(%s\)\@" let l = search(printf('\V\^\s\*\S\(%s\)\@i%s gv%dll", line_com, len(line_com)) endif else let cs = escape(bstart, '/') let ce = escape(bend, '/') normal! k let l2 = search('\V\zs\S\_s\*' . ce, 'Wc') exec "normal! m>" let l1 = search('\V' . cs . '\S\*\_s\?\zs', 'bW') exec "normal! m 0 exec printf("normal! \i%s gv%dll", bcont, len(bcont)) else " exec printf("normal! \i%s gv%dllo%dll", bcont, len(bcont), len(bcont)) endif endif endfunction function! s:change_comment_obj(mv) abort if v:operator == 'c' " This is a cheese to allow the cdcc and cdC commands to be repeated with " the dot operator. When in insert mode stop " recording into the redobuff. This is a way to get around that. " " exec "normal! i \v" let [line_com, bstart, bcont, bend, bpad, inl] = \ get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0]) exec printf("normal! %s %s \%dhvl", a:mv, line_com, len(line_com) + 3) else endif endfunction " filetype, linecomment, block start, block continue, blockend, blockpad, " inline_support let s:comment_style = { \ 'java': ['//', '/*', ' *', '*/', 1, 1], \ 'c': ['//', '/*', ' *', '*/', 1, 1], \ 'cpp': ['//', '/*', ' *', '*/', 1, 1], \ 'vim': ['"', '"', '"', '"', 0, 0] \ } function! s:uncomment(arg, ...) abort 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(bend, '/')) endif silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(line_com, '/')) silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(bcont, '/')) endfunction function! s:comment(arg, ...) abort let cb = { 'arg': a:arg } normal! m` 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' || len(ls) > 1 for l in ls if ! (l =~ '^\s*$') let sz = match(l, '^\(\s\+\)\zs\S\ze') + 1 if sz <= 0 let sz = 1 endif if sz < smallest && sz > 0 let smallest = sz endif endif endfor if self.arg == 'line' let i = 0 while i < len(ls) let ls[i] = substitute(ls[i], printf('\%%%dc', smallest), line_com . ' ', '') 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 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 fieldmarshal#modifytext(a:arg, cb) norm `` endfunction