#include <config.h>
+#include "BufferView.h"
+#include "Cursor.h"
+#include "DispatchResult.h"
+#include "FuncRequest.h"
+#include "InsetMathFont.h"
#include "InsetMathScript.h"
+#include "InsetMathSymbol.h"
#include "MathData.h"
#include "MathStream.h"
#include "MathSupport.h"
-#include "InsetMathSymbol.h"
-#include "InsetMathFont.h"
-#include "DispatchResult.h"
-#include "Cursor.h"
-#include "debug.h"
-#include "FuncRequest.h"
-#include "Undo.h"
+
+#include "support/debug.h"
#include <boost/assert.hpp>
+#include <ostream>
+
+using namespace std;
namespace lyx {
-using std::string;
-using std::max;
-using std::endl;
InsetMathScript::InsetMathScript()
: InsetMathNest(1), cell_1_is_up_(false), limits_(0)
{
BOOST_ASSERT(hasUp());
Dimension const dim = dimension(bv);
- return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker();
+ return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
}
}
-int InsetMathScript::nker() const
+int InsetMathScript::nker(BufferView const * bv) const
{
if (nuc().size()) {
- int kerning = nuc().kerning();
+ int kerning = nuc().kerning(bv);
return kerning > 0 ? kerning : 0;
}
return 0;
dim.wid = max(dim.wid, dimdown.width());
} else {
if (hasUp())
- dim.wid = max(dim.wid, nker() + dimup.width());
+ dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
if (hasDown())
dim.wid = max(dim.wid, dimdown.width());
dim.wid += nwid(bv);
} else
dim.des = nd;
metricsMarkers(dim);
- // Cache the inset dimension.
- setDimCache(mi, dim);
}
bool InsetMathScript::hasUp() const
{
- //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
- //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
+ //lyxerr << "1up: " << bool(cell_1_is_up_));
+ //lyxerr << "hasUp: " << bool(idxOfScript(true)));
return idxOfScript(true);
}
bool InsetMathScript::hasDown() const
{
- //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
- //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
+ //LYXERR0("1up: " << bool(cell_1_is_up_));
+ //LYXERR0("hasDown: " << bool(idxOfScript(false)));
return idxOfScript(false);
}
}
-bool InsetMathScript::idxRight(Cursor &) const
+bool InsetMathScript::idxForward(Cursor &) const
{
return false;
}
-bool InsetMathScript::idxLeft(Cursor &) const
+bool InsetMathScript::idxBackward(Cursor &) const
{
return false;
}
//}
} else {
if (os.firstitem())
- LYXERR(Debug::MATHED) << "suppressing {} when writing"
- << endl;
+ LYXERR(Debug::MATHED, "suppressing {} when writing");
else
os << "{}";
}
void InsetMathScript::infoize2(odocstream & os) const
{
if (limits_)
- os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+ os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
}
{
InsetMathNest::notifyCursorLeaves(cur);
- //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
+ //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
+
+ // Remove empty scripts if possible:
- // remove empty scripts if possible
- if (nargs() > 2) {
- // Case of two scripts. In this case, 1 = super, 2 = sub
- if (cur.idx() == 2 && cell(2).empty()) {
+ // The case of two scripts, but only one got empty (1 = super, 2 = sub).
+ // We keep the script inset, but remove the empty script.
+ if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
+ if (cell(2).empty()) {
// must be a subscript...
- recordUndoInset(cur);
removeScript(false);
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
- } else if (cur.idx() == 1 && cell(1).empty()) {
+ } else if (cell(1).empty()) {
// must be a superscript...
- recordUndoInset(cur);
removeScript(true);
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
}
- } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
- // could be either subscript or super script
- recordUndoInset(cur);
- removeScript(cell_1_is_up_);
+ }
+ // Now the two suicide cases:
+ // * we have only one script which is empty
+ // * we have two scripts which are both empty.
+ // The script inset is removed completely.
+ if ((nargs() == 2 && cell(1).empty())
+ || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
+ // Make undo step. We cannot use cur for this because
+ // it does not necessarily point to us. The BufferView
+ // cursor though should do.
+ int scriptSlice
+ = cur.bv().cursor().find(this);
+ BOOST_ASSERT(scriptSlice != -1);
+ Cursor & bvCur = cur.bv().cursor();
+ bvCur.cutOff(scriptSlice);
+ bvCur.recordUndoInset();
+
// Let the script inset commit suicide. This is
// modelled on Cursor.pullArg(), but tries not to
// invoke notifyCursorLeaves again and does not touch
// cur (since the top slice will be deleted
- // afterwards))
+ // afterwards)
MathData ar = cell(0);
- Cursor tmpcur = cur;
- tmpcur.pop();
- tmpcur.cell().erase(tmpcur.pos());
- tmpcur.cell().insert(tmpcur.pos(), ar);
+ bvCur.pop();
+ bvCur.cell().erase(bvCur.pos());
+ bvCur.cell().insert(bvCur.pos(), ar);
+
+ // put cursor behind
+ bvCur.pos() += ar.size();
+
+ // redraw
+ cur.updateFlags(cur.disp_.update() | Update::SinglePar);
return true;
}
- //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
+ //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
return false;
}
void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
{
- //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
+ //LYXERR("InsetMathScript: request: " << cmd);
if (cmd.action == LFUN_MATH_LIMITS) {
if (!cmd.argument().empty()) {