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 void MathCursor::push(MathAtom & t)
82 Cursor_.push_back(MathCursorPos(t.nucleus()));
86 void MathCursor::pushLeft(MathAtom & t)
88 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
90 t->idxFirst(idx(), pos());
94 void MathCursor::pushRight(MathAtom & t)
96 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
99 t->idxLast(idx(), pos());
103 bool MathCursor::popLeft()
105 //cerr << "Leaving atom to the left\n";
113 bool MathCursor::popRight()
115 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
126 void MathCursor::dump(char const * what) const
128 lyxerr << "MC: " << what << "\n";
129 lyxerr << " Cursor: " << depth() << "\n";
130 for (unsigned i = 0; i < depth(); ++i)
131 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
132 lyxerr << " Anchor: " << Anchor_.size() << "\n";
133 for (unsigned i = 0; i < Anchor_.size(); ++i)
134 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
135 lyxerr << " sel: " << selection_ << "\n";
138 void MathCursor::dump(char const *) const {}
142 bool MathCursor::isInside(MathInset const * p) const
144 for (unsigned i = 0; i < depth(); ++i)
145 if (Cursor_[i].par_ == p)
151 bool MathCursor::openable(MathAtom const & t, bool sel) const
159 if (t->asScriptInset())
163 // we can't move into anything new during selection
164 if (depth() == Anchor_.size())
166 if (t.nucleus() != Anchor_[depth()].par_)
173 bool MathCursor::posLeft()
182 bool MathCursor::posRight()
191 bool MathCursor::left(bool sel)
194 autocorrect_ = false;
195 targetx_ = -1; // "no target"
202 if (hasPrevAtom() && openable(prevAtom(), sel)) {
203 pushRight(prevAtom());
207 return posLeft() || idxLeft() || popLeft() || selection_;
211 bool MathCursor::right(bool sel)
214 autocorrect_ = false;
215 targetx_ = -1; // "no target"
222 if (hasNextAtom() && openable(nextAtom(), sel)) {
223 pushLeft(nextAtom());
227 return posRight() || idxRight() || popRight() || selection_;
231 void MathCursor::first()
234 push(formula_->par());
235 par()->idxFirst(idx(), pos());
239 void MathCursor::last()
242 push(formula_->par());
243 par()->idxLast(idx(), pos());
247 bool positionable(MathCursor::cursor_type const & cursor,
248 MathCursor::cursor_type const & anchor)
250 // avoid deeper nested insets when selecting
251 if (cursor.size() > anchor.size())
254 // anchor might be deeper, should have same path then
255 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
256 if (cursor[i].par_ != anchor[i].par_)
259 // position should be ok.
264 void MathCursor::setPos(int x, int y)
267 bool res = bruteFind(x, y,
268 formula()->xlow(), formula()->xhigh(),
269 formula()->ylow(), formula()->yhigh());
271 // this ccan happen on creation of "math-display"
280 void MathCursor::home(bool sel)
283 autocorrect_ = false;
286 if (!par()->idxHome(idx(), pos()))
292 void MathCursor::end(bool sel)
295 autocorrect_ = false;
298 if (!par()->idxEnd(idx(), pos()))
304 void MathCursor::plainErase()
306 array().erase(pos());
310 void MathCursor::markInsert()
312 //lyxerr << "inserting mark\n";
313 array().insert(pos(), MathAtom(new MathCharInset(0)));
317 void MathCursor::markErase()
319 //lyxerr << "deleting mark\n";
320 array().erase(pos());
324 void MathCursor::plainInsert(MathAtom const & t)
326 array().insert(pos(), t);
331 void MathCursor::insert(string const & str)
333 //lyxerr << "inserting '" << str << "'\n";
335 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
336 plainInsert(MathAtom(new MathCharInset(*it)));
340 void MathCursor::insert(char c)
342 //lyxerr << "inserting '" << c << "'\n";
344 plainInsert(MathAtom(new MathCharInset(c)));
348 void MathCursor::insert(MathAtom const & t)
356 void MathCursor::niceInsert(MathAtom const & t)
359 MathGridInset safe = grabAndEraseSelection();
361 // enter the new inset and move the contents of the selection if possible
364 pushLeft(nextAtom());
370 void MathCursor::insert(MathArray const & ar)
376 array().insert(pos(), ar);
381 void MathCursor::paste(MathArray const & ar)
385 array().insert(pos(), ar);
390 void MathCursor::paste(MathGridInset const & data)
392 if (data.nargs() == 1) {
393 // single cell/part of cell
397 idx_type idx; // index of upper left cell
398 MathGridInset * p = enclosingGrid(idx);
399 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
400 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
401 for (row_type row = 0; row < numrows; ++row) {
402 for (col_type col = 0; col < numcols; ++col) {
403 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
404 p->cell(i).push_back(data.cell(data.index(row, col)));
406 // append the left over horizontal cells to the last column
407 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
408 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
409 p->cell(i).push_back(data.cell(data.index(row, col)));
411 // append the left over vertical cells to the last _cell_
412 idx_type i = p->nargs() - 1;
413 for (row_type row = numrows; row < data.nrows(); ++row)
414 for (col_type col = 0; col < data.ncols(); ++col)
415 p->cell(i).push_back(data.cell(data.index(row, col)));
419 void MathCursor::backspace()
421 autocorrect_ = false;
432 MathScriptInset * p = prevAtom()->asScriptInset();
434 p->removeScript(p->hasUp());
435 // Don't delete if there is anything left
436 if (p->hasUp() || p->hasDown())
445 void MathCursor::erase()
447 autocorrect_ = false;
456 // delete empty cells if possible
458 if (par()->idxDelete(idx()))
461 // old behaviour when in last position of cell
462 if (pos() == size()) {
463 par()->idxGlue(idx());
467 MathScriptInset * p = nextAtom()->asScriptInset();
469 p->removeScript(p->hasUp());
470 // Don't delete if there is anything left
471 if (p->hasUp() || p->hasDown())
479 void MathCursor::delLine()
481 autocorrect_ = false;
489 if (par()->nrows() > 1) {
490 // grid are the only things with more than one row...
491 lyx::Assert(par()->asGridInset());
492 par()->asGridInset()->delRow(hullRow());
495 if (idx() >= par()->nargs())
496 idx() = par()->nargs() - 1;
503 bool MathCursor::up(bool sel)
508 cursor_type save = Cursor_;
512 autocorrect_ = false;
517 bool MathCursor::down(bool sel)
522 cursor_type save = Cursor_;
526 autocorrect_ = false;
531 bool MathCursor::toggleLimits()
535 MathScriptInset * t = nextAtom()->asScriptInset();
538 int old = t->limits();
539 t->limits(old < 0 ? 1 : -1);
540 return old != t->limits();
544 void MathCursor::macroModeClose()
546 MathUnknownInset * p = inMacroMode();
550 string s = p->name();
552 array().erase(pos());
558 string MathCursor::macroName() const
560 return inMacroMode() ? inMacroMode()->name() : string();
564 void MathCursor::selClear()
570 void MathCursor::selCopy()
574 theCutBuffer = grabSelection();
577 theCutBuffer = MathGridInset(1, 1);
582 void MathCursor::selCut()
585 theCutBuffer = grabAndEraseSelection();
589 void MathCursor::selDel()
599 void MathCursor::selPaste()
609 void MathCursor::selHandle(bool sel)
611 if (sel == selection_)
619 void MathCursor::selStart()
629 void MathCursor::selClearOrDel()
631 if (lyxrc.auto_region_delete)
638 void MathCursor::selGet(MathArray & ar)
642 ar = grabSelection().glue();
647 void MathCursor::drawSelection(MathPainterInfo & pain) const
654 getSelection(i1, i2);
656 if (i1.idx_ == i2.idx_) {
657 MathXArray & c = i1.xcell();
658 int x1 = c.xo() + c.pos2x(i1.pos_);
659 int y1 = c.yo() - c.ascent();
660 int x2 = c.xo() + c.pos2x(i2.pos_);
661 int y2 = c.yo() + c.descent();
662 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
664 vector<MathInset::idx_type> indices
665 = i1.par_->idxBetween(i1.idx_, i2.idx_);
666 for (unsigned i = 0; i < indices.size(); ++i) {
667 MathXArray & c = i1.xcell(indices[i]);
669 int y1 = c.yo() - c.ascent();
670 int x2 = c.xo() + c.width();
671 int y2 = c.yo() + c.descent();
672 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
677 // draw anchor if different from selection boundary
678 MathCursorPos anc = Anchor_.back();
679 if (anc != i1 && anc != i2) {
680 MathXArray & c = anc.xcell();
681 int x = c.xo() + c.pos2x(anc.pos_);
682 int y1 = c.yo() - c.ascent();
683 int y2 = c.yo() + c.descent();
684 pain.line(x, y1, x, y2, LColor::math);
690 void MathCursor::handleDelim(string const & l, string const & r)
692 handleNest(new MathDelimInset(l, r));
696 void MathCursor::handleNest(MathInset * p)
698 p->cell(0) = grabAndEraseSelection().glue();
699 insert(MathAtom(p)); // this invalidates p!
700 pushRight(prevAtom());
704 void MathCursor::getPos(int & x, int & y)
707 #warning This should probably take cellXOffset and cellYOffset into account
709 x = xarray().xo() + xarray().pos2x(pos());
710 // move cursor visually into empty cells ("blue rectangles");
717 MathInset * MathCursor::par() const
719 return cursor().par_;
723 InsetFormulaBase * MathCursor::formula()
729 MathCursor::idx_type MathCursor::idx() const
731 return cursor().idx_;
735 MathCursor::idx_type & MathCursor::idx()
737 return cursor().idx_;
741 MathCursor::pos_type MathCursor::pos() const
743 return cursor().pos_;
747 MathCursor::pos_type & MathCursor::pos()
749 return cursor().pos_;
753 MathUnknownInset * MathCursor::inMacroMode() const
757 MathUnknownInset * p = prevAtom()->asUnknownInset();
758 return (p && !p->final()) ? p : 0;
762 bool MathCursor::inMacroArgMode() const
764 return pos() > 0 && prevAtom()->getChar() == '#';
768 bool MathCursor::selection() const
774 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
776 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
777 MathGridInset * p = Cursor_[i].par_->asGridInset();
779 idx = Cursor_[i].idx_;
787 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
789 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
790 MathHullInset * p = Cursor_[i].par_->asHullInset();
792 idx = Cursor_[i].idx_;
800 void MathCursor::popToEnclosingGrid()
802 while (depth() && !Cursor_.back().par_->asGridInset())
807 void MathCursor::popToEnclosingHull()
809 while (depth() && !Cursor_.back().par_->asHullInset())
814 void MathCursor::pullArg(bool goright)
817 MathArray a = array();
819 MathScriptInset const * p = par()->asScriptInset();
821 // special handling for scripts
822 const bool up = p->hasUp();
824 MathScriptInset * q = nextAtom()->asScriptInset();
828 array().insert(pos(), a);
834 array().insert(pos(), a);
838 formula()->mutateToText();
843 void MathCursor::touch()
845 cursor_type::const_iterator it = Cursor_.begin();
846 cursor_type::const_iterator et = Cursor_.end();
847 for ( ; it != et; ++it)
852 void MathCursor::normalize()
857 MathIterator it = ibegin(formula()->par().nucleus());
858 MathIterator et = iend(formula()->par().nucleus());
859 for (; it != et; ++it)
860 if (it.par()->asBoxInset())
861 it.par()->asBoxInset()->rebreak();
865 if (idx() >= par()->nargs()) {
866 lyxerr << "this should not really happen - 1: "
867 << idx() << " " << par()->nargs() << "\n";
870 idx() = min(idx(), par()->nargs() - 1);
872 if (pos() > size()) {
873 lyxerr << "this should not really happen - 2: "
874 << pos() << " " << size() << " in idx: " << idx()
876 WriteStream wi(lyxerr, false, true);
881 pos() = min(pos(), size());
883 // remove empty scripts if possible
885 for (pos_type i = 0; i < size(); ++i) {
886 MathScriptInset * p = array()[i]->asScriptInset();
888 p->removeEmptyScripts();
895 // fix again position
896 pos() = min(pos(), size());
900 MathCursor::size_type MathCursor::size() const
902 return array().size();
906 MathCursor::col_type MathCursor::hullCol() const
909 MathHullInset * p = enclosingHull(idx);
914 MathCursor::row_type MathCursor::hullRow() const
917 MathHullInset * p = enclosingHull(idx);
922 bool MathCursor::hasPrevAtom() const
928 bool MathCursor::hasNextAtom() const
930 return pos() < size();
934 MathAtom const & MathCursor::prevAtom() const
936 lyx::Assert(pos() > 0);
937 return array()[pos() - 1];
941 MathAtom & MathCursor::prevAtom()
943 lyx::Assert(pos() > 0);
944 return array()[pos() - 1];
948 MathAtom const & MathCursor::nextAtom() const
950 lyx::Assert(pos() < size());
951 return array()[pos()];
955 MathAtom & MathCursor::nextAtom()
957 lyx::Assert(pos() < size());
958 return array()[pos()];
962 MathArray & MathCursor::array() const
964 static MathArray dummy;
966 if (idx() >= par()->nargs()) {
967 lyxerr << "############ idx_ " << idx() << " not valid\n";
972 lyxerr << "############ depth() == 0 not valid\n";
976 return cursor().cell();
980 MathXArray & MathCursor::xarray() const
982 static MathXArray dummy;
985 lyxerr << "############ depth() == 0 not valid\n";
989 return cursor().xcell();
993 void MathCursor::idxNext()
995 par()->idxNext(idx(), pos());
999 void MathCursor::idxPrev()
1001 par()->idxPrev(idx(), pos());
1005 void MathCursor::splitCell()
1007 if (idx() + 1 == par()->nargs())
1009 MathArray ar = array();
1011 array().erase(pos(), size());
1014 array().insert(0, ar);
1018 void MathCursor::breakLine()
1020 // leave inner cells
1025 MathHullInset * p = enclosingHull(dummy);
1029 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1030 p->mutate(LM_OT_EQNARRAY);
1034 p->addRow(hullRow());
1037 const row_type r = hullRow();
1038 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1039 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1043 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1048 //void MathCursor::readLine(MathArray & ar) const
1050 // idx_type base = row() * par()->ncols();
1051 // for (idx_type off = 0; off < par()->ncols(); ++off)
1052 // ar.push_back(par()->cell(base + off));
1056 char MathCursor::valign() const
1059 MathGridInset * p = enclosingGrid(idx);
1060 return p ? p->valign() : '\0';
1064 char MathCursor::halign() const
1067 MathGridInset * p = enclosingGrid(idx);
1068 return p ? p->halign(idx % p->ncols()) : '\0';
1072 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1074 MathCursorPos anc = normalAnchor();
1075 if (anc < cursor()) {
1085 MathCursorPos & MathCursor::cursor()
1087 lyx::Assert(depth());
1088 return Cursor_.back();
1092 MathCursorPos const & MathCursor::cursor() const
1094 lyx::Assert(depth());
1095 return Cursor_.back();
1099 bool MathCursor::goUpDown(bool up)
1101 // Be warned: The 'logic' implemented in this function is highly fragile.
1102 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1103 // So fiddle around with it only if you know what you are doing!
1104 int xlow, xhigh, ylow, yhigh;
1109 // check if we had something else in mind, if not, this is the future goal
1115 // try neigbouring script insets
1117 if (hasPrevAtom()) {
1118 MathScriptInset * p = prevAtom()->asScriptInset();
1119 if (p && p->has(up)) {
1122 idx() = up; // the superscript has index 1
1124 ///lyxerr << "updown: handled by scriptinset to the left\n";
1130 if (hasNextAtom()) {
1131 MathScriptInset * p = nextAtom()->asScriptInset();
1132 if (p && p->has(up)) {
1136 ///lyxerr << "updown: handled by scriptinset to the right\n";
1142 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1147 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1148 // lyxerr << "updown: handled by brute find in the same cell\n";
1152 // try to find an inset that knows better then we
1154 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1156 if (par()->idxUpDown(idx(), up)) {
1157 // we found a cell that thinks it has something "below" us.
1158 ///lyxerr << "updown: found inset that handles UpDown\n";
1159 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1160 // project (xo,yo) onto proper box
1161 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1162 /// << "\n xlow: " << xlow << " ylow: " << ylow
1163 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1164 xo = min(max(xo, xlow), xhigh);
1165 yo = min(max(yo, ylow), yhigh);
1166 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1167 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1168 ///lyxerr << "updown: handled by final brute find\n";
1174 // no such inset found, just take something "above"
1175 ///lyxerr << "updown: handled by strange case\n";
1180 up ? formula()->ylow() : yo + 4,
1181 up ? yo - 4 : formula()->yhigh()
1185 // any improvement so far?
1188 if (up ? ynew < yo : ynew > yo)
1194 bool MathCursor::bruteFind
1195 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1197 cursor_type best_cursor;
1198 double best_dist = 1e10;
1200 MathIterator it = ibegin(formula()->par().nucleus());
1201 MathIterator et = iend(formula()->par().nucleus());
1203 // avoid invalid nesting when selecting
1204 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1205 MathCursorPos const & top = it.position();
1206 int xo = top.xpos();
1207 int yo = top.ypos();
1208 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1209 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1210 // '<=' in order to take the last possible position
1211 // this is important for clicking behind \sum in e.g. '\sum_i a'
1212 if (d <= best_dist) {
1214 best_cursor = it.cursor();
1224 if (best_dist < 1e10)
1225 Cursor_ = best_cursor;
1226 return best_dist < 1e10;
1230 bool MathCursor::idxLineFirst()
1232 idx() -= idx() % par()->ncols();
1238 bool MathCursor::idxLineLast()
1240 idx() -= idx() % par()->ncols();
1241 idx() += par()->ncols() - 1;
1246 bool MathCursor::idxLeft()
1248 return par()->idxLeft(idx(), pos());
1252 bool MathCursor::idxRight()
1254 return par()->idxRight(idx(), pos());
1258 bool MathCursor::interpret(string const & s)
1260 //lyxerr << "interpret 1: '" << s << "'\n";
1264 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1265 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1266 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1268 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1270 istringstream is(s.substr(5).c_str());
1273 niceInsert(MathAtom(new MathCasesInset(n)));
1277 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1282 istringstream is(s.substr(6).c_str());
1283 is >> m >> n >> v_align >> h_align;
1287 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1291 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1293 istringstream is(s.substr(7).c_str());
1296 mathed_parse_cell(rep.from, from);
1297 mathed_parse_cell(rep.to, to);
1298 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1299 par()->replace(rep);
1303 string name = s.substr(1);
1305 if (name == "over" || name == "choose" || name == "atop") {
1306 MathArray ar = array();
1307 MathAtom t(createMathInset(name));
1308 t->asNestInset()->cell(0).swap(array());
1316 // prevent entering of recursive macros
1317 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1318 && formula()->getInsetName() == name)
1320 lyxerr << "can't enter recursive macro\n";
1324 niceInsert(createMathInset(name));
1329 bool MathCursor::script(bool up)
1331 // Hack to get \\^ and \\_ working
1332 if (inMacroMode() && macroName() == "\\") {
1334 interpret("\\mathcircumflex");
1341 MathGridInset safe = grabAndEraseSelection();
1342 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1343 prevAtom()->asScriptInset()->ensure(up);
1344 pushRight(prevAtom());
1347 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1348 nextAtom()->asScriptInset()->ensure(up);
1349 pushLeft(nextAtom());
1353 plainInsert(MathAtom(new MathScriptInset(up)));
1354 prevAtom()->asScriptInset()->ensure(up);
1355 pushRight(prevAtom());
1365 bool MathCursor::inMathMode() const
1367 if (par()->asBoxInset())
1369 if (par()->asFboxInset())
1371 if (par()->asParInset())
1377 bool MathCursor::interpret(char c)
1379 //lyxerr << "interpret 2: '" << c << "'\n";
1380 targetx_ = -1; // "no target"
1381 if (inMacroArgMode()) {
1385 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1386 if (p && 1 <= n && n <= p->numargs())
1387 insert(MathAtom(new MathMacroArgument(c - '0')));
1389 insert(createMathInset("#"));
1390 interpret(c); // try again
1396 if (inMacroMode()) {
1397 string name = macroName();
1398 //lyxerr << "interpret name: '" << name << "'\n";
1400 if (name.empty() && c == '\\') {
1402 interpret("\\backslash");
1407 inMacroMode()->name() += c;
1411 // handle 'special char' macros
1416 interpret("\\backslash");
1418 interpret(string("\\") + c);
1422 // leave macro mode and try again if necessary
1429 // leave autocorrect mode if necessary
1430 if (autocorrect_ && c == ' ') {
1431 autocorrect_ = false;
1435 // just clear selection on pressing the space bar
1436 if (selection_ && c == ' ') {
1443 if (!inMathMode()) {
1444 // suppress direct insertion of two spaces in a row
1445 // the still allows typing '<space>a<space>' and deleting the 'a', but
1446 // it is better than nothing...
1447 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1454 lyxerr << "starting with macro\n";
1455 insert(MathAtom(new MathUnknownInset("\\", false)));
1460 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1461 prevAtom()->asSpaceInset()->incSpace();
1466 // if are at the very end, leave the formula
1467 return pos() != size();
1471 insert(c); // LM_TC_TEX;
1476 if (c == '{' || c == '}', c)) {
1477 insert(c); // LM_TC_TEX;
1483 niceInsert(MathAtom(new MathBraceInset));
1492 insert(createMathInset("$"));
1497 insert(createMathInset("%"));
1502 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1503 insert(c, LM_TC_VAR);
1507 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1508 insert(c, LM_TC_VAR);
1509 lastcode_ = LM_TC_VAR;
1514 insert(c, LM_TC_TEX);
1515 //bv->owner()->message(_("TeX mode"));
1520 // try auto-correction
1521 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1524 // no special circumstances, so insert the character without any fuss
1526 autocorrect_ = true;
1531 void MathCursor::setSelection(cursor_type const & where, size_type n)
1540 void MathCursor::insetToggle()
1542 if (hasNextAtom()) {
1543 // toggle next inset ...
1544 nextAtom()->lock(!nextAtom()->lock());
1545 } else if (popLeft() && hasNextAtom()) {
1546 // ... or enclosing inset if we are in the last inset position
1547 nextAtom()->lock(!nextAtom()->lock());
1553 string MathCursor::info() const
1556 os << "Math editor mode ";
1557 for (int i = 0, n = depth(); i < n; ++i) {
1558 Cursor_[i].par_->infoize(os);
1562 // prevAtom()->infoize(os);
1564 return os.str().c_str(); // .c_str() needed for lyxstring
1568 unsigned MathCursor::depth() const
1570 return Cursor_.size();
1578 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1579 MathInset::row_type & r1, MathInset::row_type & r2,
1580 MathInset::col_type & c1, MathInset::col_type & c2)
1582 MathInset * p = i1.par_;
1583 c1 = p->col(i1.idx_);
1584 c2 = p->col(i2.idx_);
1587 r1 = p->row(i1.idx_);
1588 r2 = p->row(i2.idx_);
1596 MathGridInset MathCursor::grabSelection() const
1599 return MathGridInset();
1602 getSelection(i1, i2);
1603 // shouldn't we assert on i1.par_ == i2.par_?
1604 if (i1.idx_ == i2.idx_) {
1605 MathGridInset data(1, 1);
1606 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1611 region(i1, i2, r1, r2, c1, c2);
1612 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1613 for (row_type row = 0; row < data.nrows(); ++row)
1614 for (col_type col = 0; col < data.ncols(); ++col) {
1615 idx_type i = i1.par_->index(row + r1, col + c1);
1616 data.cell(data.index(row, col)) = i1.par_->cell(i);
1622 void MathCursor::eraseSelection()
1626 getSelection(i1, i2);
1627 if (i1.idx_ == i2.idx_)
1628 i1.cell().erase(i1.pos_, i2.pos_);
1630 MathInset * p = i1.par_;
1633 region(i1, i2, r1, r2, c1, c2);
1634 for (row_type row = r1; row <= r2; ++row)
1635 for (col_type col = c1; col <= c2; ++col)
1636 p->cell(p->index(row, col)).erase();
1642 MathGridInset MathCursor::grabAndEraseSelection()
1645 return MathGridInset();
1646 MathGridInset res = grabSelection();
1653 MathCursorPos MathCursor::normalAnchor() const
1655 if (Anchor_.size() < depth()) {
1657 lyxerr << "unusual Anchor size\n";
1659 //lyx::Assert(Anchor_.size() >= cursor.depth());
1660 // use Anchor on the same level as Cursor
1661 MathCursorPos normal = Anchor_[depth() - 1];
1662 if (depth() < Anchor_.size() && !(normal < cursor())) {
1663 // anchor is behind cursor -> move anchor behind the inset
1671 void MathCursor::handleExtern(const string & arg)
1675 istringstream iss(arg.c_str());
1676 iss >> lang >> extra;
1684 lyxerr << "use selection: " << ar << "\n";
1685 insert(pipeThroughExtern(lang, extra, ar));
1690 eq.push_back(MathAtom(new MathCharInset('=')));
1692 popToEnclosingHull();
1695 MathHullInset * hull = enclosingHull(idx);
1699 if (hull->getType() == LM_OT_SIMPLE) {
1700 MathArray::size_type pos = cursor().cell().find_last(eq);
1702 if (pos == size()) {
1704 lyxerr << "use whole cell: " << ar << "\n";
1706 ar = MathArray(array(), pos + 1, size());
1707 lyxerr << "use partial cell form pos: " << pos << "\n";
1711 insert(pipeThroughExtern(lang, extra, ar));
1715 if (hull->getType() == LM_OT_EQUATION) {
1716 lyxerr << "use equation inset\n";
1717 hull->mutate(LM_OT_EQNARRAY);
1718 MathArray & ar = cursor().cell();
1719 lyxerr << "use cell: " << ar << "\n";
1721 cursor().cell() = eq;
1723 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1729 lyxerr << "use eqnarray\n";
1731 MathArray ar = cursor().cell();
1732 lyxerr << "use cell: " << ar << "\n";
1735 cursor().cell() = eq;
1737 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1744 int MathCursor::dispatch(string const & cmd)
1746 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1747 MathCursorPos & pos = Cursor_[i];
1748 if (int res = pos.par_-> dispatch(cmd, pos.idx_, pos.pos_))