\ProvidesPackage{props}[2008/05/06 Cross-references to numbered and unnumbered propositions]

\DeclareOption{tightspacing}{%
\AtEndOfPackage{
\renewenvironment{prop}{\begin{list}{}{\stepcounter{prop}%
\renewcommand{\makelabel}{\newdisplaylabel}%
\setlength{\topsep}{0in}%
\setlength{\itemsep}{0.2\baselineskip}%
\setlength{\parsep}{0in}%
\setlength{\rightmargin}{1em}%
\setlength{\labelwidth}{\leftmargin-\labelsep}%
\renewcommand{\makelongitem}{\proplongitem}%
\renewcommand{\makeshortitem}{\propshortitem}%
}}
{\end{list}}
}
}
\ProcessOptions


% Hyperref cross-references are generated by hyperref's overloading of the refstepcounter command.  We're going to be needing to cross-reference things that aren't generated by any standard counter, so we use a dummy counter, 'hyper', to keep hyperref happy.  

\newcounter{hyper}

% \paren, by default, puts parentheses around its argument.  But in the scope of a \noparen, it does nothing.

\DeclareRobustCommand{\paren}[1]{(#1)}

\DeclareRobustCommand{\noparen}[1]
	{\let\@oldparen\paren%
	\def\paren##1{##1}%
	#1%
	\let\paren\@oldparen%
	}

% \setreftext does two things: first, it steps the "hyper" counter so that, 

\def\setreftext#1{%
	\refstepcounter{hyper}%
	\def\@currentlabel{\@neutralize{#1}}%
}

\newcommand{\refstar}{\@ifstar{\@oldref*}{\@oldref*}}	


\DeclareRobustCommand{\@neutralize}[1]{%
	\ifx\ref\refstar#1%
	\else
	\let\@oldref\ref
	\let\ref\refstar%
	#1%
	\let\ref\@oldref%
	\fi%
	}
	

\DeclareRobustCommand{\chisholm}[2]{\paren{\noparen{\ref*{#1}}#2}}

%Different sorts of items.  

\def\sitem{%
	\@ifnextchar[{\short@item}{\short@item[]}}

\def\short@item[#1]{%
	\@ifnextchar[{\short@@item{#1}}{\short@@item{#1}[#1]}}
	
\def\short@@item#1[#2]{%
	\makeshortitem{\shortitemformat{#1}}\setreftext{\shortrefformat{#2}}}


\def\litem{%
	\@ifnextchar[{\long@item}{\long@item[]}}

\def\long@item[#1]{%
	\@ifnextchar[{\long@@item{#1}}{\long@@item{#1}[#1]}}
	
\def\long@@item#1[#2]{%
	\makelongitem{\longitemformat{#1}}\setreftext{\longrefformat{#2}}}
%	\item[\hspace{\labelwidth}\hspace{\labelsep}\longitemformat{#1}]\setreftext{\longrefformat{#2}}}


\def\nitem{%
	\@ifnextchar[%
		{\num@item}%
		{\num@@item{equation}{\paren{\theequation}}{\refequation}}}

\def\num@item[#1]{%
		\@ifundefined{c@#1}%
			{\DeclareNumProp{#1}}{}%
		\@ifundefined{ref#1}%
			{\num@@item{#1}{\csname the#1\endcsname}{\csname the#1\endcsname}}%
			{\num@@item{#1}{\csname the#1\endcsname}{\csname ref#1\endcsname}}}
		

\def\num@@item#1#2#3{%
	\makeshortitem{\stepcounter{#1}\numitemformat{#2}}\setreftext{\numrefformat{#3}}}
%	\short@@item{\stepcounter{#1}#2}[#3]}
	
\def\lnitem{%
	\@ifnextchar[%
		{\longnum@item}%
		{\longnum@@item{equation}{\paren{\theequation}}{\paren{\p@equation\theequation}}}%
}

\def\longnum@item[#1]{%
		\@ifundefined{c@#1}%
			{\DeclareLongProp{#1}}{}%
		\@ifundefined{ref#1}%
			{\longnum@@item{#1}{\csname the#1\endcsname}{\csname the#1\endcsname}}%
			{\longnum@@item{#1}{\csname the#1\endcsname}{\csname ref#1\endcsname}}%
}
		

\def\longnum@@item#1#2#3{%
	\long@@item{\stepcounter{#1}#2}[#3]%
}

%Commands for the user to renew

\newcommand{\longitemformat}[1]{\textbf{#1}}

\newcommand{\longrefformat}[1]{#1}

\newcommand{\shortitemformat}[1]{#1}

\newcommand{\shortrefformat}[1]{#1}

\newcommand{\numitemformat}[1]{#1}

\newcommand{\numrefformat}[1]{#1}


%Numbered items.


\def\DeclareNumProp{%
	\@ifnextchar[%
		{\Declare@NumPropOpt}%
		{\Declare@NumProp}%
}

\def\Declare@NumProp#1{%
	\Declare@Prop{}{#1\arabic{#1}}{#1\arabic{#1}}{#1}%
}

\def\Declare@NumPropOpt[#1]{%
	\@ifnextchar[%
		{\Declare@@PropOpt{#1}}%
		{\Declare@@NumProp{#1}}%
}

\def\Declare@@NumProp#1#2{%
	\Declare@Prop{#1}{#2\arabic{#2}}{#2\arabic{#2}}{#2}%
}

\def\DeclareLongProp{%
	\@ifnextchar[%
		{\Declare@LongPropOpt}%
		{\Declare@LongProp}%
}

\def\Declare@LongProp#1{%
	\Declare@Prop{}{#1~\arabic{#1}}{#1~\arabic{#1}}{#1}%
}

\def\Declare@LongPropOpt[#1]{%
	\@ifnextchar[%
		{\Declare@@PropOpt{#1}}%
		{\Declare@@LongProp{#1}}%
}

\def\Declare@@LongProp#1#2{%
	\Declare@Prop{#1}{#2~\arabic{#2}}{#2~\arabic{#2}}{#2}%
}


\def\Declare@@PropOpt#1[#2]{%
	\@ifnextchar[%
		{\Declare@@@PropOpt{#1}{#2}}%
		{\Declare@@@Prop{#1}{#2}}%
}

\def\Declare@@@PropOpt#1#2[#3]#4{%
	\Declare@Prop{#1}{#2}{#3}{#4}%
}

\def\Declare@@@Prop#1#2#3{%
	\Declare@Prop{#1}{#2}{#2}{#3}%
}

\def\Declare@PropOpt[#1]{%
	\Declare@Prop{#1}%
}

\def\Declare@Prop#1#2#3#4{%
	\newcounter{#4}%
	\ifx\\#1\\ \else \@addtoreset{#4}{#1}\fi%
	\expandafter\gdef\csname the#4\endcsname{#2}%
	\expandafter\gdef\csname ref#4\endcsname{#3}%
}


%plumbing
\newcommand{\inlineitem}[1]
{\item[\hspace{\labelwidth}\hspace{\labelsep}#1\hspace{\labelsep}]}

\newcommand{\lineitem}[1]
{\let\oldmakelabel\makelabel%
\let\makelabel\lineitemlabel%
\item[#1]%
\let\makelabel\oldmakelabel}

\newcommand{\proplongitem}{\inlineitem}
\newcommand{\propshortitem}[1]{\item[#1]}

\newcommand{\textlongitem}[1]{#1}
\newcommand{\textshortitem}[1]{#1}

\newcommand{\makeshortitem}{\textshortitem}
\newcommand{\makelongitem}{\textlongitem}

\newcommand{\lineitemlabel}[1]
{\parbox[b]{\labelwidth}{\hspace{\labelwidth}\makebox[0pt][l]{\hspace{\labelsep}#1}\\}}

\newcommand{\newdisplaylabel}[1]
{\mbox{#1}\hfill}

%the master displayed proposition environment
\newenvironment{prop}
{\begin{list}{}{\stepcounter{prop}%
\renewcommand{\makelabel}{\newdisplaylabel}%
\setlength{\rightmargin}{\leftmargin}%
\setlength{\labelwidth}{\leftmargin-\labelsep}%
\renewcommand{\makelongitem}{\proplongitem}%
\renewcommand{\makeshortitem}{\propshortitem}%
}}
{\end{list}}

\newenvironment{flushprop}
{\begin{list}{}{\stepcounter{prop}%
\renewcommand{\makelabel}{\newdisplaylabel}%
\setlength{\leftmargin}{0cm}
\setlength{\rightmargin}{0cm}%
\setlength{\labelwidth}{\leftmargin-\labelsep}%
\renewcommand{\makelongitem}{\proplongitem}%
\renewcommand{\makeshortitem}{\propshortitem}%
}}
{\end{list}}


\newcounter{prop}


%Setting up one common counter, roman

\DeclareNumProp[section][\paren{\roman{roman}}]{roman}

% and shortcuts for it: 

\newcommand{\ritem}{\nitem[roman]}

\newcommand{\resetroman}{\setcounter{roman}{0}}



% Fixes so equations look right

\def\refstepcounter#1{\stepcounter{#1}% 
\@ifundefined{ref#1}{%
\protected@edef\@currentlabel 
{\csname p@#1\endcsname\csname the#1\endcsname}% 
}{%
\protected@edef\@currentlabel 
{\csname ref#1\endcsname}% 
}}

\def\refequation{\paren{\p@equation\theequation}}



%just boldface for new terms
% \newcommand{\newterm}[1]{\textbf{#1}}

%\def\newthing{%
%	\@ifnextchar[{\new@@thing}{\new@thing}}

%\def\new@thing#1{\new@@thing[#1]{#1}}

%\def\new@@thing[#1]#2{\newterm{#2}\setreftext{#1}}
