Skip to content

Commit 6fe1997

Browse files
authored
Merge pull request #1043 from ychin/mvim-protocol-handler-iterm-escape-fix
Fix mvim:// protocol handler not working well with iTerm2
2 parents 5de428c + 532679e commit 6fe1997

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

src/MacVim/MMAppController.m

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,23 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
17671767
// parse value
17681768
NSString *v = [arr objectAtIndex:1];
17691769

1770-
// do not decode url, since it's a file URI
1771-
BOOL decode = ![f isEqualToString:@"url"];
1770+
// Ideally we don't decode anything here. The input parameters
1771+
// should be used as-in as there would be no reason for caller
1772+
// to encoder anything. For the line component it's a simple
1773+
// string, and the URL should already be a proper file:// URL
1774+
// with all the necessary characters (e.g. space) encoded and
1775+
// we can just pass it to NSURL as-in below.
1776+
// However, iTerm2 appears to encode the slashes as well
1777+
// resulting in URL that looks like
1778+
// file://%2Fsome%2Ffolder/file%20with%20space which is wrong
1779+
// as this doesn't form a valid URL. To accommodate that, we
1780+
// decode the URL, and later on manually parse it instead of
1781+
// relying on NSURL.
1782+
// See: https://github.com/macvim-dev/macvim/issues/1020.
1783+
1784+
// BOOL decode = ![f isEqualToString:@"url"];
1785+
const BOOL decode = YES;
1786+
17721787
if (decode)
17731788
{
17741789
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
@@ -1785,31 +1800,63 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
17851800
// Actually open the file.
17861801
NSString *file = [dict objectForKey:@"url"];
17871802
if (file != nil) {
1788-
NSURL *fileUrl = [NSURL URLWithString:file];
1789-
// TextMate only opens files that already exist.
1790-
if ([fileUrl isFileURL]
1791-
&& [[NSFileManager defaultManager] fileExistsAtPath:
1792-
[fileUrl path]]) {
1793-
// Strip 'file://' path, else application:openFiles: might think
1794-
// the file is not yet open.
1795-
NSArray *filenames = [NSArray arrayWithObject:[fileUrl path]];
1796-
1797-
// Look for the line and column options.
1798-
NSDictionary *args = nil;
1799-
NSString *line = [dict objectForKey:@"line"];
1800-
if (line) {
1801-
NSString *column = [dict objectForKey:@"column"];
1802-
if (column)
1803-
args = [NSDictionary dictionaryWithObjectsAndKeys:
1804-
line, @"cursorLine",
1805-
column, @"cursorColumn",
1806-
nil];
1807-
else
1808-
args = [NSDictionary dictionaryWithObject:line
1809-
forKey:@"cursorLine"];
1803+
// Instead of passing "file" to NSURL directly, we just manually
1804+
// parse the URL because the URL is already decoded and NSURL will
1805+
// get confused by special chars like spaces. See above
1806+
// explanation.
1807+
if ([file hasPrefix:@"file:///"]) {
1808+
NSString *filePath = [file substringFromIndex:7];
1809+
// Only opens files that already exist.
1810+
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
1811+
NSArray *filenames = [NSArray arrayWithObject:filePath];
1812+
1813+
// Look for the line and column options.
1814+
NSDictionary *args = nil;
1815+
NSString *line = [dict objectForKey:@"line"];
1816+
if (line) {
1817+
NSString *column = [dict objectForKey:@"column"];
1818+
if (column)
1819+
args = [NSDictionary dictionaryWithObjectsAndKeys:
1820+
line, @"cursorLine",
1821+
column, @"cursorColumn",
1822+
nil];
1823+
else
1824+
args = [NSDictionary dictionaryWithObject:line
1825+
forKey:@"cursorLine"];
1826+
}
1827+
1828+
[self openFiles:filenames withArguments:args];
1829+
} else {
1830+
NSAlert *alert = [[NSAlert alloc] init];
1831+
[alert addButtonWithTitle:NSLocalizedString(@"OK",
1832+
@"Dialog button")];
1833+
1834+
[alert setMessageText:NSLocalizedString(@"Bad file path",
1835+
@"Bad file path dialog, title")];
1836+
[alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(
1837+
@"Cannot open file path \"%@\"",
1838+
@"Bad file path dialog, text"),
1839+
filePath]];
1840+
1841+
[alert setAlertStyle:NSAlertStyleWarning];
1842+
[alert runModal];
1843+
[alert release];
18101844
}
1845+
} else {
1846+
NSAlert *alert = [[NSAlert alloc] init];
1847+
[alert addButtonWithTitle:NSLocalizedString(@"OK",
1848+
@"Dialog button")];
1849+
1850+
[alert setMessageText:NSLocalizedString(@"Unknown File Protocol",
1851+
@"Unknown File Protocol dialog, title")];
1852+
[alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(
1853+
@"Unknown protocol in \"%@\"",
1854+
@"Unknown File Protocol dialog, text"),
1855+
file]];
18111856

1812-
[self openFiles:filenames withArguments:args];
1857+
[alert setAlertStyle:NSAlertStyleWarning];
1858+
[alert runModal];
1859+
[alert release];
18131860
}
18141861
}
18151862
} else {

0 commit comments

Comments
 (0)