%%
%% This is file `ekdosis.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% ekdosis.dtx  (with options: `package')
%% -------:| -----------------------------------------------------------------
%% ekdosis:| Typesetting TEI xml compliant critical editions
%%  Author:| Robert Alessi
%%  E-mail:| alessi@robertalessi.net
%% License:| Released under the GNU General Public License v3 or later
%%     See:| http://www.gnu.org/licenses/
%% 
%% This file is part of the `ekdosis' package
%% 
%% ekdosis -- Typesetting TEI xml compliant critical editions
%% Copyright (C) 2020--2021  Robert Alessi
%% 
%% Please send error reports and suggestions for improvements to Robert
%% Alessi <alessi@robertalessi.net>
%% 
%% This program is free software: you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation, either version 3 of the License, or
%% (at your option) any later version.
%% 
%% This program is distributed in the hope that it will be useful, but
%% WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%% General Public License for more details.
%% 
%% You should have received a copy of the GNU General Public License
%% along with this program.  If not, see
%% <http://www.gnu.org/licenses/>.
%% 
%% This work consists of the files ekdosis.dtx, ekdosis.el, this file and
%% a Makefile.
%% Running "make" generates the derived files README, ekdosis.pdf and
%% ekdosis.sty.
%% Running "make inst" installs the files in the user's TeX tree.
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{ekdosis}
    [2021/11/21 v1.4 Typesetting TEI xml-compliant critical editions]
\RequirePackage{iftex}
\RequireLuaTeX
\RequirePackage{expkv-opt}
\RequirePackage{expkv-def}
\newif\if@pkg@float
\newif\if@pkg@footins
\newif\if@pkg@keyfloat
\newif\if@pkg@fitapp
\newif\if@pkg@ekddivs
\newif\if@parnotesroman
\newif\if@pkg@parnotes
\newif\iftei@export
\newif\if@pkg@poetry@verse
\ekvdefinekeys{ekdosis}{
  choice layout = {float = {\@pkg@floattrue},
    footins = {\@pkg@floatfalse\@pkg@footinstrue},
    keyfloat = {\@pkg@floatfalse\@pkg@keyfloattrue},
    fitapp = {\@pkg@floatfalse\@pkg@fitapptrue}},
  initial layout = float,
  unknown-choice layout = \PackageError{ekdosis}{unknown
    layout=#1}{`layout' must be either `float' or `footins'.},
  choice divs = {ekdosis = {\@pkg@ekddivstrue},
    latex = {\@pkg@ekddivsfalse
      \AtBeginDocument{\luadirect{ekdosis.setekddivsfalse()}}}},
  initial divs = ekdosis,
  unknown-choice divs = \PackageError{ekdosis}{unknown divs=#1}{`divs'
    must be either `ekdosis' or `latex'.},
  choice poetry = {verse = {\@pkg@poetry@versetrue}},
  unknown-choice poetry = \PackageError{ekdosis}{unknown
    poetry=#1}{`poetry' must be `verse' for now.},
  choice parnotes = {false = {},
    true = {\@pkg@parnotestrue},
    roman = {\@pkg@parnotestrue\@parnotesromantrue}},
  default parnotes = true,
  unknown-choice parnotes = \PackageError{ekdosis}{unknown
    parnotes=#1}{`parnotes' must be either `true', or `false' or
    `roman'.},
  choice teiexport = {false = {},
    true = {\tei@exporttrue
      \AtBeginDocument{\luadirect{ekdosis.openteistream()}}%
      \AtEndDocument{\luadirect{ekdosis.closeteistream()}}},
    tidy = {\tei@exporttrue
      \AtBeginDocument{\luadirect{ekdosis.openteistream()}}%
      \AtEndDocument{\luadirect{ekdosis.closeteistream("tidy")}}}},
  default teiexport = true,
  unknown-choice teiexport = \PackageError{ekdosis}{unknown
    teiexport=#1}{`teiexport' must be either `true', `false' or
    `tidy'.}
}
\ekvoProcessLocalOptions{ekdosis}
\newif\ifekd@memoir@loaded
\@ifclassloaded{memoir}{%
  \ekd@memoir@loadedtrue\@pkg@poetry@versetrue}{}
\RequirePackage{luacode}
\RequirePackage{paracol}
\RequirePackage{etoolbox}
\RequirePackage{lineno}
\if@pkg@float
  \RequirePackage{trivfloat}
  \trivfloat{ekdapparatus}
\fi
\if@pkg@keyfloat
  \RequirePackage{keyfloat}
  \def\ekd@keyparopts#1{%
    \def\ekd@insert@keyparapp{%
      \keyparbox[!b]{#1}{\ekd@insert@apparatus}}}
  \ekd@keyparopts{}
\fi
\if@pkg@fitapp
  \RequirePackage{tcolorbox}
  \tcbuselibrary{fitting,skins}
\fi
\RequirePackage{refcount}
\RequirePackage{zref-user}
\RequirePackage{zref-abspage}
\RequirePackage{ltxcmds}
\RequirePackage{pdftexcmds}
\RequirePackage{ifoddpage}
\if@pkg@poetry@verse
  \RequirePackage{verse}
\fi
\if@pkg@parnotes
  \RequirePackage{parnotes}
\fi
\luadirect{dofile(kpse.find_file("ekdosis.lua"))}
\AtEndDocument{
  \luadirect{ekdosis.closestream()}
}
\ekvdefinekeys{ekd@setup}{
  bool showpagebreaks = \ifekd@showpb,
  store spbmk = \ekd@spbmk,
  initial spbmk = spb,
  store hpbmk = \ekd@hpbmk,
  initial hpbmk = hpb,
}
\NewDocumentCommand{\ekdsetup}{m}{\ekvset{ekd@setup}{#1}}
\@onlypreamble\ekdsetup
\ekvdefinekeys{ekd@hooks}{
  store appfontsize = \ekd@appfontsize,
  store refnumstyle = \ekd@refnumstyle,
  store postrefnum = \ekd@postrefnum,
  code familysep = \luadirect{ekdosis.setfamilysep(\luastringN{#1})},
  store lemmastyle = \ekd@lemmastyle,
  store readingstyle = \ekd@readingstyle,
  code keyparopts = \if@pkg@keyfloat\ekd@keyparopts{#1}\fi,
  dimen appheight = \ekd@app@height,
  initial appheight = .5\textheight,
  choice fitalgorithm = {fontsize = \def\ekd@fit@algorithm{fontsize},
    hybrid = \def\ekd@fit@algorithm{hybrid},
    areasize = \def\ekd@fit@algorithm{areasize},
    squeeze = \def\ekd@fit@algorithm{squeeze}},
  initial fitalgorithm = fontsize,
  unknown-choice fitalgorithm = \PackageError{ekdosis}{unknown
    fitalgorithm=#1}{`fitalgorithm' must be either `fontsize',
    `hybrid', `areasize' or `squeeze'.},
  code initialrule = \def\ekd@initial@rule{#1\NLS},
  default initialrule = \rule{0.4\columnwidth}{0.4pt},
  noval noinitialrule = \undef\ekd@initial@rule,
  initial appfontsize = \footnotesize,
  initial refnumstyle = \bfseries,
  initial postrefnum = ~,
  initial lemmastyle = {},
  initial readingstyle = {}
}
\NewDocumentCommand{\SetHooks}{m}{\ekvset{ekd@hooks}{#1}}
\ekvdefinekeys{ekd@witness}{
  store settlement = \settlement@value,
  store institution = \institution@value,
  store repository = \repository@value,
  store collection = \collection@value,
  store idno = \idno@value,
  store msName = \msName@value,
  store origDate = \origDate@value,
  store locus = \locus@value
}
\NewDocumentCommand{\DeclareWitness}{m m m O{}}{%
  \bgroup
  \ekvset{ekd@witness}{#4}
    \luadirect{ekdosis.newwitness(
      \luastringN{#1},
      \luastringN{#2},
      \luastringN{#3},
      \luastringO{\settlement@value},
      \luastringO{\institution@value},
      \luastringO{\repository@value},
      \luastringO{\collection@value},
      \luastringO{\idno@value},
      \luastringO{\msName@value},
      \luastringO{\origDate@value},
      \luastringO{\locus@value})}
    \egroup
  }
\@onlypreamble\DeclareWitness
\NewDocumentCommand{\DeclareHand}{m m m +O{}}{
  \luadirect{ekdosis.newhand(\luastringN{#1},
    \luastringN{#2},
    \luastringN{#3},
    \luastringN{#4})}
}
\@onlypreamble\DeclareHand
\ekvdefinekeys{ekd@scholar}{
  store rawname = \rawname@value,
  store forename = \forename@value,
  store surname = \surname@value,
  store addname = \addname@value,
  store note = \note@value
}
\NewDocumentCommand{\DeclareScholar}{m m O{}}{%
  \bgroup
  \ekvset{ekd@scholar}{#3}
    \luadirect{ekdosis.newscholar(
      \luastringN{#1},
      \luastringN{#2},
      \luastringO{\rawname@value},
      \luastringO{\forename@value},
      \luastringO{\surname@value},
      \luastringO{\addname@value},
      \luastringO{\note@value})}
    \egroup
  }
\@onlypreamble\DeclareScholar
\NewDocumentCommand{\DeclareSource}{m m}{
  \luadirect{ekdosis.newsource(\luastringN{#1},
    \luastringN{#2})}
}
\@onlypreamble\DeclareSource
\NewDocumentCommand{\DeclareShorthand}{m m m}{
  \luadirect{ekdosis.newshorthand(\luastringN{#1},
    \luastringN{#2},
    \luastringN{#3})}
}
\@onlypreamble\DeclareShorthand
\NewDocumentCommand{\getsiglum}{m}{%
  \luadirect{tex.sprint(ekdosis.getsiglum(\luastringN{#1}))}%
}
\NewDocumentCommand{\SigLine}{m}{%
  \luadirect{tex.sprint(ekdosis.basic_cs(\luastringN{#1}))}
}
\ekvdefinekeys{tei@settings}{
  choice autopar = {true = \luadirect{ekdosis.setteiautopar("yes")},
    false = {\luadirect{ekdosis.setteiautopar("no")}}},
  initial autopar = true,
  unknown-choice autopar = \PackageError{ekdosis}{unknown
    autopar=#1}{`autopar' must be either `true' or `false'.}
}
\NewDocumentCommand{\SetTEIxmlExport}{m}{
  \unless\ifekd@state\ekvset{tei@settings}{#1}\fi
}
\NewDocumentCommand{\TeXtoTEI}{m m O{}}{%
  \luadirect{ekdosis.newcmdtotag(\luastringN{#1},
    \luastringN{#2},
    \luastringN{#3})}
}
\NewDocumentCommand{\teidirect}{O{}mm}{\ignorespaces}
\NewDocumentCommand{\EnvtoTEI}{s m m O{}}{%
  \IfBooleanTF{#1}{%
  \luadirect{ekdosis.newenvtotag(\luastringN{#2},
    \luastringN{#3},
    \luastringN{#4},
    "yes")}
  }{%
  \luadirect{ekdosis.newenvtotag(\luastringN{#2},
    \luastringN{#3},
    \luastringN{#4})}
  }
}
\NewDocumentCommand{\TeXtoTEIPat}{m m}{%
  \luadirect{ekdosis.newpatttotag(\luastringN{#1}, \luastringN{#2})}
}
\NewDocumentCommand{\SetTEIFileName}{m}{
  \luadirect{ekdosis.setteifilename(\luastringN{#1})}
}
\@onlypreamble\SetTEIFileName
\NewDocumentCommand{\AddxmlBibResource}{m}{
  \luadirect{ekdosis.addxmlbibresource(\luastringN{#1})}
}
\@onlypreamble\AddxmlBibResource
\newif\ifekd@lang@pkg
\NewDocumentCommand{\ekd@test@lang}{}{%
  \ltx@ifpackageloaded{babel}{\ekd@lang@pkgtrue}{}%
  \ltx@ifpackageloaded{polyglossia}{\ekd@lang@pkgtrue}{}%
}
\newif\ifekd@mapps
\ekvdefinekeys{ekd@newapp}{
  choice direction = {LR = \def\direction@val{LR},
                      RL = \def\direction@val{RL}},
  unknown-choice direction = \PackageError{ekdosis}{unknown
    direction=#1}{`direction' must be either `LR' or `RL'.},
  store rule = \rule@val,
  nmeta norule = {rule=none},
  code delim = \def\delim@val{\unexpanded{#1}},
  store sep = \sep@val,
  store subsep = \subsep@val,
  store bhook = \bhook@val,
  store ehook = \ehook@val,
  store maxentries = \limit@val,
  store lang = \lang@val,
  store notelang = \notelang@val,
  initial direction = LR,
  initial delim = {},
  initial ehook = {\csname ekd@end@apparatus\endcsname}
}
\NewDocumentCommand{\DeclareApparatus}{m O{}}{
  \newbool{subsq@unit@#1}
  \booltrue{subsq@unit@#1}
  \unless\ifekd@mapps\global\ekd@mappstrue\fi
  \bgroup
  \ekvset{ekd@newapp}{#2}
  \luadirect{ekdosis.newapparatus(
    \luastringN{#1},
    \luastring{\direction@val},
    \luastringO{\rule@val},
    \luastringO{\delim@val},
    \luastringO{\sep@val},
    \luastringO{\subsep@val},
    \luastringO{\bhook@val},
    \luastringO{\ehook@val},
    \luastringO{\limit@val},
    \luastringO{\lang@val},
    \luastringO{\notelang@val}
    )}
  \egroup
}
\@onlypreamble\DeclareApparatus
\NewDocumentCommand{\addentries}{O{\ekdan@type} m}{%
  \luadirect{ekdosis.addto_bagunits(\luastringO{#1}, \luastringN{#2})}%
  \ignorespaces
}
\newcounter{ekd@pb}
\globalcounter{ekd@pb}
\NewDocumentCommand{\ekdpb}{s o m}{%
  \IfBooleanTF{#1}
  {\ifekd@showpb\marginpar{\ekd@hpbmk}\fi
    \pagebreak}
  {%
    \def\@tmpoarg{#2}%
    \def\@tmpmarg{#3}%
    \stepcounter{ekd@pb}%
    \linelabel{ekdpb:\theekd@pb}%
    \def\tmp@ln{%
      \getrefnumber{ekdpb:\theekd@pb}}%
    \def\tmp@pg{%
      \getpagerefnumber{ekdpb:\theekd@pb}}%
    \IfNoValueTF{#2}
    {\ifnum
      \pdf@strcmp{\@tmpmarg}{\tmp@ln} = 0
      \ifekd@showpb\marginpar{\ekd@spbmk}\fi
      \pagebreak
      \else
      \ifekd@showpb\marginpar{[\ekd@spbmk]}\fi
      \fi}
    {\ifnum
      \pdf@strcmp{\@tmpoarg}{\tmp@pg} = 0
      \ifnum
      \pdf@strcmp{\@tmpmarg}{\tmp@ln} = 0
      \ifekd@showpb\marginpar{\ekd@spbmk}\fi
      \pagebreak
      \else
      \ifekd@showpb\marginpar{[\ekd@spbmk]}\fi
      \fi
      \fi
    }%
  }\ignorespaces
}
\newbool{do@app}
\newif\ifekd@state
\newif\ifekd@isinapp
\newif\ifekd@isinlem
\newif\ifekd@appinapp
\providebool{al@rlmode}
\@ifpackageloaded{arabluatex}{}{%
  \def\setRL{\booltrue{al@rlmode}\pardir TRT\textdir TRT}
  \def\setLR{\boolfalse{al@rlmode}\pardir TLT \textdir TLT}
}
\protected\def\LRnum#1{\bgroup\textdir TLT#1\egroup}
\newcounter{ekd@lab}
\globalcounter{ekd@lab}
\NewDocumentCommand{\unconditional@appin}{o m}{%
  \IfNoValueTF{#1}
  {\luadirect{ekdosis.appin(\luastringO{#2})}}
  {\luadirect{ekdosis.appin(\luastringO{#2}, \luastringO{#1})}}%
}
\def\blfootnote{\gdef\@thefnmark{\relax}\@footnotetext}
 % \def\blfootnote{\gdef\@thefnmark{}\@blfootnotetext}
\long\def\@blfootnotetext#1{\insert\footins{%
    \reset@font\footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makeblfntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%
\newcommand\@makeblfntext[1]{%
    \parindent 1em%
    \noindent
    \hb@xt@0em{\hss\@makefnmark}#1}
\newif\ifrtl@app
\edef\ekdsep{] }
\edef\ekdsubsep{}
\ekvdefinekeys{default@app}{
  choice direction = {LR = \rtl@appfalse,
    RL = \rtl@apptrue},
  unknown-choice direction = \PackageError{ekdosis}{unknown
    direction=#1}{`direction' must be either `LR' or `RL'.},
  code sep = \edef\ekdsep{#1},
  code subsep = \edef\ekdsubsep{#1},
  store bhook = \ekd@begin@apparatus,
  initial bhook = {},
  store ehook = \ekd@end@apparatus,
  initial ehook = {},
  store delim = \ekd@unit@delim,
  initial delim = {},
  store rule = \ekd@default@rule,
  initial rule = \rule{0.4\columnwidth}{0.4pt},
  noval norule = \def\ekd@default@rule{\mbox{}},
  store lang = \ekd@singleapp@lang,
  initial lang = \ltx@ifpackageloaded{babel}{\languagename}{%
    \ltx@ifpackageloaded{polyglossia}{\languagename}{}},
  store notelang = \ekd@singleapp@note@lang,
  initial notelang = \ltx@ifpackageloaded{babel}{\languagename}{%
  \ltx@ifpackageloaded{polyglossia}{\languagename}{}}
}
\NewDocumentCommand{\SetApparatus}{m}{
  \ekvset{default@app}{#1}
}
\NewDocumentCommand{\SetRTLapp}{}{\rtl@apptrue}
\NewDocumentCommand{\SetLTRapp}{}{\rtl@appfalse}
\NewDocumentCommand{\SetSeparator}{m}{\edef\ekdsep{#1}}
\NewDocumentCommand{\SetSubseparator}{m}{\edef\ekdsubsep{#1}}
\NewDocumentCommand{\SetBeginApparatus}{m}{\edef\ekd@begin@apparatus{#1}}
\NewDocumentCommand{\SetEndApparatus}{m}{\edef\ekd@end@apparatus{#1}}
\NewDocumentCommand{\SetUnitDelimiter}{m}{\def\ekd@unit@delim{#1}}
\NewDocumentCommand{\SetApparatusLanguage}{m}{%
  \def\ekd@singleapp@lang{#1}}
\NewDocumentCommand{\SetApparatusNoteLanguage}{m}{%
  \def\ekd@singleapp@note@lang{#1}}
\newif\iffootnoterule
\footnoteruletrue
\let\dflt@footnoterule\footnoterule
\let\dflt@pcol@footnoterule\pcol@footnoterule
\renewcommand\footnoterule{%
  \iffootnoterule
  \dflt@footnoterule%
  \fi
}
\renewcommand\pcol@footnoterule{%
  \iffootnoterule
  \dflt@pcol@footnoterule%
  \fi
}
\NewDocumentCommand{\SetDefaultRule}{m}{%
  \def\@tempa{#1}
  \ifx\@tempa\empty\def\ekd@default@rule{\mbox{}}%
  \else%
  \def\ekd@default@rule{#1}%
  \fi}
\newcommand*{\NLS}{%
  \nobreak\@normalcr\relax
  % \par
  % \nobreak
  % \vspace{-\parskip}%
  % \leavevmode
  % \noindent
  % \ignorespaces
}
\newif\ifsubsq@unit
\subsq@unittrue
\newif\ifekd@inside@app
\newif\ifekd@keepinapp
\if@pkg@fitapp
  \newtcboxfit{\ekd@fitapp}{%
    blankest,
    fit basedim = \f@size pt,
    fit fontsize macros,
    fit height from=0pt to \ekd@app@height,
    fit algorithm = \ekd@fit@algorithm,
    float=!b}
\fi
\long\def\ekd@insert@apparatus{%
  \unless\ifekd@mapps
  \ifrtl@app\pardir TRT\leavevmode\textdir TRT\else
    \pardir TLT\leavevmode\textdir TLT\fi
  \fi
  \if@pkg@parnotes
   \if@parnotesroman
    \renewcommand*{\theparnotemark}{\roman{parnotemark}}\fi
   \parnoteclear\fi
  \ekd@inside@apptrue
  \ekd@appfontsize
  \ifekd@mapps
    \ifdefined\ekd@initial@rule
      \ekd@initial@rule
    \fi
  \fi
  \apparatus\unless\ifekd@mapps\ekd@end@apparatus\fi
  \ekd@inside@appfalse
  \if@pkg@parnotes\parnotes\parnotereset\fi
}%
\def\add@@apparatus{%
  \if@pkg@parnotes\parnotes\else\fi
  \if@pkg@footins
  \bgroup
    \unless\ifekd@mapps
      \ifrtl@app\pardir TRT\leavevmode\textdir TRT\else
        \pardir TLT\leavevmode\textdir TLT\fi
    \fi
  \blfootnote{%
    \if@pkg@parnotes
    \if@parnotesroman
    \renewcommand*{\theparnotemark}{\roman{parnotemark}}\else\fi
    \parnoteclear\else\fi
    \ekd@inside@apptrue
    \ekd@appfontsize
    \ifekd@mapps
      \ifdefined\ekd@initial@rule
        \ekd@initial@rule
      \fi
    \fi
    \apparatus\unless\ifekd@mapps\ekd@end@apparatus\fi
    \ekd@inside@appfalse
    \if@pkg@parnotes\parnotes\parnotereset\else\fi
  }%
  \egroup
  \fi
  \if@pkg@float
  \begin{ekdapparatus}[!b]%
    \ekd@insert@apparatus
  \end{ekdapparatus}%
  \fi
  \if@pkg@keyfloat
    \ekd@insert@keyparapp
  \fi
  \if@pkg@fitapp
    \ekd@fitapp{\ekd@insert@apparatus}%
  \fi
}
\def\add@apparatus{%
  \test@apparatus%
  \ifbool{do@app}{\subsq@unitfalse\add@@apparatus}{}%
}
\NewDocumentCommand{\append@app}{o +m}{%
  \ifekd@isinapp%
    \ifekd@state%
    \IfNoValueTF{#1}%
      {\luadirect{ekdosis.appin(\luastringO{#2})}}%
      {\luadirect{ekdosis.appin(\luastringO{#2}, \luastringO{#1})}}%
    \fi%
  \fi}
\NewDocumentCommand{\append@ln@app}{o +m}{%
  \IfNoValueTF{#1}
  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringO{#2}))}}
  {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringO{#2},
      \luastringO{#1}))}}}
\def\outerlinenumbers{%
  \def\makeLineNumberRunning{%
    \checkoddpage
    \ifoddpage
    \linenumberfont\hskip\linenumbersep\hskip\textwidth
    \hbox to\linenumberwidth{\hss\LineNumber}\hss
    \else
    \hss\linenumberfont\LineNumber\hskip\linenumbersep
    \fi
  }%
}
\def\innerlinenumbers{%
  \def\makeLineNumberRunning{%
    \checkoddpage
    \ifoddpage
    \hss\linenumberfont\LineNumber\hskip\linenumbersep
    \else
    \linenumberfont\hskip\linenumbersep\hskip\textwidth
    \hbox to\linenumberwidth{\hss\LineNumber}\hss
    \fi
  }%
}
\newif\ifekd@pagelineation
\newif\ifekd@pagevlineation
\NewDocumentCommand{\ekdatbegshihook}{}{%
  \ifekd@pagelineation\resetlinenumber\fi
}
\AddToHook{shipout/before}{\ekdatbegshihook}
\newif\ifekd@elidednumbers
\ekvdefinekeys{ekd@lineation}{
  choice lineation = {page = \ekd@pagelineationtrue,
    document = \ekd@pagelineationfalse,
    none = \ekd@pagelineationtrue
           \renewcommand\thelinenumber{}},
  unknown-choice lineation = \PackageError{ekdosis}{unknown
    lineation=#1}{`lineation' must be either `page' or `document'.},
  choice vlineation = {page = \ekd@pagevlineationtrue,
    document = \ekd@pagevlineationfalse},
  unknown-choice vlineation = \PackageError{ekdosis}{unknown
    vlineation=#1}{`vlineation' must be either `page' or `document'.},
  code modulonum = \chardef\c@linenumbermodulo#1\relax,
  noval modulo = \modulolinenumbers,
  code vmodulo = \ifekd@memoir@loaded\linenumberfrequency{#1}
                 \else\if@pkg@poetry@verse\poemlines{#1}\fi\fi,
  initial vmodulo = 1,
  default vmodulo = 5,
  bool vnumbrokenlines = \ifnum@brokenline,
  bool continuousvnum = \if@continuous@vnum,
  choice numbers = {elided = \ekd@elidednumberstrue,
    full = \ekd@elidednumbersfalse},
  unknown-choice numbers = \PackageError{ekdosis}{unknown
    numbers=#1}{`numbers' must be either `elided' or `full'.},
  initial numbers = elided,
  choice margin = {right = \rightlinenumbers,
    left = \leftlinenumbers,
    inner = \innerlinenumbers,
    outer = \outerlinenumbers},
  unknown-choice margin = \PackageError{ekdosis}{unknown
    margin=#1}{`margin' must be either `left', `right', \MessageBreak
    `inner' or `outer'},
  choice vmargin = {
    right = \if@pkg@poetry@verse\verselinenumbersright\fi,
    left = \if@pkg@poetry@verse\verselinenumbersleft\fi},
  unknown-choice vmargin = \PackageError{ekdosis}{unknown
    vmargin=#1}{`margin' must be either `left' ot `right'}
}
\NewDocumentCommand{\SetLineation}{m}{
  \ekvset{ekd@lineation}{#1}
}
\NewDocumentCommand{\vmodulolinenumbers}{O{5}}{%
  \ifekd@memoir@loaded
    \linenumberfrequency{#1}%
  \else
    \if@pkg@poetry@verse
      \poemlines{#1}%
    \fi
  \fi
  \ignorespaces
}
\renewcommand\linenumberfont{\normalfont\footnotesize}
\ekvdefinekeys{appnote}{
   store type = \ekdan@type,
   initial type = default
 }
\NewDocumentCommand{\SetDefaultApparatus}{m}{%
  \ekvset{appnote}{type=#1}}
\NewDocumentCommand{\app}{O{} > { \TrimSpaces } +m}{%
  \leavevmode
  \begingroup
  \ekvset{appnote}{#1}%
  \ifekd@isinapp\ekd@appinapptrue\fi
  \ekd@isinapptrue
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{ekdosis.storeabspg(
    \luastring{\zref@extract{ekd:\theekd@lab}{abspage}})}%
  \ifekd@state\add@apparatus\fi
  \luadirect{tex.sprint(ekdosis.removesp(\luastringN{#2}))}%
  \ekd@isinappfalse
  \ekd@appinappfalse
  \endgroup}
\NewDocumentCommand{\ekdpage}{}{%
  \luadirect{tex.sprint(ekdosis.getekdabspg())}%
}
\def\current@ref@arg#1#2{{%\textdir TLT%
    \unexpanded\expandafter{\ekd@refnumstyle}%
    \ifnum%
    \pdf@strcmp{\getpagerefnumber{#1}}{\getpagerefnumber{#2}}
    =
    0
    \ifnum%
    \pdf@strcmp{\getrefnumber{#1}}{\getrefnumber{#2}}
    =
    0
    %
    \ifekd@mapps%
    \ifbool{subsq@unit@\ekdan@type}{%
      \ifnum%
      \pdf@strcmp{\getrefnumber{#1}}{%
        \getrefnumber{\luadirect{tex.sprint(ekdosis.getprevnotelab())}}}
      =
      0
      \else
      \LRnum{\getrefnumber{#1}}\unexpanded\expandafter{\ekd@postrefnum}% issue the no
      \fi%
    }%
    {\LRnum{\getrefnumber{#1}}\unexpanded\expandafter{\ekd@postrefnum}}% issue the no
    \else
    \ifsubsq@unit%
    %
    \ifnum%
    \pdf@strcmp{\getrefnumber{#1}}{%
      \getrefnumber{\luadirect{tex.sprint(ekdosis.getprevnotelab())}}}
    =
    0
    \else
    \LRnum{\getrefnumber{#1}}\unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    %
    \else
    \LRnum{\getrefnumber{#1}}\unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    \fi
    %
    \else
    \ifekd@elidednumbers
    \luadirect{tex.sprint(ekdosis.numrange(\luastring{\getrefnumber{#1}},
      \luastring{\getrefnumber{#2}}))}%
        \unexpanded\expandafter{\ekd@postrefnum}% issue the nos
    \else
    \LRnum{\getrefnumber{#1}}--%
    \LRnum{\getrefnumber{#2}}\unexpanded\expandafter{\ekd@postrefnum}% issue the nos
    \fi
    \fi%
    \else
    \ifboolexpr{bool {ekd@pagelineation} or bool {ekd@pagevlineation}}
      {\LRnum{\getrefnumber{#1}}--%
      \LRnum{\getpagerefnumber{#2}}.%
      \LRnum{\getrefnumber{#2}}\unexpanded\expandafter{\ekd@postrefnum}}% issue pg and ln nos
      {\LRnum{\getrefnumber{#1}}--%
      \LRnum{\getrefnumber{#2}}\unexpanded\expandafter{\ekd@postrefnum}}% issue the nos
    \fi%
    \ifekdn@forcenum
      \LRnum{\getrefnumber{#1}}\unexpanded\expandafter{\ekd@postrefnum}% force the no
    \fi
  }%
}
\def\current@ref{{%\textdir TLT%
    \unexpanded\expandafter{\ekd@refnumstyle}%
    \ifnum%
    \pdf@strcmp{%
      \getpagerefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      {\getpagerefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}
    =
    0
    \ifnum%
    \pdf@strcmp{%
      \getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      {\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}
    =
    0
    %
    \ifekd@mapps%
    \ifbool{subsq@unit@\ekdan@type}{%
      \ifnum%
      \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevlnlab())}-b}}
      =
      0
    %%%begin
    \ifnum%
    \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}%
    {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevlnlab())}-e}}
    =
    0
    \ifekd@appinapp
    \ifnum%
    \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
    {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevprevlnlab())}-b}}
    =
    0
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    \fi
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    %%% end
      \else
      \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
        \unexpanded\expandafter{\ekd@postrefnum}% issue the no
      \fi%
    }{\LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}}% issue the no
    \else
    \ifsubsq@unit%
    %
    \ifnum%
    \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
    {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevlnlab())}-b}}
    =
    0
    %%%begin
    \ifnum%
    \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}%
    {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevlnlab())}-e}}
    =
    0
    \ifekd@appinapp
    \ifnum%
    \pdf@strcmp{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
    {\getrefnumber{\luadirect{tex.sprint(ekdosis.getprevprevlnlab())}-b}}
    =
    0
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    \fi
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    %%% end
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    %
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% issue the no
    \fi
    \fi
    %
    \else
    \ifekd@elidednumbers
    \luadirect{tex.sprint(ekdosis.numrange(
      \luastring{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}},
      \luastring{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}))}%
        \unexpanded\expandafter{\ekd@postrefnum}% issue the nos
    \else
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}--%
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}%
        \unexpanded\expandafter{\ekd@postrefnum}% issue the nos
    \fi
    \fi%
    \else
    \ifboolexpr{bool {ekd@pagelineation} or bool {ekd@pagevlineation}}
      {\LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}--%
      \LRnum{\getpagerefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}.%
      \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}%
        \unexpanded\expandafter{\ekd@postrefnum}}% issue pg and ln nos
      {\LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}--%
      \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-e}}%
        \unexpanded\expandafter{\ekd@postrefnum}}% issue the nos
    \fi%
    \ifekdl@forcenum
    \LRnum{\getrefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}}%
      \unexpanded\expandafter{\ekd@postrefnum}% force the no
    \fi
  }%
}
\newif\ifekdl@forcenum
\newif\ifekdl@nonum
\ekvdefinekeys{lem}{
  code wit = \def\ekdlr@wit{#1},
  code source = \def\ekdlr@source{#1},
  code resp = \def\ekdlr@resp{#1},
  code alt  = \def\ekdlr@alt{#1},
  code pre = \def\ekdlr@pre{#1},
  code post = \def\ekdlr@post{#1},
  code prewit = \def\ekdlr@prewit{#1},
  code postwit = \def\ekdlr@postwit{#1},
  store type = \ekdlr@type,
  store sep = \ekdl@sep,
  noval nonum = \ekdl@nonumtrue,
  noval num = \ekdl@forcenumtrue,
  bool nolem = \ifekdl@nolem,
  bool nosep = \ifekdl@nosep,
  initial sep = \ekdsep
}
\ekvdefinekeys{rdg}{
  code wit = \def\ekdlr@wit{#1},
  code source = \def\ekdlr@source{#1},
  code resp = \def\ekdlr@resp{#1},
  code alt  = \def\ekdlr@alt{#1},
  code pre = \def\ekdlr@pre{#1},
  code post = \def\ekdlr@post{#1},
  code prewit = \def\ekdlr@prewit{#1},
  code postwit = \def\ekdlr@postwit{#1},
  store subsep = \ekdr@subsep,
  initial subsep = \ekdsubsep,
  bool nosubsep = \ifekdr@nosubsep,
  store type = \ekdlr@type,
  bool nordg = \ifekdr@nordg
}
\NewDocumentCommand{\rdgGrp}{O{} > {\TrimSpaces } m}{%
  \luadirect{tex.sprint(ekdosis.removesp(\luastringN{#2}))}%
}
\def\app@lang{%
  \ifekd@mapps
  \luadirect{tex.sprint(ekdosis.getapplang(\luastring{\ekdan@type}))}%
  \else
  \ekd@singleapp@lang
  \fi
}
\def\app@note@lang{%
  \ifekd@mapps
  \luadirect{tex.sprint(ekdosis.getappnotelang(\luastring{\ekdan@type}))}%
  \else
  \ekd@singleapp@note@lang
  \fi
}
\NewDocumentCommand{\lem}{O{} m}{%
  \ekd@isinlemtrue%
  \luadirect{ekdosis.dolnlab(\luastringN{#2})}%
  \null
  \bgroup%
  \ekdl@forcenumfalse
  \ekdl@nonumfalse
  \ekvset{lem}{#1}%
  \ekd@test@lang
  \ifekd@mapps%
    \ifnum%
    \luadirect{tex.sprint(ekdosis.get_bagunits(\luastringO{\ekdan@type}))}
    = 1
    \boolfalse{subsq@unit@\ekdan@type}%
    \fi%
    \luadirect{ekdosis.increment_bagunits(\luastringO{\ekdan@type})}%
    \def\ekd@munit@delim{%
      \luadirect{tex.sprint(ekdosis.getappdelim(\luastringO{\ekdan@type}))}}%
    \luadirect{tex.sprint(ekdosis.limit_bagunits(\luastringO{\ekdan@type}))}%
  \fi%
  \ifekdl@nolem\edef\lem@app{%
    % \hskip .75em
    \ifekd@mapps
    \ifbool{subsq@unit@\ekdan@type}%
    {\ekd@munit@delim}{}%
    \else%
    \ifsubsq@unit\unexpanded\expandafter{\ekd@unit@delim}\fi%
    \fi%
    \unless\ifekdl@nonum\current@ref\fi}%\hskip .25em}%
  \else%
  \ifbool{al@rlmode}{%
    \edef\lem@app{%
      % \hskip .75em
      \ifekd@mapps
      \ifbool{subsq@unit@\ekdan@type}%
      {\ekd@munit@delim}{}%
      \else%
      \ifsubsq@unit\unexpanded\expandafter{\ekd@unit@delim}\fi%
      \fi%
      \unless\ifekdl@nonum\current@ref\fi%\hskip .25em
      \ifdefined\ekdlr@alt%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
          {\textdir TRT\unexpanded\expandafter{\ekd@lemmastyle}%
            \unexpanded\expandafter{\ekdlr@alt}}%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
      \else
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
          {\textdir TRT\unexpanded\expandafter{\ekd@lemmastyle}%
            \unexpanded{#2}}%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
      \fi
      \ifdefined\ekdlr@postwit%
        \space\unexpanded\expandafter{\ekdlr@postwit}\else\fi
      \ifdefined\ekdlr@resp\space\getsiglum{\ekdlr@resp}\else\fi
      \ifdefined\ekdlr@source\space\getsiglum{\ekdlr@source}\else\fi
      \ifdefined\ekdlr@wit\space\getsiglum{\ekdlr@wit}\else\fi
      \ifdefined\ekdlr@prewit%
        \space\unexpanded\expandafter{\ekdlr@prewit}\space\else\fi
      \ifekdl@nosep\else\unexpanded\expandafter{\ekdl@sep}\fi
    }%
  }%
  {%
    \edef\lem@app{%
      % \hskip .75em
      \ifekd@mapps
      \ifbool{subsq@unit@\ekdan@type}%
      {\ekd@munit@delim}{}%
      \else%
      \ifsubsq@unit\unexpanded\expandafter{\ekd@unit@delim}\fi%
      \fi%
      \unless\ifekdl@nonum\current@ref\fi%\hskip .25em
      \ifdefined\ekdlr@alt%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
        \ifbool{ekd@lang@pkg}%
        {{\unexpanded\expandafter{\ekd@lemmastyle}%
            \noexpand\selectlanguage{\app@lang}%
            \unexpanded\expandafter{\ekdlr@alt}}}%
        {\unexpanded\expandafter{\ekd@lemmastyle}%
          \unexpanded\expandafter{\ekdlr@alt}}%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
      \else
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
          \ifbool{ekd@lang@pkg}%
          {{\unexpanded\expandafter{\ekd@lemmastyle}%
              \noexpand\selectlanguage{\app@lang}%
              \unexpanded{#2}}}{%
            {\unexpanded\expandafter{\ekd@lemmastyle}\unexpanded{#2}}}%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
      \fi
      \ifdefined\ekdlr@prewit%
        \space\unexpanded\expandafter{\ekdlr@prewit}\space\else\fi
      \ifdefined\ekdlr@wit\space\getsiglum{\ekdlr@wit}\else\fi
      \ifdefined\ekdlr@source\space\getsiglum{\ekdlr@source}\else\fi
      \ifdefined\ekdlr@resp\space\getsiglum{\ekdlr@resp}\else\fi
      \ifdefined\ekdlr@postwit%
        \space\unexpanded\expandafter{\ekdlr@postwit}\else\fi
      \ifekdl@nosep\else\unexpanded\expandafter{\ekdl@sep}\fi
    }%
  }%
  \fi%
  \ifekd@mapps%
  \append@ln@app[\ekdan@type]{\lem@app}%
  \else%
  \append@ln@app{\lem@app}%
  \fi%
  \egroup%
  \ekd@isinlemfalse%
  \subsq@unittrue%
}
\newif\ifekd@subsq@rdg
\NewDocumentCommand{\rdg}{O{} m}{%
  \bgroup%
  \ekvset{rdg}{#1}%
  \ekd@test@lang
  % \ifekdr@nordg\append@app{}\else% do we need \append@app{} here? If
  %                               % so, keep in mind \ifekd@mapps,
  %                               like so:
  \ifekdr@nordg%
    \ifekd@mapps%
      \append@app[\ekdan@type]{}%
    \else%
    \append@app{}%
    \fi%
  \else%
  \ifbool{al@rlmode}{%
    \edef\rdg@app{%
      \ifekd@subsq@rdg
        \unless\ifekdr@nosubsep\unexpanded\expandafter{\ekdr@subsep}\fi
      \fi
      \ifdefined\ekdlr@alt%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
          {\textdir TRT\unexpanded\expandafter{\ekd@readingstyle}%
            \unexpanded\expandafter{\ekdlr@alt}}%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
      \else
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
          {\textdir TRT\unexpanded\expandafter{\ekd@readingstyle}%
            \unexpanded{#2}}%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
      \fi
      \ifdefined\ekdlr@postwit%
        \space\unexpanded\expandafter{\ekdlr@postwit}\else\fi
      \ifdefined\ekdlr@resp\space\getsiglum{\ekdlr@resp}\else\fi
      \ifdefined\ekdlr@source\space\getsiglum{\ekdlr@source}\else\fi
      \ifdefined\ekdlr@wit\space\getsiglum{\ekdlr@wit}\else\fi
      \ifdefined\ekdlr@prewit%
        \space\unexpanded\expandafter{\ekdlr@prewit}\space\else\fi
    }%
  }%
  {%
    \edef\rdg@app{%
      \ifekd@subsq@rdg
        \unless\ifekdr@nosubsep\unexpanded\expandafter{\ekdr@subsep}\fi
      \fi
      \ifdefined\ekdlr@alt%
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
        \ifbool{ekd@lang@pkg}%
        {{\unexpanded\expandafter{\ekd@readingstyle}%
            \noexpand\selectlanguage{\app@lang}%
            \unexpanded\expandafter{\ekdlr@alt}}}%
        {\unexpanded\expandafter{\ekd@readingstyle}%
          \unexpanded\expandafter{\ekdlr@alt}}%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
      \else
        \ifdefined\ekdlr@pre%
          \space\unexpanded\expandafter{\ekdlr@pre}\space\else\fi
          \ifbool{ekd@lang@pkg}%
          {{\unexpanded\expandafter{\ekd@readingstyle}%
              \noexpand\selectlanguage{\app@lang}\unexpanded{#2}}}{%
            {\unexpanded\expandafter{\ekd@readingstyle}\unexpanded{#2}}}%
        \ifdefined\ekdlr@post%
          \space\unexpanded\expandafter{\ekdlr@post}\space\else\fi
      \fi
      \ifdefined\ekdlr@prewit%
        \space\unexpanded\expandafter{\ekdlr@prewit}\space\else\fi
      \ifdefined\ekdlr@wit\space\getsiglum{\ekdlr@wit}\else\fi
      \ifdefined\ekdlr@source\space\getsiglum{\ekdlr@source}\else\fi
      \ifdefined\ekdlr@resp\space\getsiglum{\ekdlr@resp}\else\fi
      \ifdefined\ekdlr@postwit%
        \space\unexpanded\expandafter{\ekdlr@postwit}\else\fi
    }%
  }%
  \ifekd@mapps
      \append@app[\ekdan@type]{\rdg@app}%
  \else
  \append@app{\rdg@app}%
  \fi
  \fi
  \egroup
  \ekd@subsq@rdgtrue
}
\newif\ifekdn@forcenum
\ekvdefinekeys{note}{
  store type = \ekdan@type,
  store lem = \ekdn@lem,
  code labelb = \def\ekdn@labelb{#1},
  code labele = \def\ekdn@labele{#1},
  store sep = \ekdn@sep,
  bool nosep = \ifekdn@nosep,
  initial type = default,
  initial sep = \ekdsep,
  bool nonum = \ifekdn@nonum,
  noval num = \ekdn@forcenumtrue
}
\NewDocumentCommand{\note@noapp}{O{} +m}{%
  \leavevmode
  \bgroup%
  \ekvset{note}{#1}%
  \ekd@test@lang
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{ekdosis.storeabspg(
    \luastring{\zref@extract{ekd:\theekd@lab}{abspage}})}%
  \ifekd@state\add@apparatus\fi%
  \ifekd@mapps%
    \ifnum%
      \luadirect{tex.sprint(ekdosis.get_bagunits(\luastringO{\ekdan@type}))}
      = 1
      \boolfalse{subsq@unit@\ekdan@type}%
    \fi%
  \luadirect{ekdosis.increment_bagunits(\luastringO{\ekdan@type})}%
    \def\ekd@munit@delim{%
      \luadirect{tex.sprint(ekdosis.getappdelim(\luastringO{\ekdan@type}))}}%
    \luadirect{tex.sprint(ekdosis.limit_bagunits(\luastringO{\ekdan@type}))}%
  \fi%
  \ifdefined\ekdn@labelb%
    \luadirect{tex.sprint(ekdosis.setnotelab(\luastringO{\ekdn@labelb}))}%
    \ifdefined\ekdn@labele\else\def\ekdn@labele{\ekdn@labelb}\fi%
  \else\PackageError{ekdosis}{missing labelb}{`labelb' must be
    set.}\fi%
  \ifbool{al@rlmode}%
  {\edef\note@contents{%
      % \hskip .75em
      \ifekd@mapps
        \ifbool{subsq@unit@\ekdan@type}%
        {\ekd@munit@delim}{}%
      \else%
        \ifsubsq@unit\unexpanded\expandafter{\ekd@unit@delim}\fi%
      \fi%
      \unless\ifekdn@nonum\current@ref@arg{\ekdn@labelb}{\ekdn@labele}\fi%\hskip .25em
      \ifdefined\ekdn@lem%
      {\textdir TRT\unexpanded\expandafter{\ekd@lemmastyle}%
        \unexpanded\expandafter{\ekdn@lem}}%
          \unless\ifekdn@nosep
          \unexpanded\expandafter{\ekdn@sep}\fi
          \else\fi%
            {\textdir TRT\unexpanded{#2}}}}%
  {\edef\note@contents{%
      % \hskip .75em
      \ifekd@mapps
        \ifbool{subsq@unit@\ekdan@type}%
        {\ekd@munit@delim}{}%
      \else%
        \ifsubsq@unit\unexpanded\expandafter{\ekd@unit@delim}\fi%
      \fi%
      \unless\ifekdn@nonum\current@ref@arg{\ekdn@labelb}{\ekdn@labele}\fi%\hskip .25em
      \ifdefined\ekdn@lem
        \ifbool{ekd@lang@pkg}%
        {{\unexpanded\expandafter{\ekd@lemmastyle}%
            \noexpand\selectlanguage{\app@lang}%
              \unexpanded\expandafter{\ekdn@lem}}}%
          {\unexpanded\expandafter{\ekd@lemmastyle}%
            \unexpanded\expandafter{\ekdn@lem}}%
          \unless\ifekdn@nosep
          \unexpanded\expandafter{\ekdn@sep}\fi
          \else\fi%
          \ifbool{ekd@lang@pkg}%
          {{\noexpand\selectlanguage{\app@note@lang}\unexpanded{#2}}}{%
            {\unexpanded{#2}}}}}%
  \ifekd@mapps%
  \unconditional@appin[\ekdan@type]{\note@contents}%
  \else%
  \unconditional@appin{\note@contents}%
  \fi%
    \luadirect{ekdosis.setprevnotelab(\luastringO{\ekdn@labelb})}%
  \egroup
  \subsq@unittrue
  \ignorespaces
}
\ekvdefinekeys{ekd@note}{
  store pre = \pre@value,
  store post = \post@value,
  nmeta sep = {post=\ekdsep},
  nmeta subsep = {pre=\ekdsubsep}
}
\NewDocumentCommand{\ekd@note}{O{} m}{%
  \bgroup%
  \ekvset{ekd@note}{#1}%
  \edef\note@contents{%
    \ekvifdefinedNoVal{ekd@note}{pre}{}{%
      \unexpanded\expandafter{\pre@value}}%
    {\unexpanded{#2}}%
    \ekvifdefinedNoVal{ekd@note}{post}{}{%
      \unexpanded\expandafter{\post@value}}%
  }%
  \ifekd@mapps%
  \append@app[\ekdan@type]{\note@contents}%
  \else%
  \append@app{\note@contents}%
  \fi%
  \egroup%
}
\NewDocumentCommand{\ekd@note@star}{O{} m}{%
  \bgroup
  \ekvset{ekd@note}{#1}%
  \edef\note@contents{%
    \ekvifdefinedNoVal{ekd@note}{pre}{}{%
      \unexpanded\expandafter{\pre@value}}%
    \if@pkg@parnotes
      \unskip\noexpand\parnote{\unexpanded{#2}}%
    \else
      \unskip\noexpand\footnote{\unexpanded{#2}}%
    \fi
    \ekvifdefinedNoVal{ekd@note}{post}{}{%
      \unexpanded\expandafter{\post@value}}%
  }%
  \ifekd@mapps
    \append@app[\ekdan@type]{\note@contents}%
  \else
    \append@app{\note@contents}%
  \fi
  \egroup
}
\NewDocumentCommand{\note@app}{s O{} +m}{%
  \ifbool{al@rlmode}{%
    \IfBooleanTF{#1}{\ekd@note@star[#2]{%
          {\textdir TRT#3}}}
    {\ekd@note[#2]{{\textdir TRT#3}}}%
  }{%
    \IfBooleanTF{#1}{\ekd@note@star[#2]{#3}}
    {\ekd@note[#2]{#3}}%
  }%
}
\NewDocumentCommand{\note}{s O{} +m}{%
  \ifekd@state%
    \ifekd@isinapp%
      \ifekd@isinlem%
        \note@noapp[#2]{#3}%
      \else%
        \IfBooleanTF{#1}{\note@app*[#2]{#3}}{\note@app[#2]{#3}}%
      \fi%
    \else%
      \note@noapp[#2]{#3}%
      \fi%
  \fi%
}
\ekvdefinekeys{ekd@corr}{
  store suppbegin = \suppb@value,
  store suppend = \suppe@value,
  store delbegin = \delb@value,
  store delend = \dele@value,
  store sicbegin = \sicb@value,
  store sicend = \sice@value,
  store gapmark = \gapm@value,
  initial suppbegin = \ifbool{al@rlmode}{>}{<},
  initial suppend = \ifbool{al@rlmode}{<}{>},
  initial delbegin = \ifbool{al@rlmode}{\}}{\{},
  initial delend = \ifbool{al@rlmode}{\{}{\}},
  initial sicbegin = \dag,
  initial sicend = \dag,
  initial gapmark = ***,
  bool keepinapp = \ifekd@keepinapp
}
\NewDocumentCommand{\supplied}{m}{%
  \ifekd@inside@app
    \ifekd@keepinapp
      \suppb@value #1\suppe@value
    \else
      #1%
    \fi
  \else
    \suppb@value #1\suppe@value
  \fi
}
\NewDocumentCommand{\surplus}{m}{%
  \ifekd@inside@app
    \ifekd@keepinapp
      \delb@value #1\dele@value
    \else
      #1%
    \fi
  \else
    \delb@value #1\dele@value
  \fi
}
\NewDocumentCommand{\sic}{s m}{%
  \ifekd@inside@app
    \ifekd@keepinapp
      \IfBooleanTF{#1}
        {\sicb@value #2}
        {\sicb@value #2\sice@value}%
    \else
      #2%
    \fi
  \else
    \IfBooleanTF{#1}
      {\sicb@value #2}
      {\sicb@value #2\sice@value}%
  \fi
}
\NewDocumentCommand{\gap}{m}{%
  \gapm@value
}
\NewDocumentCommand{\SetCritSymbols}{m}{
  \ekvset{ekd@corr}{#1}
}
\NewDocumentCommand{\apparatus}{}{%
  \luadirect{tex.sprint(ekdosis.appout())}}
\NewDocumentCommand{\test@apparatus}{}{%
  \luadirect{tex.sprint(ekdosis.testapparatus())}}
\NewDocumentCommand{\ekd@storecol}{}{%
  \luadirect{ekdosis.storecurcol(\luastring{\thecolumn})}%
}
\NewDocumentCommand{\EkdosisOn}{}{%
  \ekd@statetrue}
\NewDocumentCommand{\EkdosisOff}{}{%
  \ekd@statefalse%
}
\def\ekd@setlineno{%
  \let\setpagewiselinenumbers\relax%
  \let\pagewiselinenumbers\relax%
  \let\endpagewiselinenumbers\relax%
  \let\runningpagewiselinenumbers\relax%
  \let\realpagewiselinenumbers\relax%
}
\NewDocumentEnvironment{ekdosis}{+b}{%
  \ekd@setlineno%
  \runninglinenumbers
    \EkdosisOn#1}{%
    \EkdosisOff
  \endrunninglinenumbers%
  \iftei@export
  \luadirect{ekdosis.exporttei(\luastringN{\par #1\par })}\fi}
\ekvdefinekeys{ekd@align}{
  store tcols = \tcols@num,
  store lcols = \lcols@num,
  store texts = \texts@value,
  store apparatus = \apparatus@value,
  bool paired = \ifekd@paired,
  choice lineation = {page = \ekd@pagelineationtrue,
                      document = \ekd@pagelineationfalse},
  unknown-choice lineation = \PackageError{ekdosis}{unknown
    lineation=#1}{`lineation' must be either `page' or `document'.},
  choice segmentation = {auto = \def\segmentation@val{auto},
                         noauto = \def\segmentation@val{noauto}},
  unknown-choice segmentation = \PackageError{ekdosis}{unknown
    segmentation=#1}{`segmentation' must be either `auto' or
    `noauto'.},
  bool flush = \ifekd@flushapp,
  initial tcols = 2,
  initial lcols = 1,
  initial texts = edition;translation,
  initial apparatus = edition,
  default segmentation = auto
}
\NewDocumentCommand{\SetAlignment}{m}{
  \ekvset{ekd@align}{#1}
}
\patchcmd{\pcol@nextpage}{%
  \endgroup}{%
  \ifekd@pagelineation\resetlinenumber\fi
  \endgroup}{}{}
\NewDocumentCommand{\EkdosisColStart}{}{%
  \ekd@setlineno%
  \runninglinenumbers
  \ekd@storecol%
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{%
    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
    "pg_i")}%
  \ifekd@pagelineation
    \luadirect{tex.sprint(ekdosis.checkresetlineno())}
  \fi
}
\NewDocumentCommand{\EkdosisColStop}{}{%
  \stepcounter{ekd@lab}%
  \zlabel{ekd:\theekd@lab}%
  \luadirect{%
    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
    "pg_ii")}%
  \endrunninglinenumbers%
}
\NewDocumentEnvironment{alignment}{O{}}
{%
  \ekvset{ekd@align}{#1}%
  \luadirect{ekdosis.mkenvdata(
    \luastring{\texts@value},
    "texts"
    )}
  \ifekd@flushapp
    \luadirect{ekdosis.newalignment("set")}
  \fi
  \luadirect{ekdosis.mkenvdata(
    \luastring{\apparatus@value}, "apparatus"
    )}
  \setrunninglinenumbers
  \luadirect{tex.sprint(ekdosis.mkenv())}
  \ifekd@paired
  \begin{paracol}[\lcols@num]{\tcols@num}
  \else
  \begin{paracol}[\lcols@num]*{\tcols@num}
  \fi
  }
  {\end{paracol}
  \iftei@export\luadirect{ekdosis.export_coldata_totei()}\fi
  \ifekd@flushapp
    \luadirect{ekdosis.newalignment("reset")}
  \fi
  \luadirect{ekdosis.flushenvdata()}
  \luadirect{ekdosis.flushcolnums()}
  }
\NewDocumentCommand{\ekd@storemark}{m}{%
  \stepcounter{ekd@lab}%
  \label{ekd:\theekd@lab}%
  \luadirect{ekdosis.storehfmark(
    \luastring{\getpagerefnumber{ekd:\theekd@lab}},
    \luastringN{#1})}%
}
\NewDocumentCommand{\endmark}{}{%
  \stepcounter{ekd@lab}%
  \label{ekd:\theekd@lab}%
  \luadirect{ekdosis.storehfmark(
    \luastring{\getpagerefnumber{ekd:\theekd@lab}},
    "", "endmk")}%
  \ifdefined\xspace\xspace\fi
}
\NewDocumentCommand{\ekdmark}{}{%
  \luadirect{tex.sprint(ekdosis.gethfmark(\luastring{\thepage}))}%
}
\ekvdefinekeys{ekd@marks}{
  choice mark = {HEL = \def\ekd@mk{HEL},
    HEC = \def\ekd@mk{HEC},
    HER = \def\ekd@mk{HER},
    HOL = \def\ekd@mk{HOL},
    HOC = \def\ekd@mk{HOC},
    HOR = \def\ekd@mk{HOR},
    FEL = \def\ekd@mk{FEL},
    FEC = \def\ekd@mk{FEC},
    FEL = \def\ekd@mk{FER},
    FOL = \def\ekd@mk{FOL},
    FOC = \def\ekd@mk{FOC},
    FOL = \def\ekd@mk{FOR}},
  unknown-choice mark = \PackageError{ekdosis}{unknown mark=#1}{`mark'
    must be either `HEL', `HEC', `HER', `HOL', `HOC', `HOR', `FEL',
    \MessageBreak `FEC', `FER', `FOL', `FOC' or `FOR'.}
}
\NewDocumentCommand{\ekdprintmark}{m m}{%
  \bgroup
  \ekvset{ekd@marks}{mark = #1}%
  \luadirect{tex.sprint(ekdosis.printmark(\luastringN{#2},
    \luastringO{\ekd@mk}))}%
  \egroup
}
\NewDocumentCommand{\ekdnohfmark}{}{%
  \luadirect{ekdosis.nohfmark()}%
}
\NewDocumentCommand{\ekdresethfmarks}{}{%
  \luadirect{ekdosis.resethfmark()}%
}
\NewDocumentCommand{\MkBodyDivs}{mmmmmm}{
  \luadirect{ekdosis.mkdivdepths(
    \luastringN{#1},
    \luastringN{#2},
    \luastringN{#3},
    \luastringN{#4},
    \luastringN{#5},
    \luastringN{#6}
    )
  }
}
\ekvdefinekeys{ekd@div}{
  code type = \def\type@value{#1},
  code n = \def\n@value{#1},
  code head = \def\head@value{#1},
  code barehead = \def\barehead@value{#1},
  store depth = \depth@value,
  code mark = \ekd@storemark{#1},
  choice toc = {book = \def\toc@value{book},
                part = \def\toc@value{part},
                chapter = \def\toc@value{chapter},
                section = \def\toc@value{section},
                subsection = \def\toc@value{subsection},
                subsubsection = \def\toc@value{subsubsection},
                paragraph = \def\toc@value{paragraph},
                subparagraph = \def\toc@value{subparagraph}},
  unknown-choice toc = \PackageError{ekdosis}{unknown toc=#1}{`toc'
    must be either `book', `part', `chapter', `section', `subsection',
    \MessageBreak `subsubsection', `paragraph' or `subparagraph'.},
  initial depth = 1
}
\NewDocumentCommand{\FormatDiv}{m m m}{
  \luadirect{ekdosis.fmtdiv(\luastring{#1},
    \luastringN{#2},
    \luastringN{#3})}
}
\NewDocumentCommand{\ekd@getfmtdiv}{m m}{%
  \luadirect{tex.sprint(ekdosis.getfmtdiv(\luastringO{#1},
    \luastringN{#2}))}%
}
\NewDocumentCommand{\ekddiv}{m}{
  \begingroup
  \ekvset{ekd@div}{#1}%
  \ifdefined\head@value
    \bgroup
      \ekd@getfmtdiv{\depth@value}{b}%
      \head@value
      \ekd@getfmtdiv{\depth@value}{e}%
    \egroup
      \ifdefined\toc@value
        \ltx@ifpackageloaded{hyperref}{\phantomsection}{}%
          \ifdefined\barehead@value
            \addcontentsline{toc}{\toc@value}{\barehead@value}%
          \else
            \addcontentsline{toc}{\toc@value}{\head@value}%
          \fi
      \fi
  \fi
  \endgroup
}
\newif\ifekd@test@vpnum
\newcounter{ekd@vpnum}
\globalcounter{ekd@vpnum}
\NewDocumentCommand{\test@vpnum}{}{%
  \ifekd@test@vpnum
    \edef\@tempa{\theekd@vpnum}%
    \stepcounter{ekd@vpnum}%
    \label{vpnum:\theekd@vpnum}%
    \ifnum
      \pdf@strcmp{\getpagerefnumber{vpnum:\@tempa}}%
        {\getpagerefnumber{vpnum:\theekd@vpnum}}
        = 0
      \else
        \resetvlinenumber
    \fi
  \else
  \label{vpnum:\theekd@vpnum}%
  \global\ekd@test@vpnumtrue
  \fi
}
\ifboolexpr{bool {@pkg@poetry@verse} or bool {ekd@memoir@loaded}}
 {\newcommand{\@vsifplus}[1]{\@ifnextchar +{\@firstoftwo{#1}}}
   \renewcommand{\@vscentercr}{%
     \ifhmode \unskip\else \@nolnerr\fi
     \@vsifgt{\ifnum@brokenline\@vstypelinenum\fi\verselinebreak}{%
       \@vsifplus{\ifnum@brokenline\@vstypelinenum\fi\stepcounter{vslineno}%
         \par\@ifstar{\nobreak\@vsxcentercr}{%
           \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}%
         }%
       }{%
         \@vstypelinenum
         \incr@vsline%
         \par\@ifstar{\nobreak\@vsxcentercr}{%
           \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}%
         }%
       }%
     }%
   }
 }{}
\if@pkg@poetry@verse
\patchcmd{\start@vsline}{%
  \ifaltindent}{%
  \ifekd@pagevlineation\test@vpnum\fi
  \ifaltindent}{}{}
\ekvdefinekeys{ekd@verse}{
  dimen width = \vwidth@val,
  initial width = \linewidth,
  code type = \def\type@value{#1},
}
\ifekd@memoir@loaded
  \def\vlvnumfont{\normalfont\footnotesize}
  \def\verselinenumfont#1{\def\vlvnumfont{#1}}
\else
  \verselinenumfont{\normalfont\footnotesize}
\fi
\setcounter{poemline}{1}
\NewDocumentEnvironment{ekdverse}{!O{}}{%
  \ekvset{ekd@verse}{#1}%
  \if@continuous@vnum\setverselinenums{\thelinenumber}{0}\fi
  \nolinenumbers
  \let\linelabel\label
  \ifekd@memoir@loaded
    \refstepcounter{verse}%
  \else
    \stepcounter{verse@envctr}%
  \fi
  \addtocounter{poemline}{-1}\refstepcounter{poemline}%
  \setcounter{vslineno}{1}%
  \let\\=\@vscentercr
  \list{}{\itemsep \z@
          \itemindent  -\vindent%
          \listparindent\itemindent
          \parsep       \stanzaskip
          \setlength{\itemsep}{0pt}%
          \setlength{\topsep}{0pt}%
          \setlength{\partopsep}{0pt}%
          \ifdim\vwidth@val < \linewidth
            \rightmargin        \z@
            \setlength{\leftmargin}{\linewidth}%
            \addtolength{\leftmargin}{-\vwidth@val}%
            \addtolength{\leftmargin}{-0.5\leftmargin}%
          \else
            \rightmargin        \leftmargin
          \fi
          \addtolength{\leftmargin}{\vindent}}%
  \item[]\ifekd@pagevlineation\test@vpnum\fi%
}
{\endlist
  \if@continuous@vnum\resetlinenumber[\thepoemline]\fi}
\else
\newlength{\ekdverseindentlength}
\setlength{\ekdverseindentlength}{\parindent}
\NewDocumentEnvironment{ekdverse}{!O{\ekdverseindentlength}}{
  \begin{list}{}{%
      \setlength{\leftmargin}{#1}
      \setlength{\itemsep}{0pt}
      \setlength{\topsep}{0pt}
      \setlength{\partopsep}{0pt}
    }
  \item[]
  }{\end{list}}
\fi
\NewDocumentCommand{\resetvlinenumber}{O{1}}{%
  \if@pkg@poetry@verse
  \setverselinenums{#1}{0}%
  \fi
}
\ekvdefinekeys{ekd@stanza}{
  code type = \def\type@value{#1}
}
\NewDocumentEnvironment{ekdstanza}{!O{}}{%
  \leavevmode\unskip
  \ekvset{ekd@stanza}{#1}%
  \ignorespaces
}{}
\NewDocumentEnvironment{ekdpar}{}{\par}{\par}
\IfFileExists{\jobname-ekd.cfg}{\input{\jobname-ekd.cfg}}{}
\endinput
%%
%% End of file `ekdosis.sty'.
