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
173 if (t->asScriptInset())
177 // we can't move into anything new during selection
178 if (depth() == Anchor_.size())
180 if (t.nucleus() != Anchor_[depth()].par_)
187 bool MathCursor::posLeft()
196 bool MathCursor::posRight()
205 bool MathCursor::left(bool sel)
208 autocorrect_ = false;
209 targetx_ = -1; // "no target"
216 if (hasPrevAtom() && openable(prevAtom(), sel)) {
217 pushRight(prevAtom());
221 return posLeft() || idxLeft() || popLeft() || selection_;
225 bool MathCursor::right(bool sel)
228 autocorrect_ = false;
229 targetx_ = -1; // "no target"
236 if (hasNextAtom() && openable(nextAtom(), sel)) {
237 pushLeft(nextAtom());
241 return posRight() || idxRight() || popRight() || selection_;
245 void MathCursor::first()
248 push(formula_->par());
249 par()->idxFirst(idx(), pos());
253 void MathCursor::last()
256 push(formula_->par());
257 par()->idxLast(idx(), pos());
261 bool positionable(MathIterator const & cursor, MathIterator const & anchor)
263 // avoid deeper nested insets when selecting
264 if (cursor.size() > anchor.size())
267 // anchor might be deeper, should have same path then
268 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
269 if (cursor[i].par_ != anchor[i].par_)
272 // position should be ok.
277 void MathCursor::setPos(int x, int y)
280 bool res = bruteFind(x, y,
281 formula()->xlow(), formula()->xhigh(),
282 formula()->ylow(), formula()->yhigh());
284 // this ccan happen on creation of "math-display"
293 bool MathCursor::home(bool sel)
296 autocorrect_ = false;
299 if (!par()->idxHome(idx(), pos()))
306 bool MathCursor::end(bool sel)
309 autocorrect_ = false;
312 if (!par()->idxEnd(idx(), pos()))
319 void MathCursor::plainErase()
321 array().erase(pos());
325 void MathCursor::markInsert()
327 //lyxerr << "inserting mark\n";
328 array().insert(pos(), MathAtom(new MathCharInset(0)));
332 void MathCursor::markErase()
334 //lyxerr << "deleting mark\n";
335 array().erase(pos());
339 void MathCursor::plainInsert(MathAtom const & t)
342 array().insert(pos(), t);
347 void MathCursor::insert(string const & str)
349 //lyxerr << "inserting '" << str << "'\n";
351 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
352 plainInsert(MathAtom(new MathCharInset(*it)));
356 void MathCursor::insert(char c)
358 //lyxerr << "inserting '" << c << "'\n";
360 plainInsert(MathAtom(new MathCharInset(c)));
364 void MathCursor::insert(MathAtom const & t)
372 void MathCursor::niceInsert(MathAtom const & t)
375 MathGridInset safe = grabAndEraseSelection();
377 // enter the new inset and move the contents of the selection if possible
380 pushLeft(nextAtom());
386 void MathCursor::insert(MathArray const & ar)
392 array().insert(pos(), ar);
397 void MathCursor::paste(MathArray const & ar)
401 array().insert(pos(), ar);
406 void MathCursor::paste(MathGridInset const & data)
408 if (data.nargs() == 1) {
409 // single cell/part of cell
413 idx_type idx; // index of upper left cell
414 MathGridInset * p = enclosingGrid(idx);
415 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
416 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
417 for (row_type row = 0; row < numrows; ++row) {
418 for (col_type col = 0; col < numcols; ++col) {
419 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
420 p->cell(i).push_back(data.cell(data.index(row, col)));
422 // append the left over horizontal cells to the last column
423 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
424 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
425 p->cell(i).push_back(data.cell(data.index(row, col)));
427 // append the left over vertical cells to the last _cell_
428 idx_type i = p->nargs() - 1;
429 for (row_type row = numrows; row < data.nrows(); ++row)
430 for (col_type col = 0; col < data.ncols(); ++col)
431 p->cell(i).push_back(data.cell(data.index(row, col)));
435 void MathCursor::backspace()
437 autocorrect_ = false;
448 MathScriptInset * p = prevAtom()->asScriptInset();
450 p->removeScript(p->hasUp());
451 // Don't delete if there is anything left
452 if (p->hasUp() || p->hasDown())
461 void MathCursor::erase()
463 autocorrect_ = false;
472 // delete empty cells if possible
474 if (par()->idxDelete(idx()))
477 // old behaviour when in last position of cell
478 if (pos() == size()) {
479 par()->idxGlue(idx());
483 MathScriptInset * p = nextAtom()->asScriptInset();
485 p->removeScript(p->hasUp());
486 // Don't delete if there is anything left
487 if (p->hasUp() || p->hasDown())
495 void MathCursor::delLine()
497 autocorrect_ = false;
505 if (par()->nrows() > 1) {
506 // grid are the only things with more than one row...
507 lyx::Assert(par()->asGridInset());
508 par()->asGridInset()->delRow(hullRow());
511 if (idx() >= par()->nargs())
512 idx() = par()->nargs() - 1;
519 bool MathCursor::up(bool sel)
524 MathIterator save = Cursor_;
528 autocorrect_ = false;
533 bool MathCursor::down(bool sel)
538 MathIterator save = Cursor_;
542 autocorrect_ = false;
547 bool MathCursor::toggleLimits()
551 MathScriptInset * t = nextAtom()->asScriptInset();
554 int old = t->limits();
555 t->limits(old < 0 ? 1 : -1);
556 return old != t->limits();
560 void MathCursor::macroModeClose()
562 MathUnknownInset * p = inMacroMode();
566 string s = p->name();
568 array().erase(pos());
574 string MathCursor::macroName() const
576 return inMacroMode() ? inMacroMode()->name() : string();
580 void MathCursor::selClear()
586 void MathCursor::selCopy()
590 theCutBuffer = grabSelection();
593 theCutBuffer = MathGridInset(1, 1);
598 void MathCursor::selCut()
601 theCutBuffer = grabAndEraseSelection();
605 void MathCursor::selDel()
615 void MathCursor::selPaste()
625 void MathCursor::selHandle(bool sel)
627 if (sel == selection_)
635 void MathCursor::selStart()
645 void MathCursor::selClearOrDel()
647 if (lyxrc.auto_region_delete)
654 void MathCursor::selGet(MathArray & ar)
658 ar = grabSelection().glue();
663 void MathCursor::drawSelection(MathPainterInfo & pi) const
669 getSelection(i1, i2);
670 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
674 void MathCursor::handleNest(MathAtom const & at)
676 at->cell(0) = grabAndEraseSelection().glue();
678 pushRight(prevAtom());
682 void MathCursor::getPos(int & x, int & y)
684 par()->getPos(idx(), pos(), x, y);
688 MathInset * MathCursor::par() const
690 return cursor().par_;
694 InsetFormulaBase * MathCursor::formula() const
700 MathCursor::idx_type MathCursor::idx() const
702 return cursor().idx_;
706 MathCursor::idx_type & MathCursor::idx()
708 return cursor().idx_;
712 MathCursor::pos_type MathCursor::pos() const
714 return cursor().pos_;
718 MathCursor::pos_type & MathCursor::pos()
720 return cursor().pos_;
724 MathUnknownInset * MathCursor::inMacroMode() const
728 MathUnknownInset * p = prevAtom()->asUnknownInset();
729 return (p && !p->final()) ? p : 0;
733 bool MathCursor::inMacroArgMode() const
735 return pos() > 0 && prevAtom()->getChar() == '#';
739 bool MathCursor::selection() const
745 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
747 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
748 MathGridInset * p = Cursor_[i].par_->asGridInset();
750 idx = Cursor_[i].idx_;
758 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
760 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
761 MathHullInset * p = Cursor_[i].par_->asHullInset();
763 idx = Cursor_[i].idx_;
771 void MathCursor::popToEnclosingGrid()
773 while (depth() && !Cursor_.back().par_->asGridInset())
778 void MathCursor::popToEnclosingHull()
780 while (depth() && !Cursor_.back().par_->asHullInset())
785 void MathCursor::pullArg()
788 MathArray a = array();
790 MathScriptInset const * p = par()->asScriptInset();
792 // special handling for scripts
793 const bool up = p->hasUp();
795 MathScriptInset * q = nextAtom()->asScriptInset();
799 array().insert(pos(), a);
805 array().insert(pos(), a);
808 formula()->mutateToText();
813 void MathCursor::touch()
815 MathIterator::const_iterator it = Cursor_.begin();
816 MathIterator::const_iterator et = Cursor_.end();
817 for ( ; it != et; ++it)
822 void MathCursor::normalize()
824 if (idx() >= par()->nargs()) {
825 lyxerr << "this should not really happen - 1: "
826 << idx() << " " << par()->nargs() << "\n";
829 idx() = min(idx(), par()->nargs() - 1);
831 if (pos() > size()) {
832 lyxerr << "this should not really happen - 2: "
833 << pos() << " " << size() << " in idx: " << idx()
835 WriteStream wi(lyxerr, false, true);
840 pos() = min(pos(), size());
842 // remove empty scripts if possible
844 for (pos_type i = 0; i < size(); ++i) {
845 MathScriptInset * p = array()[i]->asScriptInset();
847 p->removeEmptyScripts();
854 // fix again position
855 pos() = min(pos(), size());
859 MathCursor::size_type MathCursor::size() const
861 return array().size();
865 MathCursor::col_type MathCursor::hullCol() const
868 MathHullInset * p = enclosingHull(idx);
873 MathCursor::row_type MathCursor::hullRow() const
876 MathHullInset * p = enclosingHull(idx);
881 bool MathCursor::hasPrevAtom() const
887 bool MathCursor::hasNextAtom() const
889 return pos() < size();
893 MathAtom const & MathCursor::prevAtom() const
895 lyx::Assert(pos() > 0);
896 return array()[pos() - 1];
900 MathAtom & MathCursor::prevAtom()
902 lyx::Assert(pos() > 0);
903 return array()[pos() - 1];
907 MathAtom const & MathCursor::nextAtom() const
909 lyx::Assert(pos() < size());
910 return array()[pos()];
914 MathAtom & MathCursor::nextAtom()
916 lyx::Assert(pos() < size());
917 return array()[pos()];
921 MathArray & MathCursor::array() const
923 static MathArray dummy;
925 if (idx() >= par()->nargs()) {
926 lyxerr << "############ idx_ " << idx() << " not valid\n";
931 lyxerr << "############ depth() == 0 not valid\n";
935 return cursor().cell();
939 MathXArray & MathCursor::xarray() const
941 static MathXArray dummy;
944 lyxerr << "############ depth() == 0 not valid\n";
948 return cursor().xcell();
952 void MathCursor::idxNext()
954 par()->idxNext(idx(), pos());
958 void MathCursor::idxPrev()
960 par()->idxPrev(idx(), pos());
964 void MathCursor::splitCell()
966 if (idx() + 1 == par()->nargs())
968 MathArray ar = array();
970 array().erase(pos(), size());
973 array().insert(0, ar);
977 void MathCursor::breakLine()
984 MathHullInset * p = enclosingHull(dummy);
988 if (p->getType() == "simple" || p->getType() == "equation") {
989 p->mutate("eqnarray");
993 p->addRow(hullRow());
996 const row_type r = hullRow();
997 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
998 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1002 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1007 //void MathCursor::readLine(MathArray & ar) const
1009 // idx_type base = row() * par()->ncols();
1010 // for (idx_type off = 0; off < par()->ncols(); ++off)
1011 // ar.push_back(par()->cell(base + off));
1015 char MathCursor::valign() const
1018 MathGridInset * p = enclosingGrid(idx);
1019 return p ? p->valign() : '\0';
1023 char MathCursor::halign() const
1026 MathGridInset * p = enclosingGrid(idx);
1027 return p ? p->halign(idx % p->ncols()) : '\0';
1031 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1033 MathCursorPos anc = normalAnchor();
1034 if (anc < cursor()) {
1044 MathCursorPos & MathCursor::cursor()
1046 lyx::Assert(depth());
1047 return Cursor_.back();
1051 MathCursorPos const & MathCursor::cursor() const
1053 lyx::Assert(depth());
1054 return Cursor_.back();
1058 bool MathCursor::goUpDown(bool up)
1060 // Be warned: The 'logic' implemented in this function is highly fragile.
1061 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1062 // So fiddle around with it only if you know what you are doing!
1063 int xlow, xhigh, ylow, yhigh;
1068 // check if we had something else in mind, if not, this is the future goal
1074 // try neigbouring script insets
1076 if (hasPrevAtom()) {
1077 MathScriptInset * p = prevAtom()->asScriptInset();
1078 if (p && p->has(up)) {
1081 idx() = up; // the superscript has index 1
1083 ///lyxerr << "updown: handled by scriptinset to the left\n";
1089 if (hasNextAtom()) {
1090 MathScriptInset * p = nextAtom()->asScriptInset();
1091 if (p && p->has(up)) {
1095 ///lyxerr << "updown: handled by scriptinset to the right\n";
1101 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1106 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1107 // lyxerr << "updown: handled by brute find in the same cell\n";
1111 // try to find an inset that knows better then we
1113 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1115 if (par()->idxUpDown(idx(), pos(), up)) {
1116 #ifdef WITH_WARNINGS
1117 #warning this code should be moved to individual insets that handle this
1119 // position might have changed, so re-compute it
1121 // we found a cell that thinks it has something "below" us.
1122 //lyxerr << "updown: found inset that handles UpDown\n";
1123 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1124 // project (xo,yo) onto proper box
1125 //lyxerr << "\n xo: " << xo << " yo: " << yo
1126 // << "\n xlow: " << xlow << " ylow: " << ylow
1127 // << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1128 xo = min(max(xo, xlow), xhigh);
1129 yo = min(max(yo, ylow), yhigh);
1130 //lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1131 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1132 //lyxerr << "updown: handled by final brute find\n";
1138 // no such inset found, just take something "above"
1139 ///lyxerr << "updown: handled by strange case\n";
1144 up ? formula()->ylow() : yo + 4,
1145 up ? yo - 4 : formula()->yhigh()
1149 // any improvement so far?
1152 if (up ? ynew < yo : ynew > yo)
1158 bool MathCursor::bruteFind
1159 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1161 MathIterator best_cursor;
1162 double best_dist = 1e10;
1164 MathIterator it = ibegin(formula()->par().nucleus());
1165 MathIterator et = iend(formula()->par().nucleus());
1167 // avoid invalid nesting when selecting
1168 if (!selection_ || positionable(it, Anchor_)) {
1170 it.back().getPos(xo, yo);
1171 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1172 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1173 // '<=' in order to take the last possible position
1174 // this is important for clicking behind \sum in e.g. '\sum_i a'
1175 if (d <= best_dist) {
1187 if (best_dist < 1e10)
1188 Cursor_ = best_cursor;
1189 return best_dist < 1e10;
1193 bool MathCursor::idxLineFirst()
1195 idx() -= idx() % par()->ncols();
1201 bool MathCursor::idxLineLast()
1203 idx() -= idx() % par()->ncols();
1204 idx() += par()->ncols() - 1;
1209 bool MathCursor::idxLeft()
1211 return par()->idxLeft(idx(), pos());
1215 bool MathCursor::idxRight()
1217 return par()->idxRight(idx(), pos());
1221 bool MathCursor::interpret(string const & s)
1223 //lyxerr << "interpret 1: '" << s << "'\n";
1227 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1228 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1229 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1231 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1233 istringstream is(s.substr(5).c_str());
1236 niceInsert(MathAtom(new MathCasesInset(n)));
1240 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1245 istringstream is(s.substr(6).c_str());
1246 is >> m >> n >> v_align >> h_align;
1250 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1254 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1256 istringstream is(s.substr(7).c_str());
1259 mathed_parse_cell(rep.from, from);
1260 mathed_parse_cell(rep.to, to);
1261 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1262 par()->replace(rep);
1266 string name = s.substr(1);
1268 if (name == "over" || name == "choose" || name == "atop") {
1269 MathArray ar = array();
1270 MathAtom t(createMathInset(name));
1271 t->asNestInset()->cell(0).swap(array());
1279 // prevent entering of recursive macros
1280 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1281 && formula()->getInsetName() == name)
1283 lyxerr << "can't enter recursive macro\n";
1287 niceInsert(createMathInset(name));
1292 bool MathCursor::script(bool up)
1294 // Hack to get \\^ and \\_ working
1295 if (inMacroMode() && macroName() == "\\") {
1297 interpret("\\mathcircumflex");
1304 MathGridInset safe = grabAndEraseSelection();
1305 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1306 prevAtom()->asScriptInset()->ensure(up);
1307 pushRight(prevAtom());
1310 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1311 nextAtom()->asScriptInset()->ensure(up);
1312 pushLeft(nextAtom());
1316 plainInsert(MathAtom(new MathScriptInset(up)));
1317 prevAtom()->asScriptInset()->ensure(up);
1318 pushRight(prevAtom());
1328 bool MathCursor::interpret(char c)
1330 //lyxerr << "interpret 2: '" << c << "'\n";
1331 targetx_ = -1; // "no target"
1332 if (inMacroArgMode()) {
1336 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1337 if (p && 1 <= n && n <= p->numargs())
1338 insert(MathAtom(new MathMacroArgument(c - '0')));
1340 insert(createMathInset("#"));
1341 interpret(c); // try again
1347 if (inMacroMode()) {
1348 string name = macroName();
1349 //lyxerr << "interpret name: '" << name << "'\n";
1351 if (name.empty() && c == '\\') {
1353 interpret("\\backslash");
1358 inMacroMode()->name() += c;
1362 // handle 'special char' macros
1367 interpret("\\backslash");
1369 interpret(string("\\") + c);
1373 // leave macro mode and try again if necessary
1380 // This is annoying as one has to press <space> far too often.
1384 // leave autocorrect mode if necessary
1385 if (autocorrect_ && c == ' ') {
1386 autocorrect_ = false;
1391 // just clear selection on pressing the space bar
1392 if (selection_ && c == ' ') {
1400 //lyxerr << "starting with macro\n";
1401 insert(MathAtom(new MathUnknownInset("\\", false)));
1406 if (currentMode() == MathInset::TEXT_MODE) {
1407 // insert spaces in text mode,
1408 // but suppress direct insertion of two spaces in a row
1409 // the still allows typing '<space>a<space>' and deleting the 'a', but
1410 // it is better than nothing...
1411 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1415 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1416 prevAtom()->asSpaceInset()->incSpace();
1421 // if are at the very end, leave the formula
1422 return pos() != size();
1426 insert(c); // LM_TC_TEX;
1431 if (c == '{' || c == '}', c)) {
1432 insert(c); // LM_TC_TEX;
1438 niceInsert(MathAtom(new MathBraceInset));
1447 insert(createMathInset("$"));
1452 insert(createMathInset("%"));
1457 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1458 insert(c, LM_TC_VAR);
1462 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1463 insert(c, LM_TC_VAR);
1464 lastcode_ = LM_TC_VAR;
1469 insert(c, LM_TC_TEX);
1470 //bv->owner()->message(_("TeX mode"));
1475 // try auto-correction
1476 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1479 // no special circumstances, so insert the character without any fuss
1481 autocorrect_ = true;
1486 void MathCursor::setSelection(MathIterator const & where, size_type n)
1495 void MathCursor::insetToggle()
1497 if (hasNextAtom()) {
1498 // toggle next inset ...
1499 nextAtom()->lock(!nextAtom()->lock());
1500 } else if (popLeft() && hasNextAtom()) {
1501 // ... or enclosing inset if we are in the last inset position
1502 nextAtom()->lock(!nextAtom()->lock());
1508 string MathCursor::info() const
1511 os << "Math editor mode. ";
1512 for (int i = 0, n = depth(); i < n; ++i) {
1513 Cursor_[i].par_->infoize(os);
1517 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1518 prevAtom()->infoize(os);
1520 return os.str().c_str(); // .c_str() needed for lyxstring
1524 unsigned MathCursor::depth() const
1526 return Cursor_.size();
1534 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1535 MathInset::row_type & r1, MathInset::row_type & r2,
1536 MathInset::col_type & c1, MathInset::col_type & c2)
1538 MathInset * p = i1.par_;
1539 c1 = p->col(i1.idx_);
1540 c2 = p->col(i2.idx_);
1543 r1 = p->row(i1.idx_);
1544 r2 = p->row(i2.idx_);
1552 MathGridInset MathCursor::grabSelection() const
1555 return MathGridInset();
1558 getSelection(i1, i2);
1559 // shouldn't we assert on i1.par_ == i2.par_?
1560 if (i1.idx_ == i2.idx_) {
1561 MathGridInset data(1, 1);
1562 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1567 region(i1, i2, r1, r2, c1, c2);
1568 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1569 for (row_type row = 0; row < data.nrows(); ++row)
1570 for (col_type col = 0; col < data.ncols(); ++col) {
1571 idx_type i = i1.par_->index(row + r1, col + c1);
1572 data.cell(data.index(row, col)) = i1.par_->cell(i);
1578 void MathCursor::eraseSelection()
1582 getSelection(i1, i2);
1583 if (i1.idx_ == i2.idx_)
1584 i1.cell().erase(i1.pos_, i2.pos_);
1586 MathInset * p = i1.par_;
1589 region(i1, i2, r1, r2, c1, c2);
1590 for (row_type row = r1; row <= r2; ++row)
1591 for (col_type col = c1; col <= c2; ++col)
1592 p->cell(p->index(row, col)).erase();
1598 MathGridInset MathCursor::grabAndEraseSelection()
1601 return MathGridInset();
1602 MathGridInset res = grabSelection();
1609 MathCursorPos MathCursor::normalAnchor() const
1611 if (Anchor_.size() < depth()) {
1613 lyxerr << "unusual Anchor size\n";
1615 //lyx::Assert(Anchor_.size() >= cursor.depth());
1616 // use Anchor on the same level as Cursor
1617 MathCursorPos normal = Anchor_[depth() - 1];
1618 if (depth() < Anchor_.size() && !(normal < cursor())) {
1619 // anchor is behind cursor -> move anchor behind the inset
1627 void MathCursor::handleExtern(const string & arg)
1631 istringstream iss(arg.c_str());
1632 iss >> lang >> extra;
1640 lyxerr << "use selection: " << ar << "\n";
1641 insert(pipeThroughExtern(lang, extra, ar));
1646 eq.push_back(MathAtom(new MathCharInset('=')));
1648 popToEnclosingHull();
1651 MathHullInset * hull = enclosingHull(idx);
1655 if (hull->getType() == "simple") {
1656 MathArray::size_type pos = cursor().cell().find_last(eq);
1658 if (pos == size()) {
1660 lyxerr << "use whole cell: " << ar << "\n";
1662 ar = MathArray(array(), pos + 1, size());
1663 lyxerr << "use partial cell form pos: " << pos << "\n";
1667 insert(pipeThroughExtern(lang, extra, ar));
1671 if (hull->getType() == "equation") {
1672 lyxerr << "use equation inset\n";
1673 hull->mutate("eqnarray");
1674 MathArray & ar = cursor().cell();
1675 lyxerr << "use cell: " << ar << "\n";
1677 cursor().cell() = eq;
1679 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1685 lyxerr << "use eqnarray\n";
1687 MathArray ar = cursor().cell();
1688 lyxerr << "use cell: " << ar << "\n";
1691 cursor().cell() = eq;
1693 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1700 int MathCursor::dispatch(string const & cmd)
1702 // try to dispatch to adajcent items if they are not editable
1703 // actually, this should only happen for mouse clicks...
1704 if (hasNextAtom() && !openable(nextAtom(), false))
1705 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1707 if (hasPrevAtom() && !openable(prevAtom(), false))
1708 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1711 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1712 MathCursorPos & pos = Cursor_[i];
1713 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))
1720 MathInset::mode_type MathCursor::currentMode() const
1722 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1723 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1724 if (res != MathInset::UNDECIDED_MODE)
1727 return MathInset::UNDECIDED_MODE;
1731 void releaseMathCursor(BufferView * bv)
1735 mathcursor->formula()->hideInsetCursor(bv);