%% cloze.tex
%% Copyright 2015-2025 Josef Friedrich
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Josef Friedrich.
%
% This work consists of the files cloze.lua, cloze.tex,
% and cloze.sty.

% \subsection{The file \tt{cloze.tex}}
%
% The cloze package uses following naming conventions: Internal / private
% macros / commands / envirnoments are written in PascalCase, public
% ones are written in lowercase. Earlier versions of this package used
% @ characters for private macros. The lower level / private macros
% are now defined in the plain Lua\TeX{} version of the package and
% used to set cloze text in plain Lua\TeX{}.
%
%    \begin{macrocode}
\directlua{
  cloze = require('cloze')
  cloze.register_callback('visitor')
  cloze.register_functions()
}
%    \end{macrocode}
%
%    \begin{macrocode}
\input farbe.tex
%    \end{macrocode}
%
%    \begin{macrocode}
\newif\ifclozeshow\clozeshowtrue
%    \end{macrocode}
%
% \subsubsection{Internal macros}
%
% \begin{macro}{\ClozeSetToGlobal}
% Set the Lua variable |registry.is_global| to |true|. All options
% are then stored in the variable |registry.global_options|.
%    \begin{macrocode}
\def\ClozeSetToGlobal{%
  \directlua{cloze.set_options_dest(true)}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeSetToLocal}
% First unset the variable |registry.local_options|. Now set the Lua
% variable |registry.is_global| to |false|. All options are then stored
% in the variable |registry.local_options|.
%    \begin{macrocode}
\def\ClozeSetToLocal{%
  \directlua{
    cloze.unset_local_options()
    cloze.set_options_dest(false)
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeGetOption}
% This macro is used in the documentation to show the default values
% of some options.
%    \begin{macrocode}
\def\ClozeGetOption#1{%
  \directlua{
    tex.print(cloze.get_option('#1'))
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeColor}
% Convert a color definiton name to a PDF colorstack string, for example
% convert the color name |blue| to the colorstack string
% |0 0 1 rg 0 0 1 RG|. The macro definition |\ClozeColor{blue}|
% builds itself the macro |\color@blue|, which expands to the PDF
% colorstack string. The colorstack string is necessary to generate a
% PDF colorstack whatsit.
%    \begin{macrocode}
\def\ClozeColor#1{\csname\string\color@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeStartMarker}
% At the begining |\ClozeStartMarker| registers the required Lua
% callbacks. Then it inserts a whatsit marker which marks the begin of a
% gap.
%    \begin{macrocode}
\def\ClozeStartMarker#1{%
  \strut\directlua{
    cloze.register_callback('#1')
    cloze.marker('#1', 'start')
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeStopMarker}
% \cmd{\ClozeStopMarker} inserts a whatsit marker that marks the end
% of gap.
%    \begin{macrocode}
\def\ClozeStopMarker#1{%
  \strut\directlua{
    cloze.marker('#1', 'stop')
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeWrapWithFont}
% \cmd{\ClozeWrapWithFont} wraps the text with the font definition.
%    \begin{macrocode}
\def\ClozeWrapWithFont#1{%
  {%
    \clozefont\relax%
    #1%
  }%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeMargin}
% \cmd{\ClozeMargin} surrounds a text in a gap with two |kerns|.
%    \begin{macrocode}
\def\ClozeMargin#1{%
  \directlua{cloze.write_margin_node()}%
  #1%
  \directlua{cloze.write_margin_node()}%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Public plain \TeX{} macros}
%
% \begin{macro}{\clozesetoption}
% \cmd{\clozesetoption} is a wrapper for the Lua function
% |registry.set_option|. \cmd{\clozesetoption}\marg{key}\marg{value}
% sets a key \meta{key} to the value \meta{value}.
%    \begin{macrocode}
\def\clozesetoption#1#2{%
  \ClozeSetToGlobal%
  \directlua{cloze.set_option('#1', '#2')}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozesetlocaloption}
% \cmd{\clozesetlocaloption} is a wrapper for the Lua function
% |registry.set_option|. \cmd{\clozesetlocaloption}\marg{key}\marg{value}
% sets a key \meta{key} to the value \meta{value}.
%    \begin{macrocode}
\def\clozesetlocaloption#1#2{%
  \ClozeSetToLocal%
  \directlua{cloze.set_option('#1', '#2')}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozereset}
