% --------------------------------------------------------------------------
% the SNOTEZ package
% 
%   Sidenotes for LaTeX2e
% 
% --------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/enotez/
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2012--2013 Clemens Niederberger
% 
% 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 Clemens Niederberger.
% --------------------------------------------------------------------------
% The snotez package consists of the files
%  - snotez.sty, snotez_en.tex, snotez_en.pdf, README
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{snotez}[2013/04/03 v0.2 sidenote support for LaTeX 2e (CN)]

\RequirePackage{etoolbox,pgfopts,marginnote,perpage}

% --------------------------------------------------------------------------
% package options:
\newbool{snotez@marginnote}
\newbool{snotez@perpage}
\newbool{snotez@footnote}

\def\snotez@note@mark@sep{\space}
\def\snotez@note@mark@format#1{\@textsuperscript{\normalfont#1}}
\def\snotez@format{\footnotesize}
\def\snotez@text@mark@format#1{\@textsuperscript{\normalfont#1}}

\pgfkeys{
  snotez/.cd,
    % all sidenotes use \marginnote:
    marginnote/.is if       = snotez@marginnote ,
    text-format/.code       = \def\snotez@format{#1} ,
    text-format+/.code      =
      \expandafter\def\expandafter\snotez@format\expandafter{\snotez@format#1} ,
    % make sidenotes counted per page:
    perpage/.is if          = snotez@perpage ,
    note-mark-sep/.code     = \def\snotez@note@mark@sep{#1} ,
    note-mark-format/.code  = \def\snotez@note@mark@format##1{#1} ,
    text-mark-format/.code  = \def\snotez@text@mark@format##1{#1} ,
    % make all footnotes sidenotes:
    footnote/.is if         = snotez@footnote
}

\ProcessPgfOptions*

\newrobustcmd\setsidenotes[1]{\pgfqkeys{/snotez}{#1}}

% --------------------------------------------------------------------------
% the \sidenote command
% syntax:
% - \sidenote{text}
% - \sidenote[mark]{text}
% - \sidenote[offset][mark]{text}
\newrobustcmd*\sidenote{%
  \@ifnextchar[%
    {\snotez@sidenote@aux@i}
    {\snotez@sidenote@aux@ii{}[]}}% no option

\def\snotez@sidenote@aux@i[#1]{%
  \@ifnextchar[%
    {\snotez@sidenote@aux@ii{#1}}% two options
    {\snotez@sidenote@aux@ii{}[#1]}}% one option

% #1: offset
% #2: mark
% #3: text
\long\def\snotez@sidenote@aux@ii#1[#2]#3{%
  \snotez@mark{#2}%
  \snotez@text{#1}{#3}}

% this one is very much \@footnotemark from latex.ltx:
\def\snotez@write@mark#1{%
  \leavevmode
  \ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
  #1%
  \ifhmode\spacefactor\@x@sf\fi
  \relax}

\def\snotez@mark#1{%
  \ifblank{#1}% or \ifstrempty?
    {\refstepcounter{sidenote}\protected@xdef\@the@snotez@mark{\thesidenote}}
    {\protected@edef\@currentlabel{#1}\protected@xdef\@the@snotez@mark{#1}}%
  \snotez@write@mark{\hbox{\snotez@text@mark@format{\@the@snotez@mark}}}%
}

\ifcsdef{c@chapter}
  {\newcounter{sidenote}[chapter]}
  {\newcounter{sidenote}}

% --------------------------------------------------------------------------
% the \sidenotemark command
\newrobustcmd*\sidenotemark[1][]{\snotez@mark{#1}}

% --------------------------------------------------------------------------
% the \sidenotetext command
% syntax:
% - \sidenotetext{text}
% - \sidenotetext[mark]{text}
% - \sidenotetext[offset][mark]{text}
\newrobustcmd*\sidenotetext{%
  \@ifnextchar[%
    {\snotez@sidenotetext@aux@i}
    {\snotez@sidenotetext@aux@ii{}[]}}% no option

\def\snotez@sidenotetext@aux@i[#1]{%
  \@ifnextchar[%
    {\snotez@sidenotetext@aux@ii{#1}}% two options
    {\snotez@sidenotetext@aux@ii{}[#1]}}% one option

% #1: offset
% #2: mark
% #3: text
\long\def\snotez@sidenotetext@aux@ii#1[#2]#3{%
  \ifblank{#2}{}{\protected@xdef\@the@snotez@mark{#2}}%
  \snotez@text{#1}{#3}}

% #1: offset
% #2: text
\def\snotez@text#1#2{%
  \ifblank{#1}
    {%
      \snotez@marginpar{%
        \snotez@format
        \snotez@write@mark{\snotez@note@mark@format{\@the@snotez@mark}}%
        \snotez@note@mark@sep#2}%
    }
    {%
      \snotez@marginnote{%
        \snotez@format
        \snotez@write@mark{\snotez@note@mark@format{\@the@snotez@mark}}%
        \snotez@note@mark@sep#2}[#1]%
    }}

\let\snotez@marginpar\marginpar
\let\snotez@marginnote\marginnote

% --------------------------------------------------------------------------
% process preamble options:
\AtBeginDocument{
  % if option `footnote=true' let \footnote to \sidenote:
  \ifbool{snotez@footnote}
    {
      \let\footnote\sidenote
      \let\footnotemark\sidenotemark
      \let\footnotetext\sidenotetext
    }{}
  % if option `marginnote=true' use \marginnote for all notes:
  \ifbool{snotez@marginnote}
    {\let\snotez@marginpar\marginnote}
    {}
}
\AtEndPreamble{
  % if option `perpage=true' count notes per page:
  \ifbool{snotez@perpage}
    {\MakeSortedPerPage{sidenote}}
    {\MakeSorted{sidenote}}
}

\endinput

% --------------------------------------------------------------------------
% HISTORY:
2012/11/15 v0.1  - first version
2013/04/03 v0.2  - documentation, removed erroneous `fnpct' adaption, other
                   minor changes
                 - added update of \@currentlabel when a custom mark is used

% --------------------------------------------------------------------------
% TODO