Skip to content

Commit

Permalink
Added a -quitOnEmpty command line flag
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bavis-m authored and donho committed Aug 2, 2016
1 parent 27d8043 commit 8b0f516
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions PowerEditor/src/Notepad_plus.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ friend class FileManager;

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;
Expand Down Expand Up @@ -464,6 +466,8 @@ friend class FileManager;

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
Expand Down
2 changes: 2 additions & 0 deletions PowerEditor/src/Notepad_plus_Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions PowerEditor/src/NppBigSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions PowerEditor/src/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ struct CmdLineParams
int _line2go = -1;
int _column2go = -1;
int _pos2go = -1;
bool _quitOnEmpty = false;

POINT _point;
bool _isPointXValid = false;
Expand Down Expand Up @@ -755,6 +756,7 @@ struct NppGUI final
char _rightmostDelimiter = ')';
bool _delimiterSelectionOnEntireDocument = false;
bool _backSlashIsEscapeCharacterForSql = true;
bool _quitOnEmpty = false;


// 0 : do nothing
Expand Down
2 changes: 2 additions & 0 deletions PowerEditor/src/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8b0f516

Please sign in to comment.