From c33506555f5f7193b0cbce6c9878d62a18f37cc9 Mon Sep 17 00:00:00 2001
From: Juergen Spitzmueller <spitz@lyx.org>
Date: Sun, 24 Jun 2018 18:13:03 +0200
Subject: [PATCH] tex2lyx: Add support for multirow

Fixes: #11164
---
 src/tex2lyx/Preamble.cpp |   2 +-
 src/tex2lyx/TODO.txt     |   2 -
 src/tex2lyx/table.cpp    | 105 +++++++++++++++++++++++++++++++++++----
 3 files changed, 96 insertions(+), 13 deletions(-)

diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp
index fa3fb1699d..e4e10c088e 100644
--- a/src/tex2lyx/Preamble.cpp
+++ b/src/tex2lyx/Preamble.cpp
@@ -205,7 +205,7 @@ const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color",
 "float", "fontspec", "framed", "graphicx", "hhline", "ifthen", "longtable",
-"makeidx", "minted", "nomencl", "pdfpages", "prettyref", "refstyle",
+"makeidx", "minted", "multirow", "nomencl", "pdfpages", "prettyref", "refstyle",
 "rotating", "rotfloat", "splitidx", "setspace", "subscript", "tabularx","textcomp", "tipa",
 "tipx", "tone", "ulem", "url", "varioref", "verbatim", "wrapfig", "xcolor", "xltabular",
 "xunicode", 0};
diff --git a/src/tex2lyx/TODO.txt b/src/tex2lyx/TODO.txt
index b108eb2822..d55dae0e73 100644
--- a/src/tex2lyx/TODO.txt
+++ b/src/tex2lyx/TODO.txt
@@ -39,14 +39,12 @@ Format LaTeX feature                        LyX feature
 363    horizontal longtable alignment       InsetTabular
 364    branch file name suffix              \filename_suffix
 371    automatic mhchem loading             \use_mhchem
-377    multirow.sty                         InsetTabular
 378    revision info                        InsetInfo
 380    ?                                    InsetPreview
 386    LyX version                          InsetInfo
 390    forward/reverse search               \forward_search, \forward_macro
 391    decimal alignment in tables          InsetTabular
 399    automatic mathdots loading           \use_mathdots
-407    vertical offset for multirows        InsetTabular
 411    support for polyglossia              \language_package	(the cases of no package, of babel and of custom package is supported)
 415    automatic undertilde loading         \use_package undertilde
 443    unicode-math.sty                     InsetMath*
diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp
index 6f2563231d..029dba65cb 100644
--- a/src/tex2lyx/table.cpp
+++ b/src/tex2lyx/table.cpp
@@ -103,14 +103,18 @@ public:
 
 
 /// the numeric values are part of the file format!
