% auxiliary package for mol2chemfig
\ProvidesPackage{mol2chemfig}[2014/03/24 v1.4]

\RequirePackage{xcolor, chemfig, twoopt, ifmtarg}
\usetikzlibrary{decorations}

% \setbondstyle is defined by chemfig. Here, we wrap it
% in order to also save the bond style in a tikz key.
\let\mcf@setbondstyle\setbondstyle
\renewcommand{\setbondstyle}[1]{%
    \mcf@setbondstyle{#1}%
    \tikzset{mcfbond/.style={#1}}%
}

% this macro (by Christian Tellechea) draws circles in aromatic rings. 
\def\mcfcringle#1{% 
\tikz\draw[mcfbond](0,0)circle(\CF@atom@sep*#1*0.66667*\CF@cycle@inraduiscoeff);}

% auxiliary macros for rendering double and triple bonds

% tikz decoration to render parallel bond strokes
\pgfdeclaredecoration{mcfsecondstroke}{initial}{
  \state{initial}[width=\pgfdecoratedpathlength-1sp]{
     \pgfmoveto{\pgfpointorigin}
  }
  \state{final}{
     \pgflineto{\pgfpointorigin}
  }
}

% the complete sines definition is taken from http://tex.stackexchange.com/questions/25678/
\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
}

% color definitions 
\colorlet{mcfbgcolor}{white} % adjust when drawing on a non-white canvas 
\colorlet{mcfpusharrowcolor}{black}
\colorlet{mcfatomnocolor}{blue}

\tikzset{ % set styles
    % wavy bonds. Slightly modified from http://www.texdev.net/tag/chemfig/
    mcfwavy/.style =
    {
      decorate,
      decoration =
        {
          complete sines,
          post length = 0 pt,
          pre length  = 0 pt,
          % Use chemfig's atom spacing
          segment length = \the\dimexpr\csname CF@atom@sep\endcsname/6\relax,
          amplitude = \the\dimexpr\csname CF@atom@sep\endcsname/10\relax
        }
    },
    % this style is used by the \mcfelmove macro
    mcfpusharrow/.style = {
        ->,
        mcfpusharrowcolor,
        >=stealth,
        shorten <=3pt,
        shorten >=2pt,
        preaction={draw=mcfbgcolor, -,line width=1.5pt}
    },
    %
    mcfbond/.style = {}, % the chemfig default bond style is empty, too. 
    %
    % one bond crossing before another - gets a white backdrop 
    % as wide as doublesep
    mcfbgcolor/.style={white},
    %
    mcfx/.style 2 args={%
        preaction={
            draw,
            mcfbgcolor,
            line width=\CF@double@sep,
            shorten <= {#1*\CF@double@sep/100},
            shorten >= {#2*\CF@double@sep/100}
        },
        mcfbond
    },
    %
    mcfcrossbond/.style={% for backwards compatibility only
        mcfx={100}{100}
    },
    %
    secondbond/.style 2 args={
        shorten <= {#1*\CF@double@sep/100}, 
        shorten >= {#2*\CF@double@sep/100}, 
        mcfbond
    },
    %
    secondfgbond/.style 2 args={
        preaction={draw,-,mcfbgcolor,line width=\CF@double@sep},
        secondbond={#1}{#2}    
    },
    %
    secondleft/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise=\CF@double@sep},
        decorate
    },
    %
    secondright/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-\CF@double@sep},
        decorate
    },
    %
    secondfgleft/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise=\CF@double@sep},
        decorate
    },
    %
    secondfgright/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-\CF@double@sep},
        decorate
    },
    %
    dbl/.style 2 args={
      preaction={draw,secondleft={#1}{#2}}
    },
    %
    dbr/.style 2 args={
      preaction={draw,secondright={#1}{#2}}
    },
    %
    trpl/.style 2 args={
      preaction={draw,secondleft={#1}{#2}},
      preaction={draw,secondright={#1}{#2}}
    },
    %
    dblx/.style n args={4}{
      preaction={draw,secondfgleft={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    dbrx/.style n args={4}{
      preaction={draw,secondfgright={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    trplx/.style n args={4}{
      preaction={draw,secondfgleft={#1}{#2}},
      preaction={draw,secondfgright={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    drh/.style={dbr={58}{58}},
    dlh/.style={dbl={58}{58}},
    drhs/.style={dbr={58}{0}},
    dlhs/.style={dbl={58}{0}},
    drhe/.style={dbr={0}{58}},
    dlhe/.style={dbl={0}{58}},
    drn/.style={dbr={0}{0}},
    dln/.style={dbl={0}{0}}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Conveniences for drawing push arrows
% the \mcfelmove command is adapted from the
% myChemistry package by Clemens Niederberger

\newcommand{\mcfelmove}[5][]{%
\node at (0,0) {% dummy wrapper node
\chemmove{\draw[mcfpusharrow,#1](#2).. controls +(#3) and +(#5)..(#4);%
}};}

% and, since the most common use for tikz code
% is arrow shortening and lengthening: a command streamlined for that.

\newcommandtwoopt{\mcfpush}[6][][]{% we use empties to keep same defaults
\@ifmtarg{#2}{
    \@ifmtarg{#1}{\mcfelmove{#3}{#4}{#5}{#6}}
                 {\mcfelmove[shorten <=#1]{#3}{#4}{#5}{#6}}
}{% #2 not empty
    \@ifmtarg{#1}{\mcfelmove[shorten >=#2]{#3}{#4}{#5}{#6}}
                 {\mcfelmove[shorten <=#1, shorten >=#2]{#3}{#4}{#5}{#6}}
}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Stacking atoms horizontally and vertically
\newlength{\mcfvspace}
\setlength{\mcfvspace}{1.5pt}

\newcommand{\mcfabove}[2]{\chemabove[\mcfvspace]{#1}{#2}}
\newcommand{\mcfbelow}[2]{\chembelow[\mcfvspace]{#1}{#2}}

\newlength{\mcf@boxwidth}
\newlength{\mcf@boxheight}

\newcommand{\mcf@box}[3][l]{
\settowidth{\mcf@boxwidth}{\printatom{#2}}
\settoheight{\mcf@boxheight}{\printatom{#2}}
\makebox[\mcf@boxwidth][#1]{\raisebox{0pt}[\mcf@boxheight][0pt]{\printatom{#3}}}}

\newcommand{\mcfleft}[2]{\mcf@box[r]{#2}{#1#2}}
\newcommand{\mcfright}[2]{\mcf@box{#1}{#1#2}}

\newcommand{\mcfaboveright}[3]{\mcfabove{#1}{\mcf@box{#2}{#2#3}}}
\newcommand{\mcfbelowright}[3]{\mcfbelow{#1}{\mcf@box{#2}{#2#3}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Formatting of charges and atom numbers

% Charges - could for example be defined as \oplus and \ominus
\newcommand{\mcfplus}{+}
\newcommand{\mcfminus}{-}

% atom numbers (used when option atom-numbers is selected)
\newcommand{\mcfatomno}[1]%
{\raisebox{2pt}{\color{mcfatomnocolor}{\ensuremath{\mathsf{_{#1}}}}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a little help with direct processing of mol2chemfig commands
\newcommand{\mcfinput}[1]{\@@input|"mol2chemfig #1"}


