\ProvidesClass{giexam}
%
% Based on exam class, with my custom macros
%
\newif\iflecturesinline
\lecturesinlinefalse
\DeclareOption{inlec}{\lecturesinlinetrue}% Inline Lectures
\DeclareOption{soutlec}{\lecturesinlinefalse}% Standout Lectures

\newif\ifmultipleassignmentsperpage
\multipleassignmentsperpagetrue
\DeclareOption{multiass}{\multipleassignmentsperpagetrue}
\DeclareOption{singass}{\multipleassignmentsperpagefalse}

\newif\ifspaces
\spacesfalse
\DeclareOption{spaces}{\spacestrue}
\DeclareOption{nospaces}{\spacesfalse}

\newif\ifsol
\DeclareOption{sol}{\soltrue}
\DeclareOption{nosol}{\solfalse}

\newif\ifTwoColumn
\TwoColumnfalse
\DeclareOption{2col}{\TwoColumntrue}
\DeclareOption{sol2}{\soltrue\TwoColumntrue}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{exam}}
\ProcessOptions
\LoadClass{exam}
\ifTwoColumn\typeout{Typesetting with two columns}
\else\typeout{Typesetting with one column.}\fi
%\ProcessOptions

%
% Packages, etc.
%
\RequirePackage{geometry}
\RequirePackage{gimac}% Draft and twocolumn options are ignored

