%D \module
%D   [       file=anch-bck, % moved from anch-pgr (1999.08.01)
%D        version=2011.12.19,
%D          title=\CONTEXT\ Anchoring Macros,
%D       subtitle=Backgrounds,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% when tested for a while: \MPgetmultipars -> getmultipars
% when tested for a while: \MPvar          -> mpvar

\writestatus{loading}{ConTeXt Anchoring Macros / Backgrounds}

%D This module is quite okay but at some point I might add methods that use
%D attributes although not too much is to be expected, apart from better anchoring.
%D In fact better anchoring can be done independent of that.

\unprotect

% This might be overloaded later on:

% \defineoverlay[\v!text-2][\positionoverlay{\v!text-2}]
% \defineoverlay[\v!text-1][\positionoverlay{\v!text-1}]
% \defineoverlay[\v!text+1][\positionoverlay{\v!text+1}]
% \defineoverlay[\v!text+2][\positionoverlay{\v!text+2}]

% Paragraph positions:

\newinteger\c_anch_backgrounds_text_n

\def\v_anch_backgrounds_text_current    {tbg:0}
\def\v_anch_backgrounds_anchor_current_b{b:\v_anch_backgrounds_text_current}
\def\v_anch_backgrounds_anchor_current_e{e:\v_anch_backgrounds_text_current}

\def\anch_backgrounds_text_initialize_next
  {\global\advanceby\c_anch_backgrounds_text_n\plusone
   \edef\v_anch_backgrounds_text_current{tbg:\the\c_anch_backgrounds_text_n}}

% The first position can be used in the middle of a paragraph in which case we're
% too late with initializing par positions. Therefore we check if positions are
% used at all.
%
% tricky: we need to catch newly set! otherwise an old run can have positions

\protected\def\anch_backgrounds_text_initialize
  {\doifelsepositionsused\enableparpositions\donothing
   \glet\anch_backgrounds_text_initialize\relax}

\appendtoks
    \anch_backgrounds_text_initialize
\to \everystarttext

%D Some MP

\newinteger\MPparcounter

\permanent\def\MPself     {\MPvar{self}}
\permanent\def\MPbself    {b:\MPvar{self}}
\permanent\def\MPeself    {e:\MPvar{self}}
\permanent\def\MPparanchor{p:\number\MPparcounter} % \the

% \carryoverpar is needed for left/right floats

% \definetextbackground[more][state=start,backgroundcolor=red] % location=paragraph
% \definetextbackground[test][state=start,backgroundcolor=green]
%
% \page \placefigure[left]{}{}
%
% \starttextbackground[test]
%   \readfile{ward}{}{}
%   \starttextbackground[more]
%     \readfile{ward}{}{}
%   \stoptextbackground
%   \readfile{ward}{}{}
% \stoptextbackground
%
% \page \placefigure[right]{}{}
%
% \starttextbackground[test]
%   \readfile{ward}{}{}
%   \starttextbackground[more]
%     \readfile{ward}{}{}
%   \stoptextbackground
%   \readfile{ward}{}{}
% \stoptextbackground

\newinteger\c_anch_backgrounds_text_level

\installcorenamespace{textbackground}
\installcorenamespace{textbackgroundlevel}

\installcommandhandler \??textbackground {textbackground} \??textbackground

\appendtoks
    \frozen\instance\protected\edefcsname        \currenttextbackground\endcsname{\groupedcommand{\starttextbackground[\currenttextbackground]}{\stoptextbackground}}%
    \frozen\instance\protected\edefcsname\e!start\currenttextbackground\endcsname{\starttextbackground[\currenttextbackground]}%
    \frozen\instance\protected\edefcsname\e!stop \currenttextbackground\endcsname{\stoptextbackground}%
\to \everydefinetextbackground

\newconstant   \c_anch_backgrounds_pos_state
\newconditional\c_anch_backgrounds_pos_no_shape

\def\anch_backgrounds_bpos
  {\ifconditional\c_anch_backgrounds_pos_no_shape
     \bposkind\v_anch_backgrounds_text_current\plusthree
   \else
     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
   \fi}

\def\anch_backgrounds_epos
  {\ifconditional\c_anch_backgrounds_pos_no_shape
     \eposkind\v_anch_backgrounds_text_current\plusthree
   \else
     \eposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
   \fi}

% ugly hack to register usage

\newinteger \c_anch_backgrounds_text_count
\newinteger \c_anch_backgrounds_text_check
\newconstant\c_anch_backgrounds_text_state

\permanent\tolerant\protected\def\starttextbackground[#tag]#spacer[#S#settings]%
  {\begingroup
   \global\advanceby\c_anch_backgrounds_text_count\plusone
   \advanceby\c_anch_backgrounds_text_level\plusone
   \cdef\currenttextbackground{#tag}%
   \anch_backgrounds_text_initialize_next
   \ifparameter#settings\or
     \setupcurrenttextbackground[#settings]%
   \fi
   \ifcstok{\textbackgroundparameter\c!state}\v!start
     \anch_backgrounds_text_preset_yes
   \else
     \anch_backgrounds_text_preset_nop
   \fi
   \anch_backgrounds_text_start_indeed}

\def\anch_backgrounds_text_level_start
  {\c_anch_backgrounds_text_check\c_anch_backgrounds_text_count}

\def\anch_backgrounds_text_level_stop
  {\c_anch_backgrounds_text_state
     \ifnum\c_anch_backgrounds_text_count>\c_anch_backgrounds_text_check
       \plusone
     \else
       \zerocount
     \fi}

% todo \backgroundvariable\c!variant

% criterium determines when we fall back on text
% always is always forcing paragraphs

\let\anch_backgrounds_text_start_indeed\relax
\let\anch_backgrounds_text_stop_indeed \relax

\defcsname\??textbackgroundlevel\v!text\endcsname
  {\let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_txt
   \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_txt}

\defcsname\??textbackgroundlevel\v!paragraph\endcsname
  {\ifnum\c_anch_backgrounds_text_level>\textbackgroundparameter\c!criterium\relax
     \let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_txt
     \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_txt
   \else
     \let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_par
     \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_par
   \fi}

\defcsname\??textbackgroundlevel\v!always\endcsname
  {\let\anch_backgrounds_text_start_indeed\anch_backgrounds_text_start_par
   \let\anch_backgrounds_text_stop_indeed \anch_backgrounds_text_stop_par}

\defcsname\??textbackgroundlevel\v!none\endcsname
  {\anch_backgrounds_text_preset_nop}

\def\anch_backgrounds_text_preset_nop
  {\let\anch_backgrounds_text_start_indeed\relax
   \let\anch_backgrounds_text_stop_indeed \relax}

\def\anch_backgrounds_text_preset_yes
  {\anch_backgrounds_text_preset_nop
   \csname\??textbackgroundlevel\textbackgroundparameter\c!location\endcsname
   \edef\p_anch_backgrounds_text_frame     {\textbackgroundparameter\c!frame}%
   \edef\p_anch_backgrounds_text_corner    {\textbackgroundparameter\c!corner}%
   \edef\p_anch_backgrounds_text_background{\textbackgroundparameter\c!background}%
   \ifx\p_anch_backgrounds_text_frame\v!on
     \ifx\p_anch_backgrounds_text_corner\v!round
       \let\p_anch_backgrounds_text_frame\!!plustwo
     \else
       \let\p_anch_backgrounds_text_frame\!!plusone
     \fi
   \else
     \let\p_anch_backgrounds_text_frame\!!zerocount
   \fi
   \ifx\p_anch_backgrounds_text_background\v!color
     \let\p_anch_backgrounds_text_background\!!plusone
   \else
     \let\p_anch_backgrounds_text_background\!!zerocount
   \fi
   \startpositionoverlay{\textbackgroundoverlay{\textbackgroundparameter\c!level}}%
     \anch_backgrounds_text_meta_graphic % gets expanded directly
   \stoppositionoverlay}

\def\anch_backgrounds_text_meta_graphic
  {\normalexpanded
     {\setMPpositiongraphicrange % needs to be optimized
        {\v_anch_backgrounds_anchor_current_b}%
        {\v_anch_backgrounds_anchor_current_e}%
        {\textbackgroundparameter\c!method}%
        {self=\v_anch_backgrounds_text_current,
         mp=\textbackgroundparameter\c!mp,
         gridtype=\textbackgroundparameter\c!alternative,
         filltype=\p_anch_backgrounds_text_background,
         linetype=\p_anch_backgrounds_text_frame,
         dashtype=\textbackgroundparameter\c!dash,
         gridcolor=\textbackgroundparameter\c!framecolor,
         linecolor=\textbackgroundparameter\c!framecolor,
         lineoffset=\textbackgroundparameter\c!frameoffset,
         fillcolor=\textbackgroundparameter\c!backgroundcolor,
         filloffset=\textbackgroundparameter\c!backgroundoffset,
         gridwidth=\textbackgroundparameter\c!rulethickness,
         gridshift=\textbackgroundparameter\c!voffset,
         linewidth=\textbackgroundparameter\c!rulethickness,
         lineradius=\textbackgroundparameter\c!radius}}}

\permanent\protected\def\stoptextbackground
  {\anch_backgrounds_text_stop_indeed
   \carryoverpar\endgroup} % why doesn't this work ?

\permanent\protected\def\starttextbackgroundmanual
  {\begingroup
   \c_anch_backgrounds_pos_state\plusone
   \usetextbackgroundstyleandcolor\c!style\c!color
   \anch_backgrounds_bpos}

\permanent\protected\def\stoptextbackgroundmanual
  {\anch_backgrounds_epos
   \carryoverpar\endgroup}

\def\anch_backgrounds_text_start_txt
  {\ifvmode\dontleavehmode\fi
   \begingroup
   \c_anch_backgrounds_pos_state\plusone
   \usetextbackgroundstyleandcolor\c!style\c!color
   \anch_backgrounds_bpos}

\def\anch_backgrounds_text_stop_txt
  {\anch_backgrounds_epos
   \carryoverpar\endgroup}

\newgluespec\textbackgroundskip

% maybe we should have a resetter for such compensation struts

\def\anch_backgrounds_reset_attributes
  {\scratchcounter\c_attr_snapmethod
   \resetallattributes % \c_attr_linenumber\attributeunsetvalue
   \c_attr_snapmethod\scratchcounter}

\def\anch_backgrounds_text_start_par % beware .. background shapes
  {\endgraf % new
   \textbackgroundparameter\c!before
   \begingroup
   \c_anch_backgrounds_pos_state\plustwo
   \begingroup
   \anch_backgrounds_reset_attributes
   \noindent
   \ifgridsnapping
     \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!topoffset}%
     \ifdim\textbackgroundskip>\zeropoint
       \struttedbox % not always ok (e.g. setups)
         {\hpack{\raise\textbackgroundskip\hpack{\anch_backgrounds_bpos}}}%
     \else
       \anch_backgrounds_bpos
     \fi
   \else
     \anch_backgrounds_bpos
   \fi
   \endgraf % we need a vertical nobreak - 29/06/2004
   \endgroup
   \nobreak
   \vskip-\lineheight
   \nobreak
   \ifgridsnapping \else
     \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!topoffset}%
     \ifdim\textbackgroundskip>\zeropoint
        \kern\textbackgroundskip\nobreak
     \fi
   \fi
   \doadaptleftskip {\textbackgroundparameter\c!leftoffset}%
   \doadaptrightskip{\textbackgroundparameter\c!rightoffset}%
   % new
   \dosetraggedcommand{\textbackgroundparameter\c!align}%
   \raggedcommand
   %
   \usetextbackgroundstyleandcolor\c!style\c!color
   \nowhitespace
   \seteffectivehsize
   \doinhibitblank %   \blank[\v!disable]% new
   \par}

\def\anch_backgrounds_text_stop_par
  {\par
   \removelastskip % new
   \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!bottomoffset}%
   \ifdim\lastskip>\zeropoint
     \advanceby\textbackgroundskip-\lastskip
   \fi
   \ifgridsnapping
   \orelse\ifdim\textbackgroundskip>\zeropoint
     \kern\textbackgroundskip\nobreak
   \fi
   \nobreak
   \vskip-\dimexpr\lineheight+\parskip\relax % problem: we loose the hangindent
   \nobreak
   \endgroup
   \begingroup
   \forgeteverypar % NOT REALLY NEEDED, SAVES HASH/MEM
   \anch_backgrounds_reset_attributes
   \nobreak \noindent \strut \hfill \kern\zeropoint
   % so far
   \spac_helpers_assign_skip\textbackgroundskip{\textbackgroundparameter\c!bottomoffset}%
   \ifgridsnapping % experimental, pascal (todo: topoffset in same way)
     \ifdim\textbackgroundskip>\zeropoint
       \struttedbox % not always ok (e.g. setups)
         {\hpack{\lower\textbackgroundskip\hpack{\anch_backgrounds_epos}}}%
     \else
       \anch_backgrounds_epos
     \fi
   \else
     \anch_backgrounds_epos
   \fi
   \endgraf
   \carryoverpar\endgroup
   \endgraf % new
   \textbackgroundparameter\c!after}

\permanent\protected\def\checkpositionoverlays % overloads \relax in anch-pgr
  {\ifproductionrun
     \enabletextarearegistration
     \enablehiddenbackground
     \enforced\glet\checkpositionoverlays\relax
   \fi}

% shape handling

\definesystemattribute[textbackground][public]

\def\anch_backgrounds_bpos
  {\ifconditional\c_anch_backgrounds_pos_no_shape
     \c_attr_textbackground\attributeunsetvalue
     \bposkind\v_anch_backgrounds_text_current\plusthree
   \orelse\ifnum\c_anch_backgrounds_pos_state=\plusone
     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
     \clf_registerbackground{\v_anch_backgrounds_text_current}%
   \else
     \bposkind\v_anch_backgrounds_text_current\c_anch_backgrounds_pos_state
     \c_attr_textbackground\attributeunsetvalue
   \fi}

% plugs into other code

\def\strc_floats_wrap_free_region
  {\global\setbox\floatbox\hpack % we always need to wrap
     {\ifconditional\c_strc_floats_trace_free
        \strc_floats_show_free_region
      \fi
      \box\floatbox}}

\startuseMPgraphic{floatfree}
    draw_free_region(
        \the\wd\floatbox,
        \the\ht\floatbox,
        \the\dp\floatbox,
        \the\d_free_offset_left,
        \the\d_free_offset_right,
        \the\d_free_offset_top,
        \the\d_free_offset_bottom
    ) ;
\stopuseMPgraphic

\def\strc_floats_show_free_region
  {\lower\dp\floatbox\hpack to \zeropoint {\useMPgraphic{floatfree}}}

\newconditional\c_strc_floats_mark_as_free
\newconditional\c_strc_floats_trace_free
\newconstant   \c_free_offset_kind
\newdimension  \d_free_offset_left
\newdimension  \d_free_offset_right
\newdimension  \d_free_offset_top
\newdimension  \d_free_offset_bottom

\installtextracker{floats.freeregion}
  {\c_strc_floats_trace_free\conditionaltrue}
  {\c_strc_floats_trace_free\conditionalfalse}

\def\strc_floats_mark_as_free#1#2#3#4#5%
  {\c_free_offset_kind  #1%
   \d_free_offset_left  #2%
   \d_free_offset_right #3%
   \d_free_offset_top   #4%
   \d_free_offset_bottom#5%
   \strc_floats_wrap_free_region
   \anch_mark_tagged_box_free
     \floatbox
     \c_free_offset_kind
     \d_free_offset_left
     \d_free_offset_right
     \d_free_offset_top
     \d_free_offset_bottom}

\ifrelax\strc_floats_mark_pag_as_free \else
    \writestatus{error}{wrong place for pag_as_free}\wait
\fi

\ifrelax\strc_floats_mark_par_as_free \else
    \writestatus{error}{wrong place for par_as_free}\wait
\fi

\def\strc_floats_mark_pag_as_free
  {\ifpositioning
     \ifconditional\c_strc_floats_mark_as_free
       \strc_floats_mark_as_free
         \plusone
         \zeropoint
         \zeropoint
         \d_strc_floats_top
         \d_strc_floats_bottom
     \fi
   \fi}

\def\strc_floats_mark_par_as_free
  {\ifpositioning
     \ifconditional\c_strc_floats_mark_as_free
       \ifcase\c_page_sides_float_type
       \or % backspace
         \strc_floats_mark_as_free
           \plustwo
           \zeropoint
           \d_page_sides_rightoffset
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % leftedge
         \strc_floats_mark_as_free
           \plustwo
           \zeropoint
           \d_page_sides_rightoffset
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % leftmargin
         \strc_floats_mark_as_free
           \plustwo
           \zeropoint
           \d_page_sides_rightoffset
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % leftside
         \strc_floats_mark_as_free
           \plustwo
           \d_page_sides_leftskip % maybe too
           \d_page_sides_margin
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % rightside
         \strc_floats_mark_as_free
           \plusthree
           \d_page_sides_margin
           \d_page_sides_rightskip % maybe too
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % rightmargin
         \strc_floats_mark_as_free
           \plusthree
           \d_page_sides_leftoffset
           \zeropoint
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % rightedge
         \strc_floats_mark_as_free
           \plusthree
           \d_page_sides_leftoffset
           \zeropoint
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \or   % cutspace
         \strc_floats_mark_as_free
           \plusthree
           \d_page_sides_leftoffset
           \zeropoint
           \d_page_sides_topskip
           \d_page_sides_bottomskip
       \fi
     \fi
   \fi}

% so far

\setuptextbackground
  [\c!mp=mpos:region:draw,
   \c!method=mpos:region,
   \c!state=\v!start,
   \c!location=\v!text,
   \c!leftoffset=\!!zeropoint, % 1em,
   \c!rightoffset=\textbackgroundparameter\c!leftoffset,
   \c!topoffset=\!!zeropoint,  % \v!medium,
   \c!bottomoffset=\textbackgroundparameter\c!topoffset,
   \c!criterium=\plusone,
   \c!level=-1,
   \c!alternative=0,
   \c!align=,
   \c!dash=0, % to be internationalized
   \c!background=\v!color,
   \c!backgroundcolor=lightgray,
   \c!backgroundoffset=\!!zeropoint,
   \c!corner=\v!rectangular,
   \c!radius=.5\bodyfontsize,
   \c!voffset=\!!zeropoint,
   \c!frame=\v!on,
   \c!framecolor=blue,
   \c!frameoffset=\!!zeropoint,
   \c!rulethickness=\linewidth]

%D The \METAPOST\ connection:

% gridtype = 1 => baseline
% gridtype = 2 => betweenline

\setupMPvariables
  [mpos:region]
  [mp=mpos:region:unset,
   gridtype=0,
   linetype=1,
   filltype=1,
   dashtype=0, % 1 = dashed, 2 = dashed with background
   gridcolor=red,
   linecolor=blue,
   fillcolor=lightgray,
   filloffset=\zeropoint,
   linewidth=\linewidth,
   gridwidth=\linewidth,
   gridshift=\zeropoint,
   lineradius=.5\bodyfontsize,
   lineoffset=\zeropoint,
   dashtype=1]

\startuseMPgraphic{mpos:region:extra}
    % user stuff
\stopuseMPgraphic

\startuseMPgraphic{mpos:region:anchor}
    setbounds currentpicture to multibox ;
\stopuseMPgraphic

\startMPpositiongraphic{mpos:region}{fillcolor,filloffset,linecolor,gridcolor,linewidth,gridwidth,gridshift,lineradius,lineoffset}
    \includeMPgraphic{mpos:region:setup} ;
    \includeMPgraphic{mpos:region:extra} ;
    getmultipars("self", "anchor") ;
    \includeMPgraphic{\MPvar{mp}} ;
    \includeMPgraphic{mpos:region:anchor} ;
\stopMPpositiongraphic

%D For old times sake:

\startMPpositiongraphic{mpos:box}{fillcolor,linecolor,linewidth}
    string tag; tag := "\MPvar{self}" ;
    path box ; box := positionbox(tag) ;

    box := box enlarged  \MPvar{filloffset} ;
    fill box withcolor \MPvar{fillcolor} ;
    draw box withcolor \MPvar{linecolor} withpen pencircle scaled \MPvar{linewidth} ;

    positioninregion;
\stopMPpositiongraphic

\startMPpositionmethod{mpos:region}
    \MPpositiongraphic{mpos:region}{}%
\stopMPpositionmethod

\startuseMPgraphic{mpos:region:draw}
    draw_multi_pars
\stopuseMPgraphic

\startuseMPgraphic{mpos:region:show}
    show_multi_pars
\stopuseMPgraphic

\startuseMPgraphic{mpos:region:kind}
    show_multi_kind
\stopuseMPgraphic

\startuseMPgraphic{mpos:region:sideline}
    draw_multi_side
\stopuseMPgraphic

\startuseMPgraphic{mpos:region:sideline:dashed}
    draw_multi_side_path dashed evenly ;
\stopuseMPgraphic

%D As an example we define a grid background:

\pushoverloadmode

\definetextbackground
  [\v!grid]
  [\c!state=\v!stop,
   \c!location=\v!paragraph,
   \c!frame=\v!off,
   \c!framecolor=red,
   \c!background=,
   \c!alternative=1]

\ifdefined\basegrid
    \aliased\letcsname\v!grid\endcsname\basegrid
\fi

\popoverloadmode

%D Some examples (that might become modules anch-imp-whatever):

\setupMPvariables
  [mpos:encircle]
  [fillcolor=lightgray,
   filloffset=\!!zeropoint,
   linecolor=blue,
   lineoffset=5pt,
   linewidth=1pt]

\startMPpositiongraphic{mpos:encircle}{linecolor,fillcolor,linewidth,lineoffset}
    getposboxes("self","anchor") ;
    if nofposboxes = 1 :
        posboxes[1] := (posboxes[1] enlarged mpvar "lineoffset") cornered mpvar "lineoffset" ;
        fill posboxes[1] withcolor mpvar "fillcolor" ;
        draw posboxes[1] withpen pencircle scaled mpvar "linewidth" withcolor mpvar "linecolor"  ;
    fi ;
\stopMPpositiongraphic

\setupMPvariables
  [mpos:connect]
  [linecolor=red,
   lineoffset=.25ex,
   linewidth=1pt]

\startMPpositiongraphic{mpos:connect}{linecolor,lineoffset,linewidth}
    boxlinewidth  := mpvar "linewidth" ;
    boxlineoffset := mpvar "lineoffset" ;
    def boxlineoptions = withcolor mpvar "linecolor" enddef ;
    getposboxes("from,to","anchor") ;
    connect_positions ;
\stopMPpositiongraphic

\protect \endinput

\definetextbackground[underline]    [location=text,alternative=1,background=,frame=off]
\definetextbackground[overstrike]   [location=text,alternative=2,background=,frame=off]
\definetextbackground[exlines]      [location=text,alternative=3,background=,frame=off]
\definetextbackground[strikethrough][location=text,alternative=4,background=,frame=off]

\definestartstop [underline]
  [before={\starttextbackground[underline]},
   after=\stoptextbackground]

\definestartstop
  [overstrike]
  [before={\starttextbackground[overstrike]},
   after=\stoptextbackground]

\definestartstop
  [exlines]
  [before={\starttextbackground[exlines]},
   after=\stoptextbackground]

\definestartstop
  [strikethrough]
  [before={\starttextbackground[strikethrough]},
   after=\stoptextbackground]

\definetextbackground
  [sideline]
  [mp=mpos:region:sideline,
   location=paragraph,
   framecolor=red,
   frameoffset=5mm]

\definestartstop [sideline]
  [before={\starttextbackground[sideline]},
   after=\stoptextbackground]

\starttext

    \startunderline     \input tufte \stopunderline      \blank
    \startoverstrike    \input tufte \stopoverstrike     \blank
    \startexlines       \input tufte \stopexlines        \blank
    \startstrikethrough \input tufte \stopstrikethrough  \blank
    \startsideline      \input tufte \stopsideline       \blank

    \page

    \startpositionoverlay{text-1}
        \setMPpositiongraphic{connect-1-b}{mpos:connect}{from=connect-1-b,to=connect-1-e}
        \setMPpositiongraphic{connect-1-e}{mpos:connect}{from=connect-1-b,to=connect-1-e}
    \stoppositionoverlay

    \startpositionoverlay{text-1}
        \setMPpositiongraphic{encircle-1}{mpos:encircle}{self=encircle-1}
    \stoppositionoverlay

    test \hpos{connect-1-b}{START}
        \dorecurse{10}{\input ward}
    \hpos{encircle-1}{\strut HERE}
        \dorecurse{10}{\input ward}
    \hpos{connect-1-e}{STOP} test

\stoptext

