]> git.lyx.org Git - lyx.git/blobdiff - src/Counters.cpp
Update Win installer for new dictionary links. Untested.
[lyx.git] / src / Counters.cpp
index 144f5abbb38c4db326fe84c618b69eb31d1aa565..114b18020b3d0b34690318f5046391f7e09314f3 100644 (file)
@@ -6,7 +6,7 @@
  * \author Lars Gullik Bjønnes
  * \author Martin Vermeer
  * \author André Pönitz
- * \author Richard Heck (roman numerals)
+ * \author Richard Kimberly Heck (roman numerals)
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -20,6 +20,7 @@
 #include "support/convert.h"
 #include "support/counter_reps.h"
 #include "support/debug.h"
+#include "support/docstring.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
@@ -41,9 +42,10 @@ Counter::Counter()
 
 
 Counter::Counter(docstring const & mc, docstring const & ls,
-               docstring const & lsa, docstring const & guiname)
-       : initial_value_(0), saved_value_(0), master_(mc), labelstring_(ls),
-         labelstringappendix_(lsa), guiname_(guiname)
+               docstring const & lsa, docstring const & prettyformat,
+               docstring const & guiname)
+       : initial_value_(0), saved_value_(0), parent_(mc), labelstring_(ls),
+         labelstringappendix_(lsa), prettyformat_(prettyformat), guiname_(guiname)
 {
        reset();
 }
@@ -58,6 +60,8 @@ bool Counter::read(Lexer & lex)
                CT_PRETTYFORMAT,
                CT_INITIALVALUE,
                CT_GUINAME,
+               CT_LATEXNAME,
+               CT_REFFORMAT,
                CT_END
        };
 
@@ -67,7 +71,9 @@ bool Counter::read(Lexer & lex)
                { "initialvalue", CT_INITIALVALUE},
                { "labelstring", CT_LABELSTRING },
                { "labelstringappendix", CT_LABELSTRING_APPENDIX },
+               { "latexname", CT_LATEXNAME },
                { "prettyformat", CT_PRETTYFORMAT },
+               { "refformat", CT_REFFORMAT },
                { "within", CT_WITHIN }
        };
 
@@ -86,9 +92,9 @@ bool Counter::read(Lexer & lex)
                switch (le) {
                        case CT_WITHIN:
                                lex.next();
-                               master_ = lex.getDocString();
-                               if (master_ == "none")
-                                       master_.erase();
+                               parent_ = lex.getDocString();
+                               if (parent_ == "none")
+                                       parent_.erase();
                                break;
                        case CT_INITIALVALUE:
                                lex.next();
@@ -106,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();
@@ -119,10 +134,21 @@ bool Counter::read(Lexer & lex)
                                lex.next();
                                guiname_ = lex.getDocString();
                                break;
+                       case CT_LATEXNAME:
+                               lex.next();
+                               latexname_ = lex.getDocString();
+                               break;
                        case CT_END:
                                getout = true;
                                break;
                }
+               // fall back on GuiName if PrettyFormat is empty
+               if (prettyformat_.empty()) {
+                       if (guiname_.empty())
+                               prettyformat_ = from_ascii("##");
+                       else
+                               prettyformat_ = "## (" + guiname_ + ")";
+               }
        }
 
        // Here if have a full counter if getout == true
@@ -175,17 +201,26 @@ void Counter::reset()
 }
 
 
-docstring const & Counter::master() const
+docstring const & Counter::refFormat(docstring const & prefix) const
 {
-       return master_;
+       map<docstring, docstring>::const_iterator it = ref_formats_.find(prefix);
+       if (it == ref_formats_.end())
+               return prettyformat_;
+       return it->second;
 }
 
 
-bool Counter::checkAndRemoveMaster(docstring const & cnt)
+docstring const & Counter::parent() const
 {
-       if (master_ != cnt)
+       return parent_;
+}
+
+
+bool Counter::checkAndRemoveParent(docstring const & cnt)
+{
+       if (parent_ != cnt)
                return false;
-       master_ = docstring();
+       parent_ = docstring();
        return true;
 }
 
@@ -210,18 +245,19 @@ Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
 
 
 void Counters::newCounter(docstring const & newc,
-                         docstring const & masterc,
+                         docstring const & parentc,
                          docstring const & ls,
                          docstring const & lsa,
+                         docstring const & prettyformat,
                          docstring const & guiname)
 {
-       if (!masterc.empty() && !hasCounter(masterc)) {
-               lyxerr << "Master counter does not exist: "
-                      << to_utf8(masterc)
+       if (!parentc.empty() && !hasCounter(parentc)) {
+               lyxerr << "Parent counter does not exist: "
+                          << to_utf8(parentc)
                       << endl;
                return;
        }
-       counterList_[newc] = Counter(masterc, ls, lsa, guiname);
+       counterList_[newc] = Counter(parentc, ls, lsa, prettyformat, guiname);
 }
 
 
@@ -314,18 +350,18 @@ void Counters::restoreValue(docstring const & ctr) const
 }
 
 
