%% Copyright (C) 2011-2020 by Martin Scharrer <martin@scharrer-online.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
%%   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 Martin Scharrer.
%%
%% This work consists of the files filehook.dtx and filehook.ins
%% and the derived files filehook.sty, filehook-fink.sty,
%% filehook-listings.sty, filehook-memoir.sty, filehook-scrlfile.sty
%% and pgf-filehook.sty
%%
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{filehook-2019}[% filehook subpackage, do not load directly
    2020/09/29
    v0.8a
    Hooks for input files]
\newif\iffilehook@force
\DeclareOption{force}{\filehook@forcetrue}
\ProcessOptions\relax
\newif\iffilehook@newfmt
\@ifl@t@r\fmtversion{2019/10/01}{\filehook@newfmttrue}{\filehook@newfmtfalse}
\def\filehook@let#1#2{%
  \expandafter\ifx\csname #2\space\endcsname\relax
    \expandafter\let\csname #1\expandafter\endcsname\csname #2\endcsname
  \else
    \expandafter\def\csname #1\expandafter\endcsname\expandafter{\expandafter\protect\csname #1\space\endcsname}%
    \expandafter\let\csname #1\space\expandafter\endcsname\csname #2\space\endcsname
  \fi
}
\def\filehook@glet#1#2{%
  \expandafter\ifx\csname #2\space\endcsname\relax
    \expandafter\global\expandafter\let\csname #1\expandafter\endcsname\csname #2\endcsname
  \else
    \expandafter\global\expandafter\def\csname #1\expandafter\endcsname\expandafter{\expandafter\protect\csname #1\space\endcsname}%
    \expandafter\global\expandafter\let\csname #1\space\expandafter\endcsname\csname #2\space\endcsname
  \fi
}
\def\filehook@cmp#1#2{%
  \expandafter\ifx\csname #2\space\endcsname\relax
    \expandafter\ifx\csname #1\expandafter\endcsname\csname #2\endcsname
      \expandafter\expandafter\expandafter\@firstoftwo
    \else
      \expandafter\expandafter\expandafter\@secondoftwo
    \fi
  \else
    \expandafter\ifx\csname #1\space\expandafter\endcsname\csname #2\space\endcsname
      \expandafter\expandafter\expandafter\@firstoftwo
    \else
      \expandafter\expandafter\expandafter\@secondoftwo
    \fi
  \fi
}
\begingroup
\gdef\filehook@csuse#1{\ifcsname #1\endcsname\csname #1\expandafter\endcsname\fi}
\expandafter\ifx\csname csuse\endcsname\relax
    \expandafter\ifx\csname ifcsname\endcsname\relax
        \gdef\filehook@csuse#1{\expandafter\ifx\csname #1\endcsname\relax\else\csname #1\expandafter\endcsname\fi}
    \fi
\else
    \global\let\filehook@csuse\csuse
