Tutorial: Using labels in macro's: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Wikipedia python library)
 
m (→‎Solution: wiki links, <code>)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{PageAuthor|Afrow UK}}
== Description ==
== Description ==
Labels work fine in macro's until you start to insert the macro's more than once in the same Section or Function!
Labels work fine in macros until you start to insert the macros more than once in the same Section or Function!


== Solution ==
== Solution ==
The work-around for this problem is quite simple...
The work-around for this problem is quite simple…
<highlight-nsis>
<highlight-nsis>
## Our macro
## Our macro
Line 17: Line 19:
</highlight-nsis>
</highlight-nsis>


What we do is create a unique identity (${UniqueID}) for each instance of !insertmacro and place this unique identity in each label reference. This ensures that all label groups are unique no matter how many times we use !insertmacro.
What we do is create a unique identity (<code>${UniqueID}</code>) for each instance of <code>[[Reference/!insertmacro|!insertmacro]]</code> and place this unique identity in each label reference. This ensures that all label groups are unique no matter how many times we use <code>!insertmacro</code>.


We use ${__LINE__} (current line number) for the UniqueID as it will always change between each !define. We have to !undef-ine UniqueID at the end of the macro too, so the next !insertmacro can re-define a new line number to UniqueID.
We use <code>${__LINE__}</code> (current line number) for the UniqueID as it will always change between each <code>[[Reference/!define|!define]]</code>. We have to <code>[[Reference/!undef|!undef]]</code>-ine UniqueID at the end of the macro too, so the next <code>!insertmacro</code> can re-define a new line number to UniqueID.


-Stu
-Stu


Page author: Afrow UK
[[Category:Tutorials]]

Latest revision as of 13:32, 12 December 2017

Author: Afrow UK (talk, contrib)


Description

Labels work fine in macros until you start to insert the macros more than once in the same Section or Function!

Solution

The work-around for this problem is quite simple…

## Our macro
!macro DoSomething Param
 !define UniqueID ${__LINE__}
 
 StrCmp ${Param} "something" 0 NoSomething_${UniqueID}
  ...
 NoSomething_${UniqueID}:
 
 !undef UniqueID
!macroend

What we do is create a unique identity (${UniqueID}) for each instance of !insertmacro and place this unique identity in each label reference. This ensures that all label groups are unique no matter how many times we use !insertmacro.

We use ${__LINE__} (current line number) for the UniqueID as it will always change between each !define. We have to !undef-ine UniqueID at the end of the macro too, so the next !insertmacro can re-define a new line number to UniqueID.

-Stu