-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidalcycles.py
54 lines (45 loc) · 1.37 KB
/
tidalcycles.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
import os
import sublime
import sublime_plugin
sessionName = 'tidal'
class TidalEvalCommand(sublime_plugin.TextCommand):
def run(self, edit):
for selectionRegion in self.view.sel():
lineRegion = self.view.line(selectionRegion)
line = self.view.substr(lineRegion)
def getStrippedLine(cursorAt):
lineRegion = self.view.line(sublime.Region(cursorAt, cursorAt))
return self.view.substr(lineRegion).strip()
def startsWithWhitespace(s):
return s.strip()[0] != s[0]
# get surrounding lines, to evaluate entire block
lines = [line]
while True:
a = lineRegion.a
b = lineRegion.b
if not (a == 0 or getStrippedLine(a - 1) == ''):
lineRegion.a = a - 1
if not (b >= self.view.size() - 1 or getStrippedLine(b + 1) == ''):
lineRegion.b = b + 1
if lineRegion.a == a and lineRegion.b == b:
break
s = self.view.substr(lineRegion)
lines = s.splitlines()
if lines[0] == '' and lines[-1] == '':
break
lines = [
line.replace('"', '\\\"')
for line in lines
if line.strip() != ''
]
lines = [
line if (startsWithWhitespace(line) or i == 0) else ' ; {}'.format(line)
for i, line in list(enumerate(lines))
]
code = ''.join(lines)
codes = [c.strip() for c in code.split(';')]
print(codes)
for c in codes:
os.system(
'tmux send-keys -t "{}" "{}" C-m \\;'.format(sessionName, c)
)