From 6d75800f5d23f5b051302cb5ff49940748fed4c1 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 25 Mar 2012 11:16:32 +0200 Subject: [PATCH] Fix bug #6936. If \hline is entered, do not create an unknown inset, but increase the number of hlines of the current row if that is allowed. The same idea is applied to copy-paste (not part of the bug report). This is also a test for committing via git. --- src/mathed/InsetMathGrid.cpp | 51 +++++++++++++++++++++++++++++++++++- src/mathed/InsetMathGrid.h | 2 ++ src/mathed/InsetMathNest.cpp | 2 +- src/mathed/InsetMathNest.h | 2 +- src/tex2lyx/table.cpp | 2 +- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp index 5b58f09a03..ed77f71f85 100644 --- a/src/mathed/InsetMathGrid.cpp +++ b/src/mathed/InsetMathGrid.cpp @@ -13,6 +13,7 @@ #include "InsetMathGrid.h" +#include "InsetMathUnknown.h" #include "MathData.h" #include "MathParser.h" #include "MathStream.h" @@ -176,6 +177,22 @@ void InsetMathGrid::setDefaults() } +bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str) +{ + if (str == "\\hline") { + FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above"); + FuncStatus status; + if (getStatus(cur, fr, status)) { + if (status.enabled()) { + rowinfo_[cur.row()].lines_++; + return true; + } + } + } + return InsetMathNest::interpretString(cur, str); +} + + void InsetMathGrid::setHorizontalAlignments(docstring const & hh) { col_type col = 0; @@ -1332,11 +1349,25 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM); } + bool hline_enabled = false; + FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above"); + FuncStatus status; + if (getStatus(cur, fr, status)) + hline_enabled = status.enabled(); if (grid.nargs() == 1) { // single cell/part of cell cur.recordUndo(); cur.cell().insert(cur.pos(), grid.cell(0)); cur.pos() += grid.cell(0).size(); + if (hline_enabled) + rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_; + else { + for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) { + cur.cell().insert(0, + MathAtom(new InsetMathUnknown(from_ascii("\\hline")))); + cur.pos()++; + } + } } else { // multiple cells cur.recordUndoInset(); @@ -1349,6 +1380,15 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) idx_type i = index(r + cur.row(), c + col(cur.idx())); cell(i).insert(0, grid.cell(grid.index(r, c))); } + if (hline_enabled) + rowinfo_[r].lines_ += grid.rowinfo_[r].lines_; + else { + for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) { + idx_type i = index(r + cur.row(), 0); + cell(i).insert(0, + MathAtom(new InsetMathUnknown(from_ascii("\\hline")))); + } + } // append the left over horizontal cells to the last column idx_type i = index(r + cur.row(), ncols() - 1); for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c) @@ -1356,9 +1396,18 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) } // append the left over vertical cells to the last _cell_ idx_type i = nargs() - 1; - for (row_type r = numrows; r < grid.nrows(); ++r) + for (row_type r = numrows; r < grid.nrows(); ++r) { for (col_type c = 0; c < grid.ncols(); ++c) cell(i).append(grid.cell(grid.index(r, c))); + if (hline_enabled) + rowinfo_[r].lines_ += grid.rowinfo_[r].lines_; + else { + for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) { + cell(i).insert(0, + MathAtom(new InsetMathUnknown(from_ascii("\\hline")))); + } + } + } } cur.clearSelection(); // bug 393 // FIXME audit setBuffer calls diff --git a/src/mathed/InsetMathGrid.h b/src/mathed/InsetMathGrid.h index bfa286b766..7ff533be99 100644 --- a/src/mathed/InsetMathGrid.h +++ b/src/mathed/InsetMathGrid.h @@ -194,6 +194,8 @@ public: virtual char defaultColAlign(col_type) { return 'c'; } /// void setDefaults(); + /// + virtual bool interpretString(Cursor & cur, docstring const & str); /// virtual int colsep() const; diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index bd702464c3..c2f4fc16ba 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -872,7 +872,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd) // if relevant. Think typing "\frac". if (cmd.argument()[0] == ' ' && cur.inMacroMode() && cur.macroName() != "\\" - && cur.macroModeClose()) { + && cur.macroModeClose() && cur.pos() > 0) { MathAtom const atom = cur.prevAtom(); if (atom->asNestInset() && atom->isActive()) { cur.posBackward(); diff --git a/src/mathed/InsetMathNest.h b/src/mathed/InsetMathNest.h index 4e18dbda71..99537d8a17 100644 --- a/src/mathed/InsetMathNest.h +++ b/src/mathed/InsetMathNest.h @@ -172,7 +172,7 @@ public: /// interpret \p str and insert the result at the current position of /// \p cur if it is something known. Return whether \p cur was /// inserted. - bool interpretString(Cursor & cur, docstring const & str); + virtual bool interpretString(Cursor & cur, docstring const & str); private: /// lfun handler diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp index ca3a84cdae..7ba5344d13 100644 --- a/src/tex2lyx/table.cpp +++ b/src/tex2lyx/table.cpp @@ -1272,7 +1272,7 @@ void handle_tabular(Parser & p, ostream & os, string const & name, os << "\n\n"; os << "