ソースを参照

Implement loading/saving includes history + some other tweaks.

Martijn Laan 5 年 前
コミット
e122e84e84
3 ファイル変更136 行追加31 行削除
  1. 75 31
      Projects/CompForm.pas
  2. 58 0
      Projects/CompFunc.pas
  3. 3 0
      Projects/CompScintEdit.pas

+ 75 - 31
Projects/CompForm.pas

@@ -371,7 +371,7 @@ type
     function InitializeMemo(const Memo: TCompScintEdit; const PopupMenu: TPopupMenu): TCompScintEdit;
     function InitializeMemo(const Memo: TCompScintEdit; const PopupMenu: TPopupMenu): TCompScintEdit;
     procedure InitiateAutoComplete(const Key: AnsiChar);
     procedure InitiateAutoComplete(const Key: AnsiChar);
     procedure InvalidateStatusPanel(const Index: Integer);
     procedure InvalidateStatusPanel(const Index: Integer);
-    procedure LoadKnownIncludedFiles;
+    procedure LoadKnownIncludedFilesAndUpdateMemos(const AFilename: String);
     procedure MemoChange(Sender: TObject; const Info: TScintEditChangeInfo);
     procedure MemoChange(Sender: TObject; const Info: TScintEditChangeInfo);
     procedure MemoCharAdded(Sender: TObject; Ch: AnsiChar);
     procedure MemoCharAdded(Sender: TObject; Ch: AnsiChar);
     procedure MainMemoDropFiles(Sender: TObject; X, Y: Integer; AFiles: TStrings);
     procedure MainMemoDropFiles(Sender: TObject; X, Y: Integer; AFiles: TStrings);
@@ -397,7 +397,7 @@ type
     procedure ResetAllMemosLineState;
     procedure ResetAllMemosLineState;
     procedure StartProcess;
     procedure StartProcess;
     function SaveFile(const AMemo: TCompScintEdit; const SaveAs: Boolean): Boolean;
     function SaveFile(const AMemo: TCompScintEdit; const SaveAs: Boolean): Boolean;
-    procedure SaveKnownIncludedFiles;
+    procedure SaveKnownIncludedFiles(const AFilename: String);
     procedure SetErrorLine(const AMemo: TCompScintEdit; const ALine: Integer);
     procedure SetErrorLine(const AMemo: TCompScintEdit; const ALine: Integer);
     procedure SetStatusPanelVisible(const AVisible: Boolean);
     procedure SetStatusPanelVisible(const AVisible: Boolean);
     procedure SetStepLine(const AMemo: TCompScintEdit; ALine: Integer);
     procedure SetStepLine(const AMemo: TCompScintEdit; ALine: Integer);
@@ -515,7 +515,7 @@ begin
   Memo.AutoCompleteFontName := Font.Name;
   Memo.AutoCompleteFontName := Font.Name;
   Memo.AutoCompleteFontSize := Font.Size;
   Memo.AutoCompleteFontSize := Font.Size;
   Memo.CodePage := CP_UTF8;
   Memo.CodePage := CP_UTF8;
-  Memo.CompilerFileIndex := -2; { Just some invalid value, should never be seen while running }
+  Memo.CompilerFileIndex := UnknownCompilerFileIndex;
   Memo.ErrorLine := -1;
   Memo.ErrorLine := -1;
   Memo.Font.Name := 'Courier New';
   Memo.Font.Name := 'Courier New';
   Memo.Font.Size := 10;
   Memo.Font.Size := 10;
@@ -946,16 +946,64 @@ begin
   FMainMemo.ClearUndo;
   FMainMemo.ClearUndo;
 end;
 end;
 
 
-procedure TCompileForm.LoadKnownIncludedFiles;
+procedure TCompileForm.LoadKnownIncludedFilesAndUpdateMemos(const AFilename: String);
+var
+  Strings: TStringList;
+  IncludedFile: TIncludedFile;
+  I: Integer;
 begin
 begin
