Following our middle-mouse pasting discussions, I think I've implemented the optimal solution that will work under Mac and Windows too (internally to LyX). My solution is to save the selectionBuffer just before it is pasted in the same LFUN_MOUSE_PRESS. The big change is that we check if the current BufferView cursor has a selection instead of testing if there the selection buffer is not empty (via cap::selection()). This way, cap::selectionBuffer always contain the last selected text.
As usual, I can not test your patch because there is no attachment to download here (gmail) and copy/paste does not work. Anyway, I do not really understand your patch. As you said, you saveSelection just before middle button paste so that it can be pasted again and again, but what if the selection is cleared with left button or board? I mean the 'select, left button click, right button click' sequence. Bo