Re: How to comment out several lines.

Andrew Stanaski <[email protected]>
Newsgroups gmane.editors.nedit.user
Message-ID <[email protected]>
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@Skill:Ctrl+Z::: {\n\
                 smart_comment(";")\n\
         }\n\
         Comments>- Skill Comment@Skill:Shift+Ctrl+Z::: {\n\
                 smart_uncomment(";")\n\
         }\n\
         Comments>+ CDL Comment@CDL:Ctrl+Z::: {\n\
                 smart_comment("*")\n\
         }\n\
         Comments>- CDL Comment@CDL:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("*")\n\
         }\n\
         Comments>+ Shell Comment@Csh@Ksh@Sh Ksh Bash@Perl@NEdit 
Macro@Tcl@Makefile@Modulefile@Python:Ctrl+Z::: {\n\
                 smart_comment("#")\n\
         }\n\
         Comments>- Shell Comment@Csh@Ksh@Sh Ksh Bash@Perl@NEdit 
Macro@Tcl@Makefile@Modulefile@Python:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("#")\n\
         }\n\
         Comments>+ C++ Comment@C++@Java@JavaScript@Verilog:Ctrl+Z::: {\n\
                 smart_comment("//")\n\
         }\n\
         Comments>- C++ 
Comment@C++@Java@JavaScript@Verilog:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("//")\n\
         }\n\
         Comments>+ Expander Comment@Expander:Ctrl+Z::: {\n\
                 smart_comment("!")\n\
         }\n\
         Comments>- Expander Comment@Expander:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("!")\n\
         }\n\
         Comments>+ Ada Comment@Ada@VHDL:Ctrl+Z::: {\n\
                 smart_comment("--")\n\
         }\n\
         Comments>- Ada Comment@Ada@VHDL:Shift+Ctrl+Z::: {\n\
                 smart_uncomment("--")\n\
         }\n\
         Comments>+ Latex Comment@Latex:Ctrl+Z::: {\n\
                 smart_comment("%")\n\
         }\n\
         Comments>- Latex Comment@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.
-- 
NEdit Discuss mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/discuss
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.