-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT2TKeyEquivalentConverter.m
More file actions
156 lines (136 loc) · 3.71 KB
/
T2TKeyEquivalentConverter.m
File metadata and controls
156 lines (136 loc) · 3.71 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
//
// T2TKeyEquivalentConverter.m
// tm2truffle
//
// Created by Alex Gordon on 20/11/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "T2TKeyEquivalentConverter.h"
#define map_equiv(ch, name) else if (c == ch) [equiv addObject:name];
/*
Examples:
ctrl-g => [ctrl]G
ctrl-shift-g => [ctrl][shift]G
ctrl-7 => [ctrl]7
ctrl-shift-7 => [ctrl][shift]&
*/
NSArray* T2TConvertKeyEquivalent(NSString* textmate)
{
NSMutableArray* equiv = [[NSMutableArray alloc] init];
unichar* str = malloc(sizeof(unichar) * [textmate length]);
[textmate getCharacters:str range:NSMakeRange(0, [textmate length])];
//Try to parse one or more modifiers
BOOL hasControl = NO;
BOOL hasAlt = NO;
BOOL hasShift = NO;
BOOL hasCommand = NO;
NSUInteger i = 0;
NSInteger length = [textmate length];
for (i = 0; i < length - 1; i++)
{
unichar c = str[i];
if (c == '@')
hasCommand = YES;
else if (c == '$')
hasShift = YES;
else if (c == '^')
hasControl = YES;
else if (c == '~')
hasAlt = YES;
/*
map_equiv('@', @"[cmd]")
map_equiv('$', @"[shift]")
map_equiv('^', @"[ctrl]")
map_equiv('~', @"[alt]")
*/
else
break;
}
//Try to parse special keys
NSMutableString *finalString = [[NSMutableString alloc] initWithCapacity:1];
for (; i < [textmate length]; i++)
{
unichar c = str[i];
if (NO) { }
map_equiv(0x0003, @"[cancel]")
map_equiv(0x0009, @"[tab]")
map_equiv(0x000A, @"[return]")
map_equiv(0x000D, @"[enter]")
map_equiv(0x001B, @"[esc]")
map_equiv(0x0020, @"[space]")
map_equiv(0x007F, @"[backspace]")
map_equiv(0xF700, @"[up]")
map_equiv(0xF701, @"[down]")
map_equiv(0xF702, @"[left]")
map_equiv(0xF703, @"[right]")
map_equiv(0xF704, @"[f1]")
map_equiv(0xF705, @"[f2]")
map_equiv(0xF706, @"[f3]")
map_equiv(0xF707, @"[f4]")
map_equiv(0xF708, @"[f5]")
map_equiv(0xF709, @"[f6]")
map_equiv(0xF70A, @"[f7]")
map_equiv(0xF70B, @"[f8]")
map_equiv(0xF70C, @"[f9]")
map_equiv(0xF70D, @"[f10]")
map_equiv(0xF70E, @"[f11]")
map_equiv(0xF70F, @"[f12]")
map_equiv(0xF710, @"[f13]")
map_equiv(0xF711, @"[f14]")
map_equiv(0xF712, @"[f15]")
map_equiv(0xF713, @"[f16]")
map_equiv(0xF714, @"[f17]")
map_equiv(0xF715, @"[f18]")
map_equiv(0xF716, @"[f19]")
map_equiv(0xF717, @"[f20]")
map_equiv(0xF718, @"[f21]")
map_equiv(0xF719, @"[f22]")
map_equiv(0xF71A, @"[f23]")
map_equiv(0xF71B, @"[f24]")
map_equiv(0xF727, @"[insert]")
map_equiv(0xF728, @"[del]")
map_equiv(0xF729, @"[home]")
map_equiv(0xF72B, @"[end]")
map_equiv(0xF72C, @"[pageup]")
map_equiv(0xF72D, @"[pagedown]")
map_equiv(0xF72E, @"[snapshot]")
map_equiv(0xF72F, @"[scroll]")
map_equiv(0xF730, @"[pause]")
map_equiv(0xF746, @"[help]")
map_equiv(0xF735, @"[menu]")
map_equiv(0xF739, @"[clear]")
map_equiv(0xF732, @"[cancel]")
else
{
if (c >= 'A' && c <= 'Z')
{
hasShift = YES;
}
else if (c >= 'a' && c <= 'z')
{
c += 'A' - 'a';
}
else if (c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '(' || c == ')' || c == '_' || c == '+' || c == '~' || c == '{' || c == '}' || c == ':' || c == '"' || c == '|' || c == '<' || c == '>' || c == '?')
{
hasShift = YES;
}
[finalString appendFormat:@"%C", c];
}
}
//Order must be:
// [ctrl] [alt] [shift] [cmd]
//In reverse order, prepend to the array
if (hasCommand)
[equiv insertObject:@"[cmd]" atIndex:0];
if (hasShift)
[equiv insertObject:@"[shift]" atIndex:0];
if (hasAlt)
[equiv insertObject:@"[alt]" atIndex:0];
if (hasControl)
[equiv insertObject:@"[ctrl]" atIndex:0];
if ([finalString length])
[equiv addObject:finalString];
free(str);
//[equiv removeDuplicates];
return equiv;
}