LazEdit: in ParseCommandlineFilenames ask to create a file if it does not exsist.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7257 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
lazarus-bart 2020-01-09 18:04:38 +00:00
parent 887e07f476
commit c9d1e05e6c
2 changed files with 40 additions and 2 deletions

View File

@ -201,6 +201,7 @@ type
// Other main.pp constants
SLine, SCol, SModified, SIns, SOvr,
msgOpenError, msgSaveError, msgSaveAllError, msgFileIsNotText,
msgFileCreateError, msgAskCreateFile,
msgFileNotFound, msgModifiedSave, msgMruIndexOutOfBound,
msgFileTypeNotForBrowser, msgFileHasNoName, msgErrorBrowser,
msgTextNotFound: string;
@ -348,6 +349,8 @@ begin
msgSaveAllError := 'The following save all error has occured:'^m'%s';
msgFileIsNotText := 'The selected file '^m'%s'^m' does not seam to be a text file.';
msgFileNotFound := 'File not found:'^m'%s';
msgFileCreateError := 'Error creating file: '^m'%s';
msgAskCreateFile := MsgFileNotFound + ^m^m'Create file?';
msgModifiedSave := 'The following file was modified:'^m'%s'^m'Should it be saved?';
msgMruIndexOutOfBound := 'Index out of bounds [%d]'^m;
msgFileTypeNotForBrowser := 'The file type is not suited for a browser.'^m+'Continue anyway?';
@ -507,6 +510,8 @@ begin
msgSaveAllError := 'De volgende bestanden zijn niet opgeslagen:'^m'%s';
msgFileIsNotText := 'Dit bestand lijkt geen tekstbestand te zijn'^m'%s'^m'Wilt u het toch openen?';
msgFileNotFound := 'Bestand niet gevonden:'^m'%s';
msgFileCreateError := 'Fout bij aanmaken van bestand: '^m'%s';
msgAskCreateFile := MsgFileNotFound + ^m^m'Bestand aanmaken?';
msgModifiedSave := 'Bestand is gewijzigd:'^m'%s'^m'Bestand opslaan?';
msgMruIndexOutOfBound := 'Index voor recent geopende bestanden ligt buiten de grenzen [%d]'^m+
'Dit is uiteraard een fout van de programmeur';

View File

@ -467,10 +467,12 @@ type
function TryMarkSelection(const Pre, Post: String): Boolean;
public
//File procedures
function AskCreateFile(const Fn: String): Boolean;
function AskFileNameOpen: String;
function AskFileNameOpenTemplate: String;
function AskFileNameSave(const Fn: String; const FileType: TEditorFileType): String;
function AskFileNameSaveTemplate: String;
function TryCreateEmptyFile(const Fn: String): Boolean;
function TryFileOpen(const Fn: String; const AsTemplate: Boolean = False): Boolean;
function TryFileSave(Editor: TEditor; Fn: String): TIoResult;
function CloseCurrentEditor: Boolean;
@ -1600,9 +1602,26 @@ begin
S := ExpandFileNameUtf8(S); //we want full filename here, e.g. for filename in statusbar
if FileExistsUtf8(S) then
begin
if not TryFileOpen(S) then ShowError(Format(vTranslations.msgOpenError,[S]));
if not TryFileOpen(S) then
ShowError(Format(vTranslations.msgOpenError,[S]))
else
Inc(Count);
end
else ShowError(Format(vTranslations.msgFileNotFound,[S]));
else
begin
if AskCreateFile(S) then
begin
if TryCreateEmptyFile(S) then
begin
if not TryFileOpen(S) then
ShowError(Format(vTranslations.msgOpenError,[S]))
else
Inc(Count);
end
else
ShowError(Format(vTranslations.msgFileCreateError,[S]));
end;
end;
end;
end;
//Start with blank page if no files are specified on commandline
@ -1987,6 +2006,11 @@ begin
else Result := False;
end;
function TLazEditMainForm.AskCreateFile(const Fn: String): Boolean;
begin
Result := MessageDlg(AppName, Format(vTranslations.msgAskCreateFile,[Fn]), mtConfirmation, [mbYes, mbNo],0) = mrYes;
end;
@ -2062,6 +2086,15 @@ begin
else Result := EmptyStr;
end;
function TLazEditMainForm.TryCreateEmptyFile(const Fn: String): Boolean;
var
H: THandle;
begin
H := FileCreate(Fn, fmOpenWrite);
Result := H <> feInvalidHandle; //THandle(-1)
if Result then FileClose(H);
end;
function TLazEditMainForm.TryFileOpen(const Fn: String; const AsTemplate: Boolean = False): Boolean;
var
Ed: TEditor;