\fi
\endgroup
\def\filehook@include@atbegin#1{%
  \filehook@let{InputIfFileExists}{filehook@@InputIfFileExists}%
  \filehook@csuse{\filehook@include@atbegin@#1}%
  \filehook@include@@atbegin
}
\def\filehook@include@@atbegin{}
\def\filehook@include@atend#1{%
  \filehook@include@@atend
  \filehook@csuse{\filehook@include@atend@#1}%
}
\def\filehook@include@@atend{}
\def\filehook@include@after#1{%
  \filehook@include@@after
  \filehook@csuse{\filehook@include@after@#1}%
}
\def\filehook@include@@after{}
\def\filehook@input@atbegin#1{%
  \filehook@let{InputIfFileExists}{filehook@@InputIfFileExists}%
  \filehook@csuse{\filehook@input@atbegin@\filehook@ensureext{#1}}%
  \filehook@input@@atbegin
}
\def\filehook@input@@atbegin{}
\def\filehook@input@atend#1{%
  \filehook@input@@atend
  \filehook@csuse{\filehook@input@atend@\filehook@ensureext{#1}}%
}
\def\filehook@input@@atend{}
\def\filehook@atbegin#1{%
  \filehook@csuse{\filehook@atbegin@\filehook@ensureext{#1}}%
  \filehook@@atbegin
}
\def\filehook@@atbegin{}
\def\filehook@atend#1{%
  \filehook@@atend
  \filehook@csuse{\filehook@atend@\filehook@ensureext{#1}}%
}
\def\filehook@@atend{}
\def\filehook@every@atbegin#1{%
    \filehook@every@@atbegin
}
\def\filehook@every@@atbegin{}
\def\filehook@every@atend#1{%
    \filehook@every@@atend
}
\def\filehook@every@@atend{}
\def\filehook@include@atbegin@{filehook@include@atbegin@}
\def\filehook@include@atend@{filehook@include@atend@}
\def\filehook@include@after@{filehook@include@after@}
\def\filehook@input@atbegin@{filehook@input@atbegin@}
\def\filehook@input@atend@{filehook@input@atend@}
\def\filehook@input@after@{filehook@input@after@}
\def\filehook@atbegin@{filehook@atbegin@}
\def\filehook@atend@{filehook@atend@}
\def\filehook@after@{filehook@after@}
\def\filehook@append{\g@addto@macro}
\long\def\filehook@appendwarg#1#2{%
  \begingroup
    \toks@\expandafter{#1{##1}#2}%
    \edef\@tempa{\the\toks@}%
    \expandafter\gdef\expandafter#1\expandafter##\expandafter1\expandafter{\@tempa}%
  \endgroup
}
\long\def\filehook@prefix#1#2{%
  \begingroup
    \@temptokena{#2}%
    \toks@\expandafter{#1}%
    \xdef#1{\the\@temptokena\the\toks@}%
  \endgroup
}
\long\def\filehook@prefixwarg#1#2{%
  \begingroup
    \@temptokena{#2}%
    \toks@\expandafter{#1{##1}}%
    \edef\@tempa{\the\@temptokena\the\toks@}%
    \expandafter\gdef\expandafter#1\expandafter##\expandafter1\expandafter{\@tempa}%
  \endgroup
}
\def\filehook@addtohook#1#2#3{%
  \begingroup
  \edef\@tempa{#3}%
  \edef\@tempa{#2\filehook@ensureext{\@tempa}}%
  \@ifundefined{\@tempa}{\global\@namedef{\@tempa}{}}{}%
  \expandafter\endgroup
  \expandafter#1\csname\@tempa\endcsname
}
\newcommand*\AtBeginOfIncludes{%
  \filehook@append\filehook@include@@atbegin
}
\newcommand*\AtEndOfIncludes{%
  \filehook@prefix\filehook@include@@atend
}
\newcommand*\AfterIncludes{%
  \filehook@prefix\filehook@include@@after
}
\newcommand*\AtBeginOfIncludeFile[1]{%
  \filehook@addtohook\filehook@append\filehook@include@atbegin@{\filehook@ensuretex{#1}}%
}
\newcommand*\AtEndOfIncludeFile[1]{%
  \filehook@addtohook\filehook@prefix\filehook@include@atend@{\filehook@ensuretex{#1}}%
}
\newcommand*\AfterIncludeFile[1]{%
  \filehook@addtohook\filehook@prefix\filehook@include@after@{\filehook@ensuretex{#1}}%
}
\newcommand*\AtBeginOfInputs{%
  \filehook@append\filehook@input@@atbegin
}
\newcommand*\AtEndOfInputs{%
  \filehook@prefix\filehook@input@@atend
}
\newcommand*\AtBeginOfInputFile{%
  \filehook@addtohook\filehook@append\filehook@input@atbegin@
}
\newcommand*\AtEndOfInputFile{%
  \filehook@addtohook\filehook@prefix\filehook@input@atend@
}
\newcommand*\AtBeginOfFiles{%
  \filehook@append\filehook@@atbegin
}
\newcommand*\AtEndOfFiles{%
  \filehook@prefix\filehook@@atend
}
\newcommand*\AtBeginOfEveryFile{%
  \filehook@append\filehook@every@@atbegin
}
\newcommand*\AtEndOfEveryFile{%
  \filehook@prefix\filehook@every@@atend
}
\newcommand*\AtBeginOfFile{%
  \filehook@addtohook\filehook@append\filehook@atbegin@
}
\newcommand*\AtEndOfFile{%
  \filehook@addtohook\filehook@prefix\filehook@atend@
}
\newcommand*\AtBeginOfClassFile{%
    \@ifnextchar*
        {\AtBeginOfXFile@star\@clsextension}%
        {\AtBeginOfXFile@normal\@clsextension}%
}
\newcommand*\AtBeginOfPackageFile{%
    \@ifnextchar*
        {\AtBeginOfXFile@star\@pkgextension}%
        {\AtBeginOfXFile@normal\@pkgextension}%
}
\def\AtBeginOfXFile@star#1*#2{%
    \@ifl@aded{#1}{#2}%
        {\@firstofone}%
        {\AtBeginOfXFile@normal{#1}{#2}}%
}
\def\AtBeginOfXFile@normal#1#2{%
    \AtBeginOfFile{#2.#1}%
}
\newcommand*\AtEndOfClassFile{%
    \@ifnextchar*
        {\AtEndOfXFile@star\@clsextension}%
        {\AtEndOfXFile@normal\@clsextension}%
}
\newcommand*\AtEndOfPackageFile{%
    \@ifnextchar*
        {\AtEndOfXFile@star\@pkgextension}%
        {\AtEndOfXFile@normal\@pkgextension}%
}
\def\AtEndOfXFile@star#1*#2{%
    \@ifl@aded{#1}{#2}%
        {\@firstofone}%
        {\AtEndOfXFile@normal{#1}{#2}}%
}
\long\def\AtEndOfXFile@normal#1#2#3{%
    \AtEndOfFile{#2.#1}{\AtEndOfPackage{#3}}%
}
\newcommand*\ClearHook{%
    \begingroup
    \def\filehook@prefix##1##2{%
        \gdef##1{}%
        \endgroup
    }%
    \let\filehook@append\filehook@prefix
}
\let\filehook@orig@@input@\@input@
\let\filehook@orig@@iinput\@iinput
\def\@input@#1{%
  \@ifnextchar\clearpage
    {%
     \filehook@every@atbegin{#1}%
     \filehook@include@atbegin{#1}%
     \filehook@orig@@input@{#1}%
     \filehook@include@atend{#1}%
     \clearpage
     \filehook@include@after{#1}%
     \filehook@every@atend{#1}%
     \@gobble
    }%
    {\filehook@orig@@input@{#1}}%
}
\def\filehook@@iinput#1{%
  \filehook@every@atbegin{#1}%
  \filehook@input@atbegin{#1}%
  \filehook@orig@@iinput{#1}%
  \filehook@input@atend{#1}%
  \filehook@every@atend{#1}%
}
\let\@iinput\filehook@@iinput
\def\filehook@swap#1#2{#2#1}
\def\filehook@ensureext#1{%
    \expandafter\filehook@@ensureext#1\empty.tex\empty\empty
}
\def\filehook@@ensureext#1.#2\empty#3\empty{#1.#2}
\def\filehook@ensuretex#1{%
    \expandafter\filehook@@ensuretex#1\empty.tex\empty\empty
}
\def\filehook@@ensuretex#1.tex\empty#2\empty{#1.tex}
\iffilehook@newfmt
\expandafter\def\expandafter\latex@InputIfFileExists\expandafter{%
  \expandafter\protect\csname InputIfFileExists\space\endcsname
}
\expandafter\long\expandafter\def\csname latex@InputIfFileExists\space\endcsname#1#2{%
  \IfFileExists{#1}%
    {%
  \expandafter\@swaptwoargs\expandafter
      {\@filef@und}{#2\@addtofilelist{#1}\@@input}}}
\else
\long\def\latex@InputIfFileExists#1#2{%
  \IfFileExists{#1}%
    {#2\@addtofilelist{#1}%
     \@@input\@filef@und
    }%
}
\fi
\DeclareRobustCommand\filehook@default@InputIfFileExists[2]{%
  \IfFileExists{#1}%
    {\expandafter\filehook@swap
     \expandafter{\@filef@und}%
     {#2\@addtofilelist{#1}%
     \filehook@every@atbegin{#1}%
     \filehook@atbegin{#1}%
     \@@input}%
     \filehook@atend{#1}%
     \filehook@every@atend{#1}%
    }%
}
\filehook@glet{filehook@default@InputIfFileExists}{filehook@default@InputIfFileExists}%
\DeclareRobustCommand\filehook@@default@InputIfFileExists[2]{%
  \filehook@let{InputIfFileExists}{filehook@InputIfFileExists}%
  \IfFileExists{#1}%
    {\expandafter\filehook@swap
     \expandafter{\@filef@und}%
     {#2\@addtofilelist{#1}%
     \filehook@atbegin{#1}%
     \@@input}%
     \filehook@atend{#1}%
    }%
}
\filehook@glet{filehook@@default@InputIfFileExists}{filehook@@default@InputIfFileExists}%
\AtBeginOfPackageFile{scrlfile}{%
   \filehook@glet{InputIfFileExists}{latex@InputIfFileExists}%
}%
\AtEndOfPackageFile*{scrlfile}{%
   \RequirePackage{filehook-scrlfile}%
}%
\AtBeginOfPackageFile*{fink}{%
   \RequirePackage{kvoptions}%
   \begingroup
   \filehook@let{InputIfFileExists}{latex@InputIfFileExists}%
}%
\AtEndOfPackageFile*{fink}{%
   \edef\@tempa{\noexpand\PassOptionsToPackage{mainext=\fnk@mainext,maindir=\fnk@maindir}{currfile}}%
   \expandafter\endgroup\@tempa
   \RequirePackage{filehook-fink}%
}%
\AtBeginOfClassFile{memoir}{%
  \filehook@let{InputIfFileExists}{latex@InputIfFileExists}%
  \let\@iinput\filehook@orig@@iinput
}%
\AtEndOfClassFile*{memoir}{%
  \let\@iinput\filehook@@iinput
  \RequirePackage{filehook-memoir}%
}%
\filehook@cmp{InputIfFileExists}{filehook@InputIfFileExists}%
  {}% already set up
  {%
    \filehook@cmp{InputIfFileExists}{latex@InputIfFileExists}%
      {%
        \filehook@let{filehook@InputIfFileExists}{filehook@default@InputIfFileExists}%
        \filehook@let{filehook@@InputIfFileExists}{filehook@@default@InputIfFileExists}%
        \filehook@let{InputIfFileExists}{filehook@InputIfFileExists}%
      }%
      {%
        \iffilehook@force
          \filehook@let{filehook@InputIfFileExists}{filehook@default@InputIfFileExists}%
          \filehook@let{filehook@@InputIfFileExists}{filehook@@default@InputIfFileExists}%
          \filehook@let{InputIfFileExists}{filehook@InputIfFileExists}%
          \PackageWarning{filehook}{Detected unknown definition of \string\InputIfFileExists.^^J%
                                    The 'force' option of 'filehook' is in effect. Macro is overwritten with default!}%
        \else
          \PackageError{filehook}{Detected unknown definition of \string\InputIfFileExists.^^J%
                                  Use the 'force' option of 'filehook' to overwrite it.}{}%
        \fi
      }%
  }%
\AtBeginDocument{%
    % Check if definition got changed again. For the new LaTeX format we check again \InputIfFileExists<space>,
    % for the old format to \InputIfFileExists directly.
    \filehook@cmp{InputIfFileExists}{filehook@InputIfFileExists}{}{%
        \PackageWarning{filehook}{Macro \string\InputIfFileExists\space got redefined after 'filehook' was loaded.^^J%
                                  Certain file hooks might now be dysfunctional!}%
    }%
}
\endinput
%%
%% End of file `filehook-2019.sty'.
