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(MathAtom const & t)
460 void MathCursor::niceInsert(MathAtom const & t)
463 insert(t); // inserting invalidates the pointer!
464 MathAtom const & p = prevAtom();
467 right(); // do not push for e.g. MathSymbolInset
473 void MathCursor::insert(MathArray const & ar)
479 array().insert(pos(), ar);
484 void MathCursor::paste(MathArray const & ar)
488 array().insert(pos(), ar);
493 void MathCursor::backspace()
505 MathScriptInset * p = prevAtom()->asScriptInset();
507 p->removeScript(p->hasUp());
508 // Don't delete if there is anything left
509 if (p->hasUp() || p->hasDown())
518 void MathCursor::erase()
528 // delete empty cells if necessary
529 if (array().empty()) {
532 par()->idxDelete(idx(), popit, removeit);
533 if (popit && popLeft() && removeit)
541 MathScriptInset * p = nextAtom()->asScriptInset();
543 p->removeScript(p->hasUp());
544 // Don't delete if there is anything left
545 if (p->hasUp() || p->hasDown())
553 void MathCursor::delLine()
562 if (par()->nrows() > 1) {
563 // grid are the only things with more than one row...
564 lyx::Assert(par()->asGridInset());
565 par()->asGridInset()->delRow(hullRow());
568 if (idx() > par()->nargs())
569 idx() = par()->nargs();
576 bool MathCursor::up(bool sel)
581 cursor_type save = Cursor_;
589 bool MathCursor::down(bool sel)
594 cursor_type save = Cursor_;
602 bool MathCursor::toggleLimits()
606 MathScriptInset * t = nextAtom()->asScriptInset();
609 int old = t->limits();
610 t->limits(old < 0 ? 1 : -1);
611 return old != t->limits();
615 void MathCursor::macroModeClose()
617 string s = macroName();
619 size_type old = pos();
621 array().erase(pos(), old);
627 MathInset::difference_type MathCursor::macroNamePos() const
629 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
630 MathAtom & p = array().at(i);
631 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
638 string MathCursor::macroName() const
641 MathInset::difference_type i = macroNamePos();
642 for ( ; i >= 0 && i < int(pos()); ++i)
643 s += array().at(i)->getChar();
648 void MathCursor::selCopy()
652 theSelection.grab(*this);
658 void MathCursor::selCut()
662 theSelection.grab(*this);
663 theSelection.erase(*this);
666 theSelection.clear();
671 void MathCursor::selDel()
675 theSelection.erase(*this);
683 void MathCursor::selPaste()
686 theSelection.paste(*this);
687 //theSelection.grab(*this);
692 void MathCursor::selHandle(bool sel)
694 if (sel == selection_)
696 //theSelection.clear();
702 void MathCursor::selStart()
705 //theSelection.clear();
712 void MathCursor::selClear()
720 void MathCursor::selGet(MathArray & ar)
726 theSelection.grab(*this);
727 ar = theSelection.glue();
732 void MathCursor::drawSelection(Painter & pain) const
739 getSelection(i1, i2);
741 if (i1.idx_ == i2.idx_) {
742 MathXArray & c = i1.xcell();
743 int x1 = c.xo() + c.pos2x(i1.pos_);
744 int y1 = c.yo() - c.ascent();
745 int x2 = c.xo() + c.pos2x(i2.pos_);
746 int y2 = c.yo() + c.descent();
747 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
749 std::vector<MathInset::idx_type> indices
750 = i1.par_->idxBetween(i1.idx_, i2.idx_);
751 for (unsigned i = 0; i < indices.size(); ++i) {
752 MathXArray & c = i1.xcell(indices[i]);
754 int y1 = c.yo() - c.ascent();
755 int x2 = c.xo() + c.width();
756 int y2 = c.yo() + c.descent();
757 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
762 // draw anchor if different from selection boundary
763 MathCursorPos anc = Anchor_.back();
764 if (anc != i1 && anc != i2) {
765 MathXArray & c = anc.xcell();
766 int x = c.xo() + c.pos2x(anc.pos_);
767 int y1 = c.yo() - c.ascent();
768 int y2 = c.yo() + c.descent();
769 pain.line(x, y1, x, y2, LColor::math);
775 void MathCursor::handleFont(MathTextCodes t)
781 getSelection(i1, i2);
782 if (i1.idx_ == i2.idx_) {
783 MathArray & ar = i1.cell();
784 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
785 ar.at(pos)->handleFont(t);
788 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
792 void MathCursor::handleDelim(string const & l, string const & r)
794 handleNest(new MathDelimInset(l, r));
798 void MathCursor::handleNest(MathInset * p)
802 p->cell(0) = theSelection.glue();
804 insert(MathAtom(p)); // this invalidates p!
805 pushRight(prevAtom());
809 void MathCursor::getPos(int & x, int & y)
812 #warning This should probably take cellXOffset and cellYOffset into account
814 x = xarray().xo() + xarray().pos2x(pos());
819 MathInset * MathCursor::par() const
821 return cursor().par_;
825 InsetFormulaBase * MathCursor::formula()
831 MathCursor::idx_type MathCursor::idx() const
833 return cursor().idx_;
837 MathCursor::idx_type & MathCursor::idx()
839 return cursor().idx_;
843 MathCursor::pos_type MathCursor::pos() const
845 return cursor().pos_;
849 MathCursor::pos_type & MathCursor::pos()
851 return cursor().pos_;
855 bool MathCursor::inMacroMode() const
857 return macroNamePos() != -1;
861 bool MathCursor::inMacroArgMode() const
863 return pos() > 0 && prevAtom()->getChar() == '#';
867 bool MathCursor::selection() const
873 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
875 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
876 MathGridInset * p = Cursor_[i].par_->asGridInset();
878 idx = Cursor_[i].idx_;
886 void MathCursor::pullArg(bool goright)
889 MathArray a = array();
891 MathScriptInset const * p = par()->asScriptInset();
893 // special handling for scripts
894 const bool up = p->hasUp();
896 MathScriptInset * q = nextAtom()->asScriptInset();
900 array().insert(pos(), a);
906 array().insert(pos(), a);
913 void MathCursor::normalize()
917 MathIterator it = ibegin(formula()->par().nucleus());
918 MathIterator et = iend(formula()->par().nucleus());
919 for ( ; it != et; ++it)
920 if (it.par()->asBoxInset())
921 it.par()->asBoxInset()->rebreak();
924 if (idx() >= par()->nargs()) {
925 lyxerr << "this should not really happen - 1: "
926 << idx() << " " << par()->nargs() << "\n";
929 idx() = min(idx(), par()->nargs() - 1);
931 if (pos() > size()) {
932 lyxerr << "this should not really happen - 2: "
933 << pos() << " " << size() << " in idx: " << idx()
935 WriteStream wi(lyxerr, false);
940 pos() = min(pos(), size());
944 MathCursor::size_type MathCursor::size() const
946 return array().size();
950 MathCursor::col_type MathCursor::hullCol() const
952 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
956 MathCursor::row_type MathCursor::hullRow() const
958 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
962 bool MathCursor::hasPrevAtom() const
968 bool MathCursor::hasNextAtom() const
970 return pos() < size();
974 MathAtom const & MathCursor::prevAtom() const
976 lyx::Assert(pos() > 0);
977 return array().at(pos() - 1);
981 MathAtom & MathCursor::prevAtom()
983 lyx::Assert(pos() > 0);
984 return array().at(pos() - 1);
988 MathAtom const & MathCursor::nextAtom() const
990 lyx::Assert(pos() < size());
991 return array().at(pos());
995 MathAtom & MathCursor::nextAtom()
997 lyx::Assert(pos() < size());
998 return array().at(pos());
1002 MathArray & MathCursor::array() const
1004 static MathArray dummy;
1006 if (idx() >= par()->nargs()) {
1007 lyxerr << "############ idx_ " << idx() << " not valid\n";
1011 if (Cursor_.size() == 0) {
1012 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1016 return cursor().cell();
1020 MathXArray & MathCursor::xarray() const
1022 static MathXArray dummy;
1024 if (Cursor_.size() == 0) {
1025 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1029 return cursor().xcell();
1033 void MathCursor::idxNext()
1035 par()->idxNext(idx(), pos());
1039 void MathCursor::idxPrev()
1041 par()->idxPrev(idx(), pos());
1045 void MathCursor::splitCell()
1047 if (idx() + 1 == par()->nargs())
1049 MathArray ar = array();
1051 array().erase(pos(), size());
1054 array().insert(0, ar);
1058 void MathCursor::breakLine()
1060 // leave inner cells
1064 MathHullInset * p = formula()->par()->asHullInset();
1068 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1069 p->mutate(LM_OT_EQNARRAY);
1073 p->addRow(hullRow());
1076 const row_type r = hullRow();
1077 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1078 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1082 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1087 //void MathCursor::readLine(MathArray & ar) const
1089 // idx_type base = row() * par()->ncols();
1090 // for (idx_type off = 0; off < par()->ncols(); ++off)
1091 // ar.push_back(par()->cell(base + off));
1095 char MathCursor::valign() const
1098 MathGridInset * p = enclosingGrid(idx);
1099 return p ? p->valign() : '\0';
1103 char MathCursor::halign() const
1106 MathGridInset * p = enclosingGrid(idx);
1107 return p ? p->halign(idx % p->ncols()) : '\0';
1111 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1113 MathCursorPos anc = normalAnchor();
1114 if (anc < cursor()) {
1124 MathCursorPos & MathCursor::cursor()
1126 lyx::Assert(Cursor_.size());
1127 return Cursor_.back();
1131 MathCursorPos const & MathCursor::cursor() const
1133 lyx::Assert(Cursor_.size());
1134 return Cursor_.back();
1138 bool MathCursor::goUpDown(bool up)
1140 int xlow, xhigh, ylow, yhigh;
1145 // try current cell first
1146 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1151 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1154 // try to find an inset that knows better then we
1156 // we found a cell that think something "below" us.
1158 if (par()->idxUp(idx()))
1161 if (par()->idxDown(idx()))
1166 // no such inset found, just take something "above"
1171 up ? formula()->ylow() : yo + 4,
1172 up ? yo - 4 : formula()->yhigh()
1176 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1177 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1182 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1184 cursor_type best_cursor;
1185 double best_dist = 1e10;
1187 MathIterator it = ibegin(formula()->par().nucleus());
1188 MathIterator et = iend(formula()->par().nucleus());
1189 for ( ; it != et; ++it) {
1190 // avoid invalid nesting hen selecting
1191 if (selection_ && !positionable(it.cursor(), Anchor_))
1193 int xo = it.position().xpos();
1194 int yo = it.position().ypos();
1195 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1196 ylow - 2 <= yo && yo <= yhigh + 2)
1198 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1199 if (d < best_dist) {
1201 best_cursor = it.cursor();
1205 if (best_dist < 1e10)
1206 Cursor_ = best_cursor;
1207 return best_dist < 1e10;
1211 bool MathCursor::idxLeft()
1213 return par()->idxLeft(idx(), pos());
1217 bool MathCursor::idxRight()
1219 return par()->idxRight(idx(), pos());
1223 bool MathCursor::interpret(string const & s)
1225 //lyxerr << "interpret 1: '" << s << "'\n";
1230 return interpret(s[0]);
1232 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1233 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1234 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1236 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1238 istringstream is(s.substr(5).c_str());
1240 n = std::max(1u, n);
1241 niceInsert(MathAtom(new MathCasesInset(n)));
1245 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1250 istringstream is(s.substr(6).c_str());
1251 is >> m >> n >> v_align >> h_align;
1252 m = std::max(1u, m);
1253 n = std::max(1u, n);
1255 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1259 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1261 istringstream is(s.substr(7).c_str());
1264 mathed_parse_cell(rep.from, from);
1265 mathed_parse_cell(rep.to, to);
1266 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1267 par()->replace(rep);
1271 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1272 MathArray ar = array();
1273 MathAtom t(createMathInset(s.substr(1)));
1274 t->asNestInset()->cell(0).swap(array());
1282 latexkeys const * l = in_word_set(s.substr(1));
1283 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1284 lastcode_ = static_cast<MathTextCodes>(l->id);
1288 niceInsert(createMathInset(s.substr(1)));
1293 bool MathCursor::script(bool up)
1297 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1298 prevAtom()->asScriptInset()->ensure(up);
1299 pushRight(prevAtom());
1302 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1303 nextAtom()->asScriptInset()->ensure(up);
1304 pushLeft(nextAtom());
1308 plainInsert(MathAtom(new MathScriptInset(up)));
1309 prevAtom()->asScriptInset()->ensure(up);
1310 pushRight(prevAtom());
1320 bool MathCursor::interpret(char c)
1322 if (inMacroArgMode()) {
1325 if ('1' <= c && c <= '9')
1326 insert(MathAtom(new MathMacroArgument(c - '0')));
1328 insert(MathAtom(new MathSpecialCharInset('#')));
1329 interpret(c); // try again
1335 if (inMacroMode()) {
1336 string name = macroName();
1338 if (name == "\\" && c == '\\') {
1340 interpret("\\backslash");
1345 insert(c, LM_TC_TEX);
1350 insert(c, LM_TC_TEX);
1358 insert(c, LM_TC_TEX);
1367 // fall through in the other cases
1370 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1371 // suppress direct insertion of two spaces in a row
1372 // the still allows typing '<space>a<space>' and deleting the 'a', but
1373 // it is better than nothing...
1374 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1376 insert(c, LM_TC_TEXTRM);
1381 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1382 prevAtom()->asSpaceInset()->incSpace();
1387 // if are at the very end, leave the formula
1388 return pos() != size();
1392 insert(c, LM_TC_TEX);
1397 if (strchr("{}", c)) {
1398 insert(c, LM_TC_TEX);
1404 niceInsert(MathAtom(new MathBraceInset));
1412 if (strchr("$%", c)) {
1413 insert(MathAtom(new MathSpecialCharInset(c)));
1414 lastcode_ = LM_TC_VAR;
1418 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1419 insert(c, LM_TC_VAR);
1423 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1424 insert(c, LM_TC_VAR);
1425 lastcode_ = LM_TC_VAR;
1430 insert(c, LM_TC_TEX);
1431 //bv->owner()->message(_("TeX mode"));
1435 // no special circumstances, so insert the character without any fuss
1436 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1437 lastcode_ = LM_TC_MIN;
1443 MathCursorPos MathCursor::normalAnchor() const
1445 if (Anchor_.size() < Cursor_.size()) {
1447 lyxerr << "unusual Anchor size\n";
1450 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1451 // use Anchor on the same level as Cursor
1452 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1453 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1454 // anchor is behind cursor -> move anchor behind the inset
1461 void MathCursor::stripFromLastEqualSign()
1463 // find position of last '=' in the array
1464 MathArray & ar = cursor().cell();
1465 MathArray::const_iterator et = ar.end();
1466 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1467 if ((*it)->getChar() == '=')
1470 // delete everything behind this position
1471 ar.erase(et - ar.begin(), ar.size());
1476 void MathCursor::setSelection(cursor_type const & where, size_type n)
1485 string MathCursor::info() const
1487 std::ostringstream os;
1489 prevAtom()->infoize(os);