From 788ffcb3cd6b50f34131fd71c6ba4441aeea8d76 Mon Sep 17 00:00:00 2001 From: acelya Date: Mon, 23 Nov 2020 19:36:37 +0100 Subject: [PATCH] Add comment and yank (#138) --- doc/commentary.txt | 11 +++++++++++ plugin/commentary.vim | 34 ++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/doc/commentary.txt b/doc/commentary.txt index b53eca7..4cfb12a 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.) @@ -46,3 +56,4 @@ The |User| CommentaryPost autocommand fires after a successful operation and can be used for advanced customization. vim:tw=78:et:ft=help:norl: + diff --git a/plugin/commentary.vim b/plugin/commentary.vim index 304fbb9..bc09b4f 100644 --- a/plugin/commentary.vim +++ b/plugin/commentary.vim @@ -32,17 +32,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] = commentary#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, @@ -66,7 +78,7 @@ 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