To: vim-dev@vim.org Subject: Patch 6.1.421 (extra) Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.1.421 (extra, depends on 6.1.354) Problem: MS-Windows 9x: When putting text on the clipboard it can be in the wrong encoding. Solution: Convert text to the active codepage for CF_TEXT. (Glenn Maynard) Files: src/os_mswin.c *** ../vim61.420/src/os_mswin.c Wed Mar 26 21:48:04 2003 --- src/os_mswin.c Fri Mar 21 20:24:21 2003 *************** *** 969,975 **** utf8_str = alloc((unsigned)length); if (utf8_str != NULL) ! WideCharToMultiByte(enc_dbcs, 0, str, *len, utf8_str, length, 0,0); *len = length; return utf8_str; } --- 969,975 ---- utf8_str = alloc((unsigned)length); if (utf8_str != NULL) ! WideCharToMultiByte(enc_dbcs, 0, str, *len, utf8_str, length, 0, 0); *len = length; return utf8_str; } *************** *** 1018,1023 **** --- 1018,1024 ---- VimClipType_t metadata = { -1, -1, -1 }; HGLOBAL hMem = NULL; char_u *str = NULL; + char_u *to_free = NULL; char_u *hMemStr = NULL; int str_size = 0; int maxlen; *************** *** 1072,1078 **** if (hMemWstr[str_size] == NUL) break; } ! str = ucs2_to_enc(hMemWstr, &str_size); GlobalUnlock(hMemW); } } --- 1073,1079 ---- if (hMemWstr[str_size] == NUL) break; } ! to_free = str = ucs2_to_enc(hMemWstr, &str_size); GlobalUnlock(hMemW); } } *************** *** 1101,1106 **** --- 1102,1124 ---- if (str[str_size] == NUL) break; } + + /* The text is now in the active codepage. Convert to 'encoding', + * going through UCS-2. */ + maxlen = MultiByteToWideChar(CP_ACP, 0, str, str_size, NULL, 0); + to_free = alloc((unsigned)(maxlen * sizeof(WCHAR))); + if (to_free != NULL) + { + MultiByteToWideChar(CP_ACP, 0, str, str_size, + (WCHAR *)to_free, maxlen); + str_size = maxlen; + str = ucs2_to_enc((WCHAR *)to_free, &str_size); + if (str != NULL) + { + vim_free(to_free); + to_free = str; + } + } } } *************** *** 1125,1130 **** --- 1143,1149 ---- if (hMemStr != NULL) GlobalUnlock(hMem); CloseClipboard(); + vim_free(to_free); } /* *************** *** 1134,1140 **** clip_mch_set_selection(VimClipboard *cbd) { char_u *str = NULL; - long_u str_len; VimClipType_t metadata; HGLOBAL hMem = NULL; HGLOBAL hMemVim = NULL; --- 1153,1158 ---- *************** *** 1148,1163 **** cbd->owned = FALSE; /* Get the text to be put on the clipboard, with CR-LF. */ ! metadata.type = clip_convert_selection(&str, &str_len, cbd); if (metadata.type < 0) return; - metadata.txtlen = str_len; metadata.ucslen = 0; # if defined(FEAT_MBYTE) && defined(WIN3264) { WCHAR *out; ! int len = str_len; /* Convert the text to UCS-2. This is put on the clipboard as * CF_UNICODETEXT. */ --- 1166,1180 ---- cbd->owned = FALSE; /* Get the text to be put on the clipboard, with CR-LF. */ ! metadata.type = clip_convert_selection(&str, &metadata.txtlen, cbd); if (metadata.type < 0) return; metadata.ucslen = 0; # if defined(FEAT_MBYTE) && defined(WIN3264) { WCHAR *out; ! int len = metadata.txtlen; /* Convert the text to UCS-2. This is put on the clipboard as * CF_UNICODETEXT. */ *************** *** 1166,1171 **** --- 1183,1202 ---- { WCHAR *lpszMemW; + /* Convert the text for CF_TEXT to ANSI codepage. Otherwise it's + * p_enc, which has no relation to the ANSI codepage. */ + metadata.txtlen = WideCharToMultiByte(CP_ACP, 0, out, len, + NULL, 0, 0, 0); + vim_free(str); + str = (char_u *)alloc((unsigned)metadata.txtlen); + if (str == NULL) + { + vim_free(out); + return; /* out of memory */ + } + WideCharToMultiByte(CP_ACP, 0, out, len, + str, metadata.txtlen, 0, 0); + /* Allocate memory for the UCS-2 text, add one NUL word to * terminate the string. */ hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, *************** *** 1185,1198 **** /* Allocate memory for the text, add one NUL byte to terminate the string. */ ! hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, str_len + 1); { LPSTR lpszMem = (LPSTR)GlobalLock(hMem); if (lpszMem) { ! STRNCPY(lpszMem, str, str_len); ! lpszMem[str_len] = NUL; GlobalUnlock(hMem); } } --- 1216,1229 ---- /* Allocate memory for the text, add one NUL byte to terminate the string. */ ! hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1); { LPSTR lpszMem = (LPSTR)GlobalLock(hMem); if (lpszMem) { ! STRNCPY(lpszMem, str, metadata.txtlen); ! lpszMem[metadata.txtlen] = NUL; GlobalUnlock(hMem); } } *************** *** 1226,1233 **** # if defined(FEAT_MBYTE) && defined(WIN3264) if (hMemW != NULL) { ! SetClipboardData(CF_UNICODETEXT, hMemW); ! hMemW = 0; } # endif /* Always use CF_TEXT. On Win98 Notepad won't obtain the --- 1257,1264 ---- # if defined(FEAT_MBYTE) && defined(WIN3264) if (hMemW != NULL) { ! if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL) ! hMemW = NULL; } # endif /* Always use CF_TEXT. On Win98 Notepad won't obtain the *** ../vim61.420/src/version.c Wed Mar 26 22:18:13 2003 --- src/version.c Wed Mar 26 22:19:52 2003 *************** *** 613,614 **** --- 613,616 ---- { /* Add new patch number below this line */ + /**/ + 421, /**/ -- Get a life? What is the URL where it can be downloaded? /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\ \\\ Project leader for A-A-P -- http://www.A-A-P.org /// \\\ Help AIDS victims, buy at Amazon -- http://ICCF.nl/click1.html ///