Discussion:
Extending the IDE
Reinier Napoles Martinez
2012-01-29 06:49:17 UTC
Permalink
Hello list,

I started to study, how to extend the IDE.
so far I have managed to add a menu to the IDE.
How I can override the default behavior of the editor when I press Ctrl + D to run my code.


thanks.

This is my code.

unit DuplicateLine;

{$mode objfpc}{$H+}

interface


uses
Classes,Controls,SysUtils,IDECommands,MenuIntf,LCLType,SrcEditorIntf,SynEdit;


procedure DuplicateLine(Sender: TObject);
procedure Register;

implementation


procedure DuplicateLine(Sender: TObject);
var
Editor: TSourceEditorInterface;
ASynEdit: TSynEdit;
begin

Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
ASynEdit.Lines.Insert(ASynEdit.CaretY, ASynEdit.LineText);
end;


end;


procedure Register;
var
Key: TIDEShortCut;
Cat: TIDECommandCategory;
DuplicateCmd: TIDECommand;
begin
Key := IDEShortCut(VK_D,[ssCtrl],VK_UNKNOWN,[]);
Cat:=IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
DuplicateCmd := RegisterIDECommand(Cat,'Duplicate Line', 'Duplicate a Line', Key, nil, @DuplicateLine);
RegisterIDEMenuCommand(itmEditBlockIndentation, 'DuplicateLine', 'Duplicate a Line', nil, nil, DuplicateCmd);

end;


end.
--
*********************************************************************
En la tierra hace falta personas que trabajen más y critiquen menos,
que construyan más y destruyan menos, que prometan menos y
resuelvan más que esperen recibir menos y dar más que digan mejor
ahora que mañana.
Che
*********************************************************************

--
Mattias Gaertner
2012-01-29 10:43:46 UTC
Permalink
On Sun, 29 Jan 2012 01:49:17 -0500 (CST)
Post by Reinier Napoles Martinez
Hello list,
I started to study, how to extend the IDE.
so far I have managed to add a menu to the IDE.
How I can override the default behavior of the editor when I press Ctrl + D to run my code.
The defaults are fixed and the user settings are loaded before the
packages are registered. There is currently no way to alter them.

I added a function to search for all commands with some keys.

http://wiki.lazarus.freepascal.org/Extending_the_IDE#Shortcuts
Post by Reinier Napoles Martinez
thanks.
This is my code.
unit DuplicateLine;
{$mode objfpc}{$H+}
interface
uses
Classes,Controls,SysUtils,IDECommands,MenuIntf,LCLType,SrcEditorIntf,SynEdit;
procedure DuplicateLine(Sender: TObject);
procedure Register;
implementation
procedure DuplicateLine(Sender: TObject);
var
Editor: TSourceEditorInterface;
ASynEdit: TSynEdit;
begin
Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
ASynEdit.Lines.Insert(ASynEdit.CaretY, ASynEdit.LineText);
end;
end;
procedure Register;
var
Key: TIDEShortCut;
Cat: TIDECommandCategory;
DuplicateCmd: TIDECommand;
begin
Key := IDEShortCut(VK_D,[ssCtrl],VK_UNKNOWN,[]);
Cat:=IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
RegisterIDEMenuCommand(itmEditBlockIndentation, 'DuplicateLine', 'Duplicate a Line', nil, nil, DuplicateCmd);
end;
:)

Mattias

--
Martin
2012-01-29 12:54:27 UTC
Permalink
Post by Reinier Napoles Martinez
procedure DuplicateLine(Sender: TObject);
var
Editor: TSourceEditorInterface;
ASynEdit: TSynEdit;
begin
Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
ASynEdit.Lines.Insert(ASynEdit.CaretY, ASynEdit.LineText);
end;
Not the answer to your question, but...

SynEdit.Lines.Insert
does not work with undo/redo

Lazarus Synedit has
SynEdit.TextBetweenPoints[ Point(1, Y), Point(1, Y)] := 'Foo'+LineEnding;



--
Reinier Napoles Martinez
2012-01-30 02:38:00 UTC
Permalink
Post by Martin
Not the answer to your question, but...
SynEdit.Lines.Insert
does not work with undo/redo
Lazarus Synedit has
SynEdit.TextBetweenPoints[ Point(1, Y), Point(1, Y)] :=
'Foo'+LineEnding;
thank you very much for the tip , martin

--
_______________________________________________ Lazarus mailing list
***@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
--
*********************************************************************
En la tierra hace falta personas que trabajen más y critiquen menos,
que construyan más y destruyan menos, que prometan menos y
resuelvan más que esperen recibir menos y dar más que digan mejor
ahora que mañana.
Che
*********************************************************************

