Make macro smaller

Tips how to make macro smaller, easier to read and edit, and avoid errors "max 255 local variables" and "too much code in single macro".

 

Reuse variables

For example, you have code like this:

 

int w=win("Calculator" "CalcFrame")
lef 31 304 w 1 ;;push button '1'
int w1=win("Calculator" "CalcFrame")
lef 68 307 w1 1 ;;push button '2'
int w2=win("Calculator" "CalcFrame")
lef 104 305 w2 1 ;;push button '3'

 

Line 1 creates variable w (int w), finds a window (win(...)), and stores window handle in w. Line 2 uses the handle (w).

 

If window in lines 1-2, 3-4 and 4-5 is the same, use single variable and win:

 

int w=win("Calculator" "CalcFrame")
lef 31 304 w 1 ;;push button '1'
lef 68 307 w 1 ;;push button '2'
lef 104 305 w 1 ;;push button '3'

 

If window is different, and will not need previous window handle later, use multiple win and single variable:

 

int w=win("Calculator" "CalcFrame")
lef 31 304 w 1 ;;push button '1'
w=win("Calculator" "CalcFrame")
lef 68 307 w 1 ;;push button '2'
w=win("Calculator" "CalcFrame")
lef 104 305 w 1 ;;push button '3'

 

Also you can reuse Acc and other variables added by QM floating toolbar dialogs. For example, replace this:

 

int w=win("Calculator" "CalcFrame")
Acc a.Find(w "PUSHBUTTON" "5" "" 0x1005)
a.DoDefaultAction
 ...
int w1=win("Calculator" "CalcFrame")
Acc a1.Find(w1 "PUSHBUTTON" "5" "" 0x1005)
a1.DoDefaultAction

 

to this (if need the same object):

 

int w=win("Calculator" "CalcFrame")
Acc a.Find(w "PUSHBUTTON" "5" "" 0x1005)
a.DoDefaultAction
 ...
a.DoDefaultAction

 

or this (if need other object and will not need previous object):

 

int w=win("Calculator" "CalcFrame")
Acc a.Find(w "PUSHBUTTON" "5" "" 0x1005)
a.DoDefaultAction
 ...
a.Find(w "PUSHBUTTON" "5" "" 0x1005)
a.DoDefaultAction

 

See also: about variables, creating variables

 

Repeat

For example, you have code like this:

 

int w1=win("Window1")
act w1
key Cc D        ;; Ctrl+C Down
int w2=win("Window2")
act w2
key Cv D        ;; Ctrl+V Down
act w1
key Cc D        ;; Ctrl+C Down
act w2
key Cv D        ;; Ctrl+V Down
act w1
key Cc D        ;; Ctrl+C Down
act w2
key Cv D        ;; Ctrl+V Down

 

There are 3 identical 4-line code blocks. Instead use single block that is executed 3 times with rep:

 

int w1=win("Window1")
int w2=win("Window2")
rep 3
	act w1
	key Cc D        ;; Ctrl+C Down
	act w2
	key Cv D        ;; Ctrl+V Down

 

Often it's possible to repeat even when there are multiple similar but not identical code blocks. For example, replace this:

 

int w=win("Calculator" "CalcFrame")
lef 19 17 id(131 w)
0.5
lef 19 17 id(135 w)
0.5
lef 19 17 id(139 w)
0.5

 

to this:

 

int w=win("Calculator" "CalcFrame")
str ss="131[]135[]139"
str s
foreach s ss
	lef 19 17 id(val(s) w)
	0.5

 

There are 3 similar 2-line code blocks. With 10 blocks * 10 lines it would be 100 lines. With repeating (foreach) - 13 lines.

 

See also: for, foreach

 

Use functions

When there are multiple similar but not identical code blocks, often it's better to move part(s) of code to functions. For example, replace this:

 

int w=win("Calculator" "CalcFrame")
lef 1 2 id(131 w)
paste "text1"
lef 3 4 id(132 w)
paste "text2"
lef 5 6 id(133 w)
paste "text3"

 

to this:

 

int w=win("Calculator" "CalcFrame")
Function225 1 2 id(131 w) "text1"
Function225 3 4 id(132 w) "text2"
Function225 5 6 id(133 w) "text3"

 

and create function Function225 (menu File -> New):

 

 /
function x y hwnd str's
spe -1
lef x y hwnd
paste s

 

There are 3 similar 2-line code blocks. With 10 blocks * 10 lines it would be 100 lines. With function - 15 lines.

 

See also: function tips, about functions, declaration (paramters etc)

 

Use arrays

When you need several related variables of same type, you can use single array variable. Maybe it will not make macro smaller, but can be used for example to avoid the "max 255 local variables" error when you really need so many variables in single macro.

 

For example, you can replace this:

 

str s0 s1 s2 s3
str s4 s5
s0="zero"
s1="one"
 ...
s5="five"

 

to this:

 

ARRAY(str) a.create(6)
a[0]="zero"
a[1]="one"
 ...
a[5]="five"

 

Arrays also allow to make something easier and smaller. Example:

 

ARRAY(str) a="zero[]one[]...[]five"
int i
for i 0 a.len
	out a[i]

 

See also: ARRAY

 

Use user-defined types

When you need several related variables of different types, you can use single variable of a user-defined type. Maybe it will not make macro smaller, but can be used for example to avoid the "max 255 local variables" error when you really need so many variables in single macro.

 

Replace this:

 

int i1 i2
str s
i1=1
i2=2
s="text"

 

to this:

 

type MACRO_X_VAR i1 i2 str's
MACRO_X_VAR v
v.i1=1
v.i2=2
v.s="text"

 

Here MACRO_X_VAR is some unique name.

 

See also: how to declare and use user-defined types.

 

Remove declarations of unused variables

To find unused variables, check menu Run -> Compile Options -> Show unused variables and compile the macro.