patch 9.0.0965: using one window for executing autocommands is insufficient

Problem:    Using one window for executing autocommands is insufficient.
Solution:   Use up to five windows for executing autocommands.
diff --git a/src/if_ruby.c b/src/if_ruby.c
index 51cfff1..b157bb8 100644
--- a/src/if_ruby.c
+++ b/src/if_ruby.c
@@ -1371,21 +1371,24 @@
 
     if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
     {
-	// set curwin/curbuf for "buf" and save some things
+	// Set curwin/curbuf for "buf" and save some things.
 	aucmd_prepbuf(&aco, buf);
-
-	if (u_savesub(n) == OK)
+	if (curbuf == buf)
 	{
-	    ml_replace(n, (char_u *)line, TRUE);
-	    changed();
+	    // Only when it worked to set "curbuf".
+	    if (u_savesub(n) == OK)
+	    {
+		ml_replace(n, (char_u *)line, TRUE);
+		changed();
 #ifdef SYNTAX_HL
-	    syn_changed(n); // recompute syntax hl. for this line
+		syn_changed(n); // recompute syntax hl. for this line
 #endif
-	}
+	    }
 
-	// restore curwin/curbuf and a few other things
-	aucmd_restbuf(&aco);
-	// Careful: autocommands may have made "buf" invalid!
+	    // restore curwin/curbuf and a few other things
+	    aucmd_restbuf(&aco);
+	    // Careful: autocommands may have made "buf" invalid!
+	}
 
 	update_curbuf(UPD_NOT_VALID);
     }
@@ -1415,24 +1418,27 @@
 
     if (n > 0 && n <= buf->b_ml.ml_line_count)
     {
-	// set curwin/curbuf for "buf" and save some things
+	// Set curwin/curbuf for "buf" and save some things.
 	aucmd_prepbuf(&aco, buf);
-
-	if (u_savedel(n, 1) == OK)
+	if (curbuf == buf)
 	{
-	    ml_delete(n);
+	    // Only when it worked to set "curbuf".
+	    if (u_savedel(n, 1) == OK)
+	    {
+		ml_delete(n);
 
-	    // Changes to non-active buffers should properly refresh
-	    //   SegPhault - 01/09/05
-	    deleted_lines_mark(n, 1L);
+		// Changes to non-active buffers should properly refresh
+		//   SegPhault - 01/09/05
+		deleted_lines_mark(n, 1L);
 
-	    changed();
+		changed();
+	    }
+
+	    // restore curwin/curbuf and a few other things
+	    aucmd_restbuf(&aco);
+	    // Careful: autocommands may have made "buf" invalid!
 	}
 
-	// restore curwin/curbuf and a few other things
-	aucmd_restbuf(&aco);
-	// Careful: autocommands may have made "buf" invalid!
-
 	update_curbuf(UPD_NOT_VALID);
     }
     else
@@ -1458,22 +1464,25 @@
     {
 	// set curwin/curbuf for "buf" and save some things
 	aucmd_prepbuf(&aco, buf);
-
-	if (u_inssub(n + 1) == OK)
+	if (curbuf == buf)
 	{
-	    ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
+	    // Only when it worked to set "curbuf".
+	    if (u_inssub(n + 1) == OK)
+	    {
+		ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
 
-	    //  Changes to non-active buffers should properly refresh screen
-	    //    SegPhault - 12/20/04
-	    appended_lines_mark(n, 1L);
+		//  Changes to non-active buffers should properly refresh screen
+		//    SegPhault - 12/20/04
+		appended_lines_mark(n, 1L);
 
-	    changed();
+		changed();
+	    }
+
+	    // restore curwin/curbuf and a few other things
+	    aucmd_restbuf(&aco);
+	    // Careful: autocommands may have made "buf" invalid!
 	}
 
-	// restore curwin/curbuf and a few other things
-	aucmd_restbuf(&aco);
-	// Careful: autocommands may have made "buf" invalid!
-
 	update_curbuf(UPD_NOT_VALID);
     }
     else