annotate src/winclip.c @ 36081:f36c3455e686 v9.1.0709

patch 9.1.0709: GUIEnter event not found in Turkish locale Commit: https://github.com/vim/vim/commit/84e3175c4e70486b52e15cc6c40095fa311c1107 Author: Christian Brabandt <cb@256bit.org> Date: Mon Sep 2 09:59:18 2024 +0200 patch 9.1.0709: GUIEnter event not found in Turkish locale Problem: GUIEnter not found in Turkish locale (James McCoy, after v9.1.0256, the issue was there before, but v9.1.0256 made it more apparent) Solution: explicitly compare autocommand events by ASCII value and ignoring locale, because according to the documentation, events are case insensitive (:h autocommand-events) fixes: #15574 closes: #15603 Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Mon, 02 Sep 2024 10:15:03 +0200
parents 1009c33499e7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 8140
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
4168
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 *
10783
04eb70c77cf4 patch 8.0.0281: some files are still using ARGSUSED instead of UNUSED
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
13 * Routines for Win32 clipboard handling.
4168
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 #include "vim.h"
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 * 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
21 * posix environment.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 #ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 # define WIN32_LEAN_AND_MEAN
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 # include <windows.h>
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 # include "winclip.pro"
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 #endif
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 * 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
31 * errors disappear. They do not need to be correct.
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 PROTO
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 #define WINAPI
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 #define WINBASEAPI
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 typedef int DWORD;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 typedef int LPBOOL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 typedef int LPCSTR;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 typedef int LPCWSTR;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 typedef int LPSTR;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 typedef int LPWSTR;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42 typedef int UINT;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 #endif
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46 * Convert an UTF-8 string to UTF-16.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 * "instr[inlen]" is the input. "inlen" is in bytes.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 * 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
49 * Otherwise "outstr" must be a buffer of sufficient size.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50 * Returns the number of UTF-16 words produced.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 int
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53 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
54 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 int outlen = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 char_u *p = instr;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 int todo = inlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 int l;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 int ch;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 while (todo > 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
63 // Only convert if we have a complete sequence.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 l = utf_ptr2len_len(p, todo);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65 if (l > todo)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
67 // Return length of incomplete sequence.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68 if (unconvlenp != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 *unconvlenp = todo;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 break;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 }
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 ch = utf_ptr2char(p);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 if (ch >= 0x10000)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
76 // non-BMP character, encoding with surrogate pairs
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 ++outlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 if (outstr != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 *outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 *outstr++ = 0xDC00 | (ch & 0x3FF);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 }
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 else if (outstr != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 *outstr++ = ch;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 ++outlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 p += l;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 todo -= l;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 return outlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93
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 * Convert an UTF-16 string to UTF-8.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 * 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
97 * 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
98 * Otherwise "outstr" must be a buffer of sufficient size.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 * Return the number of bytes produced.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 int
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 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
103 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 int outlen = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 int todo = inlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 short_u *p = instr;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 int l;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 int ch, ch2;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 while (todo > 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 ch = *p;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
115 // surrogate pairs handling
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 ch2 = p[1];
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 ++p;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 --todo;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122 }
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 if (outstr != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 l = utf_char2bytes(ch, outstr);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 outstr += l;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 else
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 l = utf_char2len(ch);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 ++p;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 outlen += l;
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 return outlen;
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
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
139 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 * Call MultiByteToWideChar() and allocate memory for the result.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141 * 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
142 * "outlen" is in words.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
144 void
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
145 MultiByteToWideChar_alloc(UINT cp, DWORD flags,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
146 LPCSTR in, int inlen,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
147 LPWSTR *out, int *outlen)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
148 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
149 *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
26771
fc859aea8cec patch 8.2.3914: various spelling mistakes in comments
Bram Moolenaar <Bram@vim.org>
parents: 20976
diff changeset
150 // Add one word to avoid a zero-length alloc().
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16768
diff changeset
151 *out = ALLOC_MULT(WCHAR, *outlen + 1);
31827
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
152 if (*out == NULL)
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
153 return;
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
154
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
155 MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
156 (*out)[*outlen] = 0;
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
158
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
159 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
160 * Call WideCharToMultiByte() and allocate memory for the result.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
161 * 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
162 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
163 void
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
164 WideCharToMultiByte_alloc(UINT cp, DWORD flags,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
165 LPCWSTR in, int inlen,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
166 LPSTR *out, int *outlen,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
167 LPCSTR def, LPBOOL useddef)
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 *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
26771
fc859aea8cec patch 8.2.3914: various spelling mistakes in comments
Bram Moolenaar <Bram@vim.org>
parents: 20976
diff changeset
170 // Add one byte to avoid a zero-length alloc().
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16768
diff changeset
171 *out = alloc(*outlen + 1);
31827
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
172 if (*out == NULL)
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
173 return;
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
174 WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
175 (*out)[*outlen] = 0;
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
176 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
177
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
178
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
179 #ifdef FEAT_CLIPBOARD
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 * 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
182 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
183
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
184 void
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
185 win_clip_init(void)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
186 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
187 clip_init(TRUE);
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 * 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
191 * or rectangular block. Only useful for copying between two Vims.
17063
3147c7c2e86b patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
192 * "Clipboard_T" was used for previous versions, using the first
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
193 * character to specify MCHAR, MLINE or MBLOCK.
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 clip_star.format = RegisterClipboardFormat("VimClipboard2");
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
196 clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
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
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
199 // Type used for the clipboard type of Vim's data.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200 typedef struct
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
202 int type; // MCHAR, MBLOCK or MLINE
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
203 int txtlen; // length of CF_TEXT in bytes
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
204 int ucslen; // length of CF_UNICODETEXT in words
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
205 int rawlen; // length of clip_star.format_raw, including encoding,
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
206 // excluding terminating NUL
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
207 } VimClipType_t;
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 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
210 * 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
211 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
212 int
17063
3147c7c2e86b patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
213 clip_mch_own_selection(Clipboard_T *cbd UNUSED)
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 {
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 * 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
217 * 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
218 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 return FAIL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
220 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
221
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 * Make vim NOT the owner of the current selection.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
224 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 void
17063
3147c7c2e86b patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
226 clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
227 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
228 // Nothing needs to be done here
4168
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 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
232 * 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
233 * Return the allocated result and the size in "*size".
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234 * Returns NULL when out of memory.
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 static char_u *
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
237 crnl_to_nl(const char_u *str, int *size)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
238 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
239 int pos = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 int str_len = *size;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
241 char_u *ret;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
242 char_u *retp;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
243
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
244 // Avoid allocating zero bytes, it generates an error message.
16768
695d9ef00b03 patch 8.1.1386: unessesary type casts for lalloc()
Bram Moolenaar <Bram@vim.org>
parents: 16764
diff changeset
245 ret = alloc(str_len == 0 ? 1 : str_len);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
246 if (ret != NULL)
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 retp = ret;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
249 for (pos = 0; pos < str_len; ++pos)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
250 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
251 if (str[pos] == '\r' && str[pos + 1] == '\n')
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
252 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
253 ++pos;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
254 --(*size);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
255 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
256 *retp++ = str[pos];
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
257 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
258 }
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 return ret;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
261 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
262
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 * Wait for another process to Close the Clipboard.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
265 * Returns TRUE for success.
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 static int
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
268 vim_open_clipboard(void)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
269 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
270 int delay = 10;
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 while (!OpenClipboard(NULL))
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 if (delay > 500)
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
275 return FALSE; // waited too long, give up
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
276 Sleep(delay);
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
277 delay *= 2; // wait for 10, 20, 40, 80, etc. msec
4168
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 return TRUE;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
280 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
281
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 * 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
284 *
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
285 * 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
286 * 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
287 * so its handle = its pointer.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
288 * 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
289 * 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
290 * 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
291 * <VN>
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
292 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
293 void
17063
3147c7c2e86b patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
294 clip_mch_request_selection(Clipboard_T *cbd)
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
295 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
296 VimClipType_t metadata = { -1, -1, -1, -1 };
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
297 HGLOBAL hMem = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
298 char_u *str = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
299 char_u *to_free = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
300 HGLOBAL rawh = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
301 int str_size = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
302 int maxlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
303 size_t n;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
304
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
305 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
306 * 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
307 * 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
308 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
309 if (!vim_open_clipboard())
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
310 return;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
311
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
312 // Check for vim's own clipboard format first. This only gets the type of
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
313 // the data, still need to use CF_UNICODETEXT or CF_TEXT for the text.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
314 if (IsClipboardFormatAvailable(cbd->format))
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
315 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
316 VimClipType_t *meta_p;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
317 HGLOBAL meta_h;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
318
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
319 // We have metadata on the clipboard; try to get it.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
320 if ((meta_h = GetClipboardData(cbd->format)) != NULL
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
321 && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
322 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
323 // The size of "VimClipType_t" changed, "rawlen" was added later.
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
324 // Only copy what is available for backwards compatibility.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
325 n = sizeof(VimClipType_t);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
326 if (GlobalSize(meta_h) < n)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
327 n = GlobalSize(meta_h);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
328 memcpy(&metadata, meta_p, n);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
329 GlobalUnlock(meta_h);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
330 }
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
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
333 // Check for Vim's raw clipboard format first. This is used without
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
334 // conversion, but only if 'encoding' matches.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
335 if (IsClipboardFormatAvailable(cbd->format_raw)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
336 && metadata.rawlen > (int)STRLEN(p_enc))
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
337 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
338 // We have raw data on the clipboard; try to get it.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
339 if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
340 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
341 char_u *rawp;
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 rawp = (char_u *)GlobalLock(rawh);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
344 if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
345 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
346 n = STRLEN(p_enc) + 1;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
347 str = rawp + n;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
348 str_size = (int)(metadata.rawlen - n);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
349 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
350 else
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 GlobalUnlock(rawh);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
353 rawh = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
354 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
355 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
356 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
357 if (str == NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
358 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
359 // Try to get the clipboard in Unicode if it's not an empty string.
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
360 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
361 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
362 HGLOBAL hMemW;
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
363
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
364 if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
365 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
366 WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
367
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
368 // Use the length of our metadata if possible, but limit it to
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
369 // the GlobalSize() for safety.
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
370 maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR));
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
371 if (metadata.ucslen >= 0)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
372 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
373 if (metadata.ucslen > maxlen)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
374 str_size = maxlen;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
375 else
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
376 str_size = metadata.ucslen;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
377 }
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
378 else
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
379 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
380 for (str_size = 0; str_size < maxlen; ++str_size)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
381 if (hMemWstr[str_size] == NUL)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
382 break;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
383 }
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
384 to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size);
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
385 GlobalUnlock(hMemW);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
386 }
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
387 }
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
388 // Get the clipboard in the Active codepage.
15957
209f65bb155f patch 8.1.0984: unnecessary #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15868
diff changeset
389 else if (IsClipboardFormatAvailable(CF_TEXT))
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
390 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
391 if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
392 {
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
393 str = (char_u *)GlobalLock(hMem);
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
394
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
395 // The length is either what our metadata says or the strlen().
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
396 // But limit it to the GlobalSize() for safety.
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
397 maxlen = (int)GlobalSize(hMem);
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
398 if (metadata.txtlen >= 0)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
399 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
400 if (metadata.txtlen > maxlen)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
401 str_size = maxlen;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
402 else
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
403 str_size = metadata.txtlen;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
404 }
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
405 else
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
406 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
407 for (str_size = 0; str_size < maxlen; ++str_size)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
408 if (str[str_size] == NUL)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
409 break;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
410 }
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
411
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
412 // The text is in the active codepage. Convert to
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
413 // 'encoding', going through UTF-16.
15605
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
414 acp_to_enc(str, str_size, &to_free, &maxlen);
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
415 if (to_free != NULL)
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
416 {
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
417 str_size = maxlen;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
418 str = to_free;
62b3805506b3 patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 10783
diff changeset
419 }
4168
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 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
422 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
423
20976
599b29b6e540 patch 8.2.1039: cannot put NUL byte on clipboard
Bram Moolenaar <Bram@vim.org>
parents: 19380
diff changeset
424 if (str != NULL && metadata.txtlen != 0)
4168
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 char_u *temp_clipboard;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
427
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
428 // If the type is not known detect it.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
429 if (metadata.type == -1)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
430 metadata.type = MAUTO;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
431
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
432 // Translate <CR><NL> into <NL>.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
433 temp_clipboard = crnl_to_nl(str, &str_size);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
434 if (temp_clipboard != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
435 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
436 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
437 vim_free(temp_clipboard);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
438 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
439 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
440
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
441 // unlock the global object
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
442 if (hMem != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
443 GlobalUnlock(hMem);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
444 if (rawh != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
445 GlobalUnlock(rawh);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
446 CloseClipboard();
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
447 vim_free(to_free);
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 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
451 * Send the current selection to the clipboard.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
452 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
453 void
17063
3147c7c2e86b patch 8.1.1531: clipboard type name is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
454 clip_mch_set_selection(Clipboard_T *cbd)
4168
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 *str = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
457 VimClipType_t metadata;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
458 long_u txtlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
459 HGLOBAL hMemRaw = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
460 HGLOBAL hMem = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
461 HGLOBAL hMemVim = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
462 HGLOBAL hMemW = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
463
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
464 // If the '*' register isn't already filled in, fill it in now
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
465 cbd->owned = TRUE;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
466 clip_get_selection(cbd);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
467 cbd->owned = FALSE;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
468
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
469 // Get the text to be put on the clipboard, with CR-LF.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
470 metadata.type = clip_convert_selection(&str, &txtlen, cbd);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
471 if (metadata.type < 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
472 return;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
473 metadata.txtlen = (int)txtlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
474 metadata.ucslen = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
475 metadata.rawlen = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
476
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
477 // Always set the raw bytes: 'encoding', NUL and the text. This is used
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
478 // when copy/paste from/to Vim with the same 'encoding', so that illegal
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
479 // bytes can also be copied and no conversion is needed.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
480 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
481 LPSTR lpszMemRaw;
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 metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
484 hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
485 metadata.rawlen + 1);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
486 lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
487 if (lpszMemRaw != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
488 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
489 STRCPY(lpszMemRaw, p_enc);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
490 memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
491 GlobalUnlock(hMemRaw);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
492 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
493 else
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
494 metadata.rawlen = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
495 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
496
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
497 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
498 WCHAR *out;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
499 int len = metadata.txtlen;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
500
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
501 // Convert the text to UTF-16. This is put on the clipboard as
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
502 // CF_UNICODETEXT.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
503 out = (WCHAR *)enc_to_utf16(str, &len);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
504 if (out != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
505 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
506 WCHAR *lpszMemW;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
507
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
508 // Convert the text for CF_TEXT to Active codepage. Otherwise it's
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
509 // p_enc, which has no relation to the Active codepage.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
510 metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
511 NULL, 0, 0, 0);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
512 vim_free(str);
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16768
diff changeset
513 str = alloc(metadata.txtlen == 0 ? 1 : metadata.txtlen);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
514 if (str == NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
515 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
516 vim_free(out);
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
517 return; // out of memory
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
518 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
519 WideCharToMultiByte(GetACP(), 0, out, len,
5082
7b20dc804164 updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents: 4168
diff changeset
520 (LPSTR)str, metadata.txtlen, 0, 0);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
521
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
522 // Allocate memory for the UTF-16 text, add one NUL word to
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
523 // terminate the string.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
524 hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
525 (len + 1) * sizeof(WCHAR));
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
526 lpszMemW = (WCHAR *)GlobalLock(hMemW);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
527 if (lpszMemW != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
528 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
529 memcpy(lpszMemW, out, len * sizeof(WCHAR));
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
530 lpszMemW[len] = NUL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
531 GlobalUnlock(hMemW);
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 vim_free(out);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
534 metadata.ucslen = len;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
535 }
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
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
538 // Allocate memory for the text, add one NUL byte to terminate the string.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
539 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
540 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
541 LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
542
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
543 if (lpszMem)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
544 {
20976
599b29b6e540 patch 8.2.1039: cannot put NUL byte on clipboard
Bram Moolenaar <Bram@vim.org>
parents: 19380
diff changeset
545 mch_memmove((char_u *)lpszMem, str, metadata.txtlen);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
546 GlobalUnlock(hMem);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
547 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
548 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
549
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
550 // Set up metadata:
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
551 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
552 VimClipType_t *lpszMemVim = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
553
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
554 hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
555 sizeof(VimClipType_t));
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
556 lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
557 memcpy(lpszMemVim, &metadata, sizeof(metadata));
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
558 GlobalUnlock(hMemVim);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
559 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
560
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 * 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
563 * 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
564 *
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
565 * Don't pass GetActiveWindow() as an argument to OpenClipboard()
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
566 * 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
567 * reason - webb.
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 if (vim_open_clipboard())
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
570 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
571 if (EmptyClipboard())
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 SetClipboardData(cbd->format, hMemVim);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
574 hMemVim = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
575 if (hMemW != NULL)
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 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
578 hMemW = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
579 }
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
580 // Always use CF_TEXT. On Win98 Notepad won't obtain the
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
581 // CF_UNICODETEXT text, only CF_TEXT.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
582 SetClipboardData(CF_TEXT, hMem);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
583 hMem = 0;
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 CloseClipboard();
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
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
588 vim_free(str);
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
589 // Free any allocations we didn't give to the clipboard:
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
590 if (hMemRaw)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
591 GlobalFree(hMemRaw);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
592 if (hMem)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
593 GlobalFree(hMem);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
594 if (hMemW)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
595 GlobalFree(hMemW);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
596 if (hMemVim)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
597 GlobalFree(hMemVim);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
598 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
599
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
600 #endif // FEAT_CLIPBOARD
4168
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 * 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
604 * valid MS-Windows codepages or when iconv() is available.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
605 */
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 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
608 * Convert "str" from 'encoding' to UTF-16.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
609 * 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
610 * 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
611 * the result. A trailing NUL is always added.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
612 * Returns NULL when out of memory.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
613 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
614 short_u *
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
615 enc_to_utf16(char_u *str, int *lenp)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
616 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
617 vimconv_T conv;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
618 WCHAR *ret;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
619 char_u *allocbuf = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
620 int len_loc;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
621 int length;
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 if (lenp == NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
624 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
625 len_loc = (int)STRLEN(str) + 1;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
626 lenp = &len_loc;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
627 }
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 if (enc_codepage > 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
630 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
631 // We can do any CP### -> UTF-16 in one pass, and we can do it
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
632 // without iconv() (convert_* may need iconv).
5082
7b20dc804164 updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents: 4168
diff changeset
633 MultiByteToWideChar_alloc(enc_codepage, 0, (LPCSTR)str, *lenp,
7b20dc804164 updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents: 4168
diff changeset
634 &ret, &length);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
635 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
636 else
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
637 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
638 // Use "latin1" by default, we might be called before we have p_enc
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
639 // set up. Convert to utf-8 first, works better with iconv(). Does
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
640 // nothing if 'encoding' is "utf-8".
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
641 conv.vc_type = CONV_NONE;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
642 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
643 (char_u *)"utf-8") == FAIL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
644 return NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
645 if (conv.vc_type != CONV_NONE)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
646 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
647 str = allocbuf = string_convert(&conv, str, lenp);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
648 if (str == NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
649 return NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
650 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
651 convert_setup(&conv, NULL, NULL);
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 length = utf8_to_utf16(str, *lenp, NULL, NULL);
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16768
diff changeset
654 ret = ALLOC_MULT(WCHAR, length + 1);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
655 if (ret != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
656 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
657 utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
658 ret[length] = 0;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
659 }
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 vim_free(allocbuf);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
662 }
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 *lenp = length;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
665 return (short_u *)ret;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
666 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
667
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
668 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
669 * Convert an UTF-16 string to 'encoding'.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
670 * 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
671 * "lenp" is NULL, use wcslen().
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
672 * 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
673 * set to the length of the result.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
674 * Returns NULL when out of memory.
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 char_u *
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
677 utf16_to_enc(short_u *str, int *lenp)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
678 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
679 vimconv_T conv;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
680 char_u *utf8_str = NULL, *enc_str = NULL;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
681 int len_loc;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
682
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
683 if (lenp == NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
684 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
685 len_loc = (int)wcslen(str) + 1;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
686 lenp = &len_loc;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
687 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
688
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
689 if (enc_codepage > 0)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
690 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
691 // We can do any UTF-16 -> CP### in one pass.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
692 int length;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
693
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
694 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
695 (LPSTR *)&enc_str, &length, 0, 0);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
696 *lenp = length;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
697 return enc_str;
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
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
700 // Avoid allocating zero bytes, it generates an error message.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
701 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
702 if (utf8_str != NULL)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
703 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
704 *lenp = utf16_to_utf8(str, *lenp, utf8_str);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
705
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
706 // We might be called before we have p_enc set up.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
707 conv.vc_type = CONV_NONE;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
708 convert_setup(&conv, (char_u *)"utf-8",
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
709 p_enc? p_enc: (char_u *)"latin1");
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
710 if (conv.vc_type == CONV_NONE)
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
711 {
18816
15539899a112 patch 8.1.2396: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17063
diff changeset
712 // p_enc is utf-8, so we're done.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
713 enc_str = utf8_str;
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 else
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
716 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
717 enc_str = string_convert(&conv, utf8_str, lenp);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
718 vim_free(utf8_str);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
719 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
720
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
721 convert_setup(&conv, NULL, NULL);
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
722 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
723
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
724 return enc_str;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
725 }
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
726
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
727 /*
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
728 * Convert from the active codepage to 'encoding'.
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
729 * Input is "str[str_size]".
31140
a52697bcffa6 patch 9.0.0904: various comment and indent flaws
Bram Moolenaar <Bram@vim.org>
parents: 26771
diff changeset
730 * The result is in allocated memory: "out[outlen]". "outlen" includes the
a52697bcffa6 patch 9.0.0904: various comment and indent flaws
Bram Moolenaar <Bram@vim.org>
parents: 26771
diff changeset
731 * terminating NUL.
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
732 */
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
733 void
7837
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
734 acp_to_enc(
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
735 char_u *str,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
736 int str_size,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
737 char_u **out,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
738 int *outlen)
4168
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 {
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
741 LPWSTR widestr;
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
742
5082
7b20dc804164 updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents: 4168
diff changeset
743 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)str, str_size,
7b20dc804164 updated for version 7.3.1284
Bram Moolenaar <bram@vim.org>
parents: 4168
diff changeset
744 &widestr, outlen);
31827
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
745 if (widestr == NULL)
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
746 return;
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
747 ++*outlen; // Include the 0 after the string
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
748 *out = utf16_to_enc((short_u *)widestr, outlen);
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
749 vim_free(widestr);
4168
ff193256398a updated for version 7.3.836
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
750 }
5714
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
751
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
752 /*
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
753 * Convert from 'encoding' to the active codepage.
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
754 * Input is "str[str_size]".
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
755 * The result is in allocated memory: "out[outlen]". With terminating NUL.
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
756 */
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
757 void
7837
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
758 enc_to_acp(
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
759 char_u *str,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
760 int str_size,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
761 char_u **out,
33ba2adb6065 commit https://github.com/vim/vim/commit/b638a7be952544ceb03052c25b84224577a6494b
Christian Brabandt <cb@256bit.org>
parents: 5714
diff changeset
762 int *outlen)
5714
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
763
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
764 {
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
765 LPWSTR widestr;
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
766 int len = str_size;
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
767
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
768 widestr = (WCHAR *)enc_to_utf16(str, &len);
31827
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
769 if (widestr == NULL)
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
770 return;
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
771 WideCharToMultiByte_alloc(GetACP(), 0, widestr, len,
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
772 (LPSTR *)out, outlen, 0, 0);
1009c33499e7 patch 9.0.1246: code is indented more than necessary
Bram Moolenaar <Bram@vim.org>
parents: 31140
diff changeset
773 vim_free(widestr);
5714
22d7af9ff3e5 updated for version 7.4.202
Bram Moolenaar <bram@vim.org>
parents: 5082
diff changeset
774 }