forked from supercollider-quarks/ddwChucklib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChuckBrowserKeyController.sc
More file actions
444 lines (418 loc) · 13.2 KB
/
Copy pathChuckBrowserKeyController.sc
File metadata and controls
444 lines (418 loc) · 13.2 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// problem: routine does not pick up environments that are pushed
ChuckBrowserKeyController {
classvar <identifierCodes,
<states,
<wildcards;
var <>browser,
<string, routine,
<lastMTIndex,
<registers;
*initClass {
identifierCodes = (65..90) ++ (97..122) ++ (48..57) ++ $_.ascii ++ 127;
states = Dictionary[
// matchItem is done on unicode value from keystroke
// arrow keys, pass to default action of the view who has focus now
(65361..65364) -> { |instance, view, char, modifiers, unicode, keycode, key|
view.defaultKeyDownAction(char, modifiers, unicode, keycode, key);
nil // means reset
},
// A-Z, browse for a class
(65..90) -> { |instance, view, char, modifiers, unicode, keycode|
var className, nextKeySpec;
instance.browser.classMenu.focus(true);
#className, nextKeySpec = instance.parseIdentifier(char, { |string|
instance.browser.classMatch(string)
});
nextKeySpec
},
// :, browse subtypes
58 -> { |instance, view, char, modifiers, unicode, keycode|
var subType, nextKeySpec;
instance.browser.subTypeMenu.focus(true);
#subType, nextKeySpec = instance.parseIdentifier(nil, { |string|
instance.browser.subtypeMatch(string)
});
nextKeySpec
},
// ., browse instance names
46 -> { |instance, view, char, modifiers, unicode, keycode|
var inst, nextKeySpec;
instance.browser.instanceListView.focus(true);
#inst, nextKeySpec = instance.parseIdentifier(nil, { |string|
instance.browser.instanceMatch(string)
});
nextKeySpec
},
// *, restrict instance list to the subtype of the current object
42 -> { |instance|
var obj;
(obj = instance.browser.currentObject).subType.notNil.if({
instance.browser.setSubtypeForClass(obj.class, obj.subType);
instance.browser.changeClass;
});
nil
},
// ^, chuck into default MT (^^ or ^60^)
94 -> { |instance, view, char, modifiers, unicode, keycode|
var num, nextKeySpec;
#num, nextKeySpec = instance.parseNumber;
(nextKeySpec[3] == 94).if({ // ^ is command terminator
instance.browser.currentObject.chuck(MT.default, num);
});
nil
},
// #, insert ## object, #n name, #p proto into current document
// #, #N insert the same with ', ' after
35 -> { |instance, view, char, modifiers, unicode, keycode|
var nextKeySpec = instance.getKeySpec;
switch(nextKeySpec[3])
{ 35 } {
Document.current.selectedString_(
instance.browser.currentObject.asCompileString
)
}
{ 110 } {
Document.current.selectedString_(
instance.browser.currentObject.collIndex.asCompileString
)
}
{ 112 } {
instance.browser.currentObject.tryPerform(\proto);
}
{ 44 } {
Document.current.selectedString_(
instance.browser.currentObject.asCompileString ++ ", "
)
}
{ 78 } {
Document.current.selectedString_(
instance.browser.currentObject.collIndex.asCompileString ++ ", "
)
};
nil
},
// <, chuck into MCG -- << for first available, <3< for specific index
60 -> { |instance, view, char, modifiers, unicode, keycode|
var num, nextKeySpec;
#num, nextKeySpec = instance.parseNumber;
(nextKeySpec[3] == 60).if({
num.isNil.if({
num = MCG.collection.detectIndex({ |mcg|
mcg.value.mixer.isNil
});
});
instance.browser.currentObject => MCG(num);
});
nil
},
// >, chuck into VP or VC
// >> into first available VP
// >3> into specific VP
// >3. into voicer held in VP
// >name> into VC(\name)
62 -> { |instance, view, char, modifiers, unicode, keycode|
var target, nextKeySpec;
instance.browser.keyCommandView.focus;
#target, nextKeySpec = instance.parseIdentifier;
(target.size == 0).if({ target = nil }, {
target[0].inclusivelyBetween($0, $9).if({
target = target.asInteger
}/*, { target = target.asSymbol }*/);
});
target.isNil.if({
target = VP.collection.detectIndex({ |vp| vp.value.active.not });
});
target.isNumber.if({
target = VP(target);
}, {
// should modularize this
target = VC.collection.detect({ |vc|
vc.collIndex.asString[0..target.size-1] == target
});
});
target.notNil.if({
switch(nextKeySpec[3])
{ 62 } {
"% => %".format(instance.browser.currentObject.asCompileString,
target.asCompileString).debug("Executing");
instance.browser.currentObject => target;
}
{ 46 } {
"% => %".format(instance.browser.currentObject.asCompileString,
target.asVC.asCompileString).debug("Executing");
instance.browser.currentObject => target.asVC;
}
});
nil
},
// @, select last bp chucked into MT
64 -> { |instance|
var bp = MT.default.lastBP;
bp.notNil.if({ instance.browser.currentObject_(bp) },
{ "MT's lastBP is empty. Ignoring @ command.".warn; });
nil
},
// !, play/stop toggle
// !! -- if current is BP, do play or stop
// !name! -- play or stop this BP
33 -> { |instance|
var string, nextKeySpec, obj, method;
// maybe change this to display BP?
instance.browser.changeClass(BP);
#string, nextKeySpec = instance.parseIdentifier(nil, { |string|
instance.browser.instanceMatch(string)
});
(nextKeySpec[3] == 33).if({
obj = instance.browser.currentObject;
method = obj.isPlaying.if({ \stop }, { \play });
"%.%".format(obj.asCompileString, method).debug("Executing");
obj.isKindOf(BP).if({
obj.perform(method);
});
});
nil
},
// /, enter an arbitrary call to apply to current object
// e.g. F.b/.makev or F.b/.v.insp
47 -> { |instance, view, char, modifiers, unicode, keycode|
var string, nextKeySpec;
#string, nextKeySpec = instance.getCmdString;
(nextKeySpec[3] == 13).if({
((instance.browser.currentObject.asString ++ string)
.debug("Executing")
.interpret).postln
});
nil
},
// =, enter a chuck e.g. F.b=>VC(\newname)
61 -> { |instance, view, char, modifiers, unicode, keycode|
var string, nextKeySpec;
#string, nextKeySpec = instance.getCmdString("=");
(nextKeySpec[3] == 13).if({
((instance.browser.currentObject.asString ++ string)
.debug("Executing")
.interpret).postln
});
nil
},
// ctrl-`, move current doc to front
30 -> { |instance, view, char, modifiers, unicode, keycode|
(modifiers bitAnd: 0x40000 > 0).if({
Document.current.front;
});
nil
},
// $, get or set a register
36 -> { |instance, view, char, modifiers, unicode, keycode|
// next key
#view, char, modifiers, unicode, keycode = instance.getKeySpec;
(unicode == 36).if({ // $$a, set register a to current object
#view, char, modifiers, unicode, keycode = instance.getKeySpec;
(char.isPrint and: { char != $$ }).if({
instance.registers[char] = instance.browser.currentObject;
"Set register $% = %\n".postf(char,
instance.browser.currentObject.asCompileString);
});
}, { // $a, jump to object held in register
instance.registers[char].notNil.if({
instance.browser.currentObject = instance.registers[char];
});
});
nil
}
];
wildcards = Dictionary[
// current object for this class
"(*)" -> { |string, identifier, pos, instance|
"%(%)%".format(string[0..pos-1],
instance.browser.instanceForClass(identifier.asSymbol.asClass)
.asCompileString,
string[pos+3..]);
},
// last touched BP
"@" -> { |string, identifier, pos, instance|
"%BP(%)%".format(string[0..pos-1],
instance.browser.instanceForClass(BP).asCompileString,
string[pos+1..]);
},
// get a register
"\$" -> { |string, identifier, pos, instance|
var obj = instance.registers[string[pos+1]];
(string[pos+1] == $$).if({
"%$%".format(string[0..pos-1], string[pos+2..]);
}, {
obj.notNil.if({
"% % %".format(string[0..pos-1], obj.asCompileString,
string[pos+2..])
}, {
string
});
});
}
];
}
*new { |browser|
^super.newCopyArgs(browser).init
}
init {
var fn = { |view, char, modifiers, unicode, keycode, key|
if(key.inclusivelyBetween(16777234, 16777237).not) {
this.doKey(view, char, modifiers, unicode, keycode, key);
true // required for QT
} {
nil // arrows must bubble up to focused view
};
};
browser.classMenu.keyDownAction = fn;
browser.subTypeMenu.keyDownAction = fn;
browser.instanceListView.keyDownAction = fn;
browser.keyCommandView.keyDownAction = fn;
registers = IdentityDictionary.new;
this.resetState(true);
}
focus { browser.focus }
doKey { |view, char, modifiers, unicode, keycode, key|
routine ?? { this.resetState };
if(char.tryPerform(\isPrint) ? false) { string = string ++ char };
if(#[8, 127].includes(unicode)) {
string = string.left(string.size-1);
};
this.updateString;
routine.next([view, char, modifiers, unicode, keycode, key, currentEnvironment]);
}
resetState { |newRoutine = false|
string = "";
this.updateString;
(newRoutine or: { routine.isNil }).if({
routine = Routine({ |keyspec|
var view, char, modifiers, unicode, keycode, key, newkeyspec, action,
envir;
loop {
#view, char, modifiers, unicode, keycode, key, envir = keyspec;
(action = states.matchAt(if(unicode == 0) { keycode } { unicode })).notNil.if({
// if an error occurs, it might crash the routine
// and kill the interface
try {
envir.use({
newkeyspec = action.value(this, *keyspec);
});
} { |error|
error.reportError;
"Unknown error. Resetting key controller state.".warn;
this.resetState(true);
};
newkeyspec.notNil.if({
#view, char, modifiers, unicode, keycode, key, envir = newkeyspec
});
}, { newkeyspec = nil });
// if the action didn't return a keyspec,
// we have to wait for another key
newkeyspec.isNil.if({
this.resetState;
keyspec = this.getKeySpec; // running
}, {
keyspec = newkeyspec;
});
}
});
});
}
parseIdentifier { |firstChar, action|
var string;
var view, char, modifiers, unicode, keycode, key, envir;
string = firstChar.notNil.if({ firstChar.asString }, { "" });
action.value(string);
{ #view, char, modifiers, unicode, keycode, key, envir = this.getKeySpec;
identifierCodes.matchItem(unicode)
}.while({
(#[127, 8].includes(unicode)).if({
string = string.left(string.size-1);
this.updateString;
action.value(string);
}, {
string = string ++ char;
(action.value(string).isNil and: { action.notNil }).if({
// no match, drop the last char
string = string.left(string.size-1);
});
});
});
^[string, [view, char, modifiers, unicode, keycode, key, envir]]
}
parseNumber { |firstChar, action|
var string;
var view, char, modifiers, unicode, keycode, key, envir;
browser.keyCommandView.focus;
string = firstChar.notNil.if({ firstChar.asString }, { "" });
action.value(string);
{ #view, char, modifiers, unicode, keycode, key, envir = this.getKeySpec;
unicode.inclusivelyBetween(48, 57) or: { unicode == 127 }
}.while({
(#[127, 8].includes(unicode)).if({
string = string.left(string.size-1);
this.updateString;
action.value(string);
}, {
string = string ++ char;
(action.value(string).isNil and: { action.notNil }).if({
// no match, drop the last char
string = string.left(string.size-1);
});
});
});
^[(string.size > 0).if({ string.asInteger }),
[view, char, modifiers, unicode, keycode, key, envir]]
}
getCmdString { |firstChar| // just collecting chars, no action
var string;
var view, char, modifiers, unicode, keycode, key, envir;
browser.keyCommandView.focus;
string = firstChar.notNil.if({ firstChar.asString }, { "" });
{ #view, char, modifiers, unicode, keycode, key, envir = this.getKeySpec;
unicode != 13 and: { unicode != 27 } // esc to cancel
}.while({
(#[127, 8].includes(unicode)).if({
string = string.left(string.size-1);
this.updateString;
}, {
string = string ++ char;
});
});
^[this.processWildcards(string), [view, char, modifiers, unicode, keycode, key, envir]]
}
processWildcards { |string|
var index, idpos;
string = string.copy;
wildcards.keysValuesDo({ |wild, func|
index = -1;
{ (index = string.find(wild, false, index+1)).notNil }.while({
idpos = index - 1;
{ idpos >= 0 and: { identifierCodes.matchItem(string[idpos].ascii) } }.while({
idpos = idpos - 1;
});
// needed because idpos may be the punctuation before the real identifier
identifierCodes.matchItem(string[idpos].ascii).not.if({
idpos = idpos + 1;
});
// |string, identifier, pos, instance|
string = func.value(string, string[idpos..index-1], index, this);
});
});
^string
}
updateString {
this.browser.keyCommandView.string = string;
}
// this is only needed because QT fires the keyDownAction when a modifier
// is pressed -- I need to ignore those events
// doesn't trap caps-lock, though...
getKeySpec {
var next;
while {
next = true.yield;
// ignore empty modifier keystrokes
(next[1].isNil or: { next[1].ascii == 0 }) and: { next[2] > 0 }
};
^next
}
}