[LyX/2.3.x] Allow pasting references to mathed

Enrico Forestieri forenr at lyx.org
Mon Feb 1 15:47:13 UTC 2021


commit 12f67ef154f1fd3c18df6e8e7af404df300cf87f
Author: Enrico Forestieri <forenr at lyx.org>
Date:   Sat Jan 30 01:32:40 2021 +0100

    Allow pasting references to mathed
    
    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
    
    (cherry picked from commit 22ee249c3ea6426dbf24956ae36986e3ee6f81c0)
---
 src/CutAndPaste.cpp          |    8 ++++++--
 src/CutAndPaste.h            |    7 +++++--
 src/Paragraph.cpp            |    8 ++++++++
 src/Paragraph.h              |    3 ++-
 src/mathed/InsetMathGrid.cpp |    2 +-
 status.23x                   |    2 ++
 6 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp
index cad8c94..e31151b 100644
--- a/src/CutAndPaste.cpp
+++ b/src/CutAndPaste.cpp
@@ -1140,7 +1140,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();
@@ -1150,7 +1150,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);
 }
 
 
diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h
index ae80ac0..79b4563 100644
--- a/src/CutAndPaste.h
+++ b/src/CutAndPaste.h
@@ -37,8 +37,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
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index c77fbf9..5f8439d 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -3570,6 +3570,14 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options, const Out
 			if (c == META_INSET && (options & AS_STR_PLAINTEXT)) {
 				LASSERT(runparams != 0, 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);
 			}
diff --git a/src/Paragraph.h b/src/Paragraph.h
index 61cd058..85d4c87 100644
--- a/src/Paragraph.h
+++ b/src/Paragraph.h
@@ -126,7 +126,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).
 };
 
 
diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp
index eb8c4ff..cf1f74b 100644
--- a/src/mathed/InsetMathGrid.cpp
+++ b/src/mathed/InsetMathGrid.cpp
@@ -1606,7 +1606,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())
diff --git a/status.23x b/status.23x
index 89d4790..c38aaec 100644
--- a/status.23x
+++ b/status.23x
@@ -31,6 +31,8 @@ What's new
   does not move the cursor out of the whole equation anymore and is now
   equivalent to hitting the left arrow key (bug 11678).
 
+- Other than directly inserted, now references can also be pasted as insets
+  into an equation, instead of simply as text (bug 11539). 
 
 * DOCUMENTATION AND LOCALIZATION
 


More information about the lyx-cvs mailing list