Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Comment a block

Help
2007-07-30
2013-05-02
  • Hello joe developers,

    first of all thanks for this editor... it's the FIRST thing I install whenever I put my pawns on a machine!
    I had a question: is there a way to automagically comment (i.e. add a "#" at the beginning of a line) a selected block of text?

    It's be useful when I edit files like /etc/network/interfaces or xorg.conf.

    Thanks for your help and keep up the good work!

     
    • As far as I know, there is no comment/uncomment command.  Two ways you could do it are:

      ^[B
      ^KF
      \^\*\$
      rk
      #\&
      r

      or

      ^K/
      sed 's/^.*$/#&/'

      The second method might be faster if you do the same operation many times in a file since you can hit up to recall the sed command after entering it the first time.  Unfortunately the history for filter commands does not persist between invocations of joe.  The history for search and replace commands does, so that might be faster if you open and close joe frequently.

      Charles J. Tabony

       
    • Danny
      Danny
      2007-08-18

      Any way of integrating this into Joe?  Or making a semi-permanent shortcut defined for the operation?

       
      • This certainly could be, and probably should be, made a command.  For some types of files, JOE knows the syntax of comments and uses that knowledge to ignore the contents of comments when searching for matching delimiters.  However, the different styles of comments are hard coded.  See this snippet of code from uedit.c:

                                } else if ((bw->o.pound_comment && d == '#') ||
                                           (bw->o.semi_comment && d == ';') ||
                                           (bw->o.vhdl_comment && d == '-' && brch(p) == '-')) {
                                        while ((d = pgetc(p)) != NO_MORE_DATA)                 
                                                if (d == '\n')
                                                        break;
                                } else if ((bw->o.c_comment || bw->o.cpp_comment) && d == '/') {

        It would be good if JOE had some more general way of handling comment styles, such as being able to specify the string that starts a comment and the string that terminates a comment (which in most cases is a newline).

        For comments that end with a newline, a "comment block" operation would be simple.  Other types of comments, like C style comments would be trickier.  Every person has their own style of commenting a block of text.  Also, what would you do if the block already contained a comment?  If you want an uncomment block command, then how would it undo your solution to the last question?

        If you (or someone reading this) want to implement this, then you can use urindent or ulindent in ublock.c as a starting point.  Speaking of which, you can actually comment a block with pound signs by going to options (^T), selecting "Indent char", entering 35 (the ASCII code for '#'), and then using the indent block command.  Just make sure you set the indent character back before you try to actually indent something.

        You can make a semi-permanent shortcut for any sequence of commands using macros.  Hit "^K[", enter a number from 0 through 9, then enter one of the sequences of commands above, then hit "^K]".  Later, you can hit ^K followed by the number you entered to perform the sequence of commands.  The macro will be remembered between sessions of JOE.  If you use the first sequence and you don't want your cursor to move when you invoke the macro, then you can add two "^K-"s to the end of the sequence like so:

        ^K[
        0
        ^[B
        ^KF
        \^\*\$
        rk
        #\&
        r
        ^K-
        ^K-
        ^K]

        uncomment block macro:

        ^K[
        1
        ^[B
        ^KF
        \^#\*\$
        rk
        \0
        r
        ^K-
        ^K-
        ^K]

        Charles J. Tabony