% !TeX encoding=UTF-8
%% The LaTeX package sanitize-umlaut - version 1.3.0 (2023/05/15)
%% sanitize-umlaut.sty: Sanitize umlauts for makeindex
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2016-2023 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% 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
%%   https://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{sanitize-umlaut}[2023/05/15 version 1.3.0 sanitize umlauts]

\def\sanitize@unicode@char#1#2{%
  \@namedef{u8:\detokenize{#1}}{#2}%
}


% BEGIN: patch for 'utf8.def'
\long\def\sanitize@umlaut@UTFviii@two@octets{%
  \ifincsname
    \expandafter \UTF@two@octets@string
  \fi
  \UTFviii@two@octets@combine
}

\begingroup
\catcode`\~13
\catcode`\"12
\gdef\sanitize@umlaut@local@UTFviii@redefinition{%
  \def\UTFviii@tmp{\edef~{\noexpand\sanitize@umlaut@UTFviii@two@octets\noexpand~}}%
  \uccode`\~"C3%
  \uppercase\expandafter{\UTFviii@tmp}%
}
\endgroup
% END: patch for 'utf8.def'


\begingroup
\catcode`\"=12

\gdef\sanitize@umlaut@utfviii{%
  \sanitize@umlaut@local@UTFviii@redefinition%
  \sanitize@unicode@char{^^c3^^a4}{"a}%
  \sanitize@unicode@char{^^c3^^b6}{"o}%
  \sanitize@unicode@char{^^c3^^bc}{"u}%
  \sanitize@unicode@char{^^c3^^84}{"A}%
  \sanitize@unicode@char{^^c3^^96}{"O}%
  \sanitize@unicode@char{^^c3^^9c}{"U}%
  \sanitize@unicode@char{^^c3^^9f}{"s}%
}

\gdef\sanitize@umlaut@utfviii@old{%
  \sanitize@unicode@char{^^c3^^a4}{"a}%
  \sanitize@unicode@char{^^c3^^b6}{"o}%
  \sanitize@unicode@char{^^c3^^bc}{"u}%
  \sanitize@unicode@char{^^c3^^84}{"A}%
  \sanitize@unicode@char{^^c3^^96}{"O}%
  \sanitize@unicode@char{^^c3^^9c}{"U}%
  \sanitize@unicode@char{^^c3^^9f}{"s}%
}

\gdef\sanitize@umlaut@latini{%
  \DeclareInputText{228}{"a}%
  \DeclareInputText{246}{"o}%
  \DeclareInputText{252}{"u}%
  \DeclareInputText{196}{"A}%
  \DeclareInputText{214}{"O}%
  \DeclareInputText{220}{"U}%
  \DeclareInputText{223}{"s}%
}

\gdef\sanitize@umlaut@applemac{%
  \DeclareInputText{138}{"a}%
  \DeclareInputText{154}{"o}%
  \DeclareInputText{159}{"u}%
  \DeclareInputText{128}{"A}%
  \DeclareInputText{133}{"O}%
  \DeclareInputText{134}{"U}%
  \DeclareInputText{167}{"s}%
}

\endgroup


\ifdefined\inputencodingname
  \def\@tempa{utf8}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@utfviii
    \PackageInfo{sanitize-umlaut}{encoding utf8 selected}
  \else\def\@tempa{utf8-2018}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@utfviii@old
    \PackageInfo{sanitize-umlaut}{encoding utf8-2018 selected}
  \else\def\@tempa{latin1}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@latini
    \PackageInfo{sanitize-umlaut}{encoding latin1 selected}
  \else\def\@tempa{ansinew}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@latini
    \PackageInfo{sanitize-umlaut}{encoding latin1 selected}
  \else\def\@tempa{cp1252}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@latini
    \PackageInfo{sanitize-umlaut}{encoding latin1 selected}
  \else\def\@tempa{applemac}\ifx\inputencodingname\@tempa
    \let\sanitize@umlaut\sanitize@umlaut@applemac
    \PackageInfo{sanitize-umlaut}{encoding applemac selected}
  \else
    \def\sanitize@umlaut{}%
    \PackageWarning{sanitize-umlaut}{encoding \inputencodingname\space is not supported}
  \fi\fi\fi\fi\fi\fi
  \AtBeginDocument{\edef\@sanitize{\unexpanded\expandafter{\@sanitize}\unexpanded\expandafter{\sanitize@umlaut}}}
\else
  \ExplSyntaxOn

  \bool_lazy_or:nnTF { \sys_if_engine_luatex_p: }{ \sys_if_engine_xetex_p: }
    {
      \cs_new_protected_nopar:Npn \__sanuml_index:n #1
        {
          \__sanuml_index_original:w { #1 }
        }

      \cs_new_protected_nopar:Npn \__sanuml_index:nn #1#2
        {
          \__sanuml_index_original:w [ #1 ] { #2 }
        }

      \PackageInfo{sanitize-umlaut}{use~LaTeX3~token~replacement~for~\c_sys_engine_str}
      \AddToHook{begindocument}
      {
        \cs_set_eq:NN \__sanuml_index_original:w \index

        \RenewDocumentCommand \index { o m }
          {
            \tl_set:Nn \l_tmpa_tl { #2 }
            \tl_replace_all:Nnn \l_tmpa_tl { ä }{ "a }
            \tl_replace_all:Nnn \l_tmpa_tl { ö }{ "o }
            \tl_replace_all:Nnn \l_tmpa_tl { ü }{ "u }
            \tl_replace_all:Nnn \l_tmpa_tl { Ä }{ "A }
            \tl_replace_all:Nnn \l_tmpa_tl { Ö }{ "O }
            \tl_replace_all:Nnn \l_tmpa_tl { Ü }{ "U }
            \tl_replace_all:Nnn \l_tmpa_tl { ß }{ "s }
            \IfNoValueTF {#1}
              {
                \exp_args:NV \__sanuml_index:n \l_tmpa_tl
              }
              {
                \exp_args:NnV \__sanuml_index:nn {#1} \l_tmpa_tl
              }
          }
      }
    }
    {
      \PackageWarning{sanitize-umlaut}{no~input~encoding~detected.~utf8~is~selected~as~fallback~(possibly~nonsense)}
      \let\sanitize@umlaut\sanitize@umlaut@utfviii
      \AtBeginDocument{\edef\@sanitize{\unexpanded\expandafter{\@sanitize}\unexpanded\expandafter{\sanitize@umlaut}}}
    }

  \ExplSyntaxOff
\fi

