3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "math_cursor.h"
16 #include "support/limited_stack.h"
17 #include "dispatchresult.h"
19 #include "support/std_sstream.h"
20 #include "formulabase.h"
21 #include "funcrequest.h"
22 #include "math_braceinset.h"
23 #include "math_commentinset.h"
24 #include "math_charinset.h"
25 #include "math_factory.h"
26 #include "math_gridinset.h"
27 #include "math_macroarg.h"
28 #include "math_macrotemplate.h"
29 #include "math_mathmlstream.h"
30 #include "math_scriptinset.h"
31 #include "math_spaceinset.h"
32 #include "math_support.h"
33 #include "math_unknowninset.h"
35 #include <boost/assert.hpp>
41 #ifndef CXX_GLOBAL_CSTD
47 using std::ostringstream;
50 // matheds own cut buffer
51 limited_stack<string> theCutBuffer;
54 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
55 : formula_(formula), autocorrect_(false), selection_(false), targetx_(-1)
57 front ? first() : last();
62 MathCursor::~MathCursor()
64 // ensure that 'notifyCursorLeave' is called
70 void MathCursor::push(MathAtom & t)
72 Cursor_.push_back(CursorSlice(t.nucleus()));
76 void MathCursor::pushLeft(MathAtom & t)
78 //lyxerr << "Entering atom " << t << " left" << endl;
80 t->idxFirst(idx(), pos());
84 void MathCursor::pushRight(MathAtom & t)
86 //lyxerr << "Entering atom " << t << " right" << endl;
89 t->idxLast(idx(), pos());
93 bool MathCursor::popLeft()
95 //lyxerr << "Leaving atom to the left" << endl;
98 inset()->notifyCursorLeaves(idx());
101 inset()->notifyCursorLeaves(idx());
107 bool MathCursor::popRight()
109 //lyxerr << "Leaving atom "; inset()->write(cerr, false); cerr << " right" << endl;
112 inset()->notifyCursorLeaves(idx());
115 inset()->notifyCursorLeaves(idx());
124 void MathCursor::dump(char const * what) const
126 lyxerr << "MC: " << what << endl;
127 lyxerr << " Cursor: " << depth() << endl;
128 for (unsigned i = 0; i < depth(); ++i)
129 lyxerr << " i: " << i << ' ' << Cursor_[i] << endl;
130 lyxerr << " Anchor: " << Anchor_.size() << endl;
131 for (unsigned i = 0; i < Anchor_.size(); ++i)
132 lyxerr << " i: " << i << ' ' << Anchor_[i] << endl;
133 lyxerr << " sel: " << selection_ << endl;
136 void MathCursor::dump(char const *) const {}
140 bool MathCursor::isInside(MathInset const * p) const
142 for (unsigned i = 0; i < depth(); ++i)
143 if (Cursor_[i].asMathInset() == p)
149 bool MathCursor::openable(MathAtom const & t, bool sel) const
158 // we can't move into anything new during selection
159 if (depth() == Anchor_.size())
161 if (t.operator->() != Anchor_[depth()].asMathInset())
168 bool MathCursor::inNucleus() const
170 return inset()->asScriptInset() && idx() == 2;
174 bool MathCursor::posLeft()
183 bool MathCursor::posRight()
192 bool MathCursor::left(bool sel)
195 autocorrect_ = false;
196 targetx_ = -1; // "no target"
203 if (hasPrevAtom() && openable(prevAtom(), sel)) {
204 pushRight(prevAtom());
208 return posLeft() || idxLeft() || popLeft() || selection_;
212 bool MathCursor::right(bool sel)
215 autocorrect_ = false;
216 targetx_ = -1; // "no target"
223 if (hasNextAtom() && openable(nextAtom(), sel)) {
224 pushLeft(nextAtom());
228 return posRight() || idxRight() || popRight() || selection_;
232 void MathCursor::first()
235 push(formula_->par());
236 inset()->idxFirst(idx(), pos());
240 void MathCursor::last()
243 push(formula_->par());
244 inset()->idxLast(idx(), pos());
249 (MathIterator const & cursor, MathIterator const & anchor)
251 // avoid deeper nested insets when selecting
252 if (cursor.size() > anchor.size())
255 // anchor might be deeper, should have same path then
256 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
257 if (cursor[i].asMathInset() != anchor[i].asMathInset())
260 // position should be ok.
265 void MathCursor::setPos(int x, int y)
268 bool res = bruteFind(x, y,
269 formula()->xlow(), formula()->xhigh(),
270 formula()->ylow(), formula()->yhigh());
272 // this can happen on creation of "math-display"
276 targetx_ = -1; // "no target"
282 bool MathCursor::home(bool sel)
285 autocorrect_ = false;
288 if (!inset()->idxHome(idx(), pos()))
291 targetx_ = -1; // "no target"
296 bool MathCursor::end(bool sel)
299 autocorrect_ = false;
302 if (!inset()->idxEnd(idx(), pos()))
305 targetx_ = -1; // "no target"
310 void MathCursor::plainErase()
312 array().erase(pos());
316 void MathCursor::markInsert()
318 //lyxerr << "inserting mark" << endl;
319 array().insert(pos(), MathAtom(new MathCharInset(0)));
323 void MathCursor::markErase()
325 //lyxerr << "deleting mark" << endl;
326 array().erase(pos());
330 void MathCursor::plainInsert(MathAtom const & t)
333 array().insert(pos(), t);
338 void MathCursor::insert2(string const & str)
346 void MathCursor::insert(string const & str)
348 //lyxerr << "inserting '" << str << "'" << endl;
350 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
351 plainInsert(MathAtom(new MathCharInset(*it)));
355 void MathCursor::insert(char c)
357 //lyxerr << "inserting '" << c << "'" << endl;
359 plainInsert(MathAtom(new MathCharInset(c)));
363 void MathCursor::insert(MathAtom const & t)
371 void MathCursor::niceInsert(string const & t)
382 void MathCursor::niceInsert(MathAtom const & t)
385 string safe = grabAndEraseSelection();
387 // enter the new inset and move the contents of the selection if possible
390 pushLeft(nextAtom());
396 void MathCursor::insert(MathArray const & ar)
401 array().insert(pos(), ar);
406 void MathCursor::paste(string const & data)
408 dispatch(FuncRequest(LFUN_PASTE, data));
412 bool MathCursor::backspace()
414 autocorrect_ = false;
422 if (inset()->ncols() == 1 &&
423 inset()->nrows() == 1 &&
432 MathUnknownInset * p = activeMacro();
433 if (p->name().size() > 1) {
434 p->setName(p->name().substr(0, p->name().size() - 1));
439 if (hasPrevAtom() && prevAtom()->nargs() > 0) {
440 // let's require two backspaces for 'big stuff' and
441 // highlight on the first
451 bool MathCursor::erase()
453 autocorrect_ = false;
462 // delete empty cells if possible
464 if (inset()->idxDelete(idx()))
467 // special behaviour when in last position of cell
468 if (pos() == size()) {
469 bool one_cell = inset()->ncols() == 1 && inset()->nrows() == 1;
470 if (one_cell && depth() == 1 && size() == 0)
476 inset()->idxGlue(idx());
480 if (hasNextAtom() && nextAtom()->nargs() > 0)
489 bool MathCursor::up(bool sel)
494 MathIterator save = Cursor_;
498 autocorrect_ = false;
503 bool MathCursor::down(bool sel)
508 MathIterator save = Cursor_;
512 autocorrect_ = false;
517 void MathCursor::macroModeClose()
521 MathUnknownInset * p = activeMacro();
523 string s = p->name();
525 array().erase(pos());
527 // do nothing if the macro name is empty
531 string const name = s.substr(1);
533 // prevent entering of recursive macros
534 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
535 && formula()->getInsetName() == name)
536 lyxerr << "can't enter recursive macro" << endl;
538 niceInsert(createMathInset(name));
542 string MathCursor::macroName() const
544 return inMacroMode() ? activeMacro()->name() : string();
548 void MathCursor::selClear()
555 void MathCursor::selCopy()
559 theCutBuffer.push(grabSelection());
562 //theCutBuffer.erase();
567 void MathCursor::selCut()
570 theCutBuffer.push(grabAndEraseSelection());
574 void MathCursor::selDel()
584 void MathCursor::selPaste(size_t n)
588 if (n < theCutBuffer.size())
589 paste(theCutBuffer[n]);
595 void MathCursor::selHandle(bool sel)
597 if (sel == selection_)
605 void MathCursor::selStart()
615 void MathCursor::selClearOrDel()
617 if (lyxrc.auto_region_delete)
624 void MathCursor::drawSelection(PainterInfo & pi) const
630 getSelection(i1, i2);
631 i1.asMathInset()->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
635 void MathCursor::handleNest(MathAtom const & a, int c)
638 asArray(grabAndEraseSelection(), at.nucleus()->cell(c));
640 pushRight(prevAtom());
644 void MathCursor::getPos(int & x, int & y) const
646 inset()->getPos(idx(), pos(), x, y);
650 int MathCursor::targetX() const
660 MathInset * MathCursor::inset() const
662 return cursor().asMathInset();
666 InsetFormulaBase * MathCursor::formula() const
672 MathCursor::idx_type MathCursor::idx() const
674 return cursor().idx_;
678 MathCursor::idx_type & MathCursor::idx()
680 return cursor().idx_;
684 MathCursor::pos_type MathCursor::pos() const
686 return cursor().pos_;
690 void MathCursor::adjust(pos_type from, difference_type diff)
692 if (cursor().pos_ > from)
693 cursor().pos_ += diff;
694 if (Anchor_.back().pos_ > from)
695 Anchor_.back().pos_ += diff;
696 // just to be on the safe side
697 // theoretically unecessary
702 MathCursor::pos_type & MathCursor::pos()
704 return cursor().pos_;
708 bool MathCursor::inMacroMode() const
712 MathUnknownInset const * p = prevAtom()->asUnknownInset();
713 return p && !p->final();
717 MathUnknownInset * MathCursor::activeMacro()
719 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
723 MathUnknownInset const * MathCursor::activeMacro() const
725 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
729 bool MathCursor::inMacroArgMode() const
731 return pos() > 0 && prevAtom()->getChar() == '#';
735 bool MathCursor::selection() const
741 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
743 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
744 MathGridInset * p = Cursor_[i].asMathInset()->asGridInset();
746 idx = Cursor_[i].idx_;
754 void MathCursor::popToHere(MathInset const * p)
756 while (depth() && Cursor_.back().asMathInset() != p)
761 void MathCursor::popToEnclosingGrid()
763 while (depth() && !Cursor_.back().asMathInset()->asGridInset())
768 void MathCursor::popToEnclosingHull()
770 while (depth() && !Cursor_.back().asMathInset()->asHullInset())
775 void MathCursor::pullArg()
778 MathArray a = array();
781 array().insert(pos(), a);
784 formula()->mutateToText();
789 void MathCursor::touch()
791 MathIterator::const_iterator it = Cursor_.begin();
792 MathIterator::const_iterator et = Cursor_.end();
793 for ( ; it != et; ++it)
798 void MathCursor::normalize()
800 if (idx() >= inset()->nargs()) {
801 lyxerr << "this should not really happen - 1: "
802 << idx() << ' ' << inset()->nargs()
803 << " in: " << inset() << endl;
806 idx() = min(idx(), inset()->nargs() - 1);
808 if (pos() > size()) {
809 lyxerr << "this should not really happen - 2: "
810 << pos() << ' ' << size() << " in idx: " << idx()
812 WriteStream wi(lyxerr, false, true);
817 pos() = pos() < size() ? pos() : size();
821 MathCursor::size_type MathCursor::size() const
823 return array().size();
827 bool MathCursor::hasPrevAtom() const
833 bool MathCursor::hasNextAtom() const
835 return pos() < size();
839 MathAtom const & MathCursor::prevAtom() const
841 BOOST_ASSERT(pos() > 0);
842 return array()[pos() - 1];
846 MathAtom & MathCursor::prevAtom()
848 BOOST_ASSERT(pos() > 0);
849 return array()[pos() - 1];
853 MathAtom const & MathCursor::nextAtom() const
855 BOOST_ASSERT(pos() < size());
856 return array()[pos()];
860 MathAtom & MathCursor::nextAtom()
862 BOOST_ASSERT(pos() < size());
863 return array()[pos()];
867 MathArray & MathCursor::array() const
869 static MathArray dummy;
871 if (idx() >= inset()->nargs()) {
872 lyxerr << "############ idx_ " << idx() << " not valid" << endl;
877 lyxerr << "############ depth() == 0 not valid" << endl;
881 return cursor().cell();
885 void MathCursor::idxNext()
887 inset()->idxNext(idx(), pos());
891 void MathCursor::idxPrev()
893 inset()->idxPrev(idx(), pos());
897 char MathCursor::valign() const
900 MathGridInset * p = enclosingGrid(idx);
901 return p ? p->valign() : '\0';
905 char MathCursor::halign() const
908 MathGridInset * p = enclosingGrid(idx);
909 return p ? p->halign(idx % p->ncols()) : '\0';
913 void MathCursor::getSelection(CursorSlice & i1, CursorSlice & i2) const
915 CursorSlice anc = normalAnchor();
916 if (anc < cursor()) {
926 CursorSlice & MathCursor::cursor()
928 BOOST_ASSERT(depth());
929 return Cursor_.back();
933 CursorSlice const & MathCursor::cursor() const
935 BOOST_ASSERT(depth());
936 return Cursor_.back();
940 bool MathCursor::goUpDown(bool up)
942 // Be warned: The 'logic' implemented in this function is highly fragile.
943 // A distance of one pixel or a '<' vs '<=' _really_ matters.
944 // So fiddle around with it only if you know what you are doing!
949 // check if we had something else in mind, if not, this is the future goal
955 // try neigbouring script insets
959 MathScriptInset const * p = prevAtom()->asScriptInset();
960 if (p && p->has(up)) {
963 idx() = up; // the superscript has index 1
965 //lyxerr << "updown: handled by scriptinset to the left" << endl;
972 MathScriptInset const * p = nextAtom()->asScriptInset();
973 if (p && p->has(up)) {
977 //lyxerr << "updown: handled by scriptinset to the right" << endl;
983 // try current cell for e.g. text insets
984 if (inset()->idxUpDown2(idx(), pos(), up, targetx_))
987 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
992 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
993 // lyxerr << "updown: handled by brute find in the same cell" << endl;
997 // try to find an inset that knows better then we
999 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1001 if (inset()->idxUpDown(idx(), pos(), up, targetx_)) {
1002 // try to find best position within this inset
1008 // no such inset found, just take something "above"
1009 //lyxerr << "updown: handled by strange case" << endl;
1015 up ? formula()->ylow() : yo + 4,
1016 up ? yo - 4 : formula()->yhigh()
1019 // any improvement so far?
1022 if (up ? ynew < yo : ynew > yo)
1028 bool MathCursor::bruteFind
1029 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1031 MathIterator best_cursor;
1032 double best_dist = 1e10;
1034 MathIterator it = ibegin(formula()->par().nucleus());
1035 MathIterator et = iend(formula()->par().nucleus());
1037 // avoid invalid nesting when selecting
1038 if (!selection_ || positionable(it, Anchor_)) {
1040 it.back().getPos(xo, yo);
1041 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1042 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1043 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1044 // '<=' in order to take the last possible position
1045 // this is important for clicking behind \sum in e.g. '\sum_i a'
1046 if (d <= best_dist) {
1058 if (best_dist < 1e10)
1059 Cursor_ = best_cursor;
1060 return best_dist < 1e10;
1064 void MathCursor::bruteFind2(int x, int y)
1066 double best_dist = 1e10;
1068 MathIterator it = Cursor_;
1069 it.back().setPos(0);
1070 MathIterator et = Cursor_;
1071 et.back().setPos(it.cell().size());
1072 for (int i = 0; ; ++i) {
1074 it.back().getPos(xo, yo);
1075 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1076 // '<=' in order to take the last possible position
1077 // this is important for clicking behind \sum in e.g. '\sum_i a'
1078 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1079 if (d <= best_dist) {
1090 bool MathCursor::idxLineLast()
1092 idx() -= idx() % inset()->ncols();
1093 idx() += inset()->ncols() - 1;
1098 bool MathCursor::idxLeft()
1100 return inset()->idxLeft(idx(), pos());
1104 bool MathCursor::idxRight()
1106 return inset()->idxRight(idx(), pos());
1110 bool MathCursor::script(bool up)
1112 // Hack to get \\^ and \\_ working
1113 if (inMacroMode() && macroName() == "\\") {
1115 niceInsert(createMathInset("mathcircumflex"));
1122 string safe = grabAndEraseSelection();
1124 // we are in a nucleus of a script inset, move to _our_ script
1125 inset()->asScriptInset()->ensure(up);
1128 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1129 prevAtom().nucleus()->asScriptInset()->ensure(up);
1130 pushRight(prevAtom());
1133 } else if (hasPrevAtom()) {
1135 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1136 pushLeft(nextAtom());
1140 plainInsert(MathAtom(new MathScriptInset(up)));
1141 prevAtom().nucleus()->asScriptInset()->ensure(up);
1142 pushRight(prevAtom());
1152 bool MathCursor::interpret(char c)
1154 //lyxerr << "interpret 2: '" << c << "'" << endl;
1155 targetx_ = -1; // "no target"
1156 if (inMacroArgMode()) {
1160 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1161 if (p && 1 <= n && n <= p->numargs())
1162 insert(MathAtom(new MathMacroArgument(c - '0')));
1164 insert(createMathInset("#"));
1165 interpret(c); // try again
1171 if (inMacroMode()) {
1172 string name = macroName();
1173 //lyxerr << "interpret name: '" << name << "'" << endl;
1176 activeMacro()->setName(activeMacro()->name() + c);
1180 // handle 'special char' macros
1185 if (currentMode() == MathInset::TEXT_MODE)
1186 niceInsert(createMathInset("textbackslash"));
1188 niceInsert(createMathInset("backslash"));
1189 } else if (c == '{') {
1190 niceInsert(MathAtom(new MathBraceInset));
1192 niceInsert(createMathInset(string(1, c)));
1197 // leave macro mode and try again if necessary
1200 niceInsert(MathAtom(new MathBraceInset));
1206 // This is annoying as one has to press <space> far too often.
1210 // leave autocorrect mode if necessary
1211 if (autocorrect_ && c == ' ') {
1212 autocorrect_ = false;
1217 // just clear selection on pressing the space bar
1218 if (selection_ && c == ' ') {
1226 //lyxerr << "starting with macro" << endl;
1227 insert(MathAtom(new MathUnknownInset("\\", false)));
1232 if (currentMode() == MathInset::TEXT_MODE)
1238 if (currentMode() == MathInset::TEXT_MODE) {
1239 // insert spaces in text mode,
1240 // but suppress direct insertion of two spaces in a row
1241 // the still allows typing '<space>a<space>' and deleting the 'a', but
1242 // it is better than nothing...
1243 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1247 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1248 prevAtom().nucleus()->asSpaceInset()->incSpace();
1253 // if are at the very end, leave the formula
1254 return pos() != size();
1267 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1268 niceInsert(createMathInset(string(1, c)));
1273 niceInsert(MathAtom(new MathCommentInset));
1277 // try auto-correction
1278 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1281 // no special circumstances, so insert the character without any fuss
1283 autocorrect_ = true;
1288 void MathCursor::setSelection(MathIterator const & where, size_type n)
1297 void MathCursor::insetToggle()
1299 if (hasNextAtom()) {
1300 // toggle previous inset ...
1301 nextAtom().nucleus()->lock(!nextAtom()->lock());
1302 } else if (popLeft() && hasNextAtom()) {
1303 // ... or enclosing inset if we are in the last inset position
1304 nextAtom().nucleus()->lock(!nextAtom()->lock());
1310 string MathCursor::info() const
1313 os << "Math editor mode. ";
1314 for (int i = 0, n = depth(); i < n; ++i) {
1315 Cursor_[i].asMathInset()->infoize(os);
1319 prevAtom()->infoize2(os);
1325 unsigned MathCursor::depth() const
1327 return Cursor_.size();
1335 void region(CursorSlice const & i1, CursorSlice const & i2,
1336 MathInset::row_type & r1, MathInset::row_type & r2,
1337 MathInset::col_type & c1, MathInset::col_type & c2)
1339 MathInset * p = i1.asMathInset();
1340 c1 = p->col(i1.idx_);
1341 c2 = p->col(i2.idx_);
1344 r1 = p->row(i1.idx_);
1345 r2 = p->row(i2.idx_);
1353 string MathCursor::grabSelection() const
1360 getSelection(i1, i2);
1362 if (i1.idx_ == i2.idx_) {
1363 MathArray::const_iterator it = i1.cell().begin();
1364 return asString(MathArray(it + i1.pos_, it + i2.pos_));
1369 region(i1, i2, r1, r2, c1, c2);
1372 for (row_type row = r1; row <= r2; ++row) {
1375 for (col_type col = c1; col <= c2; ++col) {
1378 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
1385 void MathCursor::eraseSelection()
1389 getSelection(i1, i2);
1390 if (i1.idx_ == i2.idx_)
1391 i1.cell().erase(i1.pos_, i2.pos_);
1393 MathInset * p = i1.asMathInset();
1396 region(i1, i2, r1, r2, c1, c2);
1397 for (row_type row = r1; row <= r2; ++row)
1398 for (col_type col = c1; col <= c2; ++col)
1399 p->cell(p->index(row, col)).clear();
1405 string MathCursor::grabAndEraseSelection()
1409 string res = grabSelection();
1416 CursorSlice MathCursor::normalAnchor() const
1418 if (Anchor_.size() < depth()) {
1420 lyxerr << "unusual Anchor size" << endl;
1422 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1423 // use Anchor on the same level as Cursor
1424 CursorSlice normal = Anchor_[depth() - 1];
1425 if (depth() < Anchor_.size() && !(normal < cursor())) {
1426 // anchor is behind cursor -> move anchor behind the inset
1433 DispatchResult MathCursor::dispatch(FuncRequest const & cmd)
1435 // mouse clicks are somewhat special
1437 switch (cmd.action) {
1438 case LFUN_MOUSE_PRESS:
1439 case LFUN_MOUSE_MOTION:
1440 case LFUN_MOUSE_RELEASE:
1441 case LFUN_MOUSE_DOUBLE: {
1442 CursorSlice & pos = Cursor_.back();
1446 if (x < cmd.x && hasPrevAtom()) {
1447 DispatchResult const res =
1448 prevAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1449 if (res.dispatched())
1452 if (x > cmd.x && hasNextAtom()) {
1453 DispatchResult const res =
1454 nextAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1455 if (res.dispatched())
1463 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1464 CursorSlice & pos = Cursor_[i];
1465 DispatchResult const res =
1466 pos.asMathInset()->dispatch(cmd, pos.idx_, pos.pos_);
1467 if (res.dispatched()) {
1468 if (res.val() == FINISHED) {
1469 Cursor_.shrink(i + 1);
1475 return DispatchResult(false);
1479 MathInset::mode_type MathCursor::currentMode() const
1481 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1482 MathInset::mode_type res = Cursor_[i].asMathInset()->currentMode();
1483 if (res != MathInset::UNDECIDED_MODE)
1486 return MathInset::UNDECIDED_MODE;
1490 void MathCursor::handleFont(string const & font)
1495 safe = grabAndEraseSelection();
1498 if (array().size()) {
1499 // something left in the cell
1501 // cursor in first position
1503 } else if (pos() == array().size()) {
1504 // cursor in last position
1507 // cursor in between. split cell
1508 MathArray::iterator bt = array().begin();
1509 MathAtom at = createMathInset(font);
1510 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1511 cursor().cell().erase(bt, bt + pos());
1516 // nothing left in the cell
1524 void releaseMathCursor(BufferView * bv)
1527 InsetFormulaBase * f = mathcursor->formula();