+ if (nuc().size())
+ nuc().drawT(pain, x + dxx(), y);
+ if (hasUp())
+ up().drawT(pain, x + dx1(), y - dy1());
+ if (hasDown())
+ down().drawT(pain, x + dx0(), y + dy0());
+}
+
+
+
+bool MathScriptInset::hasLimits() const
+{
+ // obvious cases
+ if (limits_ == 1)
+ return true;
+ if (limits_ == -1)
+ return false;
+
+ // we can only display limits if the nucleus wants some
+ if (!nuc().size())
+ return false;
+ if (!nuc().back()->isScriptable())
+ return false;
+
+ // per default \int has limits beside the \int even in displayed formulas
+ if (nuc().back()->asSymbolInset())
+ if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
+ return false;
+
+ // assume "real" limits for everything else
+ return true;
+}
+
+
+void MathScriptInset::removeScript(bool up)
+{
+ lyxerr << "MathNestInset::removeScript: 1 up: " << up << endl;
+ if (nargs() == 2) {
+ lyxerr << "MathNestInset::removeScript: a up: " << up << endl;
+ if (up == cell_1_is_up_)
+ cells_.pop_back();
+ lyxerr << "MathNestInset::removeScript: b up: " << up << endl;
+ } else if (nargs() == 3) {
+ if (up == true) {
+ swap(cells_[1], cells_[2]);
+ cell_1_is_up_ = false;
+ } else {
+ cell_1_is_up_ = true;
+ }
+ cells_.pop_back();
+ }
+ lyxerr << "MathNestInset::removeScript: 2 up: " << up << endl;
+}
+
+
+bool MathScriptInset::has(bool up) const
+{
+ return idxOfScript(up);
+}
+
+
+bool MathScriptInset::hasUp() const
+{
+ //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
+ //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
+ return idxOfScript(true);
+}
+
+
+bool MathScriptInset::hasDown() const
+{
+ //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
+ //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
+ return idxOfScript(false);
+}
+
+
+InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
+{
+ if (nargs() == 1)
+ return 0;
+ if (nargs() == 2)
+ return (cell_1_is_up_ == up) ? 1 : 0;
+ if (nargs() == 3)
+ return up ? 1 : 2;
+ BOOST_ASSERT(false);
+ // Silence compiler
+ return 0;
+}
+
+
+bool MathScriptInset::idxRight(LCursor &) const
+{
+ return false;
+}
+
+
+bool MathScriptInset::idxLeft(LCursor &) const
+{
+ return false;
+}
+
+
+bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
+{
+ // in nucleus?
+ if (cur.idx() == 0) {
+ // don't go up/down if there is no cell in this direction
+ if (!has(up))
+ return false;
+ // go up/down only if in the last position
+ // or in the first position of something with displayed limits
+ if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
+ cur.idx() = idxOfScript(up);
+ cur.pos() = 0;
+ return true;
+ }
+ return false;
+ }
+
+ // Are we 'up'?
+ if (has(up) && cur.idx() == idxOfScript(true)) {
+ // can't go further up
+ if (up)
+ return false;
+ // otherwise go to last position in the nucleus
+ cur.idx() = 0;
+ cur.pos() = cur.lastpos();
+ return true;
+ }
+
+ // Are we 'down'?
+ if (has(up) && cur.idx() == idxOfScript(false)) {
+ // can't go further down
+ if (!up)
+ return false;
+ // otherwise go to last position in the nucleus
+ cur.idx() = 0;
+ cur.pos() = cur.lastpos();
+ return true;
+ }
+
+ return false;
+}
+
+
+void MathScriptInset::write(WriteStream & os) const
+{
+ if (nuc().size()) {
+ os << nuc();
+ //if (nuc().back()->takesLimits()) {
+ if (limits_ == -1)
+ os << "\\nolimits ";
+ if (limits_ == 1)
+ os << "\\limits ";
+ //}
+ } else {
+ if (os.firstitem())
+ lyxerr[Debug::MATHED] << "suppressing {} when writing"
+ << endl;
+ else
+ os << "{}";
+ }
+
+ if (hasDown() && down().size())
+ os << "_{" << down() << '}';
+
+ if (hasUp() && up().size())
+ os << "^{" << up() << '}';
+
+ if (lock_ && !os.latex())
+ os << "\\lyxlock ";
+}
+
+
+void MathScriptInset::normalize(NormalStream & os) const
+{
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (u && d)
+ os << "[subsup ";
+ else if (u)
+ os << "[sup ";
+ else if (d)
+ os << "[sub ";
+
+ if (nuc().size())
+ os << nuc() << ' ';
+ else
+ os << "[par]";
+
+ if (u && d)
+ os << down() << ' ' << up() << ']';
+ else if (d)
+ os << down() << ']';
+ else if (u)
+ os << up() << ']';
+}
+
+
+void MathScriptInset::maple(MapleStream & os) const
+{
+ if (nuc().size())
+ os << nuc();
+ if (hasDown() && down().size())
+ os << '[' << down() << ']';
+ if (hasUp() && up().size())
+ os << "^(" << up() << ')';
+}
+
+
+void MathScriptInset::mathematica(MathematicaStream & os) const
+{
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (nuc().size()) {
+ if (d)
+ os << "Subscript[" << nuc();
+ else
+ os << nuc();
+ }
+
+ if (u)
+ os << "^(" << up() << ')';
+
+ if (nuc().size()) {
+ if (d)
+ os << ',' << down() << ']';
+ }
+}
+
+
+void MathScriptInset::mathmlize(MathMLStream & os) const
+{
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (u && d)
+ os << MTag("msubsup");
+ else if (u)
+ os << MTag("msup");
+ else if (d)
+ os << MTag("msub");
+
+ if (nuc().size())
+ os << nuc();
+ else
+ os << "<mrow/>";
+
+ if (u && d)
+ os << down() << up() << ETag("msubsup");
+ else if (u)
+ os << up() << ETag("msup");
+ else if (d)
+ os << down() << ETag("msub");
+}
+
+
+void MathScriptInset::octave(OctaveStream & os) const
+{
+ if (nuc().size())
+ os << nuc();
+ if (hasDown() && down().size())
+ os << '[' << down() << ']';
+ if (hasUp() && up().size())
+ os << "^(" << up() << ')';
+}
+
+
+void MathScriptInset::infoize(std::ostream & os) const
+{
+ os << "Scripts";
+}
+
+
+void MathScriptInset::infoize2(std::ostream & os) const
+{
+ if (limits_)
+ os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+}
+
+
+void MathScriptInset::notifyCursorLeaves(LCursor & cur)
+{
+ MathNestInset::notifyCursorLeaves(cur);
+
+ // remove empty scripts if possible
+ if (nargs() > 2 && cur.idx() == 2 && cell(2).empty()) {
+ // must be a subscript...
+ removeScript(false);
+ // sanitize cursor, even if this slice will be removed immediately
+ cur.idx() = 0;
+ cur.pos() = 0;
+ } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
+ // could be either subscript or super script
+ removeScript(cell_1_is_up_);
+ // sanitize cursor, even if this slice will be removed immediately
+ cur.idx() = 0;
+ cur.pos() = 0;
+ }
+}
+
+
+void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
+{
+ //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
+
+ if (cmd.action == LFUN_MATH_LIMITS) {
+ if (!cmd.argument.empty()) {
+ if (cmd.argument == "limits")
+ limits_ = 1;
+ else if (cmd.argument == "nolimits")
+ limits_ = -1;
+ else
+ limits_ = 0;
+ } else if (limits_ == 0)
+ limits_ = hasLimits() ? -1 : 1;
+ else
+ limits_ = 0;
+ return;
+ }
+
+ MathNestInset::priv_dispatch(cur, cmd);