%%
%%	XTeX buttons for bibliography citations and references. Simply include
%%	this in your document style like..
%%	
%%	\documentstyle[acm,xtex]{article}
%%
%%	and run latex once or twice. You'll notice boxes appearing around
%%	all your \ref's and \cite's. These will invoke xtex specials such
%%	that you can move around. See xtex documentation for more details.
%%
%%

%%
%%	the xtex Button Box
%%
\newbox\@xtexBB

\gdef\@xtexbutton#1#2{%
	\begingroup
	\setbox\@xtexBB\hbox{#1}%
	\dimen0=\wd\@xtexBB \dimen1=2pt \advance\dimen0 0.5\dimen1
	\dimen2=\ht\@xtexBB
	\dimen3=2pt \advance\dimen2 \dimen3
	\dimen4=\dp\@xtexBB \advance\dimen4\dimen3
	\count0=\dimen0
	\count1=\dimen1
	\count2=\dimen2
	\count3=\dimen4
	%%
	%% Added by cwitty@jessica.stanford.edu to force information
	%% and special onto the same page
	%%
	\hbox{%
		\special{xtex: \space%
			-\the\count1 \space
			\the\count0 \space
			-\the\count3 \space
			\the\count2 \space #2 \space }%
		\box\@xtexBB%
	}%
	\endgroup
}


\gdef\@xtexpush#1#2{\@xtexbutton{#1}{xtex-page-divert(#2)}}



%%
%%	Override the \ref macro and \pageref
%%
\gdef\ref#1{\@ifundefined{r@#1}{{\bf ??}\@warning
   {Reference `#1' on page \thepage \space 
    undefined}}{\edef\@tempa{\@nameuse{r@#1}}%
    \@xtexpush{\expandafter\@car\@tempa\@nil\null}%
	      {\expandafter\@cdr\@tempa\@nil}}}

\def\pageref#1{\@ifundefined{r@#1}{{\bf ??}\@warning
   {Reference `#1' on page \thepage \space 
    undefined}}%
	{\edef\@tempa{\@nameuse{r@#1}}%
	  \@xtexpush{\expandafter\@cdr\@tempa\@nil\null}%
		  {\expandafter\@cdr\@tempa\@nil}}}

%%
%%	Override the \cite & \bibitem macro
%%
\gdef\xtexbibcite#1#2#3{%
\global\@namedef{b@#1}{#2}%
\global\@namedef{b@p@#1}{#3}%
}

\gdef\@lbibitem[#1]#2{\item[\@biblabel{#1}]\if@filesw 
      {\def\protect##1{\string ##1\space}\immediate
        \write\@auxout{\string\xtexbibcite{#2}{#1}{\thepage}}}\fi\ignorespaces}

\def\@bibitem#1{\item\if@filesw \immediate\write\@auxout
       {\string\xtexbibcite{#1}{\the\c@enumi}{\thepage}}\fi\ignorespaces}

%%
%%	if a citation has no page number defined, we don't generate
%%	a button box.
%%
\gdef\@citex[#1]#2{\if@filesw\immediate\write\@auxout{\string\citation{#2}}\fi
  \def\@citea{}%
  \@cite{\@for\@citeb:=#2\do
    {\@citea\def\@citea{,\penalty\@m\ }\@ifundefined
       {b@\@citeb}{{\bf ?}\@warning
       {Citation `\@citeb' on page \thepage \space undefined}}%
	{\@ifundefined{b@p@\@citeb}%
		{\hbox{\csname b@\@citeb\endcsname}}%
		{\@xtexpush{\hbox{\csname b@\@citeb\endcsname}}{\csname b@p@\@citeb\endcsname}}%
	}%
    }% body of do...
  }% \cite\@for...
{#1}}

%%
%%      define a system call macro
%%
\def\syscall#1#2{\@xtexbutton{{#1}}{xtex-system(#2)}}

%%
%%	index entries...
%%
%%\def\@wrindex#1{\let\thepage\relax
%%   \xdef\@gtempa{\write\@indexfile{\string
%%      \indexentry{#1}{\string\@xtexindex{\thepage}}}}\endgroup\@gtempa
%%   \if@nobreak \ifvmode\nobreak\fi\fi\@esphack}
%%\def\@xtexindex#1{\@xtexpush{#1}{#1}}

%%
%% Table of contents...
%%
\def\addcontentsline#1#2#3{\if@filesw \begingroup
\let\label\@gobble \let\index\@gobble \let\glossary\@gobble
\def\protect##1{\string\string\string##1\string\space
   \space}\@temptokena{\@xtexpush{\thepage}{\thepage}}%
\edef\@tempa{\write \@auxout{\string\@writefile{#1}{\protect
     \contentsline{#2}{#3}{\the\@temptokena}}}}\@tempa
   \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}
