]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/table.cpp
Make undo action no-ops when the buffer is read-only
[lyx.git] / src / tex2lyx / table.cpp
index 411fd5b6ab5534f929684b38458cc727856d2f6a..b162617af233d153e4efa31cba5689895ea13566 100644 (file)
@@ -566,7 +566,7 @@ void fix_colalign(vector<ColInfo> & colinfo)
 {
        // Try to move extra leftlines to the previous column.
        // We do this only if both special fields are empty, otherwise we
-       // can't tell wether the result will be the same.
+       // can't tell whether the result will be the same.
        for (size_t col = 0; col < colinfo.size(); ++col) {
                if (colinfo[col].leftlines > 1 &&
                    colinfo[col].special.empty() && col > 0 &&
@@ -589,14 +589,14 @@ void fix_colalign(vector<ColInfo> & colinfo)
        }
        // Move the lines and alignment settings to the special field if
        // necessary
-       for (size_t col = 0; col < colinfo.size(); ++col)
-               fix_colalign(colinfo[col]);
+       for (auto & col : colinfo)
+               fix_colalign(col);
 }
 
 
 /*!
  * Parse hlines and similar stuff.
- * \returns wether the token \p t was parsed
+ * \returns whether the token \p t was parsed
  */
 bool parse_hlines(Parser & p, Token const & t, string & hlines,
                  bool is_long_tabular)
@@ -712,7 +712,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                }
 
                // We need to handle structure stuff first in order to
-               // determine wether we need to output a HLINE separator
+               // determine whether we need to output a HLINE separator
                // before the row or not.
                if (t.cat() == catEscape) {
                        if (parse_hlines(p, t, hlines, is_long_tabular)) {
@@ -921,16 +921,77 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
 {
        ri.topline = true;
-       for (size_t col = 0; col < ci.size(); ++col)
-               ci[col].topline = true;
+       for (auto & col : ci)
+               col.topline = true;
 }
 
 
 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
 {
        ri.bottomline = true;
-       for (size_t col = 0; col < ci.size(); ++col)
-               ci[col].bottomline = true;
+       for (auto & col : ci)
+               col.bottomline = true;
+}
+
+
+void parse_cell_content(ostringstream & os2, Parser & parse, unsigned int flags, Context & newcontext,
+                       vector< vector<CellInfo> > & cellinfo, vector<ColInfo> & colinfo,
+                       size_t const row, size_t const col)
+{
+       bool turn = false;
+       int rotate = 0;
+       bool varwidth = false;
+       if (parse.next_token().cs() == "begin") {
+               parse.pushPosition();
+               parse.get_token();
+               string const env = parse.getArg('{', '}');
+               if (env == "sideways" || env == "turn") {
+                       string angle = "90";
+                       if (env == "turn") {
+                               turn = true;
+                               angle = parse.getArg('{', '}');
+                       }
+                       active_environments.push_back(env);
+                       parse.ertEnvironment(env);
+                       active_environments.pop_back();
+                       parse.skip_spaces();
+                       if (!parse.good() && support::isStrInt(angle))
+                               rotate = convert<int>(angle);
+               } else if (env == "cellvarwidth") {
+                       active_environments.push_back(env);
+                       parse.ertEnvironment(env);
+                       active_environments.pop_back();
+                       parse.skip_spaces();
+                       varwidth = true;
+               }
+               parse.popPosition();
+       }
+       if (rotate != 0) {
+               cellinfo[row][col].rotate = rotate;
+               parse.get_token();
+               active_environments.push_back(parse.getArg('{', '}'));
+               if (turn)
+                       parse.getArg('{', '}');
+               parse_text_in_inset(parse, os2, FLAG_END, false, newcontext);
+               active_environments.pop_back();
+               preamble.registerAutomaticallyLoadedPackage("rotating");
+       } else if (varwidth) {
+               parse.get_token();
+               active_environments.push_back(parse.getArg('{', '}'));
+               // valign arg
+               if (parse.hasOpt())
+                       cellinfo[row][col].valign = parse.getArg('[', ']')[1];
+               newcontext.in_table_cell = true;
+               parse_text_in_inset(parse, os2, FLAG_END, false, newcontext);
+               if (cellinfo[row][col].multi == CELL_NORMAL)
+                       colinfo[col].align = newcontext.cell_align;
+               else
+                       cellinfo[row][col].align = newcontext.cell_align;
+               active_environments.pop_back();
+               preamble.registerAutomaticallyLoadedPackage("varwidth");
+       } else {
+               parse_text_in_inset(parse, os2, flags, false, newcontext);
+       }
 }
 
 
@@ -939,8 +1000,9 @@ void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
 
 void handle_tabular(Parser & p, ostream & os, string const & name,
                    string const & tabularwidth, string const & halign,
-                   Context & context)
+                   Context const & context)
 {
+       Context newcontext = context;
        bool const is_long_tabular(name == "longtable" || name == "xltabular");
        bool booktabs = false;
        string tabularvalignment("middle");
@@ -1273,7 +1335,7 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                        parse.skip_spaces(true);
                                }
 
-                               if (width != "*")
+                               if (width != "*" && width != "=")
                                        colinfo[col].width = width;
                                if (!vmove.empty())
                                        cellinfo[row][col].mroffset = vmove;
@@ -1283,7 +1345,11 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                cellinfo[row][col].mrxnum = ncells - 1;
 
                                ostringstream os2;
-                               parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
+                               parse.get_token();// skip {
+                               parse_cell_content(os2, parse, FLAG_BRACE_LAST, newcontext,
+                                                       cellinfo, colinfo,
+                                                       row, col);
+                               parse.get_token();// skip }
                                if (!cellinfo[row][col].content.empty()) {
                                        // This may or may not work in LaTeX,
                                        // but it does not work in LyX.
@@ -1320,7 +1386,11 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                cellinfo[row][col].leftlines  = ci.leftlines;
                                cellinfo[row][col].rightlines = ci.rightlines;
                                ostringstream os2;
-                               parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
+                               parse.get_token();// skip {
+                               parse_cell_content(os2, parse, FLAG_BRACE_LAST, newcontext,
+                                                       cellinfo, colinfo,
+                                                       row, col);
+                               parse.get_token();// skip }
                                if (!cellinfo[row][col].content.empty()) {
                                        // This may or may not work in LaTeX,
                                        // but it does not work in LyX.
@@ -1378,49 +1448,50 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                cellinfo[row][col].align      = colinfo[col].align;
                                cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
                                ostringstream os2;
-                               parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
+                               parse_text_in_inset(parse, os2, FLAG_CELL, false, newcontext);
                                cellinfo[row][col].content += os2.str();
                                // 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 (parse.next_token().cs() == "begin") {
-                                       parse.pushPosition();
-                                       parse.get_token();
-                                       string const env = parse.getArg('{', '}');
-                                       if (env == "sideways" || env == "turn") {
-                                               string angle = "90";
-                                               if (env == "turn") {
-                                                       turn = true;
-                                                       angle = parse.getArg('{', '}');
-                                               }
-                                               active_environments.push_back(env);
-                                               parse.ertEnvironment(env);
-                                               active_environments.pop_back();
-                                               parse.skip_spaces();
-                                               if (!parse.good() && support::isStrInt(angle))
-                                                       rotate = convert<int>(angle);
+                       } else if (col == 0 && colinfo.size() > 1 &&
+                                  is_long_tabular &&
+                                  parse.next_token().cs() == "caption") {
+                               // longtable caption support in LyX is a hack:
+                               // Captions require a row of their own with
+                               // the caption flag set to true, having only
+                               // 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()) {
+                                               cerr << "Moving cell content '"
+                                                    << cells[c]
+                                                    << "' into the caption cell. "
+                                                       "This will probably not work."
+                                                    << endl;
+                                               cells[0] += cells[c];
                                        }
-                                       parse.popPosition();
                                }
+                               cells.resize(1);
+                               cellinfo[row][col].align      = colinfo[col].align;
+                               cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
+                               ostringstream os2;
+                               parse_text_in_inset(parse, os2, FLAG_CELL, false, newcontext);
+                               cellinfo[row][col].content += os2.str();
+                               // add dummy multicolumn cells
+                               for (size_t c = 1; c < colinfo.size(); ++c)
+                                       cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
+                       } else {
+                               ostringstream os2;
+                               parse_cell_content(os2, parse, FLAG_CELL, newcontext,
+                                                       cellinfo, colinfo,
+                                                       row, col);
                                cellinfo[row][col].leftlines  = colinfo[col].leftlines;
                                cellinfo[row][col].rightlines = colinfo[col].rightlines;
                                cellinfo[row][col].align      = colinfo[col].align;
-                               ostringstream os2;
-                               if (rotate != 0) {
-                                       cellinfo[row][col].rotate = rotate;
-                                       parse.get_token();
-                                       active_environments.push_back(parse.getArg('{', '}'));
-                                       if (turn)
-                                               parse.getArg('{', '}');
-                                       parse_text_in_inset(parse, os2, FLAG_END, false, context);
-                                       active_environments.pop_back();
-                                       preamble.registerAutomaticallyLoadedPackage("rotating");
-                               } else {
-                                       parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
-                               }
                                cellinfo[row][col].content += os2.str();
                        }
                }
