]> git.lyx.org Git - features.git/commitdiff
Implement the suggestion by Andre'
authorEnrico Forestieri <forenr@lyx.org>
Tue, 17 Jun 2008 11:10:43 +0000 (11:10 +0000)
committerEnrico Forestieri <forenr@lyx.org>
Tue, 17 Jun 2008 11:10:43 +0000 (11:10 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25287 a592a061-630c-0410-9148-cb99ea01b6c8

35 files changed:
src/mathed/CommandInset.cpp
src/mathed/InsetMath.cpp
src/mathed/InsetMath.h
src/mathed/InsetMathAMSArray.cpp
src/mathed/InsetMathArray.cpp
src/mathed/InsetMathBig.cpp
src/mathed/InsetMathBoldSymbol.cpp
src/mathed/InsetMathBox.cpp
src/mathed/InsetMathCases.cpp
src/mathed/InsetMathDecoration.cpp
src/mathed/InsetMathDelim.cpp
src/mathed/InsetMathEnv.cpp
src/mathed/InsetMathFrac.cpp
src/mathed/InsetMathGrid.cpp
src/mathed/InsetMathNest.cpp
src/mathed/InsetMathOverset.cpp
src/mathed/InsetMathPhantom.cpp
src/mathed/InsetMathRoot.cpp
src/mathed/InsetMathScript.cpp
src/mathed/InsetMathSize.cpp
src/mathed/InsetMathSpace.cpp
src/mathed/InsetMathSplit.cpp
src/mathed/InsetMathSqrt.cpp
src/mathed/InsetMathStackrel.cpp
src/mathed/InsetMathSubstack.cpp
src/mathed/InsetMathSymbol.cpp
src/mathed/InsetMathTabular.cpp
src/mathed/InsetMathUnderset.cpp
src/mathed/InsetMathXArrow.cpp
src/mathed/InsetMathXYArrow.cpp
src/mathed/InsetMathXYMatrix.cpp
src/mathed/MathFactory.cpp
src/mathed/MathMacro.cpp
src/mathed/MathStream.cpp
src/mathed/MathStream.h

index 3ad90d1eb88f49109ce00674f53eb24d82c7a023..bd1a1b2dd78878f57d89359507bfe5e1dc36bfc3 100644 (file)
@@ -65,12 +65,11 @@ void CommandInset::draw(PainterInfo & pi, int x, int y) const
 
 void CommandInset::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os, needs_math_mode_);
+       MathEnsurer ensurer(os, needs_math_mode_);
        os << '\\' << name_.c_str();
        if (cell(1).size())
                os << '[' << cell(1) << ']';
        os << '{' << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 0674d97382b58ffd19dc2f824518f12cd8369a05..fd8994652dcc6d609d6e37db64cda61cb35df4e2 100644 (file)
@@ -68,27 +68,13 @@ void InsetMath::drawT(TextPainter &, int, int) const
 
 void InsetMath::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        docstring const s = name();
        os << "\\" << s;
        // We need an extra ' ' unless this is a single-char-non-ASCII name
        // or anything non-ASCII follows
        if (s.size() != 1 || isAlphaASCII(s[0]))
                os.pendingSpace(true);
-       os.pendingBrace(brace);
-}
-
-
-bool InsetMath::ensureMath(WriteStream & os, bool needs_math_mode) const
-{
-       bool brace = os.pendingBrace();
-       os.pendingBrace(false);
-       if (os.latex() && os.textMode() && needs_math_mode) {
-               os << "\\ensuremath{";
-               os.textMode(false);
-               brace = true;
-       }
-       return brace;
 }
 
 
index aae7d7474e4eba0e6dc89e766a068954e5a341cb..2f91e9ede5e6f872d3b8fd753fc8b56205ebfed8 100644 (file)
@@ -181,9 +181,6 @@ public:
        /// write content as something readable by Octave
        virtual void octave(OctaveStream &) const;
 
-       /// ensure math mode when writing LaTeX
-       bool ensureMath(WriteStream & os, bool needs_math_mode = true) const;
-
        /// plain text output in ucs4 encoding
        int plaintext(odocstream &, OutputParams const &) const;
 
index 16ddb91f3c6c450d464d036aa249918a7518d86e..173c9bbda433347d8861fcc953842751115ed7d4 100644 (file)
@@ -122,11 +122,10 @@ bool InsetMathAMSArray::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetMathAMSArray::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\begin{" << name_ << '}';
        InsetMathGrid::write(os);
        os << "\\end{" << name_ << '}';
-       os.pendingBrace(brace);
 }
 
 
index 7b7d01dea9e5c0e75963c576dfac4aa695967dc6..d86040411093ed3a7e8bd212fa7d7f0a23592ff8 100644 (file)
@@ -95,7 +95,7 @@ void InsetMathArray::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathArray::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
 
        if (os.fragile())
                os << "\\protect";
@@ -113,8 +113,6 @@ void InsetMathArray::write(WriteStream & os) const
        os << "\\end{" << name_ << '}';
        // adding a \n here is bad if the array is the last item
        // in an \eqnarray...
-
-       os.pendingBrace(brace);
 }
 
 
index d0d3b957d5f47b44edbe8b735ee6a956354e2d33..5d9f58ac4f319193d8e69a08f1bf86e57b0afe7a 100644 (file)
@@ -89,11 +89,10 @@ void InsetMathBig::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathBig::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << '\\' << name_ << delim_;
        if (delim_[0] == '\\')
                os.pendingSpace(true);
-       os.pendingBrace(brace);
 }
 
 
index d3633efc8c3dd15fa62b8ba47273ec4a3f249a3c..730185f919de386c6e49bc7d9010e8ea9b65d5e1 100644 (file)
@@ -76,7 +76,7 @@ void InsetMathBoldSymbol::validate(LaTeXFeatures & features) const
 
 void InsetMathBoldSymbol::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        switch (kind_) {
        case AMS_BOLD:
                os << "\\boldsymbol{" << cell(0) << "}";
@@ -88,7 +88,6 @@ void InsetMathBoldSymbol::write(WriteStream & os) const
                os << "\\hm{" << cell(0) << "}";
                break;
        }
-       os.pendingBrace(brace);
 }
 
 
index 46544d296dae58b3e749e4f00b93b1ba4f388cee..603ea05b380dae32128e51a5fd4a8abaf35df39f 100644 (file)
@@ -39,10 +39,8 @@ InsetMathBox::InsetMathBox(docstring const & name)
 
 void InsetMathBox::write(WriteStream & os) const
 {
-       bool textmode = os.textMode();
-       os.textMode(true);
+       ModeSpecifier specifier(os, TEXT_MODE);
        os << '\\' << name_ << '{' << cell(0) << '}';
-       os.textMode(textmode);
 }
 
 
@@ -118,10 +116,8 @@ void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathFBox::write(WriteStream & os) const
 {
-       bool textmode = os.textMode();
-       os.textMode(true);
+       ModeSpecifier specifier(os, TEXT_MODE);
        os << "\\fbox{" << cell(0) << '}';
-       os.textMode(textmode);
 }
 
 
@@ -217,8 +213,7 @@ void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathMakebox::write(WriteStream & os) const
 {
-       bool textmode = os.textMode();
-       os.textMode(true);
+       ModeSpecifier specifier(os, TEXT_MODE);
        os << (framebox_ ? "\\framebox" : "\\makebox");
        if (cell(0).size() || !os.latex()) {
                os << '[' << cell(0) << ']';
@@ -226,7 +221,6 @@ void InsetMathMakebox::write(WriteStream & os) const
                        os << '[' << cell(1) << ']';
        }
        os << '{' << cell(2) << '}';
-       os.textMode(textmode);
 }
 
 
