%% -*-mode: tex-mode -*-

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Written and (C) by Jérôme Lelong <jerome.lelong@gmail.com>            %%
%%                                                                       %%
%% This program is free software; you can redistribute it and/or modify  %%
%% it under the terms of the GNU General Public License as published by  %%
%% the Free Software Foundation; either version 3 of the License, or     %%
%% (at your option) any later version.                                   %%
%%                                                                       %%
%% This program is distributed in the hope that it will be useful,       %%
%% but WITHOUT ANY WARRANTY; without even the implied warranty of        %%
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         %%
%% GNU General Public License for more details.                          %%
%%                                                                       %%
%% You should have received a copy of the GNU General Public License     %%
%% along with this program.  If not, see <http://www.gnu.org/licenses/>. %%
%%                                                                       %%
%% This file is part of the chklref package.                             %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{chklref}%
\RequirePackage{afterpackage}

\xdef\@chk@currentfile{\jobname.tex}
\xdef\old@filename{\jobname.tex}

\def\CHK@push#1{\xdef\old@filename{{#1}\old@filename}} 
\def\CHK@pop{\expandafter\CHK@popit\old@filename\theend}
\def\CHK@popit#1#2\theend#3{\def#3{#1}%
  \xdef\old@filename{#2}} 

\newwrite\chk@write
\immediate\openout\chk@write\jobname.tex.chk%
\def\chk@print#1{%
  \begingroup
    \set@display@protect
    \immediate\write\chk@write{#1}%
  \endgroup
}

\long\def \InputIfFileExists#1#2{%
  \IfFileExists{#1}{#2\@addtofilelist{#1}%
    \CHK@push{\@chk@currentfile}%
    \xdef\@chk@currentfile{#1}%
    \@@input \@filef@und%
    \CHK@pop\@temp%
    \xdef\@chk@currentfile{\@temp}}}

\def\@input#1{%
  \IfFileExists{#1}{%
    \CHK@push{\@chk@currentfile}%
    \xdef\@chk@currentfile{#1}%
    \@@input\@filef@und%
    \CHK@pop\@temp%
    \xdef\@chk@currentfile{\@temp}}
  {\typeout{No file #1.}}}
  

%% Don't want to redefine \input toto while in preamble because it breaks
%% loading babel.
\AtBeginDocument{%
\def\input{\@ifnextchar\bgroup\@iinput\@@chk@input}
\def\@@chk@input#1 {%
  \CHK@push{\@chk@currentfile}%
  \xdef\@chk@currentfile{#1}%
  \@@input #1%
  \CHK@pop\@temp%
  \xdef\@chk@currentfile{\@temp}
}}


\def\CHK@redefine#1#2{%
  \expandafter\let\csname CHK@#1@orig\expandafter\endcsname\csname#1\endcsname%
  \expandafter\def\csname#1\endcsname##1{%
    \chk@print{#2{##1} line \the\inputlineno\space file \@chk@currentfile}%
    \expandafter\expandafter\csname CHK@#1@orig\endcsname{##1}}
}

\def\CHK@redefine@#1#2{%
  \expandafter\let\csname CHK@#1@orig\expandafter\endcsname\csname#1\endcsname%
  \expandafter\def\csname#1\endcsname##1{%
    \chk@print{#2 ##1 line \the\inputlineno\space file \@chk@currentfile}%
    \expandafter\expandafter\csname CHK@#1@orig\endcsname{##1}}
}

%\let\CHK@begin@orig=\begin
  \CHK@redefine{begin}{begin}
  \CHK@redefine{end}{end}

\AfterPackage{hyperref}{
  \AtBeginDocument{%
    \let\CHK@label@orig=\label
    \def\label#1{%
      \chk@print{label #1 line \the\inputlineno\space file \@chk@currentfile}%
      \CHK@label@orig{#1}}

    \Hy@SetCatcodes
    \def\HyPsd@ref#1{\HyPsd@@ref#1*\END}%
    \def\HyPsd@@ref#1*#2\END{%
      \ifx\\#2\\%
        \HyPsd@@@ref{#1}%
      \else
        \expandafter\HyPsd@@@ref
      \fi
    }%
    \def\HyPsd@@@ref#1{%
      \expandafter\ifx\csname r@#1\endcsname\relax
        ??%
      \else
        \expandafter\expandafter\expandafter\@car\csname r@#1\endcsname\@nil
      \fi
      \chk@print{ref #1 line \the\inputlineno\space file \@chk@currentfile}%
    }
    \let\ref=\HyPsd@ref
    \Hy@RestoreCatcodes
  }
}

\AtBeginDocument{%
  \newif\ifchk@hyperref
  \newif\ifchk@amsmath
  \@ifpackageloaded{hyperref}{\chk@hyperreftrue}{}
  \@ifpackageloaded{amsmath}{\chk@amsmathtrue}{}

  \ifchk@amsmath% %With AMS-LaTeX tags
     %%\CHK@redefine{label@in@display}{label@in@display}
     \let\CHK@label@in@display@orig=\label@in@display
     \def\label@in@display#1{%
       \chk@print{label #1 line \the\inputlineno\space file \@chk@currentfile}
       \CHK@label@in@display@orig{#1}}

     \CHK@redefine@{eqref}{ref}
     \CHK@redefine@{ref}{ref}
     \CHK@redefine@{label}{label}
     \CHK@redefine@{citation}{citation}
     \CHK@redefine@{bibcite}{bibcite}

  \else
    \ifchk@hyperref\else
      \CHK@redefine@{label}{label}
      \CHK@redefine@{ref}{ref}
      \CHK@redefine@{citation}{citation}
      \CHK@redefine@{bibcite}{bibcite}
    \fi
  \fi
}

\endinput
