From f63864b8d50cffa9c90590a876a938b4ac0423eb Mon Sep 17 00:00:00 2001
From: H.G. Muller <h.g.muller@hccnet.nl>
Date: Mon, 28 Jan 2013 17:23:47 +0100
Subject: [PATCH] Preserve mode on engine loading (sometimes)

That the mode always switched to EditGame after engine loading was
particularly annoying at the start of a game (where leaving BeginningOfGame
mode would disablethe Time Control dialog, and the engine would not
automatically reply if you start playing) and AnalyzeMode. These two
modes are now preserved, BeginningOfGame by refraining from calling
EditGameEvent() and sending 'force' to the new engine, and AnalyzeMode
be calling AnalyzeModeEvent() after the engine is loaded and repositioned.
---
 backend.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/backend.c b/backend.c
index dd5106f..1aea360 100644
--- a/backend.c
+++ b/backend.c
@@ -840,6 +840,8 @@ InitEngine (ChessProgramState *cps, int n)
 
 ChessProgramState *savCps;
 
+GameMode oldMode;
+
 void
 LoadEngine ()
 {
@@ -849,21 +851,23 @@ LoadEngine ()
     if(gameInfo.variant != StringToVariant(appData.variant)) {
 	// we changed variant when loading the engine; this forces us to reset
 	Reset(TRUE, savCps != &first);
-	EditGameEvent(); // for consistency with other path, as Reset changes mode
+	oldMode = BeginningOfGame; // to prevent restoring old mode
     }
     InitChessProgram(savCps, FALSE);
-    SendToProgram("force\n", savCps);
+    if(gameMode == EditGame) SendToProgram("force\n", savCps); // in EditGame mode engine must be in force mode
     DisplayMessage("", "");
     if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
     for (i = backwardMostMove; i < currentMove; i++) SendMoveToProgram(i, savCps);
     ThawUI();
     SetGNUMode();
+    if(oldMode == AnalyzeMode) AnalyzeModeEvent();
 }
 
 void
 ReplaceEngine (ChessProgramState *cps, int n)
 {
-    EditGameEvent();
+    oldMode = gameMode; // remember mode, so it can be restored after loading sequence is complete
+    if(oldMode != BeginningOfGame) EditGameEvent();
     UnloadEngine(cps);
     appData.noChessProgram = FALSE;
     appData.clockMode = TRUE;
-- 
1.7.0.4