📄 syneditmiscprocs.pas
字号:
begin
SetLength(Result, Length(s)); // worst case
j := 0;
i := 1;
while i <= Length(s) do begin
Inc(j);
if s[i] = '\' then begin
Inc(i);
if s[i] = '\' then
Result[j] := '\'
else
Result[j] := '/';
end else
Result[j] := s[i];
Inc(i);
end; //for
SetLength(Result,j);
end; { DecodeString }
{$IFDEF RestoreRangeChecking}{$R+}{$ENDIF}
{$IFNDEF SYN_COMPILER_5_UP}
procedure FreeAndNil(var Obj);
var
P: TObject;
begin
P := TObject(Obj);
TObject(Obj) := nil;
P.Free;
end;
{$ENDIF}
{$IFNDEF SYN_COMPILER_3_UP}
procedure Assert(Expr: Boolean); { stub for Delphi 2 }
begin
end;
{$ENDIF}
{$IFNDEF SYN_COMPILER_3_UP}
function LastDelimiter(const Delimiters, S: string): Integer;
var
P: PChar;
begin
Result := Length(S);
P := PChar(Delimiters);
while Result > 0 do
begin
if (S[Result] <> #0) and (StrScan(P, S[Result]) <> nil) then
{$IFDEF SYN_MBCSSUPPORT}
if (ByteType(S, Result) = mbTrailByte) then
Dec(Result)
else
{$ENDIF}
Exit;
{$IFDEF SYN_KYLIX}
begin
if (ByteType(S, Result) <> mbTrailByte) then
Exit;
Dec(Result);
while ByteType(S, Result) = mbTrailByte do
Dec(Result);
end;
{$ENDIF}
Dec(Result);
end;
end;
{$ENDIF}
{$IFNDEF SYN_COMPILER_4_UP}
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
var
SearchStr, Patt, NewStr: string;
Offset: Integer;
begin
if rfIgnoreCase in Flags then
begin
SearchStr := AnsiUpperCase(S);
Patt := AnsiUpperCase(OldPattern);
end else
begin
SearchStr := S;
Patt := OldPattern;
end;
NewStr := S;
Result := '';
while SearchStr <> '' do
begin
{$IFDEF SYN_COMPILER_3_UP}
Offset := AnsiPos(Patt, SearchStr);
{$ELSE}
Offset := Pos(Patt, SearchStr); // Pos does not support MBCS
{$ENDIF}
if Offset = 0 then
begin
Result := Result + NewStr;
Break;
end;
Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
if not (rfReplaceAll in Flags) then
begin
Result := Result + NewStr;
Break;
end;
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
end;
end;
{$ENDIF}
{$IFDEF SYN_CLX}
type
TColorRec = packed record
Blue: Byte;
Green: Byte;
Red: Byte;
Unused: Byte;
end;
function GetRValue(RGBValue: TColor): byte;
begin
Result := TColorRec(RGBValue).Red;
end;
function GetGValue(RGBValue: TColor): byte;
begin
Result := TColorRec(RGBValue).Green;
end;
function GetBValue(RGBValue: TColor): byte;
begin
Result := TColorRec(RGBValue).Blue;
end;
function RGB(r, g, b: Byte): Cardinal;
begin
Result := (r or (g shl 8) or (b shl 16));
end;
{$ENDIF}
{$IFDEF SYN_MBCSSUPPORT}
function IsStringType(Value: Word): TStringType;
begin
Result := stNone;
(*** Controls ***)
if (Value = C3_SYMBOL) then
Result := stControl
(*** singlebyte ***)
else if ((Value and C3_HALFWIDTH) <> 0) then
begin
if (Value = C3_HALFWIDTH) or (Value = (C3_ALPHA or C3_HALFWIDTH)) then
Result := stHalfNumAlpha { Number & Alphabet }
else if ((Value and C3_SYMBOL) <> 0) or ((Value and C3_LEXICAL) <> 0) then
Result := stHalfSymbol { Symbol }
else if ((Value and C3_KATAKANA) <> 0) then
Result := stHalfKatakana; { Japanese-KATAKANA }
end
(*** doublebyte ***)
else begin
if (Value = C3_FULLWIDTH) or (Value = (C3_ALPHA or C3_FULLWIDTH)) then
Result := stWideNumAlpha { Number & Alphabet }
else if ((Value and C3_SYMBOL) <> 0) or ((Value and C3_LEXICAL) <> 0) then
Result := stWideSymbol { Symbol }
else if ((Value and C3_KATAKANA) <> 0) then
Result := stWideKatakana { Japanese-KATAKANA }
else if ((Value and C3_HIRAGANA) <> 0) then
Result := stHiragana { Japanese-HIRAGANA }
else if ((Value and C3_IDEOGRAPH) <> 0) then
Result := stIdeograph; { Ideograph }
end;
end;
{$ENDIF}
function DeleteTypePrefixAndSynSuffix(S: string): string;
begin
Result := S;
if Result[1] in ['T', 't'] then //ClassName is never empty so no AV possible
if Pos('tsyn', LowerCase(Result)) = 1 then
Delete(Result, 1, 4)
else
Delete(Result, 1, 1);
if Copy(LowerCase(Result), Length(Result) - 2, 3) = 'syn' then
SetLength(Result, Length(Result) - 3);
end;
function GetHighlighterIndex(Highlighter: TSynCustomHighlighter;
HighlighterList: TList): Integer;
var
i: Integer;
begin
Result := 1;
for i := 0 to HighlighterList.Count - 1 do
if HighlighterList[i] = Highlighter then
Exit
else if Assigned(HighlighterList[i]) and (TObject(HighlighterList[i]).ClassType = Highlighter.ClassType) then
inc(Result);
end;
function InternalEnumHighlighterAttris(Highlighter: TSynCustomHighlighter;
SkipDuplicates: Boolean; HighlighterAttriProc: THighlighterAttriProc;
Params: array of Pointer; HighlighterList: TList): Boolean;
var
i: Integer;
UniqueAttriName: string;
begin
Result := True;
if (HighlighterList.IndexOf(Highlighter) >= 0) then
begin
if SkipDuplicates then Exit;
end
else
HighlighterList.Add(Highlighter);
if Highlighter is TSynMultiSyn then
with TSynMultiSyn(Highlighter) do
begin
Result := InternalEnumHighlighterAttris(DefaultHighlighter, SkipDuplicates,
HighlighterAttriProc, Params, HighlighterList);
if not Result then Exit;
for i := 0 to Schemes.Count - 1 do
begin
UniqueAttriName := DeleteTypePrefixAndSynSuffix(Highlighter.ClassName) +
IntToStr(GetHighlighterIndex(Highlighter, HighlighterList)) + '.' +
Schemes[i].MarkerAttri.Name + IntToStr(i + 1);
Result := HighlighterAttriProc(Highlighter, Schemes[i].MarkerAttri,
UniqueAttriName, Params);
if not Result then Exit;
Result := InternalEnumHighlighterAttris(Schemes[i].Highlighter,
SkipDuplicates, HighlighterAttriProc, Params, HighlighterList);
if not Result then Exit
end
end
else if Assigned(Highlighter) then
for i := 0 to Highlighter.AttrCount - 1 do
begin
UniqueAttriName := DeleteTypePrefixAndSynSuffix(Highlighter.ClassName) +
IntToStr(GetHighlighterIndex(Highlighter, HighlighterList)) + '.' +
Highlighter.Attribute[i].Name;
Result := HighlighterAttriProc(Highlighter, Highlighter.Attribute[i],
UniqueAttriName, Params);
if not Result then Exit
end
end;
function EnumHighlighterAttris(Highlighter: TSynCustomHighlighter;
SkipDuplicates: Boolean; HighlighterAttriProc: THighlighterAttriProc;
Params: array of Pointer): Boolean;
var
HighlighterList: TList;
begin
if not Assigned(Highlighter) or not Assigned(HighlighterAttriProc) then
begin
Result := False;
Exit;
end;
HighlighterList := TList.Create;
try
Result := InternalEnumHighlighterAttris(Highlighter, SkipDuplicates,
HighlighterAttriProc, Params, HighlighterList)
finally
HighlighterList.Free
end
end;
{$IFDEF SYN_HEREDOC}
// Fast Frame Check Sequence (FCS) Implementation
// Translated from sample code given with RFC 1171 by Marko Njezic
const
fcstab : array[Byte] of Word = (
$0000, $1189, $2312, $329b, $4624, $57ad, $6536, $74bf,
$8c48, $9dc1, $af5a, $bed3, $ca6c, $dbe5, $e97e, $f8f7,
$1081, $0108, $3393, $221a, $56a5, $472c, $75b7, $643e,
$9cc9, $8d40, $bfdb, $ae52, $daed, $cb64, $f9ff, $e876,
$2102, $308b, $0210, $1399, $6726, $76af, $4434, $55bd,
$ad4a, $bcc3, $8e58, $9fd1, $eb6e, $fae7, $c87c, $d9f5,
$3183, $200a, $1291, $0318, $77a7, $662e, $54b5, $453c,
$bdcb, $ac42, $9ed9, $8f50, $fbef, $ea66, $d8fd, $c974,
$4204, $538d, $6116, $709f, $0420, $15a9, $2732, $36bb,
$ce4c, $dfc5, $ed5e, $fcd7, $8868, $99e1, $ab7a, $baf3,
$5285, $430c, $7197, $601e, $14a1, $0528, $37b3, $263a,
$decd, $cf44, $fddf, $ec56, $98e9, $8960, $bbfb, $aa72,
$6306, $728f, $4014, $519d, $2522, $34ab, $0630, $17b9,
$ef4e, $fec7, $cc5c, $ddd5, $a96a, $b8e3, $8a78, $9bf1,
$7387, $620e, $5095, $411c, $35a3, $242a, $16b1, $0738,
$ffcf, $ee46, $dcdd, $cd54, $b9eb, $a862, $9af9, $8b70,
$8408, $9581, $a71a, $b693, $c22c, $d3a5, $e13e, $f0b7,
$0840, $19c9, $2b52, $3adb, $4e64, $5fed, $6d76, $7cff,
$9489, $8500, $b79b, $a612, $d2ad, $c324, $f1bf, $e036,
$18c1, $0948, $3bd3, $2a5a, $5ee5, $4f6c, $7df7, $6c7e,
$a50a, $b483, $8618, $9791, $e32e, $f2a7, $c03c, $d1b5,
$2942, $38cb, $0a50, $1bd9, $6f66, $7eef, $4c74, $5dfd,
$b58b, $a402, $9699, $8710, $f3af, $e226, $d0bd, $c134,
$39c3, $284a, $1ad1, $0b58, $7fe7, $6e6e, $5cf5, $4d7c,
$c60c, $d785, $e51e, $f497, $8028, $91a1, $a33a, $b2b3,
$4a44, $5bcd, $6956, $78df, $0c60, $1de9, $2f72, $3efb,
$d68d, $c704, $f59f, $e416, $90a9, $8120, $b3bb, $a232,
$5ac5, $4b4c, $79d7, $685e, $1ce1, $0d68, $3ff3, $2e7a,
$e70e, $f687, $c41c, $d595, $a12a, $b0a3, $8238, $93b1,
$6b46, $7acf, $4854, $59dd, $2d62, $3ceb, $0e70, $1ff9,
$f78f, $e606, $d49d, $c514, $b1ab, $a022, $92b9, $8330,
$7bc7, $6a4e, $58d5, $495c, $3de3, $2c6a, $1ef1, $0f78
);
function CalcFCS(const ABuf; ABufSize: Cardinal): Word;
var
CurFCS: Word;
P: ^Byte;
begin
CurFCS := $ffff;
P := @ABuf;
while ABufSize <> 0 do
begin
CurFCS := (CurFCS shr 8) xor fcstab[(CurFCS xor P^) and $ff];
Dec(ABufSize);
Inc(P);
end;
Result := CurFCS;
end;
{$ENDIF}
procedure SynDrawGradient(const ACanvas: TCanvas; const AStartColor, AEndColor: TColor;
ASteps: integer; const ARect: TRect; const AHorizontal: boolean);
var
StartColorR, StartColorG, StartColorB : byte;
DiffColorR, DiffColorG, DiffColorB : integer;
i, Size : integer;
PaintRect: TRect;
begin
StartColorR := GetRValue(ColorToRGB(AStartColor));
StartColorG := GetGValue(ColorToRGB(AStartColor));
StartColorB := GetBValue(ColorToRGB(AStartColor));
DiffColorR := GetRValue(ColorToRGB(AEndColor)) - StartColorR;
DiffColorG := GetGValue(ColorToRGB(AEndColor)) - StartColorG;
DiffColorB := GetBValue(ColorToRGB(AEndColor)) - StartColorB;
ASteps := MinMax(ASteps, 2, 256);
if AHorizontal then
begin
Size := ARect.Right - ARect.Left;
PaintRect.Top := ARect.Top;
PaintRect.Bottom := ARect.Bottom;
for i := 0 to ASteps - 1 do
begin
PaintRect.Left := ARect.Left + MulDiv(i, Size, ASteps);
PaintRect.Right := ARect.Left + MulDiv(i + 1, Size, ASteps);
ACanvas.Brush.Color := RGB(StartColorR + MulDiv(i, DiffColorR, ASteps - 1),
StartColorG + MulDiv(i, DiffColorG, ASteps - 1),
StartColorB + MulDiv(i, DiffColorB, ASteps - 1));
ACanvas.FillRect(PaintRect);
end;
end
else
begin
Size := ARect.Bottom - ARect.Top;
PaintRect.Left := ARect.Left;
PaintRect.Right := ARect.Right;
for i := 0 to ASteps - 1 do
begin
PaintRect.Top := ARect.Top + MulDiv(i, Size, ASteps);
PaintRect.Bottom := ARect.Top + MulDiv(i + 1, Size, ASteps);
ACanvas.Brush.Color := RGB(StartColorR + MulDiv(i, DiffColorR, ASteps - 1),
StartColorG + MulDiv(i, DiffColorG, ASteps - 1),
StartColorB + MulDiv(i, DiffColorB, ASteps - 1));
ACanvas.FillRect(PaintRect);
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -