Debian Codegraph @

texmf-dist/tex/plain/mkpattern/mkpatter.tex

raw
1 % mkpatter.tex Version 1.2
2 %
3 % This file is part of the program mkpattern, and all files
4 % that belong to that program must be distributed together.
5 %
6 % For this version 1.2, this file mkpatter.tex is the only file
7 % belonging to the program mkpattern.
8 %
9 % The file mkpatter.dvi (or mkpatter.pdf) is a manual for this program.
10 % It is very likely that you may obtain it at the same place
11 % where you got this file from.
12 %
13 % (c) Javier A. M'ugica, 2006, 2007, 2008
14 % License: LPPL version 1.3
15 %
16 % LPPL maintenance status: maintained
17 % Current Maintainer: Javier A. M'ugica
18 %
19 % For bug reports and comments:
20 %
21 % Javier M'ugica, javier at digi21.eu
22 %
23 %
24 \ifnum\catcode`\{=1
25 \catcode`\$=12 %
26 \catcode`\&=12 %
27 \catcode`\^^K=12 %
28 \catcode`\^^A=12 %
29 \let\bye\undefined
30 \fi
31
32 \chardef\active=13
33 \catcode`\{=1
34 \catcode`\}=2
35 \catcode`\#=6 % hash mark is macro parameter character
36 \catcode`\^=7
37 \catcode`\^^I=10 % ascii tab is a blank space
38 \catcode`\^^M=9 %We need a \relax after some numbers at the end of a line
39 \catcode`\^^L=\active \outer\def^^L{\par} % ascii form-feed is "\outer\par"
40 \catcode`\&=12
41 \catcode`\_=11
42 \catcode`\~=11
43 \catcode`\@=11\relax
44
45 %Latex
46 \catcode`\:=11\relax
47 \ifx\@@end\undefined\else\let\end\@@end \let\@@end\undefined\fi
48 \ifx\tex_end:D\undefined\else \let\end\tex_end:D \let\tex_end:D\undefined\fi
49 \catcode`\:=12\relax
50
51 \newlinechar=`\^^J
52 \message{This is Mkpattern, Version 1.2 JUN 2008^^J}
53 \let\bgroup={
54 \let\egroup=}
55
56 \def\empty{}
57 \def\space{ }
58 \def\qend{\qend}
59 \countdef\m@ne=10 \m@ne=-1 %
60 \def\_qlast{?_qlast}
61 {\escapechar=\m@ne\relax
62 \xdef\backslash{\string\\}
63 \xdef\percent{\string\%}
64 }
65
66 \count11=9 %Allocates toks registers: 10, 11, ...
67 \count12=14 %Allocates count registers: 15, 16, ...
68 \count13=2 %Input streams: 3, ...
69 \count14=2 %Output streams: 3, ...
70 \def\wlog{\immediate\write\m@ne}
71 \countdef\@@count=0
72 \toksdef\tokscclv=255\relax
73 \toksdef\toks@=0\relax
74 \toksdef\toks@ne=1\relax
75 \toksdef\toksv=5\relax %It will no be changed by low level macros
76 \toksdef\toksvi=6\relax %It will no be changed by low level macros
77 \outer\def\newtoks#1{\advance\count11 1 \toksdef#1=\count11%
78 \wlog{\string#1=\backslash toks\the\count11}}
79 \outer\def\newcount#1{\advance\count12 1 \countdef#1=\count12%
80 \wlog{\string#1=\backslash count\the\count12}}
81 %These two are not used but are provided to the users
82 \outer\def\newread#1{\advance\count13 1 \chardef#1=\count13%
83 \wlog{\string#1=\backslash read\the\count13}}
84 \outer\def\newwrite#1{\advance\count14 1 \chardef#1=\count14%
85 \wlog{\string#1=\backslash write\the\count14}}
86
87 %Tracing switches. There isn't actually very much to trace
88 \newcount\tracingexceptions \tracingexceptions=0\relax
89
90 \edef\outputfilename{\jobname.pat}
91
92 \def\filename{\bgroup
93 \catcode`\|=12
94 \count255='176 \@@count=256 \makeothers
95 \count255=0 \@@count='173 \makeothers
96 \_filename
97 }
98 \def\_filename#1{\gdef\outputfilename{#1}\egroup}
99
100 \def\iwrite{\immediate\openout0=\outputfilename
101 \gdef\filename##1{\errmessage{Too late for changing the output file name}}
102 \gdef\iwrite{\immediate\write0 }\iwrite
103 }
104
105 \let\iffound\iffalse
106
107 \let\~~end\end
108 \def\~end{\ifnum\tracingexceptions>0\expandafter\reportmissingexceptios\fi\~~end}
109 \def\begin#1{\csname#1\endcsname}
110 \def\end#1{\def\tempa{#1}\ifx\tempa\empty\expandafter\~end\fi\csname end#1\endcsname}
111
112 \newtoks\toksnames \toksnames={}
113 \newtoks\tokslista
114 \newtoks\linesofar \linesofar={}
115 \newtoks\toksencoding \toksencoding={}
116 \newtoks\toksletters \toksletters={}
117
118 \def\writeline{\iwrite{\the\linesofar}\global\linesofar={}}
119 \def\conditwrite{\edef\tempa{\the\linesofar}\ifx\tempa\empty\else\writeline\fi}
120 \def\newline{\writeline\lee}
121
122 %Change of july 2008
123 \def\newwriteline{\edef\tempa{\the\linesofar}\ifx\tempa\empty\global\linesofar=\expandafter{\percent}\fi\~writeline}
124 \let\ifoldnewlines\iffalse
125 \def\newnewlines{\let\ifoldnewlines\iffalse\let\fi\fi}
126 \def\oldnewlines{\let\ifoldnewlines\iftrue\let\fi\fi}
127 %
128
129 \def\busca#1#2{% It will usually also work with #1 being lenghtier than one character,
130 \def\encuentra##1#1##2\quend{%but if the ending can be overlaped with the beginning it may not.
131 \def\tempa{##2}
132 \ifx\tempa\empty\tokscclv{iffalse}
133 \else\tokscclv{iftrue}
134 \fi
135 \expandafter\let\expandafter\iffound\csname\the\tokscclv\endcsname
136 }
137 \encuentra#2#1\quend
138 }
139
140 \def\first#1#2\qend{#1}
141 \def\second#1#2\qend{#2}
142
143 {
144 \catcode`\&=13
145 \gdef&#1={
146 \catcode`\&=0\relax
147 \defineamp{#1}
148 }
149 }
150 \def\defineamp#1#2{
151 \expandafter\edef\csname#1\endcsname{#2 }
152 \catcode`\&=13\relax
153 %Add #1 to the list of names *at the right*; they must be executed in order.
154 \toksnames\expandafter{\the\toksnames\\{#1}}
155 }
156 \def\makelists{
157 \def\\##1{\expandafter\makelist\csname##1\endcsname}
158 \the\toksnames
159 }
160 \def\makelist#1{
161 \tokslista={}
162 \expandafter\appendtolist#1\qend %#1 contains at least a trailing space
163 {\def\\##1{##1 }
164 \edef\tempa{\expandafter\empty\the\tokslista}
165 \wlog{\string#1 :->\tempa}}
166 \edef#1{\the\tokslista}
167 }
168 \def\appendtolist#1 #2\qend{
169 \def\tempa{#2}
170 \ifx\tempa\empty
171 \def\next{}
172 \else
173 \def\next{\appendtolist#2\qend}
174 \fi
175 \def\tempa{#1}
176 \ifx\tempa\empty %if #2 is not empty we have gobbled a leading space
177 \else
178 \tokslista\expandafter{\the\tokslista\\{#1}}
179 \fi
180 \next
181 }
182
183 \def\lee{\gobbles\_lee}
184 \def\gobbles#1#2{% this just gobbles up and then executes #1
185 %Test if #2 is a single token
186 \def\tempa{#2}
187 \ifx\tempa\empty\else
188 \edef\tempa{\second#2\qend}
189 \fi
190 \ifx\tempa\empty\def\next{#1#2}
191 \else\def\next{#1{#2}}
192 \fi
193 \next
194 }
195
196 \def\_lee#1{
197 \edef\tempa{\expandafter\first\string#1a\qend}% The letter a is just in case #1 is empty
198 \ifx\tempa\backslash\expandafter#1% Each control sequence that may appear inside pseudopatterns must provide its own \lee at the end
199 \else\ifx\tempa\percent
200 \catcode13=12
201 \def\next{\comment#1}
202 \else
203 \def\tempa{#1}%Test if #1 is a single token
204 \ifx\tempa\empty\else
205 \edef\tempa{\second#1\qend}
206 \fi
207 \ifx\tempa\empty\def\next{\__lee#1}
208 \else\def\next{\__lee{#1}}
209 \fi
210 \fi
211 \expandafter\next\fi
212 }
213
214 \def\__lee#1 {%Do not remove this space
215 %Backslash and comment handled above
216 %\else
217 \ifappliedtemplate
218 \busca{&}{#1}
219 \iffound
220 \edef\tempa{\the\linesofar}\ifx\tempa\empty\else\writeline\fi
221 \advance\c_amp1 %
222 \tokslista={}\procesamacro\tokslista{#1}
223 \def\next{\expandafter\lee\the\tokslista%
224 \advance\c_amp\m@ne \writeline\lee}
225 \else
226 \def\texto{#1}
227 \edef\tempa{\the\toksexceptions}
228 \ifx\tempa\empty\else\substituteifneeded\texto{#1}\fi
229 \toks@\expandafter{\texto}
230 \edef\tempa{\noexpand\linesofar{\the\linesofar\the\toks@\space}}
231 \tempa
232 \def\next{\lee}
233 \fi
234 \else
235 \tokscclv{#1}
236 \edef\texto{\the\pretemplate\the\tokscclv \the\posttemplate}
237 \let\ifappliedtemplate\iftrue\let\fi\fi
238 \def\next{\expandafter\lee\texto\let\ifappliedtemplate\iffalse\let\fi\fi \lee}
239 \fi
240 %\fi
241 \next
242 }
243
244 \def\procesamacro#1#2{
245 \leemacro#2\qend
246 \def\\##1{%
247 \edef\tempa{\the\tokslista\the\premacro##1\the\postmacro}
248 \tokslista\expandafter{\tempa}
249 }
250 \csname\macro\endcsname
251 }
252 \def\leemacro#1&#2#3\qend{
253 \premacro={#1}
254 \def\macro{#2}
255 \postmacro={#3 }
256 }
257
258 \def\template#1{
259 \conditwrite\writeline
260 \bgroup
261 \savetemplate#1\qend
262 \let\ifappliedtemplate\iffalse
263 \lee
264 }
265 \def\endtemplate{\conditwrite\egroup\lee}
266
267 \catcode`\$=6
268 \catcode`\#=12
269 \def\savetemplate$1#$2\qend{
270 \pretemplate={$1}
271 \posttemplate={$2 }
272 }
273 \catcode`\#=6
274
275 \newtoks\premacro
276 \newtoks\postmacro
277 \newtoks\pretemplate
278 \newtoks\posttemplate
279 \newtoks\toksexceptions
280 \newtoks\substitutions
281
282 %Change of july 2008
283 \def\oldnewstuff{
284 \let\ifopened\ifoldnewlines
285 \edef\tempa{\the\toksencoding}
286 \ifx\tempa\empty\else
287 \let\ifopened\iftrue\let\fi\fi
288 \fi
289 \ifopened
290 \iwrite{\string{\iffalse}\fi}
291 \fi
292 \ifx\tempa\empty\else\writeencodingreplacements \iwrite{}\fi
293 \conditwrite
294 \ifoldnewlines
295 \iwrite{\string\catcode13=10 }
296 \else
297 \let\~writeline\writeline
298 \let\writeline\newwriteline
299 \fi
300 \def\oldnewlines{\relax\lee}
301 \let\newnewlines\oldnewlines
302 }
303 %
304 \def\pseudopatterns{
305 \makelists\wlog{}
306 \writeletters \iwrite{} %We cannot use newline
307 \oldnewstuff
308 \iwrite{\string\patterns\string{\iffalse}\fi}
309 \bgroup
310 \catcode`\#=12
311 \catcode`\&=12
312 \catcode`\,=12
313 \let\ifappliedtemplate\iftrue
314 \c_amp=0 %finally not used, but kept updated
315 \toksexceptions={}
316 \substitutions={}
317 \catcode`\^^M=10
318 \let\lee\~~lee
319 \halfcomments %this itself includes lee
320 }
321 \def\endpseudopatterns{\conditwrite\iwrite{\iffalse{\fi\string}}\egroup
322 \ifoldnewlines\iwrite{\string\catcode13=5}\fi
323 \ifopened\iwrite{\iffalse{\fi\string}}\fi
324 \lee
325 }
326
327 \def\~put#1{\expandafter\global\expandafter\linesofar\expandafter{\the\linesofar #1}\egroup\lee}
328 \gdef\put{\bgroup
329 \catcode`\|=12
330 \count255='176 \@@count=256 \makeothers
331 \count255=0 \@@count='173 \makeothers
332 \~put
333 }
334
335 \def\nocomments{\catcode`\%=14 \lee}
336 \def\keepcomments{\catcode`\%=12 \let\comment\copycomment\lee}
337 \def\halfcomments{\catcode`\%=12 \let\comment\swallowcomment\lee}
338 {\catcode13=12 %
339 \gdef\copycomment#1
340 {\tokscclv={#1}\conditwrite\iwrite{\the\tokscclv}\catcode13=10 \lee}%
341 \gdef\swallowcomment#1
342 {\catcode13=10 \lee}%
343 }
344
345 \def\appendtoks_fromdelimiter#1#2#3{
346 \toks@=#1
347 \tokscclv={#2#3?_qlast#3}
348 \expandafter\ifx\csname _appendtoks_fromdelimiter#3\endcsname\relax %Don, this should be undefined
349 \expandafter\gdef\csname _appendtoks_fromdelimiter#3\endcsname##1#3{
350 \def\tempa{##1}
351 \ifx\tempa\_qlast
352 \else
353 \toks@\expandafter{\the\toks@\\{##1}}
354 \def\next{\expandafter\gobbles\csname _appendtoks_fromdelimiter#3\endcsname}
355 \expandafter\next
356 \fi
357 }
358 \fi
359 \edef\next{\noexpand\gobbles\expandafter\noexpand\csname _appendtoks_fromdelimiter#3\endcsname\the\tokscclv}
360 \next
361 #1=\toks@
362 }
363
364 \def\appendtoks_fromspace#1#2{\appendtoks_fromdelimiter#1{#2}{ }}
365 \def\appendtoks_fromcomma#1#2{\appendtoks_fromdelimiter#1{#2}{,}}
366
367 \def\exceptions#1{\catcode`\ =12 \_exceptions{#1}}
368 \def\_exceptions#1#2{
369 \catcode`\ =10 %
370 \appendtoks_fromspace\toksexceptions{#1}
371 \def\tempa{#2}
372 \ifx\tempa\empty
373 \tokscclv={#1 ?_qlast }
374 \expandafter\gobbles\expandafter\makeemptysubs\the\tokscclv
375 \else
376 \appendtoks_fromcomma\substitutions{#2}
377 \fi
378 \lee
379 }
380
381 \def\makeemptysubs#1 {
382 \def\tempa{#1}
383 \ifx\tempa\_qlast
384 \else
385 \countchars\@@count{#1}
386 \advance\@@count 1 %
387 \makeempty\tempa\@@count
388 \edef\next{\noexpand\substitutions{\the\substitutions\noexpand\\{\tempa}}}
389 \next
390 \def\next{\gobbles\makeemptysubs}
391 \expandafter\next
392 \fi
393 }
394
395 \def\countchars#1#2{
396 \count255=\m@ne
397 \_countchars#2a\qend %Ensure that there is at least one token
398 #1=\count255\relax
399 }
400 \def\_countchars#1#2{
401 \ifx#2\qend\else
402 \advance\count255 by 1\relax
403 \def\next{\_countchars#2}
404 \expandafter\next\fi
405 }
406
407 \def\makeempty#1#2{
408 \def#1{}
409 \def\next{\ifnum#2=0 \else\edef#1{#1 }\advance#2 \m@ne \expandafter\next\fi}
410 \next
411 }
412
413 \def\substituteifneeded#1#2{
414 \def\tempb{#2}
415 \toks@={} \toks@ne={}
416 \def\\##1{\def\tempa{##1}
417 \ifx\tempa\qend
418 \toksexceptions\toks@
419 \toks@={}
420 \substitutions\toks@ne
421 \toks@ne={}
422 \else\ifx\tempa\tempb
423 \pop\toksexceptions
424 \edef\tempa{\global\noexpand\toksexceptions{\the\toks@\the\toksexceptions}}
425 \tempa
426 \pushpopmacro#1\substitutions
427 \edef\tempa{\global\noexpand\substitutions{\the\toks@ne\the\substitutions}}
428 \tempa
429 \def\\####1{} %gobble the remaining \\{...}
430 \ifnum\tracingexceptions>0 %
431 \wlog{Replacing the pattern {\tempb} with {#1}}
432 \fi
433 \else
434 \let\tempa\\
435 \pushpop\toks@\toksexceptions
436 \pushpop\toks@ne\substitutions
437 \let\\\tempa
438 \fi\fi
439 }
440 \the\toksexceptions\\{\qend}
441 }
442
443 \def\pop#1{
444 \def\\##1##2//{#1={##2}}
445 \the#1//
446 }
447 \def\pushpop#1#2{
448 \def\\##1##2//{
449 #1=\expandafter{\the#1\\{##1}}
450 #2={##2}
451 }
452 \the#2//
453 }
454 \def\pushpopmacro#1#2{
455 \def\\##1##2//{
456 \def#1{##1}
457 #2={##2}
458 }
459 \the#2//
460 }
461
462 \def\reportmissingexceptios{
463 \def\\##1{\wlog{The exception {##1} did not arise.}}
464 \the\toksexceptions
465 }
466
467 \def\mixtoks#1#2{
468 \tokscclv=#1
469 \toks@=#2
470 #1={}
471 \def\tempa##1{#1=\expandafter{\the#1\\{##1}}\let\\=\tempb \the#2//}
472 \def\tempb##1##2//{#1=\expandafter{\the#1{##1}}#2={##2}\let\\=\tempa}
473 \let\\=\tempa
474 \the\tokscclv
475 }
476
477 \def\finalreplacement#1#2{#1={#2}}
478 \def\charreplacement#1#2{
479 #1={#2} %In case it is not found
480 \def\tempa{#2}
481 \let\oldslash\\
482 \def\\##1##2{\def\tempb{##1}
483 \ifx\tempa\tempb
484 #1={##2}
485 \def\\####1####2{}
486 \fi
487 }
488 \the\toksencoding
489 \let\\=\oldslash
490 }
491
492 \def\letters#1{\appendtoks_fromspace\toksletters{#1}}
493
494 \def\encodingreplacements#1#2{
495 \appendtoks_fromspace\toksv{#1}
496 \appendtoks_fromspace\toksvi{#2}
497 \mixtoks\toksv\toksvi
498 \tokscclv=\toksv \toksv={}
499 \def\\##1##2{\def\tempa{##1}\def\tempb{##2}\ifx\tempa\tempb\else
500 \toksv\expandafter{\the\toksv\\{##1}{##2}}\fi}
501 \the\tokscclv
502 \edef\next{\noexpand\toksencoding{\the\toksencoding\the\toksv}}
503 \next
504 }
505
506 \catcode`\/=0
507 \catcode`\\=12/relax
508
509 /def/writeencodingreplacements{
510 %/def/\##1##2{/iwrite{\def\foo{##2} \catcode`\##1=13 \edef##1{\foo}}}
511 /def/\##1##2{/iwrite{\catcode`\##1=13 \edef##1{\string ##2}}
512 /wlog{Replacement: ##1->##2}}
513 /the/toksencoding
514 }
515
516 /def/writeletters{
517 /def/\##1{/charreplacement/toksv{##1}
518 /iwrite{\catcode`\/the/toksv=11 \lccode`\/the/toksv=`\/the/toksv}
519 }
520 /the/toksletters
521 }
522 /catcode`/\=0
523 /catcode`//=12
524
525 \def\makeletters{
526 \expandafter\catcode\count255=11\relax
527 \advance\count255 1\relax
528 \ifnum\count255<\@@count
529 \expandafter\makeletters
530 \fi
531 }
532 \def\makeothers{
533 \expandafter\catcode\count255=12\relax
534 \advance\count255 1\relax
535 \ifnum\count255<\@@count
536 \expandafter\makeothers
537 \fi
538 }
539
540 \def\code{
541 \edef\code_percent{\the\catcode`\%}
542 \catcode`\%=14
543 \catcode`\#=6
544 \catcode`\^=7
545 \count255='50 \@@count='60
546 \makeothers
547 \catcode`\_=12
548 \catcode`\~=12
549 \catcode`\@=12
550 \catcode`\!=12
551 \catcode`\$=12
552 \catcode`\&=12\relax
553 }
554 \def\endcode{
555 \catcode`\#=11
556 \catcode`\^=11
557 \count255='50 \@@count='60
558 \makeletters
559 \catcode`\_=11
560 \catcode`\~=11
561 \catcode`\@=11
562 \catcode`\!=11
563 \catcode`\$=11
564 \catcode`\&=13
565 \catcode`\%=\code_percent\relax
566 \lee
567 }
568
569 \let\if@patternscode\iffalse
570 \def\patternscode{
571 \begin{code}
572 \catcode`\$=6
573 \catcode`\#=12
574 \catcode`\&=12
575 \catcode`\,=12
576 \let\if@patternscode\iftrue
577 }
578 \def\endpatternscode{
579 \catcode`\,=11
580 \catcode`\$=11
581 \let\if@patternscode\iffalse
582 \endcode
583 }
584
585 \def\templatedef{\if@patternscode\else\expandafter\patternscode\fi\let\if@patternscode\iffalse\@templatedef}
586 \def\@templatedef#1#2{\expandafter\def\csname #1\endcsname{\template{#2}}
587 \expandafter\def\csname end#1\endcsname{\end{template}}
588 \if@patternscode\else\expandafter\endpatternscode\fi}
589
590 \def\mkinput#1{\expandafter\lee\input #1 } %we are lucky this works
591 \let\~~endinput\endinput
592 % ...y la guinda:
593 \def\mkendinput{\expandafter\lee} %Expand the end of the file before \lee. It works!
594 \def\endinput{\expandafter\mkendinput\~~endinput}
595
596 %Esto ya es para el fichero que siga
597 \toksletters={}
598 \toksencoding={}
599 \newcount\c_amp
600
601 \catcode`\!=11
602 \catcode`\$=11
603 \catcode`\&=13
604
605 \count255='50 \@@count='60
606 \makeletters
607
608 \catcode`\[=11 \catcode`\]=11
609 \catcode`\|=11
610
611 \count255=129 \@@count=256
612 \makeletters
613
614 \catcode`\^^M=10
615 \catcode`\^=11
616 \let\~~lee\lee
617 \let\lee\relax
618 \endinput