\definesidebar[zigzag1][rulecolor=black,distance=4pt,rulethickness=1pt,alternative=2]
\definesidebar[zigzag2][rulecolor=red,distance=4pt,rulethickness=1pt,alternative=3]
\definesidebar[wave1][rulecolor=blue,distance=4pt,rulethickness=1pt,alternative=4]
\definesidebar[wave2][rulecolor=cyan,distance=4pt,rulethickness=1pt,alternative=5]
\definesidebar[obliquedashes][rulecolor=darkgreen,distance=4pt,rulethickness=1pt,alternative=6]
\definesidebar[longobliquedashes][rulecolor=orange,distance=10pt,rulethickness=1pt,alternative=7]

\startMPcode
def draw_pattern_bar(expr a, b, pattern, patternlength, patternheight, linewidth, linecolor) =
    begingroup ;
        save p, z, step ;
        pair p, z, step ;
        step := ( ( b - a ) / arclength( a -- b ) ) * patternlength ;
        path z ; z := pattern xscaled patternlength yscaled patternheight rotated (angle(step)) ;
        p := a ;
        forever :
            draw
                z shifted p
                withpen pencircle scaled linewidth
                withcolor linecolor ;
            p := p + step ;
            exitif arclength( a -- p ) > arclength( a -- b ) ;
        endfor ;
    endgroup ;
enddef ;

% from metapost/context/base/mpiv/mp-apos.mpiv
def anch_sidebars_draw (expr p_b_self, p_e_self, y_b_self, y_e_self, h_b_self, d_e_self,
        x, y, w, h, alternative, distance, linewidth, linecolor, topoffset, bottomoffset) =
    % beware, we anchor at (x,y)
    begingroup ;
    if alternative = 1 :
        interim linecap := rounded ;
    else :
        interim linecap := butt ;
    fi ;
    save a, b ; pair a, b ;
    if p_b_self = p_e_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    elseif RealPageNumber = p_b_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,0) ;
    elseif RealPageNumber = p_e_self :
        a := (-distance,h) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    else :
        a := (-distance,h) ;
        b := (-distance,0) ;
    fi ;
    a := (xpart a, min(ypart a + topoffset,   h)) ;
    b := (xpart b, max(ypart b - bottomoffset,0)) ;
    if alternative = 2 :
        draw_pattern_bar( a, b, ((0,0)--(0.25,-0.5)--(0.75,0.5)--(1,0)), 2pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 3 :
        draw_pattern_bar( a, b, ((0,0)--(0.25,-0.5)--(0.75,0.5)--(1,0)), 4pt, 1.5pt, linewidth, linecolor ) ;
    elseif alternative = 4 :
        draw_pattern_bar( a, b, ( ((0,0) .. controls (0,0.5) and (0.5,0.5) .. (0.5,0)) -- ((0.5,0) .. controls (0.5,-0.5) and (1,-0.5) .. (1,0)) ), 6pt, 4pt, linewidth, linecolor ) ;
    elseif alternative = 5 :
        draw_pattern_bar( a, b, ( (0,0) .. controls (0,1) and (1,1) .. (1,0) ), 4pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 6 :
        draw_pattern_bar( a, b, ( (0,0.5) .. (1,-0.5) ), 4pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 7 :
        draw_pattern_bar( a, b, ( (0,-0.5) .. (1,0.5) ), 22pt, 5pt, linewidth, linecolor ) ;
    else :
        draw
            a -- b
            if alternative = 1 :
                dashed (withdots scaled (linewidth/2))
            fi
            withpen pencircle scaled linewidth
            withcolor linecolor ;
    fi ;
    endgroup ;
enddef ;
\stopMPcode

\starttext
\startsidebar[zigzag1]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[zigzag2]\input sapolsky\relax\stopsidebar\par
\blank
\startsidebar[wave1]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[wave2]\input sapolsky\relax\stopsidebar\par
\blank
\startsidebar[obliquedashes]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[longobliquedashes]\input sapolsky\relax\stopsidebar\par
\stoptext

