Author Topic: Block Comment / Uncomment macro  (Read 21422 times)

ewjax

  • Community Member
  • Posts: 10
  • Hero Points: 0
Block Comment / Uncomment macro
« on: July 14, 2006, 02:43:30 am »
From the source file (see attached):

Code: [Select]
#include "slick.sh"

//
// macro to automate block commenting or uncommenting large sections of source code
//
// to use, simply line-select the desired lines, and execute the BlockComment macro.
// The highlighted lines will be block-commented out, using the appropriate comment
// characters for the source file type being edited.  If the lines are already
// block-commented out, they will be uncommented.
//
// The macro is clever enough to respond appropriately for a variety of different
// source types, including .cpp, .h, .e, .l, .java, .pl, .pm, .mak, .mk, and .f
//
// original author:  Elliott W. Jackson
//


// worker subroutine called by main BlockComment routine                                                 
static void ProcessBlockComment(_str commentstring)
{
    int commentlen;
    commentlen = length(commentstring);

    _str seltype;
    seltype = _select_type();

    if (seltype == "LINE")
    {
        // save insert mode, and set mode to insert
        int insertstate;
        insertstate = _insert_state();
        _insert_state(1);
        //messageNwait("Insert state is "insertstate);

        // save current position
        push_bookmark();

        // goto the block beginning
        begin_select();
        begin_line();

        // are we adding or removing comments?
        _str buff;
        buff = get_text(commentlen);

        int removeflag;
        if (buff == commentstring)
            removeflag = 1;
        else
            removeflag = 0;

        // comment out or uncomment all blocked lines
        int numlines;
        int count;

        numlines = count_lines_in_selection();
        for (count = 0; count < numlines; count++)
        {
            begin_line();

            if (removeflag)
            {
                int i;
                i = commentlen;
                while (i--)
                {
                    delete_char();
                }
            }
            else
                _insert_text(commentstring);

            cursor_down();
        }


        // restore original cursor pos
        pop_bookmark();

        // restore original insert mode
        _insert_state(insertstate);

        // turn off the selection
        deselect();

        // final message
        if (removeflag)
            message(numlines" lines uncommented");
        else
            message(numlines" lines commented");

    }
    else
    {
        // error message
        _message_box("Must be in line selection mode!", "Error", MB_ICONEXCLAMATION);
    }

}



_command BlockComment()
{
    // get extension
    _str ext;
    ext = lowcase(get_extension(p_buf_name));

    // handle .cpp files, .h files, vslick .e files, lex .l files
    if ( (ext == "cpp") || (ext == "h") || (ext == "e") || (ext == "l") || (ext == "java") )
    {
        ProcessBlockComment("//");
    }

    // handle perl files and make files
    else if ( (ext == "pl") || (ext == "pm") || (ext == "mk") || (ext == "mak") )
    {
        ProcessBlockComment("#");
    }

    // handle fortran files
    else if ( (ext == "f") )
    {
        ProcessBlockComment("C");
    }


    // huh?
    else
    {
        _message_box("I don't know how to BlockComment this file extension", "Error", MB_ICONEXCLAMATION);
    }
}


« Last Edit: July 14, 2006, 02:51:58 am by ewjax »

jbezem

  • Community Member
  • Posts: 80
  • Hero Points: 8
Re: Block Comment / Uncomment macro
« Reply #1 on: July 17, 2006, 08:43:58 am »
Don't forget to look in 'box.e', functions starting with 'comment_...'
Maybe not identical, but more than enough for my tastes.

FWIW,

Johan

jaykay

  • Community Member
  • Posts: 17
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #2 on: July 31, 2006, 08:54:17 am »
Hello ewjax,

I'm using your macro and am quite happy with it :-) There is only thing that I miss. I can't select a block using the keyboard. I always have to use the mouse to do the block selection. Is there an easy way to implement this ? Unfortunately I have no experience in Slick-C macro programming.

Any help/hint is appreciated.

Thanks in advance.

jaykay

jbezem

  • Community Member
  • Posts: 80
  • Hero Points: 8
Re: Block Comment / Uncomment macro
« Reply #3 on: July 31, 2006, 10:20:23 am »
I don't know the emulation you're using, but if you bind the command "select_block" to a key of your choosing, you should be prepared pretty well.

HTH,

Johan

jaykay

  • Community Member
  • Posts: 17
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #4 on: August 01, 2006, 06:35:41 am »
Hello Johan,

Thanks for the response.
I use the Visual C++ 6 emulation. Using this emulation mode, the keys "shift+up", "shift+down", "shift+right" and "shift+left" are bound to the command "cua-select".
Needs the comment/uncomment macro be modified in order to work with my cua-select mode ?

Regards

jaykay

jbezem

  • Community Member
  • Posts: 80
  • Hero Points: 8
