]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
Fix bug #7983: Do not modify params before we know we have
[lyx.git] / src / insets / InsetTabular.cpp
index 4a267e9d47e0f367d1a6fc232bde362f6bb8f3a7..dca6a18c3d021f92c48e63c5d2c5283a2adb7de9 100644 (file)
@@ -188,62 +188,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 +447,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
 
 
@@ -1339,7 +1338,13 @@ int Tabular::textVOffset(idx_type cell) const
 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
 {
        col_type c = 0;
-       while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
+       idx_type const numcells = numberOfCellsInRow(row);
+       // we check against numcells to make sure we do not crash if all the
+       // cells are multirow (bug #7535), but in that case our return value
+       // is really invalid, i.e., it is NOT the first cell in the row. but
+       // i do not know what to do here. (rgh)
+       while (c < numcells - 1
+              && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
                ++c;
        return cell_info[row][c].cellno;
 }
@@ -1348,6 +1353,9 @@ Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
 {
        col_type c = ncols() - 1;
+       // of course we check against 0 so we don't crash. but we have the same
+       // problem as in the previous routine: if all the cells are part of a
+       // multirow or part of a multi column, then our return value is invalid.
        while (c > 0
               && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
                   || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
@@ -1606,7 +1614,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);
@@ -1620,7 +1629,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);
@@ -1640,7 +1649,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);
@@ -1659,10 +1669,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];
@@ -1896,45 +1906,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;
 }
@@ -1944,11 +1958,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
@@ -1965,13 +1984,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;
 }
 
@@ -2048,8 +2088,8 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) cons
                // multirow, no line must be drawn.
                if (row != 0)
                        if (isMultiRow(cellIndex(row, c))
-                               && isMultiRow(cellIndex(row - 1, c)))
-                                       topline[c] = false;
+                           && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
+                               topline[c] = false;
                if (topline[c])
                        ++nset;
        }
@@ -2108,13 +2148,14 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) c
        for (col_type c = 0; c < ncols(); ++c) {
                bottomline.push_back(bottomLine(cellIndex(row, c)));
                topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
-               // If cell is part of a multirow and not the last or first cell of the
+               // If cell is part of a multirow and not the last cell of the
                // multirow, no line must be drawn.
                if (!lastrow)
                        if (isMultiRow(cellIndex(row, c))
-                               && isMultiRow(cellIndex(row + 1, c))) {
-                                       bottomline[c] = false;
-                                       topline[c] = false;
+                           && isMultiRow(cellIndex(row + 1, c))
+                           && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
+                               bottomline[c] = false;
+                               topline[c] = false;
                                }
                nextrowset &= topline[c];
        }
@@ -2344,17 +2385,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";
@@ -2476,7 +2507,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{";
@@ -2526,13 +2557,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";
@@ -2798,6 +2823,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;
@@ -2810,11 +2836,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);
                        }
                }
@@ -2822,11 +2849,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);
                        }
                }
@@ -2932,6 +2960,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();
@@ -2945,30 +2974,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);
                        }
                }
@@ -3388,7 +3419,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.
@@ -3396,9 +3427,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";
 }
 
 
@@ -4148,10 +4179,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:
@@ -4566,44 +4603,44 @@ 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;
 
@@ -4611,22 +4648,39 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        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;
@@ -4799,10 +4853,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; }");
 }
 
 
@@ -5353,7 +5406,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
@@ -5361,7 +5415,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);
@@ -5377,10 +5432,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;
        }
 
@@ -5389,7 +5461,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
@@ -5397,7 +5470,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);
@@ -5413,10 +5487,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;
        }