annotate src/winclip.c @ 4613:219b2fcad60d v7.3.1054

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