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() << "'\n";
222 for (unsigned int i = 0; i < Cursor_.size(); ++i)
223 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
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 return t->nargs() && t->covers(x, y);
291 bool MathCursor::posLeft()
300 bool MathCursor::posRight()
309 bool MathCursor::left(bool sel)
314 lastcode_ = LM_TC_VAR;
318 lastcode_ = LM_TC_VAR;
320 if (hasPrevAtom() && openable(prevAtom(), sel)) {
321 if (prevAtom()->isHyperActive()) {
322 lyxerr << "entering hyperactive inset\n";
324 pushRight(prevAtom());
328 return posLeft() || idxLeft() || popLeft() || selection_;
332 bool MathCursor::right(bool sel)
337 lastcode_ = LM_TC_VAR;
341 lastcode_ = LM_TC_VAR;
343 if (hasNextAtom() && openable(nextAtom(), sel)) {
344 if (nextAtom()->isHyperActive()) {
345 lyxerr << "entering hyperactive inset\n";
348 nextAtom()->edit(formula()->view(), x, y, 0);
350 pushLeft(nextAtom());
354 return posRight() || idxRight() || popRight() || selection_;
358 void MathCursor::first()
361 pushLeft(formula_->par());
365 void MathCursor::last()
372 void MathCursor::setPos(int x, int y)
375 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
378 lastcode_ = LM_TC_VAR;
381 cursor().par_ = &formula_->par();
386 //lyxerr << "found idx: " << idx() << " cursor: " << pos() << "\n";
387 int distmin = 1 << 30; // large enough
388 for (unsigned int i = 0; i < par()->nargs(); ++i) {
389 MathXArray const & ar = par()->xcell(i);
390 int x1 = x - ar.xo();
391 int y1 = y - ar.yo();
392 MathXArray::size_type c = ar.x2pos(x1);
393 int xx = abs(x1 - ar.pos2x(c));
395 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
396 // << " c: " << c << " xo: " << ar.xo() << "\n";
397 if (yy + xx <= distmin) {
403 //lyxerr << "found idx: " << idx() << " cursor: "
405 if (hasNextAtom() && positionable(nextAtom(), x, y))
406 pushLeft(nextAtom());
407 else if (hasPrevAtom() && positionable(prevAtom(), x, y))
408 pushRight(prevAtom());
416 void MathCursor::home(bool sel)
421 lastcode_ = LM_TC_VAR;
422 if (!par()->idxHome(idx(), pos()))
428 void MathCursor::end(bool sel)
433 lastcode_ = LM_TC_VAR;
434 if (!par()->idxEnd(idx(), pos()))
440 void MathCursor::plainErase()
442 array().erase(pos());
446 void MathCursor::plainInsert(MathAtom const & t)
448 array().insert(pos(), t);
453 void MathCursor::insert(char c, MathTextCodes t)
455 //lyxerr << "inserting '" << c << "'\n";
456 plainInsert(MathAtom(new MathCharInset(c, t)));
460 void MathCursor::insert(MathAtom const & t)
475 void MathCursor::niceInsert(MathAtom const & t)
478 insert(t); // inserting invalidates the pointer!
479 MathAtom const & p = prevAtom();
482 right(); // do not push for e.g. MathSymbolInset
486 #warning "redraw disabled"
488 //p->metrics(p->size());
492 void MathCursor::insert(MathArray const & ar)
498 array().insert(pos(), ar);
503 void MathCursor::paste(MathArray const & ar)
507 array().insert(pos(), ar);
512 void MathCursor::backspace()
524 MathScriptInset * p = prevAtom()->asScriptInset();
526 p->removeScript(p->hasUp());
527 // Don't delete if there is anything left
528 if (p->hasUp() || p->hasDown())
537 void MathCursor::erase()
547 // delete empty cells if necessary
548 if (array().empty()) {
551 par()->idxDelete(idx(), popit, removeit);
552 if (popit && popLeft() && removeit)
560 MathScriptInset * p = nextAtom()->asScriptInset();
562 p->removeScript(p->hasUp());
563 // Don't delete if there is anything left
564 if (p->hasUp() || p->hasDown())
572 void MathCursor::delLine()
581 if (par()->nrows() > 1)
582 par()->delRow(row());
589 bool MathCursor::up(bool sel)
596 MathInset::idx_type i = 0;
597 MathInset::pos_type p = 0;
599 // check whether we could move into the inset
600 if (hasPrevAtom() && prevAtom()->idxLastUp(i, p)) {
601 pushRight(prevAtom());
607 if (hasNextAtom() && nextAtom()->idxFirstUp(i, p)) {
608 pushLeft(nextAtom());
615 return goUp() || selection_;
619 bool MathCursor::down(bool sel)
626 MathInset::idx_type i = 0;
627 MathInset::pos_type p = 0;
629 // check whether we could move into the inset
630 if (hasPrevAtom() && prevAtom()->idxLastDown(i, p)) {
631 pushRight(prevAtom());
637 if (hasNextAtom() && nextAtom()->idxFirstDown(i, p)) {
638 pushLeft(nextAtom());
645 return goDown() || selection_;
649 bool MathCursor::toggleLimits()
653 MathScriptInset * t = prevAtom()->asScriptInset();
656 int old = t->limits();
657 t->limits(old < 0 ? 1 : -1);
658 return old != t->limits();
662 void MathCursor::macroModeClose()
664 string s = macroName();
666 size_type old = pos();
668 array().erase(pos(), old);
674 int MathCursor::macroNamePos() const
676 for (int i = pos() - 1; i >= 0; --i) {
677 MathAtom & p = array().at(i);
678 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
685 string MathCursor::macroName() const
688 for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i)
689 s += array().at(i)->getChar();
694 void MathCursor::selCopy()
698 theSelection.grab(*this);
704 void MathCursor::selCut()
708 theSelection.grab(*this);
709 theSelection.erase(*this);
712 theSelection.clear();
717 void MathCursor::selDel()
721 theSelection.erase(*this);
729 void MathCursor::selPaste()
732 theSelection.paste(*this);
733 theSelection.grab(*this);
738 void MathCursor::selHandle(bool sel)
740 if (sel == selection_)
743 theSelection.clear();
749 void MathCursor::selStart()
752 theSelection.clear();
758 void MathCursor::selClear()
765 void MathCursor::selGet(MathArray & ar)
771 theSelection.grab(*this);
772 ar = theSelection.glue();
777 void MathCursor::drawSelection(Painter & pain) const
784 getSelection(i1, i2);
785 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
787 if (i1.idx_ == i2.idx_) {
788 MathXArray & c = i1.xcell();
789 int x1 = c.xo() + c.pos2x(i1.pos_);
790 int y1 = c.yo() - c.ascent();
791 int x2 = c.xo() + c.pos2x(i2.pos_);
792 int y2 = c.yo() + c.descent();
793 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
795 std::vector<MathInset::idx_type> indices
796 = (*i1.par_)->idxBetween(i1.idx_, i2.idx_);
797 for (unsigned i = 0; i < indices.size(); ++i) {
798 MathXArray & c = i1.xcell(indices[i]);
800 int y1 = c.yo() - c.ascent();
801 int x2 = c.xo() + c.width();
802 int y2 = c.yo() + c.descent();
803 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
808 // draw anchor if different from selection boundary
809 MathCursorPos anc = Anchor_.back();
810 if (anc != i1 && anc != i2) {
811 MathXArray & c = anc.xcell();
812 int x = c.xo() + c.pos2x(anc.pos_);
813 int y1 = c.yo() - c.ascent();
814 int y2 = c.yo() + c.descent();
815 pain.line(x, y1, x, y2, LColor::mathline);
821 void MathCursor::handleFont(MathTextCodes t)
827 getSelection(i1, i2);
828 if (i1.idx_ == i2.idx_) {
829 MathArray & ar = i1.cell();
830 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
831 ar.at(pos)->handleFont(t);
834 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
838 void MathCursor::handleDelim(string const & l, string const & r)
840 handleNest(new MathDelimInset(l, r));
844 void MathCursor::handleNest(MathInset * p)
848 p->cell(0) = theSelection.glue();
850 insert(MathAtom(p)); // this invalidates p!
851 pushRight(prevAtom());
855 void MathCursor::getPos(int & x, int & y)
858 #warning This should probably take cellXOffset and cellYOffset into account
860 x = xarray().xo() + xarray().pos2x(pos());
865 MathAtom & MathCursor::par() const
867 return *cursor().par_;
871 InsetFormulaBase const * MathCursor::formula()
877 MathCursor::idx_type MathCursor::idx() const
879 return cursor().idx_;
883 MathCursor::idx_type & MathCursor::idx()
885 return cursor().idx_;
889 MathCursor::pos_type MathCursor::pos() const
891 return cursor().pos_;
895 MathCursor::pos_type & MathCursor::pos()
897 return cursor().pos_;
901 bool MathCursor::inMacroMode() const
903 return macroNamePos() != -1;
907 bool MathCursor::selection() const
913 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
915 for (int i = Cursor_.size() - 1; i >= 0; --i) {
916 MathGridInset * p = (*Cursor_[i].par_)->asGridInset();
918 idx = Cursor_[i].idx_;
926 void MathCursor::pullArg(bool goright)
929 MathArray a = array();
931 MathScriptInset const * p = par()->asScriptInset();
933 // special handling for scripts
934 const bool up = p->hasUp();
936 MathScriptInset * q = nextAtom()->asScriptInset();
940 array().insert(pos(), a);
946 array().insert(pos(), a);
953 void MathCursor::normalize() const
956 #warning This is evil!
958 MathCursor * it = const_cast<MathCursor *>(this);
960 if (idx() >= par()->nargs()) {
961 lyxerr << "this should not really happen - 1: "
962 << idx() << " " << par()->nargs() << "\n";
965 it->idx() = min(idx(), par()->nargs() - 1);
967 if (pos() > size()) {
968 lyxerr << "this should not really happen - 2: "
969 << pos() << " " << size() << " in idx: " << it->idx()
971 WriteStream wi(0, lyxerr, false);
972 it->par()->write(wi);
976 it->pos() = min(pos(), size());
980 MathCursor::size_type MathCursor::size() const
982 return array().size();
986 MathCursor::col_type MathCursor::col() const
988 return par()->col(idx());
992 MathCursor::row_type MathCursor::row() const
994 return par()->row(idx());
998 bool MathCursor::hasPrevAtom() const
1004 bool MathCursor::hasNextAtom() const
1006 return pos() < size();
1010 MathAtom const & MathCursor::prevAtom() const
1012 lyx::Assert(pos() > 0);
1013 return array().at(pos() - 1);
1017 MathAtom & MathCursor::prevAtom()
1019 lyx::Assert(pos() > 0);
1020 return array().at(pos() - 1);
1024 MathAtom const & MathCursor::nextAtom() const
1026 lyx::Assert(pos() < size());
1027 return array().at(pos());
1031 MathAtom & MathCursor::nextAtom()
1033 lyx::Assert(pos() < size());
1034 return array().at(pos());
1038 MathArray & MathCursor::array() const
1040 static MathArray dummy;
1042 if (idx() >= par()->nargs()) {
1043 lyxerr << "############ idx_ " << idx() << " not valid\n";
1047 return cursor().cell();
1051 MathXArray & MathCursor::xarray() const
1053 return cursor().xcell();
1057 void MathCursor::idxNext()
1059 par()->idxNext(idx(), pos());
1063 void MathCursor::idxPrev()
1065 par()->idxPrev(idx(), pos());
1069 void MathCursor::splitCell()
1071 if (idx() == par()->nargs() - 1)
1073 MathArray ar = array();
1075 array().erase(pos(), size());
1078 array().insert(0, ar);
1082 void MathCursor::breakLine()
1084 // leave inner cells
1088 MathHullInset * p = formula()->par()->asHullInset();
1092 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1093 p->mutate(LM_OT_EQNARRAY);
1100 const row_type r = row();
1101 for (col_type c = col() + 1; c < p->ncols(); ++c)
1102 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1106 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1111 void MathCursor::readLine(MathArray & ar) const
1113 idx_type base = row() * par()->ncols();
1114 for (idx_type off = 0; off < par()->ncols(); ++off)
1115 ar.push_back(par()->cell(base + off));
1119 char MathCursor::valign() const
1122 MathGridInset * p = enclosingGrid(idx);
1123 return p ? p->valign() : '\0';
1127 char MathCursor::halign() const
1130 MathGridInset * p = enclosingGrid(idx);
1131 return p ? p->halign(idx % p->ncols()) : '\0';
1135 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1137 MathCursorPos anc = normalAnchor();
1138 if (anc < cursor()) {
1148 MathCursorPos & MathCursor::cursor()
1150 return Cursor_.back();
1154 MathCursorPos const & MathCursor::cursor() const
1156 return Cursor_.back();
1160 bool MathCursor::goUp()
1162 // first ask the inset if it knows better then we
1163 if (par()->idxUp(idx(), pos()))
1166 // leave subscript to the nearest side
1167 MathScriptInset * p = par()->asScriptInset();
1168 if (p && idx() == 0) {
1169 if (pos() <= size() / 2)
1176 // if not, apply brute force.
1180 std::vector<MathCursorPos> save = Cursor_;
1181 y0 -= xarray().ascent();
1182 for (int y = y0 - 4; y > formula()->upperY(); y -= 4) {
1184 if (save != Cursor_ && xarray().yo() < y0)
1192 bool MathCursor::goDown()
1194 // first ask the inset if it knows better then we
1195 if (par()->idxDown(idx(), pos()))
1198 // leave superscript to the nearest side
1199 MathScriptInset * p = par()->asScriptInset();
1200 if (p && idx() == 1) {
1201 if (pos() <= size() / 2)
1208 // does the inset know
1210 // if not, apply brute force.
1214 std::vector<MathCursorPos> save = Cursor_;
1215 y0 += xarray().descent();
1216 for (int y = y0 + 4; y < formula()->lowerY(); y += 4) {
1218 if (save != Cursor_ && xarray().yo() > y0)
1226 bool MathCursor::idxLeft()
1228 return par()->idxLeft(idx(), pos());
1232 bool MathCursor::idxRight()
1234 return par()->idxRight(idx(), pos());
1238 bool MathCursor::interpret(string const & s)
1240 //lyxerr << "interpret 1: '" << s << "'\n";
1245 return interpret(s[0]);
1247 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1248 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1249 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1251 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1253 istringstream is(s.substr(5).c_str());
1255 n = std::max(1u, n);
1256 niceInsert(MathAtom(new MathCasesInset(n)));
1260 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1265 istringstream is(s.substr(6).c_str());
1266 is >> m >> n >> v_align >> h_align;
1267 m = std::max(1u, m);
1268 n = std::max(1u, n);
1270 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1274 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1276 istringstream is(s.substr(7).c_str());
1279 mathed_parse_cell(rep.from, from);
1280 mathed_parse_cell(rep.to, to);
1281 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1282 par()->replace(rep);
1286 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1287 MathArray ar = array();
1288 MathAtom t = createMathInset(s.substr(1));
1289 t->asNestInset()->cell(0).swap(array());
1297 niceInsert(createMathInset(s.substr(1)));
1302 bool MathCursor::interpret(char c)
1304 //lyxerr << "interpret 2: '" << c << "'\n";
1305 if (c == '^' || c == '_') {
1307 const bool up = (c == '^');
1309 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1310 prevAtom()->asScriptInset()->ensure(up);
1311 pushRight(prevAtom());
1314 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1315 nextAtom()->asScriptInset()->ensure(up);
1316 pushLeft(nextAtom());
1320 plainInsert(MathAtom(new MathScriptInset(up)));
1321 prevAtom()->asScriptInset()->ensure(up);
1322 pushRight(prevAtom());
1333 if (inMacroMode()) {
1334 string name = macroName();
1336 if (name == "\\" && c == '#') {
1337 insert(c, LM_TC_TEX);
1341 if (name == "\\" && c == '\\') {
1343 interpret("\\backslash");
1347 if (name == "\\#" && '1' <= c && c <= '9') {
1348 insert(c, LM_TC_TEX);
1354 insert(c, LM_TC_TEX);
1359 insert(c, LM_TC_TEX);
1371 if (lastcode_ == LM_TC_TEXTRM) {
1372 // suppress direct insertion of to spaces in a row
1373 // the still allows typing '<space>a<space>' and deleting the 'a', but
1374 // it is better than nothing
1375 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1377 insert(c, LM_TC_TEXTRM);
1382 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1383 prevAtom()->asSpaceInset()->incSpace();
1387 if (mathcursor->popRight())
1390 // if are at the very end, leave the formula
1391 return pos() != size();
1395 if (strchr("{}", c)) {
1396 insert(c, LM_TC_TEX);
1402 niceInsert(MathAtom(new MathBraceInset));
1410 if (strchr("#$%", c)) {
1411 insert(MathAtom(new MathSpecialCharInset(c)));
1412 lastcode_ = LM_TC_VAR;
1416 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1417 insert(c, LM_TC_VAR);
1421 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1422 insert(c, LM_TC_VAR);
1423 lastcode_ = LM_TC_VAR;
1428 insert(c, LM_TC_TEX);
1429 //bv->owner()->message(_("TeX mode"));
1433 // no special circumstances, so insert the character without any fuss
1434 insert(c, LM_TC_MIN);
1440 ////////////////////////////////////////////////////////////////////////
1443 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1445 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1449 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1451 if (ti.par_ != it.par_) {
1452 lyxerr << "can't compare cursor and anchor in different insets\n";
1455 if (ti.idx_ != it.idx_)
1456 return ti.idx_ < it.idx_;
1457 return ti.pos_ < it.pos_;
1461 MathArray & MathCursorPos::cell(MathCursor::idx_type idx) const
1463 return (*par_)->cell(idx);
1467 MathArray & MathCursorPos::cell() const
1469 return (*par_)->cell(idx_);
1473 MathXArray & MathCursorPos::xcell(MathCursor::idx_type idx) const
1475 return (*par_)->xcell(idx);
1479 MathXArray & MathCursorPos::xcell() const
1481 return (*par_)->xcell(idx_);
1485 MathCursorPos MathCursor::normalAnchor() const
1487 // use Anchor on the same level as Cursor
1488 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1489 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1490 // anchor is behind cursor -> move anchor behind the inset
1497 void MathCursor::stripFromLastEqualSign()
1499 // find position of last '=' in the array
1500 MathArray & ar = cursor().cell();
1501 MathArray::const_iterator et = ar.end();
1502 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1503 if ((*it)->getChar() == '=')
1506 // delete everything behind this position
1507 ar.erase(et - ar.begin(), ar.size());