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.
19 #pragma implementation
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
31 #include "math_support.h"
32 #include "formulabase.h"
33 #include "math_cursor.h"
34 #include "math_casesinset.h"
35 #include "math_factory.h"
36 #include "math_arrayinset.h"
37 #include "math_braceinset.h"
38 #include "math_charinset.h"
39 #include "math_deliminset.h"
40 #include "math_hullinset.h"
41 #include "math_scriptinset.h"
42 #include "math_spaceinset.h"
43 #include "math_specialcharinset.h"
44 #include "math_mathmlstream.h"
45 #include "math_replace.h"
46 #include "math_parser.h"
60 void grab(MathCursor const & cursor)
65 cursor.getSelection(i1, i2);
66 if (i1.idx_ == i2.idx_)
67 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
69 std::vector<MathInset::idx_type> indices =
70 (*i1.par_)->idxBetween(i1.idx_, i2.idx_);
71 for (MathInset::idx_type i = 0; i < indices.size(); ++i)
72 data_.push_back(i1.cell(indices[i]));
76 void erase(MathCursor & cursor)
80 cursor.getSelection(i1, i2);
81 if (i1.idx_ == i2.idx_)
82 i1.cell().erase(i1.pos_, i2.pos_);
84 std::vector<MathInset::idx_type> indices =
85 (*i1.par_)->idxBetween(i1.idx_, i2.idx_);
86 for (unsigned i = 0; i < indices.size(); ++i)
87 i1.cell(indices[i]).erase();
92 void paste(MathCursor & cursor) const
94 MathArray ar = glue();
98 // glues selection to one cell
99 MathArray glue() const
102 for (unsigned i = 0; i < data_.size(); ++i)
103 ar.push_back(data_[i]);
112 std::vector<MathArray> data_;
116 Selection theSelection;
120 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
122 os << "(par: " << p.par_ << " idx: " << p.idx_ << " pos: " << p.pos_ << ")";
130 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
131 : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
133 left ? first() : last();
137 void MathCursor::push(MathAtom & t)
143 Cursor_.push_back(p);
147 void MathCursor::pushLeft(MathAtom & t)
149 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
151 t->idxFirst(idx(), pos());
155 void MathCursor::pushRight(MathAtom & t)
157 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
160 t->idxLast(idx(), pos());
164 bool MathCursor::popLeft()
166 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
167 if (Cursor_.size() <= 1)
170 // nextInset()->removeEmptyScripts();
173 // nextAtom()->removeEmptyScripts();
178 bool MathCursor::popRight()
180 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
181 if (Cursor_.size() <= 1)
184 // nextInset()->removeEmptyScripts();
187 // nextInset()->removeEmptyScripts();
195 void MathCursor::dump(char const * what) const
197 lyxerr << "MC: " << what << "\n";
198 for (unsigned i = 0; i < Cursor_.size(); ++i)
199 lyxerr << " i: " << i
200 << " Cursor: pos: " << Cursor_[i].pos_
201 << " idx: " << Cursor_[i].idx_
202 << " par: " << Cursor_[i].par_ << "\n";
204 for (unsigned i = 0; i < Anchor_.size(); ++i)
205 lyxerr << " i: " << i
206 << " Anchor: pos: " << Anchor_[i].pos_
207 << " idx: " << Anchor_[i].idx_
208 << " par: " << Anchor_[i].par_ << "\n";
210 lyxerr << " sel: " << selection_ << "\n";
214 void MathCursor::seldump(char const * str) const
216 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
219 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
220 << str << "\ntheSelection: " << selection_
221 << " '" /*<< theSelection.glue()*/ << "'\nCursor:";
222 for (unsigned int i = 0; i < Cursor_.size(); ++i)
223 lyxerr << Cursor_[i].par_ << "\n'" /*<< Cursor_[i].cell()*/ << "'\n";
224 lyxerr << "\nAnchor: ";
225 for (unsigned int i = 0; i < Anchor_.size(); ++i)
226 lyxerr << Anchor_[i].par_ << "\n'" /*<< Anchor_[i].cell()*/ << "'\n";
227 //lyxerr << "\ncursor.pos_: " << pos();
228 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
229 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
234 void MathCursor::seldump(char const *) const {}
235 void MathCursor::dump(char const *) const {}
240 UpdatableInset * MathCursor::asHyperActiveInset() const
242 return par()->asHyperActiveInset();
246 bool MathCursor::isInside(MathInset const * p) const
248 for (unsigned i = 0; i < Cursor_.size(); ++i)
249 if (Cursor_[i].par_->nucleus() == p)
255 bool MathCursor::openable(MathAtom const & t, bool sel) const
257 if (t->isHyperActive())
263 if (t->asScriptInset())
267 // we can't move into anything new during selection
268 if (Cursor_.size() == Anchor_.size())
270 if (&t != Anchor_[Cursor_.size()].par_)
277 bool MathCursor::positionable(MathAtom const & t, int x, int y) const
280 // we can't move into anything new during selection
281 if (Cursor_.size() >= Anchor_.size())
283 if (&t != Anchor_[Cursor_.size()].par_)
287 //lyxerr << " positionable: 1 " << t->nargs() << "\n";
288 //lyxerr << " positionable: 2 " << t->covers(x, y) << "\n";
289 return t->nargs() && t->covers(x, y);
293 bool MathCursor::posLeft()
302 bool MathCursor::posRight()
311 bool MathCursor::left(bool sel)
316 lastcode_ = LM_TC_VAR;
320 lastcode_ = LM_TC_VAR;
322 if (hasPrevAtom() && openable(prevAtom(), sel)) {
323 if (prevAtom()->isHyperActive()) {
324 lyxerr << "entering hyperactive inset\n";
326 pushRight(prevAtom());
330 return posLeft() || idxLeft() || popLeft() || selection_;
334 bool MathCursor::right(bool sel)
339 lastcode_ = LM_TC_VAR;
343 lastcode_ = LM_TC_VAR;
345 if (hasNextAtom() && openable(nextAtom(), sel)) {
346 if (nextAtom()->isHyperActive()) {
347 lyxerr << "entering hyperactive inset\n";
350 nextAtom()->edit(formula()->view(), x, y, 0);
352 pushLeft(nextAtom());
356 return posRight() || idxRight() || popRight() || selection_;
360 void MathCursor::first()
363 pushLeft(formula_->par());
367 void MathCursor::last()
374 void MathCursor::setPos(int x, int y)
377 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
380 lastcode_ = LM_TC_VAR;
383 cursor().par_ = &formula_->par();
388 //lyxerr << "found idx: " << idx() << " cursor: " << pos() << "\n";
389 int distmin = 1 << 30; // large enough
390 for (unsigned int i = 0; i < par()->nargs(); ++i) {
391 MathXArray const & ar = par()->xcell(i);
392 int d = ar.dist(x, y);
396 pos() = ar.x2pos(x - ar.xo());
399 //lyxerr << "found idx: " << idx() << " cursor: " << pos() << "\n";
400 if (hasNextAtom() && positionable(nextAtom(), x, y))
401 pushLeft(nextAtom());
402 else if (hasPrevAtom() && positionable(prevAtom(), x, y))
403 pushRight(prevAtom());
411 void MathCursor::home(bool sel)
416 lastcode_ = LM_TC_VAR;
417 if (!par()->idxHome(idx(), pos()))
423 void MathCursor::end(bool sel)
428 lastcode_ = LM_TC_VAR;
429 if (!par()->idxEnd(idx(), pos()))
435 void MathCursor::plainErase()
437 array().erase(pos());
441 void MathCursor::plainInsert(MathAtom const & t)
443 array().insert(pos(), t);
448 void MathCursor::insert(char c, MathTextCodes t)
450 //lyxerr << "inserting '" << c << "'\n";
451 plainInsert(MathAtom(new MathCharInset(c, t)));
455 void MathCursor::insert(MathAtom const & t)
470 void MathCursor::niceInsert(MathAtom const & t)
473 insert(t); // inserting invalidates the pointer!
474 MathAtom const & p = prevAtom();
477 right(); // do not push for e.g. MathSymbolInset
483 void MathCursor::insert(MathArray const & ar)
489 array().insert(pos(), ar);
494 void MathCursor::paste(MathArray const & ar)
498 array().insert(pos(), ar);
503 void MathCursor::backspace()
515 MathScriptInset * p = prevAtom()->asScriptInset();
517 p->removeScript(p->hasUp());
518 // Don't delete if there is anything left
519 if (p->hasUp() || p->hasDown())
528 void MathCursor::erase()
538 // delete empty cells if necessary
539 if (array().empty()) {
542 par()->idxDelete(idx(), popit, removeit);
543 if (popit && popLeft() && removeit)
551 MathScriptInset * p = nextAtom()->asScriptInset();
553 p->removeScript(p->hasUp());
554 // Don't delete if there is anything left
555 if (p->hasUp() || p->hasDown())
563 void MathCursor::delLine()
572 if (par()->nrows() > 1)
573 par()->delRow(row());
575 if (idx() > par()->nargs())
576 idx() = par()->nargs();
583 bool MathCursor::up(bool sel)
590 MathInset::idx_type i = 0;
591 MathInset::pos_type p = 0;
593 // check whether we could move into the inset
594 if (hasPrevAtom() && prevAtom()->idxLastUp(i, p)) {
595 pushRight(prevAtom());
601 if (hasNextAtom() && nextAtom()->idxFirstUp(i, p)) {
602 pushLeft(nextAtom());
609 return goUp() || selection_;
613 bool MathCursor::down(bool sel)
620 MathInset::idx_type i = 0;
621 MathInset::pos_type p = 0;
623 // check whether we could move into the inset
624 if (hasPrevAtom() && prevAtom()->idxLastDown(i, p)) {
625 pushRight(prevAtom());
631 if (hasNextAtom() && nextAtom()->idxFirstDown(i, p)) {
632 pushLeft(nextAtom());
639 return goDown() || selection_;
643 bool MathCursor::toggleLimits()
647 MathScriptInset * t = nextAtom()->asScriptInset();
650 int old = t->limits();
651 t->limits(old < 0 ? 1 : -1);
652 return old != t->limits();
656 void MathCursor::macroModeClose()
658 string s = macroName();
660 size_type old = pos();
662 array().erase(pos(), old);
668 int MathCursor::macroNamePos() const
670 for (int i = pos() - 1; i >= 0; --i) {
671 MathAtom & p = array().at(i);
672 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
679 string MathCursor::macroName() const
682 for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i)
683 s += array().at(i)->getChar();
688 void MathCursor::selCopy()
692 theSelection.grab(*this);
698 void MathCursor::selCut()
702 theSelection.grab(*this);
703 theSelection.erase(*this);
706 theSelection.clear();
711 void MathCursor::selDel()
715 theSelection.erase(*this);
723 void MathCursor::selPaste()
726 theSelection.paste(*this);
727 //theSelection.grab(*this);
732 void MathCursor::selHandle(bool sel)
734 if (sel == selection_)
736 //theSelection.clear();
742 void MathCursor::selStart()
745 //theSelection.clear();
751 void MathCursor::selClear()
758 void MathCursor::selGet(MathArray & ar)
764 theSelection.grab(*this);
765 ar = theSelection.glue();
770 void MathCursor::drawSelection(Painter & pain) const
777 getSelection(i1, i2);
778 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
780 if (i1.idx_ == i2.idx_) {
781 MathXArray & c = i1.xcell();
782 int x1 = c.xo() + c.pos2x(i1.pos_);
783 int y1 = c.yo() - c.ascent();
784 int x2 = c.xo() + c.pos2x(i2.pos_);
785 int y2 = c.yo() + c.descent();
786 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
788 std::vector<MathInset::idx_type> indices
789 = (*i1.par_)->idxBetween(i1.idx_, i2.idx_);
790 for (unsigned i = 0; i < indices.size(); ++i) {
791 MathXArray & c = i1.xcell(indices[i]);
793 int y1 = c.yo() - c.ascent();
794 int x2 = c.xo() + c.width();
795 int y2 = c.yo() + c.descent();
796 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
801 // draw anchor if different from selection boundary
802 MathCursorPos anc = Anchor_.back();
803 if (anc != i1 && anc != i2) {
804 MathXArray & c = anc.xcell();
805 int x = c.xo() + c.pos2x(anc.pos_);
806 int y1 = c.yo() - c.ascent();
807 int y2 = c.yo() + c.descent();
808 pain.line(x, y1, x, y2, LColor::mathline);
814 void MathCursor::handleFont(MathTextCodes t)
820 getSelection(i1, i2);
821 if (i1.idx_ == i2.idx_) {
822 MathArray & ar = i1.cell();
823 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
824 ar.at(pos)->handleFont(t);
827 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
831 void MathCursor::handleDelim(string const & l, string const & r)
833 handleNest(new MathDelimInset(l, r));
837 void MathCursor::handleNest(MathInset * p)
841 p->cell(0) = theSelection.glue();
843 insert(MathAtom(p)); // this invalidates p!
844 pushRight(prevAtom());
848 void MathCursor::getPos(int & x, int & y)
851 #warning This should probably take cellXOffset and cellYOffset into account
853 x = xarray().xo() + xarray().pos2x(pos());
858 MathAtom & MathCursor::par() const
860 return *cursor().par_;
864 InsetFormulaBase const * MathCursor::formula()
870 MathCursor::idx_type MathCursor::idx() const
872 return cursor().idx_;
876 MathCursor::idx_type & MathCursor::idx()
878 return cursor().idx_;
882 MathCursor::pos_type MathCursor::pos() const
884 return cursor().pos_;
888 MathCursor::pos_type & MathCursor::pos()
890 return cursor().pos_;
894 bool MathCursor::inMacroMode() const
896 return macroNamePos() != -1;
900 bool MathCursor::selection() const
906 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
908 for (int i = Cursor_.size() - 1; i >= 0; --i) {
909 MathGridInset * p = (*Cursor_[i].par_)->asGridInset();
911 idx = Cursor_[i].idx_;
919 void MathCursor::pullArg(bool goright)
922 MathArray a = array();
924 MathScriptInset const * p = par()->asScriptInset();
926 // special handling for scripts
927 const bool up = p->hasUp();
929 MathScriptInset * q = nextAtom()->asScriptInset();
933 array().insert(pos(), a);
939 array().insert(pos(), a);
946 void MathCursor::normalize() const
949 #warning This is evil!
951 MathCursor * it = const_cast<MathCursor *>(this);
953 if (idx() >= par()->nargs()) {
954 lyxerr << "this should not really happen - 1: "
955 << idx() << " " << par()->nargs() << "\n";
958 it->idx() = min(idx(), par()->nargs() - 1);
960 if (pos() > size()) {
961 lyxerr << "this should not really happen - 2: "
962 << pos() << " " << size() << " in idx: " << it->idx()
964 WriteStream wi(0, lyxerr, false);
965 it->par()->write(wi);
969 it->pos() = min(pos(), size());
973 MathCursor::size_type MathCursor::size() const
975 return array().size();
979 MathCursor::col_type MathCursor::col() const
981 return par()->col(idx());
985 MathCursor::row_type MathCursor::row() const
987 return par()->row(idx());
991 bool MathCursor::hasPrevAtom() const
997 bool MathCursor::hasNextAtom() const
999 return pos() < size();
1003 MathAtom const & MathCursor::prevAtom() const
1005 lyx::Assert(pos() > 0);
1006 return array().at(pos() - 1);
1010 MathAtom & MathCursor::prevAtom()
1012 lyx::Assert(pos() > 0);
1013 return array().at(pos() - 1);
1017 MathAtom const & MathCursor::nextAtom() const
1019 lyx::Assert(pos() < size());
1020 return array().at(pos());
1024 MathAtom & MathCursor::nextAtom()
1026 lyx::Assert(pos() < size());
1027 return array().at(pos());
1031 MathArray & MathCursor::array() const
1033 static MathArray dummy;
1035 if (idx() >= par()->nargs()) {
1036 lyxerr << "############ idx_ " << idx() << " not valid\n";
1040 return cursor().cell();
1044 MathXArray & MathCursor::xarray() const
1046 return cursor().xcell();
1050 void MathCursor::idxNext()
1052 par()->idxNext(idx(), pos());
1056 void MathCursor::idxPrev()
1058 par()->idxPrev(idx(), pos());
1062 void MathCursor::splitCell()
1064 if (idx() + 1 == par()->nargs())
1066 MathArray ar = array();
1068 array().erase(pos(), size());
1071 array().insert(0, ar);
1075 void MathCursor::breakLine()
1077 // leave inner cells
1081 MathHullInset * p = formula()->par()->asHullInset();
1085 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1086 p->mutate(LM_OT_EQNARRAY);
1093 const row_type r = row();
1094 for (col_type c = col() + 1; c < p->ncols(); ++c)
1095 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1099 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1104 void MathCursor::readLine(MathArray & ar) const
1106 idx_type base = row() * par()->ncols();
1107 for (idx_type off = 0; off < par()->ncols(); ++off)
1108 ar.push_back(par()->cell(base + off));
1112 char MathCursor::valign() const
1115 MathGridInset * p = enclosingGrid(idx);
1116 return p ? p->valign() : '\0';
1120 char MathCursor::halign() const
1123 MathGridInset * p = enclosingGrid(idx);
1124 return p ? p->halign(idx % p->ncols()) : '\0';
1128 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1130 MathCursorPos anc = normalAnchor();
1131 if (anc < cursor()) {
1141 MathCursorPos & MathCursor::cursor()
1143 return Cursor_.back();
1147 MathCursorPos const & MathCursor::cursor() const
1149 return Cursor_.back();
1153 bool MathCursor::goUp()
1155 // first ask the inset if it knows better then we
1156 if (par()->idxUp(idx(), pos()))
1159 // leave subscript to the nearest side
1160 MathScriptInset * p = par()->asScriptInset();
1161 if (p && idx() == 0) {
1162 if (pos() <= size() / 2)
1169 // if not, apply brute force.
1171 bruteUpDown(formula()->upperY() + 24, xarray().yo() - 4 - xarray().ascent());
1175 bool MathCursor::goDown()
1177 // first ask the inset if it knows better then we
1178 if (par()->idxDown(idx(), pos()))
1181 // leave superscript to the nearest side
1182 MathScriptInset * p = par()->asScriptInset();
1183 if (p && idx() == 1) {
1184 if (pos() <= size() / 2)
1191 // if not, apply brute force.
1193 bruteUpDown(xarray().yo() + 4 + xarray().descent(), formula()->lowerY());
1197 bool MathCursor::bruteUpDown(int ylow, int yhigh)
1199 //lyxerr << "looking at range: " << ylow << " " << yhigh << "\n";
1203 std::vector<MathCursorPos> save = Cursor_;
1204 std::vector<MathCursorPos> best;
1205 double best_dist = 1e10; // large enough
1207 for (int y = ylow; y < yhigh; y += 4) {
1212 if (save != Cursor_ && y1 > ylow && y1 < yhigh) {
1214 double d = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);
1215 if (d < best_dist) {
1232 bool MathCursor::idxLeft()
1234 return par()->idxLeft(idx(), pos());
1238 bool MathCursor::idxRight()
1240 return par()->idxRight(idx(), pos());
1244 bool MathCursor::interpret(string const & s)
1246 //lyxerr << "interpret 1: '" << s << "'\n";
1251 return interpret(s[0]);
1253 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1254 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1255 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1257 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1259 istringstream is(s.substr(5).c_str());
1261 n = std::max(1u, n);
1262 niceInsert(MathAtom(new MathCasesInset(n)));
1266 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1271 istringstream is(s.substr(6).c_str());
1272 is >> m >> n >> v_align >> h_align;
1273 m = std::max(1u, m);
1274 n = std::max(1u, n);
1276 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1280 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1282 istringstream is(s.substr(7).c_str());
1285 mathed_parse_cell(rep.from, from);
1286 mathed_parse_cell(rep.to, to);
1287 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1288 par()->replace(rep);
1292 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1293 MathArray ar = array();
1294 MathAtom t = createMathInset(s.substr(1));
1295 t->asNestInset()->cell(0).swap(array());
1303 niceInsert(createMathInset(s.substr(1)));
1308 bool MathCursor::interpret(char c)
1310 //lyxerr << "interpret 2: '" << c << "'\n";
1311 if (c == '^' || c == '_') {
1313 const bool up = (c == '^');
1315 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1316 prevAtom()->asScriptInset()->ensure(up);
1317 pushRight(prevAtom());
1320 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1321 nextAtom()->asScriptInset()->ensure(up);
1322 pushLeft(nextAtom());
1326 plainInsert(MathAtom(new MathScriptInset(up)));
1327 prevAtom()->asScriptInset()->ensure(up);
1328 pushRight(prevAtom());
1339 if (inMacroMode()) {
1340 string name = macroName();
1342 if (name == "\\" && c == '#') {
1343 insert(c, LM_TC_TEX);
1347 if (name == "\\" && c == '\\') {
1349 interpret("\\backslash");
1353 if (name == "\\#" && '1' <= c && c <= '9') {
1354 insert(c, LM_TC_TEX);
1360 insert(c, LM_TC_TEX);
1365 insert(c, LM_TC_TEX);
1377 if (lastcode_ == LM_TC_TEXTRM) {
1378 // suppress direct insertion of to spaces in a row
1379 // the still allows typing '<space>a<space>' and deleting the 'a', but
1380 // it is better than nothing
1381 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1383 insert(c, LM_TC_TEXTRM);
1388 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1389 prevAtom()->asSpaceInset()->incSpace();
1393 if (mathcursor->popRight())
1396 // if are at the very end, leave the formula
1397 return pos() != size();
1401 if (strchr("{}", c)) {
1402 insert(c, LM_TC_TEX);
1408 niceInsert(MathAtom(new MathBraceInset));
1416 if (strchr("#$%", c)) {
1417 insert(MathAtom(new MathSpecialCharInset(c)));
1418 lastcode_ = LM_TC_VAR;
1422 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1423 insert(c, LM_TC_VAR);
1427 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1428 insert(c, LM_TC_VAR);
1429 lastcode_ = LM_TC_VAR;
1434 insert(c, LM_TC_TEX);
1435 //bv->owner()->message(_("TeX mode"));
1439 // no special circumstances, so insert the character without any fuss
1440 insert(c, LM_TC_MIN);
1446 ////////////////////////////////////////////////////////////////////////
1449 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1451 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1455 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1457 if (ti.par_ != it.par_) {
1458 lyxerr << "can't compare cursor and anchor in different insets\n";
1461 if (ti.idx_ != it.idx_)
1462 return ti.idx_ < it.idx_;
1463 return ti.pos_ < it.pos_;
1467 MathArray & MathCursorPos::cell(MathCursor::idx_type idx) const
1469 return (*par_)->cell(idx);
1473 MathArray & MathCursorPos::cell() const
1475 return (*par_)->cell(idx_);
1479 MathXArray & MathCursorPos::xcell(MathCursor::idx_type idx) const
1481 return (*par_)->xcell(idx);
1485 MathXArray & MathCursorPos::xcell() const
1487 return (*par_)->xcell(idx_);
1491 MathCursorPos MathCursor::normalAnchor() const
1493 // use Anchor on the same level as Cursor
1494 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1495 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1496 // anchor is behind cursor -> move anchor behind the inset
1503 void MathCursor::stripFromLastEqualSign()
1505 // find position of last '=' in the array
1506 MathArray & ar = cursor().cell();
1507 MathArray::const_iterator et = ar.end();
1508 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1509 if ((*it)->getChar() == '=')
1512 // delete everything behind this position
1513 ar.erase(et - ar.begin(), ar.size());