commit a1d6f09ee0961d45e177e421d58e5a55c37b4bba
Author: Juergen Spitzmueller <[email protected]>
Date:   Tue Aug 26 10:18:24 2025 +0200

    Fix refstyle fallback mechanism
    
    Not all prefixes known to LyX are supported by refstyle. Also,
    for capitalized refs, the prefixes are internally capitalized
    as well.
---
 src/TextClass.cpp       |  9 ---------
 src/TextClass.h         |  2 --
 src/insets/InsetRef.cpp | 35 +++++++++++++++++++++++++++++++----
 src/insets/InsetRef.h   |  2 ++
 4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/src/TextClass.cpp b/src/TextClass.cpp
index 86df547b43..8955b20550 100644
--- a/src/TextClass.cpp
+++ b/src/TextClass.cpp
@@ -2091,15 +2091,6 @@ bool DocumentClass::hasLaTeXLayout(std::string const & 
lay) const
 }
 
 
-bool DocumentClass::hasRefPrefix(docstring const & pr) const
-{
-       for (auto const & l : layoutlist_)
-               if (l.refprefix == pr)
-                       return true;
-       return false;
-}
-
-
 bool DocumentClass::provides(string const & p) const
 {
        return provides_.find(p) != provides_.end();
diff --git a/src/TextClass.h b/src/TextClass.h
index 97ba6ea7ef..cdab0b08db 100644
--- a/src/TextClass.h
+++ b/src/TextClass.h
@@ -436,8 +436,6 @@ public:
        ///////////////////////////////////////////////////////////////////
        /// \return true if there is a Layout with latexname lay
        bool hasLaTeXLayout(std::string const & lay) const;
-       /// \return true if there is a Layout with refprefix pr
-       bool hasRefPrefix(docstring const & pr) const;
        /// A DocumentClass nevers count as loaded, since it is dynamic
        bool loaded() const override { return false; }
        /// \return the layout object of an inset given by name. If the name
diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp
index 8bb5aad578..d151be0625 100644
--- a/src/insets/InsetRef.cpp
+++ b/src/insets/InsetRef.cpp
@@ -344,6 +344,34 @@ docstring InsetRef::getEscapedLabel(OutputParams const & 
rp) const
 }
 
 
+bool InsetRef::isRefStyleSupported(docstring & pr) const
+{
+       // lowercase capitalized prefixes
+       char_type t = lowercase(pr[0]);
+       pr[0] = t;
+
+       // These are supported by the package
+       if (pr == "part" || pr == "chap" || pr == "sec" || pr == "eq"
+           || pr == "fig" || pr == "tab" || pr == "fn")
+               return true;
+
+       // These are additionally supported by LyX
+       if (pr == "cha" || pr == "enu" || pr == "subsec")
+               return true;
+
+       // Theorems are all supported by LyX
+       DocumentClass const & tclass = buffer().masterParams().documentClass();
+       DocumentClass::const_iterator lit = tclass.begin();
+       DocumentClass::const_iterator len = tclass.end();
+       for (; lit != len; ++lit) {
+               if (!lit->thmName().empty() && lit->refprefix == pr)
+                       return true;
+       }
+
+       return false;
+}
+
+
 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
 {
        string const & cmd = getCmdName();
@@ -413,9 +441,8 @@ void InsetRef::latex(otexstream & os, OutputParams const & 
rp) const
                                        os << ",";
                        }
                        if (contains(*it, ' ') && 
buffer().masterParams().xref_package == "refstyle"
-                           && 
buffer().masterParams().documentClass().hasRefPrefix(prefix))
+                           && isRefStyleSupported(prefix))
                                // refstyle bug: labels with blanks need to be 
grouped for known commands
-                               // (basically those with known refprefixes)
                                // otherwise the blanks will be gobbled
                                os << "{" << *it << "}";
                        else {
@@ -989,8 +1016,8 @@ void InsetRef::validate(LaTeXFeatures & features) const
                                features.require("refstyle:subsecref");
                        else if (prefix == "enu" || prefix == "Enu")
                                features.require("refstyle:enuref");
-                       else if (!prefix.empty() && 
!buffer().masterParams().documentClass().hasRefPrefix(prefix)) {
-                               // fallback command for unknown prefixes
+                       else if (!prefix.empty() && 
!isRefStyleSupported(prefix)) {
+                               // fallback command for unsupported prefixes
                                docstring lcmd = 
"\\AtBeginDocument{\\providecommand" +
                                                fcmd + "[1]{\\ref{" + prefix + 
":#1}}}";
                                features.addPreambleSnippet(lcmd);
diff --git a/src/insets/InsetRef.h b/src/insets/InsetRef.h
index 2935507cc4..9dce951c01 100644
--- a/src/insets/InsetRef.h
+++ b/src/insets/InsetRef.h
@@ -143,6 +143,8 @@ private:
        std::vector<docstring> getLabels() const { return 
support::getVectorFromString(getParam("reference")); }
        ///
        bool isBroken(docstring const & label) const;
+       ///
+       bool isRefStyleSupported(docstring & pr) const;
 
        ///
        mutable docstring screen_label_;
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to