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.
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24 #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"
57 using std::ostringstream;
61 // matheds own cut buffer
62 limited_stack<string> theCutBuffer;
65 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
66 : formula_(formula), autocorrect_(false), selection_(false), targetx_(-1)
68 front ? first() : last();
73 MathCursor::~MathCursor()
75 // ensure that 'notifyCursorLeave' is called
81 void MathCursor::push(MathAtom & t)
83 Cursor_.push_back(MathCursorPos(t.nucleus()));
87 void MathCursor::pushLeft(MathAtom & t)
89 //lyxerr << "Entering atom " << t << " left\n";
91 t->idxFirst(idx(), pos());
95 void MathCursor::pushRight(MathAtom & t)
97 //lyxerr << "Entering atom " << t << " right\n";
100 t->idxLast(idx(), pos());
104 bool MathCursor::popLeft()
106 //lyxerr << "Leaving atom to the left\n";
109 par()->notifyCursorLeaves(idx());
112 par()->notifyCursorLeaves(idx());
118 bool MathCursor::popRight()
120 //lyxerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
123 par()->notifyCursorLeaves(idx());
126 par()->notifyCursorLeaves(idx());
135 void MathCursor::dump(char const * what) const
137 lyxerr << "MC: " << what << endl;
138 lyxerr << " Cursor: " << depth() << endl;
139 for (unsigned i = 0; i < depth(); ++i)
140 lyxerr << " i: " << i << ' ' << Cursor_[i] << endl;
141 lyxerr << " Anchor: " << Anchor_.size() << endl;
142 for (unsigned i = 0; i < Anchor_.size(); ++i)
143 lyxerr << " i: " << i << ' ' << Anchor_[i] << endl;
144 lyxerr << " sel: " << selection_ << endl;
147 void MathCursor::dump(char const *) const {}
151 bool MathCursor::isInside(MathInset const * p) const
153 for (unsigned i = 0; i < depth(); ++i)
154 if (Cursor_[i].par_ == p)
160 bool MathCursor::openable(MathAtom const & t, bool sel) const
169 // we can't move into anything new during selection
170 if (depth() == Anchor_.size())
172 if (t.operator->() != Anchor_[depth()].par_)
179 bool MathCursor::inNucleus() const
181 return par()->asScriptInset() && idx() == 2;
185 bool MathCursor::posLeft()
194 bool MathCursor::posRight()
203 bool MathCursor::left(bool sel)
206 autocorrect_ = false;
207 targetx_ = -1; // "no target"
214 if (hasPrevAtom() && openable(prevAtom(), sel)) {
215 pushRight(prevAtom());
219 return posLeft() || idxLeft() || popLeft() || selection_;
223 bool MathCursor::right(bool sel)
226 autocorrect_ = false;
227 targetx_ = -1; // "no target"
234 if (hasNextAtom() && openable(nextAtom(), sel)) {
235 pushLeft(nextAtom());
239 return posRight() || idxRight() || popRight() || selection_;
243 void MathCursor::first()
246 push(formula_->par());
247 par()->idxFirst(idx(), pos());
251 void MathCursor::last()
254 push(formula_->par());
255 par()->idxLast(idx(), pos());
260 (MathIterator const & cursor, MathIterator const & anchor)
262 // avoid deeper nested insets when selecting
263 if (cursor.size() > anchor.size())
266 // anchor might be deeper, should have same path then
267 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
268 if (cursor[i].par_ != anchor[i].par_)
271 // position should be ok.
276 void MathCursor::setPos(int x, int y)
279 bool res = bruteFind(x, y,
280 formula()->xlow(), formula()->xhigh(),
281 formula()->ylow(), formula()->yhigh());
283 // this can happen on creation of "math-display"
287 targetx_ = -1; // "no target"
293 bool MathCursor::home(bool sel)
296 autocorrect_ = false;
299 if (!par()->idxHome(idx(), pos()))
302 targetx_ = -1; // "no target"
307 bool MathCursor::end(bool sel)
310 autocorrect_ = false;
313 if (!par()->idxEnd(idx(), pos()))
316 targetx_ = -1; // "no target"
321 void MathCursor::plainErase()
323 array().erase(pos());
327 void MathCursor::markInsert()
329 //lyxerr << "inserting mark\n";
330 array().insert(pos(), MathAtom(new MathCharInset(0)));
334 void MathCursor::markErase()
336 //lyxerr << "deleting mark\n";
337 array().erase(pos());
341 void MathCursor::plainInsert(MathAtom const & t)
344 array().insert(pos(), t);
349 void MathCursor::insert(string const & str)
351 //lyxerr << "inserting '" << str << "'\n";
353 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
354 plainInsert(MathAtom(new MathCharInset(*it)));
358 void MathCursor::insert(char c)
360 //lyxerr << "inserting '" << c << "'\n";
362 plainInsert(MathAtom(new MathCharInset(c)));
366 void MathCursor::insert(MathAtom const & t)
374 void MathCursor::niceInsert(string const & t)
376 MathArray ar = asArray(t);
384 void MathCursor::niceInsert(MathAtom const & t)
387 string safe = grabAndEraseSelection();
389 // enter the new inset and move the contents of the selection if possible
392 pushLeft(nextAtom());
398 void MathCursor::insert(MathArray const & ar)
403 array().insert(pos(), ar);
408 void MathCursor::paste(string const & data)
410 dispatch(FuncRequest(LFUN_PASTE, data));
414 bool MathCursor::backspace()
416 autocorrect_ = false;
424 if (par()->ncols() == 1 &&
425 par()->nrows() == 1 &&
434 MathUnknownInset * p = activeMacro();
435 if (p->name().size() > 1) {
436 p->setName(p->name().substr(0, p->name().size() - 1));
447 bool MathCursor::erase()
449 autocorrect_ = false;
458 // delete empty cells if possible
460 if (par()->idxDelete(idx()))
463 // old behaviour when in last position of cell
464 if (pos() == size()) {
465 if (par()->ncols() == 1 && par()->nrows() == 1 && depth() == 1 && size() == 0)
468 par()->idxGlue(idx());
478 bool MathCursor::up(bool sel)
483 MathIterator save = Cursor_;
487 autocorrect_ = false;
492 bool MathCursor::down(bool sel)
497 MathIterator save = Cursor_;
501 autocorrect_ = false;
506 void MathCursor::macroModeClose()
510 MathUnknownInset * p = activeMacro();
512 string s = p->name();
514 array().erase(pos());
516 // do nothing if the macro name is empty
520 string name = s.substr(1);
522 // prevent entering of recursive macros
523 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
524 && formula()->getInsetName() == name)
525 lyxerr << "can't enter recursive macro\n";
527 niceInsert(createMathInset(name));
531 string MathCursor::macroName() const
533 return inMacroMode() ? activeMacro()->name() : string();
537 void MathCursor::selClear()
544 void MathCursor::selCopy()
548 theCutBuffer.push(grabSelection());
551 //theCutBuffer.erase();
556 void MathCursor::selCut()
559 theCutBuffer.push(grabAndEraseSelection());
563 void MathCursor::selDel()
573 void MathCursor::selPaste(int n)
577 if (n < theCutBuffer.size())
578 paste(theCutBuffer[n]);
584 void MathCursor::selHandle(bool sel)
586 if (sel == selection_)
594 void MathCursor::selStart()
604 void MathCursor::selClearOrDel()
606 if (lyxrc.auto_region_delete)
613 void MathCursor::drawSelection(PainterInfo & pi) const
619 getSelection(i1, i2);
620 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
624 void MathCursor::handleNest(MathAtom const & a)
627 at.nucleus()->cell(0) = asArray(grabAndEraseSelection());
629 pushRight(prevAtom());
633 void MathCursor::getPos(int & x, int & y) const
635 par()->getPos(idx(), pos(), x, y);
639 int MathCursor::targetX() const
649 MathInset * MathCursor::par() const
651 return cursor().par_;
655 InsetFormulaBase * MathCursor::formula() const
661 MathCursor::idx_type MathCursor::idx() const
663 return cursor().idx_;
667 MathCursor::idx_type & MathCursor::idx()
669 return cursor().idx_;
673 MathCursor::pos_type MathCursor::pos() const
675 return cursor().pos_;
679 void MathCursor::adjust(pos_type from, difference_type diff)
681 if (cursor().pos_ > from)
682 cursor().pos_ += diff;
683 if (Anchor_.back().pos_ > from)
684 Anchor_.back().pos_ += diff;
685 // just to be on the safe side
686 // theoretically unecessary
691 MathCursor::pos_type & MathCursor::pos()
693 return cursor().pos_;
697 bool MathCursor::inMacroMode() const
701 MathUnknownInset const * p = prevAtom()->asUnknownInset();
702 return p && !p->final();
706 MathUnknownInset * MathCursor::activeMacro()
708 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
712 MathUnknownInset const * MathCursor::activeMacro() const
714 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
718 bool MathCursor::inMacroArgMode() const
720 return pos() > 0 && prevAtom()->getChar() == '#';
724 bool MathCursor::selection() const
730 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
732 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
733 MathGridInset * p = Cursor_[i].par_->asGridInset();
735 idx = Cursor_[i].idx_;
743 void MathCursor::popToHere(MathInset const * p)
745 while (depth() && Cursor_.back().par_ != p)
750 void MathCursor::popToEnclosingGrid()
752 while (depth() && !Cursor_.back().par_->asGridInset())
757 void MathCursor::popToEnclosingHull()
759 while (depth() && !Cursor_.back().par_->asHullInset())
764 void MathCursor::pullArg()
767 MathArray a = array();
770 array().insert(pos(), a);
773 formula()->mutateToText();
778 void MathCursor::touch()
780 MathIterator::const_iterator it = Cursor_.begin();
781 MathIterator::const_iterator et = Cursor_.end();
782 for ( ; it != et; ++it)
787 void MathCursor::normalize()
789 if (idx() >= par()->nargs()) {
790 lyxerr << "this should not really happen - 1: "
791 << idx() << ' ' << par()->nargs()
792 << " in: " << par() << endl;
795 idx() = min(idx(), par()->nargs() - 1);
797 if (pos() > size()) {
798 lyxerr << "this should not really happen - 2: "
799 << pos() << ' ' << size() << " in idx: " << idx()
801 WriteStream wi(lyxerr, false, true);
806 pos() = min(pos(), size());
810 MathCursor::size_type MathCursor::size() const
812 return array().size();
816 bool MathCursor::hasPrevAtom() const
822 bool MathCursor::hasNextAtom() const
824 return pos() < size();
828 MathAtom const & MathCursor::prevAtom() const
830 lyx::Assert(pos() > 0);
831 return array()[pos() - 1];
835 MathAtom & MathCursor::prevAtom()
837 lyx::Assert(pos() > 0);
838 return array()[pos() - 1];
842 MathAtom const & MathCursor::nextAtom() const
844 lyx::Assert(pos() < size());
845 return array()[pos()];
849 MathAtom & MathCursor::nextAtom()
851 lyx::Assert(pos() < size());
852 return array()[pos()];
856 MathArray & MathCursor::array() const
858 static MathArray dummy;
860 if (idx() >= par()->nargs()) {
861 lyxerr << "############ idx_ " << idx() << " not valid\n";
866 lyxerr << "############ depth() == 0 not valid\n";
870 return cursor().cell();
874 void MathCursor::idxNext()
876 par()->idxNext(idx(), pos());
880 void MathCursor::idxPrev()
882 par()->idxPrev(idx(), pos());
886 char MathCursor::valign() const
889 MathGridInset * p = enclosingGrid(idx);
890 return p ? p->valign() : '\0';
894 char MathCursor::halign() const
897 MathGridInset * p = enclosingGrid(idx);
898 return p ? p->halign(idx % p->ncols()) : '\0';
902 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
904 MathCursorPos anc = normalAnchor();
905 if (anc < cursor()) {
915 MathCursorPos & MathCursor::cursor()
917 lyx::Assert(depth());
918 return Cursor_.back();
922 MathCursorPos const & MathCursor::cursor() const
924 lyx::Assert(depth());
925 return Cursor_.back();
929 bool MathCursor::goUpDown(bool up)
931 // Be warned: The 'logic' implemented in this function is highly fragile.
932 // A distance of one pixel or a '<' vs '<=' _really_ matters.
933 // So fiddle around with it only if you know what you are doing!
938 // check if we had something else in mind, if not, this is the future goal
944 // try neigbouring script insets
947 MathScriptInset const * p = prevAtom()->asScriptInset();
948 if (p && p->has(up)) {
951 idx() = up; // the superscript has index 1
953 ///lyxerr << "updown: handled by scriptinset to the left\n";
960 MathScriptInset const * p = nextAtom()->asScriptInset();
961 if (p && p->has(up)) {
965 ///lyxerr << "updown: handled by scriptinset to the right\n";
970 // try current cell for e.g. text insets
971 if (par()->idxUpDown2(idx(), pos(), up, targetx_))
974 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
979 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
980 // lyxerr << "updown: handled by brute find in the same cell\n";
984 // try to find an inset that knows better then we
986 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
988 if (par()->idxUpDown(idx(), pos(), up, targetx_)) {
989 // try to find best position within this inset
995 // no such inset found, just take something "above"
996 ///lyxerr << "updown: handled by strange case\n";
1002 up ? formula()->ylow() : yo + 4,
1003 up ? yo - 4 : formula()->yhigh()
1006 // any improvement so far?
1009 if (up ? ynew < yo : ynew > yo)
1015 bool MathCursor::bruteFind
1016 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1018 MathIterator best_cursor;
1019 double best_dist = 1e10;
1021 MathIterator it = ibegin(formula()->par().nucleus());
1022 MathIterator et = iend(formula()->par().nucleus());
1024 // avoid invalid nesting when selecting
1025 if (!selection_ || positionable(it, Anchor_)) {
1027 it.back().getPos(xo, yo);
1028 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1029 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1030 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1031 // '<=' in order to take the last possible position
1032 // this is important for clicking behind \sum in e.g. '\sum_i a'
1033 if (d <= best_dist) {
1045 if (best_dist < 1e10)
1046 Cursor_ = best_cursor;
1047 return best_dist < 1e10;
1051 void MathCursor::bruteFind2(int x, int y)
1053 double best_dist = 1e10;
1055 MathIterator it = Cursor_;
1056 it.back().setPos(0);
1057 MathIterator et = Cursor_;
1058 et.back().setPos(it.cell().size());
1059 for (int i = 0; ; ++i) {
1061 it.back().getPos(xo, yo);
1062 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1063 // '<=' in order to take the last possible position
1064 // this is important for clicking behind \sum in e.g. '\sum_i a'
1065 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1066 if (d <= best_dist) {
1077 bool MathCursor::idxLineLast()
1079 idx() -= idx() % par()->ncols();
1080 idx() += par()->ncols() - 1;
1085 bool MathCursor::idxLeft()
1087 return par()->idxLeft(idx(), pos());
1091 bool MathCursor::idxRight()
1093 return par()->idxRight(idx(), pos());
1097 bool MathCursor::script(bool up)
1099 // Hack to get \\^ and \\_ working
1100 if (inMacroMode() && macroName() == "\\") {
1102 niceInsert(createMathInset("mathcircumflex"));
1109 string safe = grabAndEraseSelection();
1111 // we are in a nucleus of a script inset, move to _our_ script
1112 par()->asScriptInset()->ensure(up);
1115 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1116 prevAtom().nucleus()->asScriptInset()->ensure(up);
1117 pushRight(prevAtom());
1120 } else if (hasPrevAtom()) {
1122 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1123 pushLeft(nextAtom());
1127 plainInsert(MathAtom(new MathScriptInset(up)));
1128 prevAtom().nucleus()->asScriptInset()->ensure(up);
1129 pushRight(prevAtom());
1139 bool MathCursor::interpret(char c)
1141 //lyxerr << "interpret 2: '" << c << "'\n";
1142 targetx_ = -1; // "no target"
1143 if (inMacroArgMode()) {
1147 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1148 if (p && 1 <= n && n <= p->numargs())
1149 insert(MathAtom(new MathMacroArgument(c - '0')));
1151 insert(createMathInset("#"));
1152 interpret(c); // try again
1158 if (inMacroMode()) {
1159 string name = macroName();
1160 //lyxerr << "interpret name: '" << name << "'\n";
1163 activeMacro()->setName(activeMacro()->name() + c);
1167 // handle 'special char' macros
1172 if (currentMode() == MathInset::TEXT_MODE)
1173 niceInsert(createMathInset("textbackslash"));
1175 niceInsert(createMathInset("backslash"));
1176 } else if (c == '{') {
1177 niceInsert(MathAtom(new MathBraceInset));
1179 niceInsert(createMathInset(string(1, c)));
1184 // leave macro mode and try again if necessary
1187 niceInsert(MathAtom(new MathBraceInset));
1193 // This is annoying as one has to press <space> far too often.
1197 // leave autocorrect mode if necessary
1198 if (autocorrect_ && c == ' ') {
1199 autocorrect_ = false;
1204 // just clear selection on pressing the space bar
1205 if (selection_ && c == ' ') {
1213 //lyxerr << "starting with macro\n";
1214 insert(MathAtom(new MathUnknownInset("\\", false)));
1219 if (currentMode() == MathInset::TEXT_MODE)
1225 if (currentMode() == MathInset::TEXT_MODE) {
1226 // insert spaces in text mode,
1227 // but suppress direct insertion of two spaces in a row
1228 // the still allows typing '<space>a<space>' and deleting the 'a', but
1229 // it is better than nothing...
1230 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1234 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1235 prevAtom().nucleus()->asSpaceInset()->incSpace();
1240 // if are at the very end, leave the formula
1241 return pos() != size();
1244 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1245 createMathInset(string(1, c));
1250 niceInsert(MathAtom(new MathCommentInset));
1254 // try auto-correction
1255 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1258 // no special circumstances, so insert the character without any fuss
1260 autocorrect_ = true;
1265 void MathCursor::setSelection(MathIterator const & where, size_type n)
1274 void MathCursor::insetToggle()
1276 if (hasNextAtom()) {
1277 // toggle previous inset ...
1278 nextAtom().nucleus()->lock(!nextAtom()->lock());
1279 } else if (popLeft() && hasNextAtom()) {
1280 // ... or enclosing inset if we are in the last inset position
1281 nextAtom().nucleus()->lock(!nextAtom()->lock());
1287 string MathCursor::info() const
1290 os << "Math editor mode. ";
1291 for (int i = 0, n = depth(); i < n; ++i) {
1292 Cursor_[i].par_->infoize(os);
1296 prevAtom()->infoize2(os);
1298 return STRCONV(os.str());
1302 unsigned MathCursor::depth() const
1304 return Cursor_.size();
1312 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1313 MathInset::row_type & r1, MathInset::row_type & r2,
1314 MathInset::col_type & c1, MathInset::col_type & c2)
1316 MathInset * p = i1.par_;
1317 c1 = p->col(i1.idx_);
1318 c2 = p->col(i2.idx_);
1321 r1 = p->row(i1.idx_);
1322 r2 = p->row(i2.idx_);
1330 string MathCursor::grabSelection() const
1337 getSelection(i1, i2);
1339 if (i1.idx_ == i2.idx_) {
1340 MathArray::const_iterator it = i1.cell().begin();
1341 return asString(MathArray(it + i1.pos_, it + i2.pos_));
1346 region(i1, i2, r1, r2, c1, c2);
1349 for (row_type row = r1; row <= r2; ++row) {
1352 for (col_type col = c1; col <= c2; ++col) {
1355 data += asString(i1.par_->cell(i1.par_->index(row, col)));
1362 void MathCursor::eraseSelection()
1366 getSelection(i1, i2);
1367 if (i1.idx_ == i2.idx_)
1368 i1.cell().erase(i1.pos_, i2.pos_);
1370 MathInset * p = i1.par_;
1373 region(i1, i2, r1, r2, c1, c2);
1374 for (row_type row = r1; row <= r2; ++row)
1375 for (col_type col = c1; col <= c2; ++col)
1376 p->cell(p->index(row, col)).clear();
1382 string MathCursor::grabAndEraseSelection()
1386 string res = grabSelection();
1393 MathCursorPos MathCursor::normalAnchor() const
1395 if (Anchor_.size() < depth()) {
1397 lyxerr << "unusual Anchor size\n";
1399 //lyx::Assert(Anchor_.size() >= cursor.depth());
1400 // use Anchor on the same level as Cursor
1401 MathCursorPos normal = Anchor_[depth() - 1];
1402 if (depth() < Anchor_.size() && !(normal < cursor())) {
1403 // anchor is behind cursor -> move anchor behind the inset
1410 dispatch_result MathCursor::dispatch(FuncRequest const & cmd)
1412 // mouse clicks are somewhat special
1414 switch (cmd.action) {
1415 case LFUN_MOUSE_PRESS:
1416 case LFUN_MOUSE_MOTION:
1417 case LFUN_MOUSE_RELEASE:
1418 case LFUN_MOUSE_DOUBLE: {
1419 MathCursorPos & pos = Cursor_.back();
1420 dispatch_result res = UNDISPATCHED;
1423 if (x < cmd.x && hasPrevAtom()) {
1424 res = prevAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1425 if (res != UNDISPATCHED)
1428 if (x > cmd.x && hasNextAtom()) {
1429 res = nextAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
1430 if (res != UNDISPATCHED)
1438 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1439 MathCursorPos & pos = Cursor_[i];
1440 dispatch_result res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_);
1441 if (res != UNDISPATCHED) {
1442 if (res == DISPATCHED_POP) {
1443 Cursor_.shrink(i + 1);
1449 return UNDISPATCHED;
1453 MathInset::mode_type MathCursor::currentMode() const
1455 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1456 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1457 if (res != MathInset::UNDECIDED_MODE)
1460 return MathInset::UNDECIDED_MODE;
1464 void MathCursor::handleFont(string const & font)
1469 safe = grabAndEraseSelection();
1472 if (array().size()) {
1473 // something left in the cell
1475 // cursor in first position
1477 } else if (pos() == array().size()) {
1478 // cursor in last position
1481 // cursor in between. split cell
1482 MathArray::iterator bt = array().begin();
1483 MathAtom at = createMathInset(font);
1484 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1485 cursor().cell().erase(bt, bt + pos());
1490 // nothing left in the cell
1498 void releaseMathCursor(BufferView * bv)
1501 InsetFormulaBase * f = mathcursor->formula();
1502 f->hideInsetCursor(bv);