From b88c64d80df7e12af9c338e2323983cc776ea537 Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Thu, 27 Sep 2007 17:52:48 +0000 Subject: [PATCH] Implemented support for 'enc' (disable with MM_ENABLE_CONV = 0) git-svn-id: http://macvim.googlecode.com/svn/trunk@285 96c4425d-ca35-0410-94e5-3396d5c13a8f --- MMBackend.h | 2 +- MMBackend.m | 215 ++++++++++++++++++++++++------- gui_macvim.m | 356 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 396 insertions(+), 177 deletions(-) diff --git a/MMBackend.h b/MMBackend.h index f64753188c..cee40ad813 100644 --- a/MMBackend.h +++ b/MMBackend.h @@ -83,7 +83,7 @@ - (BOOL)tabBarVisible; - (void)showTabBar:(BOOL)enable; - (void)setRows:(int)rows columns:(int)cols; -- (void)setVimWindowTitle:(char *)title; +- (void)setWindowTitle:(char *)title; - (char *)browseForFileInDirectory:(char *)dir title:(char *)title saving:(int)saving; - (int)presentDialogWithType:(int)type title:(char *)title message:(char *)msg diff --git a/MMBackend.m b/MMBackend.m index d3782895aa..23be376134 100644 --- a/MMBackend.m +++ b/MMBackend.m @@ -443,9 +443,14 @@ enum { for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) { // This function puts the label of the tab in the global 'NameBuff'. get_tabline_label(tp, FALSE); - int len = strlen((char*)NameBuff); + char_u *s = NameBuff; + int len = STRLEN(s); if (len <= 0) continue; +#if MM_ENABLE_CONV + s = CONVERT_TO_UTF8(s); +#endif + // Count the number of windows in the tabpage. //win_T *wp = tp->tp_firstwin; //int wincount; @@ -453,7 +458,11 @@ enum { //[data appendBytes:&wincount length:sizeof(int)]; [data appendBytes:&len length:sizeof(int)]; - [data appendBytes:NameBuff length:len]; + [data appendBytes:s length:len]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(s); +#endif } [self queueMessage:UpdateTabBarMsgID data:data]; @@ -482,7 +491,7 @@ enum { [self queueMessage:SetTextDimensionsMsgID data:data]; } -- (void)setVimWindowTitle:(char *)title +- (void)setWindowTitle:(char *)title { NSMutableData *data = [NSMutableData data]; int len = strlen(title); @@ -491,7 +500,7 @@ enum { [data appendBytes:&len length:sizeof(int)]; [data appendBytes:title length:len]; - [self queueMessage:SetVimWindowTitleMsgID data:data]; + [self queueMessage:SetWindowTitleMsgID data:data]; } - (char *)browseForFileInDirectory:(char *)dir title:(char *)title @@ -501,12 +510,8 @@ enum { // saving); char_u *s = NULL; - NSString *ds = dir - ? [NSString stringWithCString:dir encoding:NSUTF8StringEncoding] - : nil; - NSString *ts = title - ? [NSString stringWithCString:title encoding:NSUTF8StringEncoding] - : nil; + NSString *ds = dir ? [NSString stringWithUTF8String:dir] : nil; + NSString *ts = title ? [NSString stringWithUTF8String:title] : nil; @try { [frontendProxy showSavePanelForDirectory:ds title:ts saving:saving]; @@ -515,7 +520,14 @@ enum { beforeDate:[NSDate distantFuture]]; if (dialogReturn && [dialogReturn isKindOfClass:[NSString class]]) { - s = vim_strsave((char_u*)[dialogReturn UTF8String]); + char_u *ret = (char_u*)[dialogReturn UTF8String]; +#if MM_ENABLE_CONV + ret = CONVERT_FROM_UTF8(ret); +#endif + s = vim_strsave(ret); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(ret); +#endif } [dialogReturn release]; dialogReturn = nil; @@ -591,8 +603,14 @@ enum { retval = [[dialogReturn objectAtIndex:0] intValue]; if (txtfield && [dialogReturn count] > 1) { NSString *retString = [dialogReturn objectAtIndex:1]; - vim_strncpy((char_u*)txtfield, (char_u*)[retString UTF8String], - IOSIZE - 1); + char_u *ret = (char_u*)[retString UTF8String]; +#if MM_ENABLE_CONV + ret = CONVERT_FROM_UTF8(ret); +#endif + vim_strncpy((char_u*)txtfield, ret, IOSIZE - 1); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(ret); +#endif } } @@ -772,8 +790,7 @@ enum { BOOL parseFailed = NO; if (name) { - fontName = [[[NSString alloc] initWithCString:name - encoding:NSUTF8StringEncoding] autorelease]; + fontName = [NSString stringWithUTF8String:name]; if ([fontName isEqual:@"*"]) { // :set gfn=* shows the font panel. @@ -910,12 +927,6 @@ enum { [self queueMessage:ActivateMsgID data:nil]; } -- (void)setServerName:(NSString *)name -{ - NSData *data = [name dataUsingEncoding:NSUTF8StringEncoding]; - [self queueMessage:SetServerNameMsgID data:data]; -} - - (int)lookupColorWithKey:(NSString *)key { if (!(key && [key length] > 0)) @@ -981,11 +992,10 @@ enum { // NOTE: This method might get called whenever the run loop is tended to. // Thus it might get called whilst input is being processed. Normally this // is not a problem, but if it gets called often then it might become - // dangerous. E.g. when a focus messages is received the screen is redrawn - // because the selection color changes and if another focus message is - // received whilst the first one is being processed Vim might crash. To - // deal with this problem at the moment, we simply drop messages that are - // received while other input is being processed. + // dangerous. E.g. say a message causes the screen to be redrawn and then + // another message is received causing another simultaneous screen redraw; + // this is not good. To deal with this problem at the moment, we simply + // drop messages that are received while other input is being processed. if (inProcessInput) { #if MM_USE_INPUT_QUEUE [inputQueue addObject:[NSNumber numberWithInt:msgid]]; @@ -1056,11 +1066,23 @@ enum { clip_copy_selection(); // Get the text to put on the pasteboard. - long_u len = 0; char_u *str = 0; - int type = clip_convert_selection(&str, &len, &clip_star); + long_u llen = 0; char_u *str = 0; + int type = clip_convert_selection(&str, &llen, &clip_star); if (type < 0) return NO; + // TODO: Avoid overflow. + int len = (int)llen; +#if MM_ENABLE_CONV + if (output_conv.vc_type != CONV_NONE) { + char_u *conv_str = string_convert(&output_conv, str, &len); + if (conv_str) { + vim_free(str); + str = conv_str; + } + } +#endif + NSString *string = [[NSString alloc] initWithBytes:str length:len encoding:NSUTF8StringEncoding]; @@ -1104,7 +1126,17 @@ enum { { //NSLog(@"addInput:%@ client:%@", input, (id)client); - server_to_input_buf((char_u*)[input UTF8String]); + char_u *s = (char_u*)[input UTF8String]; + +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + + server_to_input_buf(s); + +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif [self addClient:(id)client]; @@ -1117,10 +1149,27 @@ enum { //NSLog(@"evaluateExpression:%@ client:%@", expr, (id)client); NSString *eval = nil; - char_u *res = eval_client_expr_to_string((char_u*)[expr UTF8String]); + char_u *s = (char_u*)[expr UTF8String]; + +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + + char_u *res = eval_client_expr_to_string(s); + +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif if (res != NULL) { - eval = [NSString stringWithUTF8String:(char*)res]; + s = res; +#if MM_ENABLE_CONV + s = CONVERT_TO_UTF8(s); +#endif + eval = [NSString stringWithUTF8String:(char*)s]; +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(s); +#endif vim_free(res); } @@ -1149,8 +1198,15 @@ enum { //[svrConn setReplyTimeout:MMReplyTimeout]; [svrConn setRootObject:self]; + char_u *s = (char_u*)[svrName UTF8String]; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif // NOTE: 'serverName' is a global variable - serverName = vim_strsave((char_u*)[svrName UTF8String]); + serverName = vim_strsave(s); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif #ifdef FEAT_EVAL set_vim_var_string(VV_SEND_SERVER, serverName, -1); #endif @@ -1176,8 +1232,16 @@ enum { NSConnection *conn = [self connectionForServerName:name]; if (!conn) { - if (!silent) - EMSG2(_(e_noserver), [name UTF8String]); + if (!silent) { + char_u *s = (char_u*)[name UTF8String]; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + EMSG2(_(e_noserver), s); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif + } return NO; } @@ -1193,8 +1257,18 @@ enum { if (expr) { NSString *eval = [proxy evaluateExpression:string client:self]; if (reply) { - *reply = (eval ? vim_strsave((char_u*)[eval UTF8String]) - : vim_strsave((char_u*)_(e_invexprmsg))); + if (eval) { + char_u *r = (char_u*)[eval UTF8String]; +#if MM_ENABLE_CONV + r = CONVERT_FROM_UTF8(r); +#endif + *reply = vim_strsave(r); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(r); +#endif + } else { + *reply = vim_strsave((char_u*)_(e_invexprmsg)); + } } if (!eval) @@ -1552,8 +1626,17 @@ enum { NSMutableString *name = [NSMutableString stringWithUTF8String:bytes]; [name appendString:[NSString stringWithFormat:@":h%.2f", pointSize]]; + char_u *s = (char_u*)[name UTF8String]; - set_option_value((char_u*)"gfn", 0, (char_u*)[name UTF8String], 0); +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + + set_option_value((char_u*)"guifont", 0, s, 0); + +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif // Force screen redraw (does it have to be this complicated?). redraw_all_later(CLEAR); @@ -1579,7 +1662,14 @@ enum { int i = 0; while (bytes < end && i < n) { int len = *((int*)bytes); bytes += sizeof(int); - fnames[i++] = vim_strnsave((char_u*)bytes, len); + char_u *s = (char_u*)bytes; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + fnames[i++] = vim_strsave(s); +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8_FREE(s); +#endif bytes += len; } @@ -1616,7 +1706,14 @@ enum { // messy). goto_tabpage(9999); - do_cmdline_cmd((char_u*)[cmd UTF8String]); + char_u *s = (char_u*)[cmd UTF8String]; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + do_cmdline_cmd(s); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif // Force screen redraw (does it have to be this complicated?). // (This code was taken from the end of gui_handle_drop().) @@ -1645,7 +1742,16 @@ enum { } len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; - dnd_yank_drag_data((char_u*)[string UTF8String], len); + char_u *s = (char_u*)[string UTF8String]; +#if MM_ENABLE_CONV + if (input_conv.vc_type != CONV_NONE) + s = string_convert(&input_conv, s, &len); +#endif + dnd_yank_drag_data(s, len); +#if MM_ENABLE_CONV + if (input_conv.vc_type != CONV_NONE) + vim_free(s); +#endif add_to_input_buf(dropkey, sizeof(dropkey)); #endif // FEAT_DND } else if (GotFocusMsgID == msgid) { @@ -1688,6 +1794,9 @@ enum { char_u special[3]; char_u modChars[3]; char_u *chars = (char_u*)[key UTF8String]; +#if MM_ENABLE_CONV + char_u *conv_str = NULL; +#endif int length = [key lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; // Special keys (arrow keys, function keys, etc.) are stored in a plist so @@ -1730,10 +1839,15 @@ enum { length = 3; } else if (mods & MOD_MASK_ALT) { int mtab = 0x80 | TAB; - // Convert to utf-8 - special[0] = (mtab >> 6) + 0xc0; - special[1] = mtab & 0xbf; - length = 2; + if (enc_utf8) { + // Convert to utf-8 + special[0] = (mtab >> 6) + 0xc0; + special[1] = mtab & 0xbf; + length = 2; + } else { + special[0] = mtab; + length = 1; + } mods &= ~MOD_MASK_ALT; } } else if (length > 0) { @@ -1766,6 +1880,14 @@ enum { //NSLog(@"clear alt"); mods &= ~MOD_MASK_ALT; } + +#if MM_ENABLE_CONV + if (input_conv.vc_type != CONV_NONE) { + conv_str = string_convert(&input_conv, chars, &length); + if (conv_str) + chars = conv_str; + } +#endif } if (chars && length > 0) { @@ -1781,6 +1903,11 @@ enum { // TODO: Check for CSI bytes? add_to_input_buf(chars, length); } + +#if MM_ENABLE_CONV + if (conv_str) + vim_free(conv_str); +#endif } - (void)queueMessage:(int)msgid data:(NSData *)data diff --git a/gui_macvim.m b/gui_macvim.m index 0c5aa00037..2c6f9127cc 100644 --- a/gui_macvim.m +++ b/gui_macvim.m @@ -71,11 +71,9 @@ gui_mch_init(void) if (![[MMBackend sharedInstance] checkin]) return FAIL; - // HACK! Force the 'termencoding to utf-8. For the moment also force - // 'encoding', although this will change in the future. The user can still - // change 'encoding'; doing so WILL crash the program. + // Force 'termencoding' to utf-8 (changes to 'tenc' are disallowed in + // 'option.c', so that ':set termencoding=...' is impossible). set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0); - set_option_value((char_u *)"encoding", 0L, (char_u *)"utf-8", 0); // Set values so that pixels and characters are in one-to-one // correspondence (assuming all characters have the same dimensions). @@ -209,88 +207,28 @@ gui_mch_delete_lines(int row, int num_lines) void gui_mch_draw_string(int row, int col, char_u *s, int len, int flags) { +#if MM_ENABLE_CONV + char_u *conv_str = NULL; + if (output_conv.vc_type != CONV_NONE) { + conv_str = string_convert(&output_conv, s, &len); + if (conv_str) + s = conv_str; + } +#endif + [[MMBackend sharedInstance] replaceString:(char*)s length:len row:row column:col flags:flags]; + +#if MM_ENABLE_CONV + if (conv_str) + vim_free(conv_str); +#endif } int gui_macvim_draw_string(int row, int col, char_u *s, int len, int flags) { -#if 0 - NSString *string = [[NSString alloc] - initWithBytesNoCopy:(void*)s - length:len - encoding:NSUTF8StringEncoding - freeWhenDone:NO]; - int cells = [string length]; - [string release]; - - NSLog(@"gui_macvim_draw_string(row=%d, col=%d, len=%d, cells=%d, flags=%d)", - row, col, len, cells, flags); - - [[MMBackend sharedInstance] replaceString:(char*)s length:len - row:row column:col flags:flags]; - - return cells; -#elif 0 - int c; - int cn; - int cl; - int i; - BOOL wide = NO; - int start = 0; - int endcol = col; - int startcol = col; - MMBackend *backend = [MMBackend sharedInstance]; - - for (i = 0; i < len; i += cl) { - c = utf_ptr2char(s + i); - cl = utf_ptr2len(s + i); - cn = utf_char2cells(c); - comping = utf_iscomposing(c); - - if (!comping) - endcol += cn; - - if (cn > 1 && !wide) { - // Start of wide characters. - wide = YES; - - // Output non-wide characters. - if (start > i) { - NSLog(@"Outputting %d non-wide chars (%d bytes)", - endcol-startcol, start-i); - [backend replaceString:(char*)(s+start) length:start-i - row:row column:startcol flags:flags]; - startcol = endcol; - start = i; - } - } else if (cn <= 1 && !comping && wide) { - // End of wide characters. - wide = NO; - - // Output wide characters. - if (start > i) { - NSLog(@"Outputting %d wide chars (%d bytes)", - endcol-startcol, start-i); - [backend replaceString:(char*)(s+start) length:start-i - row:row column:startcol flags:(flags|0x80)]; - startcol = endcol; - start = i; - } - } - } - - // Output remaining characters. - flags = wide ? flags|0x80 : flags; - NSLog(@"Outputting %d %s chars (%d bytes)", endcol-startcol, wide ? "wide" - : "non-wide", len-start); - [backend replaceString:(char*)(s+start) length:len-start - row:row column:startcol flags:flags]; - - return endcol - col; -#elif 1 // // Output chars until a wide char found. If a wide char is found, output a // zero-width space after it so that a wide char looks like two chars to @@ -311,7 +249,7 @@ gui_macvim_draw_string(int row, int col, char_u *s, int len, int flags) char_u *conv_str = NULL; if (output_conv.vc_type != CONV_NONE) { - char_u *conv_str = string_convert(&output_conv, s, &len); + conv_str = string_convert(&output_conv, s, &len); if (conv_str) s = conv_str; } @@ -392,23 +330,6 @@ gui_macvim_draw_string(int row, int col, char_u *s, int len, int flags) #endif return endcol - col; -#else - // This will fail abysmally when wide or composing characters are used. - [[MMBackend sharedInstance] - replaceString:(char*)s length:len row:row column:col flags:flags]; - - int i, c, cl, cn, cells = 0; - for (i = 0; i < len; i += cl) { - c = utf_ptr2char(s + i); - cl = utf_ptr2len(s + i); - cn = utf_char2cells(c); - - if (!utf_iscomposing(c)) - cells += cn; - } - - return cells; -#endif } @@ -577,19 +498,17 @@ clip_mch_request_selection(VimClipboard *cbd) int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; #if MM_ENABLE_CONV - if (input_conv.vc_type != CONV_NONE) { - NSLog(@"Converting from: '%@'", string); - char_u *conv_str = string_convert(&input_conv, str, &len); - if (conv_str) { - NSLog(@" to: '%s'", conv_str); - clip_yank_selection(type, conv_str, len, cbd); - vim_free(conv_str); - return; - } - } + if (input_conv.vc_type != CONV_NONE) + str = string_convert(&input_conv, str, &len); #endif - clip_yank_selection(type, str, len, cbd); + if (str) + clip_yank_selection(type, str, len, cbd); + +#if MM_ENABLE_CONV + if (input_conv.vc_type != CONV_NONE) + vim_free(str); +#endif } } @@ -659,9 +578,18 @@ gui_mch_add_menu(vimmenu_T *menu, int idx) MenuMenubarType; } + char_u *dname = menu->dname; +#if MM_ENABLE_CONV + dname = CONVERT_TO_UTF8(dname); +#endif + [[MMBackend sharedInstance] - addMenuWithTag:(int)menu parent:parent name:(char*)menu->dname + addMenuWithTag:(int)menu parent:parent name:(char*)dname atIndex:idx]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(dname); +#endif } @@ -673,20 +601,27 @@ gui_mch_add_menu_item(vimmenu_T *menu, int idx) { // NOTE! If 'iconfile' is not set but 'iconidx' is, use the name of the // menu item. (Should correspond to a stock item.) - char *icon = menu->iconfile ? (char*)menu->iconfile : - menu->iconidx >= 0 ? (char*)menu->dname : + char_u *icon = menu->iconfile ? menu->iconfile : + menu->iconidx >= 0 ? menu->dname : NULL; //char *name = menu_is_separator(menu->name) ? NULL : (char*)menu->dname; - char *name = (char*)menu->dname; - char *tip = menu->strings[MENU_INDEX_TIP] - ? (char*)menu->strings[MENU_INDEX_TIP] : (char*)menu->actext; + char_u *name = menu->dname; + char_u *tip = menu->strings[MENU_INDEX_TIP] + ? menu->strings[MENU_INDEX_TIP] : menu->actext; + char_u *map_str = menu->strings[MENU_INDEX_NORMAL]; + +#if MM_ENABLE_CONV + icon = CONVERT_TO_UTF8(icon); + name = CONVERT_TO_UTF8(name); + tip = CONVERT_TO_UTF8(tip); + map_str = CONVERT_TO_UTF8(map_str); +#endif // HACK! Check if menu is mapped to ':action actionName:'; if so, pass the // action along so that MacVim can bind the menu item to this action. This // means that if a menu item maps to an action in normal mode, then all // other modes will also use the same action. NSString *action = nil; - char_u *map_str = menu->strings[MENU_INDEX_NORMAL]; if (map_str) { NSString *mapping = [NSString stringWithCString:(char*)map_str encoding:NSUTF8StringEncoding]; @@ -704,13 +639,20 @@ gui_mch_add_menu_item(vimmenu_T *menu, int idx) [[MMBackend sharedInstance] addMenuItemWithTag:(int)menu parent:(int)menu->parent - name:name - tip:tip + name:(char*)name + tip:(char*)tip icon:(char*)icon keyEquivalent:menu->ke_key modifiers:menu->ke_mods action:action atIndex:idx]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(icon); + CONVERT_TO_UTF8_FREE(name); + CONVERT_TO_UTF8_FREE(tip); + CONVERT_TO_UTF8_FREE(map_str); +#endif } @@ -754,8 +696,18 @@ gui_mch_menu_hidden(vimmenu_T *menu, int hidden) void gui_mch_show_popupmenu(vimmenu_T *menu) { - [[MMBackend sharedInstance] showPopupMenuWithName:(char*)menu->name + char_u *name = menu->name; +#if MM_ENABLE_CONV + name = CONVERT_TO_UTF8(name); +#endif + + [[MMBackend sharedInstance] showPopupMenuWithName:(char*)name atMouseLocation:YES]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(name); +#endif + } @@ -765,8 +717,16 @@ gui_mch_show_popupmenu(vimmenu_T *menu) void gui_make_popup(char_u *path_name, int mouse_pos) { +#if MM_ENABLE_CONV + path_name = CONVERT_TO_UTF8(path_name); +#endif + [[MMBackend sharedInstance] showPopupMenuWithName:(char*)path_name atMouseLocation:mouse_pos]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(path_name); +#endif } @@ -861,8 +821,18 @@ gui_mch_init_font(char_u *font_name, int fontset) // HACK! This gets called whenever the user types :set gfn=fontname, so // for now we set the font here. // TODO! Proper font handling, the way Vim expects it. - return [[MMBackend sharedInstance] - setFontWithName:(char*)font_name]; + +#if MM_ENABLE_CONV + font_name = CONVERT_TO_UTF8(font_name); +#endif + + BOOL ok = [[MMBackend sharedInstance] setFontWithName:(char*)font_name]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(font_name); +#endif + + return ok; } @@ -1053,13 +1023,22 @@ ex_action(eap) return; } - NSString *name = [NSString stringWithCString:(char*)eap->arg + char_u *arg = eap->arg; +#if MM_ENABLE_CONV + arg = CONVERT_TO_UTF8(arg); +#endif + + NSString *name = [NSString stringWithCString:(char*)arg encoding:NSUTF8StringEncoding]; if (gui_macvim_is_valid_action(name)) { [[MMBackend sharedInstance] executeActionWithName:name]; } else { EMSG2(_("E???: \"%s\" is not a valid action"), eap->arg); } + +#if MM_ENABLE_CONV + arg = CONVERT_TO_UTF8(arg); +#endif } @@ -1112,10 +1091,20 @@ gui_mch_browse( //NSLog(@"gui_mch_browse(saving=%d, title=%s, dflt=%s, ext=%s, initdir=%s," // " filter=%s", saving, title, dflt, ext, initdir, filter); +#if MM_ENABLE_CONV + title = CONVERT_TO_UTF8(title); + initdir = CONVERT_TO_UTF8(initdir); +#endif + char_u *s = (char_u*)[[MMBackend sharedInstance] browseForFileInDirectory:(char*)initdir title:(char*)title saving:saving]; +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(title); + CONVERT_TO_UTF8_FREE(initdir); +#endif + return s; } #endif /* FEAT_BROWSE */ @@ -1135,11 +1124,28 @@ gui_mch_dialog( // "dfltbutton=%d textfield=%s)", type, title, message, buttons, // dfltbutton, textfield); - return [[MMBackend sharedInstance] presentDialogWithType:type - title:(char*)title - message:(char*)message - buttons:(char*)buttons - textField:(char*)textfield]; +#if MM_ENABLE_CONV + title = CONVERT_TO_UTF8(title); + message = CONVERT_TO_UTF8(message); + buttons = CONVERT_TO_UTF8(buttons); + textfield = CONVERT_TO_UTF8(textfield); +#endif + + int ret = [[MMBackend sharedInstance] + presentDialogWithType:type + title:(char*)title + message:(char*)message + buttons:(char*)buttons + textField:(char*)textfield]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(title); + CONVERT_TO_UTF8_FREE(message); + CONVERT_TO_UTF8_FREE(buttons); + CONVERT_TO_UTF8_FREE(textfield); +#endif + + return ret; } @@ -1158,8 +1164,18 @@ gui_mch_flash(int msec) guicolor_T gui_mch_get_color(char_u *name) { +#if MM_ENABLE_CONV + name = CONVERT_TO_UTF8(name); +#endif + NSString *key = [NSString stringWithUTF8String:(char*)name]; - return [[MMBackend sharedInstance] lookupColorWithKey:key]; + guicolor_T col = [[MMBackend sharedInstance] lookupColorWithKey:key]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(name); +#endif + + return col; } @@ -1210,11 +1226,21 @@ gui_mch_get_winpos(int *x, int *y) int gui_mch_haskey(char_u *name) { + BOOL ok = NO; + +#if MM_ENABLE_CONV + name = CONVERT_TO_UTF8(name); +#endif + NSString *value = [NSString stringWithUTF8String:(char*)name]; if (value) - return [[MMBackend sharedInstance] hasSpecialKeyWithValue:value]; + ok = [[MMBackend sharedInstance] hasSpecialKeyWithValue:value]; - return NO; +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(name); +#endif + + return ok; } @@ -1292,7 +1318,15 @@ gui_mch_settitle(char_u *title, char_u *icon) { //NSLog(@"gui_mch_settitle(title=%s, icon=%s)", title, icon); - [[MMBackend sharedInstance] setVimWindowTitle:(char*)title]; +#if MM_ENABLE_CONV + title = CONVERT_TO_UTF8(title); +#endif + + [[MMBackend sharedInstance] setWindowTitle:(char*)title]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(title); +#endif } #endif @@ -1348,8 +1382,16 @@ gui_macvim_is_valid_action(NSString *action) void serverRegisterName(char_u *name) { +#if MM_ENABLE_CONV + name = CONVERT_TO_UTF8(name); +#endif + NSString *svrName = [NSString stringWithUTF8String:(char*)name]; [[MMBackend sharedInstance] registerServerWithName:svrName]; + +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(name); +#endif } @@ -1361,6 +1403,11 @@ serverRegisterName(char_u *name) serverSendToVim(char_u *name, char_u *cmd, char_u **result, int *port, int asExpr, int silent) { +#if MM_ENABLE_CONV + name = CONVERT_TO_UTF8(name); + cmd = CONVERT_TO_UTF8(cmd); +#endif + BOOL ok = [[MMBackend sharedInstance] sendToServer:[NSString stringWithUTF8String:(char*)name] string:[NSString stringWithUTF8String:(char*)cmd] @@ -1369,6 +1416,11 @@ serverSendToVim(char_u *name, char_u *cmd, char_u **result, expression:asExpr silent:silent]; +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(name); + CONVERT_TO_UTF8_FREE(cmd); +#endif + return ok ? 0 : -1; } @@ -1384,7 +1436,14 @@ serverGetVimNames(void) if (list) { NSString *string = [list componentsJoinedByString:@"\n"]; - names = vim_strsave((char_u*)[string UTF8String]); + char_u *s = (char_u*)[string UTF8String]; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + names = vim_strsave(s); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif } return names; @@ -1415,9 +1474,29 @@ serverStrToPort(char_u *str) serverPeekReply(int port, char_u **str) { NSString *reply = [[MMBackend sharedInstance] peekForReplyOnPort:port]; - if (str) + int len = [reply lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + + if (str && len > 0) { *str = (char_u*)[reply UTF8String]; +#if MM_ENABLE_CONV + if (input_conv.vc_type != CONV_NONE) { + char_u *s = string_convert(&input_conv, *str, &len); + + if (len > 0) { + // HACK! Since 's' needs to be freed we cannot simply set + // '*str = s' or memory will leak. Instead, create a dummy + // NSData and return its 'bytes' pointer, then autorelease the + // NSData. + NSData *data = [NSData dataWithBytes:s length:len+1]; + *str = (char_u*)[data bytes]; + } + + vim_free(s); + } +#endif + } + return reply != nil; } @@ -1432,7 +1511,14 @@ serverReadReply(int port, char_u **str) { NSString *reply = [[MMBackend sharedInstance] waitForReplyOnPort:port]; if (reply && str) { - *str = vim_strsave((char_u*)[reply UTF8String]); + char_u *s = (char_u*)[reply UTF8String]; +#if MM_ENABLE_CONV + s = CONVERT_FROM_UTF8(s); +#endif + *str = vim_strsave(s); +#if MM_ENABLE_CONV + CONVERT_FROM_UTF8_FREE(s); +#endif return 0; } @@ -1450,10 +1536,16 @@ serverSendReply(char_u *serverid, char_u *reply) int retval = -1; int port = serverStrToPort(serverid); if (port > 0 && reply) { +#if MM_ENABLE_CONV + reply = CONVERT_TO_UTF8(reply); +#endif BOOL ok = [[MMBackend sharedInstance] sendReply:[NSString stringWithUTF8String:(char*)reply] toPort:port]; retval = ok ? 0 : -1; +#if MM_ENABLE_CONV + CONVERT_TO_UTF8_FREE(reply); +#endif } return retval;