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"
59 using std::ostringstream;
65 typedef MathInset::col_type col_type;
66 typedef MathInset::row_type row_type;
67 typedef MathInset::idx_type idx_type;
73 void region(MathCursorPos const & i1, MathCursorPos const & i2,
74 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
76 MathInset * p = i1.par_;
87 void grab(MathCursor const & cursor)
91 cursor.getSelection(i1, i2);
92 // shouldn'tt we assert on i1.par_ == i2.par_?
93 if (i1.idx_ == i2.idx_) {
94 data_ = MathGridInset(1, 1);
95 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
99 region(i1, i2, r1, r2, c1, c2);
100 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
101 for (row_type row = 0; row < data_.nrows(); ++row)
102 for (col_type col = 0; col < data_.ncols(); ++col) {
103 idx_type i = i1.par_->index(row + r1, col + c1);
104 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
109 void erase(MathCursor & cursor)
113 cursor.getSelection(i1, i2);
114 if (i1.idx_ == i2.idx_)
115 i1.cell().erase(i1.pos_, i2.pos_);
117 MathInset * p = i1.par_;
120 region(i1, i2, r1, r2, c1, c2);
121 for (row_type row = r1; row <= r2; ++row)
122 for (col_type col = c1; col <= c2; ++col)
123 p->cell(p->index(row, col)).erase();
125 cursor.cursor() = i1;
128 void paste(MathCursor & cursor) const
130 if (data_.nargs() == 1) {
131 // single cell/part of cell
132 cursor.insert(data_.cell(0));
136 MathGridInset * p = cursor.enclosingGrid(idx);
137 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
138 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
139 for (row_type row = 0; row < numrows; ++row)
140 for (col_type col = 0; col < numcols; ++col) {
141 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
142 p->cell(i).push_back(data_.cell(data_.index(row, col)));
147 // glues selection to one cell
148 MathArray glue() const
151 for (unsigned i = 0; i < data_.nargs(); ++i)
152 ar.push_back(data_.cell(i));
158 data_ = MathGridInset(1, 1);
165 Selection theSelection;
172 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
173 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
175 left ? first() : last();
179 void MathCursor::push(MathAtom & t)
181 Cursor_.push_back(MathCursorPos(t.nucleus()));
185 void MathCursor::pushLeft(MathAtom & t)
187 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
189 t->idxFirst(idx(), pos());
193 void MathCursor::pushRight(MathAtom & t)
195 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
198 t->idxLast(idx(), pos());
202 bool MathCursor::popLeft()
204 //cerr << "Leaving atom to the left\n";
205 if (Cursor_.size() <= 1)
212 bool MathCursor::popRight()
214 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
215 if (Cursor_.size() <= 1)
225 void MathCursor::dump(char const * what) const
227 lyxerr << "MC: " << what << "\n";
228 lyxerr << " Cursor: " << Cursor_.size() << "\n";
229 for (unsigned i = 0; i < Cursor_.size(); ++i)
230 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
231 lyxerr << " Anchor: " << Anchor_.size() << "\n";
232 for (unsigned i = 0; i < Anchor_.size(); ++i)
233 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
234 lyxerr << " sel: " << selection_ << "\n";
237 void MathCursor::dump(char const *) const {}
241 UpdatableInset * MathCursor::asHyperActiveInset() const
243 return par()->asHyperActiveInset();
247 bool MathCursor::isInside(MathInset const * p) const
249 for (unsigned i = 0; i < Cursor_.size(); ++i)
250 if (Cursor_[i].par_ == p)
256 bool MathCursor::openable(MathAtom const & t, bool sel) const
258 if (t->isHyperActive())
264 if (t->asScriptInset())
268 // we can't move into anything new during selection
269 if (Cursor_.size() == Anchor_.size())
271 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
278 bool MathCursor::posLeft()
287 bool MathCursor::posRight()
296 bool MathCursor::left(bool sel)
301 lastcode_ = LM_TC_MIN;
305 lastcode_ = LM_TC_MIN;
307 if (hasPrevAtom() && openable(prevAtom(), sel)) {
308 if (prevAtom()->isHyperActive()) {
309 lyxerr << "entering hyperactive inset\n";
311 pushRight(prevAtom());
315 return posLeft() || idxLeft() || popLeft() || selection_;
319 bool MathCursor::right(bool sel)
324 lastcode_ = LM_TC_MIN;
328 lastcode_ = LM_TC_MIN;
330 if (hasNextAtom() && openable(nextAtom(), sel)) {
331 if (nextAtom()->isHyperActive()) {
332 lyxerr << "entering hyperactive inset\n";
335 nextAtom()->edit(formula()->view(), x, y, 0);
337 pushLeft(nextAtom());
341 return posRight() || idxRight() || popRight() || selection_;
345 void MathCursor::first()
348 pushLeft(formula_->par());
352 void MathCursor::last()
359 bool positionable(MathCursor::cursor_type const & cursor,
360 MathCursor::cursor_type const & anchor)
362 // avoid deeper nested insets when selecting
363 if (cursor.size() > anchor.size())
366 // anchor might be deeper, should have same path then
367 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
368 if (cursor[i].par_ != anchor[i].par_)
371 // position should be ok.
376 void MathCursor::setPos(int x, int y)
379 bool res = bruteFind(x, y,
380 formula()->xlow(), formula()->xhigh(),
381 formula()->ylow(), formula()->yhigh());
383 // this ccan happen on creation of "math-display"
392 void MathCursor::home(bool sel)
397 lastcode_ = LM_TC_MIN;
398 if (!par()->idxHome(idx(), pos()))
404 void MathCursor::end(bool sel)
409 lastcode_ = LM_TC_MIN;
410 if (!par()->idxEnd(idx(), pos()))
416 void MathCursor::plainErase()
418 array().erase(pos());
422 void MathCursor::markInsert()
424 //lyxerr << "inserting mark\n";
425 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
429 void MathCursor::markErase()
431 //lyxerr << "deleting mark\n";
432 array().erase(pos());
436 void MathCursor::plainInsert(MathAtom const & t)
438 array().insert(pos(), t);
443 void MathCursor::insert(char c, MathTextCodes t)
445 //lyxerr << "inserting '" << c << "'\n";
446 plainInsert(MathAtom(new MathCharInset(c, t)));
450 void MathCursor::insert(char c)
452 insert(c, lastcode_);
456 void MathCursor::insert(MathAtom const & t)
471 void MathCursor::niceInsert(MathAtom const & t)
474 insert(t); // inserting invalidates the pointer!
475 MathAtom const & p = prevAtom();
478 right(); // do not push for e.g. MathSymbolInset
484 void MathCursor::insert(MathArray const & ar)
490 array().insert(pos(), ar);
495 void MathCursor::paste(MathArray const & ar)
499 array().insert(pos(), ar);
504 void MathCursor::backspace()
516 MathScriptInset * p = prevAtom()->asScriptInset();
518 p->removeScript(p->hasUp());
519 // Don't delete if there is anything left
520 if (p->hasUp() || p->hasDown())
529 void MathCursor::erase()
539 // delete empty cells if necessary
540 if (array().empty()) {
543 par()->idxDelete(idx(), popit, removeit);
544 if (popit && popLeft() && removeit)
552 MathScriptInset * p = nextAtom()->asScriptInset();
554 p->removeScript(p->hasUp());
555 // Don't delete if there is anything left
556 if (p->hasUp() || p->hasDown())
564 void MathCursor::delLine()
573 if (par()->nrows() > 1) {
574 // grid are the only things with more than one row...
575 lyx::Assert(par()->asGridInset());
576 par()->asGridInset()->delRow(hullRow());
579 if (idx() >= par()->nargs())
580 idx() = par()->nargs() - 1;
587 bool MathCursor::up(bool sel)
592 cursor_type save = Cursor_;
600 bool MathCursor::down(bool sel)
605 cursor_type save = Cursor_;
613 bool MathCursor::toggleLimits()
617 MathScriptInset * t = nextAtom()->asScriptInset();
620 int old = t->limits();
621 t->limits(old < 0 ? 1 : -1);
622 return old != t->limits();
626 void MathCursor::macroModeClose()
628 string s = macroName();
630 size_type old = pos();
632 array().erase(pos(), old);
638 MathInset::difference_type MathCursor::macroNamePos() const
640 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
641 MathAtom & p = array().at(i);
642 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
649 string MathCursor::macroName() const
652 MathInset::difference_type i = macroNamePos();
653 for (; i >= 0 && i < int(pos()); ++i)
654 s += array().at(i)->getChar();
659 void MathCursor::selCopy()
663 theSelection.grab(*this);
669 void MathCursor::selCut()
673 theSelection.grab(*this);
674 theSelection.erase(*this);
677 theSelection.clear();
682 void MathCursor::selDel()
686 theSelection.erase(*this);
694 void MathCursor::selPaste()
697 theSelection.paste(*this);
698 //theSelection.grab(*this);
703 void MathCursor::selHandle(bool sel)
705 if (sel == selection_)
707 //theSelection.clear();
713 void MathCursor::selStart()
716 //theSelection.clear();
723 void MathCursor::selClear()
731 void MathCursor::selGet(MathArray & ar)
737 theSelection.grab(*this);
738 ar = theSelection.glue();
743 void MathCursor::drawSelection(Painter & pain) const
750 getSelection(i1, i2);
752 if (i1.idx_ == i2.idx_) {
753 MathXArray & c = i1.xcell();
754 int x1 = c.xo() + c.pos2x(i1.pos_);
755 int y1 = c.yo() - c.ascent();
756 int x2 = c.xo() + c.pos2x(i2.pos_);
757 int y2 = c.yo() + c.descent();
758 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
760 vector<MathInset::idx_type> indices
761 = i1.par_->idxBetween(i1.idx_, i2.idx_);
762 for (unsigned i = 0; i < indices.size(); ++i) {
763 MathXArray & c = i1.xcell(indices[i]);
765 int y1 = c.yo() - c.ascent();
766 int x2 = c.xo() + c.width();
767 int y2 = c.yo() + c.descent();
768 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
773 // draw anchor if different from selection boundary
774 MathCursorPos anc = Anchor_.back();
775 if (anc != i1 && anc != i2) {
776 MathXArray & c = anc.xcell();
777 int x = c.xo() + c.pos2x(anc.pos_);
778 int y1 = c.yo() - c.ascent();
779 int y2 = c.yo() + c.descent();
780 pain.line(x, y1, x, y2, LColor::math);
786 void MathCursor::handleFont(MathTextCodes t)
792 getSelection(i1, i2);
793 if (i1.idx_ == i2.idx_) {
794 MathArray & ar = i1.cell();
795 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
796 ar.at(pos)->handleFont(t);
799 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
803 void MathCursor::handleDelim(string const & l, string const & r)
805 handleNest(new MathDelimInset(l, r));
809 void MathCursor::handleNest(MathInset * p)
813 p->cell(0) = theSelection.glue();
815 insert(MathAtom(p)); // this invalidates p!
816 pushRight(prevAtom());
820 void MathCursor::getPos(int & x, int & y)
823 #warning This should probably take cellXOffset and cellYOffset into account
825 x = xarray().xo() + xarray().pos2x(pos());
830 MathInset * MathCursor::par() const
832 return cursor().par_;
836 InsetFormulaBase * MathCursor::formula()
842 MathCursor::idx_type MathCursor::idx() const
844 return cursor().idx_;
848 MathCursor::idx_type & MathCursor::idx()
850 return cursor().idx_;
854 MathCursor::pos_type MathCursor::pos() const
856 return cursor().pos_;
860 MathCursor::pos_type & MathCursor::pos()
862 return cursor().pos_;
866 bool MathCursor::inMacroMode() const
868 return macroNamePos() != -1;
872 bool MathCursor::inMacroArgMode() const
874 return pos() > 0 && prevAtom()->getChar() == '#';
878 bool MathCursor::selection() const
884 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
886 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
887 MathGridInset * p = Cursor_[i].par_->asGridInset();
889 idx = Cursor_[i].idx_;
897 void MathCursor::pullArg(bool goright)
900 MathArray a = array();
902 MathScriptInset const * p = par()->asScriptInset();
904 // special handling for scripts
905 const bool up = p->hasUp();
907 MathScriptInset * q = nextAtom()->asScriptInset();
911 array().insert(pos(), a);
917 array().insert(pos(), a);
921 formula()->mutateToText();
926 void MathCursor::touch()
928 cursor_type::const_iterator it = Cursor_.begin();
929 cursor_type::const_iterator et = Cursor_.end();
930 for ( ; it != et; ++it)
935 void MathCursor::normalize()
940 MathIterator it = ibegin(formula()->par().nucleus());
941 MathIterator et = iend(formula()->par().nucleus());
942 for (; it != et; ++it)
943 if (it.par()->asBoxInset())
944 it.par()->asBoxInset()->rebreak();
948 if (idx() >= par()->nargs()) {
949 lyxerr << "this should not really happen - 1: "
950 << idx() << " " << par()->nargs() << "\n";
953 idx() = min(idx(), par()->nargs() - 1);
955 if (pos() > size()) {
956 lyxerr << "this should not really happen - 2: "
957 << pos() << " " << size() << " in idx: " << idx()
959 WriteStream wi(lyxerr, false);
964 pos() = min(pos(), size());
966 // remove empty scripts if possible
967 for (pos_type i = 0; i < size(); ++i) {
968 MathScriptInset * p = array().at(i)->asScriptInset();
970 p->removeEmptyScripts();
976 // fix again position
977 pos() = min(pos(), size());
981 MathCursor::size_type MathCursor::size() const
983 return array().size();
987 MathCursor::col_type MathCursor::hullCol() const
989 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
993 MathCursor::row_type MathCursor::hullRow() const
995 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
999 bool MathCursor::hasPrevAtom() const
1005 bool MathCursor::hasNextAtom() const
1007 return pos() < size();
1011 MathAtom const & MathCursor::prevAtom() const
1013 lyx::Assert(pos() > 0);
1014 return array().at(pos() - 1);
1018 MathAtom & MathCursor::prevAtom()
1020 lyx::Assert(pos() > 0);
1021 return array().at(pos() - 1);
1025 MathAtom const & MathCursor::nextAtom() const
1027 lyx::Assert(pos() < size());
1028 return array().at(pos());
1032 MathAtom & MathCursor::nextAtom()
1034 lyx::Assert(pos() < size());
1035 return array().at(pos());
1039 MathArray & MathCursor::array() const
1041 static MathArray dummy;
1043 if (idx() >= par()->nargs()) {
1044 lyxerr << "############ idx_ " << idx() << " not valid\n";
1048 if (Cursor_.size() == 0) {
1049 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1053 return cursor().cell();
1057 MathXArray & MathCursor::xarray() const
1059 static MathXArray dummy;
1061 if (Cursor_.size() == 0) {
1062 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1066 return cursor().xcell();
1070 void MathCursor::idxNext()
1072 par()->idxNext(idx(), pos());
1076 void MathCursor::idxPrev()
1078 par()->idxPrev(idx(), pos());
1082 void MathCursor::splitCell()
1084 if (idx() + 1 == par()->nargs())
1086 MathArray ar = array();
1088 array().erase(pos(), size());
1091 array().insert(0, ar);
1095 void MathCursor::breakLine()
1097 // leave inner cells
1101 MathHullInset * p = formula()->par()->asHullInset();
1105 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1106 p->mutate(LM_OT_EQNARRAY);
1110 p->addRow(hullRow());
1113 const row_type r = hullRow();
1114 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1115 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1119 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1124 //void MathCursor::readLine(MathArray & ar) const
1126 // idx_type base = row() * par()->ncols();
1127 // for (idx_type off = 0; off < par()->ncols(); ++off)
1128 // ar.push_back(par()->cell(base + off));
1132 char MathCursor::valign() const
1135 MathGridInset * p = enclosingGrid(idx);
1136 return p ? p->valign() : '\0';
1140 char MathCursor::halign() const
1143 MathGridInset * p = enclosingGrid(idx);
1144 return p ? p->halign(idx % p->ncols()) : '\0';
1148 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1150 MathCursorPos anc = normalAnchor();
1151 if (anc < cursor()) {
1161 MathCursorPos & MathCursor::cursor()
1163 lyx::Assert(Cursor_.size());
1164 return Cursor_.back();
1168 MathCursorPos const & MathCursor::cursor() const
1170 lyx::Assert(Cursor_.size());
1171 return Cursor_.back();
1175 bool MathCursor::goUpDown(bool up)
1177 int xlow, xhigh, ylow, yhigh;
1182 // try current cell first
1183 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1188 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1191 // try to find an inset that knows better then we
1193 // we found a cell that think something "below" us.
1195 if (par()->idxUp(idx()))
1198 if (par()->idxDown(idx()))
1203 // no such inset found, just take something "above"
1208 up ? formula()->ylow() : yo + 4,
1209 up ? yo - 4 : formula()->yhigh()
1213 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1214 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1219 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1221 cursor_type best_cursor;
1222 double best_dist = 1e10;
1224 MathIterator it = ibegin(formula()->par().nucleus());
1225 MathIterator et = iend(formula()->par().nucleus());
1227 // avoid invalid nesting when selecting
1228 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1229 MathCursorPos const & top = it.position();
1230 int xo = top.xpos();
1231 int yo = top.ypos();
1232 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1233 ylow - 2 <= yo && yo <= yhigh + 2)
1235 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1236 if (d < best_dist) {
1238 best_cursor = it.cursor();
1248 if (best_dist < 1e10)
1249 Cursor_ = best_cursor;
1250 return best_dist < 1e10;
1254 bool MathCursor::idxLeft()
1256 return par()->idxLeft(idx(), pos());
1260 bool MathCursor::idxRight()
1262 return par()->idxRight(idx(), pos());
1266 bool MathCursor::interpret(string const & s)
1268 //lyxerr << "interpret 1: '" << s << "'\n";
1273 return interpret(s[0]);
1275 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1276 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1277 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1279 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1281 istringstream is(s.substr(5).c_str());
1284 niceInsert(MathAtom(new MathCasesInset(n)));
1288 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1293 istringstream is(s.substr(6).c_str());
1294 is >> m >> n >> v_align >> h_align;
1298 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1302 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1304 istringstream is(s.substr(7).c_str());
1307 mathed_parse_cell(rep.from, from);
1308 mathed_parse_cell(rep.to, to);
1309 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1310 par()->replace(rep);
1314 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1315 MathArray ar = array();
1316 MathAtom t(createMathInset(s.substr(1)));
1317 t->asNestInset()->cell(0).swap(array());
1325 latexkeys const * l = in_word_set(s.substr(1));
1326 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1327 lastcode_ = static_cast<MathTextCodes>(l->id);
1331 // prevent entering of recursive macros
1332 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1333 && formula()->getInsetName() == s.substr(1))
1335 lyxerr << "can't enter recursive macro\n";
1339 niceInsert(createMathInset(s.substr(1)));
1344 bool MathCursor::script(bool up)
1348 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1349 prevAtom()->asScriptInset()->ensure(up);
1350 pushRight(prevAtom());
1353 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1354 nextAtom()->asScriptInset()->ensure(up);
1355 pushLeft(nextAtom());
1359 plainInsert(MathAtom(new MathScriptInset(up)));
1360 prevAtom()->asScriptInset()->ensure(up);
1361 pushRight(prevAtom());
1371 bool MathCursor::interpret(char c)
1373 if (inMacroArgMode()) {
1377 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1378 if (p && 1 <= n && n <= p->numargs())
1379 insert(MathAtom(new MathMacroArgument(c - '0')));
1381 insert(MathAtom(new MathSpecialCharInset('#')));
1382 interpret(c); // try again
1388 if (inMacroMode()) {
1389 string name = macroName();
1391 if (name == "\\" && c == '\\') {
1393 interpret("\\backslash");
1398 insert(c, LM_TC_TEX);
1403 insert(c, LM_TC_TEX);
1411 insert(c, LM_TC_TEX);
1413 insert(c, lastcode_);
1422 // fall through in the other cases
1425 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1426 // suppress direct insertion of two spaces in a row
1427 // the still allows typing '<space>a<space>' and deleting the 'a', but
1428 // it is better than nothing...
1429 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1431 insert(c, LM_TC_TEXTRM);
1436 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1437 prevAtom()->asSpaceInset()->incSpace();
1442 // if are at the very end, leave the formula
1443 return pos() != size();
1447 insert(c, LM_TC_TEX);
1452 if (c == '{' || c == '}', c)) {
1453 insert(c, LM_TC_TEX);
1459 niceInsert(MathAtom(new MathBraceInset));
1467 if (c == '$' || c == '%') {
1468 insert(MathAtom(new MathSpecialCharInset(c)));
1469 lastcode_ = LM_TC_VAR;
1473 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1474 insert(c, LM_TC_VAR);
1478 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1479 insert(c, LM_TC_VAR);
1480 lastcode_ = LM_TC_VAR;
1485 insert(c, LM_TC_TEX);
1486 //bv->owner()->message(_("TeX mode"));
1490 // no special circumstances, so insert the character without any fuss
1491 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1492 lastcode_ = LM_TC_MIN;
1498 MathCursorPos MathCursor::normalAnchor() const
1500 if (Anchor_.size() < Cursor_.size()) {
1502 lyxerr << "unusual Anchor size\n";
1505 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1506 // use Anchor on the same level as Cursor
1507 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1508 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1509 // anchor is behind cursor -> move anchor behind the inset
1516 void MathCursor::stripFromLastEqualSign()
1518 // find position of last '=' in the array
1519 MathArray & ar = cursor().cell();
1520 MathArray::const_iterator et = ar.end();
1521 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1522 if ((*it)->getChar() == '=')
1525 // delete everything behind this position
1526 ar.erase(et - ar.begin(), ar.size());
1531 void MathCursor::setSelection(cursor_type const & where, size_type n)
1540 string MathCursor::info() const
1544 prevAtom()->infoize(os);