Tab conversion into 4 spaces

This commit is contained in:
Mengjuei 2012-02-26 20:53:51 -08:00
parent 8e5c540597
commit 0d8e1ae20f
8 changed files with 343 additions and 343 deletions

View File

@ -132,8 +132,8 @@ public:
// text input from an IME
self = [super initWithServer:server delegate:delegate client:client];
if (self) {
// create the reading buffer
if (self) {
// create the reading buffer
_bpmfReadingBuffer = new BopomofoReadingBuffer(BopomofoKeyboardLayout::StandardLayout());
// create the lattice builder
@ -149,17 +149,17 @@ public:
if (![[NSUserDefaults standardUserDefaults] objectForKey:kDisableUserCandidateSelectionLearning]) {
[[NSUserDefaults standardUserDefaults] setObject:(id)kCFBooleanTrue forKey:kDisableUserCandidateSelectionLearning];
}
}
}
return self;
return self;
}
- (NSMenu *)menu
{
// a menu instance (autoreleased) is requested every time the user click on the input menu
NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"Input Method Menu"] autorelease];
NSMenuItem *preferenceMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""] autorelease];
[menu addItem:preferenceMenuItem];
NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"Input Method Menu"] autorelease];
NSMenuItem *preferenceMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""] autorelease];
[menu addItem:preferenceMenuItem];
// If Option key is pressed, show the learning-related menu
@ -190,8 +190,8 @@ public:
}
#endif //DEBUG
NSMenuItem *aboutMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"About McBopomofo…", @"") action:@selector(showAbout:) keyEquivalent:@""] autorelease];
[menu addItem:aboutMenuItem];
NSMenuItem *aboutMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"About McBopomofo…", @"") action:@selector(showAbout:) keyEquivalent:@""] autorelease];
[menu addItem:aboutMenuItem];
return menu;
}
@ -229,9 +229,9 @@ public:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout());
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kKeyboardLayoutPreferenceKey];
}
if (keyboardLayout < 4) {
[client overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
}
if (keyboardLayout < 4) {
[client overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
}
// set the size
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
@ -250,8 +250,8 @@ public:
[[NSUserDefaults standardUserDefaults] setInteger:textSize forKey:kCandidateListTextSizeKey];
}
if (![[NSUserDefaults standardUserDefaults] objectForKey:kChooseCandidateUsingSpaceKey]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kChooseCandidateUsingSpaceKey];
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kChooseCandidateUsingSpaceKey];
}
[(AppDelegate *)[NSApp delegate] checkForUpdate];
}
@ -344,11 +344,11 @@ public:
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName,
[NSNumber numberWithInt:0], NSMarkedClauseSegmentAttributeName, nil];
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:composedText attributes:attrDict] autorelease];
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:composedText attributes:attrDict] autorelease];
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put ths composing buffer
[client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put ths composing buffer
[client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
}
- (void)walk
@ -421,24 +421,24 @@ public:
- (BOOL)inputText:(NSString*)inputText key:(NSInteger)keyCode modifiers:(NSUInteger)flags client:(id)client
{
NSRect textFrame = NSZeroRect;
NSDictionary *attributes = nil;
BOOL useVerticalMode = NO;
@try {
attributes = [client attributesForCharacterIndex:0 lineHeightRectangle:&textFrame];
useVerticalMode = [attributes objectForKey:@"IMKTextOrientation"] && [[attributes objectForKey:@"IMKTextOrientation"] integerValue] == 0;
}
@catch (NSException *e) {
// An exception may raise while using Twitter.app's search filed.
}
NSRect textFrame = NSZeroRect;
NSDictionary *attributes = nil;
BOOL useVerticalMode = NO;
@try {
attributes = [client attributesForCharacterIndex:0 lineHeightRectangle:&textFrame];
useVerticalMode = [attributes objectForKey:@"IMKTextOrientation"] && [[attributes objectForKey:@"IMKTextOrientation"] integerValue] == 0;
}
@catch (NSException *e) {
// An exception may raise while using Twitter.app's search filed.
}
NSInteger leftKey = useVerticalMode ? 125 : 124;
NSInteger rightKey = useVerticalMode ? 126 : 123;
NSInteger downKey = useVerticalMode ? 123 : 125;
NSInteger upKey = useVerticalMode ? 124 : 126;
NSInteger leftKey = useVerticalMode ? 125 : 124;
NSInteger rightKey = useVerticalMode ? 126 : 123;
NSInteger downKey = useVerticalMode ? 123 : 125;
NSInteger upKey = useVerticalMode ? 124 : 126;
// get the unicode character code
UniChar charCode = [inputText length] ? [inputText characterAtIndex:0] : 0;
UniChar charCode = [inputText length] ? [inputText characterAtIndex:0] : 0;
if ([[client bundleIdentifier] isEqualToString:@"com.apple.Terminal"] && [NSStringFromClass([client class]) isEqualToString:@"IPMDServerClientWrapper"])
{
@ -447,9 +447,9 @@ public:
}
// if the inputText is empty, it's a function key combination, we ignore it
if (![inputText length]) {
return NO;
}
if (![inputText length]) {
return NO;
}
// if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it
if (![_composingBuffer length] && _bpmfReadingBuffer->isEmpty() && ((flags & NSCommandKeyMask) || (flags & NSControlKeyMask) || (flags & NSAlternateKeyMask) || (flags & NSNumericPadKeyMask))) {
@ -460,29 +460,29 @@ public:
// caps lock processing : if caps is locked, temporarily disabled bopomofo.
if (charCode == 8 || charCode == 13 ||
if (charCode == 8 || charCode == 13 ||
keyCode == upKey || keyCode == downKey ||
keyCode == leftKey || keyCode == rightKey) {
// Do nothing if backspace is pressed
} else if (flags & NSAlphaShiftKeyMask){
} else if (flags & NSAlphaShiftKeyMask){
// Now process all possible combination, we hope.
if ([_composingBuffer length]) [self commitComposition:client];
if ([_composingBuffer length]) [self commitComposition:client];
// First commit everything in the buffer.
if (flags & NSShiftKeyMask) return NO;
if (flags & NSShiftKeyMask) return NO;
// when shift is pressed, don't do further processing, since it outputs
// capital letter anyway.
NSString *popedText = [inputText lowercaseString];
[client insertText:popedText replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
return YES;
}
if (flags & NSNumericPadKeyMask) {
if (keyCode != 123 && keyCode != 124 && keyCode != 125 && keyCode != 126 && charCode != 32 ) {
if ([_composingBuffer length]) [self commitComposition:client];
NSString *popedText = [inputText lowercaseString];
[client insertText:popedText replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
return YES;
}
}
NSString *popedText = [inputText lowercaseString];
[client insertText:popedText replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
return YES;
}
if (flags & NSNumericPadKeyMask) {
if (keyCode != 123 && keyCode != 124 && keyCode != 125 && keyCode != 126 && charCode != 32 ) {
if ([_composingBuffer length]) [self commitComposition:client];
NSString *popedText = [inputText lowercaseString];
[client insertText:popedText replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
return YES;
}
}
// see if it's valid BPMF reading
if (_bpmfReadingBuffer->isValidKey((char)charCode)) {
@ -521,7 +521,7 @@ public:
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning]) {
NSString *trigram = [self neighborTrigramString];
// Lookup from the user dict to see if the trigram fit or not
// Lookup from the user dict to see if the trigram fit or not
NSString *overrideCandidateString = [TLCandidateLearningDictionary objectForKey:trigram];
if (overrideCandidateString) {
[self candidateSelected:(NSAttributedString *)overrideCandidateString];
@ -538,24 +538,24 @@ public:
// keyCode 125 = Down, charCode 32 = Space
if (_bpmfReadingBuffer->isEmpty() && [_composingBuffer length] > 0 && (keyCode == downKey || charCode == 32)) {
if (charCode == 32) {
// if the spacebar is NOT set to be a selection key
if (![[NSUserDefaults standardUserDefaults] boolForKey:kChooseCandidateUsingSpaceKey]) {
if (_builder->cursorIndex() >= _builder->length()) {
[_composingBuffer appendString:@" "];
[self commitComposition:client];
_bpmfReadingBuffer->clear();
}
else if (LTLanguageModel.hasUnigramsForKey(" ")) {
_builder->insertReadingAtCursor(" ");
[self popOverflowComposingTextAndWalk:client];
[self updateClientComposingBuffer:client];
}
return YES;
if (charCode == 32) {
// if the spacebar is NOT set to be a selection key
if (![[NSUserDefaults standardUserDefaults] boolForKey:kChooseCandidateUsingSpaceKey]) {
if (_builder->cursorIndex() >= _builder->length()) {
[_composingBuffer appendString:@" "];
[self commitComposition:client];
_bpmfReadingBuffer->clear();
}
else if (LTLanguageModel.hasUnigramsForKey(" ")) {
_builder->insertReadingAtCursor(" ");
[self popOverflowComposingTextAndWalk:client];
[self updateClientComposingBuffer:client];
}
return YES;
}
}
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
}
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
return YES;
}
@ -575,8 +575,8 @@ public:
[self updateClientComposingBuffer:client];
return YES;
//可能的行為包括 1. 取消組字, 把字吃掉 2. 取消 candidate 選擇, 恢復原來的
//3. 取消組字,現出注音
//可能的行為包括 1. 取消組字, 把字吃掉 2. 取消 candidate 選擇, 恢復原來的
//3. 取消組字,現出注音
}
// The Right key, note we use keyCode here
@ -623,13 +623,13 @@ public:
return YES;
}
if (keyCode == upKey || keyCode == downKey) {
if (keyCode == upKey || keyCode == downKey) {
if (!_bpmfReadingBuffer->isEmpty()) {
[self beep];
}
[self updateClientComposingBuffer:client];
return YES;
}
}
// Backspace
if (charCode == 8) {
@ -664,45 +664,45 @@ public:
return YES;
}
if ((char)charCode == '`') {
if (LTLanguageModel.hasUnigramsForKey(string("_punctuation_list"))) {
if (_bpmfReadingBuffer->isEmpty()) {
_builder->insertReadingAtCursor(string("_punctuation_list"));
[self popOverflowComposingTextAndWalk:client];
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
else { // If there is still unfinished bpmf reading, ignore the punctuation
[self beep];
}
[self updateClientComposingBuffer:client];
return YES;
}
}
string layout = string("Standard_");;
NSInteger keyboardLayout = [[NSUserDefaults standardUserDefaults] integerForKey:kKeyboardLayoutPreferenceKey];
switch (keyboardLayout) {
case 0:
layout = string("Standard_");
break;
case 1:
layout = string("ETen_");
break;
case 2:
layout = string("ETen26_");
break;
case 3:
layout = string("Hsu_");
break;
case 4:
layout = string("HanyuPinyin_");
break;
default:
break;
if ((char)charCode == '`') {
if (LTLanguageModel.hasUnigramsForKey(string("_punctuation_list"))) {
if (_bpmfReadingBuffer->isEmpty()) {
_builder->insertReadingAtCursor(string("_punctuation_list"));
[self popOverflowComposingTextAndWalk:client];
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
else { // If there is still unfinished bpmf reading, ignore the punctuation
[self beep];
}
[self updateClientComposingBuffer:client];
return YES;
}
}
string customPunctuation = string("_punctuation_") + layout + string(1, (char)charCode);
if (LTLanguageModel.hasUnigramsForKey(customPunctuation)) {
string layout = string("Standard_");;
NSInteger keyboardLayout = [[NSUserDefaults standardUserDefaults] integerForKey:kKeyboardLayoutPreferenceKey];
switch (keyboardLayout) {
case 0:
layout = string("Standard_");
break;
case 1:
layout = string("ETen_");
break;
case 2:
layout = string("ETen26_");
break;
case 3:
layout = string("Hsu_");
break;
case 4:
layout = string("HanyuPinyin_");
break;
default:
break;
}
string customPunctuation = string("_punctuation_") + layout + string(1, (char)charCode);
if (LTLanguageModel.hasUnigramsForKey(customPunctuation)) {
if (_bpmfReadingBuffer->isEmpty()) {
_builder->insertReadingAtCursor(customPunctuation);
[self popOverflowComposingTextAndWalk:client];
@ -782,7 +782,7 @@ public:
[self saveUserCandidatesDictionary];
}
size_t cursorIndex = [self actualCandidateCursorIndex];
size_t cursorIndex = [self actualCandidateCursorIndex];
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
for (vector<NodeAnchor>::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
@ -831,7 +831,7 @@ public:
NSMutableArray *termArray = [NSMutableArray array];
size_t cursorIndex = [self actualCandidateCursorIndex];
size_t cursorIndex = [self actualCandidateCursorIndex];
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
const Node* prev = 0;
@ -899,38 +899,38 @@ public:
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client
{
// candidate
[LTSharedCandidates setDismissesAutomatically:YES];
// candidate
[LTSharedCandidates setDismissesAutomatically:YES];
// wrap NSNumber; we only allow number keys 1-9 as selection keys in this project
// wrap NSNumber; we only allow number keys 1-9 as selection keys in this project
#define LTUIntObj(x) ([NSNumber numberWithInteger:x])
[LTSharedCandidates setSelectionKeys:[NSArray arrayWithObjects:LTUIntObj(18), LTUIntObj(19), LTUIntObj(20), LTUIntObj(21), LTUIntObj(23), LTUIntObj(22), LTUIntObj(26), LTUIntObj(28), LTUIntObj(25), nil]];
[LTSharedCandidates setSelectionKeys:[NSArray arrayWithObjects:LTUIntObj(18), LTUIntObj(19), LTUIntObj(20), LTUIntObj(21), LTUIntObj(23), LTUIntObj(22), LTUIntObj(26), LTUIntObj(28), LTUIntObj(25), nil]];
#undef LTUIntObj
// set the candidate panel style
BOOL useHorizontalCandidateList = [[NSUserDefaults standardUserDefaults] boolForKey:kUseHorizontalCandidateListPreferenceKey];
// set the candidate panel style
BOOL useHorizontalCandidateList = [[NSUserDefaults standardUserDefaults] boolForKey:kUseHorizontalCandidateListPreferenceKey];
if (useVerticalMode) {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
else if (useHorizontalCandidateList) {
[LTSharedCandidates setPanelType:kIMKSingleRowSteppingCandidatePanel];
}
else {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
if (useVerticalMode) {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
else if (useHorizontalCandidateList) {
[LTSharedCandidates setPanelType:kIMKSingleRowSteppingCandidatePanel];
}
else {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
// set the attributes for the candidate panel (which uses NSAttributedString)
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize:textSize], NSFontAttributeName, nil];
[LTSharedCandidates setAttributes:attributes];
// set the attributes for the candidate panel (which uses NSAttributedString)
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize:textSize], NSFontAttributeName, nil];
[LTSharedCandidates setAttributes:attributes];
[LTSharedCandidates updateCandidates];
[LTSharedCandidates show:useVerticalMode ? kIMKLocateCandidatesLeftHint : kIMKLocateCandidatesBelowHint];
[LTSharedCandidates updateCandidates];
[LTSharedCandidates show:useVerticalMode ? kIMKLocateCandidatesLeftHint : kIMKLocateCandidatesBelowHint];
// update the composing text, set the client
[self updateClientComposingBuffer:client];
_currentCandidateClient = client;
// update the composing text, set the client
[self updateClientComposingBuffer:client];
_currentCandidateClient = client;
}
#pragma mark - Misc menu items
@ -994,7 +994,7 @@ void LTLoadLanguageModel()
}
}
ifs.close();
LTLanguageModel.add(" ", " ", 0.0);
LTLanguageModel.add(" ", " ", 0.0);
// initialize the singleton learning dictionary
// putting singleton in @synchronized is the standard way in Objective-C

View File

@ -30,55 +30,55 @@
@implementation OVInputSourceHelper
+ (NSArray *)allInstalledInputSources
{
CFArrayRef list = TISCreateInputSourceList(NULL, true);
return [NSMakeCollectable(list) autorelease];
CFArrayRef list = TISCreateInputSourceList(NULL, true);
return [NSMakeCollectable(list) autorelease];
}
+ (TISInputSourceRef)inputSourceForProperty:(CFStringRef)inPropertyKey stringValue:(NSString *)inValue
{
CFTypeID stringID = CFStringGetTypeID();
CFTypeID stringID = CFStringGetTypeID();
for (id source in [self allInstalledInputSources]) {
CFTypeRef property = TISGetInputSourceProperty((TISInputSourceRef)source, inPropertyKey);
if (!property || CFGetTypeID(property) != stringID) {
continue;
}
for (id source in [self allInstalledInputSources]) {
CFTypeRef property = TISGetInputSourceProperty((TISInputSourceRef)source, inPropertyKey);
if (!property || CFGetTypeID(property) != stringID) {
continue;
}
if (inValue && [inValue compare:(NSString *)property] == NSOrderedSame) {
return (TISInputSourceRef)source;
}
}
if (inValue && [inValue compare:(NSString *)property] == NSOrderedSame) {
return (TISInputSourceRef)source;
}
}
return NULL;
return NULL;
}
+ (TISInputSourceRef)inputSourceForInputSourceID:(NSString *)inID
{
return [self inputSourceForProperty:kTISPropertyInputSourceID stringValue:inID];
return [self inputSourceForProperty:kTISPropertyInputSourceID stringValue:inID];
}
+ (BOOL)inputSourceEnabled:(TISInputSourceRef)inInputSource
{
CFBooleanRef value = TISGetInputSourceProperty(inInputSource, kTISPropertyInputSourceIsEnabled);
return value ? (BOOL)CFBooleanGetValue(value) : NO;
CFBooleanRef value = TISGetInputSourceProperty(inInputSource, kTISPropertyInputSourceIsEnabled);
return value ? (BOOL)CFBooleanGetValue(value) : NO;
}
+ (BOOL)enableInputSource:(TISInputSourceRef)inInputSource
{
OSStatus status = TISEnableInputSource(inInputSource);
return status == noErr;
OSStatus status = TISEnableInputSource(inInputSource);
return status == noErr;
}
+ (BOOL)disableInputSource:(TISInputSourceRef)inInputSource
{
OSStatus status = TISDisableInputSource(inInputSource);
return status == noErr;
OSStatus status = TISDisableInputSource(inInputSource);
return status == noErr;
}
+ (BOOL)registerInputSource:(NSURL *)inBundleURL
{
OSStatus status = TISRegisterInputSource((CFURLRef)inBundleURL);
return status == noErr;
OSStatus status = TISRegisterInputSource((CFURLRef)inBundleURL);
return status == noErr;
}
@end

View File

@ -61,7 +61,7 @@ int main(int argc, char *argv[])
if (!inputSource) {
NSLog(@"Registering input source %@ at %@.", bundleID, [bundleURL absoluteString]);
// then register
BOOL status = [OVInputSourceHelper registerInputSource:bundleURL];
BOOL status = [OVInputSourceHelper registerInputSource:bundleURL];
if (!status) {
NSLog(@"Fatal error: Cannot register input source %@ at %@.", bundleID, [bundleURL absoluteString]);