1
0
Fork 0
epmanners/epmanners.sty

129 lines
3.8 KiB
TeX

% epmanners---Teach LaTeX some manners to respect \everypar
%
% Copyright (C) 2013 Mike Gerwitz
%
% This file is part of epmanners.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%
% do not be alarmed.
\let\@@epm@the\the
\let\@@epm@ep\everypar
\newtoks\@@epm@tokempty
% first, insert an identifier into the \everypar token list, allowing us to
% recognize when tokens passed to the redefined \everypar below contain
% \the\everypar
% redefine \everypar as a command instead of a token register, allowing us
% to dictate its behavior more precisely; note that, in order for \the to
% work properly as if \everypar were still a token register, we must always
% have it produce the original \everypar
\def\everypar{%
\@@epm@tokempty{}%
\futurelet\@@epn\@epm@epf
}
\def\@epm@epf{%
\begingroup
\ifvmode
% the \expandafter here ensures that the common notation of
% \everypar\expandafter[...] will be executed as expected by assigning
% the value of \everypar to the value of the \expandafter expression,
% while still retaining proper evaluation of all other cases
\aftergroup\expandafter
\aftergroup\@epm@epf@set
\else
\aftergroup\the
\aftergroup\@@epm@ep
\fi
\endgroup
}
\newtoks\@@epm@epclean
\def\@epm@epf@set#1{%
\@epm@cleantok\@@epm@epclean{#1}%
\@@epm@ep\expandafter{\the\@@epm@epclean}%
}
\def\@epm@epf@the{%
\the\@@epm@ep
}
%%
% utilities
%
% consume next token
\def\@epm@omnom#1{}
% ``cleans'' tokens by stripping the result of \the\everypar and storing it
% in a destination token register
\def\@epm@cleantok#1#2{%
\let\@@epm@tokdest#1%
\global\@@epm@tokdest{}%
\futurelet\@@epn\@epm@docleantok#2\@@epm@end
}
\def\@epm@docleantok{%
\begingroup
% stop once we reach the end
\ifx\@@epn\@@epm@end
\aftergroup\@epm@omnom
\else
\ifx\@@epn\@epm@epf
% since this token is only used in the \everypar definition, we know
% that \the has been used; insert a \relax to cancel the \futurelet
% in its definition before continuing
\aftergroup\relax
\aftergroup\@epm@cleantok@strip
\else
% token is good; retain it and recurse, noting that we must handle
% space tokens separately to ensure that they are not gobbled up by
% the parser and lost forever
\ifcat\space\@@epn
\aftergroup\@epm@docleantok@recursesp
\else
\aftergroup\@epm@docleantok@recurse
\fi
\fi
\fi
\endgroup
}
% consumes the next token and then continues
\def\@epm@docleantok@recurse#1{%
\@@epm@tokdest\expandafter{\the\@@epm@tokdest#1}%
\futurelet\@@epn\@epm@docleantok
}
% similar to the above, but is intended to consume a blank token (charcode
% 10) and recurse, re-adding the token that follows it
\def\@epm@docleantok@recursesp#1{%
\@@epm@tokdest\expandafter{\the\@@epm@tokdest\ }%
\futurelet\@@epn\@epm@docleantok#1
}
\def\@epm@cleantok@strip#1{%
% this macro has stripped the entirety of the \everypar definition, so we
% are now free to replace it with whatever tokens we wish (that is, they
% typed \the\everypar, so we should produce \the\@@epm@ep)
\@@epm@tokdest\expandafter{%
\the\expandafter\@@epm@tokdest
\the\@@epm@ep%
}%
\futurelet\@@epn\@epm@docleantok
}