diff --git a/MacPass/MPAutotypeCommand.m b/MacPass/MPAutotypeCommand.m index 4318e865..bbce56dc 100644 --- a/MacPass/MPAutotypeCommand.m +++ b/MacPass/MPAutotypeCommand.m @@ -101,15 +101,15 @@ static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F } + (NSArray *)commandsForContext:(MPAutotypeContext *)context { - if(![context isValid]) { + if(!context.valid) { return nil; } - NSUInteger reserverd = MAX(1,[context.normalizedCommand length] / 4); + NSUInteger reserverd = MAX(1,context.normalizedCommand.length / 4); NSMutableArray *commands = [[NSMutableArray alloc] initWithCapacity:reserverd]; NSMutableArray __block *commandRanges = [[NSMutableArray alloc] initWithCapacity:reserverd]; NSRegularExpression *commandRegExp = [[NSRegularExpression alloc] initWithPattern:@"\\{[^\\{\\}]+\\}" options:NSRegularExpressionCaseInsensitive error:0]; NSAssert(commandRegExp, @"RegExp is constant. Has to work all the time"); - [commandRegExp enumerateMatchesInString:context.evaluatedCommand options:0 range:NSMakeRange(0, [context.evaluatedCommand length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { + [commandRegExp enumerateMatchesInString:context.evaluatedCommand options:0 range:NSMakeRange(0, context.evaluatedCommand.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { @autoreleasepool { [commandRanges addObject:[NSValue valueWithRange:result.range]]; } @@ -136,30 +136,38 @@ static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F [self appendAppropriatePasteCommandForEntry:context.entry withContent:pasteValue toCommands:commands]; } } - /* Test for modifier Key */ + /* Test for modifer Key */ NSString *commandString = [context.evaluatedCommand substringWithRange:commandRange]; - /* append commands for non-modifier keys */ + /* 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 modifiers; + collectedModifers = 0; // Reset the modifers; } lastLocation = commandRange.location + commandRange.length; } - /* Collect any part that isn't a command or if only paste is used */ - if(lastLocation < [context.evaluatedCommand length]) { - /* We might have some dangling modifiers */ - NSRange lastRange = NSMakeRange(lastLocation, [context.evaluatedCommand length] - lastLocation); - if(lastRange.length > 0) { + /* 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(0 != collectedModifers) { NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, 1)]; MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:collectedModifers character:modifiedKey]; if(press) { [commands addObject:press]; } - if(lastRange.length > 1) { - NSRange pasteRange = NSMakeRange(lastRange.location + 1, lastRange.length - 1); - NSString *pasteValue = [context.evaluatedCommand substringWithRange:pasteRange]; - [self appendAppropriatePasteCommandForEntry:context.entry withContent:pasteValue toCommands:commands]; - } + /* Update our states */ + collectedModifers = 0; + lastLocation++; + lastRange = NSMakeRange(lastLocation, context.evaluatedCommand.length - lastLocation); + } + /* No modifiers, just paste the rest */ + if(lastRange.length > 0) { + NSRange pasteRange = NSMakeRange(lastRange.location, lastRange.length); + NSString *pasteValue = [context.evaluatedCommand substringWithRange:pasteRange]; + [self appendAppropriatePasteCommandForEntry:context.entry withContent:pasteValue toCommands:commands]; } } return commands; @@ -174,7 +182,7 @@ static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F } + (void)appendPasteCommandForContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands { - /* Update an already inserted paste command with the new contents */ + /* Update an already inserted paste command with the new conents */ if([[commands lastObject] isKindOfClass:[MPAutotypePaste class]]) { [[commands lastObject] appendString:pasteContent]; } @@ -303,7 +311,7 @@ static CGKeyCode kMPFunctionKeyCodes[] = { kVK_F1, kVK_F2, kVK_F3, kVK_F4, kVK_F if([numberScanner scanInteger:&value]) { if([kKPKAutotypeDelay isEqualToString:uppercaseCommand]) { if(MAX(0, value) <= 0) { - return; // Value too low, just skip + return; // Value too low, just skipp } [commands addObject:[[MPAutotypeDelay alloc] initWithDelay:value]]; return; // Done diff --git a/MacPass/MPAutotypeContext.h b/MacPass/MPAutotypeContext.h index 282ecb36..8c989a93 100644 --- a/MacPass/MPAutotypeContext.h +++ b/MacPass/MPAutotypeContext.h @@ -33,7 +33,7 @@ /** * @return YES if valid, NO otherwise */ -@property (nonatomic, readonly, assign) BOOL isValid; +@property (nonatomic, readonly, assign) BOOL valid; /** * Designated initializer diff --git a/MacPass/MPAutotypeContext.m b/MacPass/MPAutotypeContext.m index 6e993ce0..1baf7352 100644 --- a/MacPass/MPAutotypeContext.m +++ b/MacPass/MPAutotypeContext.m @@ -47,7 +47,7 @@ } -- (BOOL)isValid { +- (BOOL)valid { return (self.normalizedCommand != nil); } @@ -58,4 +58,13 @@ return _evaluatedCommand; } +- (NSString *)description { + return [NSString stringWithFormat:@"command:%@\nnormalized:%@\nevaluated:%@\nentry.title:%@\nentry.uuid:%@\n", + self.command, + self.normalizedCommand, + self.evaluatedCommand, + self.entry.title, + self.entry.uuid.UUIDString]; +} + @end diff --git a/MacPass/MPDocument+Autotype.m b/MacPass/MPDocument+Autotype.m index babb032d..1ab9f386 100644 --- a/MacPass/MPDocument+Autotype.m +++ b/MacPass/MPDocument+Autotype.m @@ -78,14 +78,14 @@ KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle]; context = [[MPAutotypeContext alloc] initWithWindowAssociation:association]; } - if(context.isValid) { + if(context.valid) { [contexts addObject:context]; } } /* Fall back to preferred Entry if no match was found */ if(contexts.count == 0 && usePreferredEntry) { context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultKeystrokeSequence]; - if(context.isValid) { + if(context.valid) { [contexts addObject:context]; } }