SlickEdit Product Discussion > SlickEdit User Macros

Patch to make auto-complete key also do dynamic surround

(1/2) > >>

chrisant:
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: ---#import "mouse.e"

--- End code ---
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: ---_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()) {

--- End code ---

hs2:
Way cool chrisant ! And yes - it's fine even with good old v12...
Thanks and HP++,
HS2

chrisant:
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:
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:

--- Quote from: dunkers on April 30, 2009, 02:31:21 AM ---error: "Expecting procedure name - identifier not found - import may be required".

--- End quote ---
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.

Navigation

[0] Message Index

[#] Next page

Go to full version