📄 sample.dsm
字号:
End If
End If
ElseIf TypeOfFile = 6 Then 'VBS code
BeginComment = "'"
EndComment = "'"
EveryLine = "'"
Do
ActiveDocument.Selection = CurrText
If InStr(CurrText, "'") = 1 Or _
InStr(LCase(CurrText), "Rem") = 1 Then
ActiveDocument.Selection.SelectLine
CurrText = LTrim(ActiveDocument.Selection)
ContSearch = False
Else
Exit Do
End If
Loop
If ContSearch = False Then
ActiveDocument.Selection.LineUp
End If
ElseIf TypeOfFile = 7 Then 'DEF code
BeginComment = ";"
EndComment = ""
EveryLine = ";"
Do
ActiveDocument.Selection = CurrText
If InStr(CurrText, ";") = 1 Then
ActiveDocument.Selection.SelectLine
CurrText = LTrim(ActiveDocument.Selection)
ContSearch = False
Else
Exit Do
End If
Loop
If ContSearch = False Then
ActiveDocument.Selection.LineUp
End If
End If
If TypeOfFile = 0 Then 'Unknown type of code.
MsgBox("Unable to add revision marks. Unrecgonized file type")
ElseIf (CurrentCount < BreakAfter) Then
'The BeginComment, EveryLine, and EndComment were set as
' avoid duplicating this section...
' just insert the generalized block, with the comment markers.
ActiveDocument.Selection.StartOfLine(True)
'This is added with one assignment statement, which enables the user
' to hit undo once, and remove the entire change.
ActiveDocument.Selection = vbLf + _
BeginComment + "***********************************" + vbLf + _
EveryLine + " REVISION LOG ENTRY" + vbLf + _
EveryLine + " Revision By: " + DefaultUserName + vbLf + _
EveryLine + " Revised on " + CStr(Now) + vbLf + _
EveryLine + " Comments: ..." + vbLf + _
EveryLine + "***********************************" + _
EndComment + vbLf + vbLf
End If
End If
End Sub
Sub CloseExceptActive ()
'DESCRIPTION: Closes all editor windows except the current one.
'Windows.Item(1) is always the currently active window. So to close all
' the windows except the active one, keep looping until there is no
' longer a Windows.Item(2).
do while Windows.Count > 1
Windows.Item(2).Close(dsSaveChangesPrompt)
Loop
End Sub
Sub CommentOut ()
'DESCRIPTION: Comments out a selected block of text.
Dim win
set win = ActiveWindow
if win.type <> "Text" Then
MsgBox "This macro can only be run when a text editor window is active."
else
TypeOfFile = FileType(ActiveDocument)
If TypeOfFile > 0 And TypeOfFile < 5 Then 'C & Java use the same
'style of comments.
ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
ElseIf TypeOfFile = 5 Then
ActiveDocument.Selection = "<!-- " + ActiveDocument.Selection + " -->"
ElseIf TypeOfFile = 6 Or TypeOfFile = 7 Then
'There is no group comment like there is in the other file types,
'so we need to iterate through each line, and prepend a ' to the line.
'Also, because VBS/DEF does not have a 'end the comment at this
'particular column' delimiter, entire lines of code must be
'commented out, not sections.
If TypeOfFile = 6 Then
CommentType = " ' "
Else
CommentType = " ; "
End If
StartLine = ActiveDocument.Selection.TopLine
EndLine = ActiveDocument.Selection.BottomLine
If EndLine < StartLine Then
Temp = StartLine
StartLine = EndLine
EndLine = Temp
End If
If EndLine = StartLine Then
ActiveDocument.Selection = CommentType + ActiveDocument.Selection
Else
For i = StartLine To EndLine
ActiveDocument.Selection.GoToLine i
ActiveDocument.Selection.SelectLine
ActiveDocument.Selection = CommentType + _
ActiveDocument.Selection
Next
End If
Else
MsgBox("Unable to comment out the highlighted text" + vbLf + _
"because the file type was unrecognized." + vbLf + _
"If the file has not yet been saved, " + vbLf + _
"please save it and try again.")
End If
End If
End Sub
Sub MultiplePaste ()
'DESCRIPTION: Performs a paste of what is on the clipboard a multiple number of times.
NumPastes = InputBox("Number of pastes to make", "Multiple Paste Macro",_
"1")
For i = 1 To CInt(NumPastes)
ActiveDocument.Selection.Paste
'Because the selection remains active, the following two lines
'clear the selection, while keeping the cursor in the same place.
ActiveDocument.Selection.LineUp
ActiveDocument.Selection.LineDown
ActiveDocument.Selection = vbLf
Next
End Sub
Sub PrintAllOpenDocuments ()
'DESCRIPTION: Prints all open, active documents.
'Small, quick macro, but it can be usefull.
for each doc in Application.Documents
Doc.PrintOut
next
End Sub
Sub PoundDefOut (ifndef)
If ifndef = true Then
PoundType = "#ifndef "
Else
PoundType = "#ifdef "
End If
If FileType(ActiveDocument) <> 1 Then
MsgBox ("This macro only works on" + vbLf + _
".c, .cpp, .cxx, .h, .hpp, or .hxx files")
Else
ControlVarName = InputBox("What should the control variable be?" + _
vbLf + vbLf + "Example: #ifdef ControlVariable", PoundType + _
" out a section of code")
OK = True
If ValidId (ControlVarName) = False Then
Ok = False
MsgBox("""" + ControlVarName + """" + _
" is not a valid C identifier." + _
vbLf + "please re-run the macro with a valid C identifier")
End If
Sel = ActiveDocument.Selection
For i = 1 To Len(Sel) - 1
If Mid(Sel, i, 1) = vbLf Then
Sel = Left(Sel,i) + vbTab + Right(Sel, Len(Sel)-i)
End If
Next
If ControlVarName <> "" And Ok = True Then
Sel = vbLf + PoundType + ControlVarName + vbLf + vbTab + Sel + _
vbLf+ "#endif //" + ControlVarName
If Right(Sel,1) <> vbLf Then
Sel = Sel + vbLf
End If
ActiveDocument.Selection = Sel
End If
End If
End Sub
'The next two macros are exactly the same, except one uses ifndef and the
' other ifdef. We recycle the same code and just use a different
' preprocessor directive.
Sub ifdefOut ()
'DESCRIPTION: #ifdef / #endif out a section of code.
PoundDefOut (False)
End Sub
Sub ifndefOut ()
'DESCRIPTION: #ifndef / #endif out a section of code.
PoundDefOut (True)
End Sub
'Allows the user to make sure the current header file is included only once.
' There are two ways to do this, using the #pragma once directive or
' surrounding the entire file in a #ifndef/#endif structure. The first way
' is much cleaner, but it is VC++ specific, and therefore not portable. If
' you plan on compiling your code with other compilers, use the
' #ifndef/#endif method, otherwise, the #pragma once option is preferrable.
Sub OneTimeInclude ()
'DESCRIPTION: Adds code to the current header file so it is included only once per c/cpp file.
ext = ActiveDocument.Name
If ext = "" Then
If MsgBox("The file you are working with does not have a file extension." + _
vbLF + "Are you sure this is a C/C++ header file?", 4) = vbCancel Then
Exit Sub
End If
ext = "nofilenamegiven.h"
End If
DocName = UCase(ext)
pos = Instr(ext, ".")
Do While pos <> 1
ext = Mid(ext, pos, (Len(ext) - pos + 1))
pos = Instr(ext, ".")
Loop
ext = LCase(ext)
pos = Instr(DocName, ".")
If ext = ".h" Or ext = ".hpp" Then
'Warn user that this will not work with a compiler other than VC++.
If MsgBox("This macro uses the Visual C++ dependant #pragma once" + _
vbLf + "Is the source to be portable across compilers?", 4) _
= 6 Then
ActiveDocument.Selection.StartOfDocument (False)
Examp = "__" + Left(DocName, pos - 1) + "_" + _
UCase(Right(ext, len(ext) - 1)) + "__"
ControlVarName = InputBox("What should the control variable be?" _
+ vbLf + vbLf + "Example: #ifdef " + _
Examp, "One time header include protection", Examp)
If ValidId (ControlVarName) = True Then
ActiveDocument.Selection = "#ifndef " + ControlVarName + _
vbLf + "#define " + ControlVarName + vbLf
ActiveDocument.Selection.EndOfDocument(False)
ActiveDocument.Selection = vbLf + "#endif //" + _
ControlVarName
Else
MsgBox(ControlVarName + " is not a valid c identifier." + _
vbLf + "please re-run the macro with a valid C identifier")
End If
Else
ActiveDocument.Selection.StartOfDocument(False)
ActiveDocument.Selection = "#pragma once" + vbLf + vbLf
End If
Else
MsgBox("This macro can only be run on .h or .hpp files")
End If
End Sub
'Auto completion macro
Dim previousSelection
Dim completionWords
Dim completionWordsIndex
Sub AddToCompletionWords (word)
' If the word is already there, abort
if InStr(1, completionWords, " " & word & " ", 1) <> 0 Then
Exit Sub
End If
completionWords = completionWords & word & " "
End Sub
Function ExtractNextCompletionWord()
ExtractNextCompletionWord = ""
' If no words yet, go away
if Len(completionWords) <= 1 Then
Exit Function
End If
' Wrap to beginning if necessary
if completionWordsIndex > Len(completionWords) Then
completionWordsIndex = 2
End If
' Find next <space>
Dim newIndex
newIndex = InStr (completionWordsIndex, completionWords, " ", 0)
if newIndex = 0 Then
Exit Function
End If
ExtractNextCompletionWord = Mid(completionWords, completionWordsIndex, _
newIndex-completionWordsIndex)
completionWordsIndex = newIndex+1 'Skip over <space>
End Function
Sub FillCompletionWords (word)
' Find all words in this file which match word, and
' add them, space separated, into completionWords
previousSelection = word
completionWords = " "
completionWordsIndex = 2
dim sel
set sel = ActiveDocument.Selection
Dim searchString
searchString = "\{^\![^a-zA-Z0-9]\}" & word
Dim firstTime
firstTime = True
Dim firstLine, firstCol
Do while sel.FindText (searchString, dsMatchBackward + dsMatchRegExp)
if firstTime Then
firstLine = sel.TopLine
firstCol = sel.CurrentColumn
firstTime = False
ElseIf firstLine = sel.TopLine And firstCol = sel.CurrentColumn Then
Exit Do ' Jump out of loop before repeat
End If
sel.WordRight
sel.WordLeft dsExtend
AddToCompletionWords Trim(sel.text)
sel.Cancel
Loop
End Sub
Function SuggestNextCompletionWord()
SuggestNextCompletionWord = True
Dim word
word = ExtractNextCompletionWord()
if word <> "" then
ActiveDocument.Selection = word
previousSelection = word
end if
End Function
Sub AutoCompleteFromFile()
'DESCRIPTION: Looks through the active file, searching for the rest of the word that you began to type.
Dim doc
set doc = ActiveDocument
' Be sure active document is a text document
if doc Is Nothing Then
Exit Sub
elseif doc.Type <> "Text" Then
Exit Sub
End If
' Get word to be completed
Dim sel
set sel = doc.Selection
sel.Cancel
dim origLine, origCol
origLine = sel.TopLine
origCol = sel.CurrentColumn
sel.WordLeft dsExtend
'If the cursor is sitting just to the right of a space, an infinite loop
'results. This bit of code protects from that:
if Right(sel, 1) = " " then
sel.CharRight
Exit Sub
end If
if sel <> previousSelection Or completionWords = "" Then
FillCompletionWords sel
sel.MoveTo origLine, origCol
sel.WordLeft dsExtend
End If
SuggestNextCompletionWord
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -