Skip to content

Commit b0f562f

Browse files
committed
Add comment and yank
1 parent f8238d7 commit b0f562f

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

doc/commentary.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ gcc Comment or uncomment [count] lines.
1818
*v_gc*
1919
{Visual}gc Comment or uncomment the highlighted lines.
2020

21+
*gy*
22+
gy{motion} Yank and comment or uncomment lines that {motion}
23+
moves over.
24+
25+
*gyy*
26+
gyy Yank and comment or uncomment [count] lines.
27+
28+
*v_gy*
29+
{Visual}gy Yank and comment or uncomment the highlighted lines.
30+
2131
*o_gc*
2232
gc Text object for a comment (operator pending mode
2333
only.)

plugin/commentary.vim

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,29 @@ endfunction
2626

2727
function! s:go(...) abort
2828
if !a:0
29-
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
29+
let &operatorfunc = expand('<SID>') . 'go'
3030
return 'g@'
31-
elseif a:0 > 1
32-
let [lnum1, lnum2] = [a:1, a:2]
33-
else
34-
let [lnum1, lnum2] = [line("'["), line("']")]
3531
endif
3632

33+
let [l1, l2] = a:0 > 1 ? [a:1, a:2] : [line("'["), line("']")]
34+
call s:apply(l1, l2)
35+
endfunction
36+
37+
function! s:yank(...) abort
38+
if !a:0
39+
let &operatorfunc = expand('<SID>') . 'yank'
40+
return 'g@'
41+
endif
42+
43+
let [l1, l2] = a:0 > 1 ? [a:1, a:2] : [line("'["), line("']")]
44+
execute l1 . ',' . l2 . 'yank' v:register
45+
call s:apply(l1, l2)
46+
endfunction
47+
48+
function! s:apply(lnum1, lnum2) abort
3749
let [l, r] = s:surroundings()
3850
let uncomment = 2
39-
for lnum in range(lnum1,lnum2)
51+
for lnum in range(a:lnum1,a:lnum2)
4052
let line = matchstr(getline(lnum),'\S.*\s\@<!')
4153
let [l, r] = s:strip_white_space(l,r,line)
4254
if len(line) && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
@@ -50,7 +62,7 @@ function! s:go(...) abort
5062
let indent = '^\s*'
5163
endif
5264

53-
for lnum in range(lnum1,lnum2)
65+
for lnum in range(a:lnum1,a:lnum2)
5466
let line = getline(lnum)
5567
if strlen(r) > 2 && l.r !~# '\\'
5668
let line = substitute(line,
@@ -60,7 +72,7 @@ function! s:go(...) abort
6072
if uncomment
6173
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
6274
else
63-
let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
75+
let line = substitute(line,'^\%('.matchstr(getline(a:lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
6476
endif
6577
call setline(lnum,line)
6678
endfor
@@ -102,12 +114,18 @@ nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
102114
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
103115
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
104116
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
117+
xnoremap <expr> <Plug>(CommentaryYank) <SID>yank()
118+
nnoremap <expr> <Plug>(CommentaryYank) <SID>yank()
119+
nnoremap <expr> <Plug>(CommentaryYankLine) <SID>yank() . '_'
105120
106121
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
107122
xmap gc <Plug>Commentary
108123
nmap gc <Plug>Commentary
109124
omap gc <Plug>Commentary
110125
nmap gcc <Plug>CommentaryLine
126+
xmap gy <Plug>(CommentaryYank)
127+
nmap gy <Plug>(CommentaryYank)
128+
nmap gyy <Plug>(CommentaryYankLine)
111129
if maparg('c','n') ==# '' && !exists('v:operator')
112130
nmap cgc <Plug>ChangeCommentary
113131
endif

0 commit comments

Comments
 (0)