From 1666f3f8c518979ec2593fd14331a35efb5a42b7 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Mon, 30 Sep 2019 14:24:01 +0200
Subject: adapted \note so that it be able to work ouside \app with multi-layer
 apparatuses

---
 ekdosis.dtx | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 136 insertions(+), 5 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index aa47a63..7d757b7 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -453,14 +453,20 @@ idno={}, msName={}, origDate={}][3]{%
 \newbool{do@app}
 \newif\ifekd@state
 \newif\ifekd@isinapp
+\newif\ifekd@isinlem
 \providebool{al@rlmode}
 \@ifpackageloaded{arabluatex}{}{%
   \def\setRL{\booltrue{al@rlmode}\pardir TRT\textdir TRT}
   \def\setLR{\boolfalse{al@rlmode}\pardir TLT \textdir TLT}
 }
 \newcounter{ekd@lab}
-\def\unconditional@appin#1{%
-  \luadirect{ekdosis.appin(\luastringN{#1})}%
+ % \def\unconditional@appin#1{%
+ %   \luadirect{ekdosis.appin(\luastringN{#1})}%
+ % }
+\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}
 \newif\ifrtl@app
@@ -519,6 +525,41 @@ idno={}, msName={}, origDate={}][3]{%
     "pg_ii")}%
   \luadirect{tex.sprint(ekdosis.removesp(\luastringN{#2}))}%
   \ekd@isinappfalse}
+\def\current@ref@arg#1#2{{\textdir TLT\noexpand\bfseries%
+    \ifnum%
+    \getpagerefnumber{#1}
+    =
+    \getpagerefnumber{#2}
+      \ifnum%
+      \getrefnumber{#1}
+      =
+      \getrefnumber{#2}
+      %
+      \ifsubsq@unit%
+      %
+      \ifnum%
+      \getrefnumber{#1}
+      =
+      \getrefnumber{\luadirect{tex.sprint(ekdosis.getprevnotelab())}}
+      \else
+      \getrefnumber{#1}%
+      \fi
+      %
+      \else
+      \getrefnumber{#1}%
+      \fi
+      %
+      \else
+      \getrefnumber{#1}--%
+      \getrefnumber{#2}%
+      \fi%
+      \else
+      \getrefnumber{#1}--%
+      \getpagerefnumber{#2}.%
+      \getrefnumber{#2}%
+      \fi%
+    }
+  }%
 \def\current@ref{{\textdir TLT\noexpand\bfseries%
     \ifnum%
     \getpagerefnumber{\luadirect{tex.sprint(ekdosis.getlnlab())}-b}
@@ -561,6 +602,7 @@ idno={}, msName={}, origDate={}][3]{%
 \define@boolkeys[ekd]{lem}[ekdl@]{nolem, nosep}[true]
 \define@boolkeys[ekd]{rdg}[ekdr@]{nordg}[true]
 \NewDocumentCommand{\lem}{O{} m}{%
+  \ekd@isinlemtrue%
   \luadirect{ekdosis.dolnlab(\luastringN{#2})}%
   \bgroup%
   \setkeys[ekd]{lemrdg,lem}{#1}%
@@ -642,12 +684,21 @@ idno={}, msName={}, origDate={}][3]{%
   \fi%
   \egroup%
   \subsq@unittrue%
+  \ekd@isinlemfalse%
 }
 \NewDocumentCommand{\rdg}{O{} m}{%
   \bgroup%
   \setkeys[ekd]{lemrdg,rdg}{#1}%
-  \ifekdr@nordg\append@app{}\else% do we need \append@app{} here? If
-                                % so, keep in mind \ifekd@mapps
+  % \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{%
       \ifdefined\ekdlr@alt
@@ -705,6 +756,55 @@ idno={}, msName={}, origDate={}][3]{%
   \fi%
   \egroup%
 }
+\define@cmdkeys[ekd]{note}[ekdn@]{lem, labelb, labele}
+\define@cmdkey[ekd]{note}[ekdn@]{sep}{}
+\presetkeys[ekd]{note}{sep=\ekdsep}{}
+\NewDocumentCommand{\note@noapp}{O{} +m}{%
+  \presetkeys[ekd]{appnote}{type=default}{}%
+  \bgroup%
+  \setkeys[ekd]{appnote,note}{#1}%
+  \stepcounter{ekd@lab}%
+  \zlabel{ekd:\theekd@lab}%
+  \luadirect{ekdosis.storeabspg(
+    \luastring{\zref@extract{ekd:\theekd@lab}{abspage}})}%
+  \ifekd@state\add@apparatus\fi%
+  \luadirect{%
+    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
+    "pg_ii")}%
+  \ifekd@mapps%
+    \ifnum%
+      \luadirect{tex.sprint(ekdosis.get_bagunits(\luastringO{\ekdan@type}))}
+      = 1
+      \subsq@unitfalse%
+    \fi%
+  \luadirect{ekdosis.increment_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%
+  \edef\note@contents{%
+    \hskip .75em\ifsubsq@unit%
+    \unexpanded\expandafter{\ekd@unit@delim}\else\fi%
+    \current@ref@arg{\ekdn@labelb}{\ekdn@labele}\hskip .25em%
+      \ifdefined\ekdn@lem
+        \ltx@ifpackageloaded{babel}%
+          {{\noexpand\selectlanguage{\languagename}%
+            \unexpanded\expandafter{\ekdn@lem}}}%
+          {\unexpanded\expandafter{\ekdn@lem}}%
+          \unexpanded\expandafter{\ekdn@sep}\else\fi%
+          \ltx@ifpackageloaded{babel}%
+          {{\noexpand\selectlanguage{\languagename}\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%
+}
 \newkeycommand+[\|]{\ekd@note}[pre, post][1]{%
   |\append@app|{\ifcommandkey{pre}{\commandkey{pre}}{}%
     #1%
@@ -722,7 +822,7 @@ idno={}, msName={}, origDate={}][3]{%
     |\unskip\footnote|{#1}}
   |\fi|%
 }
-\NewDocumentCommand{\note}{s O{} +m}{%
+\NewDocumentCommand{\note@app}{s O{} +m}{%
   \ifbool{al@rlmode}{%
     \IfBooleanTF{#1}{\ekd@note@star[#2]{\unexpanded{%
           {\textdir TRT#3}}}}
@@ -732,6 +832,17 @@ idno={}, msName={}, origDate={}][3]{%
     {\ekd@note[#2]{\unexpanded{#3}}}%
   }%
 }
+\NewDocumentCommand{\note}{s O{} +m}{%
+  \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%
+}
 \NewDocumentCommand{\apparatus}{}{%
   \luadirect{tex.sprint(ekdosis.appout())}}
 \NewDocumentCommand{\test@apparatus}{}{%
@@ -1566,6 +1677,8 @@ local lnlabs = {}
 local lnlab_salt = 0
 local current_lnlab = nil
 local prev_lnlab = nil
+local current_notelab = nil
+local prev_notelab = nil
 local current_lemma = nil
 local salt = 0
 
@@ -1599,6 +1712,24 @@ function ekdosis.getprevlnlab()
    return prev_lnlab
 end
 
+function ekdosis.setnotelab(str)
+   current_notelab = str
+   return "\\linelabel{" .. current_notelab .. "}"
+end
+
+function ekdosis.getnotelab()
+   return current_notelab
+end
+
+function ekdosis.setprevnotelab(str)
+   prev_notelab = str
+   return true
+end
+
+function ekdosis.getprevnotelab()
+   return prev_notelab
+end
+
 function ekdosis.mdvappend(str, teitype)
    if teitype == nil
    then
-- 
cgit v1.2.3

