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_extern.h"
39 #include "math_factory.h"
40 #include "math_fboxinset.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macroarg.h"
44 #include "math_macrotemplate.h"
45 #include "math_mathmlstream.h"
46 #include "math_parser.h"
47 #include "math_replace.h"
48 #include "math_scriptinset.h"
49 #include "math_spaceinset.h"
50 #include "math_support.h"
51 #include "math_unknowninset.h"
63 using std::ostringstream;
67 // matheds own cut buffer
68 MathGridInset theCutBuffer = MathGridInset(1, 1);
71 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
72 : formula_(formula), autocorrect_(false), selection_(false)
74 front ? first() : last();
79 MathCursor::~MathCursor()
81 // ensure that 'notifyCursorLeave' is called
87 void MathCursor::push(MathAtom & t)
89 Cursor_.push_back(MathCursorPos(t.nucleus()));
93 void MathCursor::pushLeft(MathAtom & t)
95 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
97 t->idxFirst(idx(), pos());
101 void MathCursor::pushRight(MathAtom & t)
103 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
106 t->idxLast(idx(), pos());
110 bool MathCursor::popLeft()
112 //cerr << "Leaving atom to the left\n";
115 par()->notifyCursorLeaves();
118 par()->notifyCursorLeaves();
124 bool MathCursor::popRight()
126 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
129 par()->notifyCursorLeaves();
132 par()->notifyCursorLeaves();
141 void MathCursor::dump(char const * what) const
143 lyxerr << "MC: " << what << "\n";
144 lyxerr << " Cursor: " << depth() << "\n";
145 for (unsigned i = 0; i < depth(); ++i)
146 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
147 lyxerr << " Anchor: " << Anchor_.size() << "\n";
148 for (unsigned i = 0; i < Anchor_.size(); ++i)
149 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
150 lyxerr << " sel: " << selection_ << "\n";
153 void MathCursor::dump(char const *) const {}
157 bool MathCursor::isInside(MathInset const * p) const
159 for (unsigned i = 0; i < depth(); ++i)
160 if (Cursor_[i].par_ == p)
166 bool MathCursor::openable(MathAtom const & t, bool sel) const
174 if (t->asScriptInset())
178 // we can't move into anything new during selection
179 if (depth() == Anchor_.size())
181 if (t.nucleus() != Anchor_[depth()].par_)
188 bool MathCursor::posLeft()
197 bool MathCursor::posRight()
206 bool MathCursor::left(bool sel)
209 autocorrect_ = false;
210 targetx_ = -1; // "no target"
217 if (hasPrevAtom() && openable(prevAtom(), sel)) {
218 pushRight(prevAtom());
222 return posLeft() || idxLeft() || popLeft() || selection_;
226 bool MathCursor::right(bool sel)
229 autocorrect_ = false;
230 targetx_ = -1; // "no target"
237 if (hasNextAtom() && openable(nextAtom(), sel)) {
238 pushLeft(nextAtom());
242 return posRight() || idxRight() || popRight() || selection_;
246 void MathCursor::first()
249 push(formula_->par());
250 par()->idxFirst(idx(), pos());
254 void MathCursor::last()
257 push(formula_->par());
258 par()->idxLast(idx(), pos());
262 bool positionable(MathCursor::cursor_type const & cursor,
263 MathCursor::cursor_type const & anchor)
265 // avoid deeper nested insets when selecting
266 if (cursor.size() > anchor.size())
269 // anchor might be deeper, should have same path then
270 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
271 if (cursor[i].par_ != anchor[i].par_)
274 // position should be ok.
279 void MathCursor::setPos(int x, int y)
282 bool res = bruteFind(x, y,
283 formula()->xlow(), formula()->xhigh(),
284 formula()->ylow(), formula()->yhigh());
286 // this ccan happen on creation of "math-display"
295 void MathCursor::home(bool sel)
298 autocorrect_ = false;
301 if (!par()->idxHome(idx(), pos()))
307 void MathCursor::end(bool sel)
310 autocorrect_ = false;
313 if (!par()->idxEnd(idx(), pos()))
319 void MathCursor::plainErase()
321 array().erase(pos());
325 void MathCursor::markInsert()
327 //lyxerr << "inserting mark\n";
328 array().insert(pos(), MathAtom(new MathCharInset(0)));
332 void MathCursor::markErase()
334 //lyxerr << "deleting mark\n";
335 array().erase(pos());
339 void MathCursor::plainInsert(MathAtom const & t)
341 array().insert(pos(), t);
346 void MathCursor::insert(string const & str)
348 //lyxerr << "inserting '" << str << "'\n";
350 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
351 plainInsert(MathAtom(new MathCharInset(*it)));
355 void MathCursor::insert(char c)
357 //lyxerr << "inserting '" << c << "'\n";
359 plainInsert(MathAtom(new MathCharInset(c)));
363 void MathCursor::insert(MathAtom const & t)
371 void MathCursor::niceInsert(MathAtom const & t)
374 MathGridInset safe = grabAndEraseSelection();
376 // enter the new inset and move the contents of the selection if possible
379 pushLeft(nextAtom());
385 void MathCursor::insert(MathArray const & ar)
391 array().insert(pos(), ar);
396 void MathCursor::paste(MathArray const & ar)
400 array().insert(pos(), ar);
405 void MathCursor::paste(MathGridInset const & data)
407 if (data.nargs() == 1) {
408 // single cell/part of cell
412 idx_type idx; // index of upper left cell
413 MathGridInset * p = enclosingGrid(idx);
414 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
415 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
416 for (row_type row = 0; row < numrows; ++row) {
417 for (col_type col = 0; col < numcols; ++col) {
418 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
419 p->cell(i).push_back(data.cell(data.index(row, col)));
421 // append the left over horizontal cells to the last column
422 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
423 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
424 p->cell(i).push_back(data.cell(data.index(row, col)));
426 // append the left over vertical cells to the last _cell_
427 idx_type i = p->nargs() - 1;
428 for (row_type row = numrows; row < data.nrows(); ++row)
429 for (col_type col = 0; col < data.ncols(); ++col)
430 p->cell(i).push_back(data.cell(data.index(row, col)));
434 void MathCursor::backspace()
436 autocorrect_ = false;
447 MathScriptInset * p = prevAtom()->asScriptInset();
449 p->removeScript(p->hasUp());
450 // Don't delete if there is anything left
451 if (p->hasUp() || p->hasDown())
460 void MathCursor::erase()
462 autocorrect_ = false;
471 // delete empty cells if possible
473 if (par()->idxDelete(idx()))
476 // old behaviour when in last position of cell
477 if (pos() == size()) {
478 par()->idxGlue(idx());
482 MathScriptInset * p = nextAtom()->asScriptInset();
484 p->removeScript(p->hasUp());
485 // Don't delete if there is anything left
486 if (p->hasUp() || p->hasDown())
494 void MathCursor::delLine()
496 autocorrect_ = false;
504 if (par()->nrows() > 1) {
505 // grid are the only things with more than one row...
506 lyx::Assert(par()->asGridInset());
507 par()->asGridInset()->delRow(hullRow());
510 if (idx() >= par()->nargs())
511 idx() = par()->nargs() - 1;
518 bool MathCursor::up(bool sel)
523 cursor_type save = Cursor_;
527 autocorrect_ = false;
532 bool MathCursor::down(bool sel)
537 cursor_type save = Cursor_;
541 autocorrect_ = false;
546 bool MathCursor::toggleLimits()
550 MathScriptInset * t = nextAtom()->asScriptInset();
553 int old = t->limits();
554 t->limits(old < 0 ? 1 : -1);
555 return old != t->limits();
559 void MathCursor::macroModeClose()
561 MathUnknownInset * p = inMacroMode();
565 string s = p->name();
567 array().erase(pos());
573 string MathCursor::macroName() const
575 return inMacroMode() ? inMacroMode()->name() : string();
579 void MathCursor::selClear()
585 void MathCursor::selCopy()
589 theCutBuffer = grabSelection();
592 theCutBuffer = MathGridInset(1, 1);
597 void MathCursor::selCut()
600 theCutBuffer = grabAndEraseSelection();
604 void MathCursor::selDel()
614 void MathCursor::selPaste()
624 void MathCursor::selHandle(bool sel)
626 if (sel == selection_)
634 void MathCursor::selStart()
644 void MathCursor::selClearOrDel()
646 if (lyxrc.auto_region_delete)
653 void MathCursor::selGet(MathArray & ar)
657 ar = grabSelection().glue();
662 void MathCursor::drawSelection(MathPainterInfo & pain) const
669 getSelection(i1, i2);
671 if (i1.idx_ == i2.idx_) {
672 MathXArray & c = i1.xcell();
673 int x1 = c.xo() + c.pos2x(i1.pos_);
674 int y1 = c.yo() - c.ascent();
675 int x2 = c.xo() + c.pos2x(i2.pos_);
676 int y2 = c.yo() + c.descent();
677 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
679 vector<MathInset::idx_type> indices
680 = i1.par_->idxBetween(i1.idx_, i2.idx_);
681 for (unsigned i = 0; i < indices.size(); ++i) {
682 MathXArray & c = i1.xcell(indices[i]);
684 int y1 = c.yo() - c.ascent();
685 int x2 = c.xo() + c.width();
686 int y2 = c.yo() + c.descent();
687 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
692 // draw anchor if different from selection boundary
693 MathCursorPos anc = Anchor_.back();
694 if (anc != i1 && anc != i2) {
695 MathXArray & c = anc.xcell();
696 int x = c.xo() + c.pos2x(anc.pos_);
697 int y1 = c.yo() - c.ascent();
698 int y2 = c.yo() + c.descent();
699 pain.line(x, y1, x, y2, LColor::math);
705 void MathCursor::handleNest(MathAtom const & at)
707 at->cell(0) = grabAndEraseSelection().glue();
709 pushRight(prevAtom());
713 void MathCursor::getPos(int & x, int & y)
715 par()->getPos(idx(), pos(), x, y);
719 MathInset * MathCursor::par() const
721 return cursor().par_;
725 InsetFormulaBase * MathCursor::formula() const
731 MathCursor::idx_type MathCursor::idx() const
733 return cursor().idx_;
737 MathCursor::idx_type & MathCursor::idx()
739 return cursor().idx_;
743 MathCursor::pos_type MathCursor::pos() const
745 return cursor().pos_;
749 MathCursor::pos_type & MathCursor::pos()
751 return cursor().pos_;
755 MathUnknownInset * MathCursor::inMacroMode() const
759 MathUnknownInset * p = prevAtom()->asUnknownInset();
760 return (p && !p->final()) ? p : 0;
764 bool MathCursor::inMacroArgMode() const
766 return pos() > 0 && prevAtom()->getChar() == '#';
770 bool MathCursor::selection() const
776 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
778 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
779 MathGridInset * p = Cursor_[i].par_->asGridInset();
781 idx = Cursor_[i].idx_;
789 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
791 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
792 MathHullInset * p = Cursor_[i].par_->asHullInset();
794 idx = Cursor_[i].idx_;
802 void MathCursor::popToEnclosingGrid()
804 while (depth() && !Cursor_.back().par_->asGridInset())
809 void MathCursor::popToEnclosingHull()
811 while (depth() && !Cursor_.back().par_->asHullInset())
816 void MathCursor::pullArg(bool goright)
819 MathArray a = array();
821 MathScriptInset const * p = par()->asScriptInset();
823 // special handling for scripts
824 const bool up = p->hasUp();
826 MathScriptInset * q = nextAtom()->asScriptInset();
830 array().insert(pos(), a);
836 array().insert(pos(), a);
840 formula()->mutateToText();
845 void MathCursor::touch()
847 cursor_type::const_iterator it = Cursor_.begin();
848 cursor_type::const_iterator et = Cursor_.end();
849 for ( ; it != et; ++it)
854 void MathCursor::normalize()
859 MathIterator it = ibegin(formula()->par().nucleus());
860 MathIterator et = iend(formula()->par().nucleus());
861 for (; it != et; ++it)
862 if (it.par()->asBoxInset())
863 it.par()->asBoxInset()->rebreak();
867 if (idx() >= par()->nargs()) {
868 lyxerr << "this should not really happen - 1: "
869 << idx() << " " << par()->nargs() << "\n";
872 idx() = min(idx(), par()->nargs() - 1);
874 if (pos() > size()) {
875 lyxerr << "this should not really happen - 2: "
876 << pos() << " " << size() << " in idx: " << idx()
878 WriteStream wi(lyxerr, false, true);
883 pos() = min(pos(), size());
885 // remove empty scripts if possible
887 for (pos_type i = 0; i < size(); ++i) {
888 MathScriptInset * p = array()[i]->asScriptInset();
890 p->removeEmptyScripts();
897 // fix again position
898 pos() = min(pos(), size());
902 MathCursor::size_type MathCursor::size() const
904 return array().size();
908 MathCursor::col_type MathCursor::hullCol() const
911 MathHullInset * p = enclosingHull(idx);
916 MathCursor::row_type MathCursor::hullRow() const
919 MathHullInset * p = enclosingHull(idx);
924 bool MathCursor::hasPrevAtom() const
930 bool MathCursor::hasNextAtom() const
932 return pos() < size();
936 MathAtom const & MathCursor::prevAtom() const
938 lyx::Assert(pos() > 0);
939 return array()[pos() - 1];
943 MathAtom & MathCursor::prevAtom()
945 lyx::Assert(pos() > 0);
946 return array()[pos() - 1];
950 MathAtom const & MathCursor::nextAtom() const
952 lyx::Assert(pos() < size());
953 return array()[pos()];
957 MathAtom & MathCursor::nextAtom()
959 lyx::Assert(pos() < size());
960 return array()[pos()];
964 MathArray & MathCursor::array() const
966 static MathArray dummy;
968 if (idx() >= par()->nargs()) {
969 lyxerr << "############ idx_ " << idx() << " not valid\n";
974 lyxerr << "############ depth() == 0 not valid\n";
978 return cursor().cell();
982 MathXArray & MathCursor::xarray() const
984 static MathXArray dummy;
987 lyxerr << "############ depth() == 0 not valid\n";
991 return cursor().xcell();
995 void MathCursor::idxNext()
997 par()->idxNext(idx(), pos());
1001 void MathCursor::idxPrev()
1003 par()->idxPrev(idx(), pos());
1007 void MathCursor::splitCell()
1009 if (idx() + 1 == par()->nargs())
1011 MathArray ar = array();
1013 array().erase(pos(), size());
1016 array().insert(0, ar);
1020 void MathCursor::breakLine()
1022 // leave inner cells
1027 MathHullInset * p = enclosingHull(dummy);
1031 if (p->getType() == "simple" || p->getType() == "equation") {
1032 p->mutate("eqnarray");
1036 p->addRow(hullRow());
1039 const row_type r = hullRow();
1040 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1041 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1045 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1050 //void MathCursor::readLine(MathArray & ar) const
1052 // idx_type base = row() * par()->ncols();
1053 // for (idx_type off = 0; off < par()->ncols(); ++off)
1054 // ar.push_back(par()->cell(base + off));
1058 char MathCursor::valign() const
1061 MathGridInset * p = enclosingGrid(idx);
1062 return p ? p->valign() : '\0';
1066 char MathCursor::halign() const
1069 MathGridInset * p = enclosingGrid(idx);
1070 return p ? p->halign(idx % p->ncols()) : '\0';
1074 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1076 MathCursorPos anc = normalAnchor();
1077 if (anc < cursor()) {
1087 MathCursorPos & MathCursor::cursor()
1089 lyx::Assert(depth());
1090 return Cursor_.back();
1094 MathCursorPos const & MathCursor::cursor() const
1096 lyx::Assert(depth());
1097 return Cursor_.back();
1101 bool MathCursor::goUpDown(bool up)
1103 // Be warned: The 'logic' implemented in this function is highly fragile.
1104 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1105 // So fiddle around with it only if you know what you are doing!
1106 int xlow, xhigh, ylow, yhigh;
1111 // check if we had something else in mind, if not, this is the future goal
1117 // try neigbouring script insets
1119 if (hasPrevAtom()) {
1120 MathScriptInset * p = prevAtom()->asScriptInset();
1121 if (p && p->has(up)) {
1124 idx() = up; // the superscript has index 1
1126 ///lyxerr << "updown: handled by scriptinset to the left\n";
1132 if (hasNextAtom()) {
1133 MathScriptInset * p = nextAtom()->asScriptInset();
1134 if (p && p->has(up)) {
1138 ///lyxerr << "updown: handled by scriptinset to the right\n";
1144 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1149 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1150 // lyxerr << "updown: handled by brute find in the same cell\n";
1154 // try to find an inset that knows better then we
1156 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1158 if (par()->idxUpDown(idx(), up)) {
1159 // we found a cell that thinks it has something "below" us.
1160 ///lyxerr << "updown: found inset that handles UpDown\n";
1161 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1162 // project (xo,yo) onto proper box
1163 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1164 /// << "\n xlow: " << xlow << " ylow: " << ylow
1165 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1166 xo = min(max(xo, xlow), xhigh);
1167 yo = min(max(yo, ylow), yhigh);
1168 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1169 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1170 ///lyxerr << "updown: handled by final brute find\n";
1176 // no such inset found, just take something "above"
1177 ///lyxerr << "updown: handled by strange case\n";
1182 up ? formula()->ylow() : yo + 4,
1183 up ? yo - 4 : formula()->yhigh()
1187 // any improvement so far?
1190 if (up ? ynew < yo : ynew > yo)
1196 bool MathCursor::bruteFind
1197 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1199 cursor_type best_cursor;
1200 double best_dist = 1e10;
1202 MathIterator it = ibegin(formula()->par().nucleus());
1203 MathIterator et = iend(formula()->par().nucleus());
1205 // avoid invalid nesting when selecting
1206 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1207 MathCursorPos const & top = it.position();
1208 int xo = top.xpos();
1209 int yo = top.ypos();
1210 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1211 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1212 // '<=' in order to take the last possible position
1213 // this is important for clicking behind \sum in e.g. '\sum_i a'
1214 if (d <= best_dist) {
1216 best_cursor = it.cursor();
1226 if (best_dist < 1e10)
1227 Cursor_ = best_cursor;
1228 return best_dist < 1e10;
1232 bool MathCursor::idxLineFirst()
1234 idx() -= idx() % par()->ncols();
1240 bool MathCursor::idxLineLast()
1242 idx() -= idx() % par()->ncols();
1243 idx() += par()->ncols() - 1;
1248 bool MathCursor::idxLeft()
1250 return par()->idxLeft(idx(), pos());
1254 bool MathCursor::idxRight()
1256 return par()->idxRight(idx(), pos());
1260 bool MathCursor::interpret(string const & s)
1262 //lyxerr << "interpret 1: '" << s << "'\n";
1266 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1267 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1268 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1270 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1272 istringstream is(s.substr(5).c_str());
1275 niceInsert(MathAtom(new MathCasesInset(n)));
1279 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1284 istringstream is(s.substr(6).c_str());
1285 is >> m >> n >> v_align >> h_align;
1289 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1293 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1295 istringstream is(s.substr(7).c_str());
1298 mathed_parse_cell(rep.from, from);
1299 mathed_parse_cell(rep.to, to);
1300 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1301 par()->replace(rep);
1305 string name = s.substr(1);
1307 if (name == "over" || name == "choose" || name == "atop") {
1308 MathArray ar = array();
1309 MathAtom t(createMathInset(name));
1310 t->asNestInset()->cell(0).swap(array());
1318 // prevent entering of recursive macros
1319 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1320 && formula()->getInsetName() == name)
1322 lyxerr << "can't enter recursive macro\n";
1326 niceInsert(createMathInset(name));
1331 bool MathCursor::script(bool up)
1333 // Hack to get \\^ and \\_ working
1334 if (inMacroMode() && macroName() == "\\") {
1336 interpret("\\mathcircumflex");
1343 MathGridInset safe = grabAndEraseSelection();
1344 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1345 prevAtom()->asScriptInset()->ensure(up);
1346 pushRight(prevAtom());
1349 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1350 nextAtom()->asScriptInset()->ensure(up);
1351 pushLeft(nextAtom());
1355 plainInsert(MathAtom(new MathScriptInset(up)));
1356 prevAtom()->asScriptInset()->ensure(up);
1357 pushRight(prevAtom());
1367 bool MathCursor::inMathMode() const
1369 if (par()->asBoxInset())
1371 if (par()->asFboxInset())
1373 if (par()->asParboxInset())
1375 if (par()->asParInset())
1381 bool MathCursor::interpret(char c)
1383 //lyxerr << "interpret 2: '" << c << "'\n";
1384 targetx_ = -1; // "no target"
1385 if (inMacroArgMode()) {
1389 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1390 if (p && 1 <= n && n <= p->numargs())
1391 insert(MathAtom(new MathMacroArgument(c - '0')));
1393 insert(createMathInset("#"));
1394 interpret(c); // try again
1400 if (inMacroMode()) {
1401 string name = macroName();
1402 //lyxerr << "interpret name: '" << name << "'\n";
1404 if (name.empty() && c == '\\') {
1406 interpret("\\backslash");
1411 inMacroMode()->name() += c;
1415 // handle 'special char' macros
1420 interpret("\\backslash");
1422 interpret(string("\\") + c);
1426 // leave macro mode and try again if necessary
1433 // This is annoying as one has to press <space> far too often.
1437 // leave autocorrect mode if necessary
1438 if (autocorrect_ && c == ' ') {
1439 autocorrect_ = false;
1444 // just clear selection on pressing the space bar
1445 if (selection_ && c == ' ') {
1452 if (!inMathMode()) {
1453 // suppress direct insertion of two spaces in a row
1454 // the still allows typing '<space>a<space>' and deleting the 'a', but
1455 // it is better than nothing...
1456 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1463 //lyxerr << "starting with macro\n";
1464 insert(MathAtom(new MathUnknownInset("\\", false)));
1469 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1470 prevAtom()->asSpaceInset()->incSpace();
1475 // if are at the very end, leave the formula
1476 return pos() != size();
1480 insert(c); // LM_TC_TEX;
1485 if (c == '{' || c == '}', c)) {
1486 insert(c); // LM_TC_TEX;
1492 niceInsert(MathAtom(new MathBraceInset));
1501 insert(createMathInset("$"));
1506 insert(createMathInset("%"));
1511 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1512 insert(c, LM_TC_VAR);
1516 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1517 insert(c, LM_TC_VAR);
1518 lastcode_ = LM_TC_VAR;
1523 insert(c, LM_TC_TEX);
1524 //bv->owner()->message(_("TeX mode"));
1529 // try auto-correction
1530 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1533 // no special circumstances, so insert the character without any fuss
1535 autocorrect_ = true;
1540 void MathCursor::setSelection(cursor_type const & where, size_type n)
1549 void MathCursor::insetToggle()
1551 if (hasNextAtom()) {
1552 // toggle next inset ...
1553 nextAtom()->lock(!nextAtom()->lock());
1554 } else if (popLeft() && hasNextAtom()) {
1555 // ... or enclosing inset if we are in the last inset position
1556 nextAtom()->lock(!nextAtom()->lock());
1562 string MathCursor::info() const
1565 os << "Math editor mode. ";
1566 for (int i = 0, n = depth(); i < n; ++i) {
1567 Cursor_[i].par_->infoize(os);
1571 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1572 prevAtom()->infoize(os);
1574 return os.str().c_str(); // .c_str() needed for lyxstring
1578 unsigned MathCursor::depth() const
1580 return Cursor_.size();
1588 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1589 MathInset::row_type & r1, MathInset::row_type & r2,
1590 MathInset::col_type & c1, MathInset::col_type & c2)
1592 MathInset * p = i1.par_;
1593 c1 = p->col(i1.idx_);
1594 c2 = p->col(i2.idx_);
1597 r1 = p->row(i1.idx_);
1598 r2 = p->row(i2.idx_);
1606 MathGridInset MathCursor::grabSelection() const
1609 return MathGridInset();
1612 getSelection(i1, i2);
1613 // shouldn't we assert on i1.par_ == i2.par_?
1614 if (i1.idx_ == i2.idx_) {
1615 MathGridInset data(1, 1);
1616 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1621 region(i1, i2, r1, r2, c1, c2);
1622 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1623 for (row_type row = 0; row < data.nrows(); ++row)
1624 for (col_type col = 0; col < data.ncols(); ++col) {
1625 idx_type i = i1.par_->index(row + r1, col + c1);
1626 data.cell(data.index(row, col)) = i1.par_->cell(i);
1632 void MathCursor::eraseSelection()
1636 getSelection(i1, i2);
1637 if (i1.idx_ == i2.idx_)
1638 i1.cell().erase(i1.pos_, i2.pos_);
1640 MathInset * p = i1.par_;
1643 region(i1, i2, r1, r2, c1, c2);
1644 for (row_type row = r1; row <= r2; ++row)
1645 for (col_type col = c1; col <= c2; ++col)
1646 p->cell(p->index(row, col)).erase();
1652 MathGridInset MathCursor::grabAndEraseSelection()
1655 return MathGridInset();
1656 MathGridInset res = grabSelection();
1663 MathCursorPos MathCursor::normalAnchor() const
1665 if (Anchor_.size() < depth()) {
1667 lyxerr << "unusual Anchor size\n";
1669 //lyx::Assert(Anchor_.size() >= cursor.depth());
1670 // use Anchor on the same level as Cursor
1671 MathCursorPos normal = Anchor_[depth() - 1];
1672 if (depth() < Anchor_.size() && !(normal < cursor())) {
1673 // anchor is behind cursor -> move anchor behind the inset
1681 void MathCursor::handleExtern(const string & arg)
1685 istringstream iss(arg.c_str());
1686 iss >> lang >> extra;
1694 lyxerr << "use selection: " << ar << "\n";
1695 insert(pipeThroughExtern(lang, extra, ar));
1700 eq.push_back(MathAtom(new MathCharInset('=')));
1702 popToEnclosingHull();
1705 MathHullInset * hull = enclosingHull(idx);
1709 if (hull->getType() == "simple") {
1710 MathArray::size_type pos = cursor().cell().find_last(eq);
1712 if (pos == size()) {
1714 lyxerr << "use whole cell: " << ar << "\n";
1716 ar = MathArray(array(), pos + 1, size());
1717 lyxerr << "use partial cell form pos: " << pos << "\n";
1721 insert(pipeThroughExtern(lang, extra, ar));
1725 if (hull->getType() == "equation") {
1726 lyxerr << "use equation inset\n";
1727 hull->mutate("eqnarray");
1728 MathArray & ar = cursor().cell();
1729 lyxerr << "use cell: " << ar << "\n";
1731 cursor().cell() = eq;
1733 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1739 lyxerr << "use eqnarray\n";
1741 MathArray ar = cursor().cell();
1742 lyxerr << "use cell: " << ar << "\n";
1745 cursor().cell() = eq;
1747 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1754 int MathCursor::dispatch(string const & cmd)
1756 // try to dispatch to adajcent items if they are not editable
1757 // actually, this should only happen for mouse clicks...
1758 if (hasNextAtom() && !openable(nextAtom(), false))
1759 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1761 if (hasPrevAtom() && !openable(prevAtom(), false))
1762 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1765 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1766 MathCursorPos & pos = Cursor_[i];
1767 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))