How to comment out several lines.

Andrew Stanaski astan at cadence.com
Thu May 28 18:30:46 CEST 2009


I have some macros that I use to comment out lines for various languages. 
I bind them to the letter'Z': Ctrl-Z and Shift-Ctrl-Z for comment and 
uncomment (I also use the old expander code to do box comments with Alt-Z).

Andy S.

Macros
------

# Nedit macro file #
#
#==========================================================================
# find_comment_string
#
# Usage: find_comment_string()
#==========================================================================

define find_comment_string
{
if( $language_mode == "Skill" ) comment_str = ";"
else if( $language_mode == "Csh" || \
         $language_mode == "Sh Ksh Bash" || \
         $language_mode == "Perl" || \
         $language_mode == "NEdit Macro") comment_str = "#"
else if( $language_mode == "C++" || \
         $language_mode == "Java" || \
         $language_mode == "Verilog")   comment_str = "//"
else if( $language_mode == "CDL" )  comment_str = "*"
else
     {
     comment_str = ""
     dialog( "find_comment_string: Comment string for "$language_mode" 
unknown.", "Ok" )
     }
return( comment_str )
}



#==========================================================================
# smart_comment
#
# Usage: smart_comment("#")   # is an (optional) comment string.
#
# If something is selected (not rectangular), comments out all the lines
# selected.
#
# If the lines are all indented the same, indent the comment string too.
# If the lines are not indented the same, comment from the beginning.
#
# If there is no selection, comment at the insertion point.
#==========================================================================

define smart_comment
{
if( $n_args != 0 ) comment_str = $1
else               comment_str = find_comment_string()

if( comment_str == "" ) return

if( $selection_left < 0 )
     {
     if( $selection_start < 0 )  #-- Nothing is selected --
         {
         mark(1)
         replace(".*$", comment_str" &", "forward", "regex")
         goto_mark(1)
         }
     else
         {
         numSpace  = 0
         numTab    = 0
         prevSpace = 0
         prevTab   = 0

         first        = 1
         indentsMatch = 1

         pos = $selection_start

         while ( pos >= $selection_start && pos <= $selection_end )
             {
             char = get_character( pos )
             ++pos
             if      ( char == " "  )  ++numSpace
             else if ( char == "\t" )  ++numTab
             else
                 {
                 if( first != 1 && (numSpace != prevSpace || numTab != 
prevTab) )
                     {
                     indentsMatch = 0
                     break
                     }
                 first = 0
                 prevSpace = numSpace
                 prevTab   = numTab
                 numSpace  = 0
                 numTab    = 0
                 pos = search( "\n", pos, "regex" )
                 ++pos
                 }
             }
         mark(1)
         if( indentsMatch == 1 )
             {
             replace_in_selection("^([ \\t]*)(.*)$", "\\1"comment_str" 
\\2", "regex")
             }
         else
             {
             replace_in_selection("^[ \\t]*.*$", comment_str" &", "regex")
             }
         goto_mark(1)
         }
     }
else
     # Rectangular selection
     #
     {
     mark(1)
     comment_len = length( comment_str )

     text     = get_range( $selection_start, $selection_end )
     new_text = ""
     prev     = 0
     pos      = search_string( text, "\n", prev, "regex" )

     while ( pos > -1 )
         {
         place      = prev + $selection_left
         first_part = substring( text, prev, place )
         last_part  = substring( text, place, pos + 1 )
         new_text   = new_text first_part comment_str" " last_part

         prev = pos + 1
         pos  = search_string( text, "\n", prev, "regex" )
         }

     if( prev != $selection_end )
         {
         place      = prev + $selection_left
         first_part = substring( text, prev, place )
         last_part  = substring( text, place, $selection_end )
         new_text   = new_text first_part comment_str" " last_part
         }

     replace_range( $selection_start, $selection_end, new_text )
     goto_mark(1)
     }
}



#==========================================================================
# smart_uncomment
#
# Usage: smart_uncomment("#")   # is the (optional) comment character.
#
# If something is selected (not rectangular), removes comments
# *from the beginning of each line* on all the lines selected.
#
# If there is no selection, removes the next comment character anywhere
# within the line between the insertion point and the end of the line.
#==========================================================================

define smart_uncomment
{
if( $n_args != 0 ) comment_str = $1
else               comment_str = find_comment_string()

if( comment_str != "" && $selection_left == -1 )
     {
     if( $selection_start != -1 )  #-- Something is selected --
         {
         mark(1)
         comment_len = length( comment_str )
         literal_comment_str = ""

         for ( i = 0; i < comment_len; ++i )
             {
                 comment_char = substring( comment_str, i, i+1 )
                 literal_comment_str = literal_comment_str"["comment_char"]"
             }
         replace_in_selection( "(^[ \\t]*)"literal_comment_str" ?(.*)$", 
"\\1\\2", "regex" )
         goto_mark(1)
         }
     else
         {
 
#----------------------------------------------------------------------
         # Check to see if there is a comment character(s) in the line between
         # the cursor and the end of the line. If one exists, remove it and
         # move the line to the left. Leave the cursor in the original 
position.
 
#----------------------------------------------------------------------
         mark(1)
         cursor = $cursor
         end_of_line()
         endOfLine = $cursor
         goto_mark(1)

         lineText = get_range( cursor, endOfLine )
         pos      = search_string( lineText, comment_str, 0 )

         if( pos > -1 )
             {
                 comment_len = length( comment_str )
                 begin       = cursor + pos
                 end         = begin  + comment_len
                 endComment  = pos    + comment_len

                 if( substring( lineText, endComment, endComment + 1 ) == 
" " )
                     {
                         ++end
                     }
                 replace_range( begin, end, "" )
             }
         }
     }
}


nedit.rc file
-------------
         Comments>+ Skill Comment at Skill:Ctrl+Z::: {\n\
                 smart_comment(";")\n\
         }\n\
         Comments>- Skill Comment at Skill:Shift+Ctrl+Z::: {\n\
                 smart_uncomment(";")\n\
         }\n\
         Comments>+ CDL Comment at CDL:Ctrl+Z::: {\n\
                 smart_comment("*")\n\
         }\n\
         Comments>- CDL Comment at CDL:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("*")\n\
         }\n\
         Comments>+ Shell Comment at Csh@Ksh at Sh Ksh Bash at Perl@NEdit 
Macro at Tcl@Makefile at Modulefile@Python:Ctrl+Z::: {\n\
                 smart_comment("#")\n\
         }\n\
         Comments>- Shell Comment at Csh@Ksh at Sh Ksh Bash at Perl@NEdit 
Macro at Tcl@Makefile at Modulefile@Python:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("#")\n\
         }\n\
         Comments>+ C++ Comment at C++@Java at JavaScript@Verilog:Ctrl+Z::: {\n\
                 smart_comment("//")\n\
         }\n\
         Comments>- C++ 
Comment at C++@Java at JavaScript@Verilog:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("//")\n\
         }\n\
         Comments>+ Expander Comment at Expander:Ctrl+Z::: {\n\
                 smart_comment("!")\n\
         }\n\
         Comments>- Expander Comment at Expander:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("!")\n\
         }\n\
         Comments>+ Ada Comment at Ada@VHDL:Ctrl+Z::: {\n\
                 smart_comment("--")\n\
         }\n\
         Comments>- Ada Comment at Ada@VHDL:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("--")\n\
         }\n\
         Comments>+ Latex Comment at Latex:Ctrl+Z::: {\n\
                 smart_comment("%")\n\
         }\n\
         Comments>- Latex Comment at Latex:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("%")\n\
         }\n\


Eric Bouyoux wrote:
> Hi,
> 
> I would like to comment several successive lines in a file.
> Ex :
> Line1
> Line2
> Line3
> Line4
> 
> becomes
> 
> Line1
> //Line2
> //Line3
> Line4
> 
> I can do it with a regular expression and Replace in selection.
> 
> But I would like to know if it already exists a macro I could bind to a key.
> 
> Regards.
> 
> Eric.


More information about the Discuss mailing list