%%
%% This is file `lthooks.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lthooks.dtx  (with options: `2ekernel')
%% 
%% This is a generated file.
%% 
%% Copyright 2020 Frank Mittelbach
%% 
%% This file was generated from file(s) of the LaTeX `lthooks Bundle'.
%% --------------------------------------------------------------------------
%% 
%% It 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.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `lthooks Bundle'. You may however distribute the `lthooks Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/FrankMittelbach/fmitex/
%% 
%% where one can also log issues in case there are any.
%% 
%% 
%% File: lthooks.dtx (C) Copyright 2020 Frank Mittelbach, LaTeX Team
\providecommand\lthooksversion{v0.9b}
\providecommand\lthooksdate{2020/07/19}






\ExplSyntaxOn
\bool_new:N \g__hook_debug_bool
\cs_new_eq:NN \__hook_debug:n \use_none:n
\cs_new_protected:Npn \hook_debug_on:
  {
    \bool_gset_true:N \g__hook_debug_bool
    \__hook_debug_gset:
  }
\cs_new_protected:Npn \hook_debug_off:
  {
    \bool_gset_false:N \g__hook_debug_bool
    \__hook_debug_gset:
  }
\cs_new_protected:Npn \__hook_debug_gset:
  {
    \cs_gset_protected:Npx \__hook_debug:n ##1
      { \bool_if:NT \g__hook_debug_bool {##1} }
  }

\cs_new_eq:NN \__hook_str_compare:nn \__str_if_eq:nn
\tl_new:N \l__hook_return_tl
\tl_new:N \l__hook_tmpa_tl
\tl_new:N \l__hook_tmpb_tl
\seq_new:N \g__hook_all_seq
\tl_new:N \g__hook_removal_list_tl
\tl_new:N \l__hook_cur_hook_tl
\prop_new:N \g__hook_code_temp_prop
\tl_new:N \g__hook_hook_curr_name_tl
\seq_new:N \g__hook_name_stack_seq
\cs_new_eq:NN \__hook_tmp:w ?
\cs_generate_variant:Nn \tl_gremove_once:Nn { Nx }
\scan_new:N \s__hook_mark

\cs_new_protected:Npn \hook_new:n #1
  {
    \exp_args:Nx \__hook_new:n
      { \__hook_parse_label_default:nn {#1} { top-level } }
  }
\cs_new_protected:Npn \__hook_new:n #1 {
  \hook_if_exist:nTF {#1}
     { \ErrorHookExists }
     { \seq_gput_right:Nn \g__hook_all_seq {#1}
       \tl_new:c { g__hook_#1_code_tl }
       \__hook_declare:n {#1}
       \clist_new:c {g__hook_#1_labels_clist}
       \tl_new:c { g__hook_#1_reversed_tl }
       \__hook_provide_legacy_interface:n {#1}
     }
}

\cs_new_protected:Npn \__hook_declare:n #1
  {
    \__hook_if_exist:nF {#1}
      {
        \prop_new:c { g__hook_#1_code_prop }
        \tl_new:c { g__hook_#1_next_code_tl }
        \prop_new:c { g__hook_#1_rules_prop } % only for debugging
      }
  }

\cs_new_protected:Npn \hook_new_reversed:n #1 {
  \hook_new:n {#1}
  \tl_gset:cn { g__hook_#1_reversed_tl } { - }
}

\cs_new_protected:Npn \hook_new_pair:nn #1#2 {
  \hook_new:n {#1}  \hook_new_reversed:n {#2}
}

\cs_new_protected:Npn \__hook_provide_legacy_interface:n #1
  {
    \debug_suspend:
    \tl_if_exist:cT { @#1hook }
      {
        \tl_if_empty:cF { @#1hook }
          {
            \__hook_gput_code:nxv {#1}
              { \__hook_parse_label_default:Vn \c_novalue_tl { legacy } }
              { @#1hook }
          }
      }
    \tl_gset:co{@#1hook}{\cs:w g__hook_#1_code_tl\cs_end:}
    \debug_resume:
  }
\cs_new:Npn \__hook_parse_label_default:nn #1 #2
  {
    \tl_if_novalue:nTF {#1}
      { \__hook_currname_or_default:n {#2} }
      { \tl_trim_spaces_apply:nN {#1} \__hook_parse_dot_label:nn {#2} }
  }
\cs_generate_variant:Nn \__hook_parse_label_default:nn { V }
\cs_new:Npn \__hook_parse_dot_label:nn #1 #2
  {
    \tl_if_empty:nTF {#1}
      {
        \msg_expandable_error:nnn { hooks } { empty-label } {#2}
        #2
      }
      {
        \str_if_eq:nnTF {#1} { . }
          { \__hook_currname_or_default:n {#1} }
          { \__hook_parse_dot_label:nw {#2} #1 ./ \s__hook_mark }
      }
  }
\cs_new:Npn \__hook_parse_dot_label:nw #1 #2 ./ #3 \s__hook_mark
  {
    \tl_if_empty:nTF {#2}
      { \__hook_parse_dot_label_aux:nw {#1} #3 \s__hook_mark }
      {
        \tl_if_empty:nTF {#3}
          {#2}
          { \__hook_parse_dot_label_cleanup:w #2 ./ #3 \s__hook_mark }
      }
  }
\cs_new:Npn \__hook_parse_dot_label_cleanup:w #1 ./ \s__hook_mark {#1}
\cs_new:Npn \__hook_parse_dot_label_aux:nw #1 #2 ./ \s__hook_mark
  { \__hook_currname_or_default:n {#1} / #2 }
\cs_new:Npn \__hook_currname_or_default:n #1
  {
    \tl_if_empty:NTF \g__hook_hook_curr_name_tl
      {
        \tl_if_empty:NTF \@currname
          {#1}
          { \@currname }
      }
      { \g__hook_hook_curr_name_tl }
  }

\cs_new_protected:Npn \hook_gput_code:nnn #1 #2
  {
    \exp_args:Nxx \__hook_gput_code:nnn
      { \__hook_parse_label_default:nn {#1} { top-level } }
      { \__hook_parse_label_default:nn {#2} { top-level } }
  }
\cs_new_protected:Npn \__hook_gput_code:nnn #1 #2 #3
  {
    \__hook_if_marked_removal:nnTF {#1} {#2}
      { \__hook_unmark_removal:nn {#1} {#2} }
      {
        \hook_if_exist:nTF {#1}
          {
            \__hook_hook_gput_code_do:nnn {#1} {#2} {#3}
            \__hook_update_hook_code:n {#1}
          }
          { \__hook_try_declaring_generic_hook:nnn {#1} {#2} {#3} }
      }
  }
\cs_generate_variant:Nn \__hook_gput_code:nnn { nxv }
\cs_new_protected:Npn \__hook_hook_gput_code_do:nnn #1 #2 #3
  {
    \__hook_debug:n{\iow_term:x{****~ Add~ to~
                      \hook_if_exist:nF {#1} { undeclared~ }
                      hook~ #1~ (#2)
                      \on@line\space <-~ \tl_to_str:n{#3}} }
    \prop_get:cnNTF { g__hook_#1_code_prop } {#2} \l__hook_return_tl
      {
        \prop_gput:cno { g__hook_#1_code_prop } {#2}
          { \l__hook_return_tl #3 }
      }
      { \prop_gput:cnn { g__hook_#1_code_prop } {#2} {#3} }
  }
\cs_new_protected:Npn \__hook_gput_undeclared_hook:nnn #1 #2 #3
  {
    \__hook_declare:n {#1}
    \__hook_hook_gput_code_do:nnn {#1} {#2} {#3}
  }
\cs_new_protected:Npn \__hook_try_declaring_generic_hook:nnn #1
  {
    \__hook_try_declaring_generic_hook:nNNnn {#1}
      \hook_gput_code:nnn \__hook_gput_undeclared_hook:nnn
  }
\cs_new_protected:Npn \__hook_try_declaring_generic_next_hook:nn #1
  {
    \__hook_try_declaring_generic_hook:nNNnn {#1}
      \hook_gput_next_code:nn \__hook_gput_next_do:nn
  }
\cs_new_protected:Npn \__hook_try_declaring_generic_hook:nNNnn #1
  {
    \__hook_if_file_hook:wTF #1 / / \s__hook_mark
      {
        \exp_args:Ne \__hook_try_declaring_generic_hook_split:nNNnn
          { \exp_args:Ne \__hook_file_hook_normalise:n {#1} }
      }
      { \__hook_try_declaring_generic_hook_split:nNNnn {#1} }
  }
\cs_new_protected:Npn \__hook_try_declaring_generic_hook_split:nNNnn #1 #2 #3
  {
    \__hook_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
      { #2 }
      { #3 } {#1}
  }
\prg_new_protected_conditional:Npnn \__hook_try_declaring_generic_hook:wn
    #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
  {
    \tl_if_empty:nTF {#2}
      { \prg_return_false: }
      {
        \prop_if_in:NnTF \c__hook_generics_prop {#1}
          {
            \hook_if_exist:nF {#5} { \hook_new:n {#5} }
            \prop_if_in:NnTF \c__hook_generics_reversed_ii_prop {#2}
              { \tl_gset:cn { g__hook_#5_reversed_tl } { - } }
              {
                \prop_if_in:NnT \c__hook_generics_reversed_iii_prop {#3}
                  { \tl_gset:cn { g__hook_#5_reversed_tl } { - } }
              }
            \prg_return_true:
          }
          { \prg_return_false: }
      }
  }
\prg_new_conditional:Npnn \__hook_if_file_hook:w
    #1 / #2 / #3 \s__hook_mark { TF }
  {
    \str_if_eq:nnTF {#1} { file }
      {
        \bool_lazy_or:nnTF
            { \tl_if_empty_p:n {#3} }
            { \str_if_eq_p:nn {#3} { / } }
          { \prg_return_false: }
          {
            \prop_if_in:NnTF \c__hook_generics_file_prop {#2}
              { \prg_return_true: }
              { \prg_return_false: }
          }
      }
      { \prg_return_false: }
  }
\cs_new:Npn \__hook_file_hook_normalise:n #1
  { \__hook_strip_double_slash:n {#1} }
\cs_new:Npn \__hook_strip_double_slash:n #1
  { \__hook_strip_double_slash:w #1 // \s__hook_mark }
\cs_new:Npn \__hook_strip_double_slash:w #1 // #2 \s__hook_mark
  {
    \tl_if_empty:nTF {#2}
      {#1}
      { \__hook_strip_double_slash:w #1 / #2 \s__hook_mark }
  }

\prop_const_from_keyval:Nn \c__hook_generics_prop
  {env=,file=,package=,class=,include=}
\prop_const_from_keyval:Nn \c__hook_generics_reversed_ii_prop {after=,end=}
\prop_const_from_keyval:Nn \c__hook_generics_reversed_iii_prop {after=}
\prop_const_from_keyval:Nn \c__hook_generics_file_prop {before=,after=}

\cs_new_eq:NN \__hook_update_hook_code:n \use_none:n

\cs_new_protected:Npn \hook_gremove_code:nn #1 #2
  {
    \exp_args:Nxx \__hook_gremove_code:nn
      { \__hook_parse_label_default:nn {#1} { top-level } }
      { \__hook_parse_label_default:nn {#2} { top-level } }
  }
\cs_new_protected:Npn \__hook_gremove_code:nn #1 #2
  {
    \__hook_if_exist:nTF {#1}
      {
        \str_if_eq:nnTF {#2} {*}
          {
            \prop_gclear:c { g__hook_#1_code_prop }
            \clist_gclear:c { g__hook_#1_labels_clist } % for debugging only
          }
          {
            \prop_get:cnNTF { g__hook_#1_code_prop } {#2} \l__hook_return_tl
              { \__hook_gremove_code_do:nn }
              { \__hook_mark_removal:nn }
                  {#1} {#2}
          }
        \hook_if_exist:nT {#1}
          { \__hook_update_hook_code:n {#1} }
      }
      { \__hook_mark_removal:nn {#1} {#2} }
  }
\cs_new_protected:Npn \__hook_gremove_code_do:nn #1 #2
  {
    \prop_gremove:cn { g__hook_#1_code_prop } {#2}
    \exp_args:Nco \clist_gremove_all:Nn
      { g__hook_#1_labels_clist } { \tl_to_str:n {#2} } % for debugging only
  }

\cs_new_protected:Npn \__hook_mark_removal:nn #1 #2
  {
    \tl_gput_right:Nx \g__hook_removal_list_tl
      { \__hook_removal_tl:nn {#1} {#2} }
  }
\cs_new_protected:Npn \__hook_unmark_removal:nn #1 #2
  {
    \tl_gremove_once:Nx \g__hook_removal_list_tl
      { \__hook_removal_tl:nn {#1} {#2} }
  }
\prg_new_protected_conditional:Npnn \__hook_if_marked_removal:nn #1 #2 { TF }
  {
    \exp_args:NNx \tl_if_in:NnTF \g__hook_removal_list_tl
      { \__hook_removal_tl:nn {#1} {#2} }
      { \prg_return_true: } { \prg_return_false: }
  }
\cs_new:Npn \__hook_removal_tl:nn #1 #2
  { & \tl_to_str:n {#2} $ \tl_to_str:n {#1} $ }

\prop_new:c {g__hook_??_rules_prop}
\prop_new:c {g__hook_??_code_prop}
\prop_new:c {g__hook_??_code_tl}
\tl_new:c {g__hook_??_reversed_tl}

\cs_new_protected:Npn \__hook_debug_gset_rule:nnnn #1#2#3#4
  {
    \prop_gremove:cn{g__hook_#1_rules_prop}{#2|#4}
    \prop_gremove:cn{g__hook_#1_rules_prop}{#4|#2}
    \str_case_e:nnF {#3}
       {
         {before} { \prop_gput:cnn {g__hook_#1_rules_prop}{#2|#4}{<} }
         {after}  { \prop_gput:cnn {g__hook_#1_rules_prop}{#4|#2}{<} }
         {incompatible-error}   { \prop_gput:cnn {g__hook_#1_rules_prop}{#2|#4}{xE} }
         {incompatible-warning} { \prop_gput:cnn {g__hook_#1_rules_prop}{#2|#4}{xW} }
         {removes}      { \prop_gput:cnn {g__hook_#1_rules_prop}{#2|#4}{->} }
         {unrelated}{ \prop_gremove:cn {g__hook_#1_rules_prop}{#2|#4}
                      \prop_gremove:cn {g__hook_#1_rules_prop}{#4|#2} }
       }
       { \ERRORunknownrule }
  }
\cs_new_protected:Npn \hook_gset_rule:nnnn #1#2#3#4
  {
    \use:x
      {
        \__hook_gset_rule:nnnn
          { \__hook_parse_label_default:nn {#1} { top-level } }
          { \__hook_parse_label_default:nn {#2} { top-level } }
          {#3}
          { \__hook_parse_label_default:nn {#4} { top-level } }
      }
  }
\cs_new_protected:Npn \__hook_gset_rule:nnnn #1#2#3#4
  {
    \__hook_declare:n {#1}
    \__hook_rule_gclear:nnn {#1} {#2} {#4}
    \debug_suspend:
    \cs_if_exist_use:cTF { __hook_rule_#3_gset:nnn }
      {
          {#1} {#2} {#4}
        \__hook_update_hook_code:n {#1}
      }
      { \ERRORunknownrule }
    \debug_resume:
    \__hook_debug_gset_rule:nnnn {#1} {#2} {#3} {#4} % for debugging
  }
\cs_new_protected:Npn \__hook_rule_before_gset:nnn #1#2#3
  {
    \tl_gset:cx { g__hook_#1_rule_ \__hook_label_pair:nn {#2} {#3} _tl }
      { \__hook_label_ordered:nnTF {#2} {#3} { < } { > } }
  }
\cs_new_eq:cN { __hook_rule_<_gset:nnn } \__hook_rule_before_gset:nnn
\cs_new_protected:Npn \__hook_rule_after_gset:nnn #1#2#3
  {
    \tl_gset:cx { g__hook_#1_rule_ \__hook_label_pair:nn {#3} {#2} _tl }
      { \__hook_label_ordered:nnTF {#3} {#2} { < } { > } }
  }
\cs_new_eq:cN { __hook_rule_>_gset:nnn } \__hook_rule_after_gset:nnn
\cs_new_protected:Npn \__hook_rule_removes_gset:nnn #1#2#3
  {
    \tl_gset:cx { g__hook_#1_rule_ \__hook_label_pair:nn {#2} {#3} _tl }
      { \__hook_label_ordered:nnTF {#2} {#3} { -> } { <- } }
  }
\cs_new_protected:cpn { __hook_rule_incompatible-error_gset:nnn } #1#2#3
  { \tl_gset:cn { g__hook_#1_rule_ \__hook_label_pair:nn {#2} {#3} _tl } { xE } }
\cs_new_protected:cpn { __hook_rule_incompatible-warning_gset:nnn } #1#2#3
  { \tl_gset:cn { g__hook_#1_rule_ \__hook_label_pair:nn {#2} {#3} _tl } { xW } }
\cs_new_protected:Npn \__hook_rule_unrelated_gset:nnn #1#2#3 { }
\cs_new_protected:Npn \__hook_rule_gclear:nnn #1#2#3
  { \cs_undefine:c { g__hook_#1_rule_ \__hook_label_pair:nn {#2} {#3} _tl } }
\cs_new:Npn \__hook_label_pair:nn #1#2
  {
    \if_case:w \__hook_str_compare:nn {#1} {#2} \exp_stop_f:
           #1 | #1 %  0
    \or:   #1 | #2 % +1
    \else: #2 | #1 % -1
    \fi:
  }
\prg_new_conditional:Npnn \__hook_label_ordered:nn #1#2 { TF }
  {
    \if_int_compare:w \__hook_str_compare:nn {#1} {#2} > 0 \exp_stop_f:
      \prg_return_true:
    \else
      \prg_return_false:
    \fi:
  }
\cs_new:Npn \__hook_if_label_case:nnnnn #1#2
   {
     \cs:w use_
       \if_case:w \__hook_str_compare:nn {#1} {#2}
          i \or: ii \else: iii \fi: :nnn
     \cs_end:
   }

\cs_new_protected:Npn \__hook_initialize_all: {
  \cs_gset_eq:NN \__hook_update_hook_code:n \__hook_initialize_hook_code:n
  \__hook_debug:n { \prop_gclear:N \g__hook_used_prop }
  \seq_map_inline:Nn \g__hook_all_seq
      {
        \__hook_update_hook_code:n {##1}
      }
  \__hook_debug:n
     { \iow_term:x{^^JAll~ initialized~ (non-empty)~ hooks:}
       \prop_map_inline:Nn \g__hook_used_prop
           { \iow_term:x{^^J~ ##1~ ->~
               \exp_not:v {g__hook_##1_code_tl}~ }
           }
     }
  \cs_gset_eq:NN \hook_use:n \__hook_use_initialized:n
  \cs_gset_eq:NN \__hook_preamble_hook:n \use_none:n
}

\cs_new_protected:Npn \__hook_initialize_hook_code:n #1 {
  \__hook_debug:n{ \iow_term:x{^^JUpdate~ code~ for~ hook~
                                  '#1' \on@line :^^J} }
  \hook_if_exist:nT {#1}
    {
      \prop_if_empty:cTF {g__hook_#1_code_prop}
        { \tl_gset:co {g__hook_#1_code_tl}
                      {\cs:w g__hook_#1_next_code_tl \cs_end: } }
        {
          \__hook_if_reversed:nTF {#1}
            { \cs_set_eq:NN \__hook_tl_gput:NV    \tl_gput_left:NV
              \cs_set_eq:NN \__hook_clist_gput:NV \clist_gput_left:NV  }
            { \cs_set_eq:NN \__hook_tl_gput:NV    \tl_gput_right:NV
              \cs_set_eq:NN \__hook_clist_gput:NV \clist_gput_right:NV }
          \prop_gset_eq:Nc \g__hook_code_temp_prop { g__hook_#1_code_prop }
          \__hook_initialize_single:ccccn
            { g__hook_#1_code_prop } { g__hook_#1_code_tl }
            { g__hook_#1_next_code_tl } { g__hook_#1_labels_clist }
            {#1}
          \prop_gset_eq:cN { g__hook_#1_code_prop } \g__hook_code_temp_prop
          \__hook_debug:n{ \exp_args:NNx \prop_gput:Nnn \g__hook_used_prop {#1}{} }
        }
    }
}

\prop_new:N\g__hook_used_prop

\cs_new:Npn \__hook_tl_csname:n #1 { l__hook_label_#1_tl }
\cs_new:Npn \__hook_seq_csname:n #1 { l__hook_label_#1_seq }

\seq_new:N \l__hook_labels_seq
\int_new:N \l__hook_labels_int
\tl_new:N \l__hook_front_tl
\tl_new:N \l__hook_rear_tl
\tl_new:c { \__hook_tl_csname:n { 0 } }

\cs_new_protected:Npn \__hook_initialize_single:NNNNn #1#2#3#4#5 {
  \debug_suspend:
  \seq_clear:N \l__hook_labels_seq
  \int_zero:N  \l__hook_labels_int
  \tl_set:Nn \l__hook_cur_hook_tl {#5}
  \prop_map_inline:Nn #1
     {
       \int_incr:N \l__hook_labels_int
       \seq_put_right:Nn \l__hook_labels_seq {##1}
       \tl_set:cn { \__hook_tl_csname:n {##1} }{0}     % the counter k for number of
                                                   % j before k rules
       \seq_clear_new:c { \__hook_seq_csname:n {##1} } % sequence of successors to k
                                                   % i.e., k before j rules (stores
                                                   % the names of the j's)
     }
  \prop_map_inline:Nn #1
    {
      \prop_map_inline:Nn #1
        {
          \__hook_if_label_case:nnnnn {##1} {####1}
            { \prop_map_break: }
            { \__hook_apply_label_pair:nnn {##1} {####1} }
            { \__hook_apply_label_pair:nnn {####1} {##1} }
                {#5}
        }
    }
  \__hook_debug:n { \__hook_debug_label_data:N #1 }
  \tl_set:Nn \l__hook_rear_tl { 0 }
  \tl_set:cn { \__hook_tl_csname:n { 0 } } { 0 } % really {l__hook_label_ \l__hook_rear_tl _tl}
  \seq_map_inline:Nn \l__hook_labels_seq
      {
        \int_compare:nNnT { \cs:w \__hook_tl_csname:n {##1} \cs_end: } = 0
            {
              \tl_set:cn { \__hook_tl_csname:n { \l__hook_rear_tl } }{##1}
              \tl_set:Nn \l__hook_rear_tl {##1}
            }
      }
  \tl_set_eq:Nc \l__hook_front_tl { \__hook_tl_csname:n { 0 } }
  \tl_gclear:N #2
  \clist_gclear:N #4
  \bool_while_do:nn { ! \str_if_eq_p:Vn \l__hook_front_tl { 0 } }
       {
         \int_decr:N \l__hook_labels_int
         \prop_get:NVN #1 \l__hook_front_tl \l__hook_return_tl
         \__hook_tl_gput:NV #2 \l__hook_return_tl
         \__hook_clist_gput:NV #4 \l__hook_front_tl
         \__hook_debug:n{ \iow_term:x{Handled~ code~ for~ \l__hook_front_tl} }
         \seq_map_inline:cn { \__hook_seq_csname:n { \l__hook_front_tl } }
             {
               \tl_set:cx { \__hook_tl_csname:n {##1} }
                   { \int_eval:n { \cs:w \__hook_tl_csname:n {##1} \cs_end: - 1 } }
               \int_compare:nNnT { \cs:w \__hook_tl_csname:n {##1} \cs_end: } = 0
                   {
                     \tl_set:cn { \__hook_tl_csname:n { \l__hook_rear_tl } } {##1}
                     \tl_set:Nn \l__hook_rear_tl            {##1}
                   }
             }
         \tl_set_eq:Nc \l__hook_front_tl { \__hook_tl_csname:n { \l__hook_front_tl } }
       }
  \int_compare:nNnF \l__hook_labels_int = 0
      {
        \iow_term:x{====================}
        \iow_term:x{Error:~ label~ rules~ are~ incompatible:}
        \__hook_debug_label_data:N #1
        \iow_term:x{====================}
      }
  \tl_gput_right:Nn #2 {#3}
  \debug_resume:
}
\cs_generate_variant:Nn \__hook_initialize_single:NNNNn {cccc}

\cs_new:Npn \__hook_tl_gput:NV     {\ERROR}
\cs_new:Npn \__hook_clist_gput:NV  {\ERROR}

\cs_new_protected:Npn \__hook_apply_label_pair:nnn #1#2#3
  {
    \__hook_label_if_exist_apply:nnnF {#1} {#2} {#3}
      {
        \__hook_label_if_exist_apply:nnnF {#1} {#2} { ?? } { }
      }
  }
\cs_new_protected:Npn \__hook_label_if_exist_apply:nnnF #1#2#3
  {
    \if_cs_exist:w g__hook_ #3 _rule_ #1 | #2 _tl \cs_end:
      \__hook_apply_rule:nnn {#1} {#2} {#3}
      \exp_after:wN \use_none:n
    \else:
      \use:nn
    \fi:
  }

\cs_new_protected:Npn \__hook_apply_rule:nnn #1#2#3
  {
    \cs:w __hook_apply_
      \cs:w g__hook_#3_reversed_tl \cs_end: rule_
        \cs:w g__hook_ #3 _rule_ #1 | #2 _tl \cs_end: :nnn \cs_end:
      {#1} {#2} {#3}
  }
\cs_new_protected:cpn { __hook_apply_rule_<:nnn } #1#2#3
  {
    \__hook_debug:n { \__hook_msg_pair_found:nnn {#1} {#2} {#3} }
    \tl_set:cx { \__hook_tl_csname:n {#2} }
       { \int_eval:n{ \cs:w \__hook_tl_csname:n {#2} \cs_end: + 1 } }
    \seq_put_right:cn{ \__hook_seq_csname:n {#1} }{#2}
  }
\cs_new_protected:cpn { __hook_apply_rule_>:nnn } #1#2#3
  {
    \__hook_debug:n { \__hook_msg_pair_found:nnn {#1} {#2} {#3} }
    \tl_set:cx { \__hook_tl_csname:n {#1} }
       { \int_eval:n{ \cs:w \__hook_tl_csname:n {#1} \cs_end: + 1 } }
    \seq_put_right:cn{ \__hook_seq_csname:n {#2} }{#1}
  }
\cs_new_protected:cpn { __hook_apply_rule_xE:nnn } #1#2#3
  {
    \__hook_debug:n { \__hook_msg_pair_found:nnn {#1} {#2} {#3} }
    \msg_error:nnnnnn { hooks } { labels-incompatible }
      {#1} {#2} {#3} { 1 }
    \use:c { __hook_apply_rule_->:nnn } {#1} {#2} {#3}
    \use:c { __hook_apply_rule_<-:nnn } {#1} {#2} {#3}
  }
\cs_new_protected:cpn { __hook_apply_rule_xW:nnn } #1#2#3
  {
    \__hook_debug:n { \__hook_msg_pair_found:nnn {#1} {#2} {#3} }
    \msg_warning:nnnnnn { hooks } { labels-incompatible }
      {#1} {#2} {#3} { 0 }
  }
\cs_new_protected:cpn { __hook_apply_rule_->:nnn } #1#2#3
  {
    \__hook_debug:n
       {
         \__hook_msg_pair_found:nnn {#1} {#2} {#3}
         \iow_term:x{--->~ Drop~ '#2'~ code~ from~
           \iow_char:N \\ g__hook_ \l__hook_cur_hook_tl _code_prop ~ because~ of~ '#1' }
       }
    \prop_gput:cnn { g__hook_ \l__hook_cur_hook_tl _code_prop } {#2} { }
  }
\cs_new_protected:cpn { __hook_apply_rule_<-:nnn } #1#2#3
  {
    \__hook_debug:n
       {
         \__hook_msg_pair_found:nnn {#1} {#2} {#3}
         \iow_term:x{--->~ Drop~ '#1'~ code~ from~
           \iow_char:N \\ g__hook_ \l__hook_cur_hook_tl _code_prop ~ because~ of~ '#2' }
       }
    \prop_gput:cnn { g__hook_ \l__hook_cur_hook_tl _code_prop } {#1} { }
  }

\cs_new_eq:cc { __hook_apply_-rule_<:nnn  } { __hook_apply_rule_>:nnn }
\cs_new_eq:cc { __hook_apply_-rule_>:nnn  } { __hook_apply_rule_<:nnn }
\cs_new_eq:cc { __hook_apply_-rule_<-:nnn } { __hook_apply_rule_<-:nnn }
\cs_new_eq:cc { __hook_apply_-rule_->:nnn } { __hook_apply_rule_->:nnn }
\cs_new_eq:cc { __hook_apply_-rule_xE:nnn  } { __hook_apply_rule_xE:nnn }
\cs_new_eq:cc { __hook_apply_-rule_xW:nnn  } { __hook_apply_rule_xW:nnn }

\cs_new_protected:Npn \__hook_msg_pair_found:nnn #1#2#3
  {
    \iow_term:x{~ \str_if_eq:nnTF {#3} {??} {default} {~normal} ~
               rule~ \__hook_label_pair:nn {#1} {#2}:~
             \use:c { g__hook_#3_rule_ \__hook_label_pair:nn {#1} {#2} _tl } ~ found}
  }

\cs_new_protected:Npn \__hook_debug_label_data:N #1 {
  \iow_term:x{Code~ labels~ for~ sorting:}
  \iow_term:x{~ \seq_use:Nnnn\l__hook_labels_seq {~and~}{,~}{~and~} }  % fix name!
  \iow_term:x{^^J Data~ structure~ for~ label~ rules:}
  \prop_map_inline:Nn #1
       {
         \iow_term:x{~ ##1~ =~ \tl_use:c{ \__hook_tl_csname:n {##1} }~ ->~
           \seq_use:cnnn{ \__hook_seq_csname:n {##1} }{~->~}{~->~}{~->~}
         }
       }
  \iow_term:x{}
}

\cs_new_protected:Npn \hook_log:n #1
  {
    \exp_args:Nx \__hook_log:n
      { \__hook_parse_label_default:nn {#1} { top-level } }
  }
\cs_new_protected:Npn \__hook_log:n #1
  {
    \iow_term:x{^^JThe~ hook~ '#1':}
    \hook_if_exist:nF {#1}
      { \iow_term:x {~Hook~ is~ not~ declared!} }
    \__hook_if_exist:nTF {#1}
      {
        \iow_term:x{~Code~ chunks:}
        \prop_if_empty:cTF {g__hook_#1_code_prop}
          { \iow_term:x{\@spaces ---} }
          {
            \prop_map_inline:cn {g__hook_#1_code_prop}
              { \iow_term:x{\@spaces ##1~ ->~ \tl_to_str:n{##2} } }
          }
        \iow_term:x{~Extra~ code~ next~ invocation:}
        \iow_term:x{\@spaces
          \tl_if_empty:cTF { g__hook_#1_next_code_tl }
            {---} {->~ \str_use:c{g__hook_#1_next_code_tl} } }
        \iow_term:x{~Rules:}
        \prop_if_empty:cTF {g__hook_#1_rules_prop}
          { \iow_term:x{\@spaces ---} }
          { \prop_map_inline:cn {g__hook_#1_rules_prop}
              { \iow_term:x{\@spaces ##1~ with~ relation~ ##2} }
          }
        \hook_if_exist:nT {#1}
          { \iow_term:x { ~Execution~ order
               \prop_if_empty:cTF {g__hook_#1_rules_prop}
                 { \__hook_if_reversed:nT {#1}
                        { ~ (after~ reversal) }
                 }
                 { ~ (after~
                   \__hook_if_reversed:nT {#1} {reversal~ and~}
                   applying~ rules)
                 }
               :
              }
            \iow_term:x { \@spaces
              \clist_if_empty:cTF{g__hook_#1_labels_clist}
                 {not~ set~ yet}
                 { \clist_use:cnnn {g__hook_#1_labels_clist}
                                   { ,~ } { ,~ } { ,~ }   }
            }
          }
      }
      { \iow_term:n { ~The~hook~is~empty. } }
    \iow_term:n { }
  }

\cs_new_protected:Npn \hook_gput_next_code:nn #1
  {
    \exp_args:Nx \__hook_gput_next_code:nn
      { \__hook_parse_label_default:nn {#1} { top-level } }
  }
\cs_new_protected:Npn \__hook_gput_next_code:nn #1 #2
  {
    \__hook_declare:n {#1}
    \hook_if_exist:nTF {#1}
      { \__hook_gput_next_do:nn {#1} {#2} }
      { \__hook_try_declaring_generic_next_hook:nn {#1} {#2} }
  }
\cs_new_protected:Npn \__hook_gput_next_do:nn #1 #2
  {
    \tl_gput_right:cn { g__hook_#1_next_code_tl }
      { #2 \tl_gclear:c { g__hook_#1_next_code_tl } }
  }
\cs_new_protected:Npn \hook_use:n #1
  {
    \tl_if_exist:cTF { g__hook_#1_code_tl }
      {
        \__hook_preamble_hook:n {#1}
        \cs:w g__hook_#1_code_tl \cs_end:
      }
      { \__hook_use:wn #1 / \s__hook_mark {#1} }
  }
\cs_new:Npn \__hook_use_initialized:n #1
  {
    \tl_if_exist:cTF { g__hook_#1_code_tl }
      { \cs:w g__hook_#1_code_tl \cs_end: }
      { \__hook_use:wn #1 / \s__hook_mark {#1} }
  }
\cs_new_protected:Npn \__hook_preamble_hook:n #1
  { \__hook_initialize_hook_code:n {#1} }
\cs_new:Npn \__hook_use:wn #1 / #2 \s__hook_mark #3
  {
    \str_if_eq:nnTF {#1} { file }
      { \__hook_try_file_hook:n {#3} }
      { } % Hook doesn't exist
  }
\cs_new_protected:Npn \__hook_try_file_hook:n #1
  {
    \__hook_if_file_hook:wTF #1 / / \s__hook_mark
      {
        \exp_args:Ne \__hook_if_exist_use:n
          { \exp_args:Ne \__hook_file_hook_normalise:n {#1} }
      }
      { \__hook_if_exist_use:n {#1} } % file/ generic hook (e.g. file/before)
  }
\cs_new_protected:Npn \__hook_if_exist_use:n #1
  {
    \tl_if_exist:cT { g__hook_#1_code_tl }
      {
        \__hook_preamble_hook:n {#1}
        \cs:w g__hook_#1_code_tl \cs_end:
      }
  }
\cs_new_protected:Npn \hook_use_once:n #1
  {
    \tl_if_exist:cT { g__hook_#1_code_tl }
      {
        \clist_gput_left:Nn \g__hook_execute_immediately_clist {#1}
        \hook_use:n {#1}
      }
  }

\prg_new_conditional:Npnn \hook_if_empty:n #1 { p , T , F , TF }
  {
    \__hook_if_exist:nTF {#1}
      {
        \bool_lazy_and:nnTF
            { \prop_if_empty_p:c { g__hook_#1_code_prop } }
            { \tl_if_empty_p:c { g__hook_#1_next_code_tl } }
          { \prg_return_true: }
          { \prg_return_false: }
      }
      { \prg_return_true: }
  }

\prg_new_conditional:Npnn \hook_if_exist:n #1 { p , T , F , TF }
  {
    \tl_if_exist:cTF { g__hook_#1_code_tl }
      { \prg_return_true: }
      { \prg_return_false: }
  }

\prg_new_conditional:Npnn \__hook_if_exist:n #1 { p , T , F , TF }
  {
    \prop_if_exist:cTF { g__hook_#1_code_prop }
      { \prg_return_true: }
      { \prg_return_false: }
  }
\prg_new_conditional:Npnn \__hook_if_reversed:n #1 { p , T , F , TF }
  {
    \if_int_compare:w \cs:w g__hook_#1_reversed_tl \cs_end: 1 < 0 \exp_stop_f:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }

\clist_new:N \g__hook_execute_immediately_clist
\msg_new:nnnn { hooks } { labels-incompatible }
  {
    Labels~`#1'~and~`#2'~are~incompatible
    \str_if_eq:nnF {#3} {??} { ~in~hook~`#3' } .~
    \int_compare:nNnT {#4} = { 1 }
      { The~code~for~both~labels~will~be~dropped. }
  }
  {
    LaTeX~found~two~incompatible~labels~in~the~same~hook.~
    This~indicates~an~incompatibility~between~packages.
  }
\msg_new:nnn { hooks } { empty-label }
  { Empty~code~label~\msg_line_context:.~Using~`#1'~instead. }

\NewDocumentCommand \NewHook             { m }{ \hook_new:n {#1} }
\NewDocumentCommand \NewReversedHook     { m }{ \hook_new_reversed:n {#1} }
\NewDocumentCommand \NewMirroredHookPair { mm }{ \hook_new_pair:nn {#1}{#2} }

\NewDocumentCommand \AddToHook { m o +m }
  {
    \clist_if_in:NnTF \g__hook_execute_immediately_clist {#1}
      {#3}
      { \hook_gput_code:nnn {#1} {#2} {#3} }
  }

\NewDocumentCommand \AddToHookNext { m +m }
  { \hook_gput_next_code:nn {#1} {#2} }

\NewDocumentCommand \RemoveFromHook { m o }
  { \hook_gremove_code:nn {#1} {#2} }
\str_if_eq:VnTF \@currname { lthooks }
  {
    \seq_gpush:Nn \g__hook_name_stack_seq { lthooks }
    \cs_set_protected:Npn \__hook_tmp:w #1 #2 #3
      {
        \quark_if_recursion_tail_stop:n {#1}
        \seq_gput_right:Nn \g__hook_name_stack_seq { }
        \__hook_tmp:w
      }
    \exp_after:wN \__hook_tmp:w
      \@currnamestack
      \q_recursion_tail \q_recursion_tail
      \q_recursion_tail \q_recursion_stop
  }
  { \seq_gpush:Nn \g__hook_name_stack_seq { } }
\cs_new_protected:Npn \__hook_curr_name_push:n #1
  {
    \seq_gpush:Nn \g__hook_name_stack_seq {#1}
    \tl_gset:Nn \g__hook_hook_curr_name_tl {#1}
  }
\cs_new_protected:Npn \__hook_curr_name_pop:
  {
    \seq_gpop:NN \g__hook_name_stack_seq \l__hook_return_tl
    \seq_get:NNTF \g__hook_name_stack_seq \l__hook_return_tl
      { \tl_gset_eq:NN \g__hook_hook_curr_name_tl \l__hook_return_tl }
      { \ERROR_should_not_happen }
  }
\NewDocumentCommand \DeclareDefaultHookLabel { m }
  {
    \seq_gpop:NN \g__hook_name_stack_seq \l__hook_return_tl
    \__hook_curr_name_push:n {#1}
  }
\tl_gput_left:Nn \@pushfilename { \__hook_curr_name_push:n { } }
\tl_gput_left:Nn \@popfilename { \__hook_curr_name_pop: }
\newcommand \UseHook { \hook_use:n }
\cs_new_protected:Npn \UseOneTimeHook { \hook_use_once:n }

\cs_new_protected:Npn \ShowHook { \hook_log:n }

\cs_new_protected:Npn \DebugHookOn { \hook_debug_on: }
\cs_new_protected:Npn \DebugHookOff { \hook_debug_off: }

\NewDocumentCommand \DeclareHookRule { m m m m }
{ \hook_gset_rule:nnnn {#1}{#2}{#3}{#4} }

\NewDocumentCommand \DeclareDefaultHookRule { m m m }
                    { \hook_gset_rule:nnnn {??}{#1}{#2}{#3} }
\@onlypreamble\DeclareDefaultHookRule
\NewDocumentCommand \ClearHookRule { m m m }
{ \hook_gset_rule:nnnn {#1}{#2}{unrelated}{#3} }
\NewExpandableDocumentCommand \IfHookExistTF { m }
  { \hook_if_exist:nTF {#1} }
\NewExpandableDocumentCommand \IfHookEmptyTF { m }
  { \hook_if_empty:nTF {#1} }

\renewcommand\AtBeginDocument{\AddToHook{begindocument}}

\renewcommand\AtEndDocument {\AddToHook{enddocument}}

\begingroup
  \def\@currname{}
  \NewHook{begindocument}
  \NewHook{enddocument}
  \NewHook{rmfamily}
  \NewHook{sffamily}
  \NewHook{ttfamily}
  \NewHook{defaultfamily}
  \NewHook{documentclass}
\endgroup
\let\begin\relax  % avoid redeclaration message
\DeclareRobustCommand*\begin[1]{%
  \UseHook{env/#1/before}%
  \@ifundefined{#1}%
    {\def\reserved@a{\@latex@error{Environment~#1~undefined}\@eha}}%
    {\def\reserved@a{\def\@currenvir{#1}%
        \edef\@currenvline{\on@line}%
        \@execute@begin@hook{#1}%
        \csname #1\endcsname}}%
  \@ignorefalse
  \begingroup\@endpefalse\reserved@a}
\def\@execute@begin@hook #1{%
  \expandafter\ifx\csname #1\endcsname\document
    \endgroup
    \gdef\@execute@begin@hook##1{\UseHook{env/##1/begin}}%
    \__hook_initialize_all:
    \@execute@begin@hook{#1}%
  \else
    \UseHook{env/#1/begin}%
  \fi
}
\@namedef{end~}#1{%
  \UseHook{env/#1/end}%
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi
  \UseHook{env/#1/after}%
  \if@ignore\@ignorefalse\ignorespaces\fi}%
\@namedef{end~}#1{%
\romannumeral
\IfHookEmptyTF{env/#1/end}%
  {\expandafter\z@}%
  {\z@\UseHook{env/#1/end}}%
\csname end#1\endcsname\@checkend{#1}%
\expandafter\endgroup\if@endpe\@doendpe\fi
\UseHook{env/#1/after}%
\if@ignore\@ignorefalse\ignorespaces\fi}%
\newcommand\AtBeginEnvironment[1]    {\AddToHook{env/#1/begin}}
\newcommand\AtEndEnvironment[1]      {\AddToHook{env/#1/end}}
\newcommand\BeforeBeginEnvironment[1]{\AddToHook{env/#1/before}}
\newcommand\AfterEndEnvironment[1]   {\AddToHook{env/#1/after}}
\ExplSyntaxOff
\NewHook{begindocument/end}

\def\document{%
  \@kernel@after@env@document@begin
  \@expl@sys@load@backend@@
  \ifx\@unusedoptionlist\@empty\else
    \@latex@warning@no@line{Unused global option(s):^^J%
            \@spaces[\@unusedoptionlist]}%
  \fi
  \@colht\textheight
  \@colroom\textheight \vsize\textheight
  \columnwidth\textwidth
  \@clubpenalty\clubpenalty
  \if@twocolumn
    \advance\columnwidth -\columnsep
    \divide\columnwidth\tw@ \hsize\columnwidth \@firstcolumntrue
  \fi
  \hsize\columnwidth \linewidth\hsize
  \begingroup\@floatplacement\@dblfloatplacement
    \makeatletter\let\@writefile\@gobbletwo
    \global \let \@multiplelabels \relax
    \@input{\jobname.aux}%
  \endgroup
  \if@filesw
    \immediate\openout\@mainaux\jobname.aux
    \immediate\write\@mainaux{\relax}%
  \fi
  \process@table
  \let\glb@currsize\@empty  % Force math initialization.
  \normalsize
  \everypar{}%
  \ifx\normalsfcodes\@empty
    \ifnum\sfcode`\.=\@m
      \let\normalsfcodes\frenchspacing
    \else
      \let\normalsfcodes\nonfrenchspacing
    \fi
  \fi
  \ifx\document@default@language\m@ne
    \chardef\document@default@language\language
  \fi
  \@noskipsecfalse
  \let \@refundefined \relax
  \UseOneTimeHook{begindocument}%
  \@kernel@after@begindocument
  \ifdim\topskip<1sp\global\topskip 1sp\relax\fi
  \global\@maxdepth\maxdepth
  \global\let\@begindocumenthook\@undefined
  \ifx\@listfiles\@undefined
    \global\let\@filelist\relax
    \global\let\@addtofilelist\@gobble
  \fi
  \gdef\do##1{\global\let ##1\@notprerr}%
  \@preamblecmds
  \global\let \@nodocument \relax
  \global\let\do\noexpand
  \UseOneTimeHook{begindocument/end}%
  \ignorespaces}
\let\@kernel@after@begindocument\@empty
\edef \@kernel@after@env@document@begin{%
  \let\expandafter\noexpand\csname
       g__hook_env/document/begin_code_tl\endcsname
  \noexpand\@empty}
\let\@kernel@hook@begindocument\@empty
\NewHook{enddocument/afterlastpage}
\NewHook{enddocument/afteraux}
\NewHook{enddocument/info}
\NewHook{enddocument/end}

\def\enddocument{%
   \UseHook{enddocument}%
   \@kernel@after@enddocument
   \@checkend{document}%
   \clearpage
   \UseHook{enddocument/afterlastpage}%
   \@kernel@after@enddocument@afterlastpage
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse
       \makeatletter \@@input\jobname.aux
     \fi
     \UseHook{enddocument/afteraux}%
     \UseHook{enddocument/info}%
   \endgroup
   \UseHook{enddocument/end}%
   \deadcycles\z@\@@end}
\let\@kernel@after@enddocument\@empty
\let\@kernel@after@enddocument@afterlastpage\@empty
\def\@enddocument@kernel@warnings{%
   \ifdim \font@submax >\fontsubfuzz\relax
     \@font@warning{Size substitutions with differences\MessageBreak
                up to \font@submax\space have occurred.\@gobbletwo}%
   \fi
   \@defaultsubs
   \@refundefined
   \if@filesw
     \ifx \@multiplelabels \relax
       \if@tempswa
         \@latex@warning@no@line{Label(s) may have changed.
             Rerun to get cross-references right}%
       \fi
     \else
       \@multiplelabels
     \fi
   \fi
}
\AddToHook{enddocument/info}[kernel/filelist]{\@dofilelist}
\AddToHook{enddocument/info}[kernel/warnings]{\@enddocument@kernel@warnings}
\DeclareHookRule{enddocument/info}{kernel/filelist}{before}{kernel/warnings}
\expandafter\let\csname ver@atveryend.sty\endcsname\fmtversion
\newcommand\AfterLastShipout  {\AddToHook{enddocument/afterlastpage}}
\newcommand\AtVeryEndDocument {\AddToHook{enddocument/afteraux}}
\newcommand\AtEndAfterFileList{\AddToHook{enddocument/info}}
\newcommand\AtVeryVeryEnd     {\AddToHook{enddocument/end}}

\ExplSyntaxOn
\newcommand\BeforeClearDocument[1]
  { \AtEndDocument{#1}
    \@DEPRECATED{BeforeClearDocument \tl_to_str:n{#1}}
  }
\cs_new:Npn\@DEPRECATED #1
   {\iow_term:x{======~DEPRECATED~USAGE~#1~==========}}
\ExplSyntaxOff



\endinput
%%
%% End of file `lthooks.ltx'.
