@@ -1767,8 +1767,23 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
1767
1767
// parse value
1768
1768
NSString *v = [arr objectAtIndex: 1 ];
1769
1769
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
+
1772
1787
if (decode)
1773
1788
{
1774
1789
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
@@ -1785,31 +1800,63 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
1785
1800
// Actually open the file.
1786
1801
NSString *file = [dict objectForKey: @" url" ];
1787
1802
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 ];
1810
1844
}
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]];
1811
1856
1812
- [self openFiles: filenames withArguments: args];
1857
+ [alert setAlertStyle: NSAlertStyleWarning];
1858
+ [alert runModal ];
1859
+ [alert release ];
1813
1860
}
1814
1861
}
1815
1862
} else {
0 commit comments