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;
68 typedef MathInset::col_type col_type;
69 typedef MathInset::row_type row_type;
70 typedef MathInset::idx_type idx_type;
76 void region(MathCursorPos const & i1, MathCursorPos const & i2,
77 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
79 MathInset * p = i1.par_;
90 void grab(MathCursor const & cursor)
94 cursor.getSelection(i1, i2);
95 // shouldn'tt we assert on i1.par_ == i2.par_?
96 if (i1.idx_ == i2.idx_) {
97 data_ = MathGridInset(1, 1);
98 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
102 region(i1, i2, r1, r2, c1, c2);
103 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
104 for (row_type row = 0; row < data_.nrows(); ++row)
105 for (col_type col = 0; col < data_.ncols(); ++col) {
106 idx_type i = i1.par_->index(row + r1, col + c1);
107 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
112 void erase(MathCursor & cursor)
116 cursor.getSelection(i1, i2);
117 if (i1.idx_ == i2.idx_)
118 i1.cell().erase(i1.pos_, i2.pos_);
120 MathInset * p = i1.par_;
123 region(i1, i2, r1, r2, c1, c2);
124 for (row_type row = r1; row <= r2; ++row)
125 for (col_type col = c1; col <= c2; ++col)
126 p->cell(p->index(row, col)).erase();
128 cursor.cursor() = i1;
131 void paste(MathCursor & cursor) const
133 if (data_.nargs() == 1) {
134 // single cell/part of cell
135 cursor.paste(data_.cell(0));
138 idx_type idx; // index of upper left cell
139 MathGridInset * p = cursor.enclosingGrid(idx);
140 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
141 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
142 for (row_type row = 0; row < numrows; ++row) {
143 for (col_type col = 0; col < numcols; ++col) {
144 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
145 p->cell(i).push_back(data_.cell(data_.index(row, col)));
147 // append the left over horizontal cells to the last column
148 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
149 for (col_type col = numcols; col < data_.ncols(); ++col)
150 p->cell(i).push_back(data_.cell(data_.index(row, col)));
152 // append the left over vertical cells to the last _cell_
153 idx_type i = p->nargs() - 1;
154 for (row_type row = numrows; row < data_.nrows(); ++row)
155 for (col_type col = 0; col < data_.ncols(); ++col)
156 p->cell(i).push_back(data_.cell(data_.index(row, col)));
160 // glues selection to one cell
161 MathArray glue() const
164 for (unsigned i = 0; i < data_.nargs(); ++i)
165 ar.push_back(data_.cell(i));
171 data_ = MathGridInset(1, 1);
178 Selection theSelection;
185 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
186 : formula_(formula), lastcode_(LM_TC_MIN),
187 autocorrect_(false), selection_(false)
189 left ? first() : last();
193 void MathCursor::push(MathAtom & t)
195 Cursor_.push_back(MathCursorPos(t.nucleus()));
199 void MathCursor::pushLeft(MathAtom & t)
201 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
203 t->idxFirst(idx(), pos());
207 void MathCursor::pushRight(MathAtom & t)
209 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
212 t->idxLast(idx(), pos());
216 bool MathCursor::popLeft()
218 //cerr << "Leaving atom to the left\n";
219 if (Cursor_.size() <= 1)
226 bool MathCursor::popRight()
228 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
229 if (Cursor_.size() <= 1)
239 void MathCursor::dump(char const * what) const
241 lyxerr << "MC: " << what << "\n";
242 lyxerr << " Cursor: " << Cursor_.size() << "\n";
243 for (unsigned i = 0; i < Cursor_.size(); ++i)
244 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
245 lyxerr << " Anchor: " << Anchor_.size() << "\n";
246 for (unsigned i = 0; i < Anchor_.size(); ++i)
247 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
248 lyxerr << " sel: " << selection_ << "\n";
251 void MathCursor::dump(char const *) const {}
255 bool MathCursor::isInside(MathInset const * p) const
257 for (unsigned i = 0; i < Cursor_.size(); ++i)
258 if (Cursor_[i].par_ == p)
264 bool MathCursor::openable(MathAtom const & t, bool sel) const
269 if (t->asScriptInset())
273 // we can't move into anything new during selection
274 if (Cursor_.size() == Anchor_.size())
276 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
283 bool MathCursor::posLeft()
292 bool MathCursor::posRight()
301 bool MathCursor::left(bool sel)
304 autocorrect_ = false;
307 lastcode_ = LM_TC_MIN;
311 lastcode_ = LM_TC_MIN;
313 if (hasPrevAtom() && openable(prevAtom(), sel)) {
314 pushRight(prevAtom());
318 return posLeft() || idxLeft() || popLeft() || selection_;
322 bool MathCursor::right(bool sel)
325 autocorrect_ = false;
328 lastcode_ = LM_TC_MIN;
332 lastcode_ = LM_TC_MIN;
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 lastcode_ = LM_TC_MIN;
397 if (!par()->idxHome(idx(), pos()))
403 void MathCursor::end(bool sel)
406 autocorrect_ = false;
409 lastcode_ = LM_TC_MIN;
410 if (!par()->idxEnd(idx(), pos()))
416 void MathCursor::plainErase()
418 array().erase(pos());
422 void MathCursor::markInsert()
424 //lyxerr << "inserting mark\n";
425 array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
429 void MathCursor::markErase()
431 //lyxerr << "deleting mark\n";
432 array().erase(pos());
436 void MathCursor::plainInsert(MathAtom const & t)
438 array().insert(pos(), t);
443 void MathCursor::insert(char c, MathTextCodes t)
445 //lyxerr << "inserting '" << c << "'\n";
447 plainInsert(MathAtom(new MathCharInset(c, t)));
451 void MathCursor::insert(char c)
453 insert(c, lastcode_);
457 void MathCursor::insert(MathAtom const & t)
472 void MathCursor::niceInsert(MathAtom const & t)
475 insert(t); // inserting invalidates the pointer!
476 MathAtom const & p = prevAtom();
479 right(); // do not push for e.g. MathSymbolInset
485 void MathCursor::insert(MathArray const & ar)
491 array().insert(pos(), ar);
496 void MathCursor::paste(MathArray const & ar)
500 array().insert(pos(), ar);
505 void MathCursor::backspace()
507 autocorrect_ = false;
518 MathScriptInset * p = prevAtom()->asScriptInset();
520 p->removeScript(p->hasUp());
521 // Don't delete if there is anything left
522 if (p->hasUp() || p->hasDown())
531 void MathCursor::erase()
533 autocorrect_ = false;
542 // delete empty cells if possible
544 if (par()->idxDelete(idx()))
547 // old behaviour when in last position of cell
548 if (pos() == size()) {
549 par()->idxGlue(idx());
553 MathScriptInset * p = nextAtom()->asScriptInset();
555 p->removeScript(p->hasUp());
556 // Don't delete if there is anything left
557 if (p->hasUp() || p->hasDown())
565 void MathCursor::delLine()
567 autocorrect_ = false;
575 if (par()->nrows() > 1) {
576 // grid are the only things with more than one row...
577 lyx::Assert(par()->asGridInset());
578 par()->asGridInset()->delRow(hullRow());
581 if (idx() >= par()->nargs())
582 idx() = par()->nargs() - 1;
589 bool MathCursor::up(bool sel)
594 cursor_type save = Cursor_;
598 autocorrect_ = false;
603 bool MathCursor::down(bool sel)
608 cursor_type save = Cursor_;
612 autocorrect_ = false;
617 bool MathCursor::toggleLimits()
621 MathScriptInset * t = nextAtom()->asScriptInset();
624 int old = t->limits();
625 t->limits(old < 0 ? 1 : -1);
626 return old != t->limits();
630 void MathCursor::macroModeClose()
632 MathInset::difference_type const t = macroNamePos();
635 string s = macroName();
636 array().erase(t, pos());
643 MathInset::difference_type MathCursor::macroNamePos() const
645 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
646 MathAtom & p = array().at(i);
647 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
654 string MathCursor::macroName() const
657 MathInset::difference_type i = macroNamePos();
658 for (; i >= 0 && i < int(pos()); ++i)
659 s += array().at(i)->getChar();
664 void MathCursor::selCopy()
668 theSelection.grab(*this);
674 void MathCursor::selCut()
678 theSelection.grab(*this);
679 theSelection.erase(*this);
682 theSelection.clear();
687 void MathCursor::selDel()
691 theSelection.erase(*this);
699 void MathCursor::selPaste()
703 theSelection.paste(*this);
704 //theSelection.grab(*this);
709 void MathCursor::selHandle(bool sel)
711 if (sel == selection_)
713 //theSelection.clear();
719 void MathCursor::selStart()
722 //theSelection.clear();
729 void MathCursor::selClear()
737 void MathCursor::selClearOrDel()
739 if (lyxrc.auto_region_delete)
746 void MathCursor::selGet(MathArray & ar)
752 theSelection.grab(*this);
753 ar = theSelection.glue();
758 void MathCursor::drawSelection(Painter & pain) const
765 getSelection(i1, i2);
767 if (i1.idx_ == i2.idx_) {
768 MathXArray & c = i1.xcell();
769 int x1 = c.xo() + c.pos2x(i1.pos_);
770 int y1 = c.yo() - c.ascent();
771 int x2 = c.xo() + c.pos2x(i2.pos_);
772 int y2 = c.yo() + c.descent();
773 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
775 vector<MathInset::idx_type> indices
776 = i1.par_->idxBetween(i1.idx_, i2.idx_);
777 for (unsigned i = 0; i < indices.size(); ++i) {
778 MathXArray & c = i1.xcell(indices[i]);
780 int y1 = c.yo() - c.ascent();
781 int x2 = c.xo() + c.width();
782 int y2 = c.yo() + c.descent();
783 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
788 // draw anchor if different from selection boundary
789 MathCursorPos anc = Anchor_.back();
790 if (anc != i1 && anc != i2) {
791 MathXArray & c = anc.xcell();
792 int x = c.xo() + c.pos2x(anc.pos_);
793 int y1 = c.yo() - c.ascent();
794 int y2 = c.yo() + c.descent();
795 pain.line(x, y1, x, y2, LColor::math);
801 void MathCursor::handleFont(MathTextCodes t)
807 getSelection(i1, i2);
808 if (i1.idx_ == i2.idx_) {
809 MathArray & ar = i1.cell();
810 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
811 ar.at(pos)->handleFont(t);
814 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
818 void MathCursor::handleDelim(string const & l, string const & r)
820 handleNest(new MathDelimInset(l, r));
824 void MathCursor::handleNest(MathInset * p)
828 p->cell(0) = theSelection.glue();
830 insert(MathAtom(p)); // this invalidates p!
831 pushRight(prevAtom());
835 void MathCursor::getPos(int & x, int & y)
838 #warning This should probably take cellXOffset and cellYOffset into account
840 x = xarray().xo() + xarray().pos2x(pos());
841 // move cursor visually into empty cells ("blue rectangles");
848 MathInset * MathCursor::par() const
850 return cursor().par_;
854 InsetFormulaBase * MathCursor::formula()
860 MathCursor::idx_type MathCursor::idx() const
862 return cursor().idx_;
866 MathCursor::idx_type & MathCursor::idx()
868 return cursor().idx_;
872 MathCursor::pos_type MathCursor::pos() const
874 return cursor().pos_;
878 MathCursor::pos_type & MathCursor::pos()
880 return cursor().pos_;
884 bool MathCursor::inMacroMode() const
886 return macroNamePos() != -1;
890 bool MathCursor::inMacroArgMode() const
892 return pos() > 0 && prevAtom()->getChar() == '#';
896 bool MathCursor::selection() const
902 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
904 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
905 MathGridInset * p = Cursor_[i].par_->asGridInset();
907 idx = Cursor_[i].idx_;
909 lyxerr << "found grid and idx: " << idx << "\n";
916 void MathCursor::popToEnclosingGrid()
918 while (Cursor_.size() && !Cursor_.back().par_->asGridInset())
923 void MathCursor::pullArg(bool goright)
926 MathArray a = array();
928 MathScriptInset const * p = par()->asScriptInset();
930 // special handling for scripts
931 const bool up = p->hasUp();
933 MathScriptInset * q = nextAtom()->asScriptInset();
937 array().insert(pos(), a);
943 array().insert(pos(), a);
947 formula()->mutateToText();
952 void MathCursor::touch()
954 cursor_type::const_iterator it = Cursor_.begin();
955 cursor_type::const_iterator et = Cursor_.end();
956 for ( ; it != et; ++it)
961 void MathCursor::normalize()
966 MathIterator it = ibegin(formula()->par().nucleus());
967 MathIterator et = iend(formula()->par().nucleus());
968 for (; it != et; ++it)
969 if (it.par()->asBoxInset())
970 it.par()->asBoxInset()->rebreak();
974 if (idx() >= par()->nargs()) {
975 lyxerr << "this should not really happen - 1: "
976 << idx() << " " << par()->nargs() << "\n";
979 idx() = min(idx(), par()->nargs() - 1);
981 if (pos() > size()) {
982 lyxerr << "this should not really happen - 2: "
983 << pos() << " " << size() << " in idx: " << idx()
985 WriteStream wi(lyxerr, false, true);
990 pos() = min(pos(), size());
992 // remove empty scripts if possible
994 for (pos_type i = 0; i < size(); ++i) {
995 MathScriptInset * p = array().at(i)->asScriptInset();
997 p->removeEmptyScripts();
1004 // fix again position
1005 pos() = min(pos(), size());
1009 MathCursor::size_type MathCursor::size() const
1011 return array().size();
1015 MathCursor::col_type MathCursor::hullCol() const
1017 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
1021 MathCursor::row_type MathCursor::hullRow() const
1023 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1027 bool MathCursor::hasPrevAtom() const
1033 bool MathCursor::hasNextAtom() const
1035 return pos() < size();
1039 MathAtom const & MathCursor::prevAtom() const
1041 lyx::Assert(pos() > 0);
1042 return array().at(pos() - 1);
1046 MathAtom & MathCursor::prevAtom()
1048 lyx::Assert(pos() > 0);
1049 return array().at(pos() - 1);
1053 MathAtom const & MathCursor::nextAtom() const
1055 lyx::Assert(pos() < size());
1056 return array().at(pos());
1060 MathAtom & MathCursor::nextAtom()
1062 lyx::Assert(pos() < size());
1063 return array().at(pos());
1067 MathArray & MathCursor::array() const
1069 static MathArray dummy;
1071 if (idx() >= par()->nargs()) {
1072 lyxerr << "############ idx_ " << idx() << " not valid\n";
1076 if (Cursor_.size() == 0) {
1077 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1081 return cursor().cell();
1085 MathXArray & MathCursor::xarray() const
1087 static MathXArray dummy;
1089 if (Cursor_.size() == 0) {
1090 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1094 return cursor().xcell();
1098 void MathCursor::idxNext()
1100 par()->idxNext(idx(), pos());
1104 void MathCursor::idxPrev()
1106 par()->idxPrev(idx(), pos());
1110 void MathCursor::splitCell()
1112 if (idx() + 1 == par()->nargs())
1114 MathArray ar = array();
1116 array().erase(pos(), size());
1119 array().insert(0, ar);
1123 void MathCursor::breakLine()
1125 // leave inner cells
1129 MathHullInset * p = formula()->par()->asHullInset();
1133 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1134 p->mutate(LM_OT_EQNARRAY);
1138 p->addRow(hullRow());
1141 const row_type r = hullRow();
1142 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1143 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1147 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1152 //void MathCursor::readLine(MathArray & ar) const
1154 // idx_type base = row() * par()->ncols();
1155 // for (idx_type off = 0; off < par()->ncols(); ++off)
1156 // ar.push_back(par()->cell(base + off));
1160 char MathCursor::valign() const
1163 MathGridInset * p = enclosingGrid(idx);
1164 return p ? p->valign() : '\0';
1168 char MathCursor::halign() const
1171 MathGridInset * p = enclosingGrid(idx);
1172 return p ? p->halign(idx % p->ncols()) : '\0';
1176 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1178 MathCursorPos anc = normalAnchor();
1179 if (anc < cursor()) {
1189 MathCursorPos & MathCursor::cursor()
1191 lyx::Assert(Cursor_.size());
1192 return Cursor_.back();
1196 MathCursorPos const & MathCursor::cursor() const
1198 lyx::Assert(Cursor_.size());
1199 return Cursor_.back();
1203 bool MathCursor::goUpDown(bool up)
1205 // Be warned: The 'logic' implemented in this function is highly fragile.
1206 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1207 // So fiddle around with it only if you know what you are doing!
1208 int xlow, xhigh, ylow, yhigh;
1213 // try neigbouring script insets
1215 if (hasPrevAtom()) {
1216 MathScriptInset * p = prevAtom()->asScriptInset();
1217 if (p && p->has(up)) {
1220 idx() = up; // the superscript has index 1
1222 ///lyxerr << "updown: handled by scriptinset to the left\n";
1228 if (hasNextAtom()) {
1229 MathScriptInset * p = nextAtom()->asScriptInset();
1230 if (p && p->has(up)) {
1234 ///lyxerr << "updown: handled by scriptinset to the right\n";
1240 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1245 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1246 // lyxerr << "updown: handled by brute find in the same cell\n";
1250 // try to find an inset that knows better then we
1252 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1254 if (par()->idxUpDown(idx(), up)) {
1255 // we found a cell that thinks it has something "below" us.
1256 ///lyxerr << "updown: found inset that handles UpDown\n";
1257 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1258 // project (xo,yo) onto proper box
1259 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1260 /// << "\n xlow: " << xlow << " ylow: " << ylow
1261 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1262 xo = min(max(xo, xlow), xhigh);
1263 yo = min(max(yo, ylow), yhigh);
1264 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1265 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1266 ///lyxerr << "updown: handled by final brute find\n";
1272 // no such inset found, just take something "above"
1273 ///lyxerr << "updown: handled by strange case\n";
1278 up ? formula()->ylow() : yo + 4,
1279 up ? yo - 4 : formula()->yhigh()
1283 // any improvement so far?
1286 if (up ? ynew < yo : ynew > yo)
1292 bool MathCursor::bruteFind
1293 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1295 cursor_type best_cursor;
1296 double best_dist = 1e10;
1298 MathIterator it = ibegin(formula()->par().nucleus());
1299 MathIterator et = iend(formula()->par().nucleus());
1301 // avoid invalid nesting when selecting
1302 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1303 MathCursorPos const & top = it.position();
1304 int xo = top.xpos();
1305 int yo = top.ypos();
1306 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1307 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1308 // '<=' in order to take the last possible position
1309 // this is important for clicking behind \sum in e.g. '\sum_i a'
1310 if (d <= best_dist) {
1312 best_cursor = it.cursor();
1322 if (best_dist < 1e10)
1323 Cursor_ = best_cursor;
1324 return best_dist < 1e10;
1328 bool MathCursor::idxLeft()
1330 return par()->idxLeft(idx(), pos());
1334 bool MathCursor::idxRight()
1336 return par()->idxRight(idx(), pos());
1340 bool MathCursor::interpret(string const & s)
1342 //lyxerr << "interpret 1: '" << s << "'\n";
1346 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1347 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1348 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1350 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1352 istringstream is(s.substr(5).c_str());
1355 niceInsert(MathAtom(new MathCasesInset(n)));
1359 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1364 istringstream is(s.substr(6).c_str());
1365 is >> m >> n >> v_align >> h_align;
1369 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1373 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1375 istringstream is(s.substr(7).c_str());
1378 mathed_parse_cell(rep.from, from);
1379 mathed_parse_cell(rep.to, to);
1380 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1381 par()->replace(rep);
1385 string name = s.substr(1);
1387 if (name == "over" || name == "choose" || name == "atop") {
1388 MathArray ar = array();
1389 MathAtom t(createMathInset(name));
1390 t->asNestInset()->cell(0).swap(array());
1398 latexkeys const * l = in_word_set(name);
1399 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1400 lastcode_ = static_cast<MathTextCodes>(l->id);
1404 // prevent entering of recursive macros
1405 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1406 && formula()->getInsetName() == name)
1408 lyxerr << "can't enter recursive macro\n";
1412 niceInsert(createMathInset(name));
1417 bool MathCursor::script(bool up)
1419 // Hack to get \\^ and \\_ working
1420 if (inMacroMode() && macroName() == "\\") {
1422 interpret("\\mathcircumflex");
1430 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1431 prevAtom()->asScriptInset()->ensure(up);
1432 pushRight(prevAtom());
1435 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1436 nextAtom()->asScriptInset()->ensure(up);
1437 pushLeft(nextAtom());
1441 plainInsert(MathAtom(new MathScriptInset(up)));
1442 prevAtom()->asScriptInset()->ensure(up);
1443 pushRight(prevAtom());
1453 bool MathCursor::interpret(char c)
1455 //lyxerr << "interpret 2: '" << c << "'\n";
1456 if (inMacroArgMode()) {
1460 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1461 if (p && 1 <= n && n <= p->numargs())
1462 insert(MathAtom(new MathMacroArgument(c - '0', lastcode_)));
1464 insert(MathAtom(new MathSpecialCharInset('#')));
1465 interpret(c); // try again
1471 if (inMacroMode()) {
1472 string name = macroName();
1473 //lyxerr << "interpret name: '" << name << "'\n";
1475 // extend macro name if possible
1477 insert(c, LM_TC_TEX);
1481 // leave macro mode if explicitly requested
1487 // handle 'special char' macros
1492 interpret("\\backslash");
1494 interpret(string("\\") + c);
1498 // leave macro mode and try again
1504 // leave autocorrect mode if necessary
1505 if (autocorrect_ && c == ' ') {
1506 autocorrect_ = false;
1510 // just clear selection on pressing the space par
1511 if (selection_ && c == ' ') {
1518 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1519 // suppress direct insertion of two spaces in a row
1520 // the still allows typing '<space>a<space>' and deleting the 'a', but
1521 // it is better than nothing...
1522 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1524 insert(c, LM_TC_TEXTRM);
1529 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1530 prevAtom()->asSpaceInset()->incSpace();
1535 // if are at the very end, leave the formula
1536 return pos() != size();
1540 insert(c, LM_TC_TEX);
1545 if (c == '{' || c == '}', c)) {
1546 insert(c, LM_TC_TEX);
1552 niceInsert(MathAtom(new MathBraceInset));
1560 if (c == '$' || c == '%') {
1561 insert(MathAtom(new MathSpecialCharInset(c)));
1562 lastcode_ = LM_TC_VAR;
1566 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1567 insert(c, LM_TC_VAR);
1571 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1572 insert(c, LM_TC_VAR);
1573 lastcode_ = LM_TC_VAR;
1578 insert(c, LM_TC_TEX);
1579 //bv->owner()->message(_("TeX mode"));
1583 // try auto-correction
1584 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1587 // no special circumstances, so insert the character without any fuss
1588 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1589 lastcode_ = LM_TC_MIN;
1590 autocorrect_ = true;
1596 MathCursorPos MathCursor::normalAnchor() const
1598 if (Anchor_.size() < Cursor_.size()) {
1600 lyxerr << "unusual Anchor size\n";
1603 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1604 // use Anchor on the same level as Cursor
1605 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1606 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1607 // anchor is behind cursor -> move anchor behind the inset
1614 void MathCursor::stripFromLastEqualSign()
1616 // find position of last '=' in the array
1617 MathArray & ar = cursor().cell();
1618 MathArray::const_iterator et = ar.end();
1619 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1620 if ((*it)->getChar() == '=')
1623 // delete everything behind this position
1624 ar.erase(et - ar.begin(), ar.size());
1629 void MathCursor::setSelection(cursor_type const & where, size_type n)
1638 string MathCursor::info() const
1642 prevAtom()->infoize(os);
1643 return os.str().c_str(); // .c_str() needed for lyxstring