-  {todo}
+  if FIncludedFiles.Count <> 0 then
+    raise Exception.Create('FIncludedFiles.Count <> 0'); { NewMainFile should have been called }
 
 
-  UpdateIncludedFilesMemos;
+  try
+    if AFilename <> '' then begin
+      Strings := TStringList.Create;
+      try
+        LoadKnownIncludedFiles(AFilename, Strings);
+        if Strings.Count > 0 then begin
+          try
+            for I := 0 to Strings.Count-1 do begin
+              IncludedFile := TIncludedFile.Create;
+              IncludedFile.Filename := Strings[I];
+              IncludedFile.CompilerFileIndex := UnknownCompilerFileIndex;
+              IncludedFile.HasLastWriteTime := GetLastWriteTimeOfFile(IncludedFile.Filename,
+                @IncludedFile.LastWriteTime);
+              FIncludedFiles.Add(IncludedFile);
+            end;
+          finally
+            UpdateIncludedFilesMemos;
+          end;
+        end;
+      finally
+        Strings.Free;
+      end;
+    end;
+  except
+    { Ignore any exceptions. }
+  end;
 end;
 end;
 
 
-procedure TCompileForm.SaveKnownIncludedFiles;
+procedure TCompileForm.SaveKnownIncludedFiles(const AFilename: String);
+var
+  Strings: TStringList;
+  IncludedFile: TIncludedFile;
 begin
 begin
-  {todo}
+  try
+    if AFilename <> '' then begin
+      Strings := TStringList.Create;
+      try
+        for IncludedFile in FIncludedFiles do
+          Strings.Add(IncludedFile.Filename);
+        CompFunc.SaveKnownIncludedFiles(AFilename, Strings);
+      finally
+        Strings.Free;
+      end;
+    end;
+  except
+    { Handle exceptions locally; failure to save the includes list should not be
+      a fatal error. }
+    Application.HandleException(Self);
+  end;
 end;
 end;
 
 
 procedure TCompileForm.NewMainFileUsingWizard;
 procedure TCompileForm.NewMainFileUsingWizard;
@@ -985,7 +1033,6 @@ begin
       NewMainFile;
       NewMainFile;
       FMainMemo.Lines.Text := WizardForm.ResultScript;
       FMainMemo.Lines.Text := WizardForm.ResultScript;
       FMainMemo.ClearUndo;
       FMainMemo.ClearUndo;
-      LoadKnownIncludedFiles;
       if WizardForm.Result = wrComplete then begin
       if WizardForm.Result = wrComplete then begin
         FMainMemo.ForceModifiedState;
         FMainMemo.ForceModifiedState;
         if MsgBox('Would you like to compile the new script now?', SCompilerFormCaption, mbConfirmation, MB_YESNO) = IDYES then
         if MsgBox('Would you like to compile the new script now?', SCompilerFormCaption, mbConfirmation, MB_YESNO) = IDYES then
@@ -1033,7 +1080,7 @@ begin
     ModifyMRUMainFilesList(AFilename, True);
     ModifyMRUMainFilesList(AFilename, True);
     if MainMemoAddToRecentDocs then
     if MainMemoAddToRecentDocs then
       AddFileToRecentDocs(AFilename);
       AddFileToRecentDocs(AFilename);
-    LoadKnownIncludedFiles;
+    LoadKnownIncludedFilesAndUpdateMemos(AFilename);
   end;
   end;
 end;
 end;
 
 
@@ -1046,8 +1093,10 @@ begin
   except
   except
     Application.HandleException(Self);
     Application.HandleException(Self);
     if MsgBoxFmt('There was an error opening the file. Remove it from the list?',
     if MsgBoxFmt('There was an error opening the file. Remove it from the list?',
-       [AFilename], SCompilerFormCaption, mbError, MB_YESNO) = IDYES then
+       [AFilename], SCompilerFormCaption, mbError, MB_YESNO) = IDYES then begin
       ModifyMRUMainFilesList(AFilename, False);
       ModifyMRUMainFilesList(AFilename, False);
+      DeleteKnownIncludedFiles(AFilename);
+    end;
   end;
   end;
 end;
 end;
 
 
@@ -1113,8 +1162,10 @@ begin
   if not FOptions.UndoAfterSave then
   if not FOptions.UndoAfterSave then
     AMemo.ClearUndo;
     AMemo.ClearUndo;
   Result := True;
   Result := True;
