mirror of
https://github.com/macvim-dev/macvim.git
synced 2026-06-11 15:37:29 +02:00
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).
This commit is contained in:
@@ -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:
|
||||
@"<C-\\><C-N>:cal foreground()<CR>"];
|
||||
return;
|
||||
}
|
||||
}
|
||||
@catch (NSException *e) {
|
||||
// Do nothing ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MMVimController *vc;
|
||||
BOOL openInTabs = [[NSUserDefaults standardUserDefaults]
|
||||
boolForKey:MMOpenFilesInTabsKey];
|
||||
|
||||
+55
-41
@@ -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 <MMVimClientProtocol>)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)
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ char *MessageStrings[] =
|
||||
"LeaveFullscreenMsgID",
|
||||
"BuffersNotModifiedMsgID",
|
||||
"BuffersModifiedMsgID",
|
||||
"AddInputMsgID",
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user