[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bracket Matching in SynEdit using Delphi 7

Posted on 2007-07-27
3
Medium Priority
?
1,406 Views
Last Modified: 2008-01-09
How do I perform "Bracket Matching" in SynEdit using delphi 7?
0
Comment
Question by:Ioannis Anifantakis
  • 2
3 Comments
 
LVL 10

Expert Comment

by:dinilud
ID: 19582195

see "OnPaintTransientDemo" example in SynEdit Demo Folder
0
 
LVL 10

Accepted Solution

by:
dinilud earned 2000 total points
ID: 19582224
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  SynEditHighlighter, SynHighlighterPython, SynEditPythonBehaviour, SynEdit,
  SynHighlighterJava, StdCtrls, ExtCtrls, SynEditTypes;

type
  TForm1 = class(TForm)
    Editor: TSynEdit;
    SynJavaSyn1: TSynJavaSyn;
    Panel1: TPanel;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    procedure EditorPaintTransient(Sender: TObject; Canvas: TCanvas;
      TransientType: TTransientType);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FBracketFG: TColor;
    FBracketBG: TColor;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.EditorPaintTransient(Sender: TObject; Canvas: TCanvas;
  TransientType: TTransientType);
const AllBrackets = ['{','[','(','<','}',']',')','>'];
var Editor: TSynEdit;
    OpenChars: array[0..2] of Char;
    CloseChars: array[0..2] of Char;

  function CharToPixels(P: TBufferCoord): TPoint;
  begin
    Result:=Editor.RowColumnToPixels(Editor.BufferToDisplayPos(P));
  end;

var P: TBufferCoord;
    Pix: TPoint;
    D     : TDisplayCoord;
    S: String;
    I: Integer;
    Attri: TSynHighlighterAttributes;
    start: Integer;
    TmpCharA, TmpCharB: Char;
begin
  if TSynEdit(Sender).SelAvail then exit;
  Editor := TSynEdit(Sender);
//if you had a highlighter that used a markup language, like html or xml, then you would want to highlight
//the greater and less than signs as well as illustrated below

//  if (Editor.Highlighter = shHTML) or (Editor.Highlighter = shXML) then
//    inc(ArrayLength);

  for i := 0 to 2 do
    case i of
      0: begin OpenChars[i] := '('; CloseChars[i] := ')'; end;
      1: begin OpenChars[i] := '{'; CloseChars[i] := '}'; end;
      2: begin OpenChars[i] := '['; CloseChars[i] := ']'; end;
      3: begin OpenChars[i] := '<'; CloseChars[i] := '>'; end;
    end;

  P := Editor.CaretXY;
  D := Editor.DisplayXY;

  Start := Editor.SelStart;

  if (Start > 0) and (Start <= length(Editor.Text)) then
    TmpCharA := Editor.Text[Start]
  else TmpCharA := #0;

  if (Start < length(Editor.Text)) then
    TmpCharB := Editor.Text[Start + 1]
  else TmpCharB := #0;

  if not(TmpCharA in AllBrackets) and not(TmpCharB in AllBrackets) then exit;
  S := TmpCharB;
  if not(TmpCharB in AllBrackets) then
  begin
    P.Char := P.Char - 1;
    S := TmpCharA;
  end;
  Editor.GetHighlighterAttriAtRowCol(P, S, Attri);

  if (Editor.Highlighter.SymbolAttribute = Attri) then
  begin
    for i := low(OpenChars) to High(OpenChars) do
    begin
      if (S = OpenChars[i]) or (S = CloseChars[i]) then
      begin
        Pix := CharToPixels(P);

        Editor.Canvas.Brush.Style := bsSolid;//Clear;
        Editor.Canvas.Font.Assign(Editor.Font);
        Editor.Canvas.Font.Style := Attri.Style;

        if (TransientType = ttAfter) then
        begin
          Editor.Canvas.Font.Color := FBracketFG;
          Editor.Canvas.Brush.Color := FBracketBG;
        end else begin
          Editor.Canvas.Font.Color := Attri.Foreground;
          Editor.Canvas.Brush.Color := Attri.Background;
        end;
        if Editor.Canvas.Font.Color = clNone then
          Editor.Canvas.Font.Color := Editor.Font.Color;
        if Editor.Canvas.Brush.Color = clNone then
          Editor.Canvas.Brush.Color := Editor.Color;

        Editor.Canvas.TextOut(Pix.X, Pix.Y, S);
        P := Editor.GetMatchingBracketEx(P);

        if (P.Char > 0) and (P.Line > 0) then
        begin
          Pix := CharToPixels(P);
          if Pix.X > Editor.Gutter.Width then
          begin
            if S = OpenChars[i] then
              Editor.Canvas.TextOut(Pix.X, Pix.Y, CloseChars[i])
            else Editor.Canvas.TextOut(Pix.X, Pix.Y, OpenChars[i]);
          end;
        end;
      end; //if
    end;//for i :=
    Editor.Canvas.Brush.Style := bsSolid;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Editor.Lines.LoadFromFile(OpenDialog1.Filename);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Editor.Text := SynJavaSyn1.SampleSource;
  FBracketFG := clRed;
  FBracketBG := clNone;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SynJavaSyn1.Enabled := not(SynJavaSyn1.Enabled);
  Editor.Repaint;
end;

end.
0
 
LVL 6

Author Comment

by:Ioannis Anifantakis
ID: 19591276
here's an approach of mine, is not as good as the expert's one,
if the user press ")" the closing bracket, it finds the matched open bracket
and redirects twice back and forth the cursor to show the matching.
The cursor is in "blobk" mode and not in "vertical line" mode while it shows matching and during this time it does not allow editing as this would cause problems.  Editing is released when matching is finished.

Defenintelly not as good as the above but yet another much simpler approach


procedure TCompareFieldsFRM.editorKeyPress(Sender: TObject; var Key: Char);
begin
    application.ProcessMessages;
    if key=')' then begin
        bracketLoop:=0;
        bracketTimer.Enabled:=true;
    end;
end;

procedure TCompareFieldsFRM.bracketTimerTimer(Sender: TObject);
begin
    if bracketLoop=0 then begin
        editor.CaretX:=editor.CaretX-1;
        editor.ReadOnly:=true;
        editor.InsertCaret:=ctBlock;
    end;
    if bracketLoop<4 then begin
        editor.FindMatchingBracket;
        inc(bracketLoop);
    end else begin
        editor.ReadOnly:=false;
        editor.CaretX:=editor.CaretX+1;
        editor.InsertCaret:=ctVerticalLine;
        bracketTimer.Enabled:=false;
    end;
end;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question