Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * winclip.c |
| 12 | * |
Bram Moolenaar | 1266d67 | 2017-02-01 13:43:36 +0100 | [diff] [blame] | 13 | * Routines for Win32 clipboard handling. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 14 | * Also used by Cygwin, using os_unix.c. |
| 15 | */ |
| 16 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 17 | #include "vim.h" |
| 18 | |
| 19 | /* |
| 20 | * Compile only the clipboard handling features when compiling for cygwin |
| 21 | * posix environment. |
| 22 | */ |
| 23 | #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 24 | # define WIN32_LEAN_AND_MEAN |
| 25 | # include <windows.h> |
| 26 | # include "winclip.pro" |
| 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * When generating prototypes for Win32 on Unix, these lines make the syntax |
| 31 | * errors disappear. They do not need to be correct. |
| 32 | */ |
| 33 | #ifdef PROTO |
| 34 | #define WINAPI |
| 35 | #define WINBASEAPI |
| 36 | typedef int DWORD; |
| 37 | typedef int LPBOOL; |
| 38 | typedef int LPCSTR; |
| 39 | typedef int LPCWSTR; |
| 40 | typedef int LPSTR; |
| 41 | typedef int LPWSTR; |
| 42 | typedef int UINT; |
| 43 | #endif |
| 44 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Convert an UTF-8 string to UTF-16. |
| 47 | * "instr[inlen]" is the input. "inlen" is in bytes. |
| 48 | * When "outstr" is NULL only return the number of UTF-16 words produced. |
| 49 | * Otherwise "outstr" must be a buffer of sufficient size. |
| 50 | * Returns the number of UTF-16 words produced. |
| 51 | */ |
| 52 | int |
| 53 | utf8_to_utf16(char_u *instr, int inlen, short_u *outstr, int *unconvlenp) |
| 54 | { |
| 55 | int outlen = 0; |
| 56 | char_u *p = instr; |
| 57 | int todo = inlen; |
| 58 | int l; |
| 59 | int ch; |
| 60 | |
| 61 | while (todo > 0) |
| 62 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 63 | // Only convert if we have a complete sequence. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 64 | l = utf_ptr2len_len(p, todo); |
| 65 | if (l > todo) |
| 66 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 67 | // Return length of incomplete sequence. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 68 | if (unconvlenp != NULL) |
| 69 | *unconvlenp = todo; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | ch = utf_ptr2char(p); |
| 74 | if (ch >= 0x10000) |
| 75 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 76 | // non-BMP character, encoding with surrogate pairs |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 77 | ++outlen; |
| 78 | if (outstr != NULL) |
| 79 | { |
| 80 | *outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10); |
| 81 | *outstr++ = 0xDC00 | (ch & 0x3FF); |
| 82 | } |
| 83 | } |
| 84 | else if (outstr != NULL) |
| 85 | *outstr++ = ch; |
| 86 | ++outlen; |
| 87 | p += l; |
| 88 | todo -= l; |
| 89 | } |
| 90 | |
| 91 | return outlen; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Convert an UTF-16 string to UTF-8. |
| 96 | * The input is "instr[inlen]" with "inlen" in number of UTF-16 words. |
| 97 | * When "outstr" is NULL only return the required number of bytes. |
| 98 | * Otherwise "outstr" must be a buffer of sufficient size. |
| 99 | * Return the number of bytes produced. |
| 100 | */ |
| 101 | int |
| 102 | utf16_to_utf8(short_u *instr, int inlen, char_u *outstr) |
| 103 | { |
| 104 | int outlen = 0; |
| 105 | int todo = inlen; |
| 106 | short_u *p = instr; |
| 107 | int l; |
| 108 | int ch, ch2; |
| 109 | |
| 110 | while (todo > 0) |
| 111 | { |
| 112 | ch = *p; |
| 113 | if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1) |
| 114 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 115 | // surrogate pairs handling |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 116 | ch2 = p[1]; |
| 117 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) |
| 118 | { |
| 119 | ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000; |
| 120 | ++p; |
| 121 | --todo; |
| 122 | } |
| 123 | } |
| 124 | if (outstr != NULL) |
| 125 | { |
| 126 | l = utf_char2bytes(ch, outstr); |
| 127 | outstr += l; |
| 128 | } |
| 129 | else |
| 130 | l = utf_char2len(ch); |
| 131 | ++p; |
| 132 | outlen += l; |
| 133 | --todo; |
| 134 | } |
| 135 | |
| 136 | return outlen; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Call MultiByteToWideChar() and allocate memory for the result. |
| 141 | * Returns the result in "*out[*outlen]" with an extra zero appended. |
| 142 | * "outlen" is in words. |
| 143 | */ |
| 144 | void |
| 145 | MultiByteToWideChar_alloc(UINT cp, DWORD flags, |
| 146 | LPCSTR in, int inlen, |
| 147 | LPWSTR *out, int *outlen) |
| 148 | { |
| 149 | *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0); |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 150 | // Add one word to avoid a zero-length alloc(). |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 151 | *out = ALLOC_MULT(WCHAR, *outlen + 1); |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 152 | if (*out == NULL) |
| 153 | return; |
| 154 | |
| 155 | MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen); |
| 156 | (*out)[*outlen] = 0; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Call WideCharToMultiByte() and allocate memory for the result. |
| 161 | * Returns the result in "*out[*outlen]" with an extra NUL appended. |
| 162 | */ |
| 163 | void |
| 164 | WideCharToMultiByte_alloc(UINT cp, DWORD flags, |
| 165 | LPCWSTR in, int inlen, |
| 166 | LPSTR *out, int *outlen, |
| 167 | LPCSTR def, LPBOOL useddef) |
| 168 | { |
| 169 | *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef); |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 170 | // Add one byte to avoid a zero-length alloc(). |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 171 | *out = alloc(*outlen + 1); |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 172 | if (*out == NULL) |
| 173 | return; |
| 174 | WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef); |
| 175 | (*out)[*outlen] = 0; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 178 | |
| 179 | #ifdef FEAT_CLIPBOARD |
| 180 | /* |
| 181 | * Clipboard stuff, for cutting and pasting text to other windows. |
| 182 | */ |
| 183 | |
| 184 | void |
| 185 | win_clip_init(void) |
| 186 | { |
| 187 | clip_init(TRUE); |
| 188 | |
| 189 | /* |
| 190 | * Vim's own clipboard format recognises whether the text is char, line, |
| 191 | * or rectangular block. Only useful for copying between two Vims. |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 192 | * "Clipboard_T" was used for previous versions, using the first |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 193 | * character to specify MCHAR, MLINE or MBLOCK. |
| 194 | */ |
| 195 | clip_star.format = RegisterClipboardFormat("VimClipboard2"); |
| 196 | clip_star.format_raw = RegisterClipboardFormat("VimRawBytes"); |
| 197 | } |
| 198 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 199 | // Type used for the clipboard type of Vim's data. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 200 | typedef struct |
| 201 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 202 | int type; // MCHAR, MBLOCK or MLINE |
| 203 | int txtlen; // length of CF_TEXT in bytes |
| 204 | int ucslen; // length of CF_UNICODETEXT in words |
| 205 | int rawlen; // length of clip_star.format_raw, including encoding, |
| 206 | // excluding terminating NUL |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 207 | } VimClipType_t; |
| 208 | |
| 209 | /* |
| 210 | * Make vim the owner of the current selection. Return OK upon success. |
| 211 | */ |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 212 | int |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 213 | clip_mch_own_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 214 | { |
| 215 | /* |
| 216 | * Never actually own the clipboard. If another application sets the |
| 217 | * clipboard, we don't want to think that we still own it. |
| 218 | */ |
| 219 | return FAIL; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Make vim NOT the owner of the current selection. |
| 224 | */ |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 225 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 226 | clip_mch_lose_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 227 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 228 | // Nothing needs to be done here |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Copy "str[*size]" into allocated memory, changing CR-NL to NL. |
| 233 | * Return the allocated result and the size in "*size". |
| 234 | * Returns NULL when out of memory. |
| 235 | */ |
| 236 | static char_u * |
| 237 | crnl_to_nl(const char_u *str, int *size) |
| 238 | { |
| 239 | int pos = 0; |
| 240 | int str_len = *size; |
| 241 | char_u *ret; |
| 242 | char_u *retp; |
| 243 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 244 | // Avoid allocating zero bytes, it generates an error message. |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 245 | ret = alloc(str_len == 0 ? 1 : str_len); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 246 | if (ret != NULL) |
| 247 | { |
| 248 | retp = ret; |
| 249 | for (pos = 0; pos < str_len; ++pos) |
| 250 | { |
| 251 | if (str[pos] == '\r' && str[pos + 1] == '\n') |
| 252 | { |
| 253 | ++pos; |
| 254 | --(*size); |
| 255 | } |
| 256 | *retp++ = str[pos]; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Wait for another process to Close the Clipboard. |
| 265 | * Returns TRUE for success. |
| 266 | */ |
| 267 | static int |
| 268 | vim_open_clipboard(void) |
| 269 | { |
| 270 | int delay = 10; |
| 271 | |
| 272 | while (!OpenClipboard(NULL)) |
| 273 | { |
| 274 | if (delay > 500) |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 275 | return FALSE; // waited too long, give up |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 276 | Sleep(delay); |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 277 | delay *= 2; // wait for 10, 20, 40, 80, etc. msec |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 278 | } |
| 279 | return TRUE; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Get the current selection and put it in the clipboard register. |
| 284 | * |
| 285 | * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility. |
| 286 | * On NT/W95 the clipboard data is a fixed global memory object and |
| 287 | * so its handle = its pointer. |
| 288 | * On Win32s, however, co-operation with the Win16 system means that |
| 289 | * the clipboard data is moveable and its handle is not a pointer at all, |
| 290 | * so we can't just cast the return value of GetClipboardData to (char_u*). |
| 291 | * <VN> |
| 292 | */ |
| 293 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 294 | clip_mch_request_selection(Clipboard_T *cbd) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 295 | { |
| 296 | VimClipType_t metadata = { -1, -1, -1, -1 }; |
| 297 | HGLOBAL hMem = NULL; |
| 298 | char_u *str = NULL; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 299 | char_u *to_free = NULL; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 300 | HGLOBAL rawh = NULL; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 301 | int str_size = 0; |
| 302 | int maxlen; |
| 303 | size_t n; |
| 304 | |
| 305 | /* |
| 306 | * Don't pass GetActiveWindow() as an argument to OpenClipboard() because |
| 307 | * then we can't paste back into the same window for some reason - webb. |
| 308 | */ |
| 309 | if (!vim_open_clipboard()) |
| 310 | return; |
| 311 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 312 | // Check for vim's own clipboard format first. This only gets the type of |
| 313 | // the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 314 | if (IsClipboardFormatAvailable(cbd->format)) |
| 315 | { |
| 316 | VimClipType_t *meta_p; |
| 317 | HGLOBAL meta_h; |
| 318 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 319 | // We have metadata on the clipboard; try to get it. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 320 | if ((meta_h = GetClipboardData(cbd->format)) != NULL |
| 321 | && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL) |
| 322 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 323 | // The size of "VimClipType_t" changed, "rawlen" was added later. |
| 324 | // Only copy what is available for backwards compatibility. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 325 | n = sizeof(VimClipType_t); |
| 326 | if (GlobalSize(meta_h) < n) |
| 327 | n = GlobalSize(meta_h); |
| 328 | memcpy(&metadata, meta_p, n); |
| 329 | GlobalUnlock(meta_h); |
| 330 | } |
| 331 | } |
| 332 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 333 | // Check for Vim's raw clipboard format first. This is used without |
| 334 | // conversion, but only if 'encoding' matches. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 335 | if (IsClipboardFormatAvailable(cbd->format_raw) |
| 336 | && metadata.rawlen > (int)STRLEN(p_enc)) |
| 337 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 338 | // We have raw data on the clipboard; try to get it. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 339 | if ((rawh = GetClipboardData(cbd->format_raw)) != NULL) |
| 340 | { |
| 341 | char_u *rawp; |
| 342 | |
| 343 | rawp = (char_u *)GlobalLock(rawh); |
| 344 | if (rawp != NULL && STRCMP(p_enc, rawp) == 0) |
| 345 | { |
| 346 | n = STRLEN(p_enc) + 1; |
| 347 | str = rawp + n; |
| 348 | str_size = (int)(metadata.rawlen - n); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | GlobalUnlock(rawh); |
| 353 | rawh = NULL; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | if (str == NULL) |
| 358 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 359 | // Try to get the clipboard in Unicode if it's not an empty string. |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 360 | if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 361 | { |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 362 | HGLOBAL hMemW; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 363 | |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 364 | if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 365 | { |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 366 | WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW); |
| 367 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 368 | // Use the length of our metadata if possible, but limit it to |
| 369 | // the GlobalSize() for safety. |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 370 | maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR)); |
| 371 | if (metadata.ucslen >= 0) |
| 372 | { |
| 373 | if (metadata.ucslen > maxlen) |
| 374 | str_size = maxlen; |
| 375 | else |
| 376 | str_size = metadata.ucslen; |
| 377 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 378 | else |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 379 | { |
| 380 | for (str_size = 0; str_size < maxlen; ++str_size) |
| 381 | if (hMemWstr[str_size] == NUL) |
| 382 | break; |
| 383 | } |
| 384 | to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size); |
| 385 | GlobalUnlock(hMemW); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 386 | } |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 387 | } |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 388 | // Get the clipboard in the Active codepage. |
Bram Moolenaar | c69efcb | 2019-02-27 14:12:01 +0100 | [diff] [blame] | 389 | else if (IsClipboardFormatAvailable(CF_TEXT)) |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 390 | { |
| 391 | if ((hMem = GetClipboardData(CF_TEXT)) != NULL) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 392 | { |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 393 | str = (char_u *)GlobalLock(hMem); |
| 394 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 395 | // The length is either what our metadata says or the strlen(). |
| 396 | // But limit it to the GlobalSize() for safety. |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 397 | maxlen = (int)GlobalSize(hMem); |
| 398 | if (metadata.txtlen >= 0) |
| 399 | { |
| 400 | if (metadata.txtlen > maxlen) |
| 401 | str_size = maxlen; |
| 402 | else |
| 403 | str_size = metadata.txtlen; |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | for (str_size = 0; str_size < maxlen; ++str_size) |
| 408 | if (str[str_size] == NUL) |
| 409 | break; |
| 410 | } |
| 411 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 412 | // The text is in the active codepage. Convert to |
| 413 | // 'encoding', going through UTF-16. |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 414 | acp_to_enc(str, str_size, &to_free, &maxlen); |
| 415 | if (to_free != NULL) |
| 416 | { |
| 417 | str_size = maxlen; |
| 418 | str = to_free; |
| 419 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 420 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 423 | |
Bram Moolenaar | 25fd267 | 2020-06-22 20:30:27 +0200 | [diff] [blame] | 424 | if (str != NULL && metadata.txtlen != 0) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 425 | { |
| 426 | char_u *temp_clipboard; |
| 427 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 428 | // If the type is not known detect it. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 429 | if (metadata.type == -1) |
| 430 | metadata.type = MAUTO; |
| 431 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 432 | // Translate <CR><NL> into <NL>. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 433 | temp_clipboard = crnl_to_nl(str, &str_size); |
| 434 | if (temp_clipboard != NULL) |
| 435 | { |
| 436 | clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd); |
| 437 | vim_free(temp_clipboard); |
| 438 | } |
| 439 | } |
| 440 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 441 | // unlock the global object |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 442 | if (hMem != NULL) |
| 443 | GlobalUnlock(hMem); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 444 | if (rawh != NULL) |
| 445 | GlobalUnlock(rawh); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 446 | CloseClipboard(); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 447 | vim_free(to_free); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Send the current selection to the clipboard. |
| 452 | */ |
| 453 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 454 | clip_mch_set_selection(Clipboard_T *cbd) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 455 | { |
| 456 | char_u *str = NULL; |
| 457 | VimClipType_t metadata; |
| 458 | long_u txtlen; |
| 459 | HGLOBAL hMemRaw = NULL; |
| 460 | HGLOBAL hMem = NULL; |
| 461 | HGLOBAL hMemVim = NULL; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 462 | HGLOBAL hMemW = NULL; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 463 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 464 | // If the '*' register isn't already filled in, fill it in now |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 465 | cbd->owned = TRUE; |
| 466 | clip_get_selection(cbd); |
| 467 | cbd->owned = FALSE; |
| 468 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 469 | // Get the text to be put on the clipboard, with CR-LF. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 470 | metadata.type = clip_convert_selection(&str, &txtlen, cbd); |
| 471 | if (metadata.type < 0) |
| 472 | return; |
| 473 | metadata.txtlen = (int)txtlen; |
| 474 | metadata.ucslen = 0; |
| 475 | metadata.rawlen = 0; |
| 476 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 477 | // Always set the raw bytes: 'encoding', NUL and the text. This is used |
| 478 | // when copy/paste from/to Vim with the same 'encoding', so that illegal |
| 479 | // bytes can also be copied and no conversion is needed. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 480 | { |
| 481 | LPSTR lpszMemRaw; |
| 482 | |
| 483 | metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1); |
| 484 | hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, |
| 485 | metadata.rawlen + 1); |
| 486 | lpszMemRaw = (LPSTR)GlobalLock(hMemRaw); |
| 487 | if (lpszMemRaw != NULL) |
| 488 | { |
| 489 | STRCPY(lpszMemRaw, p_enc); |
| 490 | memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1); |
| 491 | GlobalUnlock(hMemRaw); |
| 492 | } |
| 493 | else |
| 494 | metadata.rawlen = 0; |
| 495 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 496 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 497 | { |
| 498 | WCHAR *out; |
| 499 | int len = metadata.txtlen; |
| 500 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 501 | // Convert the text to UTF-16. This is put on the clipboard as |
| 502 | // CF_UNICODETEXT. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 503 | out = (WCHAR *)enc_to_utf16(str, &len); |
| 504 | if (out != NULL) |
| 505 | { |
| 506 | WCHAR *lpszMemW; |
| 507 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 508 | // Convert the text for CF_TEXT to Active codepage. Otherwise it's |
| 509 | // p_enc, which has no relation to the Active codepage. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 510 | metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len, |
| 511 | NULL, 0, 0, 0); |
| 512 | vim_free(str); |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 513 | str = alloc(metadata.txtlen == 0 ? 1 : metadata.txtlen); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 514 | if (str == NULL) |
| 515 | { |
| 516 | vim_free(out); |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 517 | return; // out of memory |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 518 | } |
| 519 | WideCharToMultiByte(GetACP(), 0, out, len, |
Bram Moolenaar | 2982e70 | 2013-07-01 21:08:48 +0200 | [diff] [blame] | 520 | (LPSTR)str, metadata.txtlen, 0, 0); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 521 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 522 | // Allocate memory for the UTF-16 text, add one NUL word to |
| 523 | // terminate the string. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 524 | hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, |
| 525 | (len + 1) * sizeof(WCHAR)); |
| 526 | lpszMemW = (WCHAR *)GlobalLock(hMemW); |
| 527 | if (lpszMemW != NULL) |
| 528 | { |
| 529 | memcpy(lpszMemW, out, len * sizeof(WCHAR)); |
| 530 | lpszMemW[len] = NUL; |
| 531 | GlobalUnlock(hMemW); |
| 532 | } |
| 533 | vim_free(out); |
| 534 | metadata.ucslen = len; |
| 535 | } |
| 536 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 537 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 538 | // Allocate memory for the text, add one NUL byte to terminate the string. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 539 | hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1); |
| 540 | { |
| 541 | LPSTR lpszMem = (LPSTR)GlobalLock(hMem); |
| 542 | |
| 543 | if (lpszMem) |
| 544 | { |
Bram Moolenaar | 25fd267 | 2020-06-22 20:30:27 +0200 | [diff] [blame] | 545 | mch_memmove((char_u *)lpszMem, str, metadata.txtlen); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 546 | GlobalUnlock(hMem); |
| 547 | } |
| 548 | } |
| 549 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 550 | // Set up metadata: |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 551 | { |
| 552 | VimClipType_t *lpszMemVim = NULL; |
| 553 | |
| 554 | hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, |
| 555 | sizeof(VimClipType_t)); |
| 556 | lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim); |
| 557 | memcpy(lpszMemVim, &metadata, sizeof(metadata)); |
| 558 | GlobalUnlock(hMemVim); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Open the clipboard, clear it and put our text on it. |
| 563 | * Always set our Vim format. Put Unicode and plain text on it. |
| 564 | * |
| 565 | * Don't pass GetActiveWindow() as an argument to OpenClipboard() |
| 566 | * because then we can't paste back into the same window for some |
| 567 | * reason - webb. |
| 568 | */ |
| 569 | if (vim_open_clipboard()) |
| 570 | { |
| 571 | if (EmptyClipboard()) |
| 572 | { |
| 573 | SetClipboardData(cbd->format, hMemVim); |
| 574 | hMemVim = 0; |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 575 | if (hMemW != NULL) |
| 576 | { |
| 577 | if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL) |
| 578 | hMemW = NULL; |
| 579 | } |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 580 | // Always use CF_TEXT. On Win98 Notepad won't obtain the |
| 581 | // CF_UNICODETEXT text, only CF_TEXT. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 582 | SetClipboardData(CF_TEXT, hMem); |
| 583 | hMem = 0; |
| 584 | } |
| 585 | CloseClipboard(); |
| 586 | } |
| 587 | |
| 588 | vim_free(str); |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 589 | // Free any allocations we didn't give to the clipboard: |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 590 | if (hMemRaw) |
| 591 | GlobalFree(hMemRaw); |
| 592 | if (hMem) |
| 593 | GlobalFree(hMem); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 594 | if (hMemW) |
| 595 | GlobalFree(hMemW); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 596 | if (hMemVim) |
| 597 | GlobalFree(hMemVim); |
| 598 | } |
| 599 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 600 | #endif // FEAT_CLIPBOARD |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 601 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 602 | /* |
| 603 | * Note: the following two functions are only guaranteed to work when using |
| 604 | * valid MS-Windows codepages or when iconv() is available. |
| 605 | */ |
| 606 | |
| 607 | /* |
| 608 | * Convert "str" from 'encoding' to UTF-16. |
| 609 | * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen(). |
| 610 | * Output is returned as an allocated string. "*lenp" is set to the length of |
| 611 | * the result. A trailing NUL is always added. |
| 612 | * Returns NULL when out of memory. |
| 613 | */ |
| 614 | short_u * |
| 615 | enc_to_utf16(char_u *str, int *lenp) |
| 616 | { |
| 617 | vimconv_T conv; |
| 618 | WCHAR *ret; |
| 619 | char_u *allocbuf = NULL; |
| 620 | int len_loc; |
| 621 | int length; |
| 622 | |
| 623 | if (lenp == NULL) |
| 624 | { |
| 625 | len_loc = (int)STRLEN(str) + 1; |
| 626 | lenp = &len_loc; |
| 627 | } |
| 628 | |
| 629 | if (enc_codepage > 0) |
| 630 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 631 | // We can do any CP### -> UTF-16 in one pass, and we can do it |
| 632 | // without iconv() (convert_* may need iconv). |
Bram Moolenaar | 2982e70 | 2013-07-01 21:08:48 +0200 | [diff] [blame] | 633 | MultiByteToWideChar_alloc(enc_codepage, 0, (LPCSTR)str, *lenp, |
| 634 | &ret, &length); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 635 | } |
| 636 | else |
| 637 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 638 | // Use "latin1" by default, we might be called before we have p_enc |
| 639 | // set up. Convert to utf-8 first, works better with iconv(). Does |
| 640 | // nothing if 'encoding' is "utf-8". |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 641 | conv.vc_type = CONV_NONE; |
| 642 | if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1", |
| 643 | (char_u *)"utf-8") == FAIL) |
| 644 | return NULL; |
| 645 | if (conv.vc_type != CONV_NONE) |
| 646 | { |
| 647 | str = allocbuf = string_convert(&conv, str, lenp); |
| 648 | if (str == NULL) |
| 649 | return NULL; |
| 650 | } |
| 651 | convert_setup(&conv, NULL, NULL); |
| 652 | |
| 653 | length = utf8_to_utf16(str, *lenp, NULL, NULL); |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 654 | ret = ALLOC_MULT(WCHAR, length + 1); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 655 | if (ret != NULL) |
| 656 | { |
| 657 | utf8_to_utf16(str, *lenp, (short_u *)ret, NULL); |
| 658 | ret[length] = 0; |
| 659 | } |
| 660 | |
| 661 | vim_free(allocbuf); |
| 662 | } |
| 663 | |
| 664 | *lenp = length; |
| 665 | return (short_u *)ret; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Convert an UTF-16 string to 'encoding'. |
| 670 | * Input in "str" with length (counted in wide characters) "*lenp". When |
| 671 | * "lenp" is NULL, use wcslen(). |
| 672 | * Output is returned as an allocated string. If "*lenp" is not NULL it is |
| 673 | * set to the length of the result. |
| 674 | * Returns NULL when out of memory. |
| 675 | */ |
| 676 | char_u * |
| 677 | utf16_to_enc(short_u *str, int *lenp) |
| 678 | { |
| 679 | vimconv_T conv; |
| 680 | char_u *utf8_str = NULL, *enc_str = NULL; |
| 681 | int len_loc; |
| 682 | |
| 683 | if (lenp == NULL) |
| 684 | { |
| 685 | len_loc = (int)wcslen(str) + 1; |
| 686 | lenp = &len_loc; |
| 687 | } |
| 688 | |
| 689 | if (enc_codepage > 0) |
| 690 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 691 | // We can do any UTF-16 -> CP### in one pass. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 692 | int length; |
| 693 | |
| 694 | WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp, |
| 695 | (LPSTR *)&enc_str, &length, 0, 0); |
| 696 | *lenp = length; |
| 697 | return enc_str; |
| 698 | } |
| 699 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 700 | // Avoid allocating zero bytes, it generates an error message. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 701 | utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL)); |
| 702 | if (utf8_str != NULL) |
| 703 | { |
| 704 | *lenp = utf16_to_utf8(str, *lenp, utf8_str); |
| 705 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 706 | // We might be called before we have p_enc set up. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 707 | conv.vc_type = CONV_NONE; |
| 708 | convert_setup(&conv, (char_u *)"utf-8", |
| 709 | p_enc? p_enc: (char_u *)"latin1"); |
| 710 | if (conv.vc_type == CONV_NONE) |
| 711 | { |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 712 | // p_enc is utf-8, so we're done. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 713 | enc_str = utf8_str; |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | enc_str = string_convert(&conv, utf8_str, lenp); |
| 718 | vim_free(utf8_str); |
| 719 | } |
| 720 | |
| 721 | convert_setup(&conv, NULL, NULL); |
| 722 | } |
| 723 | |
| 724 | return enc_str; |
| 725 | } |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 726 | |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 727 | /* |
| 728 | * Convert from the active codepage to 'encoding'. |
| 729 | * Input is "str[str_size]". |
Bram Moolenaar | 88456cd | 2022-11-18 22:14:09 +0000 | [diff] [blame] | 730 | * The result is in allocated memory: "out[outlen]". "outlen" includes the |
| 731 | * terminating NUL. |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 732 | */ |
| 733 | void |
Bram Moolenaar | b638a7b | 2016-01-30 21:29:58 +0100 | [diff] [blame] | 734 | acp_to_enc( |
| 735 | char_u *str, |
| 736 | int str_size, |
| 737 | char_u **out, |
| 738 | int *outlen) |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 739 | |
| 740 | { |
| 741 | LPWSTR widestr; |
| 742 | |
Bram Moolenaar | 2982e70 | 2013-07-01 21:08:48 +0200 | [diff] [blame] | 743 | MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)str, str_size, |
| 744 | &widestr, outlen); |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 745 | if (widestr == NULL) |
| 746 | return; |
| 747 | ++*outlen; // Include the 0 after the string |
| 748 | *out = utf16_to_enc((short_u *)widestr, outlen); |
| 749 | vim_free(widestr); |
Bram Moolenaar | 693e40c | 2013-02-26 14:56:42 +0100 | [diff] [blame] | 750 | } |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 751 | |
| 752 | /* |
| 753 | * Convert from 'encoding' to the active codepage. |
| 754 | * Input is "str[str_size]". |
| 755 | * The result is in allocated memory: "out[outlen]". With terminating NUL. |
| 756 | */ |
| 757 | void |
Bram Moolenaar | b638a7b | 2016-01-30 21:29:58 +0100 | [diff] [blame] | 758 | enc_to_acp( |
| 759 | char_u *str, |
| 760 | int str_size, |
| 761 | char_u **out, |
| 762 | int *outlen) |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 763 | |
| 764 | { |
| 765 | LPWSTR widestr; |
| 766 | int len = str_size; |
| 767 | |
| 768 | widestr = (WCHAR *)enc_to_utf16(str, &len); |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 769 | if (widestr == NULL) |
| 770 | return; |
| 771 | WideCharToMultiByte_alloc(GetACP(), 0, widestr, len, |
| 772 | (LPSTR *)out, outlen, 0, 0); |
| 773 | vim_free(widestr); |
Bram Moolenaar | b1692e2 | 2014-03-12 19:24:37 +0100 | [diff] [blame] | 774 | } |