%%
%% This package can 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".
%% 
%% The Current Maintainer of this work is Lars Madsen (daleif@imf.au.dk).
%%

%% HISTORY
% 2007/02/24 initial release
% 2008/03/20 updated with new code, and old code removed as to reflect
%            changes in memoir

\ProvidesPackage{memexsupp}[2008/03/20 v0.1 by daleif]

% only for the memoir class
\@ifclassloaded{memoir}{}{
  \PackageError{memsupp}{This package can only be used together with
    the memoir class.}{Either outcomment this package or switch to the
  memoir class}
}

\newcounter{tempcounter}
\newcommand\MXScreatesimplemark[2]{%
  \@namedef{#1mark}{\markboth{\memUChead{#2}}{\memUChead{#2}}}}
\newcommand\MXScreatemark[5]{%
  % #1 type: chapter, section, subsection, subsubsection
  % #2 mark type: both, right
  % #3 whether to show the counter part in mainmatter (true/false),
  % #4 prefix: text before number (including the spacing)
  % #5 number postfix: (. plus spacing)
  \@tempswafalse%
  \nametest{#3}{true}%
  \ifsamename\@tempswatrue\def\@tempa{00}\fi
  \nametest{#3}{false}%
  \ifsamename\@tempswatrue\def\@tempa{01}\fi
  \if@tempswa\else
  \@memerror{bad boolean value (#3)}%
  {I expect the use of 'true' or 'false'}%
  \fi
  \expandafter\if\@tempa
    \@namedef{#1marksn}##1{##1}
  \else
    \@namedef{#1marksn}{\@gobble}
  \fi
  \@namedef{#1mark}##1{%
    \@tempswafalse%
    \nametest{#2}{both}
    \ifsamename
    \@setclcnt{#1}{tempcounter}
    \addtocounter{tempcounter}{-1}
    \markboth{%
      \memUChead{%
        \ifnum \c@secnumdepth > \value{tempcounter}
            \if@mainmatter
              \@nameuse{#1marksn}{#4\@nameuse{the#1}#5}%
            \fi
          \fi
          ##1}}{%
        \memUChead{%
          \ifnum \c@secnumdepth > \value{tempcounter}
            \if@mainmatter
              \@nameuse{#1marksn}{#4\@nameuse{the#1}#5}%
            \fi
          \fi
          ##1}}%
    \@tempswatrue
    \fi
    \nametest{#2}{right}
    \ifsamename
    \@setclcnt{#1}{tempcounter}
    \addtocounter{tempcounter}{-1}
    \markright{%
      \memUChead{%
        \ifnum \c@secnumdepth > \value{tempcounter}%
            \if@mainmatter%
              \@nameuse{#1marksn}{#4\@nameuse{the#1}#5}%
              %#4\@nameuse{the#1}#5
            \fi%
          \fi%
          ##1}}%
    \@tempswatrue
    \fi
    \if@tempswa\else
    \@memerror{%
      Unknown mark setting type (#2)
    }{%
     I expect the use of 'both' or 'right'}%	
   \fi
  }
}

\newcommand\MXSaddtopsmarks[3]{
  % #1: name of pagestyle
  % #2: code for the beginning
  % code for the end
  \expandafter\addtodef\expandafter{\csname #1pshook\endcsname}{#2}{#3}%
}






\newenvironment{MXStitlingpage}%
  {\let\footnoterule\relax
   \let\footnotesize\small
   \if@twocolumn
     \@restonecoltrue\onecolumn
   \else
     \@restonecolfalse
   \fi
   \thispagestyle{titlingpage}%
   \setcounter{page}{\@ne}%
   \let\MXSoldnewpage\newpage
   \let\MXSoldclearpage\clearpage
   \def\newpage{\MXSoldnewpage\thispagestyle{titlingpage}}%
   \def\clearpage{\MXSoldclearpage\thispagestyle{titlingpage}}%
  }{%
   \thispagestyle{titlingpage}%
   \let\newpage\MXSoldnewpage
   \let\clearpage\MXSoldclearpage
   \if@restonecol \twocolumn \fi
   \if@twoside \cleardoublepage \else \clearpage \fi
   \setcounter{page}{\@ne}}