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 bool MathCursor::isInside(MathInset const * p) const
254 for (unsigned i = 0; i < Cursor_.size(); ++i)
255 if (Cursor_[i].par_ == p)
261 bool MathCursor::openable(MathAtom const & t, bool sel) const
266 if (t->asScriptInset())
270 // we can't move into anything new during selection
271 if (Cursor_.size() == Anchor_.size())
273 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
280 bool MathCursor::posLeft()
289 bool MathCursor::posRight()
298 bool MathCursor::left(bool sel)
303 lastcode_ = LM_TC_MIN;
307 lastcode_ = LM_TC_MIN;
309 if (hasPrevAtom() && openable(prevAtom(), sel)) {
310 pushRight(prevAtom());
314 return posLeft() || idxLeft() || popLeft() || selection_;
318 bool MathCursor::right(bool sel)
323 lastcode_ = LM_TC_MIN;
327 lastcode_ = LM_TC_MIN;
329 if (hasNextAtom() && openable(nextAtom(), sel)) {
330 pushLeft(nextAtom());
334 return posRight() || idxRight() || popRight() || selection_;
338 void MathCursor::first()
341 pushLeft(formula_->par());
345 void MathCursor::last()
352 bool positionable(MathCursor::cursor_type const & cursor,
353 MathCursor::cursor_type const & anchor)
355 // avoid deeper nested insets when selecting
356 if (cursor.size() > anchor.size())
359 // anchor might be deeper, should have same path then
360 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
361 if (cursor[i].par_ != anchor[i].par_)
364 // position should be ok.
369 void MathCursor::setPos(int x, int y)
372 bool res = bruteFind(x, y,
373 formula()->xlow(), formula()->xhigh(),
374 formula()->ylow(), formula()->yhigh());
376 // this ccan happen on creation of "math-display"
385 void MathCursor::home(bool sel)
390 lastcode_ = LM_TC_MIN;
391 if (!par()->idxHome(idx(), pos()))
397 void MathCursor::end(bool sel)
402 lastcode_ = LM_TC_MIN;
403 if (!par()->idxEnd(idx(), pos()))
409 void MathCursor::plainErase()
411 array().erase(pos());
415 void MathCursor::markInsert()
417 //lyxerr << "inserting mark\n";
418 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
422 void MathCursor::markErase()
424 //lyxerr << "deleting mark\n";
425 array().erase(pos());
429 void MathCursor::plainInsert(MathAtom const & t)
431 array().insert(pos(), t);
436 void MathCursor::insert(char c, MathTextCodes t)
438 //lyxerr << "inserting '" << c << "'\n";
439 plainInsert(MathAtom(new MathCharInset(c, t)));
443 void MathCursor::insert(char c)
445 insert(c, lastcode_);
449 void MathCursor::insert(MathAtom const & t)
464 void MathCursor::niceInsert(MathAtom const & t)
467 insert(t); // inserting invalidates the pointer!
468 MathAtom const & p = prevAtom();
471 right(); // do not push for e.g. MathSymbolInset
477 void MathCursor::insert(MathArray const & ar)
483 array().insert(pos(), ar);
488 void MathCursor::paste(MathArray const & ar)
492 array().insert(pos(), ar);
497 void MathCursor::backspace()
509 MathScriptInset * p = prevAtom()->asScriptInset();
511 p->removeScript(p->hasUp());
512 // Don't delete if there is anything left
513 if (p->hasUp() || p->hasDown())
522 void MathCursor::erase()
532 // delete empty cells if possible
534 if (par()->idxDelete(idx()))
537 // old behaviour when in last position of cell
538 if (pos() == size()) {
539 par()->idxGlue(idx());
543 MathScriptInset * p = nextAtom()->asScriptInset();
545 p->removeScript(p->hasUp());
546 // Don't delete if there is anything left
547 if (p->hasUp() || p->hasDown())
555 void MathCursor::delLine()
564 if (par()->nrows() > 1) {
565 // grid are the only things with more than one row...
566 lyx::Assert(par()->asGridInset());
567 par()->asGridInset()->delRow(hullRow());
570 if (idx() >= par()->nargs())
571 idx() = par()->nargs() - 1;
578 bool MathCursor::up(bool sel)
583 cursor_type save = Cursor_;
591 bool MathCursor::down(bool sel)
596 cursor_type save = Cursor_;
604 bool MathCursor::toggleLimits()
608 MathScriptInset * t = nextAtom()->asScriptInset();
611 int old = t->limits();
612 t->limits(old < 0 ? 1 : -1);
613 return old != t->limits();
617 void MathCursor::macroModeClose()
619 MathInset::difference_type const t = macroNamePos();
622 string s = macroName();
623 array().erase(t, pos());
630 MathInset::difference_type MathCursor::macroNamePos() const
632 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
633 MathAtom & p = array().at(i);
634 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
641 string MathCursor::macroName() const
644 MathInset::difference_type i = macroNamePos();
645 for (; i >= 0 && i < int(pos()); ++i)
646 s += array().at(i)->getChar();
651 void MathCursor::selCopy()
655 theSelection.grab(*this);
661 void MathCursor::selCut()
665 theSelection.grab(*this);
666 theSelection.erase(*this);
669 theSelection.clear();
674 void MathCursor::selDel()
678 theSelection.erase(*this);
686 void MathCursor::selPaste()
689 theSelection.paste(*this);
690 //theSelection.grab(*this);
695 void MathCursor::selHandle(bool sel)
697 if (sel == selection_)
699 //theSelection.clear();
705 void MathCursor::selStart()
708 //theSelection.clear();
715 void MathCursor::selClear()
723 void MathCursor::selGet(MathArray & ar)
729 theSelection.grab(*this);
730 ar = theSelection.glue();
735 void MathCursor::drawSelection(Painter & pain) const
742 getSelection(i1, i2);
744 if (i1.idx_ == i2.idx_) {
745 MathXArray & c = i1.xcell();
746 int x1 = c.xo() + c.pos2x(i1.pos_);
747 int y1 = c.yo() - c.ascent();
748 int x2 = c.xo() + c.pos2x(i2.pos_);
749 int y2 = c.yo() + c.descent();
750 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
752 vector<MathInset::idx_type> indices
753 = i1.par_->idxBetween(i1.idx_, i2.idx_);
754 for (unsigned i = 0; i < indices.size(); ++i) {
755 MathXArray & c = i1.xcell(indices[i]);
757 int y1 = c.yo() - c.ascent();
758 int x2 = c.xo() + c.width();
759 int y2 = c.yo() + c.descent();
760 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
765 // draw anchor if different from selection boundary
766 MathCursorPos anc = Anchor_.back();
767 if (anc != i1 && anc != i2) {
768 MathXArray & c = anc.xcell();
769 int x = c.xo() + c.pos2x(anc.pos_);
770 int y1 = c.yo() - c.ascent();
771 int y2 = c.yo() + c.descent();
772 pain.line(x, y1, x, y2, LColor::math);
778 void MathCursor::handleFont(MathTextCodes t)
784 getSelection(i1, i2);
785 if (i1.idx_ == i2.idx_) {
786 MathArray & ar = i1.cell();
787 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
788 ar.at(pos)->handleFont(t);
791 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
795 void MathCursor::handleDelim(string const & l, string const & r)
797 handleNest(new MathDelimInset(l, r));
801 void MathCursor::handleNest(MathInset * p)
805 p->cell(0) = theSelection.glue();
807 insert(MathAtom(p)); // this invalidates p!
808 pushRight(prevAtom());
812 void MathCursor::getPos(int & x, int & y)
815 #warning This should probably take cellXOffset and cellYOffset into account
817 x = xarray().xo() + xarray().pos2x(pos());
818 // move cursor visually into empty cells ("blue rectangles");
825 MathInset * MathCursor::par() const
827 return cursor().par_;
831 InsetFormulaBase * MathCursor::formula()
837 MathCursor::idx_type MathCursor::idx() const
839 return cursor().idx_;
843 MathCursor::idx_type & MathCursor::idx()
845 return cursor().idx_;
849 MathCursor::pos_type MathCursor::pos() const
851 return cursor().pos_;
855 MathCursor::pos_type & MathCursor::pos()
857 return cursor().pos_;
861 bool MathCursor::inMacroMode() const
863 return macroNamePos() != -1;
867 bool MathCursor::inMacroArgMode() const
869 return pos() > 0 && prevAtom()->getChar() == '#';
873 bool MathCursor::selection() const
879 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
881 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
882 MathGridInset * p = Cursor_[i].par_->asGridInset();
884 idx = Cursor_[i].idx_;
886 lyxerr << "found grid and idx: " << idx << "\n";
893 void MathCursor::popToEnclosingGrid()
895 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
900 void MathCursor::pullArg(bool goright)
903 MathArray a = array();
905 MathScriptInset const * p = par()->asScriptInset();
907 // special handling for scripts
908 const bool up = p->hasUp();
910 MathScriptInset * q = nextAtom()->asScriptInset();
914 array().insert(pos(), a);
920 array().insert(pos(), a);
924 formula()->mutateToText();
929 void MathCursor::touch()
931 cursor_type::const_iterator it = Cursor_.begin();
932 cursor_type::const_iterator et = Cursor_.end();
933 for ( ; it != et; ++it)
938 void MathCursor::normalize()
943 MathIterator it = ibegin(formula()->par().nucleus());
944 MathIterator et = iend(formula()->par().nucleus());
945 for (; it != et; ++it)
946 if (it.par()->asBoxInset())
947 it.par()->asBoxInset()->rebreak();
951 if (idx() >= par()->nargs()) {
952 lyxerr << "this should not really happen - 1: "
953 << idx() << " " << par()->nargs() << "\n";
956 idx() = min(idx(), par()->nargs() - 1);
958 if (pos() > size()) {
959 lyxerr << "this should not really happen - 2: "
960 << pos() << " " << size() << " in idx: " << idx()
962 WriteStream wi(lyxerr, false, true);
967 pos() = min(pos(), size());
969 // remove empty scripts if possible
971 for (pos_type i = 0; i < size(); ++i) {
972 MathScriptInset * p = array().at(i)->asScriptInset();
974 p->removeEmptyScripts();
981 // fix again position
982 pos() = min(pos(), size());
986 MathCursor::size_type MathCursor::size() const
988 return array().size();
992 MathCursor::col_type MathCursor::hullCol() const
994 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
998 MathCursor::row_type MathCursor::hullRow() const
1000 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1004 bool MathCursor::hasPrevAtom() const
1010 bool MathCursor::hasNextAtom() const
1012 return pos() < size();
1016 MathAtom const & MathCursor::prevAtom() const
1018 lyx::Assert(pos() > 0);
1019 return array().at(pos() - 1);
1023 MathAtom & MathCursor::prevAtom()
1025 lyx::Assert(pos() > 0);
1026 return array().at(pos() - 1);
1030 MathAtom const & MathCursor::nextAtom() const
1032 lyx::Assert(pos() < size());
1033 return array().at(pos());
1037 MathAtom & MathCursor::nextAtom()
1039 lyx::Assert(pos() < size());
1040 return array().at(pos());
1044 MathArray & MathCursor::array() const
1046 static MathArray dummy;
1048 if (idx() >= par()->nargs()) {
1049 lyxerr << "############ idx_ " << idx() << " not valid\n";
1053 if (Cursor_.size() == 0) {
1054 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1058 return cursor().cell();
1062 MathXArray & MathCursor::xarray() const
1064 static MathXArray dummy;
1066 if (Cursor_.size() == 0) {
1067 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1071 return cursor().xcell();
1075 void MathCursor::idxNext()
1077 par()->idxNext(idx(), pos());
1081 void MathCursor::idxPrev()
1083 par()->idxPrev(idx(), pos());
1087 void MathCursor::splitCell()
1089 if (idx() + 1 == par()->nargs())
1091 MathArray ar = array();
1093 array().erase(pos(), size());
1096 array().insert(0, ar);
1100 void MathCursor::breakLine()
1102 // leave inner cells
1106 MathHullInset * p = formula()->par()->asHullInset();
1110 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1111 p->mutate(LM_OT_EQNARRAY);
1115 p->addRow(hullRow());
1118 const row_type r = hullRow();
1119 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1120 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1124 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1129 //void MathCursor::readLine(MathArray & ar) const
1131 // idx_type base = row() * par()->ncols();
1132 // for (idx_type off = 0; off < par()->ncols(); ++off)
1133 // ar.push_back(par()->cell(base + off));
1137 char MathCursor::valign() const
1140 MathGridInset * p = enclosingGrid(idx);
1141 return p ? p->valign() : '\0';
1145 char MathCursor::halign() const
1148 MathGridInset * p = enclosingGrid(idx);
1149 return p ? p->halign(idx % p->ncols()) : '\0';
1153 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1155 MathCursorPos anc = normalAnchor();
1156 if (anc < cursor()) {
1166 MathCursorPos & MathCursor::cursor()
1168 lyx::Assert(Cursor_.size());
1169 return Cursor_.back();
1173 MathCursorPos const & MathCursor::cursor() const
1175 lyx::Assert(Cursor_.size());
1176 return Cursor_.back();
1180 bool MathCursor::goUpDown(bool up)
1182 // Be warned: The 'logic' implemented in this function is highly fragile.
1183 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1184 // So fiddle around with it only if you know what you are doing!
1185 int xlow, xhigh, ylow, yhigh;
1190 // try neigbouring script insets
1192 if (hasPrevAtom()) {
1193 MathScriptInset * p = prevAtom()->asScriptInset();
1194 if (p && p->has(up)) {
1197 idx() = up; // the superscript has index 1
1199 ///lyxerr << "updown: handled by scriptinset to the left\n";
1205 if (hasNextAtom()) {
1206 MathScriptInset * p = nextAtom()->asScriptInset();
1207 if (p && p->has(up)) {
1211 ///lyxerr << "updown: handled by scriptinset to the right\n";
1217 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1222 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1223 // lyxerr << "updown: handled by brute find in the same cell\n";
1227 // try to find an inset that knows better then we
1229 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1231 if (par()->idxUpDown(idx(), up)) {
1232 // we found a cell that thinks it has something "below" us.
1233 ///lyxerr << "updown: found inset that handles UpDown\n";
1234 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1235 // project (xo,yo) onto proper box
1236 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1237 /// << "\n xlow: " << xlow << " ylow: " << ylow
1238 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1239 xo = min(max(xo, xlow), xhigh);
1240 yo = min(max(yo, ylow), yhigh);
1241 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1242 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1243 ///lyxerr << "updown: handled by final brute find\n";
1249 // no such inset found, just take something "above"
1250 ///lyxerr << "updown: handled by strange case\n";
1255 up ? formula()->ylow() : yo + 4,
1256 up ? yo - 4 : formula()->yhigh()
1260 // any improvement so far?
1263 if (up ? ynew < yo : ynew > yo)
1269 bool MathCursor::bruteFind
1270 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1272 cursor_type best_cursor;
1273 double best_dist = 1e10;
1275 MathIterator it = ibegin(formula()->par().nucleus());
1276 MathIterator et = iend(formula()->par().nucleus());
1278 // avoid invalid nesting when selecting
1279 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1280 MathCursorPos const & top = it.position();
1281 int xo = top.xpos();
1282 int yo = top.ypos();
1283 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1284 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1285 // '<=' in order to take the last possible position
1286 // this is important for clicking behind \sum in e.g. '\sum_i a'
1287 if (d <= best_dist) {
1289 best_cursor = it.cursor();
1299 if (best_dist < 1e10)
1300 Cursor_ = best_cursor;
1301 return best_dist < 1e10;
1305 bool MathCursor::idxLeft()
1307 return par()->idxLeft(idx(), pos());
1311 bool MathCursor::idxRight()
1313 return par()->idxRight(idx(), pos());
1317 bool MathCursor::interpret(string const & s)
1319 //lyxerr << "interpret 1: '" << s << "'\n";
1323 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1324 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1325 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1327 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1329 istringstream is(s.substr(5).c_str());
1332 niceInsert(MathAtom(new MathCasesInset(n)));
1336 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1341 istringstream is(s.substr(6).c_str());
1342 is >> m >> n >> v_align >> h_align;
1346 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1350 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1352 istringstream is(s.substr(7).c_str());
1355 mathed_parse_cell(rep.from, from);
1356 mathed_parse_cell(rep.to, to);
1357 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1358 par()->replace(rep);
1362 string name = s.substr(1);
1364 if (name == "over" || name == "choose" || name == "atop") {
1365 MathArray ar = array();
1366 MathAtom t(createMathInset(name));
1367 t->asNestInset()->cell(0).swap(array());
1375 latexkeys const * l = in_word_set(name);
1376 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1377 lastcode_ = static_cast<MathTextCodes>(l->id);
1381 // prevent entering of recursive macros
1382 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1383 && formula()->getInsetName() == name)
1385 lyxerr << "can't enter recursive macro\n";
1389 niceInsert(createMathInset(name));
1394 bool MathCursor::script(bool up)
1396 // Hack to get \\^ and \\_ working
1397 if (inMacroMode() && macroName() == "\\") {
1399 interpret("\\mathcircumflex");
1407 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1408 prevAtom()->asScriptInset()->ensure(up);
1409 pushRight(prevAtom());
1412 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1413 nextAtom()->asScriptInset()->ensure(up);
1414 pushLeft(nextAtom());
1418 plainInsert(MathAtom(new MathScriptInset(up)));
1419 prevAtom()->asScriptInset()->ensure(up);
1420 pushRight(prevAtom());
1430 bool MathCursor::interpret(char c)
1432 //lyxerr << "interpret 2: '" << c << "'\n";
1433 if (inMacroArgMode()) {
1437 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1438 if (p && 1 <= n && n <= p->numargs())
1439 insert(MathAtom(new MathMacroArgument(c - '0', lastcode_)));
1441 insert(MathAtom(new MathSpecialCharInset('#')));
1442 interpret(c); // try again
1448 if (inMacroMode()) {
1449 string name = macroName();
1450 //lyxerr << "interpret name: '" << name << "'\n";
1452 // extend macro name if possible
1454 insert(c, LM_TC_TEX);
1458 // leave macro mode if explicitly requested
1464 // handle 'special char' macros
1469 interpret("\\backslash");
1471 interpret(string("\\") + c);
1475 // leave macro mode and try again
1485 // fall through in the other cases
1488 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1489 // suppress direct insertion of two spaces in a row
1490 // the still allows typing '<space>a<space>' and deleting the 'a', but
1491 // it is better than nothing...
1492 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1494 insert(c, LM_TC_TEXTRM);
1499 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1500 prevAtom()->asSpaceInset()->incSpace();
1505 // if are at the very end, leave the formula
1506 return pos() != size();
1510 insert(c, LM_TC_TEX);
1515 if (c == '{' || c == '}', c)) {
1516 insert(c, LM_TC_TEX);
1522 niceInsert(MathAtom(new MathBraceInset));
1530 if (c == '$' || c == '%') {
1531 insert(MathAtom(new MathSpecialCharInset(c)));
1532 lastcode_ = LM_TC_VAR;
1536 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1537 insert(c, LM_TC_VAR);
1541 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1542 insert(c, LM_TC_VAR);
1543 lastcode_ = LM_TC_VAR;
1548 insert(c, LM_TC_TEX);
1549 //bv->owner()->message(_("TeX mode"));
1553 // no special circumstances, so insert the character without any fuss
1554 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1555 lastcode_ = LM_TC_MIN;
1561 MathCursorPos MathCursor::normalAnchor() const
1563 if (Anchor_.size() < Cursor_.size()) {
1565 lyxerr << "unusual Anchor size\n";
1568 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1569 // use Anchor on the same level as Cursor
1570 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1571 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1572 // anchor is behind cursor -> move anchor behind the inset
1579 void MathCursor::stripFromLastEqualSign()
1581 // find position of last '=' in the array
1582 MathArray & ar = cursor().cell();
1583 MathArray::const_iterator et = ar.end();
1584 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1585 if ((*it)->getChar() == '=')
1588 // delete everything behind this position
1589 ar.erase(et - ar.begin(), ar.size());
1594 void MathCursor::setSelection(cursor_type const & where, size_type n)
1603 string MathCursor::info() const
1607 prevAtom()->infoize(os);
1608 return os.str().c_str(); // .c_str() needed for lyxstring