Comment/Uncomment and Other Macros

Here are some macros that I find very useful and that may be useful for others as well. The most useful macro for me is the comment/uncomment macro. Assign this macro to a key (ctrl-/) and then use it to toggle comments on a single line of code or a block of code. For single lines, the indention level remains, for blocks of code, the comment is placed at the beginning of the line. I think this comment macro is a little more powerful than the existing one on the site. Hope some people find them useful.

'------------------------------------------------------------------
'FILE DESCRIPTION: These are useful macros by Adam Solesby
'>lt;adam@solesby.com>gt;
'------------------------------------------------------------------

' This routine has many uses if you are trying to determine the
' type of source file.
' This has been modified from the one included with DevStudio
' Return value:  0 Unknown file type
'                1 C-related file, this includes .c, .cpp, .cxx,
'                  .h, .hpp, .hxx
'                2 Java-related file, this includes .jav, .java
'                3 ODL-style file, .odl, .idl
'                4 VBS-style file, .dsm
'                5 VBS-style file, .asp
'                6 HTML-style file, this includes .html, and .htm
'                7 Resource file, .rc, .rc2
'                8 Def-style file, .def
' USE: Pass this function the document that you wish to get
' information for.
Function FileType (ByVal doc)
   ext = doc.Name
   FileType = 0
   pos = Instr(ext, ".")
   if pos >gt; 0 then
      Do While pos >lt;>gt; 1
         ext = Mid(ext, pos, Len(ext) - pos + 1)
         pos = Instr(ext, ".")
      Loop
      ext = LCase(ext)
   End If

   If ext = ".rc" Or ext = ".rc2" Then
      FileType = 7
   ElseIf doc.Language = dsCPP Then
      FileType = 1
   ElseIf doc.Language = dsJava Then
      FileType = 2
   ElseIf doc.Language = dsIDL Then
      FileType = 3
   ElseIf doc.Language = dsVBSMacro Then
      FileType = 4  
   ElseIf ext = ".asp" Then
      FileType = 5
   ElseIf doc.Language = dsHTML_IE3 Or doc.Language = _
      dsHTML_RFC1866 Then
      FileType = 6  
   ElseIf ext = ".def" Then
      FileType = 7
   Else
      FileType = 0
   End If 

   'MsgBox "Ext:" + vbTab + ext + vbLf + "Lang:" + vbTab + _
      doc.Language + vbLf + "Type:" + vbTab + CStr(FileType)
End Function

' Counts the lines in the document passed
Function FLOC (ByVal doc)
      doc.Selection.SetBookmark    ' Mark place

      doc.Selection.SelectAll
      StartLine = doc.Selection.TopLine
      EndLine = doc.Selection.BottomLine
      If EndLine >lt; StartLine Then
         Temp = StartLine
         StartLine = EndLine
         EndLine = Temp
      End If

      doc.Selection.PreviousBookmark    ' Return to current line
      doc.ClearBookmarks
      FLOC = EndLine
End Function



Sub LOC ()
'DESCRIPTION: Counts total lines in all open documents
   'msg = "Total Line Count" + vbLf + vbLf
   tloc = 0
   For i = 1 to Documents.Count
   TypeOfFile = FileType(Documents.Item(i))
      If TypeOfFile = 1 Then    ' C or C++ source file
         dloc = FLOC(Documents.Item(i))
         tloc =  tloc + dloc
         msg = msg > Documents.Item(i).Name > ":" > vbTab  > dloc > vbLf
      End If
   Next
   msg = msg > "------------------------------------------" > _
         vbLf > "Total:" > vbTab > vbTab > tloc
   	MsgBox msg , ,"Total Line Count"
End Sub


