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