removed duplicate code

This commit is contained in:
michael starke
2017-01-26 17:23:54 +01:00
parent fef22bce05
commit f0c433cac5

View File

@@ -24,10 +24,9 @@
static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F5, kVK_F6, kVK_F7, kVK_F8, kVK_F9, kVK_F10, kVK_F11, kVK_F12, kVK_F13, kVK_F14, kVK_F15, kVK_F16, kVK_F17, kVK_F18, kVK_F19 }; static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F5, kVK_F6, kVK_F7, kVK_F8, kVK_F9, kVK_F10, kVK_F11, kVK_F12, kVK_F13, kVK_F14, kVK_F15, kVK_F16, kVK_F17, kVK_F18, kVK_F19 };
typedef struct { typedef struct {
NSRange textRange;
NSRange commandRange;
NSUInteger location; NSUInteger location;
CGEventFlags modifiers; CGEventFlags modifiers;
NSRange commandRange;
} MPAutotypeParserContext; } MPAutotypeParserContext;
@interface NSNumber (AutotypeCommand) @interface NSNumber (AutotypeCommand)
@@ -83,11 +82,11 @@ typedef struct {
return keypressCommands; return keypressCommands;
} }
/* Commands that are actually just one symbol to be pasted */ /* Commands that are actually just one symbol to be pasted */
+ (NSDictionary *)pasteableCommands { + (NSDictionary *)characterCommands {
static NSDictionary *pasteableCommands; static NSDictionary *characterCommands;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
pasteableCommands = @{ characterCommands = @{
kKPKAutotypePlus: @"+", kKPKAutotypePlus: @"+",
kKPKAutotypeCaret: @"^", kKPKAutotypeCaret: @"^",
kKPKAutotypePercent: @"%", kKPKAutotypePercent: @"%",
@@ -100,7 +99,7 @@ typedef struct {
kKPKAutotypeCurlyBracketRight: @"}" kKPKAutotypeCurlyBracketRight: @"}"
}; };
}); });
return pasteableCommands; return characterCommands;
} }
/** /**
@@ -136,96 +135,52 @@ typedef struct {
[commandRanges addObject:[NSValue valueWithRange:result.range]]; [commandRanges addObject:[NSValue valueWithRange:result.range]];
} }
}]; }];
NSUInteger lastLocation = 0;
CGEventFlags collectedModifers = 0; /* add range at the end as terminator */
[commandRanges addObject:[NSValue valueWithRange:NSMakeRange(context.evaluatedCommand.length, 0)]];
MPAutotypeParserContext parserCtx;
parserCtx.location = 0;
parserCtx.modifiers = 0;
for(NSValue *rangeValue in commandRanges) { for(NSValue *rangeValue in commandRanges) {
NSRange commandRange = rangeValue.rangeValue; NSRange commandRange = rangeValue.rangeValue;
NSRange textRange = NSMakeRange(lastLocation, commandRange.location - lastLocation); parserCtx.commandRange = commandRange;
[self appendCommandsForContext:context parserContext:&parserCtx toCommands:commands];
/* All non-commands will get translated into key presses if possible, otherwiese into paste commands */
if(lastLocation < commandRange.location) {
/* If there were modifiers we need to use the next single stroke and update the modifier command */
if(collectedModifers) {
NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, 1)];
MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:collectedModifers character:modifiedKey];
if(press) {
[commands addObject:press];
}
collectedModifers = 0;
lastLocation++;
}
NSRange textRange = NSMakeRange(lastLocation, commandRange.location - lastLocation);
if(textRange.length > 0) {
NSString *textValue = [context.evaluatedCommand substringWithRange:textRange];
[self appendAppropriatePasteCommandForEntry:context.entry withContent:textValue toCommands:commands];
}
}
/* Test for modifer Key */
NSString *commandString = [context.evaluatedCommand substringWithRange:commandRange];
/* append commands for non-modifer keys */
if(![self updateModifierMask:&collectedModifers forCommand:commandString]) {
[self appendCommandForEntry:context.entry withString:commandString toCommands:commands activeModifer:collectedModifers];
collectedModifers = 0; // Reset the modifers;
}
lastLocation = commandRange.location + commandRange.length;
}
/* Collect last part that isn't a command */
if(lastLocation < context.evaluatedCommand.length) {
NSRange lastRange = NSMakeRange(lastLocation, context.evaluatedCommand.length - lastLocation);
if(lastRange.length <= 0) {
return commands;
}
/* We have modifiers */
if(collectedModifers) {
NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, 1)];
MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:collectedModifers character:modifiedKey];
if(press) {
[commands addObject:press];
}
/* Update our states */
collectedModifers = 0;
lastLocation++;
lastRange = NSMakeRange(lastLocation, context.evaluatedCommand.length - lastLocation);
}
/* No modifiers, just paste the rest */
if(lastRange.length > 0) {
NSString *pasteValue = [context.evaluatedCommand substringWithRange:lastRange];
[self appendAppropriatePasteCommandForEntry:context.entry withContent:pasteValue toCommands:commands];
}
} }
return commands; return commands;
} }
+ (void)appendCommandsForContext:(MPAutotypeContext *)context parserContext:(MPAutotypeParserContext *)parserContext toCommands:(NSMutableArray *)commands { + (void)appendCommandsForContext:(MPAutotypeContext *)context parserContext:(MPAutotypeParserContext *)parserCtx toCommands:(NSMutableArray *)commands {
if(parserContext == NULL) { if(parserCtx == NULL) {
return; // failed! return; // failed!
} }
if(parserContext->textRange.length > 0 && parserContext->textRange.location < parserContext->commandRange.location) { /* All non-commands will get translated into key presses if possible, otherwiese into paste commands */
if(parserCtx->location < parserCtx->commandRange.location) {
/* If there were modifiers we need to use the next single stroke and update the modifier command */ /* If there were modifiers we need to use the next single stroke and update the modifier command */
if(parserContext->modifiers) { if(parserCtx->modifiers) {
NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(parserContext->location, 1)]; NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(parserCtx->location, 1)];
MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:parserContext->modifiers character:modifiedKey]; MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:parserCtx->modifiers character:modifiedKey];
if(press) { if(press) {
[commands addObject:press]; [commands addObject:press];
} }
parserContext->modifiers = 0; parserCtx->modifiers = 0;
parserContext->location++; parserCtx->location++;
} }
NSRange textRange = NSMakeRange(parserContext->location, parserContext->commandRange.location - parserContext->location); NSRange textRange = NSMakeRange(parserCtx->location, parserCtx->commandRange.location - parserCtx->location);
if(textRange.length > 0) { if(textRange.length > 0) {
NSString *textValue = [context.evaluatedCommand substringWithRange:textRange]; NSString *textValue = [context.evaluatedCommand substringWithRange:textRange];
[self appendAppropriatePasteCommandForEntry:context.entry withContent:textValue toCommands:commands]; [self appendAppropriatePasteCommandForEntry:context.entry withContent:textValue toCommands:commands];
} }
} }
/* Test for modifer Key */ /* Test for modifer Key */
NSString *commandString = [context.evaluatedCommand substringWithRange:parserContext->commandRange]; NSString *commandString = [context.evaluatedCommand substringWithRange:parserCtx->commandRange];
/* append commands for non-modifer keys */ /* append commands for non-modifer keys */
if(![self updateModifierMask:&(parserContext->modifiers) forCommand:commandString]) { if(![self updateModifierMask:&(parserCtx->modifiers) forCommand:commandString]) {
[self appendCommandForEntry:context.entry withString:commandString toCommands:commands activeModifer:parserContext->modifiers]; [self appendCommandForEntry:context.entry withString:commandString toCommands:commands activeModifer:parserCtx->modifiers];
parserContext->modifiers = 0; // Reset the modifers; parserCtx->modifiers = 0; // Reset the modifers;
} }
parserContext->location = (parserContext->commandRange.location + parserContext->commandRange.length); parserCtx->location = parserCtx->commandRange.location + parserCtx->commandRange.length;
} }
+ (void)appendAppropriatePasteCommandForEntry:(KPKEntry *)entry withContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands + (void)appendAppropriatePasteCommandForEntry:(KPKEntry *)entry withContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands
@@ -313,8 +268,8 @@ typedef struct {
} }
+ (void)appendCommandForEntry:(KPKEntry *)entry withString:(NSString *)commandString toCommands:(NSMutableArray *)commands activeModifer:(CGEventFlags)flags { + (void)appendCommandForEntry:(KPKEntry *)entry withString:(NSString *)commandString toCommands:(NSMutableArray *)commands activeModifer:(CGEventFlags)flags {
if(nil == commandString) { if(!commandString || !commandString.length) {
return; // Nothing to parse return;
} }
/* Simple Special Press */ /* Simple Special Press */
NSString *uppercaseCommand = commandString.uppercaseString; NSString *uppercaseCommand = commandString.uppercaseString;
@@ -325,9 +280,9 @@ typedef struct {
return; // Done return; // Done
} }
/* {PLUS}, {TILDE}, {PERCENT}, {+}, etc */ /* {PLUS}, {TILDE}, {PERCENT}, {+}, etc */
NSString *pasteConent = [self pasteableCommands][uppercaseCommand]; NSString *character = [self characterCommands][uppercaseCommand];
if(pasteConent) { if(character) {
[self appendAppropriatePasteCommandForEntry:entry withContent:pasteConent toCommands:commands]; [self appendAppropriatePasteCommandForEntry:entry withContent:character toCommands:commands];
return; // Done return; // Done
} }