%%
%% This is file `lua-ul.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lua-ul.dtx  (with options: `package')
%% 
%% Copyright (C) 2020-2022 by Marcel Krueger
%%
%% This file 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 2005/12/01 or later.
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage
  {lua-ul}
  {2022-05-31}{0.2.0}{Underlining and related functionality for LuaTeX}
\msg_new:nnnn {lua-ul} {luatex-required} {LuaLaTeX~required} {
  Lua-UL~requires~LuaLaTeX.~
  Maybe~you~forgot~to~switch~the~engine~in~your~editor?
}
\msg_new:nnnn {lua-ul} {luacolor-required} {
  Color~support~in~lua-ul~requires~luacolor.
}{
  This~error~can~be~fixed~by~adding~\token_to_str:N \usepackage{luacolor}~in~your~preamble.
}
\sys_if_engine_luatex:F {
  \msg_error:nn {lua-ul} {luatex-required}
}
\lua_load_module:n {lua-ul}
\bool_new:N \l__luaul_predefined_bool
\bool_new:N \l__luaul_soulnames_bool
\bool_set_true:N \l__luaul_predefined_bool
\DeclareOption {minimal} { \bool_set_false:N \l__luaul_predefined_bool }
\DeclareOption {soul} { \bool_set_true:N \l__luaul_soulnames_bool }
\ProcessOptions\relax
\cs_new_protected:Npn \__luaul_maybe_define_use:Nx #1#2 {
  \cs_if_free:NT #1 {
    \cs_gset:Npx #1 {#2}
  }
  #1
}
\cs_generate_variant:Nn \__luaul_maybe_define_use:Nx {c}
\cs_new:Npn \__luaul_default_context: {
  \dim_to_decimal_in_sp:n {1ex}
  @
  \cs_if_exist:NT \LuaCol@Attribute {
    \int_use:N \LuaCol@Attribute
  }
}
\NewDocumentCommand \newunderlinetype
    { E{*}{{}} m O{\__luaul_default_context:} m } {
  \cs_new_protected:Npn #2 {
    \__luaul_maybe_define_use:cx {
      __luaul_set_underline_ \cs_to_str:N #2 _ #3 :
    } {
      \LuaULSetUnderline
        \LuaULNewUnderlineType #1 { #4 \hskip 0pt }
    }
  }
}

