Author Topic: Color-coding with finer granularity  (Read 14382 times)

Margaret

  • Community Member
  • Posts: 70
  • Hero Points: 0
Color-coding with finer granularity
« on: May 27, 2008, 06:02:22 PM »
I'd like to color-code within strings (e.g., use a separate color for embedded tokens within php strings to make them easier to see).  Can someone give me a pointer to where output coloration is handled?  Thanks!

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Color-coding with finer granularity
« Reply #1 on: May 29, 2008, 09:50:39 AM »
I'd like to color-code within strings (e.g., use a separate color for embedded tokens within php strings to make them easier to see).  Can someone give me a pointer to where output coloration is handled?  Thanks!

I guess it's handled in the slick core rather than slick c because it needs to be as fast as possible, along with the parsing.

You might be able to do it using either _SetTextColor or stream markers.  I haven't used _SetTextColor but I guess it's persistent otherwise there wouldn't be any point in having it.  I don't know if it persists across invocations of slick or across closing and opening of files - I doubt if it does, but you can always refresh the text color using the slick _switchbuf and _add_buffer call-lists or on demand after you've changed a string.

So, a possibility is to go to the start of the file and scan through it, finding all the strings and coloring parts of them.  Here's an outline - untested except for cursor_right_with_line_wrap().  I don't know how well this would work but it's worth a try.

Graeme

Code: [Select]
my_color_for_php_strings()
{
   _str ch;
   top();
   while(1)
   {
      // search forwards for the start of a string
      if (search('?','r@Cs'))
         return;
      while (_in_string()) {
         // parse, looking for whatever, then color code
         ch = get_text();
         if (ch == '$') {
            // whatever
         }
         if (cursor_right_with_line_wrap() != 0)
              return;
      }
   }
}

_command int cursor_right_with_line_wrap()
{
   int col, line;
   col = p_col;
   line = p_line;

   // change > to >= in the following line to avoid landing on newline char
   if (p_col > _text_colc()) {
      if (down(1,1) == 0)
         _begin_line();
      else
         // end of file
         return -1;
   }
   else
      right();
   
   if (line == p_line && col == p_col) {
      return -1;
   }
   return 0;
}



Margaret

  • Community Member
  • Posts: 70
  • Hero Points: 0
Re: Color-coding with finer granularity
« Reply #2 on: May 29, 2008, 11:47:45 AM »
I appreciate the response.  I strongly suspected it might be in the core, but was hoping I was wrong and simply not seeing the routines, or that the location had changed after v9 (my version).  Bugger.

So I think I need to go find the wishlist:  alias expansion even within an already-populated line, and the ability to define colors for within-string tokens.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Color-coding with finer granularity
« Reply #3 on: May 29, 2008, 06:19:12 PM »
@Margaret:  Graeme's reply shows a fairly simple way to achieve it today, without needing to wait for built in support.  It doesn't account for updating the color coding as you type, but you could achieve that with a timer, and perhaps only parsing the lines that are currently visible.

On a related note, does anyone know if there is some kind of _beforecommand_ and/or _aftercommand_ hook?  I have a growing number of small modifications to the C/C++ as-you-type formatting engine (which I intend to share with the SlickTeam when the modifications are finished), and to finish porting some of the as-you-type smarts I had written in another editor, I really need some kind of _beforecommand_ and _aftercommand_ hooks.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
before_command hook Was : Color-coding with finer granularity
« Reply #4 on: May 30, 2008, 12:12:59 PM »
@Margaret:  Graeme's reply shows a fairly simple way to achieve it today, without needing to wait for built in support.  It doesn't account for updating the color coding as you type, but you could achieve that with a timer, and perhaps only parsing the lines that are currently visible.

On a related note, does anyone know if there is some kind of _beforecommand_ and/or _aftercommand_ hook?  I have a growing number of small modifications to the C/C++ as-you-type formatting engine (which I intend to share with the SlickTeam when the modifications are finished), and to finish porting some of the as-you-type smarts I had written in another editor, I really need some kind of _beforecommand_ and _aftercommand_ hooks.

Maybe replace_name.  It's not clear to me from the help how it works so I don't know if this would do what you want.  I have no idea when references to function/ command names get resolved but replace name *might* allow you to rename an existing_name to some_new_name, then load your own version of existing_name that called some_new_name.

