File: LessKeys.sc

package info (click to toggle)
sc3-batlib 0~20250203.8185a39-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 132 kB
  • sloc: lisp: 243; sh: 8; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,502 bytes parent folder | download
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
//completes brackets and quotes in a semi-intelligent way for typing with less effort.
//Batuhan Bozkurt 2009

LessKeys
{
	classvar keyActionFunc, acters, ucTriggers;
	
	*initClass
	{
		//will try to match bracks if one of these is the next char conditionally
		acters = [" ", ")", "}", "\t", "\r", "\n", "]", ";"];
		//these keys will invoke
		ucTriggers = [40, 123, 34, 91, 39, 124];
		
		keyActionFunc = 
		{
			arg doc, char, mod, unicode, keycode;
			var initPos, nextChar;
			
			if(ucTriggers.detect({|i| i == unicode }).notNil,
			{ 
				initPos = doc.selectionStart;
				doc.selectRange(initPos, 1);
				nextChar = doc.selectedString;
				if((doc.selectionSize == 0) or: {acters.detect({|i| nextChar == i }).notNil},
				{
					doc.selectRange(initPos, 0);
					unicode.switch
					(
						40, { doc.selectedString = ")" },
						123, { doc.selectedString = "}" },
						34, { doc.selectedString = "\"" },
						//using ] below confuses lexer (if in double quotes), a sclang bug...
						91, { doc.selectedString = 93.asAscii.asString },
						39, { doc.selectedString = "'" },
						124, { doc.selectedString = "|"};
					);
					doc.selectRange(initPos, 0);
				}, { doc.selectRange(initPos, 0); });
			}) 
		};
	}
	
	*enable
	{
		Document.globalKeyDownAction = Document.globalKeyDownAction.addFunc(keyActionFunc);
		"LessKeys enabled...".postln;
	}
	
	*disable
	{
		Document.globalKeyDownAction = Document.globalKeyDownAction.removeFunc(keyActionFunc);
		"LessKeys disabled...".postln;
	}
}