diff --git a/components/codetools/codecache.pas b/components/codetools/codecache.pas index 36a58ec7f4..b99dc8175d 100644 --- a/components/codetools/codecache.pas +++ b/components/codetools/codecache.pas @@ -39,7 +39,7 @@ uses {$IFDEF MEM_CHECK} MemCheck, {$ENDIF} - Classes, SysUtils, SourceLog, LinkScanner, AVL_Tree, XMLCfg; + Classes, SysUtils, SourceLog, LinkScanner, AVL_Tree, XMLCfg, FileProcs; type TCodeCache = class; @@ -143,15 +143,6 @@ type 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; var CodeBuf1, CodeBuf2: TCodeBuffer; begin @@ -244,6 +235,7 @@ begin with Result do begin FCodeCache:=Self; LastIncludedByFile:=Self.LastIncludedByFile(AFilename); + ReadOnly:=not FileIsWritable(Filename); end; end else if Result.IsDeleted then begin // file in cache, but marked as deleted -> load from disk diff --git a/ide/main.pp b/ide/main.pp index 74e2cf473f..b95a2d710b 100644 --- a/ide/main.pp +++ b/ide/main.pp @@ -352,7 +352,8 @@ type function DoSaveCodeBufferToFile(ABuffer: TCodeBuffer; const AFilename: string; IsPartOfProject:boolean): TModalResult; 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; IsPartOfProject:boolean): TModalResult; procedure UpdateCaption; @@ -2396,7 +2397,7 @@ writeln('TMainIDE.DoSaveEditorUnit B2 ',ResourceCode<>nil); LFMCode:=nil; if ResourceCode<>nil then begin Result:=DoLoadCodeBuffer(LFMCode, - ChangeFileExt(ResourceCode.Filename,'.lfm'),false,false); + ChangeFileExt(ResourceCode.Filename,'.lfm'),false,false,true); if Result<>mrOk then exit; Result:=mrCancel; end; @@ -2737,8 +2738,8 @@ CheckHeap(IntToStr(GetMem_Cnt)); Result:=MessageDlg('File not found', 'The file "'+AFilename+'"'#13 +'was not found.'#13 - +'Ignore will go on loading the project,'#13 - +'Abort will cancel the loading.', + +'Ignore will go on loading the project,'#13 + +'Abort will cancel the loading.', mtError, [mbIgnore, mbAbort], 0); exit; end; @@ -2762,7 +2763,7 @@ CheckHeap(IntToStr(GetMem_Cnt)); Ext:=lowercase(ExtractFileExt(AFilename)); if ReOpen then begin NewUnitInfo:=Project.Units[i]; - Result:=DoLoadCodeBuffer(NewBuf,AFileName,true,true); + Result:=DoLoadCodeBuffer(NewBuf,AFileName,true,true,true); if Result<>mrOk then exit; NewUnitInfo.Source:=NewBuf; if (Ext='.pp') or (Ext='.pas') then @@ -2774,7 +2775,7 @@ CheckHeap(IntToStr(GetMem_Cnt)); Result:=DoOpenProjectFile(AFilename); exit; end; - Result:=DoLoadCodeBuffer(PreReadBuf,AFileName,true,true); + Result:=DoLoadCodeBuffer(PreReadBuf,AFileName,true,true,true); if Result<>mrOk then exit; Result:=mrCancel; // check if unit is a program @@ -2815,6 +2816,8 @@ CheckHeap(IntToStr(GetMem_Cnt)); NewUnitInfo.ReadUnitNameFromSource; Project.AddUnit(NewUnitInfo,false); end; + NewUnitInfo.ReadOnly:=NewUnitInfo.ReadOnly + or (not FileIsWritable(NewUnitInfo.Filename)); {$IFDEF IDE_DEBUG} writeln('[TMainIDE.DoOpenEditorFile] B'); {$ENDIF} @@ -2850,6 +2853,7 @@ writeln('[TMainIDE.DoOpenEditorFile] B'); NewSrcEdit.EditorComponent.CaretXY:=NewUnitInfo.CursorPos; NewSrcEdit.EditorComponent.TopLine:=NewUnitInfo.TopLine; NewSrcEdit.EditorComponent.LeftChar:=1; + NewSrcEdit.ReadOnly:=NewUnitInfo.ReadOnly; {$IFDEF IDE_DEBUG} writeln('[TMainIDE.DoOpenEditorFile] C'); @@ -2861,7 +2865,7 @@ writeln('[TMainIDE.DoOpenEditorFile] C'); LFMFilename:=ChangeFileExt(NewUnitInfo.Filename,'.lfm'); NewBuf:=nil; if FileExists(LFMFilename) then begin - Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false); + Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false,true); if Result<>mrOk then exit; Result:=mrCancel; end else begin @@ -2871,7 +2875,7 @@ writeln('[TMainIDE.DoOpenEditorFile] C'); LFMFilename:=ChangeFileExt(NewBuf.Filename,'.lfm'); NewBuf:=nil; if FileExists(LFMFilename) then begin - Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false); + Result:=DoLoadCodeBuffer(NewBuf,LFMFilename,true,false,true); if Result<>mrOk then exit; Result:=mrCancel; end; @@ -3480,6 +3484,15 @@ CheckHeap(IntToStr(GetMem_Cnt)); if ExtractFileNameOnly(AFileName)='' then exit; AFilename:=ExpandFileName(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 // load instead of lazarus program file the project info file AFileName:=ChangeFileExt(AFileName,'.lpi'); @@ -3489,14 +3502,14 @@ CheckHeap(IntToStr(GetMem_Cnt)); Result:=DoOpenEditorFile(AFilename,false); exit; end; - 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; + if 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, [mbYes, mbAbort], 0); + if Result=mrAbort then exit; + end; // close the old project if SomethingOfProjectIsModified then begin if MessageDlg('Project changed', 'Save changes to project?', @@ -3521,17 +3534,17 @@ CheckHeap(IntToStr(GetMem_Cnt)); CodeToolBoss.GlobalValues.Variables[ExternalMacroStart+'ProjectDir']:= ExtractFilePath(Project.ProjectFile); CodeToolBoss.DefineTree.ClearCache; -writeln('TMainIDE.DoOpenProjectFile B2'); if Project.MainUnit>=0 then begin // read MainUnit Source Result:=DoLoadCodeBuffer(NewBuf,Project.Units[Project.MainUnit].Filename, - true,true); -writeln('TMainIDE.DoOpenProjectFile B3'); + true,true,true); if Result=mrIgnore then Result:=mrAbort; if Result=mrAbort then exit; Project.Units[Project.MainUnit].Source:=NewBuf; end; +{$IFDEF IDE_DEBUG} writeln('TMainIDE.DoOpenProjectFile C'); +{$ENDIF} {$IFDEF IDE_MEM_CHECK} CheckHeap(IntToStr(GetMem_Cnt)); {$ENDIF} @@ -3556,9 +3569,9 @@ CheckHeap(IntToStr(GetMem_Cnt)); end; if LowestEditorIndex>=0 then begin // 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); -writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk); +//writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk); if Result=mrAbort then exit; if Result=mrOk then begin // open successful @@ -3575,7 +3588,7 @@ writeln('TMainIDE.DoOpenProjectFile C3 ',Result=mrOk); end; until LowestEditorIndex<0; Result:=mrCancel; -writeln('TMainIDE.DoOpenProjectFile D'); +//writeln('TMainIDE.DoOpenProjectFile D'); // set active editor source editor if (SourceNoteBook.NoteBook<>nil) and (Project.ActiveEditorIndexAtStart>=0) and (Project.ActiveEditorIndexAtStartmrOk then exit; + end; ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,UpdateFromDisk,Revert); if ACodeBuffer<>nil then begin - ACodeBuffer.Reload; Result:=mrOk; writeln('[TMainIDE.DoLoadCodeBuffer] ',ACodeBuffer.SourceLength,' ',ACodeBuffer.Filename); end else begin ACaption:='Read Error'; 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; end; until Result<>mrRetry; @@ -5379,8 +5401,8 @@ end. { ============================================================================= $Log$ - Revision 1.187 2001/12/17 16:46:59 lazarus - MG: new file procs, find declaration now supports relative search paths + Revision 1.188 2001/12/17 19:41:05 lazarus + MG: added binary file recognition and readonly recognition Revision 1.186 2001/12/17 11:16:08 lazarus MG: fixed open file key in source editor diff --git a/ide/uniteditor.pp b/ide/uniteditor.pp index bcd7ee018b..4aa56ddaff 100644 --- a/ide/uniteditor.pp +++ b/ide/uniteditor.pp @@ -36,7 +36,7 @@ uses Compiler, MsgView, WordCompletion, CodeToolManager, CodeCache, SourceLog, SynEdit, SynEditHighlighter, SynHighlighterPas, SynEditAutoComplete, SynEditKeyCmds, SynCompletion, Graphics, Extctrls, Menus, Splash, - FindInFilesDlg, LMessages; + FindInFilesDlg, LMessages, IDEProcs; type // -------------------------------------------------------------------------- @@ -111,6 +111,7 @@ type procedure SetModified(NewValue:boolean); Function GetInsertMode : Boolean; Function GetReadonly : Boolean; + procedure SetReadOnly(NewValue: boolean); procedure SetCodeTemplates( NewCodeTemplates: TSynEditAutoComplete); procedure SetPopupMenu(NewPopupMenu: TPopupMenu); @@ -187,7 +188,7 @@ type property ShortName : String read FShortName write fShortName; property FileName : AnsiString read GetFileName; 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 CodeTemplates: TSynEditAutoComplete read FCodeTemplates write SetCodeTemplates; @@ -623,12 +624,16 @@ Begin FEditor.SetFocus; end; - Function TSourceEditor.GetReadOnly : Boolean; Begin Result := FEditor.ReadOnly; End; +procedure TSourceEditor.SetReadOnly(NewValue: boolean); +begin + FEditor.ReadOnly:=NewValue; +end; + Procedure TSourceEditor.ProcessUserCommand(Sender: TObject; var Command: TSynEditorCommand; var AChar: char; Data: pointer); var @@ -2457,6 +2462,14 @@ Procedure TSourceNotebook.ReadOnlyClicked(Sender : TObject); var ActEdit:TSourceEditor; begin 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); UpdateStatusBar; end;