]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
Allow LyX format to be written to View>Source window.
[lyx.git] / src / insets / InsetTabular.cpp
index dc6effe946a1b79fabaf1447c607fc11dd371ef7..8e796dbbf9561832593ecaab76b0c3fe7fe86cf1 100644 (file)
@@ -149,12 +149,12 @@ TabularFeature tabularFeature[] =
        { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
        { Tabular::SET_PWIDTH, "set-pwidth", true },
        { Tabular::SET_MPWIDTH, "set-mpwidth", true },
-       { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
-       { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
-       { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
-       { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
-       { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
-       { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
+       { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
+       { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
+       { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
+       { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
+       { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
+       { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
        { Tabular::SET_USEBOX, "set-usebox", true },
        { Tabular::SET_LTHEAD, "set-lthead", true },
        { Tabular::UNSET_LTHEAD, "unset-lthead", true },
@@ -165,6 +165,7 @@ TabularFeature tabularFeature[] =
        { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
        { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
        { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
+       { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
        { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
        { Tabular::SET_LTCAPTION, "set-ltcaption", false },
        { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
@@ -188,62 +189,6 @@ TabularFeature tabularFeature[] =
 };
 
 
-template <class T>
-string const write_attribute(string const & name, T const & t)
-{
-       string const s = tostr(t);
-       return s.empty() ? s : " " + name + "=\"" + s + "\"";
-}
-
-template <>
-string const write_attribute(string const & name, string const & t)
-{
-       return t.empty() ? t : " " + name + "=\"" + t + "\"";
-}
-
-
-template <>
-string const write_attribute(string const & name, docstring const & t)
-{
-       return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
-}
-
-
-template <>
-string const write_attribute(string const & name, bool const & b)
-{
-       // we write only true attribute values so we remove a bit of the
-       // file format bloat for tabulars.
-       return b ? write_attribute(name, convert<string>(b)) : string();
-}
-
-
-template <>
-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();
-}
-
-
-template <>
-string const write_attribute(string const & name, Tabular::idx_type 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();
-}
-
-
-template <>
-string const write_attribute(string const & name, Length const & value)
-{
-       // we write only the value if we really have one same reson as above.
-       return value.zero() ? string() : write_attribute(name, value.asString());
-}
-
-
 string const tostr(LyXAlignment const & num)
 {
        switch (num) {
@@ -503,6 +448,61 @@ void l_getline(istream & is, string & str)
        }
 }
 
+template <class T>
+string const write_attribute(string const & name, T const & t)
+{
+       string const s = tostr(t);
+       return s.empty() ? s : " " + name + "=\"" + s + "\"";
+}
+
+template <>
+string const write_attribute(string const & name, string const & t)
+{
+       return t.empty() ? t : " " + name + "=\"" + t + "\"";
+}
+
+
+template <>
+string const write_attribute(string const & name, docstring const & t)
+{
+       return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
+}
+
+
+template <>
+string const write_attribute(string const & name, bool const & b)
+{
+       // we write only true attribute values so we remove a bit of the
+       // file format bloat for tabulars.
+       return b ? write_attribute(name, convert<string>(b)) : string();
+}
+
+
+template <>
+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();
+}
+
+
+template <>
+string const write_attribute(string const & name, Tabular::idx_type 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();
+}
+
+
+template <>
+string const write_attribute(string const & name, Length const & value)
+{
+       // we write only the value if we really have one same reson as above.
+       return value.zero() ? string() : write_attribute(name, value.asString());
+}
+
 } // namespace
 
 
@@ -566,7 +566,7 @@ Tabular::CellData::CellData(Buffer * buf)
          left_line(false),
          right_line(false),
          usebox(BOX_NONE),
-         rotate(false),
+         rotate(0),
          inset(new InsetTableCell(buf))
 {
        inset->setBuffer(*buf);
@@ -687,7 +687,7 @@ void Tabular::init(Buffer * buf, row_type rows_arg,
        tabular_valignment = LYX_VALIGN_MIDDLE;
        tabular_width = Length();
        longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
-       rotate = false;
+       rotate = 0;
        use_booktabs = false;
        // set silly default lines
        for (row_type r = 0; r < nrows(); ++r)
@@ -701,43 +701,6 @@ void Tabular::init(Buffer * buf, row_type rows_arg,
 }
 
 
-void Tabular::appendRow(idx_type const cell)
-{
-       row_type const row = cellRow(cell);
-
-       row_info.insert(row_info.begin() + row + 1, RowData());
-       row_info[row + 1] = row_info[row];
-
-       cell_info.insert(cell_info.begin() + row + 1,
-               cell_vector(ncols(), CellData(buffer_)));
-       for (col_type c = 0; c < ncols(); ++c) {
-               if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
-                       cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
-               else
-                       cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
-       }
-       updateIndexes();
-
-       for (col_type c = 0; c < ncols(); ++c) {
-               if (isPartOfMultiRow(row, c))
-                       continue;
-               // inherit line settings
-               idx_type const i = cellIndex(row + 1, c);
-               idx_type const j = cellIndex(row, c);
-               setLeftLine(i, leftLine(j));
-               setRightLine(i, rightLine(j));
-               setTopLine(i, topLine(j));
-               if (topLine(j) && bottomLine(j)) {
-                       setBottomLine(i, true);
-                       setBottomLine(j, false);
-               }
-               // mark track changes
-               if (buffer().params().trackChanges)
-                       cellInfo(i).inset->setChange(Change(Change::INSERTED));
-       }
-}
-
-
 void Tabular::deleteRow(row_type const row)
 {
        // Not allowed to delete last row
@@ -760,44 +723,46 @@ void Tabular::deleteRow(row_type const row)
 
 void Tabular::copyRow(row_type const row)
 {
-       row_info.insert(row_info.begin() + row, row_info[row]);
-       cell_info.insert(cell_info.begin() + row, cell_info[row]);
+       insertRow(row, true);
+}
 
-       if (buffer().params().trackChanges)
-               for (col_type c = 0; c < ncols(); ++c)
-                       cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
 
-       updateIndexes();
+void Tabular::appendRow(row_type row)
+{
+       insertRow(row, false);
 }
 
 
-void Tabular::appendColumn(idx_type const cell)
+void Tabular::insertRow(row_type const row, bool copy)
 {
-       col_type const c = cellColumn(cell);
+       row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
+       cell_info.insert(cell_info.begin() + row + 1, 
+               cell_vector(0, CellData(buffer_)));
        
-       column_info.insert(column_info.begin() + c + 1, ColumnData());
-       column_info[c + 1] = column_info[c];
-
-       for (row_type r = 0; r < nrows(); ++r) {
-               cell_info[r].insert(cell_info[r].begin() + c + 1, 
-                       CellData(buffer_));
-               if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
-                       cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
-               else
-                       cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
+       for (col_type c = 0; c < ncols(); ++c) {
+               cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
+                       copy ? CellData(cell_info[row][c]) : CellData(buffer_));
+               if (buffer().params().trackChanges)
+                       cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
+               if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
+                       cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
        }
+       
        updateIndexes();
-       for (row_type r = 0; r < nrows(); ++r) {
+       for (col_type c = 0; c < ncols(); ++c) {
+               if (isPartOfMultiRow(row, c))
+                       continue;
                // inherit line settings
-               idx_type const i = cellIndex(r, c + 1);
-               idx_type const j = cellIndex(r, c);
-               setBottomLine(i, bottomLine(j));
-               setTopLine(i, topLine(j));
+               idx_type const i = cellIndex(row + 1, c);
+               idx_type const j = cellIndex(row, c);
                setLeftLine(i, leftLine(j));
-               if (rightLine(j) && rightLine(j)) {
-                       setRightLine(i, true);
-                       setRightLine(j, false);
+               setRightLine(i, rightLine(j));
+               setTopLine(i, topLine(j));
+               if (topLine(j) && bottomLine(j)) {
+                       setBottomLine(i, true);
+                       setBottomLine(j, false);
                }
+               // mark track changes
                if (buffer().params().trackChanges)
                        cellInfo(i).inset->setChange(Change(Change::INSERTED));
        }
@@ -825,16 +790,45 @@ void Tabular::deleteColumn(col_type const col)
 
 
 void Tabular::copyColumn(col_type const col)
+{
+       insertColumn(col, true);
+}
+
+
+void Tabular::appendColumn(col_type col)
+{      
+       insertColumn(col, false);
+}
+
+
+void Tabular::insertColumn(col_type const col, bool copy)
 {
        BufferParams const & bp = buffer().params();
-       column_info.insert(column_info.begin() + col, column_info[col]);
+       column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
 
        for (row_type r = 0; r < nrows(); ++r) {
-               cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
+               cell_info[r].insert(cell_info[r].begin() + col + 1,
+                       copy ? CellData(cell_info[r][col]) : CellData(buffer_));
                if (bp.trackChanges)
                        cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
+               if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
+                       cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
        }
        updateIndexes();
+       for (row_type r = 0; r < nrows(); ++r) {
+               // inherit line settings
+               idx_type const i = cellIndex(r, col + 1);
+               idx_type const j = cellIndex(r, col);
+               setBottomLine(i, bottomLine(j));
+               setTopLine(i, topLine(j));
+               setLeftLine(i, leftLine(j));
+               if (rightLine(j) && rightLine(j)) {
+                       setRightLine(i, true);
+                       setRightLine(j, false);
+               }
+               if (buffer().params().trackChanges)
+                       cellInfo(i).inset->setChange(Change(Change::INSERTED));
+       }
 }
 
 
@@ -1395,7 +1389,7 @@ void Tabular::write(ostream & os) const
           << ">\n";
        // global longtable options
        os << "<features"
-          << write_attribute("rotate", rotate)
+          << write_attribute("rotate", convert<string>(rotate))
           << write_attribute("booktabs", use_booktabs)
           << write_attribute("islongtable", is_long_tabular)
           << write_attribute("firstHeadTopDL", endfirsthead.topDL)
@@ -1615,7 +1609,8 @@ Tabular::CellData & Tabular::cellInfo(idx_type cell) const
 }
 
 
-Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
+Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
+                                         bool const right_border)
 {
        idx_type const col = cellColumn(cell);
        idx_type const row = cellRow(cell);
@@ -1629,7 +1624,7 @@ Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
        if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
                cs.alignment = column_info[col].alignment;
        if (col > 0)
-               setRightLine(cell, rightLine(cellIndex(row, col - 1)));
+               setRightLine(cell, right_border);
 
        for (idx_type i = 1; i < number; ++i) {
                CellData & cs1 = cellInfo(cell + i);
@@ -1649,7 +1644,8 @@ bool Tabular::isMultiRow(idx_type cell) const
 }
 
 
-Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
+Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
+                                      bool const bottom_border)
 {
        idx_type const col = cellColumn(cell);
        idx_type const row = cellRow(cell);
@@ -1668,10 +1664,10 @@ Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
        // this feature would be a fileformat change
        // until LyX supports this, use the deault alignment of multirow
        // cells: left
-       cs.alignment = LYX_ALIGN_LEFT; 
+       cs.alignment = LYX_ALIGN_LEFT;
 
-       // set the bottom row of the last selected cell
-       setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
+       // set the bottom line of the last selected cell
+       setBottomLine(cell, bottom_border);
 
        for (idx_type i = 1; i < number; ++i) {
                CellData & cs1 = cell_info[row + i][col];
@@ -1746,13 +1742,13 @@ void Tabular::unsetMultiRow(idx_type cell)
 }
 
 
-void Tabular::setRotateCell(idx_type cell, bool flag)
+void Tabular::setRotateCell(idx_type cell, int value)
 {
-       cellInfo(cell).rotate = flag;
+       cellInfo(cell).rotate = value;
 }
 
 
-bool Tabular::getRotateCell(idx_type cell) const
+int Tabular::getRotateCell(idx_type cell) const
 {
        return cellInfo(cell).rotate;
 }
@@ -1764,7 +1760,7 @@ bool Tabular::needRotating() const
                return true;
        for (row_type r = 0; r < nrows(); ++r)
                for (col_type c = 0; c < ncols(); ++c)
-                       if (cell_info[r][c].rotate)
+                       if (cell_info[r][c].rotate != 0)
                                return true;
        return false;
 }
@@ -1905,45 +1901,49 @@ bool Tabular::getLTNewPage(row_type row) const
 }
 
 
-bool Tabular::haveLTHead() const
+bool Tabular::haveLTHead(bool withcaptions) const
 {
        if (!is_long_tabular)
                return false;
        for (row_type i = 0; i < nrows(); ++i)
-               if (row_info[i].endhead)
+               if (row_info[i].endhead &&
+                   (withcaptions || !row_info[i].caption))
                        return true;
        return false;
 }
 
 
-bool Tabular::haveLTFirstHead() const
+bool Tabular::haveLTFirstHead(bool withcaptions) const
 {
        if (!is_long_tabular || endfirsthead.empty)
                return false;
        for (row_type r = 0; r < nrows(); ++r)
-               if (row_info[r].endfirsthead)
+               if (row_info[r].endfirsthead &&
+                   (withcaptions || !row_info[r].caption))
                        return true;
        return false;
 }
 
 
-bool Tabular::haveLTFoot() const
+bool Tabular::haveLTFoot(bool withcaptions) const
 {
        if (!is_long_tabular)
                return false;
        for (row_type r = 0; r < nrows(); ++r)
-               if (row_info[r].endfoot)
+               if (row_info[r].endfoot &&
+                   (withcaptions || !row_info[r].caption))
                        return true;
        return false;
 }
 
 
-bool Tabular::haveLTLastFoot() const
+bool Tabular::haveLTLastFoot(bool withcaptions) const
 {
        if (!is_long_tabular || endlastfoot.empty)
                return false;
        for (row_type r = 0; r < nrows(); ++r)
-               if (row_info[r].endlastfoot)
+               if (row_info[r].endlastfoot &&
+                   (withcaptions || !row_info[r].caption))
                        return true;
        return false;
 }
@@ -1953,11 +1953,16 @@ Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
 {
        idx_type i = getFirstCellInRow(row);
        if (what) {
-               setMultiColumn(i, numberOfCellsInRow(row));
+               setMultiColumn(i, numberOfCellsInRow(row), false);
                setTopLine(i, false);
                setBottomLine(i, false);
                setLeftLine(i, false);
                setRightLine(i, false);
+               if (!row_info[row].endfirsthead && !row_info[row].endhead &&
+                   !row_info[row].endfoot && !row_info[row].endlastfoot) {
+                       setLTHead(row, true, endfirsthead, true);
+                       row_info[row].endfirsthead = true;
+               }
        } else {
                unsetMultiColumn(i);
                // When unsetting a caption row, also all existing
@@ -1974,13 +1979,34 @@ bool Tabular::ltCaption(row_type row) const
 }
 
 
-bool Tabular::haveLTCaption() const
+bool Tabular::haveLTCaption(CaptionType captiontype) const
 {
        if (!is_long_tabular)
                return false;
-       for (row_type r = 0; r < nrows(); ++r)
-               if (row_info[r].caption)
-                       return true;
+       for (row_type r = 0; r < nrows(); ++r) {
+               if (row_info[r].caption) {
+                       switch (captiontype) {
+                       case CAPTION_FIRSTHEAD:
+                               if (row_info[r].endfirsthead)
+                                       return true;
+                               break;
+                       case CAPTION_HEAD:
+                               if (row_info[r].endhead)
+                                       return true;
+                               break;
+                       case CAPTION_FOOT:
+                               if (row_info[r].endfoot)
+                                       return true;
+                               break;
+                       case CAPTION_LASTFOOT:
+                               if (row_info[r].endlastfoot)
+                                       return true;
+                               break;
+                       case CAPTION_ANY:
+                               return true;
+                       }
+               }
+       }
        return false;
 }
 
@@ -2290,9 +2316,9 @@ void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
                os << "{";
        } // end if ismultirow
 
-       if (getRotateCell(cell)) {
-               os << "\\begin{sideways}\n";
-       }
+       if (getRotateCell(cell) != 0)
+               os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
+
        if (getUsebox(cell) == BOX_PARBOX) {
                os << "\\parbox[";
                switch (valign) {
@@ -2339,8 +2365,8 @@ void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
                os << '}';
        else if (getUsebox(cell) == BOX_MINIPAGE)
                os << breakln << "\\end{minipage}";
-       if (getRotateCell(cell))
-               os << breakln << "\\end{sideways}";
+       if (getRotateCell(cell) != 0)
+               os << breakln << "\\end{turn}";
        if (ismultirow)
                os << '}';
        if (ismulticol)
@@ -2354,17 +2380,7 @@ void Tabular::TeXLongtableHeaderFooter(otexstream & os,
        if (!is_long_tabular)
                return;
 
-       // 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)
-                               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)
                        os << "\\hline\n";
@@ -2486,7 +2502,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
                                os << "\\textFR{";
                        else if (lang == "arabic_arabi")
                                os << "\\textAR{";
-                       // currently, remaning RTL languages are
+                       // currently, remaining RTL languages are
                        // arabic_arabtex and hebrew
                        else
                                os << "\\R{";
@@ -2536,13 +2552,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
                                os << " &\n";
                }
        }
-       if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
-               // if no first header and no empty first header is used,
-               // the caption needs to be terminated by \endfirsthead
-               // (bug 6057)
-               os << "\\endfirsthead";
-       else
-               os << "\\tabularnewline";
+       os << "\\tabularnewline";
        if (row_info[row].bottom_space_default) {
                if (use_booktabs)
                        os << "\\addlinespace";
@@ -2590,8 +2600,8 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
        if (runparams.lastid != -1)
                os.texrow().start(runparams.lastid, runparams.lastpos);
 
-       if (rotate)
-               os << "\\begin{sideways}\n";
+       if (rotate != 0)
+               os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
 
        if (is_long_tabular) {
                os << "\\begin{longtable}";
@@ -2715,8 +2725,8 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
                        os << "\\end{tabular}";
        }
 
-       if (rotate)
-               os << breakln << "\\end{sideways}";
+       if (rotate != 0)
+               os << breakln << "\\end{turn}";
 }
 
 
@@ -2808,6 +2818,7 @@ int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
        //+---------------------------------------------------------------------
 
        // output caption info
+       // The caption flag wins over head/foot
        if (haveLTCaption()) {
                os << "<caption>\n";
                ++ret;
@@ -2820,11 +2831,12 @@ int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
                ++ret;
        }
        // output header info
-       if (haveLTHead() || haveLTFirstHead()) {
+       if (haveLTHead(false) || haveLTFirstHead(false)) {
                os << "<thead>\n";
                ++ret;
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endhead || row_info[r].endfirsthead) {
+                       if ((row_info[r].endhead || row_info[r].endfirsthead) &&
+                           !row_info[r].caption) {
                                ret += docbookRow(os, r, runparams);
                        }
                }
@@ -2832,11 +2844,12 @@ int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
                ++ret;
        }
        // output footer info
-       if (haveLTFoot() || haveLTLastFoot()) {
+       if (haveLTFoot(false) || haveLTLastFoot(false)) {
                os << "<tfoot>\n";
                ++ret;
                for (row_type r = 0; r < nrows(); ++r) {
-                       if (row_info[r].endfoot || row_info[r].endlastfoot) {
+                       if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
+                           !row_info[r].caption) {
                                ret += docbookRow(os, r, runparams);
                        }
                }
@@ -2942,6 +2955,7 @@ docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
                }
                xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
                   << html::CR();
+               // The caption flag wins over head/foot
                if (haveLTCaption()) {
                        xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
                           << html::CR();
@@ -2955,30 +2969,32 @@ docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
        xs << html::StartTag("table") << html::CR();
 
        // output header info
-       bool const havefirsthead = haveLTFirstHead();
+       bool const havefirsthead = haveLTFirstHead(false);
        // 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();
+       bool const havehead = !havefirsthead && haveLTHead(false);
        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)) {
+                       if (((havefirsthead && row_info[r].endfirsthead) ||
+                            (havehead && row_info[r].endhead)) &&
+                           !row_info[r].caption) {
                                ret += xhtmlRow(xs, r, runparams, true);
                        }
                }
                xs << html::EndTag("thead") << html::CR();
        }
        // output footer info
-       bool const havelastfoot = haveLTLastFoot();
+       bool const havelastfoot = haveLTLastFoot(false);
        // as before.
-       bool const havefoot = !havelastfoot && haveLTFoot();
+       bool const havefoot = !havelastfoot && haveLTFoot(false);
        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)) {
+                       if (((havelastfoot && row_info[r].endlastfoot) ||
+                            (havefoot && row_info[r].endfoot)) &&
+                           !row_info[r].caption) {
                                ret += xhtmlRow(xs, r, runparams);
                        }
                }
@@ -3398,7 +3414,7 @@ void InsetTabular::write(ostream & os) const
 }
 
 
-docstring InsetTabular::contextMenu(BufferView const &, int, int) const
+string InsetTabular::contextMenu(BufferView const &, int, int) const
 {
        // FIXME: depending on the selection state,
        // we could offer a different menu.
@@ -3406,9 +3422,9 @@ docstring InsetTabular::contextMenu(BufferView const &, int, int) const
 }
 
 
-docstring InsetTabular::contextMenuName() const
+string InsetTabular::contextMenuName() const
 {
-       return from_ascii("context-tabular");
+       return "context-tabular";
 }
 
 
@@ -4158,10 +4174,16 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.bv().showDialog("tabular");
                break;
 
-       case LFUN_INSET_MODIFY:
-               if (!tabularFeatures(cur, to_utf8(cmd.argument())))
+       case LFUN_INSET_MODIFY: {
+               string arg;
+               if (cmd.getArg(1) == "from-dialog")
+                       arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
+               else
+                       arg = to_utf8(cmd.argument());
+               if (!tabularFeatures(cur, arg))
                        cur.undispatched();
                break;
+       }
 
        // insert file functions
        case LFUN_FILE_INSERT_PLAINTEXT_PARA:
@@ -4523,8 +4545,7 @@ 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);
+                       status.setOnOff(tabular.rotate != 0);
                        break;
 
                case Tabular::TABULAR_VALIGN_TOP:
@@ -4557,7 +4578,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_ROTATE_TABULAR:
-                       status.setOnOff(!tabular.rotate);
+                       status.setOnOff(tabular.rotate == 0);
                        break;
 
                case Tabular::TOGGLE_ROTATE_CELL:
@@ -4576,67 +4597,87 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                // every row can only be one thing:
-               // either a footer or header or caption
+               // either a footer or header
                case Tabular::SET_LTFIRSTHEAD:
-                       status.setEnabled(sel_row_start == sel_row_end
-                               && !tabular.ltCaption(sel_row_start));
+                       status.setEnabled(sel_row_start == sel_row_end);
                        status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
                        break;
 
                case Tabular::UNSET_LTFIRSTHEAD:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
                        break;
 
                case Tabular::SET_LTHEAD:
-                       status.setEnabled(sel_row_start == sel_row_end
-                               && !tabular.ltCaption(sel_row_start));
+                       status.setEnabled(sel_row_start == sel_row_end);
                        status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
                        break;
 
                case Tabular::UNSET_LTHEAD:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
                        break;
 
                case Tabular::SET_LTFOOT:
-                       status.setEnabled(sel_row_start == sel_row_end
-                               && !tabular.ltCaption(sel_row_start));
+                       status.setEnabled(sel_row_start == sel_row_end);
                        status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
                        break;
 
                case Tabular::UNSET_LTFOOT:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
                        break;
 
                case Tabular::SET_LTLASTFOOT:
-                       status.setEnabled(sel_row_start == sel_row_end
-                               && !tabular.ltCaption(sel_row_start));
+                       status.setEnabled(sel_row_start == sel_row_end);
                        status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
                        break;
 
                case Tabular::UNSET_LTLASTFOOT:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
                        break;
 
                case Tabular::SET_LTNEWPAGE:
                        status.setOnOff(tabular.getLTNewPage(sel_row_start));
                        break;
+               case Tabular::UNSET_LTNEWPAGE:
+                       status.setOnOff(!tabular.getLTNewPage(sel_row_start));
+                       break;
 
-               // only one row can be the caption
+               // only one row in head/firsthead/foot/lasthead can be the caption
                // and a multirow cannot be set as caption
                case Tabular::SET_LTCAPTION:
-               case Tabular::UNSET_LTCAPTION:
-               case Tabular::TOGGLE_LTCAPTION:
                        status.setEnabled(sel_row_start == sel_row_end
-                               && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
-                               && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
-                               && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
-                               && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
-                               && (!tabular.haveLTCaption()
-                                       || tabular.ltCaption(sel_row_start))
+                               && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
+                                || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
+                               && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
+                                || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
+                               && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
+                                || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
+                               && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
+                                || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
                                && !tabular.isMultiRow(sel_row_start));
                        status.setOnOff(tabular.ltCaption(sel_row_start));
                        break;
 
+               case Tabular::UNSET_LTCAPTION:
+                       status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
+                       break;
+
+               case Tabular::TOGGLE_LTCAPTION:
+                       status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
+                               || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
+                                 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
+                                && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
+                                 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
+                                && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
+                                 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
+                                && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
+                                 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
+                       status.setOnOff(tabular.ltCaption(sel_row_start));
+                       break;
+
                case Tabular::SET_BOOKTABS:
                        status.setOnOff(tabular.use_booktabs);
                        break;
@@ -4809,10 +4850,9 @@ void InsetTabular::validate(LaTeXFeatures & features) const
        // 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.
        if (features.runparams().flavor == OutputParams::HTML)
-               features.addPreambleSnippet("<style type=\"text/css\">\n"
-      "table { border: 1px solid black; display: inline-block; }\n"
-      "td { border: 1px solid black; padding: 0.5ex; }\n"
-      "</style>");
+               features.addCSSSnippet(
+                       "table { border: 1px solid black; display: inline-block; }\n"
+                       "td { border: 1px solid black; padding: 0.5ex; }");
 }
 
 
@@ -4856,7 +4896,7 @@ int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
        int const xend = xbeg + tabular.cellWidth(cell);
        row_type const row = tabular.cellRow(cell);
        int const ybeg = o.y_ - tabular.rowAscent(row)
-               - tabular.interRowSpace(row);
+               - tabular.interRowSpace(row) - tabular.textVOffset(cell);
        int const yend = ybeg + tabular.cellHeight(cell);
 
        if (x < xbeg)
@@ -5107,7 +5147,6 @@ bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
                for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
                        if (s != tabularFeature[i].feature)
                                continue;
-
                        action = tabularFeature[i].action;
                        break;
                }
@@ -5150,12 +5189,17 @@ bool InsetTabular::oneCellHasRotationState(bool rotated,
 {
        for (row_type r = row_start; r <= row_end; ++r)
                for (col_type c = col_start; c <= col_end; ++c)
-                       if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
-                               return true;
-
+                       if (rotated) {
+                               if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
+                                       return true;
+                       } else {
+                               if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
+                                       return true;
+                       }
        return false;
 }
 
+
 void InsetTabular::tabularFeatures(Cursor & cur,
        Tabular::Feature feature, string const & value)
 {
@@ -5252,12 +5296,12 @@ void InsetTabular::tabularFeatures(Cursor & cur,
 
        case Tabular::APPEND_ROW:
                // append the row into the tabular
-               tabular.appendRow(cur.idx());
+               tabular.appendRow(row);
                break;
 
        case Tabular::APPEND_COLUMN:
                // append the column into the tabular
-               tabular.appendColumn(cur.idx());
+               tabular.appendColumn(column);
                cur.idx() = tabular.cellIndex(row, column);
                break;
 
@@ -5363,7 +5407,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        // just multicol for one single cell
                        // check whether we are completely in a multicol
                        if (!tabular.isMultiColumn(cur.idx()))
-                               tabular.setMultiColumn(cur.idx(), 1);
+                               tabular.setMultiColumn(cur.idx(), 1,
+                                       tabular.rightLine(cur.idx()));
                        break;
                }
                // we have a selection so this means we just add all this
@@ -5371,7 +5416,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                idx_type const s_start = cur.selBegin().idx();
                row_type const col_start = tabular.cellColumn(s_start);
                row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
-               cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
+               cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
+                                                  tabular.rightLine(cur.selEnd().idx()));
                cur.pit() = 0;
                cur.pos() = 0;
                cur.setSelection(false);
@@ -5387,10 +5433,27 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        }
 
        case Tabular::MULTICOLUMN: {
-               if (tabular.isMultiColumn(cur.idx()))
-                       tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
-               else
+               if (!cur.selection()) {
+                       if (tabular.isMultiColumn(cur.idx()))
+                               tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
+                       else
+                               tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
+                       break;
+               }
+               bool merge = false;
+               for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
+                       row_type const r = sel_row_start;
+                       if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
+                           || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
+                               merge = true;
+               }
+               // If the selection contains at least one singlecol cell
+               // or multiple multicol cells,
+               // we assume the user will merge is to a single multicol
+               if (merge)
                        tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
+               else
+                       tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
                break;
        }
 
@@ -5399,7 +5462,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        // just multirow for one single cell
                        // check whether we are completely in a multirow
                        if (!tabular.isMultiRow(cur.idx()))
-                               tabular.setMultiRow(cur.idx(), 1);
+                               tabular.setMultiRow(cur.idx(), 1,
+                                                   tabular.bottomLine(cur.idx()));
                        break;
                }
                // we have a selection so this means we just add all this
@@ -5407,7 +5471,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                idx_type const s_start = cur.selBegin().idx();
                row_type const row_start = tabular.cellRow(s_start);
                row_type const row_end = tabular.cellRow(cur.selEnd().idx());
-               cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
+               cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
+                                               tabular.bottomLine(cur.selEnd().idx()));
                cur.pit() = 0;
                cur.pos() = 0;
                cur.setSelection(false);
@@ -5423,10 +5488,27 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        }
 
        case Tabular::MULTIROW: {
-               if (tabular.isMultiRow(cur.idx()))
-                       tabularFeatures(cur, Tabular::UNSET_MULTIROW);
-               else
+               if (!cur.selection()) {
+                       if (tabular.isMultiRow(cur.idx()))
+                               tabularFeatures(cur, Tabular::UNSET_MULTIROW);
+                       else
+                               tabularFeatures(cur, Tabular::SET_MULTIROW);
+                       break;
+               }
+               bool merge = false;
+               for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
+                       col_type const c = sel_col_start;
+                       if (!tabular.isMultiRow(tabular.cellIndex(r, c))
+                           || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
+                               merge = true;
+               }
+               // If the selection contains at least one singlerow cell
+               // or multiple multirow cells,
+               // we assume the user will merge is to a single multirow
+               if (merge)
                        tabularFeatures(cur, Tabular::SET_MULTIROW);
+               else
+                       tabularFeatures(cur, Tabular::UNSET_MULTIROW);
                break;
        }
 
@@ -5471,15 +5553,16 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                break;
 
        case Tabular::SET_ROTATE_TABULAR:
-               tabular.rotate = true;
+               tabular.rotate = convert<int>(value);
                break;
 
        case Tabular::UNSET_ROTATE_TABULAR:
-               tabular.rotate = false;
+               tabular.rotate = 0;
                break;
 
        case Tabular::TOGGLE_ROTATE_TABULAR:
-               tabular.rotate = !tabular.rotate;
+               // when pressing the rotate button we default to 90° rotation
+               tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
                break;
 
        case Tabular::TABULAR_VALIGN_TOP:
@@ -5511,13 +5594,13 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        case Tabular::SET_ROTATE_CELL:
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
                        for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c), true);
+                               tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
                break;
 
        case Tabular::UNSET_ROTATE_CELL:
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
                        for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c), false);
+                               tabular.setRotateCell(tabular.cellIndex(r, c), 0);
                break;
 
        case Tabular::TOGGLE_ROTATE_CELL:
@@ -5526,9 +5609,13 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        sel_row_start, sel_row_end, sel_col_start, sel_col_end);
 
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
-                       for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c),
-                                                                         oneNotRotated);
+                       for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
+                               // when pressing the rotate cell button we default to 90° rotation
+                               if (oneNotRotated)
+                                       tabular.setRotateCell(tabular.cellIndex(r, c), 90);
+                               else
+                                       tabular.setRotateCell(tabular.cellIndex(r, c), 0);
+                       }
                }
                break;
 
@@ -5574,8 +5661,10 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                tabular.setLTFoot(row, flag, ltt, true);
                break;
 
+       case Tabular::UNSET_LTNEWPAGE:
+               flag = false;
        case Tabular::SET_LTNEWPAGE:
-               tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
+               tabular.setLTNewPage(row, flag);
                break;
 
        case Tabular::SET_LTCAPTION: {