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

📄 syneditactions.pas

📁 SynEditStudio delphi 代码编辑器
💻 PAS
📖 第 1 页 / 共 5 页
字号:
  if Manager = self then
    Manager := nil;
  FSynEditors.Free;
  FHighlighters.Free;
  inherited;
end;

function TSynCustomManager.GetFilters: string;
var
  i : integer;
  s : string;
begin
  result := FFilter;
  if (Result <> '') and (Result[Length(Result)] <> '|') then
    Result := Result + '|';
  for I := 0 to Highlighters.count-1 do
  begin
    s := Highlighters.Highlighters[i].DefaultFilter;
    if s <> '' then
      result := result + s;
    if (Length(Result)>0) and (Result[Length(Result)] <> '|') then
      Result := Result + '|';
  end;
end;

function TSynCustomManager.GetSynEditorCount: integer;
begin
  result := FSynEditors.Count;
end;

function TSynCustomManager.GetSynEditors(Index: integer): TCustomSynEditor;
begin
  result := TCustomSynEditor(FSynEditors.Objects[Index]);
end;

procedure TSynCustomManager.Load;
begin
  if not (csDesigning in ComponentState) then
  begin
    SetCurrentDir(ExtractFileDir(ParamStr(0)));
    LoadHighlighters(HighlightersPath);
    if EditorSourceIni <> '' then
      LoadEditorSource(EditorSourceIni);
    if AutoCorrectIni <> '' then
      LoadAutoCorrect(AutoCorrectIni);
    {$IFDEF SPELLCHECK}
    if SpellCheckIni <> '' then
      LoadSpellCheck(SpellCheckIni);
    {$ENDIF}
  end;
end;

procedure TSynCustomManager.LoadAutoCorrect(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FAutoCorrect) then
    begin
      FAutoCorrect.LoadFromINI(IniFile, 'Auto Correct');
      fAutoCorrectLoad := true;
    end;
  finally
    IniFile.free;
  end;
end;

procedure TSynCustomManager.LoadEditorSource(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FEditorSource) and Assigned(IniFile) then
    begin
      FEditorSource.LoadFromIni(IniFile, 'Editor Store');
      fEditorSourceLoad := true;
    end;
  finally
    IniFile.free;
  end;
end;

procedure TSynCustomManager.LoadHighlighters(Dir: string);
var
  R : TSearchRec;
  F : Integer;
  I : Integer;

  function FindFileName(str : string): integer;
  begin
    for result := 0 to Highlighters.Count-1 do
      if LowerCase( Highlighters.Items[result].FileName) = LowerCase(str) then
        Exit;
    result := -1;
  end;
var
  XMLDocument : IXMLDocument;
  Ini : TCustomIniFile;
begin
  Dir := ExpandPath(Dir);
  XMLDocument := simpleXML.CreateXmlDocument('', '2.0');
  F := FindFirst(Dir + '*.xml', faDirectory, R);
  INI := nil;
  if FileExists(dir+'syntax.ini') then
    INI := CreateIniFile(dir+'syntax.ini');
//  L := TStringList.Create;
  try
    while F = 0 do
    begin
      I := FindFileName(R.Name);
      if i = -1 then
      begin
        with TSynHighlighterItem(Highlighters.Add) do
        begin
          Highlighter := TSynUniSyn.Create(Self);
          I := Index;
          FileName := R.Name;
        end;
      end;
      with Highlighters.Items[i] do
      begin
        FFullFileName := Dir + FileName;
        FLoad := False;
        if Highlighter is TSynUniSyn then
        begin
          if ini <> nil then
          begin
            TSynUniSyn(Highlighter).Info.General.Name :=
            ini.ReadString('syntax', R.Name +'.Name','');
            TSynUniSyn(Highlighter).Info.General.Extensions :=
            ini.ReadString('syntax', R.Name +'.Extensions','');
          end;
          if TSynUniSyn(Highlighter).Info.General.Name = '' then
          begin
            XMLDocument.Load(FFullFileName);
            with XMLDocument.DocumentElement do
              with EnsureChild('SyntaxColoring') do
                TSynUniFormatNativeXml20.ImportInfo(
                TSynUniSyn(Highlighter).Info, EnsureChild('Info'));
          end;
        end;