--
Reinier Napoles Martinez
2012-01-30 02:57:33 UTC
Permalink
Post by Mattias Gaertner
The defaults are fixed and the user settings are loaded before the
packages are registered. There is currently no way to alter them.
I added a function to search for all commands with some keys.
http://wiki.lazarus.freepascal.org/Extending_the_IDE#Shortcuts
Ok.
thank you very much for your help..
--
*********************************************************************
En la tierra hace falta personas que trabajen más y critiquen menos,
que construyan más y destruyan menos, que prometan menos y
resuelvan más que esperen recibir menos y dar más que digan mejor
ahora que mañana.
Che
*********************************************************************

--
Reinier Napoles Martinez
2012-01-30 05:08:41 UTC
Permalink
Martin how can use the property TextBetweenPoints to exchange lines.
I tried but not work me.

I have this code:
tempStr := ASynEdit.LineText;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY)]:= ASynEdit.Lines.Strings[ASynEdit.CaretY - 2] +LineEnding;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY-1), Point(1, ASynEdit.CaretY-1)]:=tempStr+LineEnding;

previously used this code:

procedure MoveLineUp(Sender: TObject);
begin
if CustSynEdit.CaretY -1 >= 1 then
begin
CustSynEdit.Lines.Exchange(CustSynEdit.CaretY -1 ,CustSynEdit.CaretY-2);
CustSynEdit.CaretY:=CustSynEdit.CaretY-1;
end;
end;


procedure MoveLineDown(Sender: TObject);
begin
if ( CustSynEdit.CaretY + 1 <= CustSynEdit.Lines.Count) then
begin
CustSynEdit.Lines.Exchange(CustSynEdit.CaretY-1,CustSynEdit.CaretY);
CustSynEdit.CaretY:=CustSynEdit.CaretY+1;
end;
end;

which has the same problem as you pointed out to me.

thanks.
--
*********************************************************************
En la tierra hace falta personas que trabajen más y critiquen menos,
que construyan más y destruyan menos, que prometan menos y
resuelvan más que esperen recibir menos y dar más que digan mejor
ahora que mañana.
Che
*********************************************************************

--
Martin
2012-01-30 12:02:37 UTC
Permalink
Post by Reinier Napoles Martinez
Martin how can use the property TextBetweenPoints to exchange lines.
I tried but not work me.
tempStr := ASynEdit.LineText;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY)]:= ASynEdit.Lines.Strings[ASynEdit.CaretY - 2] +LineEnding;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY-1), Point(1, ASynEdit.CaretY-1)]:=tempStr+LineEnding;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY)]


Inserts the new text at the begin of the line (x=1). but start and stop
of the old text are equal, so nothing is replaced.

ASynEdit.TextBetweenPoints

is lixe
- set selection (or no selection
- delete selection
- insert new
- restore old selection

selecting a full line:

ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY + 1)]



additionally ASynEdit.TextBetweenPointsEx can adjust the caret pos


y := ASynEdit.CaretY;
tempStr := ASynEdit.LineText;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY + 1)]:= ASynEdit.Lines.Strings[ASynEdit.CaretY - 2] +LineEnding;
ASynEdit.TextBetweenPoints[Point(1, ASynEdit.CaretY-1), Point(1, ASynEdit.CaretY)]:=tempStr+LineEnding;
Post by Reinier Napoles Martinez
procedure MoveLineUp(Sender: TObject);
begin
if CustSynEdit.CaretY -1>= 1 then
begin
CustSynEdit.Lines.Exchange(CustSynEdit.CaretY -1 ,CustSynEdit.CaretY-2);
CustSynEdit.CaretY:=CustSynEdit.CaretY-1;
end;
end;
procedure MoveLineDown(Sender: TObject);
begin
if ( CustSynEdit.CaretY + 1<= CustSynEdit.Lines.Count) then
begin
CustSynEdit.Lines.Exchange(CustSynEdit.CaretY-1,CustSynEdit.CaretY);
CustSynEdit.CaretY:=CustSynEdit.CaretY+1;
end;
end;
which has the same problem as you pointed out to me.
thanks.
--
Reinier Napoles Martinez
2012-02-02 23:05:38 UTC
Permalink
thanks martin and mattias. My small expert is this way.


unit Unit1;

{$mode objfpc}{$H+}

interface


