Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Close N++ when last doc is closed

UFO
2012-05-22
2013-01-25
  • UFO
    UFO
    2012-05-22

    I just saw this idea on Stackoverflow and had to try it:
    http://stackoverflow.com/questions/10343102/closing-notepad-when-the-last-document-is-closed

    The Initialisation mode has to be ATSTARTUP and the code is placed at the end of the startup.py

    def shutdownNppOnLastFileClosed(args):
        import os
        files = notepad.getFiles()
        # there are always at least 2 'buffers' open in N++
        if len(files) == 2:
            currentBufferID = notepad.getCurrentBufferID()
            for (filename, bufferID, index, view) in files:
                if os.path.exists(filename):
                    break
                notepad.activateBufferID(bufferID) 
                if editor.getLength() > 0:
                    break
                # TODO: just to be on the safe side - if we
                # reached here, we actually should also check
                # if the 2 left empty buffers are not unsaved,
                # but I couldn't find a way to do that.
            else:
                # following 'menuCommand' looks cleaner than
                # the 'sys.exit' but it currently deadlocks N++:
                #notepad.menuCommand(MENUCOMMAND.FILE_EXIT)
                sys.exit(0)
            notepad.activateBufferID(currentBufferID)
    notepad.callback(shutdownNppOnLastFileClosed, [NOTIFICATION.FILECLOSED])