% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `nomenclature'
%
% --------------------------------------------------------------------------
% 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.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{nomenclature}{2017/06/11 chemical names}

\RequirePackage{scrlfile}

\chemmacros_load_module:n {tikz}

\bool_new:N      \l__chemmacros_iupac_restricted_bool
\bool_new:N      \l__chemmacros_iupac_strict_bool

\msg_new:nnn {chemmacros} {new-iupac}
  {
    You've~ tried~ to~ define~ a~ iupac~ naming~ command~ with~
    \token_to_str:N \NewChemIUPAC ,~ but~ the~ iupac~ command~
    \token_to_str:N #1 \c_space_tl already~ exists.~ Choose~ another~ name~
    or~ use~ \token_to_str:N \RenewChemIUPAC .
  }

\msg_new:nnn {chemmacros} {renew-iupac}
  {
    You've~ tried~ to~ renew~ the~ iupac~ command~ \token_to_str:N #1 ,~ but~
    it~ doesn't~ exist.~ Use~ \token_to_str:N \NewChemIUPAC \c_space_tl
    instead.
  }

\msg_new:nnn {chemmacros} {let-iupac}
  {
    You've~ tried~ to~ let~ the~ iupac~ command~ \token_to_str:N #1
    \c_space_tl to~ the~ iupac~ command~ \token_to_str:N #2 ,~ but~ the~
    latter~ doesn't~ exist.~  Use~ \token_to_str:N \NewChemIUPAC \c_space_tl
    instead~ or~declare~ \token_to_str:N #2 \c_space_tl first.
  }

\msg_new:nnn {chemmacros} {new-iupac-shorthand}
  {
    You've~ tried~ to~ define~ a~ iupac~ shorthand~ with~ \token_to_str:N
    \NewChemIUPACShorthand ,~ but~ the~ shorthand~ \token_to_str:N #1
    \c_space_tl already~ exists.~ Please~ choose~ another~ token.
  }

\msg_new:nnn {chemmacros} {renew-iupac-shorthand}
  {
    You've~ tried~ to~ renew~ the~ iupac~ shorthand~ \token_to_str:N #1 ,~
    but~ this~ shorthand~ doesn't~ exist,~ yet.
  }

\msg_new:nnn {chemmacros} {show-iupac}
  { \\ >~ \token_to_str:N #1=iupac~macro: \\ -> #2 }

\msg_new:nnn {chemmacros} {no-iupac-command}
  { \\ >~ \token_to_str:N #1=not~ defined~ as~ iupac~ macro }

\cs_generate_variant:Nn \msg_show:nnnn {nnnV}

% --------------------------------------------------------------------------
\prop_new:N \l__chemmacros_iupac_prop

\cs_new_protected:Npn \__chemmacros_iupac_name:NN #1#2
  { \str_set:Nx #2 { \chemmacros_remove_backslash:N #1 } }

\cs_new_protected:Npn \__chemmacros_define_iupac:Nn #1#2
  {
    \__chemmacros_iupac_name:NN #1 \l__chemmacros_tmpa_str
    \prop_put:NVn \l__chemmacros_iupac_prop
      \l__chemmacros_tmpa_str
      {#2}
    \chemmacros_make_iupac:
  }
\cs_generate_variant:Nn \__chemmacros_define_iupac:Nn {NV}

\cs_new_protected:Npn \__chemmacros_let_iupac:NN #1#2
  {
    \__chemmacros_iupac_name:NN #1 \l__chemmacros_tmpa_str
    \__chemmacros_iupac_name:NN #2 \l__chemmacros_tmpb_str
    \tl_set:Nx \l__chemmacros_tmpa_tl
      { \prop_item:NV \l__chemmacros_iupac_prop \l__chemmacros_tmpb_str }
    \__chemmacros_define_iupac:NV #1 \l__chemmacros_tmpa_tl
  }

\cs_new_protected:Npn \chemmacros_show_iupac:N #1
  {
    \group_begin:
      \__chemmacros_iupac_name:NN #1 \l__chemmacros_tmpa_str
      \tl_set:Nx \l__chemmacros_tmpa_tl
        { \prop_item:NV \l__chemmacros_iupac_prop \l__chemmacros_tmpa_str }
      \tl_if_empty:NTF \l__chemmacros_tmpa_tl
        { \msg_show:nnn {chemmacros} {no-iupac-command} {#1} }
        { \msg_show:nnnV {chemmacros} {show-iupac} {#1} \l__chemmacros_tmpa_tl }
    \group_end:
  }

\prg_new_protected_conditional:Npnn \chemmacros_if_iupac:N #1 {T,F,TF}
  {
    \__chemmacros_iupac_name:NN #1 \l__chemmacros_tmpa_str
    \prop_if_in:NVTF \l__chemmacros_iupac_prop \l__chemmacros_tmpa_str
      { \prg_return_true: }
      { \prg_return_false: }
  }

\cs_new_protected:Npn \chemmacros_new_iupac:Nn #1#2
  {
    \chemmacros_if_iupac:NTF #1
      { \msg_error:nnn {chemmacros} {new-iupac} {#1} }
      { \__chemmacros_define_iupac:Nn #1 {#2} }
  }

\cs_new_protected:Npn \chemmacros_provide_iupac:Nn #1#2
  {
    \chemmacros_if_iupac:NF #1
      { \__chemmacros_define_iupac:Nn #1 {#2} }
  }

\cs_new_protected:Npn \chemmacros_define_iupac:Nn #1#2
  { \__chemmacros_define_iupac:Nn #1 {#2} }

\cs_new_protected:Npn \chemmacros_renew_iupac:Nn #1#2
  {
    \chemmacros_if_iupac:NTF #1
      { \__chemmacros_define_iupac:Nn #1 {#2} }
      { \msg_error:nnn {chemmacros} {renew-iupac} {#1} }
  }

\cs_new_protected:Npn \chemmacros_let_iupac:NN #1#2
  {
    \chemmacros_if_iupac:NTF #2
      { \__chemmacros_let_iupac:NN #1#2 }
      { \msg_error:nnnn {chemmacros} {let-iupac} {#1} {#2} }
  }

\ChemCompatibilityTo{5.2}
\cs_new_protected:Npn \chemmacros_make_iupac:
  {
    \bool_if:NT \l__chemmacros_in_document_bool
      {
        \bool_if:NTF \l__chemmacros_inside_iupac_bool
          {
            \prop_map_inline:Nn \l__chemmacros_iupac_prop
              { \cs_set_protected:cpn {##1} {##2} }
          }
          {
            \bool_if:NF \l__chemmacros_iupac_restricted_bool
              {
                \bool_if:NTF \l__chemmacros_iupac_strict_bool
                  {
                    \prop_map_inline:Nn \l__chemmacros_iupac_prop
                      { \cs_set_protected:cpn {##1} {##2} }
                  }
                  {
                    \prop_map_inline:Nn \l__chemmacros_iupac_prop
                      {
                        \cs_if_exist:cF {##1}
                          { \cs_set_protected:cpn {##1} {##2} }
                      }
                  }
              }
          }
      }    
  }
\AtBeginDocument { \chemmacros_make_iupac: }
\EndChemCompatibility

\ChemCompatibilityFrom{5.2}
\cs_new_protected:Npn \chemmacros_make_iupac:
  {
    \chemmacros_if_document:T
      {
        \bool_if:NTF \l__chemmacros_inside_iupac_bool
          {
            \prop_map_inline:Nn \l__chemmacros_iupac_prop
              { \cs_set_protected:cpn {##1} {##2} }
          }
          {
            \bool_if:NF \l__chemmacros_iupac_restricted_bool
              {
                \bool_if:NTF \l__chemmacros_iupac_strict_bool
                  {
                    \prop_map_inline:Nn \l__chemmacros_iupac_prop
                      { \cs_set_protected:cpn {##1} {##2} }
                  }
                  {
                    \prop_map_inline:Nn \l__chemmacros_iupac_prop
                      {
                        \cs_if_exist:cF {##1}
                          { \cs_set_protected:cpn {##1} {##2} }
                      }
                  }
              }
          }
      }    
  }
\AfterEndPreamble { \chemmacros_make_iupac: }
\EndChemCompatibility

\NewDocumentCommand \DeclareChemIUPAC {mm}
  { \chemmacros_define_iupac:Nn #1 {#2} }

\NewDocumentCommand \NewChemIUPAC {mm}
  { \chemmacros_new_iupac:Nn #1 {#2} }

\NewDocumentCommand \RenewChemIUPAC {mm}
  { \chemmacros_renew_iupac:Nn #1 {#2} }

\NewDocumentCommand \ProvideChemIUPAC {mm}
  { \chemmacros_provide_iupac:Nn #1 {#2} }

\NewDocumentCommand \LetChemIUPAC {mm}
  { \chemmacros_let_iupac:NN #1 #2 }

% --------------------------------------------------------------------------
\chemmacros_define_keys:nn {nomenclature}
  {
    iupac              .choice: ,
    iupac / restricted .code:n     =
      \bool_set_true:N \l__chemmacros_iupac_restricted_bool
      \bool_set_false:N \l__chemmacros_iupac_strict_bool ,
    iupac / auto       .code:n     =
      \bool_set_false:N \l__chemmacros_iupac_restricted_bool
      \bool_set_false:N \l__chemmacros_iupac_strict_bool ,
    iupac / strict     .code:n     =
      \bool_set_false:N \l__chemmacros_iupac_restricted_bool
      \bool_set_true:N \l__chemmacros_iupac_strict_bool ,
  }

% --------------------------------------------------------------------------
% stereo descriptors and other nomenclature commands
% Cahn-Ingold-Prelog
\dim_new:N  \l__chemmacros_cip_kern_dim
\dim_set:Nn \l__chemmacros_cip_kern_dim {.075em}

\chemmacros_define_keys:nn {nomenclature}
  { cip-kern .dim_set:N = \l__chemmacros_cip_kern_dim }

\ChemCompatibilityTo{5.2}
\cs_new_protected:Npn \chemmacros_cip:n #1
  { \textit{#1} \tex_kern:D \l__chemmacros_cip_kern_dim }

\NewDocumentCommand \cip {m}
  { \chemmacros_cip:n {#1} }
\EndChemCompatibility

\ChemCompatibilityBetween{5.2}{5.8}
\cs_new_protected:Npn \chemmacros_cip:n #1
  {
    \int_zero:N \l__chemmacros_tmpa_int
    \textup{(}
    \clist_map_inline:nn
      {#1}
      {
        \int_incr:N \l__chemmacros_tmpa_int
        \textit{##1}
        \int_compare:nT
          { \l__chemmacros_tmpa_int < \clist_count:n {#1} }
          { \textup{,} }
      }
    \textup{)}
    \tex_kern:D \l__chemmacros_cip_kern_dim
  }
\DeclareChemIUPAC \cip { \chemmacros_cip:n }
\EndChemCompatibility

\ChemCompatibilityFrom{5.8}
\tl_new:N \l__chemmacros_cip_inner_tl
\tl_new:N \l__chemmacros_cip_outer_tl
\tl_new:N \l__chemmacros_cip_number_tl

\chemmacros_define_keys:nn {nomenclature}
  {
    cip-inner-format  .tl_set:N  = \l__chemmacros_cip_inner_tl ,
    cip-inner-format  .initial:n = \itshape ,
    cip-outer-format  .tl_set:N  = \l__chemmacros_cip_outer_tl ,
    cip-outer-format  .initial:n = \upshape ,
    cip-number-format .tl_set:N  = \l__chemmacros_cip_number_tl ,
    cip-number-format .initial:n = \itshape
  }
  
\cs_new_protected:Npn \__chemmacros_cip:n #1
  {
    \tl_set:Nn \l__chemmacros_tmpa_tl {#1}
    \int_step_inline:nnnn {0} {1} {9}
      {
        \tl_replace_all:Nnn \l__chemmacros_tmpa_tl
          {##1}
          { { \l__chemmacros_cip_number_tl ##1} }
      }
    { \l__chemmacros_cip_inner_tl \l__chemmacros_tmpa_tl }
  }

\cs_new_protected:Npn \chemmacros_cip:n #1
  {
    \int_zero:N \l__chemmacros_tmpa_int
    \group_begin:
      \l__chemmacros_cip_outer_tl (
      \clist_map_inline:nn
        {#1}
        {
          \int_incr:N \l__chemmacros_tmpa_int
          \__chemmacros_cip:n {##1}
          \int_compare:nT
            { \l__chemmacros_tmpa_int < \clist_count:n {#1} }
            { , }
        }
      )
    \group_end:
    \tex_kern:D \l__chemmacros_cip_kern_dim
  }
\DeclareChemIUPAC \cip { \chemmacros_cip:n }
\EndChemCompatibility

% remember: TikZ needs : to be other
\cs_new_protected:Npn \chemmacros_sconf:n #1
  {
    \group_begin:
      \chemmacros_tikz:nn
        { baseline,text~ height=1.5ex,text~ depth=.25ex }
        {
          \chemmacros_tikz_node:n {anchor=base} (chemmacros@@Sconf) {#1} ;
          \chemmacros_tikz_draw:n {->,thick,rotate=90}
            ($(chemmacros@@Sconf.center)+(20\c_chemmacros_other_colon_tl.8em)$)
            arc (20\c_chemmacros_other_colon_tl340\c_chemmacros_other_colon_tl.8em);
        }
    \group_end:
  }

\cs_new_protected:Npn \chemmacros_rconf:n #1
  {
    \group_begin:
      \tikz[baseline,text~ height=1.5ex,text~ depth=.25ex]
        {
          \chemmacros_tikz_node:n {anchor=base} (chemmacros@@Rconf) {#1} ;
          \chemmacros_tikz_draw:n {<-,thick,rotate=90}
            ($(chemmacros@@Rconf.center)+(20\c_chemmacros_other_colon_tl.8em)$)
            arc (20\c_chemmacros_other_colon_tl340\c_chemmacros_other_colon_tl.8em) ;
        }
    \group_end:
  }

\NewDocumentCommand \Sconf { O{S} } { \chemmacros_sconf:n {#1} }
\NewDocumentCommand \Rconf { O{R} } { \chemmacros_rconf:n {#1} }

% coordination chemistry
\bool_new:N \l__chemmacros_bridge_super_bool

\chemmacros_define_keys:nn {nomenclature}
  {
    bridge-number         .choice: ,
    bridge-number / sub   .code:n     =
      \bool_set_false:N \l__chemmacros_bridge_super_bool ,
    bridge-number / super .code:n     =
      \bool_set_true:N \l__chemmacros_bridge_super_bool ,
    coord-use-hyphen      .bool_set:N = \l__chemmacros_coord_use_hyphen_bool ,
    coord-use-hyphen      .initial:n  = true
  }

% #1: boolean - if true hyphen is turned on
% #2: boolean - if true superscript will be used
% #3: symbol
% #4: sub-/superscript
\cs_new_protected:Npn \chemmacros_coordination_symbol:nnnn #1#2#3#4
  {
    \chemmacros_chemformula:n {#3}
    \tl_if_blank:nF {#4}
      {
        \bool_if:nTF {#2}
          { \chemmacros_chemformula:n { {}^{#4} } }
          { \chemmacros_chemformula:n { {}_{#4} } }
      }
    \bool_if:nT
      {#1}
      { \chemmacros_break_point_hyphen: }
  }

\cs_new_protected:Npn \chemmacros_hapto:n #1
  {
    \chemmacros_coordination_symbol:nnnn
      { \l__chemmacros_coord_use_hyphen_bool }
      {
        \chemmacros_if_compatibility:nnTF {>} {5.7}
          { \c_true_bool }
          { \c_false_bool }
      }
      { \chemeta }
      {#1}
  }

\cs_new_protected:Npn \chemmacros_dento:n #1
  {
    \chemmacros_coordination_symbol:nnnn
      { \l__chemmacros_coord_use_hyphen_bool }
      {
        \chemmacros_if_compatibility:nnTF {>} {5.7}
          { \c_true_bool }
          { \c_false_bool }
      }
      { \chemkappa }
      {#1}
  }

\cs_new_protected:Npn \chemmacros_bridge:n #1
  {
    \chemmacros_coordination_symbol:nnnn
      { \l__chemmacros_coord_use_hyphen_bool }
      { \l__chemmacros_bridge_super_bool }
      { \chemmu }
      {#1}
  }

% \iupac (basically the same as bpchem's \IUPAC)
% - allows multiple breaking points as compound names can get really long and
%   especially in multicolumn documents can span more than two lines
% - add a (very) little space before the hyphen and a little negative space
%   after it
% - add a little space at breaking points if not broken
% - enables all naming commands regardless if they're definied otherwise or not
\cs_new_protected:Npn \chemmacros_allow_hyphens:
  {
    \chemmacros_nobreak:
    \chemmacros_if_compatibility:nnTF {<=} {5.7}
      { \skip_horizontal:N \c_zero_dim }
      { \skip_horizontal:N \c_zero_skip }
  }

% #1: pre break
% #2: post break
% #3: no break
\ChemCompatibilityTo{5.1}
\cs_new_protected:Npn \__chemmacros_break_point_insert:nnn #1#2#3
  {
    \chemmacros_nobreak:
    \tex_discretionary:D { - } { } {#3}
    \chemmacros_allow_hyphens:
  }
\EndChemCompatibility

\ChemCompatibilityBetween{5.1}{5.8}
\cs_new_protected:Npn \__chemmacros_break_point_insert:nnn #1#2#3
  {
    \mode_if_math:TF
      {#3}
      {      
        \chemmacros_nobreak:
        \tex_discretionary:D {#1} {#2} {#3}
        \chemmacros_allow_hyphens:
      }
  }
\EndChemCompatibility

\ChemCompatibility{5.8a}
\cs_new_protected:Npn \__chemmacros_break_point_insert:nnn #1#2#3
  {
    \mode_if_math:TF
      {#3}
      {
        \peek_meaning:NTF \group_end:
          {#3}
          {
            \chemmacros_nobreak:
            \tex_discretionary:D {#1} {#2} {#3}
            \chemmacros_allow_hyphens:
          }
      }
  }
\EndChemCompatibility

\ChemCompatibilityFrom{5.8b}
\cs_new_protected:Npn \__chemmacros_peek_if_dash:TF #1#2
  {
    \peek_charcode:NTF -
      {#1}
      {#2}
  }

\cs_new_protected:Npn \__chemmacros_peek_if_paren:TF #1#2
  { % (
    \peek_charcode:NTF )
      {#1}
      { % [
        \peek_charcode:NTF ]
          {#1}
          {#2}
      }
  }
  
\cs_new_protected:Npn \__chemmacros_break_point_insert:nnn #1#2#3
  {
    \mode_if_math:TF
      {#3}
      {
        \peek_meaning:NTF \group_end:
          {#3}
          {
            \__chemmacros_peek_if_dash:TF
              {#3}
              {
                \__chemmacros_peek_if_paren:TF
                  {#3}
                  {
                    \chemmacros_nobreak:
                    \tex_discretionary:D {#1} {#2} {#3}
                    \chemmacros_allow_hyphens:
                  }
              }
          }
      }
  }
\EndChemCompatibility

\dim_new:N  \l__chemmacros_iupac_hyphen_pre_dim
\dim_set:Nn \l__chemmacros_iupac_hyphen_pre_dim { .01em }
\dim_new:N  \l__chemmacros_iupac_hyphen_post_dim
\dim_set:Nn \l__chemmacros_iupac_hyphen_post_dim { -.03em }
\dim_new:N  \l__chemmacros_iupac_break_dim
\dim_set:Nn \l__chemmacros_iupac_break_dim { .03em }
\skip_new:N  \l__chemmacros_iupac_break_skip
\skip_set:Nn \l__chemmacros_iupac_break_skip { .03em plus .07em }

\chemmacros_define_keys:nn {nomenclature}
  {
    hyphen-pre-space  .dim_set:N = \l__chemmacros_iupac_hyphen_pre_dim ,
    hyphen-post-space .dim_set:N = \l__chemmacros_iupac_hyphen_post_dim ,
    break-space       .skip_set:N = \l__chemmacros_iupac_break_skip
  }

\ChemCompatibilityTo{5.2}
\cs_new_protected:Npn \chemmacros_break_point_hyphen:
  {
    \__chemmacros_break_point_insert:nnn
      {-}
      { }
      {
        \tex_kern:D \l__chemmacros_iupac_hyphen_pre_dim
        -
        \tex_kern:D \l__chemmacros_iupac_hyphen_post_dim
      }
  }
\EndChemCompatibility

\ChemCompatibilityFrom{5.2}
\cs_new_protected:Npn \chemmacros_break_point_hyphen:
  {
    \__chemmacros_break_point_insert:nnn
      { \tex_kern:D \l__chemmacros_iupac_hyphen_pre_dim - }
      { }
      {
        \tex_kern:D \l__chemmacros_iupac_hyphen_pre_dim
        -
        \tex_kern:D \l__chemmacros_iupac_hyphen_post_dim
      }
  }
\EndChemCompatibility

\cs_new_protected:Npn \chemmacros_break_point:
  {
    \__chemmacros_break_point_insert:nnn
      {-}
      { }
      {
        \mode_if_math:TF
          { | }
          { \tex_kern:D \l__chemmacros_iupac_break_dim }
      }
  }
  
\cs_new_protected:Npn \chemmacros_break_point_opening_paren:
  { \__chemmacros_break_point_insert:nnn { - } { ( } { ( } }

\cs_new_protected:Npn \chemmacros_break_point_closing_paren:
  { \__chemmacros_break_point_insert:nnn { )- } { } { ) } }

\cs_new_protected:Npn \chemmacros_break_point_opening_bracket:
  { \__chemmacros_break_point_insert:nnn { - } { [ } { [ } }

\cs_new_protected:Npn \chemmacros_break_point_closing_bracket:
  { \__chemmacros_break_point_insert:nnn { ]- } { } { ] } }
  
\cs_new_protected:Npn \chemmacros_superscript:n #1
  { \mode_if_math:TF { \sp {#1} } { \textsuperscript {#1} } }
  
\bool_new:N \l__chemmacros_inside_iupac_bool

\seq_new:N  \l__chemmacros_iupac_shorthands_seq
\prop_new:N \l__chemmacros_iupac_shorthands_prop

% #1: token
% #2: definition
\cs_new_protected:Npn \chemmacros_define_iupac_shorthand:NN #1#2
  {
    \seq_if_in:NnF \l__chemmacros_iupac_shorthands_seq {#1}
      { \seq_put_right:Nn \l__chemmacros_iupac_shorthands_seq {#1} }
    \prop_put:Nnn \l__chemmacros_iupac_shorthands_prop {#1}
      { \char_set_active_eq:NN #1#2 }
  }

\cs_new_protected:Npn \chemmacros_remove_shorthand:N #1
  {
    \chemmacros_define_iupac_shorthand:NN #1 \c_empty_tl
    \seq_remove_all:Nn \l__chemmacros_iupac_shorthands_seq {#1}
    \prop_remove:Nn \l__chemmacros_iupac_shorthands_prop {#1}
  }

\prg_new_conditional:Npnn \chemmacros_if_iupac_shorthand:N #1 {p,T,F,TF}
  {
    \seq_if_in:NnTF \l__chemmacros_iupac_shorthands_seq {#1}
      { \prg_return_true: }
      { \prg_return_false: }
  }

\cs_new_protected:Npn \chemmacros_for_all_iupac_shorthands_do:n #1
  { \seq_map_inline:Nn \l__chemmacros_iupac_shorthands_seq {#1} }

\cs_new_protected:Npn \chemmacros_activate_iupac_shorthand:N #1
  { \chemmacros_if_iupac_shorthand:NT #1 { \char_set_catcode_active:N #1 } }

\cs_new_protected:Npn \chemmacros_activate_iupac_shorthands:n #1
  { \tl_map_inline:nn {#1} { \chemmacros_activate_iupac_shorthand:N ##1 } }

\cs_new_protected:Npn \chemmacros_activate_all_iupac_shorthands:
  {
    \chemmacros_for_all_iupac_shorthands_do:n
      { \chemmacros_activate_iupac_shorthand:N ##1 }
  }

\cs_new_protected:Npn \chemmacros_make_shorthands:
  {
    \chemmacros_if_compatibility:nnT {>=} {5.2}
      {
        \chemmacros_if_document:T
          {
            \chemmacros_for_all_iupac_shorthands_do:n
              { \prop_item:Nn \l__chemmacros_iupac_shorthands_prop {##1} }
          }
      }
  }

\NewDocumentCommand \ChemMakeIupacShorthands {}
  { \chemmacros_make_shorthands: }

\NewDocumentCommand \DeclareChemIUPACShorthand {mm} 
  { \chemmacros_define_iupac_shorthand:NN #1#2 }

\NewDocumentCommand \NewChemIUPACShorthand {mm} 
  {
    \chemmacros_if_iupac_shorthand:NTF #1
      { \msg_error:nnn {chemmacros} {new-iupac-shorthand} {#1} }
      { \chemmacros_define_iupac_shorthand:NN #1#2 }
  }

\NewDocumentCommand \ProvideChemIUPACShorthand {mm} 
  {
    \chemmacros_if_iupac_shorthand:NTF #1
      { \chemmacros_define_iupac_shorthand:NN #1#2 }
  }

\NewDocumentCommand \RenewChemIUPACShorthand {mm} 
  {
    \chemmacros_if_iupac_shorthand:NTF #1
      { \chemmacros_define_iupac_shorthand:NN #1#2 }
      { \msg_error:nnn {chemmacros} {renew-iupac-shorthand} {#1} }
  }

\NewDocumentCommand \RemoveChemIUPACShorthand {m} 
  { \chemmacros_remove_shorthand:N #1 }

\NewChemIUPACShorthand ^ \chemmacros_superscript:n
\NewChemIUPACShorthand | \chemmacros_break_point:
\NewChemIUPACShorthand - \chemmacros_break_point_hyphen:

\ChemCompatibilityFrom{5.1}
  \NewChemIUPACShorthand ( \chemmacros_break_point_opening_paren:
  \NewChemIUPACShorthand ) \chemmacros_break_point_closing_paren:
  \NewChemIUPACShorthand [ \chemmacros_break_point_opening_bracket:
  \NewChemIUPACShorthand ] \chemmacros_break_point_closing_bracket:
\EndChemCompatibility

\ChemCompatibilityFrom{5.3}
  \NewChemIUPAC \chemprime { \ensuremath {{}^{\prime}} }
  \NewChemIUPACShorthand ' \chemprime
\EndChemCompatibility

\ChemCompatibilityFrom{5.8c}
  \NewChemIUPAC \nonbreakinghyphen { \mbox{-} \nobreak \hspace{0pt} }
  \NewChemIUPACShorthand = \nonbreakinghyphen
\EndChemCompatibility

\cs_new_protected:Npn \chemmacros_iupac:nn #1#2
  {
    \group_begin:
      \bool_set_true:N \l__chemmacros_inside_iupac_bool
      \chemmacros_set_keys:nn {nomenclature} {#1}
      \chemmacros_make_iupac:
      \chemmacros_make_shorthands:
      % \prop_show:N \l__chemmacros_iupac_shorthands_prop
      \chemmacros_if_compatibility:nnT {<=} {5.2}
        { \chemmacros_define_deprecated:NN \- - }
      \tl_set_rescan:Nnn \l__chemmacros_tmpa_tl
        { \chemmacros_activate_all_iupac_shorthands: }
        {#2}
      % \tl_analysis_show:N
      \l__chemmacros_tmpa_tl
    \group_end:
  }

\cs_new_protected:Npn \chemmacros_iupac:n #1 { \chemmacros_iupac:nn {} {#1} }

\ChemCompatibilityTo{5.3}
% Thanks to Joseph Wright and Enrico Gregorio for the help on the curious
% redefinition of \- and the end of the compilation
% see http://tex.stackexchange.com/q/42405/5049 for reference
\cs_set_protected:Npx \- { \exp_not:o { \- } }
\cs_set_eq:NN \@dischyph \-
\EndChemCompatibility

\NewDocumentCommand \iupac {O{}m} { \chemmacros_iupac:nn {#1} {#2} }

% --------------------------------------------------------------------------
% R(ectus)/S(inister):
\NewChemIUPAC \rectus   { \cip {R} }
\NewChemIUPAC \sinister { \cip {S} }

\LetChemIUPAC \R \rectus
\LetChemIUPAC \S \sinister

% E(ntgegen)/Z(usammen)
\NewChemIUPAC \entgegen { \cip {E} }
\NewChemIUPAC \zusammen { \cip {Z} }

\LetChemIUPAC \E \entgegen
\LetChemIUPAC \Z \zusammen

% cis/trans
\NewChemIUPAC \cis   { \textit {cis} }
\NewChemIUPAC \trans { \textit {trans} }

% fac/mer
\NewChemIUPAC \fac { \textit {fac} }
\NewChemIUPAC \mer { \textit {mer} }

% tert
\NewChemIUPAC \tert  { \textit {tert} }

% Italian descriptors - I forgot what they mean
\NewChemIUPAC \sin { \textit {sin} }
\NewChemIUPAC \ter { \textit {ter} }

% Fischer
\NewChemIUPAC \dexter { \textsc {d} }
\NewChemIUPAC \laevus { \textsc {l} }

\LetChemIUPAC \D \dexter
\LetChemIUPAC \L \laevus

% ortho/meta/para
\NewChemIUPAC \ortho { \textit {o} }
\NewChemIUPAC \meta  { \textit {m} }
\NewChemIUPAC \para  { \textit {p} }

% syn/anti
\NewChemIUPAC \syn  { \textit {syn} }
\NewChemIUPAC \anti { \textit {anti} }

% coordination chemistry:
\NewChemIUPAC \hapto  { \chemmacros_hapto:n }
\NewChemIUPAC \dento  { \chemmacros_dento:n }
\NewChemIUPAC \bridge { \chemmacros_bridge:n }

% attachments to heteroatoms / added hydrogen
\NewChemIUPAC \hydrogen   { \textit {H} }
\NewChemIUPAC \oxygen     { \textit {O} }
\NewChemIUPAC \nitrogen   { \textit {N} }
\NewChemIUPAC \sulfur     { \textit {S} }
\NewChemIUPAC \phosphorus { \textit {P} }

% atoms:
\LetChemIUPAC \H  \hydrogen
\LetChemIUPAC \O  \oxygen
\LetChemIUPAC \N  \nitrogen
\LetChemIUPAC \Sf \sulfur
\LetChemIUPAC \P  \phosphorus

% greek letters
\NewChemIUPAC \a { \chemalpha }
\NewChemIUPAC \b { \chembeta }
\NewChemIUPAC \g { \chemgamma }
\NewChemIUPAC \d { \chemdelta }
\NewChemIUPAC \k { \chemkappa }
\NewChemIUPAC \m { \chemmu }
\NewChemIUPAC \n { \chemeta }
\NewChemIUPAC \w { \chemomega }

% --------------------------------------------------------------------------
% latin phrases
\tl_new:N   \l__chemmacros_latin_format_tl
  
\NewChemMacroset {Latin} {mm}
  { \chemmacros_define_latin:Nn #1 {#2} }

\ChemCompatibilityTo{5.7}
\chemmacros_define_keys:nn {nomenclature}
  {
    latin-format .tl_set:N  = \l__chemmacros_latin_format_tl ,
    latin-format .initial:n = \textit ,
  }

\cs_new_protected:Npn \chemmacros_latin:n #1
  { { \l__chemmacros_latin_format_tl {#1} } }
  
\cs_new_protected:Npn \chemmacros_define_latin:Nn #1#2
  { \cs_set_protected:Npn #1 { \chemmacros_latin:n {#2} } }
  
\AtBeginDocument
  {
    % if chemstyle is loaded use chemstyle's definition:
    \chemmacros_if_package_loaded:nT {chemstyle}
      {
        \AfterPackage* {chemstyle}
          {
            \cs_undefine:N \invacuo
            \cs_set_eq:NN \chemmacros_latin:n \cst@latin
          }
      }
    \cs_if_exist:NTF \latin
      { \cs_set_eq:NN  \latin \chemmacros_latin:n }
      { \cs_new_eq:NN  \latin \chemmacros_latin:n }
    \NewChemLatin \insitu   {in~situ}
    \NewChemLatin \abinitio {ab~initio}
    \NewChemLatin \invacuo  {in~vacuo}
  }
\EndChemCompatibility
  
\ChemCompatibilityFrom{5.7}
\chemmacros_define_keys:nn {nomenclature}
  {
    format .tl_set:N  = \l__chemmacros_latin_format_tl ,
    format .initial:n = \emph ,
  }
  
\cs_new_protected:Npn \chemmacros_write_latin:n #1
  {
    \group_begin:
      \l__chemmacros_latin_format_tl {#1}
    \group_end:
  }

\cs_new_protected:Npn \chemmacros_latin:n #1
  { \chemmacros_write_latin:n {#1} }

\cs_new_protected:Npn \chemmacros_define_latin:Nn #1#2
  { \DeclareDocumentCommand #1 {O{}} { \latin [##1] {#2} } }

\NewDocumentCommand \latin {O{}m}
  {
    \group_begin:
      \chemmacros_set_keys:nn {nomenclature} {#1}
      \chemmacros_latin:n {#2}
    \group_end:
  }
\EndChemCompatibility

\ChemCompatibilityBetween{5.7}{5.9}
\AtBeginDocument
  {
    \AfterPackage* {chemstyle} { \cs_undefine:N \invacuo }
    \NewChemLatin \insitu   {in~situ}
    \NewChemLatin \abinitio {ab~initio}
    \NewChemLatin \invacuo  {in~vacuo}
  }
\EndChemCompatibility

\ChemCompatibilityFrom{5.9}
\cs_undefine:N \invacuo
\NewChemLatin \invacuo  {in~vacuo}
\NewChemLatin \abinitio {ab~initio}
\NewChemLatin \insitu   {in~situ}
\EndChemCompatibility

% --------------------------------------------------------------------------
% hyperref support
% ?? unsure about adding IUPAC commands
\AfterPackage* {hyperref}
  {
    \pdfstringdefDisableCommands
      {
        % \cs_set:Npn \- { - } % maybe not a good idea...
        % \cs_set:Npn \| { }
        % \cs_set:Npn \pH { pH }
        % \cs_set:Npn \pOH { pOH }
        \cs_set:Npn \iupac #1 {#1}
        \cs_set:Npn \cip   #1 {(#1)}
        \cs_set:Npn \cis   {cis}
        \cs_set:Npn \trans {trans}
        \cs_set:Npn \tert  {tert}
        \cs_set:Npn \ortho {o}
        \cs_set:Npn \meta  {m}
        \cs_set:Npn \para  {p}
        \cs_set:Npn \syn   {syn}
        \cs_set:Npn \anti  {anti}
        \cs_set:Npn \R     {(R)}
        \cs_set:Npn \S     {(S)}
        \cs_set:Npn \E     {(E)}
        \cs_set:Npn \Z     {(Z)}
        \cs_set:Npn \D     {D}
        \cs_set:Npn \L     {L}
        \cs_set:Npn \H     {H}
        \cs_set:Npn \O     {O}
        \cs_set:Npn \N     {N}
        \cs_set:Npn \Sf    {S}
        \cs_set:Npn \P     {P}
        \cs_set:Npn \dexter     {D}
        \cs_set:Npn \laevus     {L}
        \cs_set:Npn \rectus     {(R)}
        \cs_set:Npn \sinister   {(S)}
        \cs_set:Npn \entgegen   {(E)}
        \cs_set:Npn \zusammen   {(Z)}
        \cs_set:Npn \hydrogen   {H}
        \cs_set:Npn \oxygen     {O}
        \cs_set:Npn \nitrogen   {N}
        \cs_set:Npn \sulfur     {S}
        \cs_set:Npn \phosphorus {P}
        \cs_set:Npn \chemprime  {'}
      }
  }
  
% --------------------------------------------------------------------------
\file_input_stop:

2015/10/01 - use new function \chemmacros_if_document:TF for
             \chemmacros_make_iupac:
           - \cip only italicizes elements of csv list
           - fix bug in \RemoveChemIUPACShorthand
2016/01/02 - \chemprime
2016/01/23 - give shorthand definitions only inside \iupac
2016/01/25 - small improvements (?) of the shorthand code
2016/05/08 - check if \latin is already defined (eg by achemso) and overwrite
             definition
2016/05/10 - define \chemmacros_hapto:n and friends in terms of
             \chemmacros_coordination_symbol:nnnn
           - \iupac gets an optional argument for setting options
2016/05/31 - overwrite any \latin definition -- or rather: just define \latin
           - \latin and all macros defined with \NewChemLatin get an optional
             argument for setting options
2016/06/07 - rename functions which erroneously were assigned to the
             `chemformula' module
2017/04/17 - fix erroneous definition of \chemmacros_allow_hyphens:
           - new options `cip-inner-format', `cip-outer-format' and
             `cip-number-format'
2017/06/11 - change behaviour of \__chemmacros_break_point_insert:nnn so
             it does nothing as last token in \iupac
2020/01/02 - \chemmacros_show_iupac:N
           - \chemmacros_if_iupac:NTF
