applidium / Vim

Port of the Vim text editor to the iOS
https://applidium.com/en
533 stars 100 forks source link

Support for "Open file in..." #12

Closed zeedark closed 6 years ago

zeedark commented 12 years ago

It would be great if, in addition to Dropbox and iCloud, it integrates with "Open file in..." menu.

Ecco commented 12 years ago

Indeed. The patch isn't complicated, but tedious to write : you need to gather all UTIs that Vim supports, and there's quite a bunch of them :-) Maybe this is something that MacVim already does and that could be borrowed from there tough.

N-Holzschuch commented 12 years ago

Actually, you can do it using generic types: public.source-code, public.plain-text, public.html, public.script and public.text seem to cover practically all the interesting cases.

Now there's the question of writing OpenURL, though...

N-Holzschuch commented 12 years ago

I think that is the solution to the problem:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Per Apple's documentation : Performs the specified selector on the application's main thread during that thread's next run loop cycle.

    NSURL* url = nil;
    if (launchOptions && launchOptions.count) {
        // Someone asked us to restart the application
        // Need to extract the URL to open
        url = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    }

    gui_ios.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    gui_ios.view_controller = [[VimViewController alloc] init];
    gui_ios.window.rootViewController = gui_ios.view_controller;
    [gui_ios.view_controller release];
    [gui_ios.window makeKeyAndVisible];

    [self performSelectorOnMainThread:@selector(_VimMain:) withObject:url waitUntilDone:NO];
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([gui_ios.view_controller canBecomeFirstResponder]) {
        NSString* urlString = url.absoluteString;
        if (url.isFileURL) {
            // Find "Documents/" in the urlString.
            NSRange position = [urlString rangeOfString:@"Documents/"];
            if (position.location == NSNotFound) return NO;
            position.location += position.length;
            position.length = [urlString length] - position.location;
            NSString* fileName = [urlString substringWithRange:position];
            char command[255];
            sprintf(command, "tabedit %s", [fileName UTF8String]);
            do_cmdline_cmd((char_u *)command);
            command[0] = Ctrl_L;
            command[1] = 0x0;
            add_to_input_buf(command, 1);
            return YES;
        }
    }
    return NO;
}

- (void)_VimMain:(NSURL *)url {
    NSString * vimPath = [[NSBundle mainBundle] resourcePath];
    vim_setenv((char_u *)"VIM", (char_u *)[vimPath UTF8String]);
    vim_setenv((char_u *)"VIMRUNTIME", (char_u *)[[vimPath stringByAppendingPathComponent:@"runtime"] UTF8String]);

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if (paths.count > 0) {
        vim_setenv((char_u *)"HOME", (char_u *)[[paths objectAtIndex:0] UTF8String]);
        [[NSFileManager defaultManager] changeCurrentDirectoryPath:[paths objectAtIndex:0]];
    }

    char * argv[2] = { "vim", nil};
    int numArgs = 1;
    if (url && (url != nil) && (url.isFileURL)) {
        // Need to find the file name
        // Find "Documents/" in the urlString.
        NSString* urlString = url.absoluteString;
        NSRange position = [urlString rangeOfString:@"Documents/"];
        if (position.location != NSNotFound) {
            position.location += position.length;
            position.length = [urlString length] - position.location;
            char* fileName = [[urlString substringWithRange:position] UTF8String];
            argv[1] = fileName;
            numArgs += 1;
        }
    }

    VimMain(numArgs, argv);
}

I had to rewrite OpenURL, didFinishLaunchingWithOptions and _VimMain (so that it can take a parameter). Of course, you'll also need to edit the plist to add public.text and the other file types you want.

I have tested it, and it allows me to "open with Vim" all text files. Really useful.

au-phiware commented 11 years ago

@N-Holzschuch make a pull request! :+1: :+1:

nicolasbraun commented 6 years ago

Related to #6