% --------------------------------------------------------------------------
%
% 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--2020 Clemens Niederberger
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% 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 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
\ChemModule{acid-base}{2018/02/22 acid/base}

\chemmacros_load_modules:n {charges}

% --------------------------------------------------------------------------
% equilibrium constants:

\tl_new:N  \l__chemmacros_equilibrium_constant_tl
\tl_set:Nn \l__chemmacros_equilibrium_constant_tl {K}
\prop_new:N \l__chemmacros_eq_constants_prop

\prg_new_conditional:Npnn \chemmacros_if_eq_constant:Nn #1#2 {T,F,TF}
  {
    \cs_if_exist:NTF #1
      {
        \tl_if_eq:xnTF
          { \prop_item:Nn \l__chemmacros_eq_constants_prop {#1} }
          {#2}
          { \prg_return_true: }
          { \prg_return_false: }
      }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \tl_if_eq:nnTF {x}

% #1: cs
% #2: translation id and option name
% #3: default translation
\cs_new_protected:Npn \chemmacros_define_eq_constant:Nnn #1#2#3
  {
    \chemmacros_if_eq_constant:NnF {#1} {#2}
      {
        \cs_new_protected:Npn #1 {}
        \prop_put:Nnn \l__chemmacros_eq_constants_prop {#1} {#2}
        \tl_new:c {l__chemmacros_#2_tl}
        \chemmacros_define_keys:nn {acid-base}
          { #2 .tl_set:c = l__chemmacros_#2_tl }
      }
    \tl_set:cn
      {l__chemmacros_#2_tl}
      { \chemmacros_translate:n {#2} }
    \chemmacros_if_compatibility:nnTF {>=} {5.7}
      { \chemmacros_declare_translation:nnn {#2} {fallback} {#3} }
      { \DeclareTranslationFallback {#2} {#3} }
    \cs_set_protected:Npn #1
      {
        \ensuremath
          {
            \chemmacros_bold:n
              {
                \l__chemmacros_equilibrium_constant_tl
                \c_math_subscript_token
                  { \tl_use:c {l__chemmacros_#2_tl} }
              }
          }
      }
  }

\NewChemMacroset {EqConstant} {mmm}
  { \chemmacros_define_eq_constant:Nnn #1 {#2} {#3} }

\NewChemEqConstant \Ka {K-acid}  { \mathrm{a} }
\NewChemEqConstant \Kb {K-base}  { \mathrm{b} }
\NewChemEqConstant \Kw {K-water} { \mathrm{w} }

\chemmacros_if_compatibility:nnT {>=} {5.4}
  {
    \chemmacros_define_keys:nn {acid-base}
      { eq-constant .tl_set:N = \l__chemmacros_equilibrium_constant_tl }
  }

\chemmacros_define_keys:nn {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 ,
    p-style           .initial:n = upright
  }

\cs_new_protected:Npn \chemmacros_p:n #1
  {
    \group_begin:
      \chemmacros_if_compatibility:nnTF {<=} {5.8b}
        { \mbox }
        { \chemmacros_text:n }
        {
          \chemmacros_p_style:n {p}
          \ensuremath {#1}
        }
    \group_end:
  }

\NewDocumentCommand \p  {m} { \chemmacros_p:n {#1} }
\NewDocumentCommand \pH  {} { \chemmacros_p:n { \chemmacros_chemformula:n {H} } }
\NewDocumentCommand \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} } }
      }
  }

% --------------------------------------------------------------------------
\ChemCompatibilityTo{5.6}
\DeclareTranslation {English} {K-acid}  {\mathrm{a}}
\DeclareTranslation {German}  {K-acid}  {\mathrm{s}}
\DeclareTranslation {Dutch}   {K-acid}  {\mathrm{z}}

\DeclareTranslation {English} {K-base}  {\mathrm{b}}

\DeclareTranslation {English} {K-water} {\mathrm{w}}
\EndChemCompatibility

\ChemCompatibilityFrom{5.6}
\chemmacros_declare_translations:nn {K-acid}
  {
    German = \mathrm {s} ,
    Dutch  = \mathrm {z}
  }
\EndChemCompatibility
% --------------------------------------------------------------------------
\file_input_stop:

2016/02/04 - \NewChemEqConstant and family, new option `eq-constant'
2016/04/27 - moved translations from lang module to acid-base module
2016/05/31 - adapt to changes in `lang' module
2018/02/22 - fix bug in \chemmacros_p:n (https://tex.stackexchange.com/q/416227/)
