]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/table.cpp
Fix #10778 (issue with CJK and language nesting)
[lyx.git] / src / tex2lyx / table.cpp
index ca3a84cdae6a879e5dbbc3239c5601923a6ee6fd..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 {
 
@@ -147,7 +145,7 @@ public:
 class ltType {
 public:
        // constructor
-       ltType() : topDL(false), bottomDL(false), empty(false) {}
+       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
@@ -209,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
@@ -412,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
@@ -726,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();
 
@@ -778,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();
                }
@@ -1156,8 +1159,20 @@ 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';
                                }
@@ -1194,11 +1209,43 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                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();
                        }
                }
@@ -1269,10 +1316,11 @@ 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", "0")
+          << write_attribute("rotate", rotate)
           << write_attribute("booktabs", booktabs)
           << write_attribute("islongtable", is_long_tabular);
        if (is_long_tabular) {