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"
27 #include "BufferView.h"
30 #include "frontends/Painter.h"
31 #include "math_cursor.h"
32 #include "formulabase.h"
33 #include "math_autocorrect.h"
34 #include "math_arrayinset.h"
35 #include "math_braceinset.h"
36 #include "math_casesinset.h"
37 #include "math_charinset.h"
38 #include "math_extern.h"
39 #include "math_factory.h"
40 #include "math_hullinset.h"
41 #include "math_iterator.h"
42 #include "math_macroarg.h"
43 #include "math_macrotemplate.h"
44 #include "math_mathmlstream.h"
45 #include "math_parser.h"
46 #include "math_replace.h"
47 #include "math_scriptinset.h"
48 #include "math_spaceinset.h"
49 #include "math_support.h"
50 #include "math_unknowninset.h"
62 using std::ostringstream;
66 // matheds own cut buffer
67 MathGridInset theCutBuffer = MathGridInset(1, 1);
70 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
71 : formula_(formula), autocorrect_(false), selection_(false)
73 front ? first() : last();
78 MathCursor::~MathCursor()
80 // ensure that 'notifyCursorLeave' is called
86 void MathCursor::push(MathAtom & t)
88 Cursor_.push_back(MathCursorPos(t.nucleus()));
92 void MathCursor::pushLeft(MathAtom & t)
94 //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
96 t->idxFirst(idx(), pos());
100 void MathCursor::pushRight(MathAtom & t)
102 //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
105 t->idxLast(idx(), pos());
109 bool MathCursor::popLeft()
111 //cerr << "Leaving atom to the left\n";
114 par()->notifyCursorLeaves();
117 par()->notifyCursorLeaves();
123 bool MathCursor::popRight()
125 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
128 par()->notifyCursorLeaves();
131 par()->notifyCursorLeaves();
140 void MathCursor::dump(char const * what) const
142 lyxerr << "MC: " << what << "\n";
143 lyxerr << " Cursor: " << depth() << "\n";
144 for (unsigned i = 0; i < depth(); ++i)
145 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
146 lyxerr << " Anchor: " << Anchor_.size() << "\n";
147 for (unsigned i = 0; i < Anchor_.size(); ++i)
148 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
149 lyxerr << " sel: " << selection_ << "\n";
152 void MathCursor::dump(char const *) const {}
156 bool MathCursor::isInside(MathInset const * p) const
158 for (unsigned i = 0; i < depth(); ++i)
159 if (Cursor_[i].par_ == p)
165 bool MathCursor::openable(MathAtom const & t, bool sel) const
173 if (t->asScriptInset())
177 // we can't move into anything new during selection
178 if (depth() == Anchor_.size())
180 if (t.nucleus() != Anchor_[depth()].par_)
187 bool MathCursor::posLeft()
196 bool MathCursor::posRight()
205 bool MathCursor::left(bool sel)
208 autocorrect_ = false;
209 targetx_ = -1; // "no target"
216 if (hasPrevAtom() && openable(prevAtom(), sel)) {
217 pushRight(prevAtom());
221 return posLeft() || idxLeft() || popLeft() || selection_;
225 bool MathCursor::right(bool sel)
228 autocorrect_ = false;
229 targetx_ = -1; // "no target"
236 if (hasNextAtom() && openable(nextAtom(), sel)) {
237 pushLeft(nextAtom());
241 return posRight() || idxRight() || popRight() || selection_;
245 void MathCursor::first()
248 push(formula_->par());
249 par()->idxFirst(idx(), pos());
253 void MathCursor::last()
256 push(formula_->par());
257 par()->idxLast(idx(), pos());
261 bool positionable(MathCursor::cursor_type const & cursor,
262 MathCursor::cursor_type const & anchor)
264 // avoid deeper nested insets when selecting
265 if (cursor.size() > anchor.size())
268 // anchor might be deeper, should have same path then
269 for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
270 if (cursor[i].par_ != anchor[i].par_)
273 // position should be ok.
278 void MathCursor::setPos(int x, int y)
281 bool res = bruteFind(x, y,
282 formula()->xlow(), formula()->xhigh(),
283 formula()->ylow(), formula()->yhigh());
285 // this ccan happen on creation of "math-display"
294 bool MathCursor::home(bool sel)
297 autocorrect_ = false;
300 if (!par()->idxHome(idx(), pos()))
307 bool MathCursor::end(bool sel)
310 autocorrect_ = false;
313 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)
343 array().insert(pos(), t);
348 void MathCursor::insert(string const & str)
350 //lyxerr << "inserting '" << str << "'\n";
352 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
353 plainInsert(MathAtom(new MathCharInset(*it)));
357 void MathCursor::insert(char c)
359 //lyxerr << "inserting '" << c << "'\n";
361 plainInsert(MathAtom(new MathCharInset(c)));
365 void MathCursor::insert(MathAtom const & t)
373 void MathCursor::niceInsert(MathAtom const & t)
376 MathGridInset safe = grabAndEraseSelection();
378 // enter the new inset and move the contents of the selection if possible
381 pushLeft(nextAtom());
387 void MathCursor::insert(MathArray const & ar)
393 array().insert(pos(), ar);
398 void MathCursor::paste(MathArray const & ar)
402 array().insert(pos(), ar);
407 void MathCursor::paste(MathGridInset const & data)
409 if (data.nargs() == 1) {
410 // single cell/part of cell
414 idx_type idx; // index of upper left cell
415 MathGridInset * p = enclosingGrid(idx);
416 col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
417 row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
418 for (row_type row = 0; row < numrows; ++row) {
419 for (col_type col = 0; col < numcols; ++col) {
420 idx_type i = p->index(row + p->row(idx), col + p->col(idx));
421 p->cell(i).push_back(data.cell(data.index(row, col)));
423 // append the left over horizontal cells to the last column
424 idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
425 for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
426 p->cell(i).push_back(data.cell(data.index(row, col)));
428 // append the left over vertical cells to the last _cell_
429 idx_type i = p->nargs() - 1;
430 for (row_type row = numrows; row < data.nrows(); ++row)
431 for (col_type col = 0; col < data.ncols(); ++col)
432 p->cell(i).push_back(data.cell(data.index(row, col)));
436 void MathCursor::backspace()
438 autocorrect_ = false;
449 MathScriptInset * p = prevAtom()->asScriptInset();
451 p->removeScript(p->hasUp());
452 // Don't delete if there is anything left
453 if (p->hasUp() || p->hasDown())
462 void MathCursor::erase()
464 autocorrect_ = false;
473 // delete empty cells if possible
475 if (par()->idxDelete(idx()))
478 // old behaviour when in last position of cell
479 if (pos() == size()) {
480 par()->idxGlue(idx());
484 MathScriptInset * p = nextAtom()->asScriptInset();
486 p->removeScript(p->hasUp());
487 // Don't delete if there is anything left
488 if (p->hasUp() || p->hasDown())
496 void MathCursor::delLine()
498 autocorrect_ = false;
506 if (par()->nrows() > 1) {
507 // grid are the only things with more than one row...
508 lyx::Assert(par()->asGridInset());
509 par()->asGridInset()->delRow(hullRow());
512 if (idx() >= par()->nargs())
513 idx() = par()->nargs() - 1;
520 bool MathCursor::up(bool sel)
525 cursor_type save = Cursor_;
529 autocorrect_ = false;
534 bool MathCursor::down(bool sel)
539 cursor_type save = Cursor_;
543 autocorrect_ = false;
548 bool MathCursor::toggleLimits()
552 MathScriptInset * t = nextAtom()->asScriptInset();
555 int old = t->limits();
556 t->limits(old < 0 ? 1 : -1);
557 return old != t->limits();
561 void MathCursor::macroModeClose()
563 MathUnknownInset * p = inMacroMode();
567 string s = p->name();
569 array().erase(pos());
575 string MathCursor::macroName() const
577 return inMacroMode() ? inMacroMode()->name() : string();
581 void MathCursor::selClear()
587 void MathCursor::selCopy()
591 theCutBuffer = grabSelection();
594 theCutBuffer = MathGridInset(1, 1);
599 void MathCursor::selCut()
602 theCutBuffer = grabAndEraseSelection();
606 void MathCursor::selDel()
616 void MathCursor::selPaste()
626 void MathCursor::selHandle(bool sel)
628 if (sel == selection_)
636 void MathCursor::selStart()
646 void MathCursor::selClearOrDel()
648 if (lyxrc.auto_region_delete)
655 void MathCursor::selGet(MathArray & ar)
659 ar = grabSelection().glue();
664 void MathCursor::drawSelection(MathPainterInfo & pain) const
671 getSelection(i1, i2);
673 if (i1.idx_ == i2.idx_) {
674 MathXArray & c = i1.xcell();
675 int x1 = c.xo() + c.pos2x(i1.pos_);
676 int y1 = c.yo() - c.ascent();
677 int x2 = c.xo() + c.pos2x(i2.pos_);
678 int y2 = c.yo() + c.descent();
679 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
681 vector<MathInset::idx_type> indices
682 = i1.par_->idxBetween(i1.idx_, i2.idx_);
683 for (unsigned i = 0; i < indices.size(); ++i) {
684 MathXArray & c = i1.xcell(indices[i]);
686 int y1 = c.yo() - c.ascent();
687 int x2 = c.xo() + c.width();
688 int y2 = c.yo() + c.descent();
689 pain.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
694 // draw anchor if different from selection boundary
695 MathCursorPos anc = Anchor_.back();
696 if (anc != i1 && anc != i2) {
697 MathXArray & c = anc.xcell();
698 int x = c.xo() + c.pos2x(anc.pos_);
699 int y1 = c.yo() - c.ascent();
700 int y2 = c.yo() + c.descent();
701 pain.line(x, y1, x, y2, LColor::math);
707 void MathCursor::handleNest(MathAtom const & at)
709 at->cell(0) = grabAndEraseSelection().glue();
711 pushRight(prevAtom());
715 void MathCursor::getPos(int & x, int & y)
717 par()->getPos(idx(), pos(), x, y);
721 MathInset * MathCursor::par() const
723 return cursor().par_;
727 InsetFormulaBase * MathCursor::formula() const
733 MathCursor::idx_type MathCursor::idx() const
735 return cursor().idx_;
739 MathCursor::idx_type & MathCursor::idx()
741 return cursor().idx_;
745 MathCursor::pos_type MathCursor::pos() const
747 return cursor().pos_;
751 MathCursor::pos_type & MathCursor::pos()
753 return cursor().pos_;
757 MathUnknownInset * MathCursor::inMacroMode() const
761 MathUnknownInset * p = prevAtom()->asUnknownInset();
762 return (p && !p->final()) ? p : 0;
766 bool MathCursor::inMacroArgMode() const
768 return pos() > 0 && prevAtom()->getChar() == '#';
772 bool MathCursor::selection() const
778 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
780 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
781 MathGridInset * p = Cursor_[i].par_->asGridInset();
783 idx = Cursor_[i].idx_;
791 MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
793 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
794 MathHullInset * p = Cursor_[i].par_->asHullInset();
796 idx = Cursor_[i].idx_;
804 void MathCursor::popToEnclosingGrid()
806 while (depth() && !Cursor_.back().par_->asGridInset())
811 void MathCursor::popToEnclosingHull()
813 while (depth() && !Cursor_.back().par_->asHullInset())
818 void MathCursor::pullArg()
821 MathArray a = array();
823 MathScriptInset const * p = par()->asScriptInset();
825 // special handling for scripts
826 const bool up = p->hasUp();
828 MathScriptInset * q = nextAtom()->asScriptInset();
832 array().insert(pos(), a);
838 array().insert(pos(), a);
841 formula()->mutateToText();
846 void MathCursor::touch()
848 cursor_type::const_iterator it = Cursor_.begin();
849 cursor_type::const_iterator et = Cursor_.end();
850 for ( ; it != et; ++it)
855 void MathCursor::normalize()
857 if (idx() >= par()->nargs()) {
858 lyxerr << "this should not really happen - 1: "
859 << idx() << " " << par()->nargs() << "\n";
862 idx() = min(idx(), par()->nargs() - 1);
864 if (pos() > size()) {
865 lyxerr << "this should not really happen - 2: "
866 << pos() << " " << size() << " in idx: " << idx()
868 WriteStream wi(lyxerr, false, true);
873 pos() = min(pos(), size());
875 // remove empty scripts if possible
877 for (pos_type i = 0; i < size(); ++i) {
878 MathScriptInset * p = array()[i]->asScriptInset();
880 p->removeEmptyScripts();
887 // fix again position
888 pos() = min(pos(), size());
892 MathCursor::size_type MathCursor::size() const
894 return array().size();
898 MathCursor::col_type MathCursor::hullCol() const
901 MathHullInset * p = enclosingHull(idx);
906 MathCursor::row_type MathCursor::hullRow() const
909 MathHullInset * p = enclosingHull(idx);
914 bool MathCursor::hasPrevAtom() const
920 bool MathCursor::hasNextAtom() const
922 return pos() < size();
926 MathAtom const & MathCursor::prevAtom() const
928 lyx::Assert(pos() > 0);
929 return array()[pos() - 1];
933 MathAtom & MathCursor::prevAtom()
935 lyx::Assert(pos() > 0);
936 return array()[pos() - 1];
940 MathAtom const & MathCursor::nextAtom() const
942 lyx::Assert(pos() < size());
943 return array()[pos()];
947 MathAtom & MathCursor::nextAtom()
949 lyx::Assert(pos() < size());
950 return array()[pos()];
954 MathArray & MathCursor::array() const
956 static MathArray dummy;
958 if (idx() >= par()->nargs()) {
959 lyxerr << "############ idx_ " << idx() << " not valid\n";
964 lyxerr << "############ depth() == 0 not valid\n";
968 return cursor().cell();
972 MathXArray & MathCursor::xarray() const
974 static MathXArray dummy;
977 lyxerr << "############ depth() == 0 not valid\n";
981 return cursor().xcell();
985 void MathCursor::idxNext()
987 par()->idxNext(idx(), pos());
991 void MathCursor::idxPrev()
993 par()->idxPrev(idx(), pos());
997 void MathCursor::splitCell()
999 if (idx() + 1 == par()->nargs())
1001 MathArray ar = array();
1003 array().erase(pos(), size());
1006 array().insert(0, ar);
1010 void MathCursor::breakLine()
1012 // leave inner cells
1017 MathHullInset * p = enclosingHull(dummy);
1021 if (p->getType() == "simple" || p->getType() == "equation") {
1022 p->mutate("eqnarray");
1026 p->addRow(hullRow());
1029 const row_type r = hullRow();
1030 for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
1031 p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
1035 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1040 //void MathCursor::readLine(MathArray & ar) const
1042 // idx_type base = row() * par()->ncols();
1043 // for (idx_type off = 0; off < par()->ncols(); ++off)
1044 // ar.push_back(par()->cell(base + off));
1048 char MathCursor::valign() const
1051 MathGridInset * p = enclosingGrid(idx);
1052 return p ? p->valign() : '\0';
1056 char MathCursor::halign() const
1059 MathGridInset * p = enclosingGrid(idx);
1060 return p ? p->halign(idx % p->ncols()) : '\0';
1064 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1066 MathCursorPos anc = normalAnchor();
1067 if (anc < cursor()) {
1077 MathCursorPos & MathCursor::cursor()
1079 lyx::Assert(depth());
1080 return Cursor_.back();
1084 MathCursorPos const & MathCursor::cursor() const
1086 lyx::Assert(depth());
1087 return Cursor_.back();
1091 bool MathCursor::goUpDown(bool up)
1093 // Be warned: The 'logic' implemented in this function is highly fragile.
1094 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1095 // So fiddle around with it only if you know what you are doing!
1096 int xlow, xhigh, ylow, yhigh;
1101 // check if we had something else in mind, if not, this is the future goal
1107 // try neigbouring script insets
1109 if (hasPrevAtom()) {
1110 MathScriptInset * p = prevAtom()->asScriptInset();
1111 if (p && p->has(up)) {
1114 idx() = up; // the superscript has index 1
1116 ///lyxerr << "updown: handled by scriptinset to the left\n";
1122 if (hasNextAtom()) {
1123 MathScriptInset * p = nextAtom()->asScriptInset();
1124 if (p && p->has(up)) {
1128 ///lyxerr << "updown: handled by scriptinset to the right\n";
1134 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1139 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1140 // lyxerr << "updown: handled by brute find in the same cell\n";
1144 // try to find an inset that knows better then we
1146 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1148 if (par()->idxUpDown(idx(), pos(), up)) {
1149 #ifdef WITH_WARNINGS
1150 #warning this code should be moved to individual insets that handle this
1152 // position might have changed, so re-compute it
1154 // we found a cell that thinks it has something "below" us.
1155 //lyxerr << "updown: found inset that handles UpDown\n";
1156 xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1157 // project (xo,yo) onto proper box
1158 //lyxerr << "\n xo: " << xo << " yo: " << yo
1159 // << "\n xlow: " << xlow << " ylow: " << ylow
1160 // << "\n xhigh: " << xhigh << " yhigh: " << yhigh;
1161 xo = min(max(xo, xlow), xhigh);
1162 yo = min(max(yo, ylow), yhigh);
1163 //lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n";
1164 bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
1165 //lyxerr << "updown: handled by final brute find\n";
1171 // no such inset found, just take something "above"
1172 ///lyxerr << "updown: handled by strange case\n";
1177 up ? formula()->ylow() : yo + 4,
1178 up ? yo - 4 : formula()->yhigh()
1182 // any improvement so far?
1185 if (up ? ynew < yo : ynew > yo)
1191 bool MathCursor::bruteFind
1192 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1194 cursor_type best_cursor;
1195 double best_dist = 1e10;
1197 MathIterator it = ibegin(formula()->par().nucleus());
1198 MathIterator et = iend(formula()->par().nucleus());
1200 // avoid invalid nesting when selecting
1201 if (!selection_ || positionable(it.cursor(), Anchor_)) {
1203 it.position().getPos(xo, yo);
1204 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1205 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1206 // '<=' in order to take the last possible position
1207 // this is important for clicking behind \sum in e.g. '\sum_i a'
1208 if (d <= best_dist) {
1210 best_cursor = it.cursor();
1220 if (best_dist < 1e10)
1221 Cursor_ = best_cursor;
1222 return best_dist < 1e10;
1226 bool MathCursor::idxLineFirst()
1228 idx() -= idx() % par()->ncols();
1234 bool MathCursor::idxLineLast()
1236 idx() -= idx() % par()->ncols();
1237 idx() += par()->ncols() - 1;
1242 bool MathCursor::idxLeft()
1244 return par()->idxLeft(idx(), pos());
1248 bool MathCursor::idxRight()
1250 return par()->idxRight(idx(), pos());
1254 bool MathCursor::interpret(string const & s)
1256 //lyxerr << "interpret 1: '" << s << "'\n";
1260 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1261 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1262 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1264 if (s.size() >= 5 && s.substr(0, 5) == "cases") {
1266 istringstream is(s.substr(5).c_str());
1269 niceInsert(MathAtom(new MathCasesInset(n)));
1273 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1278 istringstream is(s.substr(6).c_str());
1279 is >> m >> n >> v_align >> h_align;
1283 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1287 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1289 istringstream is(s.substr(7).c_str());
1292 mathed_parse_cell(rep.from, from);
1293 mathed_parse_cell(rep.to, to);
1294 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1295 par()->replace(rep);
1299 string name = s.substr(1);
1301 if (name == "over" || name == "choose" || name == "atop") {
1302 MathArray ar = array();
1303 MathAtom t(createMathInset(name));
1304 t->asNestInset()->cell(0).swap(array());
1312 // prevent entering of recursive macros
1313 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1314 && formula()->getInsetName() == name)
1316 lyxerr << "can't enter recursive macro\n";
1320 niceInsert(createMathInset(name));
1325 bool MathCursor::script(bool up)
1327 // Hack to get \\^ and \\_ working
1328 if (inMacroMode() && macroName() == "\\") {
1330 interpret("\\mathcircumflex");
1337 MathGridInset safe = grabAndEraseSelection();
1338 if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1339 prevAtom()->asScriptInset()->ensure(up);
1340 pushRight(prevAtom());
1343 } else if (hasNextAtom() && nextAtom()->asScriptInset()) {
1344 nextAtom()->asScriptInset()->ensure(up);
1345 pushLeft(nextAtom());
1349 plainInsert(MathAtom(new MathScriptInset(up)));
1350 prevAtom()->asScriptInset()->ensure(up);
1351 pushRight(prevAtom());
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 if necessary
1413 // This is annoying as one has to press <space> far too often.
1417 // leave autocorrect mode if necessary
1418 if (autocorrect_ && c == ' ') {
1419 autocorrect_ = false;
1424 // just clear selection on pressing the space bar
1425 if (selection_ && c == ' ') {
1433 //lyxerr << "starting with macro\n";
1434 insert(MathAtom(new MathUnknownInset("\\", false)));
1439 if (currentMode() == MathInset::TEXT_MODE) {
1440 // insert spaces in text mode,
1441 // but suppress direct insertion of two spaces in a row
1442 // the still allows typing '<space>a<space>' and deleting the 'a', but
1443 // it is better than nothing...
1444 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1448 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1449 prevAtom()->asSpaceInset()->incSpace();
1454 // if are at the very end, leave the formula
1455 return pos() != size();
1459 insert(c); // LM_TC_TEX;
1464 if (c == '{' || c == '}', c)) {
1465 insert(c); // LM_TC_TEX;
1471 niceInsert(MathAtom(new MathBraceInset));
1480 insert(createMathInset("$"));
1485 insert(createMathInset("%"));
1490 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1491 insert(c, LM_TC_VAR);
1495 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1496 insert(c, LM_TC_VAR);
1497 lastcode_ = LM_TC_VAR;
1502 insert(c, LM_TC_TEX);
1503 //bv->owner()->message(_("TeX mode"));
1508 // try auto-correction
1509 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1512 // no special circumstances, so insert the character without any fuss
1514 autocorrect_ = true;
1519 void MathCursor::setSelection(cursor_type const & where, size_type n)
1528 void MathCursor::insetToggle()
1530 if (hasNextAtom()) {
1531 // toggle next inset ...
1532 nextAtom()->lock(!nextAtom()->lock());
1533 } else if (popLeft() && hasNextAtom()) {
1534 // ... or enclosing inset if we are in the last inset position
1535 nextAtom()->lock(!nextAtom()->lock());
1541 string MathCursor::info() const
1544 os << "Math editor mode. ";
1545 for (int i = 0, n = depth(); i < n; ++i) {
1546 Cursor_[i].par_->infoize(os);
1550 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1551 prevAtom()->infoize(os);
1553 return os.str().c_str(); // .c_str() needed for lyxstring
1557 unsigned MathCursor::depth() const
1559 return Cursor_.size();
1567 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1568 MathInset::row_type & r1, MathInset::row_type & r2,
1569 MathInset::col_type & c1, MathInset::col_type & c2)
1571 MathInset * p = i1.par_;
1572 c1 = p->col(i1.idx_);
1573 c2 = p->col(i2.idx_);
1576 r1 = p->row(i1.idx_);
1577 r2 = p->row(i2.idx_);
1585 MathGridInset MathCursor::grabSelection() const
1588 return MathGridInset();
1591 getSelection(i1, i2);
1592 // shouldn't we assert on i1.par_ == i2.par_?
1593 if (i1.idx_ == i2.idx_) {
1594 MathGridInset data(1, 1);
1595 data.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
1600 region(i1, i2, r1, r2, c1, c2);
1601 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1602 for (row_type row = 0; row < data.nrows(); ++row)
1603 for (col_type col = 0; col < data.ncols(); ++col) {
1604 idx_type i = i1.par_->index(row + r1, col + c1);
1605 data.cell(data.index(row, col)) = i1.par_->cell(i);
1611 void MathCursor::eraseSelection()
1615 getSelection(i1, i2);
1616 if (i1.idx_ == i2.idx_)
1617 i1.cell().erase(i1.pos_, i2.pos_);
1619 MathInset * p = i1.par_;
1622 region(i1, i2, r1, r2, c1, c2);
1623 for (row_type row = r1; row <= r2; ++row)
1624 for (col_type col = c1; col <= c2; ++col)
1625 p->cell(p->index(row, col)).erase();
1631 MathGridInset MathCursor::grabAndEraseSelection()
1634 return MathGridInset();
1635 MathGridInset res = grabSelection();
1642 MathCursorPos MathCursor::normalAnchor() const
1644 if (Anchor_.size() < depth()) {
1646 lyxerr << "unusual Anchor size\n";
1648 //lyx::Assert(Anchor_.size() >= cursor.depth());
1649 // use Anchor on the same level as Cursor
1650 MathCursorPos normal = Anchor_[depth() - 1];
1651 if (depth() < Anchor_.size() && !(normal < cursor())) {
1652 // anchor is behind cursor -> move anchor behind the inset
1660 void MathCursor::handleExtern(const string & arg)
1664 istringstream iss(arg.c_str());
1665 iss >> lang >> extra;
1673 lyxerr << "use selection: " << ar << "\n";
1674 insert(pipeThroughExtern(lang, extra, ar));
1679 eq.push_back(MathAtom(new MathCharInset('=')));
1681 popToEnclosingHull();
1684 MathHullInset * hull = enclosingHull(idx);
1688 if (hull->getType() == "simple") {
1689 MathArray::size_type pos = cursor().cell().find_last(eq);
1691 if (pos == size()) {
1693 lyxerr << "use whole cell: " << ar << "\n";
1695 ar = MathArray(array(), pos + 1, size());
1696 lyxerr << "use partial cell form pos: " << pos << "\n";
1700 insert(pipeThroughExtern(lang, extra, ar));
1704 if (hull->getType() == "equation") {
1705 lyxerr << "use equation inset\n";
1706 hull->mutate("eqnarray");
1707 MathArray & ar = cursor().cell();
1708 lyxerr << "use cell: " << ar << "\n";
1710 cursor().cell() = eq;
1712 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1718 lyxerr << "use eqnarray\n";
1720 MathArray ar = cursor().cell();
1721 lyxerr << "use cell: " << ar << "\n";
1724 cursor().cell() = eq;
1726 cursor().cell() = pipeThroughExtern(lang, extra, ar);
1733 int MathCursor::dispatch(string const & cmd)
1735 // try to dispatch to adajcent items if they are not editable
1736 // actually, this should only happen for mouse clicks...
1737 if (hasNextAtom() && !openable(nextAtom(), false))
1738 if (int res = nextAtom()->dispatch(cmd, 0, 0))
1740 if (hasPrevAtom() && !openable(prevAtom(), false))
1741 if (int res = prevAtom()->dispatch(cmd, 0, 0))
1744 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1745 MathCursorPos & pos = Cursor_[i];
1746 if (int res = pos.par_->dispatch(cmd, pos.idx_, pos.pos_))
1753 MathInset::mode_type MathCursor::currentMode() const
1755 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1756 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1757 if (res != MathInset::UNDECIDED_MODE)
1760 return MathInset::UNDECIDED_MODE;
1764 void releaseMathCursor(BufferView * bv)
1768 mathcursor->formula()->hideInsetCursor(bv);