@@ -275,10 +269,8 @@ void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathBoxed::write(WriteStream & os) const
 {
-       bool textmode = os.textMode();
-       os.textMode(false);
+       ModeSpecifier specifier(os, MATH_MODE);
        os << "\\boxed{" << cell(0) << '}';
-       os.textMode(textmode);
 }
 
 
index 6f1444c3ad23808bb9728356007477998730c803..15f7c0e43cd03cbf8767610b89f1e88f561b0dea 100644 (file)
@@ -107,7 +107,7 @@ bool InsetMathCases::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetMathCases::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        if (os.fragile())
                os << "\\protect";
        os << "\\begin{cases}\n";
@@ -115,7 +115,6 @@ void InsetMathCases::write(WriteStream & os) const
        if (os.fragile())
                os << "\\protect";
        os << "\\end{cases}";
-       os.pendingBrace(brace);
 }
 
 
index 17aaaf04d0d6a5e172825ca6af10caf9a8d47e14..7998373d22b733b5cbb3f76cc41e3207f128f8eb 100644 (file)
@@ -139,11 +139,10 @@ void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathDecoration::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        if (os.fragile() && protect())
                os << "\\protect";
        os << '\\' << key_->name << '{' << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index c345a03d4d9e0fd123b0d00eac7f6ad94188304a..9a1ca07f864bc1dd2bdb6f43074475ab668581ae 100644 (file)
@@ -59,10 +59,9 @@ Inset * InsetMathDelim::clone() const
 
 void InsetMathDelim::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\left" << convertDelimToLatexName(left_) << cell(0)
           << "\\right" << convertDelimToLatexName(right_);
-       os.pendingBrace(brace);
 }
 
 
index 939b0765aadebe8976c8c149888dad24583c1fe1..3daba8179de46d9030624ae1fe26fcba5a299d1d 100644 (file)
@@ -48,9 +48,8 @@ void InsetMathEnv::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathEnv::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\begin{" << name_ << '}' << cell(0) << "\\end{" << name_ << '}';
-       os.pendingBrace(brace);
 }
 
 
index 456faa792ac26eb31342070480b69508288d8f78..07e1c80502782e3058e36a97578423bd0f6b5a75 100644 (file)
@@ -265,8 +265,7 @@ void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 
 void InsetMathFrac::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
-
+       MathEnsurer ensurer(os);
        switch (kind_) {
        case ATOP:
                os << '{' << cell(0) << "\\atop " << cell(1) << '}';
@@ -290,8 +289,6 @@ void InsetMathFrac::write(WriteStream & os) const
                        os << "\\unit{" << cell(0) << '}';
                break;
        }
-
-       os.pendingBrace(brace);
 }
 
 
@@ -539,8 +536,7 @@ bool InsetMathBinom::extraBraces() const
 
 void InsetMathBinom::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
-
+       MathEnsurer ensurer(os);
        switch (kind_) {
        case BINOM:
                os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
@@ -555,8 +551,6 @@ void InsetMathBinom::write(WriteStream & os) const
                os << '{' << cell(0) << " \\brack " << cell(1) << '}';
                break;
        }
-
-       os.pendingBrace(brace);
 }
 
 
index 1e4f2e8dff13a123a49db640cade9202f532c83a..21365b0f8bfe6a847aaed3b7ba8a56bc6f4f0201 100644 (file)
@@ -967,9 +967,7 @@ void InsetMathGrid::mathmlize(MathStream & os) const
 
 void InsetMathGrid::write(WriteStream & os) const
 {
-       bool brace = os.pendingBrace();
-       os.pendingBrace(false);
-
+       MathEnsurer ensurer(os, false);
        docstring eol;
        for (row_type row = 0; row < nrows(); ++row) {
                os << verboseHLine(rowinfo_[row].lines_);
@@ -983,11 +981,8 @@ void InsetMathGrid::write(WriteStream & os) const
                        }
                for (col_type col = 0; col < lastcol; ++col) {
                        os << cell(index(row, col));
-                       if (os.pendingBrace()) {
-                               os.pendingBrace(false);
-                               os.textMode(true);
-                               os << '}';
-                       }
+                       if (os.pendingBrace())
+                               ModeSpecifier specifier(os, TEXT_MODE);
                        os << eocString(col, lastcol);
                }
                eol = eolString(row, emptyline, os.fragile());
@@ -1006,8 +1001,6 @@ void InsetMathGrid::write(WriteStream & os) const
                }
                os << s;
        }
-
-       os.pendingBrace(brace);
 }
 
 
index 372c30786a9fa182ddbaf76449f063305be58048..85d0be52b8435f39b4ed592e6f6f50f01b1c6701 100644 (file)
@@ -336,8 +336,7 @@ MathData InsetMathNest::glue() const
 
 void InsetMathNest::write(WriteStream & os) const
 {
-       bool textmode = os.textMode();
-       os.textMode(currentMode() == TEXT_MODE);
+       ModeSpecifier specifier(os, currentMode());
        docstring const latex_name = name().c_str();
        os << '\\' << latex_name;
        for (size_t i = 0; i < nargs(); ++i)
@@ -348,7 +347,6 @@ void InsetMathNest::write(WriteStream & os) const
                os << "\\lyxlock";
                os.pendingSpace(true);
        }
-       os.textMode(textmode);
 }
 
 
index 114ed0ebfc9f2f99e710d0faef240dc7fc620b69..f443921d60211794c16420f1585786fb43593d44 100644 (file)
@@ -73,9 +73,8 @@ bool InsetMathOverset::idxLast(Cursor & cur) const
 
 void InsetMathOverset::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\overset{" << cell(0) << "}{" << cell(1) << '}';
-       os.pendingBrace(brace);
 }
 
 
index e4777e3953c793771c8d17cb17b2e3433335400f..f070e5505350041ab7e5b6681593a0616cc148fa 100644 (file)
@@ -119,7 +119,7 @@ void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathPhantom::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        switch (kind_) {
        case phantom:
                os << "\\phantom{";
@@ -132,7 +132,6 @@ void InsetMathPhantom::write(WriteStream & os) const
                break;
        }
        os << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 0102ebd08ddcda61dc4a36e7df5762c2de3ae6cc..95ad1a3e06b678070ba04732ebf85cb1a6e55912 100644 (file)
@@ -72,9 +72,8 @@ void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathRoot::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 25830420ec3374a258f1853490820afe43a7d695..5c4fcbfb3ed9919215e9dd56b1a72298b70aa9a5 100644 (file)
@@ -525,7 +525,7 @@ bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
 
 void InsetMathScript::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
 
        if (nuc().size()) {
                os << nuc();
@@ -550,8 +550,6 @@ void InsetMathScript::write(WriteStream & os) const
 
        if (lock_ && !os.latex())
                os << "\\lyxlock ";
-
-       os.pendingBrace(brace);
 }
 
 
index 1ad777d9a622b2b4a5aa9a04c75095f8eb28f3c4..4e6e3e9796e2487c8ee8f46bc3c2c75788f03600 100644 (file)
@@ -52,9 +52,8 @@ void InsetMathSize::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathSize::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "{\\" << key_->name << ' ' << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 4aa9ed58c1d025a70f8f1a5b8f9077ae4a47eb11..b9286087cf26599187d568adf3b50a72906f3dd5 100644 (file)
@@ -147,10 +147,9 @@ void InsetMathSpace::normalize(NormalStream & os) const
 void InsetMathSpace::write(WriteStream & os) const
 {
        if (space_ >= 0 && space_ < nSpace) {
-               bool brace = ensureMath(os);
+               MathEnsurer ensurer(os);
                os << '\\' << latex_mathspace[space_];
                os.pendingSpace(true);
-               os.pendingBrace(brace);
        }
 }
 
