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

chrisant

  • Senior Community Member
  • Posts: 1410
  • 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: 2793
  • Hero Points: 347
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: 1410
  • 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: 2793
  • Hero Points: 347
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: 24
  • 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: 303
  • Hero Points: 27
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: 1410
  • 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: 24
  • 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: 1410
  • 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!

dunkers

  • Senior Community Member
  • Posts: 774
  • Hero Points: 36
Re: Highlight matching parens macro
« Reply #11 on: November 27, 2023, 10:16:05 PM »
Don't suppose anyone has fixed this up to work with V28?

dunkers

  • Senior Community Member
  • Posts: 774
  • Hero Points: 36
Re: Highlight matching parens macro
« Reply #12 on: November 28, 2023, 12:27:12 PM »
Has there been a  breaking change to Slick-C, Specifically, with default arguments?

I had a look at what's stopping HighlightParens from working. On loading I get a popup (attached) saying it contains a call to _StreamMarkerRemoveAllType with not enough parameters. Nevertheless I trigger it and sure enough get a Slick-C stack dump, so it's not fibbing.

Looking at the code I see:

Code: [Select]
/**
 * Removes all stream markers that have the specified type.
 *
 * @param type Type of stream marker.
 *
 * @appliesTo Edit_Window, Editor_Control
 *
 * @categories Marker_Functions
 *
 */
extern void _StreamMarkerRemoveAllType(int type,int buf_id=-1);

My understanding, which may not be correct, is the the -1 assigned to buf_id is the default if that parameter isn't actually specified. And, indeed, the SlickEdit help says:
Quote
The assignment operator has special meaning in an argument declaration. It defines a default value for an argument. The default value is used if the caller does not specify the parameter.

So I changed HighlightParens.e, line 101, to read:

Code: [Select]
_StreamMarkerRemoveAllType( s_markerType, -1 );
And now there is no warning on load, no crash and the macro works as it used to. It looks to me like the default parameter isn't being recognised as such.

Dennis

  • Senior Community Member
  • Posts: 3954
  • Hero Points: 515
Re: Highlight matching parens macro
« Reply #13 on: December 08, 2023, 06:49:34 PM »
#include "markers.sh"

dunkers

  • Senior Community Member
  • Posts: 774
  • Hero Points: 36
Re: Highlight matching parens macro
« Reply #14 on: December 08, 2023, 07:04:29 PM »
Duh, of course  ::)

Thanks :)