Add auto save loaded session on exit feature

This new feature works only if Multi-instance settings is NOT set to "Default (Mono-instance)".

Fix #1646, fix #3241, fix #3574, fix #4228, close #10935
This commit is contained in:
Don Ho 2021-12-22 19:32:55 +01:00
parent a563a8b812
commit a16930fffe
9 changed files with 30 additions and 7 deletions

View File

@ -1051,7 +1051,7 @@ You can define several column markers by using white space to separate the diffe
<MultiInstance title="Multi-Instance &amp; Date">
<Item id="6151" name="Multi-instance settings"/>
<Item id="6152" name="Open session in a new instance of Notepad++"/>
<Item id="6152" name="Open session in a new instance (and save session automatically on exit)"/>
<Item id="6153" name="Always in multi-instance mode"/>
<Item id="6154" name="Default (mono-instance)"/>
<Item id="6155" name="* The modification of this setting needs to restart Notepad++"/>

View File

@ -1028,7 +1028,7 @@ Vous pouvez définir plusieurs marqueurs de colonne en utilisant un espace pour
<MultiInstance title="Instance Multiple &amp; Date">
<Item id="6151" name="Options d'instance multiple"/>
<Item id="6152" name="Ouverture de session dans une nouvelle instance"/>
<Item id="6152" name="Ouverture de session dans une nouvelle instance (et enregistrer la session automatiquement en sortant)"/>
<Item id="6153" name="Toujours en mode instance multiple"/>
<Item id="6154" name="Défaut (instance unique)"/>
<Item id="6155" name="* Redémarrez Notepad++ pour prendre en compte la modification"/>

View File

@ -1043,7 +1043,7 @@
</AutoCompletion>
<MultiInstance title="多重實體與日期時間">
<Item id="6151" name="多重 Notepad++ 實體設定"/>
<Item id="6152" name="在新的 Notepad++ 實體開啟工作階段"/>
<Item id="6152" name="在新的 Notepad++ 實體開啟工作階段(並在結束程式前自動儲存載入的工作階段)"/>
<Item id="6153" name="允許多重實體"/>
<Item id="6154" name="禁止多重實體(預設)"/>
<Item id="6155" name="(重新啟動 Notepad++ 以使變更生效。)"/>

View File

@ -6096,6 +6096,8 @@ std::vector<generic_string> Notepad_plus::loadCommandlineParams(const TCHAR * co
{
const bool isSnapshotMode = false;
const bool shouldLoadFileBrowser = true;
nppParams.setLoadedSessionFilePath(fnss.getFileName(0));
loadSession(session2Load, isSnapshotMode, shouldLoadFileBrowser);
}
return std::vector<generic_string>();

View File

@ -1995,6 +1995,14 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
if (nppgui._rememberLastSession && !nppgui._isCmdlineNosessionActivated)
saveSession(currentSession);
//
// saving session.xml into loaded session if a saved session is loaded and saveLoadedSessionOnExit option is enabled
//
generic_string loadedSessionFilePath = nppParam.getLoadedSessionFilePath();
if (!loadedSessionFilePath.empty() && PathFileExists(loadedSessionFilePath.c_str()))
nppParam.writeSession(currentSession, loadedSessionFilePath.c_str());
// write settings on cloud if enabled, if the settings files don't exist
if (nppgui._cloudPath != TEXT("") && nppParam.isCloudPathChanged())
{

View File

@ -2296,15 +2296,17 @@ bool Notepad_plus::fileLoadSession(const TCHAR *fn)
bool isAllSuccessful = true;
Session session2Load;
if ((NppParameters::getInstance()).loadSession(session2Load, sessionFileName.c_str()))
if (nppParam.loadSession(session2Load, sessionFileName.c_str()))
{
const bool isSnapshotMode = false;
const bool shouldLoadFileBrowser = true;
isAllSuccessful = loadSession(session2Load, isSnapshotMode, shouldLoadFileBrowser);
result = true;
if (isEmptyNpp && (nppGUI._multiInstSetting == multiInstOnSession || nppGUI._multiInstSetting == multiInst))
nppParam.setLoadedSessionFilePath(sessionFileName);
}
if (!isAllSuccessful)
(NppParameters::getInstance()).writeSession(session2Load, sessionFileName.c_str());
nppParam.writeSession(session2Load, sessionFileName.c_str());
}
if (result == false)
{
@ -2315,6 +2317,7 @@ bool Notepad_plus::fileLoadSession(const TCHAR *fn)
MB_OK);
}
}
return result;
}

View File

@ -2093,7 +2093,6 @@ void NppParameters::setWorkingDir(const TCHAR * newPath)
}
}
bool NppParameters::loadSession(Session & session, const TCHAR *sessionFileName)
{
TiXmlDocument *pXmlSessionDocument = new TiXmlDocument(sessionFileName);

View File

@ -1540,6 +1540,15 @@ public:
};
bool loadSession(Session & session, const TCHAR *sessionFileName);
void setLoadedSessionFilePath(const generic_string & loadedSessionFilePath) {
_loadedSessionFullFilePath = loadedSessionFilePath;
};
generic_string getLoadedSessionFilePath() {
return _loadedSessionFullFilePath;
};
int langTypeToCommandID(LangType lt) const;
WNDPROC getEnableThemeDlgTexture() const {return _enableThemeDialogTextureFuncAddr;};
@ -1731,6 +1740,8 @@ private:
generic_string _cmdSettingsDir;
generic_string _titleBarAdditional;
generic_string _loadedSessionFullFilePath;
public:
void setShortcutDirty() { _isAnyShortcutModified = true; };
void setAdminMode(bool isAdmin) { _isAdminMode = isAdmin; }

View File

@ -383,7 +383,7 @@ STYLE DS_SETFONT | DS_FIXEDSYS | DS_CONTROL | WS_CHILD
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
GROUPBOX "Multi-instance settings",IDC_MULTIINST_GB_STATIC,89,3,268,89,BS_CENTER
CONTROL "Open session in a new instance of Notepad++",IDC_SESSIONININST_RADIO,"Button",BS_AUTORADIOBUTTON,122,17,226,10
CONTROL "Open session in a new instance (and save session automatically on exit)",IDC_SESSIONININST_RADIO,"Button",BS_AUTORADIOBUTTON|BS_MULTILINE,122,10,226,20
CONTROL "Always in multi-instance mode",IDC_MULTIINST_RADIO,"Button",BS_AUTORADIOBUTTON,122,34,218,10
CONTROL "Default (mono-instance)",IDC_MONOINST_RADIO,"Button",BS_AUTORADIOBUTTON,122,51,196,10
LTEXT "* The modification of this setting needs to restart Notepad++",IDD_STATIC_RESTARTNOTE,110,70,239,20