]> git.lyx.org Git - features.git/commitdiff
Allow pasting references to mathed
authorEnrico Forestieri <forenr@lyx.org>
Sat, 30 Jan 2021 00:32:40 +0000 (01:32 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Sat, 30 Jan 2021 00:32:40 +0000 (01:32 +0100)
A reference can be directly inserted into mathed but cannot be pasted
because the pasted material is returned in plain text format. This patch
allows getting a string from the cut stack in a suitable format allowing
the math parser to actually create an InsetRef.

Fixes #11539

src/CutAndPaste.cpp
src/CutAndPaste.h
src/Paragraph.cpp
src/Paragraph.h
src/mathed/InsetMathGrid.cpp

index fc555c1375bf157f91ea57d7a39544fa1feaf50f..306be8bac4d737ccf0795a7cf2a693108ea27e27 100644 (file)
@@ -1167,7 +1167,7 @@ void clearCutStack()
 }
 
 
-docstring selection(size_t sel_index, DocumentClassConstPtr docclass)
+docstring selection(size_t sel_index, DocumentClassConstPtr docclass, bool for_math)
 {
        if (sel_index >= theCuts.size())
                return docstring();
@@ -1177,7 +1177,11 @@ docstring selection(size_t sel_index, DocumentClassConstPtr docclass)
        if (!buffer)
                return docstring();
 
-       return buffer->paragraphs().back().asString(AS_STR_INSETS | AS_STR_NEWLINES);
+       int options = AS_STR_INSETS | AS_STR_NEWLINES;
+       if (for_math)
+               options |= AS_STR_MATHED;
+
+       return buffer->paragraphs().back().asString(options);
 }
 
 
index 13c19a22765e316522b425bf7198d530a713cd7b..6ed17dc585d063a099dcf37a742d696e155a697c 100644 (file)
@@ -41,8 +41,11 @@ namespace cap {
 std::vector<docstring> availableSelections(Buffer const *);
 /// Get the number of available elements in the cut buffer.
 size_type numberOfSelections();
-/// Get the sel_index-th element of the cut buffer in plain text format.
-docstring selection(size_t sel_index, DocumentClassConstPtr docclass);
+/**
+ * Get the sel_index-th element of the cut buffer in plain text format
+ * or, if \param for_math is true, in a format suitable for mathed.
+ */
+docstring selection(size_t sel_index, DocumentClassConstPtr docclass, bool for_math = false);
 
 /**
  * Replace using the font of the first selected character and select
index b348ae4c39de73dcb42c18c22428bb370c78ee16..77611c2e264dfa72d35801fd2673440eac6c8413 100644 (file)
@@ -4144,6 +4144,14 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options, const Out
                        if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
                                LASSERT(runparams != nullptr, return docstring());
                                getInset(i)->plaintext(os, *runparams);
+                       } else if (c == META_INSET && (options & AS_STR_MATHED)
+                                  && getInset(i)->lyxCode() == REF_CODE) {
+                               Buffer const & buf = getInset(i)->buffer();
+                               OutputParams rp(&buf.params().encoding());
+                               Font const font(inherit_font, buf.params().language);
+                               rp.local_font = &font;
+                               otexstream ots(os);
+                               getInset(i)->latex(ots, rp);
                        } else {
                                getInset(i)->toString(os);
                        }
index 4812684c3de1788be93ac205ca954ee73c4a2f95..c67a507397c671273c093396efbc46cda04124e1 100644 (file)
@@ -123,7 +123,8 @@ enum AsStringParameter
        AS_STR_INSETS = 2, ///< Go into insets.
        AS_STR_NEWLINES = 4, ///< Get also newline characters.
        AS_STR_SKIPDELETE = 8, ///< Skip deleted text in change tracking.
-       AS_STR_PLAINTEXT = 16 ///< Don't export formatting when descending into insets.
+       AS_STR_PLAINTEXT = 16, ///< Don't export formatting when descending into insets.
+       AS_STR_MATHED = 32 ///< Use a format suitable for mathed (eg. for InsetRef).
 };
 
 
index 4dbd8a1bdc387d7ce5bd1ee8608ea63e1435b752..3a8b37609c2a33e3a31e7fe56452a782b01cbf30 100644 (file)
@@ -1566,7 +1566,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        idocstringstream is(cmd.argument());
                        int n = 0;
                        is >> n;
-                       topaste = cap::selection(n, buffer().params().documentClassPtr());
+                       topaste = cap::selection(n, buffer().params().documentClassPtr(), true);
                }
                InsetMathGrid grid(buffer_, 1, 1);
                if (!topaste.empty())