Debian Codegraph @

texmf-dist/doc/latex/concepts/with.sty

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \iffalse %%%%
2 % %
3 % Copyright (c) 2012 - Michiel Helvensteijn (www.mhelvens.net) %
4 % %
5 % http://latex-with.googlecode.com %
6 % %
7 % This work may be distributed and/or modified under the conditions %
8 % of the LaTeX Project Public License, either version 1.3 of this %
9 % license or (at your option) any later version. The latest version %
10 % of this license is in http://www.latex-project.org/lppl.txt %
11 % and version 1.3 or later is part of all distributions of LaTeX %
12 % version 2005/12/01 or later. %
13 % %
14 % This work has the LPPL maintenance status `author-maintained'. %
15 % %
16 % The Current Maintainer of this work is Michiel Helvensteijn. %
17 % %
18 % This work consists of the file with.sty and with.tex. %
19 % %
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \fi %%%%
21
22 % \CheckSum{0}
23 %
24 % \CharacterTable
25 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
26 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
27 % Digits \0\1\2\3\4\5\6\7\8\9
28 % Exclamation \! Double quote \" Hash (number) \#
29 % Dollar \$ Percent \% Ampersand \&
30 % Acute accent \' Left paren \( Right paren \)
31 % Asterisk \* Plus \+ Comma \,
32 % Minus \- Point \. Solidus \/
33 % Colon \: Semicolon \; Less than \<
34 % Equals \= Greater than \> Question mark \?
35 % Commercial at \@ Left bracket \[ Backslash \\
36 % Right bracket \] Circumflex \^ Underscore \_
37 % Grave accent \` Left brace \{ Vertical bar \|
38 % Right brace \} Tilde \~}
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 % \subsection{Package Info} %
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44
45
46 % \begin{macrocode}
47 \NeedsTeXFormat{LaTeX2e}
48 \RequirePackage{expl3}
49 \ProvidesExplPackage{with}{2012/12/23}{0.0.1}
50 {an inline construct for passing token lists as parameters}
51 % \end{macrocode}
52
53
54
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 % \subsection{Required Packages} %
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58
59
60
61 % \begin{macrocode}
62 \RequirePackage{xparse}
63 \RequirePackage{l3regex}
64 % \end{macrocode}
65
66
67
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % \subsection{Package Code} %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
72
73
74 % \begin{macrocode}
75 \cs_generate_variant:Nn \cs_generate_variant:Nn {cn}
76 % \end{macrocode}
77
78
79
80 % \begin{macrocode}
81 \cs_new:Nn \gen_use:nn {
82 \cs_generate_variant:cn
83 { #1 : \prg_replicate:nn{\tl_count:n{#2}}{n} }
84 { #2 }
85 \use:c{ #1 : #2 }
86 }
87 % \end{macrocode}
88
89
90
91 % \begin{macrocode}
92 \cs_new_protected:Nn \with:n {#1}
93 \cs_new_protected:Nn \with:nn {
94 \cs_set:Npn \__with:n ##1 {#2}
95 \__with:n {#1} }
96 \cs_new_protected:Nn \with:nnn {
97 \cs_set:Npn \__with:nn ##1##2 {#3}
98 \__with:nn {#1}{#2} }
99 \cs_new_protected:Nn \with:nnnn {
100 \cs_set:Npn \__with:nnn ##1##2##3 {#4}
101 \__with:nnn {#1}{#2}{#3} }
102 \cs_new_protected:Nn \with:nnnnn {
103 \cs_set:Npn \__with:nnnn ##1##2##3##4 {#5}
104 \__with:nnnn {#1}{#2}{#3}{#4} }
105 \cs_new_protected:Nn \with:nnnnnn {
106 \cs_set:Npn \__with:nnnnn ##1##2##3##4##5 {#6}
107 \__with:nnnnn {#1}{#2}{#3}{#4}{#5} }
108 \cs_new_protected:Nn \with:nnnnnnn {
109 \cs_set:Npn \__with:nnnnnn ##1##2##3##4##5##6 {#7}
110 \__with:nnnnnn {#1}{#2}{#3}{#4}{#5}{#6} }
111 \cs_new_protected:Nn \with:nnnnnnnn {
112 \cs_set:Npn \__with:nnnnnnn ##1##2##3##4##5##6##7 {#8}
113 \__with:nnnnnnn {#1}{#2}{#3}{#4}{#5}{#6}{#7} }
114 \cs_new_protected:Nn \with:nnnnnnnnn {
115 \cs_set:Npn \__with:nnnnnnnn ##1##2##3##4##5##6##7##8 {#9}
116 \__with:nnnnnnnn {#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8} }
117 % \end{macrocode}
118
119
120
121 % \begin{macrocode}
122 \msg_new:nnnn{with}{invalid-parameter-specs}{
123 The~argument~specification~'#1'~is~not~valid.
124 }{
125 The~argument~specification~should~consist~of~between~
126 one~and~seven~of~the~letters~'n',~'o',~'f',~'x',~'u'~or~'U'.
127 }
128 % \end{macrocode}
129
130
131
132 % \begin{macrocode}
133 \msg_new:nnnn{with}{invalid-parameter-number}{
134 You~specified~#1~arguments~but~provided~#2.
135 }{
136 Your~argument~specification~is~'#3',~which~means~you~should~
137 provide~#1~optional~arguments.~However,~you~provided~only~#2.
138 }
139 % \end{macrocode}
140
141
142
143 % \begin{macrocode}
144 \tl_new:N \l__with_argspec_tl
145 \tl_new:c {l__with_p_1_tl} \tl_new:c {l__with_p_2_tl}
146 \tl_new:c {l__with_p_3_tl} \tl_new:c {l__with_p_4_tl}
147 \tl_new:c {l__with_p_5_tl} \tl_new:c {l__with_p_6_tl}
148 \tl_new:c {l__with_p_7_tl}
149 \tl_new:N \l__with_code_tl
150 % \end{macrocode}
151
152
153
154 % \begin{macrocode}
155 \int_new:N \l__with_arg_int
156 \cs_new:Nn \__with_param_name: {
157 l__with_p_
158 \int_use:N\l__with_arg_int
159 _tl
160 }
161 % \end{macrocode}
162
163
164
165 % \begin{macrocode}
166 \tl_new:N \l__with_unique_counter_tl
167 \tl_new:N \l__with_param_tl
168 % \end{macrocode}
169
170
171
172 % \begin{macrocode}
173 \cs_generate_variant:Nn \with:nn {xn}
174 \cs_generate_variant:Nn \with:nnnnnnnnn {oooooooon}
175 % \end{macrocode}
176
177
178
179 % \begin{macrocode}
180 \NewDocumentCommand \with {m +o +o +o +o +o +o +o +m} {
181 % \end{macrocode}
182
183
184
185 % \begin{macrocode}
186 \regex_match:nnF {^[noxfuU]{1,7}$} {#1}
187 { \msg_critical:nnn{with}{invalid-parameter-specs}{#1} }
188 % \end{macrocode}
189
190
191
192 % \begin{macrocode}
193 \tl_set:Nn \l__with_argspec_tl {#1}
194 \int_zero:N \l__with_arg_int
195 \IfValueT{#2} {
196 \tl_set:cn {l__with_p_1_tl} {#2} \int_incr:N \l__with_arg_int
197 \IfValueT{#3} {
198 \tl_set:cn {l__with_p_2_tl} {#3} \int_incr:N \l__with_arg_int
199 \IfValueT{#4} {
200 \tl_set:cn {l__with_p_3_tl} {#4} \int_incr:N \l__with_arg_int
201 \IfValueT{#5} {
202 \tl_set:cn {l__with_p_4_tl} {#5} \int_incr:N \l__with_arg_int
203 \IfValueT{#6} {
204 \tl_set:cn {l__with_p_5_tl} {#6} \int_incr:N \l__with_arg_int
205 \IfValueT{#7} {
206 \tl_set:cn {l__with_p_6_tl} {#7} \int_incr:N \l__with_arg_int
207 \IfValueT{#8} {
208 \tl_set:cn {l__with_p_7_tl} {#8} \int_incr:N \l__with_arg_int
209 }}}}}}}
210 \tl_set:Nn \l__with_code_tl {#9}
211 % \end{macrocode}
212
213
214
215 % \begin{macrocode}
216 \int_compare:nNnF {\tl_count:n{#1}} = {\l__with_arg_int} {
217 \msg_error:nnxxx{with}{invalid-parameter-number}
218 { \tl_count:n{#1} }
219 { \int_use:N \l__with_arg_int }
220 { #1 }
221 }
222 % \end{macrocode}
223
224
225
226 % \begin{macrocode}
227 \tl_replace_all:Nnn \l__with_argspec_tl {u} {x}
228 \tl_replace_all:Nnn \l__with_argspec_tl {U} {c}
229 % \end{macrocode}
230
231
232
233 % \begin{macrocode}
234 \int_zero:N \l__with_arg_int
235 \tl_map_inline:nn {#1} {
236 \int_incr:N \l__with_arg_int
237 % \end{macrocode}
238
239
240
241 % \begin{macrocode}
242 \tl_if_in:nnT {uU} {##1} {
243 \tl_set:Nx \l__with_unique_counter_tl
244 { g__with_unique_ \use:c{\__with_param_name:} _count_int }
245
246 \int_if_exist:cF {\l__with_unique_counter_tl}
247 { \int_new:c {\l__with_unique_counter_tl} }
248
249 \int_incr:c {\l__with_unique_counter_tl}
250
251 \tl_put_left:cx {\__with_param_name:}
252 { g_with_unique_ }
253
254 \tl_put_right:cx {\__with_param_name:}
255 { _ \int_use:c{\l__with_unique_counter_tl} }
256 }
257 % \end{macrocode}
258
259
260
261 % \uninteresting\begin{macrocode}
262 }
263 % \end{macrocode}
264
265
266
267 % \begin{macrocode}
268 \with:oooooooon {\l__with_argspec_tl}
269 {\cs:w l__with_p_1_tl \cs_end:}
270 {\cs:w l__with_p_2_tl \cs_end:}
271 {\cs:w l__with_p_3_tl \cs_end:}
272 {\cs:w l__with_p_4_tl \cs_end:}
273 {\cs:w l__with_p_5_tl \cs_end:}
274 {\cs:w l__with_p_6_tl \cs_end:}
275 {\cs:w l__with_p_7_tl \cs_end:} {
276 \with:oooooooon {##1o}{##2}{##3}{##4}
277 {##5}{##6}{##7}{##8} {
278 \int_case:nnn {\l__with_arg_int} {
279 {1} { \gen_use:nn{with}{####1}
280 {####2} {\l__with_code_tl} }
281 {2} { \gen_use:nn{with}{####1}
282 {####2}{####3} {\l__with_code_tl} }
283 {3} { \gen_use:nn{with}{####1}
284 {####2}{####3}{####4} {\l__with_code_tl} }
285 {4} { \gen_use:nn{with}{####1}
286 {####2}{####3}{####4}
287 {####5} {\l__with_code_tl} }
288 {5} { \gen_use:nn{with}{####1}
289 {####2}{####3}{####4}
290 {####5}{####6} {\l__with_code_tl} }
291 {6} { \gen_use:nn{with}{####1}
292 {####2}{####3}{####4}
293 {####5}{####6}{####7} {\l__with_code_tl} }
294 {7} { \gen_use:nn{with}{####1}
295 {####2}{####3}{####4}
296 {####5}{####6}{####7}
297 {####8} {\l__with_code_tl} }
298 }{}
299 }
300 }
301 % \end{macrocode}
302
303
304
305 % \uninteresting\begin{macrocode}
306 }
307 % \end{macrocode}
308
309