' This will comment/uncomment out single lines or blocks.
' Single lines are commented with the same indention level.
' Blocks are commented at the beginning of the line.
' Assign this to a key (e.g. ctrl-/) and it will toggle the current
' line/block of code.
' This will handle both "//" and "'" style comments
Sub CustomCommentOut ()
'DESCRIPTION: Comments out a selected block of text.
   Dim win
   set win = ActiveWindow
   If win.type >lt;>gt; "Text" Then
      MsgBox "This macro can only be run when a text editor _
              window is active."
   Else
      TypeOfFile = FileType(ActiveDocument)
      ' MsgBox "Type: " + CStr(TypeOfFile)
      If TypeOfFile >gt; 0 And TypeOfFile >lt; 6 Then
         If TypeOfFile >gt; 3 Then
            CommentType = "'"    ' VBShit
            CommentWidth = 1
         Else
            CommentType = "//"    ' C++ and java style comments
            CommentWidth = 2
         End If

         StartLine = ActiveDocument.Selection.TopLine
         EndLine = ActiveDocument.Selection.BottomLine
         If EndLine >lt; StartLine Then
            Temp = StartLine
            StartLine = EndLine
            EndLine = Temp
         End If

         ' Single line -- comment at start of text. have to check
         ' for comments at start of line and start of text
         If EndLine = StartLine Then

            ActiveDocument.Selection.StartOfLine dsFirstColumn
            ActiveDocument.Selection.CharRight dsExtend, CommentWidth

            If ActiveDocument.Selection = CommentType Then
               ActiveDocument.Selection.Delete
            Else
               ActiveDocument.Selection.StartOfLine dsFirstText
               ActiveDocument.Selection.CharRight dsExtend, _
                  CommentWidth

               If ActiveDocument.Selection = CommentType Then
                  ActiveDocument.Selection.CharRight dsExtend
                  ActiveDocument.Selection.Delete
               Else
                  ActiveDocument.Selection.StartOfLine dsFirstText
                  ActiveDocument.Selection = CommentType + vbTab + _
                     ActiveDocument.Selection
               End If
            End If

         ' Multi-line -- comment at start of line
         Else
            For i = StartLine To EndLine
               ActiveDocument.Selection.GoToLine i

               CommentLoc = dsFirstColumn

               ActiveDocument.Selection.StartOfLine CommentLoc
               ActiveDocument.Selection.CharRight dsExtend, CommentWidth

               If ActiveDocument.Selection = CommentType Then
                  ActiveDocument.Selection.Delete
               Else
                  ActiveDocument.Selection.StartOfLine CommentLoc
                  ActiveDocument.Selection = CommentType + _
                     ActiveDocument.Selection
               End If

            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 Duplicate()
'DESCRIPTION: Duplicates the current selected line

   ActiveDocument.Selection.Copy
   ActiveDocument.Selection.CharRight
   ActiveDocument.Selection.Paste
End Sub

Sub RemoveLineFeed()
'DESCRIPTION: Go to end of line and delete line feed

   ActiveDocument.Selection.EndOfLine
   ActiveDocument.Selection.Delete
   ActiveDocument.Selection.LineDown
End Sub

Sub EndOfLinePaste()
'DESCRIPTION: This macro pastes the contents of the clipboard at the
'end of the line and then goes to the next line.

   ActiveDocument.Selection.EndOfLine
   ActiveDocument.Selection.Paste
   ActiveDocument.Selection.LineDown
   ActiveDocument.Selection.EndOfLine
End Sub

Sub StartOfLinePaste()
'DESCRIPTION: This macro pastes the contents of the clipboard at the
'start of the line and then goes to the next line.

   ActiveDocument.Selection.StartOfLine dsFirstText
   ActiveDocument.Selection.Paste
   ActiveDocument.Selection.LineDown
   ActiveDocument.Selection.StartOfLine dsFirstText
End Sub


