]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
try to correct x position where curosr drops of inset.
[lyx.git] / src / mathed / math_cursor.C
index 77d3f450411fba263014e4eead90979a3c9f296a..6a371a2a9b121baa0c54a7a1e87c38fa037328db 100644 (file)
 #include "frontends/Painter.h"
 #include "math_cursor.h"
 #include "formulabase.h"
+#include "funcrequest.h"
 #include "math_autocorrect.h"
 #include "math_arrayinset.h"
 #include "math_braceinset.h"
-#include "math_casesinset.h"
+#include "math_commentinset.h"
 #include "math_charinset.h"
 #include "math_extern.h"
 #include "math_factory.h"
@@ -91,7 +92,7 @@ void MathCursor::push(MathAtom & t)
 
 void MathCursor::pushLeft(MathAtom & t)
 {
-       //cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
+       //lyxerr << "Entering atom " << t << " left\n";
        push(t);
        t->idxFirst(idx(), pos());
 }
@@ -99,7 +100,7 @@ void MathCursor::pushLeft(MathAtom & t)
 
 void MathCursor::pushRight(MathAtom & t)
 {
-       //cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
+       //lyxerr << "Entering atom " << t << " right\n";
        posLeft();
        push(t);
        t->idxLast(idx(), pos());
@@ -411,31 +412,10 @@ void MathCursor::paste(MathArray const & ar)
 
 void MathCursor::paste(MathGridInset const & data)
 {
-       if (data.nargs() == 1) {
-               // single cell/part of cell
-               paste(data.cell(0));
-       } else {
-               // multiple cells
-               idx_type idx; // index of upper left cell
-               MathGridInset * p = enclosingGrid(idx);
-               col_type const numcols = min(data.ncols(), p->ncols() - p->col(idx));
-               row_type const numrows = min(data.nrows(), p->nrows() - p->row(idx));
-               for (row_type row = 0; row < numrows; ++row) {
-                       for (col_type col = 0; col < numcols; ++col) {
-                               idx_type i = p->index(row + p->row(idx), col + p->col(idx));
-                               p->cell(i).append(data.cell(data.index(row, col)));
-                       }
-                       // append the left over horizontal cells to the last column
-                       idx_type i = p->index(row + p->row(idx), p->ncols() - 1);
-                       for (MathInset::col_type col = numcols; col < data.ncols(); ++col)
-                               p->cell(i).append(data.cell(data.index(row, col)));
-               }
-               // append the left over vertical cells to the last _cell_
-               idx_type i = p->nargs() - 1;
-               for (row_type row = numrows; row < data.nrows(); ++row)
-                       for (col_type col = 0; col < data.ncols(); ++col)
-                               p->cell(i).append(data.cell(data.index(row, col)));
-       }
+       ostringstream os;
+  WriteStream wi(os, false, false);
+  data.write(wi);
+       dispatch(FuncRequest(LFUN_PASTE, os.str()));
 }
 
 
@@ -452,6 +432,14 @@ void MathCursor::backspace()
                return;
        }
 
