]> 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 217014c36ab7cdf03a4cead5d07b864b00db7f3c..b162617af233d153e4efa31cba5689895ea13566 100644 (file)
@@ -39,7 +39,7 @@ namespace {
 class ColInfo {
 public:
        ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
-               varwidth(false), decimal_point('\0') {}
+               varwidth(false), decimal_point('\0'), vcolumn(false) {}
        /// column alignment
        char align;
        /// vertical alignment
@@ -56,6 +56,8 @@ public:
        bool varwidth;
        /// decimal separator
        char decimal_point;
+       /// V column type
+       bool vcolumn;
 };
 
 
@@ -126,7 +128,9 @@ class CellInfo {
 public:
        CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
                     leftlines(0), rightlines(0), topline(false),
-                    bottomline(false), rotate(0), mrxnum(0) {}
+                    bottomline(false), topline_ltrim(false),
+                    topline_rtrim(false), bottomline_ltrim(false),
+                    bottomline_rtrim(false), rotate(0), mrxnum(0) {}
        /// cell content
        string content;
        /// multicolumn flag
@@ -143,6 +147,14 @@ public:
        bool topline;
        /// do we have a line below?
        bool bottomline;
+       /// Left trimming of top line
+       bool topline_ltrim;
+       /// Right trimming of top line
+       bool topline_rtrim;
+       /// Left trimming of bottom line
+       bool bottomline_ltrim;
+       /// Right trimming of top line
+       bool bottomline_rtrim;
        /// how is the cell rotated?
        int rotate;
        /// width for multicolumn cells
@@ -298,7 +310,9 @@ void ci2special(ColInfo & ci)
                        ci.special += ">{\\centering" + arraybackslash + "}";
                        break;
                }
-               if (ci.varwidth)
+               if (ci.vcolumn)
+                       ci.special += 'V';
+               else if (ci.varwidth)
                        ci.special += 'X';
                else if (ci.valign == 'n')
                        ci.special += 'p';
@@ -384,6 +398,21 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                                colinfo.push_back(next);
                                next = ColInfo();
                                break;
+                       case 'V': {
+                               // V column type (varwidth package)
+                               string const s = trimSpaceAndEol(p.verbatim_item());
+                               // V{\linewidth} is treated as a normal column
+                               // (which allows for line breaks). The V type is
+                               // automatically set by LyX in this case
+                               if (s != "\\linewidth" || !next.special.empty()) {
+                                       next.vcolumn = true;
+                                       next.width = s;
+                                       ci2special(next);
+                               }
+                               colinfo.push_back(next);
+                               next = ColInfo();
+                               break;
+                       }
                        case 'p':
                        case 'b':
                        case 'm':
@@ -413,7 +442,7 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                                                next.special += '|';
                                } else if (colinfo.back().special.empty())
                                        ++colinfo.back().rightlines;
-                               else if (next.special.empty())
+                               else if (next.special.empty() && p.next_token().cat() != catEnd)
                                        ++next.leftlines;
                                else
                                        colinfo.back().special += '|';
@@ -477,14 +506,14 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                        case '@':
                                // text instead of the column spacing
                        case '!': {
-                                       // text in addition to the column spacing
-                                       string const arg =  p.verbatim_item();
-                                       next.special += t.character();
-                                       next.special += '{' + arg + '}';
-                                       string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
-                                       if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
-                                               next.decimal_point = arg.back();
-                                       break;
+                               // text in addition to the column spacing
+                               string const arg =  p.verbatim_item();
+                               next.special += t.character();
+                               next.special += '{' + arg + '}';
+                               string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
+                               if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
+                                       next.decimal_point = arg.back();
+                               break;
                        }
                        default: {
                                // try user defined column types
@@ -537,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 &&
@@ -560,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)
@@ -582,14 +611,21 @@ bool parse_hlines(Parser & p, Token const & t, string & hlines,
                hlines += "\\cline{" + p.verbatim_item() + '}';
 
        else if (t.cs() == "cmidrule") {
-               // We cannot handle the \cmidrule(l){3-4} form
                p.pushPosition();
                p.skip_spaces(true);
-               bool const hasParentheses(p.getFullArg('(', ')').first);
-               p.popPosition();
-               if (hasParentheses)
+               // We do not support the optional height argument
+               if (p.hasOpt())
                        return false;
-               hlines += "\\cmidrule{" + p.verbatim_item() + '}';
+               // We support the \cmidrule(l){3-4} form but
+               // not the trim length parameters (l{<with>}r{<width>})
+               string const trim = p.getFullParentheseArg();
+               string const range = p.verbatim_item();
+               if (!trim.empty()) {
+                       if (support::contains(trim, "{"))
+                               return false;
+                       hlines += "\\cmidrule" + trim + "{" + range + "}";
+               } else
+                       hlines += "\\cmidrule{" + range + '}';
        }
 
        else if (t.cs() == "addlinespace") {
@@ -676,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)) {
@@ -885,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);
+       }
 }
 
 
