The attached patch is a much improved version of my previous patch.
The biggest change is that I add a ClearSelection flag to each LFUN
and call saveSelection accordingly. This version also has Abdel's fix
for mathed, table, window switch etc.

The patch itself is now easy:

1. remove all previous saveSelection(), or replace them with
theSelection().haveSelection(bvcur.selection());.

2. add ClearSelection flag to LyXAction.h/cpp

3. in text3, table, mathed dispatch functions, add

+       // if this LFUN will clear selection, saveSelection for persistent
+       // selection
+       if (lyxaction.funcHasFlag(cmd.action, LyXAction::ClearSelection))
+               cap::saveSelection(bvcur);
+

OK to apply? If you find some other LFUN's also clear selection,
simply add a ClearSelection flag to them in LyXAction.cpp.

Bo
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp	(revision 18990)
+++ src/LyXAction.cpp	(working copy)
@@ -109,13 +109,13 @@
 		{ LFUN_BREAK_PARAGRAPH_SKIP, "break-paragraph-skip", Noop },
 		{ LFUN_BUILD_PROGRAM, "build-program", ReadOnly },
 		{ LFUN_BUFFER_AUTO_SAVE, "buffer-auto-save", Noop },
-		{ LFUN_BUFFER_BEGIN, "buffer-begin", ReadOnly },
+		{ LFUN_BUFFER_BEGIN, "buffer-begin", ReadOnly | ClearSelection },
 		{ LFUN_BUFFER_BEGIN_SELECT, "buffer-begin-select", ReadOnly },
 		{ LFUN_BUFFER_CHILD_OPEN, "buffer-child-open", ReadOnly },
 		{ LFUN_BUFFER_CHKTEX, "buffer-chktex", ReadOnly },
 		{ LFUN_BUFFER_TOGGLE_COMPRESSION, "buffer-toggle-compression", Noop},
 		{ LFUN_BUFFER_CLOSE, "buffer-close", ReadOnly },
-		{ LFUN_BUFFER_END, "buffer-end", ReadOnly },
+		{ LFUN_BUFFER_END, "buffer-end", ReadOnly | ClearSelection },
 		{ LFUN_BUFFER_END_SELECT, "buffer-end-select", ReadOnly },
 		{ LFUN_BUFFER_EXPORT, "buffer-export", ReadOnly },
 		{ LFUN_BUFFER_EXPORT_CUSTOM, "buffer-export-custom", ReadOnly },
@@ -124,7 +124,7 @@
 		{ LFUN_BUFFER_NEW, "buffer-new", NoBuffer },
 		{ LFUN_BUFFER_NEW_TEMPLATE,"buffer-new-template", NoBuffer },
 		{ LFUN_BUFFER_RELOAD, "buffer-reload", ReadOnly },
-		{ LFUN_BUFFER_SWITCH, "buffer-switch", ReadOnly },
+		{ LFUN_BUFFER_SWITCH, "buffer-switch", ReadOnly | ClearSelection },
 		{ LFUN_BUFFER_TOGGLE_READ_ONLY, "buffer-toggle-read-only", ReadOnly },
 		{ LFUN_BUFFER_UPDATE, "buffer-update", ReadOnly },
 		{ LFUN_BUFFER_VIEW, "buffer-view", ReadOnly },
@@ -132,25 +132,25 @@
 		{ LFUN_BUFFER_WRITE_AS, "buffer-write-as", ReadOnly },
 		{ LFUN_CANCEL, "cancel", NoBuffer },
 		{ LFUN_CAPTION_INSERT, "caption-insert", Noop },
-		{ LFUN_CHAR_BACKWARD, "char-backward", ReadOnly | NoUpdate},
+		{ LFUN_CHAR_BACKWARD, "char-backward", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_CHAR_BACKWARD_SELECT, "backward-select", ReadOnly | SingleParUpdate },
-		{ LFUN_CHAR_DELETE_BACKWARD, "delete-backward", SingleParUpdate },
-		{ LFUN_CHAR_DELETE_FORWARD, "delete-forward", SingleParUpdate },
-		{ LFUN_CHAR_FORWARD, "char-forward", ReadOnly | NoUpdate},
+		{ LFUN_CHAR_DELETE_BACKWARD, "delete-backward", SingleParUpdate | ClearSelection },
+		{ LFUN_CHAR_DELETE_FORWARD, "delete-forward", SingleParUpdate | ClearSelection },
+		{ LFUN_CHAR_FORWARD, "char-forward", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_CHAR_FORWARD_SELECT, "forward-select", ReadOnly | SingleParUpdate },
 		{ LFUN_CLIPBOARD_PASTE, "clipboard-paste", Noop },
 		{ LFUN_COMMAND_EXECUTE, "command-execute", NoBuffer },
 		{ LFUN_COMMAND_PREFIX, "command-prefix", NoBuffer },
 		{ LFUN_COMMAND_SEQUENCE, "command-sequence", NoBuffer },
 		{ LFUN_COPY, "copy", ReadOnly },
-		{ LFUN_CUT, "cut", Noop },
+		{ LFUN_CUT, "cut", ClearSelection | ClearSelection },
 		{ LFUN_DATE_INSERT, "date-insert", Noop },
-		{ LFUN_DELETE_BACKWARD_SKIP, "delete-backward-skip", Noop },
-		{ LFUN_DELETE_FORWARD_SKIP, "delete-forward-skip", Noop },
+		{ LFUN_DELETE_BACKWARD_SKIP, "delete-backward-skip", ClearSelection },
+		{ LFUN_DELETE_FORWARD_SKIP, "delete-forward-skip", ClearSelection },
 		{ LFUN_DEPTH_DECREMENT, "depth-decrement", Noop },
 		{ LFUN_DEPTH_INCREMENT, "depth-increment", Noop },
 		{ LFUN_DOTS_INSERT, "dots-insert", Noop },
-		{ LFUN_DOWN, "down", ReadOnly | NoUpdate },
+		{ LFUN_DOWN, "down", ReadOnly | NoUpdate | ClearSelection },
 		{ LFUN_DOWN_SELECT, "down-select", ReadOnly | SingleParUpdate },
 		{ LFUN_DROP_LAYOUTS_CHOICE, "drop-layouts-choice", ReadOnly },
 		{ LFUN_END_OF_SENTENCE_PERIOD_INSERT, "end-of-sentence-period-insert", Noop },
@@ -203,10 +203,10 @@
 		{ LFUN_LAYOUT, "layout", Noop },
 		{ LFUN_LAYOUT_PARAGRAPH, "layout-paragraph", ReadOnly },
 		{ LFUN_LAYOUT_TABULAR, "layout-tabular", Noop },
-		{ LFUN_LINE_BEGIN, "line-begin", ReadOnly | NoUpdate},
+		{ LFUN_LINE_BEGIN, "line-begin", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_LINE_BEGIN_SELECT, "line-begin-select", ReadOnly | SingleParUpdate },
-		{ LFUN_LINE_DELETE, "line-delete-forward", Noop }, // there is no line-delete-backward
-		{ LFUN_LINE_END, "line-end", ReadOnly | NoUpdate},
+		{ LFUN_LINE_DELETE, "line-delete-forward", ClearSelection }, // there is no line-delete-backward
+		{ LFUN_LINE_END, "line-end", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_LINE_END_SELECT, "line-end-select", ReadOnly | SingleParUpdate },
 #if 0
 		{ LFUN_LIST_INSERT, "list-insert", Noop },
@@ -244,7 +244,7 @@
 		{ LFUN_INSET_TOGGLE, "", ReadOnly },
 		{ LFUN_NEXT_INSET_TOGGLE, "next-inset-toggle", ReadOnly },
 		{ LFUN_ALL_INSETS_TOGGLE, "all-insets-toggle", ReadOnly },
-		{ LFUN_PARAGRAPH_DOWN, "paragraph-down", ReadOnly | NoUpdate},
+		{ LFUN_PARAGRAPH_DOWN, "paragraph-down", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_PARAGRAPH_DOWN_SELECT, "paragraph-down-select", ReadOnly },
 		{ LFUN_PARAGRAPH_GOTO, "paragraph-goto", ReadOnly },
 		{ LFUN_OUTLINE_UP, "outline-up", Noop },
@@ -252,7 +252,7 @@
 		{ LFUN_OUTLINE_IN, "outline-in", Noop },
 		{ LFUN_OUTLINE_OUT, "outline-out", Noop },
 		{ LFUN_PARAGRAPH_SPACING, "paragraph-spacing", Noop },
