% language=us runpath=texruns:manuals/cld

% This is just a safeguard as per end 2025 assume processing with mkxl, not so much
% for funcitonality but e.g. \unexpanded has been replaced by \protected below.

\continuewhenlmtxmode

\startenvironment cld-environment

\usemodule[abr-04]

\setuplayout
  [width=middle,
   height=middle,
   backspace=2cm,
   topspace=1cm,
   footer=0pt,
   bottomdistance=1cm,
   bottom=1cm,
   bottomspace=2cm]

\setuppagenumbering
  [alternative=doublesided]

\definecolor[darkred]  [r=.5]
\definecolor[darkgreen][g=.5]
\definecolor[darkblue] [b=.5]

\definecolor[red]  [darkred]
\definecolor[green][darkgreen]
\definecolor[blue] [darkblue]

\definetype
  [boldtypebig]
  [style=\ttbfa]

\definetype
  [boldtype]
  [style=\ttbf]

\definetyping
  [smalltyping]
  [bodyfont=small]

\setuptype
  [color=blue]

\setuptyping
  [color=blue]

\setupbodyfont
  [palatino,11pt]

\setuphead
  [chapter]
  [style=\bfc,
   color=blue]

\setuphead
  [section]
  [style=\bfb,
   color=blue]

\definehead
  [summary]
  [subsubsubsubject]

% \starttexdefinition protected SummaryCommand #1
%     \ifempty{\structureuservariable{category}}\else
%       \ttbfa[\structureuservariable{category}]\space
%     \fi
%     #1
% \stoptexdefinition

\setuphead
  [summary]
  [style=,
 % textcommand=\SummaryCommand,
   deeptextcommand=\boldtypebig,
   color=blue]

\definehead
  [subsummary]
  [subsubsubsubsubject]

\setuphead
  [subsummary]
  [style=,
   before=\blank,
   after=\blank,
   deeptextcommand=\type,
   command=\MySubSummaryHead,
   color=blue]

