]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
updates to latexfeatures stuff; allow empty \document_path
[lyx.git] / src / mathed / math_cursor.C
index f0ef8c9c1ed6fb1e792773ecffb127684e31c4bb..e54922cae0898837ea7d06d86854bba7785278ca 100644 (file)
  *   the GNU General Public Licence version 2 or later.
  */
 
+#include <config.h>
+
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#include <config.h>
-#include <algorithm>
-#include <cctype>
-
 #include "support/lstrings.h"
 #include "support/LAssert.h"
 #include "debug.h"
 #include "LColor.h"
 #include "Painter.h"
+#include "math_cursor.h"
 #include "formulabase.h"
 #include "math_arrayinset.h"
 #include "math_braceinset.h"
+#include "math_boxinset.h"
 #include "math_casesinset.h"
 #include "math_charinset.h"
-#include "math_cursor.h"
 #include "math_deliminset.h"
 #include "math_factory.h"
 #include "math_hullinset.h"
@@ -46,6 +45,9 @@
 #include "math_specialcharinset.h"
 #include "math_support.h"
 
+#include <algorithm>
+#include <cctype>
+
 #define FILEDEBUG 0
 
 using std::endl;
@@ -122,16 +124,22 @@ struct Selection
        }
 
        void paste(MathCursor & cursor) const
-       {       
-               idx_type idx;
-               MathGridInset * p = cursor.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).push_back(data_.cell(data_.index(row, col)));
-                       }
+       {
+               if (data_.nargs() == 1) {
+                       // single cell/part of cell
+                       cursor.insert(data_.cell(0));
+               } else {
+                       // mulitple cells
+                       idx_type idx;
+                       MathGridInset * p = cursor.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).push_back(data_.cell(data_.index(row, col)));
+                               }
+               }
        }
 
        // glues selection to one cell
@@ -160,7 +168,7 @@ Selection theSelection;
 
 
 MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
-       : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
+       : formula_(formula), lastcode_(LM_TC_MIN), selection_(false)
 {
        left ? first() : last();
 }
@@ -292,11 +300,11 @@ bool MathCursor::left(bool sel)
        dump("Left 1");
        if (inMacroMode()) {
                macroModeClose();
-               lastcode_ = LM_TC_VAR;
+               lastcode_ = LM_TC_MIN;
                return true;
        }
        selHandle(sel);