-void Counters::resetSlaves(docstring const & count)
+void Counters::resetChildren(docstring const & count)
 {
        for (auto & ctr : counterList_) {
-               if (ctr.second.master() == count) {
+               if (ctr.second.parent() == count) {
                        ctr.second.reset();
-                       resetSlaves(ctr.first);
+                       resetChildren(ctr.first);
                }
        }
 }
 
 
-void Counters::stepMaster(docstring const & ctr, UpdateType utype)
+void Counters::stepParent(docstring const & ctr, UpdateType utype)
 {
        CounterList::iterator it = counterList_.find(ctr);
        if (it == counterList_.end()) {
@@ -333,11 +369,11 @@ void Counters::stepMaster(docstring const & ctr, UpdateType utype)
                       << to_utf8(ctr) << endl;
                return;
        }
-       step(it->second.master(), utype);
+       step(it->second.parent(), utype);
 }
 
 
-void Counters::step(docstring const & ctr, UpdateType utype)
+void Counters::step(docstring const & ctr, UpdateType /* deleted */)
 {
        CounterList::iterator it = counterList_.find(ctr);
        if (it == counterList_.end()) {
@@ -347,13 +383,11 @@ void Counters::step(docstring const & ctr, UpdateType utype)
        }
 
        it->second.step();
-       if (utype == OutputUpdate) {
-               LBUFERR(!counter_stack_.empty());
-               counter_stack_.pop_back();
-               counter_stack_.push_back(ctr);
-       }
+       LBUFERR(!counter_stack_.empty());
+       counter_stack_.pop_back();
+       counter_stack_.push_back(ctr);
 
-       resetSlaves(ctr);
+       resetChildren(ctr);
 }
 
 
@@ -373,6 +407,22 @@ docstring const & Counters::guiName(docstring const & cntr) const
 }
 
 
+docstring const & Counters::latexName(docstring const & cntr) const
+{
+       CounterList::const_iterator it = counterList_.find(cntr);
+       if (it == counterList_.end()) {
+               lyxerr << "step: Counter does not exist: "
+                          << to_utf8(cntr) << endl;
+               return empty_docstring();
+       }
+
+       docstring const & latexname = it->second.latexName();
+       if (latexname.empty())
+               return cntr;
+       return latexname;
+}
+
+
 void Counters::reset()
 {
        appendix_ = false;
@@ -398,30 +448,30 @@ void Counters::reset(docstring const & match)
 }
 
 
+bool Counters::copy(docstring const & cnt, docstring const & newcnt)
+{
+       auto const it = counterList_.find(cnt);
+       if (it == counterList_.end())
+               return false;
+       counterList_[newcnt] = it->second;
+       return true;
+}
+
+
 bool Counters::remove(docstring const & cnt)
 {
        bool retval = counterList_.erase(cnt);
        if (!retval)
                return false;
        for (auto & ctr : counterList_) {
-               if (ctr.second.checkAndRemoveMaster(cnt))
-                       LYXERR(Debug::TCLASS, "Removed master counter `" +
+               if (ctr.second.checkAndRemoveParent(cnt))
+                       LYXERR(Debug::TCLASS, "Removed parent counter `" +
                                        to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
        }
        return retval;
 }
 
 
-void Counters::copy(Counters & from, Counters & to, docstring const & match)
-{
-       for (auto const & ctr : counterList_) {
-               if (ctr.first.find(match) != string::npos || match == "") {
-                       to.set(ctr.first, from.value(ctr.first));
-               }
-       }
-}
-
-
 docstring Counters::labelItem(docstring const & ctr,
                              docstring const & numbertype) const
 {
@@ -453,6 +503,9 @@ docstring Counters::labelItem(docstring const & ctr,
        if (numbertype == "fnsymbol")
                return fnsymbolCounter(val);
 
+       if (numbertype == "superarabic")
+               return superarabicCounter(val);
+
        return convert<docstring>(val);
 }
 
@@ -499,8 +552,8 @@ docstring Counters::flattenLabelString(docstring const & counter,
 
        callers.push_back(counter);
        if (ls.empty()) {
-               if (!c.master().empty())
-                       ls = flattenLabelString(c.master(), in_appendix, lang, callers)
+               if (!c.parent().empty())
+                       ls = flattenLabelString(c.parent(), in_appendix, lang, callers)
                                + from_ascii(".");
                callers.pop_back();
                return ls + from_ascii("\\arabic{") + counter + "}";
@@ -569,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
 {
@@ -579,7 +649,7 @@ docstring Counters::prettyCounter(docstring const & name,
 
        docstring const value = theCounter(name, lang);
        docstring const & format =
-           translateIfPossible(ctr.prettyFormat(), lang);
+               translateIfPossible(counterLabel(ctr.prettyFormat(), lang), lang);
        if (format.empty())
                return value;
        return subst(format, from_ascii("##"), value);