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_extern.h"
40 #include "math_factory.h"
41 #include "math_fboxinset.h"
42 #include "math_hullinset.h"
43 #include "math_iterator.h"
44 #include "math_macroarg.h"
45 #include "math_macrotemplate.h"
46 #include "math_mathmlstream.h"
47 #include "math_parser.h"
48 #include "math_replace.h"
49 #include "math_scriptinset.h"
50 #include "math_spaceinset.h"
51 #include "math_support.h"
52 #include "math_unknowninset.h"
64 using std::ostringstream;
68 // matheds own cut buffer
69 MathGridInset theCutBuffer = MathGridInset(1, 1);
72 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
73 : formula_(formula), autocorrect_(false), selection_(false)
75 front ? first() : last();
80 MathCursor::~MathCursor()
82 // ensure that 'notifyCursorLeave' is called
88 void MathCursor::push(MathAtom & t)
90 Cursor_.push_back(MathCursorPos(t.nucleus()));
94 void MathCursor::pushLeft(MathAtom & t)
96 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
98 t->idxFirst(idx(), pos());
102 void MathCursor::pushRight(MathAtom & t)
104 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
107 t->idxLast(idx(), pos());
111 bool MathCursor::popLeft()
113 //cerr << "Leaving atom to the left\n";
116 par()->notifyCursorLeaves();
119 par()->notifyCursorLeaves();
125 bool MathCursor::popRight()
127 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
130 par()->notifyCursorLeaves();
133 par()->notifyCursorLeaves();
142 void MathCursor::dump(char const * what) const
144 lyxerr << "MC: " << what << "\n";
145 lyxerr << " Cursor: " << depth() << "\n";
146 for (unsigned i = 0; i < depth(); ++i)
147 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
148 lyxerr << " Anchor: " << Anchor_.size() << "\n";
149 for (unsigned i = 0; i < Anchor_.size(); ++i)
150 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
151 lyxerr << " sel: " << selection_ << "\n";
154 void MathCursor::dump(char const *) const {}
158 bool MathCursor::isInside(MathInset const * p) const
160 for (unsigned i = 0; i < depth(); ++i)
161 if (Cursor_[i].par_ == p)
167 bool MathCursor::openable(MathAtom const & t, bool sel) const
175 if (t->asScriptInset())
179 // we can't move into anything new during selection
180 if (depth() == Anchor_.size())
182 if (t.nucleus() != Anchor_[depth()].par_)
189 bool MathCursor::posLeft()
198 bool MathCursor::posRight()
207 bool MathCursor::left(bool sel)
210 autocorrect_ = false;
211 targetx_ = -1; // "no target"
218 if (hasPrevAtom() && openable(prevAtom(), sel)) {
219 pushRight(prevAtom());
223 return posLeft() || idxLeft() || popLeft() || selection_;
227 bool MathCursor::right(bool sel)
230 autocorrect_ = false;
231 targetx_ = -1; // "no target"
238 if (hasNextAtom() && openable(nextAtom(), sel)) {
239 pushLeft(nextAtom());
243 return posRight() || idxRight() || popRight() || selection_;
247 void MathCursor::first()
250 push(formula_->par());
251 par()->idxFirst(idx(), pos());
255 void MathCursor::last()
258 push(formula_->par());
259 par()->idxLast(idx(), pos());
263 bool positionable(MathCursor::cursor_type const & cursor,
264 MathCursor::cursor_type const & anchor)
266 // avoid deeper nested insets when selecting
267 if (cursor.size() > anchor.size())
270 // anchor might be deeper, should have same path then
271 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
272 if (cursor[i].par_ != anchor[i].par_)
275 // position should be ok.
280 void MathCursor::setPos(int x, int y)
283 bool res = bruteFind(x, y,
284 formula()->xlow(), formula()->xhigh(),
285 formula()->ylow(), formula()->yhigh());
287 // this ccan happen on creation of "math-display"
296 void MathCursor::home(bool sel)
299 autocorrect_ = false;
302 if (!par()->idxHome(idx(), pos()))
308 void MathCursor::end(bool sel)
311 autocorrect_ = false;
314 if (!par()->idxEnd(idx(), pos()))
320 void MathCursor::plainErase()
322 array().erase(pos());
326 void MathCursor::markInsert()
328 //lyxerr << "inserting mark\n";
329 array().insert(pos(), MathAtom(new MathCharInset(0)));
333 void MathCursor::markErase()
335 //lyxerr << "deleting mark\n";
336 array().erase(pos());
340 void MathCursor::plainInsert(MathAtom const & t)
342 array().insert(pos(), t);
347 void MathCursor::insert(string const & str)
349 //lyxerr << "inserting '" << str << "'\n";
351 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
352 plainInsert(MathAtom(new MathCharInset(*it)));
356 void MathCursor::insert(char c)
358 //lyxerr << "inserting '" << c << "'\n";
360 plainInsert(MathAtom(new MathCharInset(c)));
364 void MathCursor::insert(MathAtom const & t)
372 void MathCursor::niceInsert(MathAtom const & t)
375 MathGridInset safe = grabAndEraseSelection();
377 // enter the new inset and move the contents of the selection if possible
380 pushLeft(nextAtom());
386 void MathCursor::insert(MathArray const & ar)
392 array().insert(pos(), ar);
397 void MathCursor::paste(MathArray const & ar)
401 array().insert(pos(), ar);
406 void MathCursor::paste(MathGridInset const & data)
408 if (data.nargs() == 1) {
409 // single cell/part of cell
413 idx_type idx; // index of upper left cell
414 MathGridInset * p = enclosingGrid(idx);
415 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
416 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
417 for (row_type row = 0; row < numrows; ++row) {
418 for (col_type col = 0; col < numcols; ++col) {
419 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
420 p->cell(i).push_back(data.cell(data.index(row, col)));
422 // append the left over horizontal cells to the last column
423 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
424 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
425 p->cell(i).push_back(data.cell(data.index(row, col)));
427 // append the left over vertical cells to the last _cell_
428 idx_type i = p->nargs() - 1;
429 for (row_type row = numrows; row < data.nrows(); ++row)
430 for (col_type col = 0; col < data.ncols(); ++col)
431 p->cell(i).push_back(data.cell(data.index(row, col)));
435 void MathCursor::backspace()
437 autocorrect_ = false;
448 MathScriptInset * p = prevAtom()->asScriptInset();
450 p->removeScript(p->hasUp());
451 // Don't delete if there is anything left
452 if (p->hasUp() || p->hasDown())
461 void MathCursor::erase()
463 autocorrect_ = false;
472 // delete empty cells if possible
474 if (par()->idxDelete(idx()))
477 // old behaviour when in last position of cell
478 if (pos() == size()) {
479 par()->idxGlue(idx());
483 MathScriptInset * p = nextAtom()->asScriptInset();
485 p->removeScript(p->hasUp());
486 // Don't delete if there is anything left
487 if (p->hasUp() || p->hasDown())
495 void MathCursor::delLine()
497 autocorrect_ = false;
505 if (par()->nrows() > 1) {
506 // grid are the only things with more than one row...
507 lyx::Assert(par()->asGridInset());
508 par()->asGridInset()->delRow(hullRow());
511 if (idx() >= par()->nargs())
512 idx() = par()->nargs() - 1;
519 bool MathCursor::up(bool sel)
524 cursor_type save = Cursor_;
528 autocorrect_ = false;
533 bool MathCursor::down(bool sel)
538 cursor_type save = Cursor_;
542 autocorrect_ = false;
547 bool MathCursor::toggleLimits()
551 MathScriptInset * t = nextAtom()->asScriptInset();
554 int old = t->limits();
555 t->limits(old < 0 ? 1 : -1);
556 return old != t->limits();
560 void MathCursor::macroModeClose()
562 MathUnknownInset * p = inMacroMode();
566 string s = p->name();
568 array().erase(pos());
574 string MathCursor::macroName() const
576 return inMacroMode() ? inMacroMode()->name() : string();
580 void MathCursor::selClear()
586 void MathCursor::selCopy()
590 theCutBuffer = grabSelection();
593 theCutBuffer = MathGridInset(1, 1);
598 void MathCursor::selCut()
601 theCutBuffer = grabAndEraseSelection();
605 void MathCursor::selDel()
615 void MathCursor::selPaste()
625 void MathCursor::selHandle(bool sel)
627 if (sel == selection_)
635 void MathCursor::selStart()
645 void MathCursor::selClearOrDel()
647 if (lyxrc.auto_region_delete)
654 void MathCursor::selGet(MathArray & ar)
658 ar = grabSelection().glue();
663 void MathCursor::drawSelection(MathPainterInfo & pain) const
670 getSelection(i1, i2);
672 if (i1.idx_ == i2.idx_) {
673 MathXArray & c = i1.xcell();
674 int x1 = c.xo() + c.pos2x(i1.pos_);
675 int y1 = c.yo() - c.ascent();
676 int x2 = c.xo() + c.pos2x(i2.pos_);
677 int y2 = c.yo() + c.descent();
678 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
680 vector<MathInset::idx_type> indices
681 = i1.par_->idxBetween(i1.idx_, i2.idx_);
682 for (unsigned i = 0; i < indices.size(); ++i) {
683 MathXArray & c = i1.xcell(indices[i]);
685 int y1 = c.yo() - c.ascent();
686 int x2 = c.xo() + c.width();
687 int y2 = c.yo() + c.descent();
688 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
693 // draw anchor if different from selection boundary
694 MathCursorPos anc = Anchor_.back();
695 if (anc != i1 && anc != i2) {
696 MathXArray & c = anc.xcell();
697 int x = c.xo() + c.pos2x(anc.pos_);
698 int y1 = c.yo() - c.ascent();
699 int y2 = c.yo() + c.descent();
700 pain.line(x, y1, x, y2, LColor::math);
706 void MathCursor::handleDelim(string const & l, string const & r)
708 handleNest(new MathDelimInset(l, r));
712 void MathCursor::handleNest(MathInset * p)
714 p->cell(0) = grabAndEraseSelection().glue();
715 insert(MathAtom(p)); // this invalidates p!
716 pushRight(prevAtom());
720 void MathCursor::getPos(int & x, int & y)
722 par()->getPos(idx(), pos(), x, y);
726 MathInset * MathCursor::par() const
728 return cursor().par_;
732 InsetFormulaBase * MathCursor::formula() const
738 MathCursor::idx_type MathCursor::idx() const
740 return cursor().idx_;
744 MathCursor::idx_type & MathCursor::idx()
746 return cursor().idx_;
750 MathCursor::pos_type MathCursor::pos() const
752 return cursor().pos_;
756 MathCursor::pos_type & MathCursor::pos()
758 return cursor().pos_;
762 MathUnknownInset * MathCursor::inMacroMode() const
766 MathUnknownInset * p = prevAtom()->asUnknownInset();
767 return (p && !p->final()) ? p : 0;
771 bool MathCursor::inMacroArgMode() const
773 return pos() > 0 && prevAtom()->getChar() == '#';
777 bool MathCursor::selection() const
783 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
785 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
786 MathGridInset * p = Cursor_[i].par_->asGridInset();
788 idx = Cursor_[i].idx_;
796 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
798 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
799 MathHullInset * p = Cursor_[i].par_->asHullInset();
801 idx = Cursor_[i].idx_;
809 void MathCursor::popToEnclosingGrid()
811 while (depth() && !Cursor_.back().par_->asGridInset())
816 void MathCursor::popToEnclosingHull()
818 while (depth() && !Cursor_.back().par_->asHullInset())
823 void MathCursor::pullArg(bool goright)
826 MathArray a = array();
828 MathScriptInset const * p = par()->asScriptInset();
830 // special handling for scripts
831 const bool up = p->hasUp();
833 MathScriptInset * q = nextAtom()->asScriptInset();
837 array().insert(pos(), a);
843 array().insert(pos(), a);
847 formula()->mutateToText();
852 void MathCursor::touch()
854 cursor_type::const_iterator it = Cursor_.begin();
855 cursor_type::const_iterator et = Cursor_.end();
856 for ( ; it != et; ++it)
861 void MathCursor::normalize()
866 MathIterator it = ibegin(formula()->par().nucleus());
867 MathIterator et = iend(formula()->par().nucleus());
868 for (; it != et; ++it)
869 if (it.par()->asBoxInset())
870 it.par()->asBoxInset()->rebreak();
874 if (idx() >= par()->nargs()) {
875 lyxerr << "this should not really happen - 1: "
876 << idx() << " " << par()->nargs() << "\n";
879 idx() = min(idx(), par()->nargs() - 1);
881 if (pos() > size()) {
882 lyxerr << "this should not really happen - 2: "
883 << pos() << " " << size() << " in idx: " << idx()
885 WriteStream wi(lyxerr, false, true);
890 pos() = min(pos(), size());
892 // remove empty scripts if possible
894 for (pos_type i = 0; i < size(); ++i) {
895 MathScriptInset * p = array()[i]->asScriptInset();
897 p->removeEmptyScripts();
904 // fix again position
905 pos() = min(pos(), size());
909 MathCursor::size_type MathCursor::size() const
911 return array().size();
915 MathCursor::col_type MathCursor::hullCol() const
918 MathHullInset * p = enclosingHull(idx);
923 MathCursor::row_type MathCursor::hullRow() const
926 MathHullInset * p = enclosingHull(idx);
931 bool MathCursor::hasPrevAtom() const
937 bool MathCursor::hasNextAtom() const
939 return pos() < size();
943 MathAtom const & MathCursor::prevAtom() const
945 lyx::Assert(pos() > 0);
946 return array()[pos() - 1];
950 MathAtom & MathCursor::prevAtom()
952 lyx::Assert(pos() > 0);
953 return array()[pos() - 1];
957 MathAtom const & MathCursor::nextAtom() const
959 lyx::Assert(pos() < size());
960 return array()[pos()];
964 MathAtom & MathCursor::nextAtom()
966 lyx::Assert(pos() < size());
967 return array()[pos()];
971 MathArray & MathCursor::array() const
973 static MathArray dummy;
975 if (idx() >= par()->nargs()) {
976 lyxerr << "############ idx_ " << idx() << " not valid\n";
981 lyxerr << "############ depth() == 0 not valid\n";
985 return cursor().cell();
989 MathXArray & MathCursor::xarray() const
991 static MathXArray dummy;
994 lyxerr << "############ depth() == 0 not valid\n";
998 return cursor().xcell();
1002 void MathCursor::idxNext()
1004 par()->idxNext(idx(), pos());
1008 void MathCursor::idxPrev()
1010 par()->idxPrev(idx(), pos());
1014 void MathCursor::splitCell()
1016 if (idx() + 1 == par()->nargs())
1018 MathArray ar = array();
1020 array().erase(pos(), size());
1023 array().insert(0, ar);
1027 void MathCursor::breakLine()
1029 // leave inner cells
1034 MathHullInset * p = enclosingHull(dummy);
1038 if (p->getType() == "simple" || p->getType() == "equation") {
1039 p->mutate("eqnarray");
1043 p->addRow(hullRow());
1046 const row_type r = hullRow();
1047 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1048 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1052 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1057 //void MathCursor::readLine(MathArray & ar) const
1059 // idx_type base = row() * par()->ncols();
1060 // for (idx_type off = 0; off < par()->ncols(); ++off)
1061 // ar.push_back(par()->cell(base + off));
1065 char MathCursor::valign() const
1068 MathGridInset * p = enclosingGrid(idx);
1069 return p ? p->valign() : '\0';
1073 char MathCursor::halign() const
1076 MathGridInset * p = enclosingGrid(idx);
1077 return p ? p->halign(idx % p->ncols()) : '\0';
1081 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1083 MathCursorPos anc = normalAnchor();
1084 if (anc < cursor()) {
1094 MathCursorPos & MathCursor::cursor()
1096 lyx::Assert(depth());
1097 return Cursor_.back();
1101 MathCursorPos const & MathCursor::cursor() const
1103 lyx::Assert(depth());
1104 return Cursor_.back();
1108 bool MathCursor::goUpDown(bool up)
1110 // Be warned: The 'logic' implemented in this function is highly fragile.
1111 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1112 // So fiddle around with it only if you know what you are doing!
1113 int xlow, xhigh, ylow, yhigh;
1118 // check if we had something else in mind, if not, this is the future goal
1124 // try neigbouring script insets
1126 if (hasPrevAtom()) {
1127 MathScriptInset * p = prevAtom()->asScriptInset();
1128 if (p && p->has(up)) {
1131 idx() = up; // the superscript has index 1
1133 ///lyxerr << "updown: handled by scriptinset to the left\n";
1139 if (hasNextAtom()) {
1140 MathScriptInset * p = nextAtom()->asScriptInset();
1141 if (p && p->has(up)) {
1145 ///lyxerr << "updown: handled by scriptinset to the right\n";
1151 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1156 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1157 // lyxerr << "updown: handled by brute find in the same cell\n";
1161 // try to find an inset that knows better then we
1163 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1165 if (par()->idxUpDown(idx(), up)) {
1166 // we found a cell that thinks it has something "below" us.
1167 ///lyxerr << "updown: found inset that handles UpDown\n";
1168 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1169 // project (xo,yo) onto proper box
1170 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1171 /// << "\n xlow: " << xlow << " ylow: " << ylow
1172 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1173 xo = min(max(xo, xlow), xhigh);
1174 yo = min(max(yo, ylow), yhigh);
1175 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1176 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1177 ///lyxerr << "updown: handled by final brute find\n";
1183 // no such inset found, just take something "above"
1184 ///lyxerr << "updown: handled by strange case\n";
1189 up ? formula()->ylow() : yo + 4,
1190 up ? yo - 4 : formula()->yhigh()
1194 // any improvement so far?
1197 if (up ? ynew < yo : ynew > yo)
1203 bool MathCursor::bruteFind
1204 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1206 cursor_type best_cursor;
1207 double best_dist = 1e10;
1209 MathIterator it = ibegin(formula()->par().nucleus());
1210 MathIterator et = iend(formula()->par().nucleus());
1212 // avoid invalid nesting when selecting
1213 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1214 MathCursorPos const & top = it.position();
1215 int xo = top.xpos();
1216 int yo = top.ypos();
1217 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1218 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1219 // '<=' in order to take the last possible position
1220 // this is important for clicking behind \sum in e.g. '\sum_i a'
1221 if (d <= best_dist) {
1223 best_cursor = it.cursor();
1233 if (best_dist < 1e10)
1234 Cursor_ = best_cursor;
1235 return best_dist < 1e10;
1239 bool MathCursor::idxLineFirst()
1241 idx() -= idx() % par()->ncols();
1247 bool MathCursor::idxLineLast()
1249 idx() -= idx() % par()->ncols();
1250 idx() += par()->ncols() - 1;
1255 bool MathCursor::idxLeft()
1257 return par()->idxLeft(idx(), pos());
1261 bool MathCursor::idxRight()
1263 return par()->idxRight(idx(), pos());
1267 bool MathCursor::interpret(string const & s)
1269 //lyxerr << "interpret 1: '" << s << "'\n";
1273 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1274 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1275 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1277 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1279 istringstream is(s.substr(5).c_str());
1282 niceInsert(MathAtom(new MathCasesInset(n)));
1286 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1291 istringstream is(s.substr(6).c_str());
1292 is >> m >> n >> v_align >> h_align;
1296 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1300 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1302 istringstream is(s.substr(7).c_str());
1305 mathed_parse_cell(rep.from, from);
1306 mathed_parse_cell(rep.to, to);
1307 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1308 par()->replace(rep);
1312 string name = s.substr(1);
1314 if (name == "over" || name == "choose" || name == "atop") {
1315 MathArray ar = array();
1316 MathAtom t(createMathInset(name));
1317 t->asNestInset()->cell(0).swap(array());
1325 // prevent entering of recursive macros
1326 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1327 && formula()->getInsetName() == name)
1329 lyxerr << "can't enter recursive macro\n";
1333 niceInsert(createMathInset(name));
1338 bool MathCursor::script(bool up)
1340 // Hack to get \\^ and \\_ working
1341 if (inMacroMode() && macroName() == "\\") {
1343 interpret("\\mathcircumflex");
1350 MathGridInset safe = grabAndEraseSelection();
1351 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1352 prevAtom()->asScriptInset()->ensure(up);
1353 pushRight(prevAtom());
1356 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1357 nextAtom()->asScriptInset()->ensure(up);
1358 pushLeft(nextAtom());
1362 plainInsert(MathAtom(new MathScriptInset(up)));
1363 prevAtom()->asScriptInset()->ensure(up);
1364 pushRight(prevAtom());
1374 bool MathCursor::inMathMode() const
1376 if (par()->asBoxInset())
1378 if (par()->asFboxInset())
1380 if (par()->asParboxInset())
1382 if (par()->asParInset())
1388 bool MathCursor::interpret(char c)
1390 //lyxerr << "interpret 2: '" << c << "'\n";
1391 targetx_ = -1; // "no target"
1392 if (inMacroArgMode()) {
1396 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1397 if (p && 1 <= n && n <= p->numargs())
1398 insert(MathAtom(new MathMacroArgument(c - '0')));
1400 insert(createMathInset("#"));
1401 interpret(c); // try again
1407 if (inMacroMode()) {
1408 string name = macroName();
1409 //lyxerr << "interpret name: '" << name << "'\n";
1411 if (name.empty() && c == '\\') {
1413 interpret("\\backslash");
1418 inMacroMode()->name() += c;
1422 // handle 'special char' macros
1427 interpret("\\backslash");
1429 interpret(string("\\") + c);
1433 // leave macro mode and try again if necessary
1440 // This is annoying as one has to press <space> far too often.
1444 // leave autocorrect mode if necessary
1445 if (autocorrect_ && c == ' ') {
1446 autocorrect_ = false;
1451 // just clear selection on pressing the space bar
1452 if (selection_ && c == ' ') {
1459 if (!inMathMode()) {
1460 // suppress direct insertion of two spaces in a row
1461 // the still allows typing '<space>a<space>' and deleting the 'a', but
1462 // it is better than nothing...
1463 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1470 lyxerr << "starting with macro\n";
1471 insert(MathAtom(new MathUnknownInset("\\", false)));
1476 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1477 prevAtom()->asSpaceInset()->incSpace();
1482 // if are at the very end, leave the formula
1483 return pos() != size();
1487 insert(c); // LM_TC_TEX;
1492 if (c == '{' || c == '}', c)) {
1493 insert(c); // LM_TC_TEX;
1499 niceInsert(MathAtom(new MathBraceInset));
1508 insert(createMathInset("$"));
1513 insert(createMathInset("%"));
1518 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1519 insert(c, LM_TC_VAR);
1523 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1524 insert(c, LM_TC_VAR);
1525 lastcode_ = LM_TC_VAR;
1530 insert(c, LM_TC_TEX);
1531 //bv->owner()->message(_("TeX mode"));
1536 // try auto-correction
1537 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1540 // no special circumstances, so insert the character without any fuss
1542 autocorrect_ = true;
1547 void MathCursor::setSelection(cursor_type const & where, size_type n)
1556 void MathCursor::insetToggle()
1558 if (hasNextAtom()) {
1559 // toggle next inset ...
1560 nextAtom()->lock(!nextAtom()->lock());
1561 } else if (popLeft() && hasNextAtom()) {
1562 // ... or enclosing inset if we are in the last inset position
1563 nextAtom()->lock(!nextAtom()->lock());
1569 string MathCursor::info() const
1572 os << "Math editor mode. ";
1573 for (int i = 0, n = depth(); i < n; ++i) {
1574 Cursor_[i].par_->infoize(os);
1578 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1579 prevAtom()->infoize(os);
1581 return os.str().c_str(); // .c_str() needed for lyxstring
1585 unsigned MathCursor::depth() const
1587 return Cursor_.size();
1595 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1596 MathInset::row_type & r1, MathInset::row_type & r2,
1597 MathInset::col_type & c1, MathInset::col_type & c2)
1599 MathInset * p = i1.par_;
1600 c1 = p->col(i1.idx_);
1601 c2 = p->col(i2.idx_);
1604 r1 = p->row(i1.idx_);
1605 r2 = p->row(i2.idx_);
1613 MathGridInset MathCursor::grabSelection() const
1616 return MathGridInset();
1619 getSelection(i1, i2);
1620 // shouldn't we assert on i1.par_ == i2.par_?
1621 if (i1.idx_ == i2.idx_) {
1622 MathGridInset data(1, 1);
1623 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1628 region(i1, i2, r1, r2, c1, c2);
1629 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1630 for (row_type row = 0; row < data.nrows(); ++row)
1631 for (col_type col = 0; col < data.ncols(); ++col) {
1632 idx_type i = i1.par_->index(row + r1, col + c1);
1633 data.cell(data.index(row, col)) = i1.par_->cell(i);
1639 void MathCursor::eraseSelection()
1643 getSelection(i1, i2);
1644 if (i1.idx_ == i2.idx_)
1645 i1.cell().erase(i1.pos_, i2.pos_);
1647 MathInset * p = i1.par_;
1650 region(i1, i2, r1, r2, c1, c2);
1651 for (row_type row = r1; row <= r2; ++row)
1652 for (col_type col = c1; col <= c2; ++col)
1653 p->cell(p->index(row, col)).erase();
1659 MathGridInset MathCursor::grabAndEraseSelection()
1662 return MathGridInset();
1663 MathGridInset res = grabSelection();
1670 MathCursorPos MathCursor::normalAnchor() const
1672 if (Anchor_.size() < depth()) {
1674 lyxerr << "unusual Anchor size\n";
1676 //lyx::Assert(Anchor_.size() >= cursor.depth());
1677 // use Anchor on the same level as Cursor
1678 MathCursorPos normal = Anchor_[depth() - 1];
1679 if (depth() < Anchor_.size() && !(normal < cursor())) {
1680 // anchor is behind cursor -> move anchor behind the inset
1688 void MathCursor::handleExtern(const string & arg)
1692 istringstream iss(arg.c_str());
1693 iss >> lang >> extra;
1701 lyxerr << "use selection: " << ar << "\n";
1702 insert(pipeThroughExtern(lang, extra, ar));
1707 eq.push_back(MathAtom(new MathCharInset('=')));
1709 popToEnclosingHull();
1712 MathHullInset * hull = enclosingHull(idx);
1716 if (hull->getType() == "simple") {
1717 MathArray::size_type pos = cursor().cell().find_last(eq);
1719 if (pos == size()) {
1721 lyxerr << "use whole cell: " << ar << "\n";
1723 ar = MathArray(array(), pos + 1, size());
1724 lyxerr << "use partial cell form pos: " << pos << "\n";
1728 insert(pipeThroughExtern(lang, extra, ar));
1732 if (hull->getType() == "equation") {
1733 lyxerr << "use equation inset\n";
1734 hull->mutate("eqnarray");
1735 MathArray & ar = cursor().cell();
1736 lyxerr << "use cell: " << ar << "\n";
1738 cursor().cell() = eq;
1740 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1746 lyxerr << "use eqnarray\n";
1748 MathArray ar = cursor().cell();
1749 lyxerr << "use cell: " << ar << "\n";
1752 cursor().cell() = eq;
1754 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1761 int MathCursor::dispatch(string const & cmd)
1763 // try to dispatch to adajcent items if they are not editable
1764 // actually, this should only happen for mouse clicks...
1765 if (hasNextAtom() && !openable(nextAtom(), false))
1766 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1768 if (hasPrevAtom() && !openable(prevAtom(), false))
1769 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1772 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1773 MathCursorPos & pos = Cursor_[i];
1774 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))