]> git.lyx.org Git - lyx.git/commitdiff
New RefFormat tag for counters, and PrettyFormat for floats.
authorRichard Kimberly Heck <rikiheck@lyx.org>
Fri, 28 Jul 2023 21:37:13 +0000 (17:37 -0400)
committerRichard Kimberly Heck <rikiheck@lyx.org>
Fri, 28 Jul 2023 21:37:13 +0000 (17:37 -0400)
Layout format updated to 104.

lib/scripts/layout2layout.py
src/Counters.cpp
src/Counters.h
src/TextClass.cpp
src/frontends/qt/GuiRef.cpp
src/insets/InsetLabel.cpp
src/insets/InsetLabel.h
src/insets/InsetRef.cpp

index 7d3cca7be8d6a96aedeea29c6dbfa3a39bbc3aca..611639412a78e4235a55e41c890546f4e080c06f 100644 (file)
@@ -11,7 +11,7 @@
 # This script will update a .layout file to current format
 
 # The latest layout format is also defined in src/TextClass.cpp
-currentFormat = 103
+currentFormat = 104
 
 
 # Incremented to format 4, 6 April 2007, lasgouttes
@@ -348,6 +348,9 @@ currentFormat = 103
 # Incremented to format 103, 27 July 2023 by rikiheck
 # Allow e.g. \roman{section} in PrettyFormat
 
+# Incremented to format 104, 28 July 2023 by rikiheck
+# RefFormat for counters and PrettyFormat for floats
+
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
 
@@ -595,7 +598,7 @@ def convert(lines, end_format):
                 i += 1
             continue
 
-        if 101 <= format <= 102:
+        if 101 <= format <= 103:
             # nothing to do.
             i += 1
             continue
index 1a9c6af6375de88fb7831c01ac35901899f31188..9376ef0aac78aa0e751757ad786e5d6a93bff917 100644 (file)
@@ -61,6 +61,7 @@ bool Counter::read(Lexer & lex)
                CT_INITIALVALUE,
                CT_GUINAME,
                CT_LATEXNAME,
+               CT_REFFORMAT,
                CT_END
        };
 
@@ -72,6 +73,7 @@ bool Counter::read(Lexer & lex)
                { "labelstringappendix", CT_LABELSTRING_APPENDIX },
                { "latexname", CT_LATEXNAME },
                { "prettyformat", CT_PRETTYFORMAT },
+               { "refformat", CT_REFFORMAT },
                { "within", CT_WITHIN }
        };
 
@@ -110,6 +112,15 @@ bool Counter::read(Lexer & lex)
                                lex.next();
                                prettyformat_ = lex.getDocString();
                                break;
+                       case CT_REFFORMAT: {
+                               lex.next();
+                               docstring const key = lex.getDocString();
+                               lex.next();
+                               docstring const value = lex.getDocString();
+                               ref_formats_[key] = value;
+                               LYXERR0("refformat: " << key << " => " << value);
+                               break;
+                       }
                        case CT_LABELSTRING:
                                lex.next();
                                labelstring_ = lex.getDocString();
@@ -131,11 +142,12 @@ bool Counter::read(Lexer & lex)
                                getout = true;
                                break;
                }
-               if (prettyformat_ == "") { // fall back on GuiName if PrettyFormat is empty
-                       if (guiname_ == "")
+               // fall back on GuiName if PrettyFormat is empty
+               if (prettyformat_.empty()) {
+                       if (guiname_.empty())
                                prettyformat_ = from_ascii("##");
                        else
-                               prettyformat_ = "## (" + guiname_ + " counter)";
+                               prettyformat_ = "## (" + guiname_ + ")";
                }
        }
 
@@ -189,6 +201,15 @@ void Counter::reset()
 }
 
 
