% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `acid-base'
%
% --------------------------------------------------------------------------
% 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{acid-base}{2015/06/09 acid/base}

\chemmacros_load_modules:n {charges}

% --------------------------------------------------------------------------
\tl_new:N  \l__chemmacros_k_acid_tl
\tl_new:N  \l__chemmacros_k_base_tl
\tl_new:N  \l__chemmacros_k_water_tl

\tl_set:Nn \l__chemmacros_k_acid_tl  { \chemmacros_translate:n {K-acid} }
\tl_set:Nn \l__chemmacros_k_base_tl  { \chemmacros_translate:n {K-base} }
\tl_set:Nn \l__chemmacros_k_water_tl { \chemmacros_translate:n {K-water} }

\keys_define:nn {chemmacros/acid-base}
  {
    p-style           .choice: ,
    p-style / slanted .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textsl ,
    p-style / italics .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textit ,
    p-style / upright .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textup ,
    K-acid            .tl_set:N = \l__chemmacros_k_acid_tl ,
    K-base            .tl_set:N = \l__chemmacros_k_base_tl ,
    K-water           .tl_set:N = \l__chemmacros_k_water_tl
  }

\cs_new_eq:NN \chemmacros_p_style:n \textup

\cs_new_protected:Npn \Ka
  {
    \ensuremath
      {
        \chemmacros_bold:n
          {
            K
            \c_math_subscript_token
              { \l__chemmacros_k_acid_tl }
          }
      }
  }

\cs_new_protected:Npn \Kb
  {
    \ensuremath
      {
        \chemmacros_bold:n
          {
            K
            \c_math_subscript_token
              { \l__chemmacros_k_base_tl }
          }
      }
  }

\cs_new_protected:Npn \Kw
  {
    \ensuremath
      {
        \chemmacros_bold:n
          {
            K
            \c_math_subscript_token
              { \l__chemmacros_k_water_tl }
          }
      }
  }

\cs_new_protected:Npn \chemmacros_p:n #1
  {
    \group_begin:
      \mbox
        {
          \chemmacros_p_style:n { p }
          \ensuremath { #1 }
        }
    \group_end:
  }

\NewDocumentCommand \p { m }
  { \chemmacros_p:n { #1 } }

\cs_new_protected:Npn \pH
  { \chemmacros_p:n { \chemmacros_chemformula:n {H} } }

\cs_new_protected:Npn \pOH
  { \chemmacros_p:n { \chemmacros_chemformula:n {OH} } }

\NewDocumentCommand \pKa { o }
  {
    \chemmacros_p:n
      {
        \Ka \IfNoValueF { #1 }
          { {} \c_math_subscript_token { \chemmacros_bold:n { #1 } } }
      }
  }

\NewDocumentCommand \pKb { o }
  {
    \chemmacros_p:n
      {
        \Kb \IfNoValueF { #1 }
          { {} \c_math_subscript_token { \chemmacros_bold:n { #1 } } }
      }
  }

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