diff --git a/doc/commentary.txt b/doc/commentary.txt index 01f73da..941c12c 100644 --- a/doc/commentary.txt +++ b/doc/commentary.txt @@ -18,6 +18,16 @@ gcc Comment or uncomment [count] lines. *v_gc* {Visual}gc Comment or uncomment the highlighted lines. + *gy* +gy{motion} Yank and comment or uncomment lines that {motion} + moves over. + + *gyy* +gyy Yank and comment or uncomment [count] lines. + + *v_gy* +{Visual}gy Yank and comment or uncomment the highlighted lines. + *o_gc* gc Text object for a comment (operator pending mode only.) diff --git a/plugin/commentary.vim b/plugin/commentary.vim index 7dfc96c..c279a11 100644 --- a/plugin/commentary.vim +++ b/plugin/commentary.vim @@ -26,17 +26,29 @@ endfunction function! s:go(...) abort if !a:0 - let &operatorfunc = matchstr(expand(''), '[^. ]*$') + let &operatorfunc = expand('') . 'go' return 'g@' - elseif a:0 > 1 - let [lnum1, lnum2] = [a:1, a:2] - else - let [lnum1, lnum2] = [line("'["), line("']")] endif + let [l1, l2] = a:0 > 1 ? [a:1, a:2] : [line("'["), line("']")] + call s:apply(l1, l2) +endfunction + +function! s:yank(...) abort + if !a:0 + let &operatorfunc = expand('') . 'yank' + return 'g@' + endif + + let [l1, l2] = a:0 > 1 ? [a:1, a:2] : [line("'["), line("']")] + execute l1 . ',' . l2 . 'yank' v:register + call s:apply(l1, l2) +endfunction + +function! s:apply(lnum1, lnum2) abort let [l, r] = s:surroundings() let uncomment = 2 - for lnum in range(lnum1,lnum2) + for lnum in range(a:lnum1,a:lnum2) let line = matchstr(getline(lnum),'\S.*\s\@ 2 && l.r !~# '\\' let line = substitute(line, @@ -61,11 +73,11 @@ function! s:go(...) abort if uncomment let line = substitute(line,'\S.*\s\@ CommentaryLine go() . '_' onoremap Commentary :call textobject(get(v:, 'operator', '') ==# 'c') nnoremap ChangeCommentary c:call textobject(1) nmap CommentaryUndo :echoerr "Change your CommentaryUndo map to CommentaryCommentary" +xnoremap (CommentaryYank) yank() +nnoremap (CommentaryYank) yank() +nnoremap (CommentaryYankLine) yank() . '_' if !hasmapto('Commentary') || maparg('gc','n') ==# '' xmap gc Commentary nmap gc Commentary omap gc Commentary nmap gcc CommentaryLine + xmap gy (CommentaryYank) + nmap gy (CommentaryYank) + nmap gyy (CommentaryYankLine) if maparg('c','n') ==# '' && !exists('v:operator') nmap cgc ChangeCommentary endif