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_macrotemplate.h"
42 #include "math_mathmlstream.h"
43 #include "math_parser.h"
44 #include "math_replace.h"
45 #include "math_scriptinset.h"
46 #include "math_spaceinset.h"
47 #include "math_specialcharinset.h"
48 #include "math_support.h"
60 using std::ostringstream;
66 typedef MathInset::col_type col_type;
67 typedef MathInset::row_type row_type;
68 typedef MathInset::idx_type idx_type;
74 void region(MathCursorPos const & i1, MathCursorPos const & i2,
75 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
77 MathInset * p = i1.par_;
88 void grab(MathCursor const & cursor)
92 cursor.getSelection(i1, i2);
93 // shouldn'tt we assert on i1.par_ == i2.par_?
94 if (i1.idx_ == i2.idx_) {
95 data_ = MathGridInset(1, 1);
96 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
100 region(i1, i2, r1, r2, c1, c2);
101 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
102 for (row_type row = 0; row < data_.nrows(); ++row)
103 for (col_type col = 0; col < data_.ncols(); ++col) {
104 idx_type i = i1.par_->index(row + r1, col + c1);
105 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
110 void erase(MathCursor & cursor)
114 cursor.getSelection(i1, i2);
115 if (i1.idx_ == i2.idx_)
116 i1.cell().erase(i1.pos_, i2.pos_);
118 MathInset * p = i1.par_;
121 region(i1, i2, r1, r2, c1, c2);
122 for (row_type row = r1; row <= r2; ++row)
123 for (col_type col = c1; col <= c2; ++col)
124 p->cell(p->index(row, col)).erase();
126 cursor.cursor() = i1;
129 void paste(MathCursor & cursor) const
131 if (data_.nargs() == 1) {
132 // single cell/part of cell
133 cursor.insert(data_.cell(0));
137 MathGridInset * p = cursor.enclosingGrid(idx);
138 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
139 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
140 for (row_type row = 0; row < numrows; ++row)
141 for (col_type col = 0; col < numcols; ++col) {
142 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
143 p->cell(i).push_back(data_.cell(data_.index(row, col)));
148 // glues selection to one cell
149 MathArray glue() const
152 for (unsigned i = 0; i < data_.nargs(); ++i)
153 ar.push_back(data_.cell(i));
159 data_ = MathGridInset(1, 1);
166 Selection theSelection;
173 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
174 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
176 left ? first() : last();
180 void MathCursor::push(MathAtom & t)
182 Cursor_.push_back(MathCursorPos(t.nucleus()));
186 void MathCursor::pushLeft(MathAtom & t)
188 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
190 t->idxFirst(idx(), pos());
194 void MathCursor::pushRight(MathAtom & t)
196 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
199 t->idxLast(idx(), pos());
203 bool MathCursor::popLeft()
205 //cerr << "Leaving atom to the left\n";
206 if (Cursor_.size() <= 1)
213 bool MathCursor::popRight()
215 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
216 if (Cursor_.size() <= 1)
226 void MathCursor::dump(char const * what) const
228 lyxerr << "MC: " << what << "\n";
229 lyxerr << " Cursor: " << Cursor_.size() << "\n";
230 for (unsigned i = 0; i < Cursor_.size(); ++i)
231 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
232 lyxerr << " Anchor: " << Anchor_.size() << "\n";
233 for (unsigned i = 0; i < Anchor_.size(); ++i)
234 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
235 lyxerr << " sel: " << selection_ << "\n";
238 void MathCursor::dump(char const *) const {}
242 UpdatableInset * MathCursor::asHyperActiveInset() const
244 return par()->asHyperActiveInset();
248 bool MathCursor::isInside(MathInset const * p) const
250 for (unsigned i = 0; i < Cursor_.size(); ++i)
251 if (Cursor_[i].par_ == p)
257 bool MathCursor::openable(MathAtom const & t, bool sel) const
259 if (t->isHyperActive())
265 if (t->asScriptInset())
269 // we can't move into anything new during selection
270 if (Cursor_.size() == Anchor_.size())
272 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
279 bool MathCursor::posLeft()
288 bool MathCursor::posRight()
297 bool MathCursor::left(bool sel)
302 lastcode_ = LM_TC_MIN;
306 lastcode_ = LM_TC_MIN;
308 if (hasPrevAtom() && openable(prevAtom(), sel)) {
309 if (prevAtom()->isHyperActive()) {
310 lyxerr << "entering hyperactive inset\n";
312 pushRight(prevAtom());
316 return posLeft() || idxLeft() || popLeft() || selection_;
320 bool MathCursor::right(bool sel)
325 lastcode_ = LM_TC_MIN;
329 lastcode_ = LM_TC_MIN;
331 if (hasNextAtom() && openable(nextAtom(), sel)) {
332 if (nextAtom()->isHyperActive()) {
333 lyxerr << "entering hyperactive inset\n";
336 nextAtom()->edit(formula()->view(), x, y, 0);
338 pushLeft(nextAtom());
342 return posRight() || idxRight() || popRight() || selection_;
346 void MathCursor::first()
349 pushLeft(formula_->par());
353 void MathCursor::last()
360 bool positionable(MathCursor::cursor_type const & cursor,
361 MathCursor::cursor_type const & anchor)
363 // avoid deeper nested insets when selecting
364 if (cursor.size() > anchor.size())
367 // anchor might be deeper, should have same path then
368 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
369 if (cursor[i].par_ != anchor[i].par_)
372 // position should be ok.
377 void MathCursor::setPos(int x, int y)
380 bool res = bruteFind(x, y,
381 formula()->xlow(), formula()->xhigh(),
382 formula()->ylow(), formula()->yhigh());
384 // this ccan happen on creation of "math-display"
393 void MathCursor::home(bool sel)
398 lastcode_ = LM_TC_MIN;
399 if (!par()->idxHome(idx(), pos()))
405 void MathCursor::end(bool sel)
410 lastcode_ = LM_TC_MIN;
411 if (!par()->idxEnd(idx(), pos()))
417 void MathCursor::plainErase()
419 array().erase(pos());
423 void MathCursor::markInsert()
425 //lyxerr << "inserting mark\n";
426 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
430 void MathCursor::markErase()
432 //lyxerr << "deleting mark\n";
433 array().erase(pos());
437 void MathCursor::plainInsert(MathAtom const & t)
439 array().insert(pos(), t);
444 void MathCursor::insert(char c, MathTextCodes t)
446 //lyxerr << "inserting '" << c << "'\n";
447 plainInsert(MathAtom(new MathCharInset(c, t)));
451 void MathCursor::insert(char c)
453 insert(c, lastcode_);
457 void MathCursor::insert(MathAtom const & t)
472 void MathCursor::niceInsert(MathAtom const & t)
475 insert(t); // inserting invalidates the pointer!
476 MathAtom const & p = prevAtom();
479 right(); // do not push for e.g. MathSymbolInset
485 void MathCursor::insert(MathArray const & ar)
491 array().insert(pos(), ar);
496 void MathCursor::paste(MathArray const & ar)
500 array().insert(pos(), ar);
505 void MathCursor::backspace()
517 MathScriptInset * p = prevAtom()->asScriptInset();
519 p->removeScript(p->hasUp());
520 // Don't delete if there is anything left
521 if (p->hasUp() || p->hasDown())
530 void MathCursor::erase()
540 // delete empty cells if necessary
541 if (array().empty()) {
544 par()->idxDelete(idx(), popit, removeit);
545 if (popit && popLeft() && removeit)
553 MathScriptInset * p = nextAtom()->asScriptInset();
555 p->removeScript(p->hasUp());
556 // Don't delete if there is anything left
557 if (p->hasUp() || p->hasDown())
565 void MathCursor::delLine()
574 if (par()->nrows() > 1) {
575 // grid are the only things with more than one row...
576 lyx::Assert(par()->asGridInset());
577 par()->asGridInset()->delRow(hullRow());
580 if (idx() >= par()->nargs())
581 idx() = par()->nargs() - 1;
588 bool MathCursor::up(bool sel)
593 cursor_type save = Cursor_;
601 bool MathCursor::down(bool sel)
606 cursor_type save = Cursor_;
614 bool MathCursor::toggleLimits()
618 MathScriptInset * t = nextAtom()->asScriptInset();
621 int old = t->limits();
622 t->limits(old < 0 ? 1 : -1);
623 return old != t->limits();
627 void MathCursor::macroModeClose()
629 string s = macroName();
631 size_type old = pos();
633 array().erase(pos(), old);
639 MathInset::difference_type MathCursor::macroNamePos() const
641 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
642 MathAtom & p = array().at(i);
643 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
650 string MathCursor::macroName() const
653 MathInset::difference_type i = macroNamePos();
654 for (; i >= 0 && i < int(pos()); ++i)
655 s += array().at(i)->getChar();
660 void MathCursor::selCopy()
664 theSelection.grab(*this);
670 void MathCursor::selCut()
674 theSelection.grab(*this);
675 theSelection.erase(*this);
678 theSelection.clear();
683 void MathCursor::selDel()
687 theSelection.erase(*this);
695 void MathCursor::selPaste()
698 theSelection.paste(*this);
699 //theSelection.grab(*this);
704 void MathCursor::selHandle(bool sel)
706 if (sel == selection_)
708 //theSelection.clear();
714 void MathCursor::selStart()
717 //theSelection.clear();
724 void MathCursor::selClear()
732 void MathCursor::selGet(MathArray & ar)
738 theSelection.grab(*this);
739 ar = theSelection.glue();
744 void MathCursor::drawSelection(Painter & pain) const
751 getSelection(i1, i2);
753 if (i1.idx_ == i2.idx_) {
754 MathXArray & c = i1.xcell();
755 int x1 = c.xo() + c.pos2x(i1.pos_);
756 int y1 = c.yo() - c.ascent();
757 int x2 = c.xo() + c.pos2x(i2.pos_);
758 int y2 = c.yo() + c.descent();
759 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
761 vector<MathInset::idx_type> indices
762 = i1.par_->idxBetween(i1.idx_, i2.idx_);
763 for (unsigned i = 0; i < indices.size(); ++i) {
764 MathXArray & c = i1.xcell(indices[i]);
766 int y1 = c.yo() - c.ascent();
767 int x2 = c.xo() + c.width();
768 int y2 = c.yo() + c.descent();
769 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
774 // draw anchor if different from selection boundary
775 MathCursorPos anc = Anchor_.back();
776 if (anc != i1 && anc != i2) {
777 MathXArray & c = anc.xcell();
778 int x = c.xo() + c.pos2x(anc.pos_);
779 int y1 = c.yo() - c.ascent();
780 int y2 = c.yo() + c.descent();
781 pain.line(x, y1, x, y2, LColor::math);
787 void MathCursor::handleFont(MathTextCodes t)
793 getSelection(i1, i2);
794 if (i1.idx_ == i2.idx_) {
795 MathArray & ar = i1.cell();
796 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
797 ar.at(pos)->handleFont(t);
800 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
804 void MathCursor::handleDelim(string const & l, string const & r)
806 handleNest(new MathDelimInset(l, r));
810 void MathCursor::handleNest(MathInset * p)
814 p->cell(0) = theSelection.glue();
816 insert(MathAtom(p)); // this invalidates p!
817 pushRight(prevAtom());
821 void MathCursor::getPos(int & x, int & y)
824 #warning This should probably take cellXOffset and cellYOffset into account
826 x = xarray().xo() + xarray().pos2x(pos());
831 MathInset * MathCursor::par() const
833 return cursor().par_;
837 InsetFormulaBase * MathCursor::formula()
843 MathCursor::idx_type MathCursor::idx() const
845 return cursor().idx_;
849 MathCursor::idx_type & MathCursor::idx()
851 return cursor().idx_;
855 MathCursor::pos_type MathCursor::pos() const
857 return cursor().pos_;
861 MathCursor::pos_type & MathCursor::pos()
863 return cursor().pos_;
867 bool MathCursor::inMacroMode() const
869 return macroNamePos() != -1;
873 bool MathCursor::inMacroArgMode() const
875 return pos() > 0 && prevAtom()->getChar() == '#';
879 bool MathCursor::selection() const
885 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
887 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
888 MathGridInset * p = Cursor_[i].par_->asGridInset();
890 idx = Cursor_[i].idx_;
898 void MathCursor::pullArg(bool goright)
901 MathArray a = array();
903 MathScriptInset const * p = par()->asScriptInset();
905 // special handling for scripts
906 const bool up = p->hasUp();
908 MathScriptInset * q = nextAtom()->asScriptInset();
912 array().insert(pos(), a);
918 array().insert(pos(), a);
922 formula()->mutateToText();
927 void MathCursor::touch()
929 cursor_type::const_iterator it = Cursor_.begin();
930 cursor_type::const_iterator et = Cursor_.end();
931 for ( ; it != et; ++it)
936 void MathCursor::normalize()
941 MathIterator it = ibegin(formula()->par().nucleus());
942 MathIterator et = iend(formula()->par().nucleus());
943 for (; it != et; ++it)
944 if (it.par()->asBoxInset())
945 it.par()->asBoxInset()->rebreak();
949 if (idx() >= par()->nargs()) {
950 lyxerr << "this should not really happen - 1: "
951 << idx() << " " << par()->nargs() << "\n";
954 idx() = min(idx(), par()->nargs() - 1);
956 if (pos() > size()) {
957 lyxerr << "this should not really happen - 2: "
958 << pos() << " " << size() << " in idx: " << idx()
960 WriteStream wi(lyxerr, false);
965 pos() = min(pos(), size());
967 // remove empty scripts if possible
968 for (pos_type i = 0; i < size(); ++i) {
969 MathScriptInset * p = array().at(i)->asScriptInset();
971 p->removeEmptyScripts();
977 // fix again position
978 pos() = min(pos(), size());
982 MathCursor::size_type MathCursor::size() const
984 return array().size();
988 MathCursor::col_type MathCursor::hullCol() const
990 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
994 MathCursor::row_type MathCursor::hullRow() const
996 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1000 bool MathCursor::hasPrevAtom() const
1006 bool MathCursor::hasNextAtom() const
1008 return pos() < size();
1012 MathAtom const & MathCursor::prevAtom() const
1014 lyx::Assert(pos() > 0);
1015 return array().at(pos() - 1);
1019 MathAtom & MathCursor::prevAtom()
1021 lyx::Assert(pos() > 0);
1022 return array().at(pos() - 1);
1026 MathAtom const & MathCursor::nextAtom() const
1028 lyx::Assert(pos() < size());
1029 return array().at(pos());
1033 MathAtom & MathCursor::nextAtom()
1035 lyx::Assert(pos() < size());
1036 return array().at(pos());
1040 MathArray & MathCursor::array() const
1042 static MathArray dummy;
1044 if (idx() >= par()->nargs()) {
1045 lyxerr << "############ idx_ " << idx() << " not valid\n";
1049 if (Cursor_.size() == 0) {
1050 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1054 return cursor().cell();
1058 MathXArray & MathCursor::xarray() const
1060 static MathXArray dummy;
1062 if (Cursor_.size() == 0) {
1063 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1067 return cursor().xcell();
1071 void MathCursor::idxNext()
1073 par()->idxNext(idx(), pos());
1077 void MathCursor::idxPrev()
1079 par()->idxPrev(idx(), pos());
1083 void MathCursor::splitCell()
1085 if (idx() + 1 == par()->nargs())
1087 MathArray ar = array();
1089 array().erase(pos(), size());
1092 array().insert(0, ar);
1096 void MathCursor::breakLine()
1098 // leave inner cells
1102 MathHullInset * p = formula()->par()->asHullInset();
1106 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1107 p->mutate(LM_OT_EQNARRAY);
1111 p->addRow(hullRow());
1114 const row_type r = hullRow();
1115 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1116 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1120 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1125 //void MathCursor::readLine(MathArray & ar) const
1127 // idx_type base = row() * par()->ncols();
1128 // for (idx_type off = 0; off < par()->ncols(); ++off)
1129 // ar.push_back(par()->cell(base + off));
1133 char MathCursor::valign() const
1136 MathGridInset * p = enclosingGrid(idx);
1137 return p ? p->valign() : '\0';
1141 char MathCursor::halign() const
1144 MathGridInset * p = enclosingGrid(idx);
1145 return p ? p->halign(idx % p->ncols()) : '\0';
1149 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1151 MathCursorPos anc = normalAnchor();
1152 if (anc < cursor()) {
1162 MathCursorPos & MathCursor::cursor()
1164 lyx::Assert(Cursor_.size());
1165 return Cursor_.back();
1169 MathCursorPos const & MathCursor::cursor() const
1171 lyx::Assert(Cursor_.size());
1172 return Cursor_.back();
1176 bool MathCursor::goUpDown(bool up)
1178 int xlow, xhigh, ylow, yhigh;
1183 // try current cell first
1184 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1189 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1192 // try to find an inset that knows better then we
1194 // we found a cell that think something "below" us.
1196 if (par()->idxUp(idx()))
1199 if (par()->idxDown(idx()))
1204 // no such inset found, just take something "above"
1209 up ? formula()->ylow() : yo + 4,
1210 up ? yo - 4 : formula()->yhigh()
1214 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1215 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1220 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1222 cursor_type best_cursor;
1223 double best_dist = 1e10;
1225 MathIterator it = ibegin(formula()->par().nucleus());
1226 MathIterator et = iend(formula()->par().nucleus());
1228 // avoid invalid nesting when selecting
1229 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1230 MathCursorPos const & top = it.position();
1231 int xo = top.xpos();
1232 int yo = top.ypos();
1233 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1234 ylow - 2 <= yo && yo <= yhigh + 2)
1236 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1237 if (d < best_dist) {
1239 best_cursor = it.cursor();
1249 if (best_dist < 1e10)
1250 Cursor_ = best_cursor;
1251 return best_dist < 1e10;
1255 bool MathCursor::idxLeft()
1257 return par()->idxLeft(idx(), pos());
1261 bool MathCursor::idxRight()
1263 return par()->idxRight(idx(), pos());
1267 bool MathCursor::interpret(string const & s)
1269 //lyxerr << "interpret 1: '" << s << "'\n";
1274 return interpret(s[0]);
1276 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1277 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1278 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1280 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1282 istringstream is(s.substr(5).c_str());
1285 niceInsert(MathAtom(new MathCasesInset(n)));
1289 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1294 istringstream is(s.substr(6).c_str());
1295 is >> m >> n >> v_align >> h_align;
1299 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1303 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1305 istringstream is(s.substr(7).c_str());
1308 mathed_parse_cell(rep.from, from);
1309 mathed_parse_cell(rep.to, to);
1310 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1311 par()->replace(rep);
1315 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1316 MathArray ar = array();
1317 MathAtom t(createMathInset(s.substr(1)));
1318 t->asNestInset()->cell(0).swap(array());
1326 latexkeys const * l = in_word_set(s.substr(1));
1327 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1328 lastcode_ = static_cast<MathTextCodes>(l->id);
1332 // prevent entering of recursive macros
1333 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1334 && formula()->getInsetName() == s.substr(1))
1336 lyxerr << "can't enter recursive macro\n";
1340 niceInsert(createMathInset(s.substr(1)));
1345 bool MathCursor::script(bool up)
1349 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1350 prevAtom()->asScriptInset()->ensure(up);
1351 pushRight(prevAtom());
1354 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1355 nextAtom()->asScriptInset()->ensure(up);
1356 pushLeft(nextAtom());
1360 plainInsert(MathAtom(new MathScriptInset(up)));
1361 prevAtom()->asScriptInset()->ensure(up);
1362 pushRight(prevAtom());
1372 bool MathCursor::interpret(char c)
1374 if (inMacroArgMode()) {
1378 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1379 if (p && 1 <= n && n <= p->numargs())
1380 insert(MathAtom(new MathMacroArgument(c - '0')));
1382 insert(MathAtom(new MathSpecialCharInset('#')));
1383 interpret(c); // try again
1389 if (inMacroMode()) {
1390 string name = macroName();
1392 if (name == "\\" && c == '\\') {
1394 interpret("\\backslash");
1399 insert(c, LM_TC_TEX);
1404 insert(c, LM_TC_TEX);
1412 insert(c, LM_TC_TEX);
1414 insert(c, lastcode_);
1423 // fall through in the other cases
1426 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1427 // suppress direct insertion of two spaces in a row
1428 // the still allows typing '<space>a<space>' and deleting the 'a', but
1429 // it is better than nothing...
1430 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1432 insert(c, LM_TC_TEXTRM);
1437 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1438 prevAtom()->asSpaceInset()->incSpace();
1443 // if are at the very end, leave the formula
1444 return pos() != size();
1448 insert(c, LM_TC_TEX);
1453 if (c == '{' || c == '}', c)) {
1454 insert(c, LM_TC_TEX);
1460 niceInsert(MathAtom(new MathBraceInset));
1468 if (c == '$' || c == '%') {
1469 insert(MathAtom(new MathSpecialCharInset(c)));
1470 lastcode_ = LM_TC_VAR;
1474 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1475 insert(c, LM_TC_VAR);
1479 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1480 insert(c, LM_TC_VAR);
1481 lastcode_ = LM_TC_VAR;
1486 insert(c, LM_TC_TEX);
1487 //bv->owner()->message(_("TeX mode"));
1491 // no special circumstances, so insert the character without any fuss
1492 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1493 lastcode_ = LM_TC_MIN;
1499 MathCursorPos MathCursor::normalAnchor() const
1501 if (Anchor_.size() < Cursor_.size()) {
1503 lyxerr << "unusual Anchor size\n";
1506 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1507 // use Anchor on the same level as Cursor
1508 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1509 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1510 // anchor is behind cursor -> move anchor behind the inset
1517 void MathCursor::stripFromLastEqualSign()
1519 // find position of last '=' in the array
1520 MathArray & ar = cursor().cell();
1521 MathArray::const_iterator et = ar.end();
1522 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1523 if ((*it)->getChar() == '=')
1526 // delete everything behind this position
1527 ar.erase(et - ar.begin(), ar.size());
1532 void MathCursor::setSelection(cursor_type const & where, size_type n)
1541 string MathCursor::info() const
1545 prevAtom()->infoize(os);