% Hints and notes
\newcommand{\hint}[2][Hint]{{\footnotesize {\sc #1: }#2}}
\newcommand{\hintb}[2][Hint]{{\footnotesize [{\sc #1: }#2]}}
\newcommand{\note}[1]{{\footnotesize #1}}% Different definition in gimac
\newcommand{\noteb}[1]{{\footnotesize [#1]}}

% Starred items etc. TODO: Make code better! \ifnum \@enumdepth or something
\newcommand{\itemopt}{\item[\textasteriskcentered]}

% Environment for instructions
\newenvironment{instr}[1][.9\linewidth]{%
  \begin{center}%
  \begin{minipage}{#1}\itshape\small%
  %\setlength{\parindent}{2ex}
  \begin{framed}%
  \begin{compactitem}[\textbullet]%
}{%
  \end{compactitem}%
  \end{framed}%
  \end{minipage}%
  \end{center}%
}

% Environment for Assignments
\newenvironment{assignment}[2]{%
    \setcounter{hwpart}{0}\addtocounter{hwno}{1}\vspace{\bigskipamount}%
    \noindent{\large \bf Assignment~\arabic{hwno}:} Assigned {#1}. Due {#2}
    %\begin{enumerate}[\hspace{2ex}1.]\setlength{\itemsep}{0pt}
    \begin{questions}\setlength{\itemsep}{0pt}
}{%
    \end{questions}
    %\end{enumerate}
    \ifmultipleassignmentsperpage\else\newpage\fi
}
\newcounter{hwno}
\newcounter{hwpart}
\newcommand{\hwpart}[1][Part]{
    \addtocounter{hwpart}{1}\uplevel{\noindent\textsc{#1 \Roman{hwpart}:}}}

% Leave spaces (for multipage exams)
% \newcommand{\LeaveSpaces}{1}
% \newcommand{\ifs}[1]{\ifnum\LeaveSpaces=0{}\else{#1}\fi}
\newcommand{\solbreak}{\ifspaces\newpage\fi}
\newcommand{\solspace}[1]{\ifspaces\vspace{\stretch{#1}}\fi}
\newcommand{\blankpage}{\ifspaces\fullwidth{\noindent\centering This page has been left blank for your work.}\newpage\fi}

\newcommand{\mygradetable}{%
    \small
    \newlength{\extrarowheight}
    \setlength{\extrarowheight}{1ex}
    \newlength{\extrarowdepth}
    \setlength{\extrarowdepth}{1.5ex}%

    \addtolength{\extrarowheight}{\baselineskip}
    \newcommand{\Startrow}{\rule{0pt}{\extrarowheight}}
    \newcommand{\Endrow}{\\[\extrarowdepth]}
    \begin{tabular}[c]{|c|c|}
	\hline
	\Startrow Question & Points\Endrow
	\hline
	\newcounter{i}\forloop{i}{1}{\value{i}<\numquestions \OR \value{i}=\numquestions}{\Startrow \arabic{i} & \Endrow\hline}
	\Startrow Total & \\
	\relax[\numpoints] & \Endrow
	\hline
    \end{tabular}
}

% Titles and date %{{{1
\renewcommand{\title}[1]{\def\doctitle{#1}}
\newcommand{\subtitle}[1]{\def\docsubtitle{#1}}
\renewcommand{\date}[1]{\def\docdate{#1}}
\let\docdate\@empty
\let\docsubtitle\@empty
\renewcommand{\maketitle}{%
\begin{center}
{\Large\bf\doctitle}%
\markboth{\hspace{4ex}\doctitle\hfill}{\hfill\doctitle\hspace{4ex}}%
\ifx\@empty\docsubtitle\else%
\medskip\\
{\large\docsubtitle}%
\fi
\ifx\@empty\docdate\else%
\smallskip\\
{\small\docdate}%
\fi%
\bigskip%
\end{center}
}

% Questions and subparts.%{{{1
\newcounter{exampartno}\setcounter{exampartno}{1}
\newcommand{\exampart}[1]{\fullwidth{\Large\bf Part \arabic{exampartno}: #1}\addtocounter{exampartno}{1}}

% Only define an environment if it's not defined before
\newcommand\provenvironment[3]{
\@ifundefined{#1}
    {\newenvironment{#1}{#2}{#3}}
    {}
}

\newenvironment{mcparts}[1][2]{%
    \vspace{-1em}\begin{multicols}{#1}\begin{parts}
}{%
    \end{parts}\end{multicols}\vspace{-1em}%
}

\provenvironment{parts}{
    \begin{enumerate}
    \setlength{\itemsep}{\smallskipamount}
}{
    \end{enumerate}
}

\provenvironment{questions}{
    \begin{enumerate}[\hspace{-3ex}1.]
    \setlength{\itemsep}{\medskipamount}
}{
    \end{enumerate}
}

\@ifundefined{questionshook}{\newcommand{\questionshook}{}}{}
\@ifundefined{partshook}{\newcommand{\partshook}{}}{}
\@ifundefined{subpartshook}{\newcommand{\subpartshook}{}}{}
\@ifundefined{subsubpartshook}{\newcommand{\subsubpartshook}{}}{}
\renewcommand{\questionshook}{\setlength{\parindent}{2ex}}
\renewcommand{\partshook}{\setlength{\parindent}{2ex}}

% Environment for solutions. (do \solfalse to omit solutions) %{{{1
\newenvironment{qn}{\ifsol\itshape\fi}{\ifsol\normalfont\fi}
\newenvironment{sol}[1][Solution]{%
    \begin{proof}[#1]%
    \setlength{\parindent}{2ex}
}{%
    \end{proof}%
}

% Theorems %{{{1
\newtheorem{claim}[theorem]{Claim}

% Unnumbered theorem / prop
\theoremstyle{definition}
\newtheorem{problem}[theorem]{Problem}

% Macros for writing lecture schedule. %{{{1
%
\newcounter{lecture}
\setcounter{lecture}{0}
%\newcommand{\TheLecture}[1]{\textit{L\rlap{\arabic{lecture},}\phantom{88,} #1.}\hspace{2ex}}
\newcommand{\topicbullet}{\textbullet\hspace{1ex}}
\newcommand{\subtopicbullet}{--\hspace{1ex}}
\newcommand{\subsubtopicbullet}{\textasteriskcentered\hspace{1ex}}


\newlength{\topicindent}
\settowidth{\topicindent}{\hbox{\topicbullet}}
\newlength{\topicsmargin}
\iflecturesinline
    \setlength{\topicsmargin}{\topicindent}
\else
    \settowidth{\topicsmargin}{\textit{L88, Mon.\ 88/88.}}
\fi
\addtolength{\topicsmargin}{.25cm}

\newlength{\subtopicsmargin}%For indenting subtopic lists.
\setlength{\subtopicsmargin}{\topicsmargin}
\addtolength{\subtopicsmargin}{\topicindent}

\newlength{\subtopicindent}%For indenting subsubtopic lists.
\settowidth{\subtopicindent}{\hbox{\subtopicbullet}}
\newlength{\subsubtopicsmargin}
\setlength{\subsubtopicsmargin}{\subtopicsmargin}
\addtolength{\subsubtopicsmargin}{\subtopicindent}

\newlength{\marginlength}

\newif\ifshowlecture
\newif\ifsubtopics
\newif\ifsubsubtopics
\showlecturefalse\subtopicsfalse\subsubtopicsfalse

\makeatletter
\newcommand{\TheLecture}{\ignorespaces
    \iflecturesinline
	\textbf{Lecture \arabic{lecture}, \lecturetext\ifx\lecturetitle\@empty.\else: \lecturetitle.\fi}
    \else
	\ifsubsubtopics
	    \setlength{\marginlength}\subsubtopicsmargin
	\else\ifsubtopics
	    \setlength{\marginlength}\subtopicsmargin
	\else
	    \setlength{\marginlength}\topicsmargin
	\fi\fi
	\makebox[\marginlength][l]{\hspace{.25cm}\textit{L\arabic{lecture}, \lecturetext.}}
    \fi
}
\newcommand{\lecture}[2][\@empty]{\global\showlecturetrue\gdef\lecturetext{#2}\global\edef\lecturetitle{#1}}
\makeatother

\newcommand{\bulletsymbol}{\ifsubsubtopics\subsubtopicbullet\else\ifsubtopics\subtopicbullet\else\topicbullet\fi\fi}
\newcommand{\topic}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\end{subtopics}\fi
    \showtopic}
\newcommand{\subtopic}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\else\begin{subtopics}\fi
    \showtopic
}
\newcommand{\subsubtopic}{%
    \ifsubtopics\else
	\typeout{subsubtopics used with subtopics}
	%XXX Raise an error and stop
    \fi
    \ifsubsubtopics\else\begin{subsubtopics}\fi
    \showtopic
}
\newcommand{\showtopic}{
    \ifshowlecture
	\global\showlecturefalse\addtocounter{lecture}{1}
	\iflecturesinline
	    \fullwidth{\TheLecture}
	    \item
	\else
	    \item[\llap{\TheLecture}\bulletsymbol]
	\fi
    \else
	\item[\bulletsymbol]
    \fi}

\newenvironment{topics}{%
    \begin{list}{\topicbullet}{\leftmargin=\topicsmargin\labelwidth=\topicindent\labelsep=0ex\itemsep=0pt\topsep=0pt}
}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\end{subtopics}\fi
    \end{list}
}
\newenvironment{subtopics}{
    \begin{list}{\subtopicbullet}{\leftmargin=\topicindent\labelsep=0ex\topsep=0pt}\subtopicstrue%
}{%
    \end{list}\subtopicsfalse
}
\newenvironment{subsubtopics}{%
    \begin{list}{\subsubtopicbullet}{\leftmargin=\subtopicindent\labelsep=0ex\topsep=0pt}\subsubtopicstrue
}{%
    \end{list}\subsubtopicsfalse
}
%}}}

\ifspaces
  \solfalse
  \geometry{margin=2cm}
  \pagestyle{head}
  \runningheader{\exdate}{\exname}{Page \thepage~of \numpages}
  \runningheadrule
  \firstpageheader{}{}{\textbf{Name: }\rule{5cm}{.1mm}}
\else
  \pagestyle{empty}
  \geometry{margin=2cm}
\fi

\ifsol
  \spacesfalse
  \pagestyle{head}
  \runningheader{\exname\ Solutions}{}{\thepage}
\fi

\ifTwoColumn
  \typeout{Got here.}
  \geometry{landscape,includehead,headheight=.25cm,headsep=.05cm,left=1cm,right=1cm,top=.7cm,bottom=1cm}
  \twocolumn
\fi

%
% Convenience math symbols / macros that probably don't belong to gimac.
% 
\renewcommand{\epsilon}{\varepsilon}
\newcommand{\suchthat}{\backepsilon}
\newcommand{\cnot}{\centernot}
\newcommand{\decreasesto}{\searrow}
\newcommand{\increasesto}{\nearrow}
\newcommand{\weaklyto}{\xrightarrow{\text{w}}}
\newcommand{\cross}{\times}
\newcommand{\closure}[1]{\overline{#1}}
\newcommand{\interior}[1]{\mathring{#1}}
\newcommand{\union}{\bigcup}
\newcommand{\intersection}{\bigcap}
\newcommand{\intersect}{\intersection}

\newcommand{\frowny}{\ensuremath{\ddot{\frown}}}
\newcommand{\smiley}{\ensuremath{\ddot{\smile}}}

\newcommand{\xaxis}{$x$-axis}
\newcommand{\yaxis}{$y$-axis}
\newcommand{\zaxis}{$z$-axis}

\newcommand{\prop}[1]{\noindent\textbf{#1:}}
\providecommand{\qed}{\raisebox{-1em}{QED}}

% Commonly used operators
\DeclareMathOperator{\cosec}{cosec}
\DeclareMathOperator{\cosech}{cosech}
\DeclareMathOperator{\sech}{sech}
\DeclareMathOperator{\im}{im}
\DeclareMathOperator{\re}{Re}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\Span}{span}
\DeclareMathOperator{\Ad}{adj}
\DeclareMathOperator{\mat}{Mat}
\DeclareMathOperator{\ann}{ann}
\DeclareMathOperator{\co}{co}
\newcommand{\coc}{\overline{\co}}
\DeclareMathOperator{\esssup}{ess\,sup}

% Commonly used constructs
\newcommand{\ed}{$\epsilon$-$\delta$\xspace}
\newcommand{\dlim}{\lim\limits}%display style limits
\newcommand{\dliminf}{\liminf\limits}
\newcommand{\dlimsup}{\limsup\limits}
\newcommand{\dlimto}[2][x]{\dlim_{#1 \to #2}}
\newcommand{\fracpart}[1]{\boldsymbol{\{}#1\boldsymbol{\}}}
\newcommand{\intpart}[1]{\boldsymbol{[}#1\boldsymbol{]}}
\newcommand{\hatsym}{^{\wedge}}
\newcommand{\dmax}{\max\limits}
\newcommand{\dmin}{\min\limits}

\newcommand{\pts}[2][\@empty]{\textit{\tiny (#2 pts\ifx\@empty#1\else, #1\fi)}}

% Vectors
\newcommand{\colvec}[1]{\iftextstyle\bigl(\begin{smallmatrix}#1\end{smallmatrix}\bigr)\else\begin{pmatrix}#1\end{pmatrix}\fi}
\newcommand{\scolvec}[1]{\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}

% Matrices
%\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
%    \hskip -\arraycolsep
%    \let\@ifnextchar\new@ifnextchar
%    \array{#1}%
%}

% Misc
\newcommand{\per}{_\text{per}}


% vim: set ft=tex :

