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"
62 using std::ostringstream;
70 typedef MathInset::col_type col_type;
71 typedef MathInset::row_type row_type;
72 typedef MathInset::idx_type idx_type;
78 void region(MathCursorPos const & i1, MathCursorPos const & i2,
79 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
81 MathInset * p = i1.par_;
92 void grab(MathCursor const & cursor)
96 cursor.getSelection(i1, i2);
97 // shouldn'tt we assert on i1.par_ == i2.par_?
98 if (i1.idx_ == i2.idx_) {
99 data_ = MathGridInset(1, 1);
100 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
104 region(i1, i2, r1, r2, c1, c2);
105 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
106 for (row_type row = 0; row < data_.nrows(); ++row)
107 for (col_type col = 0; col < data_.ncols(); ++col) {
108 idx_type i = i1.par_->index(row + r1, col + c1);
109 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
114 void erase(MathCursor & cursor)
118 cursor.getSelection(i1, i2);
119 if (i1.idx_ == i2.idx_)
120 i1.cell().erase(i1.pos_, i2.pos_);
122 MathInset * p = i1.par_;
125 region(i1, i2, r1, r2, c1, c2);
126 for (row_type row = r1; row <= r2; ++row)
127 for (col_type col = c1; col <= c2; ++col)
128 p->cell(p->index(row, col)).erase();
130 cursor.cursor() = i1;
133 void paste(MathCursor & cursor) const
135 if (data_.nargs() == 1) {
136 // single cell/part of cell
137 cursor.paste(data_.cell(0));
140 idx_type idx; // index of upper left cell
141 MathGridInset * p = cursor.enclosingGrid(idx);
142 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
143 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
144 for (row_type row = 0; row < numrows; ++row) {
145 for (col_type col = 0; col < numcols; ++col) {
146 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
147 p->cell(i).push_back(data_.cell(data_.index(row, col)));
149 // append the left over horizontal cells to the last column
150 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
151 for (col_type col = numcols; col < data_.ncols(); ++col)
152 p->cell(i).push_back(data_.cell(data_.index(row, col)));
154 // append the left over vertical cells to the last _cell_
155 idx_type i = p->nargs() - 1;
156 for (row_type row = numrows; row < data_.nrows(); ++row)
157 for (col_type col = 0; col < data_.ncols(); ++col)
158 p->cell(i).push_back(data_.cell(data_.index(row, col)));
162 // glues selection to one cell
163 MathArray glue() const
166 for (unsigned i = 0; i < data_.nargs(); ++i)
167 ar.push_back(data_.cell(i));
173 data_ = MathGridInset(1, 1);
180 Selection theSelection;
187 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
188 : formula_(formula), lastcode_(LM_TC_MIN),
189 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;
309 lastcode_ = LM_TC_MIN;
313 lastcode_ = LM_TC_MIN;
315 if (hasPrevAtom() && openable(prevAtom(), sel)) {
316 pushRight(prevAtom());
320 return posLeft() || idxLeft() || popLeft() || selection_;
324 bool MathCursor::right(bool sel)
327 autocorrect_ = false;
330 lastcode_ = LM_TC_MIN;
334 lastcode_ = LM_TC_MIN;
336 if (hasNextAtom() && openable(nextAtom(), sel)) {
337 pushLeft(nextAtom());
341 return posRight() || idxRight() || popRight() || selection_;
345 void MathCursor::first()
348 pushLeft(formula_->par());
352 void MathCursor::last()
359 bool positionable(MathCursor::cursor_type const & cursor,
360 MathCursor::cursor_type const & anchor)
362 // avoid deeper nested insets when selecting
363 if (cursor.size() > anchor.size())
366 // anchor might be deeper, should have same path then
367 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
368 if (cursor[i].par_ != anchor[i].par_)
371 // position should be ok.
376 void MathCursor::setPos(int x, int y)
379 bool res = bruteFind(x, y,
380 formula()->xlow(), formula()->xhigh(),
381 formula()->ylow(), formula()->yhigh());
383 // this ccan happen on creation of "math-display"
392 void MathCursor::home(bool sel)
395 autocorrect_ = false;
398 lastcode_ = LM_TC_MIN;
399 if (!par()->idxHome(idx(), pos()))
405 void MathCursor::end(bool sel)
408 autocorrect_ = false;
411 lastcode_ = LM_TC_MIN;
412 if (!par()->idxEnd(idx(), pos()))
418 void MathCursor::plainErase()
420 array().erase(pos());
424 void MathCursor::markInsert()
426 //lyxerr << "inserting mark\n";
427 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
431 void MathCursor::markErase()
433 //lyxerr << "deleting mark\n";
434 array().erase(pos());
438 void MathCursor::plainInsert(MathAtom const & t)
440 array().insert(pos(), t);
445 void MathCursor::insert(char c, MathTextCodes t)
447 //lyxerr << "inserting '" << c << "'\n";
449 plainInsert(MathAtom(new MathCharInset(c, t)));
453 void MathCursor::insert(char c)
455 insert(c, lastcode_);
459 void MathCursor::insert(MathAtom const & t)
474 void MathCursor::niceInsert(MathAtom const & t)
477 insert(t); // inserting invalidates the pointer!
478 MathAtom const & p = prevAtom();
481 right(); // do not push for e.g. MathSymbolInset
487 void MathCursor::insert(MathArray const & ar)
493 array().insert(pos(), ar);
498 void MathCursor::paste(MathArray const & ar)
502 array().insert(pos(), ar);
507 void MathCursor::backspace()
509 autocorrect_ = false;
520 MathScriptInset * p = prevAtom()->asScriptInset();
522 p->removeScript(p->hasUp());
523 // Don't delete if there is anything left
524 if (p->hasUp() || p->hasDown())
533 void MathCursor::erase()
535 autocorrect_ = false;
544 // delete empty cells if possible
546 if (par()->idxDelete(idx()))
549 // old behaviour when in last position of cell
550 if (pos() == size()) {
551 par()->idxGlue(idx());
555 MathScriptInset * p = nextAtom()->asScriptInset();
557 p->removeScript(p->hasUp());
558 // Don't delete if there is anything left
559 if (p->hasUp() || p->hasDown())
567 void MathCursor::delLine()
569 autocorrect_ = false;
577 if (par()->nrows() > 1) {
578 // grid are the only things with more than one row...
579 lyx::Assert(par()->asGridInset());
580 par()->asGridInset()->delRow(hullRow());
583 if (idx() >= par()->nargs())
584 idx() = par()->nargs() - 1;
591 bool MathCursor::up(bool sel)
596 cursor_type save = Cursor_;
600 autocorrect_ = false;
605 bool MathCursor::down(bool sel)
610 cursor_type save = Cursor_;
614 autocorrect_ = false;
619 bool MathCursor::toggleLimits()
623 MathScriptInset * t = nextAtom()->asScriptInset();
626 int old = t->limits();
627 t->limits(old < 0 ? 1 : -1);
628 return old != t->limits();
632 void MathCursor::macroModeClose()
634 MathInset::difference_type const t = macroNamePos();
637 string s = macroName();
638 array().erase(t, pos());
645 MathInset::difference_type MathCursor::macroNamePos() const
647 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
648 MathAtom & p = array().at(i);
649 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
656 string MathCursor::macroName() const
659 MathInset::difference_type i = macroNamePos();
660 for (; i >= 0 && i < int(pos()); ++i)
661 s += array().at(i)->getChar();
666 void MathCursor::selCopy()
670 theSelection.grab(*this);
676 void MathCursor::selCut()
680 theSelection.grab(*this);
681 theSelection.erase(*this);
684 theSelection.clear();
689 void MathCursor::selDel()
693 theSelection.erase(*this);
701 void MathCursor::selPaste()
705 theSelection.paste(*this);
706 //theSelection.grab(*this);
711 void MathCursor::selHandle(bool sel)
713 if (sel == selection_)
715 //theSelection.clear();
721 void MathCursor::selStart()
724 //theSelection.clear();
731 void MathCursor::selClear()
739 void MathCursor::selClearOrDel()
741 if (lyxrc.auto_region_delete)
748 void MathCursor::selGet(MathArray & ar)
754 theSelection.grab(*this);
755 ar = theSelection.glue();
760 void MathCursor::drawSelection(Painter & pain) const
767 getSelection(i1, i2);
769 if (i1.idx_ == i2.idx_) {
770 MathXArray & c = i1.xcell();
771 int x1 = c.xo() + c.pos2x(i1.pos_);
772 int y1 = c.yo() - c.ascent();
773 int x2 = c.xo() + c.pos2x(i2.pos_);
774 int y2 = c.yo() + c.descent();
775 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
777 vector<MathInset::idx_type> indices
778 = i1.par_->idxBetween(i1.idx_, i2.idx_);
779 for (unsigned i = 0; i < indices.size(); ++i) {
780 MathXArray & c = i1.xcell(indices[i]);
782 int y1 = c.yo() - c.ascent();
783 int x2 = c.xo() + c.width();
784 int y2 = c.yo() + c.descent();
785 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
790 // draw anchor if different from selection boundary
791 MathCursorPos anc = Anchor_.back();
792 if (anc != i1 && anc != i2) {
793 MathXArray & c = anc.xcell();
794 int x = c.xo() + c.pos2x(anc.pos_);
795 int y1 = c.yo() - c.ascent();
796 int y2 = c.yo() + c.descent();
797 pain.line(x, y1, x, y2, LColor::math);
803 void MathCursor::handleFont(MathTextCodes t)
809 getSelection(i1, i2);
810 if (i1.idx_ == i2.idx_) {
811 MathArray & ar = i1.cell();
812 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
813 ar.at(pos)->handleFont(t);
816 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
820 void MathCursor::handleDelim(string const & l, string const & r)
822 handleNest(new MathDelimInset(l, r));
826 void MathCursor::handleNest(MathInset * p)
830 p->cell(0) = theSelection.glue();
832 insert(MathAtom(p)); // this invalidates p!
833 pushRight(prevAtom());
837 void MathCursor::getPos(int & x, int & y)
840 #warning This should probably take cellXOffset and cellYOffset into account
842 x = xarray().xo() + xarray().pos2x(pos());
843 // move cursor visually into empty cells ("blue rectangles");
850 MathInset * MathCursor::par() const
852 return cursor().par_;
856 InsetFormulaBase * MathCursor::formula()
862 MathCursor::idx_type MathCursor::idx() const
864 return cursor().idx_;
868 MathCursor::idx_type & MathCursor::idx()
870 return cursor().idx_;
874 MathCursor::pos_type MathCursor::pos() const
876 return cursor().pos_;
880 MathCursor::pos_type & MathCursor::pos()
882 return cursor().pos_;
886 bool MathCursor::inMacroMode() const
888 return macroNamePos() != -1;
892 bool MathCursor::inMacroArgMode() const
894 return pos() > 0 && prevAtom()->getChar() == '#';
898 bool MathCursor::selection() const
904 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
906 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
907 MathGridInset * p = Cursor_[i].par_->asGridInset();
909 idx = Cursor_[i].idx_;
911 lyxerr << "found grid and idx: " << idx << "\n";
918 void MathCursor::popToEnclosingGrid()
920 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
925 void MathCursor::pullArg(bool goright)
928 MathArray a = array();
930 MathScriptInset const * p = par()->asScriptInset();
932 // special handling for scripts
933 const bool up = p->hasUp();
935 MathScriptInset * q = nextAtom()->asScriptInset();
939 array().insert(pos(), a);
945 array().insert(pos(), a);
949 formula()->mutateToText();
954 void MathCursor::touch()
956 cursor_type::const_iterator it = Cursor_.begin();
957 cursor_type::const_iterator et = Cursor_.end();
958 for ( ; it != et; ++it)
963 void MathCursor::normalize()
968 MathIterator it = ibegin(formula()->par().nucleus());
969 MathIterator et = iend(formula()->par().nucleus());
970 for (; it != et; ++it)
971 if (it.par()->asBoxInset())
972 it.par()->asBoxInset()->rebreak();
976 if (idx() >= par()->nargs()) {
977 lyxerr << "this should not really happen - 1: "
978 << idx() << " " << par()->nargs() << "\n";
981 idx() = min(idx(), par()->nargs() - 1);
983 if (pos() > size()) {
984 lyxerr << "this should not really happen - 2: "
985 << pos() << " " << size() << " in idx: " << idx()
987 WriteStream wi(lyxerr, false, true);
992 pos() = min(pos(), size());
994 // remove empty scripts if possible
996 for (pos_type i = 0; i < size(); ++i) {
997 MathScriptInset * p = array().at(i)->asScriptInset();
999 p->removeEmptyScripts();
1001 // array().erase(i);
1006 // fix again position
1007 pos() = min(pos(), size());
1011 MathCursor::size_type MathCursor::size() const
1013 return array().size();
1017 MathCursor::col_type MathCursor::hullCol() const
1019 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
1023 MathCursor::row_type MathCursor::hullRow() const
1025 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1029 bool MathCursor::hasPrevAtom() const
1035 bool MathCursor::hasNextAtom() const
1037 return pos() < size();
1041 MathAtom const & MathCursor::prevAtom() const
1043 lyx::Assert(pos() > 0);
1044 return array().at(pos() - 1);
1048 MathAtom & MathCursor::prevAtom()
1050 lyx::Assert(pos() > 0);
1051 return array().at(pos() - 1);
1055 MathAtom const & MathCursor::nextAtom() const
1057 lyx::Assert(pos() < size());
1058 return array().at(pos());
1062 MathAtom & MathCursor::nextAtom()
1064 lyx::Assert(pos() < size());
1065 return array().at(pos());
1069 MathArray & MathCursor::array() const
1071 static MathArray dummy;
1073 if (idx() >= par()->nargs()) {
1074 lyxerr << "############ idx_ " << idx() << " not valid\n";
1078 if (Cursor_.size() == 0) {
1079 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1083 return cursor().cell();
1087 MathXArray & MathCursor::xarray() const
1089 static MathXArray dummy;
1091 if (Cursor_.size() == 0) {
1092 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1096 return cursor().xcell();
1100 void MathCursor::idxNext()
1102 par()->idxNext(idx(), pos());
1106 void MathCursor::idxPrev()
1108 par()->idxPrev(idx(), pos());
1112 void MathCursor::splitCell()
1114 if (idx() + 1 == par()->nargs())
1116 MathArray ar = array();
1118 array().erase(pos(), size());
1121 array().insert(0, ar);
1125 void MathCursor::breakLine()
1127 // leave inner cells
1131 MathHullInset * p = formula()->par()->asHullInset();
1135 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1136 p->mutate(LM_OT_EQNARRAY);
1140 p->addRow(hullRow());
1143 const row_type r = hullRow();
1144 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1145 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1149 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1154 //void MathCursor::readLine(MathArray & ar) const
1156 // idx_type base = row() * par()->ncols();
1157 // for (idx_type off = 0; off < par()->ncols(); ++off)
1158 // ar.push_back(par()->cell(base + off));
1162 char MathCursor::valign() const
1165 MathGridInset * p = enclosingGrid(idx);
1166 return p ? p->valign() : '\0';
1170 char MathCursor::halign() const
1173 MathGridInset * p = enclosingGrid(idx);
1174 return p ? p->halign(idx % p->ncols()) : '\0';
1178 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1180 MathCursorPos anc = normalAnchor();
1181 if (anc < cursor()) {
1191 MathCursorPos & MathCursor::cursor()
1193 lyx::Assert(Cursor_.size());
1194 return Cursor_.back();
1198 MathCursorPos const & MathCursor::cursor() const
1200 lyx::Assert(Cursor_.size());
1201 return Cursor_.back();
1205 bool MathCursor::goUpDown(bool up)
1207 // Be warned: The 'logic' implemented in this function is highly fragile.
1208 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1209 // So fiddle around with it only if you know what you are doing!
1210 int xlow, xhigh, ylow, yhigh;
1215 // try neigbouring script insets
1217 if (hasPrevAtom()) {
1218 MathScriptInset * p = prevAtom()->asScriptInset();
1219 if (p && p->has(up)) {
1222 idx() = up; // the superscript has index 1
1224 ///lyxerr << "updown: handled by scriptinset to the left\n";
1230 if (hasNextAtom()) {
1231 MathScriptInset * p = nextAtom()->asScriptInset();
1232 if (p && p->has(up)) {
1236 ///lyxerr << "updown: handled by scriptinset to the right\n";
1242 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1247 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1248 // lyxerr << "updown: handled by brute find in the same cell\n";
1252 // try to find an inset that knows better then we
1254 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1256 if (par()->idxUpDown(idx(), up)) {
1257 // we found a cell that thinks it has something "below" us.
1258 ///lyxerr << "updown: found inset that handles UpDown\n";
1259 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1260 // project (xo,yo) onto proper box
1261 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1262 /// << "\n xlow: " << xlow << " ylow: " << ylow
1263 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1264 xo = min(max(xo, xlow), xhigh);
1265 yo = min(max(yo, ylow), yhigh);
1266 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1267 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1268 ///lyxerr << "updown: handled by final brute find\n";
1274 // no such inset found, just take something "above"
1275 ///lyxerr << "updown: handled by strange case\n";
1280 up ? formula()->ylow() : yo + 4,
1281 up ? yo - 4 : formula()->yhigh()
1285 // any improvement so far?
1288 if (up ? ynew < yo : ynew > yo)
1294 bool MathCursor::bruteFind
1295 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1297 cursor_type best_cursor;
1298 double best_dist = 1e10;
1300 MathIterator it = ibegin(formula()->par().nucleus());
1301 MathIterator et = iend(formula()->par().nucleus());
1303 // avoid invalid nesting when selecting
1304 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1305 MathCursorPos const & top = it.position();
1306 int xo = top.xpos();
1307 int yo = top.ypos();
1308 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1309 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1310 // '<=' in order to take the last possible position
1311 // this is important for clicking behind \sum in e.g. '\sum_i a'
1312 if (d <= best_dist) {
1314 best_cursor = it.cursor();
1324 if (best_dist < 1e10)
1325 Cursor_ = best_cursor;
1326 return best_dist < 1e10;
1330 bool MathCursor::idxLeft()
1332 return par()->idxLeft(idx(), pos());
1336 bool MathCursor::idxRight()
1338 return par()->idxRight(idx(), pos());
1342 bool MathCursor::interpret(string const & s)
1344 //lyxerr << "interpret 1: '" << s << "'\n";
1348 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1349 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1350 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1352 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1354 istringstream is(s.substr(5).c_str());
1357 niceInsert(MathAtom(new MathCasesInset(n)));
1361 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1366 istringstream is(s.substr(6).c_str());
1367 is >> m >> n >> v_align >> h_align;
1371 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1375 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1377 istringstream is(s.substr(7).c_str());
1380 mathed_parse_cell(rep.from, from);
1381 mathed_parse_cell(rep.to, to);
1382 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1383 par()->replace(rep);
1387 string name = s.substr(1);
1389 if (name == "over" || name == "choose" || name == "atop") {
1390 MathArray ar = array();
1391 MathAtom t(createMathInset(name));
1392 t->asNestInset()->cell(0).swap(array());
1400 latexkeys const * l = in_word_set(name);
1401 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1402 lastcode_ = static_cast<MathTextCodes>(l->id);
1406 // prevent entering of recursive macros
1407 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1408 && formula()->getInsetName() == name)
1410 lyxerr << "can't enter recursive macro\n";
1414 niceInsert(createMathInset(name));
1419 bool MathCursor::script(bool up)
1421 // Hack to get \\^ and \\_ working
1422 if (inMacroMode() && macroName() == "\\") {
1424 interpret("\\mathcircumflex");
1432 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1433 prevAtom()->asScriptInset()->ensure(up);
1434 pushRight(prevAtom());
1437 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1438 nextAtom()->asScriptInset()->ensure(up);
1439 pushLeft(nextAtom());
1443 plainInsert(MathAtom(new MathScriptInset(up)));
1444 prevAtom()->asScriptInset()->ensure(up);
1445 pushRight(prevAtom());
1455 bool MathCursor::interpret(char c)
1457 //lyxerr << "interpret 2: '" << c << "'\n";
1458 if (inMacroArgMode()) {
1462 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1463 if (p && 1 <= n && n <= p->numargs())
1464 insert(MathAtom(new MathMacroArgument(c - '0', lastcode_)));
1466 insert(MathAtom(new MathSpecialCharInset('#')));
1467 interpret(c); // try again
1473 if (inMacroMode()) {
1474 string name = macroName();
1475 //lyxerr << "interpret name: '" << name << "'\n";
1477 // extend macro name if possible
1479 insert(c, LM_TC_TEX);
1483 // leave macro mode if explicitly requested
1489 // handle 'special char' macros
1494 interpret("\\backslash");
1496 interpret(string("\\") + c);
1500 // leave macro mode and try again
1506 // leave autocorrect mode if necessary
1507 if (autocorrect_ && c == ' ') {
1508 autocorrect_ = false;
1512 // just clear selection on pressing the space par
1513 if (selection_ && c == ' ') {
1520 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1521 // suppress direct insertion of two spaces in a row
1522 // the still allows typing '<space>a<space>' and deleting the 'a', but
1523 // it is better than nothing...
1524 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1526 insert(c, LM_TC_TEXTRM);
1531 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1532 prevAtom()->asSpaceInset()->incSpace();
1537 // if are at the very end, leave the formula
1538 return pos() != size();
1542 insert(c, LM_TC_TEX);
1547 if (c == '{' || c == '}', c)) {
1548 insert(c, LM_TC_TEX);
1554 niceInsert(MathAtom(new MathBraceInset));
1562 if (c == '$' || c == '%') {
1563 insert(MathAtom(new MathSpecialCharInset(c)));
1564 lastcode_ = LM_TC_VAR;
1568 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1569 insert(c, LM_TC_VAR);
1573 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1574 insert(c, LM_TC_VAR);
1575 lastcode_ = LM_TC_VAR;
1580 insert(c, LM_TC_TEX);
1581 //bv->owner()->message(_("TeX mode"));
1585 // try auto-correction
1586 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1589 // no special circumstances, so insert the character without any fuss
1590 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1591 lastcode_ = LM_TC_MIN;
1592 autocorrect_ = true;
1598 MathCursorPos MathCursor::normalAnchor() const
1600 if (Anchor_.size() < Cursor_.size()) {
1602 lyxerr << "unusual Anchor size\n";
1605 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1606 // use Anchor on the same level as Cursor
1607 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1608 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1609 // anchor is behind cursor -> move anchor behind the inset
1616 void MathCursor::stripFromLastEqualSign()
1618 // find position of last '=' in the array
1619 MathArray & ar = cursor().cell();
1620 MathArray::const_iterator et = ar.end();
1621 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1622 if ((*it)->getChar() == '=')
1625 // delete everything behind this position
1626 ar.erase(et - ar.begin(), ar.size());
1631 void MathCursor::setSelection(cursor_type const & where, size_type n)
1640 string MathCursor::info() const
1644 prevAtom()->infoize(os);
1645 return os.str().c_str(); // .c_str() needed for lyxstring