From 8b0f5165d245781acbff80e91e941782f3d6dd2b Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 2 Jun 2016 13:47:13 -0700 Subject: [PATCH] Added a -quitOnEmpty command line flag closes #1923, Fixes #1006 -quitOnEmpty command line flag makes Notepad++ to quit when the last tab is closed. Useful for people who use Notead++ for things like editing Git commit messages (using -multiInst -notabbar -nosession), and want to signal they are done editing by closing the tab with Ctrl-W instead of Alt-F4. --- PowerEditor/src/Notepad_plus.cpp | 11 +++++++++++ PowerEditor/src/Notepad_plus.h | 4 ++++ PowerEditor/src/Notepad_plus_Window.cpp | 2 ++ PowerEditor/src/NppBigSwitch.cpp | 2 ++ PowerEditor/src/NppIO.cpp | 14 ++++++++++++++ PowerEditor/src/Parameters.h | 2 ++ PowerEditor/src/winmain.cpp | 2 ++ 7 files changed, 37 insertions(+) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 0b617c283..9ea2fbba2 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -3266,6 +3266,17 @@ bool Notepad_plus::canHideView(int whichOne) return canHide; } +bool Notepad_plus::isEmpty() +{ + if (bothActive()) return false; + + DocTabView * tabToCheck = (_mainWindowStatus & WindowMainActive) ? &_mainDocTab : &_subDocTab; + + Buffer * buf = MainFileManager->getBufferByID(tabToCheck->getBufferByIndex(0)); + bool isEmpty = ((tabToCheck->nbItem() == 1) && !buf->isDirty() && buf->isUntitled()); + return isEmpty; +} + void Notepad_plus::loadBufferIntoView(BufferID id, int whichOne, bool dontClose) { DocTabView * tabToOpen = (whichOne == MAIN_VIEW)?&_mainDocTab:&_subDocTab; diff --git a/PowerEditor/src/Notepad_plus.h b/PowerEditor/src/Notepad_plus.h index 6a74b97d1..faf9538ef 100644 --- a/PowerEditor/src/Notepad_plus.h +++ b/PowerEditor/src/Notepad_plus.h @@ -364,6 +364,8 @@ private: bool _sysMenuEntering = false; + // make sure we don't recursively call doClose when closing the last file with -quitOnEmpty + bool _isAttemptingCloseOnQuit = false; // For FullScreen/PostIt features VisibleGUIConf _beforeSpecialView; @@ -464,6 +466,8 @@ private: bool canHideView(int whichOne); //true if view can safely be hidden (no open docs etc) + bool isEmpty(); // true if we have 1 view with 1 clean, untitled doc + int switchEditViewTo(int gid); //activate other view (set focus etc) void docGotoAnotherEditView(FileTransferMode mode); //TransferMode diff --git a/PowerEditor/src/Notepad_plus_Window.cpp b/PowerEditor/src/Notepad_plus_Window.cpp index 166d06a25..1dc683898 100644 --- a/PowerEditor/src/Notepad_plus_Window.cpp +++ b/PowerEditor/src/Notepad_plus_Window.cpp @@ -176,6 +176,8 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin if (nppGUI._rememberLastSession && !cmdLineParams->_isNoSession) _notepad_plus_plus_core.loadLastSession(); + nppGUI._quitOnEmpty = cmdLineParams->_quitOnEmpty; + if (not cmdLineParams->_isPreLaunch) { if (cmdLineParams->isPointValid()) diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 5179c5ba9..16ac711e7 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -1646,7 +1646,9 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lPa //Causing them to show on restart even though they are loaded by session _lastRecentFileList.setLock(true); //only lock when the session is remembered } + _isAttemptingCloseOnQuit = true; bool allClosed = fileCloseAll(false, isSnapshotMode); //try closing files before doing anything else + _isAttemptingCloseOnQuit = false; if (nppgui._rememberLastSession) _lastRecentFileList.setLock(false); //only lock when the session is remembered diff --git a/PowerEditor/src/NppIO.cpp b/PowerEditor/src/NppIO.cpp index bcc463737..07d8c4ba0 100644 --- a/PowerEditor/src/NppIO.cpp +++ b/PowerEditor/src/NppIO.cpp @@ -599,6 +599,10 @@ void Notepad_plus::doClose(BufferID id, int whichOne, bool doDeleteBackup) if (i == -1) return; + size_t numInitialOpenBuffers = + ((_mainWindowStatus & WindowMainActive) == WindowMainActive ? _mainDocTab.nbItem() : 0) + + ((_mainWindowStatus & WindowSubActive) == WindowSubActive ? _subDocTab.nbItem() : 0); + if (doDeleteBackup) MainFileManager->deleteCurrentBufferBackup(); @@ -675,6 +679,16 @@ void Notepad_plus::doClose(BufferID id, int whichOne, bool doDeleteBackup) } } command(IDM_VIEW_REFRESHTABAR); + + if (NppParameters::getInstance()->getNppGUI()._quitOnEmpty) + { + // the user closed the last open tab + if (numInitialOpenBuffers == 1 && isEmpty() && !_isAttemptingCloseOnQuit) + { + command(IDM_FILE_EXIT); + } + } + return; } diff --git a/PowerEditor/src/Parameters.h b/PowerEditor/src/Parameters.h index c6bb3e94b..6e56865ef 100644 --- a/PowerEditor/src/Parameters.h +++ b/PowerEditor/src/Parameters.h @@ -183,6 +183,7 @@ struct CmdLineParams int _line2go = -1; int _column2go = -1; int _pos2go = -1; + bool _quitOnEmpty = false; POINT _point; bool _isPointXValid = false; @@ -755,6 +756,7 @@ struct NppGUI final char _rightmostDelimiter = ')'; bool _delimiterSelectionOnEntireDocument = false; bool _backSlashIsEscapeCharacterForSql = true; + bool _quitOnEmpty = false; // 0 : do nothing diff --git a/PowerEditor/src/winmain.cpp b/PowerEditor/src/winmain.cpp index f0952e9f8..152231687 100644 --- a/PowerEditor/src/winmain.cpp +++ b/PowerEditor/src/winmain.cpp @@ -240,6 +240,7 @@ const TCHAR FLAG_HELP[] = TEXT("--help"); const TCHAR FLAG_ALWAYS_ON_TOP[] = TEXT("-alwaysOnTop"); const TCHAR FLAG_OPENSESSIONFILE[] = TEXT("-openSession"); const TCHAR FLAG_RECURSIVE[] = TEXT("-r"); +const TCHAR FLAG_QUIT_ON_EMPTY[] = TEXT("-quitOnEmpty"); static void doException(Notepad_plus_Window & notepad_plus_plus) @@ -304,6 +305,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) cmdLineParams._point.x = getNumberFromParam('x', params, cmdLineParams._isPointXValid); cmdLineParams._point.y = getNumberFromParam('y', params, cmdLineParams._isPointYValid); cmdLineParams._easterEggName = getEasterEggNameFromParam(params, cmdLineParams._quoteType); + cmdLineParams._quitOnEmpty = isInList(FLAG_QUIT_ON_EMPTY, params); if (showHelp)