% engine=luatex

\environment mk-environment

\startcomponent mk-bidi

\definefontfeature
  [arab]
  [mode=node,language=dflt,script=arab,
   init=yes,medi=yes,fina=yes,isol=yes,
   liga=yes,dlig=yes,rlig=yes,clig=yes,
   mark=yes,mkmk=yes,kern=yes,curs=yes]

\font\Arabic=arabtype*arab at 20pt

\chapter {Getting lost}

\def\LATIN{LATIN} {\setdirection[1]} % enable this
\def\ARAB {محمد}

\def\aIdris{ادريس}
\def\aHans{هانس}
\def\aTaco{تاكو}

\def\avIdris{إِدْرِيس}
\def\avHans{هَانْسْ}
\def\avTaco{تَاكُو}

\startluacode
    function document.split_tokens(str)
        for s in str:bytes() do
            tex.sprint(tex.ctxcatcodes,string.format("\\hbox{\\char %s}",s))
        end
    end
\stopluacode

\def\biditest#1#2#3% font text raw
  {\dontleavehmode\hbox
    {\framed[offset=overlay]{\tttf#2}\quad
    % \ctxlua{mirror.trace = true}%
     \framed[offset=overlay]{#1#3}\quad
    % \ctxlua{mirror.trace = false}
     \tttf\ctxlua{document.split_tokens([[\detokenize{#3}]])}}}

\startbuffer[bidi-sample]
\biditest\Arabic{LATIN BARA}{\textdir TLT\relax   \LATIN\ \ARAB}\par
\biditest\Arabic{BARA LATIN}{\textdir TRT\relax   \LATIN\ \ARAB}\par
\biditest\Arabic{LATIN ARAB}{\textdir TLT{\bidilro \LATIN\ \ARAB}}\par % right -> left
\biditest\Arabic{LATIN ARAB}{\textdir TRT{\bidilro \LATIN\ \ARAB}}\par % right -> left
\biditest\Arabic{BARA NITAL}{\textdir TLT{\bidirlo \LATIN\ \ARAB}}\par % left -> right
\biditest\Arabic{BARA NITAL}{\textdir TRT{\bidirlo \LATIN\ \ARAB}}\par % left -> right
\stopbuffer

\startbuffer[bidi-sample]
\biditest\Arabic{LATIN BARA}{\textdir TLT\relax   \LATIN\ \ARAB}\par
\biditest\Arabic{BARA LATIN}{\textdir TRT\relax   \LATIN\ \ARAB}\par
\biditest\Arabic{LATIN ARAB}{\textdir TLT\bidilro \LATIN\ \ARAB}\par % right -> left
\biditest\Arabic{LATIN ARAB}{\textdir TRT\bidilro \LATIN\ \ARAB}\par % right -> left
\biditest\Arabic{BARA NITAL}{\textdir TLT\bidirlo \LATIN\ \ARAB}\par % left -> right
\biditest\Arabic{BARA NITAL}{\textdir TRT\bidirlo \LATIN\ \ARAB}\par % left -> right
\stopbuffer

\startbuffer[bidi-setup]
\setupdirections[bidi=off]
\stopbuffer

{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \startpacked \getbuffer[bidi-sample] \stoppacked}

\startbuffer[bidi-setup]
\setupdirections[bidi=global]
\stopbuffer

{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \startpacked \getbuffer[bidi-sample] \stoppacked}

\startbuffer[bidi-setup]
\setupdirections[bidi=local]
\stopbuffer

{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \startpacked \getbuffer[bidi-sample] \stoppacked}

\page

\startluacode
    local faked = { }
    function document.fake_direction(direction,chars)
        local d = characters.data
        for u in chars:utfvalues() do
            faked[u] = d[u].direction
            d[u].direction=direction
        end
    end
    function document.restore_directions()
        local d = characters.data
        for k, v in pairs(faked) do
            d[k].direction=v
        end
        faked = { }
    end
\stopluacode

\def\StartFakeArab{\bgroup\ctxlua{document.fake_direction("al","onetwothreefourfive")}}
\def\StopFakeArab {\ctxlua{document.restore_directions()}\egroup}

\startbuffer
\start         \textdir TLT one \bidilro two \bidirlo three \bidipop four \bidipop five \par \stop
\StartFakeArab \textdir TLT one \bidilro two \bidirlo three \bidipop four \bidipop five \par \StopFakeArab
\start         \textdir TRT one \bidilro two \bidirlo three \bidipop four \bidipop five \par \stop
\StartFakeArab \textdir TRT one \bidilro two \bidirlo three \bidipop four \bidipop five \par \StopFakeArab
\stopbuffer

\start \setupdirections[bidi=local] \typebuffer \getbuffer \stop

\stopcomponent
