From bb6c3f74b7794ee3213e370cbf35dc8a1a7c3849 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Wed, 7 Nov 2018 22:10:25 +0100
Subject: get rid of afterpage (reimplementation of ekdosis)

---
 ekdosis.dtx | 139 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 103 insertions(+), 36 deletions(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index cc31f61..165a9a6 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -328,8 +328,9 @@ Running "make install" installs the files in the local TeX tree.
 \RequirePackage[addpageno]{lineno}
 \RequirePackage{vplref}
 \RequirePackage{keyfloat}
-\RequirePackage{afterpage}
+% \RequirePackage{afterpage}
 \RequirePackage{refcount}
+\RequirePackage[user,abspage]{zref}  
 \if@pkg@parnotes
   \RequirePackage{parnotes}
 \fi
@@ -343,22 +344,48 @@ Running "make install" installs the files in the local TeX tree.
 %    \end{macrocode}
 %    \begin{macrocode}
 \newbool{isapp}
+\newbool{doapp}
 \newbool{ekdstate}
-\newbool{ekdstarted}
+% \newbool{ekdstarted}
 \providebool{al@rlmode}
 \newcounter{ekd@lab}
+\def\blfootnote{\gdef\@thefnmark{\relax}\@footnotetext}
+\def\add@@apparatus{%
+  % \blfootnote{\apparatus}%
+  \if@pkg@parnotes\parnotes\else\fi%
+  \keyparbox[b!]{}{%
+    \rule{5cm}{0.5pt}\\
+    \if@pkg@parnotes%
+    \if@parnotesroman%
+    \renewcommand*{\theparnotemark}{\roman{parnotemark}}\else\fi%
+    \parnoteclear\else\fi%
+    \footnotesize\apparatus%
+    \if@pkg@parnotes\parnotes\parnotereset\else\fi%
+  }%
+}
+\def\add@apparatus{%
+  \ifbool{isapp}{\testapparatus}{\add@@apparatus%
+    \global\setbool{isapp}{true}}%
+  \ifbool{doapp}{\add@@apparatus}{}%
+}
 \NewDocumentCommand{\append}{+m}{%
   \ifbool{ekdstate}{%
-    \label{ekd:\theekd@lab}%
-    \luadirect{appin(\luastring{<\getpagerefnumber{ekd:\theekd@lab}>},
+    \luadirect{appin(\luastring{<\zref@extract{ekd:\theekd@lab}{abspage}>},
       "no")}%
     \luadirect{appin(\luastringN{#1}, "no")}%
-    \luadirect{appin(\luastring{</\getpagerefnumber{ekd:\theekd@lab}>},
+    \luadirect{appin(\luastring{</\zref@extract{ekd:\theekd@lab}{abspage}>},
       "yes")}%
-    \stepcounter{ekd@lab}}{}}
+  }{}}
 \NewDocumentCommand{\obappend}{+m}%
   {\luadirect{tex.sprint(ekdosis.mdvappend(\luastringN{#1}))}}
-\NewDocumentCommand{\app}{+m}{#1}
+\NewDocumentCommand{\app}{+m}{%
+  \stepcounter{ekd@lab}%
+  \zlabel{ekd:\theekd@lab}%
+  \luadirect{%
+    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
+    "pg_ii")}%
+  \add@apparatus%
+    #1}
 \NewDocumentCommand{\lem}{o +m}{%
   \ifbool{al@rlmode}%
   {\IfNoValueTF{#1}%
@@ -380,34 +407,29 @@ Running "make install" installs the files in the local TeX tree.
 }
 \NewDocumentCommand{\apparatus}{}{\luadirect{tex.sprint(appout())}}
 \NewDocumentCommand{\testapparatus}{}{%
-  \luadirect{tex.sprint(testapparatus())}}
-\def\blfootnote{\gdef\@thefnmark{\relax}\@footnotetext}
-\def\add@@apparatus{%
-  \testapparatus%
-  \ifbool{isapp}{%
-    % \blfootnote{\apparatus}%
-    \if@pkg@parnotes\parnotes\else\fi%
-      \keyparbox[b!]{}{%
-        \rule{5cm}{0.5pt}\\
-        \if@pkg@parnotes%
-        \if@parnotesroman%
-        \renewcommand*{\theparnotemark}{\roman{parnotemark}}\else\fi%
-        \parnoteclear\else\fi%
-        \footnotesize\apparatus%
-        \if@pkg@parnotes\parnotes\parnotereset\else\fi%
-      }%
-}{}}
-\def\add@apparatus{%
-  \add@@apparatus%
-  \afterpage{\add@apparatus}}
+  \luadirect{tex.sprint(ekdosis.testapparatus())}}
+ % \def\add@apparatus{%
+ %   \add@@apparatus%
+ %   \afterpage{\add@apparatus}}
+ % \NewDocumentCommand{\EkdosisStart}{}{%
+ %   \add@@apparatus%
+ %   \afterpage{\add@apparatus}}
 \NewDocumentCommand{\EkdosisStart}{}{%
-  \add@@apparatus%
-  \afterpage{\add@apparatus}}
+  \stepcounter{ekd@lab}%
+  \zlabel{ekd:\theekd@lab}%
+  \luadirect{%
+    ekdosis.storeabspg(\luastring{\zref@extract{ekd:\theekd@lab}{abspage}},
+    "pg_i")}%
+}
 \NewDocumentCommand{\EkdosisOn}{}{\setbool{ekdstate}{true}}
-\NewDocumentCommand{\EkdosisOff}{}{\setbool{ekdstate}{false}}
+\NewDocumentCommand{\EkdosisOff}{}{%
+  \setbool{ekdstate}{false}%
+  \global\setbool{isapp}{false}%
+}
 \newenvironment{ekdosis}{%
-\ifbool{ekdstarted}{}{\EkdosisStart\global\setbool{ekdstarted}{true}}
-\EkdosisOn}{\EkdosisOff}
+  % \ifbool{ekdstarted}{}{\EkdosisStart\global\setbool{ekdstarted}{true}}
+  \EkdosisStart%
+  \EkdosisOn}{\EkdosisOff}
 %    \end{macrocode}
 % \iffalse
 %</package>
@@ -431,6 +453,37 @@ function remove()
    os.remove(tex.jobname..".ekd")
 end
 
+local pg_i = 0
+local pg_ii = 0
+local cur_abs_pg = 0
+
+ekdosis.update_abspg = function(n)
+   cur_abs_pg = n
+   return true
+end
+
+ekdosis.get_abs_page = function()
+   return cur_abs_pg
+end
+
+ekdosis.storeabspg = function(n, pg)
+   if pg == "pg_i" then
+      pg_i = n
+   elseif pg == "pg_ii" then
+      pg_ii = n
+   end
+   cur_abs_pg = n
+   return true
+end
+
+ekdosis.getabspg = function(pg) --not used
+   if pg == "pg_i" then
+      return pg_i
+   elseif pg == "pg_ii" then
+      return pg_ii
+   end
+end
+
 function isintable(table, value)
    for _, v in pairs(table) do
       if v == value then return true end
@@ -456,18 +509,19 @@ function appout()
       t = f:read("*a")
       local output = {}
       for i in string.gmatch(t,
-			     "<"..tex.count[0]..">.-</"..tex.count[0]..">")
+			     "<"..cur_abs_pg..">.-</"..cur_abs_pg..">")
       do
 	 table.insert(output, i)
       end
       f:close()
       str = table.concat(output, "\n")
-      str = string.gsub(str, "</"..tex.count[0]..">", " ")
-      str = string.gsub(str, "<"..tex.count[0]..">", "")
+      str = string.gsub(str, "</"..cur_abs_pg..">", " ")
+      str = string.gsub(str, "<"..cur_abs_pg..">", "")
       return str
    else end
 end
 
+--[[
 function testapparatus()
    local file = io.open(tex.jobname..".ekd", "r")
    if file ~= nil then
@@ -477,6 +531,17 @@ function testapparatus()
    else return "\\boolfalse{isapp}" end
    file:close()
 end
+--]]
+
+ekdosis.testapparatus = function()
+   if pg_ii == pg_i then
+      return "\\boolfalse{doapp}"
+   elseif pg_ii > pg_i then
+      return "\\booltrue{doapp}"
+   else
+      return "\\boolfalse{doapp}"
+   end
+end
 
 md5items = {}
 local salt = 0
@@ -490,7 +555,9 @@ ekdosis.mdvappend = function(str)
       table.insert(md5items, i)
       salt = salt + 1
    end
-   return "\\linelabel{"..i.."}\\append{\\textbf{\\getrefnumber{"..i.."}} "..str.."}"
+   return "\\linelabel{"..i.."}"
+   ..
+   "\\append{\\textbf{\\getrefnumber{"..i.."}} "..str.."}"
 end
 
 --[[ -- of no use
-- 
cgit v1.2.3