\protected\def\MySummaryHead#1#2%
  {\framed
     [frame=off,
      bottomframe=on,
      offset=0cm]
     {#2}}

\protected\def\MySubSummaryHead#1#2%
  {\framed
     [frame=off,
      bottomframe=on,
      offset=0cm]
     {#2}}

\setupwhitespace
  [big]

\setupheadertexts
  []

\setupheadertexts
  []
  [{\getmarking[chapter]\quad\pagenumber}]
  [{\pagenumber\quad\getmarking[chapter]}]
  []

\setupheader
  [color=darkblue]

\setuplist
  [chapter,title]
  [color=darkblue,
   style=bold]

\setupbottom
  [style=\bfx,
   color=darkred]

\setupbottomtexts
  [preliminary, uncorrected version -- \currentdate]

% special functions

\startluacode
    function context.whatevertocontext(result) -- can be used more below
        if type(result) == "table" then
            table.tocontext(result)
        else
            string.tocontext(result)
        end
    end
\stopluacode

\protected\def\StartShowLuaExample
  {\begingroup
 % \catcode\spaceasciicode     \othercatcode % I need to see how that comes out.
   \catcode\percentasciicode   \othercatcode
   \catcode\dollarasciicode    \othercatcode
   \catcode\circumflexasciicode\othercatcode}

\protected\def\StopShowLuaExample
  {\endgroup}

\protected\def\ShowLuaExampleZero    {\StartShowLuaExample\ShowLuaExampleZeroIndeed    }
\protected\def\ShowLuaExampleOne     {\StartShowLuaExample\ShowLuaExampleOneIndeed     }
\protected\def\ShowLuaExampleTwo     {\StartShowLuaExample\ShowLuaExampleTwoIndeed     }
\protected\def\ShowLuaExampleThree   {\StartShowLuaExample\ShowLuaExampleThreeIndeed   }
\protected\def\ShowLuaExampleFour    {\StartShowLuaExample\ShowLuaExampleFourIndeed    }
\protected\def\ShowLuaExampleFive    {\StartShowLuaExample\ShowLuaExampleFiveIndeed    }
\protected\def\ShowLuaExampleSix     {\StartShowLuaExample\ShowLuaExampleSixIndeed     }
\protected\def\ShowLuaExampleSeven   {\StartShowLuaExample\ShowLuaExampleSevenIndeed   }
\protected\def\ShowLuaExampleEight   {\StartShowLuaExample\ShowLuaExampleEightIndeed   }
\protected\def\ShowLuaExampleNine    {\StartShowLuaExample\ShowLuaExampleNineIndeed    }
\protected\def\ShowLuaExampleTable   {\StartShowLuaExample\ShowLuaExampleTableIndeed   }
\protected\def\ShowLuaExampleTableHex{\StartShowLuaExample\ShowLuaExampleTableHexIndeed}
\protected\def\ShowLuaExampleString  {\StartShowLuaExample\ShowLuaExampleStringIndeed  }
\protected\def\ShowLuaExampleBoolean {\StartShowLuaExample\ShowLuaExampleBooleanIndeed }

\protected\def\ShowLuaExampleZeroIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{string.tocontext(#1.#2(#3))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleOneIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{table.tocontext(#3)}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleTwoIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{table.tocontext(#1.#2(#3))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleThreeIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{string.tocontext(tostring(#1.#2(#3)))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleFourIndeed#1#2#3#4%
  {\obeyluatokens
   \startsubsummary[title={t=#3 #1.#2(t#4)}]
   \ctxlua{local t = #3 #1.#2(t#4) table.tocontext(t)}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleFiveIndeed#1#2%
  {\obeyluatokens
   \startsubsummary[title={#1.#2}]
   \ctxlua{string.tocontext(tostring(#1.#2))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleSixIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{string.tocontext(#1.#2(#3))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleSevenIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{string.tocontext(table.concat({#1.#2(#3)}," "))}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleEightIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2(#3)}]
   \ctxlua{buffers.assign("temp",#1.#2(#3))}%
   \typebuffer[temp]
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleNineIndeed#1#2#3%
  {\obeyluatokens
   \startsubsummary[title={#1.#2#3}]
   \ctxlua{context.whatevertocontext(#1.#2#3)}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleTableIndeed#1%
  {\obeyluatokens
   \startsubsummary[title={#1}]
   \ctxlua{table.tocontext(#1,false)}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleTableHexIndeed#1%
  {\obeyluatokens
   \startsubsummary[title={#1}]
   \ctxlua{table.tocontext(#1,false,false,true,true)} % name, reduce, noquotes, hex
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleStringIndeed#1%
  {\obeyluatokens
   \startsubsummary[title={#1}]
   \ctxlua{string.tocontext(#1)}
   \stopsubsummary
   \StopShowLuaExample}

\protected\def\ShowLuaExampleBooleanIndeed#1%
  {\obeyluatokens
   \startsubsummary[title={#1}]
   \ctxlua{boolean.tocontext(#1)}
   \stopsubsummary
   \StopShowLuaExample}

% interaction

\setupinteraction
  [state=start,
   color=,
   contrastcolor=]

\setuplist
  [chapter,section]
  [interaction=all]

% a hack:

\startluacode
    function document.checkcldresource(filename)
        if environment.arguments.runpath then
            -- We're running elsewhere so we can have started fresh.
            local cldname = file.replacesuffix(filename,"cld")
            local pdfname = file.replacesuffix(filename,"pdf")
            if not lfs.isfile(pdfname) then
                -- We don't have the titlepage yet but need to fetch
                -- the template from the real path.
                local path = environment.arguments.path
                if lfs.isdir(path) then
                    os.execute('context --global --path="' .. path .. '" ' .. cldname)
                else
                    -- bad news
                end
            end
        end
    end
\stopluacode


\stopenvironment
