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;
313 if (hasPrevAtom() && openable(prevAtom(), sel)) {
314 pushRight(prevAtom());
318 return posLeft() || idxLeft() || popLeft() || selection_;
322 bool MathCursor::right(bool sel)
325 autocorrect_ = false;
332 if (hasNextAtom() && openable(nextAtom(), sel)) {
333 pushLeft(nextAtom());
337 return posRight() || idxRight() || popRight() || selection_;
341 void MathCursor::first()
344 pushLeft(formula_->par());
348 void MathCursor::last()
355 bool positionable(MathCursor::cursor_type const & cursor,
356 MathCursor::cursor_type const & anchor)
358 // avoid deeper nested insets when selecting
359 if (cursor.size() > anchor.size())
362 // anchor might be deeper, should have same path then
363 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
364 if (cursor[i].par_ != anchor[i].par_)
367 // position should be ok.
372 void MathCursor::setPos(int x, int y)
375 bool res = bruteFind(x, y,
376 formula()->xlow(), formula()->xhigh(),
377 formula()->ylow(), formula()->yhigh());
379 // this ccan happen on creation of "math-display"
388 void MathCursor::home(bool sel)
391 autocorrect_ = false;
394 if (!par()->idxHome(idx(), pos()))
400 void MathCursor::end(bool sel)
403 autocorrect_ = false;
406 if (!par()->idxEnd(idx(), pos()))
412 void MathCursor::plainErase()
414 array().erase(pos());
418 void MathCursor::markInsert()
420 //lyxerr << "inserting mark\n";
421 array().insert(pos(), MathAtom(new MathCharInset(0)));
425 void MathCursor::markErase()
427 //lyxerr << "deleting mark\n";
428 array().erase(pos());
432 void MathCursor::plainInsert(MathAtom const & t)
434 array().insert(pos(), t);
439 void MathCursor::insert(char c)
441 //lyxerr << "inserting '" << c << "'\n";
443 plainInsert(MathAtom(new MathCharInset(c)));
447 void MathCursor::insert(MathAtom const & t)
462 void MathCursor::niceInsert(MathAtom const & t)
465 insert(t); // inserting invalidates the pointer!
466 MathAtom const & p = prevAtom();
469 right(); // do not push for e.g. MathSymbolInset
475 void MathCursor::insert(MathArray const & ar)
481 array().insert(pos(), ar);
486 void MathCursor::paste(MathArray const & ar)
490 array().insert(pos(), ar);
495 void MathCursor::backspace()
497 autocorrect_ = false;
508 MathScriptInset * p = prevAtom()->asScriptInset();
510 p->removeScript(p->hasUp());
511 // Don't delete if there is anything left
512 if (p->hasUp() || p->hasDown())
521 void MathCursor::erase()
523 autocorrect_ = false;
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()
557 autocorrect_ = false;
565 if (par()->nrows() > 1) {
566 // grid are the only things with more than one row...
567 lyx::Assert(par()->asGridInset());
568 par()->asGridInset()->delRow(hullRow());
571 if (idx() >= par()->nargs())
572 idx() = par()->nargs() - 1;
579 bool MathCursor::up(bool sel)
584 cursor_type save = Cursor_;
588 autocorrect_ = false;
593 bool MathCursor::down(bool sel)
598 cursor_type save = Cursor_;
602 autocorrect_ = false;
607 bool MathCursor::toggleLimits()
611 MathScriptInset * t = nextAtom()->asScriptInset();
614 int old = t->limits();
615 t->limits(old < 0 ? 1 : -1);
616 return old != t->limits();
620 void MathCursor::macroModeClose()
622 MathUnknownInset * p = inMacroMode();
626 string s = p->name();
628 array().erase(pos());
634 string MathCursor::macroName() const
636 return inMacroMode() ? inMacroMode()->name() : "";
640 void MathCursor::selCopy()
644 theSelection.grab(*this);
650 void MathCursor::selCut()
654 theSelection.grab(*this);
655 theSelection.erase(*this);
658 theSelection.clear();
663 void MathCursor::selDel()
667 theSelection.erase(*this);
675 void MathCursor::selPaste()
679 theSelection.paste(*this);
680 //theSelection.grab(*this);
685 void MathCursor::selHandle(bool sel)
687 if (sel == selection_)
689 //theSelection.clear();
695 void MathCursor::selStart()
698 //theSelection.clear();
705 void MathCursor::selClear()
713 void MathCursor::selClearOrDel()
715 if (lyxrc.auto_region_delete)
722 void MathCursor::selGet(MathArray & ar)
728 theSelection.grab(*this);
729 ar = theSelection.glue();
734 void MathCursor::drawSelection(MathPainterInfo & pain) const
741 getSelection(i1, i2);
743 if (i1.idx_ == i2.idx_) {
744 MathXArray & c = i1.xcell();
745 int x1 = c.xo() + c.pos2x(i1.pos_);
746 int y1 = c.yo() - c.ascent();
747 int x2 = c.xo() + c.pos2x(i2.pos_);
748 int y2 = c.yo() + c.descent();
749 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
751 vector<MathInset::idx_type> indices
752 = i1.par_->idxBetween(i1.idx_, i2.idx_);
753 for (unsigned i = 0; i < indices.size(); ++i) {
754 MathXArray & c = i1.xcell(indices[i]);
756 int y1 = c.yo() - c.ascent();
757 int x2 = c.xo() + c.width();
758 int y2 = c.yo() + c.descent();
759 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
764 // draw anchor if different from selection boundary
765 MathCursorPos anc = Anchor_.back();
766 if (anc != i1 && anc != i2) {
767 MathXArray & c = anc.xcell();
768 int x = c.xo() + c.pos2x(anc.pos_);
769 int y1 = c.yo() - c.ascent();
770 int y2 = c.yo() + c.descent();
771 pain.line(x, y1, x, y2, LColor::math);
777 void MathCursor::handleDelim(string const & l, string const & r)
779 handleNest(new MathDelimInset(l, r));
783 void MathCursor::handleNest(MathInset * p)
787 p->cell(0) = theSelection.glue();
789 insert(MathAtom(p)); // this invalidates p!
790 pushRight(prevAtom());
794 void MathCursor::getPos(int & x, int & y)
797 #warning This should probably take cellXOffset and cellYOffset into account
799 x = xarray().xo() + xarray().pos2x(pos());
800 // move cursor visually into empty cells ("blue rectangles");
807 MathInset * MathCursor::par() const
809 return cursor().par_;
813 InsetFormulaBase * MathCursor::formula()
819 MathCursor::idx_type MathCursor::idx() const
821 return cursor().idx_;
825 MathCursor::idx_type & MathCursor::idx()
827 return cursor().idx_;
831 MathCursor::pos_type MathCursor::pos() const
833 return cursor().pos_;
837 MathCursor::pos_type & MathCursor::pos()
839 return cursor().pos_;
843 MathUnknownInset * MathCursor::inMacroMode() const
847 MathUnknownInset * p = prevAtom()->asUnknownInset();
848 return (p && !p->final()) ? p : 0;
852 bool MathCursor::inMacroArgMode() const
854 return pos() > 0 && prevAtom()->getChar() == '#';
858 bool MathCursor::selection() const
864 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
866 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
867 MathGridInset * p = Cursor_[i].par_->asGridInset();
869 idx = Cursor_[i].idx_;
871 lyxerr << "found grid and idx: " << idx << "\n";
878 void MathCursor::popToEnclosingGrid()
880 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
885 void MathCursor::pullArg(bool goright)
888 MathArray a = array();
890 MathScriptInset const * p = par()->asScriptInset();
892 // special handling for scripts
893 const bool up = p->hasUp();
895 MathScriptInset * q = nextAtom()->asScriptInset();
899 array().insert(pos(), a);
905 array().insert(pos(), a);
909 formula()->mutateToText();
914 void MathCursor::touch()
916 cursor_type::const_iterator it = Cursor_.begin();
917 cursor_type::const_iterator et = Cursor_.end();
918 for ( ; it != et; ++it)
923 void MathCursor::normalize()
928 MathIterator it = ibegin(formula()->par().nucleus());
929 MathIterator et = iend(formula()->par().nucleus());
930 for (; it != et; ++it)
931 if (it.par()->asBoxInset())
932 it.par()->asBoxInset()->rebreak();
936 if (idx() >= par()->nargs()) {
937 lyxerr << "this should not really happen - 1: "
938 << idx() << " " << par()->nargs() << "\n";
941 idx() = min(idx(), par()->nargs() - 1);
943 if (pos() > size()) {
944 lyxerr << "this should not really happen - 2: "
945 << pos() << " " << size() << " in idx: " << idx()
947 WriteStream wi(lyxerr, false, true);
952 pos() = min(pos(), size());
954 // remove empty scripts if possible
956 for (pos_type i = 0; i < size(); ++i) {
957 MathScriptInset * p = array().at(i)->asScriptInset();
959 p->removeEmptyScripts();
966 // fix again position
967 pos() = min(pos(), size());
971 MathCursor::size_type MathCursor::size() const
973 return array().size();
977 MathCursor::col_type MathCursor::hullCol() const
979 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
983 MathCursor::row_type MathCursor::hullRow() const
985 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
989 bool MathCursor::hasPrevAtom() const
995 bool MathCursor::hasNextAtom() const
997 return pos() < size();
1001 MathAtom const & MathCursor::prevAtom() const
1003 lyx::Assert(pos() > 0);
1004 return array().at(pos() - 1);
1008 MathAtom & MathCursor::prevAtom()
1010 lyx::Assert(pos() > 0);
1011 return array().at(pos() - 1);
1015 MathAtom const & MathCursor::nextAtom() const
1017 lyx::Assert(pos() < size());
1018 return array().at(pos());
1022 MathAtom & MathCursor::nextAtom()
1024 lyx::Assert(pos() < size());
1025 return array().at(pos());
1029 MathArray & MathCursor::array() const
1031 static MathArray dummy;
1033 if (idx() >= par()->nargs()) {
1034 lyxerr << "############ idx_ " << idx() << " not valid\n";
1038 if (Cursor_.size() == 0) {
1039 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1043 return cursor().cell();
1047 MathXArray & MathCursor::xarray() const
1049 static MathXArray dummy;
1051 if (Cursor_.size() == 0) {
1052 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1056 return cursor().xcell();
1060 void MathCursor::idxNext()
1062 par()->idxNext(idx(), pos());
1066 void MathCursor::idxPrev()
1068 par()->idxPrev(idx(), pos());
1072 void MathCursor::splitCell()
1074 if (idx() + 1 == par()->nargs())
1076 MathArray ar = array();
1078 array().erase(pos(), size());
1081 array().insert(0, ar);
1085 void MathCursor::breakLine()
1087 // leave inner cells
1091 MathHullInset * p = formula()->par()->asHullInset();
1095 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1096 p->mutate(LM_OT_EQNARRAY);
1100 p->addRow(hullRow());
1103 const row_type r = hullRow();
1104 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1105 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1109 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1114 //void MathCursor::readLine(MathArray & ar) const
1116 // idx_type base = row() * par()->ncols();
1117 // for (idx_type off = 0; off < par()->ncols(); ++off)
1118 // ar.push_back(par()->cell(base + off));
1122 char MathCursor::valign() const
1125 MathGridInset * p = enclosingGrid(idx);
1126 return p ? p->valign() : '\0';
1130 char MathCursor::halign() const
1133 MathGridInset * p = enclosingGrid(idx);
1134 return p ? p->halign(idx % p->ncols()) : '\0';
1138 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1140 MathCursorPos anc = normalAnchor();
1141 if (anc < cursor()) {
1151 MathCursorPos & MathCursor::cursor()
1153 lyx::Assert(Cursor_.size());
1154 return Cursor_.back();
1158 MathCursorPos const & MathCursor::cursor() const
1160 lyx::Assert(Cursor_.size());
1161 return Cursor_.back();
1165 bool MathCursor::goUpDown(bool up)
1167 // Be warned: The 'logic' implemented in this function is highly fragile.
1168 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1169 // So fiddle around with it only if you know what you are doing!
1170 int xlow, xhigh, ylow, yhigh;
1175 // try neigbouring script insets
1177 if (hasPrevAtom()) {
1178 MathScriptInset * p = prevAtom()->asScriptInset();
1179 if (p && p->has(up)) {
1182 idx() = up; // the superscript has index 1
1184 ///lyxerr << "updown: handled by scriptinset to the left\n";
1190 if (hasNextAtom()) {
1191 MathScriptInset * p = nextAtom()->asScriptInset();
1192 if (p && p->has(up)) {
1196 ///lyxerr << "updown: handled by scriptinset to the right\n";
1202 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1207 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1208 // lyxerr << "updown: handled by brute find in the same cell\n";
1212 // try to find an inset that knows better then we
1214 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1216 if (par()->idxUpDown(idx(), up)) {
1217 // we found a cell that thinks it has something "below" us.
1218 ///lyxerr << "updown: found inset that handles UpDown\n";
1219 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1220 // project (xo,yo) onto proper box
1221 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1222 /// << "\n xlow: " << xlow << " ylow: " << ylow
1223 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1224 xo = min(max(xo, xlow), xhigh);
1225 yo = min(max(yo, ylow), yhigh);
1226 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1227 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1228 ///lyxerr << "updown: handled by final brute find\n";
1234 // no such inset found, just take something "above"
1235 ///lyxerr << "updown: handled by strange case\n";
1240 up ? formula()->ylow() : yo + 4,
1241 up ? yo - 4 : formula()->yhigh()
1245 // any improvement so far?
1248 if (up ? ynew < yo : ynew > yo)
1254 bool MathCursor::bruteFind
1255 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1257 cursor_type best_cursor;
1258 double best_dist = 1e10;
1260 MathIterator it = ibegin(formula()->par().nucleus());
1261 MathIterator et = iend(formula()->par().nucleus());
1263 // avoid invalid nesting when selecting
1264 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1265 MathCursorPos const & top = it.position();
1266 int xo = top.xpos();
1267 int yo = top.ypos();
1268 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1269 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1270 // '<=' in order to take the last possible position
1271 // this is important for clicking behind \sum in e.g. '\sum_i a'
1272 if (d <= best_dist) {
1274 best_cursor = it.cursor();
1284 if (best_dist < 1e10)
1285 Cursor_ = best_cursor;
1286 return best_dist < 1e10;
1290 bool MathCursor::idxLeft()
1292 return par()->idxLeft(idx(), pos());
1296 bool MathCursor::idxRight()
1298 return par()->idxRight(idx(), pos());
1302 bool MathCursor::interpret(string const & s)
1304 //lyxerr << "interpret 1: '" << s << "'\n";
1308 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1309 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1310 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1312 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1314 istringstream is(s.substr(5).c_str());
1317 niceInsert(MathAtom(new MathCasesInset(n)));
1321 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1326 istringstream is(s.substr(6).c_str());
1327 is >> m >> n >> v_align >> h_align;
1331 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1335 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1337 istringstream is(s.substr(7).c_str());
1340 mathed_parse_cell(rep.from, from);
1341 mathed_parse_cell(rep.to, to);
1342 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1343 par()->replace(rep);
1347 string name = s.substr(1);
1349 if (name == "over" || name == "choose" || name == "atop") {
1350 MathArray ar = array();
1351 MathAtom t(createMathInset(name));
1352 t->asNestInset()->cell(0).swap(array());
1360 // prevent entering of recursive macros
1361 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1362 && formula()->getInsetName() == name)
1364 lyxerr << "can't enter recursive macro\n";
1368 niceInsert(createMathInset(name));
1373 bool MathCursor::script(bool up)
1375 // Hack to get \\^ and \\_ working
1376 if (inMacroMode() && macroName() == "\\") {
1378 interpret("\\mathcircumflex");
1386 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1387 prevAtom()->asScriptInset()->ensure(up);
1388 pushRight(prevAtom());
1391 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1392 nextAtom()->asScriptInset()->ensure(up);
1393 pushLeft(nextAtom());
1397 plainInsert(MathAtom(new MathScriptInset(up)));
1398 prevAtom()->asScriptInset()->ensure(up);
1399 pushRight(prevAtom());
1409 bool MathCursor::interpret(char c)
1411 //lyxerr << "interpret 2: '" << c << "'\n";
1412 if (inMacroArgMode()) {
1416 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1417 if (p && 1 <= n && n <= p->numargs())
1418 insert(MathAtom(new MathMacroArgument(c - '0')));
1420 insert(MathAtom(new MathSpecialCharInset('#')));
1421 interpret(c); // try again
1427 if (inMacroMode()) {
1428 string name = macroName();
1429 //lyxerr << "interpret name: '" << name << "'\n";
1431 if (name.empty() && c == '\\') {
1433 interpret("\\backslash");
1438 inMacroMode()->name() += c;
1442 // handle 'special char' macros
1447 interpret("\\backslash");
1449 interpret(string("\\") + c);
1453 // leave macro mode and try again
1459 // leave autocorrect mode if necessary
1460 if (autocorrect_ && c == ' ') {
1461 autocorrect_ = false;
1465 // just clear selection on pressing the space par
1466 if (selection_ && c == ' ') {
1473 if (/*lastcode_ == LM_TC_TEXTRM ||*/ par()->asBoxInset()) {
1474 // suppress direct insertion of two spaces in a row
1475 // the still allows typing '<space>a<space>' and deleting the 'a', but
1476 // it is better than nothing...
1477 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1479 insert(c); // LM_TC_TEXTRM;
1484 lyxerr << "starting with macro\n";
1485 insert(MathAtom(new MathUnknownInset("\\", false)));
1490 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1491 prevAtom()->asSpaceInset()->incSpace();
1496 // if are at the very end, leave the formula
1497 return pos() != size();
1501 insert(c); // LM_TC_TEX;
1506 if (c == '{' || c == '}', c)) {
1507 insert(c); // LM_TC_TEX;
1513 niceInsert(MathAtom(new MathBraceInset));
1521 if (c == '$' || c == '%') {
1522 insert(MathAtom(new MathSpecialCharInset(c)));
1527 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1528 insert(c, LM_TC_VAR);
1532 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1533 insert(c, LM_TC_VAR);
1534 lastcode_ = LM_TC_VAR;
1539 insert(c, LM_TC_TEX);
1540 //bv->owner()->message(_("TeX mode"));
1545 // try auto-correction
1546 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1549 // no special circumstances, so insert the character without any fuss
1551 autocorrect_ = true;
1557 MathCursorPos MathCursor::normalAnchor() const
1559 if (Anchor_.size() < Cursor_.size()) {
1561 lyxerr << "unusual Anchor size\n";
1564 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1565 // use Anchor on the same level as Cursor
1566 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1567 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1568 // anchor is behind cursor -> move anchor behind the inset
1575 void MathCursor::stripFromLastEqualSign()
1577 // find position of last '=' in the array
1578 MathArray & ar = cursor().cell();
1579 MathArray::const_iterator et = ar.end();
1580 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1581 if ((*it)->getChar() == '=')
1584 // delete everything behind this position
1585 ar.erase(et - ar.begin(), ar.size());
1590 void MathCursor::setSelection(cursor_type const & where, size_type n)
1599 void MathCursor::insetToggle()
1602 nextAtom()->lock(!nextAtom()->lock());
1606 string MathCursor::info() const
1609 os << "Math editor mode ";
1610 for (int i = 0, n = Cursor_.size(); i < n; ++i) {
1611 Cursor_[i].par_->infoize(os);
1615 // prevAtom()->infoize(os);
1617 return os.str().c_str(); // .c_str() needed for lyxstring