Debian Codegraph @

texmf-dist/tex/latex/spectralsequences/sseqforeach.code.tex

raw
1 %%
2 %% Package: spectralsequences v1.3.3 2023-01-28
3 %% Author: Hood Chatham
4 %% Email: [email protected]
5 %% Date: 2023-01-28
6 %% License: Latex Project Public License
7 %%
8 %% File: sseqforeach.code.tex
9 %%
10 %% Patches the \foreach command to obtain better error reporting
11 %% Also defines some of my own looping commands, \Do, \DoUntilOutOfBounds, \DoUntilOutOfBoundsThenNMore
12 %% All of the \foreach stuff we are modifying is defined by tikz in the file /pgf/utilities/pgffor.code.tex
13 %%
14
15 \def\sseq@for@vars{}
16 \def\sseq@for@savemacro#1{\sseq@d@addto@macro\sseq@for@vars{\sseq@for@vars@do#1}}
17 \def\sseq@for@savemacro@noslash#1{\sseq@d@addto@macro\sseq@for@vars{\sseq@for@vars@do@noslash#1}}
18 \def\sseq@for@vars@do#1{; \string#1 = #1}
19 \def\sseq@for@vars@do@noslash#1{; \sseq@macroname#1 = #1}
20
21 \ExplSyntaxOn
22 \def\sseq@for@printvars{\ifx\sseq@for@vars\pgfutil@empty\else\exp_last_unbraced:Nf\@gobble\sseq@for@vars\fi}
23
24 \newcount\sseq@Do@depth
25
26 \def\sseq@save@Do@var{
27 \sseq@atbeginforeach@msgsetup
28 \advance\sseq@Do@depth\@ne
29 \exp_args:Nc \sseq@for@savemacro@noslash { iteration ~ \the \sseq@Do@depth }
30 \def\iteration{0}
31 }
32
33 \def\sseq@stepiteration{
34 \edef\iteration{\the\numexpr\iteration+1}
35 \cs_set_eq:cN { iteration ~ \the \sseq@Do@depth } \iteration
36 }
37
38 \protected\def\sseq@Do{
39 \begingroup
40 \edef\sseq@inputline{\the\inputlineno}
41 \sseq@callas{\Do}
42 \sseq@call{\sseq@Do@}
43 }
44
45 \def\sseq@Do@#1#2{
46 \sseq@esetthiscall{\string\Do\unexpanded{#1}}
47 \sseq@opushstacktrace{\string\Do{#1}}
48 \sseq@ifintexpr{#1}{
49 \sseq@savestack
50 \sseq@save@Do@var
51 \prg_replicate:nn {#1} {
52 \sseq@stepiteration
53 #2
54 \relax
55 }
56 \sseq@restorestack
57 }{\sseq@error@xx{Do-invalid-int-expr}{\string\Do}{\unexpanded{#1}}}
58 \endgroup
59 }
60
61 \protected\def\sseq@DoUntilOutOfBounds{
62 \begingroup
63 \edef\sseq@inputline{\the\inputlineno}
64 \sseq@callas{\DoUntilOutOfBounds}
65 \sseq@call{\sseq@DoUntilOutOfBounds@}
66 }
67
68 \def\sseq@DoUntilOutOfBounds@ #1 {
69 \sseq@esetthiscall{\string\DoUntilOutOfBounds}
70 \sseq@opushstacktrace{\string\DoUntilOutOfBounds}
71 \ifx\sseq@xminmax\@gobbletwo\else\ifx\sseq@yminmax\@gobbletwo\else
72 \sseq@error@x{DoUntil-no-bound}{\string\DoUntilOutOfBounds}
73 \sseq@breakfifi
74 \fi\fi
75 \def\sseq@commandname{\string\DoUntilOutOfBounds}
76 \sseq@savestack % so we can nest these
77 \sseq@DoUntilOutOfBounds@body{#1}
78 \sseq@restorestack
79 \sseq@breakpoint
80 \endgroup
81 }
82
83 \protected\def\sseq@DoUntilOutOfBoundsThenNMore{
84 \begingroup
85 \edef\sseq@inputline{\the\inputlineno}
86 \sseq@callas{\DoUntilOutOfBoundsThenNMore}
87 \sseq@call{\sseq@DoUntilOutOfBoundsThenNMore@}
88 }
89
90 \def\sseq@DoUntilOutOfBoundsThenNMore@ #1#2 {
91 \sseq@esetthiscall{\string\DoUntilOutOfBoundsThenNMore\unexpanded{#1}}
92 \sseq@opushstacktrace{\string\DoUntilOutOfBoundsThenNMore{#1}}
93 \ifx\sseq@xminmax\@gobbletwo\else\ifx\sseq@yminmax\@gobbletwo\else
94 \sseq@error@n{DoUntil-no-bound}{\DoUntilOutOfBoundsThenNMore}
95 \sseq@breakfifi
96 \fi\fi
97 \sseq@ifintexpr{#1}{}{
98 \sseq@error@xx{Do-invalid-int-expr}{\string\DoUntilOutOfBoundsThenNMore}{\unexpanded{#1}}
99 \sseq@break
100 }
101 \def\sseq@commandname{\string\DoUntilOutOfBoundsThenNMore}
102 \sseq@savestack
103 \sseq@DoUntilOutOfBounds@body{#2}
104 \prg_replicate:nn {#1} {
105 \sseq@stepiteration
106 #2
107 \relax
108 }
109 \sseq@restorestack
110 \sseq@breakpoint
111 \endgroup
112 }
113
114 \def\sseq@DoUntilOutOfBounds@body#1{
115 \sseq@save@Do@var % Set iteration so that \DoUntilOutOfBoundsThenNMore doesn't get upset if we're already out of bounds
116 % If we're already out of bounds, we'll just do nothing.
117 \sseq_if_out_of_bounds_noparse:nnTF{\lastx{0}}{\lasty{0}}{
118 \bool_set_true:N \l_tmpa_bool
119 }{
120 \bool_set_false:N \l_tmpa_bool
121 % Now we need to set up the loop descent condition.
122 \sseq@tempxb=\lastx{0}\relax % Record current x and y values
123 \sseq@tempyb=\lasty{0}\relax
124 \sseq@stepiteration
125 #1 % run loop body once
126 \relax % protect from \d page grabber
127 \sseq_if_out_of_bounds_noparse:nnTF{\lastx{0}}{\lasty{0}}{ % If we're out of bounds now, we can quit
128 \bool_set_true:N \l_tmpa_bool
129 }{ % Otherwise, determine descent check
130 \sseq@tempx=\lastx{0}\relax % store new last value for comparison
131 \sseq@tempy=\lasty{0}\relax
132 \bool_set_false:N \l_tmpb_bool % This is to record whether there is a defined x range or y range. If neither, we'll throw an error.
133 % \def\sseq@checkbound{\bool_set_false:N \l_tmpb_bool}
134 \ifx\sseq@xminmax\@gobbletwo
135 \ifnum\sseq@tempx>\sseq@tempxb
136 \bool_set_true:N \l_tmpb_bool % We have a descent condition
137 % \sseq@d@addto@macro\sseq@checkbound{
138 % \ifnum\sseq@tempx>\sseq@tempxb % Any stage of the loop is okay as long as it increases x
139 % \bool_set_true:N \l_tmpb_bool
140 % \fi
141 % }
142 \else
143 \ifnum\sseq@tempx<\sseq@tempxb
144 \bool_set_true:N \l_tmpb_bool
145 % \sseq@d@addto@macro\sseq@checkbound{ % Any stage of the loop is okay as long as it decreases x
146 % \ifnum\sseq@tempx<\sseq@tempxb
147 % \bool_set_true:N \l_tmpb_bool
148 % \fi
149 % }
150 \fi
151 \fi
152 \fi
153 \ifx\sseq@yminmax\@gobbletwo
154 \ifnum\sseq@tempy>\sseq@tempyb
155 \bool_set_true:N \l_tmpb_bool
156 % \sseq@d@addto@macro\sseq@checkbound{
157 % \ifnum\sseq@tempy>\sseq@tempyb
158 % \bool_set_true:N \l_tmpb_bool
159 % \fi
160 % }
161 \else
162 \ifnum\sseq@tempy<\sseq@tempyb
163 \bool_set_true:N \l_tmpb_bool
164 % \sseq@d@addto@macro\sseq@checkbound{
165 % \ifnum\sseq@tempy<\sseq@tempyb
166 % \bool_set_true:N \l_tmpb_bool
167 % \fi
168 % }
169 \fi
170 \fi
171 \fi
172 \if_bool:N \l_tmpb_bool\else:
173 \sseq@error@x{DoUntil-no-progress}{\sseq@commandname}
174 \fi:
175 }
176 }
177 \bool_until_do:Nn \l_tmpa_bool {
178 \sseq@stepiteration
179 #1
180 \relax
181 \sseq_if_out_of_bounds_noparse:nnTF{\lastx{0}}{\lasty{0}}{% we're done
182 \bool_set_true:N \l_tmpa_bool
183 }{% Check descent condition
184 % \ifnum\iteration/10*10-\iteration=\z@
185 % \sseq@tempx=\lastx{0}\relax % store new last value for comparison
186 % \sseq@tempy=\lasty{0}\relax
187 % \bool_set_false:N \l_tmpb_bool
188 % \sseq@checkbound
189 % \if_bool:N \l_tmpb_bool\else:
190 % \sseq@error{DoUntilOutOfBounds-descent-failed}
191 % \fi:
192 % \sseq@tempxb=\sseq@tempx
193 % \sseq@tempyb=\sseq@tempx
194 % \fi
195 }
196 }
197 }
198
199
200
201 \ExplSyntaxOff
202
203
204 \def\sseq@for@nopatch{\sseq@error{foreach-patch-failed}\def\sseq@patchfor{}\endinput}
205
206 \newtoks\sseq@foreachcall
207
208 \def\sseq@pgffor@atbeginforeach{%
209 \begingroup %
210 \sseq@atbeginforeach@msgsetup
211 % \pgffor@macro@list calls \pgffor@normal@list, so we need to mark that the list has already been added to foreachcall.
212 \sseq@tempiftrue
213 }
214
215
216 \def\sseq@patchfor{%
217 \let\pgffor@atbeginforeach\sseq@pgffor@atbeginforeach
218 \let\pgffor@@vars@opt\sseq@pgffor@@vars@opt
219 }
220 \def\sseq@pgffor@modify#1{%
221 \@xp\let\csname sseq@\sseq@macroname#1\endcsname #1%
222 \eappto\sseq@patchfor{\let\@nx#1 \@xp\@nx\csname sseq@\sseq@macroname#1\endcsname}%
223 }
224
225
226
227 \def\sseq@pgffor@recordarg#1#2{
228 \sseq@pgffor@modify#1
229 \@xp\pretocmd\csname sseq@\sseq@macroname#1\endcsname{\sseq@foreachcall\@xp{\the\sseq@foreachcall#2}}{}{\sseq@for@nopatch}
230 }
231 \def\sseq@pgffor@erecordarg#1#2{
232 \sseq@pgffor@modify#1
233 \@xp\pretocmd\csname sseq@\sseq@macroname#1\endcsname{\sseq@eval{\sseq@foreachcall{\the\sseq@foreachcall#2}}}{}{\sseq@for@nopatch}
234 }
235
236
237 \bgroup\lccode`\*=`\#\lowercase{\egroup
238
239 % Modify the foreach argument parser commands to put the call into \sseq@foreachcall and to tell us what the variables are
240 \def\sseq@pgffor@@vars@opt[#1]{\sseq@foreachcall\@xp{\the\sseq@foreachcall#1}\pgfkeys{/sseqpages/foreach/.cd,#1}\pgffor@vars}
241
242 \sseq@pgffor@recordarg\pgffor@@vars{*1}
243 \pretocmd\sseq@pgffor@@vars{\sseq@for@savemacro*1}{}{\sseq@for@nopatch}
244
245 \sseq@pgffor@recordarg\pgffor@@vars@slash@gobble{/}
246 \sseq@pgffor@recordarg\pgffor@macro@list{in *1}
247 \pretocmd\sseq@pgffor@macro@list{\sseq@tempiffalse}{}{\sseq@for@nopatch}% Don't add this again in \pgffor@normal@lis
248
249 \sseq@pgffor@modify\pgffor@normal@list
250 % Add list to argument if it wasn't a macro
251 \pretocmd\sseq@pgffor@normal@list{\ifsseq@tempif\sseq@foreachcall\@xp{\the\sseq@foreachcall in {*1}}\fi\sseq@tempiftrue}{}{\sseq@for@nopatch}
252
253 \sseq@pgffor@recordarg\pgffor@collectforeach@macro{\foreach}
254 \sseq@pgffor@recordarg\pgffor@collectforeach@normal{\foreach}
255
256 \sseq@pgffor@modify\pgffor@iterate
257 \pretocmd\sseq@pgffor@iterate{\sseq@opushstacktrace{\the\sseq@foreachcall}\sseq@thiscalltoks\@xp{\the\sseq@foreachcall}}{}{\sseq@for@nopatch}
258
259 \sseq@pgffor@modify\pgffor@doloop
260 \sseq@pgffor@modify\pgffor@invokebody
261 \patchcmd\sseq@pgffor@doloop{\pgffor@begingroup}{\pgffor@begingroup\sseq@xsetlastcall{\the\sseq@foreachcall}}{}{\sseq@for@nopatch}
262 \patchcmd\sseq@pgffor@invokebody{\pgffor@begingroup}{\pgffor@begingroup\sseq@xsetlastcall{\the\sseq@foreachcall}}{}{\sseq@for@nopatch}
263 }
264
265 %TODO: also hook \pgffor@assign@parse, \pgffor@remember@parse, \pgffor@count@parse.
266