diff --git a/doc/commentary.txt b/doc/commentary.txt index 01f73da..c95b298 100644 --- a/doc/commentary.txt +++ b/doc/commentary.txt @@ -29,6 +29,26 @@ gcu *:Commentary* :[range]Commentary Comment or uncomment [range] lines +In addition, you can yank a section of text before commenting it out: + +gcy{motion} Yank and then comment or uncomment the lines that + {motion} moves over. + +gcy Yank and then comment or uncomment [count] lines. + +{Visual}gcy Yank and then comment or uncomment the highlighted + lines. + +You can also duplicate a section of text before commenting it out: + +gcd{motion} Duplicate and then comment or uncomment the lines that + {motion} moves over. + +gcd Duplicate and then comment or uncomment [count] lines. + +{Visual}gcd Duplicate and then comment or uncomment the highlighted + lines. + The |User| CommentaryPost autocommand fires after a successful operation and can be used for advanced customization. diff --git a/plugin/commentary.vim b/plugin/commentary.vim index 2c81622..9a61c88 100644 --- a/plugin/commentary.vim +++ b/plugin/commentary.vim @@ -121,4 +121,54 @@ if !hasmapto('Commentary') || maparg('gc','n') ==# '' nmap gcu CommentaryCommentary endif +if !get(g:, "commentary_disable_dupe_and_comment_mappings", 0) + + function! s:setcommentaryreg(reg) + let s:targetreg = a:reg + endfunction + + function! s:yankandcomment(type,...) + " only linewise operations make sense (to me, at least) + " so I am ignoring `type` + if a:0 + let [mark1, mark2] = [a:type, a:1] + let reg = a:2 + else + let [mark1, mark2] = ["'[", "']"] + let reg = get(s:, "targetreg", '"') + endif + execute 'normal! ' . mark1 . '"' . reg . 'y' . mark2 . ']' + call go(line(mark1),line(mark2)) + execute 'normal! ' . mark1 + endfunction + + function! s:yankcommentpaste(type,...) + if a:0 + let [mark1, mark2] = [a:type, a:1] + else + let [mark1, mark2] = ["'[", "']"] + endif + let savereg = @" + execute "normal " . mark1 ."gcy" . mark2 . "]" + execute "normal! " . mark2 . "p" . mark1 + let @" = savereg + endfunction + + xnoremap CommentaryYank :callyankandcomment("'<", "'>", v:register) + nnoremap CommentaryYank :call setcommentaryreg(v:register):set opfunc=yankandcommentg@ + nnoremap CommentaryYankLine :call setcommentaryreg(v:register):set opfunc=yankandcommentexe 'norm! 'v:count1.'g@_' + xnoremap CommentaryDupe :callyankcommentpaste("'<", "'>", v:register):normal! '>j + nnoremap CommentaryDupe :call setcommentaryreg(v:register):set opfunc=yankcommentpasteg@ + nnoremap CommentaryDupeLine :call setcommentaryreg(v:register):set opfunc=yankcommentpasteexe 'norm! 'v:count1.'g@_' + xnoremap Commentary :call go(line("'<"),line("'>")) + + xmap gcy CommentaryYank + nmap gcy CommentaryYank + nmap gcyy CommentaryYankLine + xmap gcd CommentaryDupe + nmap gcd CommentaryDupe + nmap gcdd CommentaryDupeLine + +endif + " vim:set et sw=2: