From 8ed440ea20b083fb93a611b4d73dbf0035737b21 Mon Sep 17 00:00:00 2001 From: ervandew Date: Sat, 6 Nov 2010 17:20:16 -0700 Subject: [PATCH 1/4] add var for default align + add support for aligning at index 0 --- doc/NERD_commenter.txt | 23 ++++++++++++++++++++++- plugin/NERD_commenter.vim | 31 +++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/doc/NERD_commenter.txt b/doc/NERD_commenter.txt index d7493b1..4c295cc 100644 --- a/doc/NERD_commenter.txt +++ b/doc/NERD_commenter.txt @@ -429,6 +429,9 @@ then the script would do a sexy comment on the last visual selection. uncommenting. |'NERDCompactSexyComs'| Specifies whether to use the compact style sexy comments. +|'NERDDefaultAlign'| Specifies the default aligment to use, + one of 'none', 'left', 'start', or + 'both'. ------------------------------------------------------------------------------ 4.3 Options details *NERDComOptionsDetails* @@ -602,6 +605,14 @@ Otherwise they would become: > If you want the spaces to be removed only if |'NERDSpaceDelims'| is set then set NERDRemoveExtraSpaces to 0. +Note: When using 'start' as the default alignment, the enabling of +NERDRemoveExtraSpaces will still result in the removal of a space after the +delimiter. This can be undesirable since aligning the delimiters at the very +start of the line (index 0) will usually result in spaces between the comment +delimiters and the text which probably shouldn't be removed. So when using +'start' as the default alignment, take care to also disable +NERDRemoveExtraSpaces. + ------------------------------------------------------------------------------ *'NERDLPlace'* *'NERDRPlace'* @@ -658,7 +669,17 @@ as opposed to this: > < If you want spaces to be added then set NERDSpaceDelims to 1 in your vimrc. -See also |'NERDRemoveExtraSpaces'|. + +------------------------------------------------------------------------------ + *'NERDDefaultAlign'* +Values: 'none', 'left', 'start', 'both' +Default 'none'. + +Specifies the default alignment to use when inserting comments. + +Note: When using 'start' as the default alignment be sure to disable +NERDRemoveExtraSpaces. See the note at the bottom of |NERDRemoveExtraSpaces| +for more details. ------------------------------------------------------------------------------ *'NERDCompactSexyComs'* diff --git a/plugin/NERD_commenter.vim b/plugin/NERD_commenter.vim index 982b140..83ed376 100644 --- a/plugin/NERD_commenter.vim +++ b/plugin/NERD_commenter.vim @@ -62,6 +62,7 @@ call s:InitVariable("g:NERDRemoveExtraSpaces", 1) call s:InitVariable("g:NERDRPlace", "<]") call s:InitVariable("g:NERDSpaceDelims", 0) call s:InitVariable("g:NERDDelimiterRequests", 1) +call s:InitVariable("g:NERDDefaultAlign", "none") let s:NERDFileNameEscape="[]#*$%'\" ?`!&();<>\\" "vf ;;dA:hcs"'A {j^f(lyi(k$p0f{a A }0f{a 'left':jdd^ @@ -629,13 +630,13 @@ endfunction " Args: " -forceNested: a flag indicating whether the called is requesting the comment " to be nested if need be -" -align: should be "left" or "both" or "none" +" -align: should be "left", "start", "both" or "none" " -firstLine/lastLine: the top and bottom lines to comment function s:CommentLines(forceNested, align, firstLine, lastLine) " we need to get the left and right indexes of the leftmost char in the " block of of lines and the right most char so that we can do alignment of " the delimiters if the user has specified - let leftAlignIndx = s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) + let leftAlignIndx = a:align == "start" ? 0 : s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) let rightAlignIndx = s:RightMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) " gotta add the length of the left delimiter onto the rightAlignIndx cos @@ -663,7 +664,7 @@ function s:CommentLines(forceNested, align, firstLine, lastLine) " check if we can comment this line if !isCommented || g:NERDUsePlaceHolders || s:Multipart() - if a:align == "left" || a:align == "both" + if a:align == "left" || a:align == "start" || a:align == "both" let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx) else let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) @@ -867,6 +868,12 @@ endfunction " -firstLine/lastLine: the top and bottom lines to comment function s:CommentLinesToggle(forceNested, firstLine, lastLine) let currentLine = a:firstLine + + let align = g:NERDDefaultAlign + let leftAlignIndx = align == "start" ? 0 : s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) + let rightAlignIndx = s:RightMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) + let rightAlignIndx = rightAlignIndx + strlen(s:Left({'space': 1})) + while currentLine <= a:lastLine " get the next line, check commentability and convert spaces to tabs @@ -881,8 +888,16 @@ function s:CommentLinesToggle(forceNested, firstLine, lastLine) let theLine = s:SwapOutterMultiPartDelimsForPlaceHolders(theLine) endif - let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) - let theLine = s:AddRightDelim(s:Right({'space': 1}), theLine) + if align == 'left' || align == 'start' || align == 'both' + let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx) + else + let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) + endif + if align == "both" + let theLine = s:AddRightDelimAligned(s:Right({'space': 1}), theLine, rightAlignIndx) + else + let theLine = s:AddRightDelim(s:Right({'space': 1}), theLine) + endif endif " restore leading tabs if appropriate @@ -929,7 +944,7 @@ function s:CommentRegion(topLine, topCol, bottomLine, bottomCol, forceNested) let topOfRange = a:topLine+1 let bottomOfRange = a:bottomLine-1 if topOfRange <= bottomOfRange - call s:CommentLines(a:forceNested, "none", topOfRange, bottomOfRange) + call s:CommentLines(a:forceNested, g:NERDDefaultAlign, topOfRange, bottomOfRange) endif "comment the bottom line @@ -1021,7 +1036,7 @@ function! NERDComment(isVisual, type) range elseif a: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) + call s:CommentLines(forceNested, g:NERDDefaultAlign, firstLine, lastLine) endif elseif a:type == 'alignLeft' || a:type == 'alignBoth' @@ -1040,7 +1055,7 @@ function! NERDComment(isVisual, type) range try call s:CommentLinesSexy(firstLine, lastLine) catch /NERDCommenter.Delimiters/ - call s:CommentLines(forceNested, "none", firstLine, lastLine) + call s:CommentLines(forceNested, g:NERDDefaultAlign, firstLine, lastLine) catch /NERDCommenter.Nesting/ call s:NerdEcho("Sexy comment aborted. Nested sexy cannot be nested", 0) endtry From 8ef3473c2ef6daefacba1d3e7d0c40c811289e85 Mon Sep 17 00:00:00 2001 From: ervandew Date: Sat, 6 Nov 2010 17:52:50 -0700 Subject: [PATCH 2/4] fix a couple typos --- doc/NERD_commenter.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/NERD_commenter.txt b/doc/NERD_commenter.txt index 4c295cc..a543522 100644 --- a/doc/NERD_commenter.txt +++ b/doc/NERD_commenter.txt @@ -429,7 +429,7 @@ then the script would do a sexy comment on the last visual selection. uncommenting. |'NERDCompactSexyComs'| Specifies whether to use the compact style sexy comments. -|'NERDDefaultAlign'| Specifies the default aligment to use, +|'NERDDefaultAlign'| Specifies the default alignment to use, one of 'none', 'left', 'start', or 'both'. @@ -669,6 +669,7 @@ as opposed to this: > < If you want spaces to be added then set NERDSpaceDelims to 1 in your vimrc. +See also |'NERDRemoveExtraSpaces'|. ------------------------------------------------------------------------------ *'NERDDefaultAlign'* From 0c046544984a02b5a3fd7d4c035119e043d8d3e3 Mon Sep 17 00:00:00 2001 From: ervandew Date: Wed, 6 Feb 2013 07:54:06 -0800 Subject: [PATCH 3/4] add config support for default delims if there are no configured delims for the current filetype, allow the default delims to be defined by a global setting since c style comment delims are not always the best defaults for everyone. --- plugin/NERD_commenter.vim | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugin/NERD_commenter.vim b/plugin/NERD_commenter.vim index d9225cc..d84aa70 100644 --- a/plugin/NERD_commenter.vim +++ b/plugin/NERD_commenter.vim @@ -441,6 +441,15 @@ function s:SetUpForNewFiletype(filetype, forceReset) endfunction function s:CreateDelimMapFromCms() + if exists('g:NERDDefaultDelims') + let delims = g:NERDDefaultDelims + for i in ['left', 'leftAlt', 'right', 'rightAlt'] + if !has_key(delims, i) + let delims[i] = '' + endif + endfor + return delims + endif return { \ 'left': substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', ''), \ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'), From 4f3d9c0e121f147fa88427e73e3eefef765852b7 Mon Sep 17 00:00:00 2001 From: ervandew Date: Mon, 11 Feb 2013 15:03:07 -0800 Subject: [PATCH 4/4] only use g:NERDDefaultDelims if no filetype has been set --- plugin/NERD_commenter.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/NERD_commenter.vim b/plugin/NERD_commenter.vim index d84aa70..651d82f 100644 --- a/plugin/NERD_commenter.vim +++ b/plugin/NERD_commenter.vim @@ -441,7 +441,7 @@ function s:SetUpForNewFiletype(filetype, forceReset) endfunction function s:CreateDelimMapFromCms() - if exists('g:NERDDefaultDelims') + if &ft == '' && exists('g:NERDDefaultDelims') let delims = g:NERDDefaultDelims for i in ['left', 'leftAlt', 'right', 'rightAlt'] if !has_key(delims, i)