Skip to content

Commit 301c733

Browse files
committed
Add support for yanking into a register
1 parent 0b3d928 commit 301c733

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

plugin/NERD_commenter.vim

+54-14
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,50 @@ function s:InvertComment(firstLine, lastLine)
10351035
endwhile
10361036
endfunction
10371037

1038+
" Function: NERDCommentYank(mode, type) function {{{2
1039+
" This function handles yanking
1040+
"
1041+
" Args:
1042+
" -mode: a flag indicating whether the comment is requested in visual
1043+
" mode or not
1044+
" -register: the register to yank into
1045+
function NERDCommentYank(mode, register) range
1046+
let isVisual = a:mode =~ '[vsx]'
1047+
if isVisual
1048+
let firstLine = line("'<")
1049+
let lastLine = line("'>")
1050+
let firstCol = col("'<")
1051+
let lastCol = col("'>") - (&selection == 'exclusive' ? 1 : 0)
1052+
else
1053+
let firstLine = a:firstline
1054+
let lastLine = a:lastline
1055+
endif
1056+
1057+
let reg = ''
1058+
let range = ''
1059+
let rangeCount = ''
1060+
1061+
if a:register != ""
1062+
let reg = '"'.a:register
1063+
endif
1064+
1065+
if firstLine != lastLine
1066+
let range = firstLine .','. lastLine
1067+
let rangeCount = lastLine - firstLine + 1
1068+
endif
1069+
1070+
if isVisual
1071+
normal! gvy
1072+
else
1073+
execute range .'yank '. a:register
1074+
endif
1075+
execute range .'call NERDComment('. a:mode .', "Comment")'
1076+
1077+
if !isVisual
1078+
silent! call repeat#set(rangeCount.reg.'\<plug>NERDCommenterYank',-1)
1079+
endif
1080+
endfunction
1081+
10381082
" Function: NERDComment(mode, type) function {{{2
10391083
" This function is a Wrapper for the main commenting functions
10401084
"
@@ -1043,7 +1087,7 @@ endfunction
10431087
" 'n' for Normal mode, 'x' for Visual mode
10441088
" -type: the type of commenting requested. Can be 'Sexy', 'Invert',
10451089
" 'Minimal', 'Toggle', 'AlignLeft', 'AlignBoth', 'Comment',
1046-
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
1090+
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment'
10471091
function! NERDComment(mode, type) range
10481092
let isVisual = a:mode =~ '[vsx]'
10491093
" we want case sensitivity when commenting
@@ -1064,8 +1108,6 @@ function! NERDComment(mode, type) range
10641108
let lastLine = a:lastline
10651109
endif
10661110

1067-
let countWasGiven = (!isVisual && firstLine != lastLine)
1068-
10691111
let forceNested = (a:type ==? 'Nested' || g:NERDDefaultNesting)
10701112

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

1133-
elseif a:type ==? 'Yank'
1134-
if isVisual
1135-
normal! gvy
1136-
elseif countWasGiven
1137-
execute firstLine .','. lastLine .'yank'
1138-
else
1139-
normal! yy
1140-
endif
1141-
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
11421175
endif
11431176

11441177
let &ignorecase = oldIgnoreCase
@@ -2720,8 +2753,15 @@ function! s:CreateMaps(modes, target, desc, combo)
27202753
" Build up a map command like
27212754
" 'noremap <silent> <plug>NERDCommenterComment :call NERDComment("n", "Comment")'
27222755
let plug = '<plug>NERDCommenter' . a:target
2723-
let plug_start = 'noremap <silent> ' . plug . ' :call NERDComment("'
2724-
let plug_end = '", "' . a:target . '")<cr>'
2756+
if a:target ==? 'Yank'
2757+
let func_name = 'NERDCommentYank'
2758+
let target = 'v:register'
2759+
else
2760+
let func_name = 'NERDComment'
2761+
let target = "'" . a:target . "'"
2762+
endif
2763+
let plug_start = 'noremap <silent> ' . plug . ' :call ' . func_name . '("'
2764+
let plug_end = '", "' . target . '")<cr>'
27252765
" Build up a menu command like
27262766
" 'menu <silent> comment.Comment<Tab>\\cc <plug>NERDCommenterComment'
27272767
let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],

0 commit comments

Comments
 (0)