From fdc6ef167612afc2dbb16576baae9e8acee7c861 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Tue, 24 Sep 2019 18:39:49 +0200
Subject: adjust line numbers inside layers independently

---
 ekdosis.dtx | 59 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 46 insertions(+), 13 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index c81489d..aa47a63 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -482,10 +482,10 @@ idno={}, msName={}, origDate={}][3]{%
     \if@parnotesroman%
     \renewcommand*{\theparnotemark}{\roman{parnotemark}}\else\fi%
     \parnoteclear\else\fi%
-    \footnotesize\apparatus\ekd@end@apparatus%
+    \footnotesize\apparatus\unless\ifekd@mapps\ekd@end@apparatus\fi%
     \if@pkg@parnotes\parnotes\parnotereset\else\fi%
   }%
-  % \unconditional@appin{\rule{5cm}{0.5pt}\newline}
+  \unless\ifekd@mapps\unconditional@appin{\rule{5cm}{0.5pt}\newline}\fi
 }
 \def\add@apparatus{%
   \test@apparatus%
@@ -564,6 +564,14 @@ idno={}, msName={}, origDate={}][3]{%
   \luadirect{ekdosis.dolnlab(\luastringN{#2})}%
   \bgroup%
   \setkeys[ekd]{lemrdg,lem}{#1}%
+  \ifekd@mapps%
+    \ifnum%
+      \luadirect{tex.sprint(ekdosis.get_bagunits(\luastringO{\ekdan@type}))}
+      = 1
+      \subsq@unitfalse%
+    \fi%
+  \luadirect{ekdosis.increment_bagunits(\luastringO{\ekdan@type})}%
+  \fi%
   \ifekdl@nolem\edef\lem@app{%
     \hskip .75em\ifsubsq@unit%
     \unexpanded\expandafter{\ekd@unit@delim}\else\fi%
@@ -628,7 +636,7 @@ idno={}, msName={}, origDate={}][3]{%
   }%
   \fi%
   \ifekd@mapps%
-      \append@ln@app[\ekdan@type]{\lem@app}%
+  \append@ln@app[\ekdan@type]{\lem@app}%
   \else%
   \append@ln@app{\lem@app}%
   \fi%
@@ -1434,7 +1442,8 @@ end
 -- handle multiple layers in apparatuses
 --
 local apparatuses = {}
-local bagtype = {}
+local bagunits = {}
+
 function ekdosis.newapparatus(teitype)
    if isintable(apparatuses, teitype)
    then
@@ -1444,10 +1453,25 @@ function ekdosis.newapparatus(teitype)
    else
       table.insert(apparatuses, { a = teitype })
    end
-   bagtype[type] = 1
+   bagunits[teitype] = 1
    return true
 end
 
+function ekdosis.get_bagunits(teitype)
+   return bagunits[teitype]
+end
+
+function ekdosis.increment_bagunits(teitype)
+   bagunits[teitype] = (bagunits[teitype] or 0) + 1
+end
+
+local function reset_bagunits()
+   for i = 1,#apparatuses
+   do
+      bagunits[apparatuses[i].a] = 1
+   end
+end
+
 function ekdosis.appin(str, teitype)
    local f = io.open(tex.jobname.."_tmp.ekd", "a+")
    if next(apparatuses) == nil
@@ -1475,7 +1499,6 @@ function ekdosis.appout()
       local output = {}
       if next(apparatuses) == nil then
 	 -- table.insert(output, "BEGIN")
-	 table.insert(output, "\\par")
 	 for i in string.gmatch(t,
 				"<"..cur_abs_pg.."%-0>.-</"..cur_abs_pg.."%-0>")
 	 do
@@ -1486,14 +1509,18 @@ function ekdosis.appout()
 	 local n = 1
 	 while apparatuses[n]
 	 do
-	    -- table.insert(output, "BEGIN")
-	    table.insert(output, "\\par")
-	    for i in string.gmatch(t,
-				   "<"..cur_abs_pg.."%-"..n..">.-</"..cur_abs_pg.."%-"..n..">")
-	    do
-	       table.insert(output, i)
+	    if string.match(t, "<"..cur_abs_pg.."%-"..n..">.-</"..cur_abs_pg.."%-"..n..">")
+	    then
+	       -- table.insert(output, "BEGIN")
+	       table.insert(output, "\\par\\rule{5cm}{0.5pt}\\newline")
+	       for i in string.gmatch(t,
+				      "<"..cur_abs_pg.."%-"..n..">.-</"..cur_abs_pg.."%-"..n..">")
+	       do
+		  table.insert(output, i)
+	       end
+	       table.insert(output, "\\csname ekd@end@apparatus\\endcsname")
+	       -- table.insert(output, "END")
 	    end
-	    -- table.insert(output, "END")
 	    n = n + 1
 	 end
       end
@@ -1520,11 +1547,17 @@ function ekdosis.testapparatus()
    if tonumber(pg_ii) < tonumber(pg_i)
    or tonumber(pg_ii) < tonumber(cur_abs_pg) then
       pg_i = cur_abs_pg
+      if next(apparatuses) ~= nil then
+	 reset_bagunits()
+      end
       return "\\booltrue{do@app}"
    elseif tonumber(pg_ii) == tonumber(pg_i) then
       return "\\boolfalse{do@app}"
    elseif tonumber(pg_ii) > tonumber(pg_i) then
       pg_i = cur_abs_pg
+      if next(apparatuses) ~= nil then
+	 reset_bagunits()
+      end
       return "\\booltrue{do@app}"
    end
 end
-- 
cgit v1.2.3

