Author Topic: Automatic close parenthesis  (Read 8397 times)

afflictedd2

  • Guest
Automatic close parenthesis
« on: May 05, 2008, 10:29:16 pm »
Hi everyone,

I've been looking for this option.. to close parenthesis as soon as I type the open parenthesis
and put the cursor in the middle, same with the quotes (l) "l", I like it that way better because
I don't have to keep track of parenthesis.

Thanks,

Ed.


Graeme

  • Senior Community Member
  • Posts: 2748
  • Hero Points: 340
Re: Automatic close parenthesis
« Reply #1 on: May 06, 2008, 12:57:22 pm »
Hi everyone,

I've been looking for this option.. to close parenthesis as soon as I type the open parenthesis
and put the cursor in the middle, same with the quotes (l) "l", I like it that way better because
I don't have to keep track of parenthesis.

Thanks,

Ed.



There's no configuration option that provides this AFAICS.  There's a couple of possibilities. 

The safest way is to define a macro like this
Code: [Select]
_command void my_paren() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE)
{
   _insert_text('()');
   left();
}
and bind a key to it such as Ctrl-Shift-P

Alternatively, you could have
Code: [Select]
_command void my_paren() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE)
{
   _insert_text(')');
   left();
    keyin('(');
}
and bind to Ctrl-Shift-P, which has the advantage of performing the special processing you normally get when typing an opening paren (I think).

Another way is to modify the code in c.e c_paren() as follows - the lines followed by "// <<<<<<<<<<<<<<<<  added" have been added.
If you want to try this, on the slick cmd line, type fp c_paren and you will be taken to the c_paren function in c.e.  After changing the code, you can load it by using the load module command on the macro menu.

Quote
_command void c_paren() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE|VSARG2_LASTKEY)
{
   // Called from command line?
   if (command_state()) {
      call_root_key('(');
      return;
   }

   // Handle Assembler embedded in C
   typeless orig_values;
   int embedded_status=_EmbeddedStart(orig_values,'');
   if (embedded_status==1) {
      call_key(last_event());
      _EmbeddedEnd(orig_values);
      return;
   }

   _insert_text(')');        // <<<<<<<<<<<<<<<<  added
   left();                // <<<<<<<<<<<<<<<<  added

   // Check syntax expansion options
   typeless expand, be_style, indent_fl;
   parse name_info(_edit_window().p_index) with . expand . . be_style indent_fl .;
   if (expand && p_SyntaxIndent>=0 && !_in_comment() &&
       !c_expand_space(p_SyntaxIndent,be_style,indent_fl)) {
      return;
   }

   // not the syntax expansion case, so try function help
   auto_functionhelp_key();
}


Yet another way is to define a macro like this
Code: [Select]
_command void my_paren() name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE)
{
   _insert_text(')');
   left();
   if (p_extension == 'Slick-C') {
      slick_paren();
   }
   else
   {
      c_paren();
   }
}

You could bind the '(' key to this macro, however, you will have to unbind the '(' from both c_paren and slick_paren commands to get this to work and once you've done that there's no way to get the normal bindings of '(' back  (I haven't found one so far anyway!) so I don't recommend this.

I can't guarantee your computer won't self destruct or that slick won't misbehave when you try these things so I strongly recommend you backup your entire configuration folder first so you can back out any changes if you get stuck.

In case you haven't written a macro before, you can place your macros in a file called vusrmacs.e which should be in your configuration folder and load it using "load module" on the macro menu.  Make sure your macro file has #include "slick.sh" at the start.

Graeme
« Last Edit: May 06, 2008, 12:59:20 pm by Graeme »

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Automatic close parenthesis
« Reply #2 on: May 06, 2008, 05:19:18 pm »
You could bind the '(' key to this macro, however, you will have to unbind the '(' from both c_paren and slick_paren commands to get this to work and once you've done that there's no way to get the normal bindings of '(' back  (I haven't found one so far anyway!)

I think you are talking about having a single key bound to multiple different commands, based on multiple different modes.  If yes, then it's pretty easy to bind keys based on mode:

In the Keyboard->Key Bindings node of the "Options" dialog, select a command, then click the Add... button (as you normally do when adding a new key binding).  In the "Bind Key" dialog that comes up, click the Advanced >> button to expand the dialog.  Click the Bind to mode checkbox, and select the mode you want from the dropdown.  For example "ANSI-C" or "C/C++" or "Slick-C" or etc.

So you could add a default (no mode) binding for '(' to one command, then add a C/C++ binding for '(' to a second command, then add a Slick-C binding for '(' to a third command.  In the second and third commands, it is possible to "chain" back to the command in the default binding via call_root_key.  (I think I've seen another way also in the stock Slick-C macros, but I forget what or where).

HTH!
Chris
« Last Edit: May 06, 2008, 05:21:03 pm by chrisant »

Graeme

  • Senior Community Member
  • Posts: 2748
  • Hero Points: 340
Re: Automatic close parenthesis
« Reply #3 on: May 06, 2008, 10:37:23 pm »
Thanks Chris.

afflictedd2

  • Guest
Re: Automatic close parenthesis
« Reply #4 on: May 06, 2008, 10:42:25 pm »
Thank you both!