dont use b: vars to store the comment delim data, use a hash

This commit is contained in:
marty 2010-08-01 20:51:47 +12:00
parent cea72da179
commit e0191f2ce9

View File

@ -402,41 +402,27 @@ augroup END
" set for this buffer.
"
function s:SetUpForNewFiletype(filetype, forceReset)
"if we have already set the delimiters for this buffer then dont go thru
"it again
if !a:forceReset && exists("b:NERDLeft") && b:NERDLeft != ''
return
endif
let b:NERDSexyComMarker = ''
if has_key(s:delimiterMap, a:filetype)
let data = s:delimiterMap[a:filetype]
let left = has_key(data, 'left') ? data['left'] : ''
let right = has_key(data, 'right') ? data['right'] : ''
let leftAlt = has_key(data, 'leftAlt') ? data['leftAlt'] : ''
let rightAlt = has_key(data, 'rightAlt') ? data['rightAlt'] : ''
let b:NERDCommenterDelims = s:delimiterMap[a:filetype]
for i in ['left', 'leftAlt', 'right', 'rightAlt']
if !has_key(b:NERDCommenterDelims, i)
let b:NERDCommenterDelims[i] = ''
endif
endfor
else
"extract the delims from &commentstring
let left= substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', '')
let right= substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g')
let leftAlt = ''
let rightAlt = ''
let b:NERDCommenterDelims = s:CreateDelimMapFromCms()
endif
if !exists('g:NERD_' . &filetype . '_alt_style')
let b:NERDLeft = left
let b:NERDRight = right
let b:NERDLeftAlt = leftAlt
let b:NERDRightAlt = rightAlt
else
let b:NERDLeft = leftAlt
let b:NERDRight = rightAlt
let b:NERDLeftAlt = left
let b:NERDRightAlt = right
endif
endfunction
function s:CreateDelimMapFromCms()
return {
\ 'left': substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', ''),
\ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'),
\ 'leftAlt': '',
\ 'rightAlt': '' }
endfunction
" Function: s:SwitchToAlternativeDelimiters(printMsgs) function {{{2
@ -451,7 +437,7 @@ endfunction
function s:SwitchToAlternativeDelimiters(printMsgs)
"if both of the alternative delimiters are empty then there is no
"alternative comment style so bail out
if b:NERDLeftAlt == "" && b:NERDRightAlt == ""
if b:NERDCommenterDelims['leftAlt'] == '' && b:NERDCommenterDelims['rightAlt'] == ''
if a:printMsgs
call s:NerdEcho("Cannot use alternative delimiters, none are specified", 0)
endif
@ -459,22 +445,20 @@ function s:SwitchToAlternativeDelimiters(printMsgs)
endif
"save the current delimiters
let tempLeft = b:NERDLeft
let tempRight = b:NERDRight
let tempLeft = s:GetLeft(0,0,0)
let tempRight = s:GetRight(0,0,0)
"swap current delimiters for alternative
let b:NERDLeft = b:NERDLeftAlt
let b:NERDRight = b:NERDRightAlt
let b:NERDCommenterDelims['left'] = b:NERDCommenterDelims['leftAlt']
let b:NERDCommenterDelims['right'] = b:NERDCommenterDelims['rightAlt']
"set the previously current delimiters to be the new alternative ones
let b:NERDLeftAlt = tempLeft
let b:NERDRightAlt = tempRight
let b:NERDCommenterDelims['leftAlt'] = tempLeft
let b:NERDCommenterDelims['rightAlt'] = tempRight
"tell the user what comment delimiters they are now using
if a:printMsgs
let leftNoEsc = b:NERDLeft
let rightNoEsc = b:NERDRight
call s:NerdEcho("Now using " . leftNoEsc . " " . rightNoEsc . " to delimit comments", 1)
call s:NerdEcho("Now using " . s:GetLeft(0,0,0) . " " . s:GetRight(0,0,0) . " to delimit comments", 1)
endif
return 1
@ -603,19 +587,19 @@ function s:CommentBlock(top, bottom, lSide, rSide, forceNested )
"stick the right delimiter down
let theLine = strpart(theLine, 0, rSide+strlen(leftSpaced)) . rightSpaced . strpart(theLine, rSide+strlen(leftSpaced))
let firstLeftDelim = s:FindDelimiterIndex(b:NERDLeft, theLine)
let lastRightDelim = s:LastIndexOfDelim(b:NERDRight, theLine)
let firstLeftDelim = s:FindDelimiterIndex(s:GetLeft(0,0,0), theLine)
let lastRightDelim = s:LastIndexOfDelim(s:GetRight(0,0,0), theLine)
if firstLeftDelim != -1 && lastRightDelim != -1
let searchStr = strpart(theLine, 0, lastRightDelim)
let searchStr = strpart(searchStr, firstLeftDelim+strlen(b:NERDLeft))
let searchStr = strpart(searchStr, firstLeftDelim+strlen(s:GetLeft(0,0,0)))
"replace the outter most delims in searchStr with
"place-holders
let theLineWithPlaceHolders = s:ReplaceDelims(b:NERDLeft, b:NERDRight, g:NERDLPlace, g:NERDRPlace, searchStr)
let theLineWithPlaceHolders = s:ReplaceDelims(s:GetLeft(0,0,0), s:GetRight(0,0,0), g:NERDLPlace, g:NERDRPlace, searchStr)
"add the right delimiter onto the line
let theLine = strpart(theLine, 0, firstLeftDelim+strlen(b:NERDLeft)) . theLineWithPlaceHolders . strpart(theLine, lastRightDelim)
let theLine = strpart(theLine, 0, firstLeftDelim+strlen(s:GetLeft(0,0,0))) . theLineWithPlaceHolders . strpart(theLine, lastRightDelim)
endif
endif
endif
@ -674,7 +658,7 @@ function s:CommentLines(forceNested, align, firstLine, lastLine)
" find out if the line is commented using normal delims and/or
" alternate ones
let isCommented = s:IsCommented(b:NERDLeft, b:NERDRight, theLine) || s:IsCommented(b:NERDLeftAlt, b:NERDRightAlt, theLine)
let isCommented = s:IsCommented(s:GetLeft(0,0,0), s:GetRight(0,0,0), theLine) || s:IsCommented(s:GetLeft(1,0,0), s:GetRight(1,0,0), theLine)
" check if we can comment this line
if !isCommented || g:NERDUsePlaceHolders || s:Multipart()
@ -955,7 +939,7 @@ function s:CommentRegion(topLine, topCol, bottomLine, bottomCol, forceNested)
endif
"stick the cursor back on the char it was on before the comment
call cursor(a:topLine, a:topCol + strlen(b:NERDLeft) + g:NERDSpaceDelims)
call cursor(a:topLine, a:topCol + strlen(s:GetLeft(0,0,0)) + g:NERDSpaceDelims)
"if we switched delims then we gotta go back to what they were before
if switchedDelims == 1
@ -980,7 +964,7 @@ function s:InvertComment(firstLine, lastLine)
let sexyComBounds = s:FindBoundingLinesOfSexyCom(currentLine)
" if the line is commented normally, uncomment it
if s:IsCommentedFromStartOfLine(b:NERDLeft, theLine) || s:IsCommentedFromStartOfLine(b:NERDLeftAlt, theLine)
if s:IsCommentedFromStartOfLine(s:GetLeft(0,0,0), theLine) || s:IsCommentedFromStartOfLine(s:GetLeft(1,0,0), theLine)
call s:UncommentLines(currentLine, currentLine)
let currentLine = currentLine + 1
@ -1063,7 +1047,7 @@ function! NERDComment(isVisual, type) range
elseif a:type == 'toggle'
let theLine = getline(firstLine)
if s:IsInSexyComment(firstLine) || s:IsCommentedFromStartOfLine(b:NERDLeft, theLine) || s:IsCommentedFromStartOfLine(b:NERDLeftAlt, theLine)
if s:IsInSexyComment(firstLine) || s:IsCommentedFromStartOfLine(s:GetLeft(0,0,0), theLine) || s:IsCommentedFromStartOfLine(s:GetLeft(1,0,0), theLine)
call s:UncommentLines(firstLine, lastLine)
else
call s:CommentLinesToggle(forceNested, firstLine, lastLine)
@ -1380,54 +1364,54 @@ function s:UncommentLineNormal(line)
let line = a:line
"get the comment status on the line so we know how it is commented
let lineCommentStatus = s:IsCommentedOuttermost(b:NERDLeft, b:NERDRight, b:NERDLeftAlt, b:NERDRightAlt, line)
let lineCommentStatus = s:IsCommentedOuttermost(s:GetLeft(0,0,0), s:GetRight(0,0,0), s:GetLeft(1,0,0), s:GetRight(1,0,0), line)
"it is commented with b:NERDLeft and b:NERDRight so remove these delims
"it is commented with s:GetLeft(0,0,0) and s:GetRight(0,0,0) so remove these delims
if lineCommentStatus == 1
let line = s:RemoveDelimiters(b:NERDLeft, b:NERDRight, line)
let line = s:RemoveDelimiters(s:GetLeft(0,0,0), s:GetRight(0,0,0), line)
"it is commented with b:NERDLeftAlt and b:NERDRightAlt so remove these delims
"it is commented with s:GetLeft(1,0,0) and s:GetRight(1,0,0) so remove these delims
elseif lineCommentStatus == 2 && g:NERDRemoveAltComs
let line = s:RemoveDelimiters(b:NERDLeftAlt, b:NERDRightAlt, line)
let line = s:RemoveDelimiters(s:GetLeft(1,0,0), s:GetRight(1,0,0), line)
"it is not properly commented with any delims so we check if it has
"any random left or right delims on it and remove the outtermost ones
else
"get the positions of all delim types on the line
let indxLeft = s:FindDelimiterIndex(b:NERDLeft, line)
let indxLeftAlt = s:FindDelimiterIndex(b:NERDLeftAlt, line)
let indxRight = s:FindDelimiterIndex(b:NERDRight, line)
let indxRightAlt = s:FindDelimiterIndex(b:NERDRightAlt, line)
let indxLeft = s:FindDelimiterIndex(s:GetLeft(0,0,0), line)
let indxLeftAlt = s:FindDelimiterIndex(s:GetLeft(1,0,0), line)
let indxRight = s:FindDelimiterIndex(s:GetRight(0,0,0), line)
let indxRightAlt = s:FindDelimiterIndex(s:GetRight(1,0,0), line)
"remove the outter most left comment delim
if indxLeft != -1 && (indxLeft < indxLeftAlt || indxLeftAlt == -1)
let line = s:RemoveDelimiters(b:NERDLeft, '', line)
let line = s:RemoveDelimiters(s:GetLeft(0,0,0), '', line)
elseif indxLeftAlt != -1
let line = s:RemoveDelimiters(b:NERDLeftAlt, '', line)
let line = s:RemoveDelimiters(s:GetLeft(1,0,0), '', line)
endif
"remove the outter most right comment delim
if indxRight != -1 && (indxRight < indxRightAlt || indxRightAlt == -1)
let line = s:RemoveDelimiters('', b:NERDRight, line)
let line = s:RemoveDelimiters('', s:GetRight(0,0,0), line)
elseif indxRightAlt != -1
let line = s:RemoveDelimiters('', b:NERDRightAlt, line)
let line = s:RemoveDelimiters('', s:GetRight(1,0,0), line)
endif
endif
let indxLeft = s:FindDelimiterIndex(b:NERDLeft, line)
let indxLeftAlt = s:FindDelimiterIndex(b:NERDLeftAlt, line)
let indxLeft = s:FindDelimiterIndex(s:GetLeft(0,0,0), line)
let indxLeftAlt = s:FindDelimiterIndex(s:GetLeft(1,0,0), line)
let indxLeftPlace = s:FindDelimiterIndex(g:NERDLPlace, line)
let indxRightPlace = s:FindDelimiterIndex(g:NERDRPlace, line)
let indxRightAlt = s:FindDelimiterIndex(b:NERDRightAlt, line)
let indxRightAlt = s:FindDelimiterIndex(s:GetRight(1,0,0), line)
let indxRightPlace = s:FindDelimiterIndex(g:NERDRPlace, line)
let right = b:NERDRight
let left = b:NERDLeft
let right = s:GetRight(0,0,0)
let left = s:GetLeft(0,0,0)
if !s:Multipart()
let right = b:NERDRightAlt
let left = b:NERDLeftAlt
let right = s:GetRight(1,0,0)
let left = s:GetLeft(1,0,0)
endif
@ -1518,7 +1502,7 @@ endfunction
" Function: s:AltMultipart() {{{2
" returns 1 if the alternative delims are multipart
function s:AltMultipart()
return b:NERDRightAlt != ''
return b:NERDCommenterDelims['rightAlt'] != ''
endfunction
" Function: s:CanCommentLine(forceNested, line) {{{2
@ -1589,7 +1573,7 @@ endfunction
" -lineNum: the line num of the line to check for commentability
function s:CanToggleCommentLine(forceNested, lineNum)
let theLine = getline(a:lineNum)
if (s:IsCommentedFromStartOfLine(b:NERDLeft, theLine) || s:IsCommentedFromStartOfLine(b:NERDLeftAlt, theLine)) && !a:forceNested
if (s:IsCommentedFromStartOfLine(s:GetLeft(0,0,0), theLine) || s:IsCommentedFromStartOfLine(s:GetLeft(1,0,0), theLine)) && !a:forceNested
return 0
endif
@ -1709,9 +1693,9 @@ endfunction
function s:DoesBlockHaveMultipartDelim(top, bottom)
if s:HasMultipartDelims()
if s:Multipart()
return s:DoesBlockHaveDelim(b:NERDLeft, a:top, a:bottom) || s:DoesBlockHaveDelim(b:NERDRight, a:top, a:bottom)
return s:DoesBlockHaveDelim(s:GetLeft(0,0,0), a:top, a:bottom) || s:DoesBlockHaveDelim(s:GetRight(0,0,0), a:top, a:bottom)
else
return s:DoesBlockHaveDelim(b:NERDLeftAlt, a:top, a:bottom) || s:DoesBlockHaveDelim(b:NERDRightAlt, a:top, a:bottom)
return s:DoesBlockHaveDelim(s:GetLeft(1,0,0), a:top, a:bottom) || s:DoesBlockHaveDelim(s:GetRight(1,0,0), a:top, a:bottom)
endif
endif
return 0
@ -1861,15 +1845,8 @@ endfunction
" (the space string will only be added if NERDSpaceDelims is set)
" -esc: specifies whether the tricky chars in the delim should be ESCed
function s:GetLeft(alt, space, esc)
let delim = b:NERDLeft
let delim = a:alt ? b:NERDCommenterDelims['leftAlt'] : b:NERDCommenterDelims['left']
if a:alt
if b:NERDLeftAlt == ''
return ''
else
let delim = b:NERDLeftAlt
endif
endif
if delim == ''
return ''
endif
@ -1893,15 +1870,8 @@ endfunction
" (the space string will only be added if NERDSpaceDelims is set)
" -esc: specifies whether the tricky chars in the delim should be ESCed
function s:GetRight(alt, space, esc)
let delim = b:NERDRight
let delim = a:alt ? b:NERDCommenterDelims['rightAlt'] : b:NERDCommenterDelims['right']
if a:alt
if !s:AltMultipart()
return ''
else
let delim = b:NERDRightAlt
endif
endif
if delim == ''
return ''
endif
@ -1942,14 +1912,14 @@ function s:GetSexyComMarker(space, esc)
else
"find a comment marker by getting the longest available left delim
"(that has a corresponding right delim) and taking the last char
let lenLeft = strlen(b:NERDLeft)
let lenLeftAlt = strlen(b:NERDLeftAlt)
let lenLeft = strlen(s:GetLeft(0,0,0))
let lenLeftAlt = strlen(s:GetLeft(1,0,0))
let left = ''
let right = ''
if s:Multipart() && lenLeft >= lenLeftAlt
let left = b:NERDLeft
let left = s:GetLeft(0,0,0)
elseif s:AltMultipart()
let left = b:NERDLeftAlt
let left = s:GetLeft(1,0,0)
else
return -1
endif
@ -1978,8 +1948,8 @@ endfunction
" (the space string will only be added if NERDSpaceDelims is set)
" -esc: specifies whether the tricky chars in the string are ESCed
function s:GetSexyComLeft(space, esc)
let lenLeft = strlen(b:NERDLeft)
let lenLeftAlt = strlen(b:NERDLeftAlt)
let lenLeft = strlen(s:GetLeft(0,0,0))
let lenLeftAlt = strlen(s:GetLeft(1,0,0))
let left = ''
"assume c style sexy comments if possible
@ -1988,9 +1958,9 @@ function s:GetSexyComLeft(space, esc)
else
"grab the longest left delim that has a right
if s:Multipart() && lenLeft >= lenLeftAlt
let left = b:NERDLeft
let left = s:GetLeft(0,0,0)
elseif s:AltMultipart()
let left = b:NERDLeftAlt
let left = s:GetLeft(1,0,0)
else
return -1
endif
@ -2016,8 +1986,8 @@ endfunction
" is specified for the current filetype)
" -esc: specifies whether the tricky chars in the string are ESCed
function s:GetSexyComRight(space, esc)
let lenLeft = strlen(b:NERDLeft)
let lenLeftAlt = strlen(b:NERDLeftAlt)
let lenLeft = strlen(s:GetLeft(0,0,0))
let lenLeftAlt = strlen(s:GetLeft(1,0,0))
let right = ''
"assume c style sexy comments if possible
@ -2026,9 +1996,9 @@ function s:GetSexyComRight(space, esc)
else
"grab the right delim that pairs with the longest left delim
if s:Multipart() && lenLeft >= lenLeftAlt
let right = b:NERDRight
let right = s:GetRight(0,0,0)
elseif s:AltMultipart()
let right = b:NERDRightAlt
let right = s:GetRight(1,0,0)
else
return -1
endif
@ -2064,7 +2034,7 @@ endfunction
" Function: s:HasCStyleComments() {{{2
" Returns 1 iff the current filetype has c style comment delimiters
function s:HasCStyleComments()
return (b:NERDLeft == '/*' && b:NERDRight == '*/') || (b:NERDLeftAlt == '/*' && b:NERDRightAlt == '*/')
return (s:GetLeft(0,0,0) == '/*' && s:GetRight(0,0,0) == '*/') || (s:GetLeft(1,0,0) == '/*' && s:GetRight(1,0,0) == '*/')
endfunction
" Function: s:IsCommentedNormOrSexy(lineNum) {{{2
@ -2077,7 +2047,7 @@ function s:IsCommentedNormOrSexy(lineNum)
let theLine = getline(a:lineNum)
"if the line is commented normally return 1
if s:IsCommented(b:NERDLeft, b:NERDRight, theLine) || s:IsCommented(b:NERDLeftAlt, b:NERDRightAlt, theLine)
if s:IsCommented(s:GetLeft(0,0,0), s:GetRight(0,0,0), theLine) || s:IsCommented(s:GetLeft(1,0,0), s:GetRight(1,0,0), theLine)
return 1
endif
@ -2293,11 +2263,11 @@ function s:IsSexyComment(topline, bottomline)
let left = ''
let right = ''
if s:Multipart()
let left = b:NERDLeft
let right = b:NERDRight
let left = s:GetLeft(0,0,0)
let right = s:GetRight(0,0,0)
elseif s:AltMultipart()
let left = b:NERDLeftAlt
let right = b:NERDRightAlt
let left = s:GetLeft(1,0,0)
let right = s:GetRight(1,0,0)
else
return 0
endif
@ -2422,7 +2392,7 @@ function s:LeftMostIndx(countCommentedLines, countEmptyLines, topline, bottomlin
" commented, check it
let theLine = getline(currentLine)
if a:countEmptyLines || theLine !~ '^[ \t]*$'
if a:countCommentedLines || (!s:IsCommented(b:NERDLeft, b:NERDRight, theLine) && !s:IsCommented(b:NERDLeftAlt, b:NERDRightAlt, theLine))
if a:countCommentedLines || (!s:IsCommented(s:GetLeft(0,0,0), s:GetRight(0,0,0), theLine) && !s:IsCommented(s:GetLeft(1,0,0), s:GetRight(1,0,0), theLine))
" convert spaces to tabs and get the number of leading spaces for
" this line and update leftMostIndx if need be
let theLine = s:ConvertLeadingTabsToSpaces(theLine)
@ -2447,7 +2417,7 @@ endfunction
" Function: s:Multipart() {{{2
" returns 1 if the current delims are multipart
function s:Multipart()
return b:NERDRight != ''
return s:GetRight(0,0,0) != ''
endfunction
" Function: s:NerdEcho(msg, typeOfMsg) {{{2
@ -2578,7 +2548,7 @@ function s:RightMostIndx(countCommentedLines, countEmptyLines, topline, bottomli
let theLine = getline(currentLine)
if a:countEmptyLines || theLine !~ '^[ \t]*$'
if a:countCommentedLines || (!s:IsCommented(b:NERDLeft, b:NERDRight, theLine) && !s:IsCommented(b:NERDLeftAlt, b:NERDRightAlt, theLine))
if a:countCommentedLines || (!s:IsCommented(s:GetLeft(0,0,0), s:GetRight(0,0,0), theLine) && !s:IsCommented(s:GetLeft(1,0,0), s:GetRight(1,0,0), theLine))
" update rightMostIndx if need be
let theLine = s:ConvertLeadingTabsToSpaces(theLine)
@ -2613,20 +2583,20 @@ endfunction
function s:SwapOutterMultiPartDelimsForPlaceHolders(line)
" find out if the line is commented using normal delims and/or
" alternate ones
let isCommented = s:IsCommented(b:NERDLeft, b:NERDRight, a:line)
let isCommentedAlt = s:IsCommented(b:NERDLeftAlt, b:NERDRightAlt, a:line)
let isCommented = s:IsCommented(s:GetLeft(0,0,0), s:GetRight(0,0,0), a:line)
let isCommentedAlt = s:IsCommented(s:GetLeft(1,0,0), s:GetRight(1,0,0), a:line)
let line2 = a:line
"if the line is commented and there is a right delimiter, replace
"the delims with place-holders
if isCommented && s:Multipart()
let line2 = s:ReplaceDelims(b:NERDLeft, b:NERDRight, g:NERDLPlace, g:NERDRPlace, a:line)
let line2 = s:ReplaceDelims(s:GetLeft(0,0,0), s:GetRight(0,0,0), g:NERDLPlace, g:NERDRPlace, a:line)
"similarly if the line is commented with the alternative
"delimiters
elseif isCommentedAlt && s:AltMultipart()
let line2 = s:ReplaceDelims(b:NERDLeftAlt, b:NERDRightAlt, g:NERDLPlace, g:NERDRPlace, a:line)
let line2 = s:ReplaceDelims(s:GetLeft(1,0,0), s:GetRight(1,0,0), g:NERDLPlace, g:NERDRPlace, a:line)
endif
return line2
@ -2642,11 +2612,11 @@ function s:SwapOutterPlaceHoldersForMultiPartDelims(line)
let left = ''
let right = ''
if s:Multipart()
let left = b:NERDLeft
let right = b:NERDRight
let left = s:GetLeft(0,0,0)
let right = s:GetRight(0,0,0)
elseif s:AltMultipart()
let left = b:NERDLeftAlt
let right = b:NERDRightAlt
let left = s:GetLeft(1,0,0)
let right = s:GetRight(1,0,0)
endif
let line = s:ReplaceDelims(g:NERDLPlace, g:NERDRPlace, left, right, a:line)