From 7e59309233835a6ed0c556b0dd3fdd6aa9bcfd9a Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Fri, 9 Apr 2021 11:32:35 +0200
Subject: allow two consecutive \note{} commands to share the same labelb

---
 ekdosis.dtx | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

(limited to 'ekdosis.dtx')

diff --git a/ekdosis.dtx b/ekdosis.dtx
index ad01873..f7827de 100644
--- a/ekdosis.dtx
+++ b/ekdosis.dtx
@@ -9598,6 +9598,22 @@ local function note_totei(str)
    return str
 end
 
+local function remove_extra_anchors(str)
+   str = string.gsub(str, "(</note>)(<anchor.->)(<note.->)", function(enote, anchor, bnote)
+   			local id_one = string.gsub(anchor, "(%<anchor )(.-)(/%>)", "%2")
+   			id_one = string.sub(get_attr_value(id_one, "xml:id"), 2, -2)
+   			local id_two = string.match(bnote, "target%=.-right%((.-)%)")
+   			id_two = string.gsub(id_two, ".-right%((.-)%)", "%1")
+   			if id_one == id_two
+   			then
+   			   return string.format("%s%s", enote, bnote)
+   			else
+   			   return string.format("%s%s%s", enote, anchor, bnote)
+   			end
+   end)
+   return str
+end
+
 local function app_totei(str)
    str = gsub(str,
 	      dblbkslash *
@@ -10302,6 +10318,7 @@ local function textotei(str)
    str = xml_entities(str)
    str = texpatttotei(str)
    str = note_totei(str)
+   str = remove_extra_anchors(str)
    str = app_totei(str)
    str = rdgGrp_totei(str)
    str = lem_rdg_totei(str)
@@ -11155,6 +11172,7 @@ local lnlabs = {}
 local lnlab_salt = 0
 local current_lnlab = nil
 local prev_lnlab = nil
+local notelabs = {}
 local current_notelab = nil
 local prev_notelab = nil
 local current_lemma = nil
@@ -11192,7 +11210,13 @@ end
 
 function ekdosis.setnotelab(str)
    current_notelab = str
-   return "\\linelabel{" .. current_notelab .. "}"
+   if isfound(notelabs, current_notelab)
+   then
+      return ""
+   else
+      table.insert(notelabs, current_notelab)
+      return "\\linelabel{" .. current_notelab .. "}"
+   end
 end
 
 function ekdosis.getnotelab()
-- 
cgit v1.2.3

