forked from torifat/iAvro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AvroKeyboardController.m
253 lines (217 loc) · 10.1 KB
/
AvroKeyboardController.m
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
//
// AvroKeyboard
//
// Created by Rifat Nabi on 6/21/12.
// Copyright (c) 2012 OmicronLab. All rights reserved.
//
#import "AvroKeyboardController.h"
#import "Suggestion.h"
#import "Candidates.h"
#import "CacheManager.h"
#import "RegexKitLite.h"
#import "AvroParser.h"
#import "AutoCorrect.h"
@implementation AvroKeyboardController
@synthesize prefix = _prefix, term = _term, suffix = _suffix;
- (id)initWithServer:(IMKServer*)server delegate:(id)delegate client:(id)inputClient {
self = [super initWithServer:server delegate:delegate client:inputClient];
if (self) {
_currentClient = inputClient;
_composedBuffer = [[NSMutableString alloc] initWithString:@""];
_currentCandidates = [[NSMutableArray alloc] initWithCapacity:0];
_prevSelected = -1;
}
return self;
}
- (void)dealloc {
[_prefix release];
[_term release];
[_suffix release];
[_currentCandidates release];
[_composedBuffer release];
[super dealloc];
}
- (void)findCurrentCandidates {
[_currentCandidates removeAllObjects];
if (_composedBuffer && [_composedBuffer length] > 0) {
NSString* regex = @"(^(?::`|\\.`|[-\\]\\\\~!@#&*()_=+\\[{}'\";<>/?|.,])*?(?=(?:,{2,}))|^(?::`|\\.`|[-\\]\\\\~!@#&*()_=+\\[{}'\";<>/?|.,])*)(.*?(?:,,)*)((?::`|\\.`|[-\\]\\\\~!@#&*()_=+\\[{}'\";<>/?|.,])*$)";
NSArray* items = [_composedBuffer captureComponentsMatchedByRegex:regex];
if (items && [items count] > 0) {
// Split Prefix, Term & Suffix
[self setPrefix:[[AvroParser sharedInstance] parse:[items objectAtIndex:1]]];
[self setTerm:[items objectAtIndex:2]];
[self setSuffix:[[AvroParser sharedInstance] parse:[items objectAtIndex:3]]];
_currentCandidates = [[[Suggestion sharedInstance] getList:[self term]] retain];
if (_currentCandidates && [_currentCandidates count] > 0) {
NSString* prevString = nil;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"]) {
_prevSelected = -1;
prevString = [[CacheManager sharedInstance] stringForKey:[self term]];
}
int i;
for (i = 0; i < [_currentCandidates count]; ++i) {
NSString* item = [_currentCandidates objectAtIndex:i];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"] &&
_prevSelected && [item isEqualToString:prevString] ) {
_prevSelected = i;
}
[_currentCandidates replaceObjectAtIndex:i withObject:
[NSString stringWithFormat:@"%@%@%@", [self prefix], item, [self suffix]]];
}
// Emoticons
if ([_composedBuffer isEqualToString:[self term]] == NO &&
[[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"]) {
NSString* smily = [[AutoCorrect sharedInstance] find:_composedBuffer];
if (smily) {
[_currentCandidates insertObject:smily atIndex:0];
}
}
}
else {
[_currentCandidates addObject:[self prefix]];
}
}
}
}
- (void)updateCandidatesPanel {
if (_currentCandidates && [_currentCandidates count] > 0) {
NSUserDefaults *defaultsDictionary = [NSUserDefaults standardUserDefaults];
// NSString *candidateFontName = [defaultsDictionary objectForKey:@"candidateFontName"];
// float candidateFontSize = [[defaultsDictionary objectForKey:@"candidateFontSize"] floatValue];
// NSFont *candidateFont = [NSFont fontWithName:candidateFontName size:candidateFontSize];
// [[Candidates sharedInstance] setAttributes:[NSDictionary dictionaryWithObject:candidateFont forKey:NSFontAttributeName]];
[[Candidates sharedInstance] setPanelType:[defaultsDictionary integerForKey:@"CandidatePanelType"]];
[[Candidates sharedInstance] updateCandidates];
[[Candidates sharedInstance] show:kIMKLocateCandidatesBelowHint];
if (_prevSelected > -1) {
[[Candidates sharedInstance] selectCandidate:_prevSelected];
}
}
else {
[[Candidates sharedInstance] hide];
}
}
- (NSArray*)candidates:(id)sender {
return _currentCandidates;
}
- (void)candidateSelectionChanged:(NSAttributedString*)candidateString {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"]) {
if ([self term] && [[self term] length] > 0) {
BOOL comp = [[candidateString string] isEqualToString:[_currentCandidates objectAtIndex:0]];
if ((comp && _prevSelected == -1) == NO) {
NSRange range = NSMakeRange([[self prefix] length],
[candidateString length] - ([[self prefix] length] + [[self suffix] length]));
[[CacheManager sharedInstance] setString:[[candidateString string] substringWithRange:range] forKey:[self term]];
// Reverse Suffix Caching
NSArray* tmpArray = [[CacheManager sharedInstance] baseForKey:[candidateString string]];
if (tmpArray && [tmpArray count] > 0) {
[[CacheManager sharedInstance] setString:[tmpArray objectAtIndex:1] forKey:[tmpArray objectAtIndex:0]];
}
}
}
}
}
- (void)candidateSelected:(NSAttributedString*)candidateString {
[_currentClient insertText:candidateString replacementRange:NSMakeRange(NSNotFound, 0)];
[self clearCompositionBuffer];
[_currentCandidates removeAllObjects];
[self updateCandidatesPanel];
}
- (void)commitComposition:(id)sender {
[sender insertText:_composedBuffer replacementRange:NSMakeRange(NSNotFound, 0)];
[self clearCompositionBuffer];
[_currentCandidates removeAllObjects];
[self updateCandidatesPanel];
}
- (id)composedString:(id)sender {
return [[[NSAttributedString alloc] initWithString:_composedBuffer] autorelease];
}
- (void)clearCompositionBuffer {
[_composedBuffer deleteCharactersInRange:NSMakeRange(0, [_composedBuffer length])];
}
/*
Implement one of the three ways to receive input from the client.
Here are the three approaches:
1. Support keybinding.
In this approach the system takes each keydown and trys to map the keydown to an action method that the input method has implemented. If an action is found the system calls didCommandBySelector:client:. If no action method is found inputText:client: is called. An input method choosing this approach should implement
-(BOOL)inputText:(NSString*)string client:(id)sender;
-(BOOL)didCommandBySelector:(SEL)aSelector client:(id)sender;
2. Receive all key events without the keybinding, but do "unpack" the relevant text data.
Key events are broken down into the Unicodes, the key code that generated them, and modifier flags. This data is then sent to the input method's inputText:key:modifiers:client: method. For this approach implement:
-(BOOL)inputText:(NSString*)string key:(NSInteger)keyCode modifiers:(NSUInteger)flags client:(id)sender;
3. Receive events directly from the Text Services Manager as NSEvent objects. For this approach implement:
-(BOOL)handleEvent:(NSEvent*)event client:(id)sender;
*/
/*!
@method
@abstract Receive incoming text.
@discussion This method receives key board input from the client application. The method receives the key input as an NSString. The string will have been created from the keydown event by the InputMethodKit.
*/
- (BOOL)inputText:(NSString*)string client:(id)sender {
// Return YES to indicate the the key input was received and dealt with. Key processing will not continue in that case. In
// other words the system will not deliver a key down event to the application.
// Returning NO means the original key down will be passed on to the client.
if ([string isEqualToString:@" "]) {
if (_currentCandidates && [_currentCandidates count]) {
[self candidateSelected:[[Candidates sharedInstance] selectedCandidateString]];
}
return NO;
}
else {
[_composedBuffer appendString:string];
[self findCurrentCandidates];
[self updateComposition];
[self updateCandidatesPanel];
return YES;
}
}
- (void)deleteBackward:(id)sender {
// We're called only when [compositionBuffer length] > 0
[_composedBuffer deleteCharactersInRange:NSMakeRange([_composedBuffer length] - 1, 1)];
[self findCurrentCandidates];
[self updateComposition];
[self updateCandidatesPanel];
}
- (void)insertTab:(id)sender {
[self commitText:@"\t"];
}
- (void)insertNewline:(id)sender {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"CommitNewLineOnEnter"]) {
[self commitText:@"\n"];
}
else {
[self commitText:@""];
}
}
- (BOOL)didCommandBySelector:(SEL)aSelector client:(id)sender {
if ([self respondsToSelector:aSelector]) {
// The NSResponder methods like insertNewline: or deleteBackward: are
// methods that return void. didCommandBySelector method requires
// that you return YES if the command is handled and NO if you do not.
// This is necessary so that unhandled commands can be passed on to the
// client application. For that reason we need to test in the case where
// we might not handle the command.
if (_composedBuffer && [_composedBuffer length] > 0) {
if (aSelector == @selector(insertTab:)
|| aSelector == @selector(insertNewline:)
|| aSelector == @selector(deleteBackward:)) {
[self performSelector:aSelector withObject:sender];
return YES;
}
}
}
return NO;
}
- (void)commitText:(NSString*)string {
if (_currentCandidates) {
[self candidateSelected:[[Candidates sharedInstance] selectedCandidateString]];
[_currentClient insertText:string replacementRange:NSMakeRange(NSNotFound, 0)];
}
else {
NSBeep();
}
}
- (NSMenu*)menu {
return [[NSApp delegate] menu];
}
@end