@@ -1516,8 +1587,8 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
        //cerr << "// output what we have\n";
        // output what we have
        size_type cols = colinfo.size();
-       for (size_t col = 0; col < colinfo.size(); ++col) {
-               if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
+       for (auto const & col : colinfo) {
+               if (col.decimal_point != '\0' && col.align != 'd')
                        --cols;
        }
        os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
@@ -1545,18 +1616,18 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
        os << write_attribute("tabularwidth", tabularwidth) << ">\n";
 
        //cerr << "// after header\n";
-       for (size_t col = 0; col < colinfo.size(); ++col) {
-               if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
+       for (auto const & col : colinfo) {
+               if (col.decimal_point != '\0' && col.align != 'd')
                        continue;
                os << "<column alignment=\""
-                          << verbose_align(colinfo[col].align) << "\"";
-               if (colinfo[col].decimal_point != '\0')
-                       os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
+                          << verbose_align(col.align) << "\"";
+               if (col.decimal_point != '\0')
+                       os << " decimal_point=\"" << col.decimal_point << "\"";
                os << " valignment=\""
-                  << verbose_valign(colinfo[col].valign) << "\""
-                  << write_attribute("width", translate_len(colinfo[col].width))
-                  << write_attribute("special", colinfo[col].special)
-                  << write_attribute("varwidth", colinfo[col].varwidth)
+                  << verbose_valign(col.valign) << "\""
+                  << write_attribute("width", translate_len(col.width))
+                  << write_attribute("special", col.special)
+                  << write_attribute("varwidth", col.varwidth)
                   << ">\n";
        }
        //cerr << "// after cols\n";