% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `errorcheck'
%
% --------------------------------------------------------------------------
% 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{errorcheck}{2016/10/05 error checking for unloaded modules}[5.2]

\RequirePackage{scrlfile}

\msg_new:nnn {chemmacros} {undefined}
  {
    The~ #1~ `#2'~ is~ undefined.~ You~ need~ to~ load~ the~
    `#3'~ module.
  }

\cs_generate_variant:Nn \msg_error:nnnnn {nnnx}

\cs_new_protected:Npn \chemmacros_requires_module:nNn #1#2#3
  {
    \chemmacros_if_module_loaded:nF {#3}
      {
        \cs_if_free:NT #2
          {
            \cs_set:Npn #2
              {
                \msg_error:nnnxn {chemmacros} {undefined}
                  {#1}
                  {\token_to_str:N #2}
                  {#3}
              }
            \chemmacros_before_module:nn {#3} { \cs_undefine:N #2 }
          }
      }
  }
\cs_generate_variant:Nn \chemmacros_requires_module:nNn {nc}

\cs_new_protected:Npn \chemmacros_command_requires_module:Nn #1#2
  { \chemmacros_requires_module:nNn {command} #1 {#2} }

\cs_new_protected:Npn \chemmacros_environment_requires_module:nn #1#2
  { \chemmacros_requires_module:ncn {environment} {#1} {#2} }

% --------------------------------------------------------------------------
% add checking for the most common user commands and environments:
% isotopes module
\chemmacros_command_requires_module:Nn \isotope {isotopes}

% mechanisms module
\chemmacros_command_requires_module:Nn \mech {mechanisms}

% newman module
\chemmacros_command_requires_module:Nn \newman {newman}

% orbital module
\chemmacros_command_requires_module:Nn \orbital {orbital}

% polymers module
\chemmacros_command_requires_module:Nn \makepolymerdelims {polymers}

% reactions module
\chemmacros_environment_requires_module:nn {reaction}       {reactions}
\chemmacros_environment_requires_module:nn {reaction*}      {reactions}
\chemmacros_environment_requires_module:nn {reactions}      {reactions}
\chemmacros_environment_requires_module:nn {reactions*}     {reactions}
\chemmacros_command_requires_module:Nn     \listofreactions {reactions}
\chemmacros_command_requires_module:Nn     \AddRxnDesc      {reactions}

% redox module
\chemmacros_command_requires_module:Nn \ox    {redox}
\chemmacros_command_requires_module:Nn \OX    {redox}
\chemmacros_command_requires_module:Nn \redox {redox}

% scheme module
\chemmacros_environment_requires_module:nn {scheme}        {scheme}
\chemmacros_command_requires_module:Nn     \listschemename {scheme}
\chemmacros_command_requires_module:Nn     \schemename     {scheme}
\chemmacros_command_requires_module:Nn     \listofschemes  {scheme}

\BeforePackage {chemscheme}
  {
    \cs_undefine:N \scheme
    \cs_undefine:N \endscheme
    \cs_undefine:N \listschemename
    \cs_undefine:N \schemename
    \cs_undefine:N \listofschemes
  }

% spectroscopy module
\chemmacros_command_requires_module:Nn     \NMR           {spectroscopy}
\chemmacros_environment_requires_module:nn {experimental} {spectroscopy}

% thermodynamics module
\chemmacros_command_requires_module:Nn \state    {thermodynamics}
\chemmacros_command_requires_module:Nn \enthalpy {thermodynamics}
\chemmacros_command_requires_module:Nn \entropy  {thermodynamics}
\chemmacros_command_requires_module:Nn \gibbs    {thermodynamics}

% xfrac module
\chemmacros_command_requires_module:Nn \chemfrac {xfrac}

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

\file_input_stop:

2015/10/14 - first version
2016/03/08 - add `polymers' module
2016/05/31 - enable `chemscheme' to be loaded after chemmacros
2016/10/06 - check if commands exist before defining them to raise errors