% The usage of the command \cmd{\clozereset} is described in detail in
% section \TmpSecRef{sec:command-clozereset}.
%    \begin{macrocode}
\def\clozereset{%
  \ClozeSetToGlobal%
  \directlua{cloze.reset()}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeshow}
% The usage of the command \cmd{\clozeshow} is described in detail in
% section \TmpSecRef{sec:command-clozeshow}.
%    \begin{macrocode}
\def\clozeshow{%
  \clozeshowtrue%
  \ClozeSetToGlobal%
  \clozesetoption{visibility}{true}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozehide}
% The usage of the command \cmd{\clozehide} is described in detail in
% section \TmpSecRef{sec:command-clozehide}.
%    \begin{macrocode}
\def\clozehide{%
  \clozeshowfalse%
  \ClozeSetToGlobal%
  \clozesetoption{visibility}{false}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\clozefont}
% The usage of the command \cmd{\clozefont} is described in detail in
% section \TmpSecRef{sec:command-clozefont}.
%    \begin{macrocode}
\def\clozefont{\it}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozesetfont}
% The usage of the command \cmd{\clozesetfont} is described in detail in
% section \TmpSecRef{sec:command-clozesetfont}.
%    \begin{macrocode}
\def\clozesetfont#1{%
  \def\clozefont{%
    #1%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cloze}
% This is the plain Lua\TeX-Version of the macro \cmd{\cloze}.
%    \begin{macrocode}
\def\cloze#1{%
  \ClozeStartMarker{basic}%
  \ClozeWrapWithFont{\ClozeMargin{#1}}%
  \ClozeStopMarker{basic}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefix}
% This is the plain Lua\TeX-Version of \cmd{\clozefix}. The usage of the
% command \cmd{\clozefix} is described in detail in
% section \TmpSecRef{sec:command-clozefix}.
%    \begin{macrocode}
\def\clozefix#1{%
  \ClozeStartMarker{fix}%
  {%
    \clozefont\relax%
    \ClozeMargin{#1}%
  }%
  \ClozeStopMarker{fix}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozenol}
% This is the plain Lua\TeX-Version of the macro \cmd{\clozenol}. The
% usage of the command \cmd{\clozenol} is described in detail in
% section \TmpSecRef{sec:command-clozenol}.
%    \begin{macrocode}
\def\clozenol#1{%
  \clozesetoption{thickness}{0pt}%
  \ClozeStartMarker{basic}%
  \ClozeWrapWithFont{\ClozeMargin{#1}}%
  \ClozeStopMarker{basic}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeline}
% This is the plain Lua\TeX-Version of the macro \cmd{\clozeline}. The
% usage of the command \cmd{\clozeline} is described in detail in
% section \TmpSecRef{sec:command-clozeline}.
%    \begin{macrocode}
\def\clozeline{%
  \directlua{cloze.write_line_nodes()}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozelinefil}
% This is the plain Lua\TeX-Version of the macro \cmd{\clozelinefil}.
% The usage of the command \cmd{\clozelinefil} is described in detail in
% section \TmpSecRef{sec:command-clozelinefil}.
%    \begin{macrocode}
\def\clozelinefil{%
  \strut%
  \directlua{cloze.write_linefil_nodes()}%
  \strut%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefil}
% This is the plain Lua\TeX-Version of the macro \cmd{\clozefil}.
% The usage of the command \cmd{\clozefil} is described in detail in
% section \TmpSecRef{sec:command-clozefil}.
%    \begin{macrocode}
\def\clozefil#1{%
  \cloze{#1}\clozelinefil%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\clozeparplain}
% The usage of the macro \cmd{\clozeparplain} is described in detail in
% section \TmpSecRef{sec:command-clozeparplain}.
%    \begin{macrocode}
\def\clozeparplain#1{%
  \par%
  \ClozeStartMarker{par}%
  \ClozeWrapWithFont{#1}%
  \ClozeStopMarker{par}%
  \par%
  \directlua{cloze.unregister_callback('par')}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeparcapture}
% The usage of the macro \cmd{\clozeparcapture} is described in detail in
% section \TmpSecRef{sec:command-clozeparcapture}.
%    \begin{macrocode}
\def\clozeparcapture#1\par {%
  \clozeparplain{#1}%
}
\let\clozeparcmd=\clozeparcapture
%    \end{macrocode}
% \end{macro}