-		{ LFUN_PARAGRAPH_UP, "paragraph-up", ReadOnly | NoUpdate},
+		{ LFUN_PARAGRAPH_UP, "paragraph-up", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_PARAGRAPH_UP_SELECT, "paragraph-up-select", ReadOnly },
 		{ LFUN_PASTE, "paste", Noop },
 		{ LFUN_PREFERENCES_SAVE, "preferences-save", NoBuffer },
@@ -262,11 +262,11 @@
 		{ LFUN_REDO, "redo", Noop },
 		{ LFUN_LABEL_GOTO, "label-goto", ReadOnly },
 		{ LFUN_REFERENCE_NEXT, "reference-next", ReadOnly },
-		{ LFUN_SCREEN_DOWN, "screen-down", ReadOnly },
+		{ LFUN_SCREEN_DOWN, "screen-down", ReadOnly | ClearSelection },
 		{ LFUN_SCREEN_DOWN_SELECT, "screen-down-select", ReadOnly },
 		{ LFUN_SCREEN_FONT_UPDATE, "screen-font-update", NoBuffer },
 		{ LFUN_SCREEN_RECENTER, "screen-recenter", ReadOnly },
-		{ LFUN_SCREEN_UP, "screen-up", ReadOnly },
+		{ LFUN_SCREEN_UP, "screen-up", ReadOnly | ClearSelection },
 		{ LFUN_SCREEN_UP_SELECT, "screen-up-select", ReadOnly },
 		{ LFUN_SELF_INSERT, "self-insert", SingleParUpdate },
 		{ LFUN_SPACE_INSERT, "space-insert", Noop },
@@ -279,8 +279,8 @@
 		{ LFUN_SERVER_NOTIFY, "server-notify", ReadOnly },
 		{ LFUN_SERVER_SET_XY, "server-set-xy", ReadOnly },
 		{ LFUN_SET_COLOR, "set-color", ReadOnly | NoBuffer },
-		{ LFUN_CELL_BACKWARD, "cell-backward", Noop },
-		{ LFUN_CELL_FORWARD, "cell-forward", Noop },
+		{ LFUN_CELL_BACKWARD, "cell-backward", ClearSelection },
+		{ LFUN_CELL_FORWARD, "cell-forward", ClearSelection },
 		{ LFUN_CELL_SPLIT, "cell-split", Noop },
 		{ LFUN_TABULAR_INSERT, "tabular-insert", Noop },
 		{ LFUN_TABULAR_FEATURE, "tabular-feature", Noop },
@@ -291,7 +291,7 @@
 		{ LFUN_TOC_INSERT, "toc-insert", Noop },
 		{ LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR, "toggle-cursor-follows-scrollbar", ReadOnly },
 		{ LFUN_UNDO, "undo", Noop },
-		{ LFUN_UP, "up", ReadOnly | NoUpdate},
+		{ LFUN_UP, "up", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_UP_SELECT, "up-select", ReadOnly | SingleParUpdate },
 		{ LFUN_URL_INSERT, "url-insert", Noop },
 		{ LFUN_VC_CHECK_IN, "vc-check-in", ReadOnly },
@@ -299,14 +299,14 @@
 		{ LFUN_VC_REGISTER, "vc-register", ReadOnly },
 		{ LFUN_VC_REVERT, "vc-revert", ReadOnly },
 		{ LFUN_VC_UNDO_LAST, "vc-undo-last", ReadOnly },
-		{ LFUN_WORD_BACKWARD, "word-backward", ReadOnly | NoUpdate},
+		{ LFUN_WORD_BACKWARD, "word-backward", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_WORD_BACKWARD_SELECT, "word-backward-select", ReadOnly | SingleParUpdate },
 		{ LFUN_WORD_CAPITALIZE, "word-capitalize", Noop },
-		{ LFUN_WORD_DELETE_BACKWARD, "word-delete-backward", Noop },
-		{ LFUN_WORD_DELETE_FORWARD, "word-delete-forward", Noop },
+		{ LFUN_WORD_DELETE_BACKWARD, "word-delete-backward", ClearSelection },
+		{ LFUN_WORD_DELETE_FORWARD, "word-delete-forward", ClearSelection },
 		{ LFUN_WORD_FIND_BACKWARD, "word-find-backward", ReadOnly },
 		{ LFUN_WORD_FIND_FORWARD, "word-find-forward", ReadOnly },
-		{ LFUN_WORD_FORWARD, "word-forward", ReadOnly | NoUpdate},
+		{ LFUN_WORD_FORWARD, "word-forward", ReadOnly | NoUpdate | ClearSelection},
 		{ LFUN_WORD_FORWARD_SELECT, "word-forward-select", ReadOnly | SingleParUpdate },
 		{ LFUN_WORD_LOWCASE, "word-lowcase", Noop },
 		{ LFUN_WORD_SELECT, "word-select", ReadOnly },
@@ -354,13 +354,13 @@
 		{ LFUN_WORDS_COUNT, "words-count", ReadOnly },
 		{ LFUN_FINISHED_RIGHT, "", ReadOnly },
 		{ LFUN_FINISHED_LEFT, "", ReadOnly },
-		{ LFUN_MOUSE_PRESS, "", ReadOnly },
+		{ LFUN_MOUSE_PRESS, "", ReadOnly | ClearSelection },
 		{ LFUN_MOUSE_MOTION, "", ReadOnly | SingleParUpdate },
 		{ LFUN_MOUSE_RELEASE, "", ReadOnly },
 		{ LFUN_MOUSE_DOUBLE, "", ReadOnly },
 		{ LFUN_MOUSE_TRIPLE, "", ReadOnly },
-		{ LFUN_PARAGRAPH_MOVE_DOWN, "paragraph-move-down", Noop },
-		{ LFUN_PARAGRAPH_MOVE_UP, "paragraph-move-up", Noop },
+		{ LFUN_PARAGRAPH_MOVE_DOWN, "paragraph-move-down", ClearSelection },
+		{ LFUN_PARAGRAPH_MOVE_UP, "paragraph-move-up", ClearSelection },
 		{ LFUN_WINDOW_NEW, "window-new", NoBuffer },
 		{ LFUN_WINDOW_CLOSE, "window-close", NoBuffer },
 		{ LFUN_UNICODE_INSERT, "unicode-insert", Noop },
Index: src/insets/InsetTabular.cpp
===================================================================
--- src/insets/InsetTabular.cpp	(revision 18990)
+++ src/insets/InsetTabular.cpp	(working copy)
@@ -42,6 +42,7 @@
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "Undo.h"
+#include "LyXAction.h"
 
 #include "support/convert.h"
 #include "support/lstrings.h"
@@ -3188,6 +3189,11 @@
 	CursorSlice sl = cur.top();
 	Cursor & bvcur = cur.bv().cursor();
 
+	// if this LFUN will clear selection, saveSelection for persistent
+	// selection
+	if (lyxaction.funcHasFlag(cmd.action, LyXAction::ClearSelection))
+		cap::saveSelection(bvcur);
+
 	switch (cmd.action) {
 
 	case LFUN_MOUSE_PRESS:
@@ -3241,28 +3247,20 @@
 
 	case LFUN_MOUSE_RELEASE:
 		//lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
-		if (cmd.button() == mouse_button::button1) {
-			if (bvcur.selection()) {
-				// Bug3238: disable persistent selection for table cells for now
-				if (tablemode(bvcur))
-					theSelection().haveSelection(true);
-				else
-					saveSelection(bvcur);
-			}
-		} else if (cmd.button() == mouse_button::button3)
+		if (cmd.button() == mouse_button::button1)
+			theSelection().haveSelection(bvcur.selection());
+		else if (cmd.button() == mouse_button::button3)
 			InsetTabularMailer(*this).showDialog(&cur.bv());
 		break;
 
 	case LFUN_CELL_BACKWARD:
 		movePrevCell(cur);
 		cur.selection() = false;
-		saveSelection(cur);
 		break;
 
 	case LFUN_CELL_FORWARD:
 		moveNextCell(cur);
 		cur.selection() = false;
-		saveSelection(cur);
 		break;
 
 	case LFUN_CHAR_FORWARD_SELECT:
@@ -3275,7 +3273,7 @@
 			// to LFUN_FINISHED_RIGHT (e.g. a case in bug 3782)
 			if (cmd.action == LFUN_CHAR_FORWARD_SELECT ||
 				cmd.action == LFUN_FINISHED_RIGHT)
-				saveSelection(cur);
+				theSelection().haveSelection(cur.selection());
 			if (sl == cur.top())
 				cmd = FuncRequest(LFUN_FINISHED_RIGHT);
 			else
@@ -3289,7 +3287,7 @@
 		if (!cur.result().dispatched()) {
 			isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
 			if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
-				saveSelection(cur);
+				theSelection().haveSelection(cur.selection());
 			if (sl == cur.top())
 				cmd = FuncRequest(LFUN_FINISHED_LEFT);
 			else
@@ -3311,13 +3309,8 @@
 				TextMetrics const & tm =
 					cur.bv().textMetrics(cell(cur.idx())->getText(0));
 				cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
-				if (cmd.action == LFUN_DOWN_SELECT) {
-					// Bug3238: disable persistent selection for table cells for now
-					if (tablemode(cur))
-						theSelection().haveSelection(true);
-					else
-						saveSelection(cur);
-				}
+				if (cmd.action == LFUN_DOWN_SELECT)
+					theSelection().haveSelection(cur.selection());
 			}
 		if (sl == cur.top()) {
 			// we trick it to go to the RIGHT after leaving the
@@ -3343,13 +3336,8 @@
 				ParagraphMetrics const & pm =
 					tm.parMetrics(cur.lastpit());
 				cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
-				if (cmd.action == LFUN_UP_SELECT) {
-					// Bug3238: disable persistent selection for table cells for now
-					if (tablemode(cur))
-						theSelection().haveSelection(true);
-					else
-						saveSelection(cur);
-				}
+				if (cmd.action == LFUN_UP_SELECT)
+					theSelection().haveSelection(cur.selection());
 			}
 		if (sl == cur.top()) {
 			cmd = FuncRequest(LFUN_UP);
Index: src/LyXAction.h
===================================================================
--- src/LyXAction.h	(revision 18990)
+++ src/LyXAction.h	(working copy)
@@ -55,7 +55,8 @@
 		NoBuffer = 2, //< Can be used when there is no document open
 		Argument = 4, //< Requires argument
 		NoUpdate = 8, //< Does not (usually) require update
-		SingleParUpdate = 16 //< Usually only requires this par updated
+		SingleParUpdate = 16, //< Usually only requires this par updated
+		ClearSelection = 32, //< This LFUN will clear current selection
 	};
 
 	LyXAction();
Index: src/mathed/InsetMathNest.cpp
===================================================================
--- src/mathed/InsetMathNest.cpp	(revision 18990)
+++ src/mathed/InsetMathNest.cpp	(working copy)
@@ -47,6 +47,7 @@
 #include "Text.h"
 #include "OutputParams.h"
 #include "Undo.h"
+#include "LyXAction.h"
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
@@ -442,6 +443,11 @@
 	//lyxerr << "InsetMathNest: request: " << cmd << std::endl;
 	//CursorSlice sl = cur.current();
 
+	// if this LFUN will clear selection, saveSelection for persistent
+	// selection
+	if (lyxaction.funcHasFlag(cmd.action, LyXAction::ClearSelection))
+		cap::saveSelection(cur.bv().cursor());
+
 	switch (cmd.action) {
 
 	case LFUN_PASTE: {
@@ -582,7 +588,7 @@
 		cur.selection() = true;
 		cur.pos() = cur.lastpos();
 		cur.idx() = cur.lastidx();
-		cap::saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_PARAGRAPH_UP:
@@ -1207,7 +1213,7 @@
 		else {
 			Cursor & bvcur = cur.bv().cursor();
 			bvcur.selection() = true;
-			cap::saveSelection(bvcur);
+			theSelection().haveSelection(bvcur.selection());
 		}
 		return;
 	}
Index: src/LyXFunc.cpp
===================================================================
--- src/LyXFunc.cpp	(revision 18990)
+++ src/LyXFunc.cpp	(working copy)
@@ -218,6 +218,10 @@
 
 void LyXFunc::setLyXView(LyXView * lv)
 {
+	if (lyx_view_ && lyx_view_ != lv)
+		// save current selection to the selection buffer to allow
+		// middle-button paste in another window
+		cap::saveSelection(lyx_view_->view()->cursor());
 	lyx_view_ = lv;
 }
 
Index: src/CutAndPaste.cpp
===================================================================
--- src/CutAndPaste.cpp	(revision 18990)
+++ src/CutAndPaste.cpp	(working copy)
@@ -672,24 +672,15 @@
 
 void saveSelection(Cursor & cur)
 {
-	LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION << ": `"
-	       << to_utf8(cur.selectionAsString(true)) << "'."
-	       << endl;
-
-#if 0
-	// FIXME: The two lines below would allow middle-mouse 
-	// pasting that preserves the LyX formatting when the selection
-	// is internal. They would also allow to use the feature on
-	// Windows and Mac. In the future, we may want to optionally enable
-	// this feature via a rc setting.
-	// This is currently disabled because it eats too much resources
-	// while selecting (cf. bug 3877)
-	if (cur.selection())
+	// This function is called, not when a selection is formed, but when
+	// a selection is cleared. Therefore, multiple keyboard selection
+	// will not repeatively trigger this function (bug 3877).
+	if (cur.selection()) {
+		LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION << ": `"
+			   << to_utf8(cur.selectionAsString(true)) << "'."
+			   << endl;
 		copySelectionToStack(cur, selectionBuffer);
-#endif
-
-	// tell X whether we now have a valid selection
-	theSelection().haveSelection(cur.selection());
+	}
 }
 
 
@@ -746,7 +737,6 @@
 	pasteParagraphList(cur, theCuts[sel_index].first,
 			   theCuts[sel_index].second, errorList);
 	cur.setSelection();
-	saveSelection(cur);
 }
 
 
@@ -825,7 +815,6 @@
 		cur.setSelection(selbeg, -int(str.length()));
 	} else
 		cur.setSelection(selbeg, str.length());
-	saveSelection(cur);
 }
 
 
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp	(revision 18990)
+++ src/Text3.cpp	(working copy)
@@ -119,7 +119,7 @@
 	{
 		if (selecting || cur.mark())
 			cur.setSelection();
-		saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 	}
 
 
@@ -370,6 +370,11 @@
 		cur.bottom().text(), cur.bottom().pit());
 	Dimension olddim = pm.dim();
 
+	// if this LFUN will clear selection, saveSelection for persistent
+	// selection
+	if (lyxaction.funcHasFlag(cmd.action, LyXAction::ClearSelection))
+		saveSelection(cur.bv().cursor());
+
 	switch (cmd.action) {
 
 	case LFUN_PARAGRAPH_MOVE_DOWN: {
@@ -488,8 +493,8 @@
 				&& cur.boundary() == oldBoundary) {
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_RIGHT);
-		} else if (cur.selection())
-			saveSelection(cur);
+		}
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_CHAR_BACKWARD:
@@ -506,8 +511,7 @@
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_LEFT);
 		}
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_UP_SELECT:
@@ -532,9 +536,7 @@
 		} else
 			cur.undispatched();
 		
-		// save new selection
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 	}
 
@@ -542,16 +544,14 @@
 	case LFUN_PARAGRAPH_UP_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
 		needsUpdate |= cursorUpParagraph(cur);
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_PARAGRAPH_DOWN:
 	case LFUN_PARAGRAPH_DOWN_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
 		needsUpdate |= cursorDownParagraph(cur);
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_SCREEN_UP:
@@ -562,8 +562,7 @@
 		else {
 			cursorPrevious(cur);
 		}
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_SCREEN_DOWN:
@@ -575,8 +574,7 @@
 		else {
 			cursorNext(cur);
 		}
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_LINE_BEGIN:
@@ -589,8 +587,7 @@
 	case LFUN_LINE_END_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
 		needsUpdate |= cursorEnd(cur);
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_WORD_FORWARD:
@@ -600,8 +597,7 @@
 			needsUpdate |= cursorLeftOneWord(cur);
 		else
 			needsUpdate |= cursorRightOneWord(cur);
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_WORD_BACKWARD:
@@ -611,8 +607,7 @@
 			needsUpdate |= cursorRightOneWord(cur);
 		else
 			needsUpdate |= cursorLeftOneWord(cur);
-		if (cur.selection())
-			saveSelection(cur);
+		theSelection().haveSelection(cur.selection());
 		break;
 
 	case LFUN_WORD_SELECT: {
@@ -1033,7 +1028,7 @@
 			cursorEnd(cur);
 			cur.setSelection();
 			bv->cursor() = cur;
-			saveSelection(cur);
+			theSelection().haveSelection(cur.selection());
 		}
 		break;
 
@@ -1145,7 +1140,7 @@
 				// but bvcur is current mouse position
 				Cursor & bvcur = cur.bv().cursor();
 				bvcur.selection() = true;
-				saveSelection(bvcur);
+				theSelection().haveSelection(true);
 			}
 			needsUpdate = false;
 			cur.noUpdate();
@@ -1589,7 +1584,7 @@
 	case LFUN_ESCAPE:
 		if (cur.selection()) {
 			cur.selection() = false;
-			saveSelection(cur);
+			theSelection().haveSelection(cur.selection());
 		} else {
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_RIGHT);

Reply via email to