Recent

Author Topic: IDE pkg, basic attempt works, need a little help for a IDE right click menu  (Read 859 times)

frakno

  • Jr. Member
  • **
  • Posts: 93
hello, I fail despite many attempts to create and install an IDE package.
Icreated a new package, and i have the three files below.
when i compile and install it my lazarus doesnt start.

Please can you help me

jfcustomidetools.lpk:
Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <CONFIG>
  3.   <Package Version="5">
  4.     <PathDelim Value="\"/>
  5.     <Name Value="jfCustomIDETools"/>
  6.     <Type Value="RunAndDesignTime"/>
  7.     <CompilerOptions>
  8.       <Version Value="11"/>
  9.       <PathDelim Value="\"/>
  10.       <SearchPaths>
  11.         <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)\"/>
  12.       </SearchPaths>
  13.     </CompilerOptions>
  14.     <Files>
  15.       <Item>
  16.         <Filename Value="jfcustomidetoolsunit.pas"/>
  17.         <UnitName Value="jfCustomIDEToolsUnit"/>
  18.       </Item>
  19.     </Files>
  20.     <RequiredPkgs>
  21.       <Item>
  22.         <PackageName Value="SynEdit"/>
  23.       </Item>
  24.       <Item>
  25.         <PackageName Value="IDEIntf"/>
  26.       </Item>
  27.       <Item>
  28.         <PackageName Value="LCL"/>
  29.       </Item>
  30.       <Item>
  31.         <PackageName Value="LCLBase"/>
  32.       </Item>
  33.       <Item>
  34.         <PackageName Value="FCL"/>
  35.       </Item>
  36.     </RequiredPkgs>
  37.     <UsageOptions>
  38.       <UnitPath Value="$(PkgOutDir)"/>
  39.     </UsageOptions>
  40.     <PublishOptions>
  41.       <Version Value="2"/>
  42.       <UseFileFilters Value="True"/>
  43.     </PublishOptions>
  44.   </Package>
  45. </CONFIG>
  46.  
jfcustomidetools.pas:
Code: Pascal  [Select][+][-]
  1. { This file was automatically created by Lazarus. Do not edit!
  2.   This source is only used to compile and install the package.
  3.  }
  4.  
  5. unit jfCustomIDETools;
  6.  
  7. {$warn 5023 off : no warning about unused units}
  8. interface
  9.  
  10. uses
  11.    jfCustomIDEToolsUnit, LazarusPackageIntf;
  12.  
  13. implementation
  14.  
  15. procedure Register;
  16. begin
  17. end;
  18.  
  19. initialization
  20.   RegisterPackage ( 'jfCustomIDETools', @Register ) ;
  21. end.
  22.  
