From 28c3a55c083d3841868cc66c71bbaf473da7b4aa Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Wed, 7 Nov 2007 23:46:52 +0100 Subject: [PATCH] Add eval & add input methods to MacVim, apply to file open Now possible to evaluate expressions in a Vim process from MacVim [MMBackend evaluateExpression:], and to send arbitrary input to Vim [MMVimController addVimInput:]. Expression evaluation is used to query Vim state from within MacVim. When opening a file use these methods to check if it is already loaded, if so raise the corresponding window (doesn't work for multiple files). --- src/MacVim/MMAppController.m | 24 +++++++++++ src/MacVim/MMBackend.m | 96 +++++++++++++++++++++++++------------------- src/MacVim/MMVimController.h | 2 +- src/MacVim/MMVimController.m | 12 ++++++ src/MacVim/MacVim.h | 2 + src/MacVim/MacVim.m | 1 + 6 files changed, 95 insertions(+), 42 deletions(-) diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m index e4cbf486..f6815a79 100644 --- a/src/MacVim/MMAppController.m +++ b/src/MacVim/MMAppController.m @@ -175,6 +175,30 @@ static NSTimeInterval MMReplyTimeout = 5; return; } + if ([files count] == 1) { + // Check if the file is already open...if so raise that window. + i, count = [vimControllers count]; + for (i = 0; i < count; ++i) { + MMVimController *controller = [vimControllers objectAtIndex:i]; + id proxy = [controller backendProxy]; + + @try { + NSString *expr = [NSString stringWithFormat: + @"bufloaded(\"%@\")", [files objectAtIndex:0]]; + NSString *eval = [proxy evaluateExpression:expr]; + if ([eval isEqual:@"1"]) { + // TODO: Select the tab with 'file' open. + [controller addVimInput: + @":cal foreground()"]; + return; + } + } + @catch (NSException *e) { + // Do nothing ... + } + } + } + MMVimController *vc; BOOL openInTabs = [[NSUserDefaults standardUserDefaults] boolForKey:MMOpenFilesInTabsKey]; diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index d947e140..4d77f760 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -71,6 +71,7 @@ enum { - (void)handleDropFiles:(NSData *)data; - (void)handleDropString:(NSData *)data; - (BOOL)checkForModifiedBuffers; +- (void)addInput:(NSString *)input; @end @@ -1139,6 +1140,13 @@ enum { int col = *((int*)bytes); bytes += sizeof(int); gui_mouse_moved(col, row); + } else if (AddInputMsgID == msgid) { + NSString *string = [[NSString alloc] initWithData:data + encoding:NSUTF8StringEncoding]; + if (string) { + [self addInput:string]; + [string release]; + } } else { // Not keyboard or mouse event, queue it and handle later. //NSLog(@"Add event %s to input event queue", MessageStrings[msgid]); @@ -1166,6 +1174,36 @@ enum { } } +- (NSString *)evaluateExpression:(in bycopy NSString *)expr +{ + NSString *eval = nil; + char_u *s = (char_u*)[expr UTF8String]; + +#ifdef FEAT_MBYTE + s = CONVERT_FROM_UTF8(s); +#endif + + char_u *res = eval_client_expr_to_string(s); + +#ifdef FEAT_MBYTE + CONVERT_FROM_UTF8_FREE(s); +#endif + + if (res != NULL) { + s = res; +#ifdef FEAT_MBYTE + s = CONVERT_TO_UTF8(s); +#endif + eval = [NSString stringWithUTF8String:(char*)s]; +#ifdef FEAT_MBYTE + CONVERT_TO_UTF8_FREE(s); +#endif + vim_free(res); + } + + return eval; +} + - (BOOL)starRegisterToPasteboard:(byref NSPasteboard *)pboard { if (VIsual_active && (State & NORMAL) && clip_star.available) { @@ -1237,18 +1275,7 @@ enum { { //NSLog(@"addInput:%@ client:%@", input, (id)client); - char_u *s = (char_u*)[input UTF8String]; - -#ifdef FEAT_MBYTE - s = CONVERT_FROM_UTF8(s); -#endif - - server_to_input_buf(s); - -#ifdef FEAT_MBYTE - CONVERT_FROM_UTF8_FREE(s); -#endif - + [self addInput:input]; [self addClient:(id)client]; inputReceived = YES; @@ -1257,36 +1284,8 @@ enum { - (NSString *)evaluateExpression:(in bycopy NSString *)expr client:(in byref id )client { - //NSLog(@"evaluateExpression:%@ client:%@", expr, (id)client); - - NSString *eval = nil; - char_u *s = (char_u*)[expr UTF8String]; - -#ifdef FEAT_MBYTE - s = CONVERT_FROM_UTF8(s); -#endif - - char_u *res = eval_client_expr_to_string(s); - -#ifdef FEAT_MBYTE - CONVERT_FROM_UTF8_FREE(s); -#endif - - if (res != NULL) { - s = res; -#ifdef FEAT_MBYTE - s = CONVERT_TO_UTF8(s); -#endif - eval = [NSString stringWithUTF8String:(char*)s]; -#ifdef FEAT_MBYTE - CONVERT_TO_UTF8_FREE(s); -#endif - vim_free(res); - } - [self addClient:(id)client]; - - return eval; + return [self evaluateExpression:expr]; } - (void)registerServerWithName:(NSString *)name @@ -2085,6 +2084,21 @@ enum { return NO; } +- (void)addInput:(NSString *)input +{ + char_u *s = (char_u*)[input UTF8String]; + +#ifdef FEAT_MBYTE + s = CONVERT_FROM_UTF8(s); +#endif + + server_to_input_buf(s); + +#ifdef FEAT_MBYTE + CONVERT_FROM_UTF8_FREE(s); +#endif +} + @end // MMBackend (Private) diff --git a/src/MacVim/MMVimController.h b/src/MacVim/MMVimController.h index c8fb1183..e53e5fdf 100644 --- a/src/MacVim/MMVimController.h +++ b/src/MacVim/MMVimController.h @@ -54,7 +54,7 @@ - (void)sendMessage:(int)msgid data:(NSData *)data; - (BOOL)sendMessageNow:(int)msgid data:(NSData *)data timeout:(NSTimeInterval)timeout; - +- (void)addVimInput:(NSString *)string; @end // vim: set ft=objc: diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m index 0f0a3c60..b06b78b7 100644 --- a/src/MacVim/MMVimController.m +++ b/src/MacVim/MMVimController.m @@ -284,6 +284,17 @@ static NSTimeInterval MMResendInterval = 0.5; return sendOk; } +- (void)addVimInput:(NSString *)string +{ + // This is a very general method of adding input to the Vim process. It is + // basically the same as calling remote_send() on the process (see + // ':h remote_send'). + if (string) { + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; + [self sendMessage:AddInputMsgID data:data]; + } +} + - (id)backendProxy { return backendProxy; @@ -757,6 +768,7 @@ static NSTimeInterval MMResendInterval = 0.5; [windowController adjustLinespace:linespace]; } else if (ActivateMsgID == msgid) { + //NSLog(@"ActivateMsgID"); [NSApp activateIgnoringOtherApps:YES]; [[windowController window] makeKeyAndOrderFront:self]; } else if (SetServerNameMsgID == msgid) { diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index db6d1b07..e497c30a 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -30,6 +30,7 @@ - (oneway void)processInput:(int)msgid data:(in bycopy NSData *)data; - (oneway void)processInputAndData:(in bycopy NSArray *)messages; - (oneway void)setDialogReturn:(in bycopy id)obj; +- (NSString *)evaluateExpression:(in bycopy NSString *)expr; - (BOOL)starRegisterToPasteboard:(byref NSPasteboard *)pboard; @end @@ -152,6 +153,7 @@ enum { LeaveFullscreenMsgID, BuffersNotModifiedMsgID, BuffersModifiedMsgID, + AddInputMsgID, }; diff --git a/src/MacVim/MacVim.m b/src/MacVim/MacVim.m index 5ad21d86..23e90325 100644 --- a/src/MacVim/MacVim.m +++ b/src/MacVim/MacVim.m @@ -63,6 +63,7 @@ char *MessageStrings[] = "LeaveFullscreenMsgID", "BuffersNotModifiedMsgID", "BuffersModifiedMsgID", + "AddInputMsgID", }; -- 2.11.4.GIT