On Wed, Jun 29, 2005 at 10:46:18AM +0300, Martin Vermeer wrote:

...
 
> Here is the patch, armed with comments for posterity.

And here a better one.

- Martin
 
Index: cursor_slice.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor_slice.h,v
retrieving revision 1.21
diff -u -p -r1.21 cursor_slice.h
--- cursor_slice.h      31 Jan 2005 16:29:38 -0000      1.21
+++ cursor_slice.h      29 Jun 2005 19:39:53 -0000
@@ -62,6 +62,10 @@ public:
        idx_type idx() const { return idx_; }
        /// return the cell this cursor is in
        idx_type & idx() { return idx_; }
+       /// Save current cursor idx as row, col
+       void idxSave() { col_ = idx_ % ncols(); row_ = idx_ / ncols(); }
+       /// update idx to correspond to row, col
+       void idxLoad() { idx_ = col_ + ncols() * row_; }
        /// return the last cell in this inset
        idx_type lastidx() const { return nargs() - 1; }
        /// return the offset of the paragraph this cursor is in
@@ -70,7 +74,7 @@ public:
        pit_type & pit() { return pit_; }
        /// increments the paragraph this cursor is in
        void incrementPar();
-       /// increments the paragraph this cursor is in
+       /// decrements the paragraph this cursor is in
        void decrementPar();
        /// return the position within the paragraph
        pos_type pos() const { return pos_; }
@@ -80,13 +84,13 @@ public:
        pos_type lastpos() const;
        /// return the number of embedded cells
        size_t nargs() const;
-       /// return the number of embedded cells
+       /// return the number of columns
        size_t ncols() const;
-       /// return the number of embedded cells
+       /// return the number of rows
        size_t nrows() const;
        /// return the grid row of the current cell
        row_type row() const;
-       /// return the grid row of the current cell
+       /// return the grid column of the current cell
        col_type col() const;
 
        ///
@@ -123,6 +127,10 @@ public:
 private:
        /// cell index of a position in this inset
        idx_type idx_;
+       /// row position in inset
+       row_type row_;
+       /// column position in inset
+       col_type col_;
        /// paragraph in this cell (used by texted)
        pit_type pit_;
        /// true of 'pit' was properly initialized
Index: dociterator.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.h,v
retrieving revision 1.17
diff -u -p -r1.17 dociterator.h
--- dociterator.h       14 Feb 2005 08:17:23 -0000      1.17
+++ dociterator.h       29 Jun 2005 19:39:53 -0000
@@ -91,6 +91,10 @@ public:
        idx_type idx() const { return top().idx(); }
        /// return the cell of the inset this cursor is in
        idx_type & idx() { return top().idx(); }
+       ///
+       void idxSave() { top().idxSave(); }
+       ///
+       void idxLoad() { top().idxLoad(); }
        /// return the last possible cell in this inset
        idx_type lastidx() const;
        /// return the paragraph this cursor is in
Index: mathed/math_gridinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_gridinset.C,v
retrieving revision 1.162
diff -u -p -r1.162 math_gridinset.C
--- mathed/math_gridinset.C     26 Apr 2005 11:12:20 -0000      1.162
+++ mathed/math_gridinset.C     29 Jun 2005 19:39:53 -0000
@@ -1108,11 +1108,20 @@ void MathGridInset::doDispatch(LCursor &
                                if (cur.idx() > nargs())
                                        cur.idx() -= ncols();
                        }
-               else if (s == "copy-row")
+               else if (s == "copy-row") {
+                       // Here (as later) we save the cursor col/row 
+                       // in order to restore it after operation. 
+                       cur.idxSave();
                        for (int i = 0, n = extractInt(is); i < n; ++i)
                                copyRow(cur.row());
-               else if (s == "swap-row")
+                       cur.idxLoad();
+                       }
+               else if (s == "swap-row") {
                        swapRow(cur.row());
+                       // Trick to suppress same-idx-means-different-cell 
+                       // assertion crash:
+                       cur.pos() = 0; 
+                       }
                else if (s == "add-hline-above")
                        rowinfo_[cur.row()].lines_++;
                else if (s == "add-hline-below")
@@ -1123,24 +1132,27 @@ void MathGridInset::doDispatch(LCursor &
                        rowinfo_[cur.row()+1].lines_--;
                else if (s == "append-column")
                        for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type const r = cur.row();
-                               col_type const c = cur.col();
-                               addCol(c);
-                               cur.idx() = index(r, c);
+                               cur.idxSave();
+                               addCol(cur.col());
+                               cur.idxLoad();
                        }
                else if (s == "delete-column")
                        for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type const r = cur.row();
-                               col_type const c = cur.col();
+                               cur.idxSave();
                                delCol(col(cur.idx()));
-                               cur.idx() = index(r, c);
+                               cur.idxLoad();
                                if (cur.idx() > nargs())
                                        cur.idx() -= ncols();
                        }
-               else if (s == "copy-column")
+               else if (s == "copy-column") {
+                       cur.idxSave();
                        copyCol(cur.col());
-               else if (s == "swap-column")
+                       cur.idxLoad();
+                       }
+               else if (s == "swap-column") {
                        swapCol(cur.col());
+                       cur.pos() = 0; // trick, see above
+                       }
                else if (s == "add-vline-left")
                        colinfo_[cur.col()].lines_++;
                else if (s == "add-vline-right")

Attachment: pgpf4tQOUiapB.pgp
Description: PGP signature

Reply via email to