\ProvidesPackage{mol2chemfig}[2019/11/24 v1.5]

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

\def\mcf@three@ea{\expandafter\expandafter\expandafter}

% this macro (by Christian Tellechea) draws circles in aromatic rings. 
\def\mcfcringle#1{% 
 \mcf@three@ea\tikz\mcf@three@ea\draw\mcf@three@ea[\useKV[chemfig]{chemfig
    style}](0,0)circle(\useKV[chemfig]{atom
    sep}*#1*0.66667*\useKV[chemfig]{cycle radius coeff});}

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

\def\CF@@keytomacro#1#2{\edef#1{\unexpanded\expandafter\expandafter\expandafter{\useKV[chemfig]{#2}}}}

\tikzset{ % set styles
    % wavy bonds. Slightly modified from http://www.texdev.net/tag/chemfig/
    setatomsep/.code={
      \CF@@keytomacro\CF@@atomsep{atom sep}
      \pgfmathsetmacro\mcf@@segmentlength{\CF@@atomsep/6}
      \pgfmathsetmacro\mcf@@amplitude{\CF@@atomsep/10}
    },
    %
    mcfwavy/.style =
    {
      setatomsep,
      decorate,
      decoration =
        {
          complete sines,
          post length = 0 pt,
          pre length  = 0 pt,
          % Use chemfig's atom spacing
          segment length=\mcf@@segmentlength, 
          amplitude=\mcf@@amplitude
        }
    },
    % this style is used by the \mcfelmove macro
    mcfpusharrow/.style = {
        ->,  
        mcfpusharrowcolor,
        >=stealth,  
        shorten <=3pt,
        shorten >=2pt,
        preaction={draw=mcfbgcolor, -,line width=1.5pt}
    },
    %
    % 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={\useKV[chemfig]{double bond sep}},
            shorten <= {#1*\useKV[chemfig]{double bond sep}/100},
            shorten >= {#2*\useKV[chemfig]{double bond sep}/100}
        }
    },
    %
    mcfcrossbond/.style={% for backwards compatibility only
        mcfx={100}{100}
    },
    %
    secondbond/.style 2 args={
        shorten <= {#1*\useKV[chemfig]{double bond sep}/100}, 
        shorten >= {#2*\useKV[chemfig]{double bond sep}/100}
    },
    %
    secondfgbond/.style 2 args={
        preaction={draw,-,mcfbgcolor,line width={\useKV[chemfig]{double bond sep}}},
        secondbond={#1}{#2}    
    },
    %
    secondleft/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise={\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondright/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-{\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondfgleft/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise={\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondfgright/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-{\useKV[chemfig]{double bond 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"}

.
