Author Topic: Key bindings for color coding  (Read 1813 times)

sigmund

  • Community Member
  • Posts: 88
  • Hero Points: 9
Key bindings for color coding
« on: October 16, 2016, 09:43:39 pm »
I frequently add/remove symbols for color coding, and in SlickEdit v19 I had macros that automatically could update the user.vlx file for doing this.

With SlickEdit v21 this color coding information is stored in the user.cfg.xml file.  This is all fine and good, the macros actually got a lot easier now.  However, I had to do some hacking in ccode.e to make it work.  It would be great if ccode.e could be updated so that these hacks are not required.  Here is what I had to do:

1. Move the CLEXDEF struct out to a new ccode.sh header file.

2. Change the following functions to not be static:
  • _init_clexdef()
  • SaveLexer()
  • LoadLexer()

Here are my color coding macros that I then can bind to keys:

Code: [Select]
_command
vusrmacs_colorCode1() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('punctuation');
}

_command
vusrmacs_colorCode2() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('libkeywords');
}

_command
vusrmacs_colorCode3() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('operators');
}

_command
vusrmacs_colorCode4() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('userkeywords');
}

_command
vusrmacs_colorCode5() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('keywords');
}

static void
vusrmacs_colorCode(
    _str colorName)
{
    int result;
    _str newSymbol = cur_word(result);

    CLEXDEF clexdef;
    _init_clexdef(clexdef);

    LoadLexer(p_lexer_name, clexdef);

    _str colorNames[];
    colorNames :+= 'punctuation';
    colorNames :+= 'libkeywords';
    colorNames :+= 'operators';
    colorNames :+= 'userkeywords';
    colorNames :+= 'keywords';

    int i;
    for (i = 0; i < colorNames._length(); i++)
    {
        STRARRAYPTR array;

        if      (i == 0) array = &clexdef.symbol1;
        else if (i == 1) array = &clexdef.symbol2;
        else if (i == 2) array = &clexdef.symbol3;
        else if (i == 3) array = &clexdef.symbol4;
        else             array = &clexdef.keywords;

        int j, deleteIndex = -1;
        for (j = 0; j < array->_length(); j++)
        {
            if (newSymbol == (*array)[j])
            {
                deleteIndex = j;
                break;
            }
        }

        if (deleteIndex >= 0)
        {
            array->_deleteel(deleteIndex);
        }
        else if (colorName == colorNames[i])
        {
            array->_insertel(newSymbol, 0);
        }
    }

    SaveLexer(p_lexer_name, clexdef);

    _clex_load(p_lexer_name);

    cursor_down();
}

It would be great if something like this could be added to SlickEdit itself.

sigmund

  • Community Member
  • Posts: 88
  • Hero Points: 9
Re: Key bindings for color coding
« Reply #1 on: April 01, 2018, 12:40:09 am »
I upgraded from SlickEdit v21 to v22 and now my color coding macros broke.  Can someone from the SlickEdit team please help me make them work again?  It is way too slow to use the GUI to add tokens, I don't understand why Slick does not ship with macros for this.

Here are my macros that worked great for v21:

Code: [Select]
_command
vusrmacs_colorCode1() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('punctuation');
}

_command
vusrmacs_colorCode2() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('libkeywords');
}

_command
vusrmacs_colorCode3() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('operators');
}

_command
vusrmacs_colorCode4() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('userkeywords');
}

_command
vusrmacs_colorCode5() name_info(',')
{
    _macro('R',1);
    vusrmacs_colorCode('keywords');
}

static void
vusrmacs_colorCode(
    _str colorName)
{
    int result;
    _str newSymbol = cur_word(result);

    CLEXDEF clexdef;
    _init_clexdef(clexdef);

    LoadLexer(p_lexer_name, clexdef);

    _str colorNames[];
    colorNames :+= 'punctuation';
    colorNames :+= 'libkeywords';
    colorNames :+= 'operators';
    colorNames :+= 'userkeywords';
    colorNames :+= 'keywords';

    int i;
    for (i = 0; i < colorNames._length(); i++)
    {
        STRARRAYPTR array;

        if      (i == 0) array = &clexdef.symbol1;
        else if (i == 1) array = &clexdef.symbol2;
        else if (i == 2) array = &clexdef.symbol3;
        else if (i == 3) array = &clexdef.symbol4;
        else             array = &clexdef.keywords;

        int j, deleteIndex = -1;
        for (j = 0; j < array->_length(); j++)
        {
            if (newSymbol == (*array)[j])
            {
                deleteIndex = j;
                break;
            }
        }

        if (deleteIndex >= 0)
        {
            array->_deleteel(deleteIndex);
        }
        else if (colorName == colorNames[i])
        {
            array->_insertel(newSymbol, 0);
        }
    }

    SaveLexer(p_lexer_name, clexdef);

    _clex_load(p_lexer_name);

    cursor_down();
}

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 5553
  • Hero Points: 449
Re: Key bindings for color coding
« Reply #2 on: April 01, 2018, 03:37:21 pm »
Try the attached macro file.
« Last Edit: April 01, 2018, 03:39:08 pm by Clark »

sigmund

  • Community Member
  • Posts: 88
  • Hero Points: 9
Re: Key bindings for color coding
« Reply #3 on: April 01, 2018, 04:57:31 pm »
Fantastic, this works great  :D

I attached a slightly modified version that removes the color coding for a token if it already was color coded using the same color coding type.  This is for instance useful when a symbol gets renamed, in which case one wants to remove the color coding for the old token.

Thank you so much for your help!
« Last Edit: April 01, 2018, 04:59:37 pm by sigmund »

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 5553
  • Hero Points: 449
Re: Key bindings for color coding
« Reply #4 on: April 01, 2018, 06:30:51 pm »
Oops...I didn't add '$' ad the end of the regex. Here's a fix (includes your changes).

sigmund

  • Community Member
  • Posts: 88
  • Hero Points: 9
Re: Key bindings for color coding
« Reply #5 on: April 01, 2018, 08:24:05 pm »
Perfect, thanks!