@@ -1089,6 +1089,50 @@ function s:InvertComment(firstLine, lastLine)
1089
1089
endwhile
1090
1090
endfunction
1091
1091
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
+
1092
1136
" Function: NERDComment(mode, type) function {{{2
1093
1137
" This function is a Wrapper for the main commenting functions
1094
1138
"
@@ -1097,7 +1141,7 @@ endfunction
1097
1141
" 'n' for Normal mode, 'x' for Visual mode
1098
1142
" -type: the type of commenting requested. Can be 'Sexy', 'Invert',
1099
1143
" 'Minimal', 'Toggle', 'AlignLeft', 'AlignBoth', 'Comment',
1100
- " 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
1144
+ " 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment'
1101
1145
function ! NERDComment (mode , type ) range
1102
1146
let isVisual = a: mode = ~ ' [vsx]'
1103
1147
@@ -1183,16 +1227,6 @@ function! NERDComment(mode, type) range
1183
1227
1184
1228
elseif a: type == ? ' Uncomment'
1185
1229
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")'
1196
1230
endif
1197
1231
1198
1232
call s: RecoverStateAfterLineComment (state )
@@ -2846,14 +2880,48 @@ function! s:CreateMaps(modes, target, desc, combo)
2846
2880
endif
2847
2881
endfor
2848
2882
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
+
2849
2917
call s: CreateMaps (' nx' , ' Comment' , ' Comment' , ' cc' )
2850
2918
call s: CreateMaps (' nx' , ' Toggle' , ' Toggle' , ' c<space>' )
2851
2919
call s: CreateMaps (' nx' , ' Minimal' , ' Minimal' , ' cm' )
2852
2920
call s: CreateMaps (' nx' , ' Nested' , ' Nested' , ' cn' )
2853
2921
call s: CreateMaps (' n' , ' ToEOL' , ' To EOL' , ' c$' )
2854
2922
call s: CreateMaps (' nx' , ' Invert' , ' Invert' , ' ci' )
2855
2923
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' )
2857
2925
call s: CreateMaps (' n' , ' Append' , ' Append' , ' cA' )
2858
2926
call s: CreateMaps (' ' , ' :' , ' -Sep-' , ' ' )
2859
2927
call s: CreateMaps (' nx' , ' AlignLeft' , ' Left aligned' , ' cl' )
0 commit comments