%%% Slice------------------------------------------------------------
%%% File: Timeline.Sty
%%% Written to work with either LaTeX or plain TeX
%%%
%%% In LaTeX:
%%% \begin{timeline}{length}(start,stop)
%%%   .
%%%   .
%%%   .
%%% \end{timeline}
%%%
%%% in plain TeX
%%% \timeline{length}(start,stop)
%%%   .
%%%   .
%%%   .
%%% \endtimeline
%%% in between the two, we may have:
%%% \item{date}{description}
%%% \item[sortkey]{date}{description}
%%% \optrule
%%%
%%% the options to timeline are:
%%%      length The amount of vertical space that the timeline should
%%%                use.
%%%      (start,stop) indicate the range of the timeline. All dates or
%%%                sortkeys should lie in the range [start,stop]
%%%
%%% \item without the sort key expects date to be a number (such as a
%%%      year).
%%% \item with the sort key expects the sort key to be a number; date
%%%      can be anything. This can be used for log scale time lines
%%%      or dates that include months or days.
%%% putting \optrule inside of the timeline environment will cause a
%%%      vertical rule to be drawn down the center of the timeline.
 
\catcode`\@=11     % Pretend @ is a letter
\newcount\startat            \newcount\tllength
\newdimen\putithere          \newdimen\itwasthere
\newcount\scr@tchi           \newdimen\scr@tchii
 
% A vertically centered lap
\long\def\ylap#1{\vbox to \z@{\vss#1\vss}}
 
%         Vertical `laps'; cf. \llap and \rlap
\long\def\ulap#1{\vbox to \z@{\vss#1}}
\long\def\dlap#1{\vbox to \z@{#1\vss}}
 
\def\timeline#1(#2,#3){\ifvmode\else\par\fi$$\vbox to#1\bgroup
                                       % The \vbox command is
                                       % surrounded by $$..$$ to make it
                                       % fit in well with paragraphs.
    \offinterlineskip
    \startat=#2\tllength=#3
         \advance\tllength by-\startat % \tllength should be the total
                                       % length of the timeline.
    \def\item{\@ifnextchar[\@item\@itemnosrtkey}
    \def\@item[##1]##2##3{\scr@tchi=##1
         \advance\scr@tchi by-\startat
         \putithere=#1 \divide\putithere by 65536 % to avoid arithmetic
                                                  % overflow
              \multiply\putithere by \scr@tchi    % we only remain
              \divide\putithere by\tllength       % accurate to 1pt in
              \multiply\putithere by 65536        % these calculations
                                       % Now \putithere has how far
                                       % down we should go for this item.
         \scr@tchii=\putithere \advance\scr@tchii by -\itwasthere
         \ifdim\scr@tchii<12pt
              \ifx\lrswitch L \@putright{\putithere}{##2}{##3}
              \else \@putleft{\putithere}{##2}{##3}
              \fi
         \else \@putleft{\putithere}{##2}{##3}
         \fi
         \itwasthere=\putithere
    }
    \def\@itemnosrtkey##1##2{\@item[##1]{##1}{##2}}
    \def\@putright##1##2##3{\dlap
         {\kern##1\centerline
              {\rlap
                   {\ $\bullet$\hskip1.5em{\bf ##2} \ ##3}}}
         \let\lrswitch=R
    }
    \def\@putleft##1##2##3{\dlap
         {\kern##1\centerline
              {\llap
                   {{\bf ##2} \ ##3\hskip1.5em$\bullet$\ }}}
         \let\lrswitch=L
    }
    \def\optrule{\dlap
         {\centerline
              {\dimen0=#1 \advance\dimen0 by 6pt % This calculation is
                                                 % kept local
                   \vrule depth \dimen0 height-6pt}}
    }
}
\def\endtimeline{\vfill\egroup
          \ifx\@latexerr\undefined\else
             \global\@ignoretrue
          \fi
          \vbox{\vskip\baselineskip}$$}
                   % Put the extra \vskip in a \vbox to hide it from
                   % the math gods.
\ifx\@latexerr\undefined
    \def\@ifnextchar#1#2#3{\let\@tempe #1\def\@tempa{#2}\def\@tempb{#3}
         \futurelet\@tempc\@ifnch}
     \def\@ifnch{\ifx \@tempc \@sptoken \let\@tempd\@xifnch
           \else \ifx \@tempc \@tempe\let\@tempd\@tempa\else\let\@tempd\@tempb
                \fi
           \fi \@tempd}
 
     % NOTE: the following hacking must precede the definition of \:
     %  as math medium space.
 
     \def\:{\let\@sptoken= } \:  % this makes \@sptoken a space token
 
     \def\:{\@xifnch} \expandafter\def\: {\futurelet\@tempc\@ifnch}
     \catcode`\@=12   % Let's not pretend @ is a letter
\fi
