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 #pragma implementation
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "BufferView.h"
30 #include "frontends/Painter.h"
31 #include "math_cursor.h"
32 #include "formulabase.h"
33 #include "funcrequest.h"
34 #include "math_autocorrect.h"
35 #include "math_arrayinset.h"
36 #include "math_braceinset.h"
37 #include "math_commentinset.h"
38 #include "math_charinset.h"
39 #include "math_extern.h"
40 #include "math_factory.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macroarg.h"
44 #include "math_macrotemplate.h"
45 #include "math_mathmlstream.h"
46 #include "math_parser.h"
47 #include "math_replace.h"
48 #include "math_scriptinset.h"
49 #include "math_spaceinset.h"
50 #include "math_support.h"
51 #include "math_unknowninset.h"
63 using std::ostringstream;
67 // matheds own cut buffer
68 MathGridInset theCutBuffer = MathGridInset(1, 1);
71 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
72 : formula_(formula), autocorrect_(false), selection_(false)
74 front ? first() : last();
79 MathCursor::~MathCursor()
81 // ensure that 'notifyCursorLeave' is called
87 void MathCursor::push(MathAtom & t)
89 Cursor_.push_back(MathCursorPos(t.nucleus()));
93 void MathCursor::pushLeft(MathAtom & t)
95 //lyxerr << "Entering atom " << t << " left\n";
97 t->idxFirst(idx(), pos());
101 void MathCursor::pushRight(MathAtom & t)
103 //lyxerr << "Entering atom " << t << " right\n";
106 t->idxLast(idx(), pos());
110 bool MathCursor::popLeft()
112 //cerr << "Leaving atom to the left\n";
115 par()->notifyCursorLeaves();
118 par()->notifyCursorLeaves();
124 bool MathCursor::popRight()
126 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
129 par()->notifyCursorLeaves();
132 par()->notifyCursorLeaves();
141 void MathCursor::dump(char const * what) const
143 lyxerr << "MC: " << what << "\n";
144 lyxerr << " Cursor: " << depth() << "\n";
145 for (unsigned i = 0; i < depth(); ++i)
146 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
147 lyxerr << " Anchor: " << Anchor_.size() << "\n";
148 for (unsigned i = 0; i < Anchor_.size(); ++i)
149 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
150 lyxerr << " sel: " << selection_ << "\n";
153 void MathCursor::dump(char const *) const {}
157 bool MathCursor::isInside(MathInset const * p) const
159 for (unsigned i = 0; i < depth(); ++i)
160 if (Cursor_[i].par_ == p)
166 bool MathCursor::openable(MathAtom const & t, bool sel) const
175 // we can't move into anything new during selection
176 if (depth() == Anchor_.size())
178 if (t.operator->() != Anchor_[depth()].par_)
185 bool MathCursor::inNucleus() const
187 return par()->asScriptInset() && idx() == 2;
191 bool MathCursor::posLeft()
200 bool MathCursor::posRight()
209 bool MathCursor::left(bool sel)
212 autocorrect_ = false;
213 targetx_ = -1; // "no target"
220 if (hasPrevAtom() && openable(prevAtom(), sel)) {
221 pushRight(prevAtom());
225 return posLeft() || idxLeft() || popLeft() || selection_;
229 bool MathCursor::right(bool sel)
232 autocorrect_ = false;
233 targetx_ = -1; // "no target"
240 if (hasNextAtom() && openable(nextAtom(), sel)) {
241 pushLeft(nextAtom());
245 return posRight() || idxRight() || popRight() || selection_;
249 void MathCursor::first()
252 push(formula_->par());
253 par()->idxFirst(idx(), pos());
257 void MathCursor::last()
260 push(formula_->par());
261 par()->idxLast(idx(), pos());
266 (MathIterator const & cursor, MathIterator const & anchor)
268 // avoid deeper nested insets when selecting
269 if (cursor.size() > anchor.size())
272 // anchor might be deeper, should have same path then
273 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
274 if (cursor[i].par_ != anchor[i].par_)
277 // position should be ok.
282 void MathCursor::setPos(int x, int y)
285 bool res = bruteFind(x, y,
286 formula()->xlow(), formula()->xhigh(),
287 formula()->ylow(), formula()->yhigh());
289 // this can happen on creation of "math-display"
293 targetx_ = -1; // "no target"
299 bool MathCursor::home(bool sel)
302 autocorrect_ = false;
305 if (!par()->idxHome(idx(), pos()))
308 targetx_ = -1; // "no target"
313 bool MathCursor::end(bool sel)
316 autocorrect_ = false;
319 if (!par()->idxEnd(idx(), pos()))
322 targetx_ = -1; // "no target"
327 void MathCursor::plainErase()
329 array().erase(pos());
333 void MathCursor::markInsert()
335 //lyxerr << "inserting mark\n";
336 array().insert(pos(), MathAtom(new MathCharInset(0)));
340 void MathCursor::markErase()
342 //lyxerr << "deleting mark\n";
343 array().erase(pos());
347 void MathCursor::plainInsert(MathAtom const & t)
350 array().insert(pos(), t);
355 void MathCursor::insert(string const & str)
357 //lyxerr << "inserting '" << str << "'\n";
359 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
360 plainInsert(MathAtom(new MathCharInset(*it)));
364 void MathCursor::insert(char c)
366 //lyxerr << "inserting '" << c << "'\n";
368 plainInsert(MathAtom(new MathCharInset(c)));
372 void MathCursor::insert(MathAtom const & t)
380 void MathCursor::niceInsert(MathAtom const & t)
383 MathGridInset safe = grabAndEraseSelection();
385 // enter the new inset and move the contents of the selection if possible
388 pushLeft(nextAtom());
394 void MathCursor::insert(MathArray const & ar)
399 array().insert(pos(), ar);
404 void MathCursor::paste(MathGridInset const & data)
407 WriteStream wi(os, false, false);
409 dispatch(FuncRequest(LFUN_PASTE, os.str()));
413 void MathCursor::backspace()
415 autocorrect_ = false;
427 MathUnknownInset * p = activeMacro();
428 if (p->name().size() > 1) {
429 p->setName(p->name().substr(0, p->name().size() - 1));
435 if (prevAtom()->asScriptInset()) {
436 // simply enter nucleus
453 void MathCursor::erase()
455 autocorrect_ = false;
464 // delete empty cells if possible
466 if (par()->idxDelete(idx()))
469 // old behaviour when in last position of cell
470 if (pos() == size()) {
471 par()->idxGlue(idx());
476 // if we are standing in front of a script inset, grab item before us and
477 // move it into nucleus
478 // and remove first thing.
479 if (hasNextAtom() && nextAtom()->asScriptInset()) {
481 MathAtom at = prevAtom();
483 array().erase(pos());
484 pushLeft(nextAtom());
486 array().push_back(at);
491 pushLeft(nextAtom());
502 bool MathCursor::up(bool sel)
507 MathIterator save = Cursor_;
511 autocorrect_ = false;
516 bool MathCursor::down(bool sel)
521 MathIterator save = Cursor_;
525 autocorrect_ = false;
530 void MathCursor::macroModeClose()
534 MathUnknownInset * p = activeMacro();
536 string s = p->name();
538 array().erase(pos());
544 string MathCursor::macroName() const
546 return inMacroMode() ? activeMacro()->name() : string();
550 void MathCursor::selClear()
557 void MathCursor::selCopy()
561 theCutBuffer = grabSelection();
564 theCutBuffer = MathGridInset(1, 1);
569 void MathCursor::selCut()
572 theCutBuffer = grabAndEraseSelection();
576 void MathCursor::selDel()
586 void MathCursor::selPaste()
596 void MathCursor::selHandle(bool sel)
598 if (sel == selection_)
606 void MathCursor::selStart()
616 void MathCursor::selClearOrDel()
618 if (lyxrc.auto_region_delete)
625 void MathCursor::selGet(MathArray & ar)
629 ar = grabSelection().glue();
633 void MathCursor::drawSelection(MathPainterInfo & pi) const
639 getSelection(i1, i2);
640 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
644 void MathCursor::handleNest(MathAtom const & a)
647 at.nucleus()->cell(0) = grabAndEraseSelection().glue();
649 pushRight(prevAtom());
653 void MathCursor::getPos(int & x, int & y) const
655 par()->getPos(idx(), pos(), x, y);
659 int MathCursor::targetX() const
669 MathInset * MathCursor::par() const
671 return cursor().par_;
675 InsetFormulaBase * MathCursor::formula() const
681 MathCursor::idx_type MathCursor::idx() const
683 return cursor().idx_;
687 MathCursor::idx_type & MathCursor::idx()
689 return cursor().idx_;
693 MathCursor::pos_type MathCursor::pos() const
695 return cursor().pos_;
699 MathCursor::pos_type & MathCursor::pos()
701 return cursor().pos_;
705 bool MathCursor::inMacroMode() const
709 MathUnknownInset const * p = prevAtom()->asUnknownInset();
710 return p && !p->final();
714 MathUnknownInset * MathCursor::activeMacro()
716 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
720 MathUnknownInset const * MathCursor::activeMacro() const
722 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
726 bool MathCursor::inMacroArgMode() const
728 return pos() > 0 && prevAtom()->getChar() == '#';
732 bool MathCursor::selection() const
738 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
740 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
741 MathGridInset * p = Cursor_[i].par_->asGridInset();
743 idx = Cursor_[i].idx_;
751 void MathCursor::popToHere(MathInset const * p)
753 while (depth() && Cursor_.back().par_ != p)
758 void MathCursor::popToEnclosingGrid()
760 while (depth() && !Cursor_.back().par_->asGridInset())
765 void MathCursor::popToEnclosingHull()
767 while (depth() && !Cursor_.back().par_->asHullInset())
772 void MathCursor::pullArg()
775 MathArray a = array();
778 array().insert(pos(), a);
781 formula()->mutateToText();
786 void MathCursor::touch()
788 MathIterator::const_iterator it = Cursor_.begin();
789 MathIterator::const_iterator et = Cursor_.end();
790 for ( ; it != et; ++it)
795 void MathCursor::normalize()
797 if (idx() >= par()->nargs()) {
798 lyxerr << "this should not really happen - 1: "
799 << idx() << " " << par()->nargs() << " in: " << par() << "\n";
802 idx() = min(idx(), par()->nargs() - 1);
804 if (pos() > size()) {
805 lyxerr << "this should not really happen - 2: "
806 << pos() << " " << size() << " in idx: " << idx()
808 WriteStream wi(lyxerr, false, true);
813 pos() = min(pos(), size());
815 // remove empty scripts if possible
817 for (pos_type i = 0; i < size(); ++i) {
818 MathScriptInset * p = array()[i].nucleus()->asScriptInset();
820 p->removeEmptyScripts();
821 if (!p->hasUp() && !p->hasDown() && p->nuc().size() == 1)
822 array()[i] = p->nuc()[0];
827 // fix again position
828 pos() = min(pos(), size());
832 MathCursor::size_type MathCursor::size() const
834 return array().size();
838 bool MathCursor::hasPrevAtom() const
844 bool MathCursor::hasNextAtom() const
846 return pos() < size();
850 MathAtom const & MathCursor::prevAtom() const
852 lyx::Assert(pos() > 0);
853 return array()[pos() - 1];
857 MathAtom & MathCursor::prevAtom()
859 lyx::Assert(pos() > 0);
860 return array()[pos() - 1];
864 MathAtom const & MathCursor::nextAtom() const
866 lyx::Assert(pos() < size());
867 return array()[pos()];
871 MathAtom & MathCursor::nextAtom()
873 lyx::Assert(pos() < size());
874 return array()[pos()];
878 MathArray & MathCursor::array() const
880 static MathArray dummy;
882 if (idx() >= par()->nargs()) {
883 lyxerr << "############ idx_ " << idx() << " not valid\n";
888 lyxerr << "############ depth() == 0 not valid\n";
892 return cursor().cell();
896 void MathCursor::idxNext()
898 par()->idxNext(idx(), pos());
902 void MathCursor::idxPrev()
904 par()->idxPrev(idx(), pos());
908 char MathCursor::valign() const
911 MathGridInset * p = enclosingGrid(idx);
912 return p ? p->valign() : '\0';
916 char MathCursor::halign() const
919 MathGridInset * p = enclosingGrid(idx);
920 return p ? p->halign(idx % p->ncols()) : '\0';
924 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
926 MathCursorPos anc = normalAnchor();
927 if (anc < cursor()) {
937 MathCursorPos & MathCursor::cursor()
939 lyx::Assert(depth());
940 return Cursor_.back();
944 MathCursorPos const & MathCursor::cursor() const
946 lyx::Assert(depth());
947 return Cursor_.back();
951 bool MathCursor::goUpDown(bool up)
953 // Be warned: The 'logic' implemented in this function is highly fragile.
954 // A distance of one pixel or a '<' vs '<=' _really_ matters.
955 // So fiddle around with it only if you know what you are doing!
960 // check if we had something else in mind, if not, this is the future goal
966 // try neigbouring script insets
969 MathScriptInset const * p = prevAtom()->asScriptInset();
970 if (p && p->has(up)) {
973 idx() = up; // the superscript has index 1
975 ///lyxerr << "updown: handled by scriptinset to the left\n";
982 MathScriptInset const * p = nextAtom()->asScriptInset();
983 if (p && p->has(up)) {
987 ///lyxerr << "updown: handled by scriptinset to the right\n";
993 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
998 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
999 // lyxerr << "updown: handled by brute find in the same cell\n";
1003 // try to find an inset that knows better then we
1005 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1007 if (par()->idxUpDown(idx(), pos(), up, targetx_)) {
1008 // try to find best position within this inset
1014 // no such inset found, just take something "above"
1015 ///lyxerr << "updown: handled by strange case\n";
1021 up ? formula()->ylow() : yo + 4,
1022 up ? yo - 4 : formula()->yhigh()
1025 // any improvement so far?
1028 if (up ? ynew < yo : ynew > yo)
1034 bool MathCursor::bruteFind
1035 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1037 MathIterator best_cursor;
1038 double best_dist = 1e10;
1040 MathIterator it = ibegin(formula()->par().nucleus());
1041 MathIterator et = iend(formula()->par().nucleus());
1043 // avoid invalid nesting when selecting
1044 if (!selection_ || positionable(it, Anchor_)) {
1046 it.back().getPos(xo, yo);
1047 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1048 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1049 // '<=' in order to take the last possible position
1050 // this is important for clicking behind \sum in e.g. '\sum_i a'
1051 if (d <= best_dist) {
1063 if (best_dist < 1e10)
1064 Cursor_ = best_cursor;
1065 return best_dist < 1e10;
1069 void MathCursor::bruteFind2(int x, int y)
1071 double best_dist = 1e10;
1073 MathIterator it = Cursor_;
1074 it.back().setPos(0);
1075 MathIterator et = Cursor_;
1076 et.back().setPos(it.cell().size());
1079 it.back().getPos(xo, yo);
1080 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1081 // '<=' in order to take the last possible position
1082 // this is important for clicking behind \sum in e.g. '\sum_i a'
1083 if (d <= best_dist) {
1094 bool MathCursor::idxLineLast()
1096 idx() -= idx() % par()->ncols();
1097 idx() += par()->ncols() - 1;
1102 bool MathCursor::idxLeft()
1104 return par()->idxLeft(idx(), pos());
1108 bool MathCursor::idxRight()
1110 return par()->idxRight(idx(), pos());
1114 bool MathCursor::interpret(string const & s)
1116 //lyxerr << "interpret 1: '" << s << "'\n";
1120 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1121 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1122 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1124 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1129 istringstream is(s.substr(6).c_str());
1130 is >> m >> n >> v_align >> h_align;
1134 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1138 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1140 istringstream is(s.substr(7).c_str());
1143 mathed_parse_cell(rep.from, from);
1144 mathed_parse_cell(rep.to, to);
1145 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1146 par()->replace(rep);
1150 string name = s.substr(1);
1152 if (name == "over" || name == "choose" || name == "atop") {
1153 MathAtom t(createMathInset(name));
1154 t.nucleus()->asNestInset()->cell(0) = array();
1163 // prevent entering of recursive macros
1164 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1165 && formula()->getInsetName() == name)
1167 lyxerr << "can't enter recursive macro\n";
1171 niceInsert(createMathInset(name));
1176 bool MathCursor::script(bool up)
1178 // Hack to get \\^ and \\_ working
1179 if (inMacroMode() && macroName() == "\\") {
1181 niceInsert(createMathInset("mathcircumflex"));
1188 MathGridInset safe = grabAndEraseSelection();
1190 // we are in a nucleus of a script inset, move to _our_ script
1191 par()->asScriptInset()->ensure(up);
1194 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1195 prevAtom().nucleus()->asScriptInset()->ensure(up);
1196 pushRight(prevAtom());
1199 } else if (hasPrevAtom()) {
1201 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1202 pushLeft(nextAtom());
1206 plainInsert(MathAtom(new MathScriptInset(up)));
1207 prevAtom().nucleus()->asScriptInset()->ensure(up);
1208 pushRight(prevAtom());
1218 bool MathCursor::interpret(char c)
1220 //lyxerr << "interpret 2: '" << c << "'\n";
1221 targetx_ = -1; // "no target"
1222 if (inMacroArgMode()) {
1226 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1227 if (p && 1 <= n && n <= p->numargs())
1228 insert(MathAtom(new MathMacroArgument(c - '0')));
1230 insert(createMathInset("#"));
1231 interpret(c); // try again
1237 if (inMacroMode()) {
1238 string name = macroName();
1239 //lyxerr << "interpret name: '" << name << "'\n";
1242 activeMacro()->setName(activeMacro()->name() + c);
1246 // handle 'special char' macros
1251 if (currentMode() == MathInset::TEXT_MODE)
1252 niceInsert(createMathInset("textbackslash"));
1254 niceInsert(createMathInset("backslash"));
1255 } else if (c == '{') {
1256 niceInsert(MathAtom(new MathBraceInset));
1258 niceInsert(createMathInset(string(1, c)));
1263 // leave macro mode and try again if necessary
1266 niceInsert(MathAtom(new MathBraceInset));
1272 // This is annoying as one has to press <space> far too often.
1276 // leave autocorrect mode if necessary
1277 if (autocorrect_ && c == ' ') {
1278 autocorrect_ = false;
1283 // just clear selection on pressing the space bar
1284 if (selection_ && c == ' ') {
1292 //lyxerr << "starting with macro\n";
1293 insert(MathAtom(new MathUnknownInset("\\", false)));
1298 if (currentMode() == MathInset::TEXT_MODE)
1304 if (currentMode() == MathInset::TEXT_MODE) {
1305 // insert spaces in text mode,
1306 // but suppress direct insertion of two spaces in a row
1307 // the still allows typing '<space>a<space>' and deleting the 'a', but
1308 // it is better than nothing...
1309 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1313 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1314 prevAtom().nucleus()->asSpaceInset()->incSpace();
1319 // if are at the very end, leave the formula
1320 return pos() != size();
1328 if (c == '{' || c == '}') {
1329 niceInsert(createMathInset(string(1, c)));
1334 insert(createMathInset("$"));
1339 niceInsert(MathAtom(new MathCommentInset));
1344 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1345 insert(c, LM_TC_VAR);
1349 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1350 insert(c, LM_TC_VAR);
1351 lastcode_ = LM_TC_VAR;
1356 insert(c, LM_TC_TEX);
1357 //bv->owner()->message(_("TeX mode"));
1362 // try auto-correction
1363 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1366 // no special circumstances, so insert the character without any fuss
1368 autocorrect_ = true;
1373 void MathCursor::setSelection(MathIterator const & where, size_type n)
1382 void MathCursor::insetToggle()
1384 if (hasNextAtom()) {
1385 // toggle previous inset ...
1386 nextAtom().nucleus()->lock(!nextAtom()->lock());
1387 } else if (popLeft() && hasNextAtom()) {
1388 // ... or enclosing inset if we are in the last inset position
1389 nextAtom().nucleus()->lock(!nextAtom()->lock());
1395 string MathCursor::info() const
1398 os << "Math editor mode. ";
1399 for (int i = 0, n = depth(); i < n; ++i) {
1400 Cursor_[i].par_->infoize(os);
1404 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1405 prevAtom()->infoize(os);
1407 return os.str().c_str(); // .c_str() needed for lyxstring
1411 unsigned MathCursor::depth() const
1413 return Cursor_.size();
1421 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1422 MathInset::row_type & r1, MathInset::row_type & r2,
1423 MathInset::col_type & c1, MathInset::col_type & c2)
1425 MathInset * p = i1.par_;
1426 c1 = p->col(i1.idx_);
1427 c2 = p->col(i2.idx_);
1430 r1 = p->row(i1.idx_);
1431 r2 = p->row(i2.idx_);
1439 MathGridInset MathCursor::grabSelection() const
1442 return MathGridInset();
1445 getSelection(i1, i2);
1446 // shouldn't we assert on i1.par_ == i2.par_?
1447 if (i1.idx_ == i2.idx_) {
1448 MathGridInset data(1, 1);
1449 MathArray::const_iterator it = i1.cell().begin();
1450 data.cell(0) = MathArray(it + i1.pos_, it + i2.pos_);
1455 region(i1, i2, r1, r2, c1, c2);
1456 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1457 for (row_type row = 0; row < data.nrows(); ++row)
1458 for (col_type col = 0; col < data.ncols(); ++col) {
1459 idx_type i = i1.par_->index(row + r1, col + c1);
1460 data.cell(data.index(row, col)) = i1.par_->cell(i);
1466 void MathCursor::eraseSelection()
1470 getSelection(i1, i2);
1471 if (i1.idx_ == i2.idx_)
1472 i1.cell().erase(i1.pos_, i2.pos_);
1474 MathInset * p = i1.par_;
1477 region(i1, i2, r1, r2, c1, c2);
1478 for (row_type row = r1; row <= r2; ++row)
1479 for (col_type col = c1; col <= c2; ++col)
1480 p->cell(p->index(row, col)).clear();
1486 MathGridInset MathCursor::grabAndEraseSelection()
1489 return MathGridInset();
1490 MathGridInset res = grabSelection();
1497 MathCursorPos MathCursor::normalAnchor() const
1499 if (Anchor_.size() < depth()) {
1501 lyxerr << "unusual Anchor size\n";
1503 //lyx::Assert(Anchor_.size() >= cursor.depth());
1504 // use Anchor on the same level as Cursor
1505 MathCursorPos normal = Anchor_[depth() - 1];
1506 if (depth() < Anchor_.size() && !(normal < cursor())) {
1507 // anchor is behind cursor -> move anchor behind the inset
1514 MathInset::result_type MathCursor::dispatch(FuncRequest const & cmd)
1516 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1517 MathCursorPos & pos = Cursor_[i];
1518 MathInset::result_type
1519 res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_);
1520 if (res != MathInset::UNDISPATCHED) {
1521 if (res == MathInset::DISPATCHED_POP) {
1522 Cursor_.shrink(i + 1);
1528 return MathInset::UNDISPATCHED;
1532 MathInset::mode_type MathCursor::currentMode() const
1534 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1535 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1536 if (res != MathInset::UNDECIDED_MODE)
1539 return MathInset::UNDECIDED_MODE;
1543 void releaseMathCursor(BufferView * bv)
1547 mathcursor->formula()->hideInsetCursor(bv);