📄 herosoft.dsm
字号:
' Replace all the trailing whitespace (thanks to Paul Bludov)
ActiveDocument.Selection.ReplaceText "\:b+\($\)", "\1", dsMatchRegExp
' Fix tabs within code surrounded by braces
TabifyMatchingBraces
' Remove any lines that are considered extraneous (ie. extra blank lines)
RemoveExtraneousLines
' Indent every "case" inside switch statements (thanks to Jim Cooper)
IndentSwitchBody
'使"\t{"变成"\t\t{",使"\t}"变成"\t\t}",
Replace " {", " @{"
Replace " }", " @}"
Replace " @{", " {"
Replace " @}", " }"
Replace "else ","else"+vbtab
Replace "else ","else"+vbtab
Replace "else ","else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab+vbtab+vbtab+vbtab+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab+vbtab+vbtab+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab+vbtab+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab+vbtab, "else"+vbtab
Replace "else"+"\:b+"+vbtab, "else"+vbtab
Replace "##lse","#else"
'Replace " = ",vbtab+"= "
' Go back to where we were at the beginning
ActiveDocument.Selection.GoToLine nLine
End Sub
' Is the cursor currently within a quoted string (or character)
function IsWithinQuotes
dim nCurrentLine, nCurrentColumn, iPos, strBuffer, nCount
nCurrentLine = ActiveDocument.Selection.CurrentLine
nCurrentColumn = ActiveDocument.Selection.CurrentColumn
ActiveDocument.Selection.Cancel
ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend
nCount = 0
iPos = 0
strBuffer = ActiveDocument.Selection
' Count all occurrences of a double quote which apply to quoted strings
do while true
iPos = InStr(iPos + 1, strBuffer, """", vbTextCompare)
if not (iPos > 0) then
exit do
end if
if iPos = 1 then ' if it's the first character, then it's valid
nCount = nCount + 1
else
' Make sure it's not preceded by a \ or a \\
if Mid(strBuffer, iPos - 1, 1) <> "\" then
nCount = nCount + 1
elseif (iPos > 2) and (Mid(strBuffer, iPos - 2, 1) = "\") then
nCount = nCount + 1
end if
end if
loop
' If number of quotes is odd, we must be inside a quoted string!
IsWithinQuotes = ((nCount > 0) and ((nCount Mod 2) <> 0))
ActiveDocument.Selection.MoveTo nCurrentLine, nCurrentColumn
' If we're not inside a quoted string, check for a quoted character
if not IsWithinQuotes then
ActiveDocument.Selection.CharLeft dsExtend
' If we find a quoted character left of us, check for one on the right
if ActiveDocument.Selection = "'" then
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.CharRight dsExtend
if ActiveDocument.Selection = "\" then
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.CharRight dsExtend
end if
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.CharRight dsExtend
if ActiveDocument.Selection = "'" then
IsWithinQuotes = true
end if
end if
ActiveDocument.Selection.MoveTo nCurrentLine, nCurrentColumn
end if
' If we're inside quotes, proceed from the next character
if IsWithinQuotes then
ActiveDocument.Selection.CharRight
end if
end function
' Is current selection preceded by a C++ comment? ("//")
function IsWithinComment
dim nCurrentLine, nCurrentColumn
nCurrentLine = ActiveDocument.Selection.CurrentLine
nCurrentColumn = ActiveDocument.Selection.CurrentColumn
ActiveDocument.Selection.Cancel
ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend
IsWithinComment = false
if (InStr(1, ActiveDocument.Selection, "//", vbTextCompare) > 0) then
IsWithinComment = true ' since it's commented out
nCurrentLine = nCurrentLine + 1 ' we proceed from the next line
end if
ActiveDocument.Selection.MoveTo nCurrentLine, 1
end function
' Should the current selection be ignored?
' (ie., is it within a comment or between quotes?)
function ShouldIgnore
ShouldIgnore = false
if IsWithinQuotes then
ShouldIgnore = true
exit function
end if
if IsWithinComment then
ShouldIgnore = true
end if
end function
' Put the cursor at the top of the document and return "" to be passed
' initially to GetCurrenPosition
function InitializePosition
ActiveDocument.Selection.StartOfDocument
InitializePosition = ""
end function
' Retrieve the current position and return true if it's greater than the
' last one. This is used to ensure that the file is only searched once
' (provided the search is started from the top)
function GetCurrentPosition(strPos)
dim nLastLine, nLastColumn, nCurrentLine, nCurrentColumn, iPos, ch
nLastLine = -1
nLastColumn = -1
nCurrentLine = ActiveDocument.Selection.CurrentLine
nCurrentColumn = ActiveDocument.Selection.CurrentColumn
' Parse the last position and extract the line and column
for iPos = 1 to Len(strPos)
ch = Mid(strPos, iPos, 1)
if ch = "," then
nLastLine = Int(Mid(strPos, 1, iPos))
nLastColumn = Int(Mid(strPos, iPos + 1))
exit for
end if
next
' Return true if we're currently past the last position
strPos = nCurrentLine & "," & nCurrentColumn
GetCurrentPosition = (nCurrentLine > nLastLine) or _
((nLastLine = nCurrentLine) and (nCurrentColumn > nLastColumn))
end function
' Move by a certain number of columns
sub MoveByColumns(nBy)
ActiveDocument.Selection.MoveTo ActiveDocument.Selection.CurrentLine, _
ActiveDocument.Selection.CurrentColumn + nBy
end sub
' Replace the given strFrom with strTo case sensitively
sub Replace(strFrom, strTo)
dim strLastPos, bContinue
strLastPos = InitializePosition
do while ActiveDocument.Selection.FindText(strFrom, _
dsMatchCase + dsMatchRegExp)
bContinue = GetCurrentPosition(strLastPos)
' Check if we're inside a comment or between quotes
if not ShouldIgnore then
' Repeat the search since ShouldIgnore puts the cursor at
' the beginning of the line
ActiveDocument.Selection.FindText strFrom, _
dsMatchCase + dsMatchRegExp
ActiveDocument.Selection = strTo
elseif not bContinue then
exit do
end if
loop
end sub
' Break the given str ending in (, so that instead of this:
' if (a) return b;
' it looks like this:
' if (a)
' return b;
sub BreakSingleLiners(str)
dim strLastPos, strFound, nCol, bBreak, strAfterFound
' Verify str ends in (, the beginning parenthesis
if Right(str, 1) <> "(" then
exit sub
end if
strLastPos = InitializePosition
while ActiveDocument.Selection.FindText(str, dsMatchCase) and _
GetCurrentPosition(strLastPos)
' Check if we're inside a comment or between quotes
if not ShouldIgnore then
' Repeat the search since ShouldIgnore puts the cursor at the
' beginning of the line
ActiveDocument.Selection.FindText str, dsMatchCase
' Find the matching brace and go to the end of the line
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.CharLeft
ExecuteCommand "GoToMatchBrace"
ActiveDocument.Selection.CharRight
nCol = ActiveDocument.Selection.CurrentColumn
ActiveDocument.Selection.EndOfLine dsExtend
strFound = LTrimTabs(ActiveDocument.Selection)
' If there's anything after the brace that isn't a comment, move
' it to the next line
if (Len(strFound) > 0) and (Left(strFound, 1) <> "/") then
bBreak = false
' Check if there's a "{" after the statement which should
' also be broken into multiple lines
if (Mid(strFound, 1, 1) = "{") and (Len(strFound) > 1) then
strAfterFound = LTrimTabs(Mid(strFound, 2))
if strAfterFound <> "" then
ActiveDocument.Selection = "{" + strAfterFound
ActiveDocument.Selection.MoveTo _
ActiveDocument.Selection.CurrentLine, nCol
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.NewLine
bBreak = true ' primitive but it works
end if
end if
if not bBreak then
ActiveDocument.Selection = strFound
ActiveDocument.Selection.MoveTo _
ActiveDocument.Selection.CurrentLine, nCol
ActiveDocument.Selection.NewLine
end if
end if
end if
wend
end sub
' Trim blanks AND tabs from the left side
function LTrimTabs(str)
dim iPos, ch
for iPos = 1 to Len(str)
ch = Mid(str, iPos, 1)
if ch <> " " and ch <> vbTab then
exit for
end if
next
LTrimTabs = Mid(str, iPos)
end function
' Isolate the given str on a new line with nothing left of it
sub IsolateOnLeft(str)
dim strLastPos, nLen, bContinue, nCurrentLine, nCurrentColumn
strLastPos = InitializePosition
while ActiveDocument.Selection.FindText("^.*" & str, dsMatchRegExp) and _
GetCurrentPosition(strLastPos)
' Check if we're inside a comment or between quotes
if not ShouldIgnore then
' Repeat the search since ShouldIgnore puts the cursor at the
' beginning of the line
ActiveDocument.Selection.FindText "^.*" & str, dsMatchRegExp
' Get the length of the found string
' (which may have been a regular expression)
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.FindText str, _
dsMatchBackward + dsMatchRegExp
nLen = Len(ActiveDocument.Selection)
ActiveDocument.Selection.CharLeft
if not ShouldIgnore then
' Now that we have the length, we need to redo
' the search and go on
ActiveDocument.Selection.StartOfLine
ActiveDocument.Selection.FindText "^.*" & str, dsMatchRegExp
bContinue = false
' If we're isolating a brace, make sure its matching brace
' isn't on the same line
if (str = "{") or (str = "}") then
ActiveDocument.Selection.CharRight
nCurrentLine = ActiveDocument.Selection.CurrentLine
nCurrentColumn = ActiveDocument.Selection.CurrentColumn
ActiveDocument.Selection.CharLeft
ExecuteCommand "GoToMatchBrace"
if ActiveDocument.Selection.CurrentLine = nCurrentLine then
ActiveDocument.Selection.MoveTo _
nCurrentLine, nCurrentColumn
bContinue = true ' we found it so move to the next match
else
ActiveDocument.Selection.MoveTo nCurrentLine, 1
ActiveDocument.Selection.FindText "^.*" & str, _
dsMatchRegExp
end if
end if
if LTrimTabs(ActiveDocument.Selection) <> str and _
not bContinue then
ActiveDocument.Selection.CharRight
MoveByColumns -nLen
ActiveDocument.Selection.NewLine
MoveByColumns nLen
end if
GetCurrentPosition strLastPos
end if
end if
wend
end sub
' Isolate the given str so that everything after it is placed on the next line
sub IsolateOnRight(str)
dim strLastPos, strRight, nCurrentLine, nCurrentColumn, nLen
strLastPos = InitializePosition
while ActiveDocument.Selection.FindText(str & ".+$", dsMatchRegExp) and _
GetCurrentPosition(strLastPos)
' Check if we're inside a comment or between quotes
ActiveDocument.Selection.CharLeft
if not ShouldIgnore then
' Repeat the search since ShouldIgnore puts the cursor at the
' beginning of the line
ActiveDocument.Selection.FindText str & ".+$", dsMatchRegExp
' Get the length of the found string
' (which may have been a regular expression)
ActiveDocument.Selection.CharLeft
ActiveDocument.Selection.FindText str, dsMatchRegExp
nLen = Len(ActiveDocument.Selection)
' Now that we have the length, we need to redo the search and go on
ActiveDocument.Selection.CharLeft
ActiveDocument.Selection.FindText str & ".+$", dsMatchRegExp
strRight = LTrimTabs(Mid(ActiveDocument.Selection, nLen + 1))
' Handle braces a bit differently
if (str = "{") or (str = "}") then
' If it's a closing brace, and the code after it contains
' a semicolon, leave it alone (ie. variable definition).
if (str = "}") then
ActiveDocument.Selection.EndOfLine dsExtend
if (InStr(1, ActiveDocument.Selection, ";", vbTextCompare) _
> 0) then
strRight = "" ' we found it so move on to the next match
end if
ActiveDocument.Selection.CharLeft
end if
' If we're isolating a brace make sure the matching brace
' isn't on the same line
if (strRight <> "") then
ActiveDocument.Selection.CharLeft
nCurrentLine = ActiveDocument.Selection.CurrentLine
nCurrentColumn = ActiveDocument.Selection.CurrentColumn
ExecuteCommand "GoToMatchBrace"
if ActiveDocument.Selection.CurrentLine = nCurrentLine then
ActiveDocument.Selection.MoveTo _
nCurrentLine, nCurrentColumn + 1
strRight = "" ' we found it so move on to the next match
else
ActiveDocument.Selection.MoveTo _
nCurrentLine, nCurrentColumn
ActiveDocument.Selection.FindText _
str & ".+$", dsMatchRegExp
end if
end if
end if
if (strRight <> "") and _
(Left(strRight, 1) <> "/") and _
(strRight <> ",") and _
(strRight <> ";") and _
(strRight <> "\") then
ActiveDocument.Selection.CharLeft
MoveByColumns nLen
ActiveDocument.Selection.NewLine
end if
end if
wend
end sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -