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_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 void MathCursor::push(MathAtom & t)
81 Cursor_.push_back(MathCursorPos(t.nucleus()));
85 void MathCursor::pushLeft(MathAtom & t)
87 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
89 t->idxFirst(idx(), pos());
93 void MathCursor::pushRight(MathAtom & t)
95 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
98 t->idxLast(idx(), pos());
102 bool MathCursor::popLeft()
104 //cerr << "Leaving atom to the left\n";
112 bool MathCursor::popRight()
114 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
125 void MathCursor::dump(char const * what) const
127 lyxerr << "MC: " << what << "\n";
128 lyxerr << " Cursor: " << depth() << "\n";
129 for (unsigned i = 0; i < depth(); ++i)
130 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
131 lyxerr << " Anchor: " << Anchor_.size() << "\n";
132 for (unsigned i = 0; i < Anchor_.size(); ++i)
133 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
134 lyxerr << " sel: " << selection_ << "\n";
137 void MathCursor::dump(char const *) const {}
141 bool MathCursor::isInside(MathInset const * p) const
143 for (unsigned i = 0; i < depth(); ++i)
144 if (Cursor_[i].par_ == p)
150 bool MathCursor::openable(MathAtom const & t, bool sel) const
158 if (t->asScriptInset())
162 // we can't move into anything new during selection
163 if (depth() == Anchor_.size())
165 if (t.nucleus() != Anchor_[depth()].par_)
172 bool MathCursor::posLeft()
181 bool MathCursor::posRight()
190 bool MathCursor::left(bool sel)
193 autocorrect_ = false;
194 targetx_ = -1; // "no target"
201 if (hasPrevAtom() && openable(prevAtom(), sel)) {
202 pushRight(prevAtom());
206 return posLeft() || idxLeft() || popLeft() || selection_;
210 bool MathCursor::right(bool sel)
213 autocorrect_ = false;
214 targetx_ = -1; // "no target"
221 if (hasNextAtom() && openable(nextAtom(), sel)) {
222 pushLeft(nextAtom());
226 return posRight() || idxRight() || popRight() || selection_;
230 void MathCursor::first()
233 push(formula_->par());
234 par()->idxFirst(idx(), pos());
238 void MathCursor::last()
241 push(formula_->par());
242 par()->idxLast(idx(), pos());
246 bool positionable(MathCursor::cursor_type const & cursor,
247 MathCursor::cursor_type const & anchor)
249 // avoid deeper nested insets when selecting
250 if (cursor.size() > anchor.size())
253 // anchor might be deeper, should have same path then
254 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
255 if (cursor[i].par_ != anchor[i].par_)
258 // position should be ok.
263 void MathCursor::setPos(int x, int y)
266 bool res = bruteFind(x, y,
267 formula()->xlow(), formula()->xhigh(),
268 formula()->ylow(), formula()->yhigh());
270 // this ccan happen on creation of "math-display"
279 void MathCursor::home(bool sel)
282 autocorrect_ = false;
285 if (!par()->idxHome(idx(), pos()))
291 void MathCursor::end(bool sel)
294 autocorrect_ = false;
297 if (!par()->idxEnd(idx(), pos()))
303 void MathCursor::plainErase()
305 array().erase(pos());
309 void MathCursor::markInsert()
311 //lyxerr << "inserting mark\n";
312 array().insert(pos(), MathAtom(new MathCharInset(0)));
316 void MathCursor::markErase()
318 //lyxerr << "deleting mark\n";
319 array().erase(pos());
323 void MathCursor::plainInsert(MathAtom const & t)
325 array().insert(pos(), t);
330 void MathCursor::insert(char c)
332 //lyxerr << "inserting '" << c << "'\n";
334 plainInsert(MathAtom(new MathCharInset(c)));
338 void MathCursor::insert(MathAtom const & t)
346 void MathCursor::niceInsert(MathAtom const & t)
349 MathGridInset safe = grabAndEraseSelection();
351 // enter the new inset and move the contents of the selection if possible
354 pushLeft(nextAtom());
360 void MathCursor::insert(MathArray const & ar)
366 array().insert(pos(), ar);
371 void MathCursor::paste(MathArray const & ar)
375 array().insert(pos(), ar);
380 void MathCursor::paste(MathGridInset const & data)
382 if (data.nargs() == 1) {
383 // single cell/part of cell
387 idx_type idx; // index of upper left cell
388 MathGridInset * p = enclosingGrid(idx);
389 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
390 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
391 for (row_type row = 0; row < numrows; ++row) {
392 for (col_type col = 0; col < numcols; ++col) {
393 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
394 p->cell(i).push_back(data.cell(data.index(row, col)));
396 // append the left over horizontal cells to the last column
397 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
398 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
399 p->cell(i).push_back(data.cell(data.index(row, col)));
401 // append the left over vertical cells to the last _cell_
402 idx_type i = p->nargs() - 1;
403 for (row_type row = numrows; row < data.nrows(); ++row)
404 for (col_type col = 0; col < data.ncols(); ++col)
405 p->cell(i).push_back(data.cell(data.index(row, col)));
409 void MathCursor::backspace()
411 autocorrect_ = false;
422 MathScriptInset * p = prevAtom()->asScriptInset();
424 p->removeScript(p->hasUp());
425 // Don't delete if there is anything left
426 if (p->hasUp() || p->hasDown())
435 void MathCursor::erase()
437 autocorrect_ = false;
446 // delete empty cells if possible
448 if (par()->idxDelete(idx()))
451 // old behaviour when in last position of cell
452 if (pos() == size()) {
453 par()->idxGlue(idx());
457 MathScriptInset * p = nextAtom()->asScriptInset();
459 p->removeScript(p->hasUp());
460 // Don't delete if there is anything left
461 if (p->hasUp() || p->hasDown())
469 void MathCursor::delLine()
471 autocorrect_ = false;
479 if (par()->nrows() > 1) {
480 // grid are the only things with more than one row...
481 lyx::Assert(par()->asGridInset());
482 par()->asGridInset()->delRow(hullRow());
485 if (idx() >= par()->nargs())
486 idx() = par()->nargs() - 1;
493 bool MathCursor::up(bool sel)
498 cursor_type save = Cursor_;
502 autocorrect_ = false;
507 bool MathCursor::down(bool sel)
512 cursor_type save = Cursor_;
516 autocorrect_ = false;
521 bool MathCursor::toggleLimits()
525 MathScriptInset * t = nextAtom()->asScriptInset();
528 int old = t->limits();
529 t->limits(old < 0 ? 1 : -1);
530 return old != t->limits();
534 void MathCursor::macroModeClose()
536 MathUnknownInset * p = inMacroMode();
540 string s = p->name();
542 array().erase(pos());
548 string MathCursor::macroName() const
550 return inMacroMode() ? inMacroMode()->name() : string();
554 void MathCursor::selClear()
560 void MathCursor::selCopy()
564 theCutBuffer = grabSelection();
567 theCutBuffer = MathGridInset(1, 1);
572 void MathCursor::selCut()
575 theCutBuffer = grabAndEraseSelection();
579 void MathCursor::selDel()
589 void MathCursor::selPaste()
599 void MathCursor::selHandle(bool sel)
601 if (sel == selection_)
609 void MathCursor::selStart()
619 void MathCursor::selClearOrDel()
621 if (lyxrc.auto_region_delete)
628 void MathCursor::selGet(MathArray & ar)
632 ar = grabSelection().glue();
637 void MathCursor::drawSelection(MathPainterInfo & pain) const
644 getSelection(i1, i2);
646 if (i1.idx_ == i2.idx_) {
647 MathXArray & c = i1.xcell();
648 int x1 = c.xo() + c.pos2x(i1.pos_);
649 int y1 = c.yo() - c.ascent();
650 int x2 = c.xo() + c.pos2x(i2.pos_);
651 int y2 = c.yo() + c.descent();
652 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
654 vector<MathInset::idx_type> indices
655 = i1.par_->idxBetween(i1.idx_, i2.idx_);
656 for (unsigned i = 0; i < indices.size(); ++i) {
657 MathXArray & c = i1.xcell(indices[i]);
659 int y1 = c.yo() - c.ascent();
660 int x2 = c.xo() + c.width();
661 int y2 = c.yo() + c.descent();
662 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
667 // draw anchor if different from selection boundary
668 MathCursorPos anc = Anchor_.back();
669 if (anc != i1 && anc != i2) {
670 MathXArray & c = anc.xcell();
671 int x = c.xo() + c.pos2x(anc.pos_);
672 int y1 = c.yo() - c.ascent();
673 int y2 = c.yo() + c.descent();
674 pain.line(x, y1, x, y2, LColor::math);
680 void MathCursor::handleDelim(string const & l, string const & r)
682 handleNest(new MathDelimInset(l, r));
686 void MathCursor::handleNest(MathInset * p)
688 p->cell(0) = grabAndEraseSelection().glue();
689 insert(MathAtom(p)); // this invalidates p!
690 pushRight(prevAtom());
694 void MathCursor::getPos(int & x, int & y)
697 #warning This should probably take cellXOffset and cellYOffset into account
699 x = xarray().xo() + xarray().pos2x(pos());
700 // move cursor visually into empty cells ("blue rectangles");
707 MathInset * MathCursor::par() const
709 return cursor().par_;
713 InsetFormulaBase * MathCursor::formula()
719 MathCursor::idx_type MathCursor::idx() const
721 return cursor().idx_;
725 MathCursor::idx_type & MathCursor::idx()
727 return cursor().idx_;
731 MathCursor::pos_type MathCursor::pos() const
733 return cursor().pos_;
737 MathCursor::pos_type & MathCursor::pos()
739 return cursor().pos_;
743 MathUnknownInset * MathCursor::inMacroMode() const
747 MathUnknownInset * p = prevAtom()->asUnknownInset();
748 return (p && !p->final()) ? p : 0;
752 bool MathCursor::inMacroArgMode() const
754 return pos() > 0 && prevAtom()->getChar() == '#';
758 bool MathCursor::selection() const
764 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
766 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
767 MathGridInset * p = Cursor_[i].par_->asGridInset();
769 idx = Cursor_[i].idx_;
777 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
779 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
780 MathHullInset * p = Cursor_[i].par_->asHullInset();
782 idx = Cursor_[i].idx_;
790 void MathCursor::popToEnclosingGrid()
792 while (depth() && !Cursor_.back().par_->asGridInset())
797 void MathCursor::popToEnclosingHull()
799 while (depth() && !Cursor_.back().par_->asHullInset())
804 void MathCursor::pullArg(bool goright)
807 MathArray a = array();
809 MathScriptInset const * p = par()->asScriptInset();
811 // special handling for scripts
812 const bool up = p->hasUp();
814 MathScriptInset * q = nextAtom()->asScriptInset();
818 array().insert(pos(), a);
824 array().insert(pos(), a);
828 formula()->mutateToText();
833 void MathCursor::touch()
835 cursor_type::const_iterator it = Cursor_.begin();
836 cursor_type::const_iterator et = Cursor_.end();
837 for ( ; it != et; ++it)
842 void MathCursor::normalize()
847 MathIterator it = ibegin(formula()->par().nucleus());
848 MathIterator et = iend(formula()->par().nucleus());
849 for (; it != et; ++it)
850 if (it.par()->asBoxInset())
851 it.par()->asBoxInset()->rebreak();
855 if (idx() >= par()->nargs()) {
856 lyxerr << "this should not really happen - 1: "
857 << idx() << " " << par()->nargs() << "\n";
860 idx() = min(idx(), par()->nargs() - 1);
862 if (pos() > size()) {
863 lyxerr << "this should not really happen - 2: "
864 << pos() << " " << size() << " in idx: " << idx()
866 WriteStream wi(lyxerr, false, true);
871 pos() = min(pos(), size());
873 // remove empty scripts if possible
875 for (pos_type i = 0; i < size(); ++i) {
876 MathScriptInset * p = array()[i]->asScriptInset();
878 p->removeEmptyScripts();
885 // fix again position
886 pos() = min(pos(), size());
890 MathCursor::size_type MathCursor::size() const
892 return array().size();
896 MathCursor::col_type MathCursor::hullCol() const
899 MathHullInset * p = enclosingHull(idx);
904 MathCursor::row_type MathCursor::hullRow() const
907 MathHullInset * p = enclosingHull(idx);
912 bool MathCursor::hasPrevAtom() const
918 bool MathCursor::hasNextAtom() const
920 return pos() < size();
924 MathAtom const & MathCursor::prevAtom() const
926 lyx::Assert(pos() > 0);
927 return array()[pos() - 1];
931 MathAtom & MathCursor::prevAtom()
933 lyx::Assert(pos() > 0);
934 return array()[pos() - 1];
938 MathAtom const & MathCursor::nextAtom() const
940 lyx::Assert(pos() < size());
941 return array()[pos()];
945 MathAtom & MathCursor::nextAtom()
947 lyx::Assert(pos() < size());
948 return array()[pos()];
952 MathArray & MathCursor::array() const
954 static MathArray dummy;
956 if (idx() >= par()->nargs()) {
957 lyxerr << "############ idx_ " << idx() << " not valid\n";
962 lyxerr << "############ depth() == 0 not valid\n";
966 return cursor().cell();
970 MathXArray & MathCursor::xarray() const
972 static MathXArray dummy;
975 lyxerr << "############ depth() == 0 not valid\n";
979 return cursor().xcell();
983 void MathCursor::idxNext()
985 par()->idxNext(idx(), pos());
989 void MathCursor::idxPrev()
991 par()->idxPrev(idx(), pos());
995 void MathCursor::splitCell()
997 if (idx() + 1 == par()->nargs())
999 MathArray ar = array();
1001 array().erase(pos(), size());
1004 array().insert(0, ar);
1008 void MathCursor::breakLine()
1010 // leave inner cells
1015 MathHullInset * p = enclosingHull(dummy);
1019 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1020 p->mutate(LM_OT_EQNARRAY);
1024 p->addRow(hullRow());
1027 const row_type r = hullRow();
1028 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1029 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1033 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1038 //void MathCursor::readLine(MathArray & ar) const
1040 // idx_type base = row() * par()->ncols();
1041 // for (idx_type off = 0; off < par()->ncols(); ++off)
1042 // ar.push_back(par()->cell(base + off));
1046 char MathCursor::valign() const
1049 MathGridInset * p = enclosingGrid(idx);
1050 return p ? p->valign() : '\0';
1054 char MathCursor::halign() const
1057 MathGridInset * p = enclosingGrid(idx);
1058 return p ? p->halign(idx % p->ncols()) : '\0';
1062 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1064 MathCursorPos anc = normalAnchor();
1065 if (anc < cursor()) {
1075 MathCursorPos & MathCursor::cursor()
1077 lyx::Assert(depth());
1078 return Cursor_.back();
1082 MathCursorPos const & MathCursor::cursor() const
1084 lyx::Assert(depth());
1085 return Cursor_.back();
1089 bool MathCursor::goUpDown(bool up)
1091 // Be warned: The 'logic' implemented in this function is highly fragile.
1092 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1093 // So fiddle around with it only if you know what you are doing!
1094 int xlow, xhigh, ylow, yhigh;
1099 // check if we had something else in mind, if not, this is the future goal
1105 // try neigbouring script insets
1107 if (hasPrevAtom()) {
1108 MathScriptInset * p = prevAtom()->asScriptInset();
1109 if (p && p->has(up)) {
1112 idx() = up; // the superscript has index 1
1114 ///lyxerr << "updown: handled by scriptinset to the left\n";
1120 if (hasNextAtom()) {
1121 MathScriptInset * p = nextAtom()->asScriptInset();
1122 if (p && p->has(up)) {
1126 ///lyxerr << "updown: handled by scriptinset to the right\n";
1132 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1137 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1138 // lyxerr << "updown: handled by brute find in the same cell\n";
1142 // try to find an inset that knows better then we
1144 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1146 if (par()->idxUpDown(idx(), up)) {
1147 // we found a cell that thinks it has something "below" us.
1148 ///lyxerr << "updown: found inset that handles UpDown\n";
1149 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1150 // project (xo,yo) onto proper box
1151 ///lyxerr << "\n xo: " << xo << " yo: " << yo
1152 /// << "\n xlow: " << xlow << " ylow: " << ylow
1153 /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1154 xo = min(max(xo, xlow), xhigh);
1155 yo = min(max(yo, ylow), yhigh);
1156 ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1157 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1158 ///lyxerr << "updown: handled by final brute find\n";
1164 // no such inset found, just take something "above"
1165 ///lyxerr << "updown: handled by strange case\n";
1170 up ? formula()->ylow() : yo + 4,
1171 up ? yo - 4 : formula()->yhigh()
1175 // any improvement so far?
1178 if (up ? ynew < yo : ynew > yo)
1184 bool MathCursor::bruteFind
1185 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1187 cursor_type best_cursor;
1188 double best_dist = 1e10;
1190 MathIterator it = ibegin(formula()->par().nucleus());
1191 MathIterator et = iend(formula()->par().nucleus());
1193 // avoid invalid nesting when selecting
1194 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1195 MathCursorPos const & top = it.position();
1196 int xo = top.xpos();
1197 int yo = top.ypos();
1198 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1199 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1200 // '<=' in order to take the last possible position
1201 // this is important for clicking behind \sum in e.g. '\sum_i a'
1202 if (d <= best_dist) {
1204 best_cursor = it.cursor();
1214 if (best_dist < 1e10)
1215 Cursor_ = best_cursor;
1216 return best_dist < 1e10;
1220 bool MathCursor::idxLineFirst()
1222 idx() -= idx() % par()->ncols();
1228 bool MathCursor::idxLineLast()
1230 idx() -= idx() % par()->ncols();
1231 idx() += par()->ncols() - 1;
1236 bool MathCursor::idxLeft()
1238 return par()->idxLeft(idx(), pos());
1242 bool MathCursor::idxRight()
1244 return par()->idxRight(idx(), pos());
1248 bool MathCursor::interpret(string const & s)
1250 //lyxerr << "interpret 1: '" << s << "'\n";
1254 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1255 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1256 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1258 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1260 istringstream is(s.substr(5).c_str());
1263 niceInsert(MathAtom(new MathCasesInset(n)));
1267 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1272 istringstream is(s.substr(6).c_str());
1273 is >> m >> n >> v_align >> h_align;
1277 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1281 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1283 istringstream is(s.substr(7).c_str());
1286 mathed_parse_cell(rep.from, from);
1287 mathed_parse_cell(rep.to, to);
1288 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1289 par()->replace(rep);
1293 string name = s.substr(1);
1295 if (name == "over" || name == "choose" || name == "atop") {
1296 MathArray ar = array();
1297 MathAtom t(createMathInset(name));
1298 t->asNestInset()->cell(0).swap(array());
1306 // prevent entering of recursive macros
1307 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1308 && formula()->getInsetName() == name)
1310 lyxerr << "can't enter recursive macro\n";
1314 niceInsert(createMathInset(name));
1319 bool MathCursor::script(bool up)
1321 // Hack to get \\^ and \\_ working
1322 if (inMacroMode() && macroName() == "\\") {
1324 interpret("\\mathcircumflex");
1331 MathGridInset safe = grabAndEraseSelection();
1332 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1333 prevAtom()->asScriptInset()->ensure(up);
1334 pushRight(prevAtom());
1337 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1338 nextAtom()->asScriptInset()->ensure(up);
1339 pushLeft(nextAtom());
1343 plainInsert(MathAtom(new MathScriptInset(up)));
1344 prevAtom()->asScriptInset()->ensure(up);
1345 pushRight(prevAtom());
1355 bool MathCursor::inMathMode() const
1357 return !par()->asBoxInset();
1361 bool MathCursor::interpret(char c)
1363 //lyxerr << "interpret 2: '" << c << "'\n";
1364 targetx_ = -1; // "no target"
1365 if (inMacroArgMode()) {
1369 MathMacroTemplate * p = formula()->par()->asMacroTemplate();
1370 if (p && 1 <= n && n <= p->numargs())
1371 insert(MathAtom(new MathMacroArgument(c - '0')));
1373 insert(createMathInset("#"));
1374 interpret(c); // try again
1380 if (inMacroMode()) {
1381 string name = macroName();
1382 //lyxerr << "interpret name: '" << name << "'\n";
1384 if (name.empty() && c == '\\') {
1386 interpret("\\backslash");
1391 inMacroMode()->name() += c;
1395 // handle 'special char' macros
1400 interpret("\\backslash");
1402 interpret(string("\\") + c);
1406 // leave macro mode and try again
1412 // leave autocorrect mode if necessary
1413 if (autocorrect_ && c == ' ') {
1414 autocorrect_ = false;
1418 // just clear selection on pressing the space par
1419 if (selection_ && c == ' ') {
1426 if (!inMathMode()) {
1427 // suppress direct insertion of two spaces in a row
1428 // the still allows typing '<space>a<space>' and deleting the 'a', but
1429 // it is better than nothing...
1430 if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
1437 lyxerr << "starting with macro\n";
1438 insert(MathAtom(new MathUnknownInset("\\", false)));
1443 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1444 prevAtom()->asSpaceInset()->incSpace();
1449 // if are at the very end, leave the formula
1450 return pos() != size();
1454 insert(c); // LM_TC_TEX;
1459 if (c == '{' || c == '}', c)) {
1460 insert(c); // LM_TC_TEX;
1466 niceInsert(MathAtom(new MathBraceInset));
1475 insert(createMathInset("$"));
1480 insert(createMathInset("%"));
1485 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1486 insert(c, LM_TC_VAR);
1490 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1491 insert(c, LM_TC_VAR);
1492 lastcode_ = LM_TC_VAR;
1497 insert(c, LM_TC_TEX);
1498 //bv->owner()->message(_("TeX mode"));
1503 // try auto-correction
1504 if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1507 // no special circumstances, so insert the character without any fuss
1509 autocorrect_ = true;
1514 void MathCursor::setSelection(cursor_type const & where, size_type n)
1523 void MathCursor::insetToggle()
1526 nextAtom()->lock(!nextAtom()->lock());
1530 string MathCursor::info() const
1533 os << "Math editor mode ";
1534 for (int i = 0, n = depth(); i < n; ++i) {
1535 Cursor_[i].par_->infoize(os);
1539 // prevAtom()->infoize(os);
1541 return os.str().c_str(); // .c_str() needed for lyxstring
1545 unsigned MathCursor::depth() const
1547 return Cursor_.size();
1555 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1556 MathInset::row_type & r1, MathInset::row_type & r2,
1557 MathInset::col_type & c1, MathInset::col_type & c2)
1559 MathInset * p = i1.par_;
1560 c1 = p->col(i1.idx_);
1561 c2 = p->col(i2.idx_);
1564 r1 = p->row(i1.idx_);
1565 r2 = p->row(i2.idx_);
1573 MathGridInset MathCursor::grabSelection() const
1576 return MathGridInset();
1579 getSelection(i1, i2);
1580 // shouldn't we assert on i1.par_ == i2.par_?
1581 if (i1.idx_ == i2.idx_) {
1582 MathGridInset data(1, 1);
1583 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1588 region(i1, i2, r1, r2, c1, c2);
1589 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1590 for (row_type row = 0; row < data.nrows(); ++row)
1591 for (col_type col = 0; col < data.ncols(); ++col) {
1592 idx_type i = i1.par_->index(row + r1, col + c1);
1593 data.cell(data.index(row, col)) = i1.par_->cell(i);
1599 void MathCursor::eraseSelection()
1603 getSelection(i1, i2);
1604 if (i1.idx_ == i2.idx_)
1605 i1.cell().erase(i1.pos_, i2.pos_);
1607 MathInset * p = i1.par_;
1610 region(i1, i2, r1, r2, c1, c2);
1611 for (row_type row = r1; row <= r2; ++row)
1612 for (col_type col = c1; col <= c2; ++col)
1613 p->cell(p->index(row, col)).erase();
1619 MathGridInset MathCursor::grabAndEraseSelection()
1622 return MathGridInset();
1623 MathGridInset res = grabSelection();
1630 MathCursorPos MathCursor::normalAnchor() const
1632 if (Anchor_.size() < depth()) {
1634 lyxerr << "unusual Anchor size\n";
1636 //lyx::Assert(Anchor_.size() >= cursor.depth());
1637 // use Anchor on the same level as Cursor
1638 MathCursorPos normal = Anchor_[depth() - 1];
1639 if (depth() < Anchor_.size() && !(normal < cursor())) {
1640 // anchor is behind cursor -> move anchor behind the inset
1648 void MathCursor::handleExtern(const string & arg)
1652 istringstream iss(arg.c_str());
1653 iss >> lang >> extra;
1661 lyxerr << "use selection: " << ar << "\n";
1662 insert(pipeThroughExtern(lang, extra, ar));
1667 eq.push_back(MathAtom(new MathCharInset('=')));
1669 popToEnclosingHull();
1672 MathHullInset * hull = enclosingHull(idx);
1676 if (hull->getType() == LM_OT_SIMPLE) {
1677 MathArray::size_type pos = cursor().cell().find_last(eq);
1679 if (pos == size()) {
1681 lyxerr << "use whole cell: " << ar << "\n";
1683 ar = MathArray(array(), pos + 1, size());
1684 lyxerr << "use partial cell form pos: " << pos << "\n";
1688 insert(pipeThroughExtern(lang, extra, ar));
1692 if (hull->getType() == LM_OT_EQUATION) {
1693 lyxerr << "use equation inset\n";
1694 hull->mutate(LM_OT_EQNARRAY);
1695 MathArray & ar = cursor().cell();
1696 lyxerr << "use cell: " << ar << "\n";
1698 cursor().cell() = eq;
1700 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1706 lyxerr << "use eqnarray\n";
1708 MathArray ar = cursor().cell();
1709 lyxerr << "use cell: " << ar << "\n";
1712 cursor().cell() = eq;
1714 cursor().cell() = pipeThroughExtern(lang, extra, ar);