\bool_if:NT \l__luaul_predefined_bool {
  \msg_new:nnnn {lua-ul} {height-inconsistent} {Specified~height~is~inconsistent~with~specified~top~and~bottom}{}
  \msg_new:nnnn {lua-ul} {deprecated} {Used~deprecated~command~`\token_to_str:N #1'.~Please~replace~this~with~`\token_to_str:N #2`.}{}
  \cs_new:Npn \__luaul_deprecated:NN #1#2 {
    \cs_new_protected:Npn #1 {
      \msg_warning:nnnn {lua-ul} {deprecated} {#1} {#2}
      \cs_gset_eq:NN #1 #2
      #1
    }
  }
  \tl_new:N \l__luaul_current_color_tl
  \cs_new:Npn \__luaul_set_color_strip:w \xcolor@ #1#2 {}
  \cs_new_protected:Npn \luaul_set_color:n #1 {
    \cs_if_free:NTF \XC@getcolor {
      \tl_set:Nn \l__luaul_current_color_tl {#1}
    } {
      \group_begin:
        \XC@getcolor {#1} \l_tmpa_tl
      \use:e { \group_end:
        \tl_set:Nn \exp_not:N \l__luaul_current_color_tl {
          \exp_after:wN \__luaul_set_color_strip:w \l_tmpa_tl
        }
      }
    }
  }
  \__luaul_deprecated:NN \luaul@setcolor \luaul_set_color:n
  \cs_new_protected:Npn \luaul_apply_color: {
    \IfPackageLoadedTF {luacolor} {
      \cs_if_free:NTF \XC@getcolor {
        \color {\l__luaul_current_color_tl}
      }{
        \exp_after:wN \XC@undeclaredcolor \l__luaul_current_color_tl
      }
    }{
      \msg_error:nn {lua-ul} {luacolor-required}
    }
  }
  \__luaul_deprecated:NN \luaul@applycolor \luaul_apply_color:
  \cs_new:Npn \luaul_context_color: {
    \l__luaul_current_color_tl
  }
  \cs_new:Npn \__luaul_default_colored_context: {
    \dim_to_decimal_in_sp:n {1ex}
    @
    \luaul_context_color:
  }
  \tl_new:N \l__luaul_highlight_color_tl
  \tl_set:Nn \l__luaul_highlight_color_tl {yellow}
  \NewDocumentCommand \LuaULSetHighLightColor {om} {
    \tl_set:Nx \l__luaul_highlight_color_tl {
      \IfValueTF {#1} {
        [#1]{#2}
      }{
        #2
      }
    }
  }

  \dim_new:N \l__luaul_top_dim
  \dim_new:N \l__luaul_bottom_dim
  \dim_new:N \l__luaul_height_dim
  \bool_new:N \l__luaul_top_set_bool
  \bool_new:N \l__luaul_bottom_set_bool
  \bool_new:N \l__luaul_height_set_bool
  \bool_new:N \l__luaul_color_set_bool

  \keys_define:nn {luaul / generic} {
    top .code:n = {
      \bool_set_true:N \l__luaul_top_set_bool
      \dim_set:Nn \l__luaul_top_dim {#1}
    },
    bottom .code:n = {
      \bool_set_true:N \l__luaul_bottom_set_bool
      \dim_set:Nn \l__luaul_bottom_dim {(#1)*-1}
    },
    height .code:n = {
      \bool_set_true:N \l__luaul_height_set_bool
      \dim_set:Nn \l__luaul_height_dim {#1}
    },
    color .code:n = {
      \bool_set_true:N \l__luaul_color_set_bool
      \luaul_set_color:n {#1}
    },
    textcolor .code:n = {
      \bool_set_false:N \l__luaul_color_set_bool
    },
    textcolor .value_forbidden:n = true,
  }

  \cs_new:Npn \__luaul_parse_generic_keys:n #1 {
    \bool_set_false:N \l__luaul_top_set_bool
    \bool_set_false:N \l__luaul_bottom_set_bool
    \bool_set_false:N \l__luaul_height_set_bool

    \keys_set:nn {luaul / generic} {#1}

    \bool_if:NTF \l__luaul_bottom_set_bool {
      \bool_if:NTF \l__luaul_top_set_bool {
        \bool_if:NTF \l__luaul_height_set_bool {
          \dim_compare:nNnF { \l__luaul_top_dim + \l__luaul_bottom_dim } = {\l__luaul_height_dim} {
            \msg_error:nn {lua-ul} {height-inconsistent}
          }
        } {
          \dim_set:Nn \l__luaul_height_dim { \l__luaul_top_dim + \l__luaul_bottom_dim }
        }
      } {
        \dim_set:Nn \l__luaul_top_dim { \l__luaul_height_dim - \l__luaul_bottom_dim }
      }
    } {
      \dim_set:Nn \l__luaul_bottom_dim { \l__luaul_height_dim - \l__luaul_top_dim }
    }
  }

  \newunderlinetype \@@underLine [
    \dim_to_decimal_in_sp:n { \l__luaul_top_dim }
    @
    \dim_to_decimal_in_sp:n { \l__luaul_bottom_dim }
    @
    \bool_if:NTF \l__luaul_color_set_bool {
      colored @ \luaul_context_color:
    } {
      \cs_if_exist:NT \LuaCol@Attribute {
        \int_use:N \LuaCol@Attribute
      }
    }
  ] {
    \bool_if:NT \l__luaul_color_set_bool {
      \luaul_apply_color:
    }
    \leaders\vrule height \l__luaul_top_dim depth \l__luaul_bottom_dim
  }
  \newunderlinetype \@basicUnderLine {
    \leaders \vrule height -.65ex depth .75ex
  }

  \NewDocumentCommand \@underLine {o} {
    \IfValueTF {#1} {
      \__luaul_parse_generic_keys:n {
        top = -.65ex,
        bottom = -.75ex,
      }
      \__luaul_parse_generic_keys:n {
        #1
      }
      \@@underLine
    } {
      \@basicUnderLine
    }
  }
  \NewDocumentCommand \underLine {o +m} {
    \group_begin:
    \@underLine [#1] #2
    \group_end:
  }

  \newunderlinetype \@@strikeThrough {
    \leaders \vrule height .55ex depth -.45ex
  }
  \newunderlinetype \@colored@strikeThrough [\__luaul_default_colored_context:] {
    \luaul_apply_color:
    \leaders\vrule height .55ex depth -.45ex
  }
  \NewDocumentCommand \@strikeThrough {o} {
    \IfValueTF {#1} {
      \luaul_set_color:n {#1}
      \@colored@strikeThrough
    }
    \@@strikeThrough
  }
  \NewDocumentCommand \strikeThrough {o +m} {
    \group_begin:
    \@strikeThrough #2
    \group_end:
  }

  \newunderlinetype \@@highLight [\__luaul_default_colored_context:] {
    \luaul_apply_color:
    \leaders\vrule height 1.75ex depth .75ex
  }
  \NewDocumentCommand \@highLight {O{\l__luaul_highlight_color_tl}} {
    \luaul_set_color:n {#1}
    \@@highLight
  }
  \NewDocumentCommand \highLight {O{\l__luaul_highlight_color_tl} +m} {
    \group_begin:
    \@highLight [#1] #2
    \group_end:
  }
  \bool_if:NT \l__luaul_soulnames_bool {
    \cs_new_eq:NN \textul \underLine     \cs_new_eq:NN \ul \textul
    \cs_new_eq:NN \textst \strikeThrough \cs_new_eq:NN \st \textst
    \cs_new_eq:NN \texthl \highLight     \cs_new_eq:NN \hl \texthl
  }
}
\ifx \reset@font \normalfont
  \let \reset@font \relax
  \DeclareRobustCommand \reset@font {%
    \normalfont
    \LuaULResetUnderline*%
  }
\else
  \MakeRobust \reset@font
  \begingroup
    \expandafter \let
        \expandafter \helper
        \csname reset@font \endcsname
  \expandafter \endgroup
  \expandafter \gdef
    \csname reset@font \expandafter \endcsname
  \expandafter {%
    \helper%
    \LuaULResetUnderline*%
  }
\fi
\output\expandafter\expandafter\expandafter{%
  \expandafter\expandafter\expandafter\LuaULResetUnderline
  \expandafter\expandafter\expandafter*%
  \expandafter\@firstofone\the\output%
}
%% 
%%
%% End of file `lua-ul.sty'.
