Coder Social home page Coder Social logo

Support for "Open file in..." about vim HOT 5 CLOSED

applidium avatar applidium commented on August 26, 2024
Support for "Open file in..."

from vim.

Comments (5)

Ecco avatar Ecco commented on August 26, 2024

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.

from vim.

N-Holzschuch avatar N-Holzschuch commented on August 26, 2024

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...

from vim.

N-Holzschuch avatar N-Holzschuch commented on August 26, 2024

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.

from vim.

au-phiware avatar au-phiware commented on August 26, 2024

@N-Holzschuch make a pull request!
👍 👍

from vim.

nicolasbraun avatar nicolasbraun commented on August 26, 2024

Related to #6

from vim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.