-  if AMemo = FMainMemo then
+  if AMemo = FMainMemo then begin
     ModifyMRUMainFilesList(AMemo.Filename, True);
     ModifyMRUMainFilesList(AMemo.Filename, True);
+    SaveKnownIncludedFiles(AMemo.Filename);
+  end;
 end;
 end;
 
 
 function TCompileForm.ConfirmCloseFile(const PromptToSave: Boolean; const OnlyMainMemo: Boolean): Boolean;
 function TCompileForm.ConfirmCloseFile(const PromptToSave: Boolean; const OnlyMainMemo: Boolean): Boolean;
@@ -1192,7 +1243,7 @@ end;
 procedure TCompileForm.ReadMRUParametersList;
 procedure TCompileForm.ReadMRUParametersList;
 begin
 begin
   try
   try
-    ReadMRUList(FMRUParametersList, 'ParameterHistory', 'History');
+    ReadMRUList(FMRUParametersList, 'ParametersHistory', 'History');
   except
   except
     { Ignore any exceptions. }
     { Ignore any exceptions. }
   end;
   end;
@@ -1208,7 +1259,7 @@ begin
     { Ignore any exceptions. }
     { Ignore any exceptions. }
   end;
   end;
   try
   try
-    ModifyMRUList(FMRUParametersList, 'ParameterHistory', 'History', AParameter, AddNewItem, @CompareText);
+    ModifyMRUList(FMRUParametersList, 'ParametersHistory', 'History', AParameter, AddNewItem, @CompareText);
   except
   except
     { Handle exceptions locally; failure to save the MRU list should not be
     { Handle exceptions locally; failure to save the MRU list should not be
       a fatal error. }
       a fatal error. }
@@ -1246,7 +1297,6 @@ type
     CurLine: String;
     CurLine: String;
     OutputExe: String;
     OutputExe: String;
     DebugInfo: Pointer;
     DebugInfo: Pointer;
-    IncludedFiles: TIncludedFiles;
     ErrorMsg: String;
     ErrorMsg: String;
     ErrorFilename: String;
     ErrorFilename: String;
     ErrorLine: Integer;
     ErrorLine: Integer;
@@ -1324,9 +1374,8 @@ begin
         end;
         end;
       iscbNotifyIncludedFiles:
       iscbNotifyIncludedFiles:
         begin
         begin
-          DecodeIncludedFilenames(Data.IncludedFilenames, IncludedFiles); { Also stores last write time }
-          if Filename <> '' then
-            Form.SaveKnownIncludedFiles;
+          DecodeIncludedFilenames(Data.IncludedFilenames, Form.FIncludedFiles); { Also stores last write time }
+          Form.SaveKnownIncludedFiles(Filename);
         end;
         end;
       iscbNotifySuccess:
       iscbNotifySuccess:
         begin
         begin
@@ -1486,7 +1535,6 @@ begin
     SetLowPriority(FOptions.LowPriorityDuringCompile, FSavePriorityClass);
     SetLowPriority(FOptions.LowPriorityDuringCompile, FSavePriorityClass);
 
 
     AppData.Filename := AFilename;
     AppData.Filename := AFilename;
-    AppData.IncludedFiles := FIncludedFiles;
 
 
     {$IFNDEF STATICCOMPILER}
     {$IFNDEF STATICCOMPILER}
     if ISDllCompileScript(Params) <> isceNoError then begin
     if ISDllCompileScript(Params) <> isceNoError then begin
@@ -2314,7 +2362,6 @@ procedure TCompileForm.TOptionsClick(Sender: TObject);
 var
 var
   OptionsForm: TOptionsForm;
   OptionsForm: TOptionsForm;
   Ini: TConfigIniFile;
   Ini: TConfigIniFile;
-  OldOpenIncludedFiles: Boolean;
   Memo: TCompScintEdit;
   Memo: TCompScintEdit;
 begin
 begin
   OptionsForm := TOptionsForm.Create(Application);
   OptionsForm := TOptionsForm.Create(Application);
