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();
383 // enter the new inset and move the contents of the selection if possible
386 pushLeft(nextAtom());
392 void MathCursor::insert(MathArray const & ar)
398 array().insert(pos(), ar);
403 void MathCursor::paste(MathArray const & ar)
407 array().insert(pos(), ar);
412 void MathCursor::paste(MathGridInset const & data)
414 if (data.nargs() == 1) {
415 // single cell/part of cell
419 idx_type idx; // index of upper left cell
420 MathGridInset * p = enclosingGrid(idx);
421 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
422 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
423 for (row_type row = 0; row < numrows; ++row) {
424 for (col_type col = 0; col < numcols; ++col) {
425 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
426 p->cell(i).append(data.cell(data.index(row, col)));
428 // append the left over horizontal cells to the last column
429 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
430 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
431 p->cell(i).append(data.cell(data.index(row, col)));
433 // append the left over vertical cells to the last _cell_
434 idx_type i = p->nargs() - 1;
435 for (row_type row = numrows; row < data.nrows(); ++row)
436 for (col_type col = 0; col < data.ncols(); ++col)
437 p->cell(i).append(data.cell(data.index(row, col)));
442 void MathCursor::backspace()
444 autocorrect_ = false;
456 if (prevAtom()->asScriptInset()) {
457 // simply enter nucleus
474 void MathCursor::erase()
476 autocorrect_ = false;
485 // delete empty cells if possible
487 if (par()->idxDelete(idx()))
490 // old behaviour when in last position of cell
491 if (pos() == size()) {
492 par()->idxGlue(idx());
497 // if we are standing in front of a script inset, grab item before us and
498 // move it into nucleus
499 // and remove first thing.
500 if (hasNextAtom() && nextAtom()->asScriptInset()) {
502 MathAtom at = prevAtom();
504 array().erase(pos());
505 pushLeft(nextAtom());
507 array().push_back(at);
512 pushLeft(nextAtom());
523 void MathCursor::delLine()
525 autocorrect_ = false;
533 if (par()->nrows() > 1) {
534 // grid are the only things with more than one row...
535 lyx::Assert(par()->asGridInset());
536 par()->asGridInset()->delRow(hullRow());
539 if (idx() >= par()->nargs())
540 idx() = par()->nargs() - 1;
547 bool MathCursor::up(bool sel)
552 MathIterator save = Cursor_;
556 autocorrect_ = false;
561 bool MathCursor::down(bool sel)
566 MathIterator save = Cursor_;
570 autocorrect_ = false;
575 bool MathCursor::toggleLimits()
579 MathScriptInset * t = nextAtom()->asScriptInset();
582 int old = t->limits();
583 t->limits(old < 0 ? 1 : -1);
584 return old != t->limits();
588 void MathCursor::macroModeClose()
590 MathUnknownInset * p = inMacroMode();
594 string s = p->name();
596 array().erase(pos());
602 string MathCursor::macroName() const
604 return inMacroMode() ? inMacroMode()->name() : string();
608 void MathCursor::selClear()
615 void MathCursor::selCopy()
619 theCutBuffer = grabSelection();
622 theCutBuffer = MathGridInset(1, 1);
627 void MathCursor::selCut()
630 theCutBuffer = grabAndEraseSelection();
634 void MathCursor::selDel()
644 void MathCursor::selPaste()
654 void MathCursor::selHandle(bool sel)
656 if (sel == selection_)
664 void MathCursor::selStart()
674 void MathCursor::selClearOrDel()
676 if (lyxrc.auto_region_delete)
683 void MathCursor::selGet(MathArray & ar)
687 ar = grabSelection().glue();
692 void MathCursor::drawSelection(MathPainterInfo & pi) const
698 getSelection(i1, i2);
699 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
703 void MathCursor::handleNest(MathAtom const & at)
705 at->cell(0) = grabAndEraseSelection().glue();
707 pushRight(prevAtom());
711 void MathCursor::getPos(int & x, int & y)
713 par()->getPos(idx(), pos(), x, y);
717 MathInset * MathCursor::par() const
719 return cursor().par_;
723 InsetFormulaBase * MathCursor::formula() const
729 MathCursor::idx_type MathCursor::idx() const
731 return cursor().idx_;
735 MathCursor::idx_type & MathCursor::idx()
737 return cursor().idx_;
741 MathCursor::pos_type MathCursor::pos() const
743 return cursor().pos_;
747 MathCursor::pos_type & MathCursor::pos()
749 return cursor().pos_;
753 MathUnknownInset * MathCursor::inMacroMode() const
757 MathUnknownInset * p = prevAtom()->asUnknownInset();
758 return (p && !p->final()) ? p : 0;
762 bool MathCursor::inMacroArgMode() const
764 return pos() > 0 && prevAtom()->getChar() == '#';
768 bool MathCursor::selection() const
774 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
776 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
777 MathGridInset * p = Cursor_[i].par_->asGridInset();
779 idx = Cursor_[i].idx_;
787 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
789 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
790 MathHullInset * p = Cursor_[i].par_->asHullInset();
792 idx = Cursor_[i].idx_;
800 void MathCursor::popToEnclosingGrid()
802 while (depth() && !Cursor_.back().par_->asGridInset())
807 void MathCursor::popToEnclosingHull()
809 while (depth() && !Cursor_.back().par_->asHullInset())
814 void MathCursor::pullArg()
817 MathArray a = array();
820 array().insert(pos(), a);
823 formula()->mutateToText();
828 void MathCursor::touch()
830 MathIterator::const_iterator it = Cursor_.begin();
831 MathIterator::const_iterator et = Cursor_.end();
832 for ( ; it != et; ++it)
837 void MathCursor::normalize()
839 if (idx() >= par()->nargs()) {
840 lyxerr << "this should not really happen - 1: "
841 << idx() << " " << par()->nargs() << " in: " << par() << "\n";
844 idx() = min(idx(), par()->nargs() - 1);
846 if (pos() > size()) {
847 lyxerr << "this should not really happen - 2: "
848 << pos() << " " << size() << " in idx: " << idx()
850 WriteStream wi(lyxerr, false, true);
855 pos() = min(pos(), size());
857 // remove empty scripts if possible
859 for (pos_type i = 0; i < size(); ++i) {
860 MathScriptInset * p = array()[i]->asScriptInset();
862 p->removeEmptyScripts();
869 // fix again position
870 pos() = min(pos(), size());
874 MathCursor::size_type MathCursor::size() const
876 return array().size();
880 MathCursor::col_type MathCursor::hullCol() const
883 MathHullInset * p = enclosingHull(idx);
888 MathCursor::row_type MathCursor::hullRow() const
891 MathHullInset * p = enclosingHull(idx);
896 bool MathCursor::hasPrevAtom() const
902 bool MathCursor::hasNextAtom() const
904 return pos() < size();
908 MathAtom const & MathCursor::prevAtom() const
910 lyx::Assert(pos() > 0);
911 return array()[pos() - 1];
915 MathAtom & MathCursor::prevAtom()
917 lyx::Assert(pos() > 0);
918 return array()[pos() - 1];
922 MathAtom const & MathCursor::nextAtom() const
924 lyx::Assert(pos() < size());
925 return array()[pos()];
929 MathAtom & MathCursor::nextAtom()
931 lyx::Assert(pos() < size());
932 return array()[pos()];
936 MathArray & MathCursor::array() const
938 static MathArray dummy;
940 if (idx() >= par()->nargs()) {
941 lyxerr << "############ idx_ " << idx() << " not valid\n";
946 lyxerr << "############ depth() == 0 not valid\n";
950 return cursor().cell();
954 MathXArray & MathCursor::xarray() const
956 static MathXArray dummy;
959 lyxerr << "############ depth() == 0 not valid\n";
963 return cursor().xcell();
967 void MathCursor::idxNext()
969 par()->idxNext(idx(), pos());
973 void MathCursor::idxPrev()
975 par()->idxPrev(idx(), pos());
979 void MathCursor::splitCell()
981 if (idx() + 1 == par()->nargs())
983 MathArray ar = array();
985 array().erase(pos(), size());
988 array().insert(0, ar);
992 void MathCursor::breakLine()
999 MathHullInset * p = enclosingHull(dummy);
1003 if (p->getType() == "simple" || p->getType() == "equation") {
1004 p->mutate("eqnarray");
1008 p->addRow(hullRow());
1011 const row_type r = hullRow();
1012 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1013 std::swap(p->cell(p->index(r, c)), p->cell(p->index(r + 1, c)));
1017 std::swap(p->cell(idx()), p->cell(idx() + p->ncols() - 1));
1022 //void MathCursor::readLine(MathArray & ar) const
1024 // idx_type base = row() * par()->ncols();
1025 // for (idx_type off = 0; off < par()->ncols(); ++off)
1026 // ar.push_back(par()->cell(base + off));
1030 char MathCursor::valign() const
1033 MathGridInset * p = enclosingGrid(idx);
1034 return p ? p->valign() : '\0';
1038 char MathCursor::halign() const
1041 MathGridInset * p = enclosingGrid(idx);
1042 return p ? p->halign(idx % p->ncols()) : '\0';
1046 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1048 MathCursorPos anc = normalAnchor();
1049 if (anc < cursor()) {
1059 MathCursorPos & MathCursor::cursor()
1061 lyx::Assert(depth());
1062 return Cursor_.back();
1066 MathCursorPos const & MathCursor::cursor() const
1068 lyx::Assert(depth());
1069 return Cursor_.back();
1073 bool MathCursor::goUpDown(bool up)
1075 // Be warned: The 'logic' implemented in this function is highly fragile.
1076 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1077 // So fiddle around with it only if you know what you are doing!
1081 // check if we had something else in mind, if not, this is the future goal
1087 // try neigbouring script insets
1089 if (hasPrevAtom()) {
1090 MathScriptInset * p = prevAtom()->asScriptInset();
1091 if (p && p->has(up)) {
1094 idx() = up; // the superscript has index 1
1096 ///lyxerr << "updown: handled by scriptinset to the left\n";
1102 if (hasNextAtom()) {
1103 MathScriptInset * p = nextAtom()->asScriptInset();
1104 if (p && p->has(up)) {
1108 ///lyxerr << "updown: handled by scriptinset to the right\n";
1114 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1119 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1120 // lyxerr << "updown: handled by brute find in the same cell\n";
1124 // try to find an inset that knows better then we
1126 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1128 if (par()->idxUpDown(idx(), pos(), up, targetx_))
1131 // no such inset found, just take something "above"
1132 ///lyxerr << "updown: handled by strange case\n";
1138 up ? formula()->ylow() : yo + 4,
1139 up ? yo - 4 : formula()->yhigh()
1142 // any improvement so far?
1145 if (up ? ynew < yo : ynew > yo)
1151 bool MathCursor::bruteFind
1152 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1154 MathIterator best_cursor;
1155 double best_dist = 1e10;
1157 MathIterator it = ibegin(formula()->par().nucleus());
1158 MathIterator et = iend(formula()->par().nucleus());
1160 // avoid invalid nesting when selecting
1161 if (!selection_ || positionable(it, Anchor_)) {
1163 it.back().getPos(xo, yo);
1164 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1165 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1166 // '<=' in order to take the last possible position
1167 // this is important for clicking behind \sum in e.g. '\sum_i a'
1168 if (d <= best_dist) {
1180 if (best_dist < 1e10)
1181 Cursor_ = best_cursor;
1182 return best_dist < 1e10;
1186 bool MathCursor::idxLineFirst()
1188 idx() -= idx() % par()->ncols();
1194 bool MathCursor::idxLineLast()
1196 idx() -= idx() % par()->ncols();
1197 idx() += par()->ncols() - 1;
1202 bool MathCursor::idxLeft()
1204 return par()->idxLeft(idx(), pos());
1208 bool MathCursor::idxRight()
1210 return par()->idxRight(idx(), pos());
1214 bool MathCursor::interpret(string const & s)
1216 //lyxerr << "interpret 1: '" << s << "'\n";
1220 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1221 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1222 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1224 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1226 istringstream is(s.substr(5).c_str());
1229 niceInsert(MathAtom(new MathCasesInset(n)));
1233 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1238 istringstream is(s.substr(6).c_str());
1239 is >> m >> n >> v_align >> h_align;
1243 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1247 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1249 istringstream is(s.substr(7).c_str());
1252 mathed_parse_cell(rep.from, from);
1253 mathed_parse_cell(rep.to, to);
1254 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1255 par()->replace(rep);
1259 string name = s.substr(1);
1261 if (name == "over" || name == "choose" || name == "atop") {
1262 MathAtom t(createMathInset(name));
1263 t->asNestInset()->cell(0) = array();
1272 // prevent entering of recursive macros
1273 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1274 && formula()->getInsetName() == name)
1276 lyxerr << "can't enter recursive macro\n";
1280 niceInsert(createMathInset(name));
1285 bool MathCursor::script(bool up)
1287 // Hack to get \\^ and \\_ working
1288 if (inMacroMode() && macroName() == "\\") {
1290 interpret("\\mathcircumflex");
1297 MathGridInset safe = grabAndEraseSelection();
1299 // we are in a nucleus of a script inset, move to _our_ script
1300 par()->asScriptInset()->ensure(up);
1303 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1304 prevAtom()->asScriptInset()->ensure(up);
1305 pushRight(prevAtom());
1308 } else if (hasPrevAtom()) {
1310 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1311 pushLeft(nextAtom());
1315 plainInsert(MathAtom(new MathScriptInset(up)));
1316 prevAtom()->asScriptInset()->ensure(up);
1317 pushRight(prevAtom());
1327 bool MathCursor::interpret(char c)
1329 //lyxerr << "interpret 2: '" << c << "'\n";
1330 targetx_ = -1; // "no target"
1331 if (inMacroArgMode()) {
1335 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1336 if (p && 1 <= n && n <= p->numargs())
1337 insert(MathAtom(new MathMacroArgument(c - '0')));
1339 insert(createMathInset("#"));
1340 interpret(c); // try again
1346 if (inMacroMode()) {
1347 string name = macroName();
1348 //lyxerr << "interpret name: '" << name << "'\n";
1350 if (name.empty() && c == '\\') {
1352 interpret("\\backslash");
1357 inMacroMode()->name() += c;
1361 // handle 'special char' macros
1366 interpret("\\backslash");
1368 interpret(string("\\") + c);
1372 // leave macro mode and try again if necessary
1379 // This is annoying as one has to press <space> far too often.
1383 // leave autocorrect mode if necessary
1384 if (autocorrect_ && c == ' ') {
1385 autocorrect_ = false;
1390 // just clear selection on pressing the space bar
1391 if (selection_ && c == ' ') {
1399 //lyxerr << "starting with macro\n";
1400 insert(MathAtom(new MathUnknownInset("\\", false)));
1405 if (currentMode() == MathInset::TEXT_MODE)
1411 if (currentMode() == MathInset::TEXT_MODE) {
1412 // insert spaces in text mode,
1413 // but suppress direct insertion of two spaces in a row
1414 // the still allows typing '<space>a<space>' and deleting the 'a', but
1415 // it is better than nothing...
1416 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1420 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1421 prevAtom()->asSpaceInset()->incSpace();
1426 // if are at the very end, leave the formula
1427 return pos() != size();
1431 insert(c); // LM_TC_TEX;
1436 if (c == '{' || c == '}', c)) {
1437 insert(c); // LM_TC_TEX;
1443 niceInsert(MathAtom(new MathBraceInset));
1452 insert(createMathInset("$"));
1457 insert(createMathInset("%"));
1462 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1463 insert(c, LM_TC_VAR);
1467 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1468 insert(c, LM_TC_VAR);
1469 lastcode_ = LM_TC_VAR;
1474 insert(c, LM_TC_TEX);
1475 //bv->owner()->message(_("TeX mode"));
1480 // try auto-correction
1481 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1484 // no special circumstances, so insert the character without any fuss
1486 autocorrect_ = true;
1491 void MathCursor::setSelection(MathIterator const & where, size_type n)
1500 void MathCursor::insetToggle()
1502 if (hasNextAtom()) {
1503 // toggle previous inset ...
1504 nextAtom()->lock(!nextAtom()->lock());
1505 } else if (popLeft() && hasNextAtom()) {
1506 // ... or enclosing inset if we are in the last inset position
1507 nextAtom()->lock(!nextAtom()->lock());
1513 string MathCursor::info() const
1516 os << "Math editor mode. ";
1517 for (int i = 0, n = depth(); i < n; ++i) {
1518 Cursor_[i].par_->infoize(os);
1522 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1523 prevAtom()->infoize(os);
1525 return os.str().c_str(); // .c_str() needed for lyxstring
1529 unsigned MathCursor::depth() const
1531 return Cursor_.size();
1539 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1540 MathInset::row_type & r1, MathInset::row_type & r2,
1541 MathInset::col_type & c1, MathInset::col_type & c2)
1543 MathInset * p = i1.par_;
1544 c1 = p->col(i1.idx_);
1545 c2 = p->col(i2.idx_);
1548 r1 = p->row(i1.idx_);
1549 r2 = p->row(i2.idx_);
1557 MathGridInset MathCursor::grabSelection() const
1560 return MathGridInset();
1563 getSelection(i1, i2);
1564 // shouldn't we assert on i1.par_ == i2.par_?
1565 if (i1.idx_ == i2.idx_) {
1566 MathGridInset data(1, 1);
1567 MathArray::const_iterator it = i1.cell().begin();
1568 data.cell(0) = MathArray(it + i1.pos_, it + i2.pos_);
1573 region(i1, i2, r1, r2, c1, c2);
1574 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1575 for (row_type row = 0; row < data.nrows(); ++row)
1576 for (col_type col = 0; col < data.ncols(); ++col) {
1577 idx_type i = i1.par_->index(row + r1, col + c1);
1578 data.cell(data.index(row, col)) = i1.par_->cell(i);
1584 void MathCursor::eraseSelection()
1588 getSelection(i1, i2);
1589 if (i1.idx_ == i2.idx_)
1590 i1.cell().erase(i1.pos_, i2.pos_);
1592 MathInset * p = i1.par_;
1595 region(i1, i2, r1, r2, c1, c2);
1596 for (row_type row = r1; row <= r2; ++row)
1597 for (col_type col = c1; col <= c2; ++col)
1598 p->cell(p->index(row, col)).clear();
1604 MathGridInset MathCursor::grabAndEraseSelection()
1607 return MathGridInset();
1608 MathGridInset res = grabSelection();
1615 MathCursorPos MathCursor::normalAnchor() const
1617 if (Anchor_.size() < depth()) {
1619 lyxerr << "unusual Anchor size\n";
1621 //lyx::Assert(Anchor_.size() >= cursor.depth());
1622 // use Anchor on the same level as Cursor
1623 MathCursorPos normal = Anchor_[depth() - 1];
1624 if (depth() < Anchor_.size() && !(normal < cursor())) {
1625 // anchor is behind cursor -> move anchor behind the inset
1633 void MathCursor::handleExtern(const string & arg)
1637 istringstream iss(arg.c_str());
1638 iss >> lang >> extra;
1646 lyxerr << "use selection: " << ar << "\n";
1647 insert(pipeThroughExtern(lang, extra, ar));
1652 eq.push_back(MathAtom(new MathCharInset('=')));
1654 popToEnclosingHull();
1657 MathHullInset * hull = enclosingHull(idx);
1661 if (hull->getType() == "simple") {
1662 MathArray::size_type pos = cursor().cell().find_last(eq);
1664 if (pos == size()) {
1666 lyxerr << "use whole cell: " << ar << "\n";
1668 ar = MathArray(array().begin() + pos + 1, array().end());
1669 lyxerr << "use partial cell form pos: " << pos << "\n";
1673 insert(pipeThroughExtern(lang, extra, ar));
1677 if (hull->getType() == "equation") {
1678 lyxerr << "use equation inset\n";
1679 hull->mutate("eqnarray");
1680 MathArray & ar = cursor().cell();
1681 lyxerr << "use cell: " << ar << "\n";
1683 cursor().cell() = eq;
1685 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1691 lyxerr << "use eqnarray\n";
1693 MathArray ar = cursor().cell();
1694 lyxerr << "use cell: " << ar << "\n";
1697 cursor().cell() = eq;
1699 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1706 int MathCursor::dispatch(string const & cmd)
1708 // try to dispatch to adajcent items if they are not editable
1709 // actually, this should only happen for mouse clicks...
1710 if (hasNextAtom() && !openable(nextAtom(), false))
1711 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1713 if (hasPrevAtom() && !openable(prevAtom(), false))
1714 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1717 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1718 MathCursorPos & pos = Cursor_[i];
1719 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))
1726 MathInset::mode_type MathCursor::currentMode() const
1728 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1729 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1730 if (res != MathInset::UNDECIDED_MODE)
1733 return MathInset::UNDECIDED_MODE;
1737 void releaseMathCursor(BufferView * bv)
1741 mathcursor->formula()->hideInsetCursor(bv);