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"
61 using std::ostringstream;
67 typedef MathInset::col_type col_type;
68 typedef MathInset::row_type row_type;
69 typedef MathInset::idx_type idx_type;
75 void region(MathCursorPos const & i1, MathCursorPos const & i2,
76 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
78 MathInset * p = i1.par_;
89 void grab(MathCursor const & cursor)
93 cursor.getSelection(i1, i2);
94 // shouldn'tt we assert on i1.par_ == i2.par_?
95 if (i1.idx_ == i2.idx_) {
96 data_ = MathGridInset(1, 1);
97 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
101 region(i1, i2, r1, r2, c1, c2);
102 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
103 for (row_type row = 0; row < data_.nrows(); ++row)
104 for (col_type col = 0; col < data_.ncols(); ++col) {
105 idx_type i = i1.par_->index(row + r1, col + c1);
106 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
111 void erase(MathCursor & cursor)
115 cursor.getSelection(i1, i2);
116 if (i1.idx_ == i2.idx_)
117 i1.cell().erase(i1.pos_, i2.pos_);
119 MathInset * p = i1.par_;
122 region(i1, i2, r1, r2, c1, c2);
123 for (row_type row = r1; row <= r2; ++row)
124 for (col_type col = c1; col <= c2; ++col)
125 p->cell(p->index(row, col)).erase();
127 cursor.cursor() = i1;
130 void paste(MathCursor & cursor) const
132 if (data_.nargs() == 1) {
133 // single cell/part of cell
134 cursor.insert(data_.cell(0));
138 MathGridInset * p = cursor.enclosingGrid(idx);
139 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
140 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
141 for (row_type row = 0; row < numrows; ++row)
142 for (col_type col = 0; col < numcols; ++col) {
143 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
144 p->cell(i).push_back(data_.cell(data_.index(row, col)));
149 // glues selection to one cell
150 MathArray glue() const
153 for (unsigned i = 0; i < data_.nargs(); ++i)
154 ar.push_back(data_.cell(i));
160 data_ = MathGridInset(1, 1);
167 Selection theSelection;
174 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
175 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
177 left ? first() : last();
181 void MathCursor::push(MathAtom & t)
183 Cursor_.push_back(MathCursorPos(t.nucleus()));
187 void MathCursor::pushLeft(MathAtom & t)
189 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
191 t->idxFirst(idx(), pos());
195 void MathCursor::pushRight(MathAtom & t)
197 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
200 t->idxLast(idx(), pos());
204 bool MathCursor::popLeft()
206 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
207 if (Cursor_.size() <= 1)
209 if (par()->asScriptInset()) {
210 par()->asScriptInset()->removeEmptyScripts();
211 if (par()->asScriptInset()->empty())
219 bool MathCursor::popRight()
221 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
222 if (Cursor_.size() <= 1)
224 if (par()->asScriptInset()) {
225 par()->asScriptInset()->removeEmptyScripts();
226 if (par()->asScriptInset()->empty())
237 void MathCursor::dump(char const * what) const
239 lyxerr << "MC: " << what << "\n";
240 lyxerr << " Cursor: " << Cursor_.size() << "\n";
241 for (unsigned i = 0; i < Cursor_.size(); ++i)
242 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
243 lyxerr << " Anchor: " << Anchor_.size() << "\n";
244 for (unsigned i = 0; i < Anchor_.size(); ++i)
245 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
246 lyxerr << " sel: " << selection_ << "\n";
249 void MathCursor::dump(char const *) const {}
253 UpdatableInset * MathCursor::asHyperActiveInset() const
255 return par()->asHyperActiveInset();
259 bool MathCursor::isInside(MathInset const * p) const
261 for (unsigned i = 0; i < Cursor_.size(); ++i)
262 if (Cursor_[i].par_ == p)
268 bool MathCursor::openable(MathAtom const & t, bool sel) const
270 if (t->isHyperActive())
276 if (t->asScriptInset())
280 // we can't move into anything new during selection
281 if (Cursor_.size() == Anchor_.size())
283 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
290 bool MathCursor::posLeft()
299 bool MathCursor::posRight()
308 bool MathCursor::left(bool sel)
313 lastcode_ = LM_TC_MIN;
317 lastcode_ = LM_TC_MIN;
319 if (hasPrevAtom() && openable(prevAtom(), sel)) {
320 if (prevAtom()->isHyperActive()) {
321 lyxerr << "entering hyperactive inset\n";
323 pushRight(prevAtom());
327 return posLeft() || idxLeft() || popLeft() || selection_;
331 bool MathCursor::right(bool sel)
336 lastcode_ = LM_TC_MIN;
340 lastcode_ = LM_TC_MIN;
342 if (hasNextAtom() && openable(nextAtom(), sel)) {
343 if (nextAtom()->isHyperActive()) {
344 lyxerr << "entering hyperactive inset\n";
347 nextAtom()->edit(formula()->view(), x, y, 0);
349 pushLeft(nextAtom());
353 return posRight() || idxRight() || popRight() || selection_;
357 void MathCursor::first()
360 pushLeft(formula_->par());
364 void MathCursor::last()
371 bool positionable(MathCursor::cursor_type const & cursor,
372 MathCursor::cursor_type const & anchor)
374 // avoid deeper nested insets when selecting
375 if (cursor.size() > anchor.size())
378 // anchor might be deeper, should have same path then
379 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
380 if (cursor[i].par_ != anchor[i].par_)
383 // position should be ok.
388 void MathCursor::setPos(int x, int y)
391 bool res = bruteFind(x, y,
392 formula()->xlow(), formula()->xhigh(),
393 formula()->ylow(), formula()->yhigh());
395 // this ccan happen on creation of "math-display"
404 void MathCursor::home(bool sel)
409 lastcode_ = LM_TC_MIN;
410 if (!par()->idxHome(idx(), pos()))
416 void MathCursor::end(bool sel)
421 lastcode_ = LM_TC_MIN;
422 if (!par()->idxEnd(idx(), pos()))
428 void MathCursor::plainErase()
430 array().erase(pos());
434 void MathCursor::markInsert()
436 //lyxerr << "inserting mark\n";
437 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
441 void MathCursor::markErase()
443 //lyxerr << "deleting mark\n";
444 array().erase(pos());
448 void MathCursor::plainInsert(MathAtom const & t)
450 array().insert(pos(), t);
455 void MathCursor::insert(char c, MathTextCodes t)
457 //lyxerr << "inserting '" << c << "'\n";
458 plainInsert(MathAtom(new MathCharInset(c, t)));
462 void MathCursor::insert(char c)
464 insert(c, lastcode_);
468 void MathCursor::insert(MathAtom const & t)
483 void MathCursor::niceInsert(MathAtom const & t)
486 insert(t); // inserting invalidates the pointer!
487 MathAtom const & p = prevAtom();
490 right(); // do not push for e.g. MathSymbolInset
496 void MathCursor::insert(MathArray const & ar)
502 array().insert(pos(), ar);
507 void MathCursor::paste(MathArray const & ar)
511 array().insert(pos(), ar);
516 void MathCursor::backspace()
528 MathScriptInset * p = prevAtom()->asScriptInset();
530 p->removeScript(p->hasUp());
531 // Don't delete if there is anything left
532 if (p->hasUp() || p->hasDown())
541 void MathCursor::erase()
551 // delete empty cells if necessary
552 if (array().empty()) {
555 par()->idxDelete(idx(), popit, removeit);
556 if (popit && popLeft() && removeit)
564 MathScriptInset * p = nextAtom()->asScriptInset();
566 p->removeScript(p->hasUp());
567 // Don't delete if there is anything left
568 if (p->hasUp() || p->hasDown())
576 void MathCursor::delLine()
585 if (par()->nrows() > 1) {
586 // grid are the only things with more than one row...
587 lyx::Assert(par()->asGridInset());
588 par()->asGridInset()->delRow(hullRow());
591 if (idx() >= par()->nargs())
592 idx() = par()->nargs() - 1;
599 bool MathCursor::up(bool sel)
604 cursor_type save = Cursor_;
612 bool MathCursor::down(bool sel)
617 cursor_type save = Cursor_;
625 bool MathCursor::toggleLimits()
629 MathScriptInset * t = nextAtom()->asScriptInset();
632 int old = t->limits();
633 t->limits(old < 0 ? 1 : -1);
634 return old != t->limits();
638 void MathCursor::macroModeClose()
640 string s = macroName();
642 size_type old = pos();
644 array().erase(pos(), old);
650 MathInset::difference_type MathCursor::macroNamePos() const
652 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
653 MathAtom & p = array().at(i);
654 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
661 string MathCursor::macroName() const
664 MathInset::difference_type i = macroNamePos();
665 for (; i >= 0 && i < int(pos()); ++i)
666 s += array().at(i)->getChar();
671 void MathCursor::selCopy()
675 theSelection.grab(*this);
681 void MathCursor::selCut()
685 theSelection.grab(*this);
686 theSelection.erase(*this);
689 theSelection.clear();
694 void MathCursor::selDel()
698 theSelection.erase(*this);
706 void MathCursor::selPaste()
709 theSelection.paste(*this);
710 //theSelection.grab(*this);
715 void MathCursor::selHandle(bool sel)
717 if (sel == selection_)
719 //theSelection.clear();
725 void MathCursor::selStart()
728 //theSelection.clear();
735 void MathCursor::selClear()
743 void MathCursor::selGet(MathArray & ar)
749 theSelection.grab(*this);
750 ar = theSelection.glue();
755 void MathCursor::drawSelection(Painter & pain) const
762 getSelection(i1, i2);
764 if (i1.idx_ == i2.idx_) {
765 MathXArray & c = i1.xcell();
766 int x1 = c.xo() + c.pos2x(i1.pos_);
767 int y1 = c.yo() - c.ascent();
768 int x2 = c.xo() + c.pos2x(i2.pos_);
769 int y2 = c.yo() + c.descent();
770 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
772 vector<MathInset::idx_type> indices
773 = i1.par_->idxBetween(i1.idx_, i2.idx_);
774 for (unsigned i = 0; i < indices.size(); ++i) {
775 MathXArray & c = i1.xcell(indices[i]);
777 int y1 = c.yo() - c.ascent();
778 int x2 = c.xo() + c.width();
779 int y2 = c.yo() + c.descent();
780 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
785 // draw anchor if different from selection boundary
786 MathCursorPos anc = Anchor_.back();
787 if (anc != i1 && anc != i2) {
788 MathXArray & c = anc.xcell();
789 int x = c.xo() + c.pos2x(anc.pos_);
790 int y1 = c.yo() - c.ascent();
791 int y2 = c.yo() + c.descent();
792 pain.line(x, y1, x, y2, LColor::math);
798 void MathCursor::handleFont(MathTextCodes t)
804 getSelection(i1, i2);
805 if (i1.idx_ == i2.idx_) {
806 MathArray & ar = i1.cell();
807 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
808 ar.at(pos)->handleFont(t);
811 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
815 void MathCursor::handleDelim(string const & l, string const & r)
817 handleNest(new MathDelimInset(l, r));
821 void MathCursor::handleNest(MathInset * p)
825 p->cell(0) = theSelection.glue();
827 insert(MathAtom(p)); // this invalidates p!
828 pushRight(prevAtom());
832 void MathCursor::getPos(int & x, int & y)
835 #warning This should probably take cellXOffset and cellYOffset into account
837 x = xarray().xo() + xarray().pos2x(pos());
842 MathInset * MathCursor::par() const
844 return cursor().par_;
848 InsetFormulaBase * MathCursor::formula()
854 MathCursor::idx_type MathCursor::idx() const
856 return cursor().idx_;
860 MathCursor::idx_type & MathCursor::idx()
862 return cursor().idx_;
866 MathCursor::pos_type MathCursor::pos() const
868 return cursor().pos_;
872 MathCursor::pos_type & MathCursor::pos()
874 return cursor().pos_;
878 bool MathCursor::inMacroMode() const
880 return macroNamePos() != -1;
884 bool MathCursor::inMacroArgMode() const
886 return pos() > 0 && prevAtom()->getChar() == '#';
890 bool MathCursor::selection() const
896 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
898 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
899 MathGridInset * p = Cursor_[i].par_->asGridInset();
901 idx = Cursor_[i].idx_;
909 void MathCursor::pullArg(bool goright)
912 MathArray a = array();
914 MathScriptInset const * p = par()->asScriptInset();
916 // special handling for scripts
917 const bool up = p->hasUp();
919 MathScriptInset * q = nextAtom()->asScriptInset();
923 array().insert(pos(), a);
929 array().insert(pos(), a);
933 formula()->mutateToText();
938 void MathCursor::touch()
940 cursor_type::const_iterator it = Cursor_.begin();
941 cursor_type::const_iterator et = Cursor_.end();
942 for ( ; it != et; ++it)
947 void MathCursor::normalize()
952 MathIterator it = ibegin(formula()->par().nucleus());
953 MathIterator et = iend(formula()->par().nucleus());
954 for (; it != et; ++it)
955 if (it.par()->asBoxInset())
956 it.par()->asBoxInset()->rebreak();
960 if (idx() >= par()->nargs()) {
961 lyxerr << "this should not really happen - 1: "
962 << idx() << " " << par()->nargs() << "\n";
965 idx() = min(idx(), par()->nargs() - 1);
967 if (pos() > size()) {
968 lyxerr << "this should not really happen - 2: "
969 << pos() << " " << size() << " in idx: " << idx()
971 WriteStream wi(lyxerr, false);
976 pos() = min(pos(), size());
980 MathCursor::size_type MathCursor::size() const
982 return array().size();
986 MathCursor::col_type MathCursor::hullCol() const
988 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
992 MathCursor::row_type MathCursor::hullRow() const
994 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
998 bool MathCursor::hasPrevAtom() const
1004 bool MathCursor::hasNextAtom() const
1006 return pos() < size();
1010 MathAtom const & MathCursor::prevAtom() const
1012 lyx::Assert(pos() > 0);
1013 return array().at(pos() - 1);
1017 MathAtom & MathCursor::prevAtom()
1019 lyx::Assert(pos() > 0);
1020 return array().at(pos() - 1);
1024 MathAtom const & MathCursor::nextAtom() const
1026 lyx::Assert(pos() < size());
1027 return array().at(pos());
1031 MathAtom & MathCursor::nextAtom()
1033 lyx::Assert(pos() < size());
1034 return array().at(pos());
1038 MathArray & MathCursor::array() const
1040 static MathArray dummy;
1042 if (idx() >= par()->nargs()) {
1043 lyxerr << "############ idx_ " << idx() << " not valid\n";
1047 if (Cursor_.size() == 0) {
1048 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1052 return cursor().cell();
1056 MathXArray & MathCursor::xarray() const
1058 static MathXArray dummy;
1060 if (Cursor_.size() == 0) {
1061 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1065 return cursor().xcell();
1069 void MathCursor::idxNext()
1071 par()->idxNext(idx(), pos());
1075 void MathCursor::idxPrev()
1077 par()->idxPrev(idx(), pos());
1081 void MathCursor::splitCell()
1083 if (idx() + 1 == par()->nargs())
1085 MathArray ar = array();
1087 array().erase(pos(), size());
1090 array().insert(0, ar);
1094 void MathCursor::breakLine()
1096 // leave inner cells
1100 MathHullInset * p = formula()->par()->asHullInset();
1104 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1105 p->mutate(LM_OT_EQNARRAY);
1109 p->addRow(hullRow());
1112 const row_type r = hullRow();
1113 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1114 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1118 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1123 //void MathCursor::readLine(MathArray & ar) const
1125 // idx_type base = row() * par()->ncols();
1126 // for (idx_type off = 0; off < par()->ncols(); ++off)
1127 // ar.push_back(par()->cell(base + off));
1131 char MathCursor::valign() const
1134 MathGridInset * p = enclosingGrid(idx);
1135 return p ? p->valign() : '\0';
1139 char MathCursor::halign() const
1142 MathGridInset * p = enclosingGrid(idx);
1143 return p ? p->halign(idx % p->ncols()) : '\0';
1147 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1149 MathCursorPos anc = normalAnchor();
1150 if (anc < cursor()) {
1160 MathCursorPos & MathCursor::cursor()
1162 lyx::Assert(Cursor_.size());
1163 return Cursor_.back();
1167 MathCursorPos const & MathCursor::cursor() const
1169 lyx::Assert(Cursor_.size());
1170 return Cursor_.back();
1174 bool MathCursor::goUpDown(bool up)
1176 int xlow, xhigh, ylow, yhigh;
1181 // try current cell first
1182 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1187 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1190 // try to find an inset that knows better then we
1192 // we found a cell that think something "below" us.
1194 if (par()->idxUp(idx()))
1197 if (par()->idxDown(idx()))
1202 // no such inset found, just take something "above"
1207 up ? formula()->ylow() : yo + 4,
1208 up ? yo - 4 : formula()->yhigh()
1212 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1213 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1218 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1220 cursor_type best_cursor;
1221 double best_dist = 1e10;
1223 MathIterator it = ibegin(formula()->par().nucleus());
1224 MathIterator et = iend(formula()->par().nucleus());
1226 // avoid invalid nesting when selecting
1227 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1228 MathCursorPos const & top = it.position();
1229 int xo = top.xpos();
1230 int yo = top.ypos();
1231 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1232 ylow - 2 <= yo && yo <= yhigh + 2)
1234 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1235 if (d < best_dist) {
1237 best_cursor = it.cursor();
1247 if (best_dist < 1e10)
1248 Cursor_ = best_cursor;
1249 return best_dist < 1e10;
1253 bool MathCursor::idxLeft()
1255 return par()->idxLeft(idx(), pos());
1259 bool MathCursor::idxRight()
1261 return par()->idxRight(idx(), pos());
1265 bool MathCursor::interpret(string const & s)
1267 //lyxerr << "interpret 1: '" << s << "'\n";
1272 return interpret(s[0]);
1274 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1275 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1276 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1278 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1280 istringstream is(s.substr(5).c_str());
1283 niceInsert(MathAtom(new MathCasesInset(n)));
1287 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1292 istringstream is(s.substr(6).c_str());
1293 is >> m >> n >> v_align >> h_align;
1297 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1301 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1303 istringstream is(s.substr(7).c_str());
1306 mathed_parse_cell(rep.from, from);
1307 mathed_parse_cell(rep.to, to);
1308 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1309 par()->replace(rep);
1313 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1314 MathArray ar = array();
1315 MathAtom t(createMathInset(s.substr(1)));
1316 t->asNestInset()->cell(0).swap(array());
1324 latexkeys const * l = in_word_set(s.substr(1));
1325 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1326 lastcode_ = static_cast<MathTextCodes>(l->id);
1330 // prevent entering of recursive macros
1331 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1332 && formula()->getInsetName() == s.substr(1))
1334 lyxerr << "can't enter recursive macro\n";
1338 niceInsert(createMathInset(s.substr(1)));
1343 bool MathCursor::script(bool up)
1347 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1348 prevAtom()->asScriptInset()->ensure(up);
1349 pushRight(prevAtom());
1352 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1353 nextAtom()->asScriptInset()->ensure(up);
1354 pushLeft(nextAtom());
1358 plainInsert(MathAtom(new MathScriptInset(up)));
1359 prevAtom()->asScriptInset()->ensure(up);
1360 pushRight(prevAtom());
1370 bool MathCursor::interpret(char c)
1372 if (inMacroArgMode()) {
1376 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1377 if (p && 1 <= n && n <= p->numargs())
1378 insert(MathAtom(new MathMacroArgument(c - '0')));
1380 insert(MathAtom(new MathSpecialCharInset('#')));
1381 interpret(c); // try again
1387 if (inMacroMode()) {
1388 string name = macroName();
1390 if (name == "\\" && c == '\\') {
1392 interpret("\\backslash");
1397 insert(c, LM_TC_TEX);
1402 insert(c, LM_TC_TEX);
1410 insert(c, LM_TC_TEX);
1412 insert(c, lastcode_);
1421 // fall through in the other cases
1424 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1425 // suppress direct insertion of two spaces in a row
1426 // the still allows typing '<space>a<space>' and deleting the 'a', but
1427 // it is better than nothing...
1428 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1430 insert(c, LM_TC_TEXTRM);
1435 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1436 prevAtom()->asSpaceInset()->incSpace();
1441 // if are at the very end, leave the formula
1442 return pos() != size();
1446 insert(c, LM_TC_TEX);
1451 if (strchr("{}", c)) {
1452 insert(c, LM_TC_TEX);
1458 niceInsert(MathAtom(new MathBraceInset));
1466 if (strchr("$%", c)) {
1467 insert(MathAtom(new MathSpecialCharInset(c)));
1468 lastcode_ = LM_TC_VAR;
1472 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1473 insert(c, LM_TC_VAR);
1477 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1478 insert(c, LM_TC_VAR);
1479 lastcode_ = LM_TC_VAR;
1484 insert(c, LM_TC_TEX);
1485 //bv->owner()->message(_("TeX mode"));
1489 // no special circumstances, so insert the character without any fuss
1490 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1491 lastcode_ = LM_TC_MIN;
1497 MathCursorPos MathCursor::normalAnchor() const
1499 if (Anchor_.size() < Cursor_.size()) {
1501 lyxerr << "unusual Anchor size\n";
1504 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1505 // use Anchor on the same level as Cursor
1506 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1507 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1508 // anchor is behind cursor -> move anchor behind the inset
1515 void MathCursor::stripFromLastEqualSign()
1517 // find position of last '=' in the array
1518 MathArray & ar = cursor().cell();
1519 MathArray::const_iterator et = ar.end();
1520 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1521 if ((*it)->getChar() == '=')
1524 // delete everything behind this position
1525 ar.erase(et - ar.begin(), ar.size());
1530 void MathCursor::setSelection(cursor_type const & where, size_type n)
1539 string MathCursor::info() const
1543 prevAtom()->infoize(os);