M-am apucat sa-mi fac un program care sa formateze un text in formatul Pascal / Delphi (
syntax highlight) ... mai are nevoie de cateva aditii, dar in mare e ok.
Codul e mai jos.
type
TForm1 = class(TForm)
...
procedure SyntaxHighlight(fis : String);
...
private
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.SyntaxHighlight(fis : String);
var line, i, j, st, P, P1, P2, A1, A2 : integer;
Source, Idents : TStringList;
Delimiters : set of char;
S : String;
Com : Boolean;
begin
Source := TStringList.Create;
Source.LoadFromFile(fis);
Idents := TStringList.Create;
Idents.LoadFromFile('idents.txt');
Delimiters := [#0 .. #255];
Delimiters := Delimiters - ['0' .. '9', 'A'..'Z', 'a'..'z'];
Com := False;
With Source Do
Begin
for line := 0 to Count - 1 Do
Begin
RichEdit1.Lines.Add(Strings[line]);
S := LowerCase(Strings[line]);
St := RichEdit1.SelStart - Length(S) - 2;
if S = ''
then Continue;
if Not Com
then
For i := 0 To Idents.Count - 1 do
Begin
P := Pos(Idents[i], S);
If (P > 0) and ((S[P-1] in Delimiters) or (P = 1))
and ((S[P + Length(Idents[i])] in Delimiters) or (P + Length(Idents[i]) - 1 = Length(S))) Then
Begin
RichEdit1.SelStart := St + P - 1;
RichEdit1.SelLength := Length(Idents[i]);
RichEdit1.SelAttributes.Style := [fsBold];
End;
End;
if Not Com
then
begin
P := Pos ('//', S); {needs polishing}
A1 := Pos ('{', S);
end;
Begin
p2 := 1;
if Not Com
then
repeat
p1 := PosEx('''', S, p2);
p2 := PosEx('''', S, p1 + 1);
if (p1 > 0) and (p2 = 0)
then p2 := Length(S);
if (p1 > 0) Then
Begin
RichEdit1.SelStart := St + p1 - 1;
If p2 > 0
then RichEdit1.SelLength := p2 - p1 + 1
else RichEdit1.SelLength := Length(S) - p1 + 1;
RichEdit1.SelAttributes.Color := $7F0000;
RichEdit1.SelAttributes.Style := [];
End
Else Break;
if (P >= p1) and (P <= p2) Then
Begin
P := 0;
End;
if (A1 >= p1) and (A1 <= p2) Then
Begin
A1 := 0;
End;
if (p1 = 0) or (p2 = 0)
then Break;
p2 := p2 + 1;
until False;
If (P > 0) or (A1 > 0) or Com Then
Begin
if P > 0
then
begin
RichEdit1.SelStart := St + P - 1;
RichEdit1.SelLength := Length(S) - P + 1;
RichEdit1.SelAttributes.Style := [fsItalic];
RichEdit1.SelAttributes.Color := $7F0000;
end;
if (A1 > 0) or Com
then
begin
Com := True;
if A1 = 0
then A1 := 1;
A2 := Pos ('}', S);
If A2 = 0
then A2 := Length(S)
else Com := False;
RichEdit1.SelStart := St + A1 - 1;
RichEdit1.SelLength := A2 - A1 + 1;
RichEdit1.SelAttributes.Style := [fsItalic];
RichEdit1.SelAttributes.Color := $7F0000;
A1 := 0;
end;
End;
End;
End;
End;
RichEdit1.SelStart := 0;
RichEdit1.SelLength := 0;
RichEdit1.SetFocus;
end;
Pentru a functiona, in directorul programului e nevoie de fisierul
idents.txt, al carui continut e mai jos:
absolute
abstract
and
array
as
asm
assembler
at
automated
begin
case
cdecl
class
const
constructor
contains
default
destructor
dispid
dispinterface
div
do
downto
dynamic
else
end
except
export
exports
external
far
file
finalization
finally
for
forward
function
goto
if
implementation
implements
in
index
inherited
initialization
inline
interface
is
label
library
message
mod
name
near
nil
nodefault
not
object
of
on
or
out
overload
override
package
packed
pascal
private
procedure
program
property
protected
public
published
raise
read
readonly
record
register
reintroduce
repeat
requires
resident
resourcestring
safecall
set
shl
shr
stdcall
stored
string
then
threadvar
to
try
type
unit
until
uses
var
virtual
while
with
write
writeonly
xor
In program e nevoie de un TRichEdit denumit RichEdit1 (denumirea standard) pe forma Form1 si mai trebuie doar apelata aceasta procedura cu numele complet al fisierului care trebuie formatat. Aceste lucruri pot fi usor schimbate.
Bineinteles, fisierul
idents.txt si codul pot fi modificate pentru a formata texte scrise in alte limbaje. Go for it

Bafta
P.S. un screenshot al unor rezultate:
