Skip to content

Commit

Permalink
Add support for yanking into a register
Browse files Browse the repository at this point in the history
  • Loading branch information
mathstuf committed Jul 30, 2013
1 parent 0b3d928 commit 301c733
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions plugin/NERD_commenter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,50 @@ function s:InvertComment(firstLine, lastLine)
endwhile
endfunction

" Function: NERDCommentYank(mode, type) function {{{2
" This function handles yanking
"
" Args:
" -mode: a flag indicating whether the comment is requested in visual
" mode or not
" -register: the register to yank into
function NERDCommentYank(mode, register) range
let isVisual = a:mode =~ '[vsx]'
if isVisual
let firstLine = line("'<")
let lastLine = line("'>")
let firstCol = col("'<")
let lastCol = col("'>") - (&selection == 'exclusive' ? 1 : 0)
else
let firstLine = a:firstline
let lastLine = a:lastline
endif

let reg = ''
let range = ''
let rangeCount = ''

if a:register != ""
let reg = '"'.a:register
endif

if firstLine != lastLine
let range = firstLine .','. lastLine
let rangeCount = lastLine - firstLine + 1
endif

if isVisual
normal! gvy
else
execute range .'yank '. a:register
endif
execute range .'call NERDComment('. a:mode .', "Comment")'

if !isVisual
silent! call repeat#set(rangeCount.reg.'\<plug>NERDCommenterYank',-1)
endif
endfunction

" Function: NERDComment(mode, type) function {{{2
" This function is a Wrapper for the main commenting functions
"
Expand All @@ -1043,7 +1087,7 @@ endfunction
" 'n' for Normal mode, 'x' for Visual mode
" -type: the type of commenting requested. Can be 'Sexy', 'Invert',
" 'Minimal', 'Toggle', 'AlignLeft', 'AlignBoth', 'Comment',
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment'
function! NERDComment(mode, type) range
let isVisual = a:mode =~ '[vsx]'
" we want case sensitivity when commenting
Expand All @@ -1064,8 +1108,6 @@ function! NERDComment(mode, type) range
let lastLine = a:lastline
endif

let countWasGiven = (!isVisual && firstLine != lastLine)

let forceNested = (a:type ==? 'Nested' || g:NERDDefaultNesting)

if a:type ==? 'Comment' || a:type ==? 'Nested'
Expand Down Expand Up @@ -1130,15 +1172,6 @@ function! NERDComment(mode, type) range
elseif a:type ==? 'Uncomment'
call s:UncommentLines(firstLine, lastLine)

elseif a:type ==? 'Yank'
if isVisual
normal! gvy
elseif countWasGiven
execute firstLine .','. lastLine .'yank'
else
normal! yy
endif
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
endif

let &ignorecase = oldIgnoreCase
Expand Down Expand Up @@ -2720,8 +2753,15 @@ function! s:CreateMaps(modes, target, desc, combo)
" Build up a map command like
" 'noremap <silent> <plug>NERDCommenterComment :call NERDComment("n", "Comment")'
let plug = '<plug>NERDCommenter' . a:target
let plug_start = 'noremap <silent> ' . plug . ' :call NERDComment("'
let plug_end = '", "' . a:target . '")<cr>'
if a:target ==? 'Yank'
let func_name = 'NERDCommentYank'
let target = 'v:register'
else
let func_name = 'NERDComment'
let target = "'" . a:target . "'"
endif
let plug_start = 'noremap <silent> ' . plug . ' :call ' . func_name . '("'
let plug_end = '", "' . target . '")<cr>'
" Build up a menu command like
" 'menu <silent> comment.Comment<Tab>\\cc <plug>NERDCommenterComment'
let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],
Expand Down

0 comments on commit 301c733

Please sign in to comment.