Recent

Author Topic: How to stop macro execution ?  (Read 1016 times)

440bx

  • Hero Member
  • *****
  • Posts: 5589
How to stop macro execution ?
« on: June 17, 2025, 07:08:23 pm »
Hello,

In a normal Pascal program, the procedure Halt can be used to stop the program's execution.

Is there something like Halt that stops the execution of a SynEdit macro ?  I tried Halt but, it didn't like it.

Thank you for your help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11463
  • Debugger - SynEdit - and more
    • wiki
Re: How to stop macro execution ?
« Reply #1 on: June 17, 2025, 07:41:15 pm »
I don't (didn't) know.

But I followed an idea: exit;
That seems to do it.

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: How to stop macro execution ?
« Reply #2 on: June 17, 2025, 07:46:46 pm »
Yes, PascalScript (used in SynEdit macros) does not implement the full Delphi/Lazarus RTL.
Halt is a compiler intrinsic in native Pascal, not available in the PascalScript environment.

I use PascalScript in my applications and do something like this:

Code: Pascal  [Select][+][-]
  1. procedure RunMacro;
  2. begin
  3.   // Macro logic here...
  4.   if ShouldStop then
  5.     Exit; // Exits the procedure
  6.   // Code after exit won't run
  7. end;
  8.  
  9. begin
  10.   RunMacro; // Execute the procedure
  11. end.
  12.  

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11463
  • Debugger - SynEdit - and more
    • wiki
Re: How to stop macro execution ?
« Reply #3 on: June 17, 2025, 07:57:28 pm »
Actually, exit appears to even work in the main begin end. block

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: How to stop macro execution ?
« Reply #4 on: June 17, 2025, 08:02:08 pm »
@Martin_fr. You're right, that's the way it is.  :)

440bx

  • Hero Member
  • *****
  • Posts: 5589
Re: How to stop macro execution ?
« Reply #5 on: June 17, 2025, 08:40:11 pm »
Yes, exit is basically Halt when used in the main begin end block.

The problem is that in some cases I painted myself in a corner.  The macro detects a problem for which there is no solution and should stop execution but, it is several layers deep in the execution where "exit" will just go up one layer, without stopping execution.

I implemented a "band-aid", simply a message saying that a fatal problem was found.  Execution continues but, now I know the result is wrong even when it is sometimes not obviously wrong.

Thank you Martin and LV.  I appreciate your input.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11463
  • Debugger - SynEdit - and more
    • wiki
Re: How to stop macro execution ?
« Reply #6 on: June 17, 2025, 08:57:35 pm »
Code: Pascal  [Select][+][-]
  1. var
  2. a: integer;
  3. begin
  4.   a := 1;
  5.   a := 1 div (a-1);
  6. end;
  7.  

440bx

  • Hero Member
  • *****
  • Posts: 5589
Re: How to stop macro execution ?
« Reply #7 on: June 17, 2025, 09:19:45 pm »
Dividing by zero does the trick.  Stops the macro execution on its tracks.

Thank you Martin, that's very useful.

I simply created a procedure:
Code: Pascal  [Select][+][-]
  1. procedure Halt();
  2.   { halts macro execution                               }
  3. var
  4.   i : integer;
  5. begin
  6.   i := i div 0;
  7. end;
  8.  
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: How to stop macro execution ?
« Reply #8 on: June 18, 2025, 05:50:38 pm »
Dividing by zero does the trick.  Stops the macro execution on its tracks.

Thank you Martin, that's very useful.

I simply created a procedure:
Code: Pascal  [Select][+][-]
  1. procedure Halt();
  2.   { halts macro execution                               }
  3. var
  4.   i : integer;
  5. begin
  6.   i := i div 0;
  7. end;
  8.  

This is an interesting trick.  :) But you can do it differently (see attached project).

Pascal Script:

Code: Pascal  [Select][+][-]
  1. var
  2.   i: integer;
  3. begin
  4.   for i := 1 to 5 do
  5.   begin
  6.     Writeln(IntToStr(i));
  7.     if i = 3 then Halt();
  8.   end;
  9.   Writeln('Hello, World!');
  10. end.
  11.  

Output:

Code: Text  [Select][+][-]
  1. 1
  2. 2
  3. 3
  4. Halt;
  5.  
« Last Edit: June 18, 2025, 06:06:29 pm by LV »

440bx

  • Hero Member
  • *****
  • Posts: 5589
Re: How to stop macro execution ?
« Reply #9 on: June 18, 2025, 06:08:26 pm »
This is an interesting trick.  :) But you can do it differently (see attached project).
I need it to work in a macro and the real "halt" isn't available in macros.

Did I miss your point ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: How to stop macro execution ?
« Reply #10 on: June 18, 2025, 06:12:49 pm »
I need it to work in a macro and the real "halt" isn't available in macros.

Did I miss your point ?

*Halt* is defined in the code of the attached project.


440bx

  • Hero Member
  • *****
  • Posts: 5589
Re: How to stop macro execution ?
« Reply #11 on: June 18, 2025, 06:55:11 pm »
I need it to work in a macro and the real "halt" isn't available in macros.

Did I miss your point ?

*Halt* is defined in the code of the attached project.
I must be missing something.  My interest is in using "Halt" in a Synedit _macro_.  The code you posted is not "macro-acceptable" since "Halt" is not available in macros and writeln isn't either.

I don't follow what you're saying, you lost me.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: How to stop macro execution ?
« Reply #12 on: June 18, 2025, 07:10:02 pm »
I'm sorry for any confusion. I provided an example of how to achieve this in PascalScript, assuming that, since SynEdit macros are based on PascalScript, something similar can be done there as well.

440bx

  • Hero Member
  • *****
  • Posts: 5589
Re: How to stop macro execution ?
« Reply #13 on: June 18, 2025, 07:39:44 pm »
I'm sorry for any confusion. I provided an example of how to achieve this in PascalScript, assuming that, since SynEdit macros are based on PascalScript, something similar can be done there as well.
Now, I get you.

The problem is that PascalScript in a macro is a small subset of the full PascalScript language.  No Halt, no writeln and no-many-other-things.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11463
  • Debugger - SynEdit - and more
    • wiki
Re: How to stop macro execution ?
« Reply #14 on: June 18, 2025, 08:06:44 pm »
If there are useful (for macros) features in the PascalScript package, and all they need is to be "enabled" (E.g. by calling some register procedure, or using some existing unit), then please provide patches or at least detailed info.

Mind you: Useful for macros.

Adding TForm is not useful for macros. Yes sure you could have an entire app running as macro, but that is not the purpose macros have (also any interactivity in macros must be modal / you can't have a macros that stays active in the background). A macro runs as a whole, and must complete and exit, before the IDE can do anything else.

If you need more, then it is time to look at IdeIntf, and write an add on (it's not that hard / just search an existing for how to register a menu or keycombo, and then use the Intf packages eg for SourceEdit)

 

TinyPortal © 2005-2018