blob: 8f54c6b5f442fe5fc21e7b65f56a4b335b0a6fc0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002 *
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 Moolenaar1266d672017-02-01 13:43:36 +010013 * Routines for Win32 clipboard handling.
Bram Moolenaar693e40c2013-02-26 14:56:42 +010014 * Also used by Cygwin, using os_unix.c.
15 */
16
Bram Moolenaar693e40c2013-02-26 14:56:42 +010017#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 Moolenaar693e40c2013-02-26 14:56:42 +010024# 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
36typedef int DWORD;
37typedef int LPBOOL;
38typedef int LPCSTR;
39typedef int LPCWSTR;
40typedef int LPSTR;
41typedef int LPWSTR;
42typedef int UINT;
43#endif
44
Bram Moolenaar693e40c2013-02-26 14:56:42 +010045/*
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
53utf8_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 Moolenaare38eab22019-12-05 21:50:01 +010063 // Only convert if we have a complete sequence.
Bram Moolenaar693e40c2013-02-26 14:56:42 +010064 l = utf_ptr2len_len(p, todo);
65 if (l > todo)
66 {
Bram Moolenaare38eab22019-12-05 21:50:01 +010067 // Return length of incomplete sequence.
Bram Moolenaar693e40c2013-02-26 14:56:42 +010068 if (unconvlenp != NULL)
69 *unconvlenp = todo;
70 break;
71 }
72
73 ch = utf_ptr2char(p);
74 if (ch >= 0x10000)
75 {
Bram Moolenaare38eab22019-12-05 21:50:01 +010076 // non-BMP character, encoding with surrogate pairs
Bram Moolenaar693e40c2013-02-26 14:56:42 +010077 ++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
102utf16_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 Moolenaare38eab22019-12-05 21:50:01 +0100115 // surrogate pairs handling
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100116 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
145MultiByteToWideChar_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 Pelleaf4a61a2021-12-27 17:21:41 +0000150 // Add one word to avoid a zero-length alloc().
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200151 *out = ALLOC_MULT(WCHAR, *outlen + 1);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000152 if (*out == NULL)
153 return;
154
155 MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
156 (*out)[*outlen] = 0;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100157}
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
164WideCharToMultiByte_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 Pelleaf4a61a2021-12-27 17:21:41 +0000170 // Add one byte to avoid a zero-length alloc().
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200171 *out = alloc(*outlen + 1);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000172 if (*out == NULL)
173 return;
174 WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
175 (*out)[*outlen] = 0;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100176}
177
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100178
179#ifdef FEAT_CLIPBOARD
180/*
181 * Clipboard stuff, for cutting and pasting text to other windows.
182 */
183
184 void
185win_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 Moolenaar0554fa42019-06-14 21:36:54 +0200192 * "Clipboard_T" was used for previous versions, using the first
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100193 * character to specify MCHAR, MLINE or MBLOCK.
194 */
195 clip_star.format = RegisterClipboardFormat("VimClipboard2");
196 clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
197}
198
Bram Moolenaare38eab22019-12-05 21:50:01 +0100199// Type used for the clipboard type of Vim's data.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100200typedef struct
201{
Bram Moolenaare38eab22019-12-05 21:50:01 +0100202 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 Moolenaar693e40c2013-02-26 14:56:42 +0100207} VimClipType_t;
208
209/*
210 * Make vim the owner of the current selection. Return OK upon success.
211 */
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100212 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200213clip_mch_own_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100214{
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 Moolenaar693e40c2013-02-26 14:56:42 +0100225 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200226clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100227{
Bram Moolenaare38eab22019-12-05 21:50:01 +0100228 // Nothing needs to be done here
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100229}
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 *
237crnl_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 Moolenaare38eab22019-12-05 21:50:01 +0100244 // Avoid allocating zero bytes, it generates an error message.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200245 ret = alloc(str_len == 0 ? 1 : str_len);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100246 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
268vim_open_clipboard(void)
269{
270 int delay = 10;
271
272 while (!OpenClipboard(NULL))
273 {
274 if (delay > 500)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100275 return FALSE; // waited too long, give up
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100276 Sleep(delay);
Bram Moolenaare38eab22019-12-05 21:50:01 +0100277 delay *= 2; // wait for 10, 20, 40, 80, etc. msec
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100278 }
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 Moolenaar0554fa42019-06-14 21:36:54 +0200294clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100295{
296 VimClipType_t metadata = { -1, -1, -1, -1 };
297 HGLOBAL hMem = NULL;
298 char_u *str = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100299 char_u *to_free = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100300 HGLOBAL rawh = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100301 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 Moolenaare38eab22019-12-05 21:50:01 +0100312 // 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 Moolenaar693e40c2013-02-26 14:56:42 +0100314 if (IsClipboardFormatAvailable(cbd->format))
315 {
316 VimClipType_t *meta_p;
317 HGLOBAL meta_h;
318
Bram Moolenaare38eab22019-12-05 21:50:01 +0100319 // We have metadata on the clipboard; try to get it.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100320 if ((meta_h = GetClipboardData(cbd->format)) != NULL
321 && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100323 // The size of "VimClipType_t" changed, "rawlen" was added later.
324 // Only copy what is available for backwards compatibility.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100325 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 Moolenaare38eab22019-12-05 21:50:01 +0100333 // Check for Vim's raw clipboard format first. This is used without
334 // conversion, but only if 'encoding' matches.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100335 if (IsClipboardFormatAvailable(cbd->format_raw)
336 && metadata.rawlen > (int)STRLEN(p_enc))
337 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100338 // We have raw data on the clipboard; try to get it.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100339 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 Moolenaare38eab22019-12-05 21:50:01 +0100359 // Try to get the clipboard in Unicode if it's not an empty string.
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100360 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100361 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100362 HGLOBAL hMemW;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100363
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100364 if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100365 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100366 WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
367
Bram Moolenaare38eab22019-12-05 21:50:01 +0100368 // Use the length of our metadata if possible, but limit it to
369 // the GlobalSize() for safety.
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100370 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 Moolenaar693e40c2013-02-26 14:56:42 +0100378 else
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100379 {
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 Moolenaar693e40c2013-02-26 14:56:42 +0100386 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100387 }
Bram Moolenaare38eab22019-12-05 21:50:01 +0100388 // Get the clipboard in the Active codepage.
Bram Moolenaarc69efcb2019-02-27 14:12:01 +0100389 else if (IsClipboardFormatAvailable(CF_TEXT))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100390 {
391 if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100392 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100393 str = (char_u *)GlobalLock(hMem);
394
Bram Moolenaare38eab22019-12-05 21:50:01 +0100395 // The length is either what our metadata says or the strlen().
396 // But limit it to the GlobalSize() for safety.
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100397 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 Moolenaare38eab22019-12-05 21:50:01 +0100412 // The text is in the active codepage. Convert to
413 // 'encoding', going through UTF-16.
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100414 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 Moolenaar693e40c2013-02-26 14:56:42 +0100420 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100421 }
422 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100423
Bram Moolenaar25fd2672020-06-22 20:30:27 +0200424 if (str != NULL && metadata.txtlen != 0)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100425 {
426 char_u *temp_clipboard;
427
Bram Moolenaare38eab22019-12-05 21:50:01 +0100428 // If the type is not known detect it.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100429 if (metadata.type == -1)
430 metadata.type = MAUTO;
431
Bram Moolenaare38eab22019-12-05 21:50:01 +0100432 // Translate <CR><NL> into <NL>.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100433 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 Moolenaare38eab22019-12-05 21:50:01 +0100441 // unlock the global object
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100442 if (hMem != NULL)
443 GlobalUnlock(hMem);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100444 if (rawh != NULL)
445 GlobalUnlock(rawh);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100446 CloseClipboard();
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100447 vim_free(to_free);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100448}
449
450/*
451 * Send the current selection to the clipboard.
452 */
453 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200454clip_mch_set_selection(Clipboard_T *cbd)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100455{
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 Moolenaar693e40c2013-02-26 14:56:42 +0100462 HGLOBAL hMemW = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100463
Bram Moolenaare38eab22019-12-05 21:50:01 +0100464 // If the '*' register isn't already filled in, fill it in now
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100465 cbd->owned = TRUE;
466 clip_get_selection(cbd);
467 cbd->owned = FALSE;
468
Bram Moolenaare38eab22019-12-05 21:50:01 +0100469 // Get the text to be put on the clipboard, with CR-LF.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100470 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 Moolenaare38eab22019-12-05 21:50:01 +0100477 // 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 Moolenaar693e40c2013-02-26 14:56:42 +0100480 {
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 Moolenaar693e40c2013-02-26 14:56:42 +0100496
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100497 {
498 WCHAR *out;
499 int len = metadata.txtlen;
500
Bram Moolenaare38eab22019-12-05 21:50:01 +0100501 // Convert the text to UTF-16. This is put on the clipboard as
502 // CF_UNICODETEXT.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100503 out = (WCHAR *)enc_to_utf16(str, &len);
504 if (out != NULL)
505 {
506 WCHAR *lpszMemW;
507
Bram Moolenaare38eab22019-12-05 21:50:01 +0100508 // Convert the text for CF_TEXT to Active codepage. Otherwise it's
509 // p_enc, which has no relation to the Active codepage.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100510 metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
511 NULL, 0, 0, 0);
512 vim_free(str);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200513 str = alloc(metadata.txtlen == 0 ? 1 : metadata.txtlen);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100514 if (str == NULL)
515 {
516 vim_free(out);
Bram Moolenaare38eab22019-12-05 21:50:01 +0100517 return; // out of memory
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100518 }
519 WideCharToMultiByte(GetACP(), 0, out, len,
Bram Moolenaar2982e702013-07-01 21:08:48 +0200520 (LPSTR)str, metadata.txtlen, 0, 0);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100521
Bram Moolenaare38eab22019-12-05 21:50:01 +0100522 // Allocate memory for the UTF-16 text, add one NUL word to
523 // terminate the string.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100524 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 Moolenaar693e40c2013-02-26 14:56:42 +0100537
Bram Moolenaare38eab22019-12-05 21:50:01 +0100538 // Allocate memory for the text, add one NUL byte to terminate the string.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100539 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
540 {
541 LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
542
543 if (lpszMem)
544 {
Bram Moolenaar25fd2672020-06-22 20:30:27 +0200545 mch_memmove((char_u *)lpszMem, str, metadata.txtlen);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100546 GlobalUnlock(hMem);
547 }
548 }
549
Bram Moolenaare38eab22019-12-05 21:50:01 +0100550 // Set up metadata:
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100551 {
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 Moolenaar693e40c2013-02-26 14:56:42 +0100575 if (hMemW != NULL)
576 {
577 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
578 hMemW = NULL;
579 }
Bram Moolenaare38eab22019-12-05 21:50:01 +0100580 // Always use CF_TEXT. On Win98 Notepad won't obtain the
581 // CF_UNICODETEXT text, only CF_TEXT.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100582 SetClipboardData(CF_TEXT, hMem);
583 hMem = 0;
584 }
585 CloseClipboard();
586 }
587
588 vim_free(str);
Bram Moolenaare38eab22019-12-05 21:50:01 +0100589 // Free any allocations we didn't give to the clipboard:
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100590 if (hMemRaw)
591 GlobalFree(hMemRaw);
592 if (hMem)
593 GlobalFree(hMem);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100594 if (hMemW)
595 GlobalFree(hMemW);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100596 if (hMemVim)
597 GlobalFree(hMemVim);
598}
599
Bram Moolenaare38eab22019-12-05 21:50:01 +0100600#endif // FEAT_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100601
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100602/*
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 *
615enc_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 Moolenaare38eab22019-12-05 21:50:01 +0100631 // We can do any CP### -> UTF-16 in one pass, and we can do it
632 // without iconv() (convert_* may need iconv).
Bram Moolenaar2982e702013-07-01 21:08:48 +0200633 MultiByteToWideChar_alloc(enc_codepage, 0, (LPCSTR)str, *lenp,
634 &ret, &length);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100635 }
636 else
637 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100638 // 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 Moolenaar693e40c2013-02-26 14:56:42 +0100641 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 Moolenaarc799fe22019-05-28 23:08:19 +0200654 ret = ALLOC_MULT(WCHAR, length + 1);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100655 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 *
677utf16_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 Moolenaare38eab22019-12-05 21:50:01 +0100691 // We can do any UTF-16 -> CP### in one pass.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100692 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 Moolenaare38eab22019-12-05 21:50:01 +0100700 // Avoid allocating zero bytes, it generates an error message.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100701 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 Moolenaare38eab22019-12-05 21:50:01 +0100706 // We might be called before we have p_enc set up.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100707 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 Moolenaare38eab22019-12-05 21:50:01 +0100712 // p_enc is utf-8, so we're done.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100713 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 Moolenaar693e40c2013-02-26 14:56:42 +0100726
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100727/*
728 * Convert from the active codepage to 'encoding'.
729 * Input is "str[str_size]".
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000730 * The result is in allocated memory: "out[outlen]". "outlen" includes the
731 * terminating NUL.
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100732 */
733 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100734acp_to_enc(
735 char_u *str,
736 int str_size,
737 char_u **out,
738 int *outlen)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100739
740{
741 LPWSTR widestr;
742
Bram Moolenaar2982e702013-07-01 21:08:48 +0200743 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)str, str_size,
744 &widestr, outlen);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000745 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 Moolenaar693e40c2013-02-26 14:56:42 +0100750}
Bram Moolenaarb1692e22014-03-12 19:24:37 +0100751
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 Moolenaarb638a7b2016-01-30 21:29:58 +0100758enc_to_acp(
759 char_u *str,
760 int str_size,
761 char_u **out,
762 int *outlen)
Bram Moolenaarb1692e22014-03-12 19:24:37 +0100763
764{
765 LPWSTR widestr;
766 int len = str_size;
767
768 widestr = (WCHAR *)enc_to_utf16(str, &len);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000769 if (widestr == NULL)
770 return;
771 WideCharToMultiByte_alloc(GetACP(), 0, widestr, len,
772 (LPSTR *)out, outlen, 0, 0);
773 vim_free(widestr);
Bram Moolenaarb1692e22014-03-12 19:24:37 +0100774}