% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `phases'
%
% --------------------------------------------------------------------------
% 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-2017 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{phases}{2016/05/31 phase descriptors}

\chemmacros_load_module:n {chemformula}

\bool_new:N \l__chemmacros_phases_sub_bool
\dim_new:N  \l__chemmacros_phases_space_dim
\dim_set:Nn \l__chemmacros_phases_space_dim { .1333 em }

\chemmacros_define_keys:nn {phases}
  {
    pos        .choice: ,
    pos / sub  .code:n    = \bool_set_true:N \l__chemmacros_phases_sub_bool ,
    pos / side .code:n    = \bool_set_false:N \l__chemmacros_phases_sub_bool ,
    space      .dim_set:N = \l__chemmacros_phases_space_dim
  }

\NewChemMacroset {Phase} {mm}
  { \chemmacros_define_phase:Nn #1 {#2} }

\ChemCompatibilityTo{5.7}
\cs_new_protected:Npn \chemmacros_define_phase:Nn #1#2
  {
    \tl_set:Nx \l__chemmacros_tmpa_tl { phase-\chemmacros_remove_backslash:N #1 }
    \exp_args:NV \@trnslt@declare@translation
      \l__chemmacros_tmpa_tl
      {fallback}
      {#2}
    \__chemmacros_define_phase:Nx #1
      { \exp_not:N \chemmacros_translate:n { \exp_not:V \l__chemmacros_tmpa_tl } }
  }
\EndChemCompatibility

\ChemCompatibilityFrom{5.7}
\cs_new_protected:Npn \chemmacros_define_phase:Nn #1#2
  {
    \tl_set:Nx \l__chemmacros_tmpa_tl { phase-\chemmacros_remove_backslash:N #1 }
    \chemmacros_declare_translation:Vnn
      \l__chemmacros_tmpa_tl
      {fallback}
      {#2}
    \__chemmacros_define_phase:Nx #1
      { \exp_not:N \chemmacros_translate:n { \exp_not:V \l__chemmacros_tmpa_tl } }
  }
\EndChemCompatibility

\cs_new_protected:Npn \__chemmacros_define_phase:Nn #1#2
  {
    \cs_if_exist:NF #1 { \cs_new:Npn #1 {} }
    \DeclareDocumentCommand #1 {o}
      { \chemmacros_phase:n { #2 \IfNoValueF {##1} {,~##1} } }
  }
\cs_generate_variant:Nn \__chemmacros_define_phase:Nn { Nx }

\cs_new_protected:Npn \chemmacros_phase:n #1
  {
    \chemmacros_leave_vmode:
    \bool_if:NTF \l__chemmacros_phases_sub_bool
      { \chemformula_subscript:n { (#1) } }
      {
        \skip_horizontal:N \l__chemmacros_phases_space_dim
        \chemmacros_text:n { (#1) }
      }
  }

% --------------------------------------------------------------------------
\NewDocumentCommand \phase {m} { \chemmacros_phase:n {#1} }

\NewChemPhase \sld {s}
\NewChemPhase \lqd {l}
\NewChemPhase \gas {g}
\NewChemPhase \aq  {aq}

% --------------------------------------------------------------------------
\ChemCompatibilityTo{5.6}
\DeclareTranslation {English} {phase-sld} {s}
\DeclareTranslation {German}  {phase-sld} {f}

\DeclareTranslation {English} {phase-lqd} {l}
\DeclareTranslation {German}  {phase-lqd} {f\/l}

\DeclareTranslation {English} {phase-gas} {g}
\DeclareTranslation {German}  {phase-gas} {g}

\DeclareTranslation {English} {phase-aq} {aq}
\DeclareTranslation {German}  {phase-aq} {aq}
\EndChemCompatibility

\ChemCompatibilityFrom{5.6}
\chemmacros_declare_translations:nn {phase-sld}
  {
    fallback = s ,
    German   = f
  }

\chemmacros_declare_translations:nn {phase-lqd}
  {
    fallback = l ,
    German   = f\/l
  }

\chemmacros_declare_translations:nn {phase-gas}
  { fallback = g }

\chemmacros_declare_translations:nn {phase-aq}
  { fallback = aq }
\EndChemCompatibility

% --------------------------------------------------------------------------
\tex_endinput:D

2015/06/09 - first version
2016/04/27 - moved translations from lang module to phases module
2016/05/31 - adapt to changes in `lang' module
