% Copyright 2006 Konstantin Korikov <lostclus@ua.fm>
%
% 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 2003/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
% 
% This Current Maintainer of this work is Konstantin Korikov.
%
% This work consists of all files listed in manifest.txt.
%
m4_ESKDX_INIT
m4_FILE_INIT
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{eskdtotal}[m4_dnl
m4_FILE_ID([[$Date:: 2010-01-14#$]]) Total number of tables, figures, etc]

\RequirePackage{eskdsect}

\let\ESKD@old@caption\@caption
\let\ESKD@old@bibitem\bibitem

\newcounter{ESKD@figure}
\newcounter{ESKD@table}
\newcounter{ESKD@bibitem}

\long\def\@caption#1[#2]#3{%
  \stepcounter{ESKD@#1}%
  \ESKD@old@caption{#1}[#2]{#3}}

\long\def\bibitem{%
  \stepcounter{ESKD@bibitem}%
  \ESKD@old@bibitem}

\def\ESKD@total@save#1#2{%
  \immediate\write\@auxout{%
    \string\gdef\string\ESKD@total@saved@\string#1{\arabic{#2}}}}

\AtEndDocument{%
  \ESKD@total@save{figure}{ESKD@figure}
  \ESKD@total@save{table}{ESKD@table}
  \ESKD@total@save{bibitem}{ESKD@bibitem}
  \ESKD@total@save{appendix}{appendix}
  \clearpage
  \addtocounter{page}{-1}
  \ESKD@total@save{page}{page}
  \addtocounter{page}{1}}

\newcommand{\ESKDtotal}[1]{%
  \@ifundefined{ESKD@total@saved@#1}{?}{%
    \@nameuse{ESKD@total@saved@#1}}}

m4_dnl vim:ft=tex:sw=2:ai
