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 "math_autocorrect.h"
34 #include "math_arrayinset.h"
35 #include "math_braceinset.h"
36 #include "math_casesinset.h"
37 #include "math_charinset.h"
38 #include "math_extern.h"
39 #include "math_factory.h"
40 #include "math_hullinset.h"
41 #include "math_iterator.h"
42 #include "math_macroarg.h"
43 #include "math_macrotemplate.h"
44 #include "math_mathmlstream.h"
45 #include "math_parser.h"
46 #include "math_replace.h"
47 #include "math_scriptinset.h"
48 #include "math_spaceinset.h"
49 #include "math_support.h"
50 #include "math_unknowninset.h"
62 using std::ostringstream;
66 // matheds own cut buffer
67 MathGridInset theCutBuffer = MathGridInset(1, 1);
70 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
71 : formula_(formula), autocorrect_(false), selection_(false)
73 front ? first() : last();
78 MathCursor::~MathCursor()
80 // ensure that 'notifyCursorLeave' is called
86 void MathCursor::push(MathAtom & t)
88 Cursor_.push_back(MathCursorPos(t.nucleus()));
92 void MathCursor::pushLeft(MathAtom & t)
94 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
96 t->idxFirst(idx(), pos());
100 void MathCursor::pushRight(MathAtom & t)
102 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
105 t->idxLast(idx(), pos());
109 bool MathCursor::popLeft()
111 //cerr << "Leaving atom to the left\n";
114 par()->notifyCursorLeaves();
117 par()->notifyCursorLeaves();
123 bool MathCursor::popRight()
125 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
128 par()->notifyCursorLeaves();
131 par()->notifyCursorLeaves();
140 void MathCursor::dump(char const * what) const
142 lyxerr << "MC: " << what << "\n";
143 lyxerr << " Cursor: " << depth() << "\n";
144 for (unsigned i = 0; i < depth(); ++i)
145 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
146 lyxerr << " Anchor: " << Anchor_.size() << "\n";
147 for (unsigned i = 0; i < Anchor_.size(); ++i)
148 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
149 lyxerr << " sel: " << selection_ << "\n";
152 void MathCursor::dump(char const *) const {}
156 bool MathCursor::isInside(MathInset const * p) const
158 for (unsigned i = 0; i < depth(); ++i)
159 if (Cursor_[i].par_ == p)
165 bool MathCursor::openable(MathAtom const & t, bool sel) const
174 // we can't move into anything new during selection
175 if (depth() == Anchor_.size())
177 if (t.nucleus() != Anchor_[depth()].par_)
184 bool MathCursor::inNucleus() const
186 return par()->asScriptInset() && idx() == 2;
190 bool MathCursor::posLeft()
199 bool MathCursor::posRight()
208 bool MathCursor::left(bool sel)
211 autocorrect_ = false;
212 targetx_ = -1; // "no target"
219 if (hasPrevAtom() && openable(prevAtom(), sel)) {
220 pushRight(prevAtom());
224 return posLeft() || idxLeft() || popLeft() || selection_;
228 bool MathCursor::right(bool sel)
231 autocorrect_ = false;
232 targetx_ = -1; // "no target"
239 if (hasNextAtom() && openable(nextAtom(), sel)) {
240 pushLeft(nextAtom());
244 return posRight() || idxRight() || popRight() || selection_;
248 void MathCursor::first()
251 push(formula_->par());
252 par()->idxFirst(idx(), pos());
256 void MathCursor::last()
259 push(formula_->par());
260 par()->idxLast(idx(), pos());
264 bool positionable(MathIterator const & cursor, MathIterator const & anchor)
266 // avoid deeper nested insets when selecting
267 if (cursor.size() > anchor.size())
270 // anchor might be deeper, should have same path then
271 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
272 if (cursor[i].par_ != anchor[i].par_)
275 // position should be ok.
280 void MathCursor::setPos(int x, int y)
283 bool res = bruteFind(x, y,
284 formula()->xlow(), formula()->xhigh(),
285 formula()->ylow(), formula()->yhigh());
287 // this can happen on creation of "math-display"
291 targetx_ = -1; // "no target"
297 bool MathCursor::home(bool sel)
300 autocorrect_ = false;
303 if (!par()->idxHome(idx(), pos()))
306 targetx_ = -1; // "no target"
311 bool MathCursor::end(bool sel)
314 autocorrect_ = false;
317 if (!par()->idxEnd(idx(), pos()))
320 targetx_ = -1; // "no target"
325 void MathCursor::plainErase()
327 array().erase(pos());
331 void MathCursor::markInsert()
333 //lyxerr << "inserting mark\n";
334 array().insert(pos(), MathAtom(new MathCharInset(0)));
338 void MathCursor::markErase()
340 //lyxerr << "deleting mark\n";
341 array().erase(pos());
345 void MathCursor::plainInsert(MathAtom const & t)
348 array().insert(pos(), t);
353 void MathCursor::insert(string const & str)
355 //lyxerr << "inserting '" << str << "'\n";
357 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
358 plainInsert(MathAtom(new MathCharInset(*it)));
362 void MathCursor::insert(char c)
364 //lyxerr << "inserting '" << c << "'\n";
366 plainInsert(MathAtom(new MathCharInset(c)));
370 void MathCursor::insert(MathAtom const & t)
378 void MathCursor::niceInsert(MathAtom const & t)
381 MathGridInset safe = grabAndEraseSelection();
385 // enter the new inset and move the contents of the selection if possible
388 pushLeft(nextAtom());
390 // lets pretend we've not moved too far away...
396 void MathCursor::insert(MathArray const & ar)
402 array().insert(pos(), ar);
407 void MathCursor::paste(MathArray const & ar)
411 array().insert(pos(), ar);
416 void MathCursor::paste(MathGridInset const & data)
418 if (data.nargs() == 1) {
419 // single cell/part of cell
423 idx_type idx; // index of upper left cell
424 MathGridInset * p = enclosingGrid(idx);
425 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
426 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
427 for (row_type row = 0; row < numrows; ++row) {
428 for (col_type col = 0; col < numcols; ++col) {
429 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
430 p->cell(i).append(data.cell(data.index(row, col)));
432 // append the left over horizontal cells to the last column
433 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
434 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
435 p->cell(i).append(data.cell(data.index(row, col)));
437 // append the left over vertical cells to the last _cell_
438 idx_type i = p->nargs() - 1;
439 for (row_type row = numrows; row < data.nrows(); ++row)
440 for (col_type col = 0; col < data.ncols(); ++col)
441 p->cell(i).append(data.cell(data.index(row, col)));
446 void MathCursor::backspace()
448 autocorrect_ = false;
460 if (prevAtom()->asScriptInset()) {
461 // simply enter nucleus
478 void MathCursor::erase()
480 autocorrect_ = false;
489 // delete empty cells if possible
491 if (par()->idxDelete(idx()))
494 // old behaviour when in last position of cell
495 if (pos() == size()) {
496 par()->idxGlue(idx());
501 // if we are standing in front of a script inset, grab item before us and
502 // move it into nucleus
503 // and remove first thing.
504 if (hasNextAtom() && nextAtom()->asScriptInset()) {
506 MathAtom at = prevAtom();
508 array().erase(pos());
509 pushLeft(nextAtom());
511 array().push_back(at);
516 pushLeft(nextAtom());
527 void MathCursor::delLine()
529 autocorrect_ = false;
537 if (par()->nrows() > 1) {
538 // grid are the only things with more than one row...
539 lyx::Assert(par()->asGridInset());
540 par()->asGridInset()->delRow(hullRow());
543 if (idx() >= par()->nargs())
544 idx() = par()->nargs() - 1;
551 bool MathCursor::up(bool sel)
556 MathIterator save = Cursor_;
560 autocorrect_ = false;
565 bool MathCursor::down(bool sel)
570 MathIterator save = Cursor_;
574 autocorrect_ = false;
579 bool MathCursor::toggleLimits()
581 if (!hasNextAtom() || !nextAtom()->asScriptInset())
583 MathScriptInset * t = nextAtom().nucleus()->asScriptInset();
584 int old = t->limits();
585 t->limits(old < 0 ? 1 : -1);
586 return old != t->limits();
590 void MathCursor::macroModeClose()
594 MathUnknownInset * p = activeMacro();
596 string s = p->name();
598 array().erase(pos());
604 string MathCursor::macroName() const
606 return inMacroMode() ? activeMacro()->name() : string();
610 void MathCursor::selClear()
617 void MathCursor::selCopy()
621 theCutBuffer = grabSelection();
624 theCutBuffer = MathGridInset(1, 1);
629 void MathCursor::selCut()
632 theCutBuffer = grabAndEraseSelection();
636 void MathCursor::selDel()
646 void MathCursor::selPaste()
656 void MathCursor::selHandle(bool sel)
658 if (sel == selection_)
666 void MathCursor::selStart()
676 void MathCursor::selClearOrDel()
678 if (lyxrc.auto_region_delete)
685 void MathCursor::selGet(MathArray & ar)
689 ar = grabSelection().glue();
693 void MathCursor::drawSelection(MathPainterInfo & pi) const
699 getSelection(i1, i2);
700 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
704 void MathCursor::handleNest(MathAtom const & at)
707 #warning temporarily disabled
708 //at->cell(0) = grabAndEraseSelection().glue();
711 pushRight(prevAtom());
715 void MathCursor::getPos(int & x, int & y)
717 par()->getPos(idx(), pos(), x, y);
721 MathInset * MathCursor::par() const
723 return cursor().par_;
727 InsetFormulaBase * MathCursor::formula() const
733 MathCursor::idx_type MathCursor::idx() const
735 return cursor().idx_;
739 MathCursor::idx_type & MathCursor::idx()
741 return cursor().idx_;
745 MathCursor::pos_type MathCursor::pos() const
747 return cursor().pos_;
751 MathCursor::pos_type & MathCursor::pos()
753 return cursor().pos_;
757 bool MathCursor::inMacroMode() const
761 MathUnknownInset const * p = prevAtom()->asUnknownInset();
762 return p && !p->final();
766 MathUnknownInset * MathCursor::activeMacro()
768 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
772 MathUnknownInset const * MathCursor::activeMacro() const
774 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
778 bool MathCursor::inMacroArgMode() const
780 return pos() > 0 && prevAtom()->getChar() == '#';
784 bool MathCursor::selection() const
790 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
792 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
793 MathGridInset * p = Cursor_[i].par_->asGridInset();
795 idx = Cursor_[i].idx_;
803 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
805 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
806 MathHullInset * p = Cursor_[i].par_->asHullInset();
808 idx = Cursor_[i].idx_;
816 void MathCursor::popToEnclosingGrid()
818 while (depth() && !Cursor_.back().par_->asGridInset())
823 void MathCursor::popToEnclosingHull()
825 while (depth() && !Cursor_.back().par_->asHullInset())
830 void MathCursor::pullArg()
833 MathArray a = array();
836 array().insert(pos(), a);
839 formula()->mutateToText();
844 void MathCursor::touch()
846 MathIterator::const_iterator it = Cursor_.begin();
847 MathIterator::const_iterator et = Cursor_.end();
848 for ( ; it != et; ++it)
853 void MathCursor::normalize()
855 if (idx() >= par()->nargs()) {
856 lyxerr << "this should not really happen - 1: "
857 << idx() << " " << par()->nargs() << " in: " << par() << "\n";
860 idx() = min(idx(), par()->nargs() - 1);
862 if (pos() > size()) {
863 lyxerr << "this should not really happen - 2: "
864 << pos() << " " << size() << " in idx: " << idx()
866 WriteStream wi(lyxerr, false, true);
871 pos() = min(pos(), size());
873 // remove empty scripts if possible
875 for (pos_type i = 0; i < size(); ++i) {
876 MathScriptInset * p = array()[i].nucleus()->asScriptInset();
878 p->removeEmptyScripts();
885 // fix again position
886 pos() = min(pos(), size());
890 MathCursor::size_type MathCursor::size() const
892 return array().size();
896 MathCursor::col_type MathCursor::hullCol() const
899 MathHullInset * p = enclosingHull(idx);
904 MathCursor::row_type MathCursor::hullRow() const
907 MathHullInset * p = enclosingHull(idx);
912 bool MathCursor::hasPrevAtom() const
918 bool MathCursor::hasNextAtom() const
920 return pos() < size();
924 MathAtom const & MathCursor::prevAtom() const
926 lyx::Assert(pos() > 0);
927 return array()[pos() - 1];
931 MathAtom & MathCursor::prevAtom()
933 lyx::Assert(pos() > 0);
934 return array()[pos() - 1];
938 MathAtom const & MathCursor::nextAtom() const
940 lyx::Assert(pos() < size());
941 return array()[pos()];
945 MathAtom & MathCursor::nextAtom()
947 lyx::Assert(pos() < size());
948 return array()[pos()];
952 MathArray & MathCursor::array() const
954 static MathArray dummy;
956 if (idx() >= par()->nargs()) {
957 lyxerr << "############ idx_ " << idx() << " not valid\n";
962 lyxerr << "############ depth() == 0 not valid\n";
966 return cursor().cell();
970 void MathCursor::idxNext()
972 par()->idxNext(idx(), pos());
976 void MathCursor::idxPrev()
978 par()->idxPrev(idx(), pos());
982 void MathCursor::splitCell()
984 if (idx() + 1 == par()->nargs())
986 MathArray ar = array();
988 array().erase(pos(), size());
991 array().insert(0, ar);
995 void MathCursor::breakLine()
1002 MathHullInset * p = enclosingHull(dummy);
1006 if (p->getType() == "simple" || p->getType() == "equation") {
1007 p->mutate("eqnarray");
1011 p->addRow(hullRow());
1014 const row_type r = hullRow();
1015 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1016 std::swap(p->cell(p->index(r, c)), p->cell(p->index(r + 1, c)));
1020 std::swap(p->cell(idx()), p->cell(idx() + p->ncols() - 1));
1025 //void MathCursor::readLine(MathArray & ar) const
1027 // idx_type base = row() * par()->ncols();
1028 // for (idx_type off = 0; off < par()->ncols(); ++off)
1029 // ar.push_back(par()->cell(base + off));
1033 char MathCursor::valign() const
1036 MathGridInset * p = enclosingGrid(idx);
1037 return p ? p->valign() : '\0';
1041 char MathCursor::halign() const
1044 MathGridInset * p = enclosingGrid(idx);
1045 return p ? p->halign(idx % p->ncols()) : '\0';
1049 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1051 MathCursorPos anc = normalAnchor();
1052 if (anc < cursor()) {
1062 MathCursorPos & MathCursor::cursor()
1064 lyx::Assert(depth());
1065 return Cursor_.back();
1069 MathCursorPos const & MathCursor::cursor() const
1071 lyx::Assert(depth());
1072 return Cursor_.back();
1076 bool MathCursor::goUpDown(bool up)
1078 // Be warned: The 'logic' implemented in this function is highly fragile.
1079 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1080 // So fiddle around with it only if you know what you are doing!
1085 // check if we had something else in mind, if not, this is the future goal
1091 // try neigbouring script insets
1093 if (hasPrevAtom()) {
1094 MathScriptInset const * p = prevAtom()->asScriptInset();
1095 if (p && p->has(up)) {
1098 idx() = up; // the superscript has index 1
1100 ///lyxerr << "updown: handled by scriptinset to the left\n";
1106 if (hasNextAtom()) {
1107 MathScriptInset const * p = nextAtom()->asScriptInset();
1108 if (p && p->has(up)) {
1112 ///lyxerr << "updown: handled by scriptinset to the right\n";
1118 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1123 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1124 // lyxerr << "updown: handled by brute find in the same cell\n";
1128 // try to find an inset that knows better then we
1130 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1132 if (par()->idxUpDown(idx(), pos(), up, targetx_))
1135 // no such inset found, just take something "above"
1136 ///lyxerr << "updown: handled by strange case\n";
1142 up ? formula()->ylow() : yo + 4,
1143 up ? yo - 4 : formula()->yhigh()
1146 // any improvement so far?
1149 if (up ? ynew < yo : ynew > yo)
1155 bool MathCursor::bruteFind
1156 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1158 MathIterator best_cursor;
1159 double best_dist = 1e10;
1161 MathIterator it = ibegin(formula()->par().nucleus());
1162 MathIterator et = iend(formula()->par().nucleus());
1164 // avoid invalid nesting when selecting
1165 if (!selection_ || positionable(it, Anchor_)) {
1167 it.back().getPos(xo, yo);
1168 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1169 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1170 // '<=' in order to take the last possible position
1171 // this is important for clicking behind \sum in e.g. '\sum_i a'
1172 if (d <= best_dist) {
1184 if (best_dist < 1e10)
1185 Cursor_ = best_cursor;
1186 return best_dist < 1e10;
1190 bool MathCursor::idxLineFirst()
1192 idx() -= idx() % par()->ncols();
1198 bool MathCursor::idxLineLast()
1200 idx() -= idx() % par()->ncols();
1201 idx() += par()->ncols() - 1;
1206 bool MathCursor::idxLeft()
1208 return par()->idxLeft(idx(), pos());
1212 bool MathCursor::idxRight()
1214 return par()->idxRight(idx(), pos());
1218 bool MathCursor::interpret(string const & s)
1220 //lyxerr << "interpret 1: '" << s << "'\n";
1224 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1225 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1226 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1228 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1230 istringstream is(s.substr(5).c_str());
1233 niceInsert(MathAtom(new MathCasesInset(n)));
1237 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1242 istringstream is(s.substr(6).c_str());
1243 is >> m >> n >> v_align >> h_align;
1247 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1251 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1253 istringstream is(s.substr(7).c_str());
1256 mathed_parse_cell(rep.from, from);
1257 mathed_parse_cell(rep.to, to);
1258 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1259 par()->replace(rep);
1263 string name = s.substr(1);
1265 if (name == "over" || name == "choose" || name == "atop") {
1266 MathAtom t(createMathInset(name));
1267 t.nucleus()->asNestInset()->cell(0) = array();
1276 // prevent entering of recursive macros
1277 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1278 && formula()->getInsetName() == name)
1280 lyxerr << "can't enter recursive macro\n";
1284 niceInsert(createMathInset(name));
1289 bool MathCursor::script(bool up)
1291 // Hack to get \\^ and \\_ working
1292 if (inMacroMode() && macroName() == "\\") {
1294 interpret("\\mathcircumflex");
1301 MathGridInset safe = grabAndEraseSelection();
1303 // we are in a nucleus of a script inset, move to _our_ script
1304 par()->asScriptInset()->ensure(up);
1307 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1308 prevAtom().nucleus()->asScriptInset()->ensure(up);
1309 pushRight(prevAtom());
1312 } else if (hasPrevAtom()) {
1314 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1315 pushLeft(nextAtom());
1319 plainInsert(MathAtom(new MathScriptInset(up)));
1320 prevAtom().nucleus()->asScriptInset()->ensure(up);
1321 pushRight(prevAtom());
1331 bool MathCursor::interpret(char c)
1333 //lyxerr << "interpret 2: '" << c << "'\n";
1334 targetx_ = -1; // "no target"
1335 if (inMacroArgMode()) {
1339 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1340 if (p && 1 <= n && n <= p->numargs())
1341 insert(MathAtom(new MathMacroArgument(c - '0')));
1343 insert(createMathInset("#"));
1344 interpret(c); // try again
1350 if (inMacroMode()) {
1351 string name = macroName();
1352 //lyxerr << "interpret name: '" << name << "'\n";
1354 if (name.empty() && c == '\\') {
1356 interpret("\\backslash");
1361 activeMacro()->setName(activeMacro()->name() + c);
1365 // handle 'special char' macros
1370 interpret("\\backslash");
1372 interpret(string("\\") + c);
1376 // leave macro mode and try again if necessary
1383 // This is annoying as one has to press <space> far too often.
1387 // leave autocorrect mode if necessary
1388 if (autocorrect_ && c == ' ') {
1389 autocorrect_ = false;
1394 // just clear selection on pressing the space bar
1395 if (selection_ && c == ' ') {
1403 //lyxerr << "starting with macro\n";
1404 insert(MathAtom(new MathUnknownInset("\\", false)));
1409 if (currentMode() == MathInset::TEXT_MODE)
1415 if (currentMode() == MathInset::TEXT_MODE) {
1416 // insert spaces in text mode,
1417 // but suppress direct insertion of two spaces in a row
1418 // the still allows typing '<space>a<space>' and deleting the 'a', but
1419 // it is better than nothing...
1420 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1424 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1425 prevAtom().nucleus()->asSpaceInset()->incSpace();
1430 // if are at the very end, leave the formula
1431 return pos() != size();
1435 insert(c); // LM_TC_TEX;
1440 if (c == '{' || c == '}', c)) {
1441 insert(c); // LM_TC_TEX;
1447 niceInsert(MathAtom(new MathBraceInset));
1456 insert(createMathInset("$"));
1461 insert(createMathInset("%"));
1466 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1467 insert(c, LM_TC_VAR);
1471 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1472 insert(c, LM_TC_VAR);
1473 lastcode_ = LM_TC_VAR;
1478 insert(c, LM_TC_TEX);
1479 //bv->owner()->message(_("TeX mode"));
1484 // try auto-correction
1485 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1488 // no special circumstances, so insert the character without any fuss
1490 autocorrect_ = true;
1495 void MathCursor::setSelection(MathIterator const & where, size_type n)
1504 void MathCursor::insetToggle()
1506 if (hasNextAtom()) {
1507 // toggle previous inset ...
1508 nextAtom().nucleus()->lock(!nextAtom()->lock());
1509 } else if (popLeft() && hasNextAtom()) {
1510 // ... or enclosing inset if we are in the last inset position
1511 nextAtom().nucleus()->lock(!nextAtom()->lock());
1517 string MathCursor::info() const
1520 os << "Math editor mode. ";
1521 for (int i = 0, n = depth(); i < n; ++i) {
1522 Cursor_[i].par_->infoize(os);
1526 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1527 prevAtom()->infoize(os);
1529 return os.str().c_str(); // .c_str() needed for lyxstring
1533 unsigned MathCursor::depth() const
1535 return Cursor_.size();
1543 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1544 MathInset::row_type & r1, MathInset::row_type & r2,
1545 MathInset::col_type & c1, MathInset::col_type & c2)
1547 MathInset * p = i1.par_;
1548 c1 = p->col(i1.idx_);
1549 c2 = p->col(i2.idx_);
1552 r1 = p->row(i1.idx_);
1553 r2 = p->row(i2.idx_);
1561 MathGridInset MathCursor::grabSelection() const
1564 return MathGridInset();
1567 getSelection(i1, i2);
1568 // shouldn't we assert on i1.par_ == i2.par_?
1569 if (i1.idx_ == i2.idx_) {
1570 MathGridInset data(1, 1);
1571 MathArray::const_iterator it = i1.cell().begin();
1572 data.cell(0) = MathArray(it + i1.pos_, it + i2.pos_);
1577 region(i1, i2, r1, r2, c1, c2);
1578 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1579 for (row_type row = 0; row < data.nrows(); ++row)
1580 for (col_type col = 0; col < data.ncols(); ++col) {
1581 idx_type i = i1.par_->index(row + r1, col + c1);
1582 data.cell(data.index(row, col)) = i1.par_->cell(i);
1588 void MathCursor::eraseSelection()
1592 getSelection(i1, i2);
1593 if (i1.idx_ == i2.idx_)
1594 i1.cell().erase(i1.pos_, i2.pos_);
1596 MathInset * p = i1.par_;
1599 region(i1, i2, r1, r2, c1, c2);
1600 for (row_type row = r1; row <= r2; ++row)
1601 for (col_type col = c1; col <= c2; ++col)
1602 p->cell(p->index(row, col)).clear();
1608 MathGridInset MathCursor::grabAndEraseSelection()
1611 return MathGridInset();
1612 MathGridInset res = grabSelection();
1619 MathCursorPos MathCursor::normalAnchor() const
1621 if (Anchor_.size() < depth()) {
1623 lyxerr << "unusual Anchor size\n";
1625 //lyx::Assert(Anchor_.size() >= cursor.depth());
1626 // use Anchor on the same level as Cursor
1627 MathCursorPos normal = Anchor_[depth() - 1];
1628 if (depth() < Anchor_.size() && !(normal < cursor())) {
1629 // anchor is behind cursor -> move anchor behind the inset
1637 void MathCursor::handleExtern(const string & arg)
1641 istringstream iss(arg.c_str());
1642 iss >> lang >> extra;
1650 lyxerr << "use selection: " << ar << "\n";
1651 insert(pipeThroughExtern(lang, extra, ar));
1656 eq.push_back(MathAtom(new MathCharInset('=')));
1658 popToEnclosingHull();
1661 MathHullInset * hull = enclosingHull(idx);
1665 if (hull->getType() == "simple") {
1666 MathArray::size_type pos = cursor().cell().find_last(eq);
1668 if (pos == size()) {
1670 lyxerr << "use whole cell: " << ar << "\n";
1672 ar = MathArray(array().begin() + pos + 1, array().end());
1673 lyxerr << "use partial cell form pos: " << pos << "\n";
1677 insert(pipeThroughExtern(lang, extra, ar));
1681 if (hull->getType() == "equation") {
1682 lyxerr << "use equation inset\n";
1683 hull->mutate("eqnarray");
1684 MathArray & ar = cursor().cell();
1685 lyxerr << "use cell: " << ar << "\n";
1687 cursor().cell() = eq;
1689 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1695 lyxerr << "use eqnarray\n";
1697 MathArray ar = cursor().cell();
1698 lyxerr << "use cell: " << ar << "\n";
1701 cursor().cell() = eq;
1703 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1710 int MathCursor::dispatch(string const & cmd)
1712 // try to dispatch to adajcent items if they are not editable
1713 // actually, this should only happen for mouse clicks...
1714 if (hasNextAtom() && !openable(nextAtom(), false))
1715 if (int res = nextAtom().nucleus()->dispatch(cmd, 0, 0))
1717 if (hasPrevAtom() && !openable(prevAtom(), false))
1718 if (int res = prevAtom().nucleus()->dispatch(cmd, 0, 0))
1721 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1722 MathCursorPos & pos = Cursor_[i];
1723 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))
1730 MathInset::mode_type MathCursor::currentMode() const
1732 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1733 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1734 if (res != MathInset::UNDECIDED_MODE)
1737 return MathInset::UNDECIDED_MODE;
1741 void releaseMathCursor(BufferView * bv)
1745 mathcursor->formula()->hideInsetCursor(bv);