Il 02/01/2011 13:20, Tommaso Cucinotta ha scritto:

 After a look at the code, I'm puzzled with the purpose and functionality
 of the related LFUNs:
         { LFUN_PASTE, "paste", Noop, Edit },
         { LFUN_CLIPBOARD_PASTE, "clipboard-paste", Noop, Edit },
         { LFUN_PRIMARY_SELECTION_PASTE, "primary-selection-paste",
 Noop, Edit },
         { LFUN_SELECTION_PASTE, "selection-paste", Noop, Edit },

Also, surprisingly, none of these seems to implement the kind of feature
 I usually find very useful in copy'n'pasting in WYSIWYG editors, i.e.,
the possibility to paste the clipboard as simple unformatted text.

So, just to waste time, I've come out with the attached patch, introducing
the PASTE_SIMPLE_TEXT LFUN and associating it with C-S-v. Perhaps this may
be useful to someone else.

Bye,

  T.

Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp	(revisione 37065)
+++ src/LyXAction.cpp	(copia locale)
@@ -1206,6 +1206,14 @@
  */
 		{ LFUN_SELECTION_PASTE, "selection-paste", Noop, Edit },
 /*!
+ * \var lyx::FuncCode lyx::LFUN_PASTE_SIMPLE_TEXT
+ * \li Action: Pastes simple text from the active clipboard.
+ * \li Syntax: paste-simple-text [<ARG>]
+ * \li Params: <ARG>: "paragraph" will cause pasting as one paragraph, i.e. "Join lines".
+ * \endvar
+ */
+		{ LFUN_PASTE_SIMPLE_TEXT, "paste-simple-text", Noop, Edit },
+/*!
  * \var lyx::FuncCode lyx::LFUN_UNDO
  * \li Action: Undoes the last edit.
  * \li Syntax: undo
Index: src/Text.cpp
===================================================================
--- src/Text.cpp	(revisione 37065)
+++ src/Text.cpp	(copia locale)
@@ -749,7 +749,7 @@
 
 
 // needed to insert the selection
-void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
+void Text::insertStringAsLines(DocIterator & dit, docstring const & str,
 		Font const & font)
 {
 	BufferParams const & bparams = owner_->buffer().params();
@@ -796,12 +796,14 @@
 			space_inserted = (*cit == ' ');
 		}
 	}
+	dit.pit() = pit;
+	dit.pos() = pos;
 }
 
 
 // turn double CR to single CR, others are converted into one
 // blank. Then insertStringAsLines is called
-void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
+void Text::insertStringAsParagraphs(DocIterator & dit, docstring const & str,
 		Font const & font)
 {
 	docstring linestr = str;
Index: src/CutAndPaste.h
===================================================================
--- src/CutAndPaste.h	(revisione 37065)
+++ src/CutAndPaste.h	(copia locale)
@@ -98,6 +98,8 @@
 /// Does handle undo. Does only work in text, not mathed.
 void pasteFromStack(Cursor & cur, ErrorList & errorList, size_t sel_index);
 
+void pasteSimpleText(Cursor & cur, bool asParagraphs);
+
 /// Paste the paragraph list \p parlist at the position given by \p cur.
 /// Does not handle undo. Does only work in text, not mathed.
 void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
Index: src/CutAndPaste.cpp
===================================================================
--- src/CutAndPaste.cpp	(revisione 37065)
+++ src/CutAndPaste.cpp	(copia locale)
@@ -1028,6 +1028,39 @@
 }
 
 
+void pasteSimpleText(Cursor & cur, bool asParagraphs)
+{
+	docstring text;
+	// Use internal clipboard if it is the most recent one
+	if (theClipboard().isInternal()) {
+		if (!checkPastePossible(0))
+			return;
+
+		ParagraphList const & pars = theCuts[0].first;
+		ParagraphList::const_iterator it = pars.begin();
+		for (; it != pars.end(); ++it) {
+			if (it != pars.begin())
+				text += "\n";
+			text += (*it).asString();
+		}
+		asParagraphs = false;
+	} else {
+		// Then try plain text
+		text = theClipboard().getAsText();
+	}
+
+	if (text.empty())
+		return;
+
+	cur.recordUndo();
+	cutSelection(cur, true, false);
+	if (asParagraphs)
+		cur.text()->insertStringAsParagraphs(cur, text, cur.current_font);
+	else
+		cur.text()->insertStringAsLines(cur, text, cur.current_font);
+}
+
+
 void pasteClipboardGraphics(Cursor & cur, ErrorList & /* errorList */,
 			    Clipboard::GraphicsType preferedType)
 {
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp	(revisione 37065)
+++ src/Text3.cpp	(copia locale)
@@ -93,6 +93,7 @@
 using cap::replaceSelection;
 using cap::grabAndEraseSelection;
 using cap::selClearOrDel;
+using cap::pasteSimpleText;
 
 // globals...
 static Font freefont(ignore_font, ignore_language);
@@ -1318,6 +1319,11 @@
 		bv->buffer().errors("Paste");
 		break;
 
+	case LFUN_PASTE_SIMPLE_TEXT:
+		cur.clearSelection();
+		pasteSimpleText(cur, cmd.argument() == "paragraph");
+		break;
+
 	case LFUN_PRIMARY_SELECTION_PASTE:
 		pasteString(cur, theSelection().get(),
 			    cmd.argument() == "paragraph");
@@ -2582,6 +2588,7 @@
 	 }
 
 	case LFUN_CLIPBOARD_PASTE:
+	case LFUN_PASTE_SIMPLE_TEXT:
 		enable = !theClipboard().empty();
 		break;
 
Index: src/FuncCode.h
===================================================================
--- src/FuncCode.h	(revisione 37065)
+++ src/FuncCode.h	(copia locale)
@@ -449,6 +449,7 @@
 	LFUN_FORWARD_SEARCH,
 	LFUN_SCRIPT_INSERT,             // gb, 20101123
 	// 350
+	LFUN_PASTE_SIMPLE_TEXT,		// tommaso, 20110102
 
 	LFUN_LASTACTION                 // end of the table
 };
Index: src/Text.h
===================================================================
--- src/Text.h	(revisione 37065)
+++ src/Text.h	(copia locale)
@@ -265,10 +265,10 @@
 	/* these things are for search and replace */
 
 	/// needed to insert the selection
-	void insertStringAsLines(DocIterator const & dit, docstring const & str,
+	void insertStringAsLines(DocIterator & dit, docstring const & str,
 		Font const & font);
 	/// needed to insert the selection
-	void insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
+	void insertStringAsParagraphs(DocIterator & dit, docstring const & str,
 		Font const & font);
 
 	/// access to our paragraphs

Reply via email to