home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / MS_DEV / VID / CLIENT / SHAREIDE / MACROS / SAMPLE.DSM < prev   
Text File  |  1996-10-28  |  21KB  |  612 lines

  1. '------------------------------------------------------------------------------
  2. 'FILE DESCRIPTION: SAMPLE.DSM is a collection of sample editor macros
  3. '------------------------------------------------------------------------------
  4.  
  5. 'This routine has many uses if you are trying to determine the type of source 
  6. '    file
  7. 'Return value:  0 Unknown file type
  8. '               1 C related file, this includes .c, .cpp, .cxx, .h, .hpp, .hxx
  9. '               2 Java related file, this includes .jav, .java
  10. '               3 ODL style, .odl, .idl
  11. '               4 Resource file, .rc, .rc2
  12. '               5 HTML style file, this includes .html, And .htm
  13. '               6 VBS style, .dsm
  14. '               7 Def style, .def
  15. 'USE: pass this function the document that you wish to get information for
  16. Function FileType (ByVal doc)
  17.     ext = doc.Name
  18.     FileType = 0
  19.     pos = Instr(ext, ".")
  20.     if pos > 0 then
  21.         Do While pos <> 1
  22.             ext = Mid(ext, pos, Len(ext) - pos + 1)
  23.             pos = Instr(ext, ".")
  24.         Loop
  25.         ext = LCase(ext)
  26.     end if
  27.     If ext = ".rc" Or ext = ".rc2" Then
  28.         FileType = 4
  29.     ElseIf doc.Language = dsCPP Then
  30.         FileType = 1
  31.     ElseIf doc.Language = dsJava Then
  32.         FileType = 2
  33.     ElseIf doc.Language = dsIDL Then
  34.         FileType = 3
  35.     ElseIf doc.Language = dsHTML_IE3 Or doc.Language = dsHTML_IE2 Or _
  36.             doc.Language = dsHTML_RFC1866 Then
  37.         FileType = 5  
  38.     ElseIf doc.Language = dsVBSMacro Then ' 
  39.         FileType = 6  
  40.     ElseIf ext = ".def" Then
  41.         FileType = 7
  42.     Else 
  43.         FileType = 0
  44.     End If 
  45. End Function
  46.  
  47.  
  48. 'This routine has many uses if you are trying to determine if an identifier 
  49. '  is a valid C identifier.
  50. '  These identifiers do not include qualification syntax, for example:
  51. '  foo.bar is not valid
  52. '  foo i valid
  53. 'Parameters:    String to test if it is a valid C identifier
  54. 'Return value:  True: passed parameter is a valid C identifier
  55. '               False: passed parameter is not a valid C identifier
  56. Function ValidId(Id)
  57.     ValidId = True
  58.     For i = 1 To Len(Id)
  59.       if Mid(Id, i, 1) < "a" Or Mid(Id, i, 1) > "z" Then
  60.         if Mid(Id, i, 1) < "A" Or Mid(Id, i, 1) > "Z" Then
  61.             if Mid(Id, i, 1) < "0" Or Mid(Id, i, 1) > "9" Then
  62.                 if Mid(Id, i, 1) <> "_" Then
  63.                     ValidId = False
  64.                 End if
  65.             End If
  66.         End If    
  67.       End If
  68.     Next
  69.     If IsNumeric(Left(Id, 1)) = True Then
  70.         ValidId = False
  71.     End If
  72. End Function
  73.  
  74.  
  75. Dim ParamArr ()  ' Dynamic array to store function arguments.
  76.  
  77. Sub AddFunctionDescription ( )
  78. 'DESCRIPTION: Creates a comment block for the currently selected C/C++ function prototype
  79.  
  80.     'Throughout this file, ActiveDocument.Selection is used in place of
  81.     'ActiveDocument.Selection.Text  The two are equivalent, And can be used
  82.     'interchangeably. The reason for the equivalence is because Text is
  83.     'regarded as the default property to use. All use of 
  84.     'ActiveDocument.Selection without any other property defaults to the Text
  85.     ' property.
  86.     if ActiveDocument.Language = dsCPP Then
  87.         Header = StripTabs(Trim(ActiveDocument.Selection))
  88.  
  89.         'Get the function return type.
  90.         if Header <> "" then
  91.             Reti = InStr(Header, " ")
  92.             Loc = InStr(Header, "(")
  93.             if Reti < Loc Then
  94.               RetTp = Left(Header, Reti)
  95.               Header = Right(Header, Len(Header) - Reti)
  96.             End If
  97.  
  98.             'Get the function name.
  99.             Loc = InStr(Header, "(") - 1
  100.             fcName = Left(Header, Loc)
  101.             Header = Right(Header, Len(Header) - Len(fcName))
  102.  
  103.             'Do we have storage type on the return type?
  104.             Trim (fcName)
  105.             If InStr(fcName," ") <> 0 Then
  106.                 retTp = retTp + Left(fcName,InStr (fcName," "))
  107.                 fcName = Right(fcName, Len(fcName) - InStr(fcName," "))
  108.             End If
  109.  
  110.             'Get the function parameters.
  111.             iPrm = 0
  112.             iPrmA = 0
  113.             prms = Header 
  114.  
  115.             'Count the number of parameters. 
  116.             Do While InStr(prms, ",") <> 0
  117.                 iPrm = iPrm + 1
  118.                 prms = Right(prms, Len(prms) - InStr(prms, ",")) 
  119.             Loop 
  120.             
  121.             'Store the parameter list in the array.
  122.             If iPrm > 0 Then  ' If multiple params
  123.                 iPrm = iPrm + 1
  124.                 iPrmA = iPrm
  125.                 Redim ParamArr(iPrm)
  126.                 Do While InStr(header, ",") <> 0
  127.                     ParamArr(iPrm) = Left(Header, InStr (Header, ",") - 1)
  128.                     'Remove brace from first parameter.
  129.                     If InStr(ParamArr(iPrm), " (") <> 0 Then
  130.                         ParamArr(iPrm) = Right(ParamArr(iPrm), _
  131.                                 Len(ParamArr(iPrm)) - InStr(ParamArr(iPrm), " ("))
  132.                         Trim(ParamArr(iPrm))
  133.                     End If
  134.                     Header = Right(Header, Len(Header) - InStr(Header,","))
  135.                     iPrm = iPrm - 1 
  136.                     Loop 
  137.                 ParamArr(iPrm) = Header 
  138.                 'Remove trailing brace from last parameter.
  139.                 If InStr(ParamArr(iPrm), ")") <> 0 Then
  140.                     ParamArr(iPrm) = Left(ParamArr(iPrm), InStr(ParamArr(iPrm), ")") - 1)
  141.                     Trim(ParamArr(iPrm))
  142.                 End If
  143.             Else 'Maybe one param.
  144.                 Redim ParamArr(1)
  145.                 Header = Right(Header, Len(Header) - 1) ' strip the first brace.
  146.                 Trim(Header)
  147.                 ParamArr(1) = StripTabs(Header)
  148.                 If InStr(ParamArr(1), ")") <> 1 Then
  149.                     ParamArr(1) = Left(ParamArr(1), InStr(ParamArr(1), ")") - 1)
  150.                     Trim(ParamArr(1))
  151.                     iPrmA = 1
  152.                 End If
  153.             End If
  154.  
  155.             'Position the cursor one line above the selected text.
  156.             ActiveDocument.Selection
  157.             ActiveDocument.Selection.LineUp
  158.             ActiveDocument.Selection.LineDown
  159.             ActiveDocument.Selection.StartOfLine
  160.             ActiveDocument.Selection = vbLf
  161.  
  162.             Descr = "// Function name    : " + fcName + _
  163.                     vbLf + "// Description        : " + _ 
  164.                     vbLf +  "// Return type        : " + RetTp + vbLf
  165.             
  166.             'Print the parameter list. 
  167.             Do While iPrmA <> 0
  168.                 'Remove a line feed from any of the arguments.
  169.                 If InStr(ParamArr(iPrmA), vbLf) <> 0 Then
  170.                     ParamArr(iPrmA) = Right(ParamArr(iPrmA), (Len(ParamArr(iPrmA)) - InStr(ParamArr(iPrmA), vbLf)))
  171.                     Trim(ParamArr(iPrmA))
  172.                 End If
  173.                 ParamArr(iPrmA) = StripTabs(ParamArr(iPrmA))
  174.                 Descr = Descr + "// Argument         : " + _
  175.                         ParamArr(iPrmA) + vbLf
  176.                 iPrmA = iPrmA - 1
  177.             Loop
  178.             ActiveDocument.Selection = Descr
  179.         End If
  180.     Else
  181.         MsgBox("You need to have an active C/C++ document open"+vbLF+"with the function prototype selected")
  182.     End If
  183. End Sub
  184.  
  185. 'Strips the leading tab spaces. 
  186. Function StripTabs (ByVal MyStr)
  187.     Do While InStr(MyStr, vbTab) <> 0
  188.         MyStr = Right(MyStr, Len(MyStr) - InStr(MyStr, vbTab)) 
  189.     Loop 
  190.     StripTabs = Trim(MyStr)
  191. End Function
  192.  
  193. Sub ToggleCommentStyle ( )
  194. 'DESCRIPTION: Toggles between comment styles: /* And //
  195.  
  196.     TmpBlock = ""
  197.     CmtBlock = ActiveDocument.Selection
  198.     TypeOfFile = FileType(ActiveDocument)
  199.     If TypeOfFile > 0 And TypeOfFile < 5 Then   'C/C++ style comment
  200.         'Get the first two characters of the comment block.
  201.         Trim(CmtBlock)
  202.         If Instr(CmtBlock,"//") <> 0 Then 
  203.             Do While Instr (CmtBlock,"//") <> 0
  204.                 TmpBlock = TmpBlock + Left (CmtBlock, Instr (CmtBlock,"//") -_
  205.                     1)
  206.                 CmtBlock = Right(CmtBlock, (Len(CmtBlock) - (Instr(CmtBlock,_
  207.                     "//") + 1)))
  208.             Loop
  209.                 CmtBlock = "/*" + TmpBlock + CmtBlock + "*/"
  210.         ElseIf Instr(CmtBlock, "/*") <> 0 Then 
  211.             CmtBlock = Right(CmtBlock, Len(CmtBlock) - (Instr(CmtBlock,"/*")_
  212.                 + 1))
  213.             Do While Instr (CmtBlock, vbLf) <> 0
  214.                 TmpBlock = TmpBlock + Left (CmtBlock, Instr(CmtBlock, vbLf))_
  215.                     + "//"
  216.                 CmtBlock = Right(CmtBlock, (Len(CmtBlock) - (Instr(CmtBlock,_
  217.                     vbLf))))
  218.             Loop
  219.             CmtBlock = Trim(CmtBlock)
  220.             CmtBlock = "//" + TmpBlock + CmtBlock 
  221.             CmtBlock = Left(CmtBlock, Instr(CmtBlock,"*/")-1)
  222.         End If
  223.     End If
  224.     ActiveDocument.Selection.Delete
  225.     ActiveDocument.Selection = CmtBlock
  226. End Sub
  227.  
  228.  
  229.  
  230. Sub AddRevisionMarks ( )
  231. 'DESCRIPTION: Adds comments to a file describing changes made
  232.  
  233.     'This routine adds a new comment block to the top of a file, where the 
  234.     ' programmer can place revision marks to describe changes made to the file
  235.     'The rules this routine uses are as follows:
  236.     ' 1) Start at the top of the file
  237.     ' 2) Scan through each line, if the current line starts with a comment, 
  238.     '      advance to the next line
  239.     ' 3) If we are currently in a group comment block, keep advancing until
  240.     '     the end of the block is found
  241.     ' 4) If we are in a line item comment (eg: //, ', rem, etc) keep advancing 
  242.     '     until a line that does not start with a comment is found. 
  243.     '     By 'start with a comment', it is meant a line, where after 
  244.     '     stripping off spaces and tabs from the beginning, the first set of
  245.     '     characters is not a comment delimiter.
  246.     ' 5) Insert a blank line, this allows the next invocation of this macro
  247.     '     to place the newer revision mark before all others.
  248.     ' 6) Insert the revision block
  249.  
  250.     'Change this to the programmer's name for a default
  251.     DefaultUserName = "..."
  252.  
  253.     'Because the user may not have closed a comment (eg a /* without a */) we
  254.     ' try to protect ourselves from an infinite loop...
  255.     BreakAfter = 10 'Max number of lines to look at scan before aborting
  256.     CurrentCount = 1
  257.  
  258.     BeginComment = "" 'The token for the specified language for the beginning
  259.                         ' of a comment
  260.     EndComment = ""   'Same, except for the end of a comment
  261.     EveryLine = ""    'Does the comment mark need to be placed on every line
  262.                         ' (VBS, DEF)
  263.  
  264.     'First, make sure the active document is a text window
  265.     ' (Not really necessary, but good practice)
  266.     If ActiveDocument.Type = "Text" Then
  267.         TypeOfFile = FileType(ActiveDocument)
  268.     
  269.         'Set ourselves at the very top of the document.
  270.         'This also clears any selection made.
  271.         ActiveDocument.Selection.StartOfDocument
  272.         ActiveDocument.Selection.SelectLine
  273.         CurrText = ActiveDocument.Selection
  274.         CurrText = LTrim(CurrText)
  275.     
  276.         'All of the following do relatively the same exact thing, 
  277.         ' except they look for different comment types
  278.         If TypeOfFile > 0 And TypeOfFile < 5 Then       'C/C++ family of code
  279.             ContSearch = True
  280.             BeginComment = "/*"
  281.             EndComment = "*/"
  282.             EveryLine = " "
  283.  
  284.             'In C/C++ style code, we need to look for a //, 
  285.             '  if not found, then look for a /*
  286.             Do
  287.                 ActiveDocument.Selection = CurrText
  288.                 If InStr(CurrText, "//") = 1 Then   'is a "//" available?
  289.                     ActiveDocument.Selection.SelectLine
  290.                     CurrText = LTrim(ActiveDocument.Selection) 'remove whitespace
  291.                     ContSearch = False   ' looking at // style comments, 
  292.                                             'don't look for a /* style
  293.                 Else
  294.                     Exit Do
  295.                 End If
  296.             Loop
  297.  
  298.             If ContSearch = False Then
  299.                 ActiveDocument.Selection.LineUp
  300.             End If
  301.  
  302.             'When the method ActiveDocument.Selection.SelectLine is called, 
  303.             '  it is the same as when you click the mouse in the margin, it 
  304.             '  selects the whole line, including the carriage return.
  305.             '  Because of this, the cursor comes down to the next line, which
  306.             '  can really confuse us. So in a number of places, you will see 
  307.             '  a grouping of LineUp/LineDown commands. By executing these
  308.             '  commands, the cursor is moved down, which clears the current
  309.             '  selection (including getting us past the carriage return), 
  310.             '  then moves us back up, thus putting us on the same line, but 
  311.             '  without the danger of skipping a line (which is what will 
  312.             '  happen without the LineUp/LineDown combination)
  313.             If ContSearch = True Then
  314.                 ActiveDocument.Selection.StartOfDocument
  315.                 'Prime the loop with the first line
  316.                 ActiveDocument.Selection.SelectLine
  317.                 CurrText = ActiveDocument.Selection           
  318.                 ActiveDocument.Selection.LineDown
  319.                 ActiveDocument.Selection.LineUp
  320.                 'Remove leading whitespace
  321.                 CurrText = LTrim(CurrText)      
  322.                 'Does line start with a /*?              
  323.                 If InStr(CurrText,"/*") = 1 Then
  324.                     while (InStr(CurrText, "*/") = 0) And _
  325.                           (BreakAfter > CurrentCount)
  326.                         ActiveDocument.Selection.SelectLine
  327.                         CurrText = ActiveDocument.Selection
  328.                         CurrText = LTrim(CurrText)                              
  329.                         ActiveDocument.Selection.LineDown
  330.                         ActiveDocument.Selection.LineUp    
  331.                         CurrentCount = CurrentCount + 1
  332.                     wend
  333.                     If (BreakAfter > CurrentCount) Then
  334.                         'Exit the loop because the search has gone on for an 
  335.                         '  unreasonable number of lines.
  336.                         MsgBox "Could not find a closing comment mark"
  337.                     End If
  338.                 End If
  339.             End If
  340.  
  341.         'The code for these are really just a copy of that from the 
  342.         '  C/C++ section...
  343.           
  344.         ElseIf TypeOfFile = 5 Then                      'HTML code
  345.             BeginComment = "<!--"
  346.             EndComment = "-->"
  347.             EveryLine = "    "
  348.             If InStr(CurrText,"<!--") = 1 Then
  349.                 If InStr(CurrText,"-->") <> 0 Then
  350.                     ActiveDocument.Selection.LineDown
  351.                 Else
  352.                     Do
  353.                         ActiveDocument.Selection.SelectLine
  354.                         CurrText = ActiveDocument.Selection
  355.                         CurrText = Left(CurrText, Len(CurrText) - 2)
  356.                         ActiveDocument.Selection = CurrText + vbLf
  357.                         If InStr(CurrText, "-->") Then
  358.                             Exit Do
  359.                         End If
  360.                     Loop
  361.                 End If
  362.             End If 
  363.  
  364.         ElseIf TypeOfFile = 6 Then                      'VBS code
  365.             BeginComment = "'"
  366.             EndComment = "'"
  367.             EveryLine = "'"
  368.             Do
  369.                 ActiveDocument.Selection = CurrText
  370.                 If InStr(CurrText, "'") = 1 Or _
  371.                    InStr(LCase(CurrText), "Rem") = 1 Then
  372.                     ActiveDocument.Selection.SelectLine
  373.                     CurrText = LTrim(ActiveDocument.Selection)
  374.                     ContSearch = False
  375.                 Else
  376.                     Exit Do
  377.                 End If
  378.             Loop
  379.             If ContSearch = False Then
  380.                 ActiveDocument.Selection.LineUp
  381.             End If
  382.  
  383.         ElseIf TypeOfFile = 7 Then                      'DEF code
  384.             BeginComment = ";"
  385.             EndComment = ""
  386.             EveryLine = ";"
  387.             Do
  388.                 ActiveDocument.Selection = CurrText
  389.                 If InStr(CurrText, ";") = 1 Then
  390.                     ActiveDocument.Selection.SelectLine
  391.                     CurrText = LTrim(ActiveDocument.Selection)
  392.                     ContSearch = False
  393.                 Else
  394.                     Exit Do
  395.                 End If
  396.             Loop
  397.             If ContSearch = False Then
  398.                 ActiveDocument.Selection.LineUp
  399.             End If      
  400.         End If
  401.  
  402.         If TypeOfFile = 0 Then                          'Unknown type of code
  403.             MsgBox("Unable to add revision marks. Unrecgonized file type")
  404.         ElseIf (CurrentCount < BreakAfter) Then
  405.             'The BeginComment, EveryLine, EndComment were set as to 
  406.             ' avoid duplicating this section...
  407.             ' just insert the generalized block, with the comment markers.
  408.             ActiveDocument.Selection.StartOfLine(True)
  409.             'This is added with one assignment statement, that way the user
  410.             ' can hit undo once, and the whole thing is removed
  411.             ActiveDocument.Selection = vbLf + _
  412.             BeginComment + "***********************************" + vbLf + _
  413.             EveryLine    + " REVISION LOG ENTRY" + vbLf + _
  414.             EveryLine    + " Revision By: " + DefaultUserName + vbLf + _
  415.             EveryLine    + " Revised on " + CStr(Now) + vbLf + _
  416.             EveryLine    + " Comments: ..." + vbLf + _
  417.             EveryLine    + "***********************************" + _
  418.             EndComment + vbLf + vbLf
  419.         End If
  420.     End If
  421. End Sub
  422.  
  423.  
  424. Sub CloseExceptActive () 
  425. 'DESCRIPTION: Closes all editor windows except the current one
  426.  
  427.     'Windows.Item(1) is always the currently active window. So to close all
  428.     ' the windows except the active one, keep looping until there is no 
  429.     ' longer a Windows.Item(2)
  430.     do while Windows.Count > 1
  431.         Windows.Item(2).Close(dsSaveChangesPrompt)
  432.     Loop
  433. End Sub
  434.  
  435.  
  436. Sub CommentOut ()
  437. 'DESCRIPTION: Comments out a selected block of text
  438.  
  439.     TypeOfFile = FileType(ActiveDocument)  
  440.     If TypeOfFile > 0 And TypeOfFile < 5 Then    'C & Java uses the same 
  441.                                                     'style of comments.
  442.         ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
  443.     ElseIf TypeOfFile = 5 Then
  444.         ActiveDocument.Selection = "<!-- " + ActiveDocument.Selection + " -->"
  445.     ElseIf TypeOfFile = 6 Or TypeOfFile = 7 Then
  446.         'This is a complicated one. There is no group comment like there is 
  447.         'in the other file types, so we need to iterate through each line, 
  448.         'And prepend a ' to it. Also, because VBS/DEF does not have a 'end 
  449.         'the comment at this particular column' delimiter, entire lines of 
  450.         'code must be commented out, not sections
  451.         If TypeOfFile = 6 Then 
  452.             CommentType = " ' "
  453.         Else
  454.             CommentType = " ; "
  455.         End If
  456.      
  457.         StartLine = ActiveDocument.Selection.TopLine
  458.         EndLine = ActiveDocument.Selection.BottomLine
  459.         If EndLine < StartLine Then
  460.             Temp = StartLine
  461.             StartLine = EndLine
  462.             EndLine = Temp
  463.         End If
  464.  
  465.         If EndLine = StartLine Then
  466.             ActiveDocument.Selection = CommentType + ActiveDocument.Selection
  467.  
  468.         Else 
  469.             For i = StartLine To EndLine
  470.                 ActiveDocument.Selection.GoToLine i
  471.                 ActiveDocument.Selection.SelectLine
  472.                 ActiveDocument.Selection = CommentType + _
  473.                     ActiveDocument.Selection
  474.             Next
  475.         End If
  476.     Else
  477.         MsgBox("Unable to comment out the highlighted text" + vbLf + _
  478.                 "because the file type was unrecognized." + vbLf + _
  479.                 "If the file has not yet been saved, " + vbLf + _
  480.                 "please save it and try again.")
  481.     End If
  482. End Sub
  483.  
  484.  
  485. Sub MultiplePaste () 
  486. 'DESCRIPTION: Performs a paste of what is on the clipboard a multiple number of times
  487.  
  488.     NumPastes = InputBox("Number of pastes to make", "Multiple Paste Macro",_
  489.                          "1")
  490.     For i = 1 To CInt(NumPastes)
  491.         ActiveDocument.Selection.Paste
  492.         'Because the selection remains active, the following two lines
  493.         'clear the selection, while keeping the cursor in the same place
  494.         ActiveDocument.Selection.LineUp
  495.         ActiveDocument.Selection.LineDown
  496.         ActiveDocument.Selection = vbLf    
  497.     Next
  498. End Sub
  499.  
  500.  
  501. Sub PrintAllOpenDocuments ()
  502. 'DESCRIPTION: Prints all open active documents
  503.  
  504.     '  For each m_doc in Documents
  505.     for i = 1 to Documents.Count
  506.         Documents.Item(i).PrintOut
  507.     next
  508. End Sub
  509.  
  510.  
  511. Sub PoundDefOut (ifndef)
  512.     If ifndef = true Then
  513.         PoundType = "#ifndef "
  514.     Else
  515.         PoundType = "#ifdef "
  516.     End If
  517.     
  518.     If FileType(ActiveDocument) <> 1 Then
  519.         MsgBox ("This macro only works on" + vbLf + _
  520.                 ".c, .cpp, .cxx, .h, .hpp, or .hxx files")
  521.     Else
  522.         ControlVarName = InputBox("What should the control variable be?" + _
  523.             vbLf + vbLf + "Example: #ifdef ControlVariable", PoundType + _
  524.             " out a section of code")
  525.         OK = True
  526.         If ValidId (ControlVarName) = False Then
  527.             Ok = False
  528.             MsgBox(ControlVarName + " is not a valid C identifier." + _
  529.                 vbLf + "please re-run the macro with a valid C identifier")
  530.         End If
  531.         
  532.         
  533.         Sel = ActiveDocument.Selection
  534.         For i = 1 To Len(Sel) - 1
  535.             If Mid(Sel, i, 1) = vbLf Then
  536.                 Sel = Left(Sel,i) + vbTab + Right(Sel, Len(Sel)-i)
  537.             End If
  538.         Next
  539.         If ControlVarName <> "" And Ok = True Then
  540.             Sel = vbLf + PoundType + ControlVarName + vbLf + vbTab + Sel + _
  541.                 vbLf+ "#endif //" + ControlVarName
  542.             If Right(Sel,1) <> vbLf Then
  543.                 Sel = Sel + vbLf
  544.             End If
  545.             ActiveDocument.Selection = Sel
  546.         End If
  547.     End If
  548. End Sub
  549.  
  550. 'The next two macros are exactly the same, except one uses ifnde and the
  551. '  other ifdef, so we just recycle the same code, just use a different 
  552. '  preprocessor directive
  553. Sub ifdefOut ()
  554. 'DESCRIPTION: #ifdef / #endif out a section of code
  555.  
  556.     PoundDefOut (False)
  557. End Sub
  558.  
  559. Sub ifndefOut ()
  560. 'DESCRIPTION: #ifndef / #endif out a section of code
  561.  
  562.     PoundDefOut (True)
  563. End Sub
  564.  
  565. 'Allows the user to make sure the current header file is included only once. 
  566. ' There are two ways to do this, using the #pragma once directive or 
  567. ' surrounding the entire file in a #ifndef/#endif structure. The first way
  568. ' is much cleaner, but it is VC++ specific, and therefore not portable. If 
  569. ' you plan on compiling your code with other compilers, use the 
  570. ' #ifndef/#endif method, otherwise, the #pragma once option is the best.
  571. Sub OneTimeInclude ()
  572. 'DESCRIPTION: Adds code to the current header file so it is included only once per c/cpp file.
  573.  
  574.     ext = ActiveDocument.Name
  575.     DocName = UCase(ActiveDocument.Name)
  576.     pos = Instr(ext, ".")
  577.     Do While pos <> 1
  578.         ext = Mid(ext, pos, Len(ext) - pos + 1)
  579.         pos = Instr(ext, ".")
  580.     Loop
  581.     ext = LCase(ext)
  582.     pos = Instr(DocName, ".")
  583.     If ext = ".h" Or ext = ".hpp" Then
  584.         'Warn user that this will not work with a compiler other than VC++
  585.         If MsgBox("This macro uses the Visual C++ dependant #pragma once" + _
  586.                 vbLf + "Is the source to be portable across compilers?", 4) _
  587.                 = 6 Then
  588.             ActiveDocument.Selection.StartOfDocument (False)
  589.             Examp = "__" + Left(DocName, pos - 1) + "_" + _
  590.                 UCase(Right(ext, len(ext) - 1)) + "__"
  591.             ControlVarName = InputBox("What should the control variable be?" _
  592.                 + vbLf + vbLf + "Example: #ifdef " + _
  593.                 Examp, "One time header include protection", Examp)
  594.             If ValidId (ControlVarName) = True Then
  595.                 ActiveDocument.Selection = "#ifndef " + ControlVarName + _
  596.                     vbLf + "#define " + ControlVarName + vbLf
  597.                 ActiveDocument.Selection.EndOfDocument(False)
  598.                 ActiveDocument.Selection = vbLf + "#endif //" + _ 
  599.                     ControlVarName
  600.             Else
  601.                 MsgBox(ControlVarName + " is not a valid c identifier." + _
  602.                 vbLf + "please re-run the macro with a valid C identifier")
  603.             End If
  604.         Else
  605.             ActiveDocument.Selection.StartOfDocument(False)
  606.             ActiveDocument.Selection = "#pragma once" + vbLf + vbLf
  607.         End If
  608.     Else
  609.         MsgBox("This macro can only be run on .h or .hpp files")
  610.     End If
  611. End Sub
  612.