commit 8cc932b96d511eb2b1354d0a5c0fcbaa03d71ed2
Author: Juergen Spitzmueller <[email protected]>
Date:   Sat Aug 23 13:27:06 2025 +0200

    Improve on fc736dc134ab9ac8e
    
    * simplify logic
    * fix formatted counter settings
---
 lib/layouts/subequations.module |  6 ++--
 src/insets/InsetFlex.cpp        |  7 +---
 src/insets/InsetLabel.cpp       | 22 ++++---------
 src/insets/InsetLabel.h         |  6 ++--
 src/mathed/InsetMathHull.cpp    | 72 +++++++++++++++++++----------------------
 5 files changed, 48 insertions(+), 65 deletions(-)

diff --git a/lib/layouts/subequations.module b/lib/layouts/subequations.module
index 17003c44f5..b16bba6ab9 100644
--- a/lib/layouts/subequations.module
+++ b/lib/layouts/subequations.module
@@ -8,6 +8,9 @@
 
 Format 111
 
+# This counter is used by the equations within the inset below
+# but the inset itself uses the equation counter (outside of
+# mathed
 Counter subequation
   GuiName              Sub-Equation
   Within               equation
@@ -18,8 +21,7 @@ End
 InsetLayout Flex:Subequations
   LyxType               custom
   Decoration            classic
-  Counter               subequation
-  StepParentCounter     true
+  Counter               equation
   LabelString           Subequations
   LatexType             Environment
   LatexName             subequations
diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp
index 6181b3228c..3383fb6289 100644
--- a/src/insets/InsetFlex.cpp
+++ b/src/insets/InsetFlex.cpp
@@ -163,13 +163,8 @@ void InsetFlex::updateBuffer(ParIterator const & it, 
UpdateType utype, bool cons
                        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(lcounter, 
it.paragraph().getParLanguage(bp)->code());
+                               cnts.theCounter(count, 
it.paragraph().getParLanguage(bp)->code());
                } else
                        custom_label += ' ' + from_ascii("#");
        }
diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp
index 60d30c7f4d..db6fc3348b 100644
--- a/src/insets/InsetLabel.cpp
+++ b/src/insets/InsetLabel.cpp
@@ -170,19 +170,13 @@ docstring InsetLabel::formattedCounter(bool const lc, 
bool const pl) const
 }
 
 
-void InsetLabel::setFormattedCounter(docstring const & fc, bool const lc, bool 
const pl)
+void InsetLabel::setFormattedCounter(docstring const & ls, docstring const & 
lp,
+                                    docstring const & us, docstring const & up)
 {
-       if (pl) {
-               if (lc)
-                       formatted_counter_lc_pl_ = fc;
-               else
-                       formatted_counter_pl_ = fc;
-       } else {
-               if (lc)
-                       formatted_counter_lc_ = fc;
-               else
-                       formatted_counter_ = fc;
-       }
+       formatted_counter_lc_ = ls;
+       formatted_counter_lc_pl_ = lp;
+       formatted_counter_ = us;
+       formatted_counter_pl_ = up;
 }
 
 
@@ -222,10 +216,6 @@ void InsetLabel::updateBuffer(ParIterator const & it, 
UpdateType, bool const /*d
        Language const * lang = it->getParLanguage(buffer().params());
        if (lang && !active_counter_.empty()) {
                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);
diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h
index f786dc5843..07f207e71b 100644
--- a/src/insets/InsetLabel.h
+++ b/src/insets/InsetLabel.h
@@ -37,8 +37,10 @@ public:
        void setCounterValue(docstring const & cv) { counter_value_ = cv; }
        ///
        void setPrettyCounter(docstring const & pc) { pretty_counter_ = pc; }
-       ///
-       void setFormattedCounter(docstring const & fc, bool lc = false, bool pl 
= false);
+       /** Formatted counter definition: lowercase singular (\p ls), lowercase 
plural (\p lp),
+           uppercase singular (\p us), and uppercase plural (\p up) */
+       void setFormattedCounter(docstring const & ls, docstring const & lp,
+                                docstring const & us, docstring const & up);
        ///
        int rowFlags() const override { return CanBreakBefore | CanBreakAfter; }
        /// Updates only the label string, doesn't handle undo nor references.
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 037dc71eb8..b6bac747bc 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -245,17 +245,25 @@ void InsetMathHull::updateBuffer(ParIterator const & it, 
UpdateType utype, bool
 
        BufferParams const & bp = buffer_->params();
        string const & lang = it->getParLanguage(bp)->code();
+       docstring eqstr;
+       bool have_cnt = false;
+       Counters & cnts =
+               buffer_->masterBuffer()->params().documentClass().counters();
+       // Counter is equation by default, but the context might require 
subequations
+       // This is the case if the equation counter is already active
+       // and we have a subequation counter (as in the subequations inset)
+       bool const need_subequations = cnts.currentCounter() == 
from_ascii("equation")
+                       && cnts.hasCounter(from_ascii("subequation"));
+       if (need_subequations)
+               // the subequations counter will be local
+               cnts.saveLastCounter();
        // if any of the equations are numbered, then we want to save the values
        // of some of the counters.
        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");
-               if (cnts.hasCounter(eqstr)) {
+               eqstr = need_subequations ? from_ascii("subequation") : 
from_ascii("equation");
+               have_cnt = cnts.hasCounter(eqstr);
+               if (have_cnt) {
+                       cnts.saveValue(eqstr);
                        for (size_t i = 0; i != labels_.size(); ++i) {
                                docstring const oldnumber = numbers_[i];
                                if (numbered(i)) {
@@ -284,48 +292,34 @@ void InsetMathHull::updateBuffer(ParIterator const & it, 
UpdateType utype, bool
        }
 
        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
+               // we need to start afresh to generate the definitions
+               cnts.restoreValue(eqstr);
                for (row_type row = 0; row != nrows(); ++row) {
                        if (numbered(row) && labels_[row]) {
+                               cnts.step(eqstr, utype);
                                labels_[row]->setCounterValue(numbers_[row]);
                                if (have_cnt) {
-                                       // We use the format definitions as 
specified in the counter definition if available
+                                       // We use the format definitions as 
specified in the counter definition
                                        
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);
+                                       labels_[row]->setFormattedCounter(
+                                               // lowercase singular
+                                               cnts.formattedCounter(eqstr, 
eqprf, lang, true, false),
+                                               // lowercase plural
+                                               cnts.formattedCounter(eqstr, 
eqprf, lang, true, true),
+                                               // uppercase singular
+                                               cnts.formattedCounter(eqstr, 
eqprf, lang, false, false),
+                                               // uppercase plural
+                                               cnts.formattedCounter(eqstr, 
eqprf, lang, false, true)
+                                       );
                                }
                        }
                }
        }
+       if (need_subequations)
+               // the subequations counter will be local
+               cnts.restoreLastCounter();
 
        // pass down
        InsetMathGrid::updateBuffer(it, utype, deleted);
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to