Mercurial > vim
annotate src/winclip.c @ 17353:c8a1757026ed
Added tag v8.1.1675 for changeset b2616a8be8a61e775db62542bfd737f28763ca46
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 13 Jul 2019 13:15:06 +0200 |
parents | 3147c7c2e86b |
children | 15539899a112 |
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(). */ | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
152 *out = ALLOC_MULT(WCHAR, *outlen + 1); |
4168 | 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(). */ | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
172 *out = alloc(*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. | |
17063
3147c7c2e86b
patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
194 * "Clipboard_T" was used for previous versions, using the first |
4168 | 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 | |
17063
3147c7c2e86b
patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
215 clip_mch_own_selection(Clipboard_T *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 | |
17063
3147c7c2e86b
patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
228 clip_mch_lose_selection(Clipboard_T *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. */ | |
16768
695d9ef00b03
patch 8.1.1386: unessesary type casts for lalloc()
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
247 ret = alloc(str_len == 0 ? 1 : str_len); |
4168 | 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 | |
17063
3147c7c2e86b
patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
296 clip_mch_request_selection(Clipboard_T *cbd) |
4168 | 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 | |
17063
3147c7c2e86b
patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
456 clip_mch_set_selection(Clipboard_T *cbd) |
4168 | 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); | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
515 str = alloc(metadata.txtlen == 0 ? 1 : metadata.txtlen); |
4168 | 516 if (str == NULL) |
517 { | |
518 vim_free(out); | |
519 return; /* out of memory */ | |
520 } | |
521 WideCharToMultiByte(GetACP(), 0, out, len, | |
5082
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
522 (LPSTR)str, metadata.txtlen, 0, 0); |
4168 | 523 |
524 /* Allocate memory for the UTF-16 text, add one NUL word to | |
525 * terminate the string. */ | |
526 hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, | |
527 (len + 1) * sizeof(WCHAR)); | |
528 lpszMemW = (WCHAR *)GlobalLock(hMemW); | |
529 if (lpszMemW != NULL) | |
530 { | |
531 memcpy(lpszMemW, out, len * sizeof(WCHAR)); | |
532 lpszMemW[len] = NUL; | |
533 GlobalUnlock(hMemW); | |
534 } | |
535 vim_free(out); | |
536 metadata.ucslen = len; | |
537 } | |
538 } | |
539 | |
540 /* Allocate memory for the text, add one NUL byte to terminate the string. | |
541 */ | |
542 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1); | |
543 { | |
544 LPSTR lpszMem = (LPSTR)GlobalLock(hMem); | |
545 | |
546 if (lpszMem) | |
547 { | |
5082
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
548 vim_strncpy((char_u *)lpszMem, str, metadata.txtlen); |
4168 | 549 GlobalUnlock(hMem); |
550 } | |
551 } | |
552 | |
553 /* Set up metadata: */ | |
554 { | |
555 VimClipType_t *lpszMemVim = NULL; | |
556 | |
557 hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, | |
558 sizeof(VimClipType_t)); | |
559 lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim); | |
560 memcpy(lpszMemVim, &metadata, sizeof(metadata)); | |
561 GlobalUnlock(hMemVim); | |
562 } | |
563 | |
564 /* | |
565 * Open the clipboard, clear it and put our text on it. | |
566 * Always set our Vim format. Put Unicode and plain text on it. | |
567 * | |
568 * Don't pass GetActiveWindow() as an argument to OpenClipboard() | |
569 * because then we can't paste back into the same window for some | |
570 * reason - webb. | |
571 */ | |
572 if (vim_open_clipboard()) | |
573 { | |
574 if (EmptyClipboard()) | |
575 { | |
576 SetClipboardData(cbd->format, hMemVim); | |
577 hMemVim = 0; | |
578 if (hMemW != NULL) | |
579 { | |
580 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL) | |
581 hMemW = NULL; | |
582 } | |
583 /* Always use CF_TEXT. On Win98 Notepad won't obtain the | |
584 * CF_UNICODETEXT text, only CF_TEXT. */ | |
585 SetClipboardData(CF_TEXT, hMem); | |
586 hMem = 0; | |
587 } | |
588 CloseClipboard(); | |
589 } | |
590 | |
591 vim_free(str); | |
592 /* Free any allocations we didn't give to the clipboard: */ | |
593 if (hMemRaw) | |
594 GlobalFree(hMemRaw); | |
595 if (hMem) | |
596 GlobalFree(hMem); | |
597 if (hMemW) | |
598 GlobalFree(hMemW); | |
599 if (hMemVim) | |
600 GlobalFree(hMemVim); | |
601 } | |
602 | |
603 #endif /* FEAT_CLIPBOARD */ | |
604 | |
605 /* | |
606 * Note: the following two functions are only guaranteed to work when using | |
607 * valid MS-Windows codepages or when iconv() is available. | |
608 */ | |
609 | |
610 /* | |
611 * Convert "str" from 'encoding' to UTF-16. | |
612 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen(). | |
613 * Output is returned as an allocated string. "*lenp" is set to the length of | |
614 * the result. A trailing NUL is always added. | |
615 * Returns NULL when out of memory. | |
616 */ | |
617 short_u * | |
618 enc_to_utf16(char_u *str, int *lenp) | |
619 { | |
620 vimconv_T conv; | |
621 WCHAR *ret; | |
622 char_u *allocbuf = NULL; | |
623 int len_loc; | |
624 int length; | |
625 | |
626 if (lenp == NULL) | |
627 { | |
628 len_loc = (int)STRLEN(str) + 1; | |
629 lenp = &len_loc; | |
630 } | |
631 | |
632 if (enc_codepage > 0) | |
633 { | |
634 /* We can do any CP### -> UTF-16 in one pass, and we can do it | |
635 * without iconv() (convert_* may need iconv). */ | |
5082
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
636 MultiByteToWideChar_alloc(enc_codepage, 0, (LPCSTR)str, *lenp, |
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
637 &ret, &length); |
4168 | 638 } |
639 else | |
640 { | |
641 /* Use "latin1" by default, we might be called before we have p_enc | |
642 * set up. Convert to utf-8 first, works better with iconv(). Does | |
643 * nothing if 'encoding' is "utf-8". */ | |
644 conv.vc_type = CONV_NONE; | |
645 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1", | |
646 (char_u *)"utf-8") == FAIL) | |
647 return NULL; | |
648 if (conv.vc_type != CONV_NONE) | |
649 { | |
650 str = allocbuf = string_convert(&conv, str, lenp); | |
651 if (str == NULL) | |
652 return NULL; | |
653 } | |
654 convert_setup(&conv, NULL, NULL); | |
655 | |
656 length = utf8_to_utf16(str, *lenp, NULL, NULL); | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
657 ret = ALLOC_MULT(WCHAR, length + 1); |
4168 | 658 if (ret != NULL) |
659 { | |
660 utf8_to_utf16(str, *lenp, (short_u *)ret, NULL); | |
661 ret[length] = 0; | |
662 } | |
663 | |
664 vim_free(allocbuf); | |
665 } | |
666 | |
667 *lenp = length; | |
668 return (short_u *)ret; | |
669 } | |
670 | |
671 /* | |
672 * Convert an UTF-16 string to 'encoding'. | |
673 * Input in "str" with length (counted in wide characters) "*lenp". When | |
674 * "lenp" is NULL, use wcslen(). | |
675 * Output is returned as an allocated string. If "*lenp" is not NULL it is | |
676 * set to the length of the result. | |
677 * Returns NULL when out of memory. | |
678 */ | |
679 char_u * | |
680 utf16_to_enc(short_u *str, int *lenp) | |
681 { | |
682 vimconv_T conv; | |
683 char_u *utf8_str = NULL, *enc_str = NULL; | |
684 int len_loc; | |
685 | |
686 if (lenp == NULL) | |
687 { | |
688 len_loc = (int)wcslen(str) + 1; | |
689 lenp = &len_loc; | |
690 } | |
691 | |
692 if (enc_codepage > 0) | |
693 { | |
694 /* We can do any UTF-16 -> CP### in one pass. */ | |
695 int length; | |
696 | |
697 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp, | |
698 (LPSTR *)&enc_str, &length, 0, 0); | |
699 *lenp = length; | |
700 return enc_str; | |
701 } | |
702 | |
703 /* Avoid allocating zero bytes, it generates an error message. */ | |
704 utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL)); | |
705 if (utf8_str != NULL) | |
706 { | |
707 *lenp = utf16_to_utf8(str, *lenp, utf8_str); | |
708 | |
709 /* We might be called before we have p_enc set up. */ | |
710 conv.vc_type = CONV_NONE; | |
711 convert_setup(&conv, (char_u *)"utf-8", | |
712 p_enc? p_enc: (char_u *)"latin1"); | |
713 if (conv.vc_type == CONV_NONE) | |
714 { | |
715 /* p_enc is utf-8, so we're done. */ | |
716 enc_str = utf8_str; | |
717 } | |
718 else | |
719 { | |
720 enc_str = string_convert(&conv, utf8_str, lenp); | |
721 vim_free(utf8_str); | |
722 } | |
723 | |
724 convert_setup(&conv, NULL, NULL); | |
725 } | |
726 | |
727 return enc_str; | |
728 } | |
729 | |
730 /* | |
731 * Convert from the active codepage to 'encoding'. | |
732 * Input is "str[str_size]". | |
733 * The result is in allocated memory: "out[outlen]". With terminating NUL. | |
734 */ | |
735 void | |
7837
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
736 acp_to_enc( |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
737 char_u *str, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
738 int str_size, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
739 char_u **out, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
740 int *outlen) |
4168 | 741 |
742 { | |
743 LPWSTR widestr; | |
744 | |
5082
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
745 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)str, str_size, |
7b20dc804164
updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents:
4168
diff
changeset
|
746 &widestr, outlen); |
4168 | 747 if (widestr != NULL) |
748 { | |
749 ++*outlen; /* Include the 0 after the string */ | |
750 *out = utf16_to_enc((short_u *)widestr, outlen); | |
751 vim_free(widestr); | |
752 } | |
753 } | |
5714 | 754 |
755 /* | |
756 * Convert from 'encoding' to the active codepage. | |
757 * Input is "str[str_size]". | |
758 * The result is in allocated memory: "out[outlen]". With terminating NUL. | |
759 */ | |
760 void | |
7837
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
761 enc_to_acp( |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
762 char_u *str, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
763 int str_size, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
764 char_u **out, |
33ba2adb6065
commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents:
5714
diff
changeset
|
765 int *outlen) |
5714 | 766 |
767 { | |
768 LPWSTR widestr; | |
769 int len = str_size; | |
770 | |
771 widestr = (WCHAR *)enc_to_utf16(str, &len); | |
772 if (widestr != NULL) | |
773 { | |
774 WideCharToMultiByte_alloc(GetACP(), 0, widestr, len, | |
775 (LPSTR *)out, outlen, 0, 0); | |
776 vim_free(widestr); | |
777 } | |
778 } |