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 front)
189 : formula_(formula), autocorrect_(false), selection_(false)
191 front ? 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
274 if (t->asScriptInset())
278 // we can't move into anything new during selection
279 if (Cursor_.size() == Anchor_.size())
281 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
288 bool MathCursor::posLeft()
297 bool MathCursor::posRight()
306 bool MathCursor::left(bool sel)
309 autocorrect_ = false;
310 targetx_ = -1; // "no target"
317 if (hasPrevAtom() && openable(prevAtom(), sel)) {
318 pushRight(prevAtom());
322 return posLeft() || idxLeft() || popLeft() || selection_;
326 bool MathCursor::right(bool sel)
329 autocorrect_ = false;
330 targetx_ = -1; // "no target"
337 if (hasNextAtom() && openable(nextAtom(), sel)) {
338 pushLeft(nextAtom());
342 return posRight() || idxRight() || popRight() || selection_;
346 void MathCursor::first()
349 push(formula_->par());
350 par()->idxFirst(idx(), pos());
354 void MathCursor::last()
357 push(formula_->par());
358 par()->idxLast(idx(), pos());
362 bool positionable(MathCursor::cursor_type const & cursor,
363 MathCursor::cursor_type const & anchor)
365 // avoid deeper nested insets when selecting
366 if (cursor.size() > anchor.size())
369 // anchor might be deeper, should have same path then
370 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
371 if (cursor[i].par_ != anchor[i].par_)
374 // position should be ok.
379 void MathCursor::setPos(int x, int y)
382 bool res = bruteFind(x, y,
383 formula()->xlow(), formula()->xhigh(),
384 formula()->ylow(), formula()->yhigh());
386 // this ccan happen on creation of "math-display"
395 void MathCursor::home(bool sel)
398 autocorrect_ = false;
401 if (!par()->idxHome(idx(), pos()))
407 void MathCursor::end(bool sel)
410 autocorrect_ = false;
413 if (!par()->idxEnd(idx(), pos()))
419 void MathCursor::plainErase()
421 array().erase(pos());
425 void MathCursor::markInsert()
427 //lyxerr << "inserting mark\n";
428 array().insert(pos(), MathAtom(new MathCharInset(0)));
432 void MathCursor::markErase()
434 //lyxerr << "deleting mark\n";
435 array().erase(pos());
439 void MathCursor::plainInsert(MathAtom const & t)
441 array().insert(pos(), t);
446 void MathCursor::insert(char c)
448 //lyxerr << "inserting '" << c << "'\n";
450 plainInsert(MathAtom(new MathCharInset(c)));
454 void MathCursor::insert(MathAtom const & t)
469 void MathCursor::niceInsert(MathAtom const & t)
472 insert(t); // inserting invalidates the pointer!
473 MathAtom & p = prevAtom();
474 // enter the new inset and move the contents of the selection if possible
477 par()->idxLast(idx(), pos());
483 void MathCursor::insert(MathArray const & ar)
489 array().insert(pos(), ar);
494 void MathCursor::paste(MathArray const & ar)
498 array().insert(pos(), ar);
503 void MathCursor::backspace()
505 autocorrect_ = false;
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()
531 autocorrect_ = false;
540 // delete empty cells if possible
542 if (par()->idxDelete(idx()))
545 // old behaviour when in last position of cell
546 if (pos() == size()) {
547 par()->idxGlue(idx());
551 MathScriptInset * p = nextAtom()->asScriptInset();
553 p->removeScript(p->hasUp());
554 // Don't delete if there is anything left
555 if (p->hasUp() || p->hasDown())
563 void MathCursor::delLine()
565 autocorrect_ = false;
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_;
596 autocorrect_ = false;
601 bool MathCursor::down(bool sel)
606 cursor_type save = Cursor_;
610 autocorrect_ = false;
615 bool MathCursor::toggleLimits()
619 MathScriptInset * t = nextAtom()->asScriptInset();
622 int old = t->limits();
623 t->limits(old < 0 ? 1 : -1);
624 return old != t->limits();
628 void MathCursor::macroModeClose()
630 MathUnknownInset * p = inMacroMode();
634 string s = p->name();
636 array().erase(pos());
642 string MathCursor::macroName() const
644 return inMacroMode() ? inMacroMode()->name() : "";
648 void MathCursor::selCopy()
652 theSelection.grab(*this);
658 void MathCursor::selCut()
662 theSelection.grab(*this);
663 theSelection.erase(*this);
666 theSelection.clear();
671 void MathCursor::selDel()
675 theSelection.erase(*this);
683 void MathCursor::selPaste()
687 theSelection.paste(*this);
688 //theSelection.grab(*this);
693 void MathCursor::selHandle(bool sel)
695 if (sel == selection_)
697 //theSelection.clear();
703 void MathCursor::selStart()
706 //theSelection.clear();
713 void MathCursor::selClear()
721 void MathCursor::selClearOrDel()
723 if (lyxrc.auto_region_delete)
730 void MathCursor::selGet(MathArray & ar)
736 theSelection.grab(*this);
737 ar = theSelection.glue();
742 void MathCursor::drawSelection(MathPainterInfo & pain) const
749 getSelection(i1, i2);
751 if (i1.idx_ == i2.idx_) {
752 MathXArray & c = i1.xcell();
753 int x1 = c.xo() + c.pos2x(i1.pos_);
754 int y1 = c.yo() - c.ascent();
755 int x2 = c.xo() + c.pos2x(i2.pos_);
756 int y2 = c.yo() + c.descent();
757 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
759 vector<MathInset::idx_type> indices
760 = i1.par_->idxBetween(i1.idx_, i2.idx_);
761 for (unsigned i = 0; i < indices.size(); ++i) {
762 MathXArray & c = i1.xcell(indices[i]);
764 int y1 = c.yo() - c.ascent();
765 int x2 = c.xo() + c.width();
766 int y2 = c.yo() + c.descent();
767 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
772 // draw anchor if different from selection boundary
773 MathCursorPos anc = Anchor_.back();
774 if (anc != i1 && anc != i2) {
775 MathXArray & c = anc.xcell();
776 int x = c.xo() + c.pos2x(anc.pos_);
777 int y1 = c.yo() - c.ascent();
778 int y2 = c.yo() + c.descent();
779 pain.line(x, y1, x, y2, LColor::math);
785 void MathCursor::handleDelim(string const & l, string const & r)
787 handleNest(new MathDelimInset(l, r));
791 void MathCursor::handleNest(MathInset * p)
795 p->cell(0) = theSelection.glue();
797 insert(MathAtom(p)); // this invalidates p!
798 pushRight(prevAtom());
802 void MathCursor::getPos(int & x, int & y)
805 #warning This should probably take cellXOffset and cellYOffset into account
807 x = xarray().xo() + xarray().pos2x(pos());
808 // move cursor visually into empty cells ("blue rectangles");
815 MathInset * MathCursor::par() const
817 return cursor().par_;
821 InsetFormulaBase * MathCursor::formula()
827 MathCursor::idx_type MathCursor::idx() const
829 return cursor().idx_;
833 MathCursor::idx_type & MathCursor::idx()
835 return cursor().idx_;
839 MathCursor::pos_type MathCursor::pos() const
841 return cursor().pos_;
845 MathCursor::pos_type & MathCursor::pos()
847 return cursor().pos_;
851 MathUnknownInset * MathCursor::inMacroMode() const
855 MathUnknownInset * p = prevAtom()->asUnknownInset();
856 return (p && !p->final()) ? p : 0;
860 bool MathCursor::inMacroArgMode() const
862 return pos() > 0 && prevAtom()->getChar() == '#';
866 bool MathCursor::selection() const
872 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
874 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
875 MathGridInset * p = Cursor_[i].par_->asGridInset();
877 idx = Cursor_[i].idx_;
879 lyxerr << "found grid and idx: " << idx << "\n";
886 void MathCursor::popToEnclosingGrid()
888 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
893 void MathCursor::pullArg(bool goright)
896 MathArray a = array();
898 MathScriptInset const * p = par()->asScriptInset();
900 // special handling for scripts
901 const bool up = p->hasUp();
903 MathScriptInset * q = nextAtom()->asScriptInset();
907 array().insert(pos(), a);
913 array().insert(pos(), a);
917 formula()->mutateToText();
922 void MathCursor::touch()
924 cursor_type::const_iterator it = Cursor_.begin();
925 cursor_type::const_iterator et = Cursor_.end();
926 for ( ; it != et; ++it)
931 void MathCursor::normalize()
936 MathIterator it = ibegin(formula()->par().nucleus());
937 MathIterator et = iend(formula()->par().nucleus());
938 for (; it != et; ++it)
939 if (it.par()->asBoxInset())
940 it.par()->asBoxInset()->rebreak();
944 if (idx() >= par()->nargs()) {
945 lyxerr << "this should not really happen - 1: "
946 << idx() << " " << par()->nargs() << "\n";
949 idx() = min(idx(), par()->nargs() - 1);
951 if (pos() > size()) {
952 lyxerr << "this should not really happen - 2: "
953 << pos() << " " << size() << " in idx: " << idx()
955 WriteStream wi(lyxerr, false, true);
960 pos() = min(pos(), size());
962 // remove empty scripts if possible
964 for (pos_type i = 0; i < size(); ++i) {
965 MathScriptInset * p = array().at(i)->asScriptInset();
967 p->removeEmptyScripts();
974 // fix again position
975 pos() = min(pos(), size());
979 MathCursor::size_type MathCursor::size() const
981 return array().size();
985 MathCursor::col_type MathCursor::hullCol() const
987 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
991 MathCursor::row_type MathCursor::hullRow() const
993 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
997 bool MathCursor::hasPrevAtom() const
1003 bool MathCursor::hasNextAtom() const
1005 return pos() < size();
1009 MathAtom const & MathCursor::prevAtom() const
1011 lyx::Assert(pos() > 0);
1012 return array().at(pos() - 1);
1016 MathAtom & MathCursor::prevAtom()
1018 lyx::Assert(pos() > 0);
1019 return array().at(pos() - 1);
1023 MathAtom const & MathCursor::nextAtom() const
1025 lyx::Assert(pos() < size());
1026 return array().at(pos());
1030 MathAtom & MathCursor::nextAtom()
1032 lyx::Assert(pos() < size());
1033 return array().at(pos());
1037 MathArray & MathCursor::array() const
1039 static MathArray dummy;
1041 if (idx() >= par()->nargs()) {
1042 lyxerr << "############ idx_ " << idx() << " not valid\n";
1046 if (Cursor_.size() == 0) {
1047 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1051 return cursor().cell();
1055 MathXArray & MathCursor::xarray() const
1057 static MathXArray dummy;
1059 if (Cursor_.size() == 0) {
1060 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1064 return cursor().xcell();
1068 void MathCursor::idxNext()
1070 par()->idxNext(idx(), pos());
1074 void MathCursor::idxPrev()
1076 par()->idxPrev(idx(), pos());
1080 void MathCursor::splitCell()
1082 if (idx() + 1 == par()->nargs())
1084 MathArray ar = array();
1086 array().erase(pos(), size());
1089 array().insert(0, ar);
1093 void MathCursor::breakLine()
1095 // leave inner cells
1099 MathHullInset * p = formula()->par()->asHullInset();
1103 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1104 p->mutate(LM_OT_EQNARRAY);
1108 p->addRow(hullRow());
1111 const row_type r = hullRow();
1112 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1113 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1117 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1122 //void MathCursor::readLine(MathArray & ar) const
1124 // idx_type base = row() * par()->ncols();
1125 // for (idx_type off = 0; off < par()->ncols(); ++off)
1126 // ar.push_back(par()->cell(base + off));
1130 char MathCursor::valign() const
1133 MathGridInset * p = enclosingGrid(idx);
1134 return p ? p->valign() : '\0';
1138 char MathCursor::halign() const
1141 MathGridInset * p = enclosingGrid(idx);
1142 return p ? p->halign(idx % p->ncols()) : '\0';
1146 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1148 MathCursorPos anc = normalAnchor();
1149 if (anc < cursor()) {
1159 MathCursorPos & MathCursor::cursor()
1161 lyx::Assert(Cursor_.size());
1162 return Cursor_.back();
1166 MathCursorPos const & MathCursor::cursor() const
1168 lyx::Assert(Cursor_.size());
1169 return Cursor_.back();
1173 bool MathCursor::goUpDown(bool up)
1175 // Be warned: The 'logic' implemented in this function is highly fragile.
1176 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1177 // So fiddle around with it only if you know what you are doing!
1178 int xlow, xhigh, ylow, yhigh;
1183 // check if we had something else in mind, if not, this is the future goal
1189 // try neigbouring script insets
1191 if (hasPrevAtom()) {
1192 MathScriptInset * p = prevAtom()->asScriptInset();
1193 if (p && p->has(up)) {
1196 idx() = up; // the superscript has index 1
1198 ///lyxerr << "updown: handled by scriptinset to the left\n";
1204 if (hasNextAtom()) {
1205 MathScriptInset * p = nextAtom()->asScriptInset();
1206 if (p && p->has(up)) {
1210 ///lyxerr << "updown: handled by scriptinset to the right\n";
1216 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1221 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1222 // lyxerr << "updown: handled by brute find in the same cell\n";
1226 // try to find an inset that knows better then we
1228 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1230 if (par()->idxUpDown(idx(), up)) {
1231 // we found a cell that thinks it has something "below" us.
1232 ///lyxerr << "updown: found inset that handles UpDown\n";
1233 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1234 // project (xo,yo) onto proper box
1235 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1236 /// << "\n xlow: " << xlow << " ylow: " << ylow
1237 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1238 xo = min(max(xo, xlow), xhigh);
1239 yo = min(max(yo, ylow), yhigh);
1240 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1241 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1242 ///lyxerr << "updown: handled by final brute find\n";
1248 // no such inset found, just take something "above"
1249 ///lyxerr << "updown: handled by strange case\n";
1254 up ? formula()->ylow() : yo + 4,
1255 up ? yo - 4 : formula()->yhigh()
1259 // any improvement so far?
1262 if (up ? ynew < yo : ynew > yo)
1268 bool MathCursor::bruteFind
1269 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1271 cursor_type best_cursor;
1272 double best_dist = 1e10;
1274 MathIterator it = ibegin(formula()->par().nucleus());
1275 MathIterator et = iend(formula()->par().nucleus());
1277 // avoid invalid nesting when selecting
1278 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1279 MathCursorPos const & top = it.position();
1280 int xo = top.xpos();
1281 int yo = top.ypos();
1282 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1283 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1284 // '<=' in order to take the last possible position
1285 // this is important for clicking behind \sum in e.g. '\sum_i a'
1286 if (d <= best_dist) {
1288 best_cursor = it.cursor();
1298 if (best_dist < 1e10)
1299 Cursor_ = best_cursor;
1300 return best_dist < 1e10;
1304 bool MathCursor::idxLeft()
1306 return par()->idxLeft(idx(), pos());
1310 bool MathCursor::idxRight()
1312 return par()->idxRight(idx(), pos());
1316 bool MathCursor::interpret(string const & s)
1318 //lyxerr << "interpret 1: '" << s << "'\n";
1322 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1323 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1324 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1326 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1328 istringstream is(s.substr(5).c_str());
1331 niceInsert(MathAtom(new MathCasesInset(n)));
1335 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1340 istringstream is(s.substr(6).c_str());
1341 is >> m >> n >> v_align >> h_align;
1345 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1349 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1351 istringstream is(s.substr(7).c_str());
1354 mathed_parse_cell(rep.from, from);
1355 mathed_parse_cell(rep.to, to);
1356 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1357 par()->replace(rep);
1361 string name = s.substr(1);
1363 if (name == "over" || name == "choose" || name == "atop") {
1364 MathArray ar = array();
1365 MathAtom t(createMathInset(name));
1366 t->asNestInset()->cell(0).swap(array());
1374 // prevent entering of recursive macros
1375 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1376 && formula()->getInsetName() == name)
1378 lyxerr << "can't enter recursive macro\n";
1382 niceInsert(createMathInset(name));
1387 bool MathCursor::script(bool up)
1389 // Hack to get \\^ and \\_ working
1390 if (inMacroMode() && macroName() == "\\") {
1392 interpret("\\mathcircumflex");
1400 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1401 prevAtom()->asScriptInset()->ensure(up);
1402 pushRight(prevAtom());
1405 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1406 nextAtom()->asScriptInset()->ensure(up);
1407 pushLeft(nextAtom());
1411 plainInsert(MathAtom(new MathScriptInset(up)));
1412 prevAtom()->asScriptInset()->ensure(up);
1413 pushRight(prevAtom());
1423 bool MathCursor::inMathMode() const
1425 return !par()->asBoxInset();
1429 bool MathCursor::interpret(char c)
1431 //lyxerr << "interpret 2: '" << c << "'\n";
1432 targetx_ = -1; // "no target"
1433 if (inMacroArgMode()) {
1437 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1438 if (p && 1 <= n && n <= p->numargs())
1439 insert(MathAtom(new MathMacroArgument(c - '0')));
1441 insert(MathAtom(new MathSpecialCharInset('#')));
1442 interpret(c); // try again
1448 if (inMacroMode()) {
1449 string name = macroName();
1450 //lyxerr << "interpret name: '" << name << "'\n";
1452 if (name.empty() && c == '\\') {
1454 interpret("\\backslash");
1459 inMacroMode()->name() += c;
1463 // handle 'special char' macros
1468 interpret("\\backslash");
1470 interpret(string("\\") + c);
1474 // leave macro mode and try again
1480 // leave autocorrect mode if necessary
1481 if (autocorrect_ && c == ' ') {
1482 autocorrect_ = false;
1486 // just clear selection on pressing the space par
1487 if (selection_ && c == ' ') {
1494 if (!inMathMode()) {
1495 // suppress direct insertion of two spaces in a row
1496 // the still allows typing '<space>a<space>' and deleting the 'a', but
1497 // it is better than nothing...
1498 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1505 lyxerr << "starting with macro\n";
1506 insert(MathAtom(new MathUnknownInset("\\", false)));
1511 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1512 prevAtom()->asSpaceInset()->incSpace();
1517 // if are at the very end, leave the formula
1518 return pos() != size();
1522 insert(c); // LM_TC_TEX;
1527 if (c == '{' || c == '}', c)) {
1528 insert(c); // LM_TC_TEX;
1534 niceInsert(MathAtom(new MathBraceInset));
1542 if (c == '$' || c == '%') {
1543 insert(MathAtom(new MathSpecialCharInset(c)));
1548 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1549 insert(c, LM_TC_VAR);
1553 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1554 insert(c, LM_TC_VAR);
1555 lastcode_ = LM_TC_VAR;
1560 insert(c, LM_TC_TEX);
1561 //bv->owner()->message(_("TeX mode"));
1566 // try auto-correction
1567 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1570 // no special circumstances, so insert the character without any fuss
1572 autocorrect_ = true;
1578 MathCursorPos MathCursor::normalAnchor() const
1580 if (Anchor_.size() < Cursor_.size()) {
1582 lyxerr << "unusual Anchor size\n";
1585 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1586 // use Anchor on the same level as Cursor
1587 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1588 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1589 // anchor is behind cursor -> move anchor behind the inset
1596 void MathCursor::stripFromLastEqualSign()
1598 // find position of last '=' in the array
1599 MathArray & ar = cursor().cell();
1600 MathArray::const_iterator et = ar.end();
1601 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1602 if ((*it)->getChar() == '=')
1605 // delete everything behind this position
1606 ar.erase(et - ar.begin(), ar.size());
1611 void MathCursor::setSelection(cursor_type const & where, size_type n)
1620 void MathCursor::insetToggle()
1623 nextAtom()->lock(!nextAtom()->lock());
1627 string MathCursor::info() const
1630 os << "Math editor mode ";
1631 for (int i = 0, n = Cursor_.size(); i < n; ++i) {
1632 Cursor_[i].par_->infoize(os);
1636 // prevAtom()->infoize(os);
1638 return os.str().c_str(); // .c_str() needed for lyxstring