commit fc736dc134ab9ac8e9a0b6eaed90f4f46c282d12
Author: Juergen Spitzmueller <sp...@lyx.org>
Date:   Fri Aug 22 17:36:18 2025 +0200

    Get rid of hardcoded routines for the subequations module
    
    Use real counter definitions instead. Only a bare minimum
    of hardcoding still required to change subequation to equation
    counter in specific contexts (outside of math insets)
---
 lib/layouts/subequations.module |  9 ++++++
 src/insets/InsetFlex.cpp        | 34 ++++++----------------
 src/insets/InsetLabel.cpp       | 40 ++++----------------------
 src/mathed/InsetMathHull.cpp    | 64 +++++++++++++++++++++++++++++------------
 4 files changed, 67 insertions(+), 80 deletions(-)

diff --git a/lib/layouts/subequations.module b/lib/layouts/subequations.module
index 83d5d541a9..8c56ac29c6 100644
--- a/lib/layouts/subequations.module
+++ b/lib/layouts/subequations.module
@@ -8,9 +8,18 @@
 
 Format 111
 
+Counter subequation
+  GuiName              Sub-Equation
+  Within               equation
+  LabelString          "\arabic{equation}\alph{subequation}"
+  PrettyFormat         "Equation|Equations (##)"
+End
+
 InsetLayout Flex:Subequations
   LyxType               custom
   Decoration            classic
+  Counter               subequation
+  StepParentCounter     true
   LabelString           Subequations
   LatexType             Environment
   LatexName             subequations
diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp
index 2053bb80c4..6181b3228c 100644
--- a/src/insets/InsetFlex.cpp
+++ b/src/insets/InsetFlex.cpp
@@ -158,36 +158,18 @@ void InsetFlex::updateBuffer(ParIterator const & it, 
UpdateType utype, bool cons
                cnts.saveLastCounter();
        }
 
-       // Special case for `subequations' module.
-       // FIXME: replace with a genuine solution long-term!
-       if (il.latextype() == InsetLaTeXType::ENVIRONMENT &&
-           il.latexname() == "subequations") {
-               docstring equation(from_ascii("equation"));
-               docstring parentequation(from_ascii("parentequation"));
-               if (!deleted)
-                       cnts.step(equation, utype);
-               // save a copy of the equation counter definition
-               cnts.copy(equation, parentequation);
-               // redefine the equation counter definition
-               docstring const eqlabel = deleted ? from_ascii("#")
-                       : cnts.theCounter(equation, 
it->getParLanguage(bp)->code());
-               cnts.newCounter(equation, parentequation,
-                               eqlabel + from_ascii("\\alph{equation}"),
-                               eqlabel + from_ascii("\\alph{equation}"),
-                               from_ascii("Equation|Equations (##)"),
-                               cnts.guiName(parentequation));
-               InsetCollapsible::updateBuffer(it, utype, deleted);
-               // reset equation counter as it was.
-               cnts.copy(parentequation, equation);
-               cnts.remove(parentequation);
-               return;
-       }
-
        if (have_counter) {
                if (!deleted) {
                        cnts.step(count, utype);
+                       if (il.stepParentCounter())
+                               cnts.stepParent(count, utype);
+                       // Exception: insets with a subequation counter
+                       // use the the main equation only for the label
+                       docstring const lcounter = (count == 
from_ascii("subequation"))
+                                       ? from_ascii("equation")
+                                       : count;
                        custom_label += ' ' +
-                               cnts.theCounter(count, 
it.paragraph().getParLanguage(bp)->code());
+                               cnts.theCounter(lcounter, 
it.paragraph().getParLanguage(bp)->code());
                } else
                        custom_label += ' ' + from_ascii("#");
        }
diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp
index 1d078a23cf..60d30c7f4d 100644
--- a/src/insets/InsetLabel.cpp
+++ b/src/insets/InsetLabel.cpp
@@ -186,30 +186,6 @@ void InsetLabel::setFormattedCounter(docstring const & fc, 
bool const lc, bool c
 }
 
 
