Skip to content

Commit

Permalink
Add 'yank-and-comment' operation.
Browse files Browse the repository at this point in the history
Support for yanking a section of text before commenting them out. By
default, mapped to '`gcy`'.
  • Loading branch information
jeetsukumaran committed Mar 15, 2015
1 parent 9c68513 commit 976201c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/commentary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
26 changes: 26 additions & 0 deletions plugin/commentary.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SID>go(line(mark1),line(mark2))
execute 'normal! ' . mark1
endfunction

xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@
nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
Expand All @@ -80,13 +98,21 @@ nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary
command! -range -bar Commentary call s:go(<line1>,<line2>)

xnoremap <silent> <Plug>CommentaryYank :<C-U>call<SID>yankandcomment("'<", "'>", v:register)<CR>
nnoremap <silent> <Plug>CommentaryYank :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<CR>g@
nnoremap <silent> <Plug>CommentaryYankLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<Bar>exe 'norm! 'v:count1.'g@_'<CR>
xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
xmap gc <Plug>Commentary
nmap gc <Plug>Commentary
omap gc <Plug>Commentary
nmap gcc <Plug>CommentaryLine
nmap cgc <Plug>ChangeCommentary
nmap gcu <Plug>Commentary<Plug>Commentary
xmap gcy <Plug>CommentaryYank
nmap gcy <Plug>CommentaryYank
nmap gcyy <Plug>CommentaryYankLine
endif

if maparg('\\','n') ==# '' && maparg('\','n') ==# '' && get(g:, 'commentary_map_backslash', 1)
Expand Down

0 comments on commit 976201c

Please sign in to comment.