/
Trigger.m
137 lines (115 loc) · 3.01 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// 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] autorelease];
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);
}
- (BOOL)paramIsPopupButton
{
return NO;
}
- (NSDictionary *)menuItemsForPoupupButton
{
return nil;
}
- (NSArray *)groupedMenuItemsForPopupButton
{
NSDictionary *menuItems = [self menuItemsForPoupupButton];
if (menuItems) {
return [NSArray arrayWithObject:menuItems];
} else {
return nil;
}
}
- (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]];
}
- (int)indexOfTag:(int)theTag
{
return theTag;
}
- (int)tagAtIndex:(int)theIndex
{
return 0;
}
- (NSArray *)tagsSortedByValueInDict:(NSDictionary *)dict
{
return [dict keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
@end