+       if (inMacroMode()) {
+               MathUnknownInset * p = activeMacro();
+               if (p->name().size() > 1) {
+                       p->setName(p->name().substr(0, p->name().size() - 1));
+                       return;
+               }
+       }
+
 /*
        if (prevAtom()->asScriptInset()) {
                // simply enter nucleus
@@ -682,12 +670,22 @@ void MathCursor::handleNest(MathAtom const & a)
 }
 
 
-void MathCursor::getPos(int & x, int & y)
+void MathCursor::getPos(int & x, int & y) const
 {
        par()->getPos(idx(), pos(), x, y);
 }
 
 
+int MathCursor::targetX() const
+{
+       if (targetx_ != -1)
+               return targetx_;
+       int x = 0, y = 0;
+       getPos(x, y);
+       return x;
+}
+
+
 MathInset * MathCursor::par() const
 {
        return cursor().par_;
@@ -770,19 +768,6 @@ MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
 }
 
 
-MathHullInset * MathCursor::enclosingHull(MathCursor::idx_type & idx) const
-{
-       for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
-               MathHullInset * p = Cursor_[i].par_->asHullInset();
-               if (p) {
-                       idx = Cursor_[i].idx_;
-                       return p;
-               }
-       }
-       return 0;
-}
-
-
 void MathCursor::popToHere(MathInset const * p)
 {
        while (depth() && Cursor_.back().par_ != p)
@@ -870,22 +855,6 @@ MathCursor::size_type MathCursor::size() const
 }
 
 
-MathCursor::col_type MathCursor::hullCol() const
-{
-       idx_type idx = 0;
-       MathHullInset * p = enclosingHull(idx);
-       return p->col(idx);
-}
-
-
-MathCursor::row_type MathCursor::hullRow() const
-{
-       idx_type idx = 0;
-       MathHullInset * p = enclosingHull(idx);
-       return p->row(idx);
-}
-
-
 bool MathCursor::hasPrevAtom() const
 {
        return pos() > 0;
@@ -1113,14 +1082,6 @@ bool MathCursor::bruteFind
 }
 
 
-bool MathCursor::idxLineFirst()
-{
-       idx() -= idx() % par()->ncols();
-       pos() = 0;
-       return true;
-}
-
-
 bool MathCursor::idxLineLast()
 {
        idx() -= idx() % par()->ncols();
@@ -1151,15 +1112,6 @@ bool MathCursor::interpret(string const & s)
        //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
        //lyxerr << "trans: '" << s[0] << "'  int: " << int(s[0]) << endl;
 
-       if (s.size() >= 5 && s.substr(0, 5) == "cases") {
-               unsigned int n = 1;
-               istringstream is(s.substr(5).c_str());
-               is >> n;
-               n = max(1u, n);
-               niceInsert(MathAtom(new MathCasesInset(n)));
-               return true;
-       }
-
        if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
                unsigned int m = 1;
                unsigned int n = 1;
@@ -1217,7 +1169,7 @@ bool MathCursor::script(bool up)
        // Hack to get \\^ and \\_ working
        if (inMacroMode() && macroName() == "\\") {
                if (up)
-                       interpret("\\mathcircumflex");
+                       niceInsert(createMathInset("mathcircumflex"));
                else
                        interpret('_');
                return true;
@@ -1277,12 +1229,6 @@ bool MathCursor::interpret(char c)
                string name = macroName();
                //lyxerr << "interpret name: '" << name << "'\n";
 
-               if (name.empty() && c == '\\') {
-                       backspace();
-                       interpret("\\backslash");
-                       return true;
-               }
-
                if (isalpha(c)) {
                        activeMacro()->setName(activeMacro()->name() + c);
                        return true;
@@ -1292,10 +1238,13 @@ bool MathCursor::interpret(char c)
                if (name == "\\") {
                        // remove the '\\'
                        backspace();
-                       if (c == '\\')
-                               interpret("\\backslash");
-                       else
-                               interpret(string("\\") + c);
+                       if (c == '\\') {
+                               if (currentMode() == MathInset::TEXT_MODE)
+                                       niceInsert(createMathInset("textbackslash"));
+                               else
+                                       niceInsert(createMathInset("backslash"));
+                       } else
+                               niceInsert(createMathInset(string(1, c)));
                        return true;
                }
 
@@ -1384,7 +1333,7 @@ bool MathCursor::interpret(char c)
        }
 
        if (c == '%') {
-               insert(createMathInset("%"));
+               niceInsert(MathAtom(new MathCommentInset));
                return true;
        }
 
@@ -1559,82 +1508,6 @@ MathCursorPos MathCursor::normalAnchor() const
 }
 
 
-
-void MathCursor::handleExtern(const string & arg)
-{
-       string lang;
-       string extra;
-       istringstream iss(arg.c_str());
-       iss >> lang >> extra;
-       if (extra.empty())
-               extra = "noextra";
-
-       if (selection()) {
-               MathArray ar;
-               selGet(ar);
-               lyxerr << "use selection: " << ar << "\n";
-               insert(pipeThroughExtern(lang, extra, ar));
-               return;
-       }
-
-       MathArray eq;
-       eq.push_back(MathAtom(new MathCharInset('=')));
-
-       popToEnclosingHull();
-
-       idx_type idx = 0;
-       MathHullInset * hull = enclosingHull(idx);
-       lyx::Assert(hull);
-       idxLineFirst();
-
-       if (hull->getType() == "simple") {
-               MathArray::size_type pos = cursor().cell().find_last(eq);
-               MathArray ar;
-               if (pos == size()) {
-                       ar = array();
-                       lyxerr << "use whole cell: " << ar << "\n";
-               } else {
-                       ar = MathArray(array().begin() + pos + 1, array().end());
-                       lyxerr << "use partial cell form pos: " << pos << "\n";
-               }
-               end();
-               insert(eq);
-               insert(pipeThroughExtern(lang, extra, ar));
-               return;
-       }
-
-       if (hull->getType() == "equation") {
-               lyxerr << "use equation inset\n";
-               hull->mutate("eqnarray");
-               MathArray & ar = cursor().cell();
-               lyxerr << "use cell: " << ar << "\n";
-               idxRight();
-               cursor().cell() = eq;
-               idxRight();
-               cursor().cell() = pipeThroughExtern(lang, extra, ar);
-               idxLineLast();
-               return;
-       }
-
-       {
-               lyxerr << "use eqnarray\n";
-               idxLineLast();
-               MathArray ar = cursor().cell();
-               lyxerr << "use cell: " << ar << "\n";
-#ifdef WITH_WARNINGS
-#warning temporarily disabled
-#endif
-               //breakLine();
-               //idxRight();
-               cursor().cell() = eq;
-               //idxRight();
-               cursor().cell() = pipeThroughExtern(lang, extra, ar);
-               idxLineLast();
-       }
-
-}
-
-
 MathInset::result_type MathCursor::dispatch(FuncRequest const & cmd)
 {
        // try to dispatch to adajcent items if they are not editable