[Solved] Making AutoSave plugin: problems lazarus / 9-Editor ---------------------------------------------------------------------- lainz · 2018-04-25 19:57 Hi, this is my AutoSave plugin, it works, but it has a big problem, it tries to save a file that's not saved. And because is triggered every 1000 ms, there are a lot of popup windows coming every second  I want to AutoSave existing files, if it has sense.. This is the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit lazautosaveunit; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, IDECommands, ExtCtrls; const  AUTOSAVEINTERVAL = 1000; // ms type   { TLazAutoSave }   TLazAutoSave = class(TObject)  private  Timer: TTimer;    procedure OnTimer(Sender: TObject);  public    constructor Create;    destructor Destroy;  end; implementation var  AutoSave: TLazAutoSave; { TLazAutoSave } procedure TLazAutoSave.OnTimer(Sender: TObject);begin  IDECommands.ExecuteIDECommand(Self, ecSaveAll);end; constructor TLazAutoSave.Create;begin  Timer := TTimer.Create(nil);  Timer.Interval := AUTOSAVEINTERVAL;  Timer.OnTimer := @OnTimer;end; destructor TLazAutoSave.Destroy;begin  Timer.Enabled := False;  Timer.Free;end; initialization  AutoSave := TLazAutoSave.Create; finalization  FreeAndNil(AutoSave); end. ---------------------------------------------------------------------- balazsszekely · 2018-04-25 20:36 QuoteI want to AutoSave existing files, if it has sense..Yes it make sense and it shouldn't be to difficult to implement. When you create your class(TLazAutoSave) add the following handler: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ProjectIntf;  TLazAutoSave = class(TObject)  private    Timer: TTimer;    FCurProject: TLazProject; //add this    procedure OnTimer(Sender: TObject);    function OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;  //add this  public    constructor Create;    destructor Destroy;  end; //... function TLazAutoSave.OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;begin  Result := mrOk;  FCurProject := AProject;end; constructor TLazAutoSave.Create;begin  Timer := TTimer.Create(nil);  Timer.Interval := AUTOSAVEINTERVAL;  Timer.OnTimer := @OnTimer;  LazarusIDE.AddHandlerOnProjectOpened(@OnProjectOpened);end; procedure TLazAutoSave.OnTimer(Sender: TObject);begin  if not FCurProject.IsVirtual then  //isvirtual = not saved yet    IDECommands.ExecuteIDECommand(Self, ecSaveAll);end; ---------------------------------------------------------------------- lainz · 2018-04-25 21:51 Thanks @GetMem. I've changed it a bit in order to work, because adding the code OnCreate did not work, because the instance of LazarusIDE was not available yet. This is the package: https://github.com/lainz/LazAutoSave And you are one of the Authors, I just copy-paste your code  The thing that doesn't work is when Lazarus is started, in the last opened project. Maybe is not triggered, or at that time is not available to do the register of the event. I don't know. But reopening the last project does the trick. And another bug, the same bug of the multiple "save dialogs" when saving for the first time a file of a project, but I will figure how to solve it. Solved. ---------------------------------------------------------------------- JuhaManninen · 2018-04-25 22:24 Maybe: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LazarusIDE.DoSaveAll([sfDoNotSaveVirtualFiles, sfCanAbort, sfQuietUnitCheck]); ---------------------------------------------------------------------- lainz · 2018-04-25 22:25 Thanks. I will test it tomorrow, but that code looks fine =) ---------------------------------------------------------------------- JuhaManninen · 2018-04-25 22:43 You don't need the project if you just want to save files. ---------------------------------------------------------------------- lainz · 2018-04-25 23:06 I see, now I understand, that will solve also the problem of "last opened project" don't doing autosave. Tomorrow I will update the repository, and maybe with a menu item to check it on and off will be enough. Edit: Can't wait, I already tested it. Thanks, it works perfectly now ---------------------------------------------------------------------- BosseB · 2020-02-05 17:03 Quote from: lainz on April 25, 2018, 11:51:25 pmThanks @GetMem. I've changed it a bit in order to work, because adding the code OnCreate did not work, because the instance of LazarusIDE was not available yet. This is the package: https://github.com/lainz/LazAutoSave ... And another bug, the same bug of the multiple "save dialogs" when saving for the first time a file of a project, but I will figure how to solve it. Solved. I am in desperate need of an Autosave function in the Lazarus IDE because the IDE keeps crashing several times a day when working via VNC in Lazarus on a Raspberry Pi4. All edits since last save are lost at this time so it is very frustrating... I located this thread and it contains the solution to my problem! And I realize it is almost 2 years old but I have a clarification question on how to use it: After downloading the sources from GitHub, how to apply them? I have never made changes to Lazarus except for installing packages like IndyLaz via OnLine Package Manager. What are the steps to get this installed with Lazarus? EDIT: I had not looked in OLPM yet, but now I did so and found this Autosave package in OLPM! Just select it and click Install! So I have happily done that and set it for 10s interval. Hopefully this will save my day when the next crash happens!