diff --git a/doc/commentary.txt b/doc/commentary.txt index d6deed0..f026eae 100644 --- a/doc/commentary.txt +++ b/doc/commentary.txt @@ -29,6 +29,16 @@ 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. + 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 a91cd71..ea1949e 100644 --- a/plugin/commentary.vim +++ b/plugin/commentary.vim @@ -72,6 +72,24 @@ function! s:textobject(inner) abort endif endfunction +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 + xnoremap Commentary :call go(line("'<"),line("'>")) nnoremap Commentary :set opfunc=gog@ nnoremap CommentaryLine :set opfunc=goexe 'norm! 'v:count1.'g@_' @@ -80,6 +98,11 @@ nnoremap ChangeCommentary c:call textobject(1) nmap CommentaryUndo CommentaryCommentary command! -range -bar Commentary call s:go(,) +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 Commentary :call go(line("'<"),line("'>")) if !hasmapto('Commentary') || maparg('gc','n') ==# '' xmap gc Commentary nmap gc Commentary @@ -87,6 +110,9 @@ if !hasmapto('Commentary') || maparg('gc','n') ==# '' nmap gcc CommentaryLine nmap cgc ChangeCommentary nmap gcu CommentaryCommentary + xmap gcy CommentaryYank + nmap gcy CommentaryYank + nmap gcyy CommentaryYankLine endif if maparg('\\','n') ==# '' && maparg('\','n') ==# '' && get(g:, 'commentary_map_backslash', 1)