Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add duplicate (yank, comment, and put) operation. #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/commentary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ gcu
*:Commentary*
:[range]Commentary Comment or uncomment [range] lines

In addition, you can yank a section of text before commenting it out:

gcy{motion} Yank and then comment or uncomment the lines that
{motion} moves over.

gcy Yank and then comment or uncomment [count] lines.

{Visual}gcy Yank and then comment or uncomment the highlighted
lines.

You can also duplicate a section of text before commenting it out:

gcd{motion} Duplicate and then comment or uncomment the lines that
{motion} moves over.

gcd Duplicate and then comment or uncomment [count] lines.

{Visual}gcd Duplicate and then comment or uncomment the highlighted
lines.

The |User| CommentaryPost autocommand fires after a successful operation and
can be used for advanced customization.

Expand Down
45 changes: 45 additions & 0 deletions plugin/commentary.vim
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ function! s:textobject(inner) abort
endif
endfunction

function! s:setcommentaryreg(reg)
let s:targetreg = a:reg
endfunction
function! s:yankandcomment(type,...)
" only linewise operations make sense (to me, at least)
" so I am ignoring `type`
if a:0
let [mark1, mark2] = [a:type, a:1]
let reg = a:2
else
let [mark1, mark2] = ["'[", "']"]
let reg = get(s:, "targetreg", '"')
endif
execute 'normal! ' . mark1 . '"' . reg . 'y' . mark2 . ']'
call <SID>go(line(mark1),line(mark2))
execute 'normal! ' . mark1
endfunction

function! s:yankcommentpaste(type,...)
if a:0
let [mark1, mark2] = [a:type, a:1]
else
let [mark1, mark2] = ["'[", "']"]
endif
let savereg = @"
execute "normal " . mark1 ."gcy" . mark2 . "]"
execute "normal! " . mark2 . "p" . mark1
let @" = savereg
endfunction

xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@
nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
Expand All @@ -80,13 +110,28 @@ nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary
command! -range -bar Commentary call s:go(<line1>,<line2>)

xnoremap <silent> <Plug>CommentaryYank :<C-U>call<SID>yankandcomment("'<", "'>", v:register)<CR>
nnoremap <silent> <Plug>CommentaryYank :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<CR>g@
nnoremap <silent> <Plug>CommentaryYankLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankandcomment<Bar>exe 'norm! 'v:count1.'g@_'<CR>

xnoremap <silent> <Plug>CommentaryDupe :<C-U>call<SID>yankcommentpaste("'<", "'>", v:register)<CR>:normal! '>j<CR>
nnoremap <silent> <Plug>CommentaryDupe :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankcommentpaste<CR>g@
nnoremap <silent> <Plug>CommentaryDupeLine :<C-U>call <SID>setcommentaryreg(v:register)<CR>:set opfunc=<SID>yankcommentpaste<Bar>exe 'norm! 'v:count1.'g@_'<CR>

xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
xmap gc <Plug>Commentary
nmap gc <Plug>Commentary
omap gc <Plug>Commentary
nmap gcc <Plug>CommentaryLine
nmap cgc <Plug>ChangeCommentary
nmap gcu <Plug>Commentary<Plug>Commentary
xmap gcy <Plug>CommentaryYank
nmap gcy <Plug>CommentaryYank
nmap gcyy <Plug>CommentaryYankLine
xmap gcd <Plug>CommentaryDupe
nmap gcd <Plug>CommentaryDupe

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause a pause since <Plug>CommentaryDupeLine overlaps <Plug>CommentaryDupe? (solution would be to wrap in parentheses, e.g., <Plug>(CommentaryDupeLine) and <Plug>(CommentaryDupe))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In practice, however, it may not be an issue? In normal
mode, where the conflict arises, <Plug>CommentaryDupe takes a motion,
and so there is no pause (except if you count Vim waiting for you to
complete the operator motion). So, you will always be typing either
gcdd or gcd{motion}`. Still, it makes sense to code defensively here
and wrap the names with parentheses, but that will break the convention
with the rest of Plug's, so I am going to wait and see if (a) @tpope
accepts this patch in the first place, and (b) if he amenable to
modifying all the Plug names.

On 3/19/15 6:03 PM, Justin M. Keyes wrote:

In plugin/commentary.vim
#42 (comment):

@@ -87,6 +126,12 @@ if !hasmapto('Commentary') || maparg('gc','n') ==# ''
nmap gcc CommentaryLine
nmap cgc ChangeCommentary
nmap gcu CommentaryCommentary

  • xmap gcy CommentaryYank
  • nmap gcy CommentaryYank
  • nmap gcyy CommentaryYankLine
  • xmap gcd CommentaryDupe
  • nmap gcd CommentaryDupe

Won't this cause a pause since |CommentaryDupeLine| overlaps
|CommentaryDupe|? (solution would be to wrap in parentheses, e.g.,
|(CommentaryDupeLine)| and |(CommentaryDupe)|)


Reply to this email directly or view it on GitHub
https://github.com/tpope/vim-commentary/pull/42/files#r26802462.


Jeet Sukumaran

[email protected]

Blog/Personal Pages:
http://jeetworks.org/
GitHub Repositories:
http://github.com/jeetsukumaran
Photographs (as stream):
http://www.flickr.com/photos/jeetsukumaran/
Photographs (by galleries):

http://www.flickr.com/photos/jeetsukumaran/sets/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am indeed adopting the parentheses convention for new plugins, but I will not be updating old plugins unless there's a concrete issue.

nmap gcdd <Plug>CommentaryDupeLine
endif

if maparg('\\','n') ==# '' && maparg('\','n') ==# '' && get(g:, 'commentary_map_backslash', 1)
Expand Down