📄 syneditmiscprocs.pas
字号:
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynEditMiscProcs.pas, released 2000-04-07.
The Original Code is based on the mwSupportProcs.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Michael Hieke.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynEditMiscProcs.pas,v 1.38 2005/01/02 16:51:03 markonjezic Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
{$IFNDEF QSYNEDITMISCPROCS}
unit SynEditMiscProcs;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
Types,
kTextDrawer,
QGraphics,
QSynEditTypes,
QSynEditHighlighter,
{$ELSE}
Windows,
Graphics,
SynEditTypes,
SynEditHighlighter,
{$ENDIF}
{$IFDEF SYN_COMPILER_4_UP}
Math,
{$ENDIF}
Classes;
type
PIntArray = ^TIntArray;
TIntArray = array[0..MaxListSize - 1] of integer;
{$IFNDEF SYN_COMPILER_4_UP}
function Max(x, y: integer): integer;
function Min(x, y: integer): integer;
{$ENDIF}
function MinMax(x, mi, ma: integer): integer;
procedure SwapInt(var l, r: integer);
function MaxPoint(const P1, P2: TPoint): TPoint;
function MinPoint(const P1, P2: TPoint): TPoint;
function GetIntArray(Count: Cardinal; InitialValue: integer): PIntArray;
{$IFNDEF SYN_CLX}
procedure InternalFillRect(dc: HDC; const rcPaint: TRect);
{$ENDIF}
// Converting tabs to spaces: To use the function several times it's better
// to use a function pointer that is set to the fastest conversion function.
type
TConvertTabsProc = function(const Line: AnsiString;
TabWidth: integer): AnsiString;
function GetBestConvertTabsProc(TabWidth: integer): TConvertTabsProc;
// This is the slowest conversion function which can handle TabWidth <> 2^n.
function ConvertTabs(const Line: AnsiString; TabWidth: integer): AnsiString;
type
TConvertTabsProcEx = function(const Line: AnsiString; TabWidth: integer;
var HasTabs: boolean): AnsiString;
function GetBestConvertTabsProcEx(TabWidth: integer): TConvertTabsProcEx;
// This is the slowest conversion function which can handle TabWidth <> 2^n.
function ConvertTabsEx(const Line: AnsiString; TabWidth: integer;
var HasTabs: boolean): AnsiString;
function GetExpandedLength(const aStr: string; aTabWidth: integer): integer;
function CharIndex2CaretPos(Index, TabWidth: integer;
const Line: string): integer;
function CaretPos2CharIndex(Position, TabWidth: integer; const Line: string;
var InsideTabChar: boolean): integer;
// search for the first char of set AChars in Line, starting at index Start
function StrScanForCharInSet(const Line: string; Start: integer;
AChars: TSynIdentChars): integer;
// the same, but searching backwards
function StrRScanForCharInSet(const Line: string; Start: integer;
AChars: TSynIdentChars): integer;
{$IFDEF SYN_MBCSSUPPORT}
type
TStringType = (stNone, stHalfNumAlpha, stHalfSymbol, stHalfKatakana,
stWideNumAlpha, stWideSymbol, stWideKatakana, stHiragana, stIdeograph,
stControl, stKashida);
{$IFNDEF SYN_COMPILER_4_UP}
const
C3_NONSPACING = 1; { nonspacing character }
C3_DIACRITIC = 2; { diacritic mark }
C3_VOWELMARK = 4; { vowel mark }
C3_SYMBOL = 8; { symbols }
C3_KATAKANA = $0010; { katakana character }
C3_HIRAGANA = $0020; { hiragana character }
C3_HALFWIDTH = $0040; { half width character }
C3_FULLWIDTH = $0080; { full width character }
C3_IDEOGRAPH = $0100; { ideographic character }
C3_KASHIDA = $0200; { Arabic kashida character }
C3_LEXICAL = $0400; { lexical character }
C3_ALPHA = $8000; { any linguistic char (C1_ALPHA) }
C3_NOTAPPLICABLE = 0; { ctype 3 is not applicable }
{$ENDIF}
// search for first multibyte char in Line, starting at index Start
function StrScanForMultiByteChar(const Line: string; Start: Integer): Integer;
// the same, but searching backwards
function StrRScanForMultiByteChar(const Line: string; Start: Integer): Integer;
// convert a type returned from GetStringTypeEx() to something friendlier
function IsStringType(Value: Word): TStringType;
{$ENDIF}
function GetEOL(Line: PChar): PChar;
// Remove all '/' characters from string by changing them into '\.'.
// Change all '\' characters into '\\' to allow for unique decoding.
function EncodeString(s: string): string;
// Decodes string, encoded with EncodeString.
function DecodeString(s: string): string;
{$IFNDEF SYN_COMPILER_5_UP}
procedure FreeAndNil(var Obj);
{$ENDIF}
{$IFNDEF SYN_COMPILER_3_UP}
procedure Assert(Expr: Boolean); { stub for Delphi 2 }
{$ENDIF}
{$IFNDEF SYN_COMPILER_3_UP}
function LastDelimiter(const Delimiters, S: string): Integer;
{$ENDIF}
{$IFNDEF SYN_COMPILER_4_UP}
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
{$ENDIF}
{$IFDEF SYN_CLX}
function GetRValue(RGBValue: TColor): byte;
function GetGValue(RGBValue: TColor): byte;
function GetBValue(RGBValue: TColor): byte;
function RGB(r, g, b: Byte): Cardinal;
{$ENDIF}
type
THighlighterAttriProc = function (Highlighter: TSynCustomHighlighter;
Attri: TSynHighlighterAttributes; UniqueAttriName: string;
Params: array of Pointer): Boolean of object;
// Enums all child highlighters and their attributes of a TSynMultiSyn through a
// callback function.
// This function also handles nested TSynMultiSyns including their MarkerAttri.
function EnumHighlighterAttris(Highlighter: TSynCustomHighlighter;
SkipDuplicates: Boolean; HighlighterAttriProc: THighlighterAttriProc;
Params: array of Pointer): Boolean;
{$IFDEF SYN_HEREDOC}
// Calculates Frame Check Sequence (FCS) 16-bit Checksum (as defined in RFC 1171)
function CalcFCS(const ABuf; ABufSize: Cardinal): Word;
{$ENDIF}
procedure SynDrawGradient(const ACanvas: TCanvas; const AStartColor, AEndColor: TColor;
ASteps: integer; const ARect: TRect; const AHorizontal: boolean);
function ExpandPath(Path : string): string;
function ExpandFile(Name : string): string;
implementation
uses
SysUtils,
{$IFDEF SYN_CLX}
QSynHighlighterMulti;
{$ELSE}
SynHighlighterMulti;
{$ENDIF}
{***}
{$IFNDEF SYN_COMPILER_4_UP}
function Max(x, y: integer): integer;
begin
if x > y then Result := x else Result := y;
end;
function Min(x, y: integer): integer;
begin
if x < y then Result := x else Result := y;
end;
{$ENDIF}
function ExpandPath(Path : string): string;
var
dir : string;
begin
result := Path;
dir := ExtractFilePath(ParamStr(0));
if ((Length(result) > 1) and (result[2] <> ':')) or (Length(result) =1) then
begin
if (result[1] = '\') then
System.Delete(result, 1, 1);
result := dir + result;
end;
if (Length(result)>0) and (result[Length(result)] <> '\') then
result := result + '\';
end;
function ExpandFile(Name : string): string;
var
dir : string;
begin
result := Name;
dir := ExtractFilePath(ParamStr(0));
if ((Length(result) > 1) and (result[2] <> ':')) or (Length(result) =1) then
begin
if (result[1] = '\') then
System.Delete(result, 1, 1);
result := dir + result;
end;
end;
function MinMax(x, mi, ma: integer): integer;
begin
x := Min( x, ma );
Result := Max( x, mi );
end;
procedure SwapInt(var l, r: integer);
var
tmp: integer;
begin
tmp := r;
r := l;
l := tmp;
end;
function MaxPoint(const P1, P2: TPoint): TPoint;
begin
if (P2.y > P1.y) or ((P2.y = P1.y) and (P2.x > P1.x)) then
Result := P2
else
Result := P1;
end;
function MinPoint(const P1, P2: TPoint): TPoint;
begin
if (P2.y < P1.y) or ((P2.y = P1.y) and (P2.x < P1.x)) then
Result := P2
else
Result := P1;
end;
{***}
function GetIntArray(Count: Cardinal; InitialValue: integer): PIntArray;
var
p: PInteger;
begin
Result := AllocMem(Count * SizeOf(integer));
if Assigned(Result) and (InitialValue <> 0) then begin
p := PInteger(Result);
while (Count > 0) do begin
p^ := InitialValue;
Inc(p);
Dec(Count);
end;
end;
end;
{$IFNDEF SYN_CLX}
procedure InternalFillRect(dc: HDC; const rcPaint: TRect);
begin
ExtTextOut(dc, 0, 0, ETO_OPAQUE, @rcPaint, nil, 0, nil);
end;
{$ENDIF}
{***}
// Don't change this function; no stack frame and efficient register use.
function GetHasTabs(pLine: PChar; var CharsBefore: integer): boolean;
begin
CharsBefore := 0;
if Assigned(pLine) then begin
while (pLine^ <> #0) do begin
if (pLine^ = #9) then break;
Inc(CharsBefore);
Inc(pLine);
end;
Result := (pLine^ = #9);
end else
Result := FALSE;
end;
function ConvertTabs1Ex(const Line: AnsiString; TabWidth: integer;
var HasTabs: boolean): AnsiString;
var
pDest: PChar;
nBeforeTab: integer;
begin
Result := Line; // increment reference count only
if GetHasTabs(pointer(Line), nBeforeTab) then begin
HasTabs := TRUE;
pDest := @Result[nBeforeTab + 1]; // this will make a copy of Line
// We have at least one tab in the string, and the tab width is 1.
// pDest points to the first tab char. We overwrite all tabs with spaces.
repeat
if (pDest^ = #9) then pDest^ := ' ';
Inc(pDest);
until (pDest^ = #0);
end else
HasTabs := FALSE;
end;
function ConvertTabs1(const Line: AnsiString; TabWidth: integer): AnsiString;
var
HasTabs: boolean;
begin
Result := ConvertTabs1Ex(Line, TabWidth, HasTabs);
end;
function ConvertTabs2nEx(const Line: AnsiString; TabWidth: integer;
var HasTabs: boolean): AnsiString;
var
i, DestLen, TabCount, TabMask: integer;
pSrc, pDest: PChar;
begin
Result := Line; // increment reference count only
if GetHasTabs(pointer(Line), DestLen) then begin
HasTabs := TRUE;
pSrc := @Line[1 + DestLen];
// We have at least one tab in the string, and the tab width equals 2^n.
// pSrc points to the first tab char in Line. We get the number of tabs
// and the length of the expanded string now.
TabCount := 0;
TabMask := (TabWidth - 1) xor $7FFFFFFF;
repeat
if (pSrc^ = #9) then begin
DestLen := (DestLen + TabWidth) and TabMask;
Inc(TabCount);
end else
Inc(DestLen);
Inc(pSrc);
until (pSrc^ = #0);
// Set the length of the expanded string.
SetLength(Result, DestLen);
DestLen := 0;
pSrc := PChar(Line);
pDest := PChar(Result);
// We use another TabMask here to get the difference to 2^n.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -