aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2022-09-13 01:19:54 -0600
committerJosh Rahm <joshuarahm@gmail.com>2022-09-13 01:23:56 -0600
commita3caef03c883cd9711ac60996227e13e1199e4db (patch)
tree7f888dfb1f82664eecde14b1112fc4288495f0eb
parent0514c88053e83ff5d1bf3c6ba3cfa80565df368e (diff)
downloadfieldmarshal.vim-a3caef03c883cd9711ac60996227e13e1199e4db.tar.gz
fieldmarshal.vim-a3caef03c883cd9711ac60996227e13e1199e4db.tar.bz2
fieldmarshal.vim-a3caef03c883cd9711ac60996227e13e1199e4db.zip
commenter.vim: crude text object for editing comments
Adds the text object i/
-rw-r--r--plugin/commenter.vim66
1 files changed, 65 insertions, 1 deletions
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 <cmd>set operatorfunc=<sid>comment<cr>g@
nnoremap cdd <cmd>set operatorfunc=<sid>comment<cr>g@_
nnoremap cD <cmd>set operatorfunc=<sid>comment<cr>g@$
+" Comment out the rest of the line and put the cursor into insert mode
+noremap cdC c<Plug><SID>(comment-change-object-i)
+
+" Comment out the line and go into insert mode before the first non-blank
+" character.
+noremap cdcc c<Plug><SID>(comment-change-object-I)
+
+onoremap <Plug><SID>(comment-change-object-i) <cmd>call <sid>change_comment_obj('i')<cr>
+onoremap <Plug><SID>(comment-change-object-I) <cmd>call <sid>change_comment_obj('I')<cr>
+
+onoremap i/ <cmd>call <sid>inner_comment(v:operator)<cr>
+vnoremap i/ <cmd>call <sid>inner_comment('')<cr>
+
noremap czd <cmd>set operatorfunc=<sid>uncomment<cr>g@
nnoremap czdd <cmd>set operatorfunc=<sid>uncomment<cr>g@_
+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\)\@<!', lc), 'W')
+ exec "normal! k$m>"
+ let l = search(printf('\V\^\s\*\S\(%s\)\@<!', lc), 'bW')
+ if l == 0
+ normal! 0m<
+ else
+ exec "normal! j0m<"
+ endif
+
+ normal! gvoh
+
+ if a:op == 'c'
+ exec printf("normal! \<esc>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<gvo"
+
+ if l1 != l2 && len(v:operator) > 0
+ exec printf("normal! \<esc>i%s gv%dll", bcont, len(bcont))
+ else
+ " exec printf("normal! \<esc>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 <left><right><up><down> stop
+ " recording into the redobuff. This is a way to get around that.
+ "
+ " exec "normal! i \<esc>v"
+ let [line_com, bstart, bcont, bend, bpad, inl] =
+ \ get(s:comment_style, &ft, ['#', '#', '#', '#', 0, 0])
+ exec printf("normal! %s %s \<esc>%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] =