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();
208 if (par()->asScriptInset()->empty())
216 bool MathCursor::popRight()
218 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
219 if (Cursor_.size() <= 1)
221 if (par()->asScriptInset()) {
222 par()->asScriptInset()->removeEmptyScripts();
223 if (par()->asScriptInset()->empty())
234 void MathCursor::dump(char const * what) const
236 lyxerr << "MC: " << what << "\n";
237 lyxerr << " Cursor: " << Cursor_.size() << "\n";
238 for (unsigned i = 0; i < Cursor_.size(); ++i)
239 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
240 lyxerr << " Anchor: " << Anchor_.size() << "\n";
241 for (unsigned i = 0; i < Anchor_.size(); ++i)
242 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
243 lyxerr << " sel: " << selection_ << "\n";
246 void MathCursor::dump(char const *) const {}
250 UpdatableInset * MathCursor::asHyperActiveInset() const
252 return par()->asHyperActiveInset();
256 bool MathCursor::isInside(MathInset const * p) const
258 for (unsigned i = 0; i < Cursor_.size(); ++i)
259 if (Cursor_[i].par_ == p)
265 bool MathCursor::openable(MathAtom const & t, bool sel) const
267 if (t->isHyperActive())
273 if (t->asScriptInset())
277 // we can't move into anything new during selection
278 if (Cursor_.size() == Anchor_.size())
280 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
287 bool MathCursor::posLeft()
296 bool MathCursor::posRight()
305 bool MathCursor::left(bool sel)
310 lastcode_ = LM_TC_MIN;
314 lastcode_ = LM_TC_MIN;
316 if (hasPrevAtom() && openable(prevAtom(), sel)) {
317 if (prevAtom()->isHyperActive()) {
318 lyxerr << "entering hyperactive inset\n";
320 pushRight(prevAtom());
324 return posLeft() || idxLeft() || popLeft() || selection_;
328 bool MathCursor::right(bool sel)
333 lastcode_ = LM_TC_MIN;
337 lastcode_ = LM_TC_MIN;
339 if (hasNextAtom() && openable(nextAtom(), sel)) {
340 if (nextAtom()->isHyperActive()) {
341 lyxerr << "entering hyperactive inset\n";
344 nextAtom()->edit(formula()->view(), x, y, 0);
346 pushLeft(nextAtom());
350 return posRight() || idxRight() || popRight() || selection_;
354 void MathCursor::first()
357 pushLeft(formula_->par());
361 void MathCursor::last()
368 bool positionable(MathCursor::cursor_type const & cursor,
369 MathCursor::cursor_type const & anchor)
371 // avoid deeper nested insets when selecting
372 if (cursor.size() > anchor.size())
375 // anchor might be deeper, should have same path then
376 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
377 if (cursor[i].par_ != anchor[i].par_)
380 // position should be ok.
385 void MathCursor::setPos(int x, int y)
388 bool res = bruteFind(x, y,
389 formula()->xlow(), formula()->xhigh(),
390 formula()->ylow(), formula()->yhigh());
392 // this ccan happen on creation of "math-display"
401 void MathCursor::home(bool sel)
406 lastcode_ = LM_TC_MIN;
407 if (!par()->idxHome(idx(), pos()))
413 void MathCursor::end(bool sel)
418 lastcode_ = LM_TC_MIN;
419 if (!par()->idxEnd(idx(), pos()))
425 void MathCursor::plainErase()
427 array().erase(pos());
431 void MathCursor::plainInsert(MathAtom const & t)
433 array().insert(pos(), t);
438 void MathCursor::insert(char c, MathTextCodes t)
440 //lyxerr << "inserting '" << c << "'\n";
441 plainInsert(MathAtom(new MathCharInset(c, t)));
445 void MathCursor::insert(char c)
447 insert(c, lastcode_);
451 void MathCursor::insert(MathAtom const & t)
466 void MathCursor::niceInsert(MathAtom const & t)
469 insert(t); // inserting invalidates the pointer!
470 MathAtom const & p = prevAtom();
473 right(); // do not push for e.g. MathSymbolInset
479 void MathCursor::insert(MathArray const & ar)
485 array().insert(pos(), ar);
490 void MathCursor::paste(MathArray const & ar)
494 array().insert(pos(), ar);
499 void MathCursor::backspace()
511 MathScriptInset * p = prevAtom()->asScriptInset();
513 p->removeScript(p->hasUp());
514 // Don't delete if there is anything left
515 if (p->hasUp() || p->hasDown())
524 void MathCursor::erase()
534 // delete empty cells if necessary
535 if (array().empty()) {
538 par()->idxDelete(idx(), popit, removeit);
539 if (popit && popLeft() && removeit)
547 MathScriptInset * p = nextAtom()->asScriptInset();
549 p->removeScript(p->hasUp());
550 // Don't delete if there is anything left
551 if (p->hasUp() || p->hasDown())
559 void MathCursor::delLine()
568 if (par()->nrows() > 1) {
569 // grid are the only things with more than one row...
570 lyx::Assert(par()->asGridInset());
571 par()->asGridInset()->delRow(hullRow());
574 if (idx() >= par()->nargs())
575 idx() = par()->nargs() - 1;
582 bool MathCursor::up(bool sel)
587 cursor_type save = Cursor_;
595 bool MathCursor::down(bool sel)
600 cursor_type save = Cursor_;
608 bool MathCursor::toggleLimits()
612 MathScriptInset * t = nextAtom()->asScriptInset();
615 int old = t->limits();
616 t->limits(old < 0 ? 1 : -1);
617 return old != t->limits();
621 void MathCursor::macroModeClose()
623 string s = macroName();
625 size_type old = pos();
627 array().erase(pos(), old);
633 MathInset::difference_type MathCursor::macroNamePos() const
635 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
636 MathAtom & p = array().at(i);
637 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
644 string MathCursor::macroName() const
647 MathInset::difference_type i = macroNamePos();
648 for ( ; i >= 0 && i < int(pos()); ++i)
649 s += array().at(i)->getChar();
654 void MathCursor::selCopy()
658 theSelection.grab(*this);
664 void MathCursor::selCut()
668 theSelection.grab(*this);
669 theSelection.erase(*this);
672 theSelection.clear();
677 void MathCursor::selDel()
681 theSelection.erase(*this);
689 void MathCursor::selPaste()
692 theSelection.paste(*this);
693 //theSelection.grab(*this);
698 void MathCursor::selHandle(bool sel)
700 if (sel == selection_)
702 //theSelection.clear();
708 void MathCursor::selStart()
711 //theSelection.clear();
718 void MathCursor::selClear()
726 void MathCursor::selGet(MathArray & ar)
732 theSelection.grab(*this);
733 ar = theSelection.glue();
738 void MathCursor::drawSelection(Painter & pain) const
745 getSelection(i1, i2);
747 if (i1.idx_ == i2.idx_) {
748 MathXArray & c = i1.xcell();
749 int x1 = c.xo() + c.pos2x(i1.pos_);
750 int y1 = c.yo() - c.ascent();
751 int x2 = c.xo() + c.pos2x(i2.pos_);
752 int y2 = c.yo() + c.descent();
753 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
755 std::vector<MathInset::idx_type> indices
756 = i1.par_->idxBetween(i1.idx_, i2.idx_);
757 for (unsigned i = 0; i < indices.size(); ++i) {
758 MathXArray & c = i1.xcell(indices[i]);
760 int y1 = c.yo() - c.ascent();
761 int x2 = c.xo() + c.width();
762 int y2 = c.yo() + c.descent();
763 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
768 // draw anchor if different from selection boundary
769 MathCursorPos anc = Anchor_.back();
770 if (anc != i1 && anc != i2) {
771 MathXArray & c = anc.xcell();
772 int x = c.xo() + c.pos2x(anc.pos_);
773 int y1 = c.yo() - c.ascent();
774 int y2 = c.yo() + c.descent();
775 pain.line(x, y1, x, y2, LColor::math);
781 void MathCursor::handleFont(MathTextCodes t)
787 getSelection(i1, i2);
788 if (i1.idx_ == i2.idx_) {
789 MathArray & ar = i1.cell();
790 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
791 ar.at(pos)->handleFont(t);
794 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
798 void MathCursor::handleDelim(string const & l, string const & r)
800 handleNest(new MathDelimInset(l, r));
804 void MathCursor::handleNest(MathInset * p)
808 p->cell(0) = theSelection.glue();
810 insert(MathAtom(p)); // this invalidates p!
811 pushRight(prevAtom());
815 void MathCursor::getPos(int & x, int & y)
818 #warning This should probably take cellXOffset and cellYOffset into account
820 x = xarray().xo() + xarray().pos2x(pos());
825 MathInset * MathCursor::par() const
827 return cursor().par_;
831 InsetFormulaBase * MathCursor::formula()
837 MathCursor::idx_type MathCursor::idx() const
839 return cursor().idx_;
843 MathCursor::idx_type & MathCursor::idx()
845 return cursor().idx_;
849 MathCursor::pos_type MathCursor::pos() const
851 return cursor().pos_;
855 MathCursor::pos_type & MathCursor::pos()
857 return cursor().pos_;
861 bool MathCursor::inMacroMode() const
863 return macroNamePos() != -1;
867 bool MathCursor::inMacroArgMode() const
869 return pos() > 0 && prevAtom()->getChar() == '#';
873 bool MathCursor::selection() const
879 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
881 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
882 MathGridInset * p = Cursor_[i].par_->asGridInset();
884 idx = Cursor_[i].idx_;
892 void MathCursor::pullArg(bool goright)
895 MathArray a = array();
897 MathScriptInset const * p = par()->asScriptInset();
899 // special handling for scripts
900 const bool up = p->hasUp();
902 MathScriptInset * q = nextAtom()->asScriptInset();
906 array().insert(pos(), a);
912 array().insert(pos(), a);
916 formula()->mutateToText();
921 void MathCursor::normalize()
925 MathIterator it = ibegin(formula()->par().nucleus());
926 MathIterator et = iend(formula()->par().nucleus());
927 for ( ; it != et; ++it)
928 if (it.par()->asBoxInset())
929 it.par()->asBoxInset()->rebreak();
932 if (idx() >= par()->nargs()) {
933 lyxerr << "this should not really happen - 1: "
934 << idx() << " " << par()->nargs() << "\n";
937 idx() = min(idx(), par()->nargs() - 1);
939 if (pos() > size()) {
940 lyxerr << "this should not really happen - 2: "
941 << pos() << " " << size() << " in idx: " << idx()
943 WriteStream wi(lyxerr, false);
948 pos() = min(pos(), size());
952 MathCursor::size_type MathCursor::size() const
954 return array().size();
958 MathCursor::col_type MathCursor::hullCol() const
960 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
964 MathCursor::row_type MathCursor::hullRow() const
966 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
970 bool MathCursor::hasPrevAtom() const
976 bool MathCursor::hasNextAtom() const
978 return pos() < size();
982 MathAtom const & MathCursor::prevAtom() const
984 lyx::Assert(pos() > 0);
985 return array().at(pos() - 1);
989 MathAtom & MathCursor::prevAtom()
991 lyx::Assert(pos() > 0);
992 return array().at(pos() - 1);
996 MathAtom const & MathCursor::nextAtom() const
998 lyx::Assert(pos() < size());
999 return array().at(pos());
1003 MathAtom & MathCursor::nextAtom()
1005 lyx::Assert(pos() < size());
1006 return array().at(pos());
1010 MathArray & MathCursor::array() const
1012 static MathArray dummy;
1014 if (idx() >= par()->nargs()) {
1015 lyxerr << "############ idx_ " << idx() << " not valid\n";
1019 if (Cursor_.size() == 0) {
1020 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1024 return cursor().cell();
1028 MathXArray & MathCursor::xarray() const
1030 static MathXArray dummy;
1032 if (Cursor_.size() == 0) {
1033 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1037 return cursor().xcell();
1041 void MathCursor::idxNext()
1043 par()->idxNext(idx(), pos());
1047 void MathCursor::idxPrev()
1049 par()->idxPrev(idx(), pos());
1053 void MathCursor::splitCell()
1055 if (idx() + 1 == par()->nargs())
1057 MathArray ar = array();
1059 array().erase(pos(), size());
1062 array().insert(0, ar);
1066 void MathCursor::breakLine()
1068 // leave inner cells
1072 MathHullInset * p = formula()->par()->asHullInset();
1076 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1077 p->mutate(LM_OT_EQNARRAY);
1081 p->addRow(hullRow());
1084 const row_type r = hullRow();
1085 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1086 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1090 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1095 //void MathCursor::readLine(MathArray & ar) const
1097 // idx_type base = row() * par()->ncols();
1098 // for (idx_type off = 0; off < par()->ncols(); ++off)
1099 // ar.push_back(par()->cell(base + off));
1103 char MathCursor::valign() const
1106 MathGridInset * p = enclosingGrid(idx);
1107 return p ? p->valign() : '\0';
1111 char MathCursor::halign() const
1114 MathGridInset * p = enclosingGrid(idx);
1115 return p ? p->halign(idx % p->ncols()) : '\0';
1119 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1121 MathCursorPos anc = normalAnchor();
1122 if (anc < cursor()) {
1132 MathCursorPos & MathCursor::cursor()
1134 lyx::Assert(Cursor_.size());
1135 return Cursor_.back();
1139 MathCursorPos const & MathCursor::cursor() const
1141 lyx::Assert(Cursor_.size());
1142 return Cursor_.back();
1146 bool MathCursor::goUpDown(bool up)
1148 int xlow, xhigh, ylow, yhigh;
1153 // try current cell first
1154 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1159 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1162 // try to find an inset that knows better then we
1164 // we found a cell that think something "below" us.
1166 if (par()->idxUp(idx()))
1169 if (par()->idxDown(idx()))
1174 // no such inset found, just take something "above"
1179 up ? formula()->ylow() : yo + 4,
1180 up ? yo - 4 : formula()->yhigh()
1184 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1185 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1190 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1192 cursor_type best_cursor;
1193 double best_dist = 1e10;
1195 MathIterator it = ibegin(formula()->par().nucleus());
1196 MathIterator et = iend(formula()->par().nucleus());
1198 // avoid invalid nesting when selecting
1199 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1200 int xo = it.position().xpos();
1201 int yo = it.position().ypos();
1202 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1203 ylow - 2 <= yo && yo <= yhigh + 2)
1205 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1206 if (d < best_dist) {
1208 best_cursor = it.cursor();
1218 if (best_dist < 1e10)
1219 Cursor_ = best_cursor;
1220 return best_dist < 1e10;
1224 bool MathCursor::idxLeft()
1226 return par()->idxLeft(idx(), pos());
1230 bool MathCursor::idxRight()
1232 return par()->idxRight(idx(), pos());
1236 bool MathCursor::interpret(string const & s)
1238 //lyxerr << "interpret 1: '" << s << "'\n";
1243 return interpret(s[0]);
1245 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1246 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1247 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1249 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1251 istringstream is(s.substr(5).c_str());
1253 n = std::max(1u, n);
1254 niceInsert(MathAtom(new MathCasesInset(n)));
1258 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1263 istringstream is(s.substr(6).c_str());
1264 is >> m >> n >> v_align >> h_align;
1265 m = std::max(1u, m);
1266 n = std::max(1u, n);
1268 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1272 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1274 istringstream is(s.substr(7).c_str());
1277 mathed_parse_cell(rep.from, from);
1278 mathed_parse_cell(rep.to, to);
1279 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1280 par()->replace(rep);
1284 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1285 MathArray ar = array();
1286 MathAtom t(createMathInset(s.substr(1)));
1287 t->asNestInset()->cell(0).swap(array());
1295 latexkeys const * l = in_word_set(s.substr(1));
1296 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1297 lastcode_ = static_cast<MathTextCodes>(l->id);
1301 // prevent entering of recursive macros
1302 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1303 && formula()->getInsetName() == s.substr(1))
1305 lyxerr << "can't enter recursive macro\n";
1309 niceInsert(createMathInset(s.substr(1)));
1314 bool MathCursor::script(bool up)
1318 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1319 prevAtom()->asScriptInset()->ensure(up);
1320 pushRight(prevAtom());
1323 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1324 nextAtom()->asScriptInset()->ensure(up);
1325 pushLeft(nextAtom());
1329 plainInsert(MathAtom(new MathScriptInset(up)));
1330 prevAtom()->asScriptInset()->ensure(up);
1331 pushRight(prevAtom());
1341 bool MathCursor::interpret(char c)
1343 if (inMacroArgMode()) {
1346 if ('1' <= c && c <= '9')
1347 insert(MathAtom(new MathMacroArgument(c - '0')));
1349 insert(MathAtom(new MathSpecialCharInset('#')));
1350 interpret(c); // try again
1356 if (inMacroMode()) {
1357 string name = macroName();
1359 if (name == "\\" && c == '\\') {
1361 interpret("\\backslash");
1366 insert(c, LM_TC_TEX);
1371 insert(c, LM_TC_TEX);
1379 insert(c, LM_TC_TEX);
1388 // fall through in the other cases
1391 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1392 // suppress direct insertion of two spaces in a row
1393 // the still allows typing '<space>a<space>' and deleting the 'a', but
1394 // it is better than nothing...
1395 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1397 insert(c, LM_TC_TEXTRM);
1402 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1403 prevAtom()->asSpaceInset()->incSpace();
1408 // if are at the very end, leave the formula
1409 return pos() != size();
1413 insert(c, LM_TC_TEX);
1418 if (strchr("{}", c)) {
1419 insert(c, LM_TC_TEX);
1425 niceInsert(MathAtom(new MathBraceInset));
1433 if (strchr("$%", c)) {
1434 insert(MathAtom(new MathSpecialCharInset(c)));
1435 lastcode_ = LM_TC_VAR;
1439 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1440 insert(c, LM_TC_VAR);
1444 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1445 insert(c, LM_TC_VAR);
1446 lastcode_ = LM_TC_VAR;
1451 insert(c, LM_TC_TEX);
1452 //bv->owner()->message(_("TeX mode"));
1456 // no special circumstances, so insert the character without any fuss
1457 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1458 lastcode_ = LM_TC_MIN;
1464 MathCursorPos MathCursor::normalAnchor() const
1466 if (Anchor_.size() < Cursor_.size()) {
1468 lyxerr << "unusual Anchor size\n";
1471 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1472 // use Anchor on the same level as Cursor
1473 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1474 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1475 // anchor is behind cursor -> move anchor behind the inset
1482 void MathCursor::stripFromLastEqualSign()
1484 // find position of last '=' in the array
1485 MathArray & ar = cursor().cell();
1486 MathArray::const_iterator et = ar.end();
1487 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1488 if ((*it)->getChar() == '=')
1491 // delete everything behind this position
1492 ar.erase(et - ar.begin(), ar.size());
1497 void MathCursor::setSelection(cursor_type const & where, size_type n)
1506 string MathCursor::info() const
1508 std::ostringstream os;
1510 prevAtom()->infoize(os);