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