forked from rodcloutier/Vintageous-Origami
/
xactions.py
executable file
·158 lines (122 loc) · 5.56 KB
/
xactions.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
156
157
158
import sublime
import sublime_plugin
from Origami.origami import PaneCommand as OrigamiPaneCommand
def find_duplicate(l):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
return list(x for x in l if x in seen or seen_add(x))
SPLIT_COMMANDS = {
'clone': 'clone_file_to_pane',
'carry': 'carry_file_to_pane'
}
def split_command_from_settings():
prefs = sublime.load_settings('Preferences.sublime-settings')
command = prefs.get('vintageous_origami_split_command')
if command not in SPLIT_COMMANDS:
if command is not None:
print('"vintageous_origami_split_command" option must be in %s' % SPLIT_COMMANDS.keys())
command = 'clone'
return SPLIT_COMMANDS[command]
class _vio_ctrl_w_s(sublime_plugin.WindowCommand):
"""
Split pane horizontally
"""
def run(self):
self.window.run_command("create_pane", {"direction": "down"})
self.window.run_command(split_command_from_settings(), {"direction": "down"})
class _vio_ctrl_w_v(sublime_plugin.WindowCommand):
# TODO There is already a command for this in Vintageous, find it...
"""
Split pane vertically
"""
def run(self):
self.window.run_command("create_pane", {"direction": "right"})
self.window.run_command(split_command_from_settings(), {"direction": "right"})
class _vio_ctrl_w_c(sublime_plugin.WindowCommand):
"""
Close pane
"""
def run(self):
self.window.run_command("close_pane")
# Now remove the duplicates
views = self.window.views_in_group(self.window.active_group())
buffers = [v.buffer_id() for v in views]
duplicates = find_duplicate(buffers)
for v in reversed(views):
buffer_id = v.buffer_id()
if buffer_id in duplicates:
duplicates.pop(duplicates.index(buffer_id))
self.window.focus_view(v)
# Set scratch?
self.window.run_command('close')
class _vio_ctrl_w_n(sublime_plugin.WindowCommand):
"""
Create new file in new horizontal pane
"""
def run(self):
self.window.run_command("create_pane", {"direction": "up"})
self.window.run_command("travel_to_pane", {"direction": "up"})
self.window.run_command("new_file")
def opposite_direction(direction):
opposites = {"up":"down", "right":"left", "down":"up", "left":"right"}
return opposites[direction]
class _vio_exchange_files_with_pane(OrigamiPaneCommand):
def run(self, direction):
window = self.window
window.run_command("carry_file_to_pane", {'direction': direction})
active_group = window.active_group()
views = window.views_in_group(active_group)
active_view = window.active_view()
if views:
window.focus_view(views[1])
window.run_command("carry_file_to_pane", {'direction': opposite_direction(direction)})
window.focus_view(active_view)
class _vio_ctrl_w_o(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("set_layout", {"cells": [[0, 0, 1, 1]], "cols": [0.0, 1.0], "rows": [0.0, 1.0]})
# Ex commands
from Vintageous.ex.ex_command_parser import ex_cmd_data, EX_COMMANDS
# TODO: Add support for filename, args, etc.
EX_COMMANDS[('split', 'sp')] = ex_cmd_data(command="vio_ex_split",
invocations=(),
error_on=())
EX_COMMANDS[('vsplit', 'vs')] = ex_cmd_data(command="vio_ex_vsplit",
invocations=(),
error_on=())
EX_COMMANDS[('new', 'new')] = ex_cmd_data(command="vio_ex_new",
invocations=(),
error_on=())
EX_COMMANDS[('vnew', 'vne')] = ex_cmd_data(command="vio_ex_vnew",
invocations=(),
error_on=())
EX_COMMANDS[('only', 'on')] = ex_cmd_data(command="vio_ex_only",
invocations=(),
error_on=())
# Vintageous version's
# ('vsplit', 'vs'): ex_cmd_data(
# command='ex_vsplit',
# invocations=(
# re.compile(r'^$'),
# re.compile(r'^\s*(?P<file_name>.+)$'),
# ),
# error_on=(ex_error.ERR_NO_RANGE_ALLOWED,
# ex_error.ERR_NO_BANG_ALLOWED,)
# ),
class VioExSplitCommand(sublime_plugin.WindowCommand):
def run(self, line_range=None):
_vio_ctrl_w_s.run(self)
class VioExVsplitCommand(sublime_plugin.WindowCommand):
def run(self, line_range=None):
_vio_ctrl_w_v.run(self)
class VioExNewCommand(sublime_plugin.WindowCommand):
def run(self, line_range=None):
_vio_ctrl_w_n.run(self)
class VioExVnewCommand(sublime_plugin.WindowCommand):
def run(self, line_range=None):
self.window.run_command("create_pane", {"direction": "right"})
self.window.run_command("travel_to_pane", {"direction": "right"})
self.window.run_command("new_file")
class VioExOnlyCommand(sublime_plugin.WindowCommand):
def run(self, line_range=None):
_vio_ctrl_w_o.run(self)