From d1633491dbef2da9d2a6252be65f01b7b0b1740a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 24 Oct 2001 16:10:38 +0000 Subject: [PATCH] add missing writeNormal() methods to some insets git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2935 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/array.C | 15 +++-- src/mathed/formula.C | 117 ++++++++++++---------------------- src/mathed/math_charinset.C | 2 +- src/mathed/math_cursor.C | 3 +- src/mathed/math_deliminset.C | 6 ++ src/mathed/math_deliminset.h | 2 + src/mathed/math_rootinset.C | 2 +- src/mathed/math_scriptinset.C | 43 ++++++++++++- src/mathed/math_scriptinset.h | 4 ++ src/mathed/math_sqrtinset.C | 2 +- 10 files changed, 110 insertions(+), 86 deletions(-) diff --git a/src/mathed/array.C b/src/mathed/array.C index 11042bf333..3c02985790 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -201,10 +201,17 @@ void MathArray::write(MathWriteInfo & wi) const void MathArray::writeNormal(ostream & os) const { - os << "[par "; - for (const_iterator it = begin(); it != end(); ++it) - (*it)->writeNormal(os); - os << "]"; + for (const_iterator it = begin(); it != end(); ++it) { + MathInset * p = it->nucleus(); + if (!p) + continue; + if (MathScriptInset const * q = asScript(it)) { + q->writeNormal(p, os); + ++it; + } else { + p->writeNormal(os); + } + } } diff --git a/src/mathed/formula.C b/src/mathed/formula.C index ce31858fa8..02ab30e6f6 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -18,7 +18,6 @@ #endif #include -#include #include "formula.h" #include "commandtags.h" @@ -35,6 +34,7 @@ #include "support/lyxlib.h" #include "support/syscall.h" #include "support/lstrings.h" +#include "support/filetools.h" // LibFileSearch #include "LyXView.h" #include "Painter.h" #include "lyxrc.h" @@ -63,6 +63,40 @@ namespace { ar.erase(pos, ar.size()); } + + MathArray pipeThroughExtern(string const & arg, MathArray const & ar) + { + string lang; + string extra; + istringstream iss(arg.c_str()); + iss >> lang >> extra; + if (extra.empty()) + extra = "noextra"; + + // create normalized expression + string outfile = lyx::tempName(string(), "mathextern"); + ostringstream os; + os << "[" << extra << ' '; + ar.writeNormal(os); + os << "]"; + string code = os.str().c_str(); + + // run external sript + string file = LibFileSearch(string(), "lyx2" + lang); + if (file.empty()) { + lyxerr << "converter to '" << lang << "' not found\n"; + return MathArray(); + } + string script = file + " '" + code + "' " + outfile; + lyxerr << "calling: " << script << endl; + Systemcalls cmd(Systemcalls::System, script, 0); + + // append result + MathArray res; + mathed_parse_cell(res, GetFileContents(outfile)); + return res; + } + } @@ -317,49 +351,6 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, return result; } -#if 0 -void InsetFormula::handleExtern(const string & arg) -{ - // where are we? - if (!mathcursor) - return; - - MathArray & ar = mathcursor->cursor().cell(); - - // parse args - string lang; - string extra; - istringstream iss(arg.c_str()); - iss >> lang >> extra; - if (extra.empty()) - extra = "noextra"; - - // strip last '=' and everything behind - stripFromLastEqualSign(ar); - - // create normalized expression - //string outfile = lyx::tempName("maple.out"); - string outfile = "/tmp/lyx2" + lang + ".out"; - ostringstream os; - os << "[" << extra << ' '; - ar.writeNormal(os); - os << "]"; - string code = os.str().c_str(); - - // run external sript - string script = "lyx2" + arg + " '" + code + "' " + outfile; - lyxerr << "calling: " << script << endl; - Systemcalls cmd(Systemcalls::System, script, 0); - - // append a '=' - ar.push_back(MathAtom(new MathCharInset('='))); - - // append result - ifstream is(outfile.c_str()); - mathed_parse_cell(ar, is); - mathcursor->end(); -} -#endif void InsetFormula::handleExtern(const string & arg) { @@ -374,45 +365,17 @@ void InsetFormula::handleExtern(const string & arg) mathcursor->selGet(ar); lyxerr << "use selection: " << ar << "\n"; } else { + mathcursor->end(); ar = mathcursor->cursor().cell(); + stripFromLastEqualSign(ar); + mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR))); lyxerr << "use whole cell: " << ar << "\n"; } - - // parse args - string lang; - string extra; - istringstream iss(arg.c_str()); - iss >> lang >> extra; - if (extra.empty()) - extra = "noextra"; - - // strip last '=' and everything behind - stripFromLastEqualSign(ar); - - // create normalized expression - //string outfile = lyx::tempName("maple.out"); - string outfile = "/tmp/lyx2" + lang + ".out"; - ostringstream os; - os << "[" << extra << ' '; - ar.writeNormal(os); - os << "]"; - string code = os.str().c_str(); - - // run external sript - string script = "lyx2" + lang + " '" + code + "' " + outfile; - lyxerr << "calling: " << script << endl; - Systemcalls cmd(Systemcalls::System, script, 0); - - // append result - MathArray br; - if (selected) - br.push_back(MathAtom(new MathCharInset('=', LM_TC_VAR))); - ifstream is(outfile.c_str()); - mathed_parse_cell(br, is); - mathcursor->insert(br); + mathcursor->insert(pipeThroughExtern(arg, ar)); } + bool InsetFormula::display() const { return mat()->getType() != LM_OT_SIMPLE; diff --git a/src/mathed/math_charinset.C b/src/mathed/math_charinset.C index 8b0a570184..ff1852c162 100644 --- a/src/mathed/math_charinset.C +++ b/src/mathed/math_charinset.C @@ -119,7 +119,7 @@ void MathCharInset::write(MathWriteInfo & os) const void MathCharInset::writeNormal(std::ostream & os) const { - os << char_; + os << "[char " << char_ << " " << "mathalpha" << "]"; } diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 2bcfb6bb59..6535c43bd3 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -702,7 +702,8 @@ void MathCursor::selDel() seldump("selDel"); if (selection_) { theSelection.erase(*this); - pos() = 0; + if (pos() > size()) + pos() = size(); selClear(); } } diff --git a/src/mathed/math_deliminset.C b/src/mathed/math_deliminset.C index ed1b0ea573..d9755b7cb0 100644 --- a/src/mathed/math_deliminset.C +++ b/src/mathed/math_deliminset.C @@ -49,6 +49,12 @@ void MathDelimInset::write(MathWriteInfo & os) const } +void MathDelimInset::writeNormal(std::ostream & os) const +{ + os << "[delim " << latexName(left_) << " " << latexName(right_) << "]"; +} + + int MathDelimInset::dw() const { int w = height() / 5; diff --git a/src/mathed/math_deliminset.h b/src/mathed/math_deliminset.h index 89240db8cb..f1fb2bf605 100644 --- a/src/mathed/math_deliminset.h +++ b/src/mathed/math_deliminset.h @@ -23,6 +23,8 @@ public: void draw(Painter &, int x, int y) const; /// void write(MathWriteInfo & os) const; + /// write normalized content + void writeNormal(std::ostream &) const; /// void metrics(MathMetricsInfo const & st) const; private: diff --git a/src/mathed/math_rootinset.C b/src/mathed/math_rootinset.C index d84ec004ac..d7dbad4943 100644 --- a/src/mathed/math_rootinset.C +++ b/src/mathed/math_rootinset.C @@ -70,7 +70,7 @@ void MathRootInset::writeNormal(std::ostream & os) const cell(1).writeNormal(os); os << " "; cell(0).writeNormal(os); - os << "] "; + os << "]"; } diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index 39de7947d8..3b190369e6 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -1,4 +1,7 @@ + #include +#include + #include "debug.h" #include "support.h" #include "support/LOstream.h" @@ -10,6 +13,8 @@ #include "math_scriptinset.h" +using std::ostringstream; + MathScriptInset::MathScriptInset() : MathNestInset(2), limits_(0) @@ -27,7 +32,6 @@ MathScriptInset::MathScriptInset(bool up) } - MathInset * MathScriptInset::clone() const { return new MathScriptInset(*this); @@ -251,6 +255,43 @@ void MathScriptInset::write(MathInset const * nuc, MathWriteInfo & os) const } +void MathScriptInset::writeNormal(ostream & os) const +{ + //lyxerr << "unexpected call to MathScriptInset::writeNormal()\n"; + writeNormal(0, os); +} + + +void MathScriptInset::writeNormal(MathInset const * nuc, ostream & os) const +{ + bool d = hasDown() && down().data_.size(); + bool u = hasUp() && up().data_.size(); + + ostringstream osb; + if (nuc) + nuc->writeNormal(osb); + else + osb << "[par]"; + string base = osb.str(); + + if (u && d) { + os << "[sup [sub " << osb.str() << " "; + down().data_.writeNormal(os); + os << "]"; + up().data_.writeNormal(os); + os << ']'; + } else if (u) { + os << "[sup " << osb.str() << " "; + up().data_.writeNormal(os); + os << ']'; + } else if (d) { + os << "[sub " << osb.str() << " "; + down().data_.writeNormal(os); + os << ']'; + } +} + + bool MathScriptInset::hasLimits(MathInset const * nuc) const { return limits_ == 1 || (limits_ == 0 && nuc && nuc->isScriptable()); diff --git a/src/mathed/math_scriptinset.h b/src/mathed/math_scriptinset.h index f7af1546cc..0bcb5287e4 100644 --- a/src/mathed/math_scriptinset.h +++ b/src/mathed/math_scriptinset.h @@ -23,6 +23,8 @@ public: /// void write(MathWriteInfo & os) const; /// + void writeNormal(ostream & os) const; + /// void metrics(MathMetricsInfo const & st) const; /// void draw(Painter &, int x, int y) const; @@ -30,6 +32,8 @@ public: /// void write(MathInset const *, MathWriteInfo & os) const; /// + void writeNormal(MathInset const *, ostream & os) const; + /// void metrics(MathInset const * nucleus, MathMetricsInfo const & st) const; /// void draw(MathInset const * nucleus, Painter &, int x, int y) const; diff --git a/src/mathed/math_sqrtinset.C b/src/mathed/math_sqrtinset.C index acd1440cf9..9c6b4b3ed3 100644 --- a/src/mathed/math_sqrtinset.C +++ b/src/mathed/math_sqrtinset.C @@ -53,5 +53,5 @@ void MathSqrtInset::writeNormal(std::ostream & os) const { os << "[sqrt "; cell(0).writeNormal(os); - os << "] "; + os << "]"; }