Sign in Join
Wiki copytemporary

Editor Macro Column Align Source: what changed

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

Title: Editor Macro Column Align Source

OldNewLine
1+Please see [[Editor Macros PascalScript]] for info how to use PascalScript Macros in the IDE Source editor.
2+
3+The macro shown below aligns selected code to a specific token. It looks for a specific token in each selected line, and aligns each occurrence of it.
4+
5+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)
6+
7+``` pascal
8+ text: string;
9+ a: Integer;
10+ foo: boolean
11+```
12+
13+The macro:
14+
15+``` pascal
16+function IsIdent(c: Char): Boolean;
17+begin
18+ Result := ((c >= 'a') and (c <= 'z')) or
19+ ((c >= 'A') and (c <= 'Z')) or
20+ ((c >= '0') and (c <= '9')) or
21+ (c = '_');
22+end;
23+
24+var
25+ p1, p2: TPoint;
26+ s1, s2: string;
27+ i, j, k: Integer;
28+begin
29+ if not Caller.SelAvail then exit;
30+ p1 := Caller.BlockBegin;
31+ p2 := Caller.BlockEnd;
32+ if (p1.y > p2.y) or ((p1.y = p2.y) and (p1.x > p2.x)) then begin
33+ p1 := Caller.BlockEnd;
34+ p2 := Caller.BlockBegin;
35+ end;
36+ s1 := Caller.Lines[p1.y - 1];
37+ s2 := '';
38+ i := p1.x
39+ while (i <= length(s1)) and (s1[i] in [#9, ' ']) do inc(i);
40+ j := i;
41+ if i <= length(s1) then begin
42+ if IsIdent(s1[i]) then // pascal identifier
43+ while (i <= length(s1)) and IsIdent(s1[i]) do inc(i)
44+ else
45+ while (i <= length(s1)) and not(IsIdent(s1[i]) or (s1[i] in [#9, ' '])) do inc(i);
46+ end;
47+ if i > j then s2 := copy(s1, j, i-j);
48+
49+ if not InputQuery( 'Align', 'Token', s2) then exit;
50+
51+ j := 0;
52+ for i := p1.y to p2.y do begin
53+ s1 := Caller.Lines[i - 1];
54+ k := pos(s2, s1);
55+ if (k > j) then j := k;
56+ end;
57+ if j < 1 then exit;
58+
59+ for i := p1.y to p2.y do begin
60+ s1 := Caller.Lines[i - 1];
61+ k := pos(s2, s1);
62+ if (k > 0) and (k < j) then begin
63+ Caller.LogicalCaretXY := Point(k, i);
64+ while k < j do begin
65+ ecChar(' ');
66+ inc(k);
67+ end;
68+ end;
69+ end;
70+
71+end.
72+```
73+
74+## See also
75+
76+- [[Editor Macros PascalScript]]
77+- [[IDE Window: Editor Macros]]
78+
79+---
80+*From the Free Pascal wiki, page "Editor Macro Column Align Source", as it stood on 2020-09-21 after a change by Trev, 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).*
81+Category: IDE Macros
82+Category: Lazarus
83+Category: Pascal Script
84+Category: PascalScript

Back to history