%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ezedits.sty
%% Copyright 2024 J. A. Smiga
%
% 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 J. A. Smiga.
%
% This work consists of the file ezedits.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% This package was written by Joseph A. Smiga <joseph.smiga@rochester.edu>
%% Originally developed ca. 2018, this has been refined over the years.
%%
%% Some useful commands: (not up to date, see documentation)
%% Included are a set of commands to generate comments for a LaTeX file.
%%
%% This package requires the following standard packages: 
%%     ulem: for \sout
%%     xcolor: for coloring text.
%%     pdfcomment: if pdfcomment option is used
%%
%% Options are:
%%     arrows: add arrows pointing to edits in margins (in captions, a pair of arrows are added, instead).
%%     accept: accept all edits (arrows still appear).
%%     pdfcomments: write edits as pdf annotations (arrows still appear).


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ezedits}[2024/09/12]

% \RequirePackage{xcolor}

\newif\if@accept\@acceptfalse % accept all comments (does not suppress package warnings)
\newif\if@pdfcomm\@pdfcommfalse % write pdf comments
\newif\if@showarrows\@showarrowsfalse
\newif\if@importulem\@importulemtrue

\DeclareOption{accept}{
    \@accepttrue
}
\DeclareOption{pdfcomment}{ % add pdf comments
    \@pdfcommtrue
    \AtEndOfPackage{\RequirePackage{pdfcomment}}
}
\DeclareOption{arrows}{ % point to edits with arrows in the margin
    \@showarrowstrue
    \AtEndOfPackage{\RequirePackage{marginnote}}
}
\DeclareOption{nosout}{ % do not crossout deleted text
    \@importulemfalse
}

\ProcessOptions\relax

\if@importulem
    \RequirePackage[normalem]{ulem}% import ulem (for sout)
    % update \sout to work better with \cite
    \let\old@cite\cite% remember original
    \def\new@cite#1{\mbox{\old@cite{#1}}}% put cite in mbox
    \def\new@sout#1{\let\cite\new@cite\sout{#1}\let\cite\old@cite}% redefine sout 
\else
    \def\new@sout{}% do nothing
\fi
    
%% edit record
\newcounter{ez@editcount}
\newcounter{ez@totalcount}
\gdef\ez@editrecord{}
\newcommand{\ez@addeditpage}{%
    \ifx\ez@lastpage\undefined% first edit
        \xdef\ez@lastpage{\thepage}% initialize "last page" variable
        % counter starts at 0, so it does not need to be updated
    \else% 2nd+ edit
        \if\ez@lastpage\thepage% same page as last edit
            \relax% handled after if
        \else% new page
            % add record
            \xdef\ez@editrecord{\ez@editrecord \ez@lastpage & \arabic{ez@editcount} \\}%
            % reset counter/page number
            \setcounter{ez@editcount}{0}%
            \xdef\ez@lastpage{\thepage}%
        \fi%
    \fi%
    % Note stepping needs to be "protected" (counters do not always like being changed, since this can mess up, e.g., TOC)
    \protect\stepcounter{ez@editcount}% update counter
    \protect\stepcounter{ez@totalcount}%
}
%TODO there is an issue with \thepage not always being accurate
%   Possible solution: use \label{} (with generated unique labels) and \pageref{} instead of \thepage.
%   -> This solution may cause out-of-order problems with floating images.

\newcommand{\makeeditreport}{%
    \begin{tabular}{l|r}
        %\hline\hline
        Page & \# edits/notes \\
        \hline
        \ifx\ez@lastpage\undefined
            \relax% no edits
        \else%
            \ez@editrecord
            \ez@lastpage & \arabic{ez@editcount}% current state
        \fi \\
        \hline
        Total & \arabic{ez@totalcount}
        %\hline\hline
    \end{tabular}
}

