-
Notifications
You must be signed in to change notification settings - Fork 33
/
actual.py
155 lines (119 loc) · 4.28 KB
/
actual.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
import os
import sublime
import sublime_plugin
from .view import ActualVim
from .edit import Edit
from . import settings
class ActualSkipCmd(sublime_plugin.TextCommand): pass
class ActualEnable(sublime_plugin.ApplicationCommand):
def is_enabled(self):
return not settings.enabled()
def run(self):
settings.enable()
class ActualDisable(sublime_plugin.ApplicationCommand):
def is_enabled(self):
return settings.enabled()
def run(self):
settings.disable()
class ActualEnableView(sublime_plugin.TextCommand):
def is_enabled(self):
return settings.enabled() and not self.view.settings().get('av_input')
def run(self, edit):
self.view.settings().set('av_input', True)
v = ActualVim.get(self.view, exact=False, create=False)
if v:
v.update_view()
class ActualDisableView(sublime_plugin.TextCommand):
def is_enabled(self):
return settings.enabled() and self.view.settings().get('av_input', False)
def run(self, edit):
self.view.settings().set('av_input', False)
v = ActualVim.get(self.view, exact=False, create=False)
if v:
v.update_view()
class ActualKeypress(sublime_plugin.TextCommand):
def is_enabled(self):
v = ActualVim.get(self.view, exact=False, create=False)
return bool(v)
def run(self, edit, key=None, character=None):
v = ActualVim.get(self.view, exact=False, create=False)
if v:
if character is not None:
key = character
if key is not None:
if key == '<':
key = '<lt>'
v.press(key, edit=edit)
class ActualViewListener(sublime_plugin.ViewEventListener):
@staticmethod
def is_applicable(settings):
return True
return all((
not settings.get('scratch'),
))
@property
def v(self):
return ActualVim.get(self.view)
# vim buffer only gets created when we call activate()
# to prevent goto anything / other ephemeral views from spewing buffers
def on_load(self):
self.v.activate()
def on_activated(self):
self.v.activate()
# def on_deactivated_async(self):
# pass
# if we don't do this async, bad selections never display, which reduces flickering
def on_selection_modified(self):
self.v.sel_to_vim()
def on_modified(self):
self.v.sync_to_vim()
class ActualGlobalListener(sublime_plugin.EventListener):
def on_new(self, view):
# on_new/on_close doesn't work in view-specific listener
v = ActualVim.get(view)
v.activate()
def on_pre_close(self, view):
v = ActualVim.get(view, create=False)
if v:
v.close()
def on_post_save_async(self, view):
v = ActualVim.get(view, create=False)
if v:
v.set_path(view.file_name())
v.buf.options['modified'] = view.is_dirty()
# block sublime -> vim copies during text commands
# to prevent inconsistent updates
# then force a copy afterwards
def on_text_command(self, view, name, args):
v = ActualVim.get(view, exact=False, create=False)
if not v:
return
if name == 'paste' and view.window().active_panel() == 'input' and v.cmd_panel:
tmp = []
for c in sublime.get_clipboard():
if c == '\n':
tmp.append('<cr>')
break
tmp.append(c)
v.press(''.join(tmp))
return ('actual_skip_cmd', {})
if name == 'drag_select':
v.drag_select = args.get('by')
if not name.startswith('actual_'):
v.block = True
def on_window_command(self, view, name, args):
self.on_text_command(view, name, args)
def on_post_text_command(self, view, name, args):
v = ActualVim.get(view, exact=False, create=False)
if not v:
return
if v.block:
v.block = False
if v.block_hit:
v.block_hit = False
def fix():
v.sync_to_vim(force=True)
sublime.set_timeout(fix, 1)
def on_post_window_command(self, view, name, args):
self.on_post_text_command(view, name, args)