% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `mechanisms'
%
% --------------------------------------------------------------------------
% Clemens Niederberger
% --------------------------------------------------------------------------
% https://github.org/cgnieder/chemmacros/
% contact@mychemistry.eu
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
% Copyright 2011-2015 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.
% --------------------------------------------------------------------------
\ChemModule{mechanisms}{2015/06/09 reaction mechanisms}

\RequirePackage{amstext}

% --------------------------------------------------------------------------
% reaction mechanisms
% \mech[<type>]
% <type> - substitutions: {}, 1, 2, se, 1e, 2e, ar
%        - eliminations:  e, e1, e2, cb
\tl_new:N \l__chemmacros_mechanisms_type_tl
\tl_new:N \l__chemmacros_mechanisms_mol_tl
\tl_new:N \l__chemmacros_mechanisms_ar_tl

\cs_new_protected:Npn \__chemmacros_set_mechanisms:nnn #1#2#3
  {
    \tl_set:Nn \l__chemmacros_mechanisms_type_tl { #1 }
    \tl_set:Nn \l__chemmacros_mechanisms_mol_tl  { #2 }
    \tl_set:Nn \l__chemmacros_mechanisms_ar_tl   { #3 }
  }

\keys_define:nn {chemmacros/mechanisms}
  {
    type      .choice: ,
    type /    .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {N} }
            \c_math_toggle_token
          }
          { }
      } ,
    type / 1  .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {N} }
            \c_math_toggle_token
            1
          }
          { }
      } ,
    type / 2  .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {N} }
            \c_math_toggle_token
            2
          }
          { }
      } ,
    type / se .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {E} }
            \c_math_toggle_token
          }
          { }
      } ,
    type / 1e .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {E} }
            \c_math_toggle_token
            1
          }
          { }
      } ,
    type / 2e .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {E} }
            \c_math_toggle_token
            2
          }
          { }
      } ,
    type / ar .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { S }
          {
            \c_math_toggle_token
              \c_math_subscript_token { \text {E} }
            \c_math_toggle_token
          }
          { Ar - }
      } ,
    type / e  .code:n    =
      { \__chemmacros_set_mechanisms:nnn { E } { } { } } ,
    type / e1 .code:n    =
      { \__chemmacros_set_mechanisms:nnn { E } { 1 } { } } ,
    type / e2 .code:n    =
      { \__chemmacros_set_mechanisms:nnn { E } { 2 } { } } ,
    type / cb .code:n    =
      {
        \__chemmacros_set_mechanisms:nnn { E }
          {
            1
            \c_math_toggle_token
              \c_math_subscript_token { \text {cb} }
            \c_math_toggle_token
          }
          { }
      } ,
    type      .default:n = 
  }

\cs_new_protected:Npn \chemmacros_mechanisms:n #1
  {
    \tl_if_blank:nTF {#1}
      { \keys_set:nn {chemmacros/mechanisms} { type } }
      { \keys_set:nn {chemmacros/mechanisms} { type = #1 } }
    \mbox
      {
        \tl_use:N \l__chemmacros_mechanisms_ar_tl
        \tl_use:N \l__chemmacros_mechanisms_type_tl
        \tl_use:N \l__chemmacros_mechanisms_mol_tl
      }
  }

\NewDocumentCommand \mech {o}
  {
    \IfNoValueTF {#1}
      { \chemmacros_mechanisms:n { } }
      { \chemmacros_mechanisms:n {#1} }
  }

% --------------------------------------------------------------------------