% Style of \draftnote (may be a better way to write this)
\newcommand{\@draftstyle}[1]{
    {\ttfamily%
    \ifx\textcolor\undefined%
        #1%
    \else%
        \textcolor{red}{#1}%
    \fi}%
}
% let user change the style
\newcommand{\setnotestyle}[1]{\let\@draftstyle#1}

%% Editing commands 
\newcommand{\draftnote}[1]{%
    \PackageWarning{ezedits}{Unresolved note: #1}%
    \ez@addeditpage{}% update record
    \if@showarrows%
        \@ifundefined{@captype}%
            {\marginnote{\@draftstyle{$\Longleftarrow$}}}%
            {\@draftstyle{$\Longrightarrow$}}%
    \fi%
    \if@accept{}\relax\else%
        \if@pdfcomm%
            \pdfmargincomment{#1}%
        \else%
            \@draftstyle{[#1]}%
        \fi%
    \fi%
    \if@showarrows% if in caption, draw end arrow
        \@ifundefined{@captype}{}{\@draftstyle{$\Longleftarrow$}}%
    \fi%
}

\newcommand{\defineEdit}[3]{%
    % "\###Edit" for removing and adding text
    \expandafter\newcommand\csname #1Edit\endcsname[2]{%
        \PackageWarning{ezedits}{Unresolved modifications by #1: "##1" ---> "##2"}%
        \ez@addeditpage{}% update record
        \if@showarrows%
            \@ifundefined{@captype}%
                {\marginnote{{#3{$\Longleftarrow$}}}}%
                {{#3{$\Longrightarrow$}}}%
        \fi%
        \if@accept% Display as if accepted
            ##2%
        \else%
            \if@pdfcomm%
                \pdfmarkupcomment[author=#1,markup=StrikeOut]{##1{$_\wedge$}}{##2}%
            \else%
                {#2{\new@sout{##1}}}{#3{##2}}%
            \fi%
        \fi%
        \if@showarrows% if in caption, draw end arrow
            \@ifundefined{@captype}{}{{#3{$\Longleftarrow$}}}%
        \fi%
    }%
    % "\###Ins" for adding text
    \expandafter\newcommand\csname #1Ins\endcsname[1]{%
        \ez@addeditpage{}% update record
        \PackageWarning{ezedits}{Unresolved modifications by #1: "##1" inserted.}%
        \if@showarrows%
            \@ifundefined{@captype}%
                {\marginnote{{#3{$\Longleftarrow$}}}}%
                {{#3{$\Longrightarrow$}}}%
        \fi%
        \if@accept% Display as if accepted
            ##1%
        \else%
            \if@pdfcomm%
                % \pdfcomment[author=#1,icon=Insert]{##1}%
                \pdfmarkupcomment[author=#1,markup=Underline]{$_\wedge$}{##1}%
            \else%
                {#3{##1}}%
            \fi%
        \fi%
        \if@showarrows% if in caption, draw end arrow
            \@ifundefined{@captype}{}{{#3{$\Longleftarrow$}}}%
        \fi%
    }%
    % "\###Del" for removing text
    \expandafter\newcommand\csname #1Del\endcsname[1]{%
        \PackageWarning{ezedits}{Unresolved modifications by #1: "##1" removed.}%
        \ez@addeditpage{}% update record
        \if@showarrows%
            \@ifundefined{@captype}%
                {\marginnote{{#3{$\Longleftarrow$}}}}%
                {{#3{$\Longrightarrow$}}}%
        \fi%
        \if@accept% Display as if accepted
            {}%
        \else%
            \if@pdfcomm%
                \pdfmarkupcomment[author=#1,markup=StrikeOut,color=red]{##1}{}%
            \else%
                {#2{\new@sout{##1}}}%
            \fi%
        \fi%
        \if@showarrows% if in caption, draw end arrow
            \@ifundefined{@captype}{}{{#3{$\Longleftarrow$}}}%
        \fi%
    }%
}

\endinput