\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{monofill}[2012/10/29 v0.2 monospace alignment (UL)]

%% Copyright (C) 2012 Uwe Lueck,
%% http://www.contact-ednotes.sty.de.vu
%% -- author-maintained in the sense of LPPL below --
%%
%% This file can be redistributed and/or modified under
%% the terms of the LaTeX Project Public License; either
%% version 1.3c of the License, or any later version.
%% The latest version of this license is in
%%     http://www.latex-project.org/lppl.txt
%% We did our best to help you, but there is NO WARRANTY.
%%
%% Please report bugs, problems, and suggestions via
%%
%%   http://www.contact-ednotes.sty.de.vu
%%
%% == User Commands      ==
%% \label{sec:user}
%% %% \[...\] rather than \\[...] 2012/10/29:
%% \[|\MFfieldtemplate[<fill-element>]{<field>}{<template>}|\]
%% determines the width of fields with id <field> to be the same 
%% as of <template>:
\newcommand*{\MFfieldtemplate}[3][\MFfillelement]{%
%% % `\space' is the default <fill-element>, stored as first argument
%% % of `\MF@make@bg'. 
%% `\@bg' delimits the ``background" or 
%% ``filler list". The field id is stored at the end ahead.
    \MF@make@bg#1#3\MF@store@field@bg\@bg{#2}}
%% `\MF@make@bg' is defined in Sec.~\ref{sec:field}.
%% \medbreak\noindent                               %% \medbreak 2012/03/29
%% %% <- TODO as in morgan.sty "vert. cmd. box"
%% \[|\MFfillelement|\]                             %% \[...\] 2012/10/29
%% is the default for <fill-element>, 
%% defined to be (like) `\space' here:
\newcommand*{\MFfillelement}{}  \let\MFfillelement\space
%% <fill-element> \emph{must} be a \emph{``single item"}
%% (that \TeX\ converts into a single token, due to our 
%%  comparison mechanism), so for using somewhat more complex <complex> than `\space',
%% \[|\renewcommand*{\MFfillelement}{<complex>}|\]  %% |...| 2012/10/29
%% must be used instead of the optional argument.---It was very hard 
%% for me with \emph{typesetting}, what finally worked were
%% |\MFspace| and
%% |\MFenspace| as alternative optional arguments. 
%% It is fine for half-quad spaces such as characters with `\tt' 
%% figures with more Computer Modern fonts:
\newcommand*{\MFspace}{\mbox{ }}
% \newcommand*{\MFenspace}{\leavevmode\enspace}
\newcommand*{\MFenspace}{\mbox{\enspace}}
%% For using the \ctanpkgref{nicefilelist} and \ctanpkgref{hardwrap} 
%% packges together, I needed the following |\MFotherspace| as 
%% `\MFfillelement'---expanding to a character token that is a 
%% blank space according to its character code, but belongs to the 
%% ``other" category:
\newcommand*{\MFotherspace}{} {\@makeother\ \gdef\MFotherspace{ }}
%% More generally, I guess that this is the perfect ``filling
%% element" in text to be wrapped by 'hardwrap'.
%% \medbreak\noindent                               %% \medbreak 2012/10/29
%% \[|\MFleftinfield{<text>}{<field>}|\] 
%% returns <text>, 
%% followed by <fill-elements> to get as many elements 
%% (characters) as the <template> associated with <field>:
\newcommand*{\MFleftinfield}{\MF@check@field l}
%% % \smallskip\noindent
%% \[|\MFrightinfield{<text>}{<field>}|\] %% \[...\] 2012/10/29
%% returns the <fill-elements> before giving <text>:
\newcommand*{\MFrightinfield}{\MF@check@field r}
%% `\MF@check@field' is defined in Sec.~\ref{sec:text}.
%%
%% \pagebreak                               %% 2012/10/29
%% == Internal Commands  ==
%% === Tools             ===
%% We test arguments <arg> on emptiness by 
%% |\MF@if@empty{<arg>}{<yes>}{<no>}|:
\newcommand*{\MF@if@empty}[1]{%
    \ifx\MF@store@field@bg#1\MF@store@field@bg
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi}
%% |\MF@field| stores the name space for filling jobs:
\newcommand*{\MF@field}{MF@field:}
%%
%% === Field Declaration ===
%% \label{sec:field}
%% |\MF@make@bg| essentially builds a list of as many filler elements 
%% as the template has characters, using a loop macro `\MF@make@bg'. 
%% The current list of filler elements is delimited by `\@bg'.
\def\MF@make@bg#1#2#3\MF@store@field@bg{%
    \MF@if@empty{#3}%
%% First case: `#2' is the last template element. 
%% We run `\MF@store@field@bg' with an additional filler 
%% element:\footnote{Another run of `&\MF@make@bg fails' ...}
                {\MF@store@field@bg#1}%
%% Second case: the filler list gets an additional element, 
%% and the loop repeats:
                {\MF@make@bg#1#3\MF@store@field@bg#1}%
}
%% |\MF@store@field@bg<background>\@bg{<field>}|
%% essentially stores the filler list (``<background>"), 
%% or more precisely ...
\def\MF@store@field@bg#1\@bg#2{%
%% Here is the \strong{only assignment} when the macros run: %% \strong 2012/03/29
%% a command \[`\MF@field:<field>{<text>}'\] 
%% is defined.\footnote{This is the common, confusing way 
%%                      to describe such situations. 
%%                      Actually, the definition assigns a 
%%                      macro meaning to a ``named token" 
%%                      whose name is ```MF@field:<field>'". 
%%                      \emph{Typing} `&\MF@field:<field>' won't work.}
    \@namedef{\MF@field#2}##1{%
        \MF@reduce@bg##1\rest@t#1\rest@f{##1}{#2}}}
%%
%% === Checking Field   ===
%% \label{sec:text}
%% |\MF@check@field{<align>}{<text>}{<field>}|
%% runs |\MF@field:<field>{<text>}| from above, 
%% provided the latter has been defined (by `\MFfieldtemplate').
%% The <align> command is appended.
\newcommand*{\MF@check@field}[3]{%
    \@ifundefined{\MF@field#3}%
%                  {\PackageError{field "#3" not defined}%
%                                {use \string\MFfieldtemplate}}%
%% With v0.1, I thought about errors and warnings properly only 
%% more below ...
                 {\MF@field@undeclared{#2}{#3}}%
                 {\csname\MF@field#3\endcsname{#2}#1}}
%% |\MF@field@undeclared{<text>}{<field>}| just outputs <text>. 
\newcommand*{\MF@field@undeclared}[2]{#1}
%% A proper message is problematic in \strong{pure expansion}
%% as on screen or in \file{.log} files. 
%% Package option |fake-undefined| (Sec.~\ref{sec:opt}) 
%% offers another ``cheap" solution.
%% (TODO)
%% 
%% === Trying Alignment ===
%% |\MF@reduce@bg<r-text>\rest@t<r-fill>\rest@f{<text>}{<field>}<align>| 
%% \\[\smallskipamount]
%% is invoked by that `\MF@field:<field>' that 
%% `\MF@store@field@bg' defines as above.
%% It takes away one element both from the (remaining) <text> 
%% (delimited by `\rest@t') and the filler list (delimited by `\rest@f'). 
%% The full <text> has been stored ahead.
\def\MF@reduce@bg#1#2\rest@t#3#4\rest@f{%
    \MF@if@empty{#2}%
        {\MF@if@empty{#4}%
%% When we have removed the last elements of both lists at the same time, 
%% we just return <text>:
                     \@firstofthree
%% When we have removed the last element of <text>, and there still is
%% a filler element, we perform the alignment:
                     {\MF@fine@align{#4}}}%
        {\MF@if@empty{#4}%
%% When we have removed the last filler element, and a <text> element 
%% is still present, we return <text>, maybe together with a warning:
                     \MF@bad@align
%% When neither #1 nor #3 have been the last elements in their lists, 
%% we run `\MF@reduce@bg' on the remaining lists:
                     {\MF@reduce@bg#2\rest@t#4\rest@f}}}
%% |\@firstofthree{<use>}{<skip>}{<skip>}| may be known or not ...
\long\def\@firstofthree#1#2#3{#1}
%% |\MF@fine@align{<filler>}{<text>}{<field>}<align>| ...
\newcommand*{\MF@fine@align}[4]{\if r#4#1#2\else#2#1\fi}
%% |\MF@bad@align{<text>}{<field>}{<align>}|\\[\smallskipamount]
%% % gobbles <align>,
%% % throws a message about <text> and <field>, and outputs <text>.
%% % If this happens at \strong{pure expansion} such as writing 
%% % to \emph{screen} or a \emph{plain text file}, something really 
%% % awful may happen ... (we may adjust `\MF@bad@align' better 
%% % for this case, another time ... TODO)
%% at present is similar to `\@firstofthree'. 
%% In a future package version, we may add some warning or so 
%% for cases where it is useful---while it is not useful to write 
%% \emph{code} for warnings to screen and \file{.log} 
%% (the originally intended use of the package).
%% We offer a ``cheap" possibility of throwing some error by 
%% package option |fake-undefined|---see Sec.~\ref{sec:opt}
\newcommand*{\MF@bad@align}[3]{#1}
%% Actually, in v0.1 |\MF@check@field| appends the <field> 
%% argument hoping it could be used in a warning.
%% 
%% == Package Option     ==
%% \label{sec:opt}
%% With applications like `\listfiles', it may be useful to get an 
%% ``undefined" error where the name of the undefined command 
%% is a kind of ``secret message" ...
\DeclareOption{fake-undefined}{%
%% #1 is <text> and will be output, #2 is <field>, cf.~above.
    \def\MF@field@undeclared#1#2{#1\monofillFieldUndeclared}
    \def\MF@bad@align#1#2#3{#1\monofillFieldTooSmall}}
\ProcessOptions
%% == \cs{endinput} and Version HISTORY == 
\endinput
%%
%% VERSION HISTORY

v0.1   2012/03/18   started
       2012/03/19   completed 
v0.1a  2012/03/29   doc.: \medbreak (fix); \strong
v0.2   2012/10/29   \MFotherspace; doc. slightly reformatted