Skip to content

Commit 19cca1e

Browse files
committed
Add support for yanking into a register
1 parent b2dca14 commit 19cca1e

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

plugin/NERD_commenter.vim

+80-12
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,50 @@ function s:InvertComment(firstLine, lastLine)
10891089
endwhile
10901090
endfunction
10911091

1092+
" Function: NERDCommentYank(mode, register) function {{{2
1093+
" This function handles yanking
1094+
"
1095+
" Args:
1096+
" -mode: a character indicating the mode in which the comment is requested:
1097+
" 'n' for Normal mode, 'x' for Visual mode
1098+
" -register: the register to yank into
1099+
function NERDCommentYank(mode, register) range
1100+
let isVisual = a:mode =~ '[vsx]'
1101+
if isVisual
1102+
let firstLine = line("'<")
1103+
let lastLine = line("'>")
1104+
let firstCol = col("'<")
1105+
let lastCol = col("'>") - (&selection == 'exclusive' ? 1 : 0)
1106+
else
1107+
let firstLine = a:firstline
1108+
let lastLine = a:lastline
1109+
endif
1110+
1111+
let reg = ''
1112+
let range = ''
1113+
let rangeCount = ''
1114+
1115+
if a:register != ""
1116+
let reg = '"'.a:register
1117+
endif
1118+
1119+
if firstLine != lastLine
1120+
let range = firstLine .','. lastLine
1121+
let rangeCount = lastLine - firstLine + 1
1122+
endif
1123+
1124+
if isVisual
1125+
normal! gvy
1126+
else
1127+
execute range .'yank '. a:register
1128+
endif
1129+
execute range .'call NERDComment('. isVisual .', "norm")'
1130+
1131+
if !isVisual
1132+
silent! call repeat#set(rangeCount.reg.'\<plug>NERDCommenterYank',-1)
1133+
endif
1134+
endfunction
1135+
10921136
" Function: NERDComment(mode, type) function {{{2
10931137
" This function is a Wrapper for the main commenting functions
10941138
"
@@ -1097,7 +1141,7 @@ endfunction
10971141
" 'n' for Normal mode, 'x' for Visual mode
10981142
" -type: the type of commenting requested. Can be 'Sexy', 'Invert',
10991143
" 'Minimal', 'Toggle', 'AlignLeft', 'AlignBoth', 'Comment',
1100-
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
1144+
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment'
11011145
function! NERDComment(mode, type) range
11021146
let isVisual = a:mode =~ '[vsx]'
11031147

@@ -1183,16 +1227,6 @@ function! NERDComment(mode, type) range
11831227

11841228
elseif a:type ==? 'Uncomment'
11851229
call s:UncommentLines(firstLine, lastLine)
1186-
1187-
elseif a:type ==? 'Yank'
1188-
if isVisual
1189-
normal! gvy
1190-
elseif countWasGiven
1191-
execute firstLine .','. lastLine .'yank'
1192-
else
1193-
normal! yy
1194-
endif
1195-
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
11961230
endif
11971231

11981232
call s:RecoverStateAfterLineComment(state)
@@ -2846,14 +2880,48 @@ function! s:CreateMaps(modes, target, desc, combo)
28462880
endif
28472881
endfor
28482882
endfunction
2883+
2884+
" Create menu items for the specified modes. If a:combo is not empty, then
2885+
" also define mappings and show a:combo in the menu items.
2886+
function! s:CreateYankMaps(modes, target, desc, combo)
2887+
" Build up a map command like
2888+
" 'noremap <silent> <plug>NERDCommenterYank :call NERDCommentYank("n", v:register)'
2889+
let plug = '<plug>NERDCommenter' . a:target
2890+
let plug_start = 'noremap <silent> ' . plug . ' :call NERDCommentYank("'
2891+
let plug_end = '", v:register)<cr>'
2892+
" Build up a menu command like
2893+
" 'menu <silent> comment.Comment<Tab>\\cc <plug>NERDCommenterComment'
2894+
let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],
2895+
\ g:NERDMenuMode, '')
2896+
let menu_command = 'menu <silent> ' . menuRoot . '.' . escape(a:desc, ' ')
2897+
if strlen(a:combo)
2898+
let leader = exists('g:mapleader') ? g:mapleader : '\'
2899+
let menu_command .= '<Tab>' . escape(leader, '\') . a:combo
2900+
endif
2901+
let menu_command .= ' ' . (strlen(a:combo) ? plug : a:target)
2902+
" Execute the commands built above for each requested mode.
2903+
for mode in (a:modes == '') ? [''] : split(a:modes, '\zs')
2904+
if strlen(a:combo)
2905+
execute mode . plug_start . mode . plug_end
2906+
if g:NERDCreateDefaultMappings && !hasmapto(plug, mode)
2907+
execute mode . 'map <leader>' . a:combo . ' ' . plug
2908+
endif
2909+
endif
2910+
" Check if the user wants the menu to be displayed.
2911+
if g:NERDMenuMode != 0
2912+
execute mode . menu_command
2913+
endif
2914+
endfor
2915+
endfunction
2916+
28492917
call s:CreateMaps('nx', 'Comment', 'Comment', 'cc')
28502918
call s:CreateMaps('nx', 'Toggle', 'Toggle', 'c<space>')
28512919
call s:CreateMaps('nx', 'Minimal', 'Minimal', 'cm')
28522920
call s:CreateMaps('nx', 'Nested', 'Nested', 'cn')
28532921
call s:CreateMaps('n', 'ToEOL', 'To EOL', 'c$')
28542922
call s:CreateMaps('nx', 'Invert', 'Invert', 'ci')
28552923
call s:CreateMaps('nx', 'Sexy', 'Sexy', 'cs')
2856-
call s:CreateMaps('nx', 'Yank', 'Yank then comment', 'cy')
2924+
call s:CreateYankMaps('nx', 'Yank', 'Yank then comment', 'cy')
28572925
call s:CreateMaps('n', 'Append', 'Append', 'cA')
28582926
call s:CreateMaps('', ':', '-Sep-', '')
28592927
call s:CreateMaps('nx', 'AlignLeft', 'Left aligned', 'cl')

0 commit comments

Comments
 (0)