-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyncscroll.py
109 lines (102 loc) · 4.3 KB
/
syncscroll.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
import sublime, sublime_plugin
try:
import _thread # py3
except ImportError:
import thread as _thread # py2
import time
synch_scroll_running = False
synch_scroll_current_view_object = None
def updatePos(view):
#print 'updatepos'
view.settings().set('origPos',view.viewport_position())
def initialize(view):
#print 'initialize'
if not view.settings().has('syncScroll'):
view.settings().set('syncScroll',False)
#the add on change should be here, it's elsewhere for debug reasons
updatePos(view)
view.settings().clear_on_change('syncScroll') #for debug reasons
view.settings().add_on_change('syncScroll', updateStatus) #when syncScroll is toggled, update status bar
settings = sublime.load_settings('Sync View Scroll.sublime-settings')
status_off = settings.get('status_off')
if status_off:
view.set_status('syncScroll', status_off)
def plugin_loaded():
if not 'running_synch_scroll_loop' in globals():
global running_synch_scroll_loop
running_synch_scroll_loop = True
_thread.start_new_thread(synch_scroll_loop, ())
#on startup initialize every view
print ("syncScroll starting")
for window in sublime.windows():
for view in window.views():
initialize(view)
def synch_scroll_loop():
while True:
global synch_scroll_running
if not synch_scroll_running:
synch_scroll_running = True
sublime.set_timeout(lambda: synch_scroll(), 0)
time.sleep(0.08)
def synch_scroll():
global synch_scroll_running
global synch_scroll_current_view_object
# print ("one timeout")
current_view = synch_scroll_current_view_object
if current_view is None or current_view.is_loading() or not current_view.settings().get('syncScroll'):
synch_scroll_running = False
return
callingViewPosX, callingViewPosY = current_view.viewport_position()
origCallingViewPosX, origCallingViewPosY = current_view.settings().get('origPos')
# print ('modified. origCallingViewPos=', origCallingViewPosX, origCallingViewPosY, 'callingViewPos= ', callingViewPosX, callingViewPosY)
if callingViewPosX != origCallingViewPosY or callingViewPosY != origCallingViewPosY: #and it moved vertically or horizontally
# print ("it moved")
for view in current_view.window().views():
if view.settings().get('syncScroll') and view.id() != current_view.id(): #if view has syncScroll enabled AND we're not talking about the same view as view
#we move view
viewPosX, viewPosY = view.viewport_position()
newViewPosX = viewPosX+callingViewPosX-origCallingViewPosX
newViewPosY = viewPosY+callingViewPosY-origCallingViewPosY
# print ("moving. viewPos= ",viewPosX,viewPosY," newViewPos= ",newViewPosX,newViewPosY)
view.set_viewport_position((newViewPosX,newViewPosY), True) #move the other view
updatePos(view)
updatePos(current_view) #update original positions
synch_scroll_running = False
def updateStatus():
# print "updateStatus"
settings = sublime.load_settings('Sync View Scroll.sublime-settings')
for window in sublime.windows():
for view in window.views():
if view.settings().get('syncScroll'):
status_on = settings.get('status_on')
if status_on:
view.set_status('syncScroll', status_on)
else:
status_off = settings.get('status_off')
if status_off:
view.set_status('syncScroll', status_off)
else:
view.erase_status('syncScroll')
class syncScrollListener(sublime_plugin.EventListener):
def on_activated(self, view):
global synch_scroll_current_view_object
synch_scroll_current_view_object = view
view.settings().set('origPos', view.viewport_position())
def on_load(self,view):
#on load add settings to a view
# print ("on_load")
initialize(view)
def on_text_command(self, current_view, command_name, args):
if current_view.settings().get('syncScroll') and command_name == 'move_to' and args['to'] in ['bof', 'eof']:
for view in current_view.window().views():
if view.settings().get('syncScroll') and view.id() != current_view.id():
view.run_command(command_name, args)
class ToggleSyncScrollCommand(sublime_plugin.TextCommand):
def run(self, edit):
current_state = self.view.settings().get('syncScroll')
self.view.settings().set('syncScroll',not current_state)
def is_checked(self):
if not self.view.settings().has('syncScroll'):
initialize(self.view)
# print ("current setting",self.view.settings().get('syncScroll'))
return self.view.settings().get('syncScroll')