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.
21 #pragma implementation
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
29 #include "math_cursor.h"
30 #include "formulabase.h"
31 #include "math_arrayinset.h"
32 #include "math_braceinset.h"
33 #include "math_boxinset.h"
34 #include "math_casesinset.h"
35 #include "math_charinset.h"
36 #include "math_deliminset.h"
37 #include "math_factory.h"
38 #include "math_hullinset.h"
39 #include "math_iterator.h"
40 #include "math_macroarg.h"
41 #include "math_macrotemplate.h"
42 #include "math_mathmlstream.h"
43 #include "math_parser.h"
44 #include "math_replace.h"
45 #include "math_scriptinset.h"
46 #include "math_spaceinset.h"
47 #include "math_specialcharinset.h"
48 #include "math_support.h"
61 using std::ostringstream;
67 typedef MathInset::col_type col_type;
68 typedef MathInset::row_type row_type;
69 typedef MathInset::idx_type idx_type;
75 void region(MathCursorPos const & i1, MathCursorPos const & i2,
76 row_type & r1, row_type & r2, col_type & c1, col_type & c2)
78 MathInset * p = i1.par_;
89 void grab(MathCursor const & cursor)
93 cursor.getSelection(i1, i2);
94 // shouldn'tt we assert on i1.par_ == i2.par_?
95 if (i1.idx_ == i2.idx_) {
96 data_ = MathGridInset(1, 1);
97 data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
101 region(i1, i2, r1, r2, c1, c2);
102 data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
103 for (row_type row = 0; row < data_.nrows(); ++row)
104 for (col_type col = 0; col < data_.ncols(); ++col) {
105 idx_type i = i1.par_->index(row + r1, col + c1);
106 data_.cell(data_.index(row, col)) = i1.par_->cell(i);
111 void erase(MathCursor & cursor)
115 cursor.getSelection(i1, i2);
116 if (i1.idx_ == i2.idx_)
117 i1.cell().erase(i1.pos_, i2.pos_);
119 MathInset * p = i1.par_;
122 region(i1, i2, r1, r2, c1, c2);
123 for (row_type row = r1; row <= r2; ++row)
124 for (col_type col = c1; col <= c2; ++col)
125 p->cell(p->index(row, col)).erase();
127 cursor.cursor() = i1;
130 void paste(MathCursor & cursor) const
132 if (data_.nargs() == 1) {
133 // single cell/part of cell
134 cursor.insert(data_.cell(0));
138 MathGridInset * p = cursor.enclosingGrid(idx);
139 col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
140 row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
141 for (row_type row = 0; row < numrows; ++row)
142 for (col_type col = 0; col < numcols; ++col) {
143 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
144 p->cell(i).push_back(data_.cell(data_.index(row, col)));
149 // glues selection to one cell
150 MathArray glue() const
153 for (unsigned i = 0; i < data_.nargs(); ++i)
154 ar.push_back(data_.cell(i));
160 data_ = MathGridInset(1, 1);
167 Selection theSelection;
174 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
175 : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
177 left ? first() : last();
181 void MathCursor::push(MathAtom & t)
183 Cursor_.push_back(MathCursorPos(t.nucleus()));
187 void MathCursor::pushLeft(MathAtom & t)
189 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
191 t->idxFirst(idx(), pos());
195 void MathCursor::pushRight(MathAtom & t)
197 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
200 t->idxLast(idx(), pos());
204 bool MathCursor::popLeft()
206 //cerr << "Leaving atom to the left\n";
207 if (Cursor_.size() <= 1)
214 bool MathCursor::popRight()
216 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
217 if (Cursor_.size() <= 1)
227 void MathCursor::dump(char const * what) const
229 lyxerr << "MC: " << what << "\n";
230 lyxerr << " Cursor: " << Cursor_.size() << "\n";
231 for (unsigned i = 0; i < Cursor_.size(); ++i)
232 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
233 lyxerr << " Anchor: " << Anchor_.size() << "\n";
234 for (unsigned i = 0; i < Anchor_.size(); ++i)
235 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
236 lyxerr << " sel: " << selection_ << "\n";
239 void MathCursor::dump(char const *) const {}
243 UpdatableInset * MathCursor::asHyperActiveInset() const
245 return par()->asHyperActiveInset();
249 bool MathCursor::isInside(MathInset const * p) const
251 for (unsigned i = 0; i < Cursor_.size(); ++i)
252 if (Cursor_[i].par_ == p)
258 bool MathCursor::openable(MathAtom const & t, bool sel) const
260 if (t->isHyperActive())
266 if (t->asScriptInset())
270 // we can't move into anything new during selection
271 if (Cursor_.size() == Anchor_.size())
273 if (t.nucleus() != Anchor_[Cursor_.size()].par_)
280 bool MathCursor::posLeft()
289 bool MathCursor::posRight()
298 bool MathCursor::left(bool sel)
303 lastcode_ = LM_TC_MIN;
307 lastcode_ = LM_TC_MIN;
309 if (hasPrevAtom() && openable(prevAtom(), sel)) {
310 if (prevAtom()->isHyperActive()) {
311 lyxerr << "entering hyperactive inset\n";
313 pushRight(prevAtom());
317 return posLeft() || idxLeft() || popLeft() || selection_;
321 bool MathCursor::right(bool sel)
326 lastcode_ = LM_TC_MIN;
330 lastcode_ = LM_TC_MIN;
332 if (hasNextAtom() && openable(nextAtom(), sel)) {
333 if (nextAtom()->isHyperActive()) {
334 lyxerr << "entering hyperactive inset\n";
337 nextAtom()->edit(formula()->view(), x, y, 0);
339 pushLeft(nextAtom());
343 return posRight() || idxRight() || popRight() || selection_;
347 void MathCursor::first()
350 pushLeft(formula_->par());
354 void MathCursor::last()
361 bool positionable(MathCursor::cursor_type const & cursor,
362 MathCursor::cursor_type const & anchor)
364 // avoid deeper nested insets when selecting
365 if (cursor.size() > anchor.size())
368 // anchor might be deeper, should have same path then
369 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
370 if (cursor[i].par_ != anchor[i].par_)
373 // position should be ok.
378 void MathCursor::setPos(int x, int y)
381 bool res = bruteFind(x, y,
382 formula()->xlow(), formula()->xhigh(),
383 formula()->ylow(), formula()->yhigh());
385 // this ccan happen on creation of "math-display"
394 void MathCursor::home(bool sel)
399 lastcode_ = LM_TC_MIN;
400 if (!par()->idxHome(idx(), pos()))
406 void MathCursor::end(bool sel)
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";
448 plainInsert(MathAtom(new MathCharInset(c, t)));
452 void MathCursor::insert(char c)
454 insert(c, lastcode_);
458 void MathCursor::insert(MathAtom const & t)
473 void MathCursor::niceInsert(MathAtom const & t)
476 insert(t); // inserting invalidates the pointer!
477 MathAtom const & p = prevAtom();
480 right(); // do not push for e.g. MathSymbolInset
486 void MathCursor::insert(MathArray const & ar)
492 array().insert(pos(), ar);
497 void MathCursor::paste(MathArray const & ar)
501 array().insert(pos(), ar);
506 void MathCursor::backspace()
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()
541 // delete empty cells if necessary
542 if (array().empty()) {
545 par()->idxDelete(idx(), popit, removeit);
546 if (popit && popLeft() && removeit)
554 MathScriptInset * p = nextAtom()->asScriptInset();
556 p->removeScript(p->hasUp());
557 // Don't delete if there is anything left
558 if (p->hasUp() || p->hasDown())
566 void MathCursor::delLine()
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_;
602 bool MathCursor::down(bool sel)
607 cursor_type save = Cursor_;
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 string s = macroName();
632 size_type old = pos();
634 array().erase(pos(), old);
640 MathInset::difference_type MathCursor::macroNamePos() const
642 for (MathInset::difference_type i = pos() - 1; i >= 0; --i) {
643 MathAtom & p = array().at(i);
644 if (p->code() == LM_TC_TEX && p->getChar() == '\\')
651 string MathCursor::macroName() const
654 MathInset::difference_type i = macroNamePos();
655 for (; i >= 0 && i < int(pos()); ++i)
656 s += array().at(i)->getChar();
661 void MathCursor::selCopy()
665 theSelection.grab(*this);
671 void MathCursor::selCut()
675 theSelection.grab(*this);
676 theSelection.erase(*this);
679 theSelection.clear();
684 void MathCursor::selDel()
688 theSelection.erase(*this);
696 void MathCursor::selPaste()
699 theSelection.paste(*this);
700 //theSelection.grab(*this);
705 void MathCursor::selHandle(bool sel)
707 if (sel == selection_)
709 //theSelection.clear();
715 void MathCursor::selStart()
718 //theSelection.clear();
725 void MathCursor::selClear()
733 void MathCursor::selGet(MathArray & ar)
739 theSelection.grab(*this);
740 ar = theSelection.glue();
745 void MathCursor::drawSelection(Painter & pain) const
752 getSelection(i1, i2);
754 if (i1.idx_ == i2.idx_) {
755 MathXArray & c = i1.xcell();
756 int x1 = c.xo() + c.pos2x(i1.pos_);
757 int y1 = c.yo() - c.ascent();
758 int x2 = c.xo() + c.pos2x(i2.pos_);
759 int y2 = c.yo() + c.descent();
760 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
762 vector<MathInset::idx_type> indices
763 = i1.par_->idxBetween(i1.idx_, i2.idx_);
764 for (unsigned i = 0; i < indices.size(); ++i) {
765 MathXArray & c = i1.xcell(indices[i]);
767 int y1 = c.yo() - c.ascent();
768 int x2 = c.xo() + c.width();
769 int y2 = c.yo() + c.descent();
770 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
775 // draw anchor if different from selection boundary
776 MathCursorPos anc = Anchor_.back();
777 if (anc != i1 && anc != i2) {
778 MathXArray & c = anc.xcell();
779 int x = c.xo() + c.pos2x(anc.pos_);
780 int y1 = c.yo() - c.ascent();
781 int y2 = c.yo() + c.descent();
782 pain.line(x, y1, x, y2, LColor::math);
788 void MathCursor::handleFont(MathTextCodes t)
794 getSelection(i1, i2);
795 if (i1.idx_ == i2.idx_) {
796 MathArray & ar = i1.cell();
797 for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
798 ar.at(pos)->handleFont(t);
801 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
805 void MathCursor::handleDelim(string const & l, string const & r)
807 handleNest(new MathDelimInset(l, r));
811 void MathCursor::handleNest(MathInset * p)
815 p->cell(0) = theSelection.glue();
817 insert(MathAtom(p)); // this invalidates p!
818 pushRight(prevAtom());
822 void MathCursor::getPos(int & x, int & y)
825 #warning This should probably take cellXOffset and cellYOffset into account
827 x = xarray().xo() + xarray().pos2x(pos());
832 MathInset * MathCursor::par() const
834 return cursor().par_;
838 InsetFormulaBase * MathCursor::formula()
844 MathCursor::idx_type MathCursor::idx() const
846 return cursor().idx_;
850 MathCursor::idx_type & MathCursor::idx()
852 return cursor().idx_;
856 MathCursor::pos_type MathCursor::pos() const
858 return cursor().pos_;
862 MathCursor::pos_type & MathCursor::pos()
864 return cursor().pos_;
868 bool MathCursor::inMacroMode() const
870 return macroNamePos() != -1;
874 bool MathCursor::inMacroArgMode() const
876 return pos() > 0 && prevAtom()->getChar() == '#';
880 bool MathCursor::selection() const
886 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
888 for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
889 MathGridInset * p = Cursor_[i].par_->asGridInset();
891 idx = Cursor_[i].idx_;
899 void MathCursor::pullArg(bool goright)
902 MathArray a = array();
904 MathScriptInset const * p = par()->asScriptInset();
906 // special handling for scripts
907 const bool up = p->hasUp();
909 MathScriptInset * q = nextAtom()->asScriptInset();
913 array().insert(pos(), a);
919 array().insert(pos(), a);
923 formula()->mutateToText();
928 void MathCursor::touch()
930 cursor_type::const_iterator it = Cursor_.begin();
931 cursor_type::const_iterator et = Cursor_.end();
932 for ( ; it != et; ++it)
937 void MathCursor::normalize()
942 MathIterator it = ibegin(formula()->par().nucleus());
943 MathIterator et = iend(formula()->par().nucleus());
944 for (; it != et; ++it)
945 if (it.par()->asBoxInset())
946 it.par()->asBoxInset()->rebreak();
950 if (idx() >= par()->nargs()) {
951 lyxerr << "this should not really happen - 1: "
952 << idx() << " " << par()->nargs() << "\n";
955 idx() = min(idx(), par()->nargs() - 1);
957 if (pos() > size()) {
958 lyxerr << "this should not really happen - 2: "
959 << pos() << " " << size() << " in idx: " << idx()
961 WriteStream wi(lyxerr, false);
966 pos() = min(pos(), size());
968 // remove empty scripts if possible
969 for (pos_type i = 0; i < size(); ++i) {
970 MathScriptInset * p = array().at(i)->asScriptInset();
972 p->removeEmptyScripts();
978 // fix again position
979 pos() = min(pos(), size());
983 MathCursor::size_type MathCursor::size() const
985 return array().size();
989 MathCursor::col_type MathCursor::hullCol() const
991 return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
995 MathCursor::row_type MathCursor::hullRow() const
997 return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
1001 bool MathCursor::hasPrevAtom() const
1007 bool MathCursor::hasNextAtom() const
1009 return pos() < size();
1013 MathAtom const & MathCursor::prevAtom() const
1015 lyx::Assert(pos() > 0);
1016 return array().at(pos() - 1);
1020 MathAtom & MathCursor::prevAtom()
1022 lyx::Assert(pos() > 0);
1023 return array().at(pos() - 1);
1027 MathAtom const & MathCursor::nextAtom() const
1029 lyx::Assert(pos() < size());
1030 return array().at(pos());
1034 MathAtom & MathCursor::nextAtom()
1036 lyx::Assert(pos() < size());
1037 return array().at(pos());
1041 MathArray & MathCursor::array() const
1043 static MathArray dummy;
1045 if (idx() >= par()->nargs()) {
1046 lyxerr << "############ idx_ " << idx() << " not valid\n";
1050 if (Cursor_.size() == 0) {
1051 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1055 return cursor().cell();
1059 MathXArray & MathCursor::xarray() const
1061 static MathXArray dummy;
1063 if (Cursor_.size() == 0) {
1064 lyxerr << "############ Cursor_.size() == 0 not valid\n";
1068 return cursor().xcell();
1072 void MathCursor::idxNext()
1074 par()->idxNext(idx(), pos());
1078 void MathCursor::idxPrev()
1080 par()->idxPrev(idx(), pos());
1084 void MathCursor::splitCell()
1086 if (idx() + 1 == par()->nargs())
1088 MathArray ar = array();
1090 array().erase(pos(), size());
1093 array().insert(0, ar);
1097 void MathCursor::breakLine()
1099 // leave inner cells
1103 MathHullInset * p = formula()->par()->asHullInset();
1107 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1108 p->mutate(LM_OT_EQNARRAY);
1112 p->addRow(hullRow());
1115 const row_type r = hullRow();
1116 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1117 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1121 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1126 //void MathCursor::readLine(MathArray & ar) const
1128 // idx_type base = row() * par()->ncols();
1129 // for (idx_type off = 0; off < par()->ncols(); ++off)
1130 // ar.push_back(par()->cell(base + off));
1134 char MathCursor::valign() const
1137 MathGridInset * p = enclosingGrid(idx);
1138 return p ? p->valign() : '\0';
1142 char MathCursor::halign() const
1145 MathGridInset * p = enclosingGrid(idx);
1146 return p ? p->halign(idx % p->ncols()) : '\0';
1150 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1152 MathCursorPos anc = normalAnchor();
1153 if (anc < cursor()) {
1163 MathCursorPos & MathCursor::cursor()
1165 lyx::Assert(Cursor_.size());
1166 return Cursor_.back();
1170 MathCursorPos const & MathCursor::cursor() const
1172 lyx::Assert(Cursor_.size());
1173 return Cursor_.back();
1177 bool MathCursor::goUpDown(bool up)
1179 int xlow, xhigh, ylow, yhigh;
1184 // try current cell first
1185 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1190 if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
1193 // try to find an inset that knows better then we
1195 // we found a cell that think something "below" us.
1197 if (par()->idxUp(idx()))
1200 if (par()->idxDown(idx()))
1205 // no such inset found, just take something "above"
1210 up ? formula()->ylow() : yo + 4,
1211 up ? yo - 4 : formula()->yhigh()
1215 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1216 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1221 bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1223 cursor_type best_cursor;
1224 double best_dist = 1e10;
1226 MathIterator it = ibegin(formula()->par().nucleus());
1227 MathIterator et = iend(formula()->par().nucleus());
1229 // avoid invalid nesting when selecting
1230 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1231 MathCursorPos const & top = it.position();
1232 int xo = top.xpos();
1233 int yo = top.ypos();
1234 if (xlow - 2 <= xo && xo <= xhigh + 2 &&
1235 ylow - 2 <= yo && yo <= yhigh + 2)
1237 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1238 if (d < best_dist) {
1240 best_cursor = it.cursor();
1250 if (best_dist < 1e10)
1251 Cursor_ = best_cursor;
1252 return best_dist < 1e10;
1256 bool MathCursor::idxLeft()
1258 return par()->idxLeft(idx(), pos());
1262 bool MathCursor::idxRight()
1264 return par()->idxRight(idx(), pos());
1268 bool MathCursor::interpret(string const & s)
1270 //lyxerr << "interpret 1: '" << s << "'\n";
1275 return interpret(s[0]);
1277 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1278 //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
1279 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1281 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1283 istringstream is(s.substr(5).c_str());
1286 niceInsert(MathAtom(new MathCasesInset(n)));
1290 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1295 istringstream is(s.substr(6).c_str());
1296 is >> m >> n >> v_align >> h_align;
1300 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1304 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1306 istringstream is(s.substr(7).c_str());
1309 mathed_parse_cell(rep.from, from);
1310 mathed_parse_cell(rep.to, to);
1311 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1312 par()->replace(rep);
1316 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1317 MathArray ar = array();
1318 MathAtom t(createMathInset(s.substr(1)));
1319 t->asNestInset()->cell(0).swap(array());
1327 latexkeys const * l = in_word_set(s.substr(1));
1328 if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
1329 lastcode_ = static_cast<MathTextCodes>(l->id);
1333 // prevent entering of recursive macros
1334 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1335 && formula()->getInsetName() == s.substr(1))
1337 lyxerr << "can't enter recursive macro\n";
1341 niceInsert(createMathInset(s.substr(1)));
1346 bool MathCursor::script(bool up)
1350 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1351 prevAtom()->asScriptInset()->ensure(up);
1352 pushRight(prevAtom());
1355 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1356 nextAtom()->asScriptInset()->ensure(up);
1357 pushLeft(nextAtom());
1361 plainInsert(MathAtom(new MathScriptInset(up)));
1362 prevAtom()->asScriptInset()->ensure(up);
1363 pushRight(prevAtom());
1373 bool MathCursor::interpret(char c)
1375 if (inMacroArgMode()) {
1379 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1380 if (p && 1 <= n && n <= p->numargs())
1381 insert(MathAtom(new MathMacroArgument(c - '0')));
1383 insert(MathAtom(new MathSpecialCharInset('#')));
1384 interpret(c); // try again
1390 if (inMacroMode()) {
1391 string name = macroName();
1393 if (name == "\\" && c == '\\') {
1395 interpret("\\backslash");
1400 insert(c, LM_TC_TEX);
1405 insert(c, LM_TC_TEX);
1413 insert(c, LM_TC_TEX);
1415 insert(c, lastcode_);
1424 // fall through in the other cases
1427 if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
1428 // suppress direct insertion of two spaces in a row
1429 // the still allows typing '<space>a<space>' and deleting the 'a', but
1430 // it is better than nothing...
1431 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1433 insert(c, LM_TC_TEXTRM);
1438 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1439 prevAtom()->asSpaceInset()->incSpace();
1444 // if are at the very end, leave the formula
1445 return pos() != size();
1449 insert(c, LM_TC_TEX);
1454 if (strchr("{}", c)) {
1455 insert(c, LM_TC_TEX);
1461 niceInsert(MathAtom(new MathBraceInset));
1469 if (strchr("$%", c)) {
1470 insert(MathAtom(new MathSpecialCharInset(c)));
1471 lastcode_ = LM_TC_VAR;
1475 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1476 insert(c, LM_TC_VAR);
1480 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1481 insert(c, LM_TC_VAR);
1482 lastcode_ = LM_TC_VAR;
1487 insert(c, LM_TC_TEX);
1488 //bv->owner()->message(_("TeX mode"));
1492 // no special circumstances, so insert the character without any fuss
1493 insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
1494 lastcode_ = LM_TC_MIN;
1500 MathCursorPos MathCursor::normalAnchor() const
1502 if (Anchor_.size() < Cursor_.size()) {
1504 lyxerr << "unusual Anchor size\n";
1507 //lyx::Assert(Anchor_.size() >= Cursor_.size());
1508 // use Anchor on the same level as Cursor
1509 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1510 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1511 // anchor is behind cursor -> move anchor behind the inset
1518 void MathCursor::stripFromLastEqualSign()
1520 // find position of last '=' in the array
1521 MathArray & ar = cursor().cell();
1522 MathArray::const_iterator et = ar.end();
1523 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
1524 if ((*it)->getChar() == '=')
1527 // delete everything behind this position
1528 ar.erase(et - ar.begin(), ar.size());
1533 void MathCursor::setSelection(cursor_type const & where, size_type n)
1542 string MathCursor::info() const
1546 prevAtom()->infoize(os);