diff --git a/doc/NERD_commenter.txt b/doc/NERD_commenter.txt index f10c021..0170d2a 100644 --- a/doc/NERD_commenter.txt +++ b/doc/NERD_commenter.txt @@ -162,7 +162,7 @@ lines were selected in visual-line mode. 3.2.2 Nested comment map *NERDComNestedComment* Default mapping: [count]||cn -Mapped to: NERDCommenterNest +Mapped to: NERDCommenterNested Applicable modes: normal visual visual-line visual-block. Performs nested commenting. Works the same as ||cc except that if a line @@ -288,7 +288,7 @@ to insert mode between the new delimiters. 3.2.10 Insert comment map *NERDComInsertComment* Default mapping: disabled by default. -Map it to: NERDCommenterInInsert +Map it to: NERDCommenterInsert Applicable modes: insert. Adds comment delimiters at the current cursor position and inserts diff --git a/plugin/NERD_commenter.vim b/plugin/NERD_commenter.vim index 086b8d5..efefbfe 100644 --- a/plugin/NERD_commenter.vim +++ b/plugin/NERD_commenter.vim @@ -3,7 +3,7 @@ " Description: vim global plugin that provides easy code commenting " Maintainer: Martin Grenfell " Version: 2.3.0 -" Last Change: 08th December, 2010 +" Last Change: Wed Dec 14 08:00 AM 2011 EST " License: This program is free software. It comes without any warranty, " to the extent permitted by applicable law. You can redistribute " it and/or modify it under the terms of the Do What The Fuck You @@ -34,7 +34,7 @@ let loaded_nerd_comments = 1 " 1 if the var is set, 0 otherwise function s:InitVariable(var, value) if !exists(a:var) - exec 'let ' . a:var . ' = ' . "'" . a:value . "'" + execute 'let ' . a:var . ' = ' . "'" . a:value . "'" return 1 endif return 0 @@ -62,10 +62,6 @@ call s:InitVariable("g:NERDRemoveExtraSpaces", 0) call s:InitVariable("g:NERDRPlace", "<]") call s:InitVariable("g:NERDSpaceDelims", 0) -if !exists("g:NERDCustomDelimiters") - let g:NERDCustomDelimiters = {} -endif - let s:NERDFileNameEscape="[]#*$%'\" ?`!&();<>\\" let s:delimiterMap = { @@ -391,10 +387,9 @@ let s:delimiterMap = { \ 'z8a': { 'left': ';' } \ } -"merge in the custom delimiters -for ft in keys(g:NERDCustomDelimiters) - let s:delimiterMap[ft] = g:NERDCustomDelimiters[ft] -endfor +if exists("g:NERDCustomDelimiters") + call extend(s:delimiterMap, g:NERDCustomDelimiters) +endif " Section: Comment mapping functions, autocommands and commands {{{1 " ============================================================================ @@ -1021,16 +1016,17 @@ function s:InvertComment(firstLine, lastLine) endwhile endfunction -" Function: NERDComment(isVisual, type) function {{{2 +" Function: s:NERDComment(mode, type) function {{{2 " This function is a Wrapper for the main commenting functions " " Args: -" -isVisual: a flag indicating whether the comment is requested in visual -" mode or not -" -type: the type of commenting requested. Can be 'sexy', 'invert', -" 'minimal', 'toggle', 'alignLeft', 'alignBoth', 'norm', -" 'nested', 'toEOL', 'append', 'insert', 'uncomment', 'yank' -function! NERDComment(isVisual, type) range +" -mode: a character indicating the mode in which the comment is requested: +" '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' +function! s:NERDComment(mode, type) range + let isVisual = a:mode =~ '[vsx]' " we want case sensitivity when commenting let oldIgnoreCase = &ignorecase set noignorecase @@ -1039,7 +1035,7 @@ function! NERDComment(isVisual, type) range call s:NerdEcho("filetype plugins should be enabled. See :help NERDComInstallation and :help :filetype-plugin-on", 0) endif - if a:isVisual + if isVisual let firstLine = line("'<") let lastLine = line("'>") let firstCol = col("'<") @@ -1049,32 +1045,32 @@ function! NERDComment(isVisual, type) range let lastLine = a:lastline endif - let countWasGiven = (a:isVisual == 0 && firstLine != lastLine) + let countWasGiven = (!isVisual && firstLine != lastLine) - let forceNested = (a:type == 'nested' || g:NERDDefaultNesting) + let forceNested = (a:type == 'Nested' || g:NERDDefaultNesting) - if a:type == 'norm' || a:type == 'nested' - if a:isVisual && visualmode() == "" + if a:type == 'Comment' || a:type == 'Nested' + if isVisual && visualmode() == "\" call s:CommentBlock(firstLine, lastLine, firstCol, lastCol, forceNested) - elseif a:isVisual && visualmode() == "v" && (g:NERDCommentWholeLinesInVMode==0 || (g:NERDCommentWholeLinesInVMode==2 && s:HasMultipartDelims())) + elseif isVisual && visualmode() == "v" && (g:NERDCommentWholeLinesInVMode==0 || (g:NERDCommentWholeLinesInVMode==2 && s:HasMultipartDelims())) call s:CommentRegion(firstLine, firstCol, lastLine, lastCol, forceNested) else call s:CommentLines(forceNested, "none", firstLine, lastLine) endif - elseif a:type == 'alignLeft' || a:type == 'alignBoth' + elseif a:type == 'AlignLeft' || a:type == 'AlignBoth' let align = "none" - if a:type == "alignLeft" + if a:type == "AlignLeft" let align = "left" - elseif a:type == "alignBoth" + elseif a:type == "AlignBoth" let align = "both" endif call s:CommentLines(forceNested, align, firstLine, lastLine) - elseif a:type == 'invert' + elseif a:type == 'Invert' call s:InvertComment(firstLine, lastLine) - elseif a:type == 'sexy' + elseif a:type == 'Sexy' try call s:CommentLinesSexy(firstLine, lastLine) catch /NERDCommenter.Delimiters/ @@ -1083,7 +1079,7 @@ function! NERDComment(isVisual, type) range call s:NerdEcho("Sexy comment aborted. Nested sexy cannot be nested", 0) endtry - elseif a:type == 'toggle' + elseif a:type == 'Toggle' let theLine = getline(firstLine) if s:IsInSexyComment(firstLine) || s:IsCommentedFromStartOfLine(s:Left(), theLine) || s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine) @@ -1092,7 +1088,7 @@ function! NERDComment(isVisual, type) range call s:CommentLinesToggle(forceNested, firstLine, lastLine) endif - elseif a:type == 'minimal' + elseif a:type == 'Minimal' try call s:CommentLinesMinimal(firstLine, lastLine) catch /NERDCommenter.Delimiters/ @@ -1101,29 +1097,29 @@ function! NERDComment(isVisual, type) range call s:NerdEcho("Place holders are required but disabled.", 0) endtry - elseif a:type == 'toEOL' + elseif a:type == 'ToEOL' call s:SaveScreenState() call s:CommentBlock(firstLine, firstLine, col("."), col("$")-1, 1) call s:RestoreScreenState() - elseif a:type == 'append' + elseif a:type == 'Append' call s:AppendCommentToLine() - elseif a:type == 'insert' + elseif a:type == 'Insert' call s:PlaceDelimitersAndInsBetween() - elseif a:type == 'uncomment' + elseif a:type == 'Uncomment' call s:UncommentLines(firstLine, lastLine) - elseif a:type == 'yank' - if a:isVisual + elseif a:type == 'Yank' + if isVisual normal! gvy elseif countWasGiven execute firstLine .','. lastLine .'yank' else normal! yy endif - execute firstLine .','. lastLine .'call NERDComment('. a:isVisual .', "norm")' + execute firstLine .','. lastLine .'call s:NERDComment("'. a:mode .'", "Comment")' endif let &ignorecase = oldIgnoreCase @@ -2692,128 +2688,60 @@ function s:UntabbedCol(line, col) let lineTabsToSpaces = substitute(lineTruncated, '\t', s:TabSpace(), 'g') return strlen(lineTabsToSpaces) endfunction -" Section: Comment mapping setup {{{1 +" Section: Comment mapping and menu item setup {{{1 " =========================================================================== -" switch to/from alternative delimiters -nnoremap NERDCommenterAltDelims :call SwitchToAlternativeDelimiters(1) - -" comment out lines -nnoremap NERDCommenterComment :call NERDComment(0, "norm") -vnoremap NERDCommenterComment :call NERDComment(1, "norm") - -" toggle comments -nnoremap NERDCommenterToggle :call NERDComment(0, "toggle") -vnoremap NERDCommenterToggle :call NERDComment(1, "toggle") - -" minimal comments -nnoremap NERDCommenterMinimal :call NERDComment(0, "minimal") -vnoremap NERDCommenterMinimal :call NERDComment(1, "minimal") - -" sexy comments -nnoremap NERDCommenterSexy :call NERDComment(0, "sexy") -vnoremap NERDCommenterSexy :call NERDComment(1, "sexy") - -" invert comments -nnoremap NERDCommenterInvert :call NERDComment(0, "invert") -vnoremap NERDCommenterInvert :call NERDComment(1, "invert") - -" yank then comment -nmap NERDCommenterYank :call NERDComment(0, "yank") -vmap NERDCommenterYank :call NERDComment(1, "yank") - -" left aligned comments -nnoremap NERDCommenterAlignLeft :call NERDComment(0, "alignLeft") -vnoremap NERDCommenterAlignLeft :call NERDComment(1, "alignLeft") - -" left and right aligned comments -nnoremap NERDCommenterAlignBoth :call NERDComment(0, "alignBoth") -vnoremap NERDCommenterAlignBoth :call NERDComment(1, "alignBoth") - -" nested comments -nnoremap NERDCommenterNest :call NERDComment(0, "nested") -vnoremap NERDCommenterNest :call NERDComment(1, "nested") - -" uncomment -nnoremap NERDCommenterUncomment :call NERDComment(0, "uncomment") -vnoremap NERDCommenterUncomment :call NERDComment(1, "uncomment") - -" comment till the end of the line -nnoremap NERDCommenterToEOL :call NERDComment(0, "toEOL") - -" append comments -nmap NERDCommenterAppend :call NERDComment(0, "append") - -" insert comments -inoremap NERDCommenterInInsert :call NERDComment(0, "insert") - - -function! s:CreateMaps(target, combo) - if !hasmapto(a:target, 'n') - exec 'nmap ' . a:combo . ' ' . a:target - endif - - if !hasmapto(a:target, 'v') - exec 'vmap ' . a:combo . ' ' . a:target +" Create menu items for the specified modes. If a:combo is not empty, then +" also define mappings and show a:combo in the menu items. +function! s:CreateMaps(modes, target, desc, combo) + " Build up a map command like + " 'noremap NERDCommenterComment :call NERDComment("n", "Comment")' + let plug = 'NERDCommenter' . a:target + let plug_start = 'noremap ' . plug . ' :call NERDComment("' + let plug_end = '", "' . a:target . '")' + " Build up a menu command like + " 'menu comment.Comment\\cc NERDCommenterComment' + let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment'], + \ g:NERDMenuMode, '') + let menu_command = 'menu ' . menuRoot . '.' . escape(a:desc, ' ') + if strlen(a:combo) + let leader = exists('mapleader') ? mapleader : '\' + let menu_command .= '' . escape(leader, '\') . a:combo endif + let menu_command .= ' ' . (strlen(a:combo) ? plug : a:target) + " Execute the commands built above for each requested mode. + for mode in (a:modes == '') ? [''] : split(a:modes, '\zs') + if strlen(a:combo) + execute mode . plug_start . mode . plug_end + if g:NERDCreateDefaultMappings && !hasmapto(plug, mode) + execute mode . 'map ' . a:combo . ' ' . plug + endif + endif + " Check if the user wants the menu to be displayed. + if g:NERDMenuMode != 0 + execute mode . menu_command + endif + endfor endfunction +call s:CreateMaps('nx', 'Comment', 'Comment', 'cc') +call s:CreateMaps('nx', 'Toggle', 'Toggle', 'c') +call s:CreateMaps('nx', 'Minimal', 'Minimal', 'cm') +call s:CreateMaps('nx', 'Nested', 'Nested', 'cn') +call s:CreateMaps('n', 'ToEOL', 'To EOL', 'c$') +call s:CreateMaps('nx', 'Invert', 'Invert', 'ci') +call s:CreateMaps('nx', 'Sexy', 'Sexy', 'cs') +call s:CreateMaps('nx', 'Yank', 'Yank then comment', 'cy') +call s:CreateMaps('n', 'Append', 'Append', 'cA') +call s:CreateMaps('', ':', '-Sep-', '') +call s:CreateMaps('nx', 'AlignLeft', 'Left aligned', 'cl') +call s:CreateMaps('nx', 'AlignBoth', 'Left and right aligned', 'cb') +call s:CreateMaps('', ':', '-Sep2-', '') +call s:CreateMaps('nx', 'Uncomment', 'Uncomment', 'cu') +call s:CreateMaps('n', 'AltDelims', 'Switch Delimiters', 'ca') +call s:CreateMaps('i', 'Insert', 'Insert Comment Here', '') +call s:CreateMaps('', ':', '-Sep3-', '') +call s:CreateMaps('', ':help NERDCommenterContents', 'Help', '') -if g:NERDCreateDefaultMappings - call s:CreateMaps('NERDCommenterComment', 'cc') - call s:CreateMaps('NERDCommenterToggle', 'c') - call s:CreateMaps('NERDCommenterMinimal', 'cm') - call s:CreateMaps('NERDCommenterSexy', 'cs') - call s:CreateMaps('NERDCommenterInvert', 'ci') - call s:CreateMaps('NERDCommenterYank', 'cy') - call s:CreateMaps('NERDCommenterAlignLeft', 'cl') - call s:CreateMaps('NERDCommenterAlignBoth', 'cb') - call s:CreateMaps('NERDCommenterNest', 'cn') - call s:CreateMaps('NERDCommenterUncomment', 'cu') - call s:CreateMaps('NERDCommenterToEOL', 'c$') - call s:CreateMaps('NERDCommenterAppend', 'cA') - - if !hasmapto('NERDCommenterAltDelims', 'n') - nmap ca NERDCommenterAltDelims - endif -endif - - - -" Section: Menu item setup {{{1 -" =========================================================================== -"check if the user wants the menu to be displayed -if g:NERDMenuMode != 0 - - let menuRoot = "" - if g:NERDMenuMode == 1 - let menuRoot = 'comment' - elseif g:NERDMenuMode == 2 - let menuRoot = '&comment' - elseif g:NERDMenuMode == 3 - let menuRoot = '&Plugin.&comment' - endif - - function! s:CreateMenuItems(target, desc, root) - exec 'nmenu ' . a:root . '.' . a:desc . ' ' . a:target - exec 'vmenu ' . a:root . '.' . a:desc . ' ' . a:target - endfunction - call s:CreateMenuItems("NERDCommenterComment", 'Comment', menuRoot) - call s:CreateMenuItems("NERDCommenterToggle", 'Toggle', menuRoot) - call s:CreateMenuItems('NERDCommenterMinimal', 'Minimal', menuRoot) - call s:CreateMenuItems('NERDCommenterNest', 'Nested', menuRoot) - exec 'nmenu '. menuRoot .'.To\ EOL NERDCommenterToEOL' - call s:CreateMenuItems('NERDCommenterInvert', 'Invert', menuRoot) - call s:CreateMenuItems('NERDCommenterSexy', 'Sexy', menuRoot) - call s:CreateMenuItems('NERDCommenterYank', 'Yank\ then\ comment', menuRoot) - exec 'nmenu '. menuRoot .'.Append NERDCommenterAppend' - exec 'menu '. menuRoot .'.-Sep- :' - call s:CreateMenuItems('NERDCommenterAlignLeft', 'Left\ aligned', menuRoot) - call s:CreateMenuItems('NERDCommenterAlignBoth', 'Left\ and\ right\ aligned', menuRoot) - exec 'menu '. menuRoot .'.-Sep2- :' - call s:CreateMenuItems('NERDCommenterUncomment', 'Uncomment', menuRoot) - exec 'nmenu '. menuRoot .'.Switch\ Delimiters NERDCommenterAltDelims' - exec 'imenu '. menuRoot .'.Insert\ Comment\ Here NERDCommenterInInsert' - exec 'menu '. menuRoot .'.-Sep3- :' - exec 'menu '. menuRoot .'.Help :help NERDCommenterContents' -endif +" switch to/from alternative delimiters (does not use wrapper function) +nnoremap NERDCommenterAltDelims :call SwitchToAlternativeDelimiters(1) " vim: set foldmethod=marker :