]> git.lyx.org Git - features.git/commitdiff
Implement proper pasting from multiple table cells to non-table (#4447)
authorJuergen Spitzmueller <spitz@lyx.org>
Mon, 29 Jun 2020 13:13:45 +0000 (15:13 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Mon, 29 Jun 2020 13:13:45 +0000 (15:13 +0200)
src/CutAndPaste.cpp
src/insets/InsetTabular.cpp
src/insets/InsetTabular.h

index 4abcbaaa3b5bd82bcdf65f944c56ae20864bce24..09cedf8c4489d8313997c1ded4852fd3c3680813 100644 (file)
@@ -1087,22 +1087,42 @@ void copySelectionToTemp(Cursor & cur)
 
 void copySelection(Cursor const & cur, docstring const & plaintext)
 {
-       // In tablemode, because copy and paste actually use special table stack
-       // we do not attempt to get selected paragraphs under cursor. Instead, a
-       // paragraph with the plain text version is generated so that table cells
-       // can be pasted as pure text somewhere else.
+       // In tablemode, because copy and paste actually use a special table stack,
+       // we need to go through the cells and collect the paragraphs. 
+       // In math matrices, we generate a plain text version.
        if (cur.selBegin().idx() != cur.selEnd().idx()) {
                ParagraphList pars;
-               Paragraph par;
                BufferParams const & bp = cur.buffer()->params();
-               par.setLayout(bp.documentClass().plainLayout());
-               // Replace (column-separating) tabs by space (#4449)
-               docstring const clean_text = subst(plaintext, '\t', ' ');
-               // For pasting into text, we set the language to the paragraph language
-               // (rather than the default_language which is always English; see #11898)
-               par.insert(0, clean_text, Font(sane_font, par.getParLanguage(bp)),
-                          Change(Change::UNCHANGED));
-               pars.push_back(par);
+               if (cur.inMathed()) {
+                       Paragraph par;
+                       par.setLayout(bp.documentClass().plainLayout());
+                       // Replace (column-separating) tabs by space (#4449)
+                       docstring const clean_text = subst(plaintext, '\t', ' ');
+                       // For pasting into text, we set the language to the paragraph language
+                       // (rather than the default_language which is always English; see #11898)
+                       par.insert(0, clean_text, Font(sane_font, par.getParLanguage(bp)),
+                                  Change(Change::UNCHANGED));
+                       pars.push_back(par);
+               } else {
+                       // Get paragraphs from all cells
+                       InsetTabular * table = cur.inset().asInsetTabular();
+                       LASSERT(table, return);
+                       ParagraphList tplist = table->asParList(cur.selBegin().idx(), cur.selEnd().idx());
+                       for (auto & cpar : tplist) {
+                               cpar.setLayout(bp.documentClass().plainLayout());
+                               pars.push_back(cpar);
+                               // since the pars are merged later, we separate them by blank
+                               Paragraph epar;
+                               epar.insert(0, from_ascii(" "), Font(sane_font, epar.getParLanguage(bp)),
+                                           Change(Change::UNCHANGED));
+                               pars.push_back(epar);
+                       }
+                       // remove last empty par
+                       pars.pop_back();
+                       // merge all paragraphs to one
+                       while (pars.size() > 1)
+                               mergeParagraph(bp, pars, 0);
+               }
                theCuts.push(make_pair(pars, bp.documentClassPtr()));
        } else {
                copySelectionToStack(cur, theCuts);
index 68fff9b51f816e16746a13ee3571a60674876434..4dcb1c2bbaacb4cba263684c3ac60f250e82b164 100644 (file)
@@ -7206,6 +7206,22 @@ docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
 }
 
 
+ParagraphList InsetTabular::asParList(idx_type stidx, idx_type enidx)
+{
+       LASSERT(stidx <= enidx, return ParagraphList());
+       ParagraphList retval;
+       col_type const col1 = tabular.cellColumn(stidx);
+       col_type const col2 = tabular.cellColumn(enidx);
+       row_type const row1 = tabular.cellRow(stidx);
+       row_type const row2 = tabular.cellRow(enidx);
+       for (col_type col = col1; col <= col2; col++)
+               for (row_type row = row1; row <= row2; row++)
+                       for (auto par : tabular.cellInset(row, col)->paragraphs())
+                               retval.push_back(par);
+       return retval;
+}
+
+
 void InsetTabular::getSelection(Cursor & cur,
        row_type & rs, row_type & re, col_type & cs, col_type & ce) const
 {
index abb5e402f68dad7afe591a2206cebfa56d1bc3ec..0e76bdd9aeb0465128c851eb9ba67ff690a790b0 100644 (file)
@@ -1075,6 +1075,8 @@ public:
        /// writes the cells between stidx and enidx as a string, optionally
        /// descending into the insets
        docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
+       ///
+       ParagraphList asParList(idx_type stidx, idx_type enidx);
 
        /// Returns whether the cell in the specified row and column is selected.
        bool isCellSelected(Cursor & cur, row_type row, col_type col) const;