@@ -2344,8 +2391,6 @@ begin
     OptionsForm.FontPanel.ParentBackground := False;
     OptionsForm.FontPanel.ParentBackground := False;
     OptionsForm.FontPanel.Color := FMainMemo.Color;
     OptionsForm.FontPanel.Color := FMainMemo.Color;
 
 
-    OldOpenIncludedFiles := FOptions.OpenIncludedFiles;
-
     if OptionsForm.ShowModal <> mrOK then
     if OptionsForm.ShowModal <> mrOK then
       Exit;
       Exit;
 
 
@@ -2372,10 +2417,7 @@ begin
     FOptions.ThemeType := TThemeType(OptionsForm.ThemeComboBox.ItemIndex);
     FOptions.ThemeType := TThemeType(OptionsForm.ThemeComboBox.ItemIndex);
     
     
     UpdateCaption;
     UpdateCaption;
-    if not OldOpenIncludedFiles and FOptions.OpenIncludedFiles then
-      LoadKnownIncludedFiles
-    else
-      UpdateIncludedFilesMemos;
+    UpdateIncludedFilesMemos;
     for Memo in FMemos do begin
     for Memo in FMemos do begin
       { Move caret to start of line to ensure it doesn't end up in the middle
       { Move caret to start of line to ensure it doesn't end up in the middle
         of a double-byte character if the code page changes from SBCS to DBCS }
         of a double-byte character if the code page changes from SBCS to DBCS }
@@ -2532,8 +2574,7 @@ begin
       NextMemoIndex := FirstIncludedFilesMemoIndex;
       NextMemoIndex := FirstIncludedFilesMemoIndex;
       FLoadingIncludedFiles := True;
       FLoadingIncludedFiles := True;
       try
       try
-        for I := 0 to FIncludedFiles.Count-1 do begin
-          IncludedFile := FIncludedFiles[I];
+        for IncludedFile in FIncludedFiles do begin
           IncludedFile.Memo := FMemos[NextMemoIndex];
           IncludedFile.Memo := FMemos[NextMemoIndex];
           try
           try
             if not IncludedFile.Memo.Used or
             if not IncludedFile.Memo.Used or
@@ -2545,6 +2586,9 @@ begin
               IncludedFile.Memo.BreakPoints.Clear;
               IncludedFile.Memo.BreakPoints.Clear;
               OpenFile(IncludedFile.Memo, IncludedFile.Filename, False); { Also updates FileLastWriteTime }
               OpenFile(IncludedFile.Memo, IncludedFile.Filename, False); { Also updates FileLastWriteTime }
               IncludedFile.Memo.Used := True;
               IncludedFile.Memo.Used := True;
+            end else if IncludedFile.Memo.CompilerFileIndex = UnknownCompilerFileIndex then begin
+             { Previously the included file came from the history }
+              IncludedFile.Memo.CompilerFileIndex := IncludedFile.CompilerFileIndex;
             end;
             end;
             NewTabs.Add(PathExtractName(IncludedFile.Filename));
             NewTabs.Add(PathExtractName(IncludedFile.Filename));
             NewHints.Add(GetFileTitle(IncludedFile.Filename));
             NewHints.Add(GetFileTitle(IncludedFile.Filename));
@@ -2586,8 +2630,8 @@ begin
       FMemos[I].Used := False;
       FMemos[I].Used := False;
       FMemos[I].Visible := False;
       FMemos[I].Visible := False;
     end;
     end;
-    for I := 0 to FIncludedFiles.Count-1 do
-      FIncludedFiles[I].Memo := nil;
+    for IncludedFile in FIncludedFiles do
+      IncludedFile.Memo := nil;
     MemosTabSet.Visible := False;
     MemosTabSet.Visible := False;
     MemosTabSet.TabIndex := 0; { For next time }
     MemosTabSet.TabIndex := 0; { For next time }
   end;
   end;
@@ -3342,7 +3386,7 @@ begin
 
 
     SetString(FCompiledCodeDebugInfo, PAnsiChar(DebugInfo), Header.CompiledCodeDebugInfoLength);
     SetString(FCompiledCodeDebugInfo, PAnsiChar(DebugInfo), Header.CompiledCodeDebugInfoLength);
 
 