If you have maintenance & support, I think "support" includes help with macro programming problems.

Graeme

MartyL

  • Senior Community Member
  • Posts: 166
  • Hero Points: 29
  • Synergex
Re: Color-coding with finer granularity
« Reply #5 on: June 04, 2008, 05:00:39 PM »
On a related note, does anyone know if there is some kind of _beforecommand_ and/or _aftercommand_ hook?  I have a growing number of small modifications to the C/C++ as-you-type formatting engine (which I intend to share with the SlickTeam when the modifications are finished), and to finish porting some of the as-you-type smarts I had written in another editor, I really need some kind of _beforecommand_ and _aftercommand_ hooks.

I don't know about _beforecommand_, but the closest thing I know of in Slick-C to _aftercommand_ would be _post_call().

Code: [Select]
/**
 * Posts a calls to the Slick-C® function corresponding to name table
 * index, proc_index, and optional passes the first argument, arg1.  The
 * call is made after the editor returns to a message loop.  This function
 * should only be used under circumstances where an operation can not be
 * performed immediately.  The dialog editor uses this function to display
 * error message boxes during on_lost_focus events.  During the
 * on_got_focus and on_lost_focus events you can not display a dialog box.
 * Use find_index to get an index to a global function.
 *
 * @param pfnCallback         pointer to function to call
 * @param CallbackArgument    argument to pass to function
 *
 * @categories Miscellaneous_Functions
 */
int _post_call(typeless pfnCallback, _str CallbackArgument=null)

If you're trying to hook a function in the one of the DLLs, I don't think this will be much use to you.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Color-coding with finer granularity
« Reply #6 on: June 04, 2008, 06:10:26 PM »
It sounds like it enqueues a one-time deferred call.  That's a handy function to know about in general, thanks (HP++).  For my needs at the moment, it won't help me much without a way to have a hook get called immediately prior to every editing command.  I'll certainly make use of _post_call sooner or later, though!

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Color-coding with finer granularity
« Reply #7 on: June 27, 2008, 12:23:35 AM »
...Only two weeks later and now I had a need for _post_call:

I've written a macro that highlights "todo" style comments.  It uses a timer every 200 milliseconds, which checks p_LastModified to quickly bail out if no work needs to be done (on my 2GHz machine it adds no measurable CPU load; ProcExp says SE is using less than 0.01% CPU while idle, even if I add a say('tick') in the timer proc).  It needs to _set_timer in definit, but that seems to lead to needing to rebuild the vslick.sta file.  ;)  But it seems to work fine if definit uses _post_call to enqueue a deferred call to do the real initialization work.

I'll post the highlighter macro in a week or two (in a new topic) after I've addressed some unnecessary redundant calls in my code and let it "bake" a while.

Dan112123

  • Community Member
  • Posts: 44
  • Hero Points: 2
Re: Color-coding with finer granularity
« Reply #8 on: July 02, 2008, 06:33:18 PM »
...Only two weeks later and now I had a need for _post_call:

I've written a macro that highlights "todo" style comments.  It uses a timer every 200 milliseconds, which checks p_LastModified to quickly bail out if no work needs to be done (on my 2GHz machine it adds no measurable CPU load; ProcExp says SE is using less than 0.01% CPU while idle, even if I add a say('tick') in the timer proc).  It needs to _set_timer in definit, but that seems to lead to needing to rebuild the vslick.sta file.  ;)  But it seems to work fine if definit uses _post_call to enqueue a deferred call to do the real initialization work.

I'll post the highlighter macro in a week or two (in a new topic) after I've addressed some unnecessary redundant calls in my code and let it "bake" a while.

Did you have a chance to work on this macro? If you post it somewhere please edit this thread and post a link to the new thread.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Color-coding with finer granularity
« Reply #9 on: July 02, 2008, 08:59:21 PM »
I was distracted by preparing for a large presention I gave this morning.  After I catch up on the stuff I let slide, I'll be able to finish up the macro.  Stability seems solid, but I want to eliminate some wasted redundant processing before posting it.  I'll post it as a new topic, and also reply here to add a cross link.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Color-coding with finer granularity
« Reply #10 on: July 03, 2008, 11:46:43 PM »
The highlighter macro is available here.