]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
Fix bug #253 (Incorrect protection of closing quotation marks)
[lyx.git] / src / insets / InsetTabular.cpp
index 6ad5cc6f0ef442b36e8ae46a521e8839da47143a..4a267e9d47e0f367d1a6fc232bde362f6bb8f3a7 100644 (file)
@@ -183,6 +183,7 @@ TabularFeature tabularFeature[] =
        { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
        { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
        { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
+       { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
        { Tabular::LAST_ACTION, "", false }
 };
 
@@ -515,19 +516,25 @@ string const featureAsString(Tabular::Feature action)
 }
 
 
-InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
+DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
 {
-       InsetTableCell tail = InsetTableCell(head);
-
-       DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
+       DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
        for (; dit; dit.forwardChar())
-               if (dit.inTexted() && dit.depth()==1
+               if (dit.inTexted() && dit.depth() == 1
                        && dit.paragraph().find(align_d, false, false, dit.pos()))
                        break;
 
-       pit_type const psize = head.paragraphs().front().size();
+       return dit;
+}
+
+
+InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
+{
+       InsetTableCell tail = InsetTableCell(head);
+       DocIterator const dit = separatorPos(&head, align_d);
        hassep = dit;
        if (hassep) {
+               pit_type const psize = head.paragraphs().front().size();
                head.paragraphs().front().eraseChars(dit.pos(), psize, false);
                tail.paragraphs().front().eraseChars(0, 
                        dit.pos() < psize ? dit.pos() + 1 : psize, false);
@@ -678,6 +685,7 @@ void Tabular::init(Buffer * buf, row_type rows_arg,
        updateIndexes();
        is_long_tabular = false;
        tabular_valignment = LYX_VALIGN_MIDDLE;
+       tabular_width = Length();
        longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
        rotate = false;
        use_booktabs = false;
@@ -1340,8 +1348,9 @@ Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
 {
        col_type c = ncols() - 1;
-       while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
-               || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
+       while (c > 0
+              && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
+                  || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
                --c;
        return cell_info[row][c].cellno;
 }
@@ -1391,8 +1400,10 @@ void Tabular::write(ostream & os) const
           << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
           << write_attribute("lastFootEmpty", endlastfoot.empty);
        // longtables cannot be aligned vertically
-       if (!is_long_tabular)
+       if (!is_long_tabular) {
           os << write_attribute("tabularvalignment", tabular_valignment);
+          os << write_attribute("tabularwidth", tabular_width);
+       }
        if (is_long_tabular)
           os << write_attribute("longtabularalignment",
                                 longtabular_alignment);
@@ -1489,6 +1500,7 @@ void Tabular::read(Lexer & lex)
        getTokenValue(line, "booktabs", use_booktabs);
        getTokenValue(line, "islongtable", is_long_tabular);
        getTokenValue(line, "tabularvalignment", tabular_valignment);
+       getTokenValue(line, "tabularwidth", tabular_width);
        getTokenValue(line, "longtabularalignment", longtabular_alignment);
        getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
        getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
@@ -1886,6 +1898,8 @@ bool Tabular::getLTNewPage(row_type row) const
 
 bool Tabular::haveLTHead() const
 {
+       if (!is_long_tabular)
+               return false;
        for (row_type i = 0; i < nrows(); ++i)
                if (row_info[i].endhead)
                        return true;
@@ -1895,7 +1909,7 @@ bool Tabular::haveLTHead() const
 
 bool Tabular::haveLTFirstHead() const
 {
-       if (endfirsthead.empty)
+       if (!is_long_tabular || endfirsthead.empty)
                return false;
        for (row_type r = 0; r < nrows(); ++r)
                if (row_info[r].endfirsthead)
@@ -1906,6 +1920,8 @@ bool Tabular::haveLTFirstHead() const
 
 bool Tabular::haveLTFoot() const
 {
+       if (!is_long_tabular)
+               return false;
        for (row_type r = 0; r < nrows(); ++r)
                if (row_info[r].endfoot)
                        return true;
@@ -1915,7 +1931,7 @@ bool Tabular::haveLTFoot() const
 
 bool Tabular::haveLTLastFoot() const
 {
-       if (endlastfoot.empty)
+       if (!is_long_tabular || endlastfoot.empty)
                return false;
        for (row_type r = 0; r < nrows(); ++r)
                if (row_info[r].endlastfoot)
@@ -1951,6 +1967,8 @@ bool Tabular::ltCaption(row_type row) const
 
 bool Tabular::haveLTCaption() const
 {
+       if (!is_long_tabular)
+               return false;
        for (row_type r = 0; r < nrows(); ++r)
                if (row_info[r].caption)
                        return true;
@@ -2016,7 +2034,7 @@ bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
 }
 
 
-int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
+void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
 {
        // we only output complete row lines and the 1st row here, the rest
        // is done in Tabular::TeXBottomHLine(...)
@@ -2038,7 +2056,7 @@ int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
 
        // do nothing if empty first row, or incomplete row line after
        if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
-               return 0;
+               return;
 
        // only output complete row lines and the 1st row's clines
        if (nset == ncols()) {
@@ -2075,11 +2093,10 @@ int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
                }
        }
        os << "\n";
-       return 1;
 }
 
 
-int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
+void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
 {
        // we output bottomlines of row r and the toplines of row r+1
        // if the latter do not span the whole tabular
@@ -2113,7 +2130,7 @@ int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) co
 
        // do nothing if empty, OR incomplete row line with a topline in next row
        if (nset == 0 || (nextrowset && nset != ncols()))
-               return 0;
+               return;
 
        if (nset == ncols()) {
                if (use_booktabs)
@@ -2148,17 +2165,15 @@ int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) co
                }
        }
        os << "\n";
-       return 1;
 }
 
 
-int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
-                                                        bool & ismulticol, bool & ismultirow) const
+void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
+                             bool & ismulticol, bool & ismultirow) const
 {
-       int ret = 0;
        row_type const r = cellRow(cell);
        if (is_long_tabular && row_info[r].caption)
-               return ret;
+               return;
 
        Tabular::VAlignment valign =  getVAlignment(cell, !isMultiColumn(cell));
        LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
@@ -2181,8 +2196,11 @@ int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
                || (coldouble != celldouble);
 
        // we center in multicol when no decimal point
-       ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
-               && (cellInfo(cell).decimal_width == 0));
+       if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
+               docstring const align_d = column_info[c].decimal_point;
+               DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
+               ismulticol |= !dit;
+       }
 
        // up counter by 1 for each decimally aligned col since they use 2 latex cols
        int latexcolspan = columnSpan(cell);
@@ -2264,7 +2282,6 @@ int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
 
        if (getRotateCell(cell)) {
                os << "\\begin{sideways}\n";
-               ++ret;
        }
        if (getUsebox(cell) == BOX_PARBOX) {
                os << "\\parbox[";
@@ -2296,139 +2313,99 @@ int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
                }
                os << "]{" << from_ascii(getPWidth(cell).asLatexString())
                   << "}\n";
-               ++ret;
        }
-       return ret;
 }
 
 
-int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
-                                                         bool ismulticol, bool ismultirow) const
+void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
+                              bool ismulticol, bool ismultirow) const
 {
-       int ret = 0;
        row_type const r = cellRow(cell);
        if (is_long_tabular && row_info[r].caption)
-               return ret;
+               return;
 
        // usual cells
        if (getUsebox(cell) == BOX_PARBOX)
                os << '}';
-       else if (getUsebox(cell) == BOX_MINIPAGE) {
-               os << "%\n\\end{minipage}";
-               ret += 2;
-       }
-       if (getRotateCell(cell)) {
-               os << "%\n\\end{sideways}";
-               ++ret;
-       }
+       else if (getUsebox(cell) == BOX_MINIPAGE)
+               os << breakln << "\\end{minipage}";
+       if (getRotateCell(cell))
+               os << breakln << "\\end{sideways}";
        if (ismultirow)
                os << '}';
        if (ismulticol)
                os << '}';
-
-       return ret;
 }
 
 
