From e224ef029b58de61fead592a6134775dbd9a97b8 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Tue, 17 Jun 2008 11:10:43 +0000 Subject: [PATCH] Implement the suggestion by Andre' git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25287 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/CommandInset.cpp | 3 +- src/mathed/InsetMath.cpp | 16 +----- src/mathed/InsetMath.h | 3 - src/mathed/InsetMathAMSArray.cpp | 3 +- src/mathed/InsetMathArray.cpp | 4 +- src/mathed/InsetMathBig.cpp | 3 +- src/mathed/InsetMathBoldSymbol.cpp | 3 +- src/mathed/InsetMathBox.cpp | 16 ++---- src/mathed/InsetMathCases.cpp | 3 +- src/mathed/InsetMathDecoration.cpp | 3 +- src/mathed/InsetMathDelim.cpp | 3 +- src/mathed/InsetMathEnv.cpp | 3 +- src/mathed/InsetMathFrac.cpp | 10 +--- src/mathed/InsetMathGrid.cpp | 13 +---- src/mathed/InsetMathNest.cpp | 4 +- src/mathed/InsetMathOverset.cpp | 3 +- src/mathed/InsetMathPhantom.cpp | 3 +- src/mathed/InsetMathRoot.cpp | 3 +- src/mathed/InsetMathScript.cpp | 4 +- src/mathed/InsetMathSize.cpp | 3 +- src/mathed/InsetMathSpace.cpp | 3 +- src/mathed/InsetMathSplit.cpp | 3 +- src/mathed/InsetMathSqrt.cpp | 3 +- src/mathed/InsetMathStackrel.cpp | 3 +- src/mathed/InsetMathSubstack.cpp | 3 +- src/mathed/InsetMathSymbol.cpp | 3 +- src/mathed/InsetMathTabular.cpp | 11 +--- src/mathed/InsetMathUnderset.cpp | 3 +- src/mathed/InsetMathXArrow.cpp | 3 +- src/mathed/InsetMathXYArrow.cpp | 3 +- src/mathed/InsetMathXYMatrix.cpp | 3 +- src/mathed/MathFactory.cpp | 38 +++++++++++++ src/mathed/MathMacro.cpp | 19 +------ src/mathed/MathStream.cpp | 1 - src/mathed/MathStream.h | 89 ++++++++++++++++++++++++++++++ 35 files changed, 164 insertions(+), 130 deletions(-) diff --git a/src/mathed/CommandInset.cpp b/src/mathed/CommandInset.cpp index 3ad90d1eb8..bd1a1b2dd7 100644 --- a/src/mathed/CommandInset.cpp +++ b/src/mathed/CommandInset.cpp @@ -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); } diff --git a/src/mathed/InsetMath.cpp b/src/mathed/InsetMath.cpp index 0674d97382..fd8994652d 100644 --- a/src/mathed/InsetMath.cpp +++ b/src/mathed/InsetMath.cpp @@ -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; } diff --git a/src/mathed/InsetMath.h b/src/mathed/InsetMath.h index aae7d7474e..2f91e9ede5 100644 --- a/src/mathed/InsetMath.h +++ b/src/mathed/InsetMath.h @@ -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; diff --git a/src/mathed/InsetMathAMSArray.cpp b/src/mathed/InsetMathAMSArray.cpp index 16ddb91f3c..173c9bbda4 100644 --- a/src/mathed/InsetMathAMSArray.cpp +++ b/src/mathed/InsetMathAMSArray.cpp @@ -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); } diff --git a/src/mathed/InsetMathArray.cpp b/src/mathed/InsetMathArray.cpp index 7b7d01dea9..d860404110 100644 --- a/src/mathed/InsetMathArray.cpp +++ b/src/mathed/InsetMathArray.cpp @@ -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); } diff --git a/src/mathed/InsetMathBig.cpp b/src/mathed/InsetMathBig.cpp index d0d3b957d5..5d9f58ac4f 100644 --- a/src/mathed/InsetMathBig.cpp +++ b/src/mathed/InsetMathBig.cpp @@ -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); } diff --git a/src/mathed/InsetMathBoldSymbol.cpp b/src/mathed/InsetMathBoldSymbol.cpp index d3633efc8c..730185f919 100644 --- a/src/mathed/InsetMathBoldSymbol.cpp +++ b/src/mathed/InsetMathBoldSymbol.cpp @@ -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); } diff --git a/src/mathed/InsetMathBox.cpp b/src/mathed/InsetMathBox.cpp index 46544d296d..603ea05b38 100644 --- a/src/mathed/InsetMathBox.cpp +++ b/src/mathed/InsetMathBox.cpp @@ -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); } diff --git a/src/mathed/InsetMathCases.cpp b/src/mathed/InsetMathCases.cpp index 6f1444c3ad..15f7c0e43c 100644 --- a/src/mathed/InsetMathCases.cpp +++ b/src/mathed/InsetMathCases.cpp @@ -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); } diff --git a/src/mathed/InsetMathDecoration.cpp b/src/mathed/InsetMathDecoration.cpp index 17aaaf04d0..7998373d22 100644 --- a/src/mathed/InsetMathDecoration.cpp +++ b/src/mathed/InsetMathDecoration.cpp @@ -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); } diff --git a/src/mathed/InsetMathDelim.cpp b/src/mathed/InsetMathDelim.cpp index c345a03d4d..9a1ca07f86 100644 --- a/src/mathed/InsetMathDelim.cpp +++ b/src/mathed/InsetMathDelim.cpp @@ -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); } diff --git a/src/mathed/InsetMathEnv.cpp b/src/mathed/InsetMathEnv.cpp index 939b0765aa..3daba8179d 100644 --- a/src/mathed/InsetMathEnv.cpp +++ b/src/mathed/InsetMathEnv.cpp @@ -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); } diff --git a/src/mathed/InsetMathFrac.cpp b/src/mathed/InsetMathFrac.cpp index 456faa792a..07e1c80502 100644 --- a/src/mathed/InsetMathFrac.cpp +++ b/src/mathed/InsetMathFrac.cpp @@ -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); } diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp index 1e4f2e8dff..21365b0f8b 100644 --- a/src/mathed/InsetMathGrid.cpp +++ b/src/mathed/InsetMathGrid.cpp @@ -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); } diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 372c30786a..85d0be52b8 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -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); } diff --git a/src/mathed/InsetMathOverset.cpp b/src/mathed/InsetMathOverset.cpp index 114ed0ebfc..f443921d60 100644 --- a/src/mathed/InsetMathOverset.cpp +++ b/src/mathed/InsetMathOverset.cpp @@ -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); } diff --git a/src/mathed/InsetMathPhantom.cpp b/src/mathed/InsetMathPhantom.cpp index e4777e3953..f070e55053 100644 --- a/src/mathed/InsetMathPhantom.cpp +++ b/src/mathed/InsetMathPhantom.cpp @@ -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); } diff --git a/src/mathed/InsetMathRoot.cpp b/src/mathed/InsetMathRoot.cpp index 0102ebd08d..95ad1a3e06 100644 --- a/src/mathed/InsetMathRoot.cpp +++ b/src/mathed/InsetMathRoot.cpp @@ -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); } diff --git a/src/mathed/InsetMathScript.cpp b/src/mathed/InsetMathScript.cpp index 25830420ec..5c4fcbfb3e 100644 --- a/src/mathed/InsetMathScript.cpp +++ b/src/mathed/InsetMathScript.cpp @@ -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); } diff --git a/src/mathed/InsetMathSize.cpp b/src/mathed/InsetMathSize.cpp index 1ad777d9a6..4e6e3e9796 100644 --- a/src/mathed/InsetMathSize.cpp +++ b/src/mathed/InsetMathSize.cpp @@ -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); } diff --git a/src/mathed/InsetMathSpace.cpp b/src/mathed/InsetMathSpace.cpp index 4aa9ed58c1..b9286087cf 100644 --- a/src/mathed/InsetMathSpace.cpp +++ b/src/mathed/InsetMathSpace.cpp @@ -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); } } diff --git a/src/mathed/InsetMathSplit.cpp b/src/mathed/InsetMathSplit.cpp index a574e172e8..4420e9e5b5 100644 --- a/src/mathed/InsetMathSplit.cpp +++ b/src/mathed/InsetMathSplit.cpp @@ -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); } diff --git a/src/mathed/InsetMathSqrt.cpp b/src/mathed/InsetMathSqrt.cpp index 14f0600498..ab63ac1e44 100644 --- a/src/mathed/InsetMathSqrt.cpp +++ b/src/mathed/InsetMathSqrt.cpp @@ -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); } diff --git a/src/mathed/InsetMathStackrel.cpp b/src/mathed/InsetMathStackrel.cpp index 87e594e2b4..964c078fa9 100644 --- a/src/mathed/InsetMathStackrel.cpp +++ b/src/mathed/InsetMathStackrel.cpp @@ -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); } diff --git a/src/mathed/InsetMathSubstack.cpp b/src/mathed/InsetMathSubstack.cpp index 1fa93585c7..059e029551 100644 --- a/src/mathed/InsetMathSubstack.cpp +++ b/src/mathed/InsetMathSubstack.cpp @@ -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); } diff --git a/src/mathed/InsetMathSymbol.cpp b/src/mathed/InsetMathSymbol.cpp index 7fff4a3e72..b152388ee0 100644 --- a/src/mathed/InsetMathSymbol.cpp +++ b/src/mathed/InsetMathSymbol.cpp @@ -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, diff --git a/src/mathed/InsetMathTabular.cpp b/src/mathed/InsetMathTabular.cpp index 8d59cbd1df..0990891699 100644 --- a/src/mathed/InsetMathTabular.cpp +++ b/src/mathed/InsetMathTabular.cpp @@ -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); } diff --git a/src/mathed/InsetMathUnderset.cpp b/src/mathed/InsetMathUnderset.cpp index eb0f63b2eb..bb7212775b 100644 --- a/src/mathed/InsetMathUnderset.cpp +++ b/src/mathed/InsetMathUnderset.cpp @@ -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); } diff --git a/src/mathed/InsetMathXArrow.cpp b/src/mathed/InsetMathXArrow.cpp index ac4cea005c..eda56040dd 100644 --- a/src/mathed/InsetMathXArrow.cpp +++ b/src/mathed/InsetMathXArrow.cpp @@ -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); } diff --git a/src/mathed/InsetMathXYArrow.cpp b/src/mathed/InsetMathXYArrow.cpp index e9d9332857..c51a26dcc2 100644 --- a/src/mathed/InsetMathXYArrow.cpp +++ b/src/mathed/InsetMathXYArrow.cpp @@ -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); } diff --git a/src/mathed/InsetMathXYMatrix.cpp b/src/mathed/InsetMathXYMatrix.cpp index 4bd3b0e4e8..79a5a76551 100644 --- a/src/mathed/InsetMathXYMatrix.cpp +++ b/src/mathed/InsetMathXYMatrix.cpp @@ -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); } diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 284faadaaf..13159b7b43 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -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); diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp index 339719483b..2c9aff1f0d 100644 --- a/src/mathed/MathMacro.cpp +++ b/src/mathed/MathMacro.cpp @@ -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); } diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index f9779d0d6f..b34fbab235 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -12,7 +12,6 @@ #include "MathStream.h" -#include "InsetMath.h" #include "MathData.h" #include "MathExtern.h" diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index 3c12b0f55f..e388c93456 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -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_; +}; + // -- 2.39.2