5

I have SynMultiSyn Highlighter (synHTMLcomplex) consisting of HTML (synHTML) and JavaScript (synJScript) like below:

synHTML       : TSynHTMLSyn;
synJScript    : TSynJScriptSyn;
synHTMLcomplex: TSynMultiSyn;

// HTML complex
with SynHTMLcomplex do begin
  DefaultLanguageName:= 'HTML complex';
  DefaultFilter      := 'HTML complex (*.html; *.htm)|*.html; *.htm';
  DefaultHighlighter := synHTML;
  with Schemes do begin
    Add.Index:= 0;
    with Items[0] do begin
      Highlighter:= synJScript;
      SchemeName := 'JavaScript';
      StartExpr  := '<script';
      EndExpr    := '</script>';
      with MarkerAttri do
        Background:= clNone;
    end;
  end;
end;

Once in a instace of SynEdit, is there any way (property or method) to know which Highlighter (synHTML or synJScript) the cursor (CaretY) is?

Anyone can help please?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
jcfaria
  • 312
  • 3
  • 14
  • I've used this component before, but a long time ago, and I'm not at my IDE right now but I'm sure the answer is yes, just not sure exactly how. – Jerry Dodge Sep 19 '13 at 01:17

1 Answers1

0

It is possible but not very easy to achieve.

The function below is an adaptation of the function used by Jan Fiala (author of PSPad editor). For me it is working fine. Many thanks Jan!

function TfrmEditor.GetCurrentHighLighter: TSynCustomHighlighter;
var
  i       : integer;
  seEditor: TSynEdit;

begin
  if (sActiveEditor = 'synEditor') then seEditor:= synEditor
                                   else seEditor:= synEditor2;

  with seEditor do
    if Highlighter is TSynMultiSyn then
    begin
      i:= (Integer(TSynEditStringList(Lines).Ranges[CaretY - 1]) and $F) - 1;

      if (i < 0) then
        result:= TSynMultiSyn(Highlighter).DefaultHighLighter
      else
        result:= TSynMultiSyn(Highlighter).Schemes[i].Highlighter
    end
    else
      result:= Highlighter;
end;

The original function of Jan is below, as soon as the explanation.

" - This is my function for return current highlighter:

function TPSSynEdit.GetCurrentHighLighter: TSynCustomHighlighter;
var
  i: Integer;
begin
  if Highlighter is TSynMultiSyn then
  begin
    i := (Integer(TSynEditStringList(Lines).Ranges[CaretY - 1]) and $F) - 2;
    if (i > TSynMultiSyn(HighLighter).Schemes.Count - 1) or (i < 0) then
      Result := TSynMultiSyn(Highlighter).DefaultHighLighter
    else
      Result := TSynMultiSyn(Highlighter).Schemes[i].Highlighter
  end
  else
    Result := Highlighter;
end;

I have this function in descendant of TSynedit. You need to replace highlighter e.g. with Editor.Highlighter and Lines with Editor.Lines or send editor as function parameter and add after begin something like: with Editor do begin ..."

Jan Fiala

jcfaria
  • 312
  • 3
  • 14