From: Enrico Forestieri Date: Mon, 16 Jun 2008 14:32:51 +0000 (+0000) Subject: Use a helper method in order to avoid code repetition. X-Git-Tag: 1.6.10~4378 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=31713932b247802b5b59482f5157c8a4f7206825;p=features.git Use a helper method in order to avoid code repetition. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25273 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/mathed/CommandInset.cpp b/src/mathed/CommandInset.cpp index 9edf51b07a..3ad90d1eb8 100644 --- a/src/mathed/CommandInset.cpp +++ b/src/mathed/CommandInset.cpp @@ -65,12 +65,7 @@ void CommandInset::draw(PainterInfo & pi, int x, int y) const void CommandInset::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode() && needs_math_mode_) { - os << "\\ensuremath{"; - os.textMode(false); - } + bool brace = ensureMath(os, needs_math_mode_); os << '\\' << name_.c_str(); if (cell(1).size()) os << '[' << cell(1) << ']'; diff --git a/src/mathed/InsetMath.cpp b/src/mathed/InsetMath.cpp index dce8d59892..0674d97382 100644 --- a/src/mathed/InsetMath.cpp +++ b/src/mathed/InsetMath.cpp @@ -68,12 +68,7 @@ void InsetMath::drawT(TextPainter &, int, int) const void InsetMath::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - } + bool brace = ensureMath(os); docstring const s = name(); os << "\\" << s; // We need an extra ' ' unless this is a single-char-non-ASCII name @@ -84,6 +79,19 @@ void InsetMath::write(WriteStream & os) const } +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; +} + + int InsetMath::plaintext(odocstream &, OutputParams const &) const { // all math plain text output shall take place in InsetMathHull diff --git a/src/mathed/InsetMath.h b/src/mathed/InsetMath.h index 2f91e9ede5..aae7d7474e 100644 --- a/src/mathed/InsetMath.h +++ b/src/mathed/InsetMath.h @@ -181,6 +181,9 @@ 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 fc839088c3..16ddb91f3c 100644 --- a/src/mathed/InsetMathAMSArray.cpp +++ b/src/mathed/InsetMathAMSArray.cpp @@ -122,13 +122,7 @@ bool InsetMathAMSArray::getStatus(Cursor & cur, FuncRequest const & cmd, void InsetMathAMSArray::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\begin{" << name_ << '}'; InsetMathGrid::write(os); os << "\\end{" << name_ << '}'; diff --git a/src/mathed/InsetMathArray.cpp b/src/mathed/InsetMathArray.cpp index 0d9c7d5c87..7b7d01dea9 100644 --- a/src/mathed/InsetMathArray.cpp +++ b/src/mathed/InsetMathArray.cpp @@ -95,13 +95,7 @@ void InsetMathArray::draw(PainterInfo & pi, int x, int y) const void InsetMathArray::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); if (os.fragile()) os << "\\protect"; diff --git a/src/mathed/InsetMathBig.cpp b/src/mathed/InsetMathBig.cpp index 6e54064aca..d0d3b957d5 100644 --- a/src/mathed/InsetMathBig.cpp +++ b/src/mathed/InsetMathBig.cpp @@ -89,13 +89,7 @@ void InsetMathBig::draw(PainterInfo & pi, int x, int y) const void InsetMathBig::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << '\\' << name_ << delim_; if (delim_[0] == '\\') os.pendingSpace(true); diff --git a/src/mathed/InsetMathBoldSymbol.cpp b/src/mathed/InsetMathBoldSymbol.cpp index 3cca1180fb..d3633efc8c 100644 --- a/src/mathed/InsetMathBoldSymbol.cpp +++ b/src/mathed/InsetMathBoldSymbol.cpp @@ -76,13 +76,7 @@ void InsetMathBoldSymbol::validate(LaTeXFeatures & features) const void InsetMathBoldSymbol::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); switch (kind_) { case AMS_BOLD: os << "\\boldsymbol{" << cell(0) << "}"; diff --git a/src/mathed/InsetMathCases.cpp b/src/mathed/InsetMathCases.cpp index eb53248402..6f1444c3ad 100644 --- a/src/mathed/InsetMathCases.cpp +++ b/src/mathed/InsetMathCases.cpp @@ -107,12 +107,7 @@ bool InsetMathCases::getStatus(Cursor & cur, FuncRequest const & cmd, void InsetMathCases::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); if (os.fragile()) os << "\\protect"; os << "\\begin{cases}\n"; diff --git a/src/mathed/InsetMathDecoration.cpp b/src/mathed/InsetMathDecoration.cpp index d53efb0354..17aaaf04d0 100644 --- a/src/mathed/InsetMathDecoration.cpp +++ b/src/mathed/InsetMathDecoration.cpp @@ -139,13 +139,7 @@ void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const void InsetMathDecoration::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); if (os.fragile() && protect()) os << "\\protect"; os << '\\' << key_->name << '{' << cell(0) << '}'; diff --git a/src/mathed/InsetMathDelim.cpp b/src/mathed/InsetMathDelim.cpp index 9cf6414e29..c345a03d4d 100644 --- a/src/mathed/InsetMathDelim.cpp +++ b/src/mathed/InsetMathDelim.cpp @@ -59,17 +59,9 @@ Inset * InsetMathDelim::clone() const void InsetMathDelim::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } - + bool brace = ensureMath(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 f04d87119f..939b0765aa 100644 --- a/src/mathed/InsetMathEnv.cpp +++ b/src/mathed/InsetMathEnv.cpp @@ -48,13 +48,7 @@ void InsetMathEnv::draw(PainterInfo & pi, int x, int y) const void InsetMathEnv::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\begin{" << name_ << '}' << cell(0) << "\\end{" << name_ << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathFrac.cpp b/src/mathed/InsetMathFrac.cpp index 58e0c1b180..456faa792a 100644 --- a/src/mathed/InsetMathFrac.cpp +++ b/src/mathed/InsetMathFrac.cpp @@ -265,13 +265,7 @@ void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const void InsetMathFrac::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); switch (kind_) { case ATOP: @@ -545,13 +539,7 @@ bool InsetMathBinom::extraBraces() const void InsetMathBinom::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); switch (kind_) { case BINOM: diff --git a/src/mathed/InsetMathOverset.cpp b/src/mathed/InsetMathOverset.cpp index 5d8edefef8..114ed0ebfc 100644 --- a/src/mathed/InsetMathOverset.cpp +++ b/src/mathed/InsetMathOverset.cpp @@ -73,12 +73,7 @@ bool InsetMathOverset::idxLast(Cursor & cur) const void InsetMathOverset::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - } + bool brace = ensureMath(os); os << "\\overset{" << cell(0) << "}{" << cell(1) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathPhantom.cpp b/src/mathed/InsetMathPhantom.cpp index ba9b89a96a..e4777e3953 100644 --- a/src/mathed/InsetMathPhantom.cpp +++ b/src/mathed/InsetMathPhantom.cpp @@ -119,13 +119,7 @@ void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const void InsetMathPhantom::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); switch (kind_) { case phantom: os << "\\phantom{"; diff --git a/src/mathed/InsetMathRoot.cpp b/src/mathed/InsetMathRoot.cpp index 006c0716ae..0102ebd08d 100644 --- a/src/mathed/InsetMathRoot.cpp +++ b/src/mathed/InsetMathRoot.cpp @@ -72,13 +72,7 @@ void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const void InsetMathRoot::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathScript.cpp b/src/mathed/InsetMathScript.cpp index 6bf53dd799..25830420ec 100644 --- a/src/mathed/InsetMathScript.cpp +++ b/src/mathed/InsetMathScript.cpp @@ -525,13 +525,7 @@ bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const void InsetMathScript::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); if (nuc().size()) { os << nuc(); diff --git a/src/mathed/InsetMathSize.cpp b/src/mathed/InsetMathSize.cpp index 898a395139..1ad777d9a6 100644 --- a/src/mathed/InsetMathSize.cpp +++ b/src/mathed/InsetMathSize.cpp @@ -52,13 +52,7 @@ void InsetMathSize::draw(PainterInfo & pi, int x, int y) const void InsetMathSize::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "{\\" << key_->name << ' ' << cell(0) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathSpace.cpp b/src/mathed/InsetMathSpace.cpp index 60950c30ad..4aa9ed58c1 100644 --- a/src/mathed/InsetMathSpace.cpp +++ b/src/mathed/InsetMathSpace.cpp @@ -147,13 +147,7 @@ void InsetMathSpace::normalize(NormalStream & os) const void InsetMathSpace::write(WriteStream & os) const { if (space_ >= 0 && space_ < nSpace) { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << '\\' << latex_mathspace[space_]; os.pendingSpace(true); os.pendingBrace(brace); diff --git a/src/mathed/InsetMathSplit.cpp b/src/mathed/InsetMathSplit.cpp index 95fbc80f5f..a574e172e8 100644 --- a/src/mathed/InsetMathSplit.cpp +++ b/src/mathed/InsetMathSplit.cpp @@ -87,13 +87,7 @@ bool InsetMathSplit::getStatus(Cursor & cur, FuncRequest const & cmd, void InsetMathSplit::write(WriteStream & ws) const { - bool brace = ws.pendingBrace(); - ws.pendingBrace(false); - if (ws.latex() && ws.textMode()) { - ws << "\\ensuremath{"; - ws.textMode(false); - brace = true; - } + bool brace = ensureMath(ws); if (ws.fragile()) ws << "\\protect"; ws << "\\begin{" << name_ << '}'; diff --git a/src/mathed/InsetMathSqrt.cpp b/src/mathed/InsetMathSqrt.cpp index 11304a9bd1..14f0600498 100644 --- a/src/mathed/InsetMathSqrt.cpp +++ b/src/mathed/InsetMathSqrt.cpp @@ -80,13 +80,7 @@ void InsetMathSqrt::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const void InsetMathSqrt::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\sqrt{" << cell(0) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathStackrel.cpp b/src/mathed/InsetMathStackrel.cpp index 410e21f1d8..87e594e2b4 100644 --- a/src/mathed/InsetMathStackrel.cpp +++ b/src/mathed/InsetMathStackrel.cpp @@ -58,13 +58,7 @@ void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const void InsetMathStackrel::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\stackrel{" << cell(0) << "}{" << cell(1) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathSubstack.cpp b/src/mathed/InsetMathSubstack.cpp index e5abe9818a..1fa93585c7 100644 --- a/src/mathed/InsetMathSubstack.cpp +++ b/src/mathed/InsetMathSubstack.cpp @@ -89,13 +89,7 @@ void InsetMathSubstack::infoize(odocstream & os) const void InsetMathSubstack::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\substack{"; InsetMathGrid::write(os); os << "}\n"; diff --git a/src/mathed/InsetMathSymbol.cpp b/src/mathed/InsetMathSymbol.cpp index 28119d2a9b..7fff4a3e72 100644 --- a/src/mathed/InsetMathSymbol.cpp +++ b/src/mathed/InsetMathSymbol.cpp @@ -206,13 +206,7 @@ void InsetMathSymbol::octave(OctaveStream & os) const void InsetMathSymbol::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << '\\' << name(); os.pendingBrace(brace); diff --git a/src/mathed/InsetMathUnderset.cpp b/src/mathed/InsetMathUnderset.cpp index ae5ab36228..eb0f63b2eb 100644 --- a/src/mathed/InsetMathUnderset.cpp +++ b/src/mathed/InsetMathUnderset.cpp @@ -84,13 +84,7 @@ bool InsetMathUnderset::idxUpDown(Cursor & cur, bool up) const void InsetMathUnderset::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\underset{" << cell(0) << "}{" << cell(1) << '}'; os.pendingBrace(brace); } diff --git a/src/mathed/InsetMathXArrow.cpp b/src/mathed/InsetMathXArrow.cpp index 78d5390a3f..ac4cea005c 100644 --- a/src/mathed/InsetMathXArrow.cpp +++ b/src/mathed/InsetMathXArrow.cpp @@ -62,19 +62,11 @@ void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const void InsetMathXArrow::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } - + bool brace = ensureMath(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 866c3bb1d6..e9d9332857 100644 --- a/src/mathed/InsetMathXYArrow.cpp +++ b/src/mathed/InsetMathXYArrow.cpp @@ -140,13 +140,7 @@ void InsetMathXYArrow::draw(PainterInfo & pi, int x, int y) const void InsetMathXYArrow::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\ar"; if (cell(0).size()) os << '[' << cell(0) << ']'; diff --git a/src/mathed/InsetMathXYMatrix.cpp b/src/mathed/InsetMathXYMatrix.cpp index 9be4e2efa6..4bd3b0e4e8 100644 --- a/src/mathed/InsetMathXYMatrix.cpp +++ b/src/mathed/InsetMathXYMatrix.cpp @@ -53,13 +53,7 @@ void InsetMathXYMatrix::metrics(MetricsInfo & mi, Dimension & dim) const void InsetMathXYMatrix::write(WriteStream & os) const { - bool brace = os.pendingBrace(); - os.pendingBrace(false); - if (os.latex() && os.textMode()) { - os << "\\ensuremath{"; - os.textMode(false); - brace = true; - } + bool brace = ensureMath(os); os << "\\xymatrix"; switch (spacing_code_) { case 'R':