+docstring const & Counter::refFormat(docstring const & prefix) const
+{
+       map<docstring, docstring>::const_iterator it = ref_formats_.find(prefix);
+       if (it == ref_formats_.end())
+               return prettyformat_;
+       return it->second;
+}
+
+
 docstring const & Counter::parent() const
 {
        return parent_;
@@ -601,6 +622,23 @@ docstring Counters::counterLabel(docstring const & format,
 }
 
 
+docstring Counters::formattedCounter(docstring const & name,
+                       docstring const & prex, string const & lang) const
+{
+       CounterList::const_iterator it = counterList_.find(name);
+       if (it == counterList_.end())
+               return from_ascii("#");
+       Counter const & ctr = it->second;
+
+       docstring const value = theCounter(name, lang);
+       docstring const & format =
+               translateIfPossible(counterLabel(ctr.refFormat(prex), lang), lang);
+       if (format.empty())
+               return value;
+       return subst(format, from_ascii("##"), value);
+}
+
+
 docstring Counters::prettyCounter(docstring const & name,
                               string const & lang) const
 {
index 41ee930583781f86cbcc525c8d930e7144825a1f..5be4aca197b8df7b878f628f6d483452864da919 100644 (file)
@@ -69,6 +69,8 @@ public:
        /// E.g., for a section counter it might be "section \thesection"
        docstring const & prettyFormat() const { return prettyformat_; }
        ///
+       docstring const & refFormat(docstring const & prefix) const;
+       ///
        docstring const & guiName() const { return guiname_; }
        ///
        docstring const & latexName() const { return latexname_; }
@@ -103,6 +105,8 @@ private:
        /// Similar, but used for formatted references in XHTML output
        docstring prettyformat_;
        ///
+       std::map<docstring, docstring> ref_formats_;
+       ///
        docstring guiname_;
        /// The name used for the counter in LaTeX
        docstring latexname_;
@@ -183,6 +187,11 @@ public:
        /// format given by Counter::prettyFormat().
        docstring prettyCounter(docstring const & cntr,
                               std::string const & lang) const;
+       /// returns a formatted version of the counter, using the
+       /// format given by Counter::prettyFormat().
+       docstring formattedCounter(docstring const & cntr,
+                                       docstring const & prefix,
+                                       std::string const & lang) const;
        ///
        docstring const & guiName(docstring const & cntr) const;
        ///
index a9688d3b1322855c81b0287f51866d995db19e18..f6ec875167bec63b1b0186cbcdce79f05d840301 100644 (file)
@@ -59,7 +59,7 @@ namespace lyx {
 // You should also run the development/tools/updatelayouts.py script,
 // to update the format of all of our layout files.
 //
-int const LAYOUT_FORMAT = 103; // rkh: allow counter specs in PrettyFormat
+int const LAYOUT_FORMAT = 104; // rkh: RefFormat for counters
 
 
 // Layout format for the current lyx file format. Controls which format is
@@ -1412,6 +1412,7 @@ bool TextClass::readFloat(Lexer & lexrc)
                FT_ALLOWS_SIDEWAYS,
                FT_ALLOWS_WIDE,
                FT_REQUIRES,
+               FT_PRETTYFORMAT,
                FT_END
        };
 
@@ -1435,6 +1436,7 @@ bool TextClass::readFloat(Lexer & lexrc)
                { "listname", FT_LISTNAME },
                { "numberwithin", FT_WITHIN },
                { "placement", FT_PLACEMENT },
+               { "prettyformat", FT_PRETTYFORMAT },
                { "refprefix", FT_REFPREFIX },
                { "requires", FT_REQUIRES },
                { "style", FT_STYLE },
@@ -1463,6 +1465,7 @@ bool TextClass::readFloat(Lexer & lexrc)
        string type;
        string within;
        string required;
+       docstring prettyformat;
        bool usesfloat = true;
        bool ispredefined = false;
        bool allowswide = true;
@@ -1566,6 +1569,10 @@ bool TextClass::readFloat(Lexer & lexrc)
                        lexrc.next();
                        htmltag = lexrc.getString();
                        break;
+               case FT_PRETTYFORMAT:
+                       lexrc.next();
+                       prettyformat = lexrc.getDocString();
+                       break;
                case FT_DOCBOOKATTR:
                        lexrc.next();
                        docbookattr = lexrc.getString();
@@ -1624,13 +1631,13 @@ bool TextClass::readFloat(Lexer & lexrc)
                // each float has its own counter
                counters_.newCounter(from_ascii(type), from_ascii(within),
                                docstring(), docstring(),
-                               bformat(_("%1$s ##"), _(name)),
+                               prettyformat.empty() ? bformat(_("%1$s ##"), _(name)) : prettyformat,
                                bformat(_("%1$s (Float)"), _(name)));
                // also define sub-float counters
                docstring const subtype = "sub-" + from_ascii(type);
                counters_.newCounter(subtype, from_ascii(type),
                                "\\alph{" + subtype + "}", docstring(),
-                               bformat(_("Sub-%1$s ##"), _(name)),
+                               prettyformat.empty() ? bformat(_("Sub-%1$s ##"), _(name)) : prettyformat,
                                bformat(_("Sub-%1$s (Float)"), _(name)));
        }
        return getout;
index fbf68247f8c5582e63a913ba061c5fa22b0c711b..6a2446a5a669070a6bfafa63f2e485ac1ef94cb8 100644 (file)
@@ -452,12 +452,10 @@ void GuiRef::redoRefs()
        QStringList refsCategories;
        // Do we have a prefix-less label at all?
        bool noprefix = false;
-       vector<std::tuple<docstring, docstring,docstring>>::const_iterator iter;
-       for (auto const & ref : refs_) {
+       for (auto const & theref : refs_) {
                // first: plain label name, second: gui name, third: pretty name
-               QString const lab = toqstr(get<0>(ref));
-               refsNames.append({lab, toqstr(get<1>(ref)),
-                                       toqstr(get<2>(*iter))});
+               QString const lab = toqstr(get<0>(theref));
+               refsNames.append({lab, toqstr(get<1>(theref)), toqstr(get<2>(theref))});
                if (groupCB->isChecked()) {
                        if (lab.contains(":")) {
                                QString const pref = lab.split(':')[0];
index 1f0a0459ac581b317127ac57d19abce92fc1a4ba..1ca8ea08ae86c94879006a937ab1e1965a11fdef 100644 (file)
@@ -193,15 +193,24 @@ void InsetLabel::updateBuffer(ParIterator const & it, UpdateType, bool const /*d
                if (active_counter_ != from_ascii("equation")) {
                        counter_value_ = cnts.theCounter(active_counter_, lang->code());
                        pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
+                       docstring prex;
+                       split(label, prex, ':');
+                       if (prex == label) {
+                               // No prefix found
+                               formatted_counter_ = pretty_counter_;
+                       } else
+                               formatted_counter_ = cnts.formattedCounter(active_counter_, prex, lang->code());
                } else {
                        // For equations, the counter value and pretty counter
                        // value will be set by the parent InsetMathHull.
                        counter_value_ = from_ascii("#");
                        pretty_counter_ = from_ascii("");
+                       formatted_counter_ = from_ascii("");
                }
        } else {
                counter_value_ = from_ascii("#");
                pretty_counter_ = from_ascii("#");
+               formatted_counter_ = from_ascii("#");
        }
 }
 
@@ -215,10 +224,10 @@ void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
        if (buffer().insetLabel(label, true) != this)
                output_active = false;
 
-       // We put both  active and inactive labels to the outliner
+       // We put both active and inactive labels to the outliner
        shared_ptr<Toc> toc = backend.toc("label");
        TocItem toc_item = TocItem(cpit, 0, screen_label_, output_active);
-       toc_item.prettyStr(pretty_counter_);
+       toc_item.prettyStr(formatted_counter_);
        toc->push_back(toc_item);
        // The refs get assigned only to the active label. If no active one exists,
        // assign the (BROKEN) refs to the first inactive one.
index 5cd4a88ddc9a891eafdf4cf7b71d4ddd1714a287..886b9c6d8a2f1a13e39b79ef6fa3e66b79fd1254 100644 (file)
@@ -30,6 +30,8 @@ public:
        ///
        docstring const & prettyCounter() const { return pretty_counter_; }
        ///
+       docstring const & formattedCounter() const { return formatted_counter_; }
+       ///
        void setPrettyCounter(docstring pc) { pretty_counter_ = pc; }
        ///
        int rowFlags() const override { return CanBreakBefore | CanBreakAfter; }
@@ -109,6 +111,8 @@ private:
        docstring counter_value_;
        ///
        docstring pretty_counter_;
+       ///
+       docstring formatted_counter_;
 };
 
 
index 4473bd7ad63440285256f5c468002ee04b477eef..7c7a7a57e19a6f663a639f88de4c73d1a6a57585 100644 (file)
@@ -404,7 +404,7 @@ docstring InsetRef::xhtml(XMLStream & xs, OutputParams const & op) const
                else if (cmd == "eqref")
                        display_string = '(' + value + ')';
                else if (cmd == "formatted") {
-                       display_string = il->prettyCounter();
+                       display_string = il->formattedCounter();
                        if (buffer().params().use_refstyle && getParam("caps") == "true")
                                capitalize(display_string);
                        // it is hard to see what to do about plurals...