chiark / gitweb /
Import upstream version 5.3.
[mup] / mup / docs / uguide / macros.html
CommitLineData
69695f33
MW
1<HTML>
2<HEAD><TITLE>
3Mup macros
4</TITLE></HEAD>
5<BODY>
6<P>
7&nbsp;&nbsp;&nbsp;<A HREF="headfoot.html">&lt;-- previous page</A>
8
9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="index.html">Table of Contents</A>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="ifclause.html">next page --&gt;</A>
10</P>
11
12<H2>
13Macros
14</H2>
15<H3>
16Simple Macros (without parameters)
17</H3>
18<P>
19Macros can be defined to avoid retyping or to give mnemonic names to
20things. A macro is defined with the following syntax:
21<BR><PRE>
22<B>define</B> <I> macro_name macro_text</I> <B>@</B>
23</PRE><BR>
24</P>
25<P>
26The <I>macro_name</I> consists of one or more upper case letters, digits,
27and underscores, with the first character being a letter.
28The <I>macro_text</I> can be any text. It can be any length from empty
29to many pages. The &quot;@&quot; terminates the macro. A literal &quot;@&quot; can be
30placed in the <I>macro_text</I> by preceding it with a backslash.
31If you want a literal backslash in the <I>macro_text</I>, it also must
32be preceded by a backslash.
33</P>
34<P>
35A macro is called by stating the <I>macro_name</I> in the input. The
36<I>macro_name</I> is replaced by the <I>macro_text</I>.
37A macro can be defined at any point in the input. It can be used as
38often as desired any time after it has been defined. A given <I>macro_name</I>
39can be redefined as many times as desired, with each new definition
40overwriting the previous definition.
41</P>
42<P>
43As an example, suppose you are printing an orchestral score, and the oboe
44part happens to be on staff 5. Rather than having to remember which staff
45it is, you could define a macro:
46<BR><PRE>
47define OBOE 5: @
48</PRE><BR>
49Not only is the name easier to remember than a number, but if you later
50decide to move the oboe part to a different place in the score, only the
51macro definition and perhaps a few other things would have to be changed.
52</P>
53<P>
54Another common use of macros might be if a musical motif occurs several
55times. You could define a macro for the motive:
56<BR><PRE>
57define SCALE 8c;d;e;f;g;a;b;c+; @
58</PRE><BR>
59then do something like:
60<BR><PRE>
61OBOE SCALE
62</PRE><BR>
63</P>
64<P>
65It is possible to remove the definition of a macro using the &quot;undef&quot;
66statement:
67<BR><PRE>
68undef OBOE
69</PRE><BR>
70</P>
71<P>
72It is possible to have parts of the input skipped over depending on whether
73certain macros are defined or not. This is done using
74&quot;ifdef,&quot; &quot;else,&quot; and &quot;endif.&quot; The keyword &quot;ifdef&quot; is followed by
75a macro name. If a macro by that name is currently defined,
76Mup will continue
77reading and processing input normally. If it finds a matching &quot;else,&quot;
78it will skip over input until the matching &quot;endif.&quot;
79If the macro is not currently defined, Mup will skip over the input
80until it finds a matching &quot;else&quot; or &quot;endif.&quot; There is also
81an &quot;ifndef&quot; command that uses the opposite logic: it will read the input
82up to the &quot;else&quot; or &quot;endif&quot; only if the macro is NOT defined.
83</P>
84<P>
85The ifdefs can be sprinkled between other items in the input;
86they need not be on separate lines. They can be nested. Examples:
87<BR><PRE>
88// make last c an octave higher if macro &quot;FRED&quot; is defined
891: c;e;g;c ifdef FRED + endif;
90
91ifdef PIANO
92 staff 1 visible=n
93else
94 ifdef VIOLIN
95 staff 2 visible=n
96 staff 3 visible=n
97 endif
98endif
99</PRE><BR>
100</P>
101<P>
102<A HREF="cmdargs.html#doption">Macros can also be set from the command line using the -D option.</A>
103Only ordinary macros can be defined using the -D option,
104not macros with parameters.
105</P>
106<H3>
107Macros with parameters
108</H3>
109<P>
110<A NAME="macparm">Macros defined within Mup input can be defined to have "parameters."</A>
111This may be useful
112when you have something that is repeated with small variations.
113When defining a macro with parameters, the macro name must be followed
114immediately by a ( with no space between the end of the name and the
115parenthesis. The opening parenthesis is followed by one or more
116parameter names, separated by commas, and ending with a close parenthesis.
117Parameter names have the same rules as macro names: they consist of
118upper case letters, numbers, and underscores, starting with an upper case
119letter. The parameter names can then appear in the text of the macro
120definition where you want a value to be substituted.
121</P>
122<P>
123As an example, suppose you are doing a score with staffs 1 through 4
124for vocal parts, and staffs 5 and 6 for a piano accompaniment, and that
125you frequently want to mark a dymanics change at the same point in time
126below each of the vocal scores and between the two piano staffs.
127You could typically do this with something like:
128<BR><PRE>
129boldital below 1-4: 1 &quot;ff&quot;;
130boldital between 5&amp;6: 1 &quot;ff&quot;;
131</PRE><BR>
132but if you needed to do this lots of times, it could get tedious.
133So let's define a macro with parameters:
134<BR><PRE>
135define DYN( COUNT, VOLUME )
136boldital below 1-4: COUNT VOLUME;
137boldital between 5&amp;6: COUNT VOLUME;
138@
139</PRE><BR>
140This macro has two parameters,
141which have been given the names COUNT and VOLUME.
142When you call the macro, you will give them values.
143For example,
144<BR><PRE>
145DYN(1,&quot;ff&quot;)
146</PRE><BR>
147would give a VOLUME of &quot;ff&quot; at COUNT 1, whereas
148<BR><PRE>
149DYN(3.5,&quot;mp&quot;)
150</PRE><BR>
151would give a VOLUME of &quot;mp&quot; at COUNT 3.5.
152</P>
153<P>
154When calling a macro with parameters, the values to give the parameters
155are given inside parentheses. The values are separated by commas.
156The values in the parentheses are copied exactly as they are,
157including any spaces, newlines, macro names, etc.
158There are only a few exceptions to this:
159you can include a comma, closing parenthesis, or backslash
160as part of a parameter value by preceding it with a backslash, and
161a backslash followed by a newline
162in a parameter value will be discarded. Thus a macro call of
163<BR><PRE>
164MAC(\\\,\))
165</PRE><BR>
166has one parameter, the text of which is 3 characters long: a backslash,
167comma, and closing parenthesis.
168</P>
169<P>
170<A NAME="quoting">If in a macro definition a parameter is used inside backticks,</A>
171as in `NAME`, the value of the parameter will be placed
172inside double quotes. Thus, another way to do the example above would be:
173<BR><PRE>
174define DYN( COUNT, VOLUME )
175boldital below 1-4: COUNT `VOLUME`;
176boldital between 5&amp;6: COUNT `VOLUME`;
177@
178
179DYN(1,ff)
180DYN(3.5,mp)
181</PRE><BR>
182</P>
183<P>
184Conceptually, when the macro is expanded, the backticks are replaced
185by double quote marks, but in addition,
186any double quote mark found in the value being passed to the parameter will
187have a backslash inserted before it, and any backslash that occurs
188within double quotes in the value will also have a backslash inserted
189before it. Thus, for example:
190<BR><PRE>
191// If we define a macro like this:
192define QUOTED(X) `X` @
193
194// then for input value passed is `X` would be which would print as
195
196print QUOTED(hello) hello &quot;hello&quot; hello
197print QUOTED(&quot;hello&quot;) &quot;hello&quot; &quot;\&quot;hello\&quot;&quot; &quot;hello&quot;
198print QUOTED(\\n) \n &quot;\n&quot; a literal newline
199print QUOTED(&quot;\\n&quot;) &quot;\n&quot; &quot;\&quot;\\n\&quot;&quot; &quot;\n&quot;
200</PRE><BR>
201</P>
202<P>
203Sometimes it can be a little tricky to get the number of backslashes right,
204or other details like that.
205<A HREF="cmdargs.html#Eoption">The -E Mup command line option</A>
206shows how macros will expand, which may help you figure out what to do.
207</P>
208<HR><P>
209&nbsp;&nbsp;&nbsp;<A HREF="headfoot.html">&lt;-- previous page</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="index.html">Table of Contents</A>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="ifclause.html">next page --&gt;</A>
210</P>
211</BODY></HTML>