Created
April 21, 2013 17:04
-
-
Save kaste/5430272 to your computer and use it in GitHub Desktop.
Automatically close old tabs in sublime 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import time | |
DEFAULT_KEEP_OPEN = 8 | |
class TidyUpCommand(sublime_plugin.EventListener): | |
def __init__(self): | |
self.views = {} | |
def on_activated(self, view): | |
if not view.file_name(): | |
return | |
settings = view.settings().get('TidyUp', {}) | |
if not settings.get('mode', 'auto') == 'auto': | |
return | |
self.views[view.id()] = time.time() | |
keep_open = settings.get('keep_open', DEFAULT_KEEP_OPEN) | |
if len(self.views.keys()) > keep_open: | |
self.tidy_up(view.window(), keep_open) | |
def tidy_up(self, window, keep_open): | |
views = sorted(self.views.items(), key=lambda (id, timestamp): timestamp) | |
# print views | |
for id, _ in views[:-keep_open]: | |
for view in window.views(): | |
if (not view.is_dirty() and not view.is_scratch() | |
and view.id() == id): | |
# print "Remove %s" % id, view.file_name() | |
group, index = window.get_view_index(view) | |
window.run_command('close_by_index', | |
dict(group=group, index=index)) | |
self.views = dict(views[-keep_open:]) | |
# def on_new(self, view): | |
# def on_load(self, view): | |
# def on_close(self, view): | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment