xref: /openbsd-src/gnu/usr.bin/texinfo/doc/macro.texi (revision 840175f0bcd9ef08275c0ee2906216088cc17115)
1*840175f0Skstailey@c This file is included in makeinfo.texi.
2*840175f0Skstailey@c
3*840175f0Skstailey@ifinfo
4*840175f0Skstailey@comment Here are some useful examples of the macro facility.
5*840175f0Skstailey
6*840175f0Skstailey@c Simply insert the right version of the texinfo name.
7*840175f0Skstailey@macro texinfo{}
8*840175f0SkstaileyTeXinfo
9*840175f0Skstailey@end macro
10*840175f0Skstailey
11*840175f0Skstailey@macro dfn{text}
12*840175f0Skstailey@dfn{\text\}
13*840175f0Skstailey@cpindex \text\
14*840175f0Skstailey@end macro
15*840175f0Skstailey
16*840175f0Skstailey@c Define a macro which expands to a pretty version of the name of the
17*840175f0Skstailey@c Makeinfo program.
18*840175f0Skstailey@macro makeinfo{}
19*840175f0Skstailey@code{Makeinfo}
20*840175f0Skstailey@end macro
21*840175f0Skstailey
22*840175f0Skstailey@c Define a macro which is used to define other macros.  This one makes
23*840175f0Skstailey@c a macro which creates a node and gives it a sectioning command.  Note
24*840175f0Skstailey@c that the created macro uses the original definition within the
25*840175f0Skstailey@c expansion text.  This takes advantage of the non-recursion feature of
26*840175f0Skstailey@c macro execution.
27*840175f0Skstailey@macro node_define{orig-name}
28*840175f0Skstailey@macro \orig-name\{title}
29*840175f0Skstailey@node \title\
30*840175f0Skstailey@\orig-name\ \title\
31*840175f0Skstailey@end macro
32*840175f0Skstailey@end macro
33*840175f0Skstailey
34*840175f0Skstailey@c Now actually define a new set of sectioning commands.
35*840175f0Skstailey@node_define {chapter}
36*840175f0Skstailey@node_define {section}
37*840175f0Skstailey@node_define {subsection}
38*840175f0Skstailey@end ifinfo
39*840175f0Skstailey
40*840175f0Skstailey@chapter The Macro Facility
41*840175f0Skstailey
42*840175f0SkstaileyThis chapter describes the new macro facility.
43*840175f0Skstailey
44*840175f0SkstaileyA @dfn{macro} is a command that you define in terms of other commands.
45*840175f0SkstaileyIt doesn't exist as a @texinfo{} command until you define it as part of
46*840175f0Skstaileythe input file to @makeinfo{}.  Once the command exists, it behaves much
47*840175f0Skstaileyas any other @texinfo{} command.  Macros are a useful way to ease the
48*840175f0Skstaileydetails and tedium of writing a `correct' info file.  The following
49*840175f0Skstaileysections explain how to write and invoke macros.
50*840175f0Skstailey
51*840175f0Skstailey@menu
52*840175f0Skstailey* How to Use Macros in @texinfo{}::
53*840175f0Skstailey                        How to use the macro facility.
54*840175f0Skstailey
55*840175f0Skstailey* Using Macros Recursively::
56*840175f0Skstailey                        How to write a macro which does (or doesn't) recurse.
57*840175f0Skstailey
58*840175f0Skstailey* Using @texinfo{} Macros As Arguments::
59*840175f0Skstailey                        Passing a macro as an argument.
60*840175f0Skstailey@end menu
61*840175f0Skstailey
62*840175f0Skstailey@section How to Use Macros in @texinfo{}
63*840175f0Skstailey
64*840175f0SkstaileyUsing macros in @texinfo{} is easy.  First you define the macro.  After
65*840175f0Skstaileythat, the macro command is available as a normal @texinfo{} command.
66*840175f0SkstaileyHere is what a definition looks like:
67*840175f0Skstailey
68*840175f0Skstailey@example
69*840175f0Skstailey@@macro @var{name}@{@var{arg1}, @var{@dots{}} @var{argn}@}
70*840175f0Skstailey@var{@texinfo{} commands@dots{}}
71*840175f0Skstailey@@end macro
72*840175f0Skstailey@end example
73*840175f0Skstailey
74*840175f0SkstaileyThe arguments that you specify that the macro takes are expanded with
75*840175f0Skstaileythe actual parameters used when calling the macro if they are seen
76*840175f0Skstaileysurrounded by backslashes.  For example, here is a definition of
77*840175f0Skstailey@code{@@codeitem}, a macro which can be used wherever @code{@@item} can
78*840175f0Skstaileybe used, but which surrounds its argument with @code{@@code@{@dots{}@}}.
79*840175f0Skstailey
80*840175f0Skstailey@example
81*840175f0Skstailey@@macro codeitem@{item@}
82*840175f0Skstailey@@item @@code@{\item\@}
83*840175f0Skstailey@@end macro
84*840175f0Skstailey@end example
85*840175f0Skstailey
86*840175f0SkstaileyWhen the macro is expanded, all of the text between the @code{@@macro}
87*840175f0Skstaileyand @code{@@end macro} is inserted into the document at the expansion
88*840175f0Skstaileypoint, with the actual parameters substituted for the named parameters.
89*840175f0SkstaileySo, a call to the above macro might look like:
90*840175f0Skstailey
91*840175f0Skstailey@example
92*840175f0Skstailey@@codeitem@{Foo@}
93*840175f0Skstailey@end example
94*840175f0Skstailey
95*840175f0Skstaileyand @makeinfo{} would execute the following code:
96*840175f0Skstailey
97*840175f0Skstailey@example
98*840175f0Skstailey@@item @@code@{Foo@}
99*840175f0Skstailey@end example
100*840175f0Skstailey
101*840175f0SkstaileyA special case is made for macros which only take a single argument, and
102*840175f0Skstaileywhich are invoked without any brace characters (i.e.,
103*840175f0Skstailey@samp{@{}@dots{}@samp{@}}) surrounding an argument; the rest of the line
104*840175f0Skstaileyis supplied as is as the sole argument to the macro.  This special case
105*840175f0Skstaileyallows one to redefine some standard @texinfo{} commands without
106*840175f0Skstaileymodifying the input file.  Along with the non-recursive action of macro
107*840175f0Skstaileyinvocation, one can easily redefine the sectioning commands to also
108*840175f0Skstaileyprovide index entries:
109*840175f0Skstailey
110*840175f0Skstailey@example
111*840175f0Skstailey@@macro chapter@{name@}
112*840175f0Skstailey@@chapter \name\
113*840175f0Skstailey@@findex \name\
114*840175f0Skstailey@@end macro
115*840175f0Skstailey@end example
116*840175f0Skstailey
117*840175f0SkstaileyThus, the text:
118*840175f0Skstailey
119*840175f0Skstailey@example
120*840175f0Skstailey@@chapter strlen
121*840175f0Skstailey@end example
122*840175f0Skstailey
123*840175f0Skstaileywill expand to:
124*840175f0Skstailey
125*840175f0Skstailey@example
126*840175f0Skstailey@@chapter strlen
127*840175f0Skstailey@@findex strlen
128*840175f0Skstailey@end example
129*840175f0Skstailey
130*840175f0Skstailey@section Using Macros Recursively
131*840175f0Skstailey
132*840175f0SkstaileyNormally, while a particular macro is executing, any call to that macro
133*840175f0Skstaileywill be seen as a call to a builtin @texinfo{} command.  This allows one
134*840175f0Skstaileyto redefine a builtin @texinfo{} command as a macro, and then use that
135*840175f0Skstaileycommand within the definition of the macro itself.  For example, one
136*840175f0Skstaileymight wish to make sure that whereever a term was defined with
137*840175f0Skstailey@code{@@dfn@{@dots{}@}}, the location of the definition would appear
138*840175f0Skstaileyin the concept index for the manual.  Here is a macro which redefines
139*840175f0Skstailey@code{@@dfn} to do just that:
140*840175f0Skstailey
141*840175f0Skstailey@example
142*840175f0Skstailey@@macro dfn@{text@}
143*840175f0Skstailey@@dfn@{\text\@}
144*840175f0Skstailey@@cpindex \text\
145*840175f0Skstailey@@end macro
146*840175f0Skstailey@end example
147*840175f0Skstailey
148*840175f0SkstaileyNote that we used the builtin @texinfo{} command @code{@@dfn} within our
149*840175f0Skstaileyoverriding macro definition.
150*840175f0Skstailey
151*840175f0SkstaileyThis behaviour itself can be overridden for macro execution by writing a
152*840175f0Skstaileyspecial @dfn{macro control command} in the definition of the macro.  The
153*840175f0Skstaileycommand is considered special because it doesn't affect the output text
154*840175f0Skstaileydirectly, rather, it affects the way in which the macro is defined.  One
155*840175f0Skstaileysuch special command is @code{@@allow-recursion}.
156*840175f0Skstailey
157*840175f0Skstailey@example
158*840175f0Skstailey@@macro silly@{arg@}
159*840175f0Skstailey@@allow-recursion
160*840175f0Skstailey\arg\
161*840175f0Skstailey@@end macro
162*840175f0Skstailey@end example
163*840175f0Skstailey
164*840175f0SkstaileyNow @code{@@silly} is a macro that can be used within a call to itself:
165*840175f0Skstailey
166*840175f0Skstailey@example
167*840175f0SkstaileyThis text @@silly@{@@silly@{some text@}@} is ``some text''.
168*840175f0Skstailey@end example
169*840175f0Skstailey
170*840175f0Skstailey@section Using @texinfo{} Macros As Arguments
171*840175f0Skstailey
172*840175f0Skstailey@printindex cp
173*840175f0SkstaileyHow to use @texinfo{} macros as arguments to other @texinfo{} macros.
174*840175f0Skstailey
175*840175f0Skstailey@bye
176*840175f0Skstailey
177*840175f0Skstailey
178