//        if FileExists(Dir + FileName) then
//          Highlighter.LoadFromFile(Dir + FileName);
        IF not Assigned(fCodeTemplate) then
          fCodeTemplate := TSynAutoComplete.Create(Self);
        IF not Assigned(fCodeInsight) then
          fCodeInsight := TSynCompletionProposal.Create(Self);
      end;
      F := FindNext(R);
    end;
  finally
    Ini.Free;
    FindClose(R);
  end;
end;

{$IFDEF SPELLCHECK}
procedure TSynCustomManager.LoadSpellCheck(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FSpellCheck) and Assigned(IniFile) then
    begin
      FSpellCheck.LoadFromIni(IniFile, 'Spell Check');
      fSpellCheckLoad := true;
    end;
  finally
    IniFile.free;
  end;
end;
{$ENDIF}

procedure TSynCustomManager.Notification(AComponent: TComponent;
  Operation: TOperation);
var
  I : Integer;
begin
  inherited;
  if (Operation = opRemove) then
    if (AComponent = FSynEditor) then
    begin
      FSynEditor := nil;
      DeleteEditor(TCustomSynEditor(AComponent));
    end else if (AComponent = fAutoCorrect) then
      fAutoCorrect := nil
    else if (AComponent = fSpellCheck) then
      fSpellCheck := nil
    else if (AComponent is TCustomSynEditor) then
      DeleteEditor(TCustomSynEditor(AComponent))
    else if (AComponent = FEditorSource) then
      FEditorSource := nil
    else if (AComponent is TSynCustomHighlighter) then
      Highlighters.RemoveHighlighter(TSynCustomHighlighter(AComponent))
    else if (AComponent is TSynAutoComplete) then
    begin
      for I := 0 to Highlighters.Count - 1 do
        if Highlighters.Items[i].CodeTemplate = AComponent then
          Highlighters.Items[i].CodeTemplate := nil;        
    end
    else if (AComponent is TSynCompletionProposal) then
    begin
      for I := 0 to Highlighters.Count - 1 do
        if Highlighters.Items[i].CodeInsight = AComponent then
          Highlighters.Items[i].CodeInsight := nil;        
    end
end;

procedure TSynCustomManager.Save;
begin
  if not (csDesigning in ComponentState) then
  begin
    SaveHighlighters(HighlightersPath);
    if EditorSourceIni <> '' then
      SaveEditorSource(EditorSourceIni);
    if AutoCorrectIni <> '' then
      SaveAutoCorrect(AutoCorrectIni);
    {$IFDEF SPELLCHECK}
    if SpellCheckIni <> '' then
      SaveSpellCheck(SpellCheckIni);
    {$ENDIF}
  end;
end;

procedure TSynCustomManager.SaveAutoCorrect(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FAutoCorrect) and Assigned(IniFile) then
      FAutoCorrect.SaveToIni(IniFile, 'Auto Correct');
  finally
    IniFile.free;
  end;
end;

procedure TSynCustomManager.SaveEditorSource(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FEditorSource) and Assigned(IniFile) then
      FEditorSource.SaveToIni(IniFile, 'Editor Store');
  finally
    IniFile.free;
  end;
end;

procedure TSynCustomManager.SaveHighlighters(Dir: string);
var
  I : integer;
  N : string;
  J : Integer;
  S : TStrings;
  INI : TCustomIniFile;
begin
  Dir := ExpandPath(Dir);
  INI := CreateIniFile(dir+'syntax.ini');
  try
    for I := 0 to Highlighters.Count-1 do
      with Highlighters.Items[i] do
      begin
        if highlighter is TSynUniSyn then
        begin
          INI.WriteString('syntax', FileName+'.Name',
          TSynUniSyn(Highlighter).Info.General.Name);
          INI.WriteString('syntax', FileName+'.Extensions',
          TSynUniSyn(Highlighter).Info.General.Extensions);
        end;

        if Not FLoad Then continue;

        N := Dir + FileName;
        Highlighter.SaveToFile(ChangeFileExt(N, '.xml'));

        S := TStringList.Create;
        try
          if Assigned(fCodeTemplate) then
          begin
            S.Add('[CodeTemplate]');
            s.AddStrings(CodeTemplate.AutoCompleteList);
            S.Add('');
          end;
          if Assigned(fCodeInsight) then
          begin
            S.Add('[CodeInsightItems]');
            s.AddStrings(CodeInsight.ItemList);
            S.Add('');
            S.Add('[CodeInsightInserts]');
            s.AddStrings(CodeInsight.InsertList);
            S.Add('');
          end;
          S.SaveToFile(ChangeFileExt(N, '.ini'));
        finally
          S.Free;
        end;  // try
      end;
  finally
    ini.Free;
  end;
