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 int MathCursor::macroNamePos() const
622 for (int 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 for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i)
635 s += array().at(i)->getChar();
640 void MathCursor::selCopy()
644 theSelection.grab(*this);
650 void MathCursor::selCut()
654 theSelection.grab(*this);
655 theSelection.erase(*this);
658 theSelection.clear();
663 void MathCursor::selDel()
667 theSelection.erase(*this);
675 void MathCursor::selPaste()
678 theSelection.paste(*this);
679 //theSelection.grab(*this);
684 void MathCursor::selHandle(bool sel)
686 if (sel == selection_)
688 //theSelection.clear();
694 void MathCursor::selStart()
697 //theSelection.clear();
704 void MathCursor::selClear()
712 void MathCursor::selGet(MathArray & ar)
718 theSelection.grab(*this);
719 ar = theSelection.glue();
724 void MathCursor::drawSelection(Painter & pain) const
731 getSelection(i1, i2);
733 if (i1.idx_ == i2.idx_) {
734 MathXArray & c = i1.xcell();
735 int x1 = c.xo() + c.pos2x(i1.pos_);
736 int y1 = c.yo() - c.ascent();
737 int x2 = c.xo() + c.pos2x(i2.pos_);
738 int y2 = c.yo() + c.descent();
739 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
741 std::vector<MathInset::idx_type> indices
742 = i1.par_->idxBetween(i1.idx_, i2.idx_);
743 for (unsigned i = 0; i < indices.size(); ++i) {
744 MathXArray & c = i1.xcell(indices[i]);
746 int y1 = c.yo() - c.ascent();
747 int x2 = c.xo() + c.width();
748 int y2 = c.yo() + c.descent();
749 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
754 // draw anchor if different from selection boundary
755 MathCursorPos anc = Anchor_.back();
756 if (anc != i1 && anc != i2) {
757 MathXArray & c = anc.xcell();
758 int x = c.xo() + c.pos2x(anc.pos_);
759 int y1 = c.yo() - c.ascent();
760 int y2 = c.yo() + c.descent();
761 pain.line(x, y1, x, y2, LColor::math);
767 void MathCursor::handleFont(MathTextCodes t)
773 getSelection(i1, i2);
774 if (i1.idx_ == i2.idx_) {
775 MathArray & ar = i1.cell();
776 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
777 ar.at(pos)->handleFont(t);
780 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
784 void MathCursor::handleDelim(string const & l, string const & r)
786 handleNest(new MathDelimInset(l, r));
790 void MathCursor::handleNest(MathInset * p)
794 p->cell(0) = theSelection.glue();
796 insert(MathAtom(p)); // this invalidates p!
797 pushRight(prevAtom());
801 void MathCursor::getPos(int & x, int & y)
804 #warning This should probably take cellXOffset and cellYOffset into account
806 x = xarray().xo() + xarray().pos2x(pos());
811 MathInset * MathCursor::par() const
813 return cursor().par_;
817 InsetFormulaBase * MathCursor::formula()
823 MathCursor::idx_type MathCursor::idx() const
825 return cursor().idx_;
829 MathCursor::idx_type & MathCursor::idx()
831 return cursor().idx_;
835 MathCursor::pos_type MathCursor::pos() const
837 return cursor().pos_;
841 MathCursor::pos_type & MathCursor::pos()
843 return cursor().pos_;
847 bool MathCursor::inMacroMode() const
849 return macroNamePos() != -1;
853 bool MathCursor::selection() const
859 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
861 for (int i = Cursor_.size() - 1; i >= 0; --i) {
862 MathGridInset * p = Cursor_[i].par_->asGridInset();
864 idx = Cursor_[i].idx_;
872 void MathCursor::pullArg(bool goright)
875 MathArray a = array();
877 MathScriptInset const * p = par()->asScriptInset();
879 // special handling for scripts
880 const bool up = p->hasUp();
882 MathScriptInset * q = nextAtom()->asScriptInset();
886 array().insert(pos(), a);
892 array().insert(pos(), a);
899 void MathCursor::normalize()
903 MathIterator it = ibegin(formula()->par().nucleus());
904 MathIterator et = iend(formula()->par().nucleus());
905 for ( ; it != et; ++it)
906 if (it.par()->asBoxInset())
907 it.par()->asBoxInset()->rebreak();
910 if (idx() >= par()->nargs()) {
911 lyxerr << "this should not really happen - 1: "
912 << idx() << " " << par()->nargs() << "\n";
915 idx() = min(idx(), par()->nargs() - 1);
917 if (pos() > size()) {
918 lyxerr << "this should not really happen - 2: "
919 << pos() << " " << size() << " in idx: " << idx()
921 WriteStream wi(lyxerr, false);
926 pos() = min(pos(), size());
930 MathCursor::size_type MathCursor::size() const
932 return array().size();
936 MathCursor::col_type MathCursor::hullCol() const
938 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
942 MathCursor::row_type MathCursor::hullRow() const
944 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
948 bool MathCursor::hasPrevAtom() const
954 bool MathCursor::hasNextAtom() const
956 return pos() < size();
960 MathAtom const & MathCursor::prevAtom() const
962 lyx::Assert(pos() > 0);
963 return array().at(pos() - 1);
967 MathAtom & MathCursor::prevAtom()
969 lyx::Assert(pos() > 0);
970 return array().at(pos() - 1);
974 MathAtom const & MathCursor::nextAtom() const
976 lyx::Assert(pos() < size());
977 return array().at(pos());
981 MathAtom & MathCursor::nextAtom()
983 lyx::Assert(pos() < size());
984 return array().at(pos());
988 MathArray & MathCursor::array() const
990 static MathArray dummy;
992 if (idx() >= par()->nargs()) {
993 lyxerr << "############ idx_ " << idx() << " not valid\n";
997 if (Cursor_.size() == 0) {
998 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1002 return cursor().cell();
1006 MathXArray & MathCursor::xarray() const
1008 static MathXArray dummy;
1010 if (Cursor_.size() == 0) {
1011 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1015 return cursor().xcell();
1019 void MathCursor::idxNext()
1021 par()->idxNext(idx(), pos());
1025 void MathCursor::idxPrev()
1027 par()->idxPrev(idx(), pos());
1031 void MathCursor::splitCell()
1033 if (idx() + 1 == par()->nargs())
1035 MathArray ar = array();
1037 array().erase(pos(), size());
1040 array().insert(0, ar);
1044 void MathCursor::breakLine()
1046 // leave inner cells
1050 MathHullInset * p = formula()->par()->asHullInset();
1054 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1055 p->mutate(LM_OT_EQNARRAY);
1059 p->addRow(hullRow());
1062 const row_type r = hullRow();
1063 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1064 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1068 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1073 //void MathCursor::readLine(MathArray & ar) const
1075 // idx_type base = row() * par()->ncols();
1076 // for (idx_type off = 0; off < par()->ncols(); ++off)
1077 // ar.push_back(par()->cell(base + off));
1081 char MathCursor::valign() const
1084 MathGridInset * p = enclosingGrid(idx);
1085 return p ? p->valign() : '\0';
1089 char MathCursor::halign() const
1092 MathGridInset * p = enclosingGrid(idx);
1093 return p ? p->halign(idx % p->ncols()) : '\0';
1097 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1099 MathCursorPos anc = normalAnchor();
1100 if (anc < cursor()) {
1110 MathCursorPos & MathCursor::cursor()
1112 lyx::Assert(Cursor_.size());
1113 return Cursor_.back();
1117 MathCursorPos const & MathCursor::cursor() const
1119 lyx::Assert(Cursor_.size());
1120 return Cursor_.back();
1124 bool MathCursor::goUpDown(bool up)
1126 int xlow, xhigh, ylow, yhigh;
1131 // try current cell first
1132 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1137 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1140 // try to find an inset that knows better then we
1142 // we found a cell that think something "below" us.
1144 if (par()->idxUp(idx()))
1147 if (par()->idxDown(idx()))
1152 // no such inset found, just take something "above"
1157 up ? formula()->ylow() : yo + 4,
1158 up ? yo - 4 : formula()->yhigh()
1162 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1163 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1168 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1170 cursor_type best_cursor;
1171 double best_dist = 1e10;
1173 MathIterator it = ibegin(formula()->par().nucleus());
1174 MathIterator et = iend(formula()->par().nucleus());
1175 for ( ; it != et; ++it) {
1176 // avoid invalid nesting hen selecting
1177 if (selection_ && !positionable(it.cursor(), Anchor_))
1179 int xo = it.position().xpos();
1180 int yo = it.position().ypos();
1181 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1182 ylow - 2 <= yo && yo <= yhigh + 2)
1184 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1185 if (d < best_dist) {
1187 best_cursor = it.cursor();
1191 if (best_dist < 1e10)
1192 Cursor_ = best_cursor;
1193 return best_dist < 1e10;
1197 bool MathCursor::idxLeft()
1199 return par()->idxLeft(idx(), pos());
1203 bool MathCursor::idxRight()
1205 return par()->idxRight(idx(), pos());
1209 bool MathCursor::interpret(string const & s)
1211 //lyxerr << "interpret 1: '" << s << "'\n";
1216 return interpret(s[0]);
1218 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1219 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1220 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1222 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1224 istringstream is(s.substr(5).c_str());
1226 n = std::max(1u, n);
1227 niceInsert(MathAtom(new MathCasesInset(n)));
1231 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1236 istringstream is(s.substr(6).c_str());
1237 is >> m >> n >> v_align >> h_align;
1238 m = std::max(1u, m);
1239 n = std::max(1u, n);
1241 niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
1245 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1247 istringstream is(s.substr(7).c_str());
1250 mathed_parse_cell(rep.from, from);
1251 mathed_parse_cell(rep.to, to);
1252 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1253 par()->replace(rep);
1257 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1258 MathArray ar = array();
1259 MathAtom t(createMathInset(s.substr(1)));
1260 t->asNestInset()->cell(0).swap(array());
1268 latexkeys const * l = in_word_set(s.substr(1));
1269 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1270 lastcode_ = static_cast<MathTextCodes>(l->id);
1274 niceInsert(createMathInset(s.substr(1)));
1279 bool MathCursor::script(bool up)
1283 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1284 prevAtom()->asScriptInset()->ensure(up);
1285 pushRight(prevAtom());
1288 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1289 nextAtom()->asScriptInset()->ensure(up);
1290 pushLeft(nextAtom());
1294 plainInsert(MathAtom(new MathScriptInset(up)));
1295 prevAtom()->asScriptInset()->ensure(up);
1296 pushRight(prevAtom());
1306 bool MathCursor::interpret(char c)
1309 // Removed super/subscript handling from here to ::script -MV
1312 if (inMacroMode()) {
1313 string name = macroName();
1315 if (name == "\\" && c == '#') {
1316 insert(c, LM_TC_TEX);
1320 if (name == "\\" && c == '\\') {
1322 interpret("\\backslash");
1326 if (name == "\\#" && '1' <= c && c <= '9') {
1327 insert(c, LM_TC_TEX);
1333 insert(c, LM_TC_TEX);
1338 insert(c, LM_TC_TEX);
1351 // fall through in the other cases
1354 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1355 // suppress direct insertion of two spaces in a row
1356 // the still allows typing '<space>a<space>' and deleting the 'a', but
1357 // it is better than nothing...
1358 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1360 insert(c, LM_TC_TEXTRM);
1365 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1366 prevAtom()->asSpaceInset()->incSpace();
1371 // if are at the very end, leave the formula
1372 return pos() != size();
1376 if (strchr("{}", c)) {
1377 insert(c, LM_TC_TEX);
1383 niceInsert(MathAtom(new MathBraceInset));
1391 if (strchr("#$%", c)) {
1392 insert(MathAtom(new MathSpecialCharInset(c)));
1393 lastcode_ = LM_TC_VAR;
1397 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1398 insert(c, LM_TC_VAR);
1402 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1403 insert(c, LM_TC_VAR);
1404 lastcode_ = LM_TC_VAR;
1409 insert(c, LM_TC_TEX);
1410 //bv->owner()->message(_("TeX mode"));
1414 // no special circumstances, so insert the character without any fuss
1415 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1416 lastcode_ = LM_TC_MIN;
1422 MathCursorPos MathCursor::normalAnchor() const
1424 if (Anchor_.size() < Cursor_.size()) {
1426 lyxerr << "unusual Anchor size\n";
1429 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1430 // use Anchor on the same level as Cursor
1431 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1432 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1433 // anchor is behind cursor -> move anchor behind the inset
1440 void MathCursor::stripFromLastEqualSign()
1442 // find position of last '=' in the array
1443 MathArray & ar = cursor().cell();
1444 MathArray::const_iterator et = ar.end();
1445 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1446 if ((*it)->getChar() == '=')
1449 // delete everything behind this position
1450 ar.erase(et - ar.begin(), ar.size());
1455 void MathCursor::setSelection(cursor_type const & where, size_type n)