String as Autotype command results in only one Paste command - fixed #338

Signed-off-by: michael starke <michael.starke@hicknhack-software.com>
This commit is contained in:
michael starke
2015-08-07 17:24:04 +02:00
parent bc458506e5
commit 70ceb268ad
4 changed files with 39 additions and 22 deletions

View File

@@ -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,32 +136,40 @@ 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);
/* 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

View File

@@ -33,7 +33,7 @@
/**
* @return YES if valid, NO otherwise
*/
@property (nonatomic, readonly, assign) BOOL isValid;
@property (nonatomic, readonly, assign) BOOL valid;
/**
* Designated initializer

View File

@@ -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

View File

@@ -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];
}
}