-int Tabular::TeXLongtableHeaderFooter(odocstream & os,
-                                        OutputParams const & runparams) const
+void Tabular::TeXLongtableHeaderFooter(otexstream & os,
+                                       OutputParams const & runparams) const
 {
        if (!is_long_tabular)
-               return 0;
+               return;
 
-       int ret = 0;
        // caption handling
        // the caption must be output before the headers
        if (haveLTCaption()) {
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].caption) {
-                               ret += TeXRow(os, r, runparams);
-                       }
+                       if (row_info[r].caption)
+                               TeXRow(os, r, runparams);
                }
        }
        // output first header info
        // first header must be output before the header, otherwise the
        // correct caption placement becomes really weird
        if (haveLTFirstHead()) {
-               if (endfirsthead.topDL) {
+               if (endfirsthead.topDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endfirsthead) {
-                               ret += TeXRow(os, r, runparams);
-                       }
+                       if (row_info[r].endfirsthead)
+                               TeXRow(os, r, runparams);
                }
-               if (endfirsthead.bottomDL) {
+               if (endfirsthead.bottomDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                os << "\\endfirsthead\n";
-               ++ret;
        }
        // output header info
        if (haveLTHead()) {
-               if (endfirsthead.empty && !haveLTFirstHead()) {
+               if (endfirsthead.empty && !haveLTFirstHead())
                        os << "\\endfirsthead\n";
-                       ++ret;
-               }
-               if (endhead.topDL) {
+               if (endhead.topDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endhead) {
-                               ret += TeXRow(os, r, runparams);
-                       }
+                       if (row_info[r].endhead)
+                               TeXRow(os, r, runparams);
                }
-               if (endhead.bottomDL) {
+               if (endhead.bottomDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                os << "\\endhead\n";
-               ++ret;
        }
        // output footer info
        if (haveLTFoot()) {
-               if (endfoot.topDL) {
+               if (endfoot.topDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endfoot) {
-                               ret += TeXRow(os, r, runparams);
-                       }
+                       if (row_info[r].endfoot)
+                               TeXRow(os, r, runparams);
                }
-               if (endfoot.bottomDL) {
+               if (endfoot.bottomDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                os << "\\endfoot\n";
-               ++ret;
-               if (endlastfoot.empty && !haveLTLastFoot()) {
+               if (endlastfoot.empty && !haveLTLastFoot())
                        os << "\\endlastfoot\n";
-                       ++ret;
-               }
        }
        // output lastfooter info
        if (haveLTLastFoot()) {
-               if (endlastfoot.topDL) {
+               if (endlastfoot.topDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endlastfoot) {
-                               ret += TeXRow(os, r, runparams);
-                       }
+                       if (row_info[r].endlastfoot)
+                               TeXRow(os, r, runparams);
                }
-               if (endlastfoot.bottomDL) {
+               if (endlastfoot.bottomDL)
                        os << "\\hline\n";
-                       ++ret;
-               }
                os << "\\endlastfoot\n";
-               ++ret;
        }
-       return ret;
 }
 
 
@@ -2442,8 +2419,8 @@ bool Tabular::isValidRow(row_type row) const
 }
 
 
-int Tabular::TeXRow(odocstream & os, row_type row,
-                      OutputParams const & runparams) const
+void Tabular::TeXRow(otexstream & os, row_type row,
+                    OutputParams const & runparams) const
 {
        idx_type cell = cellIndex(row, 0);
        shared_ptr<InsetTableCell> inset = cellInset(cell);
@@ -2451,14 +2428,13 @@ int Tabular::TeXRow(odocstream & os, row_type row,
        string const lang = par.getParLanguage(buffer().params())->lang();
 
        //output the top line
-       int ret = TeXTopHLine(os, row, lang);
+       TeXTopHLine(os, row, lang);
 
        if (row_info[row].top_space_default) {
                if (use_booktabs)
                        os << "\\addlinespace\n";
                else
                        os << "\\noalign{\\vskip\\doublerulesep}\n";
-               ++ret;
        } else if(!row_info[row].top_space.zero()) {
                if (use_booktabs)
                        os << "\\addlinespace["
@@ -2469,22 +2445,23 @@ int Tabular::TeXRow(odocstream & os, row_type row,
                           << from_ascii(row_info[row].top_space.asLatexString())
                           << "}\n";
                }
-               ++ret;
        }
        bool ismulticol = false;
        bool ismultirow = false;
        for (col_type c = 0; c < ncols(); ++c) {
                if (isPartOfMultiColumn(row, c))
                        continue;
-                       
-               if (isPartOfMultiRow(row, c) && 
-                       column_info[c].alignment != LYX_ALIGN_DECIMAL) {
-                       os << " & "; 
+
+               cell = cellIndex(row, c);
+
+               if (isPartOfMultiRow(row, c)
+                   && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
+                       if (cell != getLastCellInRow(row))
+                               os << " & "; 
                        continue;
                }
 
-               cell = cellIndex(row, c);
-               ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
+               TeXCellPreamble(os, cell, ismulticol, ismultirow);
                shared_ptr<InsetTableCell> inset = cellInset(cell);
 
                Paragraph const & par = inset->paragraphs().front();
@@ -2512,31 +2489,41 @@ int Tabular::TeXRow(odocstream & os, row_type row,
                                    ? OutputParams::PLAIN
                                    : OutputParams::ALIGNED;
 
-               if (getAlignment(cell) == LYX_ALIGN_DECIMAL
-                       && cellInfo(cell).decimal_width != 0) {
+               if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
                        // copy cell and split in 2
                        InsetTableCell head = InsetTableCell(*cellInset(cell).get());
-                       head.getText(0)->setMacrocontextPosition(
-                               cellInset(cell)->getText(0)->macrocontextPosition());
                        head.setBuffer(buffer());
+                       DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
+                       dit.pop_back();
+                       dit.push_back(CursorSlice(head));
+                       head.setMacrocontextPositionRecursive(dit);
                        bool hassep = false;
                        InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
-                       tail.getText(0)->setMacrocontextPosition(
-                               head.getText(0)->macrocontextPosition());
-                       tail.setBuffer(head.buffer());
                        head.latex(os, newrp);
-                       os << '&';
-                       ret += tail.latex(os, newrp);
-               } else if (!isPartOfMultiRow(row, c))
-                       ret += inset->latex(os, newrp);
+                       if (hassep) {
+                               os << '&';
+                               tail.setBuffer(head.buffer());
+                               dit.pop_back();
+                               dit.push_back(CursorSlice(tail));
+                               tail.setMacrocontextPositionRecursive(dit);
+                               tail.latex(os, newrp);
+                       }
+               } else if (!isPartOfMultiRow(row, c)) {
+                       if (!runparams.nice)
+                               os.texrow().start(par.id(), 0);
+                       inset->latex(os, newrp);
+               }
 
                runparams.encoding = newrp.encoding;
                if (rtl)
                        os << '}';
 
-               ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
+               TeXCellPostamble(os, cell, ismulticol, ismultirow);
                if (cell != getLastCellInRow(row)) { // not last cell in row
-                       os << " & ";
+                       if (runparams.nice)
+                               os << " & ";
+                       else
+                               os << " &\n";
                }
        }
        if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
@@ -2559,17 +2546,15 @@ int Tabular::TeXRow(odocstream & os, row_type row,
                   << ']';
        }
        os << '\n';
-       ++ret;
 
        //output the bottom line
-       ret += TeXBottomHLine(os, row, lang);
+       TeXBottomHLine(os, row, lang);
 
        if (row_info[row].interline_space_default) {
                if (use_booktabs)
                        os << "\\addlinespace\n";
                else
                        os << "\\noalign{\\vskip\\doublerulesep}\n";
-               ++ret;
        } else if (!row_info[row].interline_space.zero()) {
                if (use_booktabs)
                        os << "\\addlinespace["
@@ -2579,24 +2564,25 @@ int Tabular::TeXRow(odocstream & os, row_type row,
                        os << "\\noalign{\\vskip"
                           << from_ascii(row_info[row].interline_space.asLatexString())
                           << "}\n";
-               ++ret;
        }
-       return ret;
 }
 
 
-int Tabular::latex(odocstream & os, OutputParams const & runparams) const
+void Tabular::latex(otexstream & os, OutputParams const & runparams) const
 {
-       int ret = 0;
+       bool const is_tabular_star = !tabular_width.zero();
 
        //+---------------------------------------------------------------------
        //+                      first the opening preamble                    +
        //+---------------------------------------------------------------------
 
-       if (rotate) {
+       os << safebreakln;
+       if (runparams.lastid != -1)
+               os.texrow().start(runparams.lastid, runparams.lastpos);
+
+       if (rotate)
                os << "\\begin{sideways}\n";
-               ++ret;
-       }
+
        if (is_long_tabular) {
                os << "\\begin{longtable}";
                switch (longtabular_alignment) {
@@ -2610,7 +2596,10 @@ int Tabular::latex(odocstream & os, OutputParams const & runparams) const
                        break;
                }
        } else {
-               os << "\\begin{tabular}";
+               if (is_tabular_star)
+                       os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
+               else
+                       os << "\\begin{tabular}";
                switch (tabular_valignment) {
                case LYX_VALIGN_TOP:
                        os << "[t]";
@@ -2625,6 +2614,9 @@ int Tabular::latex(odocstream & os, OutputParams const & runparams) const
        
        os << "{";
 
+       if (is_tabular_star)
+               os << "@{\\extracolsep{\\fill}}";
+
        for (col_type c = 0; c < ncols(); ++c) {
                if (columnLeftLine(c))
                        os << '|';
@@ -2685,9 +2677,8 @@ int Tabular::latex(odocstream & os, OutputParams const & runparams) const
                        os << '|';
        }
        os << "}\n";
-       ++ret;
 
-       ret += TeXLongtableHeaderFooter(os, runparams);
+       TeXLongtableHeaderFooter(os, runparams);
 
        //+---------------------------------------------------------------------
        //+                      the single row and columns (cells)            +
@@ -2695,11 +2686,9 @@ int Tabular::latex(odocstream & os, OutputParams const & runparams) const
 
        for (row_type r = 0; r < nrows(); ++r) {
                if (isValidRow(r)) {
-                       ret += TeXRow(os, r, runparams);
-                       if (is_long_tabular && row_info[r].newpage) {
+                       TeXRow(os, r, runparams);
+                       if (is_long_tabular && row_info[r].newpage)
                                os << "\\newpage\n";
-                               ++ret;
-                       }
                }
        }
 
@@ -2709,14 +2698,15 @@ int Tabular::latex(odocstream & os, OutputParams const & runparams) const
 
        if (is_long_tabular)
                os << "\\end{longtable}";
-       else
-               os << "\\end{tabular}";
-       if (rotate) {
-               os << "\n\\end{sideways}";
-               ++ret;
+       else {
+               if (is_tabular_star)
+                       os << "\\end{tabular*}";
+               else
+                       os << "\\end{tabular}";
        }
 
-       return ret;
+       if (rotate)
+               os << breakln << "\\end{sideways}";
 }
 
 
@@ -2869,14 +2859,15 @@ int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
 
 
 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
-                          OutputParams const & runparams) const
+                          OutputParams const & runparams, bool header) const
 {
        docstring ret;
+       string const celltag = header ? "th" : "td";
        idx_type cell = getFirstCellInRow(row);
 
        xs << html::StartTag("tr");
        for (col_type c = 0; c < ncols(); ++c) {
-               if (isPartOfMultiColumn(row, c))
+               if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
                        continue;
 
                stringstream attr;
@@ -2908,10 +2899,12 @@ docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
 
                if (isMultiColumn(cell))
                        attr << " colspan='" << columnSpan(cell) << "'";
+               else if (isMultiRow(cell))
+                       attr << " rowspan='" << rowSpan(cell) << "'";
 
-               xs << html::StartTag("td", attr.str());
+               xs << html::StartTag(celltag, attr.str()) << html::CR();
                ret += cellInset(cell)->xhtml(xs, runparams);
-               xs << html::EndTag("td");
+               xs << html::EndTag(celltag) << html::CR();
                ++cell;
        }
        xs << html::EndTag("tr");
@@ -2922,16 +2915,78 @@ docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
 {
        docstring ret;
-       // It's unclear to me if we need to mess with the long table stuff. 
-       // We can borrow that too from docbook, if so.
 
-       xs << html::StartTag("tbody");
+       if (is_long_tabular) {
+               // we'll wrap it in a div, so as to deal with alignment
+               string align;
+               switch (longtabular_alignment) {
+               case LYX_LONGTABULAR_ALIGN_LEFT:
+                       align = "left";
+                       break;
+               case LYX_LONGTABULAR_ALIGN_CENTER:
+                       align = "center";
+                       break;
+               case LYX_LONGTABULAR_ALIGN_RIGHT:
+                       align = "right";
+                       break;
+               }
+               xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
+                  << html::CR();
+               if (haveLTCaption()) {
+                       xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
+                          << html::CR();
+                       for (row_type r = 0; r < nrows(); ++r)
+                               if (row_info[r].caption)
+                                       ret += xhtmlRow(xs, r, runparams);
+                       xs << html::EndTag("div") << html::CR();
+               }
+       }
+
+       xs << html::StartTag("table") << html::CR();
+
+       // output header info
+       bool const havefirsthead = haveLTFirstHead();
+       // if we have a first head, then we are going to ignore the
+       // headers for the additional pages, since there aren't any
+       // in XHTML. this test accomplishes that.
+       bool const havehead = !havefirsthead && haveLTHead();
+       if (havehead || havefirsthead) {
+               xs << html::StartTag("thead") << html::CR();
+               for (row_type r = 0; r < nrows(); ++r) {
+                       if ((havefirsthead && row_info[r].endfirsthead)
+                           || (havehead && row_info[r].endhead)) {
+                               ret += xhtmlRow(xs, r, runparams, true);
+                       }
+               }
+               xs << html::EndTag("thead") << html::CR();
+       }
+       // output footer info
+       bool const havelastfoot = haveLTLastFoot();
+       // as before.
+       bool const havefoot = !havelastfoot && haveLTFoot();
+       if (havefoot || havelastfoot) {
+               xs << html::StartTag("tfoot") << html::CR();
+               for (row_type r = 0; r < nrows(); ++r) {
+                       if ((havelastfoot && row_info[r].endlastfoot)
+                           || (havefoot && row_info[r].endfoot)) {
+                               ret += xhtmlRow(xs, r, runparams);
+                       }
+               }
+               xs << html::EndTag("tfoot") << html::CR();
+       }
+
+       xs << html::StartTag("tbody") << html::CR();
        for (row_type r = 0; r < nrows(); ++r) {
                if (isValidRow(r)) {
                        ret += xhtmlRow(xs, r, runparams);
                }
        }
-       xs << html::EndTag("tbody");
+       xs << html::EndTag("tbody")
+          << html::CR()
+          << html::EndTag("table")
+          << html::CR();
+       if (is_long_tabular)
+               xs << html::EndTag("div") << html::CR();
        return ret;
 }
 
@@ -3124,7 +3179,7 @@ void Tabular::plaintext(odocstream & os,
                if (!onlydata && plaintextTopHLine(os, r, clen))
                        os << docstring(depth * 2, ' ');
                for (col_type c = 0; c < ncols(); ++c) {
-                       if (isPartOfMultiColumn(r, c))
+                       if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
                                continue;
                        if (onlydata && c > 0)
                                // we don't use operator<< for single UCS4 character.
@@ -3425,30 +3480,32 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                                mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
 
                        // determine horizontal offset because of decimal align (if necessary)
-                       int decimal_hoffset = 0;
                        int decimal_width = 0;
                        if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
-                               // make a copy which we will split in 2
-                               InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
-                               head.getText(0)->setMacrocontextPosition(
-                                       tabular.cellInset(cell)->getText(0)->macrocontextPosition());
-                               head.setBuffer(tabular.buffer());
-                               // split in 2 and calculate width of each part
-                               bool hassep = false;
-                               InsetTableCell tail = 
-                                       splitCell(head, tabular.column_info[c].decimal_point, hassep);
-                               tail.getText(0)->setMacrocontextPosition(
-                                       head.getText(0)->macrocontextPosition());
-                               tail.setBuffer(head.buffer());
-                               Dimension dim1;
-                               head.metrics(m, dim1);
-                               decimal_hoffset = dim1.width();
-                               if (hassep) {
+                               InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
+                               tail.setBuffer(tabular.buffer());
+                               // we need to set macrocontext position everywhere
+                               // otherwise we crash with nested insets (e.g. footnotes)
+                               // after decimal point
+                               DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
+                               dit.pop_back();
+                               dit.push_back(CursorSlice(tail));
+                               tail.setMacrocontextPositionRecursive(dit);
+
+                               // remove text leading decimal point
+                               docstring const align_d = tabular.column_info[c].decimal_point;
+                               dit = separatorPos(&tail, align_d);
+
+                               pit_type const psize = tail.paragraphs().front().size();
+                               if (dit) {
+                                       tail.paragraphs().front().eraseChars(0,
+                                               dit.pos() < psize ? dit.pos() + 1 : psize, false);
+                                       Dimension dim1;
                                        tail.metrics(m, dim1);
                                        decimal_width = dim1.width();
                                }
                        }
-                       tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
+                       tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
                        tabular.cell_info[r][c].decimal_width = decimal_width;
 
                        // with LYX_VALIGN_BOTTOM the descent is relative to the last par
@@ -3969,6 +4026,8 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cmd = FuncRequest(finish_lfun);
                else
                        cur.dispatched();
+
+               cur.screenUpdateFlags(Update::Force | Update::FitCursor);
                break;
 
        }
@@ -3985,11 +4044,15 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        // setting also the right targetX.
                        cur.selHandle(act == LFUN_DOWN_SELECT);
                        if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
+                               int const xtarget = cur.targetX();
+                               // WARNING: Once cur.idx() has been reset, the cursor is in
+                               // an inconsistent state until pos() has been set. Be careful
+                               // what you do with it!
                                cur.idx() = tabular.cellBelow(cur.idx());
                                cur.pit() = 0;
                                TextMetrics const & tm =
                                        cur.bv().textMetrics(cell(cur.idx())->getText(0));
-                               cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
+                               cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
                                cur.setCurrentFont();
                        }
                }
@@ -4005,6 +4068,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.setCurrentFont();
                        return;
                }
+               cur.screenUpdateFlags(Update::FitCursor);
                break;
 
        case LFUN_UP_SELECT:
@@ -4018,13 +4082,17 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        // setting also the right targetX.
                        cur.selHandle(act == LFUN_UP_SELECT);
                        if (tabular.cellRow(cur.idx()) != 0) {
+                               int const xtarget = cur.targetX();
+                               // WARNING: Once cur.idx() has been reset, the cursor is in
+                               // an inconsistent state until pos() has been set. Be careful
+                               // what you do with it!
                                cur.idx() = tabular.cellAbove(cur.idx());
                                cur.pit() = cur.lastpit();
                                Text const * text = cell(cur.idx())->getText(0);
                                TextMetrics const & tm = cur.bv().textMetrics(text);
                                ParagraphMetrics const & pm =
                                        tm.parMetrics(cur.lastpit());
-                               cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
+                               cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
                                cur.setCurrentFont();
                        }
                }
@@ -4038,6 +4106,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.setCurrentFont();
                        return;
                }
+               cur.screenUpdateFlags(Update::FitCursor);
                break;
 
 //     case LFUN_SCREEN_DOWN: {
@@ -4249,8 +4318,16 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                if (&cur.inset() != this || cmd.getArg(0) != "tabular") 
                        break;
 
-               string const s = cmd.getArg(1);
                // FIXME: We only check for the very first argument...
+               string const s = cmd.getArg(1);
+               // We always enable the lfun if it is coming from the dialog
+               // because the dialog makes sure all the settings are valid,
+               // even though the first argument might not be valid now.
+               if (s == "from-dialog") {
+                       status.setEnabled(true);
+                       return true;
+               }
+
                int action = Tabular::LAST_ACTION;
                int i = 0;
                for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
@@ -4293,6 +4370,11 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        status.clear();
                        return true;
 
+               case Tabular::SET_TABULAR_WIDTH:
+                       status.setEnabled(!tabular.rotate &&  !tabular.is_long_tabular
+                               && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
+                       break;
+
                case Tabular::SET_DECIMAL_POINT:
                        status.setEnabled(
                                tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
@@ -4431,18 +4513,22 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
 
                case Tabular::TOGGLE_ROTATE_TABULAR:
                case Tabular::SET_ROTATE_TABULAR:
+                       status.setEnabled(tabular.tabular_width.zero());
                        status.setOnOff(tabular.rotate);
                        break;
 
                case Tabular::TABULAR_VALIGN_TOP:
+                       status.setEnabled(tabular.tabular_width.zero());
                        status.setOnOff(tabular.tabular_valignment 
                                == Tabular::LYX_VALIGN_TOP);
                        break;
                case Tabular::TABULAR_VALIGN_MIDDLE:
+                       status.setEnabled(tabular.tabular_width.zero());
                        status.setOnOff(tabular.tabular_valignment 
                                == Tabular::LYX_VALIGN_MIDDLE);
                        break;
                case Tabular::TABULAR_VALIGN_BOTTOM:
+                       status.setEnabled(tabular.tabular_width.zero());
                        status.setOnOff(tabular.tabular_valignment 
                                == Tabular::LYX_VALIGN_BOTTOM);
                        break;
@@ -4591,8 +4677,13 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
        }
 
        // disable in non-fixed-width cells
-       case LFUN_NEWLINE_INSERT:
-       case LFUN_BREAK_PARAGRAPH: {
+       case LFUN_BREAK_PARAGRAPH:
+               // multirow does not allow paragraph breaks
+               if (tabular.isMultiRow(cur.idx())) {
+                       status.setEnabled(false);
+                       return true;
+               }
+       case LFUN_NEWLINE_INSERT: {
                if (tabular.getPWidth(cur.idx()).zero()) {
                        status.setEnabled(false);
                        return true;
@@ -4653,9 +4744,9 @@ Inset::DisplayType InsetTabular::display() const
 }
 
 
-int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
+void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
 {
-       return tabular.latex(os, runparams);
+       tabular.latex(os, runparams);
 }
 
 
@@ -4697,13 +4788,7 @@ int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
 
 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 {
-       // FIXME XHTML
-       // It'd be better to be able to get this from an InsetLayout, but at present
-       // InsetLayouts do not seem really to work for things that aren't InsetTexts.
-       xs << html::StartTag("table");
-       docstring ret = tabular.xhtml(xs, rp);
-       xs << html::EndTag("table");
-       return ret;
+       return tabular.xhtml(xs, rp);
 }
 
 
@@ -5126,34 +5211,21 @@ void InsetTabular::tabularFeatures(Cursor & cur,
 
        switch (feature) {
 
+       case Tabular::SET_TABULAR_WIDTH:
+               tabular.setTabularWidth(Length(value));
+               break;
+
        case Tabular::SET_PWIDTH: {
                Length const len(value);
                tabular.setColumnPWidth(cur, cur.idx(), len);
                if (len.zero()
                    && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
                        tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
-               // check if there is a 1-column multicolumn cell
-               // if so it must get the same width
-               for (row_type r = 0; r < tabular.nrows(); ++r) {
-                       if (tabular.isMultiColumn(tabular.cellIndex(r, column))
-                               && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
-                               tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), len);
-               }
                break;
        }
 
        case Tabular::SET_MPWIDTH:
                tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
-               // if the multicolumn only spans 1 column, the width of the whole column
-               // must have the same width, see bug #7055
-               if (tabular.columnSpan(cur.idx()) == 1)
-                       for (row_type r = 0; r < tabular.nrows(); ++r) {
-                               if (!tabular.isMultiColumn(tabular.cellIndex(r, column)))
-                                       tabular.setColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
-                               else if (tabular.isMultiColumn(tabular.cellIndex(r, column))
-                                       && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
-                                       tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
-                       }
                break;
 
        case Tabular::SET_MROFFSET: