Sign in Join
Wiki copytemporary

Editor Macros PascalScript: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Editor Macros PascalScript

OldNewLine
1+## General
2+
3+The [[PascalScript]] macro functionality is available in Lazarus 1.1 and later. To use the feature you have to install the package EditorMacroScript, which includes the [[Pascal Script]] package. Pascal Script is provided by REM Objects. A minimum package is provided with the Lazarus 1.1 distribution.
4+
5+## Availability
6+
7+This functionality is only available on selected platforms/architectures:
8+
9+- Windows
10+ - 32/64bit Intel/AMD
11+- Linux
12+ - 32/64bit Intel/AMD
13+- Mac
14+ - 32(/maybe 64)bit Intel/AMD
15+ - 32bit PPC
16+
17+## Simple actions
18+
19+All simple Keyboard actions are represented as follows:
20+
21+ecLeft;
22+Move Caret one to the left (in the editor that invoked the macro)
23+
24+ecChar('a');
25+Inserts an 'a'
26+
27+See the unit SynEditKeyCmds in package [[SynEdit]], and IDECommands in IDEIntf for a full list. Or use the Recorder to get the names of actions.
28+
29+## Functions
30+
31+``` pascal
32+ Function MessageDlg( const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint ): Integer;
33+ Function MessageDlgPos( const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer): Integer;
34+ Function MessageDlgPosHelp( const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer;
35+ Procedure ShowMessage( const Msg: string );
36+ Procedure ShowMessagePos( const Msg: string; X, Y Integer );
37+ Function InputBox( const ACaption, APrompt, ADefault: string): string;
38+ Function InputQuery( const ACaption, APrompt: string; var Value: string): Boolean;
39+```
40+
41+## Objects provided
42+
43+Scripts can refer to the invoking SynEdit via the identifier "Caller".
44+
45+### Caller: TSynEdit
46+
47+The following methods and properties are available:
48+
49+Caret
50+
51+``` pascal
52+ property CaretX: Integer;
53+ property CaretY: Integer;
54+ property CaretXY: TPoint;
55+ property LogicalCaretXY: TPoint;
56+ property LogicalCaretX: Integer;
57+ procedure MoveCaretIgnoreEOL(const NewCaret: TPoint);
58+ procedure MoveLogicalCaretIgnoreEOL(const NewLogCaret: TPoint);
59+```
60+
61+Selection
62+
63+``` pascal
64+ property BlockBegin: TPoint;
65+ property BlockEnd: TPoint;
66+ property SelAvail: Boolean; // read only
67+ property SelText: string;
68+ property SelectionMode: TSynSelectionMode;
69+ procedure ClearSelection;
70+ procedure SelectAll;
71+ procedure SelectToBrace;
72+ procedure SelectWord;
73+ procedure SelectLine(WithLeadSpaces: Boolean);
74+ procedure SelectParagraph;
75+```
76+
77+Search/Replace
78+
79+``` pascal
80+ function SearchReplace(const ASearch, AReplace: string; AOptions: TSynSearchOptions): integer;
81+ function SearchReplaceEx(const ASearch, AReplace: string; AOptions: TSynSearchOptions; AStart: TPoint): integer;
82+
83+ TSynSearchOptions = set of
84+ ( ssoMatchCase, ssoWholeWord,
85+ ssoBackwards,
86+ ssoEntireScope, ssoSelectedOnly, // Default is From Caret to End-of-text (or begin-of-text if backward)
87+ ssoReplace, ssoReplaceAll, // Otherwise the function does a search only
88+ ssoPrompt, // Show the prompt before replacing
89+ ssoSearchInReplacement, // continue search-replace in replacement (with ssoReplaceAll) // replace recursive
90+ ssoRegExpr, ssoRegExprMultiLine,
91+ ssoFindContinue // Assume the current selection is the last match, and start search behind selection
92+ // (before if ssoBackward) // Default is to start at caret (Only SearchReplace / SearchReplaceEx has start/end param)
93+ );
94+```
95+
96+Returns the number of replacements done. When Searching, returns 1 if found, 0 if not found. If found the match will be selected (use BlockBegin/End).
97+
98+``` pascal
99+ if Caller.SearchReplace('FindMe', ' ', []) > 0 then begin
100+ // Selection is set to the first occurrence of FindMe (searched from position of caret)
101+ end;
102+```
103+
104+Text
105+
106+``` pascal
107+ property Lines[Index: Integer]: string; // read only
108+ property LinesCount: integer; // read only (new for Lazarus 2.2)
109+ property LineAtCaret: string; // read only
110+ procedure InsertTextAtCaret(aText: String; aCaretMode : TSynCaretAdjustMode);
111+ property TextBetweenPoints[aStartPoint, aEndPoint: TPoint]: String // Logical Points
112+ procedure SetTextBetweenPoints(aStartPoint, aEndPoint: TPoint;
113+ const AValue: String;
114+ aFlags: TSynEditTextFlags = [];
115+ aCaretMode: TSynCaretAdjustMode;
116+ aMarksMode: TSynMarksAdjustMode;
117+ aSelectionMode: TSynSelectionMode
118+ );
119+```
120+
121+Clipboard
122+
123+``` pascal
124+ procedure CopyToClipboard;
125+ procedure CutToClipboard;
126+ procedure PasteFromClipboard;
127+ property CanPaste: Boolean // read only
128+```
129+
130+Logical / Physical
131+
132+``` pascal
133+ function LogicalToPhysicalPos(const p: TPoint): TPoint;
134+ function LogicalToPhysicalCol(const Line: String; Index, LogicalPos
135+ : integer): integer;
136+ function PhysicalToLogicalPos(const p: TPoint): TPoint;
137+ function PhysicalToLogicalCol(const Line: string;
138+ Index, PhysicalPos: integer): integer;
139+ function PhysicalLineLength(Line: String; Index: integer): integer;
140+```
141+
142+### ClipBoard: TClipBoard
143+
144+``` pascal
145+ property AsText: String;
146+```
147+
148+## Available Macros / Downloads
149+
150+<table>
151+<caption>Editor macros</caption>
152+<tbody>
153+<tr>
154+<td><p><strong>Description</strong></p></td>
155+<td><p><strong>Link</strong></p></td>
156+</tr>
157+<tr>
158+<td><p><strong>Column Align Source code</strong></p>
159+<p>A macro that aligns selected code to a specific token. It looks for a specific token in each selected line, and aligns each occurrence of it.</p>
160+<p>Select the 3 lines. If the selection starts right before the ":" then the ":" will be detected. A prompt will ask you to confirm the ":". Then all ":" will be aligned. (If you use a word for alignment, please note, that it will be matched regardless of word boundaries)</p>
161+<p>You can vertically align things like := or // or (</p>
162+<table>
163+<tbody>
164+<tr>
165+<td><div class="sourceCode" id="cb1"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a> foo := <span class="dv">1</span>;</span>
166+<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> something := <span class="dv">2</span>;</span>
167+<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> x := -<span class="dv">3</span>;</span></code></pre></div></td>
168+<td><div class="sourceCode" id="cb2"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a> foo := <span class="dv">1</span>;</span>
169+<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> something := <span class="dv">2</span>;</span>
170+<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> x := -<span class="dv">3</span>;</span></code></pre></div></td>
171+</tr>
172+</tbody>
173+</table></td>
174+<td><p>[[Editor Macro Column Align Source]]</p></td>
175+</tr>
176+<tr>
177+<td><p><strong>Count array elements</strong></p>
178+<p>A macro to count the boundaries in an array constant.</p>
179+<table>
180+<tbody>
181+<tr>
182+<td><div class="sourceCode" id="cb3"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">const</span> foo: <span class="kw">array</span> [<span class="dv">0.</span>.] <span class="kw">of</span> <span class="dt">integer</span> = (</span>
183+<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>,</span>
184+<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">4</span></span>
185+<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> );</span></code></pre></div></td>
186+<td><div class="sourceCode" id="cb4"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">const</span> foo: <span class="kw">array</span> [<span class="dv">0..3</span>] <span class="kw">of</span> <span class="dt">integer</span> = (</span>
187+<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>,</span>
188+<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">4</span></span>
189+<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> );</span></code></pre></div></td>
190+</tr>
191+</tbody>
192+</table></td>
193+<td><p>See <a href="http://forum.lazarus.freepascal.org/index.php/topic,27186.msg167883.html#msg167883">http://forum.lazarus.freepascal.org/index.php/topic,27186.msg167883.html#msg167883</a> for counting array elements.</p></td>
194+</tr>
195+<tr>
196+<td><p><strong>Add PasDoc for function declaration</strong></p>
197+<p>Comments a procedure/function in <a href="https://github.com/pasdoc/pasdoc/wiki">PasDoc</a> format</p>
198+<p>Example: Executing the macro with the caret in the line of function declaration will insert the comment before.</p>
199+<table>
200+<tbody>
201+<tr>
202+<td><div class="sourceCode" id="cb5"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> AddRadioButton(<span class="kw">const</span> aCaption: <span class="dt">string</span>; aChecked: <span class="dt">boolean</span> = <span class="kw">False</span>): TCheckBox;</span></code></pre></div></td>
203+<td><div class="sourceCode" id="cb6"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co">{ Description</span></span>
204+<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="co"> @param(aCaption description )</span></span>
205+<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="co"> @param(aChecked description )</span></span>
206+<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="co"> @returns( description )</span></span>
207+<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co"> @raises( none )</span></span>
208+<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="co">}</span></span>
209+<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> AddRadioButton(<span class="kw">const</span> aCaption: <span class="dt">string</span>; aChecked: <span class="dt">boolean</span> = <span class="kw">False</span>): TRadioButton;</span></code></pre></div></td>
210+</tr>
211+</tbody>
212+</table>
213+<p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc"><span>https://github.com/DomingoGP/LazarusMacroPasDoc</span></a></p></td>
214+<td><p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroPasDocLazarus.txt">MacroPasDocLazarus.txt</a></p></td>
215+</tr>
216+<tr>
217+<td><p><strong>Sort selected lines</strong></p>
218+<p>There are two macros, one with the normal alphabetical order and another that orders by type</p>
219+<table>
220+<tbody>
221+<tr>
222+<td><div class="sourceCode" id="cb7"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a> c:<span class="dt">integer</span>;</span>
223+<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> b:<span class="dt">integer</span>;</span>
224+<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> d:<span class="dt">boolean</span>;</span>
225+<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> a:<span class="dt">boolean</span>;</span></code></pre></div></td>
226+<td><div class="sourceCode" id="cb8"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a> <span class="co">//normal sorting</span></span>
227+<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> a:<span class="dt">boolean</span>;</span>
228+<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> b:<span class="dt">integer</span>;</span>
229+<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> c:<span class="dt">integer</span>;</span>
230+<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> d:<span class="dt">boolean</span>;</span></code></pre></div></td>
231+<td><div class="sourceCode" id="cb9"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a> <span class="co">//sorting by type</span></span>
232+<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> d:<span class="dt">boolean</span>;</span>
233+<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> a:<span class="dt">boolean</span>;</span>
234+<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> c:<span class="dt">integer</span>;</span>
235+<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> b:<span class="dt">integer</span>; </span></code></pre></div></td>
236+</tr>
237+</tbody>
238+</table></td>
239+<td><p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroSortLinesLazarus.txt">MacroSortLinesLazarus.txt</a></p>
240+<p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroSortByTypeLazarus.txt">MacroSortByTypeLazarus.txt</a></p></td>
241+</tr>
242+<tr>
243+<td></td>
244+<td></td>
245+</tr>
246+<tr>
247+<td><p><strong>Column Align by procedure/function name the selected lines</strong></p>
248+<table>
249+<tbody>
250+<tr>
251+<td><div class="sourceCode" id="cb10"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span> Create;</span>
252+<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">destructor</span> Destroy; <span class="kw">override</span>;</span>
253+<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Load(<span class="kw">const</span> aFileName:TFileName):<span class="dt">boolean</span>;</span>
254+<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">procedure</span> Save(<span class="kw">const</span> aFileName:TFileName); </span>
255+<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Size:<span class="dt">integer</span>; </span></code></pre></div></td>
256+<td><div class="sourceCode" id="cb11"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span> Create;</span>
257+<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">destructor</span> Destroy; <span class="kw">override</span></span>
258+<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Load(<span class="kw">const</span> aFileName:TFileName):<span class="dt">boolean</span>;</span>
259+<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">procedure</span> Save(<span class="kw">const</span> aFileName:TFileName); </span>
260+<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Size:<span class="dt">integer</span>;</span></code></pre></div></td>
261+</tr>
262+</tbody>
263+</table></td>
264+<td><p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroAlignFunctionProcedureNamesLazarus.txt">MacroAlignFunctionProcedureNamesLazarus.txt</a></p></td>
265+</tr>
266+<tr>
267+<td></td>
268+<td></td>
269+</tr>
270+<tr>
271+<td><p><strong>Sort selected function/procedures declarations by Name, keeps the comments before the function/procedure</strong></p>
272+<table>
273+<tbody>
274+<tr>
275+<td><div class="sourceCode" id="cb12"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">destructor</span> Destroy; <span class="kw">override</span>;</span>
276+<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span> Create;</span>
277+<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">procedure</span> Save(<span class="kw">const</span> aFileName:TFileName);</span>
278+<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="co">{ Load the aFileName }</span></span>
279+<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Load(<span class="kw">const</span> aFileName:TFileName):<span class="dt">boolean</span>;</span>
280+<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Size:<span class="dt">integer</span>; </span></code></pre></div></td>
281+<td><div class="sourceCode" id="cb13"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">constructor</span> Create;</span>
282+<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">destructor</span> Destroy; <span class="kw">override</span>;</span>
283+<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="co">{ Load the aFileName }</span></span>
284+<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Load(<span class="kw">const</span> aFileName:TFileName):<span class="dt">boolean</span>;</span>
285+<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">procedure</span> Save(<span class="kw">const</span> aFileName:TFileName);</span>
286+<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span> Size:<span class="dt">integer</span>; </span></code></pre></div></td>
287+</tr>
288+</tbody>
289+</table></td>
290+<td><p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroSortFuncProcLazarus.txt">MacroSortFuncProcLazarus.txt</a></p></td>
291+</tr>
292+<tr>
293+<td></td>
294+<td></td>
295+</tr>
296+<tr>
297+<td></td>
298+<td></td>
299+</tr>
300+<tr>
301+<td><p><strong>Sort selected units in the uses clause</strong></p>
302+<table>
303+<tbody>
304+<tr>
305+<td><div class="sourceCode" id="cb14"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">uses</span></span>
306+<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> Classes, SysUtils, Forms, Controls, Graphics, Dialogs; </span></code></pre></div></td>
307+<td><div class="sourceCode" id="cb15"><pre class="sourceCode pascal"><code class="sourceCode pascal"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">uses</span></span>
308+<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> Classes, Controls, Dialogs, Forms, Graphics, SysUtils;</span></code></pre></div></td>
309+</tr>
310+</tbody>
311+</table></td>
312+<td><p><a href="https://github.com/DomingoGP/LazarusMacroPasDoc/blob/master/MacroSortUsesLazarus.txt">MacroSortUsesLazarus.txt</a></p></td>
313+</tr>
314+<tr>
315+<td></td>
316+<td></td>
317+</tr>
318+</tbody>
319+</table>
320+
321+## See also
322+
323+- [[IDE Window: Editor Macros]]
324+- [[Editor Macro Column Align Source]] - macro to align selected code to a specific token.
325+
326+---
327+*From the Free Pascal wiki, page "Editor Macros PascalScript", as it stood on 2022-01-23 after a change by DomingoGP, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history