-namespace {
-docstring stripSubrefs(docstring const & in){
-       docstring res;
-       bool have_digit = false;
-       size_t const len = in.length();
-       for (size_t i = 0; i < len; ++i) {
-               // subref can be an alphabetic letter or '?'
-               // as soon as we encounter this, break
-               char_type const c = in[i];
-               if (have_digit && (('a' <= c && c <= 'z') || c == '?'))
-                       continue;
-               else {
-                       if (isNumberChar(c) && !have_digit)
-                               have_digit = true;
-                       else if (have_digit)
-                               have_digit = false;
-                       res += c;
-               }
-       }
-       return res;
-}
-}
-
-
 void InsetLabel::updateBuffer(ParIterator const & it, UpdateType, bool const 
/*deleted*/)
 {
        docstring const & label = getParam("name");
@@ -245,8 +221,11 @@ void InsetLabel::updateBuffer(ParIterator const & it, 
UpdateType, bool const /*d
        docstring const parent = (cnts.isSubfloat()) ? 
cnts.currentParentCounter() : docstring();
        Language const * lang = it->getParLanguage(buffer().params());
        if (lang && !active_counter_.empty()) {
-               bool const equation = active_counter_ == from_ascii("equation");
-               if (!equation || it.inTexted()) {
+               if (it.inTexted()) {
+                       // Insets with a subequation counter
+                       // use the the main equation only outside of math insets
+                       if (active_counter_ == from_ascii("subequation"))
+                               active_counter_ = from_ascii("equation");
                        counter_value_ = cnts.theCounter(active_counter_, 
lang->code());
                        pretty_counter_ = cnts.prettyCounter(active_counter_, 
lang->code());
                        docstring pretty_counter_pl = 
cnts.prettyCounter(active_counter_, lang->code(), false, true);
@@ -299,15 +278,6 @@ void InsetLabel::updateBuffer(ParIterator const & it, 
UpdateType, bool const /*d
                                formatted_counter_lc_ = 
subst(formatted_counter_lc_, plain_value, counter_value_);
                                formatted_counter_lc_pl_ = 
subst(formatted_counter_lc_pl_, plain_value, counter_value_);
                        }
-                       if (equation) {
-                               // FIXME: special code just for the 
subequations module (#13199)
-                               //        replace with a genuine solution 
long-term!
-                               counter_value_ = stripSubrefs(counter_value_);
-                               formatted_counter_ = 
stripSubrefs(formatted_counter_);
-                               formatted_counter_pl_ = 
stripSubrefs(formatted_counter_pl_);
-                               formatted_counter_lc_ = 
stripSubrefs(formatted_counter_lc_);
-                               formatted_counter_lc_pl_ = 
stripSubrefs(formatted_counter_lc_pl_);
-                       }
                } else {
                        // For equations, the counter value and pretty counter
                        // value will be set by the parent InsetMathHull.
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 98206b3063..037dc71eb8 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -251,8 +251,10 @@ void InsetMathHull::updateBuffer(ParIterator const & it, 
UpdateType utype, bool
                Counters & cnts =
                        
buffer_->masterBuffer()->params().documentClass().counters();
 
-               // this has to be done separately
-               docstring const eqstr = from_ascii("equation");
+               // Counter is equation by default, but the context might 
require subequations
+               docstring const eqstr = (cnts.currentCounter() == 
from_ascii("subequation"))
+                               ? cnts.currentCounter()
+                               : from_ascii("equation");
                if (cnts.hasCounter(eqstr)) {
                        for (size_t i = 0; i != labels_.size(); ++i) {
                                docstring const oldnumber = numbers_[i];
@@ -281,23 +283,47 @@ void InsetMathHull::updateBuffer(ParIterator const & it, 
UpdateType utype, bool
                        labels_[i]->updateBuffer(it, utype, deleted);
        }
 
-       // set up equation numbers
-       for (row_type row = 0; row != nrows(); ++row) {
-               if (numbered(row) && labels_[row]) {
-                       labels_[row]->setCounterValue(numbers_[row]);
-                       labels_[row]->setPrettyCounter("(" + numbers_[row] + 
")");
-                       // lowercase singular
-                       docstring pf = translateIfPossible(from_ascii("equation 
(##)"), lang);
-                       labels_[row]->setFormattedCounter(subst(pf, 
from_ascii("##"), numbers_[row]), true, false);
-                       // lowercase plural
-                       pf = translateIfPossible(from_ascii("equations (##)"), 
lang);
-                       labels_[row]->setFormattedCounter(subst(pf, 
from_ascii("##"), numbers_[row]), true, true);
-                       // uppercase singular
-                       pf = translateIfPossible(from_ascii("Equation (##)"), 
lang);
-                       labels_[row]->setFormattedCounter(subst(pf, 
from_ascii("##"), numbers_[row]), false, false);
-                       // uppercase plural
-                       pf = translateIfPossible(from_ascii("Equations (##)"), 
lang);
-                       labels_[row]->setFormattedCounter(subst(pf, 
from_ascii("##"), numbers_[row]), false, true);
+       if (haveNumbers()) {
+               Counters & cnts =
+                       
buffer_->masterBuffer()->params().documentClass().counters();
+               // Counter is equation by default, but the context might 
require subequations
+               docstring const eqstr = (cnts.currentCounter() == 
from_ascii("subequation"))
+                               ? cnts.currentCounter()
+                               : from_ascii("equation");
+               docstring const eqprf = from_ascii("eq");
+               bool const have_cnt = cnts.hasCounter(eqstr);
+               // set up equation numbers
+               for (row_type row = 0; row != nrows(); ++row) {
+                       if (numbered(row) && labels_[row]) {
+                               labels_[row]->setCounterValue(numbers_[row]);
+                               if (have_cnt) {
+                                       // We use the format definitions as 
specified in the counter definition if available
+                                       
labels_[row]->setPrettyCounter(cnts.prettyCounter(eqstr, lang));
+                                       // lowercase singular
+                                       
labels_[row]->setFormattedCounter(cnts.formattedCounter(eqstr, eqprf, lang, 
true, false), true, false);
+                                       // lowercase plural
+                                       
labels_[row]->setFormattedCounter(cnts.formattedCounter(eqstr, eqprf, lang, 
true, true), true, true);
+                                       // uppercase singular
+                                       
labels_[row]->setFormattedCounter(cnts.formattedCounter(eqstr, eqprf, lang, 
false, false),  false, false);
+                                       // uppercase plural
+                                       
labels_[row]->setFormattedCounter(cnts.formattedCounter(eqstr, eqprf, lang, 
false, true), false, true);
+                               } else {
+                                       // Hardcoded fallbacks for the case the 
counter definition is not available
+                                       labels_[row]->setPrettyCounter("(" + 
numbers_[row] + ")");
+                                       // lowercase singular
+                                       docstring pf = 
translateIfPossible(from_ascii("equation (##)"), lang);
+                                       
labels_[row]->setFormattedCounter(subst(pf, from_ascii("##"), numbers_[row]), 
true, false);
+                                       // lowercase plural
+                                       pf = 
translateIfPossible(from_ascii("equations (##)"), lang);
+                                       
labels_[row]->setFormattedCounter(subst(pf, from_ascii("##"), numbers_[row]), 
true, true);
+                                       // uppercase singular
+                                       pf = 
translateIfPossible(from_ascii("Equation (##)"), lang);
+                                       
labels_[row]->setFormattedCounter(subst(pf, from_ascii("##"), numbers_[row]), 
false, false);
+                                       // uppercase plural
+                                       pf = 
translateIfPossible(from_ascii("Equations (##)"), lang);
+                                       
labels_[row]->setFormattedCounter(subst(pf, from_ascii("##"), numbers_[row]), 
false, true);
+                               }
+                       }
                }
        }
 
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to