end;

{$IFDEF SPELLCHECK}
procedure TSynCustomManager.SaveSpellCheck(FileName: string);
var
  IniFile : TCustomIniFile;
begin
  if FileName = '' then exit;
  FileName := ExpandFile(FileName);
  IniFile := CreateIniFile(FileName);
  try
    if Assigned(FSpellCheck) and Assigned(IniFile) then
    begin
      FSpellCheck.SaveToIni(IniFile, 'Spell Check');
      if fSpellCheck.OpenDictionary then
      begin
        fSpellCheck.SaveSkipList(fSpellCheck.UserDirectory +
          fSpellCheck.Language.Name + '.SkipWord.dic');
        fSpellCheck.SaveUserDictionary;
      end;
    end;
  finally
    IniFile.free;
  end;
end;
{$ENDIF}

procedure TSynCustomManager.SetEditorSource(const Value: TSynEditSource);
begin
  if FEditorSource <> nil then
    FEditorSource.RemoveFreeNotification(self);
  FEditorSource := Value;
  if FEditorSource <> nil then
  begin
    if not fEditorSourceLoad and FLoaded and (EditorSourceIni <> '') then
      LoadEditorSource(EditorSourceIni);
    FEditorSource.FreeNotification(self);
  end;
end;

procedure TSynCustomManager.SetAutoCorrect(const Value: TSynAutoCorrect);
begin
  if fAutoCorrect <> nil then
    fAutoCorrect.RemoveFreeNotification(Self);
  fAutoCorrect := value;
  if fAutoCorrect <> nil then
  begin
    if not fAutoCorrectLoad and FLoaded and (AutoCorrectIni <> '') then
      LoadAutoCorrect(AutoCorrectIni);
    fAutoCorrect.FreeNotification(Self);
  end;
end;

procedure TSynCustomManager.SetHighlighters(const Value: TSynHighlighters);
begin
  if value <> nil then
    FHighlighters.Assign(Value);
end;

{$IFDEF SPELLCHECK}
procedure TSynCustomManager.SetSpellCheck(const Value: TSynSpellCheck);
begin
  if fSpellCheck <> nil then
    fSpellCheck.RemoveFreeNotification(Self);
  fSpellCheck := value;
  if fSpellCheck <> nil then
  begin
    if not fSpellCheckLoad and FLoaded and (SpellCheckIni <> '') then
      LoadSpellCheck(SpellCheckIni);
    fSpellCheck.FreeNotification(Self)
  end;
end;
{$ENDIF}

procedure TSynCustomManager.SetSynEditor(const Value: TCustomSynEditor);
begin
  if FSynEditor <> nil then
    FSynEditor.RemoveFreeNotification(self);
  FSynEditor := Value;
  if FSynEditor <> nil then
    FSynEditor.FreeNotification(self);
end;

procedure TSynCustomManager.Loaded;
begin
  inherited;
  FLoaded := true;
  if AutoLoad then
    Load;
end;

procedure TSynCustomManager.BeforeDestruction;
begin
  if AutoSave then
    Save;
  inherited;
end;

{ TaSynSave }

constructor TaSynSave.Create(AOwner: TComponent);
begin
  inherited;
  fSaveDialogOptions := [ofOverwritePrompt, ofHideReadOnly, ofEnableSizing];
end;

procedure TaSynSave.ExecuteTarget(Target: TObject);
var
  Savedialog : TSaveDialog;
begin
  if SynEditAllocated(Target) then
  begin
    savedialog := TSaveDialog.Create(self);
    try
      if (Manager <> nil) then
        savedialog.Filter := Manager.GetFilters;
      savedialog.InitialDir := InitialDir;
      savedialog.FilterIndex := FilterIndex;
      savedialog.Title := Format(fSaveDialogTitle, [FActiveSynEdit.DocumentName]);
      savedialog.Options := fSaveDialogOptions;
      if save(SaveDialog) then
      begin
        InitialDir := savedialog.Initial

⌨️ 快捷键说明

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