% \iffalse meta-comment
%
% Copyright (C) 2005-8 by Ulrich M. Schwarz
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3a.
% The license can be obtained from
% http://www.latex-project.org/lppl/lppl-1-3a.txt
%
% \fi
%
%\iffalse
%<*code>
%\fi
%    Two macros are provided: |\setuniqmark| takes a single parameter,
%    the name, which should be a string of letters. |\ifuniqmark| takes
%    three parameters: a name, a true-part and a false-part. The true
%    part is executed if and only if there was exactly one call to
%    |\setuniqmark| with the given name during the previous \LaTeX\ run.
%
%    Example application: legal documents are often very strongly numbered.
%    However, if a section has only a single paragraph, this paragraph is
%    not numbered separately, this only occurs from two paragraphs onwards.
%
%    It's also possible to not-number the single theorem in your paper, but
%    fall back to numbering when you add another one.
%
%     \StopEventually{}
%
%    \begin{macrocode}

\DeclareOption{unq}{%
  \newwrite\uniq@channel
  \InputIfFileExists{\jobname.unq}{}{}%
  \immediate\openout\uniq@channel=\jobname.unq
  \AtEndDocument{%
    \immediate\closeout\uniq@channel%
  }
}
\DeclareOption{aux}{%
  \let\uniq@channel\@auxout
}

%    \end{macrocode}
%
%
%    \iffalse $Id: unique.dtx,v 1.4 2010/04/25 19:37:46 ulmi Exp $\fi
%    \begin{macro}{\setuniqmark}
%    Call this with a name to set the corresponding uniqmark. The name must
%    be suitable for |\csname|-constructs, i.e. fully expansible to a
%    string of characters. If you use some counter values to generate this,
%    it might be a good idea to try and use hyperref's |\theH...| macros,
%    which have similar restrictions. You can check whether a particular
%    |\setuniqmark| was called more than once during \emph{the last run}
%    with |\ifuniq|.
%    \begin{macrocode}
\newcommand\setuniqmark[1]{%
  \expandafter\ifx\csname uniq@now@#1\endcsname\relax
  \global\@namedef{uniq@now@#1}{\uniq@ONE}%
  \else
  \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY\else
  \immediate\write\uniq@channel{%
    \string\uniq@setmany{#1}%
  }%
  \ifuniq{#1}{%
    \uniq@warnnotunique{#1}%
  }{}%
  \fi
  \global\@namedef{uniq@now@#1}{\uniq@MANY}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    
%    \begin{macro}{\ifuniq}
%    Companion to |\setuniqmark|: if the uniqmark given in the first
%    argument was called more than once, execute the second argument,
%    otherwise execute the first argument. Note than no call to
%    |\setuniqmark| for a particular uniqmark at all means that this
%    uniqmark is unique.
%    
%    This is a lazy version: we could always say false if we already had two calls
%    to setuniqmark this run, but we have to rerun for any ifuniq prior
%    to the first setuniqmark anyway, so why bother?
%    \begin{macrocode}
\newcommand\ifuniq[1]{%
  \expandafter\ifx\csname uniq@last@#1\endcsname\uniq@MANY
  \expandafter \@secondoftwo
  \else
  \expandafter\@firstoftwo
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    
%    Two quarks to signal if we have seen an uniqmark more than once.
%    \begin{macrocode}
\def\uniq@ONE{\uniq@ONE}
\def\uniq@MANY{\uniq@MANY}
%    \end{macrocode}
%    Flag: suggest a rerun?
%    \begin{macrocode}
\newif\if@uniq@rerun
%    \end{macrocode}
%    
%    Helper macro: a call to this is written to the .aux file when we see
%    an uniqmark for the second time. This sets the right information for
%    the next run. It also checks on subsequent runs if the number of
%    uniqmarks drops to less than two, so that we'll need a rerun.
%    \begin{macrocode}
\def\uniq@setmany#1{%
  \global\@namedef{uniq@last@#1}{\uniq@MANY}%
  \AtEndDocument{%
    \uniq@warnifunique{#1}%
  }%
}
%    \end{macrocode}
%    
%    Warning if something is unique now. This always warns if the
%    setting for this run is not ``many'', because it was generated
%    by a setmany from the last run.
%    \begin{macrocode}
\def\uniq@warnifunique#1{%
  \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY\else
  \PackageWarningNoLine{uniq}{%
    `#1' is unique now.\MessageBreak
    Rerun LaTeX to pick up the change%
  }%
  \@uniq@reruntrue
  \fi
}
%    \end{macrocode}
%    
%    Warning if we have a second uniqmark this run around. Since this is
%    checked immediately, we could give the line of the second
%    occurence, but we do not do so for symmetry.
%    \begin{macrocode}
\def\uniq@warnnotunique#1{%
  \PackageWarningNoLine{uniq}{%
    `#1' is not unique anymore.\MessageBreak
    Rerun LaTeX to pick up the change%
  }%
  \@uniq@reruntrue
}
%    \end{macrocode}
%    
%    Maybe advise a rerun (duh!). This is executed at the end of the
%    second reading of the aux-file. If you manage to set uniqmarks
%    after that (though I cannot imagine why), you might need reruns
%    without being warned, so don't to that.
%    \begin{macrocode}
\def\uniq@maybesuggestrerun{%
  \if@uniq@rerun
  \PackageWarningNoLine{uniq}{%
    Uniquenesses have changed. \MessageBreak
    Rerun LaTeX to pick up the change%
  }%
  \fi
}
%    \end{macrocode}
%    
%    Make sure the check for rerun is pretty late in processing, so it
%    can catch all of the uniqmarks (hopefully).
%    \begin{macrocode}
\AtEndDocument{%
  \immediate\write\@auxout{\string\uniq@maybesuggestrerun}%
}
\ExecuteOptions{aux}
\ProcessOptions\relax
%    \end{macrocode}
%\iffalse
%</code>
%\fi
