Discussion:
How to comment out several lines.
Eric Bouyoux
2009-05-28 12:23:44 UTC
Permalink
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 - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
dantian
2009-05-28 12:35:19 UTC
Permalink
Hi,

If it's in C or C++ you can select the lines to comment and go to
"Macro" and then "Comments".
I think it should also work with linux shell scripts.
Best,

Eric.
Post by Eric Bouyoux
Hi,
I would like to comment several successive lines in a file.
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.
--
/ Be the change you wish to see in the world
/ --- Mahatma Gandhi ---

dantian.free.fr <http://dantian.free.fr>

/gtalk/: aihaike-***@public.gmane.org
/msn/: aihaike-***@public.gmane.org
/skype/: aihaike

/ Please consider the environment before printing this email.
Considérez svp l'environnement avant d'imprimer cet email. /
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Andrew Hood
2009-05-28 12:49:33 UTC
Permalink
Post by Eric Bouyoux
Hi,
I would like to comment several successive lines in a file.
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.
If the language mode is C++ this is in the macro menus.

If it is some other mode you could customise the macro definition to
include your mode as well as C++
--
There's no point in being grown up if you can't be childish sometimes.
-- Dr. Who
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
dantian
2009-05-28 12:55:34 UTC
Permalink
Yes,

to so so go to "Preferences" -> "Default Settings" -> "Customize Menus"
-> "Macro Menu"

Hope it helps.

Eric.
Post by Andrew Hood
Post by Eric Bouyoux
Hi,
I would like to comment several successive lines in a file.
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.
If the language mode is C++ this is in the macro menus.
If it is some other mode you could customise the macro definition to
include your mode as well as C++
--
/ Be the change you wish to see in the world
/ --- Mahatma Gandhi ---

dantian.free.fr <http://dantian.free.fr>

/gtalk/: aihaike-***@public.gmane.org
/msn/: aihaike-***@public.gmane.org
/skype/: aihaike

/ Please consider the environment before printing this email.
Considérez svp l'environnement avant d'imprimer cet email. /
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Andrew Stanaski
2009-05-28 16:30:46 UTC
Permalink
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 ***@Skill:Ctrl+Z::: {\n\
smart_comment(";")\n\
}\n\
Comments>- Skill ***@Skill:Shift+Ctrl+Z::: {\n\
smart_uncomment(";")\n\
}\n\
Comments>+ CDL ***@CDL:Ctrl+Z::: {\n\
smart_comment("*")\n\
}\n\
Comments>- CDL ***@CDL:Shift+Ctrl+Z::: {\n\
smart_uncomment("*")\n\
}\n\
Comments>+ Shell ***@Csh@***@Sh Ksh ***@Perl@NEdit
***@Tcl@***@Modulefile@Python:Ctrl+Z::: {\n\
smart_comment("#")\n\
}\n\
Comments>- Shell ***@Csh@***@Sh Ksh ***@Perl@NEdit
***@Tcl@***@Modulefile@Python:Shift+Ctrl+Z::: {\n\
smart_uncomment("#")\n\
}\n\
Comments>+ C++ ***@C++@***@JavaScript@Verilog:Ctrl+Z::: {\n\
smart_comment("//")\n\
}\n\
Comments>- C++
***@C++@***@JavaScript@Verilog:Shift+Ctrl+Z::: {\n\
smart_uncomment("//")\n\
}\n\
Comments>+ Expander ***@Expander:Ctrl+Z::: {\n\
smart_comment("!")\n\
}\n\
Comments>- Expander ***@Expander:Shift+Ctrl+Z::: {\n\
smart_uncomment("!")\n\
}\n\
Comments>+ Ada ***@Ada@VHDL:Ctrl+Z::: {\n\
smart_comment("--")\n\
}\n\
Comments>- Ada ***@Ada@VHDL:Shift+Ctrl+Z::: {\n\
smart_uncomment("--")\n\
}\n\
Comments>+ Latex ***@Latex:Ctrl+Z::: {\n\
smart_comment("%")\n\
}\n\
Comments>- Latex ***@Latex:Shift+Ctrl+Z::: {\n\
smart_uncomment("%")\n\
}\n\
Post by Eric Bouyoux
Hi,
I would like to comment several successive lines in a file.
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 - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Eric Bouyoux
2009-05-29 06:47:34 UTC
Permalink
Hi,

I didn't know it was possible to customize the "Macro" menu.
I don't know for the moment if I'll adapt the existing macros to Verilog
language or if I'll try to use Andrew's macros, but thanks to all, I'll
succeed to do what I wanted to.

Regards.

Eric.

Regards.

Eric.
Post by Eric Bouyoux
Hi,
I would like to comment several successive lines in a file.
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 - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Continue reading on narkive:
Loading...