This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
/
TextMate.sc
executable file
·258 lines (228 loc) · 7 KB
/
TextMate.sc
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
TextMate {
classvar menu, <openClassInTextMate, <openReferencesInTextMate;
*saveState {
(
classfiles: openClassInTextMate.state,
references: openReferencesInTextMate.state
).writeArchive("%/.textmate-settings".format(Platform.userAppSupportDir));
}
*initClass {
var opt;
var settings;
var setpath = "%/.textmate-settings".format(Platform.userAppSupportDir);
if(File.exists(setpath)) {
settings = Object.readArchive(setpath);
} {
settings = (
classfiles: true,
references: true
)
};
try {
menu = SCMenuGroup(nil, "TextMate", 7);
opt = SCMenuItem(menu, "TextMate to front")
.action_{
"osascript << END
tell application \"TextMate\" to activate
END
".unixCmd(postOutput: false)
}
.setShortCut("T");
SCMenuSeparator(menu);
openClassInTextMate = SCMenuItem(menu, "Open class files in TextMate")
.action_{ |item|
item.state = item.state.not;
this.saveState;
}
.state_(settings.classfiles);
openReferencesInTextMate = SCMenuItem(menu, "Open references in TextMate")
.action_{ |item|
item.state = item.state.not;
this.saveState;
}
.state_(settings.references);
SCMenuSeparator(menu, 4);
SCMenuItem(menu, "About SuperCollider bundle")
.action_{ |item|
"open 'http://github.com/rfwatson/supercollider-tmbundle'".unixCmd(postOutput:false);
}
} {
"TextMate found a problem installing CocoaMenuItems - you may be running SC 3.2 or older, or booting from command-line.".warn
}
}
}
// adapted from http://github.com/rfwatson/sc3ctrl
SC3Controller {
classvar nodes;
*initClass {
nodes = List[];
Platform.case(\osx) {
StartUp.add {
this.addListeners;
}
}
}
*addListeners {
var node, postToFront;
postToFront = {
Document.listener.front;
};
if(nodes.isEmpty) {
node = OSCresponderNode(nil, '/sc3ctrl/cmd') { |t, r, msg|
{
thisThread.clock = SystemClock;
msg[1].asString.interpretPrint;
postToFront.();
}.defer
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/help') { |t, r, msg|
{
msg[1].asString.openHelpFile;
}.defer
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/class') { |t, r, msg|
// TM version only
var fname, cmd;
var klass = msg[1].asString;
var allClasses = Class.allClasses.collect(_.asString);
{
if(TextMate.openClassInTextMate.state) {
if(allClasses.detect{ |str| str == klass }.notNil) { // .includes doesn't work?
fname = klass.interpret.filenameSymbol;
cmd = "grep -nh \"^" ++ klass ++ "\" \"" ++ fname ++ "\" > /tmp/grepout.tmp";
cmd.unixCmd(postOutput: false, action: {
File.use("/tmp/grepout.tmp", "r") { |f|
var content = f.readAllString;
var split = content.split($:);
if("^[0-9]+$".matchRegexp(split.first.asString)) {
("mate -l" ++ split.first + "\"" ++ fname ++ "\"").unixCmd(postOutput: false);
} {
("mate -l1 \"" ++ fname ++ "\"").unixCmd(postOutput: false);
}
};
});
}
} { // open in SC.app
klass.interpret.openCodeFile;
};
}.defer
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/implementations') { |t, r, msg|
if(TextMate.openReferencesInTextMate.state) {
{ this.methodTemplates(msg[1], true) }.defer
} { // open in SC.app
{ this.methodTemplates(msg[1], false) }.defer
}
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/references') { |t, r, msg|
if(TextMate.openReferencesInTextMate.state) {
{ this.methodReferences(msg[1], true) }.defer
} { // open in SC.app
{ this.methodReferences(msg[1], false) }.defer
}
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/stop') { |t, r, msg|
thisProcess.stop; nil;
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/clear') { |t, r, msg|
{
Document.listener.string = ""; "";
postToFront.();
}.defer;
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/postfront') { |t, r, msg|
{ postToFront.() }.defer;
}.add;
nodes.add(node);
node = OSCresponderNode(nil, '/sc3ctrl/recompile') { |t, r, msg|
{
thisProcess.recompile;
postToFront.();
}.defer;
}.add;
nodes.add(node);
}
}
*removeAllListeners {
nodes.do(_.remove);
}
// adapated from Kernel.sc
*methodTemplates { |name, openInTextMate=false|
var out, found = 0, namestring, fname;
out = CollStream.new;
out << "Implementations of '" << name << "' :\n";
Class.allClasses.do({ arg class;
class.methods.do({ arg method;
if (method.name == name, {
found = found + 1;
namestring = class.name ++ ":" ++ name;
out << " " << namestring << " : ";
if (method.argNames.isNil or: { method.argNames.size == 1 }, {
out << "this." << name;
if (name.isSetter, { out << "(val)"; });
},{
out << method.argNames.at(0);
if (name.asString.at(0).isAlpha, {
out << "." << name << "(";
method.argNames.do({ arg argName, i;
if (i > 0, {
if (i != 1, { out << ", " });
out << argName;
});
});
out << ")";
},{
out << " " << name << " ";
out << method.argNames.at(1);
});
});
out.nl;
});
});
});
if(found == 0)
{
Post << "\nNo implementations of '" << name << "'.\n";
}
{
if(openInTextMate) {
fname = "/tmp/" ++ Date.seed ++ ".sc";
File.use(fname, "w") { |f|
f << out.collection.asString;
("mate" + fname).unixCmd(postOutput: false);
};
} {
out.collection.newTextWindow(name.asString);
};
};
}
// adapted from Kernel.sc
*methodReferences { |name, openInTextMate|
var out, references, fname;
name = name.asSymbol;
out = CollStream.new;
references = Class.findAllReferences(name);
if (references.notNil, {
out << "References to '" << name << "' :\n";
references.do({ arg ref; out << " " << ref.asString << "\n"; });
if(openInTextMate) {
fname = "/tmp/" ++ Date.seed ++ ".sc";
File.use(fname, "w") { |f|
f << out.collection.asString;
("mate" + fname).unixCmd(postOutput: false);
};
} {
out.collection.newTextWindow(name.asString);
};
},{
Post << "\nNo references to '" << name << "'.\n";
});
}
}