//
  // Move bplist back to Notepad++ window in orser to save buffer automatically.
  // If, after user's changes, we are not able to convert xml to bplist,
  // we should return original bplist buffer and notify user that conversion failed.
  //
  void OnFileBeforeSave( SCNotification *notifyCode )
  {
    auto loadedBplist = g_pLoadedBplists->find( notifyCode->nmhdr.idFrom );

    HWND hSkilla;

    try
    {
      if ( loadedBplist != g_pLoadedBplists->end() )
      {
        // Read all content -> convert it back to Bplist and save
        CharVt xmlPlistStr = ReadFromSkintilla( hSkilla );

        auto binPlist = loadedBplist->second->GetBinPlist( std::move(xmlPlistStr) );
        InsertDataIntoSkilla( hSkilla, binPlist.data(), binPlist.size() );
      }
    }
    catch ( std::runtime_error& )
    {
      // Update Notepad++ window with previous-saved (good & valid) bplist buffer
      assert( loadedBplist->second->GetContentType() != ContentType::xml );

      auto binPlist = loadedBplist->second->GetBinPlist();
      InsertDataIntoSkilla( hSkilla, binPlist.data(), binPlist.size() );

      // as long as we've restored xml to original state - lets mark it as "unchanged"
      MarkDocumentIsUnmodified( hSkilla );
      
      throw;
    }
  }
  void OnBufferActivated( SCNotification *notifyCode )
  {
    auto loadedBplist = g_pLoadedBplists->find( notifyCode->nmhdr.idFrom );

    if ( loadedBplist == g_pLoadedBplists->end() )
    {
      // Get the current scintilla

      HWND hSkilla;
      CharVt rawBuff = ReadFromSkintilla( hSkilla );

      // not a bplist file
      if ( !IsValidBplist( rawBuff ) )
      {
        return;
      }

      PlistEntryPtr plistEntry = MAKE_PLIST_PTR();

      // get converted text to display
      auto XMLVt = plistEntry->GetXML( std::move( rawBuff ) );
      InsertDataIntoSkilla( hSkilla, XMLVt.data(), XMLVt.size() );

      g_pLoadedBplists->insert( std::make_pair( notifyCode->nmhdr.idFrom, std::move( plistEntry ) ) );

      MarkDocumentIsUnmodified( hSkilla );
    }
  }
  void OnBufferActivated( SCNotification *notifyCode )
  {
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
      return;
    HWND hCurrentEditView = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;




    int cbText = ::SendMessage(hCurrentEditView, SCI_GETLENGTH, NULL, NULL);

    auto loadedBplist = g_pLoadedBplists->find( notifyCode->nmhdr.idFrom );

    if ( loadedBplist == g_pLoadedBplists->end() )
    {
      // Get the current scintilla

      HWND hSkilla;
      CharVt rawBuff = ReadFromSkintilla( hSkilla );

      // not a bplist file
      if ( !IsValidBplist( rawBuff ) )
      {
        return;
      }

      PlistEntryPtr plistEntry = MAKE_PLIST_PTR();

      // get converted text to display
      auto XMLVt = plistEntry->GetXML( std::move( rawBuff ) );
      InsertDataIntoSkilla( hSkilla, XMLVt.data(), XMLVt.size() );

      g_pLoadedBplists->insert( std::make_pair( notifyCode->nmhdr.idFrom, std::move( plistEntry ) ) );

      MarkDocumentIsUnmodified( hSkilla );
    }
  }
  //
  // Raw bplist data was saved, so now lets return friendly xml plist data to notepad++
  //
  void OnFileSaved( SCNotification *notifyCode )
  {
    auto loadedBplist = g_pLoadedBplists->find( notifyCode->nmhdr.idFrom );

    if ( loadedBplist != g_pLoadedBplists->end() &&
         loadedBplist->second->GetContentType() != ContentType::corrupted ) // check that file was saved properly
    {
      // Get the current scintilla
      HWND hSkilla;
      CharVt rawBuff = ReadFromSkintilla( hSkilla );

      // not a bplist file
      if ( !IsValidBplist( rawBuff ) )
      {
        return;
      }

      // get converted text to display
      auto XMLVt = loadedBplist->second->GetXML( std::move( rawBuff ) );
      InsertDataIntoSkilla( hSkilla, XMLVt.data(), XMLVt.size() );

      MarkDocumentIsUnmodified(hSkilla);
    }
  }