-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTabright.py
137 lines (110 loc) · 3.19 KB
/
Tabright.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import sys
import sublime
import sublime_plugin
class TabrightListener(sublime_plugin.EventListener):
busy = False
def __init__(self):
settings = sublime.load_settings("Tabright.sublime-settings")
self.open_new_tabs_at = settings.get("open_new_tabs_at", "far_right")
self.files_only = settings.get("files_only", False)
self.view_ids = []
self.active_group = 0
self.run_at_reload()
self.busy = False
def on_close(self, view):
if (view.id() not in self.view_ids):
self.focus_last()
return
if len(self.view_ids) > 1:
window = sublime.active_window()
views = window.views()
oldIndex = self.view_ids.index(view.id())
index = self.view_ids.index(view.id())
if len(self.view_ids) > index + 1:
index += 1
else:
index = len(self.view_ids)-2
view_id = self.view_ids[index]
self.focus_view_id(view_id)
del(self.view_ids[oldIndex])
def on_activated(self, view):
if self.busy:
return
def callback(view=view):
return self.process_tabs(view)
self.busy = True
sublime.set_timeout(callback,10)
def run_at_reload(self):
window = sublime.active_window()
if window==None:
return
views = window.views()
if (len(views)>0):
self.process_tabs(window.active_view())
def process_tabs(self, view):
window = sublime.active_window()
if int(sublime.version()) < 3000:
is_preview = view.file_name() is not "None" and window and view.file_name() not in [file.file_name() for file in window.views()]
if is_preview:
if view.id() not in self.view_ids:
self.view_ids.append(view.id())
self.busy = False
return
group, index = window.get_view_index(view)
views = window.views_in_group(group)
view_ids = []
new_view_ids = []
# looking at a new group? just renew everything
if (group != self.active_group):
self.view_ids = []
for v in views:
if v.id() not in self.view_ids:
if self.files_only and (v.file_name() == None or v.file_name() == "None"):
pass
else:
new_view_ids.append(v.id())
view_ids.append(v.id())
if len(new_view_ids) != 1:
#we accept the current index
self.view_ids = view_ids
self.active_group = group
self.busy = False
return
# make the new tabs appear at the tabright position
view_ids = [0] * len(view_ids)
offset_old = 0;
offset_new = 0;
for v in views:
group, index = window.get_view_index(v)
if v.id() not in new_view_ids:
if (self.open_new_tabs_at == "far_left"):
index = offset_old + offset_new
else:
index = offset_old
offset_old += 1
else:
if (self.open_new_tabs_at == "far_left"):
index = offset_new
else:
index = len(view_ids)-len(new_view_ids)+offset_new
offset_new += 1
view_ids[index] = v.id()
window.set_view_index(v, group, index)
self.view_ids = view_ids
self.active_group = group
self.focus_last()
self.busy = False
def focus_last(self):
if (len(self.view_ids)==0):
return
if (self.open_new_tabs_at == "far_left"):
self.focus_view_id(self.view_ids[0])
else:
self.focus_view_id(self.view_ids[-1])
def focus_view_id(self, view_id):
window = sublime.active_window()
views = window.views()
for v in views:
if v.id() == view_id:
window.focus_view(v)