index a574e172e826b9502ccaa06a8e8e0ee276a32746..4420e9e5b56fdffdb645bbca81357cee08ed91ed 100644 (file)
@@ -87,7 +87,7 @@ bool InsetMathSplit::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetMathSplit::write(WriteStream & ws) const
 {
-       bool brace = ensureMath(ws);
+       MathEnsurer ensurer(ws);
        if (ws.fragile())
                ws << "\\protect";
        ws << "\\begin{" << name_ << '}';
@@ -99,7 +99,6 @@ void InsetMathSplit::write(WriteStream & ws) const
        if (ws.fragile())
                ws << "\\protect";
        ws << "\\end{" << name_ << "}\n";
-       ws.pendingBrace(brace);
 }
 
 
index 14f0600498ca7f3bacf31679896d6470a11b8cf1..ab63ac1e447ec4f984464c063bf45caf1e9c7d74 100644 (file)
@@ -80,9 +80,8 @@ void InsetMathSqrt::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 
 void InsetMathSqrt::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\sqrt{" << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 87e594e2b4cd1c51ffa1081870b278fd1b76dcf5..964c078fa9b3ee4b5613b3f8734e036a407b6b0e 100644 (file)
@@ -58,9 +58,8 @@ void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathStackrel::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\stackrel{" << cell(0) << "}{" << cell(1) << '}';
-       os.pendingBrace(brace);
 }
 
 
index 1fa93585c7d0a0c3d1040ad66908bd2d93b0f88c..059e02955149ba69177645ecc1977db4881da83a 100644 (file)
@@ -89,11 +89,10 @@ void InsetMathSubstack::infoize(odocstream & os) const
 
 void InsetMathSubstack::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\substack{";
        InsetMathGrid::write(os);
        os << "}\n";
-       os.pendingBrace(brace);
 }
 
 
index 7fff4a3e72031a18971e6f65bc3f1fd763289455..b152388ee075d963299100fdc8ba5d3613ed0a04 100644 (file)
@@ -206,9 +206,8 @@ void InsetMathSymbol::octave(OctaveStream & os) const
 
 void InsetMathSymbol::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << '\\' << name();
-       os.pendingBrace(brace);
 
        // $,#, etc. In theory the restriction based on catcodes, but then
        // we do not handle catcodes very well, let alone cat code changes,
index 8d59cbd1df4e6bf241e8374bd082825ae2932f9e..099089169908b92a318d66a24fb2375f9c7c5725 100644 (file)
@@ -65,14 +65,7 @@ void InsetMathTabular::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathTabular::write(WriteStream & os) const
 {
-       // This is a text mode tabular, so close any previous \ensuremath
-       // command and set the proper mode.
-       if (os.latex() && os.pendingBrace()) {
-               os.pendingBrace(false);
-               os << '}';
-       }
-       bool textmode = os.textMode();
-       os.textMode(true);
+       ModeSpecifier specifier(os, TEXT_MODE);
 
        if (os.fragile())
                os << "\\protect";
@@ -90,8 +83,6 @@ void InsetMathTabular::write(WriteStream & os) const
        os << "\\end{" << name_ << '}';
        // adding a \n here is bad if the tabular is the last item
        // in an \eqnarray...
-
-       os.textMode(textmode);
 }
 
 
index eb0f63b2eb83962320b13b27bcc0c2c7574cc57b..bb7212775b6ef40b96c5b33323750964ee276ea3 100644 (file)
@@ -84,9 +84,8 @@ bool InsetMathUnderset::idxUpDown(Cursor & cur, bool up) const
 
 void InsetMathUnderset::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\underset{" << cell(0) << "}{" << cell(1) << '}';
-       os.pendingBrace(brace);
 }
 
 
index ac4cea005cdb6ed99e810d4707e0907e915c8f17..eda56040dd63b91b98aedd5a6249896f584e652f 100644 (file)
@@ -62,12 +62,11 @@ void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathXArrow::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << '\\' << name_;
        if (cell(1).size())
                os << '[' << cell(1) << ']';
        os << '{' << cell(0) << '}';
-       os.pendingBrace(brace);
 }
 
 
index e9d933285733c109164de39877273551f4d93354..c51a26dcc2b1934c21bd3ed7715fd9d960d756ee 100644 (file)
@@ -140,14 +140,13 @@ void InsetMathXYArrow::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathXYArrow::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\ar";
        if (cell(0).size())
                os << '[' << cell(0) << ']';
        if (cell(1).size())
                os << (up_ ? '^' : '_') << '{' << cell(1) << '}';
        os << " ";
-       os.pendingBrace(brace);
 }
 
 
index 4bd3b0e4e8ff69da5e228d9b404a76710be50179..79a5a76551049673f4284ee85ca50e173b1a98d6 100644 (file)
@@ -53,7 +53,7 @@ void InsetMathXYMatrix::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void InsetMathXYMatrix::write(WriteStream & os) const
 {
-       bool brace = ensureMath(os);
+       MathEnsurer ensurer(os);
        os << "\\xymatrix";
        switch (spacing_code_) {
        case 'R':
@@ -72,7 +72,6 @@ void InsetMathXYMatrix::write(WriteStream & os) const
        os << '{';
        InsetMathGrid::write(os);
        os << "}\n";
-       os.pendingBrace(brace);
 }
 
 
index 284faadaaf8f54b279bd4e3a045172c030520350..13159b7b43dffa5150e2ce4eb90ea42c4d0f20f3 100644 (file)
@@ -45,6 +45,7 @@
 #include "MathMacro.h"
 #include "MathMacroArgument.h"
 #include "MathParser.h"
+#include "MathStream.h"
 #include "MathSupport.h"
 
 #include "insets/InsetCommand.h"
@@ -235,6 +236,43 @@ void initMath()
 }
 
 
