Author Topic: Highlight matching parens macro  (Read 11862 times)

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Highlight matching parens macro
« on: April 09, 2008, 12:40:06 am »
Here is a macro that uses different colors to highlights pairs of matching parentheses on/near the current line, and extending outwards trying to find all related pairs of parentheses that appear to be part of the same expression.  I find this macro handy on long/complex expressions, maybe you will too.

Bind a key to the HighlightParens command (I happen to use the Ctrl-0 key).

Invoking the command repeatedly extends the color coding to cover the text between the parens, starting from the innermost level of nested parens and with each subsequent invocation progressively working its way to the outermost level of parens.  Mismatched parens are highlighted in red.

The timeout for the highlights, the max distance to scan for matching parens, and the colors can all be modified near the top of the macro file.

Graeme

  • Senior Community Member
  • Posts: 1985
  • Hero Points: 227
Re: Highlight matching parens macro
« Reply #1 on: April 09, 2008, 12:22:09 pm »
Nice work.

What's the idea of stream markers?  I notice if I turn on the parentheses coloring, then insert some lines before the highlighted line, the coloring gets removed correctly.  Is that the main benefit of stream markers compared with remembering line/column positions and using set-text-color?

How did you find out that stream markers could be used for this?  I guess line markers are for putting a bitmap in the margin.

Graeme

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Highlight matching parens macro
« Reply #2 on: April 09, 2008, 05:48:44 pm »
Stream markers is how all of the built in macro code I've been reading colors text (search, isearch, regex evaluator, dynamic surround, the built in as-you-type paren matching highlights).  I don't remember for sure how I first found stream markers, I think it was from examining the search/isearch highlights.  I don't know yet whether syntax color coding uses them.

Stream markers move as the text they are attached to move, and they overlay the underlying color rather than replacing it.  Both make it much easier to remove the coloring properly afterwards, but they also mean you can overlay your color only once and let the rendering engine take it from there - e.g. no need to reapply it yourself if text moves or if the window gets repainted etc.

I don't yet know if you can apply only a foreground or only a background color.  The cursor line coloring seems to only affect the background color, and it alpha blends with the selection color, so I suspect there may be a way to set foreground-only, background-only, or perhaps alpha blended colors.

Graeme

  • Senior Community Member
  • Posts: 1985
  • Hero Points: 227
Re: Highlight matching parens macro
« Reply #3 on: April 09, 2008, 08:41:50 pm »
Thanks chrisant.  Lisa should add this information to a "stream/line markers overview" topic in the macro user guide.

Graeme

Lisa

  • Senior Community Member
  • Posts: 238
  • Hero Points: 23
  • User-friendly geek-speak translator extraordinaire
Re: Highlight matching parens macro
« Reply #4 on: April 09, 2008, 08:51:36 pm »
Thanks chrisant.  Lisa should add this information to a "stream/line markers overview" topic in the macro user guide.

Graeme


Thanks Graeme. I've added this to the todo list for the Slick-C docs.

asandler

  • Senior Community Member
  • Posts: 263
  • Hero Points: 23
Re: Highlight matching parens macro
« Reply #5 on: May 27, 2008, 03:17:22 pm »
@chrisant Excellent macro! Very useful. With your permission, I'd like to post it on my macros for SE web-page.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Highlight matching parens macro
« Reply #6 on: May 27, 2008, 11:26:00 pm »
Sure, that's a good idea.

rdsuel

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Highlight matching parens macro
« Reply #7 on: June 12, 2008, 01:25:37 pm »
I'd like to try this macro, but when I try to load it, Slick complains about the "loop" statements.  I'm using v11.0.2, is "loop" a new Slick-C command in SlickEdit 2008 or something?

-rdsuel

Lisa

  • Senior Community Member
  • Posts: 238
  • Hero Points: 23
  • User-friendly geek-speak translator extraordinaire
Re: Highlight matching parens macro
« Reply #8 on: June 12, 2008, 01:39:47 pm »
I'd like to try this macro, but when I try to load it, Slick complains about the "loop" statements.  I'm using v11.0.2, is "loop" a new Slick-C command in SlickEdit 2008 or something?

-rdsuel

This was another statement added in SlickEdit 2008 (v13). Here is the documentation that will be in the upcoming release:

The generic loop statement is similar to that found in the Ada and D languages. The following statements are equivalent:

Code: [Select]
for(;;) { ... }

loop { ... }

Example:
Code: [Select]
status := search(":","@"); 
loop {
   if ( status ) break;
   get_line(line);
   messageNwait("found match line="line);
   status = repeat_search();
}

Another example:
Code: [Select]
defmain()
{
   i:=0;
   j:=0;
   loop {
      say("test, i="i);
      i++;
      if (i>1000) {
         break;
      }
      inner: loop {
         say("defmain: j="j);
         if (j++ > 750) break inner;
      }
      say("defmain: H1");
      if (i < 500) {
         continue;
      }
      say("defmain: H2");
   }
}

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: Highlight matching parens macro
« Reply #9 on: June 12, 2008, 04:13:36 pm »
Attached a new version of the macro which uses "while(true)" instead of "loop".

rdsuel

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Highlight matching parens macro
« Reply #10 on: June 16, 2008, 02:42:33 pm »
Thanks  ... nice macro!