uses
Classes,Controls, SysUtils,IDECommands,MenuIntf,Dialogs,LCLType,SrcEditorIntf,SynEdit,LCLProc,IDEMsgIntf;

procedure ExchangeLineUp(Sender: TObject);
procedure ExchangeLineDown(Sender: TObject);

procedure DuplicateLine(Sender: TObject);
procedure Register;

implementation

procedure ExchangeLineUp(Sender: TObject);
var
Editor: TSourceEditorInterface;
ASynEdit: TSynEdit;
tempStr:String;
tempStr1:String;
y: LongInt;
begin

Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
if ASynEdit.CaretY - 1 >= 1 then
begin
y := ASynEdit.CaretY;
tempStr := ASynEdit.LineText;
ASynEdit.TextBetweenPoints[Point(1, y), Point(1, y + 1)]:= ASynEdit.Lines.Strings[y - 2] +LineEnding;
ASynEdit.TextBetweenPoints[Point(1, y-1), Point(1, y)]:=tempStr+LineEnding;
ASynEdit.CaretY := y - 1;
end;
end;
end;

procedure ExchangeLineDown(Sender: TObject);
var
Editor: TSourceEditorInterface;
ASynEdit: TSynEdit;
y: LongInt;
tempStr: String;
begin
Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
if (ASynEdit.CaretY + 1 <= ASynEdit.Lines.Count) then
begin
y:= ASynEdit.CaretY;
tempStr := ASynEdit.LineText;
ASynEdit.TextBetweenPoints[Point(1, y), Point(1, y+1)]:= ASynEdit.Lines.Strings[y] +LineEnding;
ASynEdit.TextBetweenPoints[Point(1, y+1), Point(1, y+2)]:=tempStr+LineEnding;
ASynEdit.CaretY := y + 1;
end;
end;
end;

procedure DuplicateLine(Sender: TObject);
var
Editor: TSourceEditorInterface;
TextPos: TPoint;
ScreenPos: TPoint;
Filename: String;
ASynEdit: TSynEdit;
begin
Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
if Editor.EditorControl is TSynEdit then begin
ASynEdit:=TSynEdit(Editor.EditorControl);
ASynEdit.TextBetweenPoints[ Point(1, ASynEdit.CaretY), Point(1, ASynEdit.CaretY)] := ASynEdit.LineText+LineEnding;
end;

Filename:=Editor.FileName;
ScreenPos:=Editor.CursorScreenXY;
TextPos:=Editor.CursorTextXY;

end;


procedure Register;
var
Key: TIDEShortCut;
Cat: TIDECommandCategory;
SectionLines:TIDEMenuSection;
SectionLinesMenu:TIDEMenuSection;
DuplicateCmd: TIDECommand;
ExchangeLineUpCmd: TIDECommand;
ExchangeLineDownCmd: TIDECommand;
begin
SectionLines:=RegisterIDEMenuSection(mnuEdit,'Lines');
SectionLinesMenu:= RegisterIDESubMenu(SectionLines,'Lines','Lines');

Key := IDEShortCut(VK_UP,[ssCtrl,ssShift],VK_UNKNOWN,[]);
Cat:=IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
ExchangeLineUpCmd := RegisterIDECommand(Cat,'ExchangeLineUpCmd', 'Move up', Key, nil, @ExchangeLineUp);
RegisterIDEMenuCommand(SectionLinesMenu, 'ExchangeLineUpCmd', 'Move line up', nil, nil, ExchangeLineUpCmd);

Key := IDEShortCut(VK_DOWN,[ssCtrl,ssShift],VK_UNKNOWN,[]);
ExchangeLineDownCmd := RegisterIDECommand(Cat,'ExchangeLineDownCmd', 'Move down', Key, nil, @ExchangeLineDown);
RegisterIDEMenuCommand(SectionLinesMenu, 'ExchangeLineDownCmd', 'Move line down', nil, nil, ExchangeLineDownCmd);


Key := IDEShortCut(VK_Q,[ssCtrl],VK_UNKNOWN,[]);
DuplicateCmd := RegisterIDECommand(Cat,'Duplicate Line', 'Duplicate a Line', Key, nil, @DuplicateLine);
RegisterIDEMenuCommand(SectionLinesMenu, 'DuplicateLine', 'Duplicate a Line', nil, nil, DuplicateCmd);
end;


end.


sorry for my english.
--
*********************************************************************
En la tierra hace falta personas que trabajen más y critiquen menos,
que construyan más y destruyan menos, que prometan menos y
resuelvan más que esperen recibir menos y dar más que digan mejor
ahora que mañana.
Che
*********************************************************************

--
Loading...