\setupoutput[pdf]
\setupcolors[state=start]
\setupinteraction[state=start]


\defineoverlay[TheoremFrame][\useMPgraphic{TheoremFrame}]
\defineframedtext[TheoremText][frame=off,background=TheoremFrame,width=\hsize]
\def\StartTeoFrame{\startTheoremText}
\def\StopTeoFrame {\stopTheoremText}

\def\FrameTheoremTitle#1{\setMPtext{TheoremFrame}{\hbox spread 1em{\hss\strut#1\hss}}}
\setMPtext{TheoremFrame}{}

\def\FrameTheoremCaption#1{\setMPtext{TheoremCaptionFrame}{\hbox spread 1em{\hss\strut#1\hss}}\useMPgraphic{TheoremCaption}}
\setMPtext{TheoremCaptionFrame}{}

\startuseMPgraphic{TheoremFrame}
 picture p, s; path a, b; pair c;
 p := textext.rt(\MPstring{TheoremFrame});
 a := unitsquare xyscaled(OverlayWidth, OverlayHeight);
 o := BodyFontSize ;
 p := p shifted (2o,OverlayHeight-ypart center p) ;
 drawoptions (withpen pencircle scaled 1pt withcolor .625red) ;
 b := a randomized (o/2) ;
 fill b withcolor .95white ; draw b ;
 c := center p;
 c := b intersectionpoint (c shifted (0,-o)--c shifted(0,o)) ;
 p := p shifted (c-center p) ;
 b := (boundingbox p) randomized (o/8) ;
 fill b withcolor .85white ; draw b ;
 draw p withcolor black;
 setbounds currentpicture to a ;
\stopuseMPgraphic


\startuseMPgraphic{TheoremCaption}
 picture p;
 p := textext.rt("Here");
% p := textext.rt(\MPstring{TheoremCaption});
 draw p rotated 90 withcolor blue;
 draw (5mm, 0)..(-2mm, 0mm) withcolor red;
\stopuseMPgraphic

\defineenumeration
 [Theorem]
 [location=right,
  headstyle=bold,
  text=Theorem ,
  command=\FrameTheoremCaption,
  location=inleft,
  before={\StartTeoFrame},
  after={\StopTeoFrame}]

\starttext

\FrameTheoremTitle{Immerman}
\startTheorem[3t9.4.00]
Theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem
theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem
theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem
theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem
theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem theorem
\stopTheorem


\stoptext
