Author Topic: surround selection on pressing {  (Read 6014 times)

at5dapa1

  • Senior Community Member
  • Posts: 175
  • Hero Points: 18
surround selection on pressing {
« on: October 12, 2006, 01:04:24 pm »
Hi,
if I have a selected text and pres {, I want to automatically surround the selection with braces, like Visual Assist does. I have SE11.02 with CodeWright emulation. I have Finnish keyboard, so in order to get "{", I have to press "AltGr+7".

This is a small macro:

Code: [Select]
_command my_surround_with_braces() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
    _macro('R',1);
    _str seltype;
    seltype = _select_type();
    if((seltype == "CHAR") || (seltype == "LINE") || (seltype == "BLOCK"))
    {
        //say("selection");
        surround_with('braces');
    }
    else
    {
        //say("NO selection!");
        keyin("{");
    }
}

I bind it to {. Now if I press {, it will remove the selection and replace with {???
If I set as binding Ctrl+MButtonDn, or just run the macro from the ListMacro menu, it works. How to solve this?

Thanks,
Daniel

PS: I want to do the same, for surrounding with comments, I want to assign as shortcut /, and if there's any selection then it will be automatically commented, else just will type /.


Graeme

  • Senior Community Member
  • Posts: 2366
  • Hero Points: 313
Re: surround selection on pressing {
« Reply #1 on: October 18, 2006, 12:11:43 pm »

It looks like it's not possible to override the binding of { to c-begin.  The option in "file extension setup" via the options button to not "insert braces immediately" didn't work for me either.

Since you have to use ALT-7 to get {, maybe you could just bind your macro to some other key like ALT B perhaps?

Graeme

at5dapa1

  • Senior Community Member
  • Posts: 175
  • Hero Points: 18
Re: surround selection on pressing {
« Reply #2 on: October 19, 2006, 06:19:03 am »
Thanks Graeme,
good idea Alt+B (from "Brace")  ;)

With Alt+B is working, but seems that for "{" the macro is not called at all, even into the Bind Command to Key dialog, it still shows me that my macro is binded to "{".


Graeme

  • Senior Community Member
  • Posts: 2366
  • Hero Points: 313
Re: surround selection on pressing {
« Reply #3 on: October 19, 2006, 11:49:26 am »

Yep, same on my system.  Ideally the key binding dialog should give an error message for keys that can't be overridden instead of accepting them, and a list of them added to slick help file in the key binding dialog section  :)

BTW - somehow the name of your function in the keybinding dialog is spelt wrong - you seem to have sprround instead of surround.  I hope ALT+B isn't too inconvenient for you.

Graeme

suevian

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: surround selection on pressing {
« Reply #4 on: October 19, 2006, 12:34:55 pm »
On my 11.0.2 SHIFT + { is bound to command c_begin.

I modified c_begin a little bit and it works.

Open file c.e and merge with the following:

(Note that I've removed one of the name_info arguments, that's what made it work, but honestly I have no idea what will be the effect of this removal, maybe this is just the place to ask?)

Code: [Select]
_command void c_begin() name_info(','VSARG2_CMDLINE)
{
    typeless expand, be_style, indent_fl;

    //surround with braces if selection
    if (!_isnull_selection ())
    {
        surround_with('braces');
        return;
    }

    parse name_info(_edit_window().p_index) with . expand . . be_style indent_fl .;
    int cfg = 0;
    if (!command_state() && p_col>1) {
        left();cfg=_clex_find(0,'g');right();
    }
    if ( command_state() || cfg==CFG_STRING || _in_comment() ||
        c_expand_begin(expand,p_SyntaxIndent,be_style,indent_fl) ) {
        call_root_key('{');
    } else if (_argument=='') {
        _undo('S');
    }

}


suevian

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: surround selection on pressing {
« Reply #5 on: October 19, 2006, 12:39:13 pm »
I just noticed you're using "AltGr+7" not SHIFT + {, sorry.

Anyway, maybe you can accomodate the above for your CodeRight emulation and "AltGr+7" way of typing { if this does not work.


at5dapa1

  • Senior Community Member
  • Posts: 175
  • Hero Points: 18
Re: surround selection on pressing {
« Reply #6 on: October 19, 2006, 01:07:43 pm »
Thanks to all!

... BTW - somehow the name of your function in the keybinding dialog is spelt wrong - you seem to have sprround instead of surround.  ...
Graeme

 :o Graeme you are good! I have to wipe my glasses!  :D

hs2

  • Senior Community Member
  • Posts: 2738
  • Hero Points: 288
Re: surround selection on pressing {
« Reply #7 on: October 19, 2006, 05:00:44 pm »
I think instead of removing a name_info attr. you should add one (VSARG2_MARK).
This is req.d by surround_with().
Code: [Select]
_command void c_begin() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE|VSARG2_MARK)

HS2

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 4937
  • Hero Points: 404
Re: surround selection on pressing {
« Reply #8 on: October 19, 2006, 05:56:27 pm »
Thanks hs2.  You definitely want VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE.  These flags indicate that the command requires an editor control but also can be executed when the command line has focus.  the VSARG2_MARK flag indicates the no default selection processing code should be executed before the command gets control.

hs2

  • Senior Community Member
  • Posts: 2738
  • Hero Points: 288
Re: surround selection on pressing {
« Reply #9 on: October 19, 2006, 06:37:35 pm »
Oh man .. this is the 1st explanation of the mystic name_info's I've seen (well - I didn't read the whole help) !
And just in 1 sentence which I could also understand ;)
I know some other guys were/are just guessing like me, but intuitively did it right somehow.

Thanks Clark !

HS2