3 * Purpose: Interaction for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Math interaction for a WYSIWYG math editor.
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
12 * Version: 0.8beta, Math & Lyx project.
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
21 #include "support/lstrings.h"
22 #include "support/LAssert.h"
23 #include "support/limited_stack.h"
26 #include "frontends/Painter.h"
27 #include "math_cursor.h"
28 #include "formulabase.h"
29 #include "funcrequest.h"
30 #include "math_autocorrect.h"
31 #include "math_braceinset.h"
32 #include "math_commentinset.h"
33 #include "math_charinset.h"
34 #include "math_extern.h"
35 #include "math_factory.h"
36 #include "math_fontinset.h"
37 #include "math_gridinset.h"
38 #include "math_iterator.h"
39 #include "math_macroarg.h"
40 #include "math_macrotemplate.h"
41 #include "math_mathmlstream.h"
42 #include "math_scriptinset.h"
43 #include "math_spaceinset.h"
44 #include "math_support.h"
45 #include "math_unknowninset.h"
52 using namespace lyx::support;
59 using std::ostringstream;
63 // matheds own cut buffer
64 limited_stack<string> theCutBuffer;
67 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
68 : formula_(formula), autocorrect_(false), selection_(false), targetx_(-1)
70 front ? first() : last();
75 MathCursor::~MathCursor()
77 // ensure that 'notifyCursorLeave' is called
83 void MathCursor::push(MathAtom & t)
85 Cursor_.push_back(CursorPos(t.nucleus()));
89 void MathCursor::pushLeft(MathAtom & t)
91 //lyxerr << "Entering atom " << t << " left\n";
93 t->idxFirst(idx(), pos());
97 void MathCursor::pushRight(MathAtom & t)
99 //lyxerr << "Entering atom " << t << " right\n";
102 t->idxLast(idx(), pos());
106 bool MathCursor::popLeft()
108 //lyxerr << "Leaving atom to the left\n";
111 inset()->notifyCursorLeaves(idx());
114 inset()->notifyCursorLeaves(idx());
120 bool MathCursor::popRight()
122 //lyxerr << "Leaving atom "; inset()->write(cerr, false); cerr << " right\n";
125 inset()->notifyCursorLeaves(idx());
128 inset()->notifyCursorLeaves(idx());
137 void MathCursor::dump(char const * what) const
139 lyxerr << "MC: " << what << endl;
140 lyxerr << " Cursor: " << depth() << endl;
141 for (unsigned i = 0; i < depth(); ++i)
142 lyxerr << " i: " << i << ' ' << Cursor_[i] << endl;
143 lyxerr << " Anchor: " << Anchor_.size() << endl;
144 for (unsigned i = 0; i < Anchor_.size(); ++i)
145 lyxerr << " i: " << i << ' ' << Anchor_[i] << endl;
146 lyxerr << " sel: " << selection_ << endl;
149 void MathCursor::dump(char const *) const {}
153 bool MathCursor::isInside(MathInset const * p) const
155 for (unsigned i = 0; i < depth(); ++i)
156 if (Cursor_[i].inset_ == p)
162 bool MathCursor::openable(MathAtom const & t, bool sel) const
171 // we can't move into anything new during selection
172 if (depth() == Anchor_.size())
174 if (t.operator->() != Anchor_[depth()].inset_)
181 bool MathCursor::inNucleus() const
183 return inset()->asScriptInset() && idx() == 2;
187 bool MathCursor::posLeft()
196 bool MathCursor::posRight()
205 bool MathCursor::left(bool sel)
208 autocorrect_ = false;
209 targetx_ = -1; // "no target"
216 if (hasPrevAtom() && openable(prevAtom(), sel)) {
217 pushRight(prevAtom());
221 return posLeft() || idxLeft() || popLeft() || selection_;
225 bool MathCursor::right(bool sel)
228 autocorrect_ = false;
229 targetx_ = -1; // "no target"
236 if (hasNextAtom() && openable(nextAtom(), sel)) {
237 pushLeft(nextAtom());
241 return posRight() || idxRight() || popRight() || selection_;
245 void MathCursor::first()
248 push(formula_->par());
249 inset()->idxFirst(idx(), pos());
253 void MathCursor::last()
256 push(formula_->par());
257 inset()->idxLast(idx(), pos());
262 (MathIterator const & cursor, MathIterator const & anchor)
264 // avoid deeper nested insets when selecting
265 if (cursor.size() > anchor.size())
268 // anchor might be deeper, should have same path then
269 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
270 if (cursor[i].inset_ != anchor[i].inset_)
273 // position should be ok.
278 void MathCursor::setPos(int x, int y)
281 bool res = bruteFind(x, y,
282 formula()->xlow(), formula()->xhigh(),
283 formula()->ylow(), formula()->yhigh());
285 // this can happen on creation of "math-display"
289 targetx_ = -1; // "no target"
295 bool MathCursor::home(bool sel)
298 autocorrect_ = false;
301 if (!inset()->idxHome(idx(), pos()))
304 targetx_ = -1; // "no target"
309 bool MathCursor::end(bool sel)
312 autocorrect_ = false;
315 if (!inset()->idxEnd(idx(), pos()))
318 targetx_ = -1; // "no target"
323 void MathCursor::plainErase()
325 array().erase(pos());
329 void MathCursor::markInsert()
331 //lyxerr << "inserting mark\n";
332 array().insert(pos(), MathAtom(new MathCharInset(0)));
336 void MathCursor::markErase()
338 //lyxerr << "deleting mark\n";
339 array().erase(pos());
343 void MathCursor::plainInsert(MathAtom const & t)
346 array().insert(pos(), t);
351 void MathCursor::insert2(string const & str)
359 void MathCursor::insert(string const & str)
361 //lyxerr << "inserting '" << str << "'\n";
363 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
364 plainInsert(MathAtom(new MathCharInset(*it)));
368 void MathCursor::insert(char c)
370 //lyxerr << "inserting '" << c << "'\n";
372 plainInsert(MathAtom(new MathCharInset(c)));
376 void MathCursor::insert(MathAtom const & t)
384 void MathCursor::niceInsert(string const & t)
395 void MathCursor::niceInsert(MathAtom const & t)
398 string safe = grabAndEraseSelection();
400 // enter the new inset and move the contents of the selection if possible
403 pushLeft(nextAtom());
409 void MathCursor::insert(MathArray const & ar)
414 array().insert(pos(), ar);
419 void MathCursor::paste(string const & data)
421 dispatch(FuncRequest(LFUN_PASTE, data));
425 bool MathCursor::backspace()
427 autocorrect_ = false;
435 if (inset()->ncols() == 1 &&
436 inset()->nrows() == 1 &&
445 MathUnknownInset * p = activeMacro();
446 if (p->name().size() > 1) {
447 p->setName(p->name().substr(0, p->name().size() - 1));
458 bool MathCursor::erase()
460 autocorrect_ = false;
469 // delete empty cells if possible
471 if (inset()->idxDelete(idx()))
474 // old behaviour when in last position of cell
475 if (pos() == size()) {
476 if (inset()->ncols() == 1 && inset()->nrows() == 1 && depth() == 1 && size() == 0)
479 inset()->idxGlue(idx());
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\n";
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.inset_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
635 void MathCursor::handleNest(MathAtom const & a)
638 asArray(grabAndEraseSelection(), at.nucleus()->cell(0));
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().inset_;
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].inset_->asGridInset();
746 idx = Cursor_[i].idx_;
754 void MathCursor::popToHere(MathInset const * p)
756 while (depth() && Cursor_.back().inset_ != p)
761 void MathCursor::popToEnclosingGrid()
763 while (depth() && !Cursor_.back().inset_->asGridInset())
768 void MathCursor::popToEnclosingHull()
770 while (depth() && !Cursor_.back().inset_->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() = min(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
842 return array()[pos() - 1];
846 MathAtom & MathCursor::prevAtom()
849 return array()[pos() - 1];
853 MathAtom const & MathCursor::nextAtom() const
855 Assert(pos() < size());
856 return array()[pos()];
860 MathAtom & MathCursor::nextAtom()
862 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\n";
877 lyxerr << "############ depth() == 0 not valid\n";
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(CursorPos & i1, CursorPos & i2) const
915 CursorPos anc = normalAnchor();
916 if (anc < cursor()) {
926 CursorPos & MathCursor::cursor()
929 return Cursor_.back();
933 CursorPos const & MathCursor::cursor() const
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
958 MathScriptInset const * p = prevAtom()->asScriptInset();
959 if (p && p->has(up)) {
962 idx() = up; // the superscript has index 1
964 ///lyxerr << "updown: handled by scriptinset to the left\n";
971 MathScriptInset const * p = nextAtom()->asScriptInset();
972 if (p && p->has(up)) {
976 ///lyxerr << "updown: handled by scriptinset to the right\n";
981 // try current cell for e.g. text insets
982 if (inset()->idxUpDown2(idx(), pos(), up, targetx_))
985 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
990 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
991 // lyxerr << "updown: handled by brute find in the same cell\n";
995 // try to find an inset that knows better then we
997 ///lyxerr << "updown: We are in " << *inset() << " idx: " << idx() << '\n';
999 if (inset()->idxUpDown(idx(), pos(), up, targetx_)) {
1000 // try to find best position within this inset
1006 // no such inset found, just take something "above"
1007 ///lyxerr << "updown: handled by strange case\n";
1013 up ? formula()->ylow() : yo + 4,
1014 up ? yo - 4 : formula()->yhigh()
1017 // any improvement so far?
1020 if (up ? ynew < yo : ynew > yo)
1026 bool MathCursor::bruteFind
1027 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1029 MathIterator best_cursor;
1030 double best_dist = 1e10;
1032 MathIterator it = ibegin(formula()->par().nucleus());
1033 MathIterator et = iend(formula()->par().nucleus());
1035 // avoid invalid nesting when selecting
1036 if (!selection_ || positionable(it, Anchor_)) {
1038 it.back().getPos(xo, yo);
1039 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1040 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1041 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1042 // '<=' in order to take the last possible position
1043 // this is important for clicking behind \sum in e.g. '\sum_i a'
1044 if (d <= best_dist) {
1056 if (best_dist < 1e10)
1057 Cursor_ = best_cursor;
1058 return best_dist < 1e10;
1062 void MathCursor::bruteFind2(int x, int y)
1064 double best_dist = 1e10;
1066 MathIterator it = Cursor_;
1067 it.back().setPos(0);
1068 MathIterator et = Cursor_;
1069 et.back().setPos(it.cell().size());
1070 for (int i = 0; ; ++i) {
1072 it.back().getPos(xo, yo);
1073 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1074 // '<=' in order to take the last possible position
1075 // this is important for clicking behind \sum in e.g. '\sum_i a'
1076 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1077 if (d <= best_dist) {
1088 bool MathCursor::idxLineLast()
1090 idx() -= idx() % inset()->ncols();
1091 idx() += inset()->ncols() - 1;
1096 bool MathCursor::idxLeft()
1098 return inset()->idxLeft(idx(), pos());
1102 bool MathCursor::idxRight()
1104 return inset()->idxRight(idx(), pos());
1108 bool MathCursor::script(bool up)
1110 // Hack to get \\^ and \\_ working
1111 if (inMacroMode() && macroName() == "\\") {
1113 niceInsert(createMathInset("mathcircumflex"));
1120 string safe = grabAndEraseSelection();
1122 // we are in a nucleus of a script inset, move to _our_ script
1123 inset()->asScriptInset()->ensure(up);
1126 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1127 prevAtom().nucleus()->asScriptInset()->ensure(up);
1128 pushRight(prevAtom());
1131 } else if (hasPrevAtom()) {
1133 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1134 pushLeft(nextAtom());
1138 plainInsert(MathAtom(new MathScriptInset(up)));
1139 prevAtom().nucleus()->asScriptInset()->ensure(up);
1140 pushRight(prevAtom());
1150 bool MathCursor::interpret(char c)
1152 //lyxerr << "interpret 2: '" << c << "'\n";
1153 targetx_ = -1; // "no target"
1154 if (inMacroArgMode()) {
1158 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1159 if (p && 1 <= n && n <= p->numargs())
1160 insert(MathAtom(new MathMacroArgument(c - '0')));
1162 insert(createMathInset("#"));
1163 interpret(c); // try again
1169 if (inMacroMode()) {
1170 string name = macroName();
1171 //lyxerr << "interpret name: '" << name << "'\n";
1174 activeMacro()->setName(activeMacro()->name() + c);
1178 // handle 'special char' macros
1183 if (currentMode() == MathInset::TEXT_MODE)
1184 niceInsert(createMathInset("textbackslash"));
1186 niceInsert(createMathInset("backslash"));
1187 } else if (c == '{') {
1188 niceInsert(MathAtom(new MathBraceInset));
1190 niceInsert(createMathInset(string(1, c)));
1195 // leave macro mode and try again if necessary
1198 niceInsert(MathAtom(new MathBraceInset));
1204 // This is annoying as one has to press <space> far too often.
1208 // leave autocorrect mode if necessary
1209 if (autocorrect_ && c == ' ') {
1210 autocorrect_ = false;
1215 // just clear selection on pressing the space bar
1216 if (selection_ && c == ' ') {
1224 //lyxerr << "starting with macro\n";
1225 insert(MathAtom(new MathUnknownInset("\\", false)));
1230 if (currentMode() == MathInset::TEXT_MODE)
1236 if (currentMode() == MathInset::TEXT_MODE) {
1237 // insert spaces in text mode,
1238 // but suppress direct insertion of two spaces in a row
1239 // the still allows typing '<space>a<space>' and deleting the 'a', but
1240 // it is better than nothing...
1241 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1245 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1246 prevAtom().nucleus()->asSpaceInset()->incSpace();
1251 // if are at the very end, leave the formula
1252 return pos() != size();
1255 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1256 createMathInset(string(1, c));
1261 niceInsert(MathAtom(new MathCommentInset));
1265 // try auto-correction
1266 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1269 // no special circumstances, so insert the character without any fuss
1271 autocorrect_ = true;
1276 void MathCursor::setSelection(MathIterator const & where, size_type n)
1285 void MathCursor::insetToggle()
1287 if (hasNextAtom()) {
1288 // toggle previous inset ...
1289 nextAtom().nucleus()->lock(!nextAtom()->lock());
1290 } else if (popLeft() && hasNextAtom()) {
1291 // ... or enclosing inset if we are in the last inset position
1292 nextAtom().nucleus()->lock(!nextAtom()->lock());
1298 string MathCursor::info() const
1301 os << "Math editor mode. ";
1302 for (int i = 0, n = depth(); i < n; ++i) {
1303 Cursor_[i].inset_->infoize(os);
1307 prevAtom()->infoize2(os);
1309 return STRCONV(os.str());
1313 unsigned MathCursor::depth() const
1315 return Cursor_.size();
1323 void region(CursorPos const & i1, CursorPos const & i2,
1324 MathInset::row_type & r1, MathInset::row_type & r2,
1325 MathInset::col_type & c1, MathInset::col_type & c2)
1327 MathInset * p = i1.inset_;
1328 c1 = p->col(i1.idx_);
1329 c2 = p->col(i2.idx_);
1332 r1 = p->row(i1.idx_);
1333 r2 = p->row(i2.idx_);
1341 string MathCursor::grabSelection() const
1348 getSelection(i1, i2);
1350 if (i1.idx_ == i2.idx_) {
1351 MathArray::const_iterator it = i1.cell().begin();
1352 return asString(MathArray(it + i1.pos_, it + i2.pos_));
1357 region(i1, i2, r1, r2, c1, c2);
1360 for (row_type row = r1; row <= r2; ++row) {
1363 for (col_type col = c1; col <= c2; ++col) {
1366 data += asString(i1.inset_->cell(i1.inset_->index(row, col)));
1373 void MathCursor::eraseSelection()
1377 getSelection(i1, i2);
1378 if (i1.idx_ == i2.idx_)
1379 i1.cell().erase(i1.pos_, i2.pos_);
1381 MathInset * p = i1.inset_;
1384 region(i1, i2, r1, r2, c1, c2);
1385 for (row_type row = r1; row <= r2; ++row)
1386 for (col_type col = c1; col <= c2; ++col)
1387 p->cell(p->index(row, col)).clear();
1393 string MathCursor::grabAndEraseSelection()
1397 string res = grabSelection();
1404 CursorPos MathCursor::normalAnchor() const
1406 if (Anchor_.size() < depth()) {
1408 lyxerr << "unusual Anchor size\n";
1410 //lyx::Assert(Anchor_.size() >= cursor.depth());
1411 // use Anchor on the same level as Cursor
1412 CursorPos normal = Anchor_[depth() - 1];
1413 if (depth() < Anchor_.size() && !(normal < cursor())) {
1414 // anchor is behind cursor -> move anchor behind the inset
1421 dispatch_result MathCursor::dispatch(FuncRequest const & cmd)
1423 // mouse clicks are somewhat special
1425 switch (cmd.action) {
1426 case LFUN_MOUSE_PRESS:
1427 case LFUN_MOUSE_MOTION:
1428 case LFUN_MOUSE_RELEASE:
1429 case LFUN_MOUSE_DOUBLE: {
1430 CursorPos & pos = Cursor_.back();
1431 dispatch_result res = UNDISPATCHED;
1434 if (x < cmd.x && hasPrevAtom()) {
1435 res = prevAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1436 if (res != UNDISPATCHED)
1439 if (x > cmd.x && hasNextAtom()) {
1440 res = nextAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1441 if (res != UNDISPATCHED)
1449 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1450 CursorPos & pos = Cursor_[i];
1451 dispatch_result res = pos.inset_->dispatch(cmd, pos.idx_, pos.pos_);
1452 if (res != UNDISPATCHED) {
1453 if (res == DISPATCHED_POP) {
1454 Cursor_.shrink(i + 1);
1460 return UNDISPATCHED;
1464 MathInset::mode_type MathCursor::currentMode() const
1466 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1467 MathInset::mode_type res = Cursor_[i].inset_->currentMode();
1468 if (res != MathInset::UNDECIDED_MODE)
1471 return MathInset::UNDECIDED_MODE;
1475 void MathCursor::handleFont(string const & font)
1480 safe = grabAndEraseSelection();
1483 if (array().size()) {
1484 // something left in the cell
1486 // cursor in first position
1488 } else if (pos() == array().size()) {
1489 // cursor in last position
1492 // cursor in between. split cell
1493 MathArray::iterator bt = array().begin();
1494 MathAtom at = createMathInset(font);
1495 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1496 cursor().cell().erase(bt, bt + pos());
1501 // nothing left in the cell
1509 void releaseMathCursor(BufferView * bv)
1512 InsetFormulaBase * f = mathcursor->formula();