// http://akelpad.sourceforge.net/forum/viewtopic.php?p=4222#4222
// Version: 1.4
// Author: Shengalts Aleksander aka Instructor
//
//
// Description(1033): Force create new program instance.
// Description(1049): Форсировать создание нового экземпляра программы.
//
// Arguments:
// -CmdLine="/Command(4155)" -Command line than will be appended to CmdLineBegin parameter of the second instance.
//
// Usage:
// Call("Scripts::Main", 1, "ForceNewInstance.js", `-CmdLine="/Command(4155)"`)

//Arguments
var pCmdLine=AkelPad.GetArgValue("CmdLine", "");

ForceNewInstance(pCmdLine);

function ForceNewInstance(pCmdLine)
{
  var hMainWnd=AkelPad.GetMainWnd();
  var oSet=AkelPad.ScriptSettings();
  var hNewMainWnd=0;
  var lpPluginFunction;
  var lpCmdLineBegin;
  var pCmdLineCurrent="";
  var nCmdLineBeginLen;
  var bOpenOnStartEnable=false;

  //Plugin running?
  if (lpPluginFunction=AkelPad.SendMessage(hMainWnd, 1329 /*AKD_DLLFIND*/, "Sessions::Main", 0))
  {
    //Plugin autoloading?
    if (AkelPad.MemRead(_PtrAdd(lpPluginFunction, _X64?812:800) /*offsetof(PLUGINFUNCTION, bAutoLoad)*/, 3 /*DT_DWORD*/))
    {
      //Plugin opens session on start?
      if (oSet.Begin("Sessions", 0x21 /*POB_READ|POB_PLUGS*/))
      {
        bOpenOnStartEnable=oSet.Read("OpenOnStartEnable", 1 /*PO_DWORD*/);
        oSet.End();
      }
      //Turn off open session on start
      if (bOpenOnStartEnable)
      {
        if (oSet.Begin("Sessions", 0x22 /*POB_SAVE|POB_PLUGS*/))
        {
          oSet.Write("OpenOnStartEnable", 1 /*PO_DWORD*/, false);
          oSet.End();
        }
      }
    }
  }
  if (pCmdLine)
  {
    if (oSet.Begin("", 0x42 /*POB_SAVE|POB_PROGRAM*/))
    {
      if (nCmdLineBeginLen=AkelPad.SendMessage(hMainWnd, 1222 /*AKD_GETMAININFO*/, 105 /*MI_CMDLINEBEGIN*/, 0))
      {
         if (lpCmdLineBegin=AkelPad.MemAlloc(nCmdLineBeginLen * 2 /*sizeof(wchar_t)*/))
         {
           if (AkelPad.SendMessage(hMainWnd, 1222 /*AKD_GETMAININFO*/, 105 /*MI_CMDLINEBEGIN*/, lpCmdLineBegin))
             pCmdLineCurrent=AkelPad.MemRead(lpCmdLineBegin, 1 /*DT_UNICODE*/);
           AkelPad.MemFree(lpCmdLineBegin);
         }
      }
      oSet.Write("CmdLineBegin", 3 /*PO_STRING*/, pCmdLineCurrent + " " + pCmdLine);
      oSet.End();
    }
  }

  if (AkelPad.SendMessage(hMainWnd, 1222 /*AKD_GETMAININFO*/, 153 /*MI_SINGLEOPENPROGRAM*/, 0))
  {
    AkelPad.Command(4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/);
    hNewMainWnd=AkelPad.Command(4102 /*IDM_FILE_CREATENEW*/);
    AkelPad.Command(4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/);
    AkelPad.SendMessage(hNewMainWnd, 273 /*WM_COMMAND*/, 4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/, 0);
  }
  else hNewMainWnd=AkelPad.Command(4102 /*IDM_FILE_CREATENEW*/);

  //Turn on open session on start
  if (bOpenOnStartEnable)
  {
    if (oSet.Begin("Sessions", 0x22 /*POB_SAVE|POB_PLUGS*/))
    {
      oSet.Write("OpenOnStartEnable", 1 /*PO_DWORD*/, true);
      oSet.End();
    }
  }
  if (pCmdLine)
  {
    if (oSet.Begin("", 0x42 /*POB_SAVE|POB_PROGRAM*/))
    {
      oSet.Write("CmdLineBegin", 3 /*PO_STRING*/, pCmdLineCurrent);
      oSet.End();
    }
  }
  return hNewMainWnd;
}
