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.
22 #pragma implementation
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
29 #include "frontends/Painter.h"
30 #include "math_cursor.h"
31 #include "formulabase.h"
32 #include "math_autocorrect.h"
33 #include "math_arrayinset.h"
34 #include "math_braceinset.h"
35 #include "math_boxinset.h"
36 #include "math_casesinset.h"
37 #include "math_charinset.h"
38 #include "math_deliminset.h"
39 #include "math_factory.h"
40 #include "math_hullinset.h"
41 #include "math_iterator.h"
42 #include "math_macroarg.h"
43 #include "math_macrotemplate.h"
44 #include "math_mathmlstream.h"
45 #include "math_parser.h"
46 #include "math_replace.h"
47 #include "math_scriptinset.h"
48 #include "math_spaceinset.h"
49 #include "math_specialcharinset.h"
50 #include "math_support.h"
51 #include "math_unknowninset.h"
63 using std::ostringstream;
71 typedef MathInset::col_type col_type;
72 typedef MathInset::row_type row_type;
73 typedef MathInset::idx_type idx_type;
79 void region(MathCursorPos const & i1, MathCursorPos const & i2,
80 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
82 MathInset * p = i1.par_;
93 void grab(MathCursor const & cursor)
97 cursor.getSelection(i1, i2);
98 // shouldn'tt we assert on i1.par_ == i2.par_?
99 if (i1.idx_ == i2.idx_) {
100 data_ = MathGridInset(1, 1);
101 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
105 region(i1, i2, r1, r2, c1, c2);
106 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
107 for (row_type row = 0; row < data_.nrows(); ++row)
108 for (col_type col = 0; col < data_.ncols(); ++col) {
109 idx_type i = i1.par_->index(row + r1, col + c1);
110 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
115 void erase(MathCursor & cursor)
119 cursor.getSelection(i1, i2);
120 if (i1.idx_ == i2.idx_)
121 i1.cell().erase(i1.pos_, i2.pos_);
123 MathInset * p = i1.par_;
126 region(i1, i2, r1, r2, c1, c2);
127 for (row_type row = r1; row <= r2; ++row)
128 for (col_type col = c1; col <= c2; ++col)
129 p->cell(p->index(row, col)).erase();
131 cursor.cursor() = i1;
134 void paste(MathCursor & cursor) const
136 if (data_.nargs() == 1) {
137 // single cell/part of cell
138 cursor.paste(data_.cell(0));
141 idx_type idx; // index of upper left cell
142 MathGridInset * p = cursor.enclosingGrid(idx);
143 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
144 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
145 for (row_type row = 0; row < numrows; ++row) {
146 for (col_type col = 0; col < numcols; ++col) {
147 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
148 p->cell(i).push_back(data_.cell(data_.index(row, col)));
150 // append the left over horizontal cells to the last column
151 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
152 for (col_type col = numcols; col < data_.ncols(); ++col)
153 p->cell(i).push_back(data_.cell(data_.index(row, col)));
155 // append the left over vertical cells to the last _cell_
156 idx_type i = p->nargs() - 1;
157 for (row_type row = numrows; row < data_.nrows(); ++row)
158 for (col_type col = 0; col < data_.ncols(); ++col)
159 p->cell(i).push_back(data_.cell(data_.index(row, col)));
163 // glues selection to one cell
164 MathArray glue() const
167 for (unsigned i = 0; i < data_.nargs(); ++i)
168 ar.push_back(data_.cell(i));
174 data_ = MathGridInset(1, 1);
181 Selection theSelection;
188 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
189 : formula_(formula), autocorrect_(false), selection_(false)
191 left ? first() : last();
195 void MathCursor::push(MathAtom & t)
197 Cursor_.push_back(MathCursorPos(t.nucleus()));
201 void MathCursor::pushLeft(MathAtom & t)
203 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
205 t->idxFirst(idx(), pos());
209 void MathCursor::pushRight(MathAtom & t)
211 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
214 t->idxLast(idx(), pos());
218 bool MathCursor::popLeft()
220 //cerr << "Leaving atom to the left\n";
221 if (Cursor_.size() <= 1)
228 bool MathCursor::popRight()
230 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
231 if (Cursor_.size() <= 1)
241 void MathCursor::dump(char const * what) const
243 lyxerr << "MC: " << what << "\n";
244 lyxerr << " Cursor: " << Cursor_.size() << "\n";
245 for (unsigned i = 0; i < Cursor_.size(); ++i)
246 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
247 lyxerr << " Anchor: " << Anchor_.size() << "\n";
248 for (unsigned i = 0; i < Anchor_.size(); ++i)
249 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
250 lyxerr << " sel: " << selection_ << "\n";
253 void MathCursor::dump(char const *) const {}
257 bool MathCursor::isInside(MathInset const * p) const
259 for (unsigned i = 0; i < Cursor_.size(); ++i)
260 if (Cursor_[i].par_ == p)
266 bool MathCursor::openable(MathAtom const & t, bool sel) const
271 if (t->asScriptInset())
275 // we can't move into anything new during selection
276 if (Cursor_.size() == Anchor_.size())
278 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
285 bool MathCursor::posLeft()
294 bool MathCursor::posRight()
303 bool MathCursor::left(bool sel)
306 autocorrect_ = false;
314 if (hasPrevAtom() && openable(prevAtom(), sel)) {
315 pushRight(prevAtom());
319 return posLeft() || idxLeft() || popLeft() || selection_;
323 bool MathCursor::right(bool sel)
326 autocorrect_ = false;
334 if (hasNextAtom() && openable(nextAtom(), sel)) {
335 pushLeft(nextAtom());
339 return posRight() || idxRight() || popRight() || selection_;
343 void MathCursor::first()
346 pushLeft(formula_->par());
350 void MathCursor::last()
357 bool positionable(MathCursor::cursor_type const & cursor,
358 MathCursor::cursor_type const & anchor)
360 // avoid deeper nested insets when selecting
361 if (cursor.size() > anchor.size())
364 // anchor might be deeper, should have same path then
365 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
366 if (cursor[i].par_ != anchor[i].par_)
369 // position should be ok.
374 void MathCursor::setPos(int x, int y)
377 bool res = bruteFind(x, y,
378 formula()->xlow(), formula()->xhigh(),
379 formula()->ylow(), formula()->yhigh());
381 // this ccan happen on creation of "math-display"
390 void MathCursor::home(bool sel)
393 autocorrect_ = false;
396 if (!par()->idxHome(idx(), pos()))
402 void MathCursor::end(bool sel)
405 autocorrect_ = false;
408 if (!par()->idxEnd(idx(), pos()))
414 void MathCursor::plainErase()
416 array().erase(pos());
420 void MathCursor::markInsert()
422 //lyxerr << "inserting mark\n";
423 array().insert(pos(), MathAtom(new MathCharInset(0)));
427 void MathCursor::markErase()
429 //lyxerr << "deleting mark\n";
430 array().erase(pos());
434 void MathCursor::plainInsert(MathAtom const & t)
436 array().insert(pos(), t);
441 void MathCursor::insert(char c)
443 //lyxerr << "inserting '" << c << "'\n";
445 plainInsert(MathAtom(new MathCharInset(c)));
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()
499 autocorrect_ = false;
510 MathScriptInset * p = prevAtom()->asScriptInset();
512 p->removeScript(p->hasUp());
513 // Don't delete if there is anything left
514 if (p->hasUp() || p->hasDown())
523 void MathCursor::erase()
525 autocorrect_ = false;
534 // delete empty cells if possible
536 if (par()->idxDelete(idx()))
539 // old behaviour when in last position of cell
540 if (pos() == size()) {
541 par()->idxGlue(idx());
545 MathScriptInset * p = nextAtom()->asScriptInset();
547 p->removeScript(p->hasUp());
548 // Don't delete if there is anything left
549 if (p->hasUp() || p->hasDown())
557 void MathCursor::delLine()
559 autocorrect_ = false;
567 if (par()->nrows() > 1) {
568 // grid are the only things with more than one row...
569 lyx::Assert(par()->asGridInset());
570 par()->asGridInset()->delRow(hullRow());
573 if (idx() >= par()->nargs())
574 idx() = par()->nargs() - 1;
581 bool MathCursor::up(bool sel)
586 cursor_type save = Cursor_;
590 autocorrect_ = false;
595 bool MathCursor::down(bool sel)
600 cursor_type save = Cursor_;
604 autocorrect_ = false;
609 bool MathCursor::toggleLimits()
613 MathScriptInset * t = nextAtom()->asScriptInset();
616 int old = t->limits();
617 t->limits(old < 0 ? 1 : -1);
618 return old != t->limits();
622 void MathCursor::macroModeClose()
624 MathUnknownInset * p = inMacroMode();
628 string s = p->name();
630 array().erase(pos());
636 string MathCursor::macroName() const
638 return inMacroMode() ? inMacroMode()->name() : "";
642 void MathCursor::selCopy()
646 theSelection.grab(*this);
652 void MathCursor::selCut()
656 theSelection.grab(*this);
657 theSelection.erase(*this);
660 theSelection.clear();
665 void MathCursor::selDel()
669 theSelection.erase(*this);
677 void MathCursor::selPaste()
681 theSelection.paste(*this);
682 //theSelection.grab(*this);
687 void MathCursor::selHandle(bool sel)
689 if (sel == selection_)
691 //theSelection.clear();
697 void MathCursor::selStart()
700 //theSelection.clear();
707 void MathCursor::selClear()
715 void MathCursor::selClearOrDel()
717 if (lyxrc.auto_region_delete)
724 void MathCursor::selGet(MathArray & ar)
730 theSelection.grab(*this);
731 ar = theSelection.glue();
736 void MathCursor::drawSelection(MathPainterInfo & pain) const
743 getSelection(i1, i2);
745 if (i1.idx_ == i2.idx_) {
746 MathXArray & c = i1.xcell();
747 int x1 = c.xo() + c.pos2x(i1.pos_);
748 int y1 = c.yo() - c.ascent();
749 int x2 = c.xo() + c.pos2x(i2.pos_);
750 int y2 = c.yo() + c.descent();
751 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
753 vector<MathInset::idx_type> indices
754 = i1.par_->idxBetween(i1.idx_, i2.idx_);
755 for (unsigned i = 0; i < indices.size(); ++i) {
756 MathXArray & c = i1.xcell(indices[i]);
758 int y1 = c.yo() - c.ascent();
759 int x2 = c.xo() + c.width();
760 int y2 = c.yo() + c.descent();
761 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
766 // draw anchor if different from selection boundary
767 MathCursorPos anc = Anchor_.back();
768 if (anc != i1 && anc != i2) {
769 MathXArray & c = anc.xcell();
770 int x = c.xo() + c.pos2x(anc.pos_);
771 int y1 = c.yo() - c.ascent();
772 int y2 = c.yo() + c.descent();
773 pain.line(x, y1, x, y2, LColor::math);
779 void MathCursor::handleDelim(string const & l, string const & r)
781 handleNest(new MathDelimInset(l, r));
785 void MathCursor::handleNest(MathInset * p)
789 p->cell(0) = theSelection.glue();
791 insert(MathAtom(p)); // this invalidates p!
792 pushRight(prevAtom());
796 void MathCursor::getPos(int & x, int & y)
799 #warning This should probably take cellXOffset and cellYOffset into account
801 x = xarray().xo() + xarray().pos2x(pos());
802 // move cursor visually into empty cells ("blue rectangles");
809 MathInset * MathCursor::par() const
811 return cursor().par_;
815 InsetFormulaBase * MathCursor::formula()
821 MathCursor::idx_type MathCursor::idx() const
823 return cursor().idx_;
827 MathCursor::idx_type & MathCursor::idx()
829 return cursor().idx_;
833 MathCursor::pos_type MathCursor::pos() const
835 return cursor().pos_;
839 MathCursor::pos_type & MathCursor::pos()
841 return cursor().pos_;
845 MathUnknownInset * MathCursor::inMacroMode() const
849 MathUnknownInset * p = prevAtom()->asUnknownInset();
850 return (p && !p->final()) ? p : 0;
854 bool MathCursor::inMacroArgMode() const
856 return pos() > 0 && prevAtom()->getChar() == '#';
860 bool MathCursor::selection() const
866 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
868 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
869 MathGridInset * p = Cursor_[i].par_->asGridInset();
871 idx = Cursor_[i].idx_;
873 lyxerr << "found grid and idx: " << idx << "\n";
880 void MathCursor::popToEnclosingGrid()
882 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
887 void MathCursor::pullArg(bool goright)
890 MathArray a = array();
892 MathScriptInset const * p = par()->asScriptInset();
894 // special handling for scripts
895 const bool up = p->hasUp();
897 MathScriptInset * q = nextAtom()->asScriptInset();
901 array().insert(pos(), a);
907 array().insert(pos(), a);
911 formula()->mutateToText();
916 void MathCursor::touch()
918 cursor_type::const_iterator it = Cursor_.begin();
919 cursor_type::const_iterator et = Cursor_.end();
920 for ( ; it != et; ++it)
925 void MathCursor::normalize()
930 MathIterator it = ibegin(formula()->par().nucleus());
931 MathIterator et = iend(formula()->par().nucleus());
932 for (; it != et; ++it)
933 if (it.par()->asBoxInset())
934 it.par()->asBoxInset()->rebreak();
938 if (idx() >= par()->nargs()) {
939 lyxerr << "this should not really happen - 1: "
940 << idx() << " " << par()->nargs() << "\n";
943 idx() = min(idx(), par()->nargs() - 1);
945 if (pos() > size()) {
946 lyxerr << "this should not really happen - 2: "
947 << pos() << " " << size() << " in idx: " << idx()
949 WriteStream wi(lyxerr, false, true);
954 pos() = min(pos(), size());
956 // remove empty scripts if possible
958 for (pos_type i = 0; i < size(); ++i) {
959 MathScriptInset * p = array().at(i)->asScriptInset();
961 p->removeEmptyScripts();
968 // fix again position
969 pos() = min(pos(), size());
973 MathCursor::size_type MathCursor::size() const
975 return array().size();
979 MathCursor::col_type MathCursor::hullCol() const
981 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
985 MathCursor::row_type MathCursor::hullRow() const
987 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
991 bool MathCursor::hasPrevAtom() const
997 bool MathCursor::hasNextAtom() const
999 return pos() < size();
1003 MathAtom const & MathCursor::prevAtom() const
1005 lyx::Assert(pos() > 0);
1006 return array().at(pos() - 1);
1010 MathAtom & MathCursor::prevAtom()
1012 lyx::Assert(pos() > 0);
1013 return array().at(pos() - 1);
1017 MathAtom const & MathCursor::nextAtom() const
1019 lyx::Assert(pos() < size());
1020 return array().at(pos());
1024 MathAtom & MathCursor::nextAtom()
1026 lyx::Assert(pos() < size());
1027 return array().at(pos());
1031 MathArray & MathCursor::array() const
1033 static MathArray dummy;
1035 if (idx() >= par()->nargs()) {
1036 lyxerr << "############ idx_ " << idx() << " not valid\n";
1040 if (Cursor_.size() == 0) {
1041 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1045 return cursor().cell();
1049 MathXArray & MathCursor::xarray() const
1051 static MathXArray dummy;
1053 if (Cursor_.size() == 0) {
1054 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1058 return cursor().xcell();
1062 void MathCursor::idxNext()
1064 par()->idxNext(idx(), pos());
1068 void MathCursor::idxPrev()
1070 par()->idxPrev(idx(), pos());
1074 void MathCursor::splitCell()
1076 if (idx() + 1 == par()->nargs())
1078 MathArray ar = array();
1080 array().erase(pos(), size());
1083 array().insert(0, ar);
1087 void MathCursor::breakLine()
1089 // leave inner cells
1093 MathHullInset * p = formula()->par()->asHullInset();
1097 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1098 p->mutate(LM_OT_EQNARRAY);
1102 p->addRow(hullRow());
1105 const row_type r = hullRow();
1106 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1107 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1111 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1116 //void MathCursor::readLine(MathArray & ar) const
1118 // idx_type base = row() * par()->ncols();
1119 // for (idx_type off = 0; off < par()->ncols(); ++off)
1120 // ar.push_back(par()->cell(base + off));
1124 char MathCursor::valign() const
1127 MathGridInset * p = enclosingGrid(idx);
1128 return p ? p->valign() : '\0';
1132 char MathCursor::halign() const
1135 MathGridInset * p = enclosingGrid(idx);
1136 return p ? p->halign(idx % p->ncols()) : '\0';
1140 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1142 MathCursorPos anc = normalAnchor();
1143 if (anc < cursor()) {
1153 MathCursorPos & MathCursor::cursor()
1155 lyx::Assert(Cursor_.size());
1156 return Cursor_.back();
1160 MathCursorPos const & MathCursor::cursor() const
1162 lyx::Assert(Cursor_.size());
1163 return Cursor_.back();
1167 bool MathCursor::goUpDown(bool up)
1169 // Be warned: The 'logic' implemented in this function is highly fragile.
1170 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1171 // So fiddle around with it only if you know what you are doing!
1172 int xlow, xhigh, ylow, yhigh;
1177 // check if we had something else in mind, if not, this is the future goal
1183 // try neigbouring script insets
1185 if (hasPrevAtom()) {
1186 MathScriptInset * p = prevAtom()->asScriptInset();
1187 if (p && p->has(up)) {
1190 idx() = up; // the superscript has index 1
1192 ///lyxerr << "updown: handled by scriptinset to the left\n";
1198 if (hasNextAtom()) {
1199 MathScriptInset * p = nextAtom()->asScriptInset();
1200 if (p && p->has(up)) {
1204 ///lyxerr << "updown: handled by scriptinset to the right\n";
1210 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1215 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1216 // lyxerr << "updown: handled by brute find in the same cell\n";
1220 // try to find an inset that knows better then we
1222 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1224 if (par()->idxUpDown(idx(), up)) {
1225 // we found a cell that thinks it has something "below" us.
1226 ///lyxerr << "updown: found inset that handles UpDown\n";
1227 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1228 // project (xo,yo) onto proper box
1229 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1230 /// << "\n xlow: " << xlow << " ylow: " << ylow
1231 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1232 xo = min(max(xo, xlow), xhigh);
1233 yo = min(max(yo, ylow), yhigh);
1234 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1235 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1236 ///lyxerr << "updown: handled by final brute find\n";
1242 // no such inset found, just take something "above"
1243 ///lyxerr << "updown: handled by strange case\n";
1248 up ? formula()->ylow() : yo + 4,
1249 up ? yo - 4 : formula()->yhigh()
1253 // any improvement so far?
1256 if (up ? ynew < yo : ynew > yo)
1262 bool MathCursor::bruteFind
1263 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1265 cursor_type best_cursor;
1266 double best_dist = 1e10;
1268 MathIterator it = ibegin(formula()->par().nucleus());
1269 MathIterator et = iend(formula()->par().nucleus());
1271 // avoid invalid nesting when selecting
1272 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1273 MathCursorPos const & top = it.position();
1274 int xo = top.xpos();
1275 int yo = top.ypos();
1276 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1277 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1278 // '<=' in order to take the last possible position
1279 // this is important for clicking behind \sum in e.g. '\sum_i a'
1280 if (d <= best_dist) {
1282 best_cursor = it.cursor();
1292 if (best_dist < 1e10)
1293 Cursor_ = best_cursor;
1294 return best_dist < 1e10;
1298 bool MathCursor::idxLeft()
1300 return par()->idxLeft(idx(), pos());
1304 bool MathCursor::idxRight()
1306 return par()->idxRight(idx(), pos());
1310 bool MathCursor::interpret(string const & s)
1312 //lyxerr << "interpret 1: '" << s << "'\n";
1316 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1317 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1318 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1320 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1322 istringstream is(s.substr(5).c_str());
1325 niceInsert(MathAtom(new MathCasesInset(n)));
1329 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1334 istringstream is(s.substr(6).c_str());
1335 is >> m >> n >> v_align >> h_align;
1339 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1343 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1345 istringstream is(s.substr(7).c_str());
1348 mathed_parse_cell(rep.from, from);
1349 mathed_parse_cell(rep.to, to);
1350 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1351 par()->replace(rep);
1355 string name = s.substr(1);
1357 if (name == "over" || name == "choose" || name == "atop") {
1358 MathArray ar = array();
1359 MathAtom t(createMathInset(name));
1360 t->asNestInset()->cell(0).swap(array());
1368 // prevent entering of recursive macros
1369 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1370 && formula()->getInsetName() == name)
1372 lyxerr << "can't enter recursive macro\n";
1376 niceInsert(createMathInset(name));
1381 bool MathCursor::script(bool up)
1383 // Hack to get \\^ and \\_ working
1384 if (inMacroMode() && macroName() == "\\") {
1386 interpret("\\mathcircumflex");
1394 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1395 prevAtom()->asScriptInset()->ensure(up);
1396 pushRight(prevAtom());
1399 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1400 nextAtom()->asScriptInset()->ensure(up);
1401 pushLeft(nextAtom());
1405 plainInsert(MathAtom(new MathScriptInset(up)));
1406 prevAtom()->asScriptInset()->ensure(up);
1407 pushRight(prevAtom());
1417 bool MathCursor::inMathMode() const
1419 return !par()->asBoxInset();
1423 bool MathCursor::interpret(char c)
1425 //lyxerr << "interpret 2: '" << c << "'\n";
1427 if (inMacroArgMode()) {
1431 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1432 if (p && 1 <= n && n <= p->numargs())
1433 insert(MathAtom(new MathMacroArgument(c - '0')));
1435 insert(MathAtom(new MathSpecialCharInset('#')));
1436 interpret(c); // try again
1442 if (inMacroMode()) {
1443 string name = macroName();
1444 //lyxerr << "interpret name: '" << name << "'\n";
1446 if (name.empty() && c == '\\') {
1448 interpret("\\backslash");
1453 inMacroMode()->name() += c;
1457 // handle 'special char' macros
1462 interpret("\\backslash");
1464 interpret(string("\\") + c);
1468 // leave macro mode and try again
1474 // leave autocorrect mode if necessary
1475 if (autocorrect_ && c == ' ') {
1476 autocorrect_ = false;
1480 // just clear selection on pressing the space par
1481 if (selection_ && c == ' ') {
1488 if (!inMathMode()) {
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() == ' ')
1499 lyxerr << "starting with macro\n";
1500 insert(MathAtom(new MathUnknownInset("\\", false)));
1505 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1506 prevAtom()->asSpaceInset()->incSpace();
1511 // if are at the very end, leave the formula
1512 return pos() != size();
1516 insert(c); // LM_TC_TEX;
1521 if (c == '{' || c == '}', c)) {
1522 insert(c); // LM_TC_TEX;
1528 niceInsert(MathAtom(new MathBraceInset));
1536 if (c == '$' || c == '%') {
1537 insert(MathAtom(new MathSpecialCharInset(c)));
1542 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1543 insert(c, LM_TC_VAR);
1547 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1548 insert(c, LM_TC_VAR);
1549 lastcode_ = LM_TC_VAR;
1554 insert(c, LM_TC_TEX);
1555 //bv->owner()->message(_("TeX mode"));
1560 // try auto-correction
1561 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1564 // no special circumstances, so insert the character without any fuss
1566 autocorrect_ = true;
1572 MathCursorPos MathCursor::normalAnchor() const
1574 if (Anchor_.size() < Cursor_.size()) {
1576 lyxerr << "unusual Anchor size\n";
1579 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1580 // use Anchor on the same level as Cursor
1581 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1582 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1583 // anchor is behind cursor -> move anchor behind the inset
1590 void MathCursor::stripFromLastEqualSign()
1592 // find position of last '=' in the array
1593 MathArray & ar = cursor().cell();
1594 MathArray::const_iterator et = ar.end();
1595 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1596 if ((*it)->getChar() == '=')
1599 // delete everything behind this position
1600 ar.erase(et - ar.begin(), ar.size());
1605 void MathCursor::setSelection(cursor_type const & where, size_type n)
1614 void MathCursor::insetToggle()
1617 nextAtom()->lock(!nextAtom()->lock());
1621 string MathCursor::info() const
1624 os << "Math editor mode ";
1625 for (int i = 0, n = Cursor_.size(); i < n; ++i) {
1626 Cursor_[i].par_->infoize(os);
1630 // prevAtom()->infoize(os);
1632 return os.str().c_str(); // .c_str() needed for lyxstring