From a3caef03c883cd9711ac60996227e13e1199e4db Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 13 Sep 2022 01:19:54 -0600 Subject: commenter.vim: crude text object for editing comments Adds the text object i/ --- plugin/commenter.vim | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'plugin') diff --git a/plugin/commenter.vim b/plugin/commenter.vim index 0681378..982f7c2 100644 --- a/plugin/commenter.vim +++ b/plugin/commenter.vim @@ -4,9 +4,74 @@ 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 = { @@ -16,7 +81,6 @@ let s:comment_style = { \ 'vim': ['"', '"', '"', '"', 0, 0] \ } - function! s:uncomment(arg, ...) abort let [line_com, bstart, bcont, bend, bpad, inl] = -- cgit