-enum Multicolumn {
+enum Multi {
 	/// A normal cell
 	CELL_NORMAL = 0,
 	/// A multicolumn cell. The number of columns is <tt>1 + number
 	/// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
-	CELL_BEGIN_OF_MULTICOLUMN = 1,
+	CELL_BEGIN_OF_MULTICOLUMN,
 	/// This is a dummy cell (part of a multicolumn cell)
-	CELL_PART_OF_MULTICOLUMN = 2
+	CELL_PART_OF_MULTICOLUMN,
+	///
+	CELL_BEGIN_OF_MULTIROW,
+	///
+	CELL_PART_OF_MULTIROW
 };
 
 
@@ -118,11 +122,11 @@ class CellInfo {
 public:
 	CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
 		     leftlines(0), rightlines(0), topline(false),
-		     bottomline(false), rotate(0) {}
+		     bottomline(false), rotate(0), mrxnum(0) {}
 	/// cell content
 	string content;
 	/// multicolumn flag
-	Multicolumn multi;
+	Multi multi;
 	/// cell alignment
 	char align;
 	/// vertical cell alignment
@@ -141,6 +145,10 @@ public:
 	string width;
 	/// special formatting for multicolumn cells
 	string special;
+	/// multirow offset
+	string mroffset;
+	/// number of further multirows
+	int mrxnum;
 };
 
 
@@ -410,6 +418,8 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
 				break;
 			}
 			case '*': {
+				if (p.next_token().character() != '{')
+					continue;
 				// *{n}{arg} means 'n' columns of type 'arg'
 				string const num = p.verbatim_item();
 				string const arg = p.verbatim_item();
@@ -1135,7 +1145,57 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 			Parser parse(cells[cell]);
 			parse.skip_spaces();
 			//cells[cell] << "'\n";
-			if (parse.next_token().cs() == "multicolumn") {
+			if (parse.next_token().cs() == "multirow") {
+				// We do not support the vpos arg yet.
+				if (parse.hasOpt()) {
+					string const vpos = parse.getArg('[', ']');
+					parse.skip_spaces(true);
+					cerr << "Ignoring multirow's vpos arg '"
+					     << vpos << "'!" << endl;
+				}
+				// how many cells?
+				parse.get_token();
+				size_t const ncells =
+					convert<unsigned int>(parse.verbatim_item());
+				// We do not support the bigstrut arg yet.
+				if (parse.hasOpt()) {
+					string const bs = parse.getArg('[', ']');
+					parse.skip_spaces(true);
+					cerr << "Ignoring multirow's bigstrut arg '"
+					     << bs << "'!" << endl;
+				}
+				// the width argument
+				string const width = parse.getArg('{', '}');
+				// the vmove arg
+				string vmove;
+				if (parse.hasOpt()) {
+					vmove = parse.getArg('[', ']');
+					parse.skip_spaces(true);
+				}
+
+				if (width != "*")
+					colinfo[col].width = width;
+				if (!vmove.empty())
+					cellinfo[row][col].mroffset = vmove;
+				cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
+				cellinfo[row][col].leftlines  = colinfo[col].leftlines;
+				cellinfo[row][col].rightlines = colinfo[col].rightlines;
+				cellinfo[row][col].mrxnum = ncells - 1;
+
+				ostringstream os2;
+				parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
+				if (!cellinfo[row][col].content.empty()) {
+					// This may or may not work in LaTeX,
+					// but it does not work in LyX.
+					// FIXME: Handle it correctly!
+					cerr << "Moving cell content '"
+					     << cells[cell]
+					     << "' into a multirow cell. "
+						"This will probably not work."
+					     << endl;
+				}
+				cellinfo[row][col].content += os2.str();
+			} else if (parse.next_token().cs() == "multicolumn") {
 				// how many cells?
 				parse.get_token();
 				size_t const ncells =
@@ -1284,13 +1344,23 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 	// and cellinfo.
 	// Unfortunately LyX has some limitations that we need to work around.
 
-	// Convert cells with special content to multicolumn cells
-	// (LyX ignores the special field for non-multicolumn cells).
+	// Some post work
 	for (size_t row = 0; row < rowinfo.size(); ++row) {
 		for (size_t col = 0; col < cellinfo[row].size(); ++col) {
+			// Convert cells with special content to multicolumn cells
+			// (LyX ignores the special field for non-multicolumn cells).
 			if (cellinfo[row][col].multi == CELL_NORMAL &&
 			    !cellinfo[row][col].special.empty())
 				cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
+			// Add multirow dummy cells
+			if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
+					|| cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
+				    && cellinfo[row - 1][col].mrxnum > 0) {
+				// add dummy cells for multirow
+				cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
+				cellinfo[row][col].align = 'c';
+				cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
+			}
 		}
 	}
 
@@ -1320,6 +1390,16 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 					cellinfo[row][col].rightlines = colinfo[col].rightlines;
 				if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
 					cellinfo[row][col].leftlines = colinfo[col].leftlines;
+			} else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
+				size_t s = row + 1;
+				while (s < rowinfo.size() &&
+				       cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
+					s++;
+				if (s < cellinfo[row].size() &&
+				    cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
+					cellinfo[row][col].bottomline = rowinfo[row].bottomline;
+				if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
+					cellinfo[row][col].topline = rowinfo[row].topline;
 			}
 		}
 	}
@@ -1392,8 +1472,12 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 		for (size_t col = 0; col < colinfo.size(); ++col) {
 			CellInfo const & cell = cellinfo[row][col];
 			os << "<cell";
-			if (cell.multi != CELL_NORMAL)
+			if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN
+			    || cell.multi == CELL_PART_OF_MULTICOLUMN)
 				os << " multicolumn=\"" << cell.multi << "\"";
+			if (cell.multi == CELL_BEGIN_OF_MULTIROW
+			    || cell.multi == CELL_PART_OF_MULTIROW)
+				os << " multirow=\"" << cell.multi << "\"";
 			os << " alignment=\"" << verbose_align(cell.align)
 			   << "\""
 			   << " valignment=\"" << verbose_valign(cell.valign)
@@ -1402,7 +1486,8 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 			   << write_attribute("bottomline", cell.bottomline)
 			   << write_attribute("leftline", cell.leftlines > 0)
 			   << write_attribute("rightline", cell.rightlines > 0)
-			   << write_attribute("rotate", cell.rotate);
+			   << write_attribute("rotate", cell.rotate)
+			   << write_attribute("mroffset", cell.mroffset);
 			//cerr << "\nrow: " << row << " col: " << col;
 			//if (cell.topline)
 			//	cerr << " topline=\"true\"";