Comments

  • Hey you made my day, man!

    Posted by Legacy on 10/15/2003 12:00am

    Originally posted by: Alazan

    really useful thanks man

    Reply
  • really usefull thanks ;)

    Posted by Legacy on 01/15/2003 12:00am

    Originally posted by: sschinz

    thanks alot ...

    Reply
  • How to use this with InterDev

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: zel

    I would apreciate if somone can help me to use these subroutines in InterDev

    Reply
  • Here's an #if 0 variation

    Posted by Legacy on 03/10/2002 12:00am

    Originally posted by: Bruce

    I prefer to #if out a bunch of code rather than comment each line out.  This macro will do that for a single line or a selected block of text.
    
    

    Sub IfZeroOut()
    PoundType = "#if 0"

    If FileType(ActiveDocument) <> 1 Then
    MsgBox ("This macro only works on" + vbLf + _
    ".c, .cpp, .cxx, .h, .hpp, or .hxx files")
    Else
    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
    Sel = vbLf + PoundType + vbLf + vbTab + Sel + _
    vbLf+ "#endif //" + "if zero'd out"
    If Right(Sel,1) <> vbLf Then
    Sel = Sel + vbLf
    End If
    ActiveDocument.Selection = Sel
    End If
    End Sub

    Reply
  • Small change to support /* */

    Posted by Legacy on 06/25/2001 12:00am

    Originally posted by: Marc Britten

    if you want to support commenting out parts of a complete line change the CustomCommentOut function to the below code
    
    

    if you select less than a complete line it will wrap the selection in /* */(in c++/java files) if you select that /* */ delimited block it will remove them.

    Sub CustomCommentOut ()
    '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)
    ' MsgBox "Type: " + CStr(TypeOfFile)
    If TypeOfFile > 0 And TypeOfFile < 6 Then
    If TypeOfFile > 3 Then
    CommentType = "'" ' VBShit
    CommentWidth = 1
    Else
    CommentType = "//" ' C++ and java style comments
    CommentWidth = 2
    End If

    StartLine = ActiveDocument.Selection.TopLine
    EndLine = ActiveDocument.Selection.BottomLine
    If EndLine < StartLine Then
    Temp = StartLine
    StartLine = EndLine
    EndLine = Temp
    End If

    ' Single line -- comment at start of text
    ' have to check for comments at start of line and start of text
    If EndLine = StartLine Then
    If StrComp(ActiveDocument.Selection, "", 1) = 1 And InStr(ActiveDocument.Selection, VbCrLf) = 0 Then
    If TypeOfFile < 3 then
    SelectionString = ActiveDocument.Selection

    If InStr(SelectionString, "/*") < 1 And InStr(SelectionString, "*/") < Len(SelectionString) Then
    ActiveDocument.Selection = "/*" + SelectionString + "*/"
    ElseIf InStr(SelectionString, "/*") = 1 And InStr(SelectionString, "*/") = (Len(SelectionString) - 1)Then
    SelectionString = Right(SelectionString, Len(SelectionString) - 2)
    SelectionString = Left(SelectionString, Len(SelectionString) - 2)
    ActiveDocument.Selection = SelectionString
    End If
    End If
    Else
    ActiveDocument.Selection.StartOfLine dsFirstColumn
    ActiveDocument.Selection.CharRight dsExtend, CommentWidth

    If ActiveDocument.Selection = CommentType Then
    ActiveDocument.Selection.Delete
    Else
    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection.CharRight dsExtend, CommentWidth

    If ActiveDocument.Selection = CommentType Then
    ActiveDocument.Selection.CharRight dsExtend
    ActiveDocument.Selection.Delete
    Else
    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = CommentType + vbTab + ActiveDocument.Selection
    End If
    End If
    End If

    ' Multi-line -- comment at start of line
    Else
    For i = StartLine To EndLine
    ActiveDocument.Selection.GoToLine i

    CommentLoc = dsFirstColumn

    ActiveDocument.Selection.StartOfLine CommentLoc
    ActiveDocument.Selection.CharRight dsExtend, CommentWidth

    If ActiveDocument.Selection = CommentType Then
    ActiveDocument.Selection.Delete
    Else
    ActiveDocument.Selection.StartOfLine CommentLoc
    ActiveDocument.Selection = CommentType + ActiveDocument.Selection
    End If

    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

    Reply
  • Comment out macro

    Posted by Legacy on 04/26/2001 12:00am

    Originally posted by: Bala Danasekaran

    I use this all the time.
    Thanks!

    Reply
  • Excellent, but ...

    Posted by Legacy on 03/15/2001 12:00am

    Originally posted by: rocky

    if you select some lines, and some of them have been commented, and you run this macro, they will be reversed. I have make two macros base on this. One is comment, another is uncomment. They work perfectly.

    Reply
  • really needed

    Posted by Legacy on 01/14/2001 12:00am

    Originally posted by: Veni Madhav Soni

    I got what are they n comment one is really good
    othe are ok

    Reply
  • its sure its very good

    Posted by Legacy on 11/09/2000 12:00am

    Originally posted by: dex

    its sure its very good

    Reply
  • Very Cool!

    Posted by Legacy on 10/09/2000 12:00am

    Originally posted by: Mike Spohn

    Sometimes a programmer comes across a tool that he/she wonders how he/she ever got along without. This is one of them. Thanks for a major timesaver!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds