From 3479c5dc48b192a7c8c2089c4ba0de5538ea8129 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Sun, 8 Aug 2010 18:46:06 +0200
Subject: [PATCH] Fixed: on MS-Windows sometimes files with number 4913 or
 higher are left behind.

---
 runtime/doc/todo.txt |  3 ---
 src/fileio.c         | 16 ++++++++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/runtime/doc/todo.txt b/runtime/doc/todo.txt
index 937f5c8267..0e4459337f 100644
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -77,9 +77,6 @@ I often see pasted text (from Firefox, to Vim in xterm) appear twice.
 Also, Vim in xterm sometimes loses copy/paste ability (probably after running
 an external command).
 
-Win32: The "4913" file is sometimes not deleted.  Perhaps another program has
-opened it?  Use FILE_FLAG_DELETE_ON_CLOSE? (Craig Barkhouse, 2009 Nov 6)
-
 Jumplist doesn't work properly in Insert mode? (Jean Johner, 2010 Mar 20)
 
 Problem with transparent cmdline.  Also: Terminal title is wrong with
diff --git a/src/fileio.c b/src/fileio.c
index 2881cbc88b..368fd7adfe 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -3744,6 +3744,22 @@ buf_write(buf, fname, sfname, start, end, eap, append, forceit,
 		     * can't delete an open file. */
 		    close(fd);
 		    mch_remove(IObuff);
+# ifdef MSWIN
+		    /* MS-Windows may trigger a virus scanner to open the
+		     * file, we can't delete it then.  Keep trying for half a
+		     * second. */
+		    {
+			int try;
+
+			for (try = 0; try < 10; ++try)
+			{
+			    if (mch_lstat((char *)IObuff, &st) < 0)
+				break;
+			    ui_delay(50L, TRUE);  /* wait 50 msec */
+			    mch_remove(IObuff);
+			}
+		    }
+# endif
 		}
 	    }
 	}
-- 
GitLab