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.
21 #pragma implementation
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
29 #include "math_cursor.h"
30 #include "formulabase.h"
31 #include "math_arrayinset.h"
32 #include "math_braceinset.h"
33 #include "math_boxinset.h"
34 #include "math_casesinset.h"
35 #include "math_charinset.h"
36 #include "math_deliminset.h"
37 #include "math_factory.h"
38 #include "math_hullinset.h"
39 #include "math_iterator.h"
40 #include "math_macroarg.h"
41 #include "math_mathmlstream.h"
42 #include "math_parser.h"
43 #include "math_replace.h"
44 #include "math_scriptinset.h"
45 #include "math_spaceinset.h"
46 #include "math_specialcharinset.h"
47 #include "math_support.h"
64 typedef MathInset::col_type col_type;
65 typedef MathInset::row_type row_type;
66 typedef MathInset::idx_type idx_type;
72 void region(MathCursorPos const & i1, MathCursorPos const & i2,
73 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
75 MathInset * p = i1.par_;
86 void grab(MathCursor const & cursor)
90 cursor.getSelection(i1, i2);
91 // shouldn'tt we assert on i1.par_ == i2.par_?
92 if (i1.idx_ == i2.idx_) {
93 data_ = MathGridInset(1, 1);
94 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
98 region(i1, i2, r1, r2, c1, c2);
99 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
100 for (row_type row = 0; row < data_.nrows(); ++row)
101 for (col_type col = 0; col < data_.ncols(); ++col) {
102 idx_type i = i1.par_->index(row + r1, col + c1);
103 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
108 void erase(MathCursor & cursor)
112 cursor.getSelection(i1, i2);
113 if (i1.idx_ == i2.idx_)
114 i1.cell().erase(i1.pos_, i2.pos_);
116 MathInset * p = i1.par_;
119 region(i1, i2, r1, r2, c1, c2);
120 for (row_type row = r1; row <= r2; ++row)
121 for (col_type col = c1; col <= c2; ++col)
122 p->cell(p->index(row, col)).erase();
124 cursor.cursor() = i1;
127 void paste(MathCursor & cursor) const
129 if (data_.nargs() == 1) {
130 // single cell/part of cell
131 cursor.insert(data_.cell(0));
135 MathGridInset * p = cursor.enclosingGrid(idx);
136 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
137 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
138 for (row_type row = 0; row < numrows; ++row)
139 for (col_type col = 0; col < numcols; ++col) {
140 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
141 p->cell(i).push_back(data_.cell(data_.index(row, col)));
146 // glues selection to one cell
147 MathArray glue() const
150 for (unsigned i = 0; i < data_.nargs(); ++i)
151 ar.push_back(data_.cell(i));
157 data_ = MathGridInset(1, 1);
164 Selection theSelection;
171 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
172 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
174 left ? first() : last();
178 void MathCursor::push(MathAtom & t)
180 Cursor_.push_back(MathCursorPos(t.nucleus()));
184 void MathCursor::pushLeft(MathAtom & t)
186 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
188 t->idxFirst(idx(), pos());
192 void MathCursor::pushRight(MathAtom & t)
194 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
197 t->idxLast(idx(), pos());
201 bool MathCursor::popLeft()
203 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
204 if (Cursor_.size() <= 1)
206 if (par()->asScriptInset())
207 par()->asScriptInset()->removeEmptyScripts();
213 bool MathCursor::popRight()
215 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
216 if (Cursor_.size() <= 1)
218 if (par()->asScriptInset())
219 par()->asScriptInset()->removeEmptyScripts();
228 void MathCursor::dump(char const * what) const
230 lyxerr << "MC: " << what << "\n";
231 lyxerr << " Cursor: " << Cursor_.size() << "\n";
232 for (unsigned i = 0; i < Cursor_.size(); ++i)
233 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
234 lyxerr << " Anchor: " << Anchor_.size() << "\n";
235 for (unsigned i = 0; i < Anchor_.size(); ++i)
236 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
237 lyxerr << " sel: " << selection_ << "\n";
240 void MathCursor::dump(char const *) const {}
244 UpdatableInset * MathCursor::asHyperActiveInset() const
246 return par()->asHyperActiveInset();
250 bool MathCursor::isInside(MathInset const * p) const
252 for (unsigned i = 0; i < Cursor_.size(); ++i)
253 if (Cursor_[i].par_ == p)
259 bool MathCursor::openable(MathAtom const & t, bool sel) const
261 if (t->isHyperActive())
267 if (t->asScriptInset())
271 // we can't move into anything new during selection
272 if (Cursor_.size() == Anchor_.size())
274 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
281 bool MathCursor::posLeft()
290 bool MathCursor::posRight()
299 bool MathCursor::left(bool sel)
304 lastcode_ = LM_TC_MIN;
308 lastcode_ = LM_TC_MIN;
310 if (hasPrevAtom() && openable(prevAtom(), sel)) {
311 if (prevAtom()->isHyperActive()) {
312 lyxerr << "entering hyperactive inset\n";
314 pushRight(prevAtom());
318 return posLeft() || idxLeft() || popLeft() || selection_;
322 bool MathCursor::right(bool sel)
327 lastcode_ = LM_TC_MIN;
331 lastcode_ = LM_TC_MIN;
333 if (hasNextAtom() && openable(nextAtom(), sel)) {
334 if (nextAtom()->isHyperActive()) {
335 lyxerr << "entering hyperactive inset\n";
338 nextAtom()->edit(formula()->view(), x, y, 0);
340 pushLeft(nextAtom());
344 return posRight() || idxRight() || popRight() || selection_;
348 void MathCursor::first()
351 pushLeft(formula_->par());
355 void MathCursor::last()
362 bool positionable(MathCursor::cursor_type const & cursor,
363 MathCursor::cursor_type const & anchor)
365 // avoid deeper nested insets when selecting
366 if (cursor.size() > anchor.size())
369 // anchor might be deeper, should have same path then
370 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
371 if (cursor[i].par_ != anchor[i].par_)
374 // position should be ok.
379 void MathCursor::setPos(int x, int y)
382 bool res = bruteFind(x, y,
383 formula()->xlow(), formula()->xhigh(),
384 formula()->ylow(), formula()->yhigh());
386 // this ccan happen on creation of "math-display"
395 void MathCursor::home(bool sel)
400 lastcode_ = LM_TC_MIN;
401 if (!par()->idxHome(idx(), pos()))
407 void MathCursor::end(bool sel)
412 lastcode_ = LM_TC_MIN;
413 if (!par()->idxEnd(idx(), pos()))
419 void MathCursor::plainErase()
421 array().erase(pos());
425 void MathCursor::plainInsert(MathAtom const & t)
427 array().insert(pos(), t);
432 void MathCursor::insert(char c, MathTextCodes t)
434 //lyxerr << "inserting '" << c << "'\n";
435 plainInsert(MathAtom(new MathCharInset(c, t)));
439 void MathCursor::insert(MathAtom const & t)
454 void MathCursor::niceInsert(MathAtom const & t)
457 insert(t); // inserting invalidates the pointer!
458 MathAtom const & p = prevAtom();
461 right(); // do not push for e.g. MathSymbolInset
467 void MathCursor::insert(MathArray const & ar)
473 array().insert(pos(), ar);
478 void MathCursor::paste(MathArray const & ar)
482 array().insert(pos(), ar);
487 void MathCursor::backspace()
499 MathScriptInset * p = prevAtom()->asScriptInset();
501 p->removeScript(p->hasUp());
502 // Don't delete if there is anything left
503 if (p->hasUp() || p->hasDown())
512 void MathCursor::erase()
522 // delete empty cells if necessary
523 if (array().empty()) {
526 par()->idxDelete(idx(), popit, removeit);
527 if (popit && popLeft() && removeit)
535 MathScriptInset * p = nextAtom()->asScriptInset();
537 p->removeScript(p->hasUp());
538 // Don't delete if there is anything left
539 if (p->hasUp() || p->hasDown())
547 void MathCursor::delLine()
556 if (par()->nrows() > 1) {
557 // grid are the only things with more than one row...
558 lyx::Assert(par()->asGridInset());
559 par()->asGridInset()->delRow(hullRow());
562 if (idx() > par()->nargs())
563 idx() = par()->nargs();
570 bool MathCursor::up(bool sel)
575 cursor_type save = Cursor_;
583 bool MathCursor::down(bool sel)
588 cursor_type save = Cursor_;
596 bool MathCursor::toggleLimits()
600 MathScriptInset * t = nextAtom()->asScriptInset();
603 int old = t->limits();
604 t->limits(old < 0 ? 1 : -1);
605 return old != t->limits();
609 void MathCursor::macroModeClose()
611 string s = macroName();
613 size_type old = pos();
615 array().erase(pos(), old);
621 MathInset::difference_type MathCursor::macroNamePos() const
623 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
624 MathAtom & p = array().at(i);
625 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
632 string MathCursor::macroName() const
635 MathInset::difference_type i = macroNamePos();
636 for ( ; i >= 0 && i < int(pos()); ++i)
637 s += array().at(i)->getChar();
642 void MathCursor::selCopy()
646 theSelection.grab(*this);
652 void MathCursor::selCut()
656 theSelection.grab(*this);
657 theSelection.erase(*this);
660 theSelection.clear();
665 void MathCursor::selDel()
669 theSelection.erase(*this);
677 void MathCursor::selPaste()
680 theSelection.paste(*this);
681 //theSelection.grab(*this);
686 void MathCursor::selHandle(bool sel)
688 if (sel == selection_)
690 //theSelection.clear();
696 void MathCursor::selStart()
699 //theSelection.clear();
706 void MathCursor::selClear()
714 void MathCursor::selGet(MathArray & ar)
720 theSelection.grab(*this);
721 ar = theSelection.glue();
726 void MathCursor::drawSelection(Painter & pain) const
733 getSelection(i1, i2);
735 if (i1.idx_ == i2.idx_) {
736 MathXArray & c = i1.xcell();
737 int x1 = c.xo() + c.pos2x(i1.pos_);
738 int y1 = c.yo() - c.ascent();
739 int x2 = c.xo() + c.pos2x(i2.pos_);
740 int y2 = c.yo() + c.descent();
741 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
743 std::vector<MathInset::idx_type> indices
744 = i1.par_->idxBetween(i1.idx_, i2.idx_);
745 for (unsigned i = 0; i < indices.size(); ++i) {
746 MathXArray & c = i1.xcell(indices[i]);
748 int y1 = c.yo() - c.ascent();
749 int x2 = c.xo() + c.width();
750 int y2 = c.yo() + c.descent();
751 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
756 // draw anchor if different from selection boundary
757 MathCursorPos anc = Anchor_.back();
758 if (anc != i1 && anc != i2) {
759 MathXArray & c = anc.xcell();
760 int x = c.xo() + c.pos2x(anc.pos_);
761 int y1 = c.yo() - c.ascent();
762 int y2 = c.yo() + c.descent();
763 pain.line(x, y1, x, y2, LColor::math);
769 void MathCursor::handleFont(MathTextCodes t)
775 getSelection(i1, i2);
776 if (i1.idx_ == i2.idx_) {
777 MathArray & ar = i1.cell();
778 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
779 ar.at(pos)->handleFont(t);
782 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
786 void MathCursor::handleDelim(string const & l, string const & r)
788 handleNest(new MathDelimInset(l, r));
792 void MathCursor::handleNest(MathInset * p)
796 p->cell(0) = theSelection.glue();
798 insert(MathAtom(p)); // this invalidates p!
799 pushRight(prevAtom());
803 void MathCursor::getPos(int & x, int & y)
806 #warning This should probably take cellXOffset and cellYOffset into account
808 x = xarray().xo() + xarray().pos2x(pos());
813 MathInset * MathCursor::par() const
815 return cursor().par_;
819 InsetFormulaBase * MathCursor::formula()
825 MathCursor::idx_type MathCursor::idx() const
827 return cursor().idx_;
831 MathCursor::idx_type & MathCursor::idx()
833 return cursor().idx_;
837 MathCursor::pos_type MathCursor::pos() const
839 return cursor().pos_;
843 MathCursor::pos_type & MathCursor::pos()
845 return cursor().pos_;
849 bool MathCursor::inMacroMode() const
851 return macroNamePos() != -1;
855 bool MathCursor::inMacroArgMode() const
857 return pos() > 0 && prevAtom()->getChar() == '#';
861 bool MathCursor::selection() const
867 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
869 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
870 MathGridInset * p = Cursor_[i].par_->asGridInset();
872 idx = Cursor_[i].idx_;
880 void MathCursor::pullArg(bool goright)
883 MathArray a = array();
885 MathScriptInset const * p = par()->asScriptInset();
887 // special handling for scripts
888 const bool up = p->hasUp();
890 MathScriptInset * q = nextAtom()->asScriptInset();
894 array().insert(pos(), a);
900 array().insert(pos(), a);
907 void MathCursor::normalize()
911 MathIterator it = ibegin(formula()->par().nucleus());
912 MathIterator et = iend(formula()->par().nucleus());
913 for ( ; it != et; ++it)
914 if (it.par()->asBoxInset())
915 it.par()->asBoxInset()->rebreak();
918 if (idx() >= par()->nargs()) {
919 lyxerr << "this should not really happen - 1: "
920 << idx() << " " << par()->nargs() << "\n";
923 idx() = min(idx(), par()->nargs() - 1);
925 if (pos() > size()) {
926 lyxerr << "this should not really happen - 2: "
927 << pos() << " " << size() << " in idx: " << idx()
929 WriteStream wi(lyxerr, false);
934 pos() = min(pos(), size());
938 MathCursor::size_type MathCursor::size() const
940 return array().size();
944 MathCursor::col_type MathCursor::hullCol() const
946 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
950 MathCursor::row_type MathCursor::hullRow() const
952 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
956 bool MathCursor::hasPrevAtom() const
962 bool MathCursor::hasNextAtom() const
964 return pos() < size();
968 MathAtom const & MathCursor::prevAtom() const
970 lyx::Assert(pos() > 0);
971 return array().at(pos() - 1);
975 MathAtom & MathCursor::prevAtom()
977 lyx::Assert(pos() > 0);
978 return array().at(pos() - 1);
982 MathAtom const & MathCursor::nextAtom() const
984 lyx::Assert(pos() < size());
985 return array().at(pos());
989 MathAtom & MathCursor::nextAtom()
991 lyx::Assert(pos() < size());
992 return array().at(pos());
996 MathArray & MathCursor::array() const
998 static MathArray dummy;
1000 if (idx() >= par()->nargs()) {
1001 lyxerr << "############ idx_ " << idx() << " not valid\n";
1005 if (Cursor_.size() == 0) {
1006 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1010 return cursor().cell();
1014 MathXArray & MathCursor::xarray() const
1016 static MathXArray dummy;
1018 if (Cursor_.size() == 0) {
1019 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1023 return cursor().xcell();
1027 void MathCursor::idxNext()
1029 par()->idxNext(idx(), pos());
1033 void MathCursor::idxPrev()
1035 par()->idxPrev(idx(), pos());
1039 void MathCursor::splitCell()
1041 if (idx() + 1 == par()->nargs())
1043 MathArray ar = array();
1045 array().erase(pos(), size());
1048 array().insert(0, ar);
1052 void MathCursor::breakLine()
1054 // leave inner cells
1058 MathHullInset * p = formula()->par()->asHullInset();
1062 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1063 p->mutate(LM_OT_EQNARRAY);
1067 p->addRow(hullRow());
1070 const row_type r = hullRow();
1071 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1072 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1076 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1081 //void MathCursor::readLine(MathArray & ar) const
1083 // idx_type base = row() * par()->ncols();
1084 // for (idx_type off = 0; off < par()->ncols(); ++off)
1085 // ar.push_back(par()->cell(base + off));
1089 char MathCursor::valign() const
1092 MathGridInset * p = enclosingGrid(idx);
1093 return p ? p->valign() : '\0';
1097 char MathCursor::halign() const
1100 MathGridInset * p = enclosingGrid(idx);
1101 return p ? p->halign(idx % p->ncols()) : '\0';
1105 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1107 MathCursorPos anc = normalAnchor();
1108 if (anc < cursor()) {
1118 MathCursorPos & MathCursor::cursor()
1120 lyx::Assert(Cursor_.size());
1121 return Cursor_.back();
1125 MathCursorPos const & MathCursor::cursor() const
1127 lyx::Assert(Cursor_.size());
1128 return Cursor_.back();
1132 bool MathCursor::goUpDown(bool up)
1134 int xlow, xhigh, ylow, yhigh;
1139 // try current cell first
1140 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1145 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1148 // try to find an inset that knows better then we
1150 // we found a cell that think something "below" us.
1152 if (par()->idxUp(idx()))
1155 if (par()->idxDown(idx()))
1160 // no such inset found, just take something "above"
1165 up ? formula()->ylow() : yo + 4,
1166 up ? yo - 4 : formula()->yhigh()
1170 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1171 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1176 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1178 cursor_type best_cursor;
1179 double best_dist = 1e10;
1181 MathIterator it = ibegin(formula()->par().nucleus());
1182 MathIterator et = iend(formula()->par().nucleus());
1183 for ( ; it != et; ++it) {
1184 // avoid invalid nesting hen selecting
1185 if (selection_ && !positionable(it.cursor(), Anchor_))
1187 int xo = it.position().xpos();
1188 int yo = it.position().ypos();
1189 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1190 ylow - 2 <= yo && yo <= yhigh + 2)
1192 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1193 if (d < best_dist) {
1195 best_cursor = it.cursor();
1199 if (best_dist < 1e10)
1200 Cursor_ = best_cursor;
1201 return best_dist < 1e10;
1205 bool MathCursor::idxLeft()
1207 return par()->idxLeft(idx(), pos());
1211 bool MathCursor::idxRight()
1213 return par()->idxRight(idx(), pos());
1217 bool MathCursor::interpret(string const & s)
1219 //lyxerr << "interpret 1: '" << s << "'\n";
1224 return interpret(s[0]);
1226 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1227 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1228 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1230 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1232 istringstream is(s.substr(5).c_str());
1234 n = std::max(1u, n);
1235 niceInsert(MathAtom(new MathCasesInset(n)));
1239 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1244 istringstream is(s.substr(6).c_str());
1245 is >> m >> n >> v_align >> h_align;
1246 m = std::max(1u, m);
1247 n = std::max(1u, n);
1249 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1253 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1255 istringstream is(s.substr(7).c_str());
1258 mathed_parse_cell(rep.from, from);
1259 mathed_parse_cell(rep.to, to);
1260 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1261 par()->replace(rep);
1265 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1266 MathArray ar = array();
1267 MathAtom t(createMathInset(s.substr(1)));
1268 t->asNestInset()->cell(0).swap(array());
1276 latexkeys const * l = in_word_set(s.substr(1));
1277 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1278 lastcode_ = static_cast<MathTextCodes>(l->id);
1282 niceInsert(createMathInset(s.substr(1)));
1287 bool MathCursor::script(bool up)
1291 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1292 prevAtom()->asScriptInset()->ensure(up);
1293 pushRight(prevAtom());
1296 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1297 nextAtom()->asScriptInset()->ensure(up);
1298 pushLeft(nextAtom());
1302 plainInsert(MathAtom(new MathScriptInset(up)));
1303 prevAtom()->asScriptInset()->ensure(up);
1304 pushRight(prevAtom());
1314 bool MathCursor::interpret(char c)
1316 if (inMacroArgMode()) {
1319 if ('1' <= c && c <= '9')
1320 insert(MathAtom(new MathMacroArgument(c - '0')));
1322 insert(MathAtom(new MathSpecialCharInset('#')));
1323 interpret(c); // try again
1329 if (inMacroMode()) {
1330 string name = macroName();
1332 if (name == "\\" && c == '\\') {
1334 interpret("\\backslash");
1339 insert(c, LM_TC_TEX);
1344 insert(c, LM_TC_TEX);
1352 insert(c, LM_TC_TEX);
1361 // fall through in the other cases
1364 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1365 // suppress direct insertion of two spaces in a row
1366 // the still allows typing '<space>a<space>' and deleting the 'a', but
1367 // it is better than nothing...
1368 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1370 insert(c, LM_TC_TEXTRM);
1375 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1376 prevAtom()->asSpaceInset()->incSpace();
1381 // if are at the very end, leave the formula
1382 return pos() != size();
1386 insert(c, LM_TC_TEX);
1391 if (strchr("{}", c)) {
1392 insert(c, LM_TC_TEX);
1398 niceInsert(MathAtom(new MathBraceInset));
1406 if (strchr("$%", c)) {
1407 insert(MathAtom(new MathSpecialCharInset(c)));
1408 lastcode_ = LM_TC_VAR;
1412 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1413 insert(c, LM_TC_VAR);
1417 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1418 insert(c, LM_TC_VAR);
1419 lastcode_ = LM_TC_VAR;
1424 insert(c, LM_TC_TEX);
1425 //bv->owner()->message(_("TeX mode"));
1429 // no special circumstances, so insert the character without any fuss
1430 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1431 lastcode_ = LM_TC_MIN;
1437 MathCursorPos MathCursor::normalAnchor() const
1439 if (Anchor_.size() < Cursor_.size()) {
1441 lyxerr << "unusual Anchor size\n";
1444 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1445 // use Anchor on the same level as Cursor
1446 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1447 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1448 // anchor is behind cursor -> move anchor behind the inset
1455 void MathCursor::stripFromLastEqualSign()
1457 // find position of last '=' in the array
1458 MathArray & ar = cursor().cell();
1459 MathArray::const_iterator et = ar.end();
1460 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1461 if ((*it)->getChar() == '=')
1464 // delete everything behind this position
1465 ar.erase(et - ar.begin(), ar.size());
1470 void MathCursor::setSelection(cursor_type const & where, size_type n)
1479 string MathCursor::info() const
1481 std::ostringstream os;
1483 prevAtom()->infoize(os);