forked from insanehunter/XCode4_beginning_of_line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXCode4_beginning_of_line.m
executable file
·109 lines (96 loc) · 4.2 KB
/
XCode4_beginning_of_line.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
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
static IMP original_doCommandBySelector = nil;
@interface XCode4_beginning_of_line : NSObject
@end
@implementation XCode4_beginning_of_line
static void doCommandBySelector( id self_, SEL _cmd, SEL selector )
{
/* CMD+LEFT: moves to beginning of text on line*/
if (selector == @selector(moveToBeginningOfLine:) ||
selector == @selector(moveToLeftEndOfLine:))
{
NSTextView *self = (NSTextView *)self_;
NSString *text = self.string;
NSRange selectedRange = self.selectedRange;
NSRange lineRange = [text lineRangeForRange:selectedRange];
if (lineRange.length != 0)
{
NSString *line = [text substringWithRange:lineRange];
NSRange codeStartRange = [line rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];
if (codeStartRange.location != NSNotFound)
{
NSUInteger caretLocation = selectedRange.location - lineRange.location;
if (caretLocation > codeStartRange.location || caretLocation == 0)
{
[self setSelectedRange:NSMakeRange(lineRange.location + codeStartRange.location, 0)];
return;
}
}
}
}
/* SHIFT+CMD+LEFT: highlights to beginning of text on line */
else if(selector == @selector(moveToBeginningOfLineAndModifySelection:) ||
selector == @selector(moveToLeftEndOfLineAndModifySelection:))
{
NSTextView *self = (NSTextView *)self_;
NSString *text = self.string;
NSRange selectedRange = self.selectedRange;
NSRange lineRange = [text lineRangeForRange:selectedRange];
if (lineRange.length != 0)
{
NSString *line = [text substringWithRange:lineRange];
NSRange codeStartRange = [line rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];
if (codeStartRange.location != NSNotFound)
{
NSUInteger caretLocation = selectedRange.location - lineRange.location;
if (caretLocation > codeStartRange.location)
{
[self setSelectedRange:NSMakeRange(lineRange.location + codeStartRange.location, (caretLocation - codeStartRange.location))];
return;
}
}
}
}
/* CMD+DELETE: deletes to beginning of text on line */
else if(selector == @selector(deleteToBeginningOfLine:))
{
NSTextView *self = (NSTextView *)self_;
NSString *text = self.string;
NSRange selectedRange = self.selectedRange;
NSRange lineRange = [text lineRangeForRange:selectedRange];
if (lineRange.length != 0)
{
NSString *line = [text substringWithRange:lineRange];
NSRange codeStartRange = [line rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];
if (codeStartRange.location != NSNotFound)
{
NSUInteger caretLocation = selectedRange.location - lineRange.location;
if (caretLocation > codeStartRange.location)
{
[self setSelectedRange:NSMakeRange(lineRange.location + codeStartRange.location, (caretLocation - codeStartRange.location))];
[self delete:nil];
return;
}
}
}
}
return ((void (*)(id, SEL, SEL))original_doCommandBySelector)(self_, _cmd, selector);
}
+ (void) pluginDidLoad:(NSBundle *)plugin
{
Class class = nil;
Method originalMethod = nil;
NSLog(@"%@ initializing...", NSStringFromClass([self class]));
if (!(class = NSClassFromString(@"DVTSourceTextView")))
goto failed;
if (!(originalMethod = class_getInstanceMethod(class, @selector(doCommandBySelector:))))
goto failed;
if (!(original_doCommandBySelector = method_setImplementation(originalMethod, (IMP)&doCommandBySelector)))
goto failed;
NSLog(@"%@ complete!", NSStringFromClass([self class]));
return;
failed:
NSLog(@"%@ failed. :(", NSStringFromClass([self class]));
}
@end