%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
% This is file 'pagerange.sty', version 0.2.                                %
%                                                                           %
% 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 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.        %
%                                                                           %
% The LPPL maintenance status of this software is 'author-maintained'.      %
%                                                                           %
% This software is provided 'as it is', without warranty of any kind,       %
% either expressed or implied, including, but not limited to, the           %
% implied warranties of merchantability and fitness for a particular        %
% purpose.                                                                  %
%                                                                           %
% Copyright (c) 2009 Ahmed Musa (a.musa@rocketmail.com).                    %
%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%

\ProvidesPackage{pagerange}[2009/11/10 v0.2 Split page ranges intelligently]
\NeedsTeXFormat{LaTeX2e}[1996/12/01]
\def\prg@warn#1{\PackageWarningNoLine{pagerange}{#1}}
\def\prg@err#1{\PackageError{pagerange}{#1}{\@ehc}}
\RequirePackage{lastpage}[1994/06/25]
\RequirePackage{xkeyval}[2008/08/13]
\ifcase\ifx\eTeXversion\@undefined\@ne\else
  \ifnum\eTeXversion<\tw@\@ne\fi\fi\z@
\else
  \prg@err{This package requires eTeX version 2 or later}%
\fi
\def\prg@ifdef#1{\csname @\ifdefined#1\expandafter first\else
  \expandafter second\fi oftwo\endcsname
}
\def\prg@ifundef#1{%
  \prg@ifdef#1{\csname @\ifx#1\relax\expandafter first\else
    \expandafter second\fi oftwo\endcsname}{\@firstoftwo}%
}
\def\prg@check@ifdef#1{%
  \expandafter\prg@ifundef\csname#1\endcsname{}{%
    \prg@err{\expandafter\noexpand\csname#1\endcsname already defined}}%
  \prg@check@ifdef
}
\prg@check@ifdef{pagerange}{pagestart}{pageend}{orighyphencode}%
  \iffalse\fi\fi\endcsname{}{}
\newcount\orighyphencode\orighyphencode=\the\catcode45\relax
\AtEndOfPackage{\catcode45=\orighyphencode}
\@makeother\-
\chardef\active=13\relax
\begingroup
  \catcode`\&=3\relax
  \long\gdef\prg@ifblank#1{% credit: ifmtarg.sty
    \prg@ifblank@i#1&&\@secondoftwo\@firstoftwo:}
  \long\gdef\prg@ifblank@i#1#2&#3#4#5:{#4}
\endgroup
\long\def\prg@xifblank#1{\begingroup
  \protected@edef\@tempa{\endgroup\noexpand\prg@ifblank{#1}}%
  \@tempa
}
\define@boolkeys[KV]{pagerange}[prg@]{acceptempty,hyperlink}[true]
\define@cmdkey[KV]{pagerange}[prg@]{tie}[-]{%
  \edef\prg@tie{\prg@tie}%
  \prg@ifmacro\prg@tie
}
\def\prg@ifmacro#1{%
  \prg@ifdef#1{%
    \expandafter\def\expandafter\getparameter
      \detokenize{macro}:##1->##2##3@stop{\prg@ifblank{##1}{%
        \prg@ifblank{##3}{}{\prg@ismacro@err#1}}{\prg@ismacro@err#1}
    }%
    \expandafter\getparameter\meaning#1@stop
  }{\prg@err{\string#1 undefined}}%
}
\def\prg@ismacro@err#1{\prg@err{Illegal 'tie'}}
\DeclareOptionX*{\prg@warn{Unknown option '\CurrentOption' ignored}}
\ExecuteOptionsX<pagerange>{tie}
\ProcessOptionsX*<pagerange>\relax
\gdef\pagerangeoptions#1{\setkeys[KV]{pagerange}{#1}}
\long\def\prg@forall#1#2\do#3{%
  #3{#1}%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \ifx\relax#2\relax%
    \relax
  \else
    \prg@forall#2\do{#3}%
  \fi
}
\let\pagestart\@empty
\let\pageend\@empty
\newcount\prg@cntx\prg@cntx=\z@\relax
\long\def\prg@split@pagerange#1{%
  \def\reserved@a{#1}%
  \ifx\reserved@a\prg@tie
    \advance\prg@cntx\@ne
  \fi
  \ifnum\prg@cntx=\z@
    \g@addto@macro\pagestart{#1}%
  \else\ifnum\prg@cntx=\@ne
    \ifx\reserved@a\prg@tie\else
      \g@addto@macro\pageend{#1}%
    \fi
  \else\ifnum\prg@cntx>\@ne
    \prg@err{Multiple ties in page range}
  \fi\fi\fi
}
\newcount\prg@cnta\prg@cnta=\z@\relax
\newcount\prg@cntb\prg@cntb=\z@\relax
\def\pagerange#1{%
  \let\pagestart\@empty
  \let\pageend\@empty
  \prg@cntx=\z@\relax
  \prg@xifblank{#1}{%
    \ifprg@acceptempty
      \prg@warn{Blank page range supplied:\MessageBreak
        I've therefore set the range as 1-1 and continued}
      \def\pagestart{1}%
      \def\pageend{1}%
    \else
      \prg@err{Blank page range}
    \fi
  }{%
    \begingroup
    \edef\@tempa{\endgroup%
      \noexpand\prg@forall#1\noexpand\do{\noexpand\prg@split@pagerange}}%
    \@tempa
  }%
  \ifx\@empty\pagestart
    \ifnum\prg@cntx=\@ne
      \def\pagestart{1}%
    \fi
  \fi
  \prg@cnta=\pagestart\relax
  \ifx\@empty\pageend
    \prg@cntb=100000\relax
    \ifnum\prg@cntx=\@ne
      \ifprg@hyperlink
        \def\pageend{\pageref{LastPage}}%
      \else
        \def\pageend{\pageref*{LastPage}}%
      \fi
    \fi
  \else
    \prg@cntb=\number\pageend\relax
  \fi
  \ifnum\prg@cnta>\prg@cntb
    \prg@err{Inconsistent page range}
  \fi
}

\endinput 