annotate src/os_mac_conv.c @ 2309:543ea69d037f vim73

Add clipboard support in Mac console. (Bjorn Winckler)
author Bram Moolenaar <bram@vim.org>
date Wed, 14 Jul 2010 13:58:07 +0200
parents 82b5078be2dd
children acda456c788a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
2 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
4 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
8 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
9 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
10 * os_mac_conv.c: Code specifically for Mac string conversions.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
11 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
12 * This code has been put in a separate file to avoid the conflicts that are
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
13 * caused by including both the X11 and Carbon header files.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
14 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
15
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
16 #define NO_X11_INCLUDES
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
17 #include "vim.h"
2309
543ea69d037f Add clipboard support in Mac console. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents: 1621
diff changeset
18 #ifndef FEAT_GUI_MAC
543ea69d037f Add clipboard support in Mac console. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents: 1621
diff changeset
19 # include <CoreServices/CoreServices.h>
543ea69d037f Add clipboard support in Mac console. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents: 1621
diff changeset
20 #endif
543ea69d037f Add clipboard support in Mac console. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents: 1621
diff changeset
21
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
22
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
23 #if defined(MACOS_CONVERT) || defined(PROTO)
2309
543ea69d037f Add clipboard support in Mac console. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents: 1621
diff changeset
24
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
25 # ifdef PROTO
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
26 /* A few dummy types to be able to generate function prototypes. */
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
27 typedef int UniChar;
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
28 typedef int *TECObjectRef;
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
29 typedef int CFStringRef;
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
30 # endif
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
31
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
32 static char_u *mac_utf16_to_utf8 __ARGS((UniChar *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
33 static UniChar *mac_utf8_to_utf16 __ARGS((char_u *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
34
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
35 /* Converter for composing decomposed HFS+ file paths */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
36 static TECObjectRef gPathConverter;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
37 /* Converter used by mac_utf16_to_utf8 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
38 static TECObjectRef gUTF16ToUTF8Converter;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
39
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
40 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
41 * A Mac version of string_convert_ext() for special cases.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
42 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
43 char_u *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
44 mac_string_convert(ptr, len, lenp, fail_on_error, from_enc, to_enc, unconvlenp)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
45 char_u *ptr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
46 int len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
47 int *lenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
48 int fail_on_error;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
49 int from_enc;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
50 int to_enc;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
51 int *unconvlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
52 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
53 char_u *retval, *d;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
54 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
55 int buflen, in, out, l, i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
56 CFStringEncoding from;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
57 CFStringEncoding to;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
58
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
59 switch (from_enc)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
60 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
61 case 'l': from = kCFStringEncodingISOLatin1; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
62 case 'm': from = kCFStringEncodingMacRoman; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
63 case 'u': from = kCFStringEncodingUTF8; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
64 default: return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
65 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
66 switch (to_enc)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
67 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
68 case 'l': to = kCFStringEncodingISOLatin1; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
69 case 'm': to = kCFStringEncodingMacRoman; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
70 case 'u': to = kCFStringEncodingUTF8; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
71 default: return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
72 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
73
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
74 if (unconvlenp != NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
75 *unconvlenp = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
76 cfstr = CFStringCreateWithBytes(NULL, ptr, len, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
77
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
78 if(cfstr == NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
79 fprintf(stderr, "Encoding failed\n");
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
80 /* When conversion failed, try excluding bytes from the end, helps when
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
81 * there is an incomplete byte sequence. Only do up to 6 bytes to avoid
1212
e085b0f7b036 updated for version 7.1b
vimboss
parents: 766
diff changeset
82 * looping a long time when there really is something unconvertible. */
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
83 while (cfstr == NULL && unconvlenp != NULL && len > 1 && *unconvlenp < 6)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
84 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
85 --len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
86 ++*unconvlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
87 cfstr = CFStringCreateWithBytes(NULL, ptr, len, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
88 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
89 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
90 return NULL;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
91
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
92 if (to == kCFStringEncodingUTF8)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
93 buflen = len * 6 + 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
94 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
95 buflen = len + 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
96 retval = alloc(buflen);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
97 if (retval == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
98 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
99 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
100 return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
101 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
102
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
103 #if 0
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
104 CFRange convertRange = CFRangeMake(0, CFStringGetLength(cfstr));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
105 /* Determine output buffer size */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
106 CFStringGetBytes(cfstr, convertRange, to, NULL, FALSE, NULL, 0, (CFIndex *)&buflen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
107 retval = (buflen > 0) ? alloc(buflen) : NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
108 if (retval == NULL) {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
109 CFRelease(cfstr);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
110 return NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
111 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
112
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
113 if (lenp)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
114 *lenp = buflen / sizeof(char_u);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
115
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
116 if (!CFStringGetBytes(cfstr, convertRange, to, NULL, FALSE, retval, buflen, NULL))
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
117 #endif
501
ce2181d14aa0 updated for version 7.0139
vimboss
parents: 474
diff changeset
118 if (!CFStringGetCString(cfstr, (char *)retval, buflen, to))
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
119 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
120 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
121 if (fail_on_error)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
122 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
123 vim_free(retval);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
124 return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
125 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
126
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
127 fprintf(stderr, "Trying char-by-char conversion...\n");
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
128 /* conversion failed for the whole string, but maybe it will work
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
129 * for each character */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
130 for (d = retval, in = 0, out = 0; in < len && out < buflen - 1;)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
131 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
132 if (from == kCFStringEncodingUTF8)
474
a5fcf36ef512 updated for version 7.0127
vimboss
parents: 179
diff changeset
133 l = utf_ptr2len(ptr + in);
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
134 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
135 l = 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
136 cfstr = CFStringCreateWithBytes(NULL, ptr + in, l, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
137 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
138 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
139 *d++ = '?';
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
140 out++;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
141 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
142 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
143 {
501
ce2181d14aa0 updated for version 7.0139
vimboss
parents: 474
diff changeset
144 if (!CFStringGetCString(cfstr, (char *)d, buflen - out, to))
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
145 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
146 *d++ = '?';
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
147 out++;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
148 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
149 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
150 {
501
ce2181d14aa0 updated for version 7.0139
vimboss
parents: 474
diff changeset
151 i = STRLEN(d);
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
152 d += i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
153 out += i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
154 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
155 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
156 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
157 in += l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
158 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
159 *d = NUL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
160 if (lenp != NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
161 *lenp = out;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
162 return retval;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
163 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
164 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
165 if (lenp != NULL)
501
ce2181d14aa0 updated for version 7.0139
vimboss
parents: 474
diff changeset
166 *lenp = STRLEN(retval);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
167
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
168 return retval;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
169 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
170
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
171 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
172 * Conversion from Apple MacRoman char encoding to UTF-8 or latin1, using
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
173 * standard Carbon framework.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
174 * Input: "ptr[*sizep]".
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
175 * "real_size" is the size of the buffer that "ptr" points to.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
176 * output is in-place, "sizep" is adjusted.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
177 * Returns OK or FAIL.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
178 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
179 int
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
180 macroman2enc(ptr, sizep, real_size)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
181 char_u *ptr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
182 long *sizep;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
183 long real_size;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
184 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
185 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
186 CFRange r;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
187 CFIndex len = *sizep;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
188
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
189 /* MacRoman is an 8-bit encoding, no need to move bytes to
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
190 * conv_rest[]. */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
191 cfstr = CFStringCreateWithBytes(NULL, ptr, len,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
192 kCFStringEncodingMacRoman, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
193 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
194 * If there is a conversion error, try using another
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
195 * conversion.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
196 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
197 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
198 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
199
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
200 r.location = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
201 r.length = CFStringGetLength(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
202 if (r.length != CFStringGetBytes(cfstr, r,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
203 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
204 0, /* no lossy conversion */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
205 0, /* not external representation */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
206 ptr + *sizep, real_size - *sizep, &len))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
207 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
208 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
209 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
210 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
211 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
212 mch_memmove(ptr, ptr + *sizep, len);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
213 *sizep = len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
214
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
215 return OK;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
216 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
217
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
218 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
219 * Conversion from UTF-8 or latin1 to MacRoman.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
220 * Input: "from[fromlen]"
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
221 * Output: "to[maxtolen]" length in "*tolenp"
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
222 * Unconverted rest in rest[*restlenp].
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
223 * Returns OK or FAIL.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
224 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
225 int
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
226 enc2macroman(from, fromlen, to, tolenp, maxtolen, rest, restlenp)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
227 char_u *from;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
228 size_t fromlen;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
229 char_u *to;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
230 int *tolenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
231 int maxtolen;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
232 char_u *rest;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
233 int *restlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
234 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
235 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
236 CFRange r;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
237 CFIndex l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
238
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
239 *restlenp = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
240 cfstr = CFStringCreateWithBytes(NULL, from, fromlen,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
241 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
242 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
243 while (cfstr == NULL && *restlenp < 3 && fromlen > 1)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
244 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
245 rest[*restlenp++] = from[--fromlen];
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
246 cfstr = CFStringCreateWithBytes(NULL, from, fromlen,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
247 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
248 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
249 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
250 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
251 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
252
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
253 r.location = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
254 r.length = CFStringGetLength(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
255 if (r.length != CFStringGetBytes(cfstr, r,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
256 kCFStringEncodingMacRoman,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
257 0, /* no lossy conversion */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
258 0, /* not external representation (since vim
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
259 * handles this internally */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
260 to, maxtolen, &l))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
261 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
262 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
263 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
264 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
265 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
266 *tolenp = l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
267 return OK;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
268 }
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 18
diff changeset
269
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
270 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
271 * Initializes text converters
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
272 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
273 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
274 mac_conv_init()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
275 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
276 TextEncoding utf8_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
277 TextEncoding utf8_hfsplus_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
278 TextEncoding utf8_canon_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
279 TextEncoding utf16_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
280
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
281 utf8_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
282 kTextEncodingDefaultVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
283 utf8_hfsplus_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
284 kUnicodeHFSPlusCompVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
285 utf8_canon_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
286 kUnicodeCanonicalCompVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
287 utf16_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
288 kTextEncodingDefaultVariant, kUnicode16BitFormat);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
289
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
290 if (TECCreateConverter(&gPathConverter, utf8_encoding,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
291 utf8_hfsplus_encoding) != noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
292 gPathConverter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
293
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
294 if (TECCreateConverter(&gUTF16ToUTF8Converter, utf16_encoding,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
295 utf8_canon_encoding) != noErr)
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
296 {
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
297 /* On pre-10.3, Unicode normalization is not available so
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
298 * fall back to non-normalizing converter */
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
299 if (TECCreateConverter(&gUTF16ToUTF8Converter, utf16_encoding,
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
300 utf8_encoding) != noErr)
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
301 gUTF16ToUTF8Converter = NULL;
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
302 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
303 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
304
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
305 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
306 * Destroys text converters
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
307 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
308 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
309 mac_conv_cleanup()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
310 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
311 if (gUTF16ToUTF8Converter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
312 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
313 TECDisposeConverter(gUTF16ToUTF8Converter);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
314 gUTF16ToUTF8Converter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
315 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
316
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
317 if (gPathConverter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
318 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
319 TECDisposeConverter(gPathConverter);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
320 gPathConverter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
321 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
322 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
323
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
324 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
325 * Conversion from UTF-16 UniChars to 'encoding'
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
326 * The function signature uses the real type of UniChar (as typedef'ed in
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
327 * CFBase.h) to avoid clashes with X11 header files in the .pro file
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
328 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
329 char_u *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
330 mac_utf16_to_enc(from, fromLen, actualLen)
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
331 unsigned short *from;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
332 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
333 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
334 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
335 /* Following code borrows somewhat from os_mswin.c */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
336 vimconv_T conv;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
337 size_t utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
338 char_u *utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
339 char_u *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
340
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
341 /* Convert to utf-8 first, works better with iconv */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
342 utf8_len = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
343 utf8_str = mac_utf16_to_utf8(from, fromLen, &utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
344
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
345 if (utf8_str)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
346 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
347 /* We might be called before we have p_enc set up. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
348 conv.vc_type = CONV_NONE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
349
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
350 /* If encoding (p_enc) is any unicode, it is actually in utf-8 (vim
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
351 * internal unicode is always utf-8) so don't convert in such cases */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
352
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
353 if ((enc_canon_props(p_enc) & ENC_UNICODE) == 0)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
354 convert_setup(&conv, (char_u *)"utf-8",
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
355 p_enc? p_enc: (char_u *)"macroman");
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
356 if (conv.vc_type == CONV_NONE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
357 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
358 /* p_enc is utf-8, so we're done. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
359 result = utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
360 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
361 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
362 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
363 result = string_convert(&conv, utf8_str, (int *)&utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
364 vim_free(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
365 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
366
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
367 convert_setup(&conv, NULL, NULL);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
368
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
369 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
370 *actualLen = utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
371 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
372 else if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
373 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
374
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
375 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
376 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
377
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
378 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
379 * Conversion from 'encoding' to UTF-16 UniChars
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
380 * The function return uses the real type of UniChar (as typedef'ed in
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
381 * CFBase.h) to avoid clashes with X11 header files in the .pro file
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
382 */
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
383 unsigned short *
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
384 mac_enc_to_utf16(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
385 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
386 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
387 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
388 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
389 /* Following code borrows somewhat from os_mswin.c */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
390 vimconv_T conv;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
391 size_t utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
392 char_u *utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
393 UniChar *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
394 Boolean should_free_utf8 = FALSE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
395
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
396 do
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
397 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
398 /* Use MacRoman by default, we might be called before we have p_enc
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
399 * set up. Convert to utf-8 first, works better with iconv(). Does
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
400 * nothing if 'encoding' is "utf-8". */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
401 conv.vc_type = CONV_NONE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
402 if ((enc_canon_props(p_enc) & ENC_UNICODE) == 0 &&
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
403 convert_setup(&conv, p_enc ? p_enc : (char_u *)"macroman",
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
404 (char_u *)"utf-8") == FAIL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
405 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
406
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
407 if (conv.vc_type != CONV_NONE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
408 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
409 utf8_len = fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
410 utf8_str = string_convert(&conv, from, (int *)&utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
411 should_free_utf8 = TRUE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
412 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
413 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
414 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
415 utf8_str = from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
416 utf8_len = fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
417 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
418
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
419 if (utf8_str == NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
420 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
421
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
422 convert_setup(&conv, NULL, NULL);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
423
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
424 result = mac_utf8_to_utf16(utf8_str, utf8_len, actualLen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
425
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
426 if (should_free_utf8)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
427 vim_free(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
428 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
429 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
430 while (0);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
431
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
432 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
433 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
434
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
435 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
436 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
437
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
438 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
439 * Converts from UTF-16 UniChars to CFString
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
440 * The void * return type is actually a CFStringRef
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
441 */
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
442 void *
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
443 mac_enc_to_cfstring(from, fromLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
444 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
445 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
446 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
447 UniChar *utf16_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
448 size_t utf16_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
449 CFStringRef result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
450
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
451 utf16_str = mac_enc_to_utf16(from, fromLen, &utf16_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
452 if (utf16_str)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
453 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
454 result = CFStringCreateWithCharacters(NULL, utf16_str, utf16_len/sizeof(UniChar));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
455 vim_free(utf16_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
456 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
457
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
458 return (void *)result;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
459 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
460
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
461 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
462 * Converts a decomposed HFS+ UTF-8 path to precomposed UTF-8
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
463 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
464 char_u *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
465 mac_precompose_path(decompPath, decompLen, precompLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
466 char_u *decompPath;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
467 size_t decompLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
468 size_t *precompLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
469 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
470 char_u *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
471 size_t actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
472
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
473 if (gPathConverter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
474 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
475 result = alloc(decompLen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
476 if (result)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
477 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
478 if (TECConvertText(gPathConverter, decompPath,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
479 decompLen, &decompLen, result,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
480 decompLen, &actualLen) != noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
481 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
482 vim_free(result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
483 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
484 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
485 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
486 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
487
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
488 if (precompLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
489 *precompLen = actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
490
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
491 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
492 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
493
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
494 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
495 * Converts from UTF-16 UniChars to precomposed UTF-8
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
496 */
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
497 static char_u *
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
498 mac_utf16_to_utf8(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
499 UniChar *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
500 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
501 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
502 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
503 ByteCount utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
504 ByteCount inputRead;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
505 char_u *result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
506
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
507 if (gUTF16ToUTF8Converter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
508 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
509 result = alloc(fromLen * 6 + 1);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
510 if (result && TECConvertText(gUTF16ToUTF8Converter, (ConstTextPtr)from,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
511 fromLen, &inputRead, result,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
512 (fromLen*6+1)*sizeof(char_u), &utf8_len) == noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
513 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
514 TECFlushText(gUTF16ToUTF8Converter, result, (fromLen*6+1)*sizeof(char_u), &inputRead);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
515 utf8_len += inputRead;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
516 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
517 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
518 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
519 vim_free(result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
520 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
521 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
522 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
523 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
524 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
525 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
526 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
527
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
528 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
529 *actualLen = result ? utf8_len : 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
530
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
531 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
532 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
533
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
534 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
535 * Converts from UTF-8 to UTF-16 UniChars
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
536 */
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
537 static UniChar *
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
538 mac_utf8_to_utf16(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
539 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
540 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
541 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
542 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
543 CFStringRef utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
544 CFRange convertRange;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
545 UniChar *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
546
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
547 utf8_str = CFStringCreateWithBytes(NULL, from, fromLen,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
548 kCFStringEncodingUTF8, FALSE);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
549
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
550 if (utf8_str == NULL) {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
551 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
552 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
553 return NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
554 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
555
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
556 convertRange = CFRangeMake(0, CFStringGetLength(utf8_str));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
557 result = (UniChar *)alloc(convertRange.length * sizeof(UniChar));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
558
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
559 CFStringGetCharacters(utf8_str, convertRange, result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
560
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
561 CFRelease(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
562
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
563 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
564 *actualLen = convertRange.length * sizeof(UniChar);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
565
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
566 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
567 }
1621
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
568
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
569 /*
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
570 * Sets LANG environment variable in Vim from Mac locale
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
571 */
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
572 void
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
573 mac_lang_init() {
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
574 if (mch_getenv((char_u *)"LANG") == NULL)
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
575 {
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
576 char buf[20];
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
577 if (LocaleRefGetPartString(NULL,
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
578 kLocaleLanguageMask | kLocaleLanguageVariantMask |
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
579 kLocaleRegionMask | kLocaleRegionVariantMask,
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
580 sizeof buf, buf) == noErr && *buf)
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
581 {
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
582 vim_setenv((char_u *)"LANG", (char_u *)buf);
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
583 # ifdef HAVE_LOCALE_H
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
584 setlocale(LC_ALL, "");
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
585 # endif
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
586 }
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
587 }
82b5078be2dd updated for version 7.2a
vimboss
parents: 1212
diff changeset
588 }
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 501
diff changeset
589 #endif /* MACOS_CONVERT */