Skip to content

Commit 3353f61

Browse files
committed
Add comment and yank
1 parent 349340d commit 3353f61

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)
@@ -51,7 +63,7 @@ function! s:go(...) abort
5163
endif
5264

5365
let lines = []
54-
for lnum in range(lnum1,lnum2)
66+
for lnum in range(a:lnum1,a:lnum2)
5567
let line = getline(lnum)
5668
if strlen(r) > 2 && l.r !~# '\\'
5769
let line = substitute(line,
@@ -61,7 +73,7 @@ function! s:go(...) abort
6173
if uncomment
6274
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
6375
else
64-
let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
76+
let line = substitute(line,'^\%('.matchstr(getline(a:lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
6577
endif
6678
call add(lines, line)
6779
endfor
@@ -104,12 +116,18 @@ nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
104116
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
105117
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
106118
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
119+
xnoremap <expr> <Plug>(CommentaryYank) <SID>yank()
120+
nnoremap <expr> <Plug>(CommentaryYank) <SID>yank()
121+
nnoremap <expr> <Plug>(CommentaryYankLine) <SID>yank() . '_'
107122
108123
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
109124
xmap gc <Plug>Commentary
110125
nmap gc <Plug>Commentary
111126
omap gc <Plug>Commentary
112127
nmap gcc <Plug>CommentaryLine
128+
xmap gy <Plug>(CommentaryYank)
129+
nmap gy <Plug>(CommentaryYank)
130+
nmap gyy <Plug>(CommentaryYankLine)
113131
if maparg('c','n') ==# '' && !exists('v:operator')
114132
nmap cgc <Plug>ChangeCommentary
115133
endif

0 commit comments

Comments
 (0)