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));
136 idx_type idx; // index of upper left cell
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)));
145 // append the left over horizontal cells to the last column
146 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
147 for (col_type col = numcols; col < data_.ncols(); ++col)
148 p->cell(i).push_back(data_.cell(data_.index(row, col)));
150 // append the left over vertical cells to the last _cell_
151 idx_type i = p->nargs() - 1;
152 for (row_type row = numrows; row < data_.nrows(); ++row)
153 for (col_type col = 0; col < data_.ncols(); ++col)
154 p->cell(i).push_back(data_.cell(data_.index(row, col)));
158 // glues selection to one cell
159 MathArray glue() const
162 for (unsigned i = 0; i < data_.nargs(); ++i)
163 ar.push_back(data_.cell(i));
169 data_ = MathGridInset(1, 1);
176 Selection theSelection;
183 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
184 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
186 left ? first() : last();
190 void MathCursor::push(MathAtom & t)
192 Cursor_.push_back(MathCursorPos(t.nucleus()));
196 void MathCursor::pushLeft(MathAtom & t)
198 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
200 t->idxFirst(idx(), pos());
204 void MathCursor::pushRight(MathAtom & t)
206 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
209 t->idxLast(idx(), pos());
213 bool MathCursor::popLeft()
215 //cerr << "Leaving atom to the left\n";
216 if (Cursor_.size() <= 1)
223 bool MathCursor::popRight()
225 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
226 if (Cursor_.size() <= 1)
236 void MathCursor::dump(char const * what) const
238 lyxerr << "MC: " << what << "\n";
239 lyxerr << " Cursor: " << Cursor_.size() << "\n";
240 for (unsigned i = 0; i < Cursor_.size(); ++i)
241 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
242 lyxerr << " Anchor: " << Anchor_.size() << "\n";
243 for (unsigned i = 0; i < Anchor_.size(); ++i)
244 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
245 lyxerr << " sel: " << selection_ << "\n";
248 void MathCursor::dump(char const *) const {}
252 UpdatableInset * MathCursor::asHyperActiveInset() const
254 return par()->asHyperActiveInset();
258 bool MathCursor::isInside(MathInset const * p) const
260 for (unsigned i = 0; i < Cursor_.size(); ++i)
261 if (Cursor_[i].par_ == p)
267 bool MathCursor::openable(MathAtom const & t, bool sel) const
269 if (t->isHyperActive())
275 if (t->asScriptInset())
279 // we can't move into anything new during selection
280 if (Cursor_.size() == Anchor_.size())
282 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
289 bool MathCursor::posLeft()
298 bool MathCursor::posRight()
307 bool MathCursor::left(bool sel)
312 lastcode_ = LM_TC_MIN;
316 lastcode_ = LM_TC_MIN;
318 if (hasPrevAtom() && openable(prevAtom(), sel)) {
319 if (prevAtom()->isHyperActive()) {
320 lyxerr << "entering hyperactive inset\n";
322 pushRight(prevAtom());
326 return posLeft() || idxLeft() || popLeft() || selection_;
330 bool MathCursor::right(bool sel)
335 lastcode_ = LM_TC_MIN;
339 lastcode_ = LM_TC_MIN;
341 if (hasNextAtom() && openable(nextAtom(), sel)) {
342 if (nextAtom()->isHyperActive()) {
343 lyxerr << "entering hyperactive inset\n";
346 nextAtom()->edit(formula()->view(), x, y, 0);
348 pushLeft(nextAtom());
352 return posRight() || idxRight() || popRight() || selection_;
356 void MathCursor::first()
359 pushLeft(formula_->par());
363 void MathCursor::last()
370 bool positionable(MathCursor::cursor_type const & cursor,
371 MathCursor::cursor_type const & anchor)
373 // avoid deeper nested insets when selecting
374 if (cursor.size() > anchor.size())
377 // anchor might be deeper, should have same path then
378 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
379 if (cursor[i].par_ != anchor[i].par_)
382 // position should be ok.
387 void MathCursor::setPos(int x, int y)
390 bool res = bruteFind(x, y,
391 formula()->xlow(), formula()->xhigh(),
392 formula()->ylow(), formula()->yhigh());
394 // this ccan happen on creation of "math-display"
403 void MathCursor::home(bool sel)
408 lastcode_ = LM_TC_MIN;
409 if (!par()->idxHome(idx(), pos()))
415 void MathCursor::end(bool sel)
420 lastcode_ = LM_TC_MIN;
421 if (!par()->idxEnd(idx(), pos()))
427 void MathCursor::plainErase()
429 array().erase(pos());
433 void MathCursor::markInsert()
435 //lyxerr << "inserting mark\n";
436 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
440 void MathCursor::markErase()
442 //lyxerr << "deleting mark\n";
443 array().erase(pos());
447 void MathCursor::plainInsert(MathAtom const & t)
449 array().insert(pos(), t);
454 void MathCursor::insert(char c, MathTextCodes t)
456 //lyxerr << "inserting '" << c << "'\n";
457 plainInsert(MathAtom(new MathCharInset(c, t)));
461 void MathCursor::insert(char c)
463 insert(c, lastcode_);
467 void MathCursor::insert(MathAtom const & t)
482 void MathCursor::niceInsert(MathAtom const & t)
485 insert(t); // inserting invalidates the pointer!
486 MathAtom const & p = prevAtom();
489 right(); // do not push for e.g. MathSymbolInset
495 void MathCursor::insert(MathArray const & ar)
501 array().insert(pos(), ar);
506 void MathCursor::paste(MathArray const & ar)
510 array().insert(pos(), ar);
515 void MathCursor::backspace()
527 MathScriptInset * p = prevAtom()->asScriptInset();
529 p->removeScript(p->hasUp());
530 // Don't delete if there is anything left
531 if (p->hasUp() || p->hasDown())
540 void MathCursor::erase()
550 // delete empty cells if possible
552 if (par()->idxDelete(idx()))
555 // old behaviour when in last position of cell
556 if (pos() == size()) {
557 par()->idxGlue(idx());
561 MathScriptInset * p = nextAtom()->asScriptInset();
563 p->removeScript(p->hasUp());
564 // Don't delete if there is anything left
565 if (p->hasUp() || p->hasDown())
573 void MathCursor::delLine()
582 if (par()->nrows() > 1) {
583 // grid are the only things with more than one row...
584 lyx::Assert(par()->asGridInset());
585 par()->asGridInset()->delRow(hullRow());
588 if (idx() >= par()->nargs())
589 idx() = par()->nargs() - 1;
596 bool MathCursor::up(bool sel)
601 cursor_type save = Cursor_;
609 bool MathCursor::down(bool sel)
614 cursor_type save = Cursor_;
622 bool MathCursor::toggleLimits()
626 MathScriptInset * t = nextAtom()->asScriptInset();
629 int old = t->limits();
630 t->limits(old < 0 ? 1 : -1);
631 return old != t->limits();
635 void MathCursor::macroModeClose()
637 MathInset::difference_type const t = macroNamePos();
640 string s = macroName();
641 array().erase(t, pos());
648 MathInset::difference_type MathCursor::macroNamePos() const
650 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
651 MathAtom & p = array().at(i);
652 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
659 string MathCursor::macroName() const
662 MathInset::difference_type i = macroNamePos();
663 for (; i >= 0 && i < int(pos()); ++i)
664 s += array().at(i)->getChar();
669 void MathCursor::selCopy()
673 theSelection.grab(*this);
679 void MathCursor::selCut()
683 theSelection.grab(*this);
684 theSelection.erase(*this);
687 theSelection.clear();
692 void MathCursor::selDel()
696 theSelection.erase(*this);
704 void MathCursor::selPaste()
707 theSelection.paste(*this);
708 //theSelection.grab(*this);
713 void MathCursor::selHandle(bool sel)
715 if (sel == selection_)
717 //theSelection.clear();
723 void MathCursor::selStart()
726 //theSelection.clear();
733 void MathCursor::selClear()
741 void MathCursor::selGet(MathArray & ar)
747 theSelection.grab(*this);
748 ar = theSelection.glue();
753 void MathCursor::drawSelection(Painter & pain) const
760 getSelection(i1, i2);
762 if (i1.idx_ == i2.idx_) {
763 MathXArray & c = i1.xcell();
764 int x1 = c.xo() + c.pos2x(i1.pos_);
765 int y1 = c.yo() - c.ascent();
766 int x2 = c.xo() + c.pos2x(i2.pos_);
767 int y2 = c.yo() + c.descent();
768 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
770 vector<MathInset::idx_type> indices
771 = i1.par_->idxBetween(i1.idx_, i2.idx_);
772 for (unsigned i = 0; i < indices.size(); ++i) {
773 MathXArray & c = i1.xcell(indices[i]);
775 int y1 = c.yo() - c.ascent();
776 int x2 = c.xo() + c.width();
777 int y2 = c.yo() + c.descent();
778 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
783 // draw anchor if different from selection boundary
784 MathCursorPos anc = Anchor_.back();
785 if (anc != i1 && anc != i2) {
786 MathXArray & c = anc.xcell();
787 int x = c.xo() + c.pos2x(anc.pos_);
788 int y1 = c.yo() - c.ascent();
789 int y2 = c.yo() + c.descent();
790 pain.line(x, y1, x, y2, LColor::math);
796 void MathCursor::handleFont(MathTextCodes t)
802 getSelection(i1, i2);
803 if (i1.idx_ == i2.idx_) {
804 MathArray & ar = i1.cell();
805 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
806 ar.at(pos)->handleFont(t);
809 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
813 void MathCursor::handleDelim(string const & l, string const & r)
815 handleNest(new MathDelimInset(l, r));
819 void MathCursor::handleNest(MathInset * p)
823 p->cell(0) = theSelection.glue();
825 insert(MathAtom(p)); // this invalidates p!
826 pushRight(prevAtom());
830 void MathCursor::getPos(int & x, int & y)
833 #warning This should probably take cellXOffset and cellYOffset into account
835 x = xarray().xo() + xarray().pos2x(pos());
840 MathInset * MathCursor::par() const
842 return cursor().par_;
846 InsetFormulaBase * MathCursor::formula()
852 MathCursor::idx_type MathCursor::idx() const
854 return cursor().idx_;
858 MathCursor::idx_type & MathCursor::idx()
860 return cursor().idx_;
864 MathCursor::pos_type MathCursor::pos() const
866 return cursor().pos_;
870 MathCursor::pos_type & MathCursor::pos()
872 return cursor().pos_;
876 bool MathCursor::inMacroMode() const
878 return macroNamePos() != -1;
882 bool MathCursor::inMacroArgMode() const
884 return pos() > 0 && prevAtom()->getChar() == '#';
888 bool MathCursor::selection() const
894 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
896 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
897 MathGridInset * p = Cursor_[i].par_->asGridInset();
899 idx = Cursor_[i].idx_;
901 lyxerr << "found grid and idx: " << idx << "\n";
908 void MathCursor::popToEnclosingGrid()
910 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
915 void MathCursor::pullArg(bool goright)
918 MathArray a = array();
920 MathScriptInset const * p = par()->asScriptInset();
922 // special handling for scripts
923 const bool up = p->hasUp();
925 MathScriptInset * q = nextAtom()->asScriptInset();
929 array().insert(pos(), a);
935 array().insert(pos(), a);
939 formula()->mutateToText();
944 void MathCursor::touch()
946 cursor_type::const_iterator it = Cursor_.begin();
947 cursor_type::const_iterator et = Cursor_.end();
948 for ( ; it != et; ++it)
953 void MathCursor::normalize()
958 MathIterator it = ibegin(formula()->par().nucleus());
959 MathIterator et = iend(formula()->par().nucleus());
960 for (; it != et; ++it)
961 if (it.par()->asBoxInset())
962 it.par()->asBoxInset()->rebreak();
966 if (idx() >= par()->nargs()) {
967 lyxerr << "this should not really happen - 1: "
968 << idx() << " " << par()->nargs() << "\n";
971 idx() = min(idx(), par()->nargs() - 1);
973 if (pos() > size()) {
974 lyxerr << "this should not really happen - 2: "
975 << pos() << " " << size() << " in idx: " << idx()
977 WriteStream wi(lyxerr, false, true);
982 pos() = min(pos(), size());
984 // remove empty scripts if possible
985 for (pos_type i = 0; i < size(); ++i) {
986 MathScriptInset * p = array().at(i)->asScriptInset();
988 p->removeEmptyScripts();
994 // fix again position
995 pos() = min(pos(), size());
999 MathCursor::size_type MathCursor::size() const
1001 return array().size();
1005 MathCursor::col_type MathCursor::hullCol() const
1007 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
1011 MathCursor::row_type MathCursor::hullRow() const
1013 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1017 bool MathCursor::hasPrevAtom() const
1023 bool MathCursor::hasNextAtom() const
1025 return pos() < size();
1029 MathAtom const & MathCursor::prevAtom() const
1031 lyx::Assert(pos() > 0);
1032 return array().at(pos() - 1);
1036 MathAtom & MathCursor::prevAtom()
1038 lyx::Assert(pos() > 0);
1039 return array().at(pos() - 1);
1043 MathAtom const & MathCursor::nextAtom() const
1045 lyx::Assert(pos() < size());
1046 return array().at(pos());
1050 MathAtom & MathCursor::nextAtom()
1052 lyx::Assert(pos() < size());
1053 return array().at(pos());
1057 MathArray & MathCursor::array() const
1059 static MathArray dummy;
1061 if (idx() >= par()->nargs()) {
1062 lyxerr << "############ idx_ " << idx() << " not valid\n";
1066 if (Cursor_.size() == 0) {
1067 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1071 return cursor().cell();
1075 MathXArray & MathCursor::xarray() const
1077 static MathXArray dummy;
1079 if (Cursor_.size() == 0) {
1080 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1084 return cursor().xcell();
1088 void MathCursor::idxNext()
1090 par()->idxNext(idx(), pos());
1094 void MathCursor::idxPrev()
1096 par()->idxPrev(idx(), pos());
1100 void MathCursor::splitCell()
1102 if (idx() + 1 == par()->nargs())
1104 MathArray ar = array();
1106 array().erase(pos(), size());
1109 array().insert(0, ar);
1113 void MathCursor::breakLine()
1115 // leave inner cells
1119 MathHullInset * p = formula()->par()->asHullInset();
1123 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1124 p->mutate(LM_OT_EQNARRAY);
1128 p->addRow(hullRow());
1131 const row_type r = hullRow();
1132 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1133 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1137 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1142 //void MathCursor::readLine(MathArray & ar) const
1144 // idx_type base = row() * par()->ncols();
1145 // for (idx_type off = 0; off < par()->ncols(); ++off)
1146 // ar.push_back(par()->cell(base + off));
1150 char MathCursor::valign() const
1153 MathGridInset * p = enclosingGrid(idx);
1154 return p ? p->valign() : '\0';
1158 char MathCursor::halign() const
1161 MathGridInset * p = enclosingGrid(idx);
1162 return p ? p->halign(idx % p->ncols()) : '\0';
1166 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1168 MathCursorPos anc = normalAnchor();
1169 if (anc < cursor()) {
1179 MathCursorPos & MathCursor::cursor()
1181 lyx::Assert(Cursor_.size());
1182 return Cursor_.back();
1186 MathCursorPos const & MathCursor::cursor() const
1188 lyx::Assert(Cursor_.size());
1189 return Cursor_.back();
1193 bool MathCursor::goUpDown(bool up)
1195 // Be warned: The 'logic' implemented in this function is highly fragile.
1196 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1197 // So fiddle around with it only if you know what you are doing!
1198 int xlow, xhigh, ylow, yhigh;
1203 // try neigbouring script insets
1205 if (hasPrevAtom()) {
1206 MathScriptInset * p = prevAtom()->asScriptInset();
1207 if (p && p->has(up)) {
1210 idx() = up; // the superscript has index 1
1212 ///lyxerr << "updown: handled by scriptinset to the left\n";
1218 if (hasNextAtom()) {
1219 MathScriptInset * p = nextAtom()->asScriptInset();
1220 if (p && p->has(up)) {
1224 ///lyxerr << "updown: handled by scriptinset to the right\n";
1230 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1235 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1236 // lyxerr << "updown: handled by brute find in the same cell\n";
1240 // try to find an inset that knows better then we
1242 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1244 if (par()->idxUpDown(idx(), up)) {
1245 // we found a cell that thinks it has something "below" us.
1246 ///lyxerr << "updown: found inset that handles UpDown\n";
1247 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1248 // project (xo,yo) onto proper box
1249 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1250 /// << "\n xlow: " << xlow << " ylow: " << ylow
1251 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1252 xo = min(max(xo, xlow), xhigh);
1253 yo = min(max(yo, ylow), yhigh);
1254 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1255 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1256 ///lyxerr << "updown: handled by final brute find\n";
1262 // no such inset found, just take something "above"
1263 ///lyxerr << "updown: handled by strange case\n";
1268 up ? formula()->ylow() : yo + 4,
1269 up ? yo - 4 : formula()->yhigh()
1273 // any improvement so far?
1276 if (up ? ynew < yo : ynew > yo)
1282 bool MathCursor::bruteFind
1283 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1285 cursor_type best_cursor;
1286 double best_dist = 1e10;
1288 MathIterator it = ibegin(formula()->par().nucleus());
1289 MathIterator et = iend(formula()->par().nucleus());
1291 // avoid invalid nesting when selecting
1292 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1293 MathCursorPos const & top = it.position();
1294 int xo = top.xpos();
1295 int yo = top.ypos();
1296 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1297 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1298 // '<=' in order to take the last possible position
1299 // this is important for clicking behind \sum in e.g. '\sum_i a'
1300 if (d <= best_dist) {
1302 best_cursor = it.cursor();
1312 if (best_dist < 1e10)
1313 Cursor_ = best_cursor;
1314 return best_dist < 1e10;
1318 bool MathCursor::idxLeft()
1320 return par()->idxLeft(idx(), pos());
1324 bool MathCursor::idxRight()
1326 return par()->idxRight(idx(), pos());
1330 bool MathCursor::interpret(string const & s)
1332 //lyxerr << "interpret 1: '" << s << "'\n";
1336 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1337 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1338 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1340 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1342 istringstream is(s.substr(5).c_str());
1345 niceInsert(MathAtom(new MathCasesInset(n)));
1349 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1354 istringstream is(s.substr(6).c_str());
1355 is >> m >> n >> v_align >> h_align;
1359 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1363 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1365 istringstream is(s.substr(7).c_str());
1368 mathed_parse_cell(rep.from, from);
1369 mathed_parse_cell(rep.to, to);
1370 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1371 par()->replace(rep);
1375 string name = s.substr(1);
1377 if (name == "over" || name == "choose" || name == "atop") {
1378 MathArray ar = array();
1379 MathAtom t(createMathInset(name));
1380 t->asNestInset()->cell(0).swap(array());
1388 latexkeys const * l = in_word_set(name);
1389 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1390 lastcode_ = static_cast<MathTextCodes>(l->id);
1394 // prevent entering of recursive macros
1395 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1396 && formula()->getInsetName() == name)
1398 lyxerr << "can't enter recursive macro\n";
1402 niceInsert(createMathInset(name));
1407 bool MathCursor::script(bool up)
1411 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1412 prevAtom()->asScriptInset()->ensure(up);
1413 pushRight(prevAtom());
1416 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1417 nextAtom()->asScriptInset()->ensure(up);
1418 pushLeft(nextAtom());
1422 plainInsert(MathAtom(new MathScriptInset(up)));
1423 prevAtom()->asScriptInset()->ensure(up);
1424 pushRight(prevAtom());
1434 bool MathCursor::interpret(char c)
1436 //lyxerr << "interpret 2: '" << c << "'\n";
1437 if (inMacroArgMode()) {
1441 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1442 if (p && 1 <= n && n <= p->numargs())
1443 insert(MathAtom(new MathMacroArgument(c - '0', lastcode_)));
1445 insert(MathAtom(new MathSpecialCharInset('#')));
1446 interpret(c); // try again
1452 if (inMacroMode()) {
1453 string name = macroName();
1454 //lyxerr << "interpret name: '" << name << "'\n";
1456 // extend macro name if possible
1458 insert(c, LM_TC_TEX);
1462 // leave macro mode if explicitly requested
1468 // handle 'special char' macros
1473 interpret("\\backslash");
1475 interpret(string("\\") + c);
1479 // leave macro mode and try again
1489 // fall through in the other cases
1492 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1493 // suppress direct insertion of two spaces in a row
1494 // the still allows typing '<space>a<space>' and deleting the 'a', but
1495 // it is better than nothing...
1496 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1498 insert(c, LM_TC_TEXTRM);
1503 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1504 prevAtom()->asSpaceInset()->incSpace();
1509 // if are at the very end, leave the formula
1510 return pos() != size();
1514 insert(c, LM_TC_TEX);
1519 if (c == '{' || c == '}', c)) {
1520 insert(c, LM_TC_TEX);
1526 niceInsert(MathAtom(new MathBraceInset));
1534 if (c == '$' || c == '%') {
1535 insert(MathAtom(new MathSpecialCharInset(c)));
1536 lastcode_ = LM_TC_VAR;
1540 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1541 insert(c, LM_TC_VAR);
1545 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1546 insert(c, LM_TC_VAR);
1547 lastcode_ = LM_TC_VAR;
1552 insert(c, LM_TC_TEX);
1553 //bv->owner()->message(_("TeX mode"));
1557 // no special circumstances, so insert the character without any fuss
1558 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1559 lastcode_ = LM_TC_MIN;
1565 MathCursorPos MathCursor::normalAnchor() const
1567 if (Anchor_.size() < Cursor_.size()) {
1569 lyxerr << "unusual Anchor size\n";
1572 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1573 // use Anchor on the same level as Cursor
1574 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1575 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1576 // anchor is behind cursor -> move anchor behind the inset
1583 void MathCursor::stripFromLastEqualSign()
1585 // find position of last '=' in the array
1586 MathArray & ar = cursor().cell();
1587 MathArray::const_iterator et = ar.end();
1588 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1589 if ((*it)->getChar() == '=')
1592 // delete everything behind this position
1593 ar.erase(et - ar.begin(), ar.size());
1598 void MathCursor::setSelection(cursor_type const & where, size_type n)
1607 string MathCursor::info() const
1611 prevAtom()->infoize(os);
1612 return os.str().c_str(); // .c_str() needed for lyxstring