-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Trigger.m
102 lines (86 loc) · 2.43 KB
/
Trigger.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
//
// Trigger.m
// iTerm
//
// Created by George Nachman on 9/23/11.
//
#import "Trigger.h"
#import "RegexKitLite.h"
#import "NSStringITerm.h"
NSString * const kTriggerRegexKey = @"regex";
NSString * const kTriggerActionKey = @"action";
NSString * const kTriggerParameterKey = @"parameter";
@implementation Trigger
@synthesize regex = regex_;
@synthesize action = action_;
@synthesize param = param_;
+ (Trigger *)triggerFromDict:(NSDictionary *)dict
{
NSString *className = [dict objectForKey:kTriggerActionKey];
Class class = NSClassFromString(className);
Trigger *trigger = [[class alloc] init];
trigger.regex = [dict objectForKey:kTriggerRegexKey];
trigger.param = [dict objectForKey:kTriggerParameterKey];
return trigger;
}
- (NSString *)action
{
return NSStringFromClass([self class]);
}
- (NSString *)title
{
assert(false);
}
- (NSString *)paramPlaceholder
{
assert(false);
}
- (BOOL)takesParameter
{
assert(false);
}
- (void)dealloc {
[regex_ release];
[action_ release];
[param_ release];
[super dealloc];
}
- (void)performActionWithValues:(NSArray *)values inSession:(PTYSession *)aSession
{
assert(false);
}
- (void)tryString:(NSString *)s inSession:(PTYSession *)aSession
{
NSRange range = [s rangeOfRegex:regex_];
if (range.location != NSNotFound) {
NSArray *captures = [s arrayOfCaptureComponentsMatchedByRegex:regex_];
for (NSArray *matches in captures) {
[self performActionWithValues:matches
inSession:aSession];
}
}
}
- (NSString *)paramWithBackreferencesReplacedWithValues:(NSArray *)values
{
NSString *p = self.param;
for (int i = 0; i < 9; i++) {
NSString *rep = @"";
if (values.count > i) {
rep = [values objectAtIndex:i];
}
p = [p stringByReplacingBackreference:i withString:rep];
}
p = [p stringByReplacingEscapedChar:'a' withString:@"\x07"];
p = [p stringByReplacingEscapedChar:'b' withString:@"\x08"];
p = [p stringByReplacingEscapedChar:'e' withString:@"\x1b"];
p = [p stringByReplacingEscapedChar:'n' withString:@"\n"];
p = [p stringByReplacingEscapedChar:'r' withString:@"\r"];
p = [p stringByReplacingEscapedChar:'t' withString:@"\t"];
p = [p stringByReplacingEscapedHexValuesWithChars];
return p;
}
- (NSComparisonResult)compareTitle:(Trigger *)other
{
return [[self title] compare:[other title]];
}
@end