\NeedsTeXFormat{LaTeX2e}[2.09]
\ProvidesPackage{authcomments}[2013/10/03 support for authored PDF comments]

%% Usage: \usepackage[options]{authcomments}
%%
%%  \newcommenter{commentername}{RGB color}
%%  ...
%%  \commentername{comment}
%%
%% Example:
%%
%%  ...
%%  \usepackage[checkNoComments]{authcomments}
%%  \newcommenter{ACM}{1,0.7,0}
%%  \newcommenter{Bob}{1,0.7,0}
%%  \begin{document}
%%    Text. \ACM{This is a useful comment}\Bobreply{I disagree}
%%  \end{document}
%%
%% Options:
%%
%%   [disabled] : turn off all comments
%%   [inline] : make comments appear inline
%%   [checkNoComments] : turn off comments if the file .nocomments exists
%%
%% Written by Andrew Myers, July 2013
%% Reply functionality added by Chin Isradisaikul, Feb 2015

\DeclareOption{disabled}{\def\noeditingmarks{}\typeout{** authcomments disabled}}
\DeclareOption{inline}{\def\inlinecomments{}}
\DeclareOption{checkNoComments}{\def\checknocomments{}}
\DeclareOption*{\PackageWarning{authcomments}{Unknown option \CurrentOption}}
\ProcessOptions

% PDF comments don't appear in quite the right place on a lot of viewers. Overriding
% these allows changing the positioning.
\def\authcomments@hoffset{-5pt}
\def\authcomments@voffset{5pt}
\def\authcomments@hoffsetnum{-5}

\def\authcomments@replyoffset{10} %pt

\ifx\checknocomments\undefined
    \relax
\else
    \IfFileExists{.nocomments}{\def\noeditingmarks{}}{}
\fi

\newcommand\@auth@allowspecials{\@makeother"\@makeother_\@makeother$}

\ifx\noeditingmarks\undefined
   \usepackage{xcolor}
   \ifx\inlinecomments\undefined
     \usepackage[opacity=0.75,hoffset=\authcomments@hoffset,voffset=\authcomments@voffset]{pdfcomment}
     \newcount\replies

     \newcommand\@authcomment[3]{\global\replies \authcomments@hoffsetnum\relax\pdfcomment[author=#1,color=#2]{#1: #3}\egroup}
     \newcommand\@authreply[3]{\global\advance\replies \authcomments@replyoffset\relax\pdfcomment[author=#1,color=#2,hoffset=\replies pt,voffset=-\replies pt]{#1: #3}\egroup}

     \def\newcommenter#1#2{
        \definecolor{#1@authcomment@color}{rgb}{#2}
        \expandafter\def\csname #1\endcsname{\bgroup\@auth@allowspecials\@authcomment{#1}{#1@authcomment@color}}
        \expandafter\def\csname #1reply\endcsname{\bgroup\@auth@allowspecials\@authreply{#1}{#1@authcomment@color}}
     }
   \else
     \newcommand\@authcomment[3]{\textcolor{#2}{[#1: #3]}\egroup}
     \def\newcommenter#1#2{
        \definecolor{#1@authcomment@color}{rgb}{#2}
        \colorlet{#1@authcomment@shade}{#1@authcomment@color!60!black}
        \expandafter\def\csname #1\endcsname{\bgroup\@auth@allowspecials\@authcomment{#1}{#1@authcomment@shade}}
     }
   \fi
     
\else
   \newcommand\newcommenter[2]{
        \expandafter\def\csname#1\endcsname##1{\ignorespaces}
        \expandafter\def\csname#1reply\endcsname##1{\ignorespaces}
    }
\fi
