Recent

Author Topic: Synedit: highlight string  (Read 841 times)

mh12

  • Newbie
  • Posts: 1
Synedit: highlight string
« on: February 24, 2026, 06:29:34 pm »
What is the easiest way to highlight strings like %%1%%, %%2%% in text in TSynEdit?

Example:
loremipsum%%1%%loremipsumloremipsum%%2%% loremipsum.

AI gave some OnPaintTransient code which was to usable at all.
Then it gave me code with descendant of custom highlighter which used unknown properties and methods.
TSynAnySyn cannot handle situations where the string (i.e. keyword) is not surrounded with spaces.

LV

  • Sr. Member
  • ****
  • Posts: 427
Re: Synedit: highlight string
« Reply #1 on: February 24, 2026, 08:15:44 pm »
What is the easiest way to highlight strings like %%1%%, %%2%% in text in TSynEdit?

Example:
loremipsum%%1%%loremipsumloremipsum%%2%% loremipsum.

AI gave some OnPaintTransient code which was to usable at all.
Then it gave me code with descendant of custom highlighter which used unknown properties and methods.
TSynAnySyn cannot handle situations where the string (i.e. keyword) is not surrounded with spaces.

Maybe something like this

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12206
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit: highlight string
« Reply #2 on: February 24, 2026, 09:32:16 pm »
Probably TSynEditMarkupHighlightAllMulti or TSynEditMarkupHighlightAll

Create an instance, and add to
Code: Pascal  [Select][+][-]
  1. m := TSynEditMarkupHighlightAllMulti.Create(TheSynEdit)
  2. TheSynEdit.MarkupManager.addMarkUp(m);

IIRC (needs to be checked) once added it will be owned (aka destroyed) by the edit.

The markup has AddSearchTerm(s)
and MarkupInfo for the color.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12206
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit: highlight string
« Reply #3 on: February 24, 2026, 09:37:37 pm »
You can also subclass, and implement your own search. (currently it searches all occurrences of search-term)

TSynEditMarkupHighlightAllMulti => many search terms / NO regex
TSynEditMarkupHighlightAll => also basic regex iirc, and multiline

If you subclass you can also just add the locations, but then you need to update them when the text is edited.



In general, you can look at TSynEditMarkupSelection which highlights the selection => you can write a markup from scratch. It will be given all tokens for every line, and can highlight any part of them, whatever you want to do.



It supports back/fore color, frames, font style (italic/bold/underline). And colors can have an alpha value, and priority to compare against other colors that a token may have applied

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12206
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit: highlight string
« Reply #4 on: February 24, 2026, 09:54:00 pm »
If you are looking at something more complex, e.g. context sensitive then you can write your own highlighter.

E.g. If you want to recognise strings and comments, and not HL your matches inside them.

https://wiki.freepascal.org/SynEdit_Highlighter
and sample files in the Lazarus distro.

Note that there will be some API changes to the highlighter in 5.0.

Edson

  • Hero Member
  • *****
  • Posts: 1327
Re: Synedit: highlight string
« Reply #5 on: March 02, 2026, 02:40:03 am »
What is the easiest way to highlight strings like %%1%%, %%2%% in text in TSynEdit?

Example:
loremipsum%%1%%loremipsumloremipsum%%2%% loremipsum.

You can use SynFacilSyn and use a syntax file like:

Code: XML  [Select][+][-]
  1. <Language name="simple">
  2. <String Start="%%" End="%%"/>
  3. </Language>
  4.  
               
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018