macvim-dev / macvim

Vim - the text editor - for macOS
https://macvim.org
Vim License
7.47k stars 680 forks source link

Fix bug where clipboard would be polluted when showing Services menu #1300

Closed ychin closed 1 year ago

ychin commented 1 year ago

Previously, whenever the user selects some text in MacVim, and then bring up the Services menu (either by right-clicking on the selection, or go to MacVim -> Services), MacVim will copy the selected text to the system clipboard, which is quite an unexpected behavior. Fix that here.

Part of the issue is that Vim has built-in ways to convert the selected range to a single copyable text, while managing complexities of dealing with blockwise/linewise modes. Previous implementation in MacVim was lazy in that it just invoked those code, but the side effect was that it would also copy the text to the system clipboard and pollute Vim's star register as well. This was quite undesirable because the user has not done anything other than opening a menu and wouldn't have expected the system clipboard or the Vim registers to have changed.

In this fix, we unfortunately had to do a little bit more copy-pasting to extract the useful bits so we can copy the text to a register (but not system clipboard), invoke the code to convert the register to clipboard-happy text, restore the register, and then move on. A little annoying but better than having the unintuitive / annoying behaveior from before, and this way we don't have to do too much intrusive refactoring of upstream Vim code as well.