Skip to content

Commit

Permalink
Merge pull request preservim#121 from mathstuf/dev/support-yank-into-…
Browse files Browse the repository at this point in the history
…register

Add support for yanking into a register
  • Loading branch information
alerque committed May 23, 2016
2 parents a96e635 + 301c733 commit 4a5adda
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions plugin/NERD_commenter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,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.'\<plug>NERDCommenterYank',-1)
endif
endfunction

" Function: NERDComment(mode, type) function {{{2
" This function is a Wrapper for the main commenting functions
"
Expand All @@ -1073,7 +1117,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
Expand All @@ -1094,8 +1138,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'
Expand Down Expand Up @@ -1160,15 +1202,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
Expand Down Expand Up @@ -2750,8 +2783,15 @@ function! s:CreateMaps(modes, target, desc, combo)
" Build up a map command like
" 'noremap <silent> <plug>NERDCommenterComment :call NERDComment("n", "Comment")'
let plug = '<plug>NERDCommenter' . a:target
let plug_start = 'noremap <silent> ' . plug . ' :call NERDComment("'
let plug_end = '", "' . a:target . '")<cr>'
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 <silent> ' . plug . ' :call ' . func_name . '("'
let plug_end = '", "' . target . '")<cr>'
" Build up a menu command like
" 'menu <silent> comment.Comment<Tab>\\cc <plug>NERDCommenterComment'
let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'],
Expand Down

0 comments on commit 4a5adda

Please sign in to comment.