jfcustomidetoolsunit.pas:
Code: Pascal  [Select][+][-]
  1. unit jfCustomIDEToolsUnit;
  2.  
  3. {$MODE objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Controls, Forms, LCL, LCLIntf, MenuIntf, Menus, SynEdit, SynEditMiscProcs, SynEditTypes, SysUtils;
  9.  
  10. procedure MoveLineUp(Sender: TObject);
  11.  
  12. implementation
  13.  
  14. procedure MoveLineUp(Sender: TObject);
  15. var
  16.   SynEdit: TSynEdit;
  17.   LineIndex: Integer;
  18.   LineText: string;
  19. begin
  20.   if Screen.ActiveControl is TSynEdit then
  21.   begin
  22.     SynEdit := TSynEdit(Screen.ActiveControl);
  23.     LineIndex := SynEdit.CaretY - 1;
  24.     if LineIndex > 0 then
  25.     begin
  26.       LineText := SynEdit.Lines[LineIndex];
  27.       SynEdit.Lines.Delete(LineIndex);
  28.       SynEdit.Lines.Insert(LineIndex - 1, LineText);
  29.       SynEdit.CaretY := LineIndex;
  30.     end;
  31.   end;
  32. end;
  33.  
  34. procedure RegisterMyCommands;
  35. var
  36.   EditMenu: TIDEMenuSection;
  37. begin
  38.   EditMenu := RegisterIDEMenuSection(mnuEdit, 'MyEditSection');
  39.   RegisterIDEMenuCommand(EditMenu, 'MoveLineUp', 'Move Line Up', nil, @MoveLineUp);
  40. end;
  41.  
  42. initialization
  43.   RegisterMyCommands;
  44.  
  45. end.
  46.  
  47.  


« Last Edit: January 30, 2025, 07:26:04 pm by frakno »
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: IDE package, I fail to create one
« Reply #1 on: January 28, 2025, 05:17:55 pm »
Just a remark/confirmation.

You are attempting to do this on windows 10 with Lazarus 1.6 and FPC 3.0 ?
Today is tomorrow's yesterday.

frakno

  • Jr. Member
  • **
  • Posts: 93
Re: IDE package, I fail to create one
« Reply #2 on: January 28, 2025, 05:50:55 pm »
sorry, I haven't changed it yet, now it is lazarus 4.0 with FPC 3.2.2.

I would be very grateful if someone could provide me with a simple working example
« Last Edit: January 28, 2025, 05:53:56 pm by frakno »
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: IDE package, I fail to create one
« Reply #3 on: January 29, 2025, 12:37:50 pm »
You should start learning from the wiki:
* https://wiki.freepascal.org/Lazarus_Packages
* https://wiki.freepascal.org/How_To_Write_Lazarus_Component
* https://wiki.freepascal.org/Extending_the_IDE

You can also study examples of existing packages in the "Lazarus/Components" folder, or use the Online Package Manager - there are many simple packages that can be easily remade to provide the functionality you need.

frakno

  • Jr. Member
  • **
  • Posts: 93
Re: IDE package, I fail to create one
« Reply #4 on: January 29, 2025, 01:13:16 pm »
Thanks for your links,
but believe me, I've already studied all of this.
I've tried everything for days to create my above code as a simple IDE editor extension, with integration of a menu entry for the movelineup and/or integration of a symbol for it in the toolbar in a package.
but I failed.
believe me, I'm not the kind of person who wants to have solutions from others without a lot of effort.
I can therefore only give up on this plan or someone can help me with a working minimal package example.
Thank you in advance for my potential savior.
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: IDE package, I fail to create one
« Reply #5 on: January 30, 2025, 06:19:03 pm »
In general, if we are talking about functionality, then the IDE already has commands for moving lines. Just use [Ctrl+Alt+Shift+Down/Up]. This command is not in the menu, but it is in the IDE commands, which you can find in the window: Main Menu > Tools > Options, in the Editor > Key Mappings category.

As for the package, the error is that in the main unit (jfcustomidetools.pas) of the package the procedure Register is empty, but in another unit the procedure RegisterMyCommands is created, which is called in the initialization section. At the time of initialization of units, not all of its IDE objects have been created yet, so calling RegisterIDEMenuSection leads to an error.

Therefore, packages are first registered using RegisterPackage and pass it a pointer to their registration procedure, which will be called at the right time (and will call the registration procedures of their units).

But you can't change the main unit of the package manually either, since it is recreated automatically, erasing all changes. You need to use the IDE interface to add units. In short:
* Remove RegisterMyCommands from the initialization section.
* Rename RegisterMyCommands to Register.
* In the Package Editor, select this unit and check the "Register unit" checkbox at the bottom.

frakno

  • Jr. Member
  • **
  • Posts: 93
Re: IDE package, I fail to create one
« Reply #6 on: January 30, 2025, 07:22:32 pm »
Thank you very much.

I know the IDE commands so far, but this is a first attempt and then i will add more complex functions of my own.
Yust now, I've got the package working.

I'm now trying to create a right-click menu for the commands.

I'd be very grateful for help with this too.

My working Code:
Code: Pascal  [Select][+][-]
  1. unit fExtendIde;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.    Classes, Controls, Dialogs, IDECommands, IDEMsgIntf, LCLProc, LCLType, MenuIntf,
  9.    SrcEditorIntf, SynEdit, SysUtils, Menus;
  10.  
  11. type
  12.    TIDEExtensions = class
  13.       private
  14.       public
  15.          procedure ExchangeLineUp(Sender: TObject);
  16.          procedure ExchangeLineDown(Sender: TObject);
  17.          procedure DuplicateLine(Sender: TObject);
  18.       end;
  19.  
  20. procedure Register;
  21.  
  22. implementation
  23.  
  24. { TIDEExtensions }
  25.  
  26. procedure TIDEExtensions.ExchangeLineUp(Sender: TObject);
  27. var
  28.   Editor: TSourceEditorInterface;
  29.   ASynEdit: TSynEdit;
  30.   tempStr: String;
  31.   y: longint;
  32. begin
  33.   Editor := SourceEditorManagerIntf.ActiveEditor;
  34.   if Editor = nil then
  35.     exit;
  36.   if Editor.EditorControl is TSynEdit then
  37.   begin
  38.     ASynEdit := TSynEdit(Editor.EditorControl);
  39.     if ASynEdit.CaretY - 1 >= 1 then
  40.     begin
  41.       y := ASynEdit.CaretY;
  42.       tempStr := ASynEdit.LineText;
  43.       ASynEdit.TextBetweenPoints[Point(1, y), Point(1, y + 1)] := ASynEdit.Lines.Strings[y - 2] + LineEnding;
  44.       ASynEdit.TextBetweenPoints[Point(1, y - 1), Point(1, y)] := tempStr + LineEnding;
  45.       ASynEdit.CaretY := y - 1;
  46.     end;
  47.   end;
  48. end;
  49.  
  50. procedure TIDEExtensions.ExchangeLineDown(Sender: TObject);
  51. var
  52.   Editor: TSourceEditorInterface;
  53.   ASynEdit: TSynEdit;
  54.   y: longint;
  55.   tempStr: String;
  56. begin
  57.   Editor := SourceEditorManagerIntf.ActiveEditor;
  58.   if Editor = nil then
  59.     exit;
  60.   if Editor.EditorControl is TSynEdit then
  61.   begin
  62.     ASynEdit := TSynEdit(Editor.EditorControl);
  63.     if (ASynEdit.CaretY + 1 <= ASynEdit.Lines.Count) then
  64.     begin
  65.       y := ASynEdit.CaretY;
  66.       tempStr := ASynEdit.LineText;
  67.       ASynEdit.TextBetweenPoints[Point(1, y), Point(1, y + 1)] := ASynEdit.Lines.Strings[y] + LineEnding;
  68.       ASynEdit.TextBetweenPoints[Point(1, y + 1), Point(1, y + 2)] := tempStr + LineEnding;
  69.       ASynEdit.CaretY := y + 1;
  70.     end;
  71.   end;
  72. end;
  73.  
  74. procedure TIDEExtensions.DuplicateLine(Sender: TObject);
  75. var
  76.   Editor: TSourceEditorInterface;
  77.   ASynEdit: TSynEdit;
  78. begin
  79.   Editor := SourceEditorManagerIntf.ActiveEditor;
  80.   if Editor = nil then
  81.     exit;
  82.   if Editor.EditorControl is TSynEdit then
  83.   begin
  84.     ASynEdit := TSynEdit(Editor.EditorControl);
  85.     ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY)] := ASynEdit.LineText + LineEnding;
  86.   end;
  87. end;
  88.  
  89. procedure Register;
  90. var
  91.   Key: TIDEShortCut;
  92.   Cat: TIDECommandCategory;
  93.   SectionLines: TIDEMenuSection;
  94.   SectionLinesMenu: TIDEMenuSection;
  95.   DuplicateCmd: TIDECommand;
  96.   ExchangeLineUpCmd: TIDECommand;
  97.   ExchangeLineDownCmd: TIDECommand;
  98.   {%H-}IDEExtensions: TIDEExtensions;
  99. begin
  100.   IDEExtensions := TIDEExtensions.Create;
  101.  
  102.   SectionLines := RegisterIDEMenuSection(mnuEdit, 'Lines');
  103.   SectionLinesMenu := RegisterIDESubMenu(SectionLines, 'Lines', 'Lines');
  104.  
  105.   Key := IDEShortCut(VK_UP, [ssCtrl, ssShift], VK_UNKNOWN, []);
  106.   Cat := IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
  107.   ExchangeLineUpCmd := RegisterIDECommand(Cat, 'ExchangeLineUpCmd', 'Move up', Key, @IDEExtensions.ExchangeLineUp);
  108.   RegisterIDEMenuCommand(SectionLinesMenu, 'ExchangeLineUpCmd', 'Move line up', nil, nil, ExchangeLineUpCmd);
  109.  
  110.   Key := IDEShortCut(VK_DOWN, [ssCtrl, ssShift], VK_UNKNOWN, []);
  111.   ExchangeLineDownCmd := RegisterIDECommand(Cat, 'ExchangeLineDownCmd', 'Move down', Key, @IDEExtensions.ExchangeLineDown);
  112.   RegisterIDEMenuCommand(SectionLinesMenu, 'ExchangeLineDownCmd', 'Move line down', nil, nil, ExchangeLineDownCmd);
  113.  
  114.   Key := IDEShortCut(VK_Q, [ssCtrl], VK_UNKNOWN, []);
  115.   DuplicateCmd := RegisterIDECommand(Cat, 'Duplicate Line', 'Duplicate a Line', Key, @IDEExtensions.DuplicateLine);
  116.   RegisterIDEMenuCommand(SectionLinesMenu, 'DuplicateLine', 'Duplicate a Line', nil, nil, DuplicateCmd);
  117.  
  118. end;
  119.  
  120. end.
  121.  
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

n7800

  • Sr. Member
  • ****
  • Posts: 266
As I said, you can find information in the sources of the Lazarus packages themselves - this is even better than reading outdated articles on the wiki.

For example, I remember that the Cody package adds items to both the main menu and the context menu of the editor. So I easily found out how it does it. As you can see, you just need to specify the desired menu/section/submenu when calling RegisterIDEMenuCommand, the declaration of which can be found here.

frakno

  • Jr. Member
  • **
  • Posts: 93
Thank you, this looks good.
I'll see if I can figure it out
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

 

TinyPortal © 2005-2018