Re: Block Comment / Uncomment macro
« Reply #5 on: August 01, 2006, 08:05:22 am »
Well, I use the SlickEdit mode, being an SE user for almost 15 years, so I couln't tell.
But a simple try should tell you all you need to know. But: The macro only does something when the selection is a line selection, so the keyboard shortcut for a block selection as requested in your post will not help you much there.

HTH,

Johan

jaykay

  • Community Member
  • Posts: 17
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #6 on: August 01, 2006, 09:23:25 am »
I did not know, that there are so many different selection modes.

Anyway, I found the way to do a line-select using the keyboard (Ctrl+F8 in Visual C++ 6 emulation mode, "Alt+L" in SlickEdit mode). This does a line-select as required by the comment/uncomment macro. That's OK for me at the moment although it would be nice to get the comment/uncomment macro work with the cua-select mode.

jaykay

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Block Comment / Uncomment macro
« Reply #7 on: August 01, 2006, 10:35:27 am »
Try this changed (thanks Elliott) version with relaxed selection requirement.
Automagically changes existing (e.g. cua-) selection to a line selection and works on current line, if nothing selected.
The extension checking part uses some clearer reg-exp.s (2 more comment types added).
Last the status line (+beep) is used for telling a problem instead of pop-up.

HS2
« Last Edit: August 01, 2006, 11:09:42 am by hs2 »

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Block Comment / Uncomment macro
« Reply #8 on: August 01, 2006, 01:14:10 pm »
'select-block' is the command to begin/end a block selection.  The default key binding for block selection is Ctrl+B in Visual C++ 6 emulation.  If you have changed the binding for that key, you can bind a another one to that command.

jaykay

  • Community Member
  • Posts: 17
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #9 on: August 02, 2006, 08:26:09 am »
Hello HS2,

thanks for the modification of the macro :-)
Also thanks to Elliott who wrote the original version of the macro.
Now everything works fine.

Thanks again.

jaykay

aoehlke

  • Community Member
  • Posts: 48
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #10 on: August 28, 2006, 06:12:14 pm »
Hmm, anyone help a newbie?

I have copied this macro into my /home directory/.slickedit in unix.
I then loaded the module.

now what?
how do I see the option to block comment something?
I have selected a bunch of text with my mouse... but I dont know how to activate the block comment.
shouldnt there be a menu option somewhere?

I have a whole bunch of other macros I'd like to start using... but I figured this one would make a good guinea pig.

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Block Comment / Uncomment macro
« Reply #11 on: August 28, 2006, 06:25:21 pm »
how do I see the option to block comment something?
I have selected a bunch of text with my mouse... but I dont know how to activate the block comment.
shouldnt there be a menu option somewhere?


For Windows
--  Select some text
--  Activate SlickEdit's command line, type BlockC<tab>, and the command name is completed.
--  Press <enter>

Binding the command to a key is done through Tools->Options->Key Bindings...
The main menu can be edited via Macro->Menus.  For the right-mouse-button context menu, there is an "Edit them menu" item on the context menu.

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Block Comment / Uncomment macro
« Reply #12 on: August 28, 2006, 06:28:47 pm »
There are 2 options:
1. not-so-good option: activate cmdline and type BlockComment and press <ENTER> ... clumsy, right ?
2. better: 'Tools->Options->Key Bindings' -> choose BlockComment in the list of available macros ('Commands' list ) and after that do 'Add Key or MouseClick' and hit the key combination you want.
If it's already in use Slick tells that. You may repeate the procedure using another shortcut or you may override the former binding by just doing a heedless 'Bind'.

Hope it's a bit clearer now.
You'll see that there are a lot of useful commands not bound to a shortcut and there are a lot of shortcuts not bound to commands.
That's the key to get even more editing power out of Slick ...

HS2

aoehlke

  • Community Member
  • Posts: 48
  • Hero Points: 1
Re: Block Comment / Uncomment macro
« Reply #13 on: August 28, 2006, 06:29:22 pm »
will try this... thanks.

next question is, where are the currently loaded macros stored? are they ona project by project basis?
or is it that once they are loaded, they remain that way in a per user config file setting?


hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Block Comment / Uncomment macro
« Reply #14 on: August 28, 2006, 07:05:39 pm »
They are stored (compiled version) in the vslick.sta file which resides in the 'config' directory.
Default for this dir is per user (HOME) but you may setup a (system wide) VSLICKCONFIG env. var to e.g. use a global dir or another per user dir ...
BTW: There is s.th. to read in the help...

Loaded macros are 'Slick global'.
You may setup some project specifc things in 'Project->Project Properties -> Open Tab' where you can add e.g. a bunch of Slick macros doing a complete reconfiguration of Slick ;)

HS2
« Last Edit: August 28, 2006, 07:12:31 pm by hs2 »