% File:        classlist.sty
% Version:     2005/06/19 v1.1
% Author:      Heiko Oberdiek
% Email:       <oberdiek@uni-freiburg.de>
%
% Copyright:   Copyright (C) 2005 Heiko Oberdiek.
%
%              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 Heiko Oberdiek.
%
% Function:    This package records the loaded classes and stores
%              them in a list.
%
% Background:  This packages is an answer of a newsgroup question:
%                Newsgroup: comp.text.tex
%                Subject: Finding the Document Class
%                From: Herber Schulz <>
%                Date: 18 Jun 2005 13:16:49 -0500
%                Message-ID:
%                  <herbs-D55DB9.13170418062005@news.isp.giganews.com>
%
% Use:         Load this package before \documentclass:
%                \RequirePackage{classlist}
%                \documentclass[some,options]{whatever}
%
%              If used after \documentclass, \@filelist is
%              parsed for classes. The additional data
%              specified options and requested version is no
%              longer available here.
%
%              \MainClass contains the first loaded class.
%              \ClassList stores the class entries, eg.
%                 \ClassList -> \ClassListEntry{myarticle}{a4paper}{}
%                               \ClassListEntry{article}{}{}
%              \ClassListEntry has three arguments:
%                 #1: class name
%                 #2: options given in \documentclass/\LoadClass
%                 #3: requested version, not the version of class
%              \PrintClassList prints the list on screen it can be
%                 configured by
%              \PrintClassListTitle for the title and
%              \PrintClassListEntry for formatting the entries.
%
% History:
%              2005/06/19 v1.0:
%                * first published version
%                  (comp.text.tex, CTAN).
%              2005/06/19 v1.1:
%                * After \documentclass the package looks
%                  at \@filelist instead of aborting with error.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{classlist}%
  [2005/06/19 v1.0 Record loaded classes (HO)]

\let\ClassList\@empty
\let\MainClassName\relax

\ifx\@classoptionslist\relax

  % before \documentclass

  \let\CL@org@fileswith@pti@ns\@fileswith@pti@ns
  \def\@fileswith@pti@ns#1[#2]#3[#4]{%
    % #1: \@clsextension
    % #2: options of \documentclass/\LoadClass
    % #3: class name
    % #4: requested version
    \ifx#1\@clsextension
      \@ifl@aded#1{#3}{%
        \PackageInfo{classlist}{%
          Skipping class `#3', because\MessageBreak
          this class is already loaded%
        }%
      }{%
        \@ifundefined{MainClassName}{%
          \def\MainClassName{#3}%
        }{}%
        \@temptokena\expandafter{%
          \ClassList
          \ClassListEntry{#3}{#2}{#4}%
        }%
        \edef\ClassList{\the\@temptokena}%
      }%
    \fi
    \CL@org@fileswith@pti@ns{#1}[{#2}]{#3}[{#4}]%
  }

\else

  % after \documentclass
  \PackageInfo{classlist}{Use \string\@filelist\space method}%

  \let\ClassListEntry\relax
  \expandafter\def\expandafter\CL@test
      \expandafter#\expandafter1\@clsextension#2\@nil{%
    \ifx\\#2\\%
      % name does not contain \@clsextension
    \else
      \expandafter\CL@test@i\CL@entry\@nil
    \fi
  }%
  \expandafter\def\expandafter\CL@test@i
      \expandafter#\expandafter1\@clsextension#2\@nil{%
    \ifx\\#2\\%
      \@ifundefined{opt@\CL@entry}{%
      }{%
        \@ifundefined{MainClassName}{%
          \let\MainClassName\CL@entry
        }{%
        }%
        \edef\ClassList{%
          \ClassList
          \ClassListEntry{\CL@entry}{}{}%
        }%
      }%
    \else
      % names with more than one \@clsextension are not supported.
    \fi
  }
  \@for\CL@entry:=\@filelist\do{%
    \expandafter\expandafter\expandafter\CL@test\expandafter
        \CL@entry\@clsextension\@nil
  }%
\fi

\providecommand*{\PrintClassListEntry}[3]{%
  \toks@{* #1}%
  \typeout{\the\toks@}%
}
\providecommand*{\PrintClassListTitle}{%
  \typeout{Class list:}%
}
\providecommand*{\PrintClassList}{%
  \begingroup
    \let\ClassListEntry\PrintClassListEntry
    \PrintClassListTitle
    \ClassList
  \endgroup
}

\def\CL@InfoEntry#1#2#3{%
  \advance\count@ by \@ne
  \def\x{#2}%
  \@onelevel@sanitize\x
  \edef\CL@Info{%
    \CL@Info
    \noexpand\MessageBreak
    (\the\count@) %
    #1 [\x]%
    \ifx\\#3\\%
    \else
      \space[#3]%
    \fi
  }%
}
\AtBeginDocument{%
  \begingroup
    \count@=\z@
    \def\CL@Info{Class List:}%
    \let\ClassListEntry\CL@InfoEntry
    \ClassList
    \let\on@line\@empty
    \PackageInfo{classlist}{\CL@Info}
  \endgroup
}
\endinput
