Hello,
I'm working on a project that needs syntax hylighting. It needs to highlite some keywords. What I did so far was, OnChange of TRichEdit, convert the entire line where the user is typing into black and 'Courier New'. Then I look for the keyword on the line and set it's color to blue. The problem is, (i.e. the syntax word is 'word') if I type "thisword" it will become "thisword" and I want it to become highlited only if there isn't any alphanumerical (A-Z; a-z; 0-9) character before and after the text. How do I do that? This is the code used so far:
P.S.: For the curious, it is an IDE. ThanQ, ;-)
JVFF (Janito Vaqueiro Ferreira Filho)
I'm working on a project that needs syntax hylighting. It needs to highlite some keywords. What I did so far was, OnChange of TRichEdit, convert the entire line where the user is typing into black and 'Courier New'. Then I look for the keyword on the line and set it's color to blue. The problem is, (i.e. the syntax word is 'word') if I type "thisword" it will become "thisword" and I want it to become highlited only if there isn't any alphanumerical (A-Z; a-z; 0-9) character before and after the text. How do I do that? This is the code used so far:
Code:
procedure TfrmMain.richCodeChange(Sender: TObject);
var
currentPos: TPoint;
currentLine: string;
selStart: Integer;
selLength: Integer;
begin
currentPos := richCode.CaretPos;
richCode.Lines.Strings[currentPos.Y] := '!@#$%^&*()' +
richCode.Lines.Strings[currentPos.Y];
selStart := richCode.FindText('!@#$%^&*()', 0,
length(richCode.Text), []);
selLength := selStart +
Length(richCode.Lines.Strings[currentPos.Y]);
currentLine := richCode.Lines.Strings[currentPos.Y];
delete(currentLine, 1, 10);
richCode.Lines.Strings[currentPos.Y] := currentLine;
richCode.selStart := selStart;
richCode.selLength := selLength - 10;
richCode.selAttributes.Color := clBlack;
richCode.selAttributes.Name := 'Courier New';
if pos('int', richCode.Lines.Strings[currentPos.Y]) <> 0
then begin
richCode.selStart := richCode.findText('int', selStart,
length(richCode.Lines.Strings[currentPos.Y]), []);
richCode.selLength := 3;
richCode.SelAttributes.Color := clBlue;
richCode.selLength := 0;
end;
richCode.CaretPos := CurrentPos;
end;
P.S.: For the curious, it is an IDE. ThanQ, ;-)
JVFF (Janito Vaqueiro Ferreira Filho)