Skip to content

Commit dd43581

Browse files
committed
add support for multiple selections
1 parent 8ec1194 commit dd43581

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

AppleScript (Binary).py

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,64 @@
1414
import subprocess
1515

1616
# GLOBAL STUFF
17-
saved_cursor_positions = []
18-
SYNTAX_FILE = 'Packages/AppleScript Extensions/AppleScript (Binary).sublime-syntax'
19-
END_REGEX = r'f\s?a\s?d\s?e\s?d\s?e\s?a\s?d\s?\Z'
17+
saved_cursor_data = []
18+
SYNTAX_FILE = "Packages/AppleScript Extensions/AppleScript (Binary).sublime-syntax"
19+
END_REGEX = r"f\s?a\s?d\s?e\s?d\s?e\s?a\s?d\s?\Z"
2020

21-
class SaveCursorPositionsCommand(TextCommand):
21+
22+
class SaveCursorPositionsCommand(sublime_plugin.TextCommand):
2223
def run(self, edit):
23-
global saved_cursor_positions
24+
global saved_cursor_data
25+
26+
selections = self.view.sel()
2427

25-
# Get the list of cursor positions
26-
saved_cursor_positions = [region.begin() for region in self.view.sel()]
28+
if any(region.size() > 0 for region in selections):
29+
# Save selections
30+
saved_cursor_data = [(region.a, region.b) for region in selections]
31+
else:
32+
# Save cursor positions
33+
saved_cursor_data = [(region.a,) for region in selections]
2734

28-
class RestoreCursorPositionsCommand(TextCommand):
35+
36+
class RestoreCursorPositionsCommand(sublime_plugin.TextCommand):
2937
def run(self, edit):
30-
global saved_cursor_positions
31-
if saved_cursor_positions:
32-
# Clear the current selections
38+
global saved_cursor_data
39+
40+
if saved_cursor_data:
3341
self.view.sel().clear()
3442

35-
# Restore saved cursor positions
36-
for pos in saved_cursor_positions:
37-
self.view.sel().add(sublime.Region(pos))
43+
for data in saved_cursor_data:
44+
if len(data) == 2:
45+
# Restore selection
46+
self.view.sel().add(sublime.Region(data[0], data[1]))
47+
else:
48+
# Restore cursor position
49+
self.view.sel().add(sublime.Region(data[0]))
3850

39-
# Optionally, scroll to the first cursor position
40-
self.view.show(saved_cursor_positions[0])
4151

4252
def is_syntax_set(view=None):
4353
if view is None:
4454
view = sublime.active_window().active_view()
45-
return 'AppleScript (Binary).sublime-syntax' in view.settings().get('syntax')
55+
return "AppleScript (Binary).sublime-syntax" in view.settings().get("syntax")
56+
4657

4758
def is_binary(view):
4859
selection = view.substr(Region(0, view.size()))
49-
return (re.search(END_REGEX, selection))
60+
return re.search(END_REGEX, selection)
61+
5062

5163
class ScptBinaryCommand(EventListener):
5264
def on_load(self, view):
5365
# Check if binary, convert to plain-text, mark as "was binary"
5466
if is_binary(view):
55-
view.run_command('binary_toggle')
56-
67+
view.run_command("binary_toggle")
68+
5769
def on_post_save(self, view):
5870
# Convert back to plain-text
59-
if view.get_status('is_binary'):
60-
view.run_command('save_cursor_positions')
61-
view.run_command('binary_toggle', {'force_to': True})
62-
view.run_command('restore_cursor_positions')
71+
if view.get_status("is_binary"):
72+
view.run_command("save_cursor_positions")
73+
view.run_command("binary_toggle", {"force_to": True})
74+
view.run_command("restore_cursor_positions")
6375

6476
def on_new(self, view):
6577
pass
@@ -77,58 +89,66 @@ def on_pre_save(self, view):
7789
pass
7890

7991
def on_modified(self, view):
80-
freshly_written = view.settings().get('freshly_written')
92+
freshly_written = view.settings().get("freshly_written")
8193
if freshly_written and is_binary(view):
8294

83-
view.run_command('save_cursor_positions')
84-
view.run_command('binary_toggle')
85-
view.run_command('restore_cursor_positions')
95+
view.run_command("save_cursor_positions")
96+
view.run_command("binary_toggle")
97+
view.run_command("restore_cursor_positions")
8698

87-
view.settings().erase('freshly_written')
99+
view.settings().erase("freshly_written")
88100

89101
def on_activated(self, view):
90102
pass
91103

104+
92105
class BinaryToggleCommand(TextCommand):
93106
def decode_script(self, edit, view):
94107
"""Reads in the view's file, converts it to plain and replaces the view's
95108
buffer with the plain-text."""
96-
file_name = view.file_name()
109+
file_name = view.file_name()
97110

98-
if file_name and file_name != '' and os.path.isfile(file_name) == True and file_name.endswith('.scpt'):
99-
cmd = ['osadecompile', file_name]
111+
if (
112+
file_name
113+
and file_name != ""
114+
and os.path.isfile(file_name) == True
115+
and file_name.endswith(".scpt")
116+
):
117+
cmd = ["osadecompile", file_name]
100118
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
101119
full_text, err = p.communicate()
102120

103-
view.set_encoding('UTF-8')
104-
view.replace(edit, Region(0, view.size()), str(full_text.decode('utf-8')))
121+
view.set_encoding("UTF-8")
122+
view.replace(edit, Region(0, view.size()), str(full_text.decode("utf-8")))
105123
view.end_edit(edit)
106-
view.set_status('is_binary', 'Decompiled File')
124+
view.set_status("is_binary", "Decompiled File")
107125
view.set_scratch(True)
108126

109127
def encode_script(self, view):
110128
"""Converts the view's plain-text back to a binary script and writes it out
111129
to the view's file."""
112130
file_name = view.file_name()
113131

114-
if file_name and file_name != '' and os.path.isfile(file_name) == True:
115-
bytes = view.substr(Region(0, view.size())).encode('utf-8').rstrip()
132+
if file_name and file_name != "" and os.path.isfile(file_name) == True:
133+
bytes = view.substr(Region(0, view.size())).encode("utf-8").rstrip()
116134
try:
117-
with open(file_name, 'wb') as f:
135+
with open(file_name, "wb") as f:
118136
f.write(bytes)
119137

120-
cmd = ['osacompile', '-o', file_name, file_name]
121-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
138+
cmd = ["osacompile", "-o", file_name, file_name]
139+
p = subprocess.Popen(
140+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
141+
)
122142
out, err = p.communicate()
123143

124-
view.settings().set('freshly_written', True)
144+
view.settings().set("freshly_written", True)
125145
view.sel().clear()
126146
except Exception as e:
127147
sublime.error_message(str(e))
128148
raise e
129149

130150
def run(self, edit, force_to=False):
131-
if platform.system() != 'Darwin':
151+
if platform.system() != "Darwin":
132152
sublime.error_message("Binary AppleScript can only be edited on macOS")
133153
return
134154

0 commit comments

Comments
 (0)