@@ -26,17 +26,29 @@ endfunction
2626
2727function ! 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() . '_'
102114onoremap <silent> <Plug> Commentary :<C-U> call <SID> textobject(get(v:, 'operator', '') ==# 'c')<CR>
103115nnoremap <silent> <Plug> ChangeCommentary c:<C-U> call <SID> textobject(1)<CR>
104116nmap <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
106121if ! 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