]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/table.cpp
Extend LATEXIFY command param handling and add literal param.
[lyx.git] / src / tex2lyx / table.cpp
index 45d910ac7ff9fbbd93695ef6d851dfac9f8bfca1..3cce03df4308561040fb9a1b93018fa64fbfb2ff 100644 (file)
@@ -22,6 +22,7 @@
 #include "support/convert.h"
 #include "support/lstrings.h"
 
+#include <algorithm>
 #include <iostream>
 #include <sstream>
 #include <vector>
@@ -31,9 +32,6 @@ using namespace std;
 
 namespace lyx {
 
-// filled in preamble.cpp
-map<char, int> special_columns;
-
 
 namespace {
 
@@ -75,6 +73,13 @@ class RowInfo {
 public:
        RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
                    caption(false), newpage(false) {}
+       /// Does this row have any special setting?
+       bool special() const
+       {
+               return topline || bottomline || !top_space.empty() ||
+                       !bottom_space.empty() || !interline_space.empty() ||
+                       type != LT_NORMAL || caption || newpage;
+       }
        /// horizontal line above
        bool topline;
        /// horizontal line below
@@ -111,7 +116,7 @@ class CellInfo {
 public:
        CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
                     leftlines(0), rightlines(0), topline(false),
-                    bottomline(false), rotate(false) {}
+                    bottomline(false), rotate(0) {}
        /// cell content
        string content;
        /// multicolumn flag
@@ -128,8 +133,8 @@ public:
        bool topline;
        /// do we have a line below?
        bool bottomline;
-       /// is the cell rotated?
-       bool rotate;
+       /// how is the cell rotated?
+       int rotate;
        /// width for multicolumn cells
        string width;
        /// special formatting for multicolumn cells
@@ -137,6 +142,23 @@ public:
 };
 
 
+class ltType {
+public:
+       // constructor
+       ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
+       // we have this header type (is set in the getLT... functions)
+       bool set;
+       // double borders on top
+       bool topDL;
+       // double borders on bottom
+       bool bottomDL;
+       // used for FirstHeader & LastFooter and if this is true
+       // all the rows marked as FirstHeader or LastFooter are
+       // ignored in the output and it is set to be empty!
+       bool empty;
+};
+
+
 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
 inline char const * verbose_align(char c)
 {
@@ -185,6 +207,14 @@ string const write_attribute(string const & name, string const & s)
 }
 
 
+string const write_attribute(string const & name, int const & i)
+{
+       // we write only true attribute values so we remove a bit of the
+       // file format bloat for tabulars.
+       return i ? write_attribute(name, convert<string>(i)) : string();
+}
+
+
 /*! rather brutish way to code table structure in a string:
 
 \verbatim
@@ -388,25 +418,22 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                                next.special += t.character();
                                next.special += '{' + p.verbatim_item() + '}';
                                break;
-                       default:
+                       default: {
                                // try user defined column types
-                               if (special_columns.find(t.character()) !=
-                                   special_columns.end()) {
-                                       ci2special(next);
-                                       next.special += t.character();
-                                       int const nargs =
-                                               special_columns[t.character()];
-                                       for (int i = 0; i < nargs; ++i)
-                                               next.special += '{' +
-                                                       p.verbatim_item() +
-                                                       '}';
-                                       colinfo.push_back(next);
-                                       next = ColInfo();
-                               } else
-                                       cerr << "Ignoring column specification"
-                                               " '" << t << "'." << endl;
+                               // unknown column types (nargs == -1) are
+                               // assumed to consume no arguments
+                               ci2special(next);
+                               next.special += t.character();
+                               int const nargs =
+                                       preamble.getSpecialTableColumnArguments(t.character());
+                               for (int i = 0; i < nargs; ++i)
+                                       next.special += '{' +
+                                               p.verbatim_item() + '}';
+                               colinfo.push_back(next);
+                               next = ColInfo();
                                break;
                        }
+                       }
        }
 
        // Maybe we have some column separators that need to be added to the
@@ -641,7 +668,6 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                                }
                                continue;
                        }
-
                }
 
                // We need a HLINE separator if we either have no hline
@@ -703,13 +729,13 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        }
                }
 
-               else if (t.cat() == catSpace 
+               else if (t.cat() == catSpace
                         || t.cat() == catNewline
-                        || t.cat() == catLetter 
-                        || t.cat() == catSuper 
-                        || t.cat() == catSub 
-                        || t.cat() == catOther 
-                        || t.cat() == catActive 
+                        || t.cat() == catLetter
+                        || t.cat() == catSuper
+                        || t.cat() == catSub
+                        || t.cat() == catOther
+                        || t.cat() == catActive
                         || t.cat() == catParameter)
                        os << t.cs();
 
@@ -755,7 +781,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        // treat the nested environment as a block, don't
                        // parse &, \\ etc, because they don't belong to our
                        // table if they appear.
-                       os << p.verbatimEnvironment(name);
+                       os << p.ertEnvironment(name);
                        os << "\\end{" << name << '}';
                        active_environments.pop_back();
                }
@@ -845,13 +871,18 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 
        vector< vector<CellInfo> > cellinfo(lines.size());
        vector<RowInfo> rowinfo(lines.size());
+       ltType endfirsthead;
+       ltType endhead;
+       ltType endfoot;
+       ltType endlastfoot;
 
        // split into rows
        //cerr << "// split into rows\n";
-       for (size_t row = 0; row < rowinfo.size(); ++row) {
+       for (size_t row = 0; row < rowinfo.size();) {
 
                // init row
                cellinfo[row].resize(colinfo.size());
+               bool deletelastrow = false;
 
                // split row
                vector<string> dummy;
@@ -967,37 +998,53 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                                        rowinfo[row].interline_space = translate_len(opt);
                                        }
                                } else if (t.cs() == "endhead") {
-                                       if (i > 0)
+                                       if (i == 0)
+                                               endhead.empty = true;
+                                       else
                                                rowinfo[row].type = LT_HEAD;
                                        for (int r = row - 1; r >= 0; --r) {
                                                if (rowinfo[r].type != LT_NORMAL)
                                                        break;
                                                rowinfo[r].type = LT_HEAD;
+                                               endhead.empty = false;
                                        }
+                                       endhead.set = true;
                                } else if (t.cs() == "endfirsthead") {
-                                       if (i > 0)
+                                       if (i == 0)
+                                               endfirsthead.empty = true;
+                                       else
                                                rowinfo[row].type = LT_FIRSTHEAD;
                                        for (int r = row - 1; r >= 0; --r) {
                                                if (rowinfo[r].type != LT_NORMAL)
                                                        break;
                                                rowinfo[r].type = LT_FIRSTHEAD;
+                                               endfirsthead.empty = false;
                                        }
+                                       endfirsthead.set = true;
                                } else if (t.cs() == "endfoot") {
-                                       if (i > 0)
+                                       if (i == 0)
+                                               endfoot.empty = true;
+                                       else
                                                rowinfo[row].type = LT_FOOT;
                                        for (int r = row - 1; r >= 0; --r) {
                                                if (rowinfo[r].type != LT_NORMAL)
                                                        break;
                                                rowinfo[r].type = LT_FOOT;
+                                               endfoot.empty = false;
                                        }
+                                       endfoot.set = true;
                                } else if (t.cs() == "endlastfoot") {
-                                       if (i > 0)
+                                       if (i == 0)
+                                               endlastfoot.empty = true;
+                                       else
                                                rowinfo[row].type = LT_LASTFOOT;
                                        for (int r = row - 1; r >= 0; --r) {
                                                if (rowinfo[r].type != LT_NORMAL)
                                                        break;
                                                rowinfo[r].type = LT_LASTFOOT;
+                                               endlastfoot.empty = false;
                                        }
+                                       endlastfoot.set = true;
                                } else if (t.cs() == "newpage") {
                                        if (i == 0) {
                                                if (row > 0)
@@ -1016,6 +1063,48 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                        }
                }
 
+               // LyX ends headers and footers always with \tabularnewline.
+               // This causes one additional row in the output.
+               // If the last row of a header/footer is empty, we can work
+               // around that by removing it.
+               if (row > 1) {
+                       RowInfo test = rowinfo[row-1];
+                       test.type = LT_NORMAL;
+                       if (lines[row-1].empty() && !test.special()) {
+                               switch (rowinfo[row-1].type) {
+                               case LT_FIRSTHEAD:
+                                       if (rowinfo[row].type != LT_FIRSTHEAD &&
+                                           rowinfo[row-2].type == LT_FIRSTHEAD)
+                                               deletelastrow = true;
+                                       break;
+                               case LT_HEAD:
+                                       if (rowinfo[row].type != LT_HEAD &&
+                                           rowinfo[row-2].type == LT_HEAD)
+                                               deletelastrow = true;
+                                       break;
+                               case LT_FOOT:
+                                       if (rowinfo[row].type != LT_FOOT &&
+                                           rowinfo[row-2].type == LT_FOOT)
+                                               deletelastrow = true;
+                                       break;
+                               case LT_LASTFOOT:
+                                       if (rowinfo[row].type != LT_LASTFOOT &&
+                                           rowinfo[row-2].type == LT_LASTFOOT)
+                                               deletelastrow = true;
+                                       break;
+                               case LT_NORMAL:
+                                       break;
+                               }
+                       }
+               }
+
+               if (deletelastrow) {
+                       lines.erase(lines.begin() + (row - 1));
+                       rowinfo.erase(rowinfo.begin() + (row - 1));
+                       cellinfo.erase(cellinfo.begin() + (row - 1));
+                       continue;
+               }
+
                // split into cells
                vector<string> cells;
                split(lines[row], cells, TAB);
@@ -1070,13 +1159,26 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                cellinfo[row][col].content += os.str();
 
                                // add dummy cells for multicol
-                               for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
+                               for (size_t i = 0; i < ncells - 1; ++i) {
                                        ++col;
+                                       if (col >= colinfo.size()) {
+                                               cerr << "The cell '"
+                                                       << cells[cell]
+                                                       << "' specifies "
+                                                       << convert<string>(ncells)
+                                                       << " columns while the table has only "
+                                                       << convert<string>(colinfo.size())
+                                                       << " columns!"
+                                                       << " Therefore the surplus columns will be ignored."
+                                                       << endl;
+                                               break;
+                                       }
                                        cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
                                        cellinfo[row][col].align = 'c';
                                }
 
-                       } else if (col == 0 && is_long_tabular &&
+                       } else if (col == 0 && colinfo.size() > 1 &&
+                                  is_long_tabular &&
                                   p.next_token().cs() == "caption") {
                                // longtable caption support in LyX is a hack:
                                // Captions require a row of their own with
@@ -1084,6 +1186,8 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                // one multicolumn cell. The contents of that
                                // cell must contain exactly one caption inset
                                // and nothing else.
+                               // Fortunately, the caption flag is only needed
+                               // for tables with more than one column.
                                rowinfo[row].caption = true;
                                for (size_t c = 1; c < cells.size(); ++c) {
                                        if (!cells[c].empty()) {
@@ -1104,13 +1208,44 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                // add dummy multicolumn cells
                                for (size_t c = 1; c < colinfo.size(); ++c)
                                        cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
-
                        } else {
+                               bool turn = false;
+                               int rotate = 0;
+                               if (p.next_token().cs() == "begin") {
+                                       p.pushPosition();
+                                       p.get_token();
+                                       string const env = p.getArg('{', '}');
+                                       if (env == "sideways" || env == "turn") {
+                                               string angle = "90";
+                                               if (env == "turn") {
+                                                       turn = true;
+                                                       angle = p.getArg('{', '}');
+                                               }
+                                               active_environments.push_back(env);
+                                               p.ertEnvironment(env);
+                                               active_environments.pop_back();
+                                               p.skip_spaces();
+                                               if (!p.good() && support::isStrInt(angle))
+                                                       rotate = convert<int>(angle);
+                                       }
+                                       p.popPosition();
+                               }
                                cellinfo[row][col].leftlines  = colinfo[col].leftlines;
                                cellinfo[row][col].rightlines = colinfo[col].rightlines;
                                cellinfo[row][col].align      = colinfo[col].align;
                                ostringstream os;
-                               parse_text_in_inset(p, os, FLAG_CELL, false, context);
+                               if (rotate != 0) {
+                                       cellinfo[row][col].rotate = rotate;
+                                       p.get_token();
+                                       active_environments.push_back(p.getArg('{', '}'));
+                                       if (turn)
+                                               p.getArg('{', '}');
+                                       parse_text_in_inset(p, os, FLAG_END, false, context);
+                                       active_environments.pop_back();
+                                       preamble.registerAutomaticallyLoadedPackage("rotating");
+                               } else {
+                                       parse_text_in_inset(p, os, FLAG_CELL, false, context);
+                               }
                                cellinfo[row][col].content += os.str();
                        }
                }
@@ -1126,6 +1261,8 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                        cellinfo[row - 1][col].bottomline = true;
                        rowinfo.pop_back();
                }
+
+               ++row;
        }
 
        // Now we have the table structure and content in rowinfo, colinfo
@@ -1179,13 +1316,25 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 
        //cerr << "// output what we have\n";
        // output what we have
+       string const rotate = "0";
        os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
           << "\" columns=\"" << colinfo.size() << "\">\n";
        os << "<features"
-          << write_attribute("rotate", false)
+          << write_attribute("rotate", rotate)
           << write_attribute("booktabs", booktabs)
           << write_attribute("islongtable", is_long_tabular);
-       if (!is_long_tabular)
+       if (is_long_tabular) {
+               os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
+                  << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
+                  << write_attribute("firstHeadEmpty", endfirsthead.empty)
+                  << write_attribute("headTopDL", endhead.topDL)
+                  << write_attribute("headBottomDL", endhead.bottomDL)
+                  << write_attribute("footTopDL", endfoot.topDL)
+                  << write_attribute("footBottomDL", endfoot.bottomDL)
+                  << write_attribute("lastFootTopDL", endlastfoot.topDL)
+                  << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
+                  << write_attribute("lastFootEmpty", endlastfoot.empty);
+       } else
                os << write_attribute("tabularvalignment", tabularvalignment)
                   << write_attribute("tabularwidth", tabularwidth);
        os << ">\n";