Author Topic: Patch to make auto-complete key also do dynamic surround  (Read 9885 times)

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Patch to make auto-complete key also do dynamic surround
« on: March 27, 2009, 07:38:03 am »
Patch to built in command:
codehelp-complete

Written for:
SlickEdit 14.0, but can probably be applied to 13 (maybe even 12 if dynamic surround was available in 12).

Description:
Your codehelp-complete key binding (default is Ctrl+Space in CUA emulation) will continue to work as before, with the added bonus that it invokes the dynamic-surround command in the following cases:
  • If the cursor is on an open brace, or if there is only whitespace between the cursor and an open brace.
  • If the cursor is on the pound sign of an #if or #ifdef or #ifndef or #region, or if the cursor is in whitespace before one.  (Dynamic surround doesn't support #else or #elif directives).

Installation:
1.  Open the macros\codehelp.e file.
2.  Add an additional #import line near the top of the file, amongst the other #import lines:
Code: [Select]
#import "mouse.e"
3.  Find the codehelp_complete command.
4.  Copy the patch from below to the illustrated location in the command.
5.  The community forums have a bug where the word "sub-str" (without the hyphen) cannot exist in a post.  So edit the 10 places in the patch that have "sub-str" with a hyphen, and remove the hyphen.
6.  Reload the module (Macro|Load Module).

Warning:  Modifying built in macros can potentially be dangerous.  Exercise caution.

Code: [Select]
_command void codehelp_complete() name_info(','VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL|VSARG2_LASTKEY)
{
   if (machine()=='LINUX') {
      if (!gSCIMConfigured && last_event():==name2event('c- ') && _SCIMRunning()) {
         gSCIMConfigured=true;
         int result=_message_box("Do you want Ctrl+Space to perform completion?\n\nIf you choose \"Yes\" (recommended), you can display the SCIM input method editor by pressing Ctrl+Alt+Space instead of Ctrl+Space. If you choose \"No\", Ctrl+Space will display the SCIM input method editor and you will not have any alternate key binding for performing symbol/alias completion.\n\nTo configure this later, go to Tools>Options>Redefine Common Keys and set the \"Use Ctrl+Space for input method editor\" check box.",'Configure Ctrl+Space',MB_YESNO);
         if (result==IDYES) {
            _default_option(VSOPTION_USE_CTRL_SPACE_FOR_IME,0);
         } else {
            _default_option(VSOPTION_USE_CTRL_SPACE_FOR_IME,1);
         }
         return;
      }
   }

//-------- PATCH BEGINS HERE --------
   // Ctrl+Space activates dynamic_surround:  on an open brace (or in
   // whitespace near one), or on a #if/#ifdef/#ifndef/#region directive (or
   // in whitespace before one).
   if (!command_state()) {
      _str line;
      get_line(line);
      if (length(line)) {
         int iCursor = _text_colc(p_col, 'P');
         if (iCursor > length(line))
            iCursor = length(line);
         int iFind = iCursor;
         boolean fDynamic = false;
         if (sub-str(line, iFind, 1) == '#') {
            switch (sub-str(line, iFind + 1, 2)) {
            case "if":
            case "re":
               fDynamic = true;
            }
         } else if (sub-str(line, iFind, 1) == '{') {
            fDynamic = true;
         } else if (_isSpaceChar(sub-str(line, iFind, 1))) {
            if (!fDynamic) {
               for (iFind = iCursor; iFind > 0; iFind--) {
                  if (sub-str(line, iFind, 1) == '{') {
                     fDynamic = true;
                     break;
                  } else if (!_isSpaceChar(sub-str(line, iFind, 1))) {
                     break;
                  }
               }
            }
            if (!fDynamic) {
               for (iFind = iCursor; iFind <= length(line); iFind++) {
                  if (sub-str(line, iFind, 1) == '{') {
                     fDynamic = true;
                     break;
                  } else if (sub-str(line, iFind, 1) == '#') {
                     _str tmp = sub-str(line, iFind + 1, 2);
                     fDynamic = (tmp == "if" || tmp == "re");
                     break;
                  } else if (!_isSpaceChar(sub-str(line, iFind, 1))) {
                     break;
                  }
               }
            }
         }
         if (fDynamic) {
            save_pos(auto p);
            if (!dynamic_surround(true))
               return;
            restore_pos(p);
         }
      }
   }
//-------- PATCH ENDS HERE --------
         
   //say("codehelp_complete()");
   if (!command_state()) {
« Last Edit: April 30, 2009, 07:48:46 pm by chrisant »

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Patch to make auto-complete key also do dynamic surround
« Reply #1 on: March 27, 2009, 09:05:54 pm »
Way cool chrisant ! And yes - it's fine even with good old v12...
Thanks and HP++,
HS2

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Patch to make auto-complete key also do dynamic surround
« Reply #2 on: April 01, 2009, 11:24:40 pm »
Updated the patch above to fix an issue where it accidentally tried dynamic_surround if a brace is anywhere on the current line -- it was meant to only try dynamic_surround if the cursor is in whitespace immediately before/after a brace.

dunkers

  • Senior Community Member
  • Posts: 631
  • Hero Points: 28
Re: Patch to make auto-complete key also do dynamic surround
« Reply #3 on: April 30, 2009, 02:31:21 am »
Small quibble:

Using that patch produces an error: "Expecting procedure name - identifier not found - import may be required". And the cursor is left on _isSpaceChar().

The fix is to add '#import "mouse.e"' at the top of the file.


chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Patch to make auto-complete key also do dynamic surround
« Reply #4 on: April 30, 2009, 10:29:09 am »
error: "Expecting procedure name - identifier not found - import may be required".
Indeed.  I have many custom patches in codehelp.e, and I missed the #import dependency when extracting this specific patch to share.  I've updated the original post to include adding the #import.

Phil Barila

  • Senior Community Member
  • Posts: 742
  • Hero Points: 61
Re: Patch to make auto-complete key also do dynamic surround
« Reply #5 on: April 30, 2009, 05:37:51 pm »
@SlickTeam, could you please pull this into the next release?  I love this, but I hate having modified SE macros, because then I have to keep moving the patch to every new release, not to mention the potential issues for hotfixes.  Dynamic surround is a great tool, but only when I need it, and I'm usually a much better judge of when I need it than SE is, in my experience.  Ctrl-Space is also my "Do what I mean" key, I'd very much appreciate it if SE knew that out of the box.  Thanks!  And thanks for the excellent patch, chrisant!  HP++

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Patch to make auto-complete key also do dynamic surround
« Reply #6 on: April 30, 2009, 07:50:32 pm »
Updated the patch (see first post) to add support for #if/#ifdef/#ifndef/#region directives.  I had added that 3 weeks ago in my private copy but forgot to post the update.