Skip to content

Commit 4a5adda

Browse files
committed
Merge pull request preservim#121 from mathstuf/dev/support-yank-into-register
Add support for yanking into a register
2 parents a96e635 + 301c733 commit 4a5adda

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

plugin/NERD_commenter.vim

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,50 @@ function s:InvertComment(firstLine, lastLine)
10651065
endwhile
10661066
endfunction
10671067

1068+
" Function: NERDCommentYank(mode, type) function {{{2
1069+
" This function handles yanking
1070+
"
1071+
" Args:
1072+
" -mode: a flag indicating whether the comment is requested in visual
1073+
" mode or not
1074+
" -register: the register to yank into
1075+
function NERDCommentYank(mode, register) range
1076+
let isVisual = a:mode =~ '[vsx]'
1077+
if isVisual
1078+
let firstLine = line("'<")
1079+
let lastLine = line("'>")
1080+
let firstCol = col("'<")
1081+
let lastCol = col("'>") - (&selection == 'exclusive' ? 1 : 0)
1082+
else
1083+
let firstLine = a:firstline
1084+
let lastLine = a:lastline
1085+
endif
1086+
1087+
let reg = ''
1088+
let range = ''
1089+
let rangeCount = ''
1090+
1091+
if a:register != ""
1092+
let reg = '"'.a:register
1093+
endif
1094+
1095+
if firstLine != lastLine
1096+
let range = firstLine .','. lastLine
1097+
let rangeCount = lastLine - firstLine + 1
1098+
endif
1099+
1100+
if isVisual
1101+
normal! gvy
1102+
else
1103+
execute range .'yank '. a:register
1104+
endif
1105+
execute range .'call NERDComment('. a:mode .', "Comment")'
1106+
1107+
if !isVisual
1108+
silent! call repeat#set(rangeCount.reg.'\<plug>NERDCommenterYank',-1)
1109+
endif
1110+
endfunction
1111+
10681112
" Function: NERDComment(mode, type) function {{{2
10691113
" This function is a Wrapper for the main commenting functions
10701114
"
@@ -1073,7 +1117,7 @@ endfunction
10731117
" 'n' for Normal mode, 'x' for Visual mode
10741118
" -type: the type of commenting requested. Can be 'Sexy', 'Invert',
10751119
" 'Minimal', 'Toggle', 'AlignLeft', 'AlignBoth', 'Comment',
1076-
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
1120+
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment'
10771121
function! NERDComment(mode, type) range
10781122
let isVisual = a:mode =~ '[vsx]'
10791123
" we want case sensitivity when commenting
@@ -1094,8 +1138,6 @@ function! NERDComment(mode, type) range
10941138
let lastLine = a:lastline
10951139
endif
10961140

1097-
let countWasGiven = (!isVisual && firstLine != lastLine)
1098-
10991141
let forceNested = (a:type ==? 'Nested' || g:NERDDefaultNesting)
11001142

11011143
if a:type ==? 'Comment' || a:type ==? 'Nested'
@@ -1160,15 +1202,6 @@ function! NERDComment(mode, type) range
11601202
elseif a:type ==? 'Uncomment'
11611203
call s:UncommentLines(firstLine, lastLine)
11621204

1163-
elseif a:type ==? 'Yank'
1164-
if isVisual
1165-
normal! gvy
1166-
elseif countWasGiven
1167-
execute firstLine .','. lastLine .'yank'
1168-
else
1169-
normal! yy
1170-
endif
1171-
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
11721205
endif
11731206

11741207
let &ignorecase = oldIgnoreCase
@@ -2750,8 +2783,15 @@ function! s:CreateMaps(modes, target, desc, combo)
27502783
" Build up a map command like
27512784
" 'noremap <silent> <plug>NERDCommenterComment :call NERDComment("n", "Comment")'
27522785
let plug = '<plug>NERDCommenter' . a:target
2753-
let plug_start = 'noremap <silent> ' . plug . ' :call NERDComment("'
2754-
let plug_end = '", "' . a:target . '")<cr>'
2786+
if a:target ==? 'Yank'
2787+
let func_name = 'NERDCommentYank'
2788+
let target = 'v:register'
2789+
else
2790+
let func_name = 'NERDComment'
2791+
let target = "'" . a:target . "'"
2792+
endif
2793+
let plug_start = 'noremap <silent> ' . plug . ' :call ' . func_name . '("'
2794+
let plug_end = '", "' . target . '")<cr>'
27552795
" Build up a menu command like
27562796
" 'menu <silent> comment.Comment<Tab>\\cc <plug>NERDCommenterComment'
27572797
let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],

0 commit comments

Comments
 (0)