MG: added binary file recognition and readonly recognition

git-svn-id: trunk@542 -
This commit is contained in:
lazarus 2001-12-17 19:41:06 +00:00
parent 03c31c5c96
commit a66b0fa062
3 changed files with 67 additions and 40 deletions

View File

@ -39,7 +39,7 @@ uses
{$IFDEF MEM_CHECK} {$IFDEF MEM_CHECK}
MemCheck, MemCheck,
{$ENDIF} {$ENDIF}
Classes, SysUtils, SourceLog, LinkScanner, AVL_Tree, XMLCfg; Classes, SysUtils, SourceLog, LinkScanner, AVL_Tree, XMLCfg, FileProcs;
type type
TCodeCache = class; TCodeCache = class;
@ -143,15 +143,6 @@ type
implementation implementation
function CompareFilenames(const FileName1, Filename2: string): integer;
begin
{$ifdef win32}
Result:=AnsiCompareText(FileName1,Filename2);
{$else}
Result:=AnsiCompareStr(FileName1,Filename2);
{$endif}
end;
function CompareCodeBuffers(NodeData1, NodeData2: pointer): integer; function CompareCodeBuffers(NodeData1, NodeData2: pointer): integer;
var CodeBuf1, CodeBuf2: TCodeBuffer; var CodeBuf1, CodeBuf2: TCodeBuffer;
begin begin
@ -244,6 +235,7 @@ begin
with Result do begin with Result do begin
FCodeCache:=Self; FCodeCache:=Self;
LastIncludedByFile:=Self.LastIncludedByFile(AFilename); LastIncludedByFile:=Self.LastIncludedByFile(AFilename);
ReadOnly:=not FileIsWritable(Filename);
end; end;
end else if Result.IsDeleted then begin end else if Result.IsDeleted then begin
// file in cache, but marked as deleted -> load from disk // file in cache, but marked as deleted -> load from disk

View File

