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 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
97 t->idxFirst(idx(), pos());
101 void MathCursor::pushRight(MathAtom & t)
103 //cerr << "Entering atom "; t->write(cerr, false); cerr << " 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());
265 bool positionable(MathIterator const & cursor, MathIterator const & anchor)
267 // avoid deeper nested insets when selecting
268 if (cursor.size() > anchor.size())
271 // anchor might be deeper, should have same path then
272 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
273 if (cursor[i].par_ != anchor[i].par_)
276 // position should be ok.
281 void MathCursor::setPos(int x, int y)
284 bool res = bruteFind(x, y,
285 formula()->xlow(), formula()->xhigh(),
286 formula()->ylow(), formula()->yhigh());
288 // this can happen on creation of "math-display"
292 targetx_ = -1; // "no target"
298 bool MathCursor::home(bool sel)
301 autocorrect_ = false;
304 if (!par()->idxHome(idx(), pos()))
307 targetx_ = -1; // "no target"
312 bool MathCursor::end(bool sel)
315 autocorrect_ = false;
318 if (!par()->idxEnd(idx(), pos()))
321 targetx_ = -1; // "no target"
326 void MathCursor::plainErase()
328 array().erase(pos());
332 void MathCursor::markInsert()
334 //lyxerr << "inserting mark\n";
335 array().insert(pos(), MathAtom(new MathCharInset(0)));
339 void MathCursor::markErase()
341 //lyxerr << "deleting mark\n";
342 array().erase(pos());
346 void MathCursor::plainInsert(MathAtom const & t)
349 array().insert(pos(), t);
354 void MathCursor::insert(string const & str)
356 //lyxerr << "inserting '" << str << "'\n";
358 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
359 plainInsert(MathAtom(new MathCharInset(*it)));
363 void MathCursor::insert(char c)
365 //lyxerr << "inserting '" << c << "'\n";
367 plainInsert(MathAtom(new MathCharInset(c)));
371 void MathCursor::insert(MathAtom const & t)
379 void MathCursor::niceInsert(MathAtom const & t)
382 MathGridInset safe = grabAndEraseSelection();
384 // enter the new inset and move the contents of the selection if possible
387 pushLeft(nextAtom());
393 void MathCursor::insert(MathArray const & ar)
399 array().insert(pos(), ar);
404 void MathCursor::paste(MathArray const & ar)
408 array().insert(pos(), ar);
413 void MathCursor::paste(MathGridInset const & data)
416 WriteStream wi(os, false, false);
418 dispatch(FuncRequest(LFUN_PASTE, os.str()));
422 void MathCursor::backspace()
424 autocorrect_ = false;
436 if (prevAtom()->asScriptInset()) {
437 // simply enter nucleus
454 void MathCursor::erase()
456 autocorrect_ = false;
465 // delete empty cells if possible
467 if (par()->idxDelete(idx()))
470 // old behaviour when in last position of cell
471 if (pos() == size()) {
472 par()->idxGlue(idx());
477 // if we are standing in front of a script inset, grab item before us and
478 // move it into nucleus
479 // and remove first thing.
480 if (hasNextAtom() && nextAtom()->asScriptInset()) {
482 MathAtom at = prevAtom();
484 array().erase(pos());
485 pushLeft(nextAtom());
487 array().push_back(at);
492 pushLeft(nextAtom());
503 bool MathCursor::up(bool sel)
508 MathIterator save = Cursor_;
512 autocorrect_ = false;
517 bool MathCursor::down(bool sel)
522 MathIterator save = Cursor_;
526 autocorrect_ = false;
531 bool MathCursor::toggleLimits()
533 if (!hasNextAtom() || !nextAtom()->asScriptInset())
535 MathScriptInset * t = nextAtom().nucleus()->asScriptInset();
536 int old = t->limits();
537 t->limits(old < 0 ? 1 : -1);
538 return old != t->limits();
542 void MathCursor::macroModeClose()
546 MathUnknownInset * p = activeMacro();
548 string s = p->name();
550 array().erase(pos());
556 string MathCursor::macroName() const
558 return inMacroMode() ? activeMacro()->name() : string();
562 void MathCursor::selClear()
569 void MathCursor::selCopy()
573 theCutBuffer = grabSelection();
576 theCutBuffer = MathGridInset(1, 1);
581 void MathCursor::selCut()
584 theCutBuffer = grabAndEraseSelection();
588 void MathCursor::selDel()
598 void MathCursor::selPaste()
608 void MathCursor::selHandle(bool sel)
610 if (sel == selection_)
618 void MathCursor::selStart()
628 void MathCursor::selClearOrDel()
630 if (lyxrc.auto_region_delete)
637 void MathCursor::selGet(MathArray & ar)
641 ar = grabSelection().glue();
645 void MathCursor::drawSelection(MathPainterInfo & pi) const
651 getSelection(i1, i2);
652 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
656 void MathCursor::handleNest(MathAtom const & a)
659 at.nucleus()->cell(0) = grabAndEraseSelection().glue();
661 pushRight(prevAtom());
665 void MathCursor::getPos(int & x, int & y)
667 par()->getPos(idx(), pos(), x, y);
671 MathInset * MathCursor::par() const
673 return cursor().par_;
677 InsetFormulaBase * MathCursor::formula() const
683 MathCursor::idx_type MathCursor::idx() const
685 return cursor().idx_;
689 MathCursor::idx_type & MathCursor::idx()
691 return cursor().idx_;
695 MathCursor::pos_type MathCursor::pos() const
697 return cursor().pos_;
701 MathCursor::pos_type & MathCursor::pos()
703 return cursor().pos_;
707 bool MathCursor::inMacroMode() const
711 MathUnknownInset const * p = prevAtom()->asUnknownInset();
712 return p && !p->final();
716 MathUnknownInset * MathCursor::activeMacro()
718 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
722 MathUnknownInset const * MathCursor::activeMacro() const
724 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
728 bool MathCursor::inMacroArgMode() const
730 return pos() > 0 && prevAtom()->getChar() == '#';
734 bool MathCursor::selection() const
740 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
742 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
743 MathGridInset * p = Cursor_[i].par_->asGridInset();
745 idx = Cursor_[i].idx_;
753 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
755 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
756 MathHullInset * p = Cursor_[i].par_->asHullInset();
758 idx = Cursor_[i].idx_;
766 void MathCursor::popToHere(MathInset const * p)
768 while (depth() && Cursor_.back().par_ != p)
773 void MathCursor::popToEnclosingGrid()
775 while (depth() && !Cursor_.back().par_->asGridInset())
780 void MathCursor::popToEnclosingHull()
782 while (depth() && !Cursor_.back().par_->asHullInset())
787 void MathCursor::pullArg()
790 MathArray a = array();
793 array().insert(pos(), a);
796 formula()->mutateToText();
801 void MathCursor::touch()
803 MathIterator::const_iterator it = Cursor_.begin();
804 MathIterator::const_iterator et = Cursor_.end();
805 for ( ; it != et; ++it)
810 void MathCursor::normalize()
812 if (idx() >= par()->nargs()) {
813 lyxerr << "this should not really happen - 1: "
814 << idx() << " " << par()->nargs() << " in: " << par() << "\n";
817 idx() = min(idx(), par()->nargs() - 1);
819 if (pos() > size()) {
820 lyxerr << "this should not really happen - 2: "
821 << pos() << " " << size() << " in idx: " << idx()
823 WriteStream wi(lyxerr, false, true);
828 pos() = min(pos(), size());
830 // remove empty scripts if possible
832 for (pos_type i = 0; i < size(); ++i) {
833 MathScriptInset * p = array()[i].nucleus()->asScriptInset();
835 p->removeEmptyScripts();
842 // fix again position
843 pos() = min(pos(), size());
847 MathCursor::size_type MathCursor::size() const
849 return array().size();
853 bool MathCursor::hasPrevAtom() const
859 bool MathCursor::hasNextAtom() const
861 return pos() < size();
865 MathAtom const & MathCursor::prevAtom() const
867 lyx::Assert(pos() > 0);
868 return array()[pos() - 1];
872 MathAtom & MathCursor::prevAtom()
874 lyx::Assert(pos() > 0);
875 return array()[pos() - 1];
879 MathAtom const & MathCursor::nextAtom() const
881 lyx::Assert(pos() < size());
882 return array()[pos()];
886 MathAtom & MathCursor::nextAtom()
888 lyx::Assert(pos() < size());
889 return array()[pos()];
893 MathArray & MathCursor::array() const
895 static MathArray dummy;
897 if (idx() >= par()->nargs()) {
898 lyxerr << "############ idx_ " << idx() << " not valid\n";
903 lyxerr << "############ depth() == 0 not valid\n";
907 return cursor().cell();
911 void MathCursor::idxNext()
913 par()->idxNext(idx(), pos());
917 void MathCursor::idxPrev()
919 par()->idxPrev(idx(), pos());
923 char MathCursor::valign() const
926 MathGridInset * p = enclosingGrid(idx);
927 return p ? p->valign() : '\0';
931 char MathCursor::halign() const
934 MathGridInset * p = enclosingGrid(idx);
935 return p ? p->halign(idx % p->ncols()) : '\0';
939 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
941 MathCursorPos anc = normalAnchor();
942 if (anc < cursor()) {
952 MathCursorPos & MathCursor::cursor()
954 lyx::Assert(depth());
955 return Cursor_.back();
959 MathCursorPos const & MathCursor::cursor() const
961 lyx::Assert(depth());
962 return Cursor_.back();
966 bool MathCursor::goUpDown(bool up)
968 // Be warned: The 'logic' implemented in this function is highly fragile.
969 // A distance of one pixel or a '<' vs '<=' _really_ matters.
970 // So fiddle around with it only if you know what you are doing!
975 // check if we had something else in mind, if not, this is the future goal
981 // try neigbouring script insets
984 MathScriptInset const * p = prevAtom()->asScriptInset();
985 if (p && p->has(up)) {
988 idx() = up; // the superscript has index 1
990 ///lyxerr << "updown: handled by scriptinset to the left\n";
997 MathScriptInset const * p = nextAtom()->asScriptInset();
998 if (p && p->has(up)) {
1002 ///lyxerr << "updown: handled by scriptinset to the right\n";
1008 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1013 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1014 // lyxerr << "updown: handled by brute find in the same cell\n";
1018 // try to find an inset that knows better then we
1020 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1022 if (par()->idxUpDown(idx(), pos(), up, targetx_))
1025 // no such inset found, just take something "above"
1026 ///lyxerr << "updown: handled by strange case\n";
1032 up ? formula()->ylow() : yo + 4,
1033 up ? yo - 4 : formula()->yhigh()
1036 // any improvement so far?
1039 if (up ? ynew < yo : ynew > yo)
1045 bool MathCursor::bruteFind
1046 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1048 MathIterator best_cursor;
1049 double best_dist = 1e10;
1051 MathIterator it = ibegin(formula()->par().nucleus());
1052 MathIterator et = iend(formula()->par().nucleus());
1054 // avoid invalid nesting when selecting
1055 if (!selection_ || positionable(it, Anchor_)) {
1057 it.back().getPos(xo, yo);
1058 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1059 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1060 // '<=' in order to take the last possible position
1061 // this is important for clicking behind \sum in e.g. '\sum_i a'
1062 if (d <= best_dist) {
1074 if (best_dist < 1e10)
1075 Cursor_ = best_cursor;
1076 return best_dist < 1e10;
1080 bool MathCursor::idxLineFirst()
1082 idx() -= idx() % par()->ncols();
1088 bool MathCursor::idxLineLast()
1090 idx() -= idx() % par()->ncols();
1091 idx() += par()->ncols() - 1;
1096 bool MathCursor::idxLeft()
1098 return par()->idxLeft(idx(), pos());
1102 bool MathCursor::idxRight()
1104 return par()->idxRight(idx(), pos());
1108 bool MathCursor::interpret(string const & s)
1110 //lyxerr << "interpret 1: '" << s << "'\n";
1114 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1115 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1116 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1118 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1123 istringstream is(s.substr(6).c_str());
1124 is >> m >> n >> v_align >> h_align;
1128 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1132 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1134 istringstream is(s.substr(7).c_str());
1137 mathed_parse_cell(rep.from, from);
1138 mathed_parse_cell(rep.to, to);
1139 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1140 par()->replace(rep);
1144 string name = s.substr(1);
1146 if (name == "over" || name == "choose" || name == "atop") {
1147 MathAtom t(createMathInset(name));
1148 t.nucleus()->asNestInset()->cell(0) = array();
1157 // prevent entering of recursive macros
1158 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1159 && formula()->getInsetName() == name)
1161 lyxerr << "can't enter recursive macro\n";
1165 niceInsert(createMathInset(name));
1170 bool MathCursor::script(bool up)
1172 // Hack to get \\^ and \\_ working
1173 if (inMacroMode() && macroName() == "\\") {
1175 interpret("\\mathcircumflex");
1182 MathGridInset safe = grabAndEraseSelection();
1184 // we are in a nucleus of a script inset, move to _our_ script
1185 par()->asScriptInset()->ensure(up);
1188 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1189 prevAtom().nucleus()->asScriptInset()->ensure(up);
1190 pushRight(prevAtom());
1193 } else if (hasPrevAtom()) {
1195 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1196 pushLeft(nextAtom());
1200 plainInsert(MathAtom(new MathScriptInset(up)));
1201 prevAtom().nucleus()->asScriptInset()->ensure(up);
1202 pushRight(prevAtom());
1212 bool MathCursor::interpret(char c)
1214 //lyxerr << "interpret 2: '" << c << "'\n";
1215 targetx_ = -1; // "no target"
1216 if (inMacroArgMode()) {
1220 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1221 if (p && 1 <= n && n <= p->numargs())
1222 insert(MathAtom(new MathMacroArgument(c - '0')));
1224 insert(createMathInset("#"));
1225 interpret(c); // try again
1231 if (inMacroMode()) {
1232 string name = macroName();
1233 //lyxerr << "interpret name: '" << name << "'\n";
1235 if (name.empty() && c == '\\') {
1237 interpret("\\backslash");
1242 activeMacro()->setName(activeMacro()->name() + c);
1246 // handle 'special char' macros
1251 interpret("\\backslash");
1253 interpret(string("\\") + c);
1257 // leave macro mode and try again if necessary
1264 // This is annoying as one has to press <space> far too often.
1268 // leave autocorrect mode if necessary
1269 if (autocorrect_ && c == ' ') {
1270 autocorrect_ = false;
1275 // just clear selection on pressing the space bar
1276 if (selection_ && c == ' ') {
1284 //lyxerr << "starting with macro\n";
1285 insert(MathAtom(new MathUnknownInset("\\", false)));
1290 if (currentMode() == MathInset::TEXT_MODE)
1296 if (currentMode() == MathInset::TEXT_MODE) {
1297 // insert spaces in text mode,
1298 // but suppress direct insertion of two spaces in a row
1299 // the still allows typing '<space>a<space>' and deleting the 'a', but
1300 // it is better than nothing...
1301 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1305 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1306 prevAtom().nucleus()->asSpaceInset()->incSpace();
1311 // if are at the very end, leave the formula
1312 return pos() != size();
1316 insert(c); // LM_TC_TEX;
1321 if (c == '{' || c == '}', c)) {
1322 insert(c); // LM_TC_TEX;
1328 niceInsert(MathAtom(new MathBraceInset));
1337 insert(createMathInset("$"));
1342 niceInsert(MathAtom(new MathCommentInset));
1347 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1348 insert(c, LM_TC_VAR);
1352 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1353 insert(c, LM_TC_VAR);
1354 lastcode_ = LM_TC_VAR;
1359 insert(c, LM_TC_TEX);
1360 //bv->owner()->message(_("TeX mode"));
1365 // try auto-correction
1366 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1369 // no special circumstances, so insert the character without any fuss
1371 autocorrect_ = true;
1376 void MathCursor::setSelection(MathIterator const & where, size_type n)
1385 void MathCursor::insetToggle()
1387 if (hasNextAtom()) {
1388 // toggle previous inset ...
1389 nextAtom().nucleus()->lock(!nextAtom()->lock());
1390 } else if (popLeft() && hasNextAtom()) {
1391 // ... or enclosing inset if we are in the last inset position
1392 nextAtom().nucleus()->lock(!nextAtom()->lock());
1398 string MathCursor::info() const
1401 os << "Math editor mode. ";
1402 for (int i = 0, n = depth(); i < n; ++i) {
1403 Cursor_[i].par_->infoize(os);
1407 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1408 prevAtom()->infoize(os);
1410 return os.str().c_str(); // .c_str() needed for lyxstring
1414 unsigned MathCursor::depth() const
1416 return Cursor_.size();
1424 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1425 MathInset::row_type & r1, MathInset::row_type & r2,
1426 MathInset::col_type & c1, MathInset::col_type & c2)
1428 MathInset * p = i1.par_;
1429 c1 = p->col(i1.idx_);
1430 c2 = p->col(i2.idx_);
1433 r1 = p->row(i1.idx_);
1434 r2 = p->row(i2.idx_);
1442 MathGridInset MathCursor::grabSelection() const
1445 return MathGridInset();
1448 getSelection(i1, i2);
1449 // shouldn't we assert on i1.par_ == i2.par_?
1450 if (i1.idx_ == i2.idx_) {
1451 MathGridInset data(1, 1);
1452 MathArray::const_iterator it = i1.cell().begin();
1453 data.cell(0) = MathArray(it + i1.pos_, it + i2.pos_);
1458 region(i1, i2, r1, r2, c1, c2);
1459 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1460 for (row_type row = 0; row < data.nrows(); ++row)
1461 for (col_type col = 0; col < data.ncols(); ++col) {
1462 idx_type i = i1.par_->index(row + r1, col + c1);
1463 data.cell(data.index(row, col)) = i1.par_->cell(i);
1469 void MathCursor::eraseSelection()
1473 getSelection(i1, i2);
1474 if (i1.idx_ == i2.idx_)
1475 i1.cell().erase(i1.pos_, i2.pos_);
1477 MathInset * p = i1.par_;
1480 region(i1, i2, r1, r2, c1, c2);
1481 for (row_type row = r1; row <= r2; ++row)
1482 for (col_type col = c1; col <= c2; ++col)
1483 p->cell(p->index(row, col)).clear();
1489 MathGridInset MathCursor::grabAndEraseSelection()
1492 return MathGridInset();
1493 MathGridInset res = grabSelection();
1500 MathCursorPos MathCursor::normalAnchor() const
1502 if (Anchor_.size() < depth()) {
1504 lyxerr << "unusual Anchor size\n";
1506 //lyx::Assert(Anchor_.size() >= cursor.depth());
1507 // use Anchor on the same level as Cursor
1508 MathCursorPos normal = Anchor_[depth() - 1];
1509 if (depth() < Anchor_.size() && !(normal < cursor())) {
1510 // anchor is behind cursor -> move anchor behind the inset
1518 void MathCursor::handleExtern(const string & arg)
1522 istringstream iss(arg.c_str());
1523 iss >> lang >> extra;
1530 lyxerr << "use selection: " << ar << "\n";
1531 insert(pipeThroughExtern(lang, extra, ar));
1536 eq.push_back(MathAtom(new MathCharInset('=')));
1538 popToEnclosingHull();
1541 MathHullInset * hull = enclosingHull(idx);
1545 if (hull->getType() == "simple") {
1546 MathArray::size_type pos = cursor().cell().find_last(eq);
1548 if (pos == size()) {
1550 lyxerr << "use whole cell: " << ar << "\n";
1552 ar = MathArray(array().begin() + pos + 1, array().end());
1553 lyxerr << "use partial cell form pos: " << pos << "\n";
1557 insert(pipeThroughExtern(lang, extra, ar));
1561 if (hull->getType() == "equation") {
1562 lyxerr << "use equation inset\n";
1563 hull->mutate("eqnarray");
1564 MathArray & ar = cursor().cell();
1565 lyxerr << "use cell: " << ar << "\n";
1567 cursor().cell() = eq;
1569 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1575 lyxerr << "use eqnarray\n";
1577 MathArray ar = cursor().cell();
1578 lyxerr << "use cell: " << ar << "\n";
1579 #ifdef WITH_WARNINGS
1580 #warning temporarily disabled
1584 cursor().cell() = eq;
1586 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1593 MathInset::result_type MathCursor::dispatch(FuncRequest const & cmd)
1595 // try to dispatch to adajcent items if they are not editable
1596 // actually, this should only happen for mouse clicks...
1599 if (hasNextAtom() && !openable(nextAtom(), false)) {
1600 MathInset::result_type res = nextAtom().nucleus()->dispatch(cmd, d1, d2);
1601 if (res != MathInset::UNDISPATCHED)
1604 if (hasPrevAtom() && !openable(prevAtom(), false)) {
1605 MathInset::result_type res = prevAtom().nucleus()->dispatch(cmd, d1, d2);
1606 if (res != MathInset::UNDISPATCHED)
1610 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1611 MathCursorPos & pos = Cursor_[i];
1612 MathInset::result_type const res
1613 = pos.par_->dispatch(cmd, pos.idx_, pos.pos_);
1614 if (res != MathInset::UNDISPATCHED) {
1615 if (res == MathInset::DISPATCHED_POP) {
1616 Cursor_.shrink(i + 1);
1622 return MathInset::UNDISPATCHED;
1626 MathInset::mode_type MathCursor::currentMode() const
1628 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1629 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1630 if (res != MathInset::UNDECIDED_MODE)
1633 return MathInset::UNDECIDED_MODE;
1637 void releaseMathCursor(BufferView * bv)
1641 mathcursor->formula()->hideInsetCursor(bv);