%% Copyright (c) 2010-2011 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.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 2008/05/04 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, filehook.ins
%% and the derived file filehook.sty.
%%
%% $Id: filehook.dtx 2033 2011-01-09 17:14:37Z martin $
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{filehook}
    [2011/01/09 v0.5 Hooks for input files]
\newif\iffilehook@force
\DeclareOption{force}{\filehook@forcetrue}
\ProcessOptions\relax
\def\filehook@include@atbegin#1{%
  \let\InputIfFileExists\filehook@@InputIfFileExists
  \@nameuse{\filehook@include@atbegin@#1}%
  \filehook@include@@atbegin
}
\def\filehook@include@@atbegin{}
\def\filehook@include@atend#1{%
  \filehook@include@@atend
  \@nameuse{\filehook@include@atend@#1}%
}
\def\filehook@include@@atend{}
\def\filehook@include@after#1{%
  \filehook@include@@after
  \@nameuse{\filehook@include@after@#1}%
}
\def\filehook@include@@after{}
\def\filehook@input@atbegin#1{%
  \let\InputIfFileExists\filehook@@InputIfFileExists
  \@nameuse{\filehook@input@atbegin@\filehook@ensureext{#1}}%
  \filehook@input@@atbegin
}
\def\filehook@input@@atbegin{}
\def\filehook@input@atend#1{%
  \filehook@input@@atend
  \@nameuse{\filehook@input@atend@\filehook@ensureext{#1}}%
}
\def\filehook@input@@atend{}
\def\filehook@atbegin#1{%
  \@nameuse{\filehook@atbegin@\filehook@ensureext{#1}}%
  \filehook@@atbegin
}
\def\filehook@@atbegin{}
\def\filehook@atend#1{%
  \filehook@@atend
  \@nameuse{\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}}%
}
\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}
\long\def\latex@InputIfFileExists#1#2{%
  \IfFileExists{#1}%
    {#2\@addtofilelist{#1}%
     \@@input\@filef@und
    }%
}
\long\gdef\filehook@default@InputIfFileExists#1#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}%
    }%
}
\long\gdef\filehook@@default@InputIfFileExists#1#2{%
  \let\InputIfFileExists\filehook@InputIfFileExists
  \IfFileExists{#1}%
    {\expandafter\filehook@swap
     \expandafter{\@filef@und}%
     {#2\@addtofilelist{#1}%
     \filehook@atbegin{#1}%
     \@@input}%
     \filehook@atend{#1}%
    }%
}
\long\def\scrlfile@InputIfFileExists#1#2{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1-@alias\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \scr@replacefile@msg{\csname #1-@alias\endcsname}{#1}%
    \expandafter\@firstoftwo
  \fi
  {%
    \expandafter\InputIfFileExists\expandafter{\csname
      #1-@alias\endcsname}{#2}%
  }%
  {\IfFileExists{#1}{%
      \scr@load@hook{before}{#1}%
      #2\@addtofilelist{#1}%
      \@@input \@filef@und
      \scr@load@hook{after}{#1}%
    }}%
}
\long\def\filehook@scrlfile@InputIfFileExists#1#2{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1-@alias\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \scr@replacefile@msg{\csname #1-@alias\endcsname}{#1}%
    \expandafter\@firstoftwo
  \fi
  {%
    \expandafter\InputIfFileExists\expandafter{\csname
      #1-@alias\endcsname}{#2}%
  }%
  {\IfFileExists{#1}{%
      \expandafter\filehook@swap
      \expandafter{\@filef@und}%
      {\scr@load@hook{before}{#1}%
      #2\@addtofilelist{#1}%
      \filehook@every@atbegin{#1}%
      \filehook@atbegin{#1}%
      \@@input}%
      \filehook@atend{#1}%
      \filehook@every@atend{#1}%
      \scr@load@hook{after}{#1}%
    }}%
}
\long\def\filehook@@scrlfile@InputIfFileExists#1#2{%
  \let\InputIfFileExists\filehook@InputIfFileExists
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1-@alias\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \scr@replacefile@msg{\csname #1-@alias\endcsname}{#1}%
    \expandafter\@firstoftwo
  \fi
  {%
    \expandafter\InputIfFileExists\expandafter{\csname
      #1-@alias\endcsname}{#2}%
  }%
  {\IfFileExists{#1}{%
      \expandafter\filehook@swap
      \expandafter{\@filef@und}%
      {\scr@load@hook{before}{#1}%
      #2\@addtofilelist{#1}%
      \filehook@atbegin{#1}%
      \@@input}%
      \filehook@atend{#1}%
      \scr@load@hook{after}{#1}%
    }}%
}
\AtBeginOfPackageFile*{scrlfile}{%
   \let\InputIfFileExists\latex@InputIfFileExists
}%
\AtEndOfPackageFile*{scrlfile}{%
   \RequirePackage{filehook-scrlfile}%
}%
\AtBeginOfPackageFile*{fink}{%
   \RequirePackage{kvoptions}%
   \begingroup
   \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}{%
  \let\filehook@@InputIfFileExists\latex@InputIfFileExists
  \let\InputIfFileExists\latex@InputIfFileExists
  \let\@iinput\filehook@orig@@iinput
}%
\AtEndOfClassFile*{memoir}{%
  \let\@iinput\filehook@@iinput
  \RequirePackage{filehook-memoir}%
}%
\ifcase
    \ifx\InputIfFileExists\filehook@InputIfFileExists 0\else
    \ifx\InputIfFileExists\latex@InputIfFileExists 1\else
    \iffilehook@force 1\else
    9%
    \fi\fi\fi
\relax% 0
\or% 1
    \let\filehook@InputIfFileExists\filehook@default@InputIfFileExists
    \let\filehook@@InputIfFileExists\filehook@@default@InputIfFileExists
    \let\InputIfFileExists\filehook@InputIfFileExists
    \iffilehook@force
      \PackageWarning{filehook}{Detected unknown definition of \string\InputIfFileExists.^^J%
                                The 'force' option of 'filehook' is in effect. Macro is overwritten with default!}%
    \fi
\else
    \PackageError{filehook}{Detected unknown definition of \string\InputIfFileExists.^^J%
                            Use the 'force' option of 'filehook' to overwrite it.}{}%
\fi
\AtBeginDocument{%
    \ifx\InputIfFileExists\filehook@InputIfFileExists\else
        \PackageWarning{filehook}{Macro \string\InputIfFileExists\space got redefined after 'filehook' was loaded.^^J%
                                  Certain file hooks might now be dysfunctional!}
    \fi
}
\endinput
%%
%% End of file `filehook.sty'.
