⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 Delphi编写的一个支持语法高亮显示和很多语言的文本编辑器
💻 PAS
字号:
unit Unit1;

interface

uses
   Windows,
   Messages,
   SysUtils,
   Classes,
   Dialogs,
   Forms,
   upluginutils,
   fdoc,
   uPlugin, strUtils;

Type tag = record
    text: String;
    start: Integer;
    length: Integer;
end;

type

  TuilPlugin1 = class(TuilPlugin)
    procedure uilPlugin1Commands0Execute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    frm: TfrmDoc;
  end;

function RegisterPlugin : TuilPlugin1; stdcall;

implementation

{$R *.DFM}

// IMPORTANT NOTE: If you change the name of the Plugin container,
// you must set the type below to the same type. (Delphi changes
// the declaration, but not the procedure itself. Both the return
// type and the type created must be the same as the declared type above.
function RegisterPlugin : TuilPlugin1;
begin
  Result := TuilPlugin1.Create(nil);
end;

function InStr(sStart: integer; const sData: string; const
    sFind: string): integer;
    var
      c: integer;
    label
      SkipFind;
    begin
      c := sStart - 1;
      repeat
        if c > length(sData) then
        begin
          c := 0;
          goto SkipFind;
        end;
        inc(c);
      until copy(sData, c, length(sFind)) = sFind;
    SkipFind:
      Result := c;
end;

Function ReplaceSubString(str: String; substr: String; newsubstr: String): String;
var
    pst: Integer;
    startPos: Integer;
    new_str: String;
begin
    startPos := 1;
    pst := Instr(0, str, substr);


    While pst > 0 do begin
        new_str := new_str + MidStr(str, startPos, pst - startPos) + newsubstr;
        startPos := pst + StrLen(PChar(substr));
        pst := Instr(startPos, str, substr);
    end;
    new_str := new_str + MidStr(str, startPos, StrLen(PChar(str)));
    result := new_str;
end;

Function Clean(targ: String): String;
begin
    targ := ReplaceSubString(targ, ' >', '>');
    targ := ReplaceSubString(targ, '< ', '<');
    targ := ReplaceSubString(targ, '> <', '><');
    result := targ;
end;

function String2(num: Integer; str: String): String;
var
  newstr: String;
  i: Integer;
begin
  for i := 1 to num do begin
    newstr := newstr + str;
  end;
  result := newstr;
end;


Function returnNextTag(str: String; start: Integer): tag;
var
   endPos: Integer;
   res: tag;
begin
    start := InStr(start + 1, str, '<');
    endPos := InStr(start + 1, str, '>');
    res.text := MidStr(str, start, endPos - start + 1);
    res.start := start;
    res.length := endPos - start;
    result := res;
end;


Function returnNameOfTag(str: tag): String;
var
    endPos: Integer;
    start: Integer;
begin
    start := 2;
    endPos := InStr(1, str.text, ' ');


    If MidStr(str.text, 2, 3) = '!--' Then
        endPos := 5
    Else If endPos = 0 Then
        endPos := InStr(1, str.text, '>');
    result := MidStr(str.text, start, endPos - start)
End;

Function Eformat(str: String): String;
var
     startPos: Integer;
     endPos: Integer;
     indentationLevel: Integer;
     new_str: String;
     tmp_str: PChar;
     tagName: String;
     tempEnd: Integer;
begin
    indentationLevel := 0;
    startPos := 0;
    endPos := 0;

    If (MidStr(str, 1, 1) <> '<') Then begin

        tempEnd := InStr(1, str, '<');


        If tempEnd = 0 Then
            tempEnd := StrLen(PChar(str));
        new_str := MidStr(str, 1, tempEnd);
    End;
    repeat

            If (InStr(startPos + 1, str, '</') <> 0) And (InStr(startPos + 1, str, '</') <= InStr(startPos + 1, str, '<')) Then begin
                startPos := InStr(startPos + 1, str, '</');
                endPos := InStr(startPos + 1, str, '<');


                If endPos = 0 Then
                    endPos := StrLen(PChar(str)) + 1;
                indentationLevel := indentationLevel - 1;
                new_str := new_str + AnsiString(#10) + String2(indentationLevel, '   ') + MidStr(str, startPos, endPos - startPos);
            end
            Else begin
                startPos := InStr(startPos + 1, str, '<');
                endPos := InStr(startPos + 1, str, '<');


                If endPos = 0 Then
                    endPos := StrLen(Pchar(str)) + 1;
                new_str := new_str + AnsiString(#10) + String2(indentationLevel, '   ') + MidStr(str, startPos, endPos - startPos);

                tagName := LowerCase(returnNameOfTag(returnNextTag(str, startPos)));


                If (tagName <> 'br') And (tagName <> 'hr') And (tagName <> 'img') And (tagName <> 'meta') And (tagName <> 'applet') And (tagName <> 'p') And (tagName <> '!--') And (tagName <> 'input') And (tagName <> '!doctype') And (tagName <> 'area') Then
                    indentationLevel := indentationLevel + 1;

            end;
        until startpos = 0;
        new_str := MidStr(New_Str, 2, StrLen(PChar(New_Str))-2);
        result := new_str;
end;

Function HierarchalFormat(target: String): String;
begin
target := ReplaceSubString(target, chr(10),'');
target := ReplaceSubString(target, chr(9),'');
target := Eformat(target);
target := Clean(target);
result := target;
End;

procedure TuilPlugin1.uilPlugin1Commands0Execute(Sender: TObject);
var
  s: String;
begin
  frm := getseldoc;
  if frm = nil then exit;
  s :=HierarchalFormat(frm.scimain.text);
  frm.scimain.text := s
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -