patch 9.1.1337: Undo corrupted with 'completeopt' "preinsert" when switching buffer

Problem:  Undo corrupted with 'completeopt' "preinsert" when switching
          buffer or window.
Solution: Do not delete preinsert text when switching buffer or window.
          (zeertzjq)

related: neovim/neovim#33581
closes: #17193

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/insexpand.c b/src/insexpand.c
index 2335862..ace4f55 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -2699,7 +2699,7 @@
     char_u	*word = NULL;
 
     // Remove pre-inserted text when present.
-    if (ins_compl_preinsert_effect())
+    if (ins_compl_preinsert_effect() && ins_compl_win_active(curwin))
 	ins_compl_delete();
 
     // Get here when we have finished typing a sequence of ^N and