Skip to content

Commit

Permalink
• when initiating an edit, TextMate will open the text with the caret…
Browse files Browse the repository at this point in the history
… placed on the same line as the calling text / web view

I didn’t find any interface for getting the line number from the web view, so instead I insert a unicode replacement character and look for this — this has the disadvantages that if the text is not replaced (i.e. saved from TM), this code point will stay in the text (it will be visible)

git-svn-id: http://svn.textmate.org/trunk/Tools/Edit%20in%20TextMate@3426 dfb7d73b-c2ec-0310-8fea-fb051d288c6d
  • Loading branch information
sorbits committed May 26, 2006
1 parent 3c87ba9 commit 9cdd5b2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Edit in TextMate.h
Expand Up @@ -10,5 +10,5 @@
@interface EditInTextMate : NSObject
{
}
+ (void)externalEditString:(NSString*)aString forView:(NSView*)aView;
+ (void)externalEditString:(NSString*)aString startingAtLine:(int)aLine forView:(NSView*)aView;
@end
28 changes: 24 additions & 4 deletions src/Edit in TextMate.mm
Expand Up @@ -18,6 +18,18 @@
static NSMutableDictionary* OpenFiles;
static NSString* TextMateBundleIdentifier = @"com.macromates.textmate";

#pragma options align=mac68k
struct PBX_SelectionRange
{
short unused1; // 0 (not used)
short lineNum; // line to select (<0 to specify range)
long startRange; // start of selection range (if line < 0)
long endRange; // end of selection range (if line < 0)
long unused2; // 0 (not used)
long theDate; // modification date/time
};
#pragma options align=reset

@implementation EditInTextMate
+ (void)setODBEventHandlers
{
Expand Down Expand Up @@ -67,6 +79,13 @@ + (void)asyncEditStringWithOptions:(NSDictionary*)someOptions
CFBundleGetPackageInfo(CFBundleGetMainBundle(), &packageType, &packageCreator);
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithTypeCode:packageCreator] forKeyword:keyFileSender];

if(int line = [[someOptions objectForKey:@"line"] intValue])
{
PBX_SelectionRange pos = { };
pos.lineNum = line;
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:'????' bytes:&pos length:sizeof(pos)] forKeyword:keyAEPosition];
}

OSStatus status = AESend([appleEvent aeDesc], &reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
if(status == noErr)
{
Expand All @@ -82,7 +101,7 @@ + (void)asyncEditStringWithOptions:(NSDictionary*)someOptions
[pool release];
}

+ (void)externalEditString:(NSString*)aString forView:(NSView*)aView
+ (void)externalEditString:(NSString*)aString startingAtLine:(int)aLine forView:(NSView*)aView
{
NSString* appName = [[[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"] lowercaseString];
NSString* windowTitle = [[aView window] title] ?: @"untitled";
Expand All @@ -96,9 +115,10 @@ + (void)externalEditString:(NSString*)aString forView:(NSView*)aView
fileName = [fileName stringByStandardizingPath];

NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
aString, @"string",
aView, @"view",
fileName, @"fileName",
aString, @"string",
aView, @"view",
fileName, @"fileName",
[NSNumber numberWithInt:aLine], @"line",
nil];

[OpenFiles setObject:options forKey:fileName];
Expand Down
12 changes: 11 additions & 1 deletion src/NSTextView: Edit in TextMate.mm
Expand Up @@ -19,10 +19,20 @@ - (void)editInTextMate:(id)sender

NSString* str = [[self textStorage] string];
NSRange selectedRange = [self selectedRange];
int lineNumber = 0;
if(selectedRange.length == 0)
{
NSRange range = NSMakeRange(0, 0);
do {
range = [str lineRangeForRange:NSMakeRange(NSMaxRange(range), 0)];
if(selectedRange.location < NSMaxRange(range))
break;
lineNumber++;
} while(true);
selectedRange = NSMakeRange(0, [str length]);
}

[EditInTextMate externalEditString:[str substringWithRange:selectedRange] forView:self];
[EditInTextMate externalEditString:[str substringWithRange:selectedRange] startingAtLine:lineNumber forView:self];
}

- (void)didModifyString:(NSString*)newString
Expand Down
22 changes: 20 additions & 2 deletions src/WebView: Edit in TextMate.mm
Expand Up @@ -140,14 +140,32 @@ - (void)editInTextMate:(id)sender
if(![self isEditable])
return (void)NSBeep();

NSString* const CARET = [NSString stringWithFormat:@"%C", 0xFFFD];
NSString* str = @"";
if(DOMDocumentFragment* selection = [[self selectedDOMRange] cloneContents] ?: ([self selectAll:nil], [[self selectedDOMRange] cloneContents]))
int lineNumber = 0;

DOMDocumentFragment* selection = [[self selectedDOMRange] cloneContents];
if(!selection)
{
[self insertText:CARET];
[self selectAll:nil];
selection = [[self selectedDOMRange] cloneContents];
}

if(selection)
{
str = convert_dom_to_text([[[self mainFrame] DOMDocument] createTreeWalker:selection :DOM_SHOW_ALL :nil :YES]);
while([str hasSuffix:@"\n\n"])
str = [str substringToIndex:[str length]-1];

NSArray* split = [str componentsSeparatedByString:CARET];
if([split count] == 2)
{
lineNumber = [[[split objectAtIndex:0] componentsSeparatedByString:@"\n"] count] - 1;
str = [split componentsJoinedByString:@""];
}
}
[EditInTextMate externalEditString:str forView:self];
[EditInTextMate externalEditString:str startingAtLine:lineNumber forView:self];
}

- (void)didModifyString:(NSString*)newString
Expand Down

0 comments on commit 9cdd5b2

Please sign in to comment.