@@ -903,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");
@@ -971,7 +1069,7 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
 
                for (int i = 0; i <= 2; i += 2) {
                        //cerr << "   reading from line string '" << dummy[i] << "'\n";
-                       Parser p1(dummy[i]);
+                       Parser p1(dummy[size_type(i)]);
                        while (p1.good()) {
                                Token t = p1.get_token();
                                //cerr << "read token: " << t << "\n";
@@ -997,10 +1095,13 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                                handle_hline_below(rowinfo[row], cellinfo[row]);
                                        }
                                } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
-                                       if (t.cs() == "cmidrule")
+                                       string trim;
+                                       if (t.cs() == "cmidrule") {
                                                booktabs = true;
+                                               trim = p1.getFullParentheseArg();
+                                       }
                                        string arg = p1.verbatim_item();
-                                       //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
+                                       //cerr << "read " << t.cs() << " arg: '" << arg << "', trim: '" << trim << "'\n";
                                        vector<string> cols;
                                        split(arg, cols, '-');
                                        cols.resize(2);
@@ -1037,9 +1138,25 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                                                if (i == 0) {
                                                        rowinfo[row].topline = true;
                                                        cellinfo[row][col].topline = true;
+                                                       if (support::contains(trim, 'l') && col == from) {
+                                                               //rowinfo[row].topline_ltrim = true;
+                                                               cellinfo[row][col].topline_ltrim = true;
+                                                       }
+                                                       else if (support::contains(trim, 'r') && col == to) {
+                                                               //rowinfo[row].topline_rtrim = true;
+                                                               cellinfo[row][col].topline_rtrim = true;
+                                                       }
                                                } else {
                                                        rowinfo[row].bottomline = true;
                                                        cellinfo[row][col].bottomline = true;
+                                                       if (support::contains(trim, 'l') && col == from) {
+                                                               //rowinfo[row].bottomline_ltrim = true;
+                                                               cellinfo[row][col].bottomline_ltrim = true;
+                                                       }
+                                                       else if (support::contains(trim, 'r') && col == to) {
+                                                               //rowinfo[row].bottomline_rtrim = true;
+                                                               cellinfo[row][col].bottomline_rtrim = true;
+                                                       }
                                                }
                                        }
                                } else if (t.cs() == "addlinespace") {
@@ -1218,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;
@@ -1228,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.
@@ -1265,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.
@@ -1323,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();
                        }
                }
@@ -1461,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()
@@ -1490,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";
@@ -1540,7 +1666,11 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
                           << " valignment=\"" << verbose_valign(cell.valign)
                           << "\""
                           << write_attribute("topline", cell.topline)
+                          << write_attribute("toplineltrim", cell.topline_ltrim)
+                          << write_attribute("toplinertrim", cell.topline_rtrim)
                           << write_attribute("bottomline", cell.bottomline)
+                          << write_attribute("bottomlineltrim", cell.bottomline_ltrim)
+                          << write_attribute("bottomlinertrim", cell.bottomline_rtrim)
                           << write_attribute("leftline", cell.leftlines > 0)
                           << write_attribute("rightline", cell.rightlines > 0)
                           << write_attribute("rotate", cell.rotate)