%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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/11/13]

% \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

%% counters
\newcounter{ez@totalcount}
\newcounter{ez@tempcounter}

% get page number for label (defined in .aux with \newlabel)
%     The \newlabel command defines ~\r@label to give a set of tokens. The second of which is the page number
% Defining this avoids the issue of \pageref being overwritten (e.g., by hyperref)
% This macro was adapted from lines in hyperref
\def\ez@pageref#1{%
    \expandafter\ifx\csname r@#1\endcsname\relax%
        ?% ref not defined
    \else%
        \expandafter\expandafter\expandafter% evaluate "car..."
        \expandafter\expandafter\expandafter% evaluate "gobble<...>"
        \expandafter\@car% exapndafter holds "car" -> "\@car\@gobble<...>"
        \expandafter\expandafter\expandafter% expand "csname...", put after "gobble"
        \@gobble\csname r@#1\endcsname\@nil%
    \fi%
}

%% Commands for recording edit counts
\gdef\ez@pagelist{}
\def\ez@geteditcount#1{%
    \expandafter\ifx\csname ez@count@page#1\endcsname\relax%
        % no edits on page
        0%
    \else%
        \csname ez@count@page#1\endcsname%
    \fi%
}
\def\ez@addeditcount#1{%
    \expandafter\ifx\csname ez@count@page#1\endcsname\relax%
        % Initialize
        \expandafter\gdef\csname ez@count@page#1\endcsname{0}%
        % Add to (comma-separated) list
        \xdef\ez@pagelist{\ez@pagelist\ifx\ez@pagelist\empty\else,\fi#1}
%        \xdef\ez@pagelist{\ez@pagelist#1,}
    \fi%
    % Add to count
    % Note stepping needs to be "protected" (counters do not always like being changed, since this can mess up, e.g., TOC)
    \protect\setcounter{ez@tempcounter}{\ez@geteditcount{#1}}%
    \protect\stepcounter{ez@tempcounter}%
    \expandafter\xdef\csname ez@count@page#1\endcsname{\number\value{ez@tempcounter}}%
}

%% edit record
\def\ez@addeditpage{%
	\def\ez@label{ezedit\theez@totalcount}% name of label
	\label{\ez@label}% insert label
	\ez@addeditcount{\ez@pageref{\ez@label}}% add to record for crnt page
    % Note stepping needs to be "protected" (counters do not always like being changed, since this can mess up, e.g., TOC)
    \protect\stepcounter{ez@totalcount}% update counter
}

\newcommand{\makeeditreport}{%
    \def\@run##1{\expandafter\@process##1,\@nil,}%
    \def\@process##1,{%
        \ifx##1\@nil% terminate
        \else%
            ##1 & \csname ez@count@page##1\endcsname \\
        \expandafter\@process\fi% end if, then process next token
    }%
    %
    \begin{tabular}{l|r}
        Page & \# edits/notes \\
        \hline
	    \@run\ez@pagelist
	    \hline
	    Total & \arabic{ez@totalcount}
	 \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