
\NeedsTeXFormat{LaTeX2e}[1999/01/01]
\ProvidesPackage{review}[2004/02/17]
\RequirePackage{color,keyval,twoopt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright 2004-2011 Gabriel Moreau                                     %
%                                                                        %
% This work may be distributed and/or modified under the                 %
% conditions of the LaTeX Project Public License, either version 1.3     %
% of this license or (at your option) any later version.                 %
% The latest version of this license is in                               %
%   http://www.latex-project.org/lppl.txt                                %
% and version 1.3 or later is part of all distributions of LaTeX         %
% version 2005/12/01 or later.                                           %
%                                                                        %
% This work has the LPPL maintenance status `maintained'.                %
%                                                                        %
% The Current Maintainer of this work is G. Moreau.                      %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pour gérer les deux types d'options :                                  %
%  1. les options normales (\DeclareOption{...}{...})                    %
%  2. les options type key-val option=value (\define@key{...}{...}{...}) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\@ifundefined{rev@unusedlist}{%
  \let\rev@unusedlist\@empty}{}
\@ifundefined{rev@extractkey}{%
  \def\rev@extractkey#1=#2\rev@extractkey{#1}}{}
\newcommand{\ProcessUnusedOptions}[1]{%
  \let\my@tempa\@empty
  \let\my@tempb\@empty
  \@for\CurrentOption:=\rev@unusedlist\do{%
    \expandafter\expandafter\expandafter\def
    \expandafter\expandafter\expandafter\my@key
    \expandafter\expandafter\expandafter{%
      \expandafter\rev@extractkey\CurrentOption=\rev@extractkey}%
    \@ifundefined{KV@#1@\my@key}{%
      \edef\my@tempa{\my@tempa,\CurrentOption,}}{%
      \edef\my@tempb{\my@tempb,\CurrentOption,}}}%
  \@for\CurrentOption:=\my@tempa\do{%
    \ifx\CurrentOption\@empty\else
      \PackageWarning{review}{`\CurrentOption' not defined.}\fi}%
  \edef\my@tempb{%
    \noexpand\setkeys{#1}{\my@tempb}}%
  \my@tempb
  \AtEndOfPackage{\let\@unprocessedoptions\relax}}

\DeclareOption*{%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\rev@unusedlist
  \expandafter\expandafter\expandafter{%
    \expandafter\rev@unusedlist\expandafter,%
    \CurrentOption}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initialisation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\if@rev@see \@rev@seetrue
\newlength{\rev@len}
\setlength{\rev@len}{\marginparwidth}
\addtolength{\rev@len}{-0.5cm}
\newcounter{rev@counter}
\definecolor{rev@colortext}{rgb}{1,0,0}
\definecolor{rev@colormark}{rgb}{0,0,1}

\def\rev@contextwait{wait}
\def\rev@contextreject{reject}
\def\rev@contextaccept{accept}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% options
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Options «normales»
\DeclareOption{draft}{\@rev@seetrue}
\DeclareOption{see}{\@rev@seetrue}
\DeclareOption{hide}{\@rev@seefalse}
\DeclareOption{final}{\@rev@seefalse}

%% Options passées sous forme key=val
\define@key{review}{width}{\setlength{\rev@len}{#1}}
\define@key{review}{counter}{\setcounter{rev@counter}{#1}}
%\define@key{review}{colortext}{\definecolor{rev@colortext}{rgb}{#1}}
%\define@key{review}{colormark}{\definecolor{rev@colormark}{rgb}{#1}}
%\define@key{mypack}{<key2>}[<default>]{<action 2> dépendant de #1}

\ProcessOptions
\ProcessUnusedOptions{review}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% internal command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% #1 command
\newcommand{\rev@error}[1]{%
   \PackageError{review}{option for #1 are 'accept' 'reject' or 'wait'}%
   }

% #1 author
% #2 type
% #3 other thing (with \\\hline please)
\newcommand{\rev@notewait}[3]{%
   \marginpar{%
   \begin{tabular}{|p{\rev@len}|}\hline%
   \texttt{\textcolor{#1}{\textcolor{rev@colormark}{\therev@counter{#2}}-#1}}\\\hline%
   #3%
   \end{tabular}%
   }}

% #1 author
% #2 type
\newcommand{\rev@noteaccept}[2]{%
   \marginpar{%
   $\left<\texttt{\textcolor{#1}{\textcolor{rev@colormark}{\therev@counter{#2}}-#1}}\right>$%
   }}

% #1 author
% #2 type
\newcommand{\rev@notereject}[2]{%
   \marginpar{%
   $\left/\texttt{\textcolor{#1}{\textcolor{rev@colormark}{\therev@counter{#2}}-#1}}\right/$%
   }}

% #1 author
% #2 type
\newcommand{\rev@notemark}[2]{%
   \textcolor{#1}{$^{\left<\texttt{\textcolor{rev@colormark}{\therev@counter{#2}}}\right>}$}%
   }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% external command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% #1 author
% #2 color
\newcommand{\revauthor}[2]{\definecolor{#1}{rgb}{#2}}

% #1 color
\newcommand{\revcolortext}[1]{\definecolor{rev@colortext}{rgb}{#1}}

% #1 color
\newcommand{\revcolormark}[1]{\definecolor{rev@colormark}{rgb}{#1}}


% [#1] context
% [#2] null
%  #3  author
%  #4  add
%  #5  com
\newcommandtwoopt{\revadd}[5][wait][null]{%
   \def\rev@contextlocal{#1}%
   \stepcounter{rev@counter}%
   \if@rev@see%
      \ifx\rev@contextlocal\rev@contextwait%
         \textcolor{rev@colortext}{#4}%
         \rev@notemark{#3}{a}%
         \rev@notewait{#3}{a}{#5\\\hline}%
      \else\ifx\rev@contextlocal\rev@contextaccept%
         \textcolor{rev@colortext}{#4}%
         \rev@noteaccept{#3}{a}%
      \else\ifx\rev@contextlocal\rev@contextreject%
         \rev@notereject{#3}{a}%
      \else%
         \rev@error{revadd}%
      \fi\fi\fi%
   \else%
      \ifx\rev@contextlocal\rev@contextaccept%
         #4%
      \fi%
   \fi%
   }

% [#1] context
% [#2] null
%  #3  author
%  #4  old
%  #5  new
%  #6  com
\newcommandtwoopt{\revmod}[6][wait][null]{%
   \def\rev@contextlocal{#1}%
   \stepcounter{rev@counter}%
   \if@rev@see%
      \ifx\rev@contextlocal\rev@contextwait%
         \textcolor{rev@colortext}{#5}%
         \rev@notemark{#3}{c}%
         \rev@notewait{#3}{c}{#6\\\hline#4\\\hline}%
      \else\ifx\rev@contextlocal\rev@contextaccept%
         \textcolor{rev@colortext}{#5}%
         \rev@noteaccept{#3}{c}%
      \else\ifx\rev@contextlocal\rev@contextreject%
         #4%
         \rev@notereject{#3}{c}%
      \else%
         \rev@error{revmod}%
      \fi\fi\fi%
   \else%
      \ifx\rev@contextlocal\rev@contextaccept%
         #5%
      \else%
         #4%
      \fi%
   \fi%
   }

% [#1] context
% [#2] null
%  #3  author
%  #4  old
%  #5  new
%  #6  com
\newcommandtwoopt{\revforget}[6][wait][null]{%
   \def\rev@contextlocal{#1}%
   \stepcounter{rev@counter}%
   \if@rev@see%
      \ifx\rev@contextlocal\rev@contextwait%
         \textcolor{rev@colortext}{#5}%
         \rev@notemark{#3}{f}%
         \rev@notewait{#3}{f}{#6\\\hline}%
      \else\ifx\rev@contextlocal\rev@contextaccept%
         \textcolor{rev@colortext}{#5}%
         \rev@noteaccept{#3}{f}%
      \else\ifx\rev@contextlocal\rev@contextreject%
         #4%
      \else%
         \rev@error{revforget}%
      \fi\fi\fi%
   \else%
      \ifx\rev@contextlocal\rev@contextaccept%
         #5%
      \else%
         #4%
      \fi%
   \fi%
   }

% [#1] context
% [#2] null
%  #3  author
%  #4  del
%  #5  com
\newcommandtwoopt{\revdel}[5][wait][null]{%
\def\rev@contextlocal{#1}%
\stepcounter{rev@counter}%
\if@rev@see%
   \ifx\rev@contextlocal\rev@contextwait%
      \rev@notemark{#3}{d}%
      \rev@notewait{#3}{d}{#5\\\hline#4\\\hline}%
   \else\ifx\rev@contextlocal\rev@contextaccept%
      \rev@noteaccept{#3}{d}%
   \else\ifx\rev@contextlocal\rev@contextreject%
      #4%
   \else%
      \rev@error{revdel}%
   \fi\fi\fi%
\else%
   \ifx\rev@contextlocal\rev@contextaccept%
   \else%
      #4%
   \fi%
\fi%
   }

% [#1] context
% [#2] null
%  #3  author
%  #4  com
\newcommandtwoopt{\revmark}[4][wait][null]{%
   \def\rev@contextlocal{#1}%
   \stepcounter{rev@counter}%
   \if@rev@see%
      \ifx\rev@contextlocal\rev@contextwait%
         \rev@notemark{#3}{m}%
         \rev@notewait{#3}{m}{\footnotesize#4\\\hline}%
      \else\ifx\rev@contextlocal\rev@contextaccept%
         \textcolor{rev@colortext}{#4}%
         \rev@noteaccept{#3}{m}%
      \else\ifx\rev@contextlocal\rev@contextreject%
      \else%
         \rev@error{revmark}%
      \fi\fi\fi%
   \fi%
   }

% [#1] context
% [#2] null
%  #3  author
%  #4  com
\newcommandtwoopt{\revpar}[4][wait][null]{%
   \def\rev@contextlocal{#1}%
   \stepcounter{rev@counter}%
   \if@rev@see%
      \ifx\rev@contextlocal\rev@contextwait%
%         \rev@notewait{#3}{p}{}%
%         \textcolor{#3}{$\left<\texttt{\textcolor{rev@colormark}{\therev@counter{p}}}\right.-$}%
%         \textcolor{rev@colortext}{#4}%
%         \textcolor{#3}{$-\left.\texttt{\textcolor{rev@colormark}{\therev@counter{p}}}\right>$}%
%
%
         \begin{center}%
	    \rev@notewait{#3}{p}{}%
	    \begin{tabular}{|p{0.95\linewidth}|}\hline%
               \texttt{\textcolor{#3}{\textcolor{rev@colormark}{\therev@counter{p}}-#3}}\\\hline%
               #4\\\hline%
            \end{tabular}%
	 \end{center}%
%   
      \else\ifx\rev@contextlocal\rev@contextaccept%
         \textcolor{rev@colortext}{#4}%
         \rev@noteaccept{#3}{p}%
      \else\ifx\rev@contextlocal\rev@contextreject%
      \else%
         \rev@error{revpar}%
      \fi\fi\fi%
   \fi%
   }

\endinput