+bool ensureMath(WriteStream & os, bool needs_math_mode, bool macro)
+{
+       bool brace = os.pendingBrace();
+       os.pendingBrace(false);
+       if (!os.latex())
+               return brace;
+       if (os.textMode() && needs_math_mode) {
+               os << "\\ensuremath{";
+               os.textMode(false);
+               brace = true;
+       } else if (macro && brace && !needs_math_mode) {
+               // This is a user defined macro, but not a MathMacro, so we
+               // cannot be sure what mode is needed. As it was entered in
+               // a text box, we restore the text mode.
+               os << '}';
+               os.textMode(true);
+               brace = false;
+       }
+       return brace;
+}
+
+
+bool ensureMode(WriteStream & os, InsetMath::mode_type mode)
+{
+       bool textmode = mode == InsetMath::TEXT_MODE;
+       if (os.latex() && textmode && os.pendingBrace()) {
+               os.os() << '}';
+               os.pendingBrace(false);
+               os.pendingSpace(false);
+               os.textMode(true);
+       }
+       bool oldmode = os.textMode();
+       os.textMode(textmode);
+       return oldmode;
+}
+
+
 latexkeys const * in_word_set(docstring const & str)
 {
        MathWordList::iterator it = theWordList.find(str);
index 339719483b89e533a83743e527165d4a5555ae7e..2c9aff1f0d6e3f03ab1f3a26a4cd3390a6f2842e 100644 (file)
@@ -655,27 +655,12 @@ bool MathMacro::folded() const
 
 void MathMacro::write(WriteStream & os) const
 {
-       bool brace = os.pendingBrace();
-        os.pendingBrace(false);
-       if (os.latex()) {
-               if (os.textMode() && macro_) {
-                       // This is for sure a math macro
-                       os << "\\ensuremath{";
-                       os.textMode(false);
-                       brace = true;
-               } else if (brace && !macro_) {
-                       // Cannot tell, so don't mess with the mode
-                       os << '}';
-                       os.textMode(true);
-                       brace = false;
-               }
-        }
+       MathEnsurer ensurer(os, macro_ != 0, true);
 
        // non-normal mode
        if (displayMode_ != DISPLAY_NORMAL) {
                os << "\\" << name();
                os.pendingSpace(true);
-               os.pendingBrace(brace);
                return;
        }
 
@@ -722,8 +707,6 @@ void MathMacro::write(WriteStream & os) const
        // add space if there was no argument
        if (first)
                os.pendingSpace(true);
-
-       os.pendingBrace(brace);
 }
 
 
index f9779d0d6f76a0b16be9acd29b5a45687d6ad426..b34fbab23589050a40afe8cacbb14eef80f414ee 100644 (file)
@@ -12,7 +12,6 @@
 
 #include "MathStream.h"
 
-#include "InsetMath.h"
 #include "MathData.h"
 #include "MathExtern.h"
 
index 3c12b0f55f7fb8daf8681bc1263a4e8cfb76aaf9..e388c93456fd94ab26793c70cbb184c1a1870a82 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "support/strfwd.h"
 
+#include "InsetMath.h"
 // FIXME: Move to individual insets
 #include "MetricsInfo.h"
 
@@ -98,6 +99,94 @@ WriteStream & operator<<(WriteStream &, int);
 ///
 WriteStream & operator<<(WriteStream &, unsigned int);
 
+/// ensure math mode, possibly by opening \ensuremath
+bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
+
+/// ensure the requested mode, possibly by closing \ensuremath
+bool ensureMode(WriteStream & os, InsetMath::mode_type mode);
+
+
+/**
+ * MathEnsurer - utility class for ensuring math mode
+ *
+ * A local variable of this type can be used to either ensure math mode
+ * or delay the writing of a pending brace when outputting LaTeX.
+ *
+ * Example 1:
+ *
+ *      MathEnsurer ensurer(os);
+ *
+ * If not already in math mode, inserts an \ensuremath command followed
+ * by an open brace. This brace will be automatically closed when exiting
+ * math mode. Math mode is automatically exited when writing something
+ * that doesn't explicitly require math mode.
+ *
+ * Example 2:
+ *
+ *      MathEnsurer ensurer(os, false);
+ *
+ * Simply suspend writing a closing brace until the end of ensurer's scope.
+ *
+ * Example 3:
+ *
+ *      MathEnsurer ensurer(os, needs_math_mode, true);
+ *
+ * The third parameter is set to true only for a user defined macro, which
+ * needs special handling. When it is a MathMacro, the needs_math_mode
+ * parameter is true and the behavior is as in Example 1. When the
+ * needs_math_mode parameter is false (not a MathMacro) and the macro
+ * was entered in a text box and we are in math mode, the mode is reset
+ * to text. This is because the macro was probably designed for text mode
+ * (given that it was entered in text mode and we have no way to tell the
+ * contrary).
+ */
+class MathEnsurer
+{
+public:
+       ///
+       explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
+               : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
+       ///
+       ~MathEnsurer() { os_.pendingBrace(brace_); }
+private:
+       ///
+       WriteStream & os_;
+       ///
+       bool brace_;
+};
+
+
+/**
+ * ModeSpecifier - utility class for specifying a given mode (math or text)
+ *
+ * A local variable of this type can be used to specify that a command or
+ * environment works in a given mode. For example, \mbox works in text
+ * mode, but \boxed works in math mode. Note that no mode changing commands
+ * are needed, but we have to track the current mode, hence this class.
+ *
+ * Example:
+ *
+ *      ModeSpecifier specifier(os, TEXT_MODE);
+ *
+ * Sets the current mode to text mode.
+ *
+ * At the end of specifier's scope the mode is reset to its previous value.
+ */
+class ModeSpecifier
+{
+public:
+       ///
+       explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode)
+               : os_(os), textmode_(ensureMode(os, mode)) {}
+       ///
+       ~ModeSpecifier() { os_.textMode(textmode_); }
+private:
+       ///
+       WriteStream & os_;
+       ///
+       bool textmode_;
+};
+
 
 
 //