Syntax highlight

zhaozj2021-02-08  215

The code in which the code coloring below in the Richedit will be colored with some specified words in the specified color, just like the code editor in Delphi. Procedure CodeColors (Form: TForm; style: string; riche: trichedit;

Invisible: boolean;

Const

// Symbol ...

Codec1: array [0..20] of string = ('#', '$', '(', ')', '*', ',',

'.', '/', ':', ';', '}', '<' {','} ',' <','> ',

'-', '=', ' ', '' '', '@');

// Reserved word ...

Codec2: array [0..44] of string = ('and', 'as',' begin ",

'Case', 'CHAR', 'Class', 'Const', 'Downto',

'else', 'end', 'except', 'finally', 'for',

'Forward', 'Function', 'IF', 'IMPLEMENTATION', 'INTERFACE',

'is', 'nil', 'or', 'private', 'procedure', 'public', 'raise',

'repeat', 'string', 'to', 'try', 'type', 'unit', 'buy', 'var',

'While', 'External', 'Stdcall', 'Do', 'Until', 'Array', 'Of',

'In', 'Shr', 'SHL', 'COS', 'Div';

VAR

FOUNDAT: longint;

StartPOS, ToEnd, I: Integer;

Oldcap, T: String;

FONTC, BACKC, C1, C2, C3, STRC, STRC1: TCOLOR

Begin

Oldcap: = form.caption;

WITH RICHE DO

Begin

Font.name: = 'Courier New';

Font.size: = 10;

IF Wordwrap Then WordWrap: = FALSE;

SelectAll;

SELATTRIBUTES.COLOR: = CLBLACK;

SELATTRIBUTES.Style: = [];

SELSTART: = 0;

IF invisible kil

Begin

Visible: = false;

Form.caption: = 'Executing Code Coloring ...'; END;

END;

BACKC: = CLWHITE; fontc: = clblack;

C1: = CLBLACK; C2: = CLBLACK; C3: = CLBLACK;

Strc: = CLBLUE; STRC1: = CLSILVER;

IF style = 'twilight' Then

Begin

BACKC: = CLBLACK; fontc: = CLWHITEN

C1: = CLLIME; C2: = CLSILVER; C3: = CLAQUA

STRC: = Clyight; strc1: = CLRED;

end

Else

IF style = 'default' Then

Begin

BACKC: = CLWHITE; fontc: = clblack;

C1: = clteal; c2: = clmaroon; c3: = CLBLUE;

Strc: = clmaroon; strc1: = CLSilver;

end

Else

IF style = 'ocean' Then

Begin

BACKC: = $ 00fffff80; fontc: = clblack;

C1: = CLMAROON; C2: = CLBLACK; C3: = CLBLUE;

Strc: = CLTEAL; strc1: = CLBLACK;

end

Else

IF style = 'classic' Then

Begin

BACKC: = Clnavy; fontc: = CLYELLOW;

C1: = CLLIME; C2: = CLSILVER; C3: = CLWHITEN

STRC: = Claqua; strc1: = CLSilver;

end

Else

Begin

WITH RICHE DO

Begin

T: = '{' style '= invalid style [default, clasic, twilight, ocean] only!}';

LINES.INSERT (0, t);

STARTPOS: = 0;

TOEND: = Length (Text) - StartPOS;

Foundat: = FindText (t, startpos, toend, [stwholeword]);

SELSTART: = FOUNDAT;

SELLENGTH: = Length (T);

SELATTRIBUTES.COLOR: = CLRED;

SELATTRIBUTES.Style: = [fsbold];

STARTPOS: = 0;

TOEND: = Length (Text) - StartPOS;

Foundat: = FindText ('Only!', StartPos, ToEnd, [StWholeWord]);

SELSTART: = FOUNDAT;

SELLENGTH: = 4;

SELATTRIBUTES.COLOR: = CLRED;

SELATTRIBUTES.Style: = [fsbold, fsunderline];

END;

END;

Riche.selectall;

Riche.color: = backc; riche.selattributes.color: = fontc;

For i: = 0 to 100 do

Begin

WITH RICHE DO

Begin

STARTPOS: = 0;

TOEND: = Length (Text) - StartPOS;

Foundat: = FindText (INTTOSTR (i), StartPos, Toend, [StWholeWord]);

While (Foundat <> -1) DO

Begin

SELSTART: = FOUNDAT;

SELLENGTH: = Length (INTTOSTR (i));

SELATTRIBUTES.COLOR: = C1;

SELATTRIBUTES.Style: = [];

StartPos: = Foundat Length (INTTOSTR (i));

Foundat: = FindText (INTTOSTR (i), StartPos, Toend, [StWholeWord]);

END;

END;

END;

For i: = 0 to 20 do

Begin

WITH RICHE DO

Begin

STARTPOS: = 0;

TOEND: = Length (Text) - StartPOS;

Foundat: = Findtext (Codec1 [i], startpos, toend, []);

While (Foundat <> -1) DO

Begin

SELSTART: = FOUNDAT;

SELLENGTH: = Length (CODEC1 [I]);

SELATTRIBUTES.COLOR: = C2;

StartPos: = Foundat Length (CODEC1 [i]);

Foundat: = Findtext (Codec1 [i], startpos, toend, []);

END;

END;

END;

For i: = 0 to 44 do

Begin

WITH RICHE DO

Begin

STARTPOS: = 0;

TOEND: = Length (Text) - StartPOS;

Foundat: = FindText (Codec2 [i], StartPos, ToEnd, [StWholeWord]);

While (Foundat <> -1) DO

Begin

SELSTART: = FOUNDAT;

SELLENGTH: = Length (CODEC2 [i]);

SELATTRIBUTES.COLOR: = C3;

SELATTRIBUTES.Style: = [fsbold];

StartPos: = Foundat Length (CODEC2 [I]);

Foundat: = FindText (Codec2 [i], StartPos, ToEnd, [StWholeWord]);

END;

END;

END;

STARTPOS: = 0;

WITH RICHE DO

Begin

Foundat: = Findtext ('' ', StartPos, Length (Text), []);

While Foundat <> -1 DO

Begin

SELSTART: = FOUNDAT;

StartPos: = Foundat 1;

Foundat: = Findtext ('' ', StartPos, Length (Text), []); if Foundat <> -1 THEN

Begin

SELLENGTH: = (Foundat - SELSTART) 1;

SELATTRIBUTES.Style: = [];

SELATTRIBUTES.COLOR: = STRC;

StartPos: = Foundat 1;

Foundat: = Findtext ('' ', StartPos, Length (Text), []);

END;

END;

END;

STARTPOS: = 0;

WITH RICHE DO

Begin

Foundat: = FindText ('{', StartPos, Length (text), []);

While Foundat <> -1 DO

Begin

SELSTART: = FOUNDAT;

StartPos: = Foundat 1;

Foundat: = Findtext ('}', StartPos, Length (text), []);

IF Foundat <> -1 Then

Begin

SELLENGTH: = (Foundat - SELSTART) 1;

SELATTRIBUTES.Style: = [];

SELATTRIBUTES.COLOR: = STRC1;

StartPos: = Foundat 1;

Foundat: = FindText ('{', StartPos, Length (text), []);

END;

END;

END;

IF invisible kil

Begin

Riche.visible: = true;

Form.caption: = Oldcap;

END;

Riche.SELSTART: = 0;

END;

转载请注明原文地址:https://www.9cbs.com/read-2779.html

New Post(0)