-       lastcode_ = LM_TC_VAR;
+       lastcode_ = LM_TC_MIN;
 
        if (hasPrevAtom() && openable(prevAtom(), sel)) {
                if (prevAtom()->isHyperActive()) {
@@ -315,11 +323,11 @@ bool MathCursor::right(bool sel)
        dump("Right 1");
        if (inMacroMode()) {
                macroModeClose();
-               lastcode_ = LM_TC_VAR;
+               lastcode_ = LM_TC_MIN;
                return true;
        }
        selHandle(sel);
-       lastcode_ = LM_TC_VAR;
+       lastcode_ = LM_TC_MIN;
 
        if (hasNextAtom() && openable(nextAtom(), sel)) {
                if (nextAtom()->isHyperActive()) {
@@ -369,11 +377,15 @@ bool positionable(MathCursor::cursor_type const & cursor,
 
 void MathCursor::setPos(int x, int y)
 {
-       dump("setPos 2");
+       dump("setPos 1");
        bool res = bruteFind(x, y,
                formula()->xlow(), formula()->xhigh(),
                formula()->ylow(), formula()->yhigh());
-       lyx::Assert(res);
+       if (!res) {
+               // this ccan happen on creation of "math-display"
+               dump("setPos 1.5");
+               first();
+       }
        dump("setPos 2");
 }
 
@@ -384,7 +396,7 @@ void MathCursor::home(bool sel)
        dump("home 1");
        selHandle(sel);
        macroModeClose();
-       lastcode_ = LM_TC_VAR;
+       lastcode_ = LM_TC_MIN;
        if (!par()->idxHome(idx(), pos())) 
                popLeft();
        dump("home 2");
@@ -396,7 +408,7 @@ void MathCursor::end(bool sel)
        dump("end 1");
        selHandle(sel);
        macroModeClose();
-       lastcode_ = LM_TC_VAR;
+       lastcode_ = LM_TC_MIN;
        if (!par()->idxEnd(idx(), pos()))
                popRight();
        dump("end 2");
@@ -605,9 +617,9 @@ void MathCursor::macroModeClose()
 }
 
 
-int MathCursor::macroNamePos() const
+MathInset::difference_type MathCursor::macroNamePos() const
 {
-       for (int i = pos() - 1; i >= 0; --i) { 
+       for (MathInset::difference_type i = pos() - 1; i >= 0; --i) { 
                MathAtom & p = array().at(i);
                if (p->code() == LM_TC_TEX && p->getChar() == '\\')
                        return i;
@@ -619,7 +631,8 @@ int MathCursor::macroNamePos() const
 string MathCursor::macroName() const
 {
        string s;
-       for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i) 
+       MathInset::difference_type i = macroNamePos(); 
+       for ( ; i >= 0 && i < int(pos()); ++i) 
                s += array().at(i)->getChar();
        return s;
 }
@@ -846,7 +859,7 @@ bool MathCursor::selection() const
 
 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
 {
-       for (int i = Cursor_.size() - 1; i >= 0; --i) {
+       for (MathInset::difference_type i = Cursor_.size() - 1; i >= 0; --i) {
                MathGridInset * p = Cursor_[i].par_->asGridInset();
                if (p) {
                        idx = Cursor_[i].idx_;
@@ -884,30 +897,34 @@ void MathCursor::pullArg(bool goright)
 }
 
 
-void MathCursor::normalize() const
+void MathCursor::normalize()
 {
-#ifdef WITH_WARNINGS
-#warning This is evil!
-#endif
-       MathCursor * it = const_cast<MathCursor *>(this);
+       // rebreak
+       {
+               MathIterator it = ibegin(formula()->par().nucleus());
+               MathIterator et = iend(formula()->par().nucleus());
+               for ( ; it != et; ++it) 
+                       if (it.par()->asBoxInset())
+                               it.par()->asBoxInset()->rebreak();
+       }
 
        if (idx() >= par()->nargs()) {
                lyxerr << "this should not really happen - 1: "
                       << idx() << " " << par()->nargs() << "\n";
                dump("error 2");
        }
-       it->idx() = min(idx(), par()->nargs() - 1);
+       idx() = min(idx(), par()->nargs() - 1);
 
        if (pos() > size()) {
                lyxerr << "this should not really happen - 2: "
-                       << pos() << " " << size() <<  " in idx: " << it->idx()
+                       << pos() << " " << size() <<  " in idx: " << idx()
                        << " in atom: '";
                WriteStream wi(lyxerr, false);
-               it->par()->write(wi);
+               par()->write(wi);
                lyxerr << "\n";
                dump("error 4");
        }
-       it->pos() = min(pos(), size());
+       pos() = min(pos(), size());
 }
 
 
@@ -1240,7 +1257,7 @@ bool MathCursor::interpret(string const & s)
 
        if (s == "\\over" || s == "\\choose" || s == "\\atop") {
                MathArray ar = array();
-               MathAtom t = createMathInset(s.substr(1));
+               MathAtom t(createMathInset(s.substr(1)));
                t->asNestInset()->cell(0).swap(array());
                pos() = 0;
                niceInsert(t);
@@ -1259,6 +1276,7 @@ bool MathCursor::interpret(string const & s)
        return true;
 }
 
+
 bool MathCursor::script(bool up)
 {
        macroModeClose();
@@ -1288,7 +1306,9 @@ bool MathCursor::script(bool up)
 
 bool MathCursor::interpret(char c)
 {
-       //lyxerr << "interpret 2: '" << c << "'\n";
+
+       // Removed super/subscript handling from here  to ::script -MV
+
        // handle macroMode
        if (inMacroMode()) {
                string name = macroName();
@@ -1332,7 +1352,7 @@ bool MathCursor::interpret(char c)
                // fall through in the other cases
        }
 
-       if (lastcode_ == LM_TC_TEXTRM) {
+       if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
                // suppress direct insertion of two spaces in a row
                // the still allows typing  '<space>a<space>' and deleting the 'a', but
                // it is better than nothing...
@@ -1347,7 +1367,7 @@ bool MathCursor::interpret(char c)
                        prevAtom()->asSpaceInset()->incSpace();
                        return true;
                }
-               if (mathcursor->popRight())
+               if (popRight())
                        return true;
                // if are at the very end, leave the formula
                return pos() != size();
@@ -1393,7 +1413,7 @@ bool MathCursor::interpret(char c)
        }
 
        // no special circumstances, so insert the character without any fuss
-       insert(c, lastcode_);
+       insert(c, lastcode_ == LM_TC_MIN ? MathCharInset::nativeCode(c) : lastcode_);
        lastcode_ = LM_TC_MIN;
        return true;
 }
@@ -1433,3 +1453,10 @@ void MathCursor::stripFromLastEqualSign()
 }
 
 
+void MathCursor::setSelection(cursor_type const & where, size_type n)
+{
+       selection_ = true;
+       Anchor_ = where;
+       Cursor_ = where;
+       cursor().pos_ += n;
+}