-    PrevCompilerFileIndex := -2; { Just some invalid value }
+    PrevCompilerFileIndex := UnknownCompilerFileIndex;
 
 
     for I := 0 to FDebugEntriesCount-1 do begin
     for I := 0 to FDebugEntriesCount-1 do begin
       if FDebugEntries[I].LineNumber >= 0 then begin
       if FDebugEntries[I].LineNumber >= 0 then begin

+ 58 - 0
Projects/CompFunc.pas

@@ -45,6 +45,9 @@ procedure OpenMailingListSite;
 procedure ReadMRUList(const MRUList: TStringList; const Section, Ident: String);
 procedure ReadMRUList(const MRUList: TStringList; const Section, Ident: String);
 procedure ModifyMRUList(const MRUList: TStringList; const Section, Ident: String;
 procedure ModifyMRUList(const MRUList: TStringList; const Section, Ident: String;
   const AItem: String; const AddNewItem: Boolean; CompareProc: TMRUItemCompareProc);
   const AItem: String; const AddNewItem: Boolean; CompareProc: TMRUItemCompareProc);
+procedure LoadKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
+procedure SaveKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
+procedure DeleteKnownIncludedFiles(const AFilename: String);
 procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
 procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
 procedure SetFakeShortCut(const MenuItem: TMenuItem; const Key: Word;
 procedure SetFakeShortCut(const MenuItem: TMenuItem; const Key: Word;
   const Shift: TShiftState);
   const Shift: TShiftState);
@@ -240,6 +243,61 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure LoadKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
+var
+  Ini: TConfigIniFile;
+  OldDelimiter: Char;
+begin
+  OldDelimiter := IncludedFiles.Delimiter;
+  Ini := TConfigIniFile.Create;
+  try
+    IncludedFiles.Delimiter := '*';
+    IncludedFiles.DelimitedText := Ini.ReadString('IncludedFilesHistory', AFilename, '');
+  finally
+    Ini.Free;
+    IncludedFiles.Delimiter := OldDelimiter;
+  end;
+end;
+
+procedure SaveKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
+var
+  Ini: TConfigIniFile;
+  OldDelimiter: Char;
+begin
+  if IncludedFiles.Count = 0 then begin
+    DeleteKnownIncludedFiles(AFilename);
+    Exit;
+  end;
+
+  if AFilename = '' then
+    raise Exception.Create('AFilename must be set');
+
+  OldDelimiter := IncludedFiles.Delimiter;
+  Ini := TConfigIniFile.Create;
+  try
+    IncludedFiles.Delimiter := '*';
+    Ini.WriteString('IncludedFilesHistory', AFilename, IncludedFiles.DelimitedText);
+  finally
+    Ini.Free;
+    IncludedFiles.Delimiter := OldDelimiter;
+  end;
+end;
+
+procedure DeleteKnownIncludedFiles(const AFilename: String);
+var
+  Ini: TConfigIniFile;
+begin
+  if AFilename = '' then
+    raise Exception.Create('AFilename must be set');
+
+  Ini := TConfigIniFile.Create;
+  try
+    Ini.DeleteKey('IncludedFilesHistory', AFilename);
+  finally
+    Ini.Free;
+  end;
+end;
+
 procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
 procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
 begin
 begin
   MenuItem.Caption := MenuItem.Caption + #9 + S;
   MenuItem.Caption := MenuItem.Caption + #9 + S;

+ 3 - 0
Projects/CompScintEdit.pas

@@ -30,6 +30,9 @@ const
   inSquiggly = 0;
   inSquiggly = 0;
   inPendingSquiggly = 1;
   inPendingSquiggly = 1;
 
 
+  { Just some invalid value used to indicate an unknown/uninitialized compiler FileIndex value }
+  UnknownCompilerFileIndex = -2;
+
 type
 type
   TLineState = (lnUnknown, lnHasEntry, lnEntryProcessed);
   TLineState = (lnUnknown, lnHasEntry, lnEntryProcessed);
   PLineStateArray = ^TLineStateArray;
   PLineStateArray = ^TLineStateArray;