% mkpatter.tex Version 1.2
%
% This file is part of the program mkpattern, and all files
% that belong to that program must be distributed together.
%
% For this version 1.2, this file mkpatter.tex is the only file
% belonging to the program mkpattern.
%
% The file mkpatter.dvi (or mkpatter.pdf) is a manual for this program.
% It is very likely that you may obtain it at the same place
% where you got this file from.
%
% (c) Javier A. M'ugica, 2006, 2007, 2008
% License: LPPL version 1.3
%
% LPPL maintenance status: maintained
% Current Maintainer: Javier A. M'ugica
%
% For bug reports and comments:
%
% Javier M'ugica, javier at digi21.eu
%
%
\ifnum\catcode`\{=1
\catcode`\$=12 %
\catcode`\&=12 %
\catcode`\^^K=12 %
\catcode`\^^A=12 %
\let\bye\undefined
\fi
\chardef\active=13
\catcode`\{=1
\catcode`\}=2
\catcode`\#=6 % hash mark is macro parameter character
\catcode`\^=7
\catcode`\^^I=10 % ascii tab is a blank space
\catcode`\^^M=9 %We need a \relax after some numbers at the end of a line
\catcode`\^^L=\active \outer\def^^L{\par} % ascii form-feed is "\outer\par"
\catcode`\&=12
\catcode`\_=11
\catcode`\~=11
\catcode`\@=11\relax
\count11=9 %Allocates toks registers: 10, 11, ...
\count12=14 %Allocates count registers: 15, 16, ...
\count13=2 %Input streams: 3, ...
\count14=2 %Output streams: 3, ...
\def\wlog{\immediate\write\m@ne}
\countdef\@@count=0
\toksdef\tokscclv=255\relax
\toksdef\toks@=0\relax
\toksdef\toks@ne=1\relax
\toksdef\toksv=5\relax %It will no be changed by low level macros
\toksdef\toksvi=6\relax %It will no be changed by low level macros
\outer\def\newtoks#1{\advance\count11 1 \toksdef#1=\count11%
\wlog{\string#1=\backslash toks\the\count11}}
\outer\def\newcount#1{\advance\count12 1 \countdef#1=\count12%
\wlog{\string#1=\backslash count\the\count12}}
%These two are not used but are provided to the users
\outer\def\newread#1{\advance\count13 1 \chardef#1=\count13%
\wlog{\string#1=\backslash read\the\count13}}
\outer\def\newwrite#1{\advance\count14 1 \chardef#1=\count14%
\wlog{\string#1=\backslash write\the\count14}}
%Tracing switches. There isn't actually very much to trace
\newcount\tracingexceptions \tracingexceptions=0\relax
\def\iwrite{\immediate\openout0=\outputfilename
\gdef\filename##1{\errmessage{Too late for changing the output file name}}
\gdef\iwrite{\immediate\write0 }\iwrite
}
%Change of july 2008
\def\newwriteline{\edef\tempa{\the\linesofar}\ifx\tempa\empty\global\linesofar=\expandafter{\percent}\fi\~writeline}
\let\ifoldnewlines\iffalse
\def\newnewlines{\let\ifoldnewlines\iffalse\let\fi\fi}
\def\oldnewlines{\let\ifoldnewlines\iftrue\let\fi\fi}
%
\def\busca#1#2{% It will usually also work with #1 being lenghtier than one character,
\def\encuentra##1#1##2\quend{%but if the ending can be overlaped with the beginning it may not.
\def\tempa{##2}
\ifx\tempa\empty\tokscclv{iffalse}
\else\tokscclv{iftrue}
\fi
\expandafter\let\expandafter\iffound\csname\the\tokscclv\endcsname
}
\encuentra#2#1\quend
}
\def\first#1#2\qend{#1}
\def\second#1#2\qend{#2}
{
\catcode`\&=13
\gdef={
\catcode`\&=0\relax
\defineamp{#1}
}
}
\def\defineamp#1#2{
\expandafter\edef\csname#1\endcsname{#2 }
\catcode`\&=13\relax
%Add #1 to the list of names *at the right*; they must be executed in order.
\toksnames\expandafter{\the\toksnames\\{#1}}
}
\def\makelists{
\def\\##1{\expandafter\makelist\csname##1\endcsname}
\the\toksnames
}
\def\makelist#1{
\tokslista={}
\expandafter\appendtolist#1\qend %#1 contains at least a trailing space
{\def\\##1{##1 }
\edef\tempa{\expandafter\empty\the\tokslista}
\wlog{\string#1 :->\tempa}}
\edef#1{\the\tokslista}
}
\def\appendtolist#1 #2\qend{
\def\tempa{#2}
\ifx\tempa\empty
\def\next{}
\else
\def\next{\appendtolist#2\qend}
\fi
\def\tempa{#1}
\ifx\tempa\empty %if #2 is not empty we have gobbled a leading space
\else
\tokslista\expandafter{\the\tokslista\\{#1}}
\fi
\next
}
\def\lee{\gobbles\_lee}
\def\gobbles#1#2{% this just gobbles up and then executes #1
%Test if #2 is a single token
\def\tempa{#2}
\ifx\tempa\empty\else
\edef\tempa{\second#2\qend}
\fi
\ifx\tempa\empty\def\next{#1#2}
\else\def\next{#1{#2}}
\fi
\next
}
\def\_lee#1{
\edef\tempa{\expandafter\first\string#1a\qend}% The letter a is just in case #1 is empty
\ifx\tempa\backslash\expandafter#1% Each control sequence that may appear inside pseudopatterns must provide its own \lee at the end
\else\ifx\tempa\percent
\catcode13=12
\def\next{\comment#1}
\else
\def\tempa{#1}%Test if #1 is a single token
\ifx\tempa\empty\else
\edef\tempa{\second#1\qend}
\fi
\ifx\tempa\empty\def\next{\__lee#1}
\else\def\next{\__lee{#1}}
\fi
\fi
\expandafter\next\fi
}
\def\countchars#1#2{
\count255=\m@ne
\_countchars#2a\qend %Ensure that there is at least one token
#1=\count255\relax
}
\def\_countchars#1#2{
\ifx#2\qend\else
\advance\count255 by 1\relax
\def\next{\_countchars#2}
\expandafter\next\fi
}
\def\finalreplacement#1#2{#1={#2}}
\def\charreplacement#1#2{
#1={#2} %In case it is not found
\def\tempa{#2}
\let\oldslash\\
\def\\##1##2{\def\tempb{##1}
\ifx\tempa\tempb
#1={##2}
\def\\####1####2{}
\fi
}
\the\toksencoding
\let\\=\oldslash
}
\def\mkinput#1{\expandafter\lee\input #1 } %we are lucky this works
\let\~~endinput\endinput
% ...y la guinda:
\def\mkendinput{\expandafter\lee} %Expand the end of the file before \lee. It works!
\def\endinput{\expandafter\mkendinput\~~endinput}
%Esto ya es para el fichero que siga
\toksletters={}
\toksencoding={}
\newcount\c_amp