diff --git a/plugin/NERD_commenter.vim b/plugin/NERD_commenter.vim index 13c76934..88ee9482 100644 --- a/plugin/NERD_commenter.vim +++ b/plugin/NERD_commenter.vim @@ -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.'\NERDCommenterYank',-1) + endif +endfunction + " Function: NERDComment(mode, type) function {{{2 " This function is a Wrapper for the main commenting functions " @@ -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 @@ -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' @@ -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 @@ -2720,8 +2753,15 @@ function! s:CreateMaps(modes, target, desc, combo) " Build up a map command like " 'noremap NERDCommenterComment :call NERDComment("n", "Comment")' let plug = 'NERDCommenter' . a:target - let plug_start = 'noremap ' . plug . ' :call NERDComment("' - let plug_end = '", "' . a:target . '")' + 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 ' . plug . ' :call ' . func_name . '("' + let plug_end = '", "' . target . '")' " Build up a menu command like " 'menu comment.Comment\\cc NERDCommenterComment' let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],