Debian Codegraph @

texmf-dist/tex/generic/dowith/dowith.sty

raw
1 \def\filename{dowith} \def\fileinfo{simple list loop (UL)}
2 \def\filedate{2012/11/05} \def\fileversion{v0.3}
3 %%
4 %% Copyright (C) 2011 2012 Uwe Lueck,
5 %% http://www.contact-ednotes.sty.de.vu
6 %% -- author-maintained in the sense of LPPL below --
7 %%
8 %% This file can be redistributed and/or modified under
9 %% the terms of the LaTeX Project Public License; either
10 %% version 1.3c of the License, or any later version.
11 %% The latest version of this license is in
12 %% http://www.latex-project.org/lppl.txt
13 %% We did our best to help you, but there is NO WARRANTY.
14 %%
15 %% Please report bugs, problems, and suggestions via
16 %%
17 %% http://www.contact-ednotes.sty.de.vu
18 %%
19 %% == Proceeding without \LaTeX ==
20 %% v0.3 mainly replaces imitating the
21 %% \CtanPkgRef{german}{german.sty} approach to genericity
22 %% by \ctanpkgref{plainpkg}:
23 \input plainpkg
24 \ProvidesPackage{\filename}[\filedate\space
25 \fileversion\space \fileinfo]
26 \PushCatMakeLetterAt
27 %% If \LaTeX\ is not present ...
28 \ifltx \else
29 %% ... an old version of its `\in@' is introduced.
30 %% It is bad as a subword test
31 %% (false positive cases, cf. \CtanPkgRef{nicetext}{fifinddo}
32 %% documentation), but 'dowith' will check for single tokens only.
33 %% If \LaTeX\ \emph{is} present,
34 %% on the other hand, `\ifin@' is recognized while skipping
35 %% `false' parts of conditionals, without being matched
36 %% by some `\fi' before the next `\else', so I hide it by `\csname':
37 \expandafter\newif\csname ifin@\endcsname
38 \def\in@#1#2{%
39 \def\in@@##1#1##2##3\in@@{%
40 \ifx\in@##2\in@false\else\in@true\fi}%
41 \in@@#2#1\in@\in@@}
42 \fi
43 %%
44 %% == Applying a Command == %% 2011/11/07
45 %% \label{sec:apply}
46 %% === Core ===
47 %% \label{sec:core}
48 %% |\DoWith{<cmd>}<list>\StopDoing| applies <cmd> to all elements
49 %% of <list>. An element of <list> (after tokenizing)
50 %% may be either a single token or a group `{<balanced>}'.
51 \def\DoWith#1#2{%
52 \ifx\StopDoing#2\empty
53 %% The previous `\empty' (replacing `%') is a bug fix as of v0.22
54 %% (June 2012), %% 2012/11/05
55 %% while in my extension draft I already had it in January 2012.
56 %% It allows ``empty" arglist items \qtd{\lbtok\rbtok}.
57 %% Before v0.22, such an empty brace group would have resulted
58 %% in comparing `\StopDoing' with `\else', so \qtd{\lbtok\rbtok}
59 %% would have had the same effect as `\StopDoing', the token text
60 %% after `\else' until `\fi' would have been skipped.
61 %% Instead, the user may have a reason to allow empty arguments\slash
62 %% brace groups.
63 \else#1{#2}\expandafter\DoWith\expandafter#1\fi}
64 %% |\StopDoing| delimits the list:
65 \let\StopDoing\DoWith
66 %% ... something arbitrary that is not expected to occur in a list.
67 %% With \[`\let\StopDoing*'\] instead, the star would end lists.
68 %%
69 %% |\DoWithAllOf{<cmd>}{<list>}| works like
70 %% \[`\DoWith{<cmd>}<list>\StopDoing':\]
71 \def\DoWithAllOf#1#2{\DoWith#1#2\StopDoing}
72 %%
73 %% === `&\do' being the Command ===
74 %% \label{sec:do}
75 %% When the <list> is worked at a single time in the \TeX\ run
76 %% where assignments are possible, instead of introducing a new
77 %% macro name for <cmd> you can use `\do' for <cmd> as a
78 %% ``temporary" macro and define it right before
79 %% \[`\DoWith{\do}<list>\StopDoing'\]
80 %% However, we provide \[|\DoDoWith{<cmd>}<list>\StopDoing|\]
81 %% as a substitute for the former line that at least saves one token.
82 %% For the definition of `\do', we provide
83 %% |\setdo{<def-text>}|.
84 %% It works similarly to \[`\renewcommand{\do}[1]{<def-text>}',\]
85 %% so <def-text> should contain a `#1':
86 \def\setdo{\long\def\do##1}
87 %% With |\letdo<cmd>| that is provided next where <cmd> is defined
88 %% elsewhere, you could type
89 %% \[`\letdo<cmd>\DoDoWith<list>\StopDoing'\]
90 %% It seems to me, however, that you better type
91 %% \[`\dowith<cmd><list>\StopDoing'\] instead.
92 %% So I provide `\letdo' although I consider it useless here.
93 %% It is provided somewhat for the sake of ``completeness,"
94 %% thinking that it might be useful at other occasions such as
95 %% preceding `\dospecials'.
96 \def\letdo{\let\do}
97 %% |\DoDoWith| has been described above:
98 \def\DoDoWith{\DoWith\do}
99 %% By analogy to `\DoWithAllOf', we provide
100 %% |\DoDoWithAllOf{<list>}|:
101 \def\DoDoWithAllOf{\DoWithAllOf\do}
102 %%
103 %% === Expand List Macro ===
104 %% The former facilities may be quite useless
105 %% as such a <list> will not be typed at a single place in the
106 %% source code, rather the items to run <cmd> on may be collected
107 %% occasionally when some routines run. The elements may be collected
108 %% in a macro <list-macro> expanding to <list>.
109 %% So we provide \[|\DoWithAllIn{<cmd>}{<list-macro>}|\]
110 %% (or `\DoWithAllIn<cmd><list-macro>').
111 %% There is no need to type `\StopDoing' here:
112 \def\DoWithAllIn#1#2{%
113 \expandafter\DoWith\expandafter#1#2\StopDoing}
114 %% |\DoDoWithAllIn{<list-macro>}| saves a backslash or token
115 %% for `\do' as above in Sec.~\ref{sec:do}:
116 \def\DoDoWithAllIn{\DoWithAllIn\do}
117 %%
118 %%
119 %% == Handling List Macros ==
120 %% === Initializing ===
121 %% Here is some advanced `\let<cmd>\empty', perhaps a little
122 %% irrelevant for practical purposes. Both
123 %% \[|\InitializeListMacro{<list-macro>}|\] and
124 %% \[|\ReInitializeListMacro{<list-macro>}|\] attempt to ``empty"
125 %% <list-macro>, and when we don't believe that \LaTeX\ has been
126 %% loaded, both do the same indeed. Otherwise the first one
127 %% complains when <list-macro> seems to have been used earlier
128 %% while the second complains when <list-macro> seems \emph{not} to
129 %% have been used before:
130 \ifltx %% v0.3
131 \def\InitializeListMacro#1{\@ifdefinable#1{\let#1\empty}}
132 \def\ReInitializeListMacro#1{%
133 \edef\@tempa{\expandafter\@gobble\string#1}%
134 \expandafter\@ifundefined\expandafter{\@tempa}%
135 {\@latex@error{\noexpand#1undefined}\@ehc}%
136 {\let#1\empty}}
137 \else
138 \def\InitializeListMacro#1{\let#1\empty} %% not \@empty 2011/11/07
139 \let\ReInitializeListMacro\InitializeListMacro
140 \fi
141 %% |\ToListMacroAdd{<list-macro>}{<cmd-or>}|
142 %% appends <cmd-or> to the replacement token list of <list-macro>.
143 %% <cmd-or> may either be tokenized into a single token,
144 %% or it is some `{<balanced>}'.
145 \def\ToListMacroAdd#1#2{\DefExpandStart#1{#1#2}}
146 \def\DefExpandStart#1{\expandafter\def\expandafter#1\expandafter}
147 %%
148 %% === Testing for Occurrence of a Token ===
149 %% |\TestListMacroForToken{<list-macro>}{<cmd>}|
150 %% sets `\in@true' when <cmd> occurs in <list-macro>
151 %% and sets `\in@false' otherwise: %% 2011/11/07
152 \def\TestListMacroForToken#1#2{%
153 \expandafter \in@ \expandafter #2\expandafter{#1}}
154 %% Indeed I removed an earlier `\IfTokenInListMacro',
155 %% now it's a kind of compromise between having a shorthand macro
156 %% below and a generalization for users of the package.
157 %%
158 %% === Adding and Removing ===
159 %% |\FromTokenListMacroRemove{<list-macro>}{<cmd>}|
160 %% removes the token corresponding to <cmd> from the list
161 %% stored in <list-macro>
162 %% (our parsing method does not work with braces): %% 2011/11/07
163 \def\FromTokenListMacroRemove#1#2{%
164 %% I am not happy about defining \emph{two} parser macros,
165 %% but for now ...
166 \TestListMacroForToken#1#2%
167 \ifin@
168 \def\RemoveThisToken##1#2{##1}%
169 \expandafter \DefExpandStart
170 \expandafter #1\expandafter {%
171 \expandafter\RemoveThisToken #1}%
172 %% TODO warning otherwise?
173 \fi}
174 %% %% 2011/11/07:
175 %% ... but this only removes a single occurrence ...
176 %% \[|\InTokenListMacroProvide{<list-macro>}{<cmd>}|\]
177 %% avoids multiple entries of a token
178 %% by \emph{not} adding anything when <cmd>
179 %% already occurs in <list-macro> (again, this does not work with
180 %% braces, try `\in@{{}}{{}}').
181 \def\InTokenListMacroProvide#1#2{%
182 \TestListMacroForToken#1#2%
183 \ifin@ \else %% TODO warning?
184 \ToListMacroAdd#1#2%
185 \fi}
186 %%
187 %%
188 %% == Leaving and History ==
189 \PopLetterCatAt %% v0.3
190 \endinput
191
192 VERSION HISTORY
193 v0.1 2011/06/23/28 stored separately
194 v0.2 2011/11/02 simpler, documented
195 2011/11/03 corrected \if/\else for init
196 2011/11/07 \TestListMacroForToken, \InListMacroProvide;
197 doc.: \pagebreak, structure
198 2011/11/19 modified LaTeX supplements
199 v0.21 2012/05/14 fix for "generic" and `typeoutfileinfo':
200 @ before ...!
201 v0.21a 2012/05/19 \labels sec:apply, sec:core; \pagebreak?
202 v0.22 2012/06/04 allow {} items
203 v0.3 2012/11/05 updating copyright, using `plainpkg',
204 rewording documentation there