Skip to content

Commit

Permalink
Fix open from Xcode with selection range
Browse files Browse the repository at this point in the history
This is an initial attempt to fix a problem where double clicking a
search result in Xcode would select an incorrect range of characters
when the file opened in MacVim.  It only works the file uses an 8 bit
encoding.
  • Loading branch information
b4winckler committed Jan 17, 2011
1 parent 00936cd commit 210a71b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
58 changes: 32 additions & 26 deletions src/MacVim/MMAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -903,25 +903,6 @@ - (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args
NSDictionary *openFilesDict = nil;
filenames = [self filterOpenFiles:filenames openFilesDict:&openFilesDict];

// Pass arguments to vim controllers that had files open.
id key;
NSEnumerator *e = [openFilesDict keyEnumerator];

// (Indicate that we do not wish to open any files at the moment.)
[arguments setObject:[NSNumber numberWithBool:YES] forKey:@"dontOpen"];

while ((key = [e nextObject])) {
NSArray *files = [openFilesDict objectForKey:key];
[arguments setObject:files forKey:@"filenames"];

MMVimController *vc = [key pointerValue];
[vc passArguments:arguments];

// If this controller holds the first file, then remember it for later.
if ([files containsObject:firstFile])
firstController = vc;
}

// The meaning of "layout" is defined by the WIN_* defines in main.c.
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
int layout = [ud integerForKey:MMOpenLayoutKey];
Expand All @@ -939,6 +920,9 @@ - (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args
// do this when there are no more files to open, otherwise sometimes
// the window with 'firstFile' will be raised, other times it might be
// the window that will open with the files in the 'filenames' array.
//
// NOTE: Raise window before passing arguments, otherwise the selection
// will be lost when selectionRange is set.
firstFile = [firstFile stringByEscapingSpecialFilenameCharacters];

NSString *bufCmd = @"tab sb";
Expand All @@ -954,8 +938,25 @@ - (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args
"cal foreground()<CR>", bufCmd, firstFile];

[firstController addVimInput:input];
}

return YES;
// Pass arguments to vim controllers that had files open.
id key;
NSEnumerator *e = [openFilesDict keyEnumerator];

// (Indicate that we do not wish to open any files at the moment.)
[arguments setObject:[NSNumber numberWithBool:YES] forKey:@"dontOpen"];

while ((key = [e nextObject])) {
NSArray *files = [openFilesDict objectForKey:key];
[arguments setObject:files forKey:@"filenames"];

MMVimController *vc = [key pointerValue];
[vc passArguments:arguments];

// If this controller holds the first file, then remember it for later.
if ([files containsObject:firstFile])
firstController = vc;
}

// Add filenames to "Recent Files" menu, unless they are being edited
Expand All @@ -965,6 +966,9 @@ - (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args
noteNewRecentFilePaths:filenames];
}

if ([filenames count] == 0)
return YES; // No files left to open (all were already open)

//
// b) Open any remaining files
//
Expand Down Expand Up @@ -1757,9 +1761,10 @@ - (NSMutableDictionary *)extractArgumentsFromOdocEvent:
sr->unused2, sr->theDate);

if (sr->lineNum < 0) {
// Should select a range of lines.
// Should select a range of characters.
range.location = sr->startRange + 1;
range.length = sr->endRange - sr->startRange + 1;
range.length = sr->endRange > sr->startRange
? sr->endRange - sr->startRange : 1;
} else {
// Should only move cursor to a line.
range.location = sr->lineNum + 1;
Expand Down Expand Up @@ -2338,11 +2343,12 @@ - (NSDictionary *)convertVimControllerArguments:(NSDictionary *)args
NSRange r = NSRangeFromString(rangeString);
[a addObject:@"-c"];
if (r.length > 0) {
// Select given range.
[a addObject:[NSString stringWithFormat:@"norm %dGV%dGz.0",
NSMaxRange(r), r.location]];
// Select given range of characters.
// TODO: This only works for encodings where 1 byte == 1 character
[a addObject:[NSString stringWithFormat:@"norm %dgov%dgo",
r.location, NSMaxRange(r)-1]];
} else {
// Position cursor on start of range.
// Position cursor on line at start of range.
[a addObject:[NSString stringWithFormat:@"norm %dGz.0",
r.location]];
}
Expand Down
11 changes: 6 additions & 5 deletions src/MacVim/MMBackend.m
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ - (void)handleOpenWithArguments:(NSDictionary *)args
// filenames list of filenames
// dontOpen don't open files specified in above argument
// layout which layout to use to open files
// selectionRange range of lines to select
// selectionRange range of characters to select
// searchText string to search for
// cursorLine line to position the cursor on
// cursorColumn column to position the cursor on
Expand Down Expand Up @@ -2811,13 +2811,14 @@ - (void)handleOpenWithArguments:(NSDictionary *)args
NSString *rangeString = [args objectForKey:@"selectionRange"];
if (rangeString) {
// Build a command line string that will select the given range of
// lines. If range.length == 0, then position the cursor on the given
// line but do not select.
// characters. If range.length == 0, then position the cursor on the
// line at start of range but do not select.
NSRange range = NSRangeFromString(rangeString);
NSString *cmd;
if (range.length > 0) {
cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGV%dGz.0",
NSMaxRange(range), range.location];
// TODO: This only works for encodings where 1 byte == 1 character
cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dgov%dgo",
range.location, NSMaxRange(range)-1];
} else {
cmd = [NSString stringWithFormat:@"<C-\\><C-N>%dGz.0",
range.location];
Expand Down

0 comments on commit 210a71b

Please sign in to comment.