Recent

Author Topic: [Solved] Making AutoSave plugin: problems  (Read 4491 times)

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
[Solved] Making AutoSave plugin: problems
« on: April 25, 2018, 09:57:11 pm »
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  8-)

I want to AutoSave existing files, if it has sense..

This is the code:
Code: Pascal  [Select][+][-]
  1. unit lazautosaveunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, IDECommands, ExtCtrls;
  9.  
  10. const
  11.   AUTOSAVEINTERVAL = 1000; // ms
  12.  
  13. type
  14.  
  15.   { TLazAutoSave }
  16.  
  17.   TLazAutoSave = class(TObject)
  18.   private
  19.     Timer: TTimer;
  20.     procedure OnTimer(Sender: TObject);
  21.   public
  22.     constructor Create;
  23.     destructor Destroy;
  24.   end;
  25.  
  26. implementation
  27.  
  28. var
  29.   AutoSave: TLazAutoSave;
  30.  
  31. { TLazAutoSave }
  32.  
  33. procedure TLazAutoSave.OnTimer(Sender: TObject);
  34. begin
  35.   IDECommands.ExecuteIDECommand(Self, ecSaveAll);
  36. end;
  37.  
  38. constructor TLazAutoSave.Create;
  39. begin
  40.   Timer := TTimer.Create(nil);
  41.   Timer.Interval := AUTOSAVEINTERVAL;
  42.   Timer.OnTimer := @OnTimer;
  43. end;
  44.  
  45. destructor TLazAutoSave.Destroy;
  46. begin
  47.   Timer.Enabled := False;
  48.   Timer.Free;
  49. end;
  50.  
  51. initialization
  52.   AutoSave := TLazAutoSave.Create;
  53.  
  54. finalization
  55.   FreeAndNil(AutoSave);
  56.  
  57. end.
  58.        
« Last Edit: April 26, 2018, 12:16:30 am by lainz »

balazsszekely

  • Guest
Re: Making AutoSave plugin: problems
« Reply #1 on: April 25, 2018, 10:36:10 pm »
Quote
I 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][+][-]
  1. uses ProjectIntf;
  2.  
  3.  TLazAutoSave = class(TObject)
  4.   private
  5.     Timer: TTimer;
  6.     FCurProject: TLazProject; //add this
  7.     procedure OnTimer(Sender: TObject);
  8.     function OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;  //add this
  9.   public
  10.     constructor Create;
  11.     destructor Destroy;
  12.   end;
  13.  
  14. //...
  15.  
  16. function TLazAutoSave.OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;
  17. begin
  18.   Result := mrOk;
  19.   FCurProject := AProject;
  20. end;
  21.  
  22. constructor TLazAutoSave.Create;
  23. begin
  24.   Timer := TTimer.Create(nil);
  25.   Timer.Interval := AUTOSAVEINTERVAL;
  26.   Timer.OnTimer := @OnTimer;
  27.   LazarusIDE.AddHandlerOnProjectOpened(@OnProjectOpened);
  28. end;
  29.  
  30. procedure TLazAutoSave.OnTimer(Sender: TObject);
  31. begin
  32.   if not FCurProject.IsVirtual then  //isvirtual = not saved yet
  33.     IDECommands.ExecuteIDECommand(Self, ecSaveAll);
  34. end;
  35.  
  36.  

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Making AutoSave plugin: problems
« Reply #2 on: April 25, 2018, 11:51:25 pm »
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.
« Last Edit: April 26, 2018, 12:16:15 am by lainz »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: [Solved] Making AutoSave plugin: problems
« Reply #3 on: April 26, 2018, 12:24:10 am »
Maybe:
Code: Pascal  [Select][+][-]
  1. LazarusIDE.DoSaveAll([sfDoNotSaveVirtualFiles, sfCanAbort, sfQuietUnitCheck]);
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: [Solved] Making AutoSave plugin: problems
« Reply #4 on: April 26, 2018, 12:25:25 am »
Thanks. I will test it tomorrow, but that code looks fine =)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: [Solved] Making AutoSave plugin: problems
« Reply #5 on: April 26, 2018, 12:43:43 am »
You don't need the project if you just want to save files.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: [Solved] Making AutoSave plugin: problems
« Reply #6 on: April 26, 2018, 01:06:13 am »
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  :D
« Last Edit: April 26, 2018, 02:09:58 am by lainz »

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Making AutoSave plugin: problems
« Reply #7 on: February 05, 2020, 06:03:32 pm »
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 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!


« Last Edit: February 05, 2020, 06:30:18 pm by BosseB »
--
Bo Berglund
Sweden

 

TinyPortal © 2005-2018