@ -352,7 +352,8 @@ type
function DoSaveCodeBufferToFile(ABuffer: TCodeBuffer; function DoSaveCodeBufferToFile(ABuffer: TCodeBuffer;
const AFilename: string; IsPartOfProject:boolean): TModalResult; const AFilename: string; IsPartOfProject:boolean): TModalResult;
function DoLoadCodeBuffer(var ACodeBuffer: TCodeBuffer; function DoLoadCodeBuffer(var ACodeBuffer: TCodeBuffer;
const AFilename: string; UpdateFromDisk, Revert: boolean): TModalResult; const AFilename: string; UpdateFromDisk, Revert,
CheckIfText: boolean): TModalResult;
function DoBackupFile(const Filename:string; function DoBackupFile(const Filename:string;
IsPartOfProject:boolean): TModalResult; IsPartOfProject:boolean): TModalResult;
procedure UpdateCaption; procedure UpdateCaption;
@ -2396,7 +2397,7 @@ writeln('TMainIDE.DoSaveEditorUnit B2 ',ResourceCode<>nil);
LFMCode:=nil; LFMCode:=nil;
if ResourceCode<>nil then begin if ResourceCode<>nil then begin
Result:=DoLoadCodeBuffer(LFMCode, Result:=DoLoadCodeBuffer(LFMCode,
ChangeFileExt(ResourceCode.Filename,'.lfm'),false,false); ChangeFileExt(ResourceCode.Filename,'.lfm'),false,false,true);
if Result<>mrOk then exit; if Result<>mrOk then exit;
Result:=mrCancel; Result:=mrCancel;
end; end;
@ -2737,8 +2738,8 @@ CheckHeap(IntToStr(GetMem_Cnt));
Result:=MessageDlg('File not found', Result:=MessageDlg('File not found',
'The file "'+AFilename+'"'#13 'The file "'+AFilename+'"'#13
+'was not found.'#13 +'was not found.'#13
+'Ignore will go on loading the project,'#13 +'Ignore will go on loading the project,'#13
+'Abort will cancel the loading.', +'Abort will cancel the loading.',
mtError, [mbIgnore, mbAbort], 0); mtError, [mbIgnore, mbAbort], 0);
exit; exit;
end; end;
@ -2762,7 +2763,7 @@ CheckHeap(IntToStr(GetMem_Cnt));
Ext:=lowercase(ExtractFileExt(AFilename)); Ext:=lowercase(ExtractFileExt(AFilename));
if ReOpen then begin if ReOpen then begin
NewUnitInfo:=Project.Units[i]; NewUnitInfo:=Project.Units[i];
Result:=DoLoadCodeBuffer(NewBuf,AFileName,true,true); Result:=DoLoadCodeBuffer(NewBuf,AFileName,true,true,true);
if Result<>mrOk then exit; if Result<>mrOk then exit;
NewUnitInfo.Source:=NewBuf; NewUnitInfo.Source:=NewBuf;
if (Ext='.pp') or (Ext='.pas') then if (Ext='.pp') or (Ext='.pas') then
@ -2774,7 +2775,7 @@ CheckHeap(IntToStr(GetMem_Cnt));
Result:=DoOpenProjectFile(AFilename); Result:=DoOpenProjectFile(AFilename);
exit; exit;
end; end;
Result:=DoLoadCodeBuffer(PreReadBuf,AFileName,true,true); Result:=DoLoadCodeBuffer(PreReadBuf,AFileName,true,true,true);
if Result<>mrOk then exit; if Result<>mrOk then exit;
Result:=mrCancel; Result:=mrCancel;
// check if unit is a program // check if unit is a program
@ -2815,6 +2816,8 @@ CheckHeap(IntToStr(GetMem_Cnt));
NewUnitInfo.ReadUnitNameFromSource; NewUnitInfo.ReadUnitNameFromSource;
Project.AddUnit(NewUnitInfo,false); Project.AddUnit(NewUnitInfo,false);
end; end;
NewUnitInfo.ReadOnly:=NewUnitInfo.ReadOnly
or (not FileIsWritable(NewUnitInfo.Filename));
{$IFDEF IDE_DEBUG} {$IFDEF IDE_DEBUG}
writeln('[TMainIDE.DoOpenEditorFile] B'); writeln('[TMainIDE.DoOpenEditorFile] B');
{$ENDIF} {$ENDIF}
@ -2850,6 +2853,7 @@ writeln('[TMainIDE.DoOpenEditorFile] B');
NewSrcEdit.EditorComponent.CaretXY:=NewUnitInfo.CursorPos; NewSrcEdit.EditorComponent.CaretXY:=NewUnitInfo.CursorPos;
NewSrcEdit.EditorComponent.TopLine:=NewUnitInfo.TopLine; NewSrcEdit.EditorComponent.TopLine:=NewUnitInfo.TopLine;
NewSrcEdit.EditorComponent.LeftChar:=1; NewSrcEdit.EditorComponent.LeftChar:=1;
NewSrcEdit.ReadOnly:=NewUnitInfo.ReadOnly;
{$IFDEF IDE_DEBUG} {$IFDEF IDE_DEBUG}
writeln('[TMainIDE.DoOpenEditorFile] C'); writeln('[TMainIDE.DoOpenEditorFile] C');
@ -2861,7 +2865,7 @@ writeln('[TMainIDE.DoOpenEditorFile] C');
LFMFilename:=ChangeFileExt(NewUnitInfo.Filename,'.lfm'); LFMFilename:=ChangeFileExt(NewUnitInfo.Filename,'.lfm');
NewBuf:=nil; NewBuf:=nil;
if FileExists(LFMFilename) then begin if FileExists(LFMFilename) then begin
Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false); Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false,true);
if Result<>mrOk then exit; if Result<>mrOk then exit;
Result:=mrCancel; Result:=mrCancel;
end else begin end else begin
@ -2871,7 +2875,7 @@ writeln('[TMainIDE.DoOpenEditorFile] C');
LFMFilename:=ChangeFileExt(NewBuf.Filename,'.lfm'); LFMFilename:=ChangeFileExt(NewBuf.Filename,'.lfm');
NewBuf:=nil; NewBuf:=nil;
if FileExists(LFMFilename) then begin if FileExists(LFMFilename) then begin
Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false); Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false,true);
if Result<>mrOk then exit; if Result<>mrOk then exit;
Result:=mrCancel; Result:=mrCancel;
end; end;
@ -3480,6 +3484,15 @@ CheckHeap(IntToStr(GetMem_Cnt));
if ExtractFileNameOnly(AFileName)='' then exit; if ExtractFileNameOnly(AFileName)='' then exit;
AFilename:=ExpandFileName(AFilename); AFilename:=ExpandFileName(AFilename);
Ext:=lowercase(ExtractFileExt(AFilename)); Ext:=lowercase(ExtractFileExt(AFilename));
repeat
if not FileExists(AFilename) then begin
ACaption:='File not found';
AText:='File "'+AFilename+'" not found.';
Result:=MessageDlg(ACaption, AText, mtError, [mbAbort, mbRetry], 0);
if Result=mrAbort then exit;
end;
until Result<>mrRetry;
Result:=mrCancel;
if (FileExists(ChangeFileExt(AFileName,'.lpi'))) then begin if (FileExists(ChangeFileExt(AFileName,'.lpi'))) then begin
// load instead of lazarus program file the project info file // load instead of lazarus program file the project info file
AFileName:=ChangeFileExt(AFileName,'.lpi'); AFileName:=ChangeFileExt(AFileName,'.lpi');
@ -3489,14 +3502,14 @@ CheckHeap(IntToStr(GetMem_Cnt));
Result:=DoOpenEditorFile(AFilename,false); Result:=DoOpenEditorFile(AFilename,false);
exit; exit;
end; end;
repeat if not FileIsText(AFilename) then begin
if not FileExists(AFilename) then begin ACaption:='File not text';
ACaption:='File not found'; AText:='File "'+AFilename+'"'#13
AText:='File "'+AFilename+'" not found.'; +'does not look like a text file.'#13
Result:=MessageDlg(ACaption, AText, mtError, [mbAbort, mbRetry], 0); +'Open it anyway?';
if Result=mrAbort then exit; Result:=MessageDlg(ACaption, AText, mtConfirmation, [mbYes, mbAbort], 0);
end; if Result=mrAbort then exit;
until Result<>mrRetry; end;
// close the old project // close the old project
if SomethingOfProjectIsModified then begin if SomethingOfProjectIsModified then begin
if MessageDlg('Project changed', 'Save changes to project?', if MessageDlg('Project changed', 'Save changes to project?',
@ -3521,17 +3534,17 @@ CheckHeap(IntToStr(GetMem_Cnt));
CodeToolBoss.GlobalValues.Variables[ExternalMacroStart+'ProjectDir']:= CodeToolBoss.GlobalValues.Variables[ExternalMacroStart+'ProjectDir']:=
ExtractFilePath(Project.ProjectFile); ExtractFilePath(Project.ProjectFile);
CodeToolBoss.DefineTree.ClearCache; CodeToolBoss.DefineTree.ClearCache;
writeln('TMainIDE.DoOpenProjectFile B2');
if Project.MainUnit>=0 then begin if Project.MainUnit>=0 then begin
// read MainUnit Source // read MainUnit Source
Result:=DoLoadCodeBuffer(NewBuf,Project.Units[Project.MainUnit].Filename, Result:=DoLoadCodeBuffer(NewBuf,Project.Units[Project.MainUnit].Filename,
true,true); true,true,true);
writeln('TMainIDE.DoOpenProjectFile B3');
if Result=mrIgnore then Result:=mrAbort; if Result=mrIgnore then Result:=mrAbort;
if Result=mrAbort then exit; if Result=mrAbort then exit;
Project.Units[Project.MainUnit].Source:=NewBuf; Project.Units[Project.MainUnit].Source:=NewBuf;
end; end;
{$IFDEF IDE_DEBUG}
writeln('TMainIDE.DoOpenProjectFile C'); writeln('TMainIDE.DoOpenProjectFile C');
{$ENDIF}
{$IFDEF IDE_MEM_CHECK} {$IFDEF IDE_MEM_CHECK}
CheckHeap(IntToStr(GetMem_Cnt)); CheckHeap(IntToStr(GetMem_Cnt));
{$ENDIF} {$ENDIF}
@ -3556,9 +3569,9 @@ CheckHeap(IntToStr(GetMem_Cnt));
end; end;
if LowestEditorIndex>=0 then begin if LowestEditorIndex>=0 then begin
// reopen file // reopen file
writeln('TMainIDE.DoOpenProjectFile C2 ',Project.Units[LowestUnitIndex].Filename); //writeln('TMainIDE.DoOpenProjectFile C2 ',Project.Units[LowestUnitIndex].Filename);
Result:=DoOpenEditorFile(Project.Units[LowestUnitIndex].Filename,true); Result:=DoOpenEditorFile(Project.Units[LowestUnitIndex].Filename,true);
writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk); //writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk);
if Result=mrAbort then exit; if Result=mrAbort then exit;
if Result=mrOk then begin if Result=mrOk then begin
// open successful // open successful
@ -3575,7 +3588,7 @@ writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk);
end; end;
until LowestEditorIndex<0; until LowestEditorIndex<0;
Result:=mrCancel; Result:=mrCancel;
writeln('TMainIDE.DoOpenProjectFile D'); //writeln('TMainIDE.DoOpenProjectFile D');
// set active editor source editor // set active editor source editor
if (SourceNoteBook.NoteBook<>nil) and (Project.ActiveEditorIndexAtStart>=0) if (SourceNoteBook.NoteBook<>nil) and (Project.ActiveEditorIndexAtStart>=0)
and (Project.ActiveEditorIndexAtStart<SourceNoteBook.NoteBook.Pages.Count) and (Project.ActiveEditorIndexAtStart<SourceNoteBook.NoteBook.Pages.Count)
@ -4214,21 +4227,30 @@ begin
end; end;
function TMainIDE.DoLoadCodeBuffer(var ACodeBuffer: TCodeBuffer; function TMainIDE.DoLoadCodeBuffer(var ACodeBuffer: TCodeBuffer;
const AFilename: string; UpdateFromDisk, Revert: boolean): TModalResult; const AFilename: string; UpdateFromDisk, Revert,
CheckIfText: boolean): TModalResult;
var var
ACaption,AText:string; ACaption,AText:string;
begin begin
repeat repeat
writeln('[TMainIDE.DoLoadCodeBuffer] A ',AFilename); writeln('[TMainIDE.DoLoadCodeBuffer] A ',AFilename);
if CheckIfText and (not FileIsText(AFilename)) then begin
ACaption:='File not text';
AText:='File "'+AFilename+'"'#13
+'does not look like a text file.'#13
+'Open it anyway?';
Result:=MessageDlg(ACaption, AText, mtConfirmation,
[mbOk, mbIgnore, mbAbort], 0);
if Result<>mrOk then exit;
end;
ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,UpdateFromDisk,Revert); ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,UpdateFromDisk,Revert);
if ACodeBuffer<>nil then begin if ACodeBuffer<>nil then begin
ACodeBuffer.Reload;
Result:=mrOk; Result:=mrOk;
writeln('[TMainIDE.DoLoadCodeBuffer] ',ACodeBuffer.SourceLength,' ',ACodeBuffer.Filename); writeln('[TMainIDE.DoLoadCodeBuffer] ',ACodeBuffer.SourceLength,' ',ACodeBuffer.Filename);
end else begin end else begin
ACaption:='Read Error'; ACaption:='Read Error';
AText:='Unable to read file "'+AFilename+'"!'; AText:='Unable to read file "'+AFilename+'"!';
Result:=MessageDlg(ACaption,AText,mterror,[mbabort, mbretry, mbignore],0); Result:=MessageDlg(ACaption,AText,mterror,[mbAbort, mbRetry, mbIgnore],0);
if Result=mrAbort then exit; if Result=mrAbort then exit;
end; end;
until Result<>mrRetry; until Result<>mrRetry;
@ -5379,8 +5401,8 @@ end.
{ ============================================================================= { =============================================================================
$Log$ $Log$
Revision 1.187 2001/12/17 16:46:59 lazarus Revision 1.188 2001/12/17 19:41:05 lazarus
MG: new file procs, find declaration now supports relative search paths MG: added binary file recognition and readonly recognition
Revision 1.186 2001/12/17 11:16:08 lazarus Revision 1.186 2001/12/17 11:16:08 lazarus
MG: fixed open file key in source editor MG: fixed open file key in source editor

View File

@ -36,7 +36,7 @@ uses
Compiler, MsgView, WordCompletion, CodeToolManager, CodeCache, SourceLog, Compiler, MsgView, WordCompletion, CodeToolManager, CodeCache, SourceLog,
SynEdit, SynEditHighlighter, SynHighlighterPas, SynEditAutoComplete, SynEdit, SynEditHighlighter, SynHighlighterPas, SynEditAutoComplete,
SynEditKeyCmds, SynCompletion, Graphics, Extctrls, Menus, Splash, SynEditKeyCmds, SynCompletion, Graphics, Extctrls, Menus, Splash,
FindInFilesDlg, LMessages; FindInFilesDlg, LMessages, IDEProcs;
type type
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -111,6 +111,7 @@ type
procedure SetModified(NewValue:boolean); procedure SetModified(NewValue:boolean);
Function GetInsertMode : Boolean; Function GetInsertMode : Boolean;
Function GetReadonly : Boolean; Function GetReadonly : Boolean;
procedure SetReadOnly(NewValue: boolean);
procedure SetCodeTemplates( procedure SetCodeTemplates(
NewCodeTemplates: TSynEditAutoComplete); NewCodeTemplates: TSynEditAutoComplete);
procedure SetPopupMenu(NewPopupMenu: TPopupMenu); procedure SetPopupMenu(NewPopupMenu: TPopupMenu);
@ -187,7 +188,7 @@ type
property ShortName : String read FShortName write fShortName; property ShortName : String read FShortName write fShortName;
property FileName : AnsiString read GetFileName; property FileName : AnsiString read GetFileName;
property Modified : Boolean read GetModified write SetModified; property Modified : Boolean read GetModified write SetModified;
property ReadOnly : Boolean read GetReadOnly; property ReadOnly : Boolean read GetReadOnly write SetReadOnly;
property InsertMode : Boolean read GetInsertmode; property InsertMode : Boolean read GetInsertmode;
property CodeTemplates: TSynEditAutoComplete property CodeTemplates: TSynEditAutoComplete
read FCodeTemplates write SetCodeTemplates; read FCodeTemplates write SetCodeTemplates;
@ -623,12 +624,16 @@ Begin
FEditor.SetFocus; FEditor.SetFocus;
end; end;
Function TSourceEditor.GetReadOnly : Boolean; Function TSourceEditor.GetReadOnly : Boolean;
Begin Begin
Result := FEditor.ReadOnly; Result := FEditor.ReadOnly;
End; End;
procedure TSourceEditor.SetReadOnly(NewValue: boolean);
begin
FEditor.ReadOnly:=NewValue;
end;
Procedure TSourceEditor.ProcessUserCommand(Sender: TObject; Procedure TSourceEditor.ProcessUserCommand(Sender: TObject;
var Command: TSynEditorCommand; var AChar: char; Data: pointer); var Command: TSynEditorCommand; var AChar: char; Data: pointer);
var var
@ -2457,6 +2462,14 @@ Procedure TSourceNotebook.ReadOnlyClicked(Sender : TObject);
var ActEdit:TSourceEditor; var ActEdit:TSourceEditor;
begin begin
ActEdit:=GetActiveSE; ActEdit:=GetActiveSE;
if ActEdit.ReadOnly and (ActEdit.CodeBuffer<>nil)
and (not ActEdit.CodeBuffer.IsVirtual)
and (not FileIsWritable(ActEdit.CodeBuffer.Filename)) then begin
MessageDlg('File is readonly',
'The file "'+ActEdit.CodeBuffer.Filename+'" is not writable.',
mtError,[mbCancel],0);
exit;
end;
ActEdit.EditorComponent.ReadOnly := not(ActEdit.EditorComponent.ReadOnly); ActEdit.EditorComponent.ReadOnly := not(ActEdit.EditorComponent.ReadOnly);
UpdateStatusBar; UpdateStatusBar;
end; end;