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_mathmlstream.h"
41 #include "math_parser.h"
42 #include "math_replace.h"
43 #include "math_scriptinset.h"
44 #include "math_spaceinset.h"
45 #include "math_specialcharinset.h"
46 #include "math_support.h"
63 typedef MathInset::col_type col_type;
64 typedef MathInset::row_type row_type;
65 typedef MathInset::idx_type idx_type;
71 void region(MathCursorPos const & i1, MathCursorPos const & i2,
72 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
74 MathInset * p = i1.par_;
85 void grab(MathCursor const & cursor)
89 cursor.getSelection(i1, i2);
90 // shouldn'tt we assert on i1.par_ == i2.par_?
91 if (i1.idx_ == i2.idx_) {
92 data_ = MathGridInset(1, 1);
93 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
97 region(i1, i2, r1, r2, c1, c2);
98 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
99 for (row_type row = 0; row < data_.nrows(); ++row)
100 for (col_type col = 0; col < data_.ncols(); ++col) {
101 idx_type i = i1.par_->index(row + r1, col + c1);
102 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
107 void erase(MathCursor & cursor)
111 cursor.getSelection(i1, i2);
112 if (i1.idx_ == i2.idx_)
113 i1.cell().erase(i1.pos_, i2.pos_);
115 MathInset * p = i1.par_;
118 region(i1, i2, r1, r2, c1, c2);
119 for (row_type row = r1; row <= r2; ++row)
120 for (col_type col = c1; col <= c2; ++col)
121 p->cell(p->index(row, col)).erase();
123 cursor.cursor() = i1;
126 void paste(MathCursor & cursor) const
128 if (data_.nargs() == 1) {
129 // single cell/part of cell
130 cursor.insert(data_.cell(0));
134 MathGridInset * p = cursor.enclosingGrid(idx);
135 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
136 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
137 for (row_type row = 0; row < numrows; ++row)
138 for (col_type col = 0; col < numcols; ++col) {
139 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
140 p->cell(i).push_back(data_.cell(data_.index(row, col)));
145 // glues selection to one cell
146 MathArray glue() const
149 for (unsigned i = 0; i < data_.nargs(); ++i)
150 ar.push_back(data_.cell(i));
156 data_ = MathGridInset(1, 1);
163 Selection theSelection;
170 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
171 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
173 left ? first() : last();
177 void MathCursor::push(MathAtom & t)
179 Cursor_.push_back(MathCursorPos(t.nucleus()));
183 void MathCursor::pushLeft(MathAtom & t)
185 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
187 t->idxFirst(idx(), pos());
191 void MathCursor::pushRight(MathAtom & t)
193 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
196 t->idxLast(idx(), pos());
200 bool MathCursor::popLeft()
202 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
203 if (Cursor_.size() <= 1)
205 if (par()->asScriptInset())
206 par()->asScriptInset()->removeEmptyScripts();
212 bool MathCursor::popRight()
214 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
215 if (Cursor_.size() <= 1)
217 if (par()->asScriptInset())
218 par()->asScriptInset()->removeEmptyScripts();
227 void MathCursor::dump(char const * what) const
229 lyxerr << "MC: " << what << "\n";
230 lyxerr << " Cursor: " << Cursor_.size() << "\n";
231 for (unsigned i = 0; i < Cursor_.size(); ++i)
232 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
233 lyxerr << " Anchor: " << Anchor_.size() << "\n";
234 for (unsigned i = 0; i < Anchor_.size(); ++i)
235 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
236 lyxerr << " sel: " << selection_ << "\n";
239 void MathCursor::dump(char const *) const {}
243 UpdatableInset * MathCursor::asHyperActiveInset() const
245 return par()->asHyperActiveInset();
249 bool MathCursor::isInside(MathInset const * p) const
251 for (unsigned i = 0; i < Cursor_.size(); ++i)
252 if (Cursor_[i].par_ == p)
258 bool MathCursor::openable(MathAtom const & t, bool sel) const
260 if (t->isHyperActive())
266 if (t->asScriptInset())
270 // we can't move into anything new during selection
271 if (Cursor_.size() == Anchor_.size())
273 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
280 bool MathCursor::posLeft()
289 bool MathCursor::posRight()
298 bool MathCursor::left(bool sel)
303 lastcode_ = LM_TC_MIN;
307 lastcode_ = LM_TC_MIN;
309 if (hasPrevAtom() && openable(prevAtom(), sel)) {
310 if (prevAtom()->isHyperActive()) {
311 lyxerr << "entering hyperactive inset\n";
313 pushRight(prevAtom());
317 return posLeft() || idxLeft() || popLeft() || selection_;
321 bool MathCursor::right(bool sel)
326 lastcode_ = LM_TC_MIN;
330 lastcode_ = LM_TC_MIN;
332 if (hasNextAtom() && openable(nextAtom(), sel)) {
333 if (nextAtom()->isHyperActive()) {
334 lyxerr << "entering hyperactive inset\n";
337 nextAtom()->edit(formula()->view(), x, y, 0);
339 pushLeft(nextAtom());
343 return posRight() || idxRight() || popRight() || selection_;
347 void MathCursor::first()
350 pushLeft(formula_->par());
354 void MathCursor::last()
361 bool positionable(MathCursor::cursor_type const & cursor,
362 MathCursor::cursor_type const & anchor)
364 // avoid deeper nested insets when selecting
365 if (cursor.size() > anchor.size())
368 // anchor might be deeper, should have same path then
369 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
370 if (cursor[i].par_ != anchor[i].par_)
373 // position should be ok.
378 void MathCursor::setPos(int x, int y)
381 bool res = bruteFind(x, y,
382 formula()->xlow(), formula()->xhigh(),
383 formula()->ylow(), formula()->yhigh());
385 // this ccan happen on creation of "math-display"
394 void MathCursor::home(bool sel)
399 lastcode_ = LM_TC_MIN;
400 if (!par()->idxHome(idx(), pos()))
406 void MathCursor::end(bool sel)
411 lastcode_ = LM_TC_MIN;
412 if (!par()->idxEnd(idx(), pos()))
418 void MathCursor::plainErase()
420 array().erase(pos());
424 void MathCursor::plainInsert(MathAtom const & t)
426 array().insert(pos(), t);
431 void MathCursor::insert(char c, MathTextCodes t)
433 //lyxerr << "inserting '" << c << "'\n";
434 plainInsert(MathAtom(new MathCharInset(c, t)));
438 void MathCursor::insert(MathAtom const & t)
453 void MathCursor::niceInsert(MathAtom const & t)
456 insert(t); // inserting invalidates the pointer!
457 MathAtom const & p = prevAtom();
460 right(); // do not push for e.g. MathSymbolInset
466 void MathCursor::insert(MathArray const & ar)
472 array().insert(pos(), ar);
477 void MathCursor::paste(MathArray const & ar)
481 array().insert(pos(), ar);
486 void MathCursor::backspace()
498 MathScriptInset * p = prevAtom()->asScriptInset();
500 p->removeScript(p->hasUp());
501 // Don't delete if there is anything left
502 if (p->hasUp() || p->hasDown())
511 void MathCursor::erase()
521 // delete empty cells if necessary
522 if (array().empty()) {
525 par()->idxDelete(idx(), popit, removeit);
526 if (popit && popLeft() && removeit)
534 MathScriptInset * p = nextAtom()->asScriptInset();
536 p->removeScript(p->hasUp());
537 // Don't delete if there is anything left
538 if (p->hasUp() || p->hasDown())
546 void MathCursor::delLine()
555 if (par()->nrows() > 1) {
556 // grid are the only things with more than one row...
557 lyx::Assert(par()->asGridInset());
558 par()->asGridInset()->delRow(hullRow());
561 if (idx() > par()->nargs())
562 idx() = par()->nargs();
569 bool MathCursor::up(bool sel)
574 cursor_type save = Cursor_;
582 bool MathCursor::down(bool sel)
587 cursor_type save = Cursor_;
595 bool MathCursor::toggleLimits()
599 MathScriptInset * t = nextAtom()->asScriptInset();
602 int old = t->limits();
603 t->limits(old < 0 ? 1 : -1);
604 return old != t->limits();
608 void MathCursor::macroModeClose()
610 string s = macroName();
612 size_type old = pos();
614 array().erase(pos(), old);
620 MathInset::difference_type MathCursor::macroNamePos() const
622 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
623 MathAtom & p = array().at(i);
624 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
631 string MathCursor::macroName() const
634 MathInset::difference_type i = macroNamePos();
635 for ( ; i >= 0 && i < int(pos()); ++i)
636 s += array().at(i)->getChar();
641 void MathCursor::selCopy()
645 theSelection.grab(*this);
651 void MathCursor::selCut()
655 theSelection.grab(*this);
656 theSelection.erase(*this);
659 theSelection.clear();
664 void MathCursor::selDel()
668 theSelection.erase(*this);
676 void MathCursor::selPaste()
679 theSelection.paste(*this);
680 //theSelection.grab(*this);
685 void MathCursor::selHandle(bool sel)
687 if (sel == selection_)
689 //theSelection.clear();
695 void MathCursor::selStart()
698 //theSelection.clear();
705 void MathCursor::selClear()
713 void MathCursor::selGet(MathArray & ar)
719 theSelection.grab(*this);
720 ar = theSelection.glue();
725 void MathCursor::drawSelection(Painter & pain) const
732 getSelection(i1, i2);
734 if (i1.idx_ == i2.idx_) {
735 MathXArray & c = i1.xcell();
736 int x1 = c.xo() + c.pos2x(i1.pos_);
737 int y1 = c.yo() - c.ascent();
738 int x2 = c.xo() + c.pos2x(i2.pos_);
739 int y2 = c.yo() + c.descent();
740 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
742 std::vector<MathInset::idx_type> indices
743 = i1.par_->idxBetween(i1.idx_, i2.idx_);
744 for (unsigned i = 0; i < indices.size(); ++i) {
745 MathXArray & c = i1.xcell(indices[i]);
747 int y1 = c.yo() - c.ascent();
748 int x2 = c.xo() + c.width();
749 int y2 = c.yo() + c.descent();
750 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
755 // draw anchor if different from selection boundary
756 MathCursorPos anc = Anchor_.back();
757 if (anc != i1 && anc != i2) {
758 MathXArray & c = anc.xcell();
759 int x = c.xo() + c.pos2x(anc.pos_);
760 int y1 = c.yo() - c.ascent();
761 int y2 = c.yo() + c.descent();
762 pain.line(x, y1, x, y2, LColor::math);
768 void MathCursor::handleFont(MathTextCodes t)
774 getSelection(i1, i2);
775 if (i1.idx_ == i2.idx_) {
776 MathArray & ar = i1.cell();
777 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
778 ar.at(pos)->handleFont(t);
781 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
785 void MathCursor::handleDelim(string const & l, string const & r)
787 handleNest(new MathDelimInset(l, r));
791 void MathCursor::handleNest(MathInset * p)
795 p->cell(0) = theSelection.glue();
797 insert(MathAtom(p)); // this invalidates p!
798 pushRight(prevAtom());
802 void MathCursor::getPos(int & x, int & y)
805 #warning This should probably take cellXOffset and cellYOffset into account
807 x = xarray().xo() + xarray().pos2x(pos());
812 MathInset * MathCursor::par() const
814 return cursor().par_;
818 InsetFormulaBase * MathCursor::formula()
824 MathCursor::idx_type MathCursor::idx() const
826 return cursor().idx_;
830 MathCursor::idx_type & MathCursor::idx()
832 return cursor().idx_;
836 MathCursor::pos_type MathCursor::pos() const
838 return cursor().pos_;
842 MathCursor::pos_type & MathCursor::pos()
844 return cursor().pos_;
848 bool MathCursor::inMacroMode() const
850 return macroNamePos() != -1;
854 bool MathCursor::selection() const
860 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
862 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
863 MathGridInset * p = Cursor_[i].par_->asGridInset();
865 idx = Cursor_[i].idx_;
873 void MathCursor::pullArg(bool goright)
876 MathArray a = array();
878 MathScriptInset const * p = par()->asScriptInset();
880 // special handling for scripts
881 const bool up = p->hasUp();
883 MathScriptInset * q = nextAtom()->asScriptInset();
887 array().insert(pos(), a);
893 array().insert(pos(), a);
900 void MathCursor::normalize()
904 MathIterator it = ibegin(formula()->par().nucleus());
905 MathIterator et = iend(formula()->par().nucleus());
906 for ( ; it != et; ++it)
907 if (it.par()->asBoxInset())
908 it.par()->asBoxInset()->rebreak();
911 if (idx() >= par()->nargs()) {
912 lyxerr << "this should not really happen - 1: "
913 << idx() << " " << par()->nargs() << "\n";
916 idx() = min(idx(), par()->nargs() - 1);
918 if (pos() > size()) {
919 lyxerr << "this should not really happen - 2: "
920 << pos() << " " << size() << " in idx: " << idx()
922 WriteStream wi(lyxerr, false);
927 pos() = min(pos(), size());
931 MathCursor::size_type MathCursor::size() const
933 return array().size();
937 MathCursor::col_type MathCursor::hullCol() const
939 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
943 MathCursor::row_type MathCursor::hullRow() const
945 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
949 bool MathCursor::hasPrevAtom() const
955 bool MathCursor::hasNextAtom() const
957 return pos() < size();
961 MathAtom const & MathCursor::prevAtom() const
963 lyx::Assert(pos() > 0);
964 return array().at(pos() - 1);
968 MathAtom & MathCursor::prevAtom()
970 lyx::Assert(pos() > 0);
971 return array().at(pos() - 1);
975 MathAtom const & MathCursor::nextAtom() const
977 lyx::Assert(pos() < size());
978 return array().at(pos());
982 MathAtom & MathCursor::nextAtom()
984 lyx::Assert(pos() < size());
985 return array().at(pos());
989 MathArray & MathCursor::array() const
991 static MathArray dummy;
993 if (idx() >= par()->nargs()) {
994 lyxerr << "############ idx_ " << idx() << " not valid\n";
998 if (Cursor_.size() == 0) {
999 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1003 return cursor().cell();
1007 MathXArray & MathCursor::xarray() const
1009 static MathXArray dummy;
1011 if (Cursor_.size() == 0) {
1012 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1016 return cursor().xcell();
1020 void MathCursor::idxNext()
1022 par()->idxNext(idx(), pos());
1026 void MathCursor::idxPrev()
1028 par()->idxPrev(idx(), pos());
1032 void MathCursor::splitCell()
1034 if (idx() + 1 == par()->nargs())
1036 MathArray ar = array();
1038 array().erase(pos(), size());
1041 array().insert(0, ar);
1045 void MathCursor::breakLine()
1047 // leave inner cells
1051 MathHullInset * p = formula()->par()->asHullInset();
1055 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1056 p->mutate(LM_OT_EQNARRAY);
1060 p->addRow(hullRow());
1063 const row_type r = hullRow();
1064 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1065 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1069 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1074 //void MathCursor::readLine(MathArray & ar) const
1076 // idx_type base = row() * par()->ncols();
1077 // for (idx_type off = 0; off < par()->ncols(); ++off)
1078 // ar.push_back(par()->cell(base + off));
1082 char MathCursor::valign() const
1085 MathGridInset * p = enclosingGrid(idx);
1086 return p ? p->valign() : '\0';
1090 char MathCursor::halign() const
1093 MathGridInset * p = enclosingGrid(idx);
1094 return p ? p->halign(idx % p->ncols()) : '\0';
1098 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1100 MathCursorPos anc = normalAnchor();
1101 if (anc < cursor()) {
1111 MathCursorPos & MathCursor::cursor()
1113 lyx::Assert(Cursor_.size());
1114 return Cursor_.back();
1118 MathCursorPos const & MathCursor::cursor() const
1120 lyx::Assert(Cursor_.size());
1121 return Cursor_.back();
1125 bool MathCursor::goUpDown(bool up)
1127 int xlow, xhigh, ylow, yhigh;
1132 // try current cell first
1133 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1138 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1141 // try to find an inset that knows better then we
1143 // we found a cell that think something "below" us.
1145 if (par()->idxUp(idx()))
1148 if (par()->idxDown(idx()))
1153 // no such inset found, just take something "above"
1158 up ? formula()->ylow() : yo + 4,
1159 up ? yo - 4 : formula()->yhigh()
1163 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1164 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1169 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1171 cursor_type best_cursor;
1172 double best_dist = 1e10;
1174 MathIterator it = ibegin(formula()->par().nucleus());
1175 MathIterator et = iend(formula()->par().nucleus());
1176 for ( ; it != et; ++it) {
1177 // avoid invalid nesting hen selecting
1178 if (selection_ && !positionable(it.cursor(), Anchor_))
1180 int xo = it.position().xpos();
1181 int yo = it.position().ypos();
1182 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1183 ylow - 2 <= yo && yo <= yhigh + 2)
1185 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1186 if (d < best_dist) {
1188 best_cursor = it.cursor();
1192 if (best_dist < 1e10)
1193 Cursor_ = best_cursor;
1194 return best_dist < 1e10;
1198 bool MathCursor::idxLeft()
1200 return par()->idxLeft(idx(), pos());
1204 bool MathCursor::idxRight()
1206 return par()->idxRight(idx(), pos());
1210 bool MathCursor::interpret(string const & s)
1212 //lyxerr << "interpret 1: '" << s << "'\n";
1217 return interpret(s[0]);
1219 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1220 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1221 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1223 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1225 istringstream is(s.substr(5).c_str());
1227 n = std::max(1u, n);
1228 niceInsert(MathAtom(new MathCasesInset(n)));
1232 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1237 istringstream is(s.substr(6).c_str());
1238 is >> m >> n >> v_align >> h_align;
1239 m = std::max(1u, m);
1240 n = std::max(1u, n);
1242 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1246 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1248 istringstream is(s.substr(7).c_str());
1251 mathed_parse_cell(rep.from, from);
1252 mathed_parse_cell(rep.to, to);
1253 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1254 par()->replace(rep);
1258 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1259 MathArray ar = array();
1260 MathAtom t(createMathInset(s.substr(1)));
1261 t->asNestInset()->cell(0).swap(array());
1269 latexkeys const * l = in_word_set(s.substr(1));
1270 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1271 lastcode_ = static_cast<MathTextCodes>(l->id);
1275 niceInsert(createMathInset(s.substr(1)));
1280 bool MathCursor::script(bool up)
1284 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1285 prevAtom()->asScriptInset()->ensure(up);
1286 pushRight(prevAtom());
1289 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1290 nextAtom()->asScriptInset()->ensure(up);
1291 pushLeft(nextAtom());
1295 plainInsert(MathAtom(new MathScriptInset(up)));
1296 prevAtom()->asScriptInset()->ensure(up);
1297 pushRight(prevAtom());
1307 bool MathCursor::interpret(char c)
1310 // Removed super/subscript handling from here to ::script -MV
1313 if (inMacroMode()) {
1314 string name = macroName();
1316 if (name == "\\" && c == '#') {
1317 insert(c, LM_TC_TEX);
1321 if (name == "\\" && c == '\\') {
1323 interpret("\\backslash");
1327 if (name == "\\#" && '1' <= c && c <= '9') {
1328 insert(c, LM_TC_TEX);
1334 insert(c, LM_TC_TEX);
1339 insert(c, LM_TC_TEX);
1352 // fall through in the other cases
1355 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1356 // suppress direct insertion of two spaces in a row
1357 // the still allows typing '<space>a<space>' and deleting the 'a', but
1358 // it is better than nothing...
1359 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1361 insert(c, LM_TC_TEXTRM);
1366 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1367 prevAtom()->asSpaceInset()->incSpace();
1372 // if are at the very end, leave the formula
1373 return pos() != size();
1377 if (strchr("{}", c)) {
1378 insert(c, LM_TC_TEX);
1384 niceInsert(MathAtom(new MathBraceInset));
1392 if (strchr("#$%", c)) {
1393 insert(MathAtom(new MathSpecialCharInset(c)));
1394 lastcode_ = LM_TC_VAR;
1398 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1399 insert(c, LM_TC_VAR);
1403 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1404 insert(c, LM_TC_VAR);
1405 lastcode_ = LM_TC_VAR;
1410 insert(c, LM_TC_TEX);
1411 //bv->owner()->message(_("TeX mode"));
1415 // no special circumstances, so insert the character without any fuss
1416 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1417 lastcode_ = LM_TC_MIN;
1423 MathCursorPos MathCursor::normalAnchor() const
1425 if (Anchor_.size() < Cursor_.size()) {
1427 lyxerr << "unusual Anchor size\n";
1430 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1431 // use Anchor on the same level as Cursor
1432 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1433 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1434 // anchor is behind cursor -> move anchor behind the inset
1441 void MathCursor::stripFromLastEqualSign()
1443 // find position of last '=' in the array
1444 MathArray & ar = cursor().cell();
1445 MathArray::const_iterator et = ar.end();
1446 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1447 if ((*it)->getChar() == '=')
1450 // delete everything behind this position
1451 ar.erase(et - ar.begin(), ar.size());
1456 void